Object Files and Linking
Every program we've built so far has been a single assembly file, assembled with NASM and linked with ld. Real software doesn't work that way. Production code is split across dozens or thousands of source files, compiled independently, and stitched together by a linker. Understanding that stitching matters for security work — it shows you how symbols get resolved, where supply-chain attacks hide, and how to verify a binary hasn't been tampered with.
The Compilation Pipeline
From source code to running program, the build process has distinct stages:
Each source file gets assembled on its own into an object file (.o). The linker (ld) then glues the object files together, patching up all the cross-file references along the way.
Producing Object Files with NASM
nasm -f elf64 main.asm -o main.onasm -f elf64 utils.asm -o utils.o-f elf64 tells NASM to spit out a 64-bit ELF (Executable and Linkable Format) object file. ELF is the standard binary format on Linux.
ELF Format Basics
An ELF object file contains several key components:
; What NASM puts in the object file:; .text section - your assembled machine code; .data section - initialized global data; .bss section - uninitialized data (just a size, no bytes on disk); Symbol table - names and addresses of labels; Relocation table - places where addresses need to be filled in by the linkerYou can inspect an object file with readelf or objdump:
readelf -h main.o # ELF headerreadelf -S main.o # sectionsreadelf -s main.o # symbol tablereadelf -r main.o # relocationsobjdump -d main.o # disassemblyThe same format serves two readers. The linker reads an object file through its section header table: named sections plus the symbols and relocations that describe them. The kernel's loader reads an executable through its program header table: a few segments, each a run of bytes with one set of page permissions. ld produces the second view from the first:
readelf -S shows the left side and readelf -l the right. An executable usually keeps its section headers too, for debuggers and tools, but the loader never looks at them.
Symbols: Global and External
Symbols are how the linker stitches code across object files. You control which ones are visible with global and extern.
; === main.asm ===extern print_message ; declare: this symbol is defined elsewhereglobal _start ; declare: this symbol is visible to other files
section .text_start: call print_message mov rax, 60 xor rdi, rdi syscall; === utils.asm ===global print_message ; make this symbol visible to the linker
section .data msg db "Hello from utils!", 10 msg_len equ $ - msg
section .textprint_message: mov rax, 1 mov rdi, 1 lea rsi, [msg] mov rdx, msg_len syscall retnasm -f elf64 main.asm -o main.onasm -f elf64 utils.asm -o utils.old main.o utils.o -o hello./helloThe linker pairs the extern print_message in main.o with the global print_message in utils.o. Drop the global and the label stays trapped inside its own object file, invisible to anything outside.
Relocations: How the Linker Fills in Addresses
When NASM assembles call print_message inside main.o, the final address is unknowable. That depends entirely on where the linker decides to slot utils.o. So NASM writes zeros where the address should go and leaves a relocation entry behind — a sticky note for the linker, telling it to come back and patch the right value in once everything's been placed.
$ readelf -r main.oRelocation section '.rela.text': Offset Type Symbol 000000001 R_X86_64_PLT32 print_messageThat relocation says: at offset 1 in .text, drop in the relative address of print_message. The linker burns through every relocation in one pass, after it's worked out where each section and symbol ends up in the final executable.
Static vs Dynamic Linking
Static linking copies all needed code into the executable at link time:
ld main.o utils.o -o hello # static: everything in one binaryDynamic linking defers some symbol resolution until the program loads:
gcc main.o -lc -o hello # dynamic: links against shared libc| Aspect | Static | Dynamic |
|---|---|---|
| Binary size | Larger (includes all code) | Smaller (shared libraries loaded at runtime) |
| Dependencies | Self-contained | Requires shared libraries at runtime |
| Security updates | Must rebuild to update libraries | Updating a shared library updates all programs |
| Attack surface | Fixed at build time | Shared libraries can be replaced |
The linker sits at a trust chokepoint. It fuses code from many sources into one executable, no questions asked. One poisoned object file and your binary is owned. That's why reproducible builds, build-system isolation, and code signing exist. Swap a single .o or replace a shared library, and every program downstream inherits the compromise.
Examining a Linked Executable
After linking, you can verify what ended up in the binary:
readelf -l hello # program headers (what gets loaded)readelf -S hello # section headers (all sections)nm hello # list all symbols and their addressesobjdump -d hello # full disassemblyThese tools are bread and butter for reverse engineering. When a suspicious binary lands on your desk, readelf and objdump are usually where you start.
Linking Multiple Object Files
A more realistic example with three files:
; === math.asm ===global add_numbersglobal multiply_numbers
section .textadd_numbers: ; rdi + rsi -> rax mov rax, rdi add rax, rsi ret
multiply_numbers: ; rdi * rsi -> rax mov rax, rdi imul rax, rsi retnasm -f elf64 main.asm -o main.onasm -f elf64 math.asm -o math.onasm -f elf64 utils.asm -o utils.old main.o math.o utils.o -o programThe linker chews through every extern reference across the whole bundle. Reference a symbol that nobody defined, and it spits out the dreaded "undefined reference" error.
Security Perspective: Supply Chain Attacks and Binary Integrity
Every object file feeding a build is a trust input. Supply-chain attackers know this and aim straight for the link step:
- A poisoned compiler quietly slips backdoors into the object files it produces
- A malicious library update swaps a legitimate
.soout from under you - A compromised build system shuffles object files at link time
Defences worth knowing:
- Reproducible builds: rebuild from the same source, get byte-for-byte identical output
- Code signing: cryptographic signatures pin a binary to its origin
- Build system isolation: lock down what the build can touch
- Dependency pinning: nail down exact versions, no surprises
At the assembly level, integrity verification means comparing disassembly, checking symbol tables, and confirming each section holds what it should. The same skills apply directly to malware analysis and incident response.
What is Next
Linking joins assembly to assembly. The next lesson crosses a bigger gap — calling into the C standard library from your asm.