[英]How can dlsym successfully import function from stripped binary library?
It's weird that dlsym can import functions from stripped binaries. dlsym 可以从剥离的二进制文件中导入函数,这很奇怪。
Can anyone tell me why/how?谁能告诉我为什么/如何?
=== FILE: a.c ===
int a1() { return 1; }
int a2() { return 2; }
=== end of a.c ===
=== FILE: b.c ===
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
typedef int (*fint)();
fint dlsym_fint(void *handle, char *name)
{
fint x = (fint)dlsym(handle, name);
char *err = NULL;
if ((err = dlerror()) != NULL) {
printf("dlsym: %s\n", err);
exit(1);
}
return x;
}
int main()
{
void *dl = dlopen("a.so", RTLD_NOW);
fint a = NULL;
a = dlsym_fint(dl, "a1");
printf("%p: %d\n", a, a());
a = dlsym_fint(dl, "a2");
printf("%p: %d\n", a, a());
return 0;
}
=== end of b.c ===
$ gcc -shared -fPIC -o a.so a.c
$ nm a.so
...
00000000000004ec T a1
00000000000004f7 T a2
...
$ strip a.so
$ nm a.so
nm: a.so: no symbols
$ gcc -o b b.c -ldl
$ ./b
0x2aaaaaac74ec: 1
0x2aaaaaac74f7: 2
Try readelf -s a.so
.试试
readelf -s a.so
。 The dynamic symbols are still there after that strip
.动态符号在
strip
之后仍然存在。
(Or just switch to nm -D a.so
.) (或者只是切换到
nm -D a.so
。)
strip
removes debugging symbol tables, not the dynamic symbol tables used by the dynamic linker. strip
删除调试符号表,而不是动态 linker 使用的动态符号表。 To remove those symbols as well, use -fvisibility=hidden
, and the symbol visibility function/variable attributes to select which functions you want to expose.要同时删除这些符号,请使用
-fvisibility=hidden
,并将符号可见性函数/变量属性设置为 select 要公开的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.