Bug 838348 - GCC optimises too aggressively with -fcommon and -O2
GCC optimises too aggressively with -fcommon and -O2
Status: CLOSED UPSTREAM
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
19
Unspecified Unspecified
unspecified Severity unspecified
: ---
: ---
Assigned To: Jakub Jelinek
Fedora Extras Quality Assurance
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2012-07-08 12:25 EDT by Ryan Lortie (desrt)
Modified: 2015-01-09 12:57 EST (History)
2 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2015-01-09 12:57:41 EST
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Ryan Lortie (desrt) 2012-07-08 12:25:01 EDT
I'm pretty sure this is an upstream GCC bug because it happens on Ubuntu as well, but GCC requests that bugs be filed against distributions, so I report it here...

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.7.0/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --disable-build-with-cxx --disable-build-poststage1-with-cxx --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.7.0 20120507 (Red Hat 4.7.0-5) (GCC) 


Consider the following three files:

==> a.c <==
__attribute__((visibility("hidden"))) const int x;

void b (int);

void a (void) { b (x); }

==> main.c <==
#include <stdio.h>

void b (int x) { printf ("x = %d\n", x); }

void a (void);

int main (void) { a (); return 0; }

==> x.c <==
__attribute__((visibility("hidden"))) const int x = 5;



This is more or less the classic case of failing to mark the declaration of a global variable in a header file as 'extern' leading to the classical unix "common symbol" treatment (which is questionable, but quite well-defined).

If you build this program, you get the expected output:

$ gcc -o x {a,x,main}.c
$ ./x
x = 5

Of course, this is depending on the common-symbol-combining functionality of the linker, as can be seen by using --warn-common:

$ gcc -o x {a,x,main}.c -Wl,--warn-common
/tmp/ccQm2lYI.o: warning: definition of `x' overriding common
/tmp/ccogwEdV.o: warning: common is here

We can explicitly request that this feature is disabled in GCC:

$ gcc -o x {a,x,main}.c -fno-common
/tmp/ccKhuZE1.o:(.rodata+0x0): multiple definition of `x'
/tmp/cc1cz0t6.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status

The problem comes when we compile with -O2 -fcommon (note: -fcommon is the default in F17 but I write it here for sake of being explicit):

$ gcc -o x {a,x,main}.c -O2 -fcommon
$ ./x
x = 0

The assembly code for a() looks like so:

a:
.LFB0:
        .cfi_startproc
        xorl    %edi, %edi
        jmp     b
        .cfi_endproc

We now have an incorrect output.  The reason is obvious.  GCC considers that it sees a constant global variable defined in a file with no value written to it.  Interposition is not possible (due to the symbol being marked as hidden).  Of course the value is zero, so we may as well just replace it with a constant zero ("xorl %edi, %edi").

This reasoning fails to consider the common symbol merging that the linker may very well do.  -fcommon was specified, after all, so this possibility should be expected.
Comment 1 Fedora End Of Life 2013-07-03 18:49:50 EDT
This message is a reminder that Fedora 17 is nearing its end of life.
Approximately 4 (four) weeks from now Fedora will stop maintaining
and issuing updates for Fedora 17. It is Fedora's policy to close all
bug reports from releases that are no longer maintained. At that time
this bug will be closed as WONTFIX if it remains open with a Fedora 
'version' of '17'.

Package Maintainer: If you wish for this bug to remain open because you
plan to fix it in a currently maintained version, simply change the 'version' 
to a later Fedora version prior to Fedora 17's end of life.

Bug Reporter:  Thank you for reporting this issue and we are sorry that 
we may not be able to fix it before Fedora 17 is end of life. If you 
would still like  to see this bug fixed and are able to reproduce it 
against a later version  of Fedora, you are encouraged  change the 
'version' to a later Fedora version prior to Fedora 17's end of life.

Although we aim to fix as many bugs as possible during every release's 
lifetime, sometimes those efforts are overtaken by events. Often a 
more recent Fedora release includes newer upstream software that fixes 
bugs or makes them obsolete.
Comment 2 Ryan Lortie (desrt) 2013-07-03 23:10:24 EDT
Problem still exists in F19 pre-release.
Comment 3 Fedora End Of Life 2015-01-09 12:14:40 EST
This message is a notice that Fedora 19 is now at end of life. Fedora 
has stopped maintaining and issuing updates for Fedora 19. It is 
Fedora's policy to close all bug reports from releases that are no 
longer maintained. Approximately 4 (four) weeks from now this bug will
be closed as EOL if it remains open with a Fedora 'version' of '19'.

Package Maintainer: If you wish for this bug to remain open because you
plan to fix it in a currently maintained version, simply change the 'version' 
to a later Fedora version.

Thank you for reporting this issue and we are sorry that we were not 
able to fix it before Fedora 19 is end of life. If you would still like 
to see this bug fixed and are able to reproduce it against a later version 
of Fedora, you are encouraged  change the 'version' to a later Fedora 
version prior this bug is closed as described in the policy above.

Although we aim to fix as many bugs as possible during every release's 
lifetime, sometimes those efforts are overtaken by events. Often a 
more recent Fedora release includes newer upstream software that fixes 
bugs or makes them obsolete.
Comment 4 Ryan Lortie (desrt) 2015-01-09 12:57:41 EST
I can confirm that this is fixed in Fedora 21.

gcc version 4.9.2 20141101 (Red Hat 4.9.2-1) (GCC)

Note You need to log in before you can comment on or make changes to this bug.