Skip to main content
Be Bitwise
x86 AssemblyLinking & C Interop

Position-Independent Code

Position-independent code (PIC) runs correctly no matter where the loader drops it in memory. It started life as a shared-library convenience, but today PIC is the foundation ASLR (Address Space Layout Randomisation) stands on — one of the loudest exploit mitigations on modern systems.

The Problem with Absolute Addresses

Consider this code that uses an absolute address:

nasm
section .data
msg db "Hello", 0
section .text
global _start
_start:
mov rsi, msg ; encoded as: mov rsi, 0x00402000 (fixed address)
; ...

By default, mov rsi, msg bakes the absolute address of msg straight into the instruction (something like 0x00402000). That only holds up if the program lands exactly where the linker said it would. The moment the OS loads it elsewhere — which is precisely ASLR's job — the instruction loads garbage.

RIP-Relative Addressing

The fix: RIP-relative addressing. Express memory addresses as offsets from the current instruction pointer, not as absolute values.

nasm
default rel ; tell NASM to use RIP-relative by default
section .data
msg db "Hello", 0
section .text
global _start
_start:
lea rsi, [msg] ; now encoded as: lea rsi, [rip + offset_to_msg]
; This works regardless of where the code is loaded!

default rel flips NASM into encoding memory references RIP-relative whenever possible. Skip the directive and you can still ask for it per instruction:

nasm
lea rsi, [rel msg] ; explicit RIP-relative addressing

At runtime, the CPU computes RIP + offset. The offset between the instruction and its target is fixed at link time. Code and data both shift together when ASLR randomises the base, so the math works out to the right address every time.

PIC Is Required for ASLR to Work

ASLR randomises where code and data land. That only buys you anything if the code can survive being placed anywhere. PIC uses RIP-relative addressing so it never bakes in a fixed location. No PIC, and the executable has to load at its linked address — meaning ASLR slides off it. That's exactly why PIE (Position-Independent Executable) binaries are now the default on every modern Linux distro.

LEA vs MOV for Address Loading

Under PIC, LEA (Load Effective Address) replaces MOV for grabbing addresses:

nasm
default rel
section .data
value dq 42
section .text
; WRONG for PIC: mov loads from a fixed absolute address
; mov rax, [value] ; this may use an absolute reference
; CORRECT for PIC: lea loads the address RIP-relatively
lea rax, [value] ; rax = address of value (RIP-relative)
mov rax, [rax] ; now dereference to get the value 42

In practice, once default rel is on, most addressing turns RIP-relative for free — mov rax, [value] quietly becomes mov rax, [rip + offset] under the hood. When you specifically want the address itself rather than the value, LEA is your instruction.

The GOT: Global Offset Table

A shared library reaching out to external symbols (ones living in another library or the main executable) can't use RIP-relative directly. The offset isn't known until load time. Enter the GOT (Global Offset Table) — a data section the dynamic linker populates when the program comes up.

nasm
; Accessing an external variable through the GOT:
; 1. Load the GOT entry (which contains the actual address)
; 2. Dereference to get the value
mov rax, [rel extern_var wrt ..got] ; load address from GOT
mov rax, [rax] ; dereference to get value

The GOT is just a table of pointers. The dynamic linker (ld.so) writes the real addresses in at load time, one entry per external symbol.

The PLT: Procedure Linkage Table

External function calls route through the PLT (Procedure Linkage Table). The PLT makes lazy binding possible: addresses get resolved on the first call, not up front at load time. Whether that actually happens depends on how the binary was linked, as you'll see at the end of this section.

nasm
; In NASM, call an external function in PIC through the PLT explicitly:
call printf wrt ..plt ; GAS spells this: call printf@PLT
; A disassembler shows it as a call to the stub:
; call <printf@plt>
; The PLT stub does (on first call):
; 1. Jump to the GOT entry for printf
; 2. GOT initially points back to the PLT resolver
; 3. Resolver calls the dynamic linker to find printf's real address
; 4. Dynamic linker updates the GOT entry with the real address
; 5. Subsequent calls jump directly to the real address

Lazy resolution pays off for programs that pull in many libraries. Functions you never call never get resolved, and startup gets cheaper for it. A plain call printf from NASM emits an ordinary PC-relative relocation instead, and when you link it into a PIE, ld rejects it with a "recompile with -fPIE" error, which is why the wrt ..plt form matters.

Here are the first two calls to puts side by side. The first takes the detour through the resolver, and the resolver's last act is to patch the GOT. The second call goes through the same stub and the same jmp and lands straight in libc:

Full RELRO (-z relro -z now) skips the first-call detour, and it's common: Fedora builds its packages this way, and Ubuntu's gcc has passed -z now whenever it links a PIE since 16.10. On a typical system binary the lazy path above never runs. The dynamic linker fills every GOT entry at load time and then makes the GOT read-only, which takes away a favourite target for an attacker's arbitrary write.

PIE: Position-Independent Executables

A PIE (Position-Independent Executable) is an executable built end-to-end with PIC. Modern Linux distros ship every program as PIE out of the box.

bash
# Build a PIE executable
nasm -f elf64 program.asm -o program.o
gcc program.o -o program -pie # PIE enabled (default on modern gcc)
# Build a non-PIE executable
gcc program.o -o program -no-pie # PIE disabled
# Check if a binary is PIE
readelf -h program | grep Type
# PIE: Type: DYN (Position-Independent Executable)
# Non-PIE: Type: EXEC (Executable file)

Inspecting PIC in Practice

Pop the disassembly open and the difference between PIC and non-PIC jumps out:

nasm
; Non-PIC (absolute addressing):
; 401020: 48 be 00 20 40 00 00 00 00 00 movabs rsi, 0x402000
; PIC (RIP-relative addressing):
; 401020: 48 8d 35 d9 0f 00 00 lea rsi, [rip+0xfd9]

The RIP-relative encoding clocks in at 7 bytes versus 10, and runs from anywhere. The absolute version solders a hardcoded address into the instruction stream, useful at exactly one load location.

Writing PIC-Correct Assembly

Here is a complete PIC program:

nasm
default rel ; RIP-relative addressing by default
section .data
msg db "Position-independent!", 10
msg_len equ $ - msg
section .text
global _start
_start:
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
lea rsi, [msg] ; RIP-relative: works at any address
mov rdx, msg_len ; constants do not need relocation
syscall
mov rax, 60
xor rdi, rdi
syscall

Viewing /proc/self/maps

For any running process, /proc/<pid>/maps reveals where each memory region landed, and /proc/self/maps is the reading process's own. Combine ASLR with PIE and these addresses shuffle on every launch. grep is a PIE on modern distributions, so it makes a handy test subject: it prints its own code mapping.

bash
# Run twice, compare output:
grep r-xp /proc/self/maps | head -1
# 5623a4102000-5623a4127000 r-xp 00002000 08:01 1835015 /usr/bin/grep
grep r-xp /proc/self/maps | head -1
# 563f12802000-563f12827000 r-xp 00002000 08:01 1835015 /usr/bin/grep
# Different base address each time! (Your exact numbers will differ.)

To look at the program from this lesson instead, it has to still be running when you read its maps file. The version above exits immediately, so add a blocking read from stdin before the exit, start it with ./program &, then cat /proc/$!/maps. Note that maps prints addresses as bare hex, with no 0x.

Security Perspective: PIC and ASLR

PIC is what gives ASLR its teeth. Here's the dance:

  1. The kernel drops the PIE executable at a random base address
  2. Every instruction uses RIP-relative addressing, so the code runs at any base
  3. The GOT and PLT do external symbol resolution against the live load address
  4. An attacker who wants to call a known function or read known data has to first leak the randomised base

When the code isn't position-independent, the executable has to load at its fixed linked address. ASLR can't budge it. The attacker always knows where the code is — and one of your strongest mitigations just fell over.

nasm
; Non-PIE: attacker knows _start is always at 0x401000
; PIE: attacker must leak an address to find _start

That's the entire reason every modern distribution defaults to PIE, and it's why PIC is required reading for security work.

For more on ASLR as an exploit mitigation, see ASLR and PIE.

What is Next

Linking, C interop, and PIC are now under your belt. The next module turns to the security foundations themselves — hardware memory protections, stack canaries, ASLR, and DEP/NX. The machinery that makes exploitation hard at the instruction level.

Challenges

Write a position-independent assembly function that accesses a string using RIP-relative addressing and prints it. Use 'lea rsi, [rel msg]' instead of 'mov rsi, msg'.

Assembly (NASM 2.14.02)