Dynamic Linking & the GOT/PLT
Linking, Loading & Binary Formats · OS Core Internals track
6 min read
In this module
Why Dynamic Linking?
Static linking copies every library function into the binary. Fifty programmes on your system all use printf? That's fifty copies of the same code in memory. Dynamic linking fixes this: shared libraries (.so files on Linux) are loaded once into physical memory and mapped into every process that needs them.
But dynamic linking introduces a problem. At compile time, the binary doesn't know where a shared library will be loaded in memory. The addresses of printf, malloc, and every other library function are unknown until the programme actually runs. The GOT and PLT are the mechanisms that solve this.
The Global Offset Table (GOT)
The Global Offset Table is a writable array of pointers in the process's data segment. Each entry holds the runtime address of a dynamically linked symbol: a function or global variable defined in a shared library.
When code needs to call printf, it doesn't jump to a hardcoded address. It reads the address from the GOT entry for printf and jumps there. The dynamic linker fills in those GOT entries with the correct addresses after loading the shared libraries.
The GOT stores the resolved runtime addresses of dynamically linked symbols. The PLT provides a trampoline mechanism that enables lazy binding: function addresses get resolved on their first call rather than at load time. Together, they bridge the gap between compile-time unknowns and runtime addresses.
The Procedure Linkage Table (PLT)
The PLT is a series of small code stubs, one per dynamically linked function. Instead of calling through the GOT directly, code calls through a PLT entry. Here's what happens on the first call:
First Call (Lazy Binding)
- The code calls
printf@plt(the PLT stub forprintf) - The PLT stub jumps to the address stored in
printf's GOT entry - On the first call, the GOT entry doesn't contain
printf's address. It points back into the PLT stub - The PLT stub pushes an identifier for
printfonto the stack and jumps to the dynamic linker's resolver (_dl_runtime_resolve) - The resolver looks up
printf's actual address, writes it into the GOT entry, and jumps toprintf
Subsequent Calls
- The code calls
printf@pltagain - The PLT stub jumps to the GOT entry, which now contains
printf's real address - Execution goes straight to
printf, no resolver involved
Lazy binding spreads the cost of symbol resolution across the programme's execution. Functions that are never called are never resolved.
The Dynamic Linker: ld-linux.so
On Linux, the dynamic linker is /lib64/ld-linux-x86-64.so.2 (or a similar path). It's named in the ELF binary's PT_INTERP program header. When you run a dynamically linked programme, the kernel doesn't jump straight to the programme's entry point. It loads the dynamic linker first, which then:
- Loads all shared libraries listed in the
PT_DYNAMICsegment - Performs relocations (filling in GOT entries for global data, resolving non-lazy symbols)
- Transfers control to the programme's entry point
# See what the dynamic linker loadsLD_DEBUG=libs ./my_program
# Trace symbol resolutionLD_DEBUG=symbols ./my_programLD_PRELOAD: Intercepting Library Calls
The LD_PRELOAD environment variable tells the dynamic linker to load a chosen shared library before any others. Functions defined in the preloaded library shadow functions of the same name in subsequent libraries, libc included.
# Preload a custom library that overrides mallocLD_PRELOAD=./my_malloc.so ./my_programIt gets used legitimately for debugging (memory leak detectors, for instance) and maliciously for function hooking and rootkit installation. It works because the dynamic linker searches libraries in order and grabs the first matching symbol it finds.
GOT Overwrite Attacks
Because the GOT is a writable data region full of function pointers, it's a prime target for exploitation. With an arbitrary write primitive, overwriting a GOT entry redirects the next call to that function wherever the attacker chooses.
For example, overwriting the GOT entry for exit() with the address of system() means the next time the program calls exit(user_input) it actually calls system(user_input). Hello, arbitrary command execution.
Modern mitigations include RELRO (Relocation Read-Only):
- Partial RELRO (
-z relro): once the dynamic linker has applied relocations, it makes a region calledPT_GNU_RELROread-only. That region covers.got, which holds pointers resolved at load time (global variables, and functions whose address is taken)..got.plt, the part holding lazily bound function pointers, sits outside it and stays writable so lazy binding can keep filling it in. So with partial RELRO the classic GOT overwrite still works. - Full RELRO (
-z relro -z now): every symbol is resolved at load time (no lazy binding), the lazy-binding slots join the read-only region, and the whole GOT is read-only beforemainruns. GOT overwrites stop working, at the cost of resolving every symbol up front.
Full RELRO is now standard. Ubuntu, Fedora and Debian build their packages as PIE with full RELRO, and Ubuntu's gcc does the same for your own code by default. A GOT overwrite remains a real technique against older binaries, embedded firmware and anything built with lazy binding, but on a current desktop binary expect checksec to report Full RELRO and plan around it.
# Check RELRO status of a binarychecksec --file=./my_program
# The RELRO column for three builds of the same program:gcc demo.c -o demo -Wl,-z,norelro # RELRO: No RELROgcc demo.c -o demo -Wl,-z,relro # RELRO: Partial RELROgcc demo.c -o demo -Wl,-z,relro,-z,now # RELRO: Full RELROWhy It Matters
The GOT/PLT mechanism sits at the heart of both dynamic linking and exploitation. Lazy binding explains why GOT entries are writable in the first place (and therefore attackable). LD_PRELOAD explains how function hooking and interposition work. These are fundamental building blocks for binary exploitation, malware analysis, and defensive tooling.