我最近试图从第 3 方 DSO 中隐藏一些符号,并遇到了用于创建“过滤器 DSO”的“--filter”LD 选项。来自ld manpage ,我的印象是动态链接器只会考虑过滤器 DSO 的 dynsym 中存在的符号:
"The dynamic linker will resolve symbols according to the symbol table of the filter object as usual, but it will actually link to the definitions found in the shared object name."
所以它看起来正是我需要的:一种从 DSO 中选择哪些符号参与动态链接的方法。不幸的是,它没有像我预期的那样工作(这个例子也可以从 github 克隆)
生成文件:
lib1.so: lib1.c
gcc -shared -fPIC $^ -o $@
lib2.so: lib2.c
gcc -shared -fPIC $^ -o $@
lib2f.so: lib2f.c
gcc -shared -fPIC -Wl,--filter,lib2.so $^ -o $@
main: lib1.so lib2.so lib2f.so main.c
gcc main.c -L. -l2f -l1 -o $@
clean:
rm *.o *.so main
lib1.c
#include <stdio.h>
void func1()
{
printf("func1@lib1\n");
}
void func2()
{
printf("func2@lib1\n");
}
lib2.c
#include <stdio.h>
void func1()
{
printf("func1@lib2\n");
}
void func3()
{
printf("func3@lib2\n");
}
lib2f.c(lib2.so 的过滤器):
void func3() {}
可执行
void func1();
void func2();
void func3();
int main()
{
func1();
func2();
func3();
return 0;
}
当我运行该测试程序时,我得到以下输出:
> LD_LIBRARY_PATH=. ./main
func1@lib2
func2@lib1
func3@lib2
尽管试图用 lib2f.so 来“过滤”它,但可以看到实际上引用了 lib2 中的一个符号。我希望输出看起来像
> LD_LIBRARY_PATH=. ./main
func1@lib1 // use func1 from lib1
func2@lib1
func3@lib2
是否可以使用 ld --filter
选项(又名 DT_FILTER)实现我的目标(从 DSO 中隐藏一些符号)?
如果不是,我对手册页的期望/阅读有什么问题?
描述的行为发生在 glibc 2.34 和 2.17 上。
最佳答案
根据 https://sourceware.org/bugzilla/show_bug.cgi?id=27977 :
DT_NEEDED puts the referenced object after the current object in the search scope. DT_FILTER puts it before it. I believe this is the only difference in the current glibc implementation. There is simply no filtering. It has been this way since basically forever
关于c - 使用 DT_FILTER 从 DSO 隐藏符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67879573/