简体   繁体   English

为什么不能在ubuntu上链接64bit静态libgcc

[英]why cant link 64bit static libgcc on ubuntu

I have problem link libgcc into a static linked .so 我在将libgcc链接到静态链接的.so时遇到问题

it only happens when linking 64bit module with -m64 它仅在将64位模块与-m64链接时发生

Ubuntu 64bit 12.10 gcc 4.7 Ubuntu 64位12.10 gcc 4.7

also failed on Ubuntu 64bit 12.04 gcc 4.6 在Ubuntu 64位12.04 gcc 4.6上也失败

32bit no problem 32位没问题

$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 64位失败

$ 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. 您正在尝试将非PIC代码链接到共享库中,这在x86_64(amd64)架构上是根本不可能的。 You would need a static but PIC version of libgcc, and I suspect that would be only the start of your problems. 您将需要一个静态但PIC的libgcc版本,我怀疑那仅仅是问题的开始。

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. 正常共享libgcc的原因之一是,给定的正在运行的可执行文件必须具有libgcc维护的某些关键数据结构的一个且只有一个副本。 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). 静态链接对于最终的可执行文件是有意义的,因为一个副本和一个副本将是静态链接到可执行文件中的一个副本,但是动态对象的整个要点都将被加载到另一个可执行文件中(后者又将拥有自己的副本) libgcc(共享或静态)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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