3

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

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

  • sendMsg
  • debugPrint

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

在这个共享库中,我包含了sendMsg和的标题debugPrint

我加载这个共享库dlopen()并使用dlsym().

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

它未能说明它找不到sendMsg符号。

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

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

我该如何解决这种情况?

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

1 回答 1

5

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 脚本的方式,这将很方便

于 2011-05-25T09:01:27.850 回答