ELF Format Deep Dive
Linking, Loading & Binary Formats · OS Core Internals track
6 min read
In this module
The ELF Format
Executable and Linkable Format (ELF) is the standard binary format on Linux, BSD, and most Unix-likes. Every executable, shared library, and object file on these systems is an ELF file. Knowing its structure is bedrock for reverse engineering, binary analysis, and exploit development.
The ELF Header
Every ELF file opens with a fixed-size header that identifies the file and describes how to interpret the rest. The first four bytes are the magic number:
# The ELF magic bytes7f 45 4c 46# E L FYou can inspect the ELF header with readelf:
readelf -h /usr/bin/lsThe header tells you the architecture (x86-64, ARM, etc.), what kind of binary this is (executable, shared library, object file), and where to find the two tables that describe the file's contents: the program header table and the section header table.
An ELF file shows two overlapping views. Program headers tell the kernel how to load the binary into memory at runtime. Section headers tell the linker and debugger how to interpret the file's contents. The same bytes can appear in both views.
Worked Example: Reading the First 32 Bytes
Dump the start of a binary with xxd -l 32 /usr/bin/ls and you get something like this:
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............00000010: 0300 3e00 0100 0000 c06a 0000 0000 0000 ..>......j......Every byte has a job. Reading left to right:
| Offset | Bytes | Field | Meaning |
|---|---|---|---|
0x00 | 7f 45 4c 46 | Magic | \x7f followed by E, L, F |
0x04 | 02 | Class | 64-bit (01 would be 32-bit) |
0x05 | 01 | Data | Little-endian |
0x06 | 01 | Version | Always 1 |
0x07 | 00 | OS/ABI | System V, the usual value on Linux |
0x10 | 03 00 | Type | ET_DYN: a shared object |
0x12 | 3e 00 | Machine | 0x3e = 62 = x86-64 |
0x14 | 01 00 00 00 | Version | 1 again |
0x18 | c0 6a 00 00 ... | Entry point | 0x6ac0, an offset from wherever the file gets loaded |
The multi-byte fields are little-endian, as the header itself announced at offset 5: 3e 00 reads as 0x003e. The entry point (8 bytes, continuing into the next line) is where execution starts once the dynamic linker has done its work. The exact value differs between builds of ls, so expect your own dump to differ there.
Predict: why does the header say ET_DYN, a shared object, for /usr/bin/ls, which is obviously a program?
Because it's built as a position-independent executable (PIE). A PIE has to be loadable at any address so ASLR can move it, and in ELF terms that makes it the same kind of file as a shared library. readelf -h shows the type as DYN (Position-Independent Executable file) on recent binutils. An old-style executable that only works at a fixed address would say EXEC, and its entry point would be a full absolute address such as 0x401000 rather than a small offset.
Program Headers (Segments)
The program header table divides the file into segments: contiguous chunks the kernel maps into memory when executing the binary. Each program header specifies:
- Type - what kind of segment (loadable, dynamic linking info, interpreter path, etc.)
- Offset and size - where in the file the segment lives
- Virtual address - where in the process's address space to load it
- Permissions - read, write, execute
The most important segment types:
PT_LOAD- data to be mapped into memory. Older toolchains emitted two: one read-execute (code plus read-only data) and one read-write (data). Modern linkers keep code apart from read-only data (-z separate-code, the default in GNU ld on x86-64 Linux since 2.31), so a typical binary today has four: R (headers and early read-only data), R-X (code), R (.rodata), and RW (data).PT_INTERP- the path to the dynamic linker (/lib64/ld-linux-x86-64.so.2)PT_DYNAMIC- a table of entries needed for dynamic linking (shared library dependencies, symbol tables, relocation entries)PT_GNU_STACK- controls whether the stack is executable. If this header is present with no execute permission, the kernel enforces a non-executable stack (NX/DEP). If it's missing entirely, the kernel falls back to the old behaviour: on x86 that means an executable stack (and on 32-bit x86, every readable mapping becomes executable)
readelf -l /usr/bin/ls # Display program headersSection Headers (Sections)
The section header table offers a finer-grained view, slicing the file into named sections. The linker and debugger lean on these:
.text- compiled machine code (executable instructions).data- initialised global variables.bss- uninitialised global variables (occupies no file space; zeroed at load).rodata- read-only data (string literals, constants).symtab/.dynsym- symbol tables (function and variable names with addresses).strtab/.dynstr- string tables for symbol names.plt/.got- procedure linkage table and global offset table (for dynamic linking).rel.dyn/.rela.plt- relocation entries
readelf -S /usr/bin/ls # Display section headersTwo Views, Same File
Here's the trick worth holding onto: program headers and section headers describe overlapping regions of the same file. The .text section lives inside a PT_LOAD segment. The kernel only needs program headers to load and run the binary. Section headers can be stripped entirely without affecting execution. This is common in production binaries and malware, which strip section headers to hinder analysis.
Explore the Format
The explorer below walks through the key ELF header fields and the main sections. Click each one to see its bytes and what the loader does with it. The PE and Mach-O tabs show the same ideas in Windows and Apple clothing: a magic number, a machine type, an entry point, and a table of what to map where.
Click a section to explore its structure and purpose
Why It Matters
ELF analysis is the starting point for nearly all Linux binary work. Whether you're reverse engineering malware, developing exploits, or hardening binaries, you'll be reading the ELF header to identify the architecture, checking PT_GNU_STACK to gauge NX protection, finding .got and .plt for hooking targets, and locating .symtab to map functions. You'll be reading these structures for as long as you work with Linux binaries, so it pays to get fluent early.