主程序和共享库在 __static_initialization_and_destruction_0 中初始化相同的静态变量

发布于 2024-08-28 08:39:09 字数 134 浏览 4 评论 0原文

有谁知道为什么在 dlopen() 中初始化的库会初始化主程序拥有的静态变量。主程序和共享库都有静态变量的副本,但由于某种原因共享库重新初始化主程序的静态变量副本并破坏它,当主程序尝试破坏它时会导致段错误。

这是符号表中名称损坏的情况吗?

Does anyone know why a library initialized within dlopen() would initialize a static variable owned by the main program. Both the main program and shared library have a copy of the static variable, but for some reason the shared library re-initializes the main program's copy of the static variable and destructs it, causing a segfault when the main program attempts to destruct it.

Is this a case of bad name mangling in the symbol table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

送你一个梦 2024-09-04 08:39:09

在这种情况下,运行时链接器只需要进程中符号的单个活动副本。如果共享对象和可执行文件都有该符号的副本,则运行时链接器将解析对其中之一的所有引用。

解决此问题的方法是使用 version 命令进行符号缩减构建共享对象时链接编辑器的。确保静态变量的符号不是全局的,您将获得您正在寻找的行为。

This is a case where the runtime linker only wants a single active copy of a symbol in a process. If both a shared object and the executable have a copy of the symbol, the runtime linker will resolve all references to one of those.

What you can do to solve this problem is to use symbol reduction using the version command of the link editor when building the shared object. Make sure the symbol for the static variable is not global and you will get the behavior you are looking for.

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