dlopen 有两个共享库,导出符号
我有一个 Linux 共享库 foo.so,它是使用 dlopen("foo.so", RTLD_NOW | RTLD_LOCAL) 从可执行文件加载的。我想从 foo.so dlopen 另一个库 bar.so,它引用 foo.so 中定义的符号,但链接器无法找到它们。我无法将 RTLD_LOCAL 更改为 RTLD_GLOBAL,因为我没有执行加载的可执行文件的源代码。我认为链接 foo.so 时的 -Wl,--export-dynamic
可能会有所帮助,但它不会将本地标志覆盖为 dlopen。 GCC 的新属性可见性功能看起来也没有提供答案。
有没有一种方法可以指示链接器将对 bar.so 中未定义符号的引用解析为 foo.so 中的定义,而无需将 bar 与 -lfoo 或相似性将符号链接到第三个库并将 foo 和 bar 链接到它?我唯一想到的就是从 foo.so 本身内部使用 RTLD_GLOBAL dlopen foo.so,然后 dlopen bar.so,但这让我觉得有点混乱。谢谢。
I have a linux shared library, foo.so, which is loaded from an executable using dlopen("foo.so", RTLD_NOW | RTLD_LOCAL)
. From foo.so I'd like to dlopen another library, bar.so, which references symbols defined in foo.so, but the linker fails to find them. I can't change RTLD_LOCAL to RTLD_GLOBAL, because I don't have the source to the executable doing the loading. I thought -Wl,--export-dynamic
when linking foo.so might help but it doesn't override the local flag to dlopen. GCC's new attribute visibility feature doesn't look like it offers the answer either.
Is there a way I can instruct the linker to resolve references to undefined symbols in bar.so to those definitions in foo.so, without linking bar with -lfoo or similarity moving the symbols into a 3rd library and linking both foo and bar against it? The only thing that occurs to me is to dlopen foo.so with RTLD_GLOBAL from within foo.so itself, then dlopen bar.so, but that strikes me as a bit of a mess. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
foo.so
链接到bar.so
。当可执行文件
dlopen()
的foo.so
时,bar.so
也会被加载。或者,对可执行文件进行二进制修补,以将
RTLD_GLOBAL
添加到dlopen()
调用的标志中。该代码看起来类似于Patch it to
movl $0x102, 4(%esp)
相反 (RTLD_GLOBAL == 0x100
),瞧。编辑:
如果您知道
bar.so
的名称,则可以将foo.so
链接到“存根”bar.so
。即使您没有“真正的”bar.so
,也没关系;重要的是 foo.so 对其有依赖关系。在运行时,每当加载foo.so
时,该依赖项都会导致加载bar.so
。Link
foo.so
againstbar.so
.When the executable
dlopen()
sfoo.so
,bar.so
will also be loaded.Alternatively, binary-patch the executable to add
RTLD_GLOBAL
to the flags fordlopen()
call. The code will look something likePatch it to
movl $0x102, 4(%esp)
instead (RTLD_GLOBAL == 0x100
), and voilà.EDIT:
If you know the name of
bar.so
, then you can linkfoo.so
against a "stub"bar.so
. It doesn't matter that you don't have "real"bar.so
; all that matters is thatfoo.so
has a dependency on it. At runtime that dependency will causebar.so
to be loaded wheneverfoo.so
is loaded.另一种方式如下所示:
excutable --dlopen local--> fakefoo.so(重命名为foo)--dlopen全局--> foo.so(重命名为其他)
Here is a another way show as following:
excutable --dlopen local--> fakefoo.so(rename to foo) --dlopen global--> foo.so(rename to other)