dlopen() .so 无法在剥离的可执行文件中找到符号
我在 linux 中有一个可执行文件 - exe
这个可执行文件中有一些函数,在整个代码中使用:
sendMsg
debugPrint
然后我想动态加载为我的可执行文件提供额外功能的 .so
。
在此共享库中,我包含 sendMsg
和 debugPrint
的标头。
我使用 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 inexe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
man ld
:您还可以将
-rdynamic
选项传递给 gcc/g++(如注释中所述)。根据您设置 make 脚本的方式,这会很方便man ld
: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