Segment Types

18 minintermediateModule 3: Program HeadersContinues from program-header-table

Why This Matters

Not all segments are created equal. Some contain code, some tell the dynamic linker where to find shared libraries, and some carry debugging information. Understanding segment types tells you what each part of the file is for.

A Simple Model

Think of segment types as labels on shipping boxes:

The Actual Detail

TypeValuePurpose
PT_NULL0Unused entry
PT_LOAD1Loadable segment — mapped into memory
PT_DYNAMIC2Dynamic linking information
PT_INTERP3Path to interpreter (dynamic linker)
PT_NOTE4Notes (build ID, ABI info)
PT_PHDR6Program header table itself
PT_TLS7Thread-local storage
PT_GNU_EH_FRAME0x6474e550Exception handling frame
PT_GNU_STACK0x6474e551Stack permissions (NX bit)
PT_GNU_RELRO0x6474e552Read-only after relocation

A typical executable has 2-4 PT_LOAD segments (read-only, read-execute, read-write) plus PT_DYNAMIC, PT_INTERP, PT_NOTE, and PT_GNU_STACK.

A Real Example

$ readelf -l /bin/ls
  Type     Offset   VirtAddr           Flg  What
  PHDR     ...      0x...0040          R    Program headers
  INTERP   ...      0x...02b8          R    /lib64/ld-linux-x86-64.so.2
  LOAD     0x000000 0x0000000000000000 R    Read-only (headers)
  LOAD     0x001000 0x0000000000001000 R E  Executable code
  LOAD     0x003000 0x0000000000003000 R    Read-only data
  LOAD     0x003dc0 0x0000000000004dc0 RW   Read-write data
  DYNAMIC  ...      0x...4e28          RW   Dynamic section
  GNU_STACK ...     0x0000000000000000 RW   Stack (NX enabled)

Check Your Understanding

Which segment type tells the kernel where the dynamic linker is?

Apply It

If a program has 3 PT_LOAD segments, how many memory regions does the kernel map?

Connect

Segments tell the loader what to load. But how does the loader map file offsets to virtual addresses? That's memory mapping (next concept).