了解 ldd 输出
ldd
如何知道它依赖于 libc.so.6
,而不是 libc.so.5
或 libc.so.7< /代码>?
libc.so.6 => /lib64/libc.so.6 (0x00000034f4000000)
/lib64/ld-linux-x86-64.so.2 (0x00000034f3c00000)
How does ldd
knows it's depending on libc.so.6
,not libc.so.5
or libc.so.7
?
libc.so.6 => /lib64/libc.so.6 (0x00000034f4000000)
/lib64/ld-linux-x86-64.so.2 (0x00000034f3c00000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它记录在应用程序二进制文件本身内部(在编译时指定,更准确地说是在链接步骤中,使用 ld 完成):(
还有一些附加列说明 elf 如何在动态部分中存储信息。但是您可以看到 libc.so.6 被硬编码为
.6
后缀,因为 SONAME)或者甚至不了解 ELF 文件格式:
要查找链接器如何查找库(在编译的最后一步完成),请使用
gcc
选项-Wl,- -verbose
(这要求 gcc 将选项--verbose
传递给ld
):链接器不知道有关
.digit
的任何信息后缀,它只是迭代所有试图打开libLIBNAME.so
和libLIBNAME.a
的库搜索目录,其中 LIBNAME 是-l
之后的字符串选项。 (默认添加-lc
选项)。第一个成功的是
/usr/lib/libc.so
它本身不是一个库,而是一个链接器脚本(文本文件)。以下是典型的libc.so
脚本的内容:因此,脚本
/usr/lib/libc.so
的发现早于实际的库,并且该脚本说明,什么文件将被链接,在本例中为libc.so.6
。在更常见的情况下,
lib___.so
是到诸如lib___.so.3.4.5
之类的某个版本的符号链接,并且lib___.so.3.4 中填充了 SONAME 字段.5
表示ld
链接不是到lib___.so
而是链接到lib___.so.3.4
,这是另一个到lib___.so.3.4 的符号链接代码>lib___.so.3.4.5
。.3.4
名称将记录在二进制文件的 NEEDED 字段中。It is recorded inside application binary itself (specified at compile time, more exactly at link step, done with
ld
):(there are some additional columns for how elf does store information in dynamic section. but you can see that libc.so.6 is hardcoded with
.6
suffix because of SONAME)or even without any knowledge of ELF file format:
To find, how does linker find a library (it is done at final step of compilation), use
gcc
option-Wl,--verbose
(this asks gcc to pass option--verbose
told
):Linker doesn't know anything about
.digit
suffix, it just iterate over all library search directories trying to openlibLIBNAME.so
andlibLIBNAME.a
, where LIBNAME is a string after-l
option. (-lc
option is added by default).First success is
/usr/lib/libc.so
which itself is not a library, but a linker script (text file). Here is content from typicallibc.so
script:So, script
/usr/lib/libc.so
is found earlier than actual library, and this script says, what file will be linked,libc.so.6
in this case.In more common case,
lib___.so
is symlink to some version likelib___.so.3.4.5
and there is SONAME field filled inlib___.so.3.4.5
which says told
link not tolib___.so
but tolib___.so.3.4
which is another symlink tolib___.so.3.4.5
. The.3.4
name will be recorded in NEEDED field of binary.http://www.sco.com/developers/gabi/latest /ch5.dynamic.html#dynamic_section
具有各个动态标签的含义。 1 表示它是 DT_NEEDED 标记,这意味着在这种情况下,该
结构具有有效的 d_val 联合,并在 DT_STRTAB 表中查找该联合成员指定的偏移量,以查找该二进制文件/SO 所依赖的库的名称。
http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#dynamic_section
Has the meaning of each dynamic tags. The 1 indicates it is a DT_NEEDED tag meaning in this case the
structure has d_val union valid and look up at an offset specified by thi union member in DT_STRTAB table to find the name of library that this binary/SO depends on.