Skip to main content
Be Bitwise
x86 Assemblyx86 Basics & Environment

Sections and Directives

An assembly source file is broken into sections that tell the assembler (and the OS, eventually) how each part of your program should be loaded into memory. NASM also exposes directives — pseudo-instructions that drive the assembler instead of producing machine code.

For security, sections matter because the boundaries between them are what makes some memory writable, some executable, some read-only. Get those wrong and exploitation becomes a one-line affair.

Program Sections

A NASM program leans on four sections, day to day:

.text - Executable Code

nasm
section .text
global _start
_start:
mov rax, 60
xor rdi, rdi
syscall

.text holds your machine instructions. By default the linker and loader mark those pages executable but not writable. This is where every instruction your CPU runs ends up.

.data - Initialized Data

nasm
section .data
greeting db "Hello!", 10
answer dd 42
pi dq 3.14159

.data is for variables with an initial value baked in. The assembler writes those bytes straight into the output file. At runtime the memory is writable, but not executable.

.bss - Uninitialized Data

nasm
section .bss
buffer resb 1024 ; reserve 1024 bytes
count resd 1 ; reserve space for one 32-bit integer

.bss reserves memory that starts at all zeros. It costs nothing in the binary on disk; the OS just hands you zeroed pages at load time. Park large buffers and any variable that doesn't need an initial value here.

.rodata - Read-Only Data

nasm
section .rodata
error_msg db "Fatal error", 10, 0
magic dd 0xDEADBEEF

.rodata is constant data that nothing should ever write to. The OS maps it read-only, not executable. String literals and lookup tables belong here.

The memory layout diagram below shows how these sections sit in a running process:

Process Memory Layout

0xFFFF...0x0000...StackgrowsFree SpaceHeapgrowsBSSDataText
Security Concept: W^X (Write XOR Execute)

W^X (pronounced "write xor execute") is the policy that a page of memory should be writable OR executable, but not both at once. It's a basic defence against code injection. If an attacker writes shellcode into a buffer (writable memory), they can't run it because writable pages aren't executable, and executable memory can't be written to. Toolchains and loaders follow it by default: .text is executable but not writable, .data and .bss are writable but not executable. It's a convention plus policy, not a hardware rule. Stock Linux will still hand out a writable-and-executable mapping if a program asks for one with mmap or mprotect, unless SELinux, PaX or a similar policy refuses it.

Data Definition Directives

NASM has directives for stamping specific byte patterns into the output:

DirectiveSizeMeaningExample
db1 byteDefine bytedb 0x41 or db "A"
dw2 bytesDefine worddw 0x1234
dd4 bytesDefine double worddd 100000
dq8 bytesDefine quad worddq 0xDEADBEEFCAFEBABE

A single directive can hold multiple values, strings included:

nasm
section .data
; Multiple bytes, including string and explicit values
greeting db "Hi", 10, 0 ; 'H', 'i', newline, null terminator
; An array of 32-bit integers
numbers dd 10, 20, 30, 40, 50
; A 64-bit address placeholder
pointer dq 0

Space Reservation Directives

For .bss data, the reservation directives do the work:

DirectiveSizeMeaningExample
resb1 byteReserve bytesresb 256
resw2 bytesReserve wordsresw 100
resd4 bytesReserve double wordsresd 10
resq8 bytesReserve quad wordsresq 1

No bytes go into the executable. They just signal to the linker how much zeroed memory to set aside at runtime.

Constants and Symbols

equ defines an assembler-time constant. It does not allocate memory:

nasm
SYS_WRITE equ 1
SYS_EXIT equ 60
STDOUT equ 1
BUFFER_SIZE equ 4096
section .data
msg db "Hello", 10
msg_len equ $ - msg ; $ is the current address, so this computes the length

$ is shorthand for the current assembly position. equ $ - label is the idiomatic way to compute the length of data you just laid down.

global makes a symbol visible to the linker (exported):

nasm
global _start ; the linker needs to find _start
global my_function ; export a function for other object files

extern declares a symbol defined in another object file:

nasm
extern printf ; defined in the C library
extern my_helper ; defined in another .asm file

Putting It Together

A small program that exercises every section type and the common directives:

nasm
SYS_WRITE equ 1
SYS_EXIT equ 60
STDOUT equ 1
section .rodata
title db "=== Section Demo ===", 10
title_len equ $ - title
section .data
counter dd 0 ; a mutable 32-bit counter
values dq 100, 200, 300 ; an array of 64-bit values
section .bss
input_buf resb 256 ; 256-byte input buffer
result resq 1 ; one 64-bit result slot
section .text
global _start
_start:
; Print the title
mov rax, SYS_WRITE
mov rdi, STDOUT
mov rsi, title
mov rdx, title_len
syscall
; Exit cleanly
mov rax, SYS_EXIT
xor rdi, rdi
syscall
Security Note: Section Permissions in Exploits

When analyzing or developing exploits, knowing the section layout of a binary is one of the first steps. Tools like readelf -S show you every section, its address, size, and permission flags. An attacker looks for writable sections to place data and executable sections to redirect control flow to. Mitigations like ASLR randomize section addresses, and W^X prevents combining write and execute permissions. Understanding sections is prerequisite knowledge for both attack and defense.

Inspecting Sections

Once assembled and linked, peek at the sections in your binary:

bash
nasm -f elf64 sections.asm -o sections.o
ld sections.o -o sections
readelf -S sections # show all sections with flags
objdump -d sections # disassemble the .text section
objdump -s -j .data sections # hexdump the .data section

readelf annotates each section with flags: A (allocated at runtime), W (writable), X (executable). You'll spot .text carrying AX and .data carrying WA.

The loader doesn't map sections one by one, though. ld groups sections with compatible flags into segments, and readelf -l sections shows that grouping under "Section to Segment mapping". With a current GNU ld on x86-64 Linux it comes out like this:

Segment permissions become page permissions, which is why a write into .rodata or a jump into .data ends in a segfault. .bss shares the RW segment but takes no space in the file. Its segment's memory size is simply larger than its file size, and the loader zero-fills the difference.

What is Next

Memory layout in hand, the next lesson digs into the Linux syscall interface — how your assembly code actually talks to the kernel.