简体   繁体   English

共享对象中的符号

[英]symbols in shared objects

My scenario is :我的情况是:

  • frame work static lib - libFramework.a框架静态库 - libFramework.a
  • A shared object using that framework - mySharedObj.so使用该框架的共享对象 - mySharedObj.so
  • An executable that is using both libs - myExe.elf一个同时使用两个库的可执行文件 - myExe.elf

I am compiling the so and the elf separately.我正在分别编译 so 和 elf。 Sometimes the framework has a new version and I want to update only the so or only the elf (does'nt matter) But when I am running the application (with debug) , I see that the so is using the elf framework lib How can I "force" the so use its own framework instance while running under the elf有时框架有一个新版本,我只想更新 so 或只更新 elf(无关紧要)但是当我运行应用程序(带调试)时,我看到 so 正在使用 elf 框架 lib 怎么能我“强制”所以在精灵下运行时使用自己的框架实例

BTW I have the same issue if by chance the so and elf both have a class with the same name (if not using namespaces etc.) The so is using the elf class.顺便说一句,如果 so 和 elf 碰巧都有一个同名的类(如果不使用名称空间等),我也会遇到同样的问题。 so 正在使用 elf 类。

What's probably happening is that libFramework.a is exposing dynamic symbols.可能发生的事情是 libFramework.a 暴露了动态符号。 When you start myExe.elf, it resolves those dynamic symbols from itself (or its own copy of libFramework.a, if you want to look at it like that).当您启动 myExe.elf 时,它会从自身(或它自己的 libFramework.a 副本,如果您想这样看的话)解析这些动态符号。

When mySharedObj.so is loaded, it then resolves all those symbols again, and finds the ones in myExe.elf rather than in mySharedObj.so, because it's reusing the result of the previous resolution.当加载 mySharedObj.so 时,它会再次解析所有这些符号,并在 myExe.elf 中而不是在 mySharedObj.so 中找到这些符号,因为它重用了先前解析的结果。 You can probably confirm this by running myExe.elf with LD_DEBUG=all, eg LD_DEBUG=all myExe.elf .您可以通过运行带有 LD_DEBUG=all 的 myExe.elf 来确认这一点,例如LD_DEBUG=all myExe.elf

To stop this happening, you want to prevent any dynamic symbols from being exported by libFramework.a.为了阻止这种情况发生,您希望防止 libFramework.a 导出任何动态符号。 You can use the compiler flag -fvisibility=hidden to change the default to symbols being hidden, and then make sure that your symbols aren't then being made visible again by attributes in the source code, eg __attribute__ ((visibility ("default"))) or by pushing and popping visibility #pragma GCC visibility push(default) #pragma GCC visibility pop .您可以使用编译器标志-fvisibility=hidden将默认值更改为隐藏的符号,然后确保源代码中的属性不再使您的符号可见,例如__attribute__ ((visibility ("default")))或通过推送和弹出可见性#pragma GCC visibility push(default) #pragma GCC visibility pop You can also use nm to see what symbols are being exposed as dynamic symbols.您还可以使用 nm 来查看哪些符号被公开为动态符号。

It's also worth checking this sort of stuff out for perf reasons.出于性能原因,也值得检查此类内容。 If all your symbols are dynamic, the runtime dynamic linker has to do a lot more work, so your program will take longer to load.如果您的所有符号都是动态的,则运行时动态链接器必须做更多的工作,因此您的程序将需要更长的时间来加载。 The compiler can also generate better code because it doesn't have to put in the code to let functions be called dynamically.编译器还可以生成更好的代码,因为它不必放入代码中就可以动态调用函数。 The linker can do a better job of dead code elimination, etc. There's a bunch of wins.链接器可以更好地消除死代码等。有很多胜利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM