了解 ldd 输出

发布于 2024-10-29 19:08:56 字数 250 浏览 4 评论 0原文

ldd 如何知道它依赖于 libc.so.6 ,而不是 libc.so.5libc.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 技术交流群。

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

发布评论

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

评论(2

丶情人眼里出诗心の 2024-11-05 19:08:56

它记录在应用程序二进制文件本身内部(在编译时指定,更准确地说是在链接步骤中,使用 ld 完成):(

$ readelf -d /bin/echo

Dynamic section at offset 0x5f1c contains 21 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
...

还有一些附加列说明 elf 如何在动态部分中存储信息。但是您可以看到 libc.so.6 被硬编码为 .6 后缀,因为 SONAME)

或者甚至不了解 ELF 文件格式:

$ strings /bin/echo |grep libc.so
libc.so.6

要查找链接器如何查找库(在编译的最后一步完成),请使用 gcc 选项 -Wl,- -verbose (这要求 gcc 将选项 --verbose 传递给 ld):

$ gcc a.c -Wl,--verbose

...
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.so failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.a failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.so failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.a failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/../../../libc.so succeeded
opened script file /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/../../../libc.so
opened script file /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/../../../libc.so
attempt to open /lib/libc.so.6 succeeded
/lib/libc.so.6

链接器不知道有关 .digit 的任何信息后缀,它只是迭代所有试图打开 libLIBNAME.solibLIBNAME.a 的库搜索目录,其中 LIBNAME 是 -l 之后的字符串选项。 (默认添加 -lc 选项)。

第一个成功的是 /usr/lib/libc.so 它本身不是一个库,而是一个链接器脚本(文本文件)。以下是典型的 libc.so 脚本的内容:

$ cat /usr/lib/libc.so
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf32-i386)
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a  AS_NEEDED ( /lib/ld-linux.so.2 ) )

因此,脚本 /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):

$ readelf -d /bin/echo

Dynamic section at offset 0x5f1c contains 21 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
...

(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:

$ strings /bin/echo |grep libc.so
libc.so.6

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 to ld):

$ gcc a.c -Wl,--verbose

...
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.so failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.a failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.so failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/libc.a failed
attempt to open /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/../../../libc.so succeeded
opened script file /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/../../../libc.so
opened script file /usr/lib/gcc/i686-pc-linux-gnu/4.4.4/../../../libc.so
attempt to open /lib/libc.so.6 succeeded
/lib/libc.so.6

Linker doesn't know anything about .digit suffix, it just iterate over all library search directories trying to open libLIBNAME.so and libLIBNAME.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 typical libc.so script:

$ cat /usr/lib/libc.so
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf32-i386)
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a  AS_NEEDED ( /lib/ld-linux.so.2 ) )

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 like lib___.so.3.4.5 and there is SONAME field filled in lib___.so.3.4.5 which says to ld link not to lib___.so but to lib___.so.3.4 which is another symlink to lib___.so.3.4.5. The .3.4 name will be recorded in NEEDED field of binary.

满地尘埃落定 2024-11-05 19:08:56

http://www.sco.com/developers/gabi/latest /ch5.dynamic.html#dynamic_section

具有各个动态标签的含义。 1 表示它是 DT_NEEDED 标记,这意味着在这种情况下,该

typedef struct {
    Elf32_Sword d_tag;
    union {
        Elf32_Word  d_val;
        Elf32_Addr  d_ptr;
    } d_un;
} Elf32_Dyn;

结构具有有效的 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

typedef struct {
    Elf32_Sword d_tag;
    union {
        Elf32_Word  d_val;
        Elf32_Addr  d_ptr;
    } d_un;
} Elf32_Dyn;

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.

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