Quiz Entry - updated: 2026.07.10
What does "position-independent code" (PIC) mean and why is it needed?
Position-independent code runs correctly at any load address (via PC-relative addressing plus the GOT/PLT) — which shared libraries need, since each process may map them at a different address.
Why it's needed for shared libraries:
- Multiple processes share the same library code in physical memory
- Each process may map it to a different virtual address
- Code must work at any address
How PIC works:
- No absolute addresses in code or data references
- Uses PC-relative addressing for local symbols
- Uses Global Offset Table (GOT) for external symbols
- Uses Procedure Linkage Table (PLT) for function calls
Compile with PIC:
$ gcc -fPIC -shared -o libfoo.so foo.c
Non-PIC code problem:
# Absolute address - won't work if loaded elsewhere!
mov $0x4030, %rax
PIC code solution:
# PC-relative - works anywhere!
lea 0x1234(%rip), %rax
Trade-off: PIC has slight runtime overhead (extra indirection through GOT), but enables sharing and ASLR security.
Go deeper:
Position-independent code (Wikipedia) — PC-relative addressing plus the GOT/PLT indirection.
Eli Bendersky — PIC in shared libraries — the mechanics, from source to disassembly.