• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  Portable Executable binary format structures
4  *
5  *  Copyright (c) 2016 Alexander Graf
6  *
7  *  Based on wine code
8  */
9 
10 #ifndef _PE_H
11 #define _PE_H
12 
13 #include <asm-generic/pe.h>
14 
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;
36 
37 #define IMAGE_DOS_SIGNATURE		0x5A4D     /* MZ   */
38 #define IMAGE_NT_SIGNATURE		0x00004550 /* PE00 */
39 
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
48 
49 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC	0x10b
50 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC	0x20b
51 #define IMAGE_SUBSYSTEM_EFI_APPLICATION	10
52 
53 typedef struct _IMAGE_FILE_HEADER {
54 	uint16_t Machine;
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;
62 
63 typedef struct _IMAGE_DATA_DIRECTORY {
64 	uint32_t VirtualAddress;
65 	uint32_t Size;
66 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
67 
68 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
69 
70 typedef struct _IMAGE_OPTIONAL_HEADER64 {
71 	uint16_t Magic; /* 0x20b */
72 	uint8_t  MajorLinkerVersion;
73 	uint8_t  MinorLinkerVersion;
74 	uint32_t SizeOfCode;
75 	uint32_t SizeOfInitializedData;
76 	uint32_t SizeOfUninitializedData;
77 	uint32_t AddressOfEntryPoint;
78 	uint32_t BaseOfCode;
79 	uint64_t ImageBase;
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;
89 	uint32_t SizeOfImage;
90 	uint32_t SizeOfHeaders;
91 	uint32_t CheckSum;
92 	uint16_t Subsystem;
93 	uint16_t DllCharacteristics;
94 	uint64_t SizeOfStackReserve;
95 	uint64_t SizeOfStackCommit;
96 	uint64_t SizeOfHeapReserve;
97 	uint64_t SizeOfHeapCommit;
98 	uint32_t LoaderFlags;
99 	uint32_t NumberOfRvaAndSizes;
100 	IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
101 } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
102 
103 typedef struct _IMAGE_NT_HEADERS64 {
104 	uint32_t Signature;
105 	IMAGE_FILE_HEADER FileHeader;
106 	IMAGE_OPTIONAL_HEADER64 OptionalHeader;
107 } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
108 
109 typedef struct _IMAGE_OPTIONAL_HEADER {
110 
111 	/* Standard fields */
112 
113 	uint16_t Magic; /* 0x10b or 0x107 */     /* 0x00 */
114 	uint8_t  MajorLinkerVersion;
115 	uint8_t  MinorLinkerVersion;
116 	uint32_t SizeOfCode;
117 	uint32_t SizeOfInitializedData;
118 	uint32_t SizeOfUninitializedData;
119 	uint32_t AddressOfEntryPoint;            /* 0x10 */
120 	uint32_t BaseOfCode;
121 	uint32_t BaseOfData;
122 
123 	/* NT additional fields */
124 
125 	uint32_t ImageBase;
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 */
138 	uint16_t Subsystem;
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 */
147 	/* 0xE0 */
148 } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
149 
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;
155 
156 #define IMAGE_SIZEOF_SHORT_NAME 8
157 
158 typedef struct _IMAGE_SECTION_HEADER {
159 	uint8_t	Name[IMAGE_SIZEOF_SHORT_NAME];
160 	union {
161 		uint32_t PhysicalAddress;
162 		uint32_t VirtualSize;
163 	} Misc;
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;
173 
174 #define IMAGE_DIRECTORY_ENTRY_BASERELOC         5
175 
176 typedef struct _IMAGE_BASE_RELOCATION
177 {
178         uint32_t VirtualAddress;
179         uint32_t SizeOfBlock;
180         /* WORD TypeOffset[1]; */
181 } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION;
182 
183 typedef struct _IMAGE_RELOCATION
184 {
185 	union {
186 		uint32_t VirtualAddress;
187 		uint32_t RelocCount;
188 	} DUMMYUNIONNAME;
189 	uint32_t SymbolTableIndex;
190 	uint16_t Type;
191 } IMAGE_RELOCATION, *PIMAGE_RELOCATION;
192 
193 #define IMAGE_SIZEOF_RELOCATION 10
194 
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
215 
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
234 
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
251 
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
270 
271 #endif /* _PE_H */
272