Skip to content
systemcoding.dev
Toolchains

Show program headers and segments with readelf -l

Read the program header table with readelf -l from GNU binutils 2.43 to see what the loader maps, which sections share a segment, and the interpreter path.

2 min read
Summarize in AI

Section headers describe the file for the linker. The loader never reads them. It maps the file using the program headers, and readelf -l prints exactly that table, plus a second table showing which sections landed in which segment. Run it when a binary will not start, when you wonder why a section is writable, or when you need the interpreter path.

Shell
readelf -lW demo
Text
Elf file type is DYN (Position-Independent Executable file)
Entry point 0x1060
There are 13 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000000040 0x0000000000000040 0x0002d8 0x0002d8 R   0x8
  INTERP         0x000318 0x0000000000000318 0x0000000000000318 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x000628 0x000628 R   0x1000
  LOAD           0x001000 0x0000000000001000 0x0000000000001000 0x00019d 0x00019d R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000 0x0000f4 0x0000f4 R   0x1000
  LOAD           0x002db8 0x0000000000003db8 0x0000000000003db8 0x000258 0x000260 RW  0x1000

Each LOAD row is one mmap, and MemSiz larger than FileSiz on the RW row is the .bss the kernel zero-fills. The Flg column is the real permission set: R E for code, RW for data, and an RWE anywhere is worth a second look. The -W flag stops readelf wrapping the table at 80 columns, which makes it much easier to read.

The sizes and addresses above are an example from a small PIE on x86-64, and yours will differ. The table is defined in the System V gABI, and readelf is documented in the binutils manual.

Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

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

Unsubscribe in one click. No spam, ever.