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 typedef struct _IMAGE_FILE_HEADER {
38 	uint16_t Machine;
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;
46 
47 typedef struct _IMAGE_DATA_DIRECTORY {
48 	uint32_t VirtualAddress;
49 	uint32_t Size;
50 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
51 
52 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
53 
54 typedef struct _IMAGE_OPTIONAL_HEADER64 {
55 	uint16_t Magic; /* 0x20b */
56 	uint8_t  MajorLinkerVersion;
57 	uint8_t  MinorLinkerVersion;
58 	uint32_t SizeOfCode;
59 	uint32_t SizeOfInitializedData;
60 	uint32_t SizeOfUninitializedData;
61 	uint32_t AddressOfEntryPoint;
62 	uint32_t BaseOfCode;
63 	uint64_t ImageBase;
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;
73 	uint32_t SizeOfImage;
74 	uint32_t SizeOfHeaders;
75 	uint32_t CheckSum;
76 	uint16_t Subsystem;
77 	uint16_t DllCharacteristics;
78 	uint64_t SizeOfStackReserve;
79 	uint64_t SizeOfStackCommit;
80 	uint64_t SizeOfHeapReserve;
81 	uint64_t SizeOfHeapCommit;
82 	uint32_t LoaderFlags;
83 	uint32_t NumberOfRvaAndSizes;
84 	IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
85 } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
86 
87 typedef struct _IMAGE_NT_HEADERS64 {
88 	uint32_t Signature;
89 	IMAGE_FILE_HEADER FileHeader;
90 	IMAGE_OPTIONAL_HEADER64 OptionalHeader;
91 } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
92 
93 typedef struct _IMAGE_OPTIONAL_HEADER {
94 
95 	/* Standard fields */
96 
97 	uint16_t Magic; /* 0x10b or 0x107 */     /* 0x00 */
98 	uint8_t  MajorLinkerVersion;
99 	uint8_t  MinorLinkerVersion;
100 	uint32_t SizeOfCode;
101 	uint32_t SizeOfInitializedData;
102 	uint32_t SizeOfUninitializedData;
103 	uint32_t AddressOfEntryPoint;            /* 0x10 */
104 	uint32_t BaseOfCode;
105 	uint32_t BaseOfData;
106 
107 	/* NT additional fields */
108 
109 	uint32_t ImageBase;
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 */
122 	uint16_t Subsystem;
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 */
131 	/* 0xE0 */
132 } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
133 
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;
139 
140 #define IMAGE_SIZEOF_SHORT_NAME 8
141 
142 typedef struct _IMAGE_SECTION_HEADER {
143 	uint8_t	Name[IMAGE_SIZEOF_SHORT_NAME];
144 	union {
145 		uint32_t PhysicalAddress;
146 		uint32_t VirtualSize;
147 	} Misc;
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;
157 
158 /* Indices for Optional Header Data Directories */
159 #define IMAGE_DIRECTORY_ENTRY_SECURITY		4
160 #define IMAGE_DIRECTORY_ENTRY_BASERELOC         5
161 
162 typedef struct _IMAGE_BASE_RELOCATION
163 {
164         uint32_t VirtualAddress;
165         uint32_t SizeOfBlock;
166         /* WORD TypeOffset[1]; */
167 } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION;
168 
169 typedef struct _IMAGE_RELOCATION
170 {
171 	union {
172 		uint32_t VirtualAddress;
173 		uint32_t RelocCount;
174 	} DUMMYUNIONNAME;
175 	uint32_t SymbolTableIndex;
176 	uint16_t Type;
177 } IMAGE_RELOCATION, *PIMAGE_RELOCATION;
178 
179 #define IMAGE_SIZEOF_RELOCATION 10
180 
181 /* generic relocation types */
182 #define IMAGE_REL_BASED_ABSOLUTE                0
183 #define IMAGE_REL_BASED_HIGH                    1
184 #define IMAGE_REL_BASED_LOW                     2
185 #define IMAGE_REL_BASED_HIGHLOW                 3
186 #define IMAGE_REL_BASED_HIGHADJ                 4
187 #define IMAGE_REL_BASED_MIPS_JMPADDR            5
188 #define IMAGE_REL_BASED_ARM_MOV32A              5 /* yes, 5 too */
189 #define IMAGE_REL_BASED_ARM_MOV32               5 /* yes, 5 too */
190 #define IMAGE_REL_BASED_RISCV_HI20		5 /* yes, 5 too */
191 #define IMAGE_REL_BASED_SECTION                 6
192 #define IMAGE_REL_BASED_REL                     7
193 #define IMAGE_REL_BASED_ARM_MOV32T              7 /* yes, 7 too */
194 #define IMAGE_REL_BASED_THUMB_MOV32             7 /* yes, 7 too */
195 #define IMAGE_REL_BASED_RISCV_LOW12I		7 /* yes, 7 too */
196 #define IMAGE_REL_BASED_RISCV_LOW12S		8
197 #define IMAGE_REL_BASED_MIPS_JMPADDR16          9
198 #define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
199 #define IMAGE_REL_BASED_DIR64                   10
200 #define IMAGE_REL_BASED_HIGH3ADJ                11
201 
202 /* ARM relocation types */
203 #define IMAGE_REL_ARM_ABSOLUTE          0x0000
204 #define IMAGE_REL_ARM_ADDR              0x0001
205 #define IMAGE_REL_ARM_ADDR32NB          0x0002
206 #define IMAGE_REL_ARM_BRANCH24          0x0003
207 #define IMAGE_REL_ARM_BRANCH11          0x0004
208 #define IMAGE_REL_ARM_TOKEN             0x0005
209 #define IMAGE_REL_ARM_GPREL12           0x0006
210 #define IMAGE_REL_ARM_GPREL7            0x0007
211 #define IMAGE_REL_ARM_BLX24             0x0008
212 #define IMAGE_REL_ARM_BLX11             0x0009
213 #define IMAGE_REL_ARM_SECTION           0x000E
214 #define IMAGE_REL_ARM_SECREL            0x000F
215 #define IMAGE_REL_ARM_MOV32A            0x0010
216 #define IMAGE_REL_ARM_MOV32T            0x0011
217 #define IMAGE_REL_ARM_BRANCH20T         0x0012
218 #define IMAGE_REL_ARM_BRANCH24T         0x0014
219 #define IMAGE_REL_ARM_BLX23T            0x0015
220 
221 /* ARM64 relocation types */
222 #define IMAGE_REL_ARM64_ABSOLUTE        0x0000
223 #define IMAGE_REL_ARM64_ADDR32          0x0001
224 #define IMAGE_REL_ARM64_ADDR32NB        0x0002
225 #define IMAGE_REL_ARM64_BRANCH26        0x0003
226 #define IMAGE_REL_ARM64_PAGEBASE_REL21  0x0004
227 #define IMAGE_REL_ARM64_REL21           0x0005
228 #define IMAGE_REL_ARM64_PAGEOFFSET_12A  0x0006
229 #define IMAGE_REL_ARM64_PAGEOFFSET_12L  0x0007
230 #define IMAGE_REL_ARM64_SECREL          0x0008
231 #define IMAGE_REL_ARM64_SECREL_LOW12A   0x0009
232 #define IMAGE_REL_ARM64_SECREL_HIGH12A  0x000A
233 #define IMAGE_REL_ARM64_SECREL_LOW12L   0x000B
234 #define IMAGE_REL_ARM64_TOKEN           0x000C
235 #define IMAGE_REL_ARM64_SECTION         0x000D
236 #define IMAGE_REL_ARM64_ADDR64          0x000E
237 
238 /* AMD64 relocation types */
239 #define IMAGE_REL_AMD64_ABSOLUTE        0x0000
240 #define IMAGE_REL_AMD64_ADDR64          0x0001
241 #define IMAGE_REL_AMD64_ADDR32          0x0002
242 #define IMAGE_REL_AMD64_ADDR32NB        0x0003
243 #define IMAGE_REL_AMD64_REL32           0x0004
244 #define IMAGE_REL_AMD64_REL32_1         0x0005
245 #define IMAGE_REL_AMD64_REL32_2         0x0006
246 #define IMAGE_REL_AMD64_REL32_3         0x0007
247 #define IMAGE_REL_AMD64_REL32_4         0x0008
248 #define IMAGE_REL_AMD64_REL32_5         0x0009
249 #define IMAGE_REL_AMD64_SECTION         0x000A
250 #define IMAGE_REL_AMD64_SECREL          0x000B
251 #define IMAGE_REL_AMD64_SECREL7         0x000C
252 #define IMAGE_REL_AMD64_TOKEN           0x000D
253 #define IMAGE_REL_AMD64_SREL32          0x000E
254 #define IMAGE_REL_AMD64_PAIR            0x000F
255 #define IMAGE_REL_AMD64_SSPAN32         0x0010
256 
257 /* certificate appended to PE image */
258 typedef struct _WIN_CERTIFICATE {
259 	uint32_t dwLength;
260 	uint16_t wRevision;
261 	uint16_t wCertificateType;
262 	uint8_t bCertificate[];
263 } WIN_CERTIFICATE, *LPWIN_CERTIFICATE;
264 
265 /* Definitions for the contents of the certs data block */
266 #define WIN_CERT_TYPE_PKCS_SIGNED_DATA	0x0002
267 #define WIN_CERT_TYPE_EFI_OKCS115	0x0EF0
268 #define WIN_CERT_TYPE_EFI_GUID		0x0EF1
269 
270 #define WIN_CERT_REVISION_1_0		0x0100
271 #define WIN_CERT_REVISION_2_0		0x0200
272 
273 #endif /* _PE_H */
274