Skip to content
systemcoding.dev
Assembly

Organise Assembly Into .text, .data, .bss and .rodata Sections

Put code, constants, variables and zeroed buffers into the right ELF sections in NASM and GNU as, and see how segments and permissions follow from them.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the GNU, RISC-V and Intel logos.

An assembly file is not one blob of bytes. It is a set of sections, each a named container with its own permissions, and the linker packs them into the segments that the kernel maps into memory. Where you put a byte decides whether it can be written, whether it can be executed and whether it takes space in the file. Getting this right is the difference between a program that runs and one that crashes with a segmentation fault the first time it writes to a string. This post shows the four standard sections in NASM 2.16.03 and GNU as from binutils 2.42, the flags behind them, and how to check the result with readelf and size. Section layout in ELF is defined in the System V ABI.

The four sections you use every day

SectionHoldsMemory permissionsIn the file?
.textmachine coderead, executeyes
.rodataconstants, string literals, jump tablesreadyes
.datainitialized variablesread, writeyes
.bsszero-initialized variablesread, writeno, only its size

The reasoning is security and economy. Code that cannot be written cannot be patched by a stray store, and data that cannot be executed cannot be used as code by an attacker. Constants in a read-only section can be shared between processes and fail loudly if a bug writes to them. And .bss lets you declare a megabyte buffer without making the executable a megabyte larger.

Switching sections in each assembler

Both assemblers keep a current section, and every emitted byte goes into it. You switch with a directive.

NASM:

nasm
section .text
    ; code here

section .rodata
    ; constants here

section .data
    ; initialized variables

section .bss
    ; reserved space

GNU as:

asm
    .text
    # code here

    .section .rodata
    # constants here

    .data
    # initialized variables

    .bss
    # reserved space

GAS has shorthand directives .text, .data and .bss. For .rodata there is no shorthand, so you use .section .rodata. You can switch back and forth as often as you like; the assembler appends to each section in order, which is how a macro can emit a string into .rodata in the middle of a function. In GAS, .pushsection and .popsection make that safe by saving and restoring the current section, a technique used in the Linux kernel for tables built from many places in code.

A program that uses all four

sections.asmnasm
bits 64
default rel

global _start

section .rodata
banner:     db "total: ", 0
banner_len  equ $ - banner - 1

section .data
counter:    dq 5                     ; initialized, writable

section .bss
digit:      resb 1                   ; zero at start, writable

section .text
_start:
    add    qword [counter], 2        ; 5 + 2 = 7
    mov    rax, [counter]
    add    al, '0'
    mov    [digit], al               ; store the character

    mov    eax, 1
    mov    edi, 1
    lea    rsi, [banner]
    mov    edx, banner_len
    syscall

    mov    eax, 1
    mov    edi, 1
    lea    rsi, [digit]
    mov    edx, 1
    syscall

    mov    eax, 60
    xor    edi, edi
    syscall

section .note.GNU-stack noalloc noexec nowrite progbits
Shell
nasm -f elf64 sections.asm -o sections.o && ld sections.o -o sections && ./sections; echo
Text
total: 7

The constant text sits in .rodata, the counter in .data because it starts at 5 and changes, the output character in .bss because it has no initial value, and the code in .text. The echo at the end only supplies the newline the program does not print.

The same program in GAS

sections.sasm
    .globl  _start

    .section .rodata
banner:
    .ascii  "total: "
    .set    banner_len, . - banner

    .data
counter:
    .quad   5

    .bss
digit:
    .skip   1

    .text
_start:
    addq    $2, counter(%rip)
    movq    counter(%rip), %rax
    addb    $'0', %al
    movb    %al, digit(%rip)

    movl    $1, %eax
    movl    $1, %edi
    leaq    banner(%rip), %rsi
    movl    $banner_len, %edx
    syscall

    movl    $1, %eax
    movl    $1, %edi
    leaq    digit(%rip), %rsi
    movl    $1, %edx
    syscall

    movl    $60, %eax
    xorl    %edi, %edi
    syscall

    .section .note.GNU-stack,"",@progbits

The directive for zeroed space is .skip, with .space as a synonym and .comm for common symbols. The structure matches the NASM version line for line.

What the object file records

readelf -S lists each section with its flags. For the NASM object, the relevant lines are:

Text
  [Nr] Name              Type             Address           Off    Size   ES Flg Lk Inf Al
  [ 1] .rodata           PROGBITS         0000000000000000  000040 000007 00   A  0   0  1
  [ 2] .data             PROGBITS         0000000000000000  000048 000008 00  WA  0   0  4
  [ 3] .bss              NOBITS           0000000000000000  000050 000001 00  WA  0   0  4
  [ 4] .text             PROGBITS         0000000000000000  000050 000045 00  AX  0   0 16

The flags are A for allocated (mapped at run time), W for writable and X for executable. PROGBITS means the section has bytes in the file; NOBITS is .bss, which has a size and no contents. The offsets, sizes and alignments depend on the assembler and on your source.

After linking, size summarizes the three groups the loader cares about:

Shell
size sections
Text
   text	   data	    bss	    dec	    hex	filename
    ...	      8	      1	    ...	    ...	sections

The text column holds code and read-only data together, data the initialized writable data, and bss the zeroed space. The exact numbers depend on your build.

From sections to segments

The linker does not map sections individually. It groups them into segments with the same permissions, which readelf -l shows as program headers of type LOAD:

Text
  LOAD   0x000000 0x0000000000400000 ... R     0x1000
  LOAD   0x001000 0x0000000000401000 ... R E   0x1000
  LOAD   0x002000 0x0000000000402000 ... R     0x1000
  LOAD   0x002000 0x0000000000403000 ... RW    0x1000

The read-execute segment holds .text, the read-only segment holds .rodata, and the read-write segment holds .data and .bss. The page-sized alignment is why even a tiny program has an executable of a few kilobytes. These are the permissions the CPU enforces: a write to the first three segments faults, and a jump into the last faults on a system with the NX bit.

Seeing the protection work

Writing to a constant is a quick demonstration of what the sections buy you. Change the program to store into the banner:

nasm
    mov    byte [banner], 'T'      ; banner is in .rodata

Assembling and running it produces a crash:

Text
Segmentation fault (core dumped)

The write hits a page mapped read-only. Move banner to .data and the same instruction works. The same applies in the other direction: putting code in .data and jumping to it faults on a system that marks data pages non-executable, which is the default on modern kernels.

Place read-only data in .rodata anyway, even if a write would be harmless, so that bugs show up as crashes rather than as corruption.

Custom sections and flags

You can define your own sections when you need a table the linker can gather from many files, or a block with special permissions. NASM:

nasm
section .mytable progbits alloc noexec nowrite align=8
    dq 1, 2, 3

GAS:

asm
    .section .mytable,"a",@progbits
    .quad   1, 2, 3

The GAS flag letters are a (allocated), w (writable), x (executable), and M and S for mergeable strings. The string after the comma is the type: @progbits or @nobits. A linker script or a start-up routine can then find the table through the __start_mytable and __stop_mytable symbols that GNU ld generates for sections whose names are valid C identifiers, which is the mechanism behind many registration tables.

When the section name starts with .text. or .data., the linker merges it into the corresponding standard output section. That is how -ffunction-sections output is gathered, with one .text.name section per function so --gc-sections can drop the unused ones.

Where small constants go

A subtle choice is where to place a short constant, like a 16-byte mask used by SSE code, or a literal pool for floating-point values. The right answer is .rodata, aligned for the access. For constants that only the function needs, GCC emits them in .rodata.cst16 mergeable sections so the linker can deduplicate identical values across files. You can do the same by hand with .section .rodata.cst16,"aM",@progbits,16 followed by .p2align 4 and the data, though a plain .rodata with an alignment directive is simpler and works everywhere.

Common mistakes

Putting a mutable buffer in .rodata produces a crash on the first write. Putting a large initialized array in .data bloats the file; if its contents are zero, move it to .bss. Forgetting to switch back to .text after a data block, so that instructions end up in .data, assembles quietly and crashes at the first instruction with a segmentation fault at an address inside the data segment. Leaving out the .note.GNU-stack section makes the linker warn and mark the stack executable. And in GAS, writing .data in the middle of a function and forgetting .text afterward gives the same misplaced code.

Also inToolchains
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.