如何设置共享库的动态链接器路径? [英] How to set the dynamic linker path for a shared library?
问题描述
我想用 .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;
}
我正在使用以下命令。
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 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?
推荐答案
ld
不包含.interp节,如果使用 -shared
,就像@MichaelDillon所说的那样。
ld
doesn't include a .interp section if -shared
is used, as @MichaelDillon already said. You can however provide this section yourself.
const char interp_section[] __attribute__((section(".interp"))) = "/path/to/dynamic/linker";
上面这行将保存.interp中的字符串/ path / to / dynamic / linker部分使用 GCC属性。
The line above will save the string "/path/to/dynamic/linker" in the .interp section using GCC attributes.
如果您尝试构建也可以自行执行的共享对象,请检查此问题 out。它有一个更全面的过程描述。
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.
这篇关于如何设置共享库的动态链接器路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!