What is the purpose of the procedure linkage table

2020-02-27 11:28发布

问题:

I'm curious why the Procedure Linkage Table exists. What purpose does it serve? Couldn't the assembly call the global offset table directly? What advantage does calling the PLT have over calling the Global Offset Table?

The whole process is still kind of confusing to me and I'm trying to figure out the in's and out's of dynamic linking so any help would be appreciated.

回答1:

For calls made from PIC code, you are correct that the PLT is not really needed. The compiler could just was well generate a GOT lookup and indirect call to the address obtained from the GOT. Using a PLT tends to make the code slightly more efficient though (at least, less size bloat per call) so it's generally used anyway.

Where the PLT is absolutely needed, however, is in non-PIC code that's dynamic linked. (Usually this occurs only in the main program; on many archs, non-PIC code is not even allowed/supported in shared libraries.) When the compiler generates non-PIC code for a function call, it has no way to know that the actual destination address will be resolved dynamically at runtime via the GOT. So it just generates an ordinary call instruction. The linker then is responsible, when it sees a call-type relocation for a symbol that's not resolved locally and that requires runtime linking, for generating a PLT entry that loads the address from the GOT and makes an indirect jump to it. This way, the original non-PIC function call code works unmodified.



标签: c linux binary x86