Linux 中共享库的符号绑定如何工作
当使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕我不完全理解你的问题,但你可能会在 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.
我很难解析你的问题。如果您包含一小段正在编译的示例代码,它可能会有所帮助。
从您的输出来看, 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.
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.