linux - dlsym 如何从剥离的二进制库中成功导入函数?

标签 linux strip 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

最佳答案

试试 readelf -s a.so。动态符号在 strip 之后仍然存在。

(或者只是切换到 nm -D a.so。)

关于linux - dlsym 如何从剥离的二进制库中成功导入函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6771725/

相关文章:

python - 为什么 children 死不了?

c++ - C/C++ Linux下的Packet Sniffer

python - 如何在 Python 中使用 Regex/Strip() 去除字符串末尾的随机字符?

c++ - 跨多个命名空间的动态加载

c++ - dlsym() + RTLD_NEXT 在 Ubuntu 20.04 上无法正常工作

java - 如何终止从 shell 脚本运行的 JVM?

linux - 不能在 awk 命令中使用 "["符号

python - 使用正则表达式匹配数据框中的字符串并替换 - python

c++ - 使用 strip 会删除二进制文件上的 RTTI 信息吗?

c++ - 我如何正确地将 dlsym 的返回值转换为具有 const 类型的函数?