dlopen() .so 无法在剥离的可执行文件中找到符号

发布于 2024-11-09 07:21:57 字数 842 浏览 0 评论 0原文

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

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

  • sendMsg
  • debugPrint

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

在此共享库中,我包含 sendMsgdebugPrint 的标头。

我使用 dlopen() 加载此共享库,并使用 dlsym() 创建 API。

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

它失败,指出找不到 sendMsg 符号。

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

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

我该如何解决这种情况?

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

I have an executable in linux - exe

This executable has some functions in it, that are used throughout the code:

  • sendMsg
  • debugPrint

I then want to dynamically load a .so that provides extra functionality to my executable.

In this shared library I include the headers for sendMsg and debugPrint.

I load this shared library with dlopen() and create an API with dlsym().

However, at dlopen() I use RTLD_NOW to resolve all symbols at load time.

It fails stating that it cannot find sendMsg symbol.

This symbol must be in the executable as the sendMsg.c is compiled in there.

However, my executable is stripped by the make process. As such, it would make sense that dlopen cannot find the symbol.

How can i solve this situation?

  • I could build the shared functions into a static library and link that static library into both exe and the .so. This would increase code size :(
  • I could remove the stripping of the exe so the symbols can be found
  • Do some compile time linking magic that I don't know about so the .so knows where the symbols are in exe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-11-16 07:21:57

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

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.

You can also pass the -rdynamic option to gcc/g++ (as noted int the comment). Depending on how you setup your make script, this will be convenient

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文