Bug 24097 - [3.4 Regression] x86_64 -fPIC code gen bug (extern/static linkage confusion)
Summary: [3.4 Regression] x86_64 -fPIC code gen bug (extern/static linkage confusion)
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.4.4
: P2 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
: 24106 26271 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-09-28 02:37 UTC by Mark Coiley
Modified: 2006-03-01 04:50 UTC (History)
2 users (show)

See Also:
Host:
Target: x86_64-pc-linux-gnu
Build:
Known to work: 3.3.3 4.0.0 4.1.0
Known to fail: 3.4.0 3.4.5
Last reconfirmed: 2006-01-07 04:19:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mark Coiley 2005-09-28 02:37:29 UTC
With this testcase:

int foo (int (*cb)(void)) { return (*cb)(); }
static int callback(void) { return 0; }

int main (void)
{
 int callback (void);
 return foo (callback);
}

On x86_64 with -fPIC, the address of callback as passed to foo is incorrect.

If callback isn't static, it works (through the GOT).  If the declaration of
callback is removed it works (computes the address as %rip minus something).  As
written, it does a failed combination of the two.  The result is that instead of
passing callback's address, it loads the first 8 bytes at callback's address and
passes those.  (It also erroneously claims callback is unused, but I'm guessing
that's a side-effect.)

The code is questionable, but my reading of the standard says it's legal and
that callback is still static after the (implicitly extern) declaration.

% gcc-3.4.4 -O2 -fPIC -Wall -save-temps -o bug bug.c 
bug.c:2: warning: 'callback' defined but not used

% gcc-3.4.4 -v
Reading specs from /usr/local/lib/gcc/x86_64-pc-linux-gnu/3.4.4/specs
Configured with: /tmp/src/gcc-3.4.4/configure --enable-languages=c,c++
--enable-version-specific-runtime-libs --disable-shared --enable-threads
--with-gnu-as --with-gnu-ld --with-pic --host=x86_64-pc-linux-gnu
Thread model: posix
gcc version 3.4.4

[I bootstrapped this 3.4.4; it also fails with stock 3.4.3 from RedHat.]
Comment 1 Andrew Pinski 2005-09-28 02:48:38 UTC
Confirmed, only a 3.4 regression.
Comment 2 Mark Coiley 2005-09-28 18:49:44 UTC
*** Bug 24106 has been marked as a duplicate of this bug. ***
Comment 3 Andrew Pinski 2006-02-14 00:17:54 UTC
*** Bug 26271 has been marked as a duplicate of this bug. ***
Comment 4 Andrew Pinski 2006-02-14 00:19:02 UTC
IIRC this was fixed by a latter rewrite of c-decl.c and most likely not going to be fixed for 3.4.x.
Comment 5 Gabriel Dos Reis 2006-03-01 04:50:52 UTC
Fixed in 4.0.0 and up.