简体   繁体   中英

why cant link 64bit static libgcc on ubuntu

I have problem link libgcc into a static linked .so

it only happens when linking 64bit module with -m64

Ubuntu 64bit 12.10 gcc 4.7

also failed on Ubuntu 64bit 12.04 gcc 4.6

32bit no problem

$gcc -fPIC -c -o hello.o hello.c -m32
$gcc -shared -m32 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic  -lc
$ ldd libhello.so 
    statically linked

64bit failed

$ make
gcc -fPIC -c -o hello.o hello.c
gcc -shared -m64 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic  -lc
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a(iofclose.o): relocation R_X86_64_32 against `__gcc_personality_v0' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [libhello.so] Error 1

hello.c

#include <stdio.h>

int f(){

FILE *out = fopen("/tmp/x.log", "wb");
fclose(out);

return 1;
}

Makefile

all: libhello.so

libhello.so: hello.o
    gcc -shared -m64 hello.o -o libhello.so -static-libgcc -Wl,-Bstatic  -lc

hello.o: hello.c
    gcc -fPIC -c -o hello.o hello.c

clean: 
    rm -f hello.o libhello.so

The answer is basically "you can't do that." You're trying to link non-PIC code into a shared library, which is simply impossible on the x86_64 (amd64) architecture. You would need a static but PIC version of libgcc, and I suspect that would be only the start of your problems.

One of the reasons why libgcc is normally shared is that a given running executable has to have one and only one copy of some of the key data structures that libgcc maintains. Static linking makes sense for a final executable, since that one and only one copy will be the one statically linked into the executable, but the whole point of a dynamic object is to be loaded into another executable (which in turn will have its own copy of libgcc, either shared or static).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM