• 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 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 #define IMAGE_DIRECTORY_ENTRY_BASERELOC         5
159 
160 typedef struct _IMAGE_BASE_RELOCATION
161 {
162         uint32_t VirtualAddress;
163         uint32_t SizeOfBlock;
164         /* WORD TypeOffset[1]; */
165 } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION;
166 
167 typedef struct _IMAGE_RELOCATION
168 {
169 	union {
170 		uint32_t VirtualAddress;
171 		uint32_t RelocCount;
172 	} DUMMYUNIONNAME;
173 	uint32_t SymbolTableIndex;
174 	uint16_t Type;
175 } IMAGE_RELOCATION, *PIMAGE_RELOCATION;
176 
177 #define IMAGE_SIZEOF_RELOCATION 10
178 
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
199 
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
218 
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
235 
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
254 
255 #endif /* _PE_H */
256