如何设置共享库的动态链接器路径?
我想编译一个带有 .interp
段的共享库。
#include <stdio.h>
int foo(int argc, char** argv) {
printf("Hello, world!\n");
return 0;
}
我正在使用以下命令。
gcc -c -o test.o test.c
ld --dynamic-linker=blah -shared -o test.so test.o
我最终没有 INTERP 段,就好像我从未通过 --dynamic-linker=blah
选项一样。使用readelf -l test.so 检查。构建可执行文件时,链接器会正确处理选项并将 INTERP 段放入程序头中。如何使其也适用于共享库?
I want to compile a shared library with an .interp
segment.
#include <stdio.h>
int foo(int argc, char** argv) {
printf("Hello, world!\n");
return 0;
}
I'm using the following commands.
gcc -c -o test.o test.c
ld --dynamic-linker=blah -shared -o test.so test.o
I end up without an INTERP segment, as if I never passed the --dynamic-linker=blah
option. Check with readelf -l test.so
. When building an executable, the linker processes the option correctly and puts an INTERP segment in the program header. How to do I make it work for shared libraries too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果使用
-shared
,ld
不包含 .interp 部分,正如 @MichaelDillon 已经说过的那样。不过,您可以自己提供此部分。上面的行将使用 GCC 属性。
如果您尝试构建一个本身也可执行的共享对象,请查看此问题。它对该过程有更全面的描述。
ld
doesn't include a .interp section if-shared
is used, as @MichaelDillon already said. You can however provide this section yourself.The line above will save the string "/path/to/dynamic/linker" in the .interp section using GCC attributes.
If you're trying to build a shared object that's also executable by itself, check this question out. It has a more comprehensive description of the process.
INTERP 段仅进入需要首先加载 ELF 解释器 (ld.so) 的二进制文件。共享库没有 INTERP 段,因为在加载共享库之前 ELF 解释器已经加载。
The INTERP segment only goes into binaries which need to load the ELF interpreter (ld.so) in the first place. A shared library has no INTERP segment because the ELF interpreter is already loaded before the shared library is loaded.
在大多数 Linux 系统中,ldconfig 在每次系统启动时运行,它会查找 /etc/ld.so.conf 中的定义以查找具有共享库的目录。在文件 /etc/ld.so.cache 中有共享库 sonames 和库完整路径的映射。考虑阅读这篇文章:http://grahamwideman.wordpress.com/2009/02/09/the-linux-loader-and-how-it-finds-libraries/#comment-164
In most linux systems the ldconfig is run at every system boot and it looks definitions in /etc/ld.so.conf for looking in directories that have shared libraries. In the file /etc/ld.so.cache there are mappings for shared libraries sonames and the library full path. Consider reading this article: http://grahamwideman.wordpress.com/2009/02/09/the-linux-loader-and-how-it-finds-libraries/#comment-164