MS-DOS EXE
MS-DOS EXE (or DOS EXE), also known as MZ format, is an executable file format used mainly by MS-DOS. It is the successor of COM. A number of other executable formats are extensions or hybrids of it; see EXE for those formats.
Contents |
Format details
Header structure
DOS EXE files begin with a fixed 28-byte header.
The field names in this table are taken from the IMAGE_DOS_HEADER structure defined in modern Windows SDKs. Byte order is little-endian.
Offset | Type | Name | Description and remarks |
---|---|---|---|
0 | byte[2] | e_magic | Signature - ASCII "MZ " or "ZM "
|
2 | uint16 | e_cblp | If nonzero, the number of bytes in the last page |
4 | uint16 | e_cp | Number of 512-byte pages in the file, not counting the "overlay" segment |
6 | uint16 | e_crlc | Number of relocations |
8 | uint16 | e_cparhdr | Header size, in 16-byte paragraphs |
10 | uint16 | e_minalloc | Minimum allocation |
12 | uint16 | e_maxalloc | Maximum allocation |
14 | int16 | e_ss | Initial SS register |
16 | uint16 | e_sp | Initial SP register |
18 | uint16 | e_csum | Checksum - Usually unused and set to 0 |
20 | uint16 | e_ip | Initial IP register |
22 | int16 | e_cs | Initial CS register |
24 | uint16 | e_lfarlc | Relocation table offset, in bytes from the start of the file |
26 | uint16 | e_ovno | Overlay number (or other custom data) - Usually unused |
The ZM signature was used by very old versions of the Microsoft linker (from while DOS 1.0 was still under development). By the time PC-DOS 1.0 was shipped, the ZM signature was already considered obsolete. However, DOS 1.0 accepted it for backwards compatibility, and that code was retained by all future DOS versions. Windows, however, rejects ZM and only accepts MZ.
Old versions of Microsoft's development tools would calculate the checksum correctly, but DOS has always ignored it when loading EXE files. As a result, many third party tools would either calculate it using the wrong algorithm, leave it at zero or at some other fixed value. In response to this reality, Microsoft eventually gave up and stopped setting it in their own build tools either (in Microsoft LINK 5.3, which corresponds to Microsoft C/C++ 7.0, which came out in the early 1990s). Hence, while in 1980s era executables it is commonly set, executables from the 1990s onwards it is likely zero.
Extended Header
DOS executables don't always contain these additional fields, but Windows and OS/2 executables always do:
Offset | Type | Name | Description and remarks |
---|---|---|---|
28 | byte[8] | e_res | Reserved bytes |
36 | uint16 | e_oemid | OEM identifier (rarely used) |
38 | uint16 | e_oeminfo | OEM information (rarely used, meaning depends on OEM identifier) |
40 | byte[20] | e_res2 | Reserved bytes |
60 | uint32 | e_lfanew | File offset of new format executable header (NE, LE, LX or PE) |
Special file positions
When analyzing DOS EXE files, especially "envelope" formats, it can be helpful to calculate certain special file positions. The positions given here are in bytes, from the start of the file.
- End of relocation table: e_lfarlc + 4×e_crlc
- Start of code image segment: 16×e_cparhdr
- Execution starting point (a.k.a. entry point): 16×e_cparhdr + 16×e_cs + e_ip. Note that e_cs may be negative.
- Start of overlay segment (or end of code image segment): If e_cblp=0, this is 512×e_cp. Otherwise, 512×(e_cp−1) + e_cblp.
Identification
See EXE#Identification for EXE format in general.
It's not clear if there is any completely reliable way to identify a file as strictly DOS EXE, except in the negative (i.e., it looks like EXE, and is not a valid NE, PE, etc., file).
If the relocation table offset is from 28 to 63, or any segment (relocation table or code image) overlaps the four bytes starting at offset 60, it is pretty certainly DOS EXE.
Most non-DOS EXE files set the relocation table offset to 64, but it's probably not safe to rely on that.
Sample files
Links
- Wikipedia article
- MZ, from the OSDev Wiki
- http://www.delorie.com/djgpp/doc/exe/
- DOS EXE format
- EXE Explorer utility
- Ralf Brown's Interrupt Reference has an extensive list of (mostly older) MZ-based executable formats