On this page

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
Focus of this lesson
Just like in lesson 00, the test.asm code below is a working sample for testing the assembler and linker. Don’t spend energy memorizing mov, lea, xor, or syscall yet—we’ll break those down in lessons 03 and 04. Right now, focus on the path from a plain text file to an ELF binary.

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:

test.asm
; 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 progbits

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

  • noalloc prevents allocating memory for this section at load time;
  • noexec forbids executing code from it;
  • nowrite forbids writing to it;
  • progbits sets 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.

test32.asm
; 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 progbits

bits 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 in ebx, ecx, edx;
  • on x86-64, the syscall number goes in rax, arguments in rdi, rsi, rdx, r10, r8, r9, and it uses the syscall instruction.

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 (gcc isn’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.o

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

bash
sudo pacman -S nasm binutils gdb make strace
bash
sudo apt update
sudo apt install nasm binutils gdb make strace
bash
sudo dnf install nasm binutils gdb make strace

Verify 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):

bash
docker run --rm -it --platform linux/amd64 \
  -v "$PWD:/work" -w /work ubuntu:24.04 bash

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

powershell
wsl --install -d Ubuntu

Inside 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 stripped

Here 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-64 and Intel 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
0

You 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

  1. “Architecture incompatibility error when linking.”
    Fact: ld prints ld: 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 pass ld -m elf_i386 for ELF32, or assemble for 64-bit (nasm -f elf64).

  2. “The file was created, but bash throws an execution error.”
    Fact: file build/test.o reports relocatable.
    Cause: Trying to run an object file without linking it first.
    Fix: Run ld and verify that file reports executable.

  3. “The linker warns about a missing entry point.”
    Fact: ld prints ld: warning: cannot find entry symbol _start; defaulting to 0000000000401000.
    Cause: You forgot the global _start directive in the assembly file or misspelled the label.
    Fix: Declare global _start and make sure the _start: label exists.

  4. “The finished binary is missing the non-executable stack note.”
    Fact: If you strip out the section .note.GNU-stack line, the object file loses that section and the executable loses the GNU_STACK segment:

    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'
    0

    With -z noexecstack, the segment appears with RW permissions:

    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  0x10

    Cause: The object file was assembled without a GNU-stack section and linked without -z noexecstack; in this environment ld 2.47 prints no warning, so the missing note only shows up in the headers.
    Fix: Add that section line at the end of your .asm file or pass -z noexecstack to the linker; readelf -SW and -lW flags 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.

  1. file build/test outputs ELF 64-bit ... executable.
  2. file build/test32 outputs ELF 32-bit ... executable.
  3. ./build/test; echo $? prints ok and exit code 0.
  4. ./build/test32; echo $? prints ok32 and exit code 0.
  5. Running ld -o build/test32-broken build/test32.o fails with exit code 1 and an architecture-incompatibility message.
Hints, expected output, and walkthrough

You can reproduce the linker failure with this command:

text
$ 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 $?
1

On 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

  1. nasm translates mnemonics into machine bytes inside an object file, while ld assigns addresses and creates an ELF executable.
  2. file distinguishes relocatable from executable: an object file cannot be run without linking.
  3. The four core flags control format (-f elf64), debug info (-g -F dwarf), addressing (-no-pie), and stack permissions (-z noexecstack).
  4. Linking a 32-bit object on a 64-bit host requires ld -m elf_i386.
  5. Either section .note.GNU-stack or -z noexecstack marks the stack as non-executable.
Next Up
The binary builds, though we haven’t looked inside it yet: we still don’t know how much space code and data take, where symbol names live, or where the linker patched addresses. In lesson 02, I walk through the terminal utilities for reading ELF files—from a file’s basic fingerprint down to a byte-level hex dump—and set up four short aliases.