dlsym搜可執行文件中的符號

  • 目的

    • 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從後面的一個開始搜索.
  • 發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章