简体   繁体   English

dlsym 如何从剥离的二进制库中成功导入 function?

[英]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.

相关问题 如何验证已从二进制文件中删除了无效代码? - how can I verify that dead code was stripped from the binary? 如何使用Cython从dlfcn.h中的dlsym加载函数 - how to load function with dlsym from dlfcn.h with Cython 如何dlsym加载QString函数 - How to dlsym load QString Function 为什么剥离的二进制文件在反汇编文件中仍然可以有库调用信息? - Why a stripped binary file can still have library call information in the disassembled file? 来自dlsym的std :: function导致分段错误 - std::function from dlsym results in segmentation fault 我可以从 memory 加载带有 dlsym 的符号吗 - Can I load symbols with dlsym from memory 我可以通过链接此dl从动态库中加载函数,但是如果不链接此dl,就无法在代码中使用“ dlsym”加载它 - I can load functions from dynamic library with linking this dl ,but I can not load it using 'dlsym' in the code without linking this dl 如何拆卸剥离应用程序的主要功能? - How to disassemble the main function of a stripped application? 如何使用dlopen()和dlsym()在.so文件中调用函数? - How to call function in .so file using dlopen() and dlsym()? 我如何获得操作系统提供的dlopen()/ dlsym()的地址 - How I can get address of dlopen()/dlsym() provided by the OS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM