如何限制对共享对象中符号的访问?
我有一个共享库(bar.so)形式的插件,它链接到一个更大的程序(foo)。 foo 和 bar.so 都依赖于相同的第三方库 (baz),但它们需要保持 baz 的实现完全独立。因此,当我链接 foo (使用提供的目标文件和档案)时,我需要它忽略 bar.so 中 baz 的任何使用,反之亦然。
现在,如果我将 foo 与 --trace-symbol=baz_fun
链接,其中 baz_fun 是有问题的符号之一,我会得到以下输出:
bar.so: definition of baz_fun
foo/src.a(baz.o): reference to baz_fun
我相信这告诉我 foo 正在引用 bar.so 中的 baz_fun (并且 foo 的执行证实了这一点)。
我尝试过的解决方案:
- 使用 objcopy 来“本地化”感兴趣的符号: objcopy --localize-symbols=local.syms bar.so 其中 local.syms 包含所有感兴趣的符号。我想我可能只是在这里感到困惑,也许“本地”并不意味着我认为的意思。无论如何,我从上面的链接得到相同的输出。我应该注意,如果我在使用
objcopy
之前在 bar.so 上运行nm
工具,则所有相关符号都有T
标志(大写表示全局),在 objcopy 之后有一个 t 表示它们现在是本地的。所以看来我正确使用了 objcopy 。 - 使用
-fvisibility=hidden
进行编译,但是由于其他一些限制,我需要使用 GCC 3.3,它似乎不支持该功能。我也许能够升级到较新版本的 GCC,但想确认使用此标志进行编译将在走上这条路之前对我有所帮助。
其他需要注意的事项:
- 我无法访问 foo 或 baz 的源代码,
- 我更愿意将所有插件保留在一个共享对象 (bar.so) 中。 baz 实际上是一个许可库,所以我不想将它分开
I have a plug-in in the form of a shared library (bar.so) that links into a larger program (foo). Both foo and bar.so depend on the same third party library (baz) but they need to keep their implementations of baz completely separate. So when I link foo (using the supplied object files and archives) I need it to ignore any use of baz in bar.so and vice versa.
Right now if I link foo with --trace-symbol=baz_fun
where baz_fun is one of the offending symbols I get the following output:
bar.so: definition of baz_fun
foo/src.a(baz.o): reference to baz_fun
I believe this is telling me that foo is referencing baz_fun from bar.so (and execution of foo confirms this).
Solutions that I have tried:
- Using
objcopy
to "localize" the symbols of interest:objcopy --localize-symbols=local.syms bar.so
where local.syms contains all of the symbols of interest. I think I might just be confused here and maybe "local" doesn't mean what I think it means. Regardless, I get the same output from the link above. I should note that if I run thenm
tool on bar.so prior to usingobjcopy
all of the symbols in question have theT
flag (upper-case indicating global) and afterobjcopy
they have at
indicating they are local now. So it appears I am usingobjcopy
correctly. - Compiling with
-fvisibility=hidden
however due to some other constraints I need to use GCC 3.3 which doesn't appear to support that feature. I might be able to upgrade to a newer version of GCC but would like confirmation that compiling with this flag will help me before heading down that road.
Other things to note:
- I do not have access to the source code of either foo or baz
- I would prefer to keep all of my plug-in in one shared object (bar.so). baz is actually a licensing library so I don't want it separated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
dlopen
加载带有RTLD_DEEPBIND
标志的插件。(编辑)
请注意,RTLD_DEEPBIND 是 Linux 特定的,需要 glibc 2.3.4 或更高版本。
Use
dlopen
to load your plugin withRTLD_DEEPBIND
flag.(edit)
Please note that RTLD_DEEPBIND is Linux-specific and need glibc 2.3.4 or newer.