23

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 Answers1

14

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.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Great! One final question. What would the GOT have to do differently in PIC code if there was no PLT? More specifically...Why would it be more bloated? –  Dec 10 '13 at 05:13
  • As long as it goes through the PLT, a call from PIC code usually looks identical to a call from non-PIC code, i.e. a single PC-relative call instruction. (It may have additional requirements that the GOT register already be loaded, though; this is arch-specific.) A call via the GOT without using the PLT would be the same as inlining the code that's normally in the PLT entry, which is typically 2-3 instructions to load the actual function address and jump to it. – R.. GitHub STOP HELPING ICE Dec 10 '13 at 05:18
  • Alright that's what I figured. I guess the bulk would be pretty substantial in the case that a lot of different functions are called so it's nice to have two different `layers' of calls for better understanding. Thanks you –  Dec 10 '13 at 05:22