LOGBOOK

HELP

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:

  1. No absolute addresses in code or data references
  2. Uses PC-relative addressing for local symbols
  3. Uses Global Offset Table (GOT) for external symbols
  4. 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:

From Quiz: REVE1 / Program Execution | Updated: Jul 10, 2026