Linux 中共享库的符号绑定如何工作

发布于 2024-08-23 00:18:18 字数 1279 浏览 8 评论 0原文

当使用 g++ -O0 编译 cpp 程序时,我注意到我的二进制文件不包含空字符串(basic_string)的符号: _S_empty_rep_storage 当我使用 -O2 编译同一个程序时,我注意到上述符号确实包含在二进制文件中,如下所示(在 bin 上使用 nm):

00000000006029a0 V _ZNSs4_Rep20_S_empty_rep_storageE@@GLIBCXX_3.4

我的应用程序使用几个 .so (动态库),当我的应用程序加载时,我注意到其中几个 .so 文件绑定如下(我设置 LD_DEBUG=all 并运行我的程序):

 28596: binding file /home/bbazso/usr/local/lib/mydynamiclib.so [0] to /usr/lib64/libstdc++.so.6 [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]
 28596: binding file /home/bbazso/usr/local/lib/mydynamiclib.so [0] to /home/bbazso/workspace/mytestapplication [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]
 28596: binding file /home/bbazso/workspace/mytestapplication [0] to /usr/lib64/libstdc++.so.6 [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]**

但我也注意到我的 .so 之一仅绑定如下:

  28087:    binding file /home/bbazso/usr/local/lib/anotherdynamiclib.so [0] to /usr/lib64/libstdc++.so.6 [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]

但永远不会绑定到二进制文件(mytestapplication),如上面的 mydynamiclib.so 所示。

所以我想知道这实际上意味着什么?这是否意味着 anotherdynamiclib.so 将为上面的空字符串使用与应用程序其余部分不同的符号?我想我真正要问的是符号绑定在上面示例的上下文中如何工作?

谢谢!

When compiling a cpp program with g++ -O0 I noticed that my binary does not contain the symbol for the empty string (basic_string):
_S_empty_rep_storage
When I do compile this same program with -O2 I notice that the aforementioned symbol is indeed contained within the binary as follows (using nm on the bin):

00000000006029a0 V _ZNSs4_Rep20_S_empty_rep_storageE@@GLIBCXX_3.4

My application uses several .so (dynamic libraries) and when my aplication loads I notice that several of these .so files bind as follows (I set LD_DEBUG=all and ran my program):

 28596: binding file /home/bbazso/usr/local/lib/mydynamiclib.so [0] to /usr/lib64/libstdc++.so.6 [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]
 28596: binding file /home/bbazso/usr/local/lib/mydynamiclib.so [0] to /home/bbazso/workspace/mytestapplication [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]
 28596: binding file /home/bbazso/workspace/mytestapplication [0] to /usr/lib64/libstdc++.so.6 [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]**

But I also noticed that one of my .so only binds as follows:

  28087:    binding file /home/bbazso/usr/local/lib/anotherdynamiclib.so [0] to /usr/lib64/libstdc++.so.6 [0]: normal symbol `_ZNSs4_Rep20_S_empty_rep_storageE' [GLIBCXX_3.4]

but never binds to the binary (mytestapplication) as shown above for the mydynamiclib.so.

So I was wondering what this actually means? Does this mean that anotherdynamiclib.so will use a different symbol for the empty string above than the rest of the application? I guess what I'm really asking is how does symbol binding work in the context of the example above?

Thanks!

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

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

发布评论

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

评论(2

意中人 2024-08-30 00:18:18

恐怕我不完全理解你的问题,但你可能会在 Ulrich Drepper 的一篇名为 如何编写共享库。这是我所知道的有关如何在 Linux 中使用 elf 可执行文件的符号的最好的信息。

I am afraid I don't fully understand your question but you might find an answer in a Ulrich Drepper paper called How to write shared libraries. It is the best I know regarding how symbols with elf executables in Linux.

放飞的风筝 2024-08-30 00:18:18

我很难解析你的问题。如果您包含一小段正在编译的示例代码,它可能会有所帮助。

<块引用>

我注意到上述符号确实包含在二进制文件中,如下所示
这取决于你所说的“包含在”是什么意思。符号表中有该符号的条目,但它是外部符号,而不是应用程序“导出”的符号。请注意名称中的@@GLIBCXX_3.4。该符号的实际位置位于 GLIBCXX 中(看起来它实际上解析为 libstdc++)。

从您的输出来看, nm 似乎并未实际显示 ST_BIND 信息(El32_Sym/Elf64_Sym 结构中 st_info 字段的一部分)。这是确定链接器如何处理符号绑定的信息。你能跑吗
readelf -s 你的名字
它将显示绑定。

I have difficulty parsing your question. If you included a small snippet of example code that you are compiling it might help.

I notice that the aforementioned symbol is indeed contained within the binary as follows
It depends what you mean by "contained in". There is an entry for that symbol in the symbol table, but it is an external symbol, not a symbol that your application is "exporting". Note the @@GLIBCXX_3.4 in the name. The actual location of the symbol is in GLIBCXX (which looks like it actually resolves to libstdc++).

From your output, nm does not appear to actually show the ST_BIND information (part of the st_info field in the El32_Sym/Elf64_Sym structures). This is the information that determines how the linker treats the binding of the symbol. Can you run
readelf -s YOURBINNAME
It will show the bindings.

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