连接两个共享库的一些相同的符号的 [英] linking two shared libraries with some of the same symbols
问题描述
我想用两个不同的共享库链接。这两个库定义共享的名称,但有不同的实现一些符号。我似乎无法找到一种方法,使每个库使用它自己的实现比其他。
I am trying to link with two different shared libraries. Both libraries define some symbols that share a name but have different implementations. I can't seem to find a way to make each library use its own implementation over the other.
例如,两个库定义全局函数巴()
每个内部调用。图书馆一位来自 foo1称之为()
和库从 foo2的两次调用它()
。
For example, both libraries define a global function bar()
that each calls internally. Library one calls it from foo1()
and library two calls it from foo2()
.
Lib1.so:
T bar
T foo1() // calls bar()
Lib2.so:
Lib2.so:
T bar
T foo2() // calls bar()
如果我链接到我的应用程序对Lib1.so然后Lib2.so从Lib1.so酒吧调用执行,即使被称为 foo2的()
。如果在另一方面,连结我针对Lib2.so然后Lib1.so应用,则棒总是从Lib2.so称为
If I link my application against Lib1.so and then Lib2.so the bar implementation from Lib1.so is called even when calling foo2()
. If on the other hand, I link my application against Lib2.so and then Lib1.so, then bar is always called from Lib2.so.
有没有一种方法,使图书馆总是preFER自己的实现上述任何其他库?
Is there a way to make a library always prefer its own implementation above any other library?
推荐答案
有几种方法来解决这个问题:
There are several ways to solve this:
-
通
-Bsymbolic
或-Bsymbolic函数
以连接器。这有一个全球性的影响:每一个引用全局符号(函数类型为-Bsymbolic函数
),可以解析到库中的元件被解析为一个符号。有了这个,你失去了干预内部库调用使用LD_ preLOAD这些符号的能力。 的符号仍然远销,这样他们就可以从库外被引用。
Pass
-Bsymbolic
or-Bsymbolic-functions
to the linker. This has a global effect: every reference to a global symbol (of function type for-Bsymbolic-functions
) that can be resolved to a symbol in the library is resolved to that symbol. With this you lose the ability to interpose internal library calls to those symbols using LD_PRELOAD. The symbols are still exported, so they can be referenced from outside the library.
使用的版本脚本标记符号的本地的图书馆,例如使用类似: {地方:酒吧;};
并通过 - 版本脚本= versionfile
来的链接。 的符号的不的出口。
Use a version script to mark symbols as local to the library, e.g. use something like: {local: bar;};
and pass --version-script=versionfile
to the linker. The symbols are not exported.
与approppiate 可视性标志符号(<一个href=\"http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040$c$c_007bvisibility_007d-attribute-2535\">GCC信息页面能见度),这将是任何的隐藏的内部或保护的。 保护的的知名度符号的导出为 .protected
隐藏的符号不导出和 内部的符号不导出并您妥协不是从图书馆外面打电话给他们,即使是间接地通过函数指针
Mark symbols with an approppiate visibility (GCC info page for visibility), which will be either hidden, internal, or protected. protected visibility symbols are exported as .protected
, hidden symbols are not exported, and internal symbols are not exported and you compromise not to call them from outside the library, even indirectly through function pointers.
您可以检查哪些符号与 objdump的-T
导出。
You can check which symbols are exported with objdump -T
.
这篇关于连接两个共享库的一些相同的符号的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!