简体   繁体   English

链接动态共享库时ld会做什么?

[英]What does ld do when linking against dynamic shared library?

When linking an application against a dynamic shared library such as in 在将应用程序与动态共享库(例如in)链接时

gcc -o myprog myprog.o -lmylib

I know the linker ( ld on my Linux) use the -l option to store in the produced myprog ELF executable file the name of the library ( mylib in this case) that will be used at load and link time (both when the program will be started if we ignore lazy dynamic linking). 我知道链接器(在我的Linux上为ld )使用-l选项在生成的myprog ELF可执行文件中存储库的名称(在本例中为mylib ),该库的名称将在加载和链接时使用(在程序执行时如果我们忽略懒惰的动态链接,则可以开始)。 I am wondering what are the other jobs perform by ld (I am only speaking of the static linking step done at compilation time) regarding the dynamic shared library ? 我想知道ld的其他工作(关于编译共享共享库的静态链接步骤)是关于动态共享库的吗?

  • ld must checks for undefined symbol existence in provided dynamic shared libraries ld必须检查提供的动态共享库中是否存在未定义的符号
  • any other stuff ? 还有其他东西吗?

Moreover, I will be interested on pointers you are using (books, online documentation) regarding ELF format and dynamic linking and loading processes. 此外,我将对您使用的有关ELF格式以及动态链接和加载过程的指针(书籍,在线文档)感兴趣。

While you hit the most obvious things ld needs to do when linking to ELF shared libraries, there are a few more you missed. 当您遇到链接到ELF共享库时ld需要做的最明显的事情时,还有更多您错过了。 I'll re-state the ones you mentioned and add some more: 我将重申您提到的内容并添加更多内容:

  1. Ensuring that all undefined symbols are resolved (unless the output is a shared library itself, in which case undefined symbols are valid). 确保解析所有未定义的符号(除非输出本身是共享库,在这种情况下未定义的符号有效)。

  2. Storing a reference to the library in a DT_NEEDED record of the _DYNAMIC object of the output file. 在输出文件的_DYNAMIC对象的DT_NEEDED记录中存储对该库的引用。

  3. If the output is not position-independent and references objects (in the sense of data, as opposed to functions) in the shared library, generating a copy relocation to copy the original image of the object into the main program's data segment at load time, and the proper symbol table entry so that references to the object in the shared library itself get resolved to the new copy in the main program, rather than the original copy in the library. 如果输出不是与位置无关的,并且在共享库中引用了对象(就数据而言,相对于函数而言),则生成复制重定位以在加载时将对象的原始图像复制到主程序的数据段中,以及正确的符号表条目,以便将对共享库中对象的引用解析为主程序中的新副本,而不是库中的原始副本。

  4. Generating PLT thunks for the destination of each function call in the output that's not resolved at ld -time to a definition in the output. 为输出中每个函数调用的目标生成PLT thunk,这些输出在ld -time尚未解析为输出中的定义。

These are the tasks I can think of that are specific to use of shared libraries, and of course don't include all the work that the linker already does which would be the same as for static linking. 这些是我可以想到的特定于共享库使用的任务,并且当然不包括链接器已经完成的所有工作,这些工作与静态链接相同。 One way to think of what ld does with dynamic linking is that it takes object files with a huge repertoire of relocation types (representing anything the compiler or assembler can produce) and resolves all but a small number of them (for static linking, that number would be zero), where all of the remaining relocations fit into a much more limited set of types resolvable by the dynamic linker at load time. 思考ld对动态链接的作用的一种方法是,它使用具有大量重定位类型(表示编译器或汇编器可以产生的任何内容)的目标文件,并解析除少数以外的所有文件(对于静态链接而言,该数量将为零),其中所有剩余的重定位都适合于动态链接器在加载时可解析的类型更为有限的一组集合。

One important step is the creation of a dynamic symbol table , which the runtime linker ld.so can use to link the executable against the library at runtime. 重要的一步是创建动态符号表 ,运行时链接程序ld.so可以使用该符号表在运行时将可执行文件链接到库。 It will also write the dynamic relocation table to note which machine code locations need to be changed to point to dynamically linked symbols. 它还将写入动态重定位表,以指出需要更改哪些机器代码位置以指向动态链接的符号。 To see details: 要查看详细信息:

objdump -T myprog
objdump -R myprog

Also note that the string written to the executable will actually be the SONAME of the library, which might be something like mylib.so.0 . 还要注意,写入可执行文件的字符串实际上是库的SONAME ,可能类似于mylib.so.0 This will ensure that even when you install a newer and incompatible mylib.so.1.42 at some later point, the executable will use the compatible ABI version 0 instead. 这将确保即使在以后安装较新mylib.so.1.42兼容的mylib.so.1.42时,该可执行文件也将使用兼容的ABI版本0。 For details: 有关详细信息:

ldd myprog

Of course, the linker will also link your object files against one another, but since it does that even in the absence of a dynamic shared library, I take it that you are not interested in this part of its operation. 当然,链接器还会将您的目标文件相互链接,但是由于即使没有动态共享库,链接器也会这样做,因此我认为您对此部分操作不感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM