Format:DOS MZ executable
| Developer | Microsoft |
|---|---|
| Magic | 4D 5A ("MZ") at 0 |
| Extensions | .exe (DOS); stub of Windows .exe/.dll/.sys |
| Endianness | little |
| On this wiki | Facts · Code · Notes · Topics |
The MS-DOS executable header ("MZ") that starts every DOS .EXE and, as a stub, every Windows NE, LE/LX and PE file: a 28-byte header, extended to 64 bytes in Windows-era files, whose dword at 0x3C (e_lfanew) points to the new-format header. The stub program after it identifies the linker (Microsoft link and GNU ld, lld-link, Borland).
Layout
The 28-byte MS-DOS header (extended to 64 bytes by Windows-era linkers as IMAGE_DOS_HEADER), little endian:
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0x00 | 2 | e_magic | 4D 5A "MZ" (5A 4D "ZM" is also accepted by DOS)
|
| 0x02 | 2 | e_cblp | bytes in the last 512-byte page (0 = full) |
| 0x04 | 2 | e_cp | number of 512-byte pages in the DOS image |
| 0x06 | 2 | e_crlc | number of relocation entries |
| 0x08 | 2 | e_cparhdr | header size in 16-byte paragraphs |
| 0x0A | 2 | e_minalloc | extra paragraphs needed |
| 0x0C | 2 | e_maxalloc | extra paragraphs wanted |
| 0x0E | 2+2 | e_ss, e_sp | initial SS (relative) and SP |
| 0x12 | 2 | e_csum | checksum (usually 0) |
| 0x14 | 2+2 | e_ip, e_cs | entry point IP and CS (relative) |
| 0x18 | 2 | e_lfarlc | file offset of the relocation table (0x40 or more in files with a new-style header) |
| 0x1A | 2 | e_ovno | overlay number |
| 0x1C | 8 | e_res | reserved |
| 0x24 | 2+2 | e_oemid, e_oeminfo | |
| 0x28 | 20 | e_res2 | reserved |
| 0x3C | 4 | e_lfanew | file offset of the new header: PE\0\0 (Format:PE (Portable Executable)), NE (Format:NE (New Executable)), LE/LX (VxD, OS/2 2.x)
|
The DOS image is e_cp * 512 bytes (minus 512 - e_cblp when e_cblp is not 0) and its code starts after the header (e_cparhdr * 16). In a Windows executable that image is only the stub program; everything after it belongs to the new format, so a DOS loader never sees the Windows code.
Stubs
The stub is the small DOS program that runs when the file is started under MS-DOS. It is chosen by the linker and identifies it:
| Linker | DOS header bytes 0x00-0x1B | Stub at 0x40 | e_lfanew |
|---|---|---|---|
| Microsoft link, GNU ld (binutils copies Microsoft's) | 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 ... 40 00 |
0E 1F BA 0E 00 B4 09 CD 21 B8 01 4C CD 21 + "This program cannot be run in DOS mode.\r\r\n$" |
after the Rich header (Microsoft link) or 0x80 (GNU ld)
|
| LLVM lld-link | 4D 5A 78 00 01 00 00 00 04 00 ... 40 00 |
the same code, string "This program cannot be run in DOS mode.$" without CR CR LF | 0x78
|
| Borland/Embarcadero (Delphi, C++Builder) | 4D 5A 50 00 02 00 00 00 04 00 0F 00 FF FF 00 00 B8 00 ... 40 00 1A 00 |
BA 10 00 0E 1F B4 09 CD 21 B8 01 4C CD 21 90 90 + "This program must be run under Win32\r\n$" |
0x100 in the samples seen
|
On about 8000 PE files of a Windows 11 installation and its programs (October 2026): 7739 had the Microsoft header followed by the Microsoft stub, 228 lld-link's header and stub, 4 Borland's (Inno Setup's Delphi-built uninstaller) and 9 Microsoft-like headers with small differences (e_sp 0x8B, or e_cparhdr moved).
The stub code is push cs; pop ds; mov dx, msg; mov ah, 9; int 21h; mov ax, 4C01h; int 21h: print a $-terminated string and exit with code 1.
Finding embedded executables
PE files inside memory dumps, installers, resources or archives start with one of these stubs at +0x40. Search for the stub bytes, subtract 0x40, and check that the dword at +0x3C points to PE\0\0. MZ alone gives many false positives; the stub signatures (signature facts of this page, also as a Ghidra/IDA script with export_symbols subject Format:DOS MZ executable) do not.
Overlays and packed or extended programs
The size rule above is exact: image size = (e_cp & 0x7FF) * 512, minus 512 - e_cblp when e_cblp is 1..511 (Fact:DOS MZ executable/behavior/Image size and overlay). Whatever follows is an overlay: appended data that the program reads itself (resources, a self-extractor's archive, Borland/Microsoft overlay managers), or the new-format header and code of an NE/LE/LX/PE file. e_ovno (offset 0x1A) is 0 for the main program and nonzero only for overlay files loaded through INT 21h AH=4Bh AL=3. Unverified: Borland's overlay manager appends its overlay block after the image with a header beginning FBOV ("FB" overlay); look for those four bytes at the computed image end.
The 0x3C dword is only a new-header pointer when the relocation table offset is 0x40 or more (Fact:DOS MZ executable/behavior/e lfanew only for new executables). The DOS loader rules for memory and relocations are in Fact:DOS MZ executable/behavior/Loader memory and relocation; .COM files and the PSP are in Format:DOS COM and PSP.
Recognising what the MZ file really is (packers, extenders, self-extractors) is covered in Technique:DOS executable packers and extenders: Format:EXEPACK, Format:LZEXE, Format:PKLITE, Format:LE and LX (Linear Executable) (DOS/4GW and similar), Tool:DJGPP (COFF behind the stub) and Format:Phar Lap executables.
Code & repositories
- https://github.com/erocarrera/pefile @
58186a92164842cf758822e067bc79c59b3c80d9· license: MIT
pefile, the de facto Python PE parser (MIT): every header and directory, Rich header, imphash/exphash, checksum, resources and version info, relocations, delay imports, CodeView records, write-back of modified headers. The structure formats near the top of pefile.py (lines 2402-2800) are a compact field-by-field reference.
Tested on: Read at this commit; imported from the clone with Python 3.11 on Windows 11 to cross-check peinfo.py: checksums, Rich entries, imphash (1491 files), export tables and RSDS keys agree. Note: in parse_delay_import_directory (line 5693) the VC6-style branch sets phmod from pUnloadIAT, a copy-paste slip; the test data archive is encrypted (malware samples). · checked 2026-10-17; last activity 2026-10-12; Note: the latest release ships binaries; the wiki links only the source, never the binaries - https://github.com/jsummers/deark @
8997059c4f32af79978fb6c83be7c1c26c259afa· license: MIT (declared by the contributor for the files used; GitHub reports MIT (read from COPYING; GitHub's API was busy) for the repository)
Deark (MIT): modules exepack, lzexe, pklite, diet, mscompress and the shared EXE detection in src/fmtutil-exe.c are the most complete open documentation of DOS packer recognition, MZ size computation and header layouts.
Tested on: source read, not run: Read the cited files at this commit; nothing built or run. · checked 2026-10-19 - https://github.com/dosbox-staging/dosbox-staging @
e8b4771a7de58a7d5c6d7c9c5b36c6ab09c6d93d· license: GPL-2.0-or-later (declared by the contributor for the files used; GitHub reports BSD-3-Clause AND GPL-2.0-or-later (read from LICENSE; GitHub's API was busy) for the repository)
DOSBox Staging (GPL-2.0-or-later): src/dos/dos_execute.cpp is a compact reference loader for .COM and MZ .EXE files and overlays; src/dos/dos.h and dos_classes.cpp define the PSP.
Tested on: source read, not run: Read dos_execute.cpp, dos.h and dos_classes.cpp at this commit; nothing built or run. · checked 2026-10-19 - https://github.com/open-watcom/open-watcom-v2 @
96b007f094bb21c6108b2793706fa298da59fc5e· license: Watcom-1.0 (declared by the contributor; GitHub's detector did not recognize a license file)
Open Watcom v2: bld/watcom/h/exephar.h, exesigns.h and exedos.h give the Phar Lap MP/P3/DX structures, the two-letter executable signatures (BW, P2, P3, PL, MQ...) and the DOS EXE header.
Tested on: source read, not run: Read the three headers at this commit; nothing built or run. · checked 2026-10-19
Evidence & references
- pefile (erocarrera/pefile) at commit 58186a9,
IMAGE_DOS_HEADERformat, pefile.py lines 2402-2424. - LLVM lld/COFF/Writer.cpp (main, 2026-10-17): its stub source and bytes, lines 50-80, and the DOS header it writes, lines 1855-1870.
- Byte counts from a scan of PE files with a Python script (October 2026).