Skip to main content
Be Bitwise
x86 AssemblyLinking & C Interop

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

bash
nasm -f elf64 main.asm -o main.o
nasm -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:

nasm
; 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 linker

You can inspect an object file with readelf or objdump:

bash
readelf -h main.o # ELF header
readelf -S main.o # sections
readelf -s main.o # symbol table
readelf -r main.o # relocations
objdump -d main.o # disassembly

The 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.

nasm
; === main.asm ===
extern print_message ; declare: this symbol is defined elsewhere
global _start ; declare: this symbol is visible to other files
section .text
_start:
call print_message
mov rax, 60
xor rdi, rdi
syscall
nasm
; === 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 .text
print_message:
mov rax, 1
mov rdi, 1
lea rsi, [msg]
mov rdx, msg_len
syscall
ret
bash
nasm -f elf64 main.asm -o main.o
nasm -f elf64 utils.asm -o utils.o
ld main.o utils.o -o hello
./hello

The 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.

bash
$ readelf -r main.o
Relocation section '.rela.text':
Offset Type Symbol
000000001 R_X86_64_PLT32 print_message

That 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:

bash
ld main.o utils.o -o hello # static: everything in one binary

Dynamic linking defers some symbol resolution until the program loads:

bash
gcc main.o -lc -o hello # dynamic: links against shared libc
AspectStaticDynamic
Binary sizeLarger (includes all code)Smaller (shared libraries loaded at runtime)
DependenciesSelf-containedRequires shared libraries at runtime
Security updatesMust rebuild to update librariesUpdating a shared library updates all programs
Attack surfaceFixed at build timeShared libraries can be replaced
The Linker Is a Security Boundary

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:

bash
readelf -l hello # program headers (what gets loaded)
readelf -S hello # section headers (all sections)
nm hello # list all symbols and their addresses
objdump -d hello # full disassembly

These 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:

nasm
; === math.asm ===
global add_numbers
global multiply_numbers
section .text
add_numbers: ; rdi + rsi -> rax
mov rax, rdi
add rax, rsi
ret
multiply_numbers: ; rdi * rsi -> rax
mov rax, rdi
imul rax, rsi
ret
bash
nasm -f elf64 main.asm -o main.o
nasm -f elf64 math.asm -o math.o
nasm -f elf64 utils.asm -o utils.o
ld main.o math.o utils.o -o program

The 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 .so out 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.