A linker doesn’t bind your program to a GPU or motherboard. It connects object file sections, symbol names, and virtual addresses.
When a compiled program refuses to run, I split the build into two independent steps: the assembler translating source text into machine bytes, and the linker producing the final binary. Each stage has its own rules—the assembler produces a relocatable object file, while the linker assigns addresses and writes an ELF executable. In this lesson, I’ll build a minimal testbed, walk through four core flags, and intentionally trigger an architecture-mismatch error.
Mental Model: Two Stages and Four Flags
| What it looks like | What actually happens |
|---|---|
nasm produces a ready-to-run program right away |
The assembler emits an object file: machine code, data, and symbol names, without final addresses |
ld binds the program to physical hardware |
The linker maps object sections onto virtual addresses and writes an ELF executable |
A .o file and a finished binary are the same thing |
You can’t run a .o file directly: the kernel loads an executable, not a relocatable file |
| Build flags are just cosmetic | Every flag changes file format, debug info, addressing mode, or stack permissions |
Mechanics: From Text to an ELF Binary
Object Files and ELF
nasm -f elf64 creates an object file in ELF format. To demonstrate, I’ll use a minimal 64-bit program:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; test.asm — minimal ELF64 program: write(1, "ok\n", 3) and exit(0).
default rel
%define SYS_WRITE 1
%define SYS_EXIT 60
%define STDOUT 1
section .rodata
msg: db "ok", 10
msg_len: equ $ - msg
section .text
global _start
_start:
; syscall: write(STDOUT, msg, msg_len)
mov eax, SYS_WRITE
mov edi, STDOUT
lea rsi, [msg]
mov edx, msg_len
syscall
; syscall: exit(0)
mov eax, SYS_EXIT
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbitsThe line msg: db "ok", 10 places the characters o, k, and byte 10 into memory—10 is the ASCII code for a newline (\n). Data directives like db and calculating lengths with equ $ - msg are covered in detail in lesson 04 and lesson 06.
The default rel directive enables instruction-pointer-relative (RIP-relative) addressing for data labels: every reference to a label is encoded as an offset from the current instruction rather than a hardcoded absolute address. We’ll walk through its math and encoding in lesson 08.
The line section .note.GNU-stack noalloc noexec nowrite progbits carries four attributes:
noallocprevents allocating memory for this section at load time;noexecforbids executing code from it;nowriteforbids writing to it;progbitssets the section type to “bytes stored in the file” (though the note itself has zero size).
Together, this boilerplate tells the linker that the stack must not be executable. If you remove that line, ld 2.47 omits the GNU_STACK entry from the ELF file without emitting any warning. On older Linux kernels and in 32-bit i386 mode, omitting this note makes the stack executable (rwxp) for historical compatibility; on modern x86-64 kernels (5.8 and later), the stack remains non-executable (rw-p) anyway, yet an explicit GNU_STACK RW note (or -z noexecstack) is still part of the ELF contract. How to verify this in the headers is shown in Common Pitfalls and lesson 02.
Alongside it sits an equivalent program for the 32-bit i386 architecture. We only need this 32-bit example once—to trigger a linker error when architectures don’t match. The rest of the course is strictly 64-bit.
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; test32.asm — ELF32 (i386) program using int 0x80.
bits 32
%define SYS_WRITE 4
%define SYS_EXIT 1
%define STDOUT 1
section .rodata
msg: db "ok32", 10
msg_len: equ $ - msg
section .text
global _start
_start:
; syscall: write(STDOUT, msg, msg_len)
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg
mov edx, msg_len
int 0x80
; syscall: exit(0)
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
section .note.GNU-stack noalloc noexec nowrite progbitsbits 32 tells NASM to emit 32-bit code (no default rel needed there, since 32-bit mode doesn’t support RIP-relative addressing), and int 0x80 is the legacy way to enter the kernel via a software interrupt. The syscall numbers and registers differ:
- on i386, the syscall number goes in
eax, and arguments inebx,ecx,edx; - on x86-64, the syscall number goes in
rax, arguments inrdi,rsi,rdx,r10,r8,r9, and it uses thesyscallinstruction.
I’ll cover mov in lesson 03, the _start entry point in lesson 04, and syscall itself in lesson 12. For now, this is purely a testbed for checking the toolchain.
Four Flags You Need from Day One
| Flag | Read by | What it changes |
|---|---|---|
-f elf64 |
nasm |
object file format: ELF64 instead of ELF32 or raw flat binary |
-g -F dwarf |
nasm |
adds debug information |
-no-pie |
gcc (used in lesson 00, and again starting in lesson 02) |
disables PIE and links at fixed addresses |
-z noexecstack |
ld and gcc |
marks the stack as non-executable |
Key terms from the table:
-g -F dwarf— DWARF;-no-pie— disables PIE mode when ASLR is active (gccisn’t invoked in this lesson; we’ll need this flag starting in lesson 02 when linking C99 and assembly);-z noexecstack— enables NX protection so shellcode cannot execute off the stack.
We’ll see how this shows up in binary headers in lesson 02.
$ nasm -f elf64 -g -F dwarf -o build/test.o test.asm
$ ld -z noexecstack -o build/test build/test.o
$ nasm -f elf32 -g -F dwarf -o build/test32.o test32.asm
$ ld -m elf_i386 -z noexecstack -o build/test32 build/test32.oThe -m elf_i386 flag is required when linking a 32-bit object file: it switches the BFD subsystem’s output format to 32-bit i386—it does not launch a CPU emulator.
Environment Setup
The x86-64 toolchain needs to run on Linux. If you’re on macOS or Windows, use a container or a virtual machine.
sudo pacman -S nasm binutils gdb make stracesudo apt update
sudo apt install nasm binutils gdb make stracesudo dnf install nasm binutils gdb make straceVerify your setup: nasm -v, ld -V, gdb --version.
macOS (Apple Silicon). ARM64 hardware cannot execute x86-64 machine code without an emulation layer. I recommend running a container with linux/amd64 emulation (Docker or OrbStack):
docker run --rm -it --platform linux/amd64 \
-v "$PWD:/work" -w /work ubuntu:24.04 bashInside the container, install packages using the Linux commands. Verify the environment: uname -m must print x86_64.
Windows (WSL2). Install Ubuntu under WSL2 and work strictly inside its native ext4 filesystem (for instance, in ~/asm-course), avoiding /mnt/c/... paths: those go through a slow Windows interop layer and don’t preserve standard Linux file permissions, making builds and execution slower and less predictable.
wsl --install -d UbuntuInside WSL2, the same package installation commands apply. Verify: uname -m returns x86_64.
Checking in the Terminal: .o vs. an Executable
$ file build/test.o build/test build/test32.o build/test32
build/test.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), with debug_info, not stripped
build/test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
build/test32.o: ELF 32-bit LSB relocatable, Intel i386, version 1 (SYSV), with debug_info, not stripped
build/test32: ELF 32-bit LSB executable, Intel i386, version 1 (SYSV), statically linked, with debug_info, not strippedHere is how to read the file output fields:
ELF 64-bit— ELF class (64-bit);LSB— little-endian byte order (covered in lesson 06);relocatable— addresses aren’t finalized yet; this is an object file;executable— linking is complete; the file is ready to run;x86-64andIntel i386— target architecture;version 1 (SYSV)— ELF format version 1 and the System V tag; we’ll cover that standard’s rules in lesson 18;statically linked— the binary doesn’t need a dynamic loader;with debug_info— DWARF data is embedded;not stripped— symbol names are intact.
The finished binaries run and return zero:
$ ./build/test; echo $?
ok
0
$ ./build/test32; echo $?
ok32
0You can’t execute an object file directly: it has no final addresses, and the kernel expects an executable type. You have to pass it through the linker first.
Common Pitfalls
-
“Architecture incompatibility error when linking.”
Fact:ldprintsld: i386 architecture of input file ... is incompatible with i386:x86-64 output.
Cause: The linker ran with its default 64-bit emulation on a 32-bit object file.
Fix: Explicitly passld -m elf_i386for ELF32, or assemble for 64-bit (nasm -f elf64). -
“The file was created, but bash throws an execution error.”
Fact:file build/test.oreportsrelocatable.
Cause: Trying to run an object file without linking it first.
Fix: Runldand verify thatfilereportsexecutable. -
“The linker warns about a missing entry point.”
Fact:ldprintsld: warning: cannot find entry symbol _start; defaulting to 0000000000401000.
Cause: You forgot theglobal _startdirective in the assembly file or misspelled the label.
Fix: Declareglobal _startand make sure the_start:label exists. -
“The finished binary is missing the non-executable stack note.”
Fact: If you strip out thesection .note.GNU-stackline, the object file loses that section and the executable loses theGNU_STACKsegment:text$ grep -v 'note.GNU-stack' test.asm > /tmp/test_no_note.asm $ nasm -f elf64 -o /tmp/test_no_note.o /tmp/test_no_note.asm $ ld -o /tmp/test_no_note /tmp/test_no_note.o $ readelf -SW /tmp/test_no_note.o | grep -c 'note.GNU-stack' 0 $ readelf -lW /tmp/test_no_note | grep -c 'GNU_STACK' 0With
-z noexecstack, the segment appears withRWpermissions:text$ ld -z noexecstack -o /tmp/test_no_note_ok /tmp/test_no_note.o $ readelf -lW /tmp/test_no_note_ok | grep 'GNU_STACK' GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x10Cause: The object file was assembled without a GNU-stack section and linked without
-z noexecstack; in this environmentld2.47 prints no warning, so the missing note only shows up in the headers.
Fix: Add that section line at the end of your.asmfile or pass-z noexecstackto the linker;readelf -SWand-lWflags are covered in lesson 02.
Lab
Goal. Build both 64-bit and 32-bit executables from assembly source and trigger a linker error from mismatched architectures.
Contract. Take test.asm and test32.asm. Assemble the object files with NASM, then link the 64-bit binary with ld and the 32-bit binary with ld -m elf_i386. Afterward, link the 32-bit object without -m and observe the error. Use -z noexecstack for both valid links.
Verification criteria.
file build/testoutputsELF 64-bit ... executable.file build/test32outputsELF 32-bit ... executable../build/test; echo $?printsokand exit code0../build/test32; echo $?printsok32and exit code0.- Running
ld -o build/test32-broken build/test32.ofails with exit code 1 and an architecture-incompatibility message.
Hints, expected output, and walkthrough
You can reproduce the linker failure with this command:
$ ld -o build/test32-broken build/test32.o
/usr/bin/ld: i386 architecture of input file `build/test32.o' is incompatible with i386:x86-64 output
$ echo $?
1On a 64-bit system, ld defaults to elf_x86_64 emulation. The test32.o object file has an ELF32 header. The only way to resolve the mismatch is passing the emulation flag explicitly: ld -m elf_i386. You can list supported emulations with ld -V.
Lab source code, Makefile, and automated tests for this lesson: lessons/01-setup.
Summary
nasmtranslates mnemonics into machine bytes inside an object file, whileldassigns addresses and creates an ELF executable.filedistinguishesrelocatablefromexecutable: an object file cannot be run without linking.- The four core flags control format (
-f elf64), debug info (-g -F dwarf), addressing (-no-pie), and stack permissions (-z noexecstack). - Linking a 32-bit object on a 64-bit host requires
ld -m elf_i386. - Either
section .note.GNU-stackor-z noexecstackmarks the stack as non-executable.