https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43565

--- Comment #11 from Marc Nieper-Wißkirchen <m...@nieper-wisskirchen.de> ---
If ISO C allows such linkage to be created outside of the standard, a number of
other assumption would be violated as well:

In 6.2.4 (2) it says that "an object exists, has a constant address, and
retains its last-stored value throughout its lifetime."

Only "in the case of a volatile object, the last store need not be explicit in
the program" (footnote 34).

So what about making the external creation of linkage between two different
identifiers an error/undefined behavior if they are not declared volatile? 

Firstly, not marking them volatile already already causes gcc to make wrong
assumptions (because it does not know about external aliasing):

extern int p;
extern int q;

int foo ()
{
    p = 1;
    q = 2;
    return p;
}

This program compiles at "-O2" to:

foo:
        movl    $1, p(%rip)
        movl    $1, %eax
        movl    $2, q(%rip)
        ret

Secondly, not marking them volatile would then allow the compiler to assume
that the addresses of p and q are different (because aliasing the two
identifiers would cause undefined behavior).

Reply via email to