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:PE (Portable Executable)

From ReverseEngineering.wiki
Format
Developer Microsoft (Windows NT 3.1, 1993)
Magic 4D 5A at 0; 50 45 00 00 ("PE\0\0") at the offset stored at 0x3C
Extensions .exe, .dll, .sys, .ocx, .cpl, .scr, .efi, .mui
Endianness little
On this wiki Facts · Code · Notes · Topics

Executable format of 32- and 64-bit Windows (.exe, .dll, .sys, .ocx, .efi), also used by UEFI, Xbox and .NET assemblies: an MZ stub whose offset 0x3C points to "PE\0\0", a COFF file header, an optional header (PE32 magic 0x10B or PE32+ 0x20B) with 16 data directories (imports, exports, resources, relocations, debug, TLS, .NET...), then a section table. Almost every pointer is an RVA, an offset from the image base.

Identification

  • 4D 5A ("MZ") at offset 0, a little-endian uint32 e_lfanew at 0x3C, and 50 45 00 00 ("PE\0\0") at that offset. NE, LE/LX or VZ (TE, UEFI) at the same place mean another format (see Format:DOS MZ executable, Format:NE (New Executable)).
  • The uint16 right after the 20-byte COFF file header is the optional header magic: 0x10B PE32, 0x20B PE32+ (64-bit), 0x107 ROM image.
  • Typical e_lfanew values tell the linker apart before anything is parsed: 0x78 with no Rich header is LLVM lld-link; 0x80 with no Rich header is GNU ld (MinGW, Cygwin/MSYS2), Go, a .NET compiler, or Microsoft link with the Rich header suppressed (then the linker version is 14.x, not 2.xx); larger values (0xB0-0x118 and up) are Microsoft link.exe with a Format:PE (Portable Executable)/Rich header between the DOS stub and the PE signature. See #Compiler and linker identification.
  • Inside a memory dump, installer or archive, search for the DOS stub (signature facts on Format:DOS MZ executable) and check PE\0\0 at the e_lfanew it gives; export_symbols with subject Format:DOS MZ executable gives a Ghidra/IDA script that finds them.

Layout

All fields are little endian. Offsets are from the start of the file.

Offset Size Structure
0 64 DOS header (IMAGE_DOS_HEADER, e_lfanew at 0x3C)
0x40 varies DOS stub program, then the Rich header (Microsoft link only)
e_lfanew 4 Signature PE\0\0
e_lfanew+4 20 COFF file header (IMAGE_FILE_HEADER): Machine, NumberOfSections, TimeDateStamp, PointerToSymbolTable, NumberOfSymbols, SizeOfOptionalHeader, Characteristics
e_lfanew+24 SizeOfOptionalHeader Optional header: 96 (PE32) or 112 (PE32+) bytes of fixed fields, then NumberOfRvaAndSizes (normally 16) data directories of 8 bytes (RVA, size): 224 or 240 bytes in total
e_lfanew+24+SizeOfOptionalHeader 40 x NumberOfSections Section table (IMAGE_SECTION_HEADER)
SizeOfHeaders Section data, each at PointerToRawData (multiple of FileAlignment)
end of last section Overlay: certificate table, installer payloads, debug info

PE32 has a 4-byte BaseOfData field that PE32+ lacks, and PE32+ widens ImageBase and the four stack/heap sizes to 8 bytes, so every field after BaseOfCode sits at a different offset in the two variants (CheckSum is at optional-header offset 64 in both, Subsystem at 68, DllCharacteristics at 70, the data directories at 96 or 112).

Data directories, by index: 0 export, 1 import, 2 resource, 3 exception (.pdata), 4 security (certificate table), 5 base relocations, 6 debug, 7 architecture (0), 8 global pointer, 9 TLS, 10 load config, 11 bound import, 12 IAT, 13 delay import, 14 CLR (.NET) header (managed code: Format:.NET metadata (ECMA-335)), 15 reserved.

Addresses

  • RVA (relative virtual address) = address in memory minus ImageBase. Almost every pointer in the headers and directories is an RVA. VA = ImageBase + RVA; only the IAT contents after binding, relocated absolute pointers in code/data and the TLS and load-config directories hold VAs.
  • RVA to file offset: find the section with VirtualAddress <= rva < VirtualAddress + max(VirtualSize, SizeOfRawData), then offset = PointerToRawData + (rva - VirtualAddress). RVAs below SizeOfHeaders map 1:1 to the headers. Bytes beyond SizeOfRawData but inside VirtualSize are zero-filled at load time (.bss, the tail of .data) and have no file offset.
  • Default ImageBase: 0x400000 for PE32 executables, 0x10000000 for PE32 DLLs, 0x140000000 for PE32+ executables and 0x180000000 for PE32+ DLLs (Microsoft link; GNU ld uses other values, e.g. 0x100400000 for Cygwin/MSYS2 programs). With DYNAMIC_BASE (ASLR) the loader rebases using the base relocation directory: blocks of {uint32 page RVA, uint32 block size} followed by uint16 entries whose high 4 bits are the type (3 HIGHLOW on x86, 10 DIR64 on x64/ARM64, 0 padding) and low 12 bits the offset in the page.
  • A disassembler shows addresses as VAs at the preferred ImageBase; address facts on this wiki use RVAs (data.rva on a Build: page) so they survive rebasing.

Imports and exports

  • The import directory is an array of 20-byte IMAGE_IMPORT_DESCRIPTORs ended by an all-zero entry: OriginalFirstThunk (RVA of the import lookup table), TimeDateStamp (0 until the image is bound), ForwarderChain, Name (RVA of the DLL name), FirstThunk (RVA of the IAT). Each lookup-table entry is 4 bytes (PE32) or 8 bytes (PE32+): with the top bit set (0x80000000 / 0x8000000000000000) the low 16 bits are an ordinal, otherwise it is the RVA of a {uint16 hint; char name[]} record. The loader overwrites the IAT (FirstThunk) with the resolved addresses, so code calls through call [IAT slot]; name the IAT slots first when starting on an unknown binary.
  • Delay-load imports (directory 13) use 32-byte descriptors; attribute bit 0 set means the fields are RVAs (all current linkers); 0 means old-style VAs (VC6-era x86 files).
  • The export directory (40 bytes) gives the DLL name, the ordinal base, and three parallel tables: AddressOfFunctions (RVAs indexed by ordinal - base), AddressOfNames (sorted name RVAs) and AddressOfNameOrdinals (uint16 indexes into the function table). A function RVA that falls inside the export directory's own range is a forwarder string such as NTDLL.RtlAcquireSRWLockExclusive (211 of the 1698 exports of a Windows 11 x64 kernel32.dll).
  • imphash (pefile get_imphash, also used by YARA and VirusTotal) is the MD5 of the comma-joined, lowercased dll.function list in import order, with .dll/.ocx/.sys stripped from the DLL name; ordinal imports from ws2_32, wsock32 and oleaut32 are replaced by names from pefile's frozen tables, others become ord<N>.

Checksum

The optional header CheckSum (offset 64) is a 16-bit one's-complement-style sum of the whole file as little-endian 16-bit words with carries folded, skipping the CheckSum field itself, plus the file length (the algorithm of imagehlp!CheckSumMappedFile). The loader checks it only for drivers, boot-time DLLs and DLLs loaded into critical processes; Microsoft link sets it with /RELEASE (all Windows system files have it), MinGW-built files observed had it set too, and many third-party programs leave it 0. The certificate table is included in the sum, so signing changes the checksum.

Overlay

Anything after the end of the last section's raw data is the overlay. The loader never maps it. It holds the Authenticode certificate table (directory 4, whose "VirtualAddress" is a file offset, not an RVA, 8-byte aligned), the payload of self-extracting archives and installers (Format:NSIS installer: a firstheader with "NullsoftInst" at the 512-aligned overlay start; Inno Setup, WiX burn), appended debug data, or game data. Tools that compute the overlay start must skip directory 4 when looking for the furthest referenced byte, or they report the signature as part of the image.

Compiler and linker identification

What the headers say without disassembly, observed on about 8000 PE files of a Windows 11 installation and the programs installed on it (October 2026):

Toolchain Rich header MajorLinker.MinorLinker Other signs
Microsoft link.exe (MSVC; also Rust *-pc-windows-msvc, e.g. ripgrep 15.2.0: linker 14.51, Rich linker build 36248) yes 6.0 (VC6), 7.x, 8.0 (VS2005), 9.0, 10.0, 11.0, 12.0, 14.0 (VS2015), 14.1x (VS2017), 14.2x (VS2019), 14.3x-14.4x (VS2022), 14.5x (VS2026) e_lfanew > 0x80, MS DOS stub; Rich entries name the exact toolset build
Microsoft link.exe with the Rich header suppressed no 14.x (14.51 in OBS Studio 32.2.2) e_lfanew 0x80, MS DOS stub; seen in 10 files of Software:OBS Studio 32.2.2 (obs-outputs.dll and others). Unverified: produced by the undocumented link option /EMITTOOLVERSIONINFO:NO; test by linking a hello-world with and without it
LLVM lld-link no 14.0 (hard-coded) e_lfanew 0x78, its own DOS stub without \r\r\n (228 of 228 lld-stub files had linker 14.0)
GNU ld (MinGW-w64, Cygwin, MSYS2) no the binutils version (2.25 ... 2.46) e_lfanew 0x80, MS-identical DOS stub, sections .xdata, .bss, .idata, often .CRT, .tls, .buildid
Go (gc toolchain) no 3.0 (hard-coded) .symtab, Go build ID at the start of .text; see Library:Go runtime
.NET compilers, IL-only assemblies no 48.0 on most (4233 of 4441 managed files; also .NET Framework 4.x System.dll), 8.0 and 80.0 on others. Unverified: 48.0 is Roslyn's default CLR header (directory 14, see Format:.NET metadata (ECMA-335)), sections .text, .rsrc, .reloc only, one import mscoree.dll!_CorExeMain or _CorDllMain (mscorlib.dll has none)
Crossgen2 (.NET ReadyToRun images: the .NET 5+ shared framework, PublishReadyToRun) no 11.0 (191 of 191 checked) CLR header with flag IL_LIBRARY and a READYTORUN_HEADER ("RTR"), no import directory, sections .text, .data, .reloc; the Crossgen2 version string is in the R2R CompilerIdentifier section
C++/CLI mixed-mode assemblies yes (Microsoft link.exe) 14.x CLR header without ILONLY (often NATIVE_ENTRYPOINT), extra sections such as .nep, many native imports
Embarcadero Delphi / C++Builder no 2.25 observed (Inno Setup's Delphi-built uninstaller) Borland DOS stub "This program must be run under Win32", sections .itext, .didata

The TimeDateStamp is the link time only when the build is not reproducible: a REPRO debug entry (link /Brepro, lld-link /Brepro, all Windows 10/11 system files) means it is a hash; see Format:PE (Portable Executable)/Debug directory.

Tools

  • pefile (Python, MIT): parses every header and directory, Rich header, imphash, checksum, resources and version info; the de facto reference for scripts. python pefile.py FILE dumps everything.
  • Code:PE (Portable Executable)/peinfo (Python): dependency-free inspector written for this page (headers, sections, Rich header with key verification, checksum, imports/imphash, exports, debug/PDB key, .NET header, overlay).
  • dumpbin /headers /imports /exports (Visual Studio), llvm-readobj --file-headers --coff-imports, objdump -x (binutils), Ghidra and IDA loaders.

Topics

  • Rich header — The undocumented block that Microsoft link.exe writes between the DOS stub and the PE header: "DanS", a list of (product id, build, count) entries for every com
  • Debug directory — Data directory 6 of a PE file: 28-byte IMAGE_DEBUG_DIRECTORY entries pointing to the CodeView record (RSDS: PDB GUID, age and path, the key for symbol servers),
  • TLS directory — Data directory 9 of a PE file: the IMAGE_TLS_DIRECTORY (24 bytes in PE32, 40 in PE32+) with the template of the thread-local data and a NULL-terminated array…
  • Resource directory — Data directory 2 of a PE file: a three-level tree (type, name or id, language) of IMAGE_RESOURCE_DIRECTORY tables with RT_* types such as icons, dialogs…
  • Authenticode — How a PE file carries a code signature: data directory 4 points (by file offset) to WIN_CERTIFICATE records holding a PKCS#7 SignedData whose content is an…
  • Load config directory — Data directory 10 of a PE file: IMAGE_LOAD_CONFIG_DIRECTORY, a versioned structure (its first dword is its own size) holding the security cookie, the SafeSEH…
  • Exception directory — Data directory 3 of a 64-bit PE (.pdata): a sorted array of 12-byte RUNTIME_FUNCTION entries, one per non-leaf function, pointing to UNWIND_INFO records that…

Code & repositories

  • https://github.com/dishather/richprint @ b0a4f598cc076a8c2ff4b1b90850004bc17da31c · license: BSD-2-Clause
    richprint: prints the Rich header of PE and COFF files; its comp_id.txt maps comp.id values (product id + build) to tool names and Visual Studio releases from VS97 to VS2026 18.12 Insiders (about 4800 lines). The reference to turn a Rich entry into a Visual Studio update.
    Tested on: comp_id.txt read at this commit; its product-id names and linker build numbers agree with the Rich headers and linker versions of Windows 11 system files and installed MSVC programs (e.g. 14.38 with builds 33130-33145, 14.51 with 36248). The C++ tool itself was not built (no compiler on the test machine). · checked 2026-10-17; last activity 2026-10-14
  • https://github.com/lief-project/LIEF @ 15777f106f6220046cf26ee99940d1f56751325e · license: Apache-2.0
    LIEF (Apache-2.0): PE parser with Authenticode signature parsing (Parser::parse_signature, SignatureParser, OIDToString), load configuration with the IMAGE_GUARD flags and CFG table stride, and x64/ARM64 runtime-function and unwind-code decoders.
    Tested on: source read, not run: Read at this commit (sparse checkout of include/LIEF/PE and src/PE); not built. · 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/erocarrera/pefile @ 58186a92164842cf758822e067bc79c59b3c80d9 · license: MIT
    pefile (MIT): structure formats for TLS, resource tree, VS_VERSIONINFO, load config and a full x64 UNWIND_INFO decoder (UnwindInfo class, lines 1802-2200).
    Tested on: source read, not run: Read at this commit. · checked 2026-10-18; last activity 2026-10-12; Note: the latest release ships binaries; the wiki links only the source, never the binaries
  • https://github.com/mtrojnar/osslsigncode @ 868d6e95c603a8057e89336f12fe12b5d2258e00 · license: GPL-3.0-or-later (declared by the contributor; GitHub's detector did not recognize COPYING.txt)
    osslsigncode (GPL-3.0-or-later): Authenticode signing and verification for PE files; pe.c shows the exact digest ranges, the certificate table loop and the page hash table calculation (pe_page_hash_calc). Read for format facts only, no code copied.
    Tested on: source read, not run: Read at this commit; not built. · checked 2026-10-18; last activity 2026-10-15; Note: the latest release ships binaries; the wiki links only the source, never the binaries

Pitfalls

  • The Windows loader rounds a section's PointerToRawData down to a multiple of 512 when FileAlignment is at least 512 (pefile adjust_PointerToRawData copies this); malformed or packed files rely on it, so a parser that uses the raw value reads the wrong bytes.
  • When SectionAlignment is below the page size (4096), FileAlignment must equal SectionAlignment and the sections are laid out so that RVA == file offset; drivers and small hand-made files use this.
  • Do not trust NumberOfRvaAndSizes beyond 16 or SizeOfOptionalHeader to be the standard size: the section table starts at e_lfanew + 24 + SizeOfOptionalHeader, whatever that value is.
  • Directory 4 holds a file offset, not an RVA (see #Overlay).
  • An export RVA inside the export directory is a forwarder string, not code.
  • PE32 vs PE32+ is decided by the optional header magic, not by Machine: ARM64EC/ARM64X images (Machine 0xA641 / 0xA64E) are PE32+.

Evidence & references

  • Microsoft, "PE Format" (learn.microsoft.com/en-us/windows/win32/debug/pe-format): header fields, machine types, data directories, certificate table as a file pointer, relocation types, import lookup table bit 31/63, debug types including REPRO.
  • pefile at commit 58186a9 (2026-10-12): structure formats lines 2402-2660, constant tables lines 110-360, checksum lines 7720-7760, overlay lines 7850-7895, loader rounding lines 7915-7955, imphash lines 5770-5823 and ordlookup/init.py.
  • LLVM lld/COFF/Writer.cpp (main, 2026-10-17): DOS stub lines 50-80, linker version 14.0 lines 1910-1916; Go src/cmd/link/internal/ld/pe.go lines 1172-1175 (linker version 3.0).
  • Checksum, imports/imphash, exports and CodeView parsing of peinfo.py agree with imagehlp!CheckSumMappedFile and pefile on Windows 11 system files (kernel32.dll x64 and x86, ntdll.dll, cmd.exe, imagehlp.dll, mscorlib.dll) and MinGW-built git.exe; the checksum matched on 3992 of the 3999 files with a non-zero CheckSum.