static linking of program with threads causes assertation in libgcc
Original Reporter info from Mantis: ppopov99
-
Reporter name: Peter Popov
Original Reporter info from Mantis: ppopov99
- Reporter name: Peter Popov
Description:
In the process of porting fpc to Blue Gene/P i found a bug in the linker. The bug appears when linking statically the pthread library. The root of the problem is wrong order of the initialization routines of libc and libgcc. The problem appears when linking statically against pthread in the standard version of linux-powerpc as well. I suspect this will also happen on i386. FPC 2.3.1 links (linux-powerpc) in the following order (from link.res):
cprt0.o
crtbegin.o
crti.o
.....
crtend.o
crtn.o
As a result, the exception handling data of libgcc gets messed up and an assertation is thrown when calling pthread_exit() as well as other pthread functions.
Instead, the linking order should be (checked with gcc -###)
cprt0.o
crti.o
crtbeginT.o
.....
crtend.o
crtn.o
Some remarks:
- fpc -Xt switch doesn't work, one has to explicitly add {$linkobj gcc_eh}, see steps to reproduce
- Note that the later order makes sense, as the ini/fini of libc and libgcc are nested.
- One should use crtbeginT.o instead of crtbegin.0
- The linking order of libpthread, libgcc, libgcc_eh and libc is also essential. GCC links them in the following order:
INPUT(
-lpthread
)
GROUP(
-lgcc
-lgcc_eh
-lc
)
Different order of linking may produce code whith messed up gcc_eh internals. It turns out that the GROUP statement is essential, as ld makes several passes over libgcc, libgcc_eh and libc.
Steps to reproduce:
compile the provided sourse with -Xt -sh -a options. link.res looks like this:
INPUT(
/bgusr/ppopov/fpc/lib/fpc/2.3.1/units/powerpc-linux/rtl/cprt0.o
/usr/lib/gcc/powerpc64-suse-linux/4.1.2/crtbegin.o
/usr/lib/crti.o
phello.o
/bgusr/ppopov/fpc/lib/fpc/2.3.1/units/powerpc-linux/rtl/system.o
)
INPUT(
-lpthread
-lc
-lgcc
)
INPUT(
/usr/lib/gcc/powerpc64-suse-linux/4.1.2/crtend.o
/usr/lib/crtn.o
)
Add -lgc_eh and execute ppas.sh. The code will crash. It works fine when the linker input is:
INPUT(
/bgusr/ppopov/fpc/lib/fpc/2.3.1/units/powerpc-linux/rtl/cprt0.o
/usr/lib/crti.o
/usr/lib/gcc/powerpc64-suse-linux/4.1.2/crtbeginT.o
phello.o
/bgusr/ppopov/fpc/lib/fpc/2.3.1/units/powerpc-linux/rtl/system.o
)
INPUT(
-lpthread
)
GROUP(
-lgcc
-lgcc_eh
-lc
)
INPUT(
/usr/lib/gcc/powerpc64-suse-linux/4.1.2/crtend.o
/usr/lib/crtn.o
)
Additional information:
$ gcc -v
Using built-in specs.
Target: powerpc64-suse-linux
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.1.2 --enable-ssp --disable-libssp --disable-libgcj --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new --program-suffix= --enable-version-specific-runtime-libs --without-system-libunwind --with-cpu=default32 --enable-secureplt --with-long-double-128 --host=powerpc64-suse-linux
Thread model: posix
gcc version 4.1.2 20070115 (prerelease) (SUSE Linux)
Mantis conversion info:
- Mantis ID: 14265
- OS: linux
- OS Build: suse 2.6.16.46
- Platform: powerpc
- Version: 2.3.1
- Fixed in version: 2.4.0
- Fixed in revision: 13717 (#0698121f)