Memory layout: sections
The three sections
Every NASM program declares its memory in sections. .text is code and is read-only, .data is initialized data, and .bss is reserved-but-zero storage that takes no space in the file:
default rel section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 3 syscall mov byte [buf], 42 mov rax, 60 xor rdi, rdi syscall section .data msg db "hi", 10 section .bss buf resb 4096
The program writes to the buffer once, so the assembled file has to contain the code that does it; the buffer itself is what the file does not contain. Assemble, link and run it:
nasm -f elf64 -o sections.o sections.asm
ld -o sections sections.o
./sections
echo $?
hi
0
Why .bss is free
A .bss reserve costs nothing in the file: the ELF header records the size, and the kernel zeroes the pages when it loads the program. A 4 KB buffer in .data would add 4 KB to the file; in .bss it adds nothing.
The process image
The loader maps .text at a low address, .data after it, then .bss, then the heap, and the stack at the top of the address space. Section order in your source does not decide the layout: the linker script does.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which section holds the code of a program?
Answer
.text
.text is the code section, and the loader maps it read-only and executable.
-
Which section holds initialized data?
Answer
.data
.data stores its bytes in the file and gets them mapped at load time.
-
Which section takes no space in the executable file?
Answer
.bss
A .bss section is NOBITS: the file records its size and address, not its bytes.
-
What fills a .bss buffer before the program starts?
Answer
The kernel, which maps zeroed pages
The kernel supplies zero pages, so the file needs no room for the buffer.
-
What does
resb 4096do?Answer
It reserves 4096 bytes of uninitialized storage
resb reserves bytes and stores nothing in the object file; db would store a value.
-
How many bytes does
dwdefine for one value?Answer
2
dw is a word, two bytes; db is one, dd four and dq eight.
-
What does this program print?
default rel section .text global _start _start: mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 3 syscall mov rax, 60 xor rdi, rdi syscall section .data msg db "hi", 10Answer
hi
The three bytes are h, i and a newline, so the visible text is hi.
-
What does this program print?
default rel section .text global _start _start: mov byte [buf], 65 mov rax, 1 mov rdi, 1 mov rsi, buf mov rdx, 1 syscall mov rax, 60 xor rdi, rdi syscall section .bss buf resb 1Answer
A
65 is the ASCII code of A, and write prints the byte from the .bss buffer.
-
What decides where the sections land in memory?
Answer
The linker script
The linker script places the sections; the source order does not decide the addresses.
-
Why does a 4 KB buffer in .bss add nothing to the file size?
Answer
The section is marked NOBITS, so the file stores no bytes for it
NOBITS means the section exists in memory but has no file contents.
The code on this page was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev