dlsym 如何成功从剥离的二进制库导入函数?
奇怪的是 dlsym 可以从剥离的二进制文件中导入函数。
谁能告诉我为什么/如何?
=== 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
It's weird that dlsym can import functions from stripped binaries.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 readelf -s a.so 。动态符号在
条带
之后仍然存在。(或者直接切换到 nm -D a.so。)
Try
readelf -s a.so
. The dynamic symbols are still there after thatstrip
.(Or just switch to
nm -D a.so
.)strip
删除调试符号表,而不是动态链接器使用的动态符号表。要删除这些符号,请使用-fvisibility=hidden
和 符号可见性函数/变量属性来选择要公开的函数。strip
removes debugging symbol tables, not the dynamic symbol tables used by the dynamic 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.