1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Portable Executable binary format structures
5 * Copyright (c) 2016 Alexander Graf
13 #include <asm-generic/pe.h>
15 typedef struct _IMAGE_DOS_HEADER {
16 uint16_t e_magic; /* 00: MZ Header signature */
17 uint16_t e_cblp; /* 02: Bytes on last page of file */
18 uint16_t e_cp; /* 04: Pages in file */
19 uint16_t e_crlc; /* 06: Relocations */
20 uint16_t e_cparhdr; /* 08: Size of header in paragraphs */
21 uint16_t e_minalloc; /* 0a: Minimum extra paragraphs needed */
22 uint16_t e_maxalloc; /* 0c: Maximum extra paragraphs needed */
23 uint16_t e_ss; /* 0e: Initial (relative) SS value */
24 uint16_t e_sp; /* 10: Initial SP value */
25 uint16_t e_csum; /* 12: Checksum */
26 uint16_t e_ip; /* 14: Initial IP value */
27 uint16_t e_cs; /* 16: Initial (relative) CS value */
28 uint16_t e_lfarlc; /* 18: File address of relocation table */
29 uint16_t e_ovno; /* 1a: Overlay number */
30 uint16_t e_res[4]; /* 1c: Reserved words */
31 uint16_t e_oemid; /* 24: OEM identifier (for e_oeminfo) */
32 uint16_t e_oeminfo; /* 26: OEM information; e_oemid specific */
33 uint16_t e_res2[10]; /* 28: Reserved words */
34 uint32_t e_lfanew; /* 3c: Offset to extended header */
35 } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
37 #define IMAGE_DOS_SIGNATURE 0x5A4D /* MZ */
38 #define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
40 #define IMAGE_FILE_MACHINE_I386 0x014c
41 #define IMAGE_FILE_MACHINE_ARM 0x01c0
42 #define IMAGE_FILE_MACHINE_THUMB 0x01c2
43 #define IMAGE_FILE_MACHINE_ARMNT 0x01c4
44 #define IMAGE_FILE_MACHINE_AMD64 0x8664
45 #define IMAGE_FILE_MACHINE_ARM64 0xaa64
46 #define IMAGE_FILE_MACHINE_RISCV32 0x5032
47 #define IMAGE_FILE_MACHINE_RISCV64 0x5064
49 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
50 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
51 #define IMAGE_SUBSYSTEM_EFI_APPLICATION 10
53 typedef struct _IMAGE_FILE_HEADER {
55 uint16_t NumberOfSections;
56 uint32_t TimeDateStamp;
57 uint32_t PointerToSymbolTable;
58 uint32_t NumberOfSymbols;
59 uint16_t SizeOfOptionalHeader;
60 uint16_t Characteristics;
61 } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
63 typedef struct _IMAGE_DATA_DIRECTORY {
64 uint32_t VirtualAddress;
66 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
68 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
70 typedef struct _IMAGE_OPTIONAL_HEADER64 {
71 uint16_t Magic; /* 0x20b */
72 uint8_t MajorLinkerVersion;
73 uint8_t MinorLinkerVersion;
75 uint32_t SizeOfInitializedData;
76 uint32_t SizeOfUninitializedData;
77 uint32_t AddressOfEntryPoint;
80 uint32_t SectionAlignment;
81 uint32_t FileAlignment;
82 uint16_t MajorOperatingSystemVersion;
83 uint16_t MinorOperatingSystemVersion;
84 uint16_t MajorImageVersion;
85 uint16_t MinorImageVersion;
86 uint16_t MajorSubsystemVersion;
87 uint16_t MinorSubsystemVersion;
88 uint32_t Win32VersionValue;
90 uint32_t SizeOfHeaders;
93 uint16_t DllCharacteristics;
94 uint64_t SizeOfStackReserve;
95 uint64_t SizeOfStackCommit;
96 uint64_t SizeOfHeapReserve;
97 uint64_t SizeOfHeapCommit;
99 uint32_t NumberOfRvaAndSizes;
100 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
101 } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
103 typedef struct _IMAGE_NT_HEADERS64 {
105 IMAGE_FILE_HEADER FileHeader;
106 IMAGE_OPTIONAL_HEADER64 OptionalHeader;
107 } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
109 typedef struct _IMAGE_OPTIONAL_HEADER {
111 /* Standard fields */
113 uint16_t Magic; /* 0x10b or 0x107 */ /* 0x00 */
114 uint8_t MajorLinkerVersion;
115 uint8_t MinorLinkerVersion;
117 uint32_t SizeOfInitializedData;
118 uint32_t SizeOfUninitializedData;
119 uint32_t AddressOfEntryPoint; /* 0x10 */
123 /* NT additional fields */
126 uint32_t SectionAlignment; /* 0x20 */
127 uint32_t FileAlignment;
128 uint16_t MajorOperatingSystemVersion;
129 uint16_t MinorOperatingSystemVersion;
130 uint16_t MajorImageVersion;
131 uint16_t MinorImageVersion;
132 uint16_t MajorSubsystemVersion; /* 0x30 */
133 uint16_t MinorSubsystemVersion;
134 uint32_t Win32VersionValue;
135 uint32_t SizeOfImage;
136 uint32_t SizeOfHeaders;
137 uint32_t CheckSum; /* 0x40 */
139 uint16_t DllCharacteristics;
140 uint32_t SizeOfStackReserve;
141 uint32_t SizeOfStackCommit;
142 uint32_t SizeOfHeapReserve; /* 0x50 */
143 uint32_t SizeOfHeapCommit;
144 uint32_t LoaderFlags;
145 uint32_t NumberOfRvaAndSizes;
146 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; /* 0x60 */
148 } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
150 typedef struct _IMAGE_NT_HEADERS {
151 uint32_t Signature; /* "PE"\0\0 */ /* 0x00 */
152 IMAGE_FILE_HEADER FileHeader; /* 0x04 */
153 IMAGE_OPTIONAL_HEADER32 OptionalHeader; /* 0x18 */
154 } IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
156 #define IMAGE_SIZEOF_SHORT_NAME 8
158 typedef struct _IMAGE_SECTION_HEADER {
159 uint8_t Name[IMAGE_SIZEOF_SHORT_NAME];
161 uint32_t PhysicalAddress;
162 uint32_t VirtualSize;
164 uint32_t VirtualAddress;
165 uint32_t SizeOfRawData;
166 uint32_t PointerToRawData;
167 uint32_t PointerToRelocations;
168 uint32_t PointerToLinenumbers;
169 uint16_t NumberOfRelocations;
170 uint16_t NumberOfLinenumbers;
171 uint32_t Characteristics;
172 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
174 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5
176 typedef struct _IMAGE_BASE_RELOCATION
178 uint32_t VirtualAddress;
179 uint32_t SizeOfBlock;
180 /* WORD TypeOffset[1]; */
181 } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION;
183 typedef struct _IMAGE_RELOCATION
186 uint32_t VirtualAddress;
189 uint32_t SymbolTableIndex;
191 } IMAGE_RELOCATION, *PIMAGE_RELOCATION;
193 #define IMAGE_SIZEOF_RELOCATION 10
195 /* generic relocation types */
196 #define IMAGE_REL_BASED_ABSOLUTE 0
197 #define IMAGE_REL_BASED_HIGH 1
198 #define IMAGE_REL_BASED_LOW 2
199 #define IMAGE_REL_BASED_HIGHLOW 3
200 #define IMAGE_REL_BASED_HIGHADJ 4
201 #define IMAGE_REL_BASED_MIPS_JMPADDR 5
202 #define IMAGE_REL_BASED_ARM_MOV32A 5 /* yes, 5 too */
203 #define IMAGE_REL_BASED_ARM_MOV32 5 /* yes, 5 too */
204 #define IMAGE_REL_BASED_RISCV_HI20 5 /* yes, 5 too */
205 #define IMAGE_REL_BASED_SECTION 6
206 #define IMAGE_REL_BASED_REL 7
207 #define IMAGE_REL_BASED_ARM_MOV32T 7 /* yes, 7 too */
208 #define IMAGE_REL_BASED_THUMB_MOV32 7 /* yes, 7 too */
209 #define IMAGE_REL_BASED_RISCV_LOW12I 7 /* yes, 7 too */
210 #define IMAGE_REL_BASED_RISCV_LOW12S 8
211 #define IMAGE_REL_BASED_MIPS_JMPADDR16 9
212 #define IMAGE_REL_BASED_IA64_IMM64 9 /* yes, 9 too */
213 #define IMAGE_REL_BASED_DIR64 10
214 #define IMAGE_REL_BASED_HIGH3ADJ 11
216 /* ARM relocation types */
217 #define IMAGE_REL_ARM_ABSOLUTE 0x0000
218 #define IMAGE_REL_ARM_ADDR 0x0001
219 #define IMAGE_REL_ARM_ADDR32NB 0x0002
220 #define IMAGE_REL_ARM_BRANCH24 0x0003
221 #define IMAGE_REL_ARM_BRANCH11 0x0004
222 #define IMAGE_REL_ARM_TOKEN 0x0005
223 #define IMAGE_REL_ARM_GPREL12 0x0006
224 #define IMAGE_REL_ARM_GPREL7 0x0007
225 #define IMAGE_REL_ARM_BLX24 0x0008
226 #define IMAGE_REL_ARM_BLX11 0x0009
227 #define IMAGE_REL_ARM_SECTION 0x000E
228 #define IMAGE_REL_ARM_SECREL 0x000F
229 #define IMAGE_REL_ARM_MOV32A 0x0010
230 #define IMAGE_REL_ARM_MOV32T 0x0011
231 #define IMAGE_REL_ARM_BRANCH20T 0x0012
232 #define IMAGE_REL_ARM_BRANCH24T 0x0014
233 #define IMAGE_REL_ARM_BLX23T 0x0015
235 /* ARM64 relocation types */
236 #define IMAGE_REL_ARM64_ABSOLUTE 0x0000
237 #define IMAGE_REL_ARM64_ADDR32 0x0001
238 #define IMAGE_REL_ARM64_ADDR32NB 0x0002
239 #define IMAGE_REL_ARM64_BRANCH26 0x0003
240 #define IMAGE_REL_ARM64_PAGEBASE_REL21 0x0004
241 #define IMAGE_REL_ARM64_REL21 0x0005
242 #define IMAGE_REL_ARM64_PAGEOFFSET_12A 0x0006
243 #define IMAGE_REL_ARM64_PAGEOFFSET_12L 0x0007
244 #define IMAGE_REL_ARM64_SECREL 0x0008
245 #define IMAGE_REL_ARM64_SECREL_LOW12A 0x0009
246 #define IMAGE_REL_ARM64_SECREL_HIGH12A 0x000A
247 #define IMAGE_REL_ARM64_SECREL_LOW12L 0x000B
248 #define IMAGE_REL_ARM64_TOKEN 0x000C
249 #define IMAGE_REL_ARM64_SECTION 0x000D
250 #define IMAGE_REL_ARM64_ADDR64 0x000E
252 /* AMD64 relocation types */
253 #define IMAGE_REL_AMD64_ABSOLUTE 0x0000
254 #define IMAGE_REL_AMD64_ADDR64 0x0001
255 #define IMAGE_REL_AMD64_ADDR32 0x0002
256 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
257 #define IMAGE_REL_AMD64_REL32 0x0004
258 #define IMAGE_REL_AMD64_REL32_1 0x0005
259 #define IMAGE_REL_AMD64_REL32_2 0x0006
260 #define IMAGE_REL_AMD64_REL32_3 0x0007
261 #define IMAGE_REL_AMD64_REL32_4 0x0008
262 #define IMAGE_REL_AMD64_REL32_5 0x0009
263 #define IMAGE_REL_AMD64_SECTION 0x000A
264 #define IMAGE_REL_AMD64_SECREL 0x000B
265 #define IMAGE_REL_AMD64_SECREL7 0x000C
266 #define IMAGE_REL_AMD64_TOKEN 0x000D
267 #define IMAGE_REL_AMD64_SREL32 0x000E
268 #define IMAGE_REL_AMD64_PAIR 0x000F
269 #define IMAGE_REL_AMD64_SSPAN32 0x0010