Home > Back-end >  Understanding GOT (Global Offset Table) and PLT?
Understanding GOT (Global Offset Table) and PLT?

Time:10-08

Before writing this question I wanted to highlight that I did my own research for Weeks read tens of articles but still this problem wasn't solved and the explanations I got don't make sense at all (maybe because I'm new to linking world). So I hope someone can provide simply yet very detailed answer.

I know that GOT (Global Offset Table) helps us resolve global symbols in dynamic linking which are referenced from another. Plus I read: "Each shared library has its own GOT"

  1. But that is problematic, what if 2 programs use same shared library? both will have same values for global variables which shouldn't be the case.

  2. As to my MAIN question: If I don't want to use lazy binding then why we need PLT at all, why not just use normal GOT as with variables?

CodePudding user response:

(2) - that's exactly what gcc -fno-plt does; using call puts@gotpcrel(%rip) which references the normal GOT entry, not the part of the GOT that's updated by PLT stubs.
See x86_64: Is it possible to "in-line substitute" PLT/GOT references?


(1) "Each shared library has its own GOT" means as opposed to having one per process. It's not saying that there's only one GOT for the library in shared memory that every process using the library maps.

Remember that Unix-like OSes (like all modern mainstream OSes) use virtual memory to isolate processes from each other, so it normally goes without saying that every process has its own independent copy of read/write data.

Of course global variables like errno or environ aren't shared between processes using the same library, that would break things so you can rule out that interpretation. (As well as being not what dynamic linking is doing if you strace /bin/ls)

  • Related