Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:ELF (Executable and Linkable Format)

From ReverseEngineering.wiki
(Redirected from Format:ELF)
Format
Developer UNIX System Laboratories (System V Release 4, 1989); now the System V gABI plus per-processor psABI supplements
Versions e_version 1 (EV_CURRENT) only
Magic 7F 45 4C 46 at 0, then EI_CLASS (1 = 32-bit, 2 = 64-bit) and EI_DATA (1 = little, 2 = big endian)
Extensions none (executables), .so, .o, .ko, .elf, .axf, .prx, .debug
Endianness either, given by e_ident[5] (EI_DATA)
On this wiki Facts · Code · Notes · Topics

Object, executable, shared-library and core-dump format of Linux, Android, the BSDs, Solaris and most embedded toolchains and consoles: the magic 7F 45 4C 46 ("\x7fELF"), a 52- or 64-byte header giving class (32/64-bit), byte order, OS ABI, file type and machine, then a program header table (segments, what the loader maps) and an optional section header table (sections, what linkers and debuggers use).

PlayStation 2 game executables are 32-bit little-endian MIPS ELFs with e_machine 8 and an R5900 flag in e_flags: see Format:PS2 EE executable (ELF).

Identification

  • Bytes 0-3 are 7F 45 4C 46 ("\x7fELF"). The rest of the 16-byte e_ident: [4] EI_CLASS (1 ELFCLASS32, 2 ELFCLASS64), [5] EI_DATA (1 little endian, 2 big endian; every later multi-byte field follows it), [6] EI_VERSION (1), [7] EI_OSABI, [8] EI_ABIVERSION, [9-15] zero padding. A 64-bit little-endian Linux file starts 7F 45 4C 46 02 01 01 00.
  • EI_OSABI is 0 (System V) on most Linux files, even though 3 means GNU/Linux: in the 111 top-level ELF files of pyelftools' two test directories 102 have 0, 3 have 3 (clang-built objects and one executable), 2 have 9 (FreeBSD) and 4 have 6 (Solaris, ABI version 1). Do not identify the OS from this byte alone; use the notes and PT_INTERP (below).
  • e_type (offset 16): 1 ET_REL (.o, .ko kernel modules), 2 ET_EXEC (fixed-address executable), 3 ET_DYN (shared object or position-independent executable), 4 ET_CORE (core dump). A PIE is ET_DYN with a PT_INTERP segment and, from recent linkers, DF_1_PIE (0x08000000) in DT_FLAGS_1.
  • e_machine (offset 18): 3 x86, 62 x86-64, 40 ARM (32-bit), 183 AArch64, 8 MIPS, 20 PowerPC, 21 PPC64, 22 S/390, 43 SPARC V9, 18 SPARC32PLUS, 42 SuperH, 44 TriCore, 94 Xtensa, 243 RISC-V, 247 eBPF, 258 LoongArch, 224 AMD GPU, 190 NVIDIA CUDA. See Fact:ELF (Executable and Linkable Format)/enum/e machine.
  • e_flags is processor-specific. ARM: EABI version in the top byte (0x05000000 = EABI5), 0x400 hard-float and 0x200 soft-float (objects without floating-point code may have neither), 0x00800000 BE8 (a big-endian sample has 0x05800200). MIPS: ABI and ISA level; RISC-V: float ABI and RVC.

Layout

Offsets and sizes for ELF32 / ELF64:

Offset Size Structure
0 52 / 64 ELF header (Elf32_Ehdr / Elf64_Ehdr): e_ident, e_type, e_machine, e_version, e_entry, e_phoff, e_shoff, e_flags, e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx
e_phoff (usually 52 / 64) e_phnum x 32 / 56 Program header table (Elf_Phdr): the segments
Section contents (code, data, string tables, symbol tables, notes, debug info)
e_shoff (usually near the end) e_shnum x 40 / 64 Section header table (Elf_Shdr); entry 0 is all zero (or holds the extended counts)

The 64-bit program header moves p_flags to the second field (p_type, p_flags, p_offset, p_vaddr, p_paddr, p_filesz, p_memsz, p_align) so the 64-bit fields stay aligned; the 32-bit one has it seventh. The symbol entry is reordered the same way (see #Symbols). A parser that reuses one field order for both classes reads garbage.

Extended numbering. With 0xFF00 or more sections e_shnum is 0 and the real count is in sh_size of section header 0; e_shstrndx is then 0xFFFF (SHN_XINDEX) and the real index is in sh_link of section 0; with 0xFFFF or more segments e_phnum is 0xFFFF (PN_XNUM) and the real count is in sh_info of section 0. pyelftools' many_sections.o.elf (65287 sections, GCC -ffunction-sections) exercises the first two.

Segments and sections

Two views of the same bytes:

  • Segments (program headers) are all the kernel and the dynamic loader look at. PT_LOAD entries map p_filesz bytes from p_offset to p_vaddr; p_memsz > p_filesz zero-fills the rest (.bss). Flags: PF_X = 1, PF_W = 2, PF_R = 4 (so 5 = R-X, 6 = RW-). Other types: PT_DYNAMIC (2, the dynamic table), PT_INTERP (3, path of the dynamic loader), PT_NOTE (4), PT_PHDR (6), PT_TLS (7), and the GNU ones PT_GNU_EH_FRAME 0x6474E550, PT_GNU_STACK 0x6474E551 (its flags say whether the stack is executable), PT_GNU_RELRO 0x6474E552, PT_GNU_PROPERTY 0x6474E553.
  • Sections are for the linker, debuggers and disassemblers and are optional at run time: strip --strip-section-headers, packers and hostile files remove or falsify them, and the program still runs. Section names come from the string table section e_shstrndx.
  • Virtual address to file offset: find the PT_LOAD with p_vaddr <= va < p_vaddr + p_filesz; offset = va - p_vaddr + p_offset. Every pointer in the dynamic table (DT_STRTAB, DT_SYMTAB, DT_HASH...) is such a virtual address, not a file offset; in shared objects and PIEs they are relative to load base 0.
  • Typical PT_INTERP strings: /lib64/ld-linux-x86-64.so.2 (glibc x86-64), /lib/ld-linux.so.2 (glibc i386), /lib/ld-linux-aarch64.so.1, /lib/ld-linux-armhf.so.3, /lib/ld-musl-<arch>.so.1 (musl), /system/bin/linker or linker64 (Android), /libexec/ld-elf.so.1 (FreeBSD). No PT_INTERP: a static executable, or a firmware/kernel image.

Common sections: .text, .rodata, .data, .bss (SHT_NOBITS, no file bytes), .init/.fini and .init_array/.fini_array (constructor pointer arrays), .plt / .plt.got / .plt.sec (lazy-binding stubs) and .got / .got.plt, .dynsym/.dynstr, .symtab/.strtab, .rel(a).dyn/.rel(a).plt, .eh_frame and .eh_frame_hdr (unwind tables, present even in C programs), .gnu.version* (symbol versions), .comment, .note.*, .debug_* (DWARF). sh_link and sh_info depend on the type: for SHT_SYMTAB/SHT_DYNSYM, link = string table and info = index of the first non-local symbol; for SHT_REL/SHT_RELA, link = symbol table and info = section being patched; for SHT_DYNAMIC and SHT_HASH, link = string or symbol table.

Symbols

  • Elf32_Sym (16 bytes): st_name, st_value, st_size, st_info, st_other, st_shndx. Elf64_Sym (24 bytes): st_name, st_info, st_other, st_shndx, st_value, st_size.
  • st_info = bind << 4 | type. Bind: 0 LOCAL, 1 GLOBAL, 2 WEAK, 10 GNU_UNIQUE. Type: 0 NOTYPE, 1 OBJECT, 2 FUNC, 3 SECTION, 4 FILE, 5 COMMON, 6 TLS, 10 GNU_IFUNC (a resolver function that returns the implementation, used by glibc for memcpy and friends). Visibility is st_other & 3 (0 default, 1 internal, 2 hidden, 3 protected).
  • st_shndx: 0 SHN_UNDEF (an import), 0xFFF1 SHN_ABS, 0xFFF2 SHN_COMMON, 0xFFFF SHN_XINDEX (real index in SHT_SYMTAB_SHNDX).
  • .symtab (all symbols, including static functions) is removed by strip; .dynsym (imports and exports) must stay because the loader needs it. On ARM, symbol values of Thumb functions have bit 0 set, and $a, $t, $d mapping symbols mark ARM code, Thumb code and data.
  • Without section headers, the dynamic symbol table is found through DT_SYMTAB / DT_STRTAB, and its length through the hash table: nchain of DT_HASH, or the end of the last GNU hash chain. See #Hash tables.

Hash tables

The dynamic loader looks names up through a hash table: the original SysV DT_HASH (.hash: nbucket, nchain, bucket[], chain[], with elf_hash) and the GNU DT_GNU_HASH (.gnu.hash: nbuckets, symoffset, bloom_size, bloom_shift, a Bloom filter of ELFCLASS-sized words, buckets[], then one 32-bit hash per symbol from symoffset on, bit 0 marking the end of a chain, with the djb2-style gnu_hash). Modern Linux distributions link with --hash-style=gnu only; Android and older or embedded files often have both or only DT_HASH. The GNU table only covers the symbols from symoffset on (exports, sorted by bucket); undefined imports come before it.

Both hash functions, the tables and the symbol-count recovery were checked with Code:ELF (Executable and Linkable Format)/elfinfo (Python) on every ELF file of pyelftools' test corpus: 26 DT_HASH tables (2711 named symbols, each found through its bucket chain) and 40 DT_GNU_HASH tables (2812 symbols, each found and each passing the Bloom filter). The one failure is lib_with_two_dynstr_sections_reversed.so.elf, a deliberately doctored file whose DT_STRTAB no longer points at the real names. Facts with test vectors: Fact:ELF (Executable and Linkable Format)/behavior/SysV elf hash, Fact:ELF (Executable and Linkable Format)/behavior/GNU hash.

Dynamic section

PT_DYNAMIC points to an array of {Elf_Sxword d_tag; Elf_Xword d_val} pairs (8 or 16 bytes) ending with DT_NULL. String-valued tags (DT_NEEDED 1, DT_SONAME 14, DT_RPATH 15, DT_RUNPATH 29) are offsets into DT_STRTAB (5); pointer tags are virtual addresses. The DT_NEEDED list is the import list of a shared object or program (like the PE import directory, but per library, not per function: which function comes from which library is only decided at load time, unless symbol versioning ties it, e.g. printf@GLIBC_2.2.5 via .gnu.version_r). Other tags: DT_SYMTAB 6, DT_HASH 4, DT_GNU_HASH 0x6FFFFEF5, DT_RELA/RELASZ/RELAENT 7/8/9, DT_REL 17, DT_JMPREL 23 (PLT relocations), DT_PLTGOT 3, DT_INIT/FINI 12/13, DT_INIT_ARRAY 25, DT_FLAGS 30, DT_FLAGS_1 0x6FFFFFFB, DT_VERSYM 0x6FFFFFF0, DT_VERNEED 0x6FFFFFFE, DT_RELR 36 (compact relative relocations), and Android's packed relocations DT_ANDROID_REL / RELA (0x6000000F / 0x60000011, data starting "APS2").

Symbol version tables (.gnu.version, .gnu.version_r, .gnu.version_d) are described on Format:ELF (Executable and Linkable Format)/Symbol versioning.

Relocations

Elf_Rel = {r_offset, r_info}, Elf_Rela adds a signed r_addend (8/12 bytes in ELF32, 16/24 in ELF64). ELF32: symbol = r_info >> 8, type = r_info & 0xFF. ELF64: symbol = r_info >> 32, type = r_info & 0xFFFFFFFF. Exception: 64-bit MIPS stores r_info as a 32-bit symbol index followed by four bytes r_ssym, r_type3, r_type2, r_type (three composed relocation types), in either byte order; reading it as one 64-bit integer on a little-endian MIPS64 file gives nonsense (checked on pyelftools' mips64-relocs-le.o.elf: bytes 0b 00 00 00 00 05 18 07 = symbol 11 foo, types R_MIPS_GPREL16 (7), R_MIPS_SUB (24), R_MIPS_HI16 (5)). x86-64 relocation types worth knowing: 1 R_X86_64_64, 2 PC32, 4 PLT32, 6 GLOB_DAT, 7 JUMP_SLOT, 8 RELATIVE, 37 IRELATIVE.

More: relocation types and PLT/GOT stub bytes (x86-64, AArch64, IBT/BTI), TLS models, init arrays and IFUNC, APS2 and RELR.

Notes

A note is {uint32 n_namesz; uint32 n_descsz; uint32 n_type}, the owner name (NUL-terminated, counted in n_namesz) padded to 4 bytes, then the descriptor padded to 4 bytes; the type is only meaningful together with the owner name. In 8-aligned note sections (.note.gnu.property of 64-bit files, sh_addralign/p_align 8) the padding is to 8 bytes, measured from the start of the note. Notes seen in practice:

Owner Type Section Content
GNU 1 NT_GNU_ABI_TAG .note.ABI-tag 4 words: OS (0 Linux, 1 Hurd, 2 Solaris, 3 FreeBSD), minimum kernel major, minor, patch (e.g. Linux 3.2.0)
GNU 3 NT_GNU_BUILD_ID .note.gnu.build-id the build ID, naming the matching debug file: 20 bytes (SHA-1, the default of GNU ld and gold) in all but one sample of the corpus, 16 bytes (md5 or uuid) in the Android gold-linked one; LLD writes none unless --build-id is given (both LLD-linked samples have none)
GNU 4 NT_GNU_GOLD_VERSION .note.gnu.gold-version "gold 1.12": linked by GNU gold
GNU 5 NT_GNU_PROPERTY_TYPE_0 .note.gnu.property properties: 0xC0000002 x86 IBT/SHSTK (CET), 0xC0000000 AArch64 BTI/PAC, 0xC0008002 / 0xC0010002 x86 ISA level
Go 4 .note.go.buildid Go build ID, see Library:Go runtime
Android 1 .note.android.ident uint32 target API level (23 in pyelftools' sample), newer NDKs add the NDK version strings
FreeBSD 1 .note.tag __FreeBSD_version
CORE / LINUX 1 NT_PRSTATUS, 2 FPREGSET, 3 PRPSINFO, 6 AUXV, 0x46494C45 NT_FILE, 0x53494749 NT_SIGINFO core files (PT_NOTE) registers, process info, mapped files

Debug information

  • .debug_info, .debug_line, .debug_abbrev, .debug_str... are Format:DWARF. .eh_frame uses the DWARF CFI format too (Format:DWARF/.eh_frame), but is loaded and always present.
  • Separate debug files: the stripped binary keeps a .gnu_debuglink section (file name, NUL, zero padding to a multiple of 4, then the CRC-32 of the whole debug file, the zlib/PNG CRC-32 in the file's byte order) and/or the build ID. Debuggers look in /usr/lib/debug/.build-id/<first 2 hex digits>/<rest>.debug, and debuginfod servers serve /buildid/<hex>/debuginfo. Checked on pyelftools' debuglink: the stored CRC 0x29B7C5F1 equals zlib.crc32 of debuglink.debug.
  • Compressed sections have SHF_COMPRESSED (0x800) and start with Elf_Chdr (ch_type, ch_size, ch_addralign, 12 bytes in ELF32; 24 in ELF64 with a reserved word after ch_type); ch_type 1 is zlib (Format:Zlib stream), 2 zstd (Format:Zstandard; unverified on this wiki: the data after the header should be a normal frame starting 28 B5 2F FD) (gABI; pyelftools' enum at the cited commit only knows 1). Older toolchains used .zdebug_* sections starting "ZLIB" plus an 8-byte big-endian size instead.
  • .gnu_debugdata ("MiniDebugInfo", GDB manual; used by Fedora and others) holds a whole Format:XZ-compressed ELF with a reduced .symtab of function names for stripped binaries. Unverified on this wiki: check that the section starts FD 37 7A 58 5A 00 and decompresses to an ELF with Python's lzma.decompress.

Compiler and toolchain identification

Without disassembly, these name the toolchain (all seen in pyelftools' corpus):

  • .comment (SHT_PROGBITS, NUL-separated strings, one per contributing compiler or linker): GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GCC: (GNU) 12.1.0, clang version 10.0.0, Linker: LLD 14.0.6, ARM Linker, 5.03 [Build 76] and ARM Assembler, 5.03 (Arm Compiler 5), IAR ANSI C/C++ Compiler V9.40.1.364/W64 ... for ARM. strip keeps .comment unless told otherwise.
  • A .note.gnu.gold-version note means GNU gold; a .note.go.buildid note and a .gopclntab section mean Go (Format:Go pclntab); .note.android.ident means an Android NDK build; Altium notes appear in TASKING (TriCore) executables.
  • C++ code: _Z symbols, imports of __cxa_throw / __gxx_personality_v0 and relocations against _ZTVN10__cxxabiv117__class_type_infoE mean GCC or Clang C++ under the Platform:Itanium C++ ABI; the exception class constant in the runtime separates libstdc++ ("GNUCC++") from libc++abi ("CLNGC++"), and std::__1::/std::__ndk1:: versus std::__cxx11:: names tell libc++ from libstdc++.
  • PT_INTERP names the C library (glibc, musl, Bionic) the program was linked for. For glibc programs the _start bytes at e_entry and the highest GLIBC_2.x symbol version also date the build environment (before or after glibc 2.34): see Library:GNU C Library.

Tools

  • readelf (binutils; -h -l -S -s -d -n -r -V), objdump, llvm-readelf / llvm-readobj, eu-readelf (elfutils), file.
  • pyelftools (Python, public domain): headers, sections, segments, symbols, relocations, dynamic tags, notes, symbol versions, hash tables, DWARF; its test corpus covers x86, ARM, AArch64, MIPS, PowerPC, RISC-V, SPARC, S/390 and LoongArch files.
  • LIEF (C++/Python, Apache-2.0) reads and rewrites ELF, PE and Mach-O.
  • Code:ELF (Executable and Linkable Format)/elfinfo (Python): dependency-free inspector written for this page.
  • Ghidra and IDA load ELF directly and apply .symtab/.dynsym names and DWARF.

Topics

  • Symbol versioning — GNU symbol versioning of ELF: .gnu.version (Versym, one uint16 per .dynsym entry), .gnu.version_r (Verneed/Vernaux: versions this file needs from each…
  • Relocations and PLT — x86-64 and AArch64 relocation types an analyst meets in dynamic ELF files, and what the .plt, .plt.sec and .got.plt stubs look like in bytes (classic lazy…
  • Android packed relocations and RELR — The compact relocation encodings of Android and modern Linux ELF files: Android's APS2 SLEB128 packed relocations (DT_ANDROID_REL/RELA), SHT_RELR/DT_RELR…
  • TLS — Thread-local storage in ELF: the PT_TLS segment (.tdata/.tbss), STT_TLS symbols, the four access models (general dynamic, local dynamic, initial exec, local…
  • Initialization arrays and IFUNC — How ELF code runs before main and how IFUNC symbols pick an implementation: DT_PREINIT_ARRAY, DT_INIT, DT_INIT_ARRAY, DT_FINI_ARRAY (and legacy .ctors)…
  • Format:ELF (Executable and Linkable Format)/Core dumps - ET_CORE files: NT_PRSTATUS, NT_PRPSINFO, NT_FILE, NT_AUXV layouts.
  • Core dumps — Linux ELF core files (e_type ET_CORE): one PT_NOTE segment of process notes (NT_PRSTATUS per thread, NT_PRPSINFO, NT_SIGINFO, NT_AUXV, NT_FILE, register-set…

Code & repositories

  • https://github.com/eliben/pyelftools @ 5a04389392e25d8128e1acb59828a0a85be791b1 · license: Unlicense (declared: the detector found none; check the repository)
    pyelftools, the pure-Python ELF and DWARF parser (public domain; its LICENSE is the Unlicense text, the bundled construct copy has its own license): every ELF structure in elftools/elf/structs.py, constants in enums.py and constants.py, SysV and GNU hash tables in hash.py, notes, symbol versions, relocations for many architectures, a readelf clone (scripts/readelf.py) and a DWARF reader. Its test directories hold about 110 small ELF files for x86, ARM, AArch64 (LE and BE), MIPS32/64, PPC64, S/390, SPARC, TriCore, RISC-V and LoongArch, including corrupt and edge-case files (65287 sections, no section headers, two .dynstr).
    Tested on: Read at this commit (2026-10-08); its structure definitions and hash code were the source for Format:ELF facts, and its test files were parsed with Code:ELF (Executable and Linkable Format)/elfinfo (Python). The library itself was not executed. · checked 2026-10-17; last activity 2026-10-08
  • https://github.com/llvm/llvm-project @ fe04b9b583adcf40c44c3f525682ec28af4509ae · license: Apache-2.0 WITH LLVM-exception (declared by the contributor for the files used; GitHub reports MIT (read from LICENSE.TXT; GitHub's detector did not classify it) for the repository)
    LLVM: BinaryFormat/ELF.h (all ELF constants, GNU property bits, VERSYM) and lld/ELF/Arch (PLT, GOT and TLS relaxation bytes per architecture; APS2 encoder comment in SyntheticSections.cpp).
    Tested on: source read, not run: source read, not run · checked 2026-10-18; last activity 2026-10-18; Note: the latest release ships binaries; the wiki links only the source, never the binaries
  • https://github.com/torvalds/linux @ 32c52ec87d6245ee2d12ed5dd509e40062939b50 · license: GPL-2.0-only WITH Linux-syscall-note (declared by the contributor for the files used; GitHub reports GPL-2.0 WITH Linux-syscall-note (read from COPYING; GitHub's detector did not classify it) for the repository)
    include/uapi/linux/elf.h, include/linux/elfcore.h, fs/binfmt_elf.c: core file note types and structs
    Tested on: source read, not run: read only, master · checked 2026-10-18; last activity 2026-10-18

Pitfalls

  • The program header and symbol field orders differ between ELF32 and ELF64; e_phentsize/e_shentsize give the entry size, use it rather than a constant.
  • Section headers can be missing, zeroed or deliberately wrong (packers such as UPX keep them minimal; hostile files put bogus values). The loader only uses segments and the dynamic table. In doubt, trust PT_LOAD/PT_DYNAMIC: pyelftools' lib_with_two_dynstr_sections*.so.elf show a file where the section headers and DT_STRTAB disagree about the dynamic string table.
  • sh_addr/p_vaddr are addresses, sh_offset/p_offset file offsets: in ET_REL objects every section has address 0, so tools show section-relative offsets.
  • An ET_DYN file may be a library or a PIE executable; check PT_INTERP and DF_1_PIE.
  • Bloom filter words of DT_GNU_HASH are 32-bit in ELF32 files and 64-bit in ELF64 files.
  • 64-bit MIPS relocations do not use the generic r_info split (see #Relocations).

Evidence & references

  • pyelftools (public domain) at commit 5a04389392e2 (2026-10-08): elftools/elf/structs.py (all structure layouts), elftools/elf/enums.py (constants), elftools/elf/hash.py (hash functions and table walks), elftools/elf/elffile.py (extended numbering), elftools/elf/notes.py; test files under test/testfiles_for_readelf and test/testfiles_for_unittests are the samples named on this page and in its facts.
  • System V ABI, gABI chapters 4 and 5 (ELF header, sections, symbols, relocation, program header, dynamic linking), the Linux Foundation refspecs and the Oracle Linker and Libraries Guide (hash table, symbol versioning, section compression).