【问题标题】:why cant link 64bit static libgcc on ubuntu为什么无法在 ubuntu 上链接 64 位静态 libgcc
【发布时间】:2013-02-25 16:57:44
【问题描述】:

我有问题将 libgcc 链接到静态链接的 .so 中

只有在将 64 位模块与 -m64 链接时才会发生这种情况

Ubuntu 64 位 12.10 gcc 4.7

在 Ubuntu 64bit 12.04 gcc 4.6 上也失败了

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

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

你好.c

#include <stdio.h>

int f(){

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

return 1;
}

生成文件

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

【问题讨论】:

标签: c gcc ubuntu static


【解决方案1】:

答案基本上是“你不能那样做”。您正在尝试将非 PIC 代码链接到共享库,这在 x86_64 (amd64) 架构上根本不可能。你需要一个静态但 PIC 版本的 libgcc,我怀疑这只是你问题的开始。

libgcc 通常被共享的原因之一是给定的运行可执行文件必须具有 libgcc 维护的一些关键数据结构的一个且只有一个副本。静态链接对于最终的可执行文件是有意义的,因为只有一个副本将是静态链接到可执行文件的副本,但动态对象的全部意义在于将其加载到另一个可执行文件中(而后者又将拥有自己的副本libgcc,无论是共享的还是静态的)。

【讨论】:

  • 谢谢。我的问题实际上是我的 .so 无法在 Ubuntu 11.10 上加载,因为 U 11.10 没有 libgcc 2.14 兼容符号。
猜你喜欢
  • 2017-06-28
  • 2013-02-15
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 2011-03-02
  • 2010-10-02
  • 2015-01-20
  • 2014-05-15
相关资源
最近更新 更多