On this page

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.

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

resb 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 16 enforces alignment of the next address up to a 16-byte boundary: in .data, padding may appear between objects;
  • times BIG_SIZE db 0xAA repeats the directive on the right the specified number of times at assembly time, not runtime. The repeat count is a compile-time constant, so times 16 db 0 in .data writes 16 real zero bytes into the file, whereas resb 16 only reserves 16 addresses; in data_10m.asm (just below), this generates 10 MiB of 0xAA bytes.

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.

address +0
low byte 44

address +1
byte 33

address +2
byte 22

address +3
high byte 11

address +0
low byte 44

address +1
byte 33

address +2
byte 22

address +3
high byte 11

Fig. 1. The integer 0x11223344 in x86-64 memory

Strings work differently: their bytes sit at increasing addresses in the exact order characters are written. Only multi-byte integers are byte-reversed.

string ok

+0: 0x6f, o

+1: 0x6b, k

+2: 0x0a, newline

string ok

+0: 0x6f, o

+1: 0x6b, k

+2: 0x0a, newline

Fig. 2. A string in memory: characters are never 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 = 0x1122334455667788

nm 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 scratch
  • r — 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: .rodata comes before .data, and .bss sits 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 0x11

In 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;
  • 44332211 and 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 of value32 and loads them into a register—identical to uint32_t x = *p; if p points to value32;
  • mov dword [scratch], eax goes the other way and matches *q = x;: the register’s value is written to memory at scratch;
  • the dword size 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          287454021
  • value32 in memory stayed unchanged: reading doesn’t modify the source;
  • scratch in .bss now holds 0x11223345—the low byte incremented from 0x44 to 0x45, while the upper bytes remained untouched because the carry never left the 32-bit boundary;
  • eax holds 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:

10⋅220=10 485 760 bytes=0xA00000 10 \cdot 2^{20} = 10\,485\,760\text{ bytes} = \mathtt{0xA00000}
data_10m.asm
; 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
bss_10m.asm
; 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 progbits

The file sizes differ by nearly 10 MiB:

$ stat -c '%n %s' build/data_10m build/bss_10m
build/data_10m 10495176
build/bss_10m 5328

stat -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 16
  • PROGBITS — bytes physically live in the file at offset Off;
  • NOBITS — the section exists purely as an address range and size;
  • Size is a00000 (10 MiB) on both lines;
  • Off on .bss points 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_10m

The 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

  1. "db can hold any integer."
    Fact: bad_db.asm declares the integer as a byte:

    nasm
    section .data
        value32: db 0x11223344

    NASM 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                                   D

    Cause: db is strictly one byte; the upper bytes of 0x11223344 don’t fit and get truncated.
    Fix: use dd for 32-bit values and dq for 64-bit values, and verify widths in a hex dump.

  2. “Writing one byte updates the entire 32-bit field.”
    Fact: after mov byte [value32], 1, only the first byte changes in the dump:

    text
    before: 0x402000 <value32>: 0x44 0x33 0x22 0x11
    after:  0x402000 <value32>: 0x01 0x33 0x22 0x11

    Cause: write width is determined by the operand size, not the symbol’s type in source code.
    Fix: write mov dword [value32], 1 when you want to overwrite the whole field.

  3. “You can set an initial value in .bss.”
    Fact: bad_bss.asm declares an initialized buffer:

    nasm
    section .bss
        buffer: db 1

    NASM 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  4

    Cause: .bss has no bytes in the file, so initial values can’t be stored there.
    Fix: keep non-zero initial values in .data, and use only resb/resd/resq in .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 value16 set to 0xABCD in .data;
  • add a 4096-byte zero_page buffer in .bss;
  • program output and exit code stay unchanged;
  • the file size on disk must not grow by 4 KiB.

Verification criteria.

  1. nm -n build/mem_layout | grep value16 shows type d.
  2. gdb -q --nx build/mem_layout with break _start and x/2xb &value16 prints 0xcd 0xab.
  3. x/4xb &zero_page prints four zero bytes.
  4. size build/mem_layout shows bss growing 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:

nasm
section .data
    value32: dd 0x11223344
    value64: dq 0x1122334455667788
    value16: dw 0xABCD

section .bss
    scratch: resb 16
    zero_page: resb 4096

nm confirms the section via the type letter, and GDB confirms byte order:

text
0000000000403040 d value16
0x403040 <value16>: 0xcd 0xab
0x403054 <zero_page>: 0x00 0x00 0x00 0x00

size after the edit:

text
   text	   data	    bss	    dec	    hex	filename
     99	     14	   4116	   4229	   1085	build/mem_layout

Section headers show where the 4116 bytes come from:

text
  [ 3] .data             PROGBITS        0000000000403034 002034 00000e 00  WA  0   0  4
  [ 4] .bss              NOBITS          0000000000403044 002042 001014 00  WA  0   0  4

In 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

  1. .rodata holds read-only bytes in the file, .data holds mutable initialized bytes, and .bss reserves an address range only.
  2. .bss has type NOBITS: its size appears in the header, while no bytes are stored in the file.
  3. In little-endian, the least significant byte sits at the lowest address, so 0x11223344 reads as 44 33 22 11.
  4. String characters sit at ascending addresses and are never reversed.
  5. nm, readelf -SW, size, objdump -s, and x/Nxb in GDB all inspect the same layout from different angles.
  6. Square brackets in an operand mean dereferencing: mov eax, dword [value32] reads memory, mov dword [scratch], eax writes to it, and lea only computes an address.
Next Up
Bytes and sections make sense now, yet arithmetic still looks like “a result in a register.” In lesson 07, I unpack the next layer—four status bits: zero, sign, carry, and overflow. They explain why the maximum unsigned 32-bit integer plus one produces zero with a carry, while inc leaves the carry flag untouched.