4. The ELF Format: Reading a Linux Executable Like an Analyst

ELF header to section table: the segments, sections, symbols, dynamic linking, PLT/GOT and constructors of a Linux program, and what each one gives away about a suspicious file

Think about a touring theatre show arriving at a new venue. The crew who unload the trucks need exactly one document: the load-in sheet. It says which crate goes to which part of the stage, how much floor space to clear, which pieces must be bolted down and which stay movable, which local hire company supplies the lighting rig, and which cue starts the show. The audience gets a different document: the programme, listing the scenes and songs by name. The programme is wonderful for understanding the show. But the crew never read it, the show runs fine if the printer forgets it, and nobody checks that it matches what's on stage.

A Linux executable carries both documents. The program headers are the load-in sheet: the kernel and the dynamic linker read them to lay the program out in memory, and nothing else. The section headers are the programme: names like .text and .data that compilers, linkers, debuggers and analysis tools rely on, but that the loader ignores completely. That split is the single most important idea in this article, because a malicious file can delete its programme or print a false one, and still run perfectly.

In the PE article we read a Windows executable from the first byte to the last. This is its Linux sibling: the Executable and Linkable Format (ELF), used for programs, shared libraries, kernel modules and core dumps on Linux and most other Unix-like systems, from servers to routers to Android phones. We'll read a complete ELF by hand, convert between its two kinds of address, follow a dynamic program's calls into the C library, and at every stop ask the same question as before: what does this tell me about the file, and could it be lying?

Everything here is synthetic or benign

The ELF dissected on this page was assembled byte by byte for this article: an 848-byte program whose only action is to exit with status 42. The readelf listings of a dynamically linked program come from a hello-world compiled for this page. The interactive tools build their samples in your browser, and a file you load into them is parsed locally, never uploaded and never executed. As always in this series, handle real samples only inside your lab, renamed to something unrunnable like sample.bin without the execute bit.

Two Views of the Same File

ELF was designed for System V Release 4 Unix and is specified in the System V ABI, which is where the field names below (e_entry, p_vaddr, sh_type and so on) come from. On Linux they're also declared in /usr/include/elf.h and described in man 5 elf. Linux switched to ELF from the older a.out format in the mid-1990s.

The "Linkable" in the name is a clue to the design. The same format serves two different consumers, and gives each its own table:

  • The program header table describes segments: ranges of the file to map into memory, with permissions, plus a few special entries for the loader. This is the execution view. The kernel and the dynamic linker use it, and only it.
  • The section header table describes sections: named, typed pieces such as code (.text), initialised data (.data), symbol tables and string tables. This is the linking view. The linker needs it to build the program, and debuggers, disassemblers and readelf -S use it to show you a tidy, named picture. Once the program is built, nothing that runs it reads this table.

A segment usually contains several sections. Our sample's first loadable segment holds the headers and .text; its second holds .data and .bss. Several sections, like the symbol table, belong to no segment at all: they're never loaded.

This is where ELF differs most from PE. In a PE file, the section table is the loader's map: Windows maps each section using its own entry. ELF splits the two jobs apart, which gives us the analyst's rule for this format, parallel to the one for PE:

  • Program headers and the dynamic section are load-bearing. If they're wrong, the program doesn't run. In a sample that runs, they're telling the truth.
  • Section headers, section names and symbol names are claims. They can be stripped, renamed, or replaced with garbage, and the program still runs exactly the same.

The Map: Our Sample from Top to Bottom

An ELF file has exactly one fixed point: the 64-byte ELF header at offset 0. Everything else is wherever the header says it is. The header gives the offsets of the two tables, the program header table (usually straight after the header) and the section header table (usually at the very end). Here's the layout of the synthetic program we'll take apart, with both views drawn side by side:

SEGMENTS (loader) OFFSET FILE (not to scale) SECTIONS (tools) 0x0000x0400x0F00x1000x1080x1500x15F0x1900x350 ELF header (64 bytes) Program header table (3 × 56) code: mov, mov, syscall data: 2A 00 00 00 symbol table symbol names section names Section header table (7 × 64) PT_LOAD #1 R-X, 0x000 to 0x0FD PT_LOAD #2 RW-, 4 bytes on disk in no segment: never loaded no section: e_phoff 0x40,e_shoff 0x190 point here[1] .text[2] .data [3] .bss[4] .symtab[5] .strtab[6] .shstrtabentry [0] is always empty.bss: no file bytes PT_GNU_STACK: no bytes, only a permission flag

Three things to notice before we read a single byte. The two program header rows at the top belong to no section: the loader's map is outside the "programme" entirely. The .bss section has a name and a size but no bytes in the file. And everything from 0x108 down (about two thirds of this file) is never loaded into memory. Delete it, and the program runs exactly the same. We'll prove that later.

The ELF header is 64 bytes in a 64-bit file (52 in a 32-bit one). Its first 16 bytes, e_ident, are readable before you know anything else about the file, because they tell you how to read the rest:

Offset Name Our sample Meaning
0x00 magic 7F 45 4C 46 \x7F then ELF
0x04 EI_CLASS 02 01 = 32-bit (ELFCLASS32), 02 = 64-bit (ELFCLASS64)
0x05 EI_DATA 01 01 = little-endian, 02 = big-endian
0x06 EI_VERSION 01 Always 1
0x07 EI_OSABI 00 00 = System V, 03 = GNU/Linux, 09 = FreeBSD
0x08 EI_ABIVERSION 00 Usually 0
0x09 padding 00 × 7 Unused, should be zero

Class and byte order decide the size and byte order of every field after this point, so a parser that gets them wrong reads nonsense. Unlike PE files, which are in practice always little-endian, ELF is used on big-endian CPUs too: MIPS routers, PowerPC and SPARC systems. OS/ABI is mostly 00 even for Linux programs; the GNU value 03 appears when a binary uses GNU-specific extensions (statically linked glibc programs can carry it). Non-zero padding bytes are a sign that someone has been editing the header by hand, or hiding a few bytes there.

The rest of the header, with our sample's values:

Offset Field Size Our sample Meaning
0x10 e_type 2 2 Kind of file (below)
0x12 e_machine 2 0x3E Target CPU: x86-64
0x14 e_version 4 1 Always 1
0x18 e_entry 8 0x4000F0 Where execution starts (a virtual address)
0x20 e_phoff 8 0x40 File offset of the program header table
0x28 e_shoff 8 0x190 File offset of the section header table
0x30 e_flags 4 0 CPU-specific flags (used on ARM, MIPS, RISC-V)
0x34 e_ehsize 2 64 Size of this header
0x36 e_phentsize 2 56 Size of one program header
0x38 e_phnum 2 3 Number of program headers
0x3A e_shentsize 2 64 Size of one section header
0x3C e_shnum 2 7 Number of section headers
0x3E e_shstrndx 2 6 Which section holds the section names

e_type: what kind of file is this?

Value Name What it is
1 ET_REL Relocatable object: a compiler's .o output, and also Linux kernel modules (.ko)
2 ET_EXEC Executable that must load at the fixed addresses it was linked for
3 ET_DYN Shared object: a library (.so) or a position-independent executable (PIE)
4 ET_CORE Core dump

Two of these deserve a closer look. A kernel module is an ET_REL file, so a .ko in a sample set is a candidate kernel rootkit, which raises the stakes considerably. And since most Linux distributions now build programs as PIE by default (so ASLR can load them anywhere, the equivalent of PE's DYNAMIC_BASE), most modern executables are ET_DYN, just like libraries. Tools tell a PIE apart from a library by its PT_INTERP header and a PIE flag in the dynamic section, which is how file knows to print "pie executable". A non-PIE ET_EXEC like our sample loads at a fixed address, traditionally 0x400000 on x86-64. Static malware built with simple toolchains is often ET_EXEC, and that's not suspicious on its own.

e_machine: which CPU?

Value CPU Where you meet it
0x03 Intel 80386 (32-bit x86) Older Linux malware, 32-bit builds
0x3E x86-64 Servers, desktops, cloud
0x28 ARM (32-bit) Routers, cameras, older phones
0xB7 AArch64 (64-bit ARM) Phones, newer devices, ARM servers
0x08 MIPS Routers and other embedded devices
0x14 / 0x15 PowerPC / PowerPC64 Embedded devices, older servers
0x2A SuperH Older embedded devices
0xF3 RISC-V Newer embedded devices

IoT botnets are the reason this table matters. Mirai and its many descendants are built for a long list of these architectures at once, so finding the same sample in MIPS, ARM, SuperH and x86 builds (identical strings, different e_machine) is a strong family indicator. It also tells you which disassembler settings and which emulator you'll need.

e_entry: where execution starts

For an ET_EXEC file, e_entry is the absolute address of the first instruction; for a PIE or library it's an offset from wherever the file gets loaded, much like a PE RVA. In a C program it points at the C library's startup code, _start, not at main. A library usually has an entry point that is never used (the dynamic linker doesn't call it), and it may be zero.

Walkthrough: Reading the Whole File by Hand

Here's the synthetic program's first 0x190 bytes: everything except the section header table. Like PE, ELF on x86 is little-endian, so F0 00 40 00 00 00 00 00 is the number 0x4000F0.

00000000  7F 45 4C 46 02 01 01 00  00 00 00 00 00 00 00 00  .ELF............
00000010  02 00 3E 00 01 00 00 00  F0 00 40 00 00 00 00 00  ..>.......@.....
00000020  40 00 00 00 00 00 00 00  90 01 00 00 00 00 00 00  @...............
00000030  00 00 00 00 40 00 38 00  03 00 40 00 07 00 06 00  [email protected]...@.....
00000040  01 00 00 00 05 00 00 00  00 00 00 00 00 00 00 00  ................
00000050  00 00 40 00 00 00 00 00  00 00 40 00 00 00 00 00  ..@.......@.....
00000060  FD 00 00 00 00 00 00 00  FD 00 00 00 00 00 00 00  ................
00000070  00 10 00 00 00 00 00 00  01 00 00 00 06 00 00 00  ................
00000080  00 01 00 00 00 00 00 00  00 11 40 00 00 00 00 00  ..........@.....
00000090  00 11 40 00 00 00 00 00  04 00 00 00 00 00 00 00  ..@.............
000000A0  10 01 00 00 00 00 00 00  00 10 00 00 00 00 00 00  ................
000000B0  51 E5 74 64 06 00 00 00  00 00 00 00 00 00 00 00  Q.td............
000000C0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000000D0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000000E0  10 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000000F0  8B 3D 0A 10 00 00 B8 3C  00 00 00 0F 05 00 00 00  .=.....<........
00000100  2A 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  *...............
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000120  01 00 00 00 01 00 02 00  00 11 40 00 00 00 00 00  ..........@.....
00000130  04 00 00 00 00 00 00 00  08 00 00 00 12 00 01 00  ................
00000140  F0 00 40 00 00 00 00 00  0D 00 00 00 00 00 00 00  ..@.............
00000150  00 73 74 61 74 75 73 00  5F 73 74 61 72 74 00 00  .status._start..
00000160  2E 74 65 78 74 00 2E 64  61 74 61 00 2E 62 73 73  .text..data..bss
00000170  00 2E 73 79 6D 74 61 62  00 2E 73 74 72 74 61 62  ..symtab..strtab
00000180  00 2E 73 68 73 74 72 74  61 62 00 00 00 00 00 00  ..shstrtab......

Walk it in the order the kernel does:

  1. 0x00: identification

    7F 45 4C 46 is the magic. 02: 64-bit. 01: little-endian. 01: version 1. 00: System V ABI. Then eight zero bytes of ABI version and padding.

  2. 0x10: type, machine, entry

    02 00: ET_EXEC, a fixed-address executable. 3E 00: x86-64. 01 00 00 00: version 1 again. At 0x18, eight bytes F0 00 40 00 00 00 00 00: the entry point 0x4000F0.

  3. 0x20: where the tables are

    e_phoff = 0x40: the program headers start right after this header. e_shoff = 0x190: the section headers are near the end. At 0x30: flags 0, header size 0x40, program header size 0x38 (56) times 3, section header size 0x40 (64) times 7, and e_shstrndx = 6.

  4. 0x40: the first program header

    01 00 00 00: PT_LOAD. 05 00 00 00: flags R+X. Then offset 0, virtual address 0x400000, physical address 0x400000 (ignored on Linux), file size 0xFD, memory size 0xFD, alignment 0x1000. In words: "map the first 0xFD bytes of the file at 0x400000, readable and executable."

  5. 0x78: the second program header

    Another PT_LOAD, flags 06 (R+W). Offset 0x100, address 0x401100, file size 4, memory size 0x110. Four bytes come from the file; the other 0x10C are zero-filled by the kernel. That's .data followed by .bss.

  6. 0xB0: the third program header

    51 E5 74 64 is 0x6474E551, PT_GNU_STACK (the bytes spell Q.td, a reliable landmark in hex dumps). It has no offset, address or size, only flags 06: the stack is readable and writable but not executable. Its alignment 0x10 sits at 0xE0.

  7. 0xF0: the code

    The entry point's file offset (we'll see why it's 0xF0 below). Three instructions: 8B 3D 0A 10 00 00 is mov edi, [rip+0x100A], which loads the four bytes at 0x4000F6 + 0x100A = 0x401100, the .data value. B8 3C 00 00 00 is mov eax, 60, the number of Linux's exit system call on x86-64. 0F 05 is syscall. The program exits with whatever was in .data.

  8. 0x100: the data, and the parts that never load

    2A 00 00 00 is 42, the exit status. From 0x108: the symbol table (three 24-byte entries), the symbol names status and _start at 0x150, and the section names at 0x15F. None of this is inside a PT_LOAD.

And here's the rest of the file, the section header table: seven 64-byte entries, the first of which is always all zeros.

00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000001A0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000001B0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000001C0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
000001D0  01 00 00 00 01 00 00 00  06 00 00 00 00 00 00 00  ................
000001E0  F0 00 40 00 00 00 00 00  F0 00 00 00 00 00 00 00  ..@.............
000001F0  0D 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000200  10 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000210  07 00 00 00 01 00 00 00  03 00 00 00 00 00 00 00  ................
00000220  00 11 40 00 00 00 00 00  00 01 00 00 00 00 00 00  ..@.............
00000230  04 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000240  04 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000250  0D 00 00 00 08 00 00 00  03 00 00 00 00 00 00 00  ................
00000260  10 11 40 00 00 00 00 00  04 01 00 00 00 00 00 00  ..@.............
00000270  00 01 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000280  10 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000290  12 00 00 00 02 00 00 00  00 00 00 00 00 00 00 00  ................
000002A0  00 00 00 00 00 00 00 00  08 01 00 00 00 00 00 00  ................
000002B0  48 00 00 00 00 00 00 00  05 00 00 00 02 00 00 00  H...............
000002C0  08 00 00 00 00 00 00 00  18 00 00 00 00 00 00 00  ................
000002D0  1A 00 00 00 03 00 00 00  00 00 00 00 00 00 00 00  ................
000002E0  00 00 00 00 00 00 00 00  50 01 00 00 00 00 00 00  ........P.......
000002F0  0F 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000300  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................
00000310  22 00 00 00 03 00 00 00  00 00 00 00 00 00 00 00  "...............
00000320  00 00 00 00 00 00 00 00  5F 01 00 00 00 00 00 00  ........_.......
00000330  2C 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ,...............
00000340  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  ................

Take entry 1 at 0x1D0: name offset 1, type 1 (SHT_PROGBITS), flags 6 (allocated + executable), address 0x4000F0, file offset 0xF0, size 0xD, alignment 0x10. To get its name, go to section e_shstrndx = 6, whose entry (at 0x310) says its bytes are at file offset 0x15F; one byte in from there is .text. Every section name is found that way: an index into a string table that is itself a section.

readelf does all of this for you. Its output for our file (-W means "wide", so lines aren't truncated):

$ readelf -h -l -W sample.bin
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF64
  Data:                              2's complement, little endian
  OS/ABI:                            UNIX - System V
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Entry point address:               0x4000f0
  Start of program headers:          64 (bytes into file)
  Start of section headers:          400 (bytes into file)
  Number of program headers:         3
  Number of section headers:         7
  Section header string table index: 6

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x0000fd 0x0000fd R E 0x1000
  LOAD           0x000100 0x0000000000401100 0x0000000000401100 0x000004 0x000110 RW  0x1000
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10

 Section to Segment mapping:
  Segment Sections...
   00     .text
   01     .data .bss
   02

(Lightly trimmed: a few constant lines are omitted.) That's the whole skeleton of a working program: a 64-bit, statically linked, fixed-address executable with one code segment, one data segment, a non-executable stack, and an unstripped symbol table. And for the record, the fingerprint you'd file it under:

SHA-256  c646af3d332dff301a819424a39f117f36383b07373816873c4e46fab4f91d3c
MD5      b728d31466e2d62530c486c4e972de05
Size     848 bytes

Program Headers: The Loader's Instructions

Each 56-byte program header (Elf64_Phdr) has the same eight fields: p_type, p_flags, p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align. (In 32-bit files they're 32 bytes and p_flags moves to after p_memsz, a classic trap for hand-written parsers.) The flags are three permission bits: PF_X = 1, PF_W = 2, PF_R = 4, so 5 is R-X and 6 is RW-. The types you'll actually meet:

Type Value What the loader does with it
PT_LOAD 1 Map this range of the file at this address, with these permissions
PT_DYNAMIC 2 Where the dynamic section is: libraries, symbols, relocations
PT_INTERP 3 Path of the program interpreter (the dynamic linker)
PT_NOTE 4 Notes: ABI tag, build ID, properties
PT_PHDR 6 Where the program header table itself is in memory
PT_TLS 7 Template for thread-local storage
PT_GNU_EH_FRAME 0x6474E550 Exception-unwinding lookup table
PT_GNU_STACK 0x6474E551 Whether the stack should be executable
PT_GNU_RELRO 0x6474E552 Region to make read-only after relocation
PT_GNU_PROPERTY 0x6474E553 CPU security features (e.g. CET on x86)

PT_LOAD: the only thing that becomes memory

Every byte of a running program's image comes from a PT_LOAD segment. The kernel maps p_filesz bytes from p_offset to p_vaddr, and if p_memsz is larger, zero-fills the rest. That's how .bss, the uninitialised globals, takes up memory but no file space, exactly like a PE section whose VirtualSize exceeds its SizeOfRawData.

A normal toolchain produces a small, predictable set. Our sample has the classic two (code R-X, data RW-). A modern GNU linker usually produces four: headers and read-only metadata (R), code (R-X), read-only data (R), and writable data (RW-), so that headers and constant data are never mapped executable. What it never produces is a segment that is writable and executable at once (flags RWE, value 7).

PT_INTERP: who loads the loader

A dynamically linked program can't start on its own: someone has to load its libraries first. PT_INTERP names that someone, the program interpreter, as a plain path string. On x86-64 glibc systems it's /lib64/ld-linux-x86-64.so.2. The kernel maps the program, maps the interpreter too, and starts the interpreter rather than the program. The interpreter then loads libraries, applies relocations, runs initialisers, and finally jumps to e_entry.

That means the interpreter path is a load-bearing field that runs code, so it's worth checking. Normal values are a short list: /lib64/ld-linux-x86-64.so.2, /lib/ld-linux.so.2 (32-bit x86), /lib/ld-linux-aarch64.so.1, /lib/ld-linux-armhf.so.3, /lib/ld-musl-x86_64.so.1 (musl, as on Alpine Linux), and /system/bin/linker64 on Android. An interpreter in /tmp, /dev/shm, a hidden directory or a user's home is a very loud flag: whatever lives at that path runs first, with full control of the process.

A statically linked program has no PT_INTERP, and the kernel jumps straight to e_entry. Our sample is one of those.

PT_DYNAMIC, PT_GNU_RELRO and PT_NOTE

PT_DYNAMIC points at the dynamic section, which gets its own section below. PT_GNU_RELRO marks the part of the data segment (the GOT and similar tables) that the dynamic linker should make read-only once it has finished relocating, a hardening feature known as RELRO. PT_NOTE points at small tagged records: the .note.ABI-tag that tells file a program is "for GNU/Linux 3.2.0", and the build ID, a hash of the build that's handy for clustering samples from the same build. Go programs carry their own Go build ID note.

PT_GNU_STACK: the executable-stack switch

This header has no contents, only flags. RW means a non-executable stack, the Linux counterpart of PE's NX_COMPAT. RWE asks for an executable stack, which some very old code and some hand-written assembly need, and which makes injected code on the stack runnable. If the header is missing altogether, the kernel has to guess: on 32-bit x86 it falls back to the old, permissive behaviour, while 64-bit x86 kernels since Linux 5.8 default to non-executable. A missing or executable PT_GNU_STACK says the file was built by unusual tools, by hand, or with a deliberate request; any of those is worth a note.

Virtual Addresses and File Offsets

The PE article needed three kinds of address. ELF needs only two:

  • A file offset: a position in the file. A hex editor shows these.
  • A virtual address: where the byte appears in memory. e_entry, p_vaddr, sh_addr, symbol values and pointers in the dynamic section are all virtual addresses.

There's no separate RVA because ELF doesn't need one. In an ET_EXEC file, the virtual addresses are absolute: our entry point really will be at 0x4000F0. In a PIE or shared library, the virtual addresses start near zero and the whole image slides by a random load base at run time, so they behave exactly like PE RVAs (debuggers then show base + address, e.g. 0x555555555060 for a PIE's 0x1060 when ASLR is off under gdb).

Converting works segment by segment. Find the PT_LOAD whose address range contains the address, and swap one starting point for the other:

Write aa for the address, vv for the segment's p_vaddr, pp for its p_offset and ff for its p_filesz. If v≤a<v+fv \le a \lt v + f:

off=a−v+p\text{off} = a - v + p

If the address is past p_filesz but before p_memsz, it's zero-fill and has no file offset. If no PT_LOAD contains it, it isn't part of any segment. Our sample's segments:

Segment  Flags  p_offset  p_vaddr   p_filesz  p_memsz
LOAD #1  R-X    0x000     0x400000  0x0FD     0x0FD
LOAD #2  RW-    0x100     0x401100  0x004     0x110

Worked example: where is the entry point in the file?

e_entry is 0x4000F0. You want its bytes in a hex editor.

  1. Find the segment. LOAD #1 covers 0x400000 up to 0x400000 + 0xFD = 0x4000FD. 0x4000F0 is inside it.
  2. Is it backed by file bytes? Its offset into the segment, 0xF0, is less than p_filesz (0xFD), so yes.
  3. Add the segment's file start: 0xF0 + 0x000 = 0xF0. That's the file offset, exactly where the walkthrough found the code.
  4. Check the permissions: LOAD #1 is R-X, so the entry point is in executable memory, as it should be.

Two more, to see the other cases:

  • status at 0x401100 is in LOAD #2 (0x401100 up to 0x401210 in memory), offset 0 into it, which is less than p_filesz 4. File offset: 0x401100 − 0x401100 + 0x100 = 0x100. That's the 2A.
  • .bss at 0x401110 is also in LOAD #2, at offset 0x10. But p_filesz is only 4, so 0x401110 has no file offset: the kernel fills it with zeros. A packed ELF does the same thing on a grand scale, with a segment whose p_filesz is tiny and whose p_memsz is hundreds of kilobytes, waiting for the unpacker to fill it.

Going the other way, file offset 0x100 lies in LOAD #2's file range (0x100 to 0x104), so it maps to 0x100 − 0x100 + 0x401100 = 0x401100. File offset 0x150 lies in no PT_LOAD's file range: the symbol names are never loaded, so they have no address.

Why the offsets and addresses look so "unaligned"

Memory permissions are set per 4 KB page, and the kernel maps files a page at a time. So a PT_LOAD doesn't have to start on a page boundary; it just has to agree with the file about where in the page it starts. The rule is that p_vaddr and p_offset are equal modulo p_align: here 0x401100 and 0x100 both end in 0x100. The kernel then maps the whole 4 KB page that contains the segment. That packs the file tightly (PE pads each section to FileAlignment instead), at the price of some odd side effects:

On disk: 0x350 bytes, tightly packed LOAD #1 bytessymbols, names,section headers 0x0000x0FD0x1000x350 ← LOAD #2: four bytes at 0x100 (.data) never loaded: everything after 0x104 In memory: two 4 KB pages headers + code leftover copy .bss (zero) … 0x4000000x4000FD0x4010000x4010000x4011000x4012100x402000 R-X page RW- page rest of the page: mapped because pages are 4 KB, but not part of the segment zero-filled: p_memsz (0x110) is bigger than p_filesz (4)

Both segments come from the same 4 KB page of the file, so that page is mapped twice: once read-execute at 0x400000, once read-write at 0x401000. As a result, the bytes around each segment are visible in memory too: the whole 848-byte file appears at 0x400000, and a leftover copy of the headers and code sits, writable, at 0x401000. The kernel zeroes everything in the data page after p_filesz, which is what makes .bss start out zero. Nothing here is suspicious; it's how every traditionally linked ELF is laid out. But it explains why a memory dump of a process holds bytes that "belong" to no segment, and why segment boundaries (not page boundaries) are what you should compare when you look for code that doesn't belong.

A real PIE looks the same, only with addresses relative to the load base. These are the PT_LOADs of a hello-world compiled for this article with a current GCC:

  Type   Offset   VirtAddr           FileSiz  MemSiz   Flg Align
  LOAD   0x000000 0x0000000000000000 0x0006a8 0x0006a8 R   0x1000
  LOAD   0x001000 0x0000000000001000 0x000191 0x000191 R E 0x1000
  LOAD   0x002000 0x0000000000002000 0x000160 0x000160 R   0x1000
  LOAD   0x002160 0x0000000000003160 0x000278 0x000280 RW  0x1000

The first three segments have offset equal to address, but the data segment doesn't: file offset 0x2160 becomes address 0x3160. A tool that assumes "address = offset" works for most of this file and silently gives wrong answers for every global variable. Always convert through the segment.

ELF Address Mapper

Pick a segment table (the article's sample, the PIE above, or the packed-style sample), type a virtual address or a file offset, and step through the conversion. The diagram shows where the address falls in the file and in memory, including the page that gets mapped twice and the zero-filled tail of the data segment.

A PIE or shared object slides by a random load base at run time; set one to see where its addresses land. Leave it empty for base 0. Or start from an example:

Click either bar to convert the address under the pointer.

File offset
Virtual address

Segments in use

Section Headers: The Programme

Each 64-byte section header (Elf64_Shdr) has a name (an offset into the section-name string table), a type, flags, an address in memory (zero if the section isn't loaded), a file offset, a size, two cross-reference fields (sh_link, sh_info), an alignment and, for tables, the size of one entry. Our sample's, as readelf -S shows them:

$ readelf -S -W sample.bin
  [Nr] Name       Type      Address          Off    Size   ES Flg Lk Inf Al
  [ 0]            NULL      0000000000000000 000000 000000 00      0   0  0
  [ 1] .text      PROGBITS  00000000004000f0 0000f0 00000d 00  AX  0   0 16
  [ 2] .data      PROGBITS  0000000000401100 000100 000004 00  WA  0   0  4
  [ 3] .bss       NOBITS    0000000000401110 000104 000100 00  WA  0   0 16
  [ 4] .symtab    SYMTAB    0000000000000000 000108 000048 18      5   2  8
  [ 5] .strtab    STRTAB    0000000000000000 000150 00000f 00      0   0  1
  [ 6] .shstrtab  STRTAB    0000000000000000 00015f 00002c 00      0   0  1

The flags are W (writable, SHF_WRITE = 1), A (allocated: occupies memory at run time, SHF_ALLOC = 2) and X (executable, SHF_EXECINSTR = 4). The types worth knowing:

Type Value Contents
SHT_PROGBITS 1 Code or data as-is (.text, .data, .rodata, .plt, .got)
SHT_SYMTAB 2 The full symbol table (.symtab)
SHT_STRTAB 3 Strings (.strtab, .dynstr, .shstrtab)
SHT_RELA 4 Relocations with addends (.rela.dyn, .rela.plt)
SHT_DYNAMIC 6 The dynamic section
SHT_NOTE 7 Notes (.note.gnu.build-id, .note.ABI-tag)
SHT_NOBITS 8 Takes memory but no file bytes (.bss)
SHT_DYNSYM 11 The dynamic symbol table (.dynsym)
SHT_INIT_ARRAY 14 Constructor pointers (.init_array)

A typical dynamically linked program has about thirty sections. These names are the ones you'll see over and over: .interp (the interpreter path), .dynsym and .dynstr (imported and exported symbols), .rela.dyn and .rela.plt (relocations), .init and .fini, .plt and .got.plt (the call trampolines below), .text, .rodata (constants and string literals), .init_array and .fini_array, .dynamic, .data, .bss, and .comment, which holds compiler version strings like GCC: (GNU) 15.2.0. That last one survives strip, and is a cheap toolchain fingerprint (and, like everything in this table, a claim).

Sections are optional, and can lie

Here's the experiment promised earlier. Take the sample, cut the file off after the data at 0x104, and zero the three section fields in the header (e_shoff, e_shnum, e_shstrndx). The symbol tables and section headers are gone:

$ file nosec.bin
nosec.bin: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, no section header
$ readelf -S nosec.bin
There are no sections in this file.
$ objdump -d nosec.bin
nosec.bin:     file format elf64-x86-64
$ ./nosec.bin; echo $?
42

objdump -d disassembles sections, so it now prints nothing at all, while the kernel, which only ever read the program headers, runs the program exactly as before. Pointing e_shoff at a nonsense offset instead (0x41414141) has the same result: file reports "missing section headers", readelf gives up with an error, and the program still exits with 42. (Running these was safe only because the synthetic sample does nothing but exit; never run a real sample outside your lab.)

Section headers get removed or mangled for three reasons:

  • Size. sstrip (from Brian Raiter's ELFkickers) removes the section header table and everything else the loader doesn't need. It's used in embedded firmware, where every byte of flash counts, and by malware authors for the same reason.
  • Packing. UPX-packed ELF files, as triage noted, usually have no section headers at all: the packed program is a couple of PT_LOADs and a stub.
  • Anti-analysis. Garbage section headers break tools that trust them. Some older versions of debuggers and disassemblers crashed or refused to open such files; current ones mostly fall back to the program headers, but scripts and quick tools often don't.

Worse than missing section headers are lying ones. Because nothing checks sections against segments, a file can claim that .text is at one address while the entry point and the real code live somewhere else entirely, or label a writable, executable PT_LOAD as harmless .rodata. So the rule for sections is: use them as a convenient index, but when anything matters (where the code is, what's executable, what's loaded), check it against the program headers. readelf -l prints a "Section to Segment mapping" at the end for exactly this comparison.

Symbols, and What Stripping Removes

A symbol is a name attached to an address: a function, a global variable, or a placeholder for something that will come from a library. Each 24-byte Elf64_Sym holds a name (an offset into a string table), a type and binding packed into one byte, the index of the section it belongs to, a value (its address) and a size. Our sample's three:

$ readelf -s sample.bin
Symbol table '.symtab' contains 3 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000401100     4 OBJECT  LOCAL  DEFAULT    2 status
     2: 00000000004000f0    13 FUNC    GLOBAL DEFAULT    1 _start

Read entry 2 against the hex at 0x138: name offset 08 (into .strtab at 0x150: _start), info 0x12 (binding 1, GLOBAL, in the high nibble; type 2, FUNC, in the low one), section 1 (.text), value 0x4000F0, size 13.

An ELF can carry two symbol tables, and the difference between them matters:

.symtab (and .strtab)

  • Every function and global, including static (file-local) ones.
  • Not loaded into memory; only tools read it.
  • Removed by strip. Release builds usually are stripped.
  • What makes nm, gdb and disassemblers show names like main and parse_config.

.dynsym (and .dynstr)

  • Only symbols that cross a library boundary: imports and exports.
  • Loaded, and used by the dynamic linker at run time.
  • Survives strip, because removing it would break the program.
  • The ELF equivalent of a PE's import and export tables. Read it with readelf --dyn-syms or nm -D.

Stripping our hello-world shows the split cleanly. Before, nm lists about thirty symbols, including main and the constructor early that we'll meet below. After strip, nm reports "no symbols", but nm -D still lists every import:

$ nm -D hello.stripped
                 w __cxa_finalize@GLIBC_2.2.5
                 w __gmon_start__
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 U __libc_start_main@GLIBC_2.34
                 U puts@GLIBC_2.2.5
                 U write@GLIBC_2.2.5

U means undefined here (imported), w a weak reference that may stay unresolved. The @GLIBC_2.34 suffixes are symbol versions, and they leak a small fact: this binary was linked against glibc 2.34 or newer, so it won't run on older systems. The oldest-looking malware often turns out to have been built on a surprisingly recent distribution.

In a stripped binary you'll have to find main yourself. On x86-64 glibc, _start loads main's address into rdi and calls __libc_start_main, so the instruction just before that call (lea rdi, [rip+...]) points straight at it. Disassemblers know this pattern and usually name main for you.

The Dynamic Section: What a Program Needs

A dynamically linked program's PT_DYNAMIC segment holds the dynamic section: an array of 16-byte tag/value pairs, ended by a DT_NULL tag, that tells the dynamic linker everything it needs. Here's the hello-world's, trimmed:

$ readelf -d hello
Dynamic section at offset 0x2178 contains 28 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x1000
 0x000000000000000d (FINI)               0x1184
 0x0000000000000019 (INIT_ARRAY)         0x3160
 0x000000000000001b (INIT_ARRAYSZ)       16 (bytes)
 0x0000000000000005 (STRTAB)             0x420
 0x0000000000000006 (SYMTAB)             0x360
 0x0000000000000003 (PLTGOT)             0x33a0
 0x0000000000000017 (JMPREL)             0x678
 0x0000000000000007 (RELA)               0x5a0
 0x000000006ffffffb (FLAGS_1)            Flags: PIE
 0x0000000000000000 (NULL)               0x0

The dynamic linker finds its symbol table, string table and relocations here, not through sections, which is why stripping the section headers doesn't hurt a dynamic program either. The tags an analyst reads first:

  • DT_NEEDED: one per directly required library. This is the list of DLL names from a PE import table, minus the function names. Those are in .dynsym, and they aren't tied to a particular library: the dynamic linker searches the loaded libraries in order. libc.so.6 is in almost everything; libpthread, libdl, libcurl, libssl or libcrypto hint at threads, run-time loading, HTTP and encryption.
  • DT_RPATH / DT_RUNPATH: extra directories to search for those libraries. A path containing $ORIGIN (meaning "the directory this file is in") is common in self-contained applications. A search path pointing at /tmp, /dev/shm or a hidden directory lets whoever controls it supply a library of their choosing.
  • DT_SONAME: a library's own name. Like the PE export directory's internal name, it can reveal what a renamed file was originally called.
  • DT_INIT, DT_INIT_ARRAY, DT_PREINIT_ARRAY: code that runs before main. Its own section, below.
  • DT_FLAGS / DT_FLAGS_1 with BIND_NOW or NOW: resolve all imports at start-up instead of lazily (also below). PIE marks a position-independent executable.

Why not just run ldd?

The obvious command for "what libraries does this need?" is ldd. Don't run it on a sample. By default ldd works by starting the program's dynamic linker with the environment variable LD_TRACE_LOADED_OBJECTS set, so that it lists the libraries instead of running the program. The ldd(1) manual page itself warns that some versions handle a program with an unusual interpreter by executing the program directly, and concludes that you "should never employ ldd on an untrusted executable". Since PT_INTERP is attacker-controlled, a sample can name its own interpreter. Use tools that only read:

$ readelf -d sample.bin | grep NEEDED       # direct dependencies only
$ objdump -p sample.bin | grep NEEDED       # the same, the ldd man page's suggestion
$ lddtree sample.bin                        # full tree, by parsing (pax-utils)

ldd shows the full dependency tree (libraries of libraries), and the first two read-only commands show only direct dependencies. That's the right trade-off for an untrusted file. lddtree recovers the tree by parsing each library's dynamic section rather than running anything.

Relocations, the PLT and the GOT

A program that calls puts doesn't know where puts will be: libc.so.6 is loaded at a random address, and differs between systems. ELF solves this with relocations, records that say "patch this location with the address of that symbol", and with two small tables the patches go into.

Each x86-64 relocation (Elf64_Rela, 24 bytes) gives an offset to patch, a type and symbol index packed into one field, and an addend. The hello-world's import relocations:

$ readelf -rW hello
Relocation section '.rela.plt' at offset 0x678 contains 2 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
00000000000033b8  0000000300000007 R_X86_64_JUMP_SLOT     0000000000000000 puts@GLIBC_2.2.5 + 0
00000000000033c0  0000000400000007 R_X86_64_JUMP_SLOT     0000000000000000 write@GLIBC_2.2.5 + 0

The Info value 0x0000000300000007 is symbol 3 (puts in .dynsym) in the top half and type 7 (R_X86_64_JUMP_SLOT) in the bottom half. The other common types are R_X86_64_GLOB_DAT (6, the address of an imported variable or function pointer) and R_X86_64_RELATIVE (8, "add the load base", which is how a PIE fixes up its own pointers, the counterpart of PE base relocations).

The patched slots live in the GOT, the global offset table: an array of addresses in writable data. Code never calls puts directly; it calls a tiny trampoline in the PLT, the procedure linkage table, which jumps through the GOT slot. Together they're the ELF equivalent of a PE import address table, with one twist: lazy binding.

.text main: call puts@plt .plt (read-only code) 1020 PLT0: push [GOT+8]1026 jmp *[GOT+16]1030 jmp *[0x33b8]1036 push 0103b jmp PLT0 puts@plt .got.plt (writable data) 33a0 &_DYNAMIC33a8 link map33b0 resolver33b8 puts slotfirst: 0x1036then: libc puts ld.so resolver looks up "puts" in libc.so.6 libc.so.6 puts 1 2 3 4 5 via GOT+16 6 writes 7 First call: 1 → 2 → 3 (the slot points back into the PLT) → 4 → 5 → 6 (the resolver patches the slot) → puts. Every later call: 1 → 2 → 7, straight to puts. Addresses are the hello-world's, before the PIE load base is added.

On disk, each GOT slot points back into its own PLT entry: 0x33b8 holds 0x1036, the push 0 right after the jump (you can see it with readelf -x .got.plt). So the first call to puts bounces back, pushes the relocation index, and jumps into the dynamic linker's resolver, which finds puts, writes its address into the GOT slot, and calls it. Every later call jumps straight through the patched slot. This saves start-up time in programs that import thousands of functions and use few of them.

The hello-world was linked with -z lazy to show the classic scheme. Many distributions now link with -z now (resolve everything at start-up) together with full RELRO, which makes the GOT read-only before main runs. For an analyst, the scheme matters in two ways:

  • Reading code. A disassembler shows call puts@plt; the @plt means "an import". In a stripped binary, the PLT and the .rela.plt relocations are often the only way calls get names, which is one reason static, stripped binaries (no PLT at all) are so much harder.
  • Hooking. A writable GOT is a table of function pointers that every call goes through. Code that's already inside a process (an injected library, a parasite) can overwrite a slot to redirect, say, readdir to its own function that hides files. Full RELRO (PT_GNU_RELRO plus BIND_NOW) closes that door for the program's own GOT.

Code Before main: Constructors and Preloading

The PE article's TLS callbacks were code that ran before the entry point. ELF has several equivalents, and malware uses all of them:

Mechanism Where it lives When it runs
DT_PREINIT_ARRAY .preinit_array, executables only Before any initialiser of any library
DT_INIT .init (a single function) When its object is initialised
DT_INIT_ARRAY .init_array (array of pointers) Straight after DT_INIT
.ctors Legacy GCC constructor list Called from .init (old toolchains)
DT_FINI_ARRAY, DT_FINI .fini_array, .fini At exit, in reverse

In C, __attribute__((constructor)) puts a function into .init_array; C++ global objects with constructors end up there too. (GCC versions before 4.7 used .ctors instead, so a .ctors section suggests an old toolchain or a deliberately odd build.) The hello-world has one:

__attribute__((constructor)) static void early(void) { write(1, "early\n", 6); }
int main(void) { puts("hello"); return 0; }

Its .init_array holds two pointers, 0x1140 (frame_dummy, the compiler's own) and 0x1149 (early), and early prints before main does. The order matters for debugging:

  1. The kernel maps the program and its interpreter

    Nothing of the program has run yet. A debugger that starts the program (gdb's starti) can stop here, at the interpreter's first instruction.

  2. The dynamic linker loads and relocates libraries

    Every DT_NEEDED library (and anything in LD_PRELOAD), then relocations.

  3. Library initialisers run

    First the program's .preinit_array, if it has one; then each library's DT_INIT and .init_array, dependencies first. This is ELF's DllMain: loading a shared object runs its constructors, whether the program ever calls it or not.

  4. The program's entry point, then its own initialisers

    The dynamic linker jumps to e_entry (_start), and the C library's start-up code (__libc_start_main) runs the program's own .init and .init_array.

  5. main

    Where most people put their first breakpoint, after all of the above.

So a breakpoint on main misses every constructor, and one on e_entry still misses the libraries'. In a sample, list .init_array entries (readelf -x .init_array, or the INIT_ARRAY tag if sections are gone) and look at each function before trusting main to be the beginning. A static binary has no dynamic linker, but its C library start-up code still runs .init_array.

LD_PRELOAD and /etc/ld.so.preload

Step 2 has a hook built in. The dynamic linker loads any library named in the LD_PRELOAD environment variable, or listed in the file /etc/ld.so.preload, before the program's own libraries. Symbols are looked up in load order, so a preloaded library that defines readdir replaces the C library's readdir for that program. It's a legitimate debugging and profiling feature, and the basis of a whole class of user-mode rootkits: a shared object, written into /etc/ld.so.preload, that hooks the functions ls, ps and netstat use, hiding its files, processes and connections from every dynamically linked program on the system. Public examples include the Jynx and Azazel rootkits; Symbiote, reported by Intezer and BlackBerry in 2022, was a shared object loaded this way that hooked libc functions to steal credentials and hide itself.

Structurally, such a sample is an ET_DYN file with no PT_INTERP, a constructor, and exports with libc's names: readdir, readdir64, fopen, open, stat, accept, pam_authenticate. A library that exports functions it doesn't seem to own, especially file, directory, process and authentication functions, is shaped for preloading. Two defensive notes: check /etc/ld.so.preload on any Linux machine you're investigating (it normally doesn't exist), and remember that a preload rootkit can't fool a statically linked tool, such as a static BusyBox, which is why incident responders carry one.

Static Binaries, Go and Rust

A statically linked program contains its C library: no PT_INTERP, no PT_DYNAMIC, no .dynsym, no PLT. It runs on any Linux kernel for its CPU, which is exactly why IoT malware loves it: one binary per architecture, no worries about which libc a router ships. Combined with stripping, it's the hardest kind of ELF to read statically, because nothing names anything. Thousands of anonymous functions, most of them the C library, and no import list to say which ones matter. Tools fight back with signature matching: IDA's FLIRT, Ghidra's Function ID and similar systems recognise library functions by their bytes and name them, and they work best when you know (from strings, .comment, or the code style) which libc and version was linked in: glibc, musl, uClibc and dietlibc all look different.

Two modern languages produce distinctive ELFs that turn up often in Linux malware:

  • Go. Programs are statically linked by default (unless they use cgo), and large: a "hello world" is a megabyte or two. The good news is that Go keeps its own runtime metadata in the binary, in sections like .gopclntab and .go.buildinfo, and that data survives strip: function names, source file paths, the Go version, and the module's dependency list. Tools such as Mandiant's GoReSym (and the Go support built into current disassemblers) recover function names from it. Running go version -m sample.bin prints the build information by reading the file, not executing it.
  • Rust. Programs link the C library dynamically by default on Linux but carry the Rust standard library and every crate inside. Panic messages leave source paths in the strings: /rustc/ followed by the compiler's commit hash, and paths from the cargo registry that name each crate and version, which is a dependency list in all but name. Names are mangled (_ZN... or _R...) and, if the symbols weren't stripped, demangle into very readable paths.

Both languages produce unusual-looking code and huge binaries, and both trip naive "this looks packed" heuristics, so check what the binary is before you decide what it's hiding.

Red Flags: A Structural Checklist

Here's everything above as one pass over any ELF. As with PE, none of these is a verdict; each is a question with an innocent answer and a guilty one.

Observation Why it's interesting Innocent explanations
Statically linked and stripped Nothing named, no import list Embedded firmware, Go programs, minimal containers
No section headers, or e_shoff past the end of the file Removed by a packer or sstrip, or deliberately broken Size-optimised firmware
Section headers that disagree with the segments Sections are claims; something wants tools to see a false picture Very rare
A PT_LOAD that is writable and executable (RWE) Code written at run time, then run: unpackers, shellcode loaders Some JITs and very old toolchains
p_memsz far larger than p_filesz in an executable segment Space reserved for unpacked code None very convincing; .bss is normally in a RW- segment
Few segments, no sections, high entropy, UPX! Packed Legitimately compressed tools
Entry point not in .text, or in no executable PT_LOAD A stub or injected code runs first (a classic sign of an ELF infector) Hand-written programs
PT_INTERP outside the usual loader paths Attacker-controlled code runs first Custom runtimes, Android, musl, Nix (/nix/store/...)
PT_GNU_STACK missing or RWE Executable stack Old or hand-written assembly
RPATH/RUNPATH into /tmp, /dev/shm or a hidden directory Library hijacking Badly packaged software
A shared object exporting libc names (readdir, fopen, open) Built for LD_PRELOAD hooking Profilers, sandboxes, compatibility shims
Constructors in .init_array doing real work Code before main C++ global objects, normal libraries
ET_REL file that isn't a build artefact Probably a kernel module (.ko): possible kernel rootkit Drivers
Bytes past the end of the last segment and section Appended payload or configuration Self-extracting archives, signatures appended by tools

The packed-file rows are worth a picture, because it's the shape you'll meet most. On the left is our sample; on the right, a packed-style sample of the kind the tools below include:

Normal build

7 section headers
Type       Offset  VirtAddr  FileSiz  MemSiz   Flg
LOAD       0x0000  0x400000  0x00FD   0x00FD   R E
LOAD       0x0100  0x401100  0x0004   0x0110   RW
GNU_STACK  0       0         0        0        RW
Entry 0x4000F0: start of .text

Packed-style build (synthetic)

There are no sections in this file.
Type       Offset  VirtAddr  FileSiz  MemSiz   Flg
LOAD       0x0000  0x400000  0x0000   0x2C000  RW
LOAD       0x0000  0x42C000  0xB4E8   0xB4E8   RWE
GNU_STACK  0       0         0        0        RW
Entry 0x436A10: deep in the last
segment, which is RWE; entropy 7.9

The packed-style file names nothing, reserves 176 KB of memory with no file bytes behind it, puts its entry point near the end of a segment that is writable and executable, and is almost all high-entropy bytes. Each fact on its own has an innocent reading. Together they describe an unpacking stub waiting to decompress a program into that empty space.

The ELF Header Explorer below puts the whole article into one tool. Start with the built-in synthetic samples: the "normal" one is the exact 848-byte program dissected above, and each of the others trips at least one of these flags.

ELF Header Explorer

Choose a file from your computer or one of the built-in synthetic samples. Files are parsed entirely in your browser and are never uploaded anywhere. Nothing is executed. The built-in samples are small byte buffers generated on this page; none of them is real software or real malware. Click any field to highlight its bytes in the hex view.

Or drop a file anywhere on this panel.

File type
Entry point
Linking
Anomaly flags

Anomaly flags

    Structure

    The ELF Toolkit

    Most of what you need is already installed on any Linux machine with a compiler, and all of it is on REMnux. None of these execute the file (which is why ldd isn't on the list):

    • file: type, class, endianness, CPU, static or dynamic, interpreter, stripped or not, and missing or broken section headers. The one-line summary to start with.
    • readelf (GNU binutils): the reference parser. -h header, -l program headers and the section-to-segment mapping, -S sections, -d dynamic section, -r relocations, -s and --dyn-syms symbols, -n notes, -x NAME and -p NAME hex and string dumps of one section. Add -W so wide lines aren't cut. It never uses BFD, the library behind objdump, so it copes better with damaged files.
    • objdump: -d disassembles (sections required), -p prints the private headers including NEEDED, -T the dynamic symbols.
    • nm: symbols, with -D for the dynamic ones. Combine with c++filt or nm -C to demangle C++ names.
    • strings, from triage, for the readable text.
    • Detect It Easy, rabin2 (rabin2 -I, -S, -l, -i, -E from radare2), and the libraries LIEF and pyelftools for scripting, which parse ELF just as they parse PE. LIEF can also rebuild a damaged file's headers, which is handy for getting a deliberately broken sample to open in a disassembler.

    A first pass over an unknown ELF, in the order this article took it:

    $ file sample.bin                       # what is it? static? stripped? interpreter?
    $ readelf -h sample.bin                 # class, endianness, type, machine, entry
    $ readelf -lW sample.bin                # segments, permissions, interpreter, mapping
    $ readelf -SW sample.bin                # sections, if any (compare with the above)
    $ readelf -d sample.bin                 # NEEDED, RUNPATH, INIT_ARRAY, FLAGS
    $ readelf --dyn-syms -W sample.bin      # imports and exports
    $ readelf -x .init_array sample.bin     # constructors, if the section exists

    What's Next

    You can now read a Linux executable the way the kernel and the dynamic linker do: decode the ELF header, follow the program headers to see what actually gets loaded and with which permissions, convert between virtual addresses and file offsets through the PT_LOADs, and read the dynamic section, the symbol tables, the relocations and the PLT/GOT that connect a program to its libraries. You also know which parts are only claims: section headers, section names, the symbol table, .comment. As with PE, the most interesting findings come from catching the two disagreeing, and from noticing code that runs before anyone was watching.

    That completes the formats. Both PE and ELF tell you what a program asks for and where its code is; the next step in the series is reading that code, with disassemblers and debuggers, inside the lab.