An ELF section is a linker promise, not a storage mechanism. Only the bytes someone actually wrote end up on disk.
After looking at registers and GDB, memory seems uniform: pick an address, and a value sits there. In reality, each section carries its own contract—read-only, read-write, or zero-filled with no bytes on disk. In this lesson, I build a program with three sections, inspect it with symbols, hex dumps, and section headers, and check how 0x11223344 is laid out in memory.
Mental Model: File on Disk vs. Loaded Process
| What it looks like | What happens under the hood |
|---|---|
| A string, global variable, and array are stored the same way | Sections carry distinct permissions: .rodata is read-only, while .data and .bss are writable |
0x11223344 sits in memory just as written |
The least significant byte 44 sits at the lowest address; the most significant byte 11 at the highest |
| A 10 MiB array takes 10 MiB on disk | .bss stores no bytes: the file holds only its size, and the Linux kernel zero-fills the memory |
Zeros in .bss are stored values |
Zeros appear when pages are mapped, not read from disk |
Three Sections and Their Contract
In ELF, three data sections split the job. First, .data holds bytes that must exist on disk and remain writable.
Second, .bss stores no bytes on disk yet reserves virtual address space in memory.
The third section in our testbed is .rodata: read-only data also stored in the file. The banner string is declared there alongside its length computed with equ.
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; mem_layout.asm — three data sections and byte order in memory.
default rel
%define SYS_WRITE 1
%define SYS_EXIT 60
%define STDOUT 1
section .rodata
banner: db "value32 = 0x11223344, value64 = 0x1122334455667788", 10
banner_len: equ $ - banner
section .data
value32: dd 0x11223344
value64: dq 0x1122334455667788
section .bss
scratch: resb 16
section .text
global _start
_start:
; syscall: write(STDOUT, banner, banner_len)
mov eax, SYS_WRITE
mov edi, STDOUT
lea rsi, [banner]
mov edx, banner_len
syscall
; reads value32 from .data, adds 1, and stores the result in .bss
mov eax, dword [value32]
add eax, 1
mov dword [scratch], eax
after_mem_rw:
; syscall: exit(0)
mov eax, SYS_EXIT
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbitsresb reserves 16 bytes without writing a single byte to the file: its argument is a byte count, not an initial value, and you cannot set an initial value with resb. The value32, value64, and scratch symbols receive addresses, while their contents depend on the section type.
Data directives specify the width of a single element:
| Directive | Width | What it does |
|---|---|---|
db |
1 byte | writes a byte to .data or .rodata |
dw |
2 bytes | writes a 16-bit word |
dd |
4 bytes | writes a 32-bit double word |
dq |
8 bytes | writes a 64-bit quad word |
resb |
N bytes | reserves N bytes in .bss without writing them to the file |
resw |
2N bytes | reserves N 16-bit words |
resd |
4N bytes | reserves N 32-bit words |
resq |
8N bytes | reserves N 64-bit words |
align 16enforces alignment of the next address up to a 16-byte boundary: in.data, padding may appear between objects;times BIG_SIZE db 0xAArepeats the directive on the right the specified number of times at assembly time, not runtime. The repeat count is a compile-time constant, sotimes 16 db 0in.datawrites 16 real zero bytes into the file, whereasresb 16only reserves 16 addresses; indata_10m.asm(just below), this generates 10 MiB of0xAAbytes.
The Linux kernel zero-fills .bss pages on first touch: the file contains only an address range, and the physical page is handed out already cleared to zeros.
Little-Endian: Least Significant Byte First
Byte order in memory follows little-endian: this isn’t about how you write a literal in source code, it’s about the order bytes land in memory.
Strings work differently: their bytes sit at increasing addresses in the exact order characters are written. Only multi-byte integers are byte-reversed.
Checking in the Terminal
Building and running confirms the data reaches stdout:
$ cd lessons/06-memory-data
$ make build
$ ./build/mem_layout
value32 = 0x11223344, value64 = 0x1122334455667788nm shows which section each symbol landed in:
$ nm -n build/mem_layout | grep -E ' (banner|value32|value64|scratch)$'
0000000000402000 r banner
0000000000403034 d value32
0000000000403038 d value64
0000000000403040 b scratchr— read-only data, the string in.rodata;d— initialized data, the integers in.data;b— BSS, a buffer with no bytes in the file;- addresses climb upward:
.rodatacomes before.data, and.bsssits right after it.
The jump from 0x402000 (.rodata, length 0x34) to 0x403034 (.data) instead of 0x403000 preserves the congruence p_offset ≡ p_vaddr (mod 4096): the RW segment begins on the next page at offset 0x34, ensuring .rodata and .data never share a page with conflicting permissions.
A GDB dump after loading shows the actual byte order:
$ gdb -q --nx -batch build/mem_layout \
-ex 'set debuginfod enabled off' \
-ex 'break _start' -ex run \
-ex 'x/4xb &value32' -ex 'x/8xb &value64'
Breakpoint 1 at 0x401000: file mem_layout.asm, line 27.
Breakpoint 1, _start () at mem_layout.asm:27
27 mov eax, SYS_WRITE
0x403034 <value32>: 0x44 0x33 0x22 0x11
0x403038 <value64>: 0x88 0x77 0x66 0x55 0x44 0x33 0x22 0x11In memory, 0x11223344 reads as 44 33 22 11, and 0x1122334455667788 as 88 77 ... 11. The human-readable integer order exists only in source code and formatted output, not in memory bytes.
objdump shows the exact same bytes inside the file’s section:
$ objdump -s -j .data build/mem_layout
build/mem_layout: file format elf64-x86-64
Contents of section .data:
403034 44332211 88776655 44332211 D3"..wfUD3".403034— address of the first byte on that dump line;44332211and subsequent groups — four bytes each in hex;- the right column
D3"..wfUD3".— those same bytes rendered as ASCII characters, not separate data.
Reading and Writing Memory
So far, data was only read from disk at load time. Now mem_layout.asm also mutates a field at runtime—see the snippet below:
; reads value32 from .data, adds 1, and stores the result in .bss
mov eax, dword [value32]
add eax, 1
mov dword [scratch], eax
after_mem_rw:Square brackets perform pointer dereferencing, familiar from C99:
mov eax, dword [value32]reads 4 bytes from the address ofvalue32and loads them into a register—identical touint32_t x = *p;ifppoints tovalue32;mov dword [scratch], eaxgoes the other way and matches*q = x;: the register’s value is written to memory atscratch;- the
dwordsize specifier is mandatory: the assembler cannot infer field width from a symbol name.
The contrast with lea from lesson 01 is fundamental: lea rsi, [banner] computes an address without touching memory, whereas mov with brackets dereferences that address.
A breakpoint on after_mem_rw pauses execution right after the write:
$ gdb -q --nx -batch build/mem_layout \
-ex 'set debuginfod enabled off' \
-ex 'break after_mem_rw' -ex run \
-ex 'x/4xb &value32' -ex 'x/4xb &scratch' \
-ex 'info registers eax'
Breakpoint 1 at 0x401027: file mem_layout.asm, line 40.
Breakpoint 1, after_mem_rw () at mem_layout.asm:40
40 mov eax, SYS_EXIT
0x403034 <value32>: 0x44 0x33 0x22 0x11
0x403040 <scratch>: 0x45 0x33 0x22 0x11
eax 0x11223345 287454021value32in memory stayed unchanged: reading doesn’t modify the source;scratchin.bssnow holds0x11223345—the low byte incremented from0x44to0x45, while the upper bytes remained untouched because the carry never left the 32-bit boundary;eaxholds the exact value written to memory: the register and the memory slot are two copies of the same number.
Meanwhile, the .data dump on disk didn’t change: objdump -s -j .data reads the file, while mov dword [scratch], eax modified the process’s image in RAM. Memory mutations live only until the process exits.
The Section That Isn’t in the File
Two testbeds reserve 10 MiB each, in different ways: one puts the bytes in .data, the other in .bss. Ten mebibytes in bytes:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; data_10m.asm — 10 MiB of initialized bytes in .data.
default rel
%define SYS_EXIT 60
%define BIG_SIZE 10485760
section .data
align 16
big: times BIG_SIZE db 0xAA
section .text
global _start
_start:
; syscall: exit(0)
mov eax, SYS_EXIT
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbits; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; bss_10m.asm — the same 10 MiB, but in .bss: none of it lives on disk.
default rel
%define SYS_EXIT 60
%define BIG_SIZE 10485760
section .bss
align 16
big: resb BIG_SIZE
section .text
global _start
_start:
; syscall: exit(0)
mov eax, SYS_EXIT
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbitsThe file sizes differ by nearly 10 MiB:
$ stat -c '%n %s' build/data_10m build/bss_10m
build/data_10m 10495176
build/bss_10m 5328stat -c '%n %s' prints each file’s name and byte size; the nearly 10 MiB gap comes from bytes written to the file in one case and merely reserved in the other.
Section headers explain the difference:
$ readelf -SW build/data_10m | grep ' .data '
[ 2] .data PROGBITS 0000000000402000 002000 a00000 00 WA 0 0 16
$ readelf -SW build/bss_10m | grep ' .bss '
[ 2] .bss NOBITS 0000000000402000 002000 a00000 00 WA 0 0 16PROGBITS— bytes physically live in the file at offsetOff;NOBITS— the section exists purely as an address range and size;Sizeisa00000(10 MiB) on both lines;Offon.bsspoints to a file position, yet no bytes are actually written there.
size summarizes this into three columns:
$ size build/data_10m build/bss_10m
text data bss dec hex filename
9 10485760 0 10485769 a00009 build/data_10m
9 0 10485760 10485769 a00009 build/bss_10mThe dec totals match, yet the first binary puts 10 MiB in data and the second puts it in bss. That’s why large uninitialized buffers live in .bss: the binary stays tiny on disk.
data_10m (.data, PROGBITS):
File: [ headers | 10 MiB of 0xAA ] ──► RAM: [ 10 MiB of 0xAA loaded from file ]
bss_10m (.bss, NOBITS):
File: [ headers | size 0xa00000 ] ──► RAM: [ kernel supplies zero pages on touch ]Common Pitfalls
-
"
dbcan hold any integer."
Fact:bad_db.asmdeclares the integer as a byte:nasmsection .data value32: db 0x11223344NASM warns and keeps only one byte:
text$ nasm -f elf64 -o build/bad_db.o bad_db.asm bad_db.asm:10: warning: byte exceeds bounds [-w+number-overflow] $ objdump -s -j .data build/bad_db.o 0000 44 DCause:
dbis strictly one byte; the upper bytes of0x11223344don’t fit and get truncated.
Fix: useddfor 32-bit values anddqfor 64-bit values, and verify widths in a hex dump. -
“Writing one byte updates the entire 32-bit field.”
Fact: aftermov byte [value32], 1, only the first byte changes in the dump:textbefore: 0x402000 <value32>: 0x44 0x33 0x22 0x11 after: 0x402000 <value32>: 0x01 0x33 0x22 0x11Cause: write width is determined by the operand size, not the symbol’s type in source code.
Fix: writemov dword [value32], 1when you want to overwrite the whole field. -
“You can set an initial value in
.bss.”
Fact:bad_bss.asmdeclares an initialized buffer:nasmsection .bss buffer: db 1NASM ignores the initialization:
text$ nasm -f elf64 -o build/bad_bss.o bad_bss.asm bad_bss.asm:10: warning: attempt to initialize memory in BSS section `.bss': ignored [-w+other] $ readelf -SW build/bad_bss.o | grep '\.bss' [ 1] .bss NOBITS 0000000000000000 000200 000001 00 WA 0 0 4Cause:
.bsshas no bytes in the file, so initial values can’t be stored there.
Fix: keep non-zero initial values in.data, and use onlyresb/resd/resqin.bss.
Lab
Goal. Extend mem_layout.asm with a 16-bit value and a 4 KiB buffer, and prove with dumps that the new field lives in .data while the buffer lives in .bss.
Contract and requirements.
- add
value16set to0xABCDin.data; - add a 4096-byte
zero_pagebuffer in.bss; - program output and exit code stay unchanged;
- the file size on disk must not grow by 4 KiB.
Verification criteria.
nm -n build/mem_layout | grep value16shows typed.gdb -q --nx build/mem_layoutwithbreak _startandx/2xb &value16prints0xcd 0xab.x/4xb &zero_pageprints four zero bytes.size build/mem_layoutshowsbssgrowing by 4100 bytes—from 16 to 4116: 4096 bytes for the buffer plus 4 bytes of section-end alignment padding;stat -c '%s'changes only slightly.
Expected dumps and walkthrough
value16 is declared with dw, and the buffer with resb:
section .data
value32: dd 0x11223344
value64: dq 0x1122334455667788
value16: dw 0xABCD
section .bss
scratch: resb 16
zero_page: resb 4096nm confirms the section via the type letter, and GDB confirms byte order:
0000000000403040 d value16
0x403040 <value16>: 0xcd 0xab
0x403054 <zero_page>: 0x00 0x00 0x00 0x00size after the edit:
text data bss dec hex filename
99 14 4116 4229 1085 build/mem_layoutSection headers show where the 4116 bytes come from:
[ 3] .data PROGBITS 0000000000403034 002034 00000e 00 WA 0 0 4
[ 4] .bss NOBITS 0000000000403044 002042 001014 00 WA 0 0 4In the object file, .bss occupies exactly 0x1010 = 4112 bytes: scratch takes 16 bytes starting at offset 0x0, and zero_page starts at offset 0x10 for 4096 bytes; there is no padding between them, so readelf -SW build/mem_layout.o reports a .bss size of 001010. When linked, .bss starts at 0x403044: .data grew from 12 to 14 bytes, and its end 0x403042 is 4-byte aligned up to 0x403044, pushing the next section 4 bytes forward—starting at 0x403044 instead of 0x403040. That alignment gap sits before the section and isn’t counted in its sh_size. The linker’s built-in script ends .bss with . = ALIGN(. != 0 ? 64 / 8 : 1);, aligning the section end to an 8-byte boundary. 0x403044 + 0x1010 = 0x404054 isn’t divisible by 8, so the end rounds up to 0x404058: 0x404058 - 0x403044 = 0x1014 = 4116. In the baseline mem_layout, .bss starts at 0x403040 and ends at 0x403050, which is already 8-byte aligned, so its size stays 0x10 = 16. Meanwhile, the file on disk grows by only a few dozen bytes—for the new value16 symbol, its DWARF entries, and two bytes in .data—while the 4096 bytes of zero_page never hit the disk.
Lab source code, Makefile, and automated tests for this lesson: lessons/06-memory-data.
Summary
.rodataholds read-only bytes in the file,.dataholds mutable initialized bytes, and.bssreserves an address range only..bsshas typeNOBITS: its size appears in the header, while no bytes are stored in the file.- In little-endian, the least significant byte sits at the lowest address, so
0x11223344reads as44 33 22 11. - String characters sit at ascending addresses and are never reversed.
nm,readelf -SW,size,objdump -s, andx/Nxbin GDB all inspect the same layout from different angles.- Square brackets in an operand mean dereferencing:
mov eax, dword [value32]reads memory,mov dword [scratch], eaxwrites to it, andleaonly computes an address.
inc leaves the carry flag untouched.