2011-05-25 73 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