Reading — step 1 of 5
Read
~1 min readProduction
Relocations
A relocation is an instruction to the linker/loader: "patch this location with the address of this symbol (plus offset)."
Types (x86-64):
- R_X86_64_64: 64-bit absolute address.
- R_X86_64_PC32: 32-bit signed PC-relative offset.
- R_X86_64_GOT32, R_X86_64_GOTPCREL: GOT entry / PC-rel ref to GOT.
- R_X86_64_PLT32: PC-rel reference to PLT.
- R_X86_64_RELATIVE: image base + addend (no symbol).
- R_X86_64_GLOB_DAT: address of symbol → GOT[i].
- R_X86_64_JUMP_SLOT: address of symbol → PLT[i].
- R_X86_64_TPOFF / DTPOFF: TLS offsets.
A relocation entry:
struct Rela {
uint64_t r_offset; // where to patch
uint64_t r_info; // symbol index + reloc type
int64_t r_addend; // value to add
};
For static linking:
- Relocations resolve at link time.
- Linker patches the executable.
- Stripped from final binary.
For dynamic linking:
- Some relocations resolve at link time (within .so).
- Others (GLOB_DAT, JUMP_SLOT) resolve at load time / call time.
Position-independent code:
- Use only PC-relative or GOT-indirect references.
- Loader can place anywhere; no patching of code (only GOT).
- Required for shared libraries; common for executables (PIE for ASLR).
Example:
mov rax, [rip + foo@GOTPCREL] ; load GOT entry for foo
mov rbx, [rax] ; deref
vs non-PIC absolute:
mov rax, foo ; absolute address
Toolchain flags:
-fPIC: position-independent code.-pie: position-independent executable.-Wl,-z,now: full RELRO.-Wl,-z,relro: standard RELRO.
ASLR (Address Space Layout Randomization):
- Each load picks random base for executable + libraries + stack + heap.
- Defends against ROP / address-guessing attacks.
- Requires PIE binaries.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…