如何链接到不同的 libc 文件?
由于版本差异,我想随程序一起提供共享库,而不是使用目标系统的共享库。
ldd
说我的程序使用这些共享库:
linux-gate.so.1 => (0xf7ef0000)**(made by kernel)**
libc.so.6 => /lib32/libc.so.6 (0xf7d88000)**(libc-2.7.so)**
/lib/ld-linux.so.2 (0xf7ef1000)**(ld-2.7.so)**
我已经通过编译成功链接了 ld-xxx.so:
gcc -std=c99 -D_POSIX_C_SOURCE=200112L -O2 -m32 -s -Wl,-dynamic-linker,ld-2.7.so myprogram.c
但我还没有成功链接 libc-xxx.so
。我怎样才能做到这一点?
I want to supply the shared libraries along with my program rather than using the target system's due to version differences.
ldd
says my program uses these shared libs:
linux-gate.so.1 => (0xf7ef0000)**(made by kernel)**
libc.so.6 => /lib32/libc.so.6 (0xf7d88000)**(libc-2.7.so)**
/lib/ld-linux.so.2 (0xf7ef1000)**(ld-2.7.so)**
I have successfully linked ld-xxx.so by compiling with:
gcc -std=c99 -D_POSIX_C_SOURCE=200112L -O2 -m32 -s -Wl,-dynamic-linker,ld-2.7.so myprogram.c
But I have not managed to successful link libc-xxx.so
. How can I do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了如何做到这一点:
rpath 指定提供的库所在的位置。该文件夹应包含:
libc.so.6
、libdl.so.2
、libgcc_s.so.1
以及其他内容。使用 strace 检查您的二进制文件使用了哪些库。ld.so
是提供的链接器gcc -Xlinker -rpath=/default/path/to/libraries -Xlinker -I/default/path/to/libraries/ld.so program.c
I found out how to do it:
rpath specifies where the provided libraries are located. This folder should contain:
libc.so.6
,libdl.so.2
,libgcc_s.so.1
and maybe more. Check with strace to find out which libraries your binary file uses.ld.so
is the provided linkergcc -Xlinker -rpath=/default/path/to/libraries -Xlinker -I/default/path/to/libraries/ld.so program.c
将
-nodefaultlibs
或-nostdlib
传递给 gcc 将告诉它不要将默认库作为参数传递给 ld。然后,您将能够显式指定要链接的 libc。有关这两个选项的更多详细信息和注意事项,请参阅gcc(1)
手册页。Passing
-nodefaultlibs
or-nostdlib
to gcc will tell it to not pass the default libraries as arguments to ld. You will then be able to explicitly specify the libc you want to link against. See thegcc(1)
man page for more details and caveats regarding both options.