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 typedef struct _IMAGE_FILE_HEADER {
39 uint16_t NumberOfSections;
40 uint32_t TimeDateStamp;
41 uint32_t PointerToSymbolTable;
42 uint32_t NumberOfSymbols;
43 uint16_t SizeOfOptionalHeader;
44 uint16_t Characteristics;
45 } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
47 typedef struct _IMAGE_DATA_DIRECTORY {
48 uint32_t VirtualAddress;
50 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
52 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
54 typedef struct _IMAGE_OPTIONAL_HEADER64 {
55 uint16_t Magic; /* 0x20b */
56 uint8_t MajorLinkerVersion;
57 uint8_t MinorLinkerVersion;
59 uint32_t SizeOfInitializedData;
60 uint32_t SizeOfUninitializedData;
61 uint32_t AddressOfEntryPoint;
64 uint32_t SectionAlignment;
65 uint32_t FileAlignment;
66 uint16_t MajorOperatingSystemVersion;
67 uint16_t MinorOperatingSystemVersion;
68 uint16_t MajorImageVersion;
69 uint16_t MinorImageVersion;
70 uint16_t MajorSubsystemVersion;
71 uint16_t MinorSubsystemVersion;
72 uint32_t Win32VersionValue;
74 uint32_t SizeOfHeaders;
77 uint16_t DllCharacteristics;
78 uint64_t SizeOfStackReserve;
79 uint64_t SizeOfStackCommit;
80 uint64_t SizeOfHeapReserve;
81 uint64_t SizeOfHeapCommit;
83 uint32_t NumberOfRvaAndSizes;
84 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
85 } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
87 typedef struct _IMAGE_NT_HEADERS64 {
89 IMAGE_FILE_HEADER FileHeader;
90 IMAGE_OPTIONAL_HEADER64 OptionalHeader;
91 } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
93 typedef struct _IMAGE_OPTIONAL_HEADER {
97 uint16_t Magic; /* 0x10b or 0x107 */ /* 0x00 */
98 uint8_t MajorLinkerVersion;
99 uint8_t MinorLinkerVersion;
101 uint32_t SizeOfInitializedData;
102 uint32_t SizeOfUninitializedData;
103 uint32_t AddressOfEntryPoint; /* 0x10 */
107 /* NT additional fields */
110 uint32_t SectionAlignment; /* 0x20 */
111 uint32_t FileAlignment;
112 uint16_t MajorOperatingSystemVersion;
113 uint16_t MinorOperatingSystemVersion;
114 uint16_t MajorImageVersion;
115 uint16_t MinorImageVersion;
116 uint16_t MajorSubsystemVersion; /* 0x30 */
117 uint16_t MinorSubsystemVersion;
118 uint32_t Win32VersionValue;
119 uint32_t SizeOfImage;
120 uint32_t SizeOfHeaders;
121 uint32_t CheckSum; /* 0x40 */
123 uint16_t DllCharacteristics;
124 uint32_t SizeOfStackReserve;
125 uint32_t SizeOfStackCommit;
126 uint32_t SizeOfHeapReserve; /* 0x50 */
127 uint32_t SizeOfHeapCommit;
128 uint32_t LoaderFlags;
129 uint32_t NumberOfRvaAndSizes;
130 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; /* 0x60 */
132 } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
134 typedef struct _IMAGE_NT_HEADERS {
135 uint32_t Signature; /* "PE"\0\0 */ /* 0x00 */
136 IMAGE_FILE_HEADER FileHeader; /* 0x04 */
137 IMAGE_OPTIONAL_HEADER32 OptionalHeader; /* 0x18 */
138 } IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
140 #define IMAGE_SIZEOF_SHORT_NAME 8
142 typedef struct _IMAGE_SECTION_HEADER {
143 uint8_t Name[IMAGE_SIZEOF_SHORT_NAME];
145 uint32_t PhysicalAddress;
146 uint32_t VirtualSize;
148 uint32_t VirtualAddress;
149 uint32_t SizeOfRawData;
150 uint32_t PointerToRawData;
151 uint32_t PointerToRelocations;
152 uint32_t PointerToLinenumbers;
153 uint16_t NumberOfRelocations;
154 uint16_t NumberOfLinenumbers;
155 uint32_t Characteristics;
156 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
158 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5
160 typedef struct _IMAGE_BASE_RELOCATION
162 uint32_t VirtualAddress;
163 uint32_t SizeOfBlock;
164 /* WORD TypeOffset[1]; */
165 } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION;
167 typedef struct _IMAGE_RELOCATION
170 uint32_t VirtualAddress;
173 uint32_t SymbolTableIndex;
175 } IMAGE_RELOCATION, *PIMAGE_RELOCATION;
177 #define IMAGE_SIZEOF_RELOCATION 10
179 /* generic relocation types */
180 #define IMAGE_REL_BASED_ABSOLUTE 0
181 #define IMAGE_REL_BASED_HIGH 1
182 #define IMAGE_REL_BASED_LOW 2
183 #define IMAGE_REL_BASED_HIGHLOW 3
184 #define IMAGE_REL_BASED_HIGHADJ 4
185 #define IMAGE_REL_BASED_MIPS_JMPADDR 5
186 #define IMAGE_REL_BASED_ARM_MOV32A 5 /* yes, 5 too */
187 #define IMAGE_REL_BASED_ARM_MOV32 5 /* yes, 5 too */
188 #define IMAGE_REL_BASED_RISCV_HI20 5 /* yes, 5 too */
189 #define IMAGE_REL_BASED_SECTION 6
190 #define IMAGE_REL_BASED_REL 7
191 #define IMAGE_REL_BASED_ARM_MOV32T 7 /* yes, 7 too */
192 #define IMAGE_REL_BASED_THUMB_MOV32 7 /* yes, 7 too */
193 #define IMAGE_REL_BASED_RISCV_LOW12I 7 /* yes, 7 too */
194 #define IMAGE_REL_BASED_RISCV_LOW12S 8
195 #define IMAGE_REL_BASED_MIPS_JMPADDR16 9
196 #define IMAGE_REL_BASED_IA64_IMM64 9 /* yes, 9 too */
197 #define IMAGE_REL_BASED_DIR64 10
198 #define IMAGE_REL_BASED_HIGH3ADJ 11
200 /* ARM relocation types */
201 #define IMAGE_REL_ARM_ABSOLUTE 0x0000
202 #define IMAGE_REL_ARM_ADDR 0x0001
203 #define IMAGE_REL_ARM_ADDR32NB 0x0002
204 #define IMAGE_REL_ARM_BRANCH24 0x0003
205 #define IMAGE_REL_ARM_BRANCH11 0x0004
206 #define IMAGE_REL_ARM_TOKEN 0x0005
207 #define IMAGE_REL_ARM_GPREL12 0x0006
208 #define IMAGE_REL_ARM_GPREL7 0x0007
209 #define IMAGE_REL_ARM_BLX24 0x0008
210 #define IMAGE_REL_ARM_BLX11 0x0009
211 #define IMAGE_REL_ARM_SECTION 0x000E
212 #define IMAGE_REL_ARM_SECREL 0x000F
213 #define IMAGE_REL_ARM_MOV32A 0x0010
214 #define IMAGE_REL_ARM_MOV32T 0x0011
215 #define IMAGE_REL_ARM_BRANCH20T 0x0012
216 #define IMAGE_REL_ARM_BRANCH24T 0x0014
217 #define IMAGE_REL_ARM_BLX23T 0x0015
219 /* ARM64 relocation types */
220 #define IMAGE_REL_ARM64_ABSOLUTE 0x0000
221 #define IMAGE_REL_ARM64_ADDR32 0x0001
222 #define IMAGE_REL_ARM64_ADDR32NB 0x0002
223 #define IMAGE_REL_ARM64_BRANCH26 0x0003
224 #define IMAGE_REL_ARM64_PAGEBASE_REL21 0x0004
225 #define IMAGE_REL_ARM64_REL21 0x0005
226 #define IMAGE_REL_ARM64_PAGEOFFSET_12A 0x0006
227 #define IMAGE_REL_ARM64_PAGEOFFSET_12L 0x0007
228 #define IMAGE_REL_ARM64_SECREL 0x0008
229 #define IMAGE_REL_ARM64_SECREL_LOW12A 0x0009
230 #define IMAGE_REL_ARM64_SECREL_HIGH12A 0x000A
231 #define IMAGE_REL_ARM64_SECREL_LOW12L 0x000B
232 #define IMAGE_REL_ARM64_TOKEN 0x000C
233 #define IMAGE_REL_ARM64_SECTION 0x000D
234 #define IMAGE_REL_ARM64_ADDR64 0x000E
236 /* AMD64 relocation types */
237 #define IMAGE_REL_AMD64_ABSOLUTE 0x0000
238 #define IMAGE_REL_AMD64_ADDR64 0x0001
239 #define IMAGE_REL_AMD64_ADDR32 0x0002
240 #define IMAGE_REL_AMD64_ADDR32NB 0x0003
241 #define IMAGE_REL_AMD64_REL32 0x0004
242 #define IMAGE_REL_AMD64_REL32_1 0x0005
243 #define IMAGE_REL_AMD64_REL32_2 0x0006
244 #define IMAGE_REL_AMD64_REL32_3 0x0007
245 #define IMAGE_REL_AMD64_REL32_4 0x0008
246 #define IMAGE_REL_AMD64_REL32_5 0x0009
247 #define IMAGE_REL_AMD64_SECTION 0x000A
248 #define IMAGE_REL_AMD64_SECREL 0x000B
249 #define IMAGE_REL_AMD64_SECREL7 0x000C
250 #define IMAGE_REL_AMD64_TOKEN 0x000D
251 #define IMAGE_REL_AMD64_SREL32 0x000E
252 #define IMAGE_REL_AMD64_PAIR 0x000F
253 #define IMAGE_REL_AMD64_SSPAN32 0x0010