目的
用
dlsym
搜索可執行文件中的函數.
編譯
-rdynamic
- 必須得加呀.不加找不到這個符號.
案例
代碼
[root@localhost temp]# g++ test.cpp -ldl -rdynamic [root@localhost temp]# ./a.out cool,show: 2 [root@localhost temp]# cat test.cpp #include<dlfcn.h> #include<iostream> void show(int a) { std::cout << "cool,show: " << a << std::endl; } int main() { void (*td)(int); *(void**)&td = dlsym(RTLD_DEFAULT,"_Z4showi"); td(2); }
說明
- 必須執行
RTLD_DEFAULT
從當前開始搜索.RTLD_NEXT
從後面的一個開始搜索.