Quiz Entry - updated: 2026.07.10
What is position-independent code (PIC) and why is it used?
PIC is code that runs correctly no matter what address it's loaded at, because it never hard-codes absolute addresses.
* Position-independent code avoids absolute addresses: nearby data is RIP-relative, external data goes through the GOT, external calls through the PLT — enabling ASLR. *
A shared library can be mapped to a different address in every process, so its code can't assume "my data is at 0x4000." PIC solves this by computing addresses relative to the current instruction instead.
Why it's needed:
- Shared libraries must load at arbitrary addresses (and be shared between processes)
- ASLR (Address Space Layout Randomization) deliberately randomizes load addresses as a security defense, so nothing can be hard-coded
How it works:
- RIP-relative addressing reaches nearby data:
mov global_var(%rip), %eaxcomputes the address from the program counter - A Global Offset Table (GOT) holds addresses of external data
- A Procedure Linkage Table (PLT) handles calls to external functions
mov global_var, %eax # non-PIC: hard-coded absolute address
mov global_var(%rip),%eax # PIC: address computed from %rip
You build it with gcc -fPIC -shared lib.c -o lib.so.
Go deeper:
Position-independent code (Wikipedia) — RIP-relative addressing, GOT/PLT, shared libraries and ASLR.