2011-05-25 69 views
1

我在linux可執行 - exe的dlopen()。所以,沒有找到符號在剝離可執行

該可執行文件中有一些功能,即在整個代碼中使用:

  • sendMsg
  • debugPrint

那麼我想動態加載.so,可提供額外的功能ity到我的可執行文件。

在此共享庫中,我包含了sendMsgdebugPrint的標頭。

我使用dlopen()加載這個共享庫並使用dlsym()創建一個API。

但是,在dlopen()我使用RTLD_NOW在加載時解析所有符號。

它失敗,說明它找不到sendMsg符號。

此符號必須在可執行文件中,因爲sendMsg.c在那裏編譯。

但是,我的可執行文件被make進程剝離。因此,dlopen找不到符號是有意義的。

我該如何解決這種情況?

  • 我可以建立共享的功能集成到一個靜態庫和靜態庫鏈接到兩個exe.so。這將增加代碼尺寸:(
  • 我可以刪除exe這樣的符號,可以發現
  • 做一些編譯時鏈接的魔法,我不知道這樣的.so知道哪裏的符號是exe的剝離

回答

3

man ld

-E 
    --export-dynamic 
    --no-export-dynamic 
     When creating a dynamically linked executable, using the -E option or the --export-dynamic option causes the linker to add all symbols to the dynamic symbol table. The 
     dynamic symbol table is the set of symbols which are visible from dynamic objects at run time. 

     If you do not use either of these options (or use the --no-export-dynamic option to restore the default behavior), the dynamic symbol table will normally contain only those 
     symbols which are referenced by some dynamic object mentioned in the link. 

     If you use "dlopen" to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably 
     need to use this option when linking the program itself. 

     You can also use the dynamic list to control what symbols should be added to the dynamic symbol table if the output format supports it. See the description of 
     --dynamic-list. 

     Note that this option is specific to ELF targeted ports. PE targets support a similar function to export all symbols from a DLL or EXE; see the description of 
     --export-all-symbols below. 

您也可以通過-rdynamic選項GCC/G ++(如INT註釋說明)取決於你如何設置你的化妝腳本。 ,這將是方便

+0

http://stackoverflow.com/questions/480617/dlopen-issue 二手動態,這是平臺無關的。 感謝雖然,第一次動態鏈接在Linux上。 – 2011-05-25 09:25:32