Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 151832 - [4.1/bad-code] gcc does not generate PIC with inline functions and -O0
Summary: [4.1/bad-code] gcc does not generate PIC with inline functions and -O0
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: AMD64 Linux
: High normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
: 152719 156082 163734 164767 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-10-18 07:28 UTC by David Turner
Modified: 2007-05-19 22:57 UTC (History)
7 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
pic.i (pic.i,87 bytes, text/plain)
2006-10-18 23:09 UTC, SpanKY
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Turner 2006-10-18 07:28:50 UTC
I'm trying to build hprof from http://download.savannah.nongnu.org/releases/pyprof/hprof-0.1.1.tar.gz

This works on my Debian x86 box, but not on my gentoo x86_64 box.  I have seen other reports of the same error for different software on gentoo, but no fixes:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/194965 and
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/195670

http://lists.ximian.com/pipermail/openoffice/2005-August/001104.html

http://lists.kde.org/?l=kde-devel&m=112898516904717&w=2

Notice that the error message exhorts us to use -fPIC, when we are already doing so.  

I have also submitted this bug to the hprof people in the hopes that they can help solve this -- but at this point, I expect that it is a gentoo bug.

novalis@novalis /tmp/hprof-0.1.1 $ python setup.py  build running build running build_py
running build_ext
building '_hotshot' extension
creating build/temp.linux-x86_64-2.4
creating build/temp.linux-x86_64-2.4/src
x86_64-pc-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -fPIC -I/usr/include/python2.4 -c src/_hotshot.c -o build/temp.linux-x86_64-2.4/src/_hotshot.o
x86_64-pc-linux-gnu-gcc -pthread -shared build/temp.linux-x86_64-2.4/src/_hotshot.o -o build/lib.linux-x86_64-2.4/_hotshot.so
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.1/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating a DT_TEXTREL in object.
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.1/../../../../x86_64-pc-linux-gnu/bin/ld: build/temp.linux-x86_64-2.4/src/_hotshot.o: relocation R_X86_64_PC32 against `get_tdelta_external' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.1/../../../../x86_64-pc-linux-gnu/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
error: command 'x86_64-pc-linux-gnu-gcc' failed with exit status 1
Comment 1 Jakub Moc (RETIRED) gentoo-dev 2006-10-18 07:32:11 UTC
We don't support stuff that's not in portage tree, report such things upstream.
Comment 2 David Turner 2006-10-18 07:44:07 UTC
Gentoo's version of binutils clearly has a bug -- it tells you to "recompile
with -fPIC" when -fPIC is already used.  The four specific examples I turned up are all demonstrations of the bug in binutils.  
Comment 3 Jakub Moc (RETIRED) gentoo-dev 2006-10-18 07:46:32 UTC
Sure, apparent from the wonderful proof that it compiles on x86... Definitely out binutils is broken... Please, review the following before bugging us again about broken upstream code:

http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3
Comment 4 Jakub Moc (RETIRED) gentoo-dev 2006-10-18 07:47:25 UTC
And also http://www.gentoo.org/proj/en/hardened/pic-internals.xml
Comment 5 David Turner 2006-10-18 08:02:33 UTC
OK, I've read all the docs and my situation doesn't fall under any of the listed cases.  Here is a very short test case -- note that removing "inline" makes the compile succeed:

x86_64-pc-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -fPIC -c foo.c -o foo.o

x86_64-pc-linux-gnu-gcc -pthread -shared foo.o -o foo.so

foo.c:
static inline int frobnicate(int x) {

  return x + 1;
}

int refrobnicate(int y) {
  void *frob = (void *)frobnicate;
  
  int (*frob_fn)(int) = frob;
  return frob_fn(y) + frob_fn(y);
  
}
Comment 6 Simon Stelling (RETIRED) gentoo-dev 2006-10-18 08:20:02 UTC
You can't declare a function as inline and then point at it, because it does not exist as an instance in the code, it's "copy-pasted" into the calling function. This is not a toolchain bug.
Comment 7 David Turner 2006-10-18 08:59:58 UTC
(In reply to comment #6)
> You can't declare a function as inline and then point at it, because it does
> not exist as an instance in the code, it's "copy-pasted" into the calling
> function. This is not a toolchain bug.

1. Inline does *not* require that the function be copy-pasted.  It's only a hint.  Further, the C standard explicitly contemplates external linkage of inline functions (and thus that there might be multiple versions of an inline function).  See section 6.7.4

2. Even if C *did* have this rule, the error message generated is totally unhelpful.  It should say, "You can't have a function pointer to an inline function."  It in fact says something totally incorrect ("recompile with -fPIC").
Comment 8 Simon Stelling (RETIRED) gentoo-dev 2006-10-18 11:43:03 UTC
(In reply to comment #7)
> 1. Inline does *not* require that the function be copy-pasted.  It's only a
> hint.  Further, the C standard explicitly contemplates external linkage of
> inline functions (and thus that there might be multiple versions of an inline
> function).  See section 6.7.4

Indeed, it says:

5 A function declared with an inline function specifier is an inline function. The function specifier may appear more than once; the behavior is the same as if it appeared only once. Making a function an inline function suggests that calls to the function be as fast as possible.118) The extent to which such suggestions are effective is implementation-defined.119)

Further, http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Inline.html says:
"Inlining of functions is an optimization and it really 
Comment 9 Simon Stelling (RETIRED) gentoo-dev 2006-10-18 11:43:03 UTC
(In reply to comment #7)
> 1. Inline does *not* require that the function be copy-pasted.  It's only a
> hint.  Further, the C standard explicitly contemplates external linkage of
> inline functions (and thus that there might be multiple versions of an inline
> function).  See section 6.7.4

Indeed, it says:

5 A function declared with an inline function specifier is an inline function. The function specifier may appear more than once; the behavior is the same as if it appeared only once. Making a function an inline function suggests that calls to the function be as fast as possible.118) The extent to which such suggestions are effective is implementation-defined.119)

Further, http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Inline.html says:
"Inlining of functions is an optimization and it really works only in optimizing compilation. If you don't use -O, no function is really inline."
...and...
"When a function is both inline and static, if all calls to the function are integrated into the caller, and the function's address is never used, then the function's own assembler code is never referenced."

That being said, I have seen this with dev-libs/icu-3.4.1. I could work it around there by using -O2 (it disabled the inlining) and the code in question IS used, which makes this whole thing confusing at least.
Comment 10 Malcolm Lashley (RETIRED) gentoo-dev 2006-10-18 12:10:47 UTC
Ok, Simon and I did some more digging and it looks like a gcc bug. (Oh noes..)

Using foo.c as above, compiled .so fine with gcc-3.4.6-r2.

With gcc-4.1.1-r1
Fails to create .so with reporters command line, fails with the addition of -fkeep-inline-functions, and fails with -fno-inline.
Looking at the objdump -d output for each of the foo.o's with gcc-4.1 they are identical no matter which of these flags is passed (i.e. no frobnicate function). (ditto readelf -s for each of the 4.1.1 objects.)

Possibly useful info.
duality hprof-0.1.1 # readelf -s foo.o.346 | grep frobnicate
     5: 000000000000003e    14 FUNC    LOCAL  DEFAULT    1 frobnicate
     9: 0000000000000000    62 FUNC    GLOBAL DEFAULT    1 refrobnicate
duality hprof-0.1.1 # readelf -s foo.o.411 | grep frobnicate
     8: 0000000000000000    61 FUNC    GLOBAL DEFAULT    1 refrobnicate
     9: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND frobnicate

(If anyone requires specific objdump, readelf or savetemps output, I can provide it)
Comment 11 SpanKY gentoo-dev 2006-10-18 23:09:54 UTC
Created attachment 99992 [details]
pic.i

reduced testcase
Comment 12 SpanKY gentoo-dev 2006-10-18 23:12:23 UTC
gcc-4.0.3 and gcc-4.2 work just fine
Comment 13 SpanKY gentoo-dev 2006-10-21 20:30:02 UTC
this has been fixed in gcc-4.1.2, but i dont know if i feel like tracking down the patch that fixed it from 4.1.1 ...
Comment 14 Simon Stelling (RETIRED) gentoo-dev 2006-10-24 17:15:35 UTC
*** Bug 152719 has been marked as a duplicate of this bug. ***
Comment 15 Simon Stelling (RETIRED) gentoo-dev 2007-01-27 20:13:59 UTC
*** Bug 163734 has been marked as a duplicate of this bug. ***
Comment 16 Simon Stelling (RETIRED) gentoo-dev 2007-01-27 20:18:51 UTC
*** Bug 156082 has been marked as a duplicate of this bug. ***
Comment 17 Jakub Moc (RETIRED) gentoo-dev 2007-02-01 06:52:14 UTC
*** Bug 164767 has been marked as a duplicate of this bug. ***
Comment 18 Christian Faulhammer (RETIRED) gentoo-dev 2007-05-19 22:57:16 UTC
(In reply to comment #13)
> this has been fixed in gcc-4.1.2, but i dont know if i feel like tracking down
> the patch that fixed it from 4.1.1 ...

 As 4.1.2 has gone stable, this bug is closed