Bug 14105 - GCC over-optimisating extern
Summary: GCC over-optimisating extern
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.3.2
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-02-11 05:47 UTC by Omar Kilani
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: i586-trustix-linux
Target: i586-trustix-linux
Build: i586-trustix-linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Omar Kilani 2004-02-11 05:47:57 UTC
Sample code (x.c):

int main(void) {
  int return_pcap_debug(void) {
    extern int pcap_debug;

    return pcap_debug;
  }

  return 0;
}

Compiled with (-Os, -O1, -O2 are all the same):

gcc x.c -o x

Produces: 

/tmp/ccJy8Ead.o(.text+0xa): In function `return_pcap_debug.0':
: undefined reference to `pcap_debug'
collect2: ld returned 1 exit status

But, when compiled with:

gcc -O3 x.c -o x

The code compiles.

This is an over-optimisation, and is pretty silly, especially since the returned
value in the inlined block is an extern.

If you link the code against something that does export pcap_debug, it links
properly.
Comment 1 Andrew Pinski 2004-02-11 05:56:04 UTC
This is not over optimizing as return_pcap_debug is an function inside a function which is a gcc 
extension but the function is not used as all (aka like a static funciton), so gcc is removing the function.  
Note that at -O2 (because of -funit-at-a-time) gcc 3.4.0 will remove the function also.