dlopen 有两个共享库,导出符号

发布于 2024-09-12 02:12:07 字数 486 浏览 1 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

痴骨ら 2024-09-19 02:12:07

foo.so 链接到 bar.so

当可执行文件dlopen()foo.so时,bar.so也会被加载。

或者,对可执行文件进行二进制修补,以将 RTLD_GLOBAL 添加到 dlopen() 调用的标志中。该代码看起来类似于

    movl    $2, 4(%esp)       # $2 == RTLD_NOW; RTLD_LOCAL is 0
    movl    $0xNNNNN, (%esp)  # $0xNNNNN == &"foo.so"
    call    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 against bar.so.

When the executable dlopen()s foo.so, bar.so will also be loaded.

Alternatively, binary-patch the executable to add RTLD_GLOBAL to the flags for dlopen() call. The code will look something like

    movl    $2, 4(%esp)       # $2 == RTLD_NOW; RTLD_LOCAL is 0
    movl    $0xNNNNN, (%esp)  # $0xNNNNN == &"foo.so"
    call    dlopen

Patch it to movl $0x102, 4(%esp) instead (RTLD_GLOBAL == 0x100), and voilà.

EDIT:
If you know the name of bar.so, then you can link foo.so against a "stub" bar.so. It doesn't matter that you don't have "real" bar.so; all that matters is that foo.so has a dependency on it. At runtime that dependency will cause bar.so to be loaded whenever foo.so is loaded.

梦回旧景 2024-09-19 02:12:07

另一种方式如下所示:

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)

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