Relocations, PLT, GOT and Dynamic Linking
When a program calls puts, the compiler doesn't know where puts will be: the C library is loaded at a random address chosen when the program starts. The gap is closed by relocations (notes that say "patch this place once the address is known"), and for calls to shared libraries by two small tables, the PLT and the GOT. Every call to a library function in a disassembly goes through them, so reading call puts@plt correctly is basic literacy. They also matter for security: the GOT is a table of function pointers, a classic target for memory-corruption exploits, and RELRO exists to protect it.
Definitions
- Relocation: an entry saying "at offset X, write the address of symbol S (plus addend A), computed this way (type T)". Object files (
.o) are full of them; the static linker resolves most of them and leaves the rest as dynamic relocations for the loader. - Relocation types you'll meet on x86-64:
R_X86_64_PC32(32-bit PC-relative reference, e.g. to a string),R_X86_64_PLT32(call to a function, through the PLT if needed),R_X86_64_RELATIVE(add the load address: PIE pointers),R_X86_64_GLOB_DAT(fill a GOT slot with a symbol's address),R_X86_64_JUMP_SLOT(fill a PLT's GOT slot). - GOT (Global Offset Table,
.got/.got.plt): an array of addresses filled in by the dynamic loader. Code reaches imported data and functions through it. - PLT (Procedure Linkage Table,
.plt/.plt.sec): tiny stubs, one per imported function.call puts@pltlands on a stub that doesjmp [GOT slot of puts]. - Lazy binding: the GOT slot initially points back into the PLT, to code that asks the dynamic loader to resolve the symbol on the first call.
BIND_NOW(-z now) resolves everything at start-up instead. - RELRO (
-z relro): after relocation, the loader makes the GOT region read-only. Full RELRO =-z relro -z now: everything resolved at start, then the whole GOT is read-only. Partial RELRO leaves.got.pltwritable for lazy binding.
From object file to executable
The session compiles a two-call program. In the object file, the calls and the string references are placeholders with relocations; in the linked program, only dynamic relocations remain.
cat > hi.c <<'SRC'
#include <stdio.h>
int main(void) {
puts("first");
puts("second");
return 0;
}
SRC
gcc -O1 -c hi.c -o hi.o
echo "== object file: placeholders + relocations"
objdump -dr -M intel --no-show-raw-insn hi.o | grep -E '^\s+[0-9a-f]+:|R_X86' | sed 's/^ *//'
gcc -O1 hi.c -o hi
echo "== executable: dynamic relocations by type"
readelf -rW hi | awk '/R_X86_64/{print $3}' | sort | uniq -c
echo "== the PLT stub for puts"
objdump -d -M intel --no-show-raw-insn -j .plt.sec hi | grep -E '^\s+[0-9a-f]+:' | sed 's/^ *//'
readelf -dW hi | grep -E '\((FLAGS|FLAGS_1)\)'== object file: placeholders + relocations
0: endbr64
4: sub rsp,0x8
8: lea rdi,[rip+0x0] # f <main+0xf>
b: R_X86_64_PC32 .LC0-0x4
f: call 14 <main+0x14>
10: R_X86_64_PLT32 puts-0x4
14: lea rdi,[rip+0x0] # 1b <main+0x1b>
17: R_X86_64_PC32 .LC1-0x4
1b: call 20 <main+0x20>
1c: R_X86_64_PLT32 puts-0x4
20: mov eax,0x0
25: add rsp,0x8
29: ret
== executable: dynamic relocations by type
5 R_X86_64_GLOB_DAT
1 R_X86_64_JUMP_SLOT
3 R_X86_64_RELATIVE
== the PLT stub for puts
1050: endbr64
1054: jmp QWORD PTR [rip+0x2f76] # 3fd0 <puts@GLIBC_2.2.5>
105a: nop WORD PTR [rax+rax*1+0x0]
0x000000000000001e (FLAGS) BIND_NOW
0x000000006ffffffb (FLAGS_1) Flags: NOW PIE
In the object file, lea rdi, [rip+0x0] and call with a zero displacement are holes; the relocation lines beneath them say what goes there (.LC0, the string, and puts, through the PLT; the -0x4 addend accounts for the displacement being relative to the end of the instruction). In the executable, the remaining dynamic relocations are a few RELATIVE ones (pointers inside the PIE that need the load address added), GLOB_DAT slots and one JUMP_SLOT for puts. The PLT stub is just endbr64 plus jmp QWORD PTR [rip+...], an indirect jump through the GOT slot at 0x3fd0. The dynamic flags BIND_NOW and NOW PIE show Ubuntu's default: everything bound at start-up.
Watching lazy binding
To see the GOT change, the session builds the same program with lazy binding and no RELRO (-z lazy -z norelro; lazy binding was the usual default before distributions switched to -z now), stops in gdb before and after the first call, and asks what the puts GOT slot points to. gdb names the slot puts@got.plt.
cat > hi.c <<'SRC'
#include <stdio.h>
int main(void) {
puts("first");
puts("second");
return 0;
}
SRC
gcc -O0 -g -Wl,-z,lazy -Wl,-z,norelro hi.c -o hi-lazy
gcc -O0 -g hi.c -o hi-now
for f in hi-lazy hi-now; do
echo "== $f: $(checksec --output=csv --file=$f | cut -d, -f1)"
gdb -q -batch -ex 'set disassembly-flavor intel' -ex 'break 3' -ex 'break 4' -ex 'run' \
-ex "x/3i *(long *)&'puts@got.plt'" -ex 'continue' \
-ex "info symbol *(long *)&'puts@got.plt'" ./$f 2>&1 \
| grep -E '^ *0x|in section' | sed -E 's/^ *0x[0-9a-f]+/0xADDR/'
done== hi-lazy: No RELRO
0xADDR: endbr64
0xADDR: push 0x0
0xADDR: jmp 0x5aeeacb98020
puts in section .text of /lib/x86_64-linux-gnu/libc.so.6
== hi-now: Full RELRO
0xADDR <__GI__IO_puts>: endbr64
0xADDR <__GI__IO_puts+4>: push rbp
0xADDR <__GI__IO_puts+5>: mov rbp,rsp
puts in section .text of /lib/x86_64-linux-gnu/libc.so.6
With lazy binding, before the first call the slot points back into the binary's own .plt: a stub that pushes the relocation index (push 0x0) and jumps to the resolver. After puts("first"), the slot holds the real address of puts in libc. With BIND_NOW the slot already holds the libc address at the first line of main, and full RELRO has made it read-only.
Why the GOT matters for security, and how attacks on it are detected
The GOT is a table of function pointers that the program trusts blindly. An attacker with a memory-write bug (an out-of-bounds write, a format-string %n) who can overwrite the puts slot redirects the next puts call anywhere. That is the classic GOT overwrite, taught as a concept in Memory Safety.
- Detection in binaries:
checksecorreadelf -lW(GNU_RELRO) plusreadelf -d(BIND_NOW) show whether a binary is protected; a fleet scan for "No RELRO" or "Partial RELRO" finds the weak ones. - Detection at run time: GOT entries pointing outside the expected library's code (into a heap, a stack or an anonymous mapping) are a sign of hooking or exploitation; memory forensics tools check exactly this.
- Legitimate hooking uses the same mechanism:
LD_PRELOADinterposition and some profilers redirect GOT or symbol resolution on purpose (Instrumentation).
How it is prevented
- Link with full RELRO:
-Wl,-z,relro,-z,now(the GCC default on Ubuntu, and the package-build default on Fedora and other distributions; check plain upstream toolchains and cross-compilers). - PIE plus ASLR so the GOT's address isn't known in advance, and fix the memory-write bug itself.
- On Windows the equivalent table is the IAT (PE Imports); Control Flow Guard and read-only IATs play the same role.
Pitfalls
call puts@pltis not a call into libc; it calls the stub in your binary. The realputsis wherever the GOT says..plt.secexists because of Intel CET (-fcf-protection): the stubs are split into two tables so every target starts withendbr64.- Static binaries have no PLT/GOT for libc (no dynamic linking), but may still have a small GOT for IFUNC resolvers.
- Addresses in object files are all zero: always read relocations (
objdump -dr) with an unlinked.o.
Practice
1. In the object file, why is the addend of the puts relocation -0x4?
The 32-bit displacement of call is relative to the end of the instruction, which is 4 bytes after the start of the displacement field; the linker computes S + A - P with A = -4.
2. A binary has GNU_RELRO but no BIND_NOW. What does checksec report and what stays writable?
Partial RELRO: .got (GLOB_DAT slots) becomes read-only, but .got.plt stays writable because lazy binding writes to it on each first call.
3. With lazy binding, how many times does the dynamic loader's resolver run for the two calls to puts?
Once, on the first call; it patches the GOT slot, and the second call jumps directly to libc.
Test yourself
What does the PLT stub for puts do?
- Contains a copy of puts
- Jumps indirectly through the GOT slot for puts
- Calls the kernel
- Prints the string
Which relocation type fills the GOT slot of a function called through the PLT?
- R_X86_64_RELATIVE
- R_X86_64_JUMP_SLOT
- R_X86_64_PC32
- R_X86_64_PLT32 in the executable
What does full RELRO protect against?
- Stack buffer overflows
- Overwriting GOT entries after start-up, because the GOT is resolved at start and made read-only
- SQL injection
- Reverse engineering
Before the first call with lazy binding, the puts GOT slot points to:
- puts in libc
- A stub in the binary's .plt that pushes the relocation index and jumps to the resolver
- NULL
- main