Skip to content
What's Inside an Executable? Visual Guide to ELF Files
Fundamentals 18 min read

What's Inside an Executable? Visual Guide to ELF Files

This article provides a visual deep-dive into the ELF file format, covering its header structure, program and section headers, segments versus sections, code/data/bss layout, symbol tables, relocation mechanics, dynamic linking with GOT/PLT, the execve loading sequence, and practical inspection tools like readelf and objdump.

IT Services Circle
IT Services Circle
IT Services Circle

01 One ELF, Three Ways to Read It

An ELF (Executable and Linkable Format) file serves three roles: the kernel loader maps file ranges into process memory; the linker organizes code, data, symbols, and relocations; debuggers and analyzers locate functions, variables, debug info, and machine instructions. Understanding ELF means distinguishing the file structure, the linking view, and the runtime view.

The ELF Header sits at the start, acting as a directory: it identifies the file type, target architecture, entry address, and the locations of two tables.

Program Header Table describes Segments: which file bytes map to which virtual addresses, their size, and permissions (R/W/X). The kernel reads this table to load the program.

Section Header Table describes Sections: names, types, file offsets, sizes, and inter-section relationships. Linkers, debuggers, and disassemblers rely on this table.

Thus the same bytes have two primary views:

Runtime view: Program Header → Segment → memory mapping
Linking view: Section Header → Section → code, data, symbols, relocations

A runnable executable can work without a complete Section Header Table; the kernel does not need section names like .text or .data to load it. Conversely, a relocatable object file ( .o) usually has full section info but may lack loadable Program Headers.

02 ELF Header: The File's Entry Index

The ELF Header begins with a 4-byte magic: 0x7F 'E' 'L' 'F'. Key fields include: EI_CLASS: 32-bit or 64-bit ELF. EI_DATA: endianness (little or big). e_type: object file type — ET_REL (relocatable), ET_EXEC (executable), ET_DYN (shared object/PIE). e_machine: target ISA (e.g., x86-64, AArch64). e_entry: declared entry virtual address (not main; typically _start which initializes the C runtime before calling main). e_phoff, e_phnum: Program Header Table file offset and entry count. e_shoff, e_shnum: Section Header Table file offset and entry count.

File offsets ( e_phoff, e_shoff) locate tables on disk; virtual addresses ( e_entry) refer to memory after loading. For PIE, the entry is relocated relative to the load base.

03 Section vs. Segment: What's the Difference?

Section

(节) serves compilation, linking, and analysis; Segment (段) serves loading and execution. Sections group by semantics:

.text      machine instructions
.rodata    read-only constants
.data      initialized writable data
.bss       uninitialized/zero-initialized data
.symtab    symbol table
.rela.*    relocation records

Segments describe contiguous file ranges mapped to contiguous virtual address ranges. A PT_LOAD entry specifies:

p_offset  file start
p_vaddr   virtual address after mapping
p_filesz  bytes provided in file
p_memsz   bytes needed in memory
p_flags   mapping permissions R/W/X

Multiple sections can fall into one segment; an executable may have multiple PT_LOAD segments. Typical layout groups instructions into R-X, read-only constants into R--, writable data into RW-. Grouping is determined by linker scripts, platform ABI, and hardening options — not a single ELF-mandated layout.

04 Where Code, Constants, and Variables Live

.text : machine instructions → R-X mapping.

.rodata : string literals, read-only constants, compiler-generated read-only tables → R-- mapping.

.data : initialized global/static variables with non-zero values. Initial contents stored in file; loaded into RW- region.

.bss : uninitialized or zero-initialized globals/statics. File records only size; no zero bytes stored. At load, if p_memsz > p_filesz, the kernel zero-fills the extra memory — .bss often resides in this gap. A 100 MB zero-initialized array does not bloat the ELF file but consumes virtual memory at runtime and physical pages on first write.

Heap and stack are not pre-stored ELF sections. The kernel creates the initial stack during process image creation; the heap grows on demand during execution.

05 Symbol Tables and Relocation: Solving Names and Addresses

When compiling a source file, external function/variable addresses are unknown. The object file records two structures:

Symbol Table

Answers "what is this name?" Examples: puts: undefined in current object, must be resolved from another object or library. add: defined in current object's .text. initialized: defined in current object's .data.

Static symbols reside in .symtab with names in .strtab. They can be stripped ( strip) without affecting execution.

Relocation Table

Answers "where to patch and how to compute?" Each entry contains: offset: location to patch. symbol: symbol the calculation depends on. type: relocation formula (architecture-specific). addend: explicit addend (present in RELA entries).

Formulas differ: absolute address may use S + A; PC-relative may use S + A - P (S = symbol address, A = addend, P = patch location). Relocation is not merely filling an absolute address; the linker or dynamic linker must compute per architecture and relocation type.

06 Dynamic Linking: Finding Shared Library Functions

Dynamically linked ELF files do not copy shared library code; they retain runtime dependencies and unresolved references. Key structures: .interp / PT_INTERP: path to the dynamic linker (program interpreter). .dynamic: runtime metadata, e.g., DT_NEEDED dependencies. .dynsym, .dynstr: symbols and names needed for dynamic linking. .rela.dyn, .rela.plt: runtime relocation records.

GOT (Global Offset Table): holds resolved addresses at runtime.

PLT (Procedure Linkage Table): provides jump stubs for external calls.

Calling puts() goes through PLT, then uses the GOT slot. If already resolved, control jumps directly to libc; if lazy binding and unresolved, the dynamic linker resolves the symbol, computes relocation, writes the address into GOT, then continues. Lazy binding is optional; programs can force eager binding via linker options, environment, or hardening settings. PLT, GOT, and dynamic linker roles remain unchanged regardless of binding time.

07 What Happens After execve?

execve

replaces the current process's address space with a new program image; PID stays the same, but old code, data, heap, and stack are replaced. Loading steps:

Validate ELF Header
→ Read Program Header
→ Map PT_LOAD segments
→ Set up initial user stack
→ Choose static or dynamic entry path

The kernel uses p_offset, p_vaddr, p_filesz, p_memsz, and permissions to create virtual memory mappings. It places argc, argv, envp, and auxv on the initial stack.

No PT_INTERP: kernel jumps directly to the executable's entry (static executable).

Has PT_INTERP: kernel loads the dynamic linker, jumps to its entry. The dynamic linker loads shared libraries, resolves symbols, performs relocations, then transfers control to the executable's _start. _start initializes the C runtime and finally calls main — that's why the kernel doesn't call main directly.

08 Dissecting an ELF with Tools

Example C program ( demo.c):

extern int puts(const char *text);
int initialized = 7;
int zero_filled;
static const char message[] = "Hello ELF";
int add(int left, int right) { return left + right + initialized; }
int main(void) { zero_filled = add(2, 3); puts(message); return zero_filled; }

Compile and run:

gcc -O0 -g demo.c -o demo
./demo
echo $?

Output:

Hello ELF
12

Inspection commands (GNU tools on Linux):

# File format, bits, architecture
file demo
# ELF Header
readelf -h demo
# Program Header & Segment mapping
readelf -l demo
# Section Header & section list
readelf -S demo
# Symbol table
readelf -s demo
# Relocation records
readelf -r demo
# Dynamic linking metadata & dependencies
readelf -d demo
# Disassemble machine instructions
objdump -d demo

Compile only (no link) to see object file details:

gcc -O0 -g -c demo.c -o demo.o
readelf -S demo.o
readelf -s demo.o
readelf -r demo.o

In demo.o you'll typically see: initialized in

.data
zero_filled

in

.bss
message

in

.rodata
add

, main in

.text
puts

as UND (undefined, awaiting link) .rela.text records relocations for puts, globals, and read-only data references

Exact addresses, section indices, and offsets vary with compiler version, architecture, optimization level, PIE settings, and linker scripts. Focus on structural relationships, not absolute values from a single run.

09 Summary

An ELF file contains not only the CPU's machine instructions but also the structural metadata that lets linkers and loaders make sense of those bytes.

ELF Header
→ Confirms format, locates entry and two tables

Program Header Table
→ Describes Segments, guides kernel memory mapping

Section Header Table
→ Describes Sections, supports linking, debugging, analysis

Symbol Table + Relocation
→ Resolves names and addresses not yet fixed

Dynamic Linking Metadata + GOT/PLT
→ Finds shared libraries and external functions at runtime
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

dynamic linkingELFloaderbinary analysislinkerprogram loadingLinux internalsexecutable file format
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.