Dan*_*iel 0 linux gcc g++ shared-libraries function-interposition
我目前正在 linux 上链接两个第三方共享库(A.so 和 B.so)。问题是这两个 so 都与另一个库静态链接,因此来自 A.so 和 B.so 的大约 400 个函数具有相同的名称。当我用 -lA -lB 或 -lB -lA 编译和链接时,根据顺序,由于函数插入导致问题和代码无法运行,函数分别从 A 或 B 中提取。我想知道是否有办法将函数名称绑定到它们的库,以便可以链接和使用这两个库?因为那些重叠的函数名是在 A 和 B 内部调用的,所以我不能使用像 objcopy 之类的东西。 dlopen 会有帮助吗?
我想知道是否有办法将函数名称绑定到它们的库,以便可以链接和使用这两个库?
两个库链接的时候,他们应该控制了他们导出的符号,应该隐藏了“另一个”库,但他们没有......
dlopen 会有帮助吗?
是的:如果你dlopen("A.so", RTLD_LOCAL);和dlopen("B.so", RTLD_LOCAL);,那么这两个库都不会被添加到全局范围内,它们也不会“看到”彼此。
您必须从A.so和 中明确查找所需的符号B.so,但这是您能做的最好的事情。
更新:
是否有一种快速链接到静态库的方法,而无需在构建 A.so 时从该“其他”库中导出符号
这最好通过使用-fvisibility=hidden标志和__attribute__((visibility("default")))应该导出的符号来完成。例子:
#define EXPORTED __attribute__((visibility("default")))
struct Foo {
void EXPORTED ExportedFunction();
void EXPORTED AnotherExportedFunction();
void InternalFunction();
};
void Foo::ExportedFunction() { }
void Foo::AnotherExportedFunction() { }
void Foo::InternalFunction() { }
gcc -shared -fPIC -o foo.so foo.cc
nm -CD foo.so | grep Foo::
00000000000005fc T Foo::ExportedFunction()
0000000000000610 T Foo::InternalFunction()
0000000000000606 T Foo::AnotherExportedFunction()
Run Code Online (Sandbox Code Playgroud)
如果没有明确的出口控制,一切都会被出口(包括InternalFunction我们不想要的)。
gcc -shared -fPIC -o foo.so foo.cc -fvisibility=hidden
nm -CD foo.so | grep Foo::
00000000000005bc T Foo::ExportedFunction()
00000000000005c6 T Foo::AnotherExportedFunction()
Run Code Online (Sandbox Code Playgroud)
瞧:只有我们明确想要出口的东西。