Introduction
ELF is the format of programs, shared libraries, object files and core dumps on Linux, the BSDs and most other Unix-like systems, as well as on the PlayStation consoles and in many embedded toolchains. It appeared in Unix System V Release 4 around 1989.
Most ELF programs have no extension at all: /bin/ls is an ELF file. The
extensions that do appear are .so for shared libraries, .o
for object files, .ko for Linux kernel modules, and .elf or
.axf for firmware built by embedded toolchains.
Structure
An ELF file starts with the ELF header. Its first 16 bytes, the identification, give
the magic 7F 45 4C 46 (\x7FELF), the class (1 for 32-bit, 2
for 64-bit), the byte order (1 for little-endian, 2 for big-endian), the version and the
target system’s ABI. Everything after that is read in the class and byte order those
bytes give. The rest of the header gives the file type (relocatable object, executable,
shared object or core dump), the machine, the entry point and the positions of two
tables.
The two tables are two views of the same bytes. The program header table lists
segments: what the system loads into memory, and where. The section header table lists
sections: .text, .data, the symbol tables, the debug
information and so on, which the linker and debuggers use. An object file needs only
sections and a program can run with only segments.
Every field and code value is on the reference pages: ELF header (identification, file types, machines, OS/ABI values), sections (section types, flags, groups, compression, special sections), program headers and notes (segment types, permissions, note types), symbols and relocations (with the x86-64 and AArch64 relocation types) and dynamic linking (dynamic tags, flags, hash table, symbol versioning).
0x00e_ident: magic, class, byte order, version, ABI16 bytes0x10e_type, e_machine, e_version, e_entry, table offsets, sizes and counts36 or 48 bytese_phoffprogram header table: one entry per segmentvaries…sections and segments: code, data, symbols, strings, debug informationvariese_shoffsection header table: one entry per sectionvaries
In the explorer
Drop an ELF file on the explorer, with or without an extension (it is known by its first four bytes). It shows:
- the header field by field (class, byte order, OS/ABI, type, machine by name, flags decoded for ARM, RISC-V, MIPS, AVR, ARC, PowerPC64, Xtensa, C-SKY and LoongArch), in both classes and both byte orders;
- the program headers and section headers with their types and flags named, and every section as what it holds: symbol tables (with symbol versions), relocations (type names for i386, x86-64, ARM, AArch64, RISC-V, MIPS, m68k and ColdFire, PowerPC, PowerPC64, AVR, MSP430 and Xtensa; numbers for other machines), the dynamic section with libraries, run path and flags, notes (build ID, ABI tag, GNU properties, Android), SysV and GNU hash tables, version tables, string tables, start-up and exit function arrays, section groups and compressed-section headers;
- a file with no section headers (stripped by hand, or a core file): the dynamic section, interpreter and notes are read from the program headers;
- the code of every executable section, disassembled for x86-64, 32-bit x86 (and 16-bit, by choice), AArch64, 32-bit ARM, Thumb and Thumb-2 (machine type 40, ARM and Thumb state from the mapping symbols and the architecture from
.ARM.attributes; VFP, Advanced SIMD (NEON) and the AES, SHA and PMULL instructions are decoded; MVE, half-precision, dot-product and complex-number instructions and the v8-Mttinstructions are not), RISC-V, MIPS (machine types 8 and 10), SuperH (machine type 42; the chip frome_flags, SH-1 to SH-4A, with choices for the FPSCR modes), the Z80 family (Z80, eZ80, SM83, 8080 and 8085, machine type 220, with a Mode choice), AVR (machine type 83; the core frome_flags, with a choice of aliases, byte or word addresses and the I/O register names of four devices), MSP430 and MSP430X (machine type 105; the CPU from.MSP430.attributes), the Motorola 68000 family and ColdFire (machine types 4 and 52; the chip frome_flags), PowerPC and PowerPC64 (machine types 20 and 21; 32-bit, 64-bit, AltiVec and Gekko), the DEC PDP-11 (machine type 65) and Xtensa (machine type 94; ESP8266, ESP32, ESP32-S2 and ESP32-S3), with the symbols as labels, the entry point marked and, for x86 and AArch64, the PLT stubs named after the functions they reach. For every other machine the sections are shown as bytes and the viewer says there is no decoder yet.
The options bar offers Disassemble as: the processor comes from e_machine and the choice only overrides it (a Thumb or RISC-V decoder will appear there when it exists). A .ko kernel module is read the same way, as a relocatable object.
The viewer’s output was compared field by field with llvm-readobj on 45 files built with clang and lld (x86-64, i386, AArch64, ARM, RISC-V, MIPS, PowerPC, AVR, MSP430, LoongArch, BPF and others, both byte orders) and 14 real files, and its x86-64, i386 and AArch64 instruction text with llvm-objdump.
Not described
Known, not yet covered
- Reference: for ARM, PowerPC, PowerPC64, MIPS, s390, SPARC, LoongArch, m68k, AVR, MSP430 and Xtensa only names and numbers are given (from glibc’s
elf.hor LLVM’sELFRelocsfiles); what each type computes is in that processor’s psABI, which was not read. Other processors (Alpha, IA-64, ARC, Hexagon, C-SKY, BPF and the many others) have no relocation table yet; each is in its own psABI, in glibc’self.hor in LLVM’sELFRelocsfolder. The Z80 family and the PDP-11 have no relocation list in either source. See symbols and relocations. - Reference: the i386 psABI’s own wording (only glibc’s comments were read);
e_flagsof the machines not listed on the header page; the GNU hash function;.eh_frame,.eh_frame_hdr, the ARM, RISC-V and other.attributessections and DWARF; s390 note values; the meaning of most GNU and Sun dynamic tags. - Viewer: only x86, x86-64, AArch64, 32-bit ARM and Thumb (without MVE, half-precision and dot-product instructions and the v8-M
ttinstructions), RISC-V, MIPS (32-bit encodings only: MIPS16e and microMIPS are not decoded), the Z80 family, AVR and MSP430 (with MSP430X), the 68000 family and ColdFire, SuperH, PowerPC and PowerPC64, the PDP-11 and Xtensa are disassembled (machine types 3, 6, 62, 183, 40, 243, 8, 10, 220, 83, 105, 4, 52, 42, 20, 21, 65 and 94); the other machines show bytes;.eh_frame,.eh_frame_hdr, the.attributessections and DWARF are shown as bytes; compressed sections are not unpacked; relocation names exist for 13 machine types (ColdFire shares the m68k names), numbers for the rest. - Viewer: checked on built files and on libraries and firmware from this Mac; there is no real core file, no real big-endian program, no real FreeBSD, Solaris or Android file, and no 32-bit x86 or AArch64 kernel module among them. Those branches are checked on built files only. It was not tried in a browser.
Also used for
- Linux kernel module – Loadable Linux drivers and features: ELF objects with module info.
- Mach-O executable and library – Programs, .dylib libraries and universal binaries on macOS and iOS.
Links
Software
- Linux and the BSDs – these and other Unix-like systems run ELF programs directly
- GNU Binutils – GNU’s binary tools, with
readelfandobjdump(Linux, macOS, Windows) - LLVM – compiler toolkit with
llvm-readelfandllvm-objdump(Linux, macOS, Windows) - GDB – the GNU debugger (Linux, macOS, Windows)
- Ghidra – the NSA’s free reverse engineering suite (Windows, macOS, Linux)
- IDA Pro – commercial disassembler and decompiler (Windows, macOS, Linux)
- Binary Ninja – commercial reverse engineering platform (Windows, macOS, Linux)
- radare2 – free command-line reverse engineering framework (Windows, macOS, Linux)
Developers
- elfutils – tools and the
libelfandlibdwlibraries (C) - pyelftools – reads ELF files and their DWARF debug information (Python)
- LIEF – reads and modifies ELF, PE and Mach-O (C++, with Python and Rust bindings)
- goblin – parses ELF, PE and Mach-O (Rust)
Reference
- elf(5) – the Linux manual page for the format
- Tool Interface Standard ELF specification – version 1.2 of the specification (PDF)