动态链接器在加载使用共享库的可执行文件时是如何链接的?

huangapple go评论65阅读模式
英文:

How is dynamic linker linked when executable using shared library is loaded?

问题

I'm reading about the process of dynamic linking currently, and I read that the dynamic linker is also a shared library (ld.linux.so). Then how is ld.linux.so linked?

Is it linked in a static way?

我目前正在阅读有关动态链接过程的内容,我了解到动态链接器也是一个共享库(ld.linux.so)。那么ld.linux.so是如何链接的呢?

它是以静态方式链接的吗?

英文:

I'm reading about the process of dynamic linking currently, and I read that the dynamic linker is also a shared library(ld.linux.so). Then how is ld.linux.so linked?

Is it linked in a static way?

答案1

得分: 1

是的。这是一个共享库,它本身不依赖其他库(也不能依赖其他库,因为没有东西可以解决这些依赖关系)-它必须完全自包含。

特别是,ld.so 包含了自己的许多 libc 例程的副本(mallocmmapstrcpy 等)。您可以使用 nm /lib64/ld-linux-x86-64.so.2 来查看它们(如果您的发行版没有从 ld-linux 中删除符号)。

此外,内核将此特殊的共享库 mmap 到随机地址,它必须在启动时进行自我重定位(因为再次,没有其他东西会执行这种重定位)。

在不允许 PC 相对数据寻址的平台上,这种自我重定位可能会相当复杂。

英文:

> Is it linked in a static way?

Yes. It's a shared library which itself has no dependencies on other libraries (and can't have any, since there is nothing that would resolve such dependencies) -- it must be entirely self-contained.

In particular, ld.so contains its own copies of many libc routines (malloc, mmap, strcpy, etc.). You can see them with nm /lib64/ld-linux-x86-64.so.2 (if your distribution doesn't strip symbols from ld-linux).

Further, this special shared library is mmaped by the kernel at random address, and it must relocate itself when it starts (since again, there is nothing else that would perform such relocation).

This self-relocation could be rather involved on platforms which don't allow PC-relative data addressing.

答案2

得分: -1

代码部分不需要翻译。以下是翻译好的内容:

当内核加载一个可执行文件时,它会查找 ELF 文件的 .interp 部分,缩写为“解释器”。对于动态链接的可执行文件,在构建时,静态链接器将其设置为 'ld-linux'。在运行时,内核加载此解释器,该解释器处理其他库的链接。

您可以使用 readelf -p .interp <executable> 命令来查看这一点。例如,在我的系统上运行 readelf -p .interp /bin/ls 输出如下:

部分 '.interp' 的字符串转储:
  [     0]  /lib64/ld-linux-x86-64.so.2

另请参阅 手册 以获取有关 ld-linux 的更多信息。

英文:

When the kernel loads an excecutable, it looks for the .interp section of the ELF file; short for interpreter. For dynamically linked executables this will be set to 'ld-linux' by the static linker during build time. During runtime, the kernel loads this interpreter which handles the linking of other libraries.

You can see this using readelf -p .interp &lt;executable&gt;. For example, on my system running readelf -p .interp /bin/ls outputs

String dump of section &#39;.interp&#39;:
  [     0]  /lib64/ld-linux-x86-64.so.2

See also the manual for ld-linux.

huangapple
  • 本文由 发表于 2023年2月24日 01:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定