Symbol Resolution and Dynamic Linking

左心房为你撑大大i 提交于 2019-12-21 18:33:12

问题


I have been reading about the relocation and symbol resolution process and I have a few questions on the same.

So the whole process(of loading the exec) starts with exec(BA_OS) command. During exec(BA_OS), the system retrieves a path name from the PT_INTERP segment and creates the initial process image from the interpreter file’s segments. That is, instead of using the original executable file’s segment images, the system composes a memory image for the interpreter. It then is the interpreter’s responsibility to receive control from the system and provide an environment for the application program.

After that dynamic linker does the following(as long as LD_BIND_NOW has a non-null value):

  • Adding the executable file's memory segments to the process image;
  • Adding shared object memory segments to the process image;
  • Performing relocations for the executable file and its shared objects;
  • Closing the file descriptor that was used to read the executable file, if one was given to the dynamic linker;
  • Transferring control to the program, making it look as if the program had received control directly from exec(BA_OS).

So, my question now is

1. When are these shared objects loaded in memory?

The second step above states that linker adds shared object memory segments to the process image. Do all(and by all I mean shared object and their dependencies and dependencies of them and so on) the libraries are loaded at this point? Or the linker only creates a process image using dependencies and loads the library in physical memory later when required?

2. How dynamic linker get the address to patch the GOT entry of the symbol?

Below is what happens(or what I know what happens) when a function is called for the first time.

  • Jump to the PLT entry of our symbol.
  • Jump to the GOT entry of our symbol.
  • Jump back to the PLT entry and push an offset on the stack. That the offset is actually an Elf_Rel structure describing how to patch the symbol.
  • Jump to the PLT stub entry.
  • Push a pointer to a link_map structure(on another article this was a pointer to the Relocation table) in order for the linker to find in which library the symbol belongs to.
  • Call the dynamic linker.
  • Patch the GOT entry.

What does the dynamic linker do when it is called? How does it use the items on the stack (the offset and the pointer) to patch the GOT entry? Suppose there are a large number of .so files, then is there any order in which dynamic linker searches through those files?


I have just started learning about this stuff. Please correct me if my understanding is incorrect. If you know of any other good resources please let me know.

来源:https://stackoverflow.com/questions/50453228/symbol-resolution-and-dynamic-linking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!