PE Format Deep Dive
Linking, Loading & Binary Formats · OS Core Internals track
4 min read
In this module
The PE Format
The Portable Executable (PE) format is the standard binary format on Windows. Every .exe, .dll, .sys driver, and .ocx control on Windows is a PE file. It descends from the older COFF (Common Object File Format) and drags along some historical baggage, but understanding its structure is essential for Windows reverse engineering and security research.
DOS Header and Stub
Every PE file begins with a DOS header, a relic from the MS-DOS era. The first two bytes are the magic number:
4D 5A # "MZ" - Mark Zbikowski's initials (designer of the DOS executable format)The DOS header exists so that if someone tries to run a PE binary under DOS, a tiny embedded programme (the DOS stub) executes and prints a message instead. The field that matters in the DOS header is e_lfanew at offset 0x3C, a 4-byte pointer to the actual PE signature.
At the offset given by e_lfanew, you find the PE signature:
50 45 00 00 # "PE\0\0"A PE file starts with a DOS header (magic 4D 5A). The e_lfanew field at offset 0x3C points to the PE signature (50 45 00 00), which is followed by the COFF header and optional header. The layered structure is the price of staying backwards-compatible with DOS.
COFF Header
Immediately after the PE signature sits the COFF header (also called the File Header). It contains:
- Machine type - the target architecture (0x8664 for x86-64, 0x14C for x86)
- Number of sections - how many section headers follow
- Timestamp - when the binary was linked (useful in forensics)
- Size of optional header - the length of the next header
- Characteristics - flags indicating whether this is an executable, a DLL, or whether it has been stripped
Optional Header
Despite the name, the optional header is required for executables and DLLs. It specifies:
- AddressOfEntryPoint - the RVA (Relative Virtual Address) where execution begins
- ImageBase - the preferred base address in memory (typically
0x00400000for 32-bit executables,0x0000000140000000for 64-bit) - SectionAlignment and FileAlignment - alignment constraints for sections in memory and on disk
- Data Directories - an array of 16 entries pointing to important structures like the import table, export table, resource table, and relocation table
Sections
PE sections are analogous to ELF sections. Common ones include:
.text- executable code.data- initialised read-write data.rdata- read-only data (constants, import tables).bss- uninitialised data.rsrc- resources (icons, strings, version info, embedded files).reloc- base relocation data (needed for ASLR)
Each section header records the section's virtual address, size, file offset, and permission flags (readable, writable, executable).
The Import Address Table (IAT)
The Import Address Table is the PE equivalent of ELF's GOT. When a PE binary calls a function from a DLL (say, kernel32.dll!CreateFileW), it doesn't call the function directly. It calls through a pointer in the IAT.
At load time, the Windows loader resolves each imported function and writes its actual address into the IAT. The binary's code then uses indirect calls through those IAT pointers. The analogy with the GOT has one big difference: binding is eager. There's no PLT-style lazy resolution on first call; every normal import is resolved before the program starts. A binary that wants lazy loading has to ask for it explicitly with delay-load imports, which the linker implements with a separate table and a small helper stub.
The import data is organised into an Import Directory Table with one entry per DLL. Each entry points to an Import Lookup Table (function names or ordinals) and an IAT (initially identical, then overwritten by the loader with resolved addresses).
Explore the Format
The explorer below lets you wander through a PE binary's structure interactively. Walk through the DOS header, PE signature, COFF header, optional header, and sections to see how they fit together.
Click a section to explore its structure and purpose
PE vs ELF at a Glance
| Feature | ELF | PE |
|---|---|---|
| Magic | 7f 45 4c 46 | 4D 5A + 50 45 00 00 |
| Dynamic imports | GOT/PLT | IAT |
| Code section | .text | .text |
| Stack NX control | PT_GNU_STACK | Linker flag (/NXCOMPAT) |
| ASLR support | PIE compilation | .reloc section + DynamicBase flag |
Why It Matters
PE analysis is the foundation of Windows malware analysis, exploit development, and security auditing. The IAT is a favourite target for API hooking (overwriting IAT entries to redirect function calls). The import directory reveals what capabilities a binary uses. Understanding e_lfanew, section layouts, and data directories is essential for parsing binaries, writing loaders, and spotting format-level anti-analysis tricks like header manipulation.