PLT and GOT

Dynamic Linking

The Procedure Linkage Table (PLT) and Global Offset Table (GOT) are sections within an Executable and Linkable Format (ELF) file that play a significant role in dynamic linking.

The purpose of dynamic linking is to reduce the size of binaries by allowing them to rely on system libraries, such as the C standard library (libc), to provide the majority of their functionality.

For example, an ELF file does not include its own version of the 'printf' function compiled within it. Instead, it dynamically links to the 'printf' function of the system it is running on. In addition to smaller binary sizes, this also means that users can upgrade their libraries without having to download all the binaries again each time a new version is released.

How it works ?

The linking is performed through the cooperation of the Procedure Linkage Table (PLT) and the Global Offset Table (GOT).

When the 'printf' function, for example, is called in C and compiled as an ELF executable, it is not included as 'printf' in the file. Instead, it is compiled as 'printf@plt' :

   0x0804925b <+137>:   push   0x804a012
   0x08049260 <+142>:   call   0x8049040 <printf@plt>
   0x08049265 <+147>:   add    esp,0x10

The program does not know the actual location of 'printf', so it jumps to the 'printf@plt' entry instead. When this occurs, 'printf@plt' performs some specific actions.

  • If there isn't a GOT entry, it will resolve it and jump there.

  • If there is a GOT entry for printf, it jumps to the address stored there.

The GOT is a massive table of addresses; these addresses are the actual locations in memory of the library functions. printf@got, for example, will contain the address of printf in memory. When the PLT gets called, it reads the GOT address and redirects execution there. If the address is empty, it coordinates with the ld.so (also called the dynamic linker/loader) to get the function address and stores it in the GOT.

  • Calling the PLT address of a function is equivalent to calling the function itself

  • The GOT address contains addresses of functions in libraries, and the GOT is within the binary.

Read tables

Some tools permits to read the PLT and GOT tables :

Resources

Last updated