【问题标题】:dlopen() .so fails to find symbols in a stripped executabledlopen() .so 无法在剥离的可执行文件中找到符号
【发布时间】:2011-05-25 08:53:53
【问题描述】:

我在 linux 中有一个可执行文件 - exe

这个可执行文件中有一些函数,在整个代码中都会用到:

  • sendMsg
  • debugPrint

然后我想动态加载一个.so,它为我的可执行文件提供额外的功能。

在这个共享库中,我包含了 sendMsgdebugPrint 的标头。

我用dlopen() 加载这个共享库并用dlsym() 创建一个API。

但是,在dlopen(),我使用RTLD_NOW 在加载时解析所有符号。

它无法找到sendMsg 符号。

这个符号必须在可执行文件中,因为sendMsg.c 在其中编译。

但是,我的可执行文件被make 进程剥离。因此,dlopen 找不到符号是有道理的。

我该如何解决这种情况?

  • 我可以将共享函数构建到一个静态库中,并将该静态库链接到exe.so。这会增加代码大小:(
  • 我可以删除 exe 的剥离,以便可以找到符号
  • 做一些我不知道的编译时链接魔术,以便.so 知道符号在exe 中的位置

【问题讨论】:

    标签: linux dynamic-linking dynamic-loading


    【解决方案1】:

    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++(如注释中所述)。根据您设置 make 脚本的方式,这会很方便

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多