xref: /freebsd/contrib/elftoolchain/libpe/pe.h (revision 1ae6faf1645516bbb97eaf2a43d867b59b22309b)
1 /*-
2  * Copyright (c) 2015 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $Id: pe.h 3441 2016-04-07 15:04:20Z emaste $
27  */
28 
29 #ifndef	_PE_H_
30 #define	_PE_H_
31 
32 #include <stdint.h>
33 
34 /*
35  * MS-DOS header.
36  */
37 
38 typedef struct _PE_DosHdr {
39 	char dh_magic[2];
40 	uint16_t dh_lastsize;
41 	uint16_t dh_nblock;
42 	uint16_t dh_nreloc;
43 	uint16_t dh_hdrsize;
44 	uint16_t dh_minalloc;
45 	uint16_t dh_maxalloc;
46 	uint16_t dh_ss;
47 	uint16_t dh_sp;
48 	uint16_t dh_checksum;
49 	uint16_t dh_ip;
50 	uint16_t dh_cs;
51 	uint16_t dh_relocpos;
52 	uint16_t dh_noverlay;
53 	uint16_t dh_reserved1[4];
54 	uint16_t dh_oemid;
55 	uint16_t dh_oeminfo;
56 	uint16_t dh_reserved2[10];
57 	uint32_t dh_lfanew;
58 } PE_DosHdr;
59 
60 /*
61  * Rich header.
62  */
63 
64 typedef struct _PE_RichHdr {
65 	uint32_t rh_xor;
66 	uint32_t rh_total;
67 	uint32_t *rh_compid;
68 	uint32_t *rh_cnt;
69 } PE_RichHdr;
70 
71 /*
72  * COFF header: Machine Types.
73  */
74 
75 #define	IMAGE_FILE_MACHINE_UNKNOWN	0x0	/* not specified */
76 #define	IMAGE_FILE_MACHINE_AM33		0x1d3	/* Matsushita AM33 */
77 #define	IMAGE_FILE_MACHINE_AMD64	0x8664	/* x86-64 */
78 #define	IMAGE_FILE_MACHINE_ARM		0x1c0	/* ARM LE */
79 #define	IMAGE_FILE_MACHINE_ARMNT	0x1c4	/* ARMv7(or higher) Thumb */
80 #define	IMAGE_FILE_MACHINE_ARM64	0xaa64	/* ARMv8 64-bit */
81 #define	IMAGE_FILE_MACHINE_EBC		0xebc	/* EFI byte code */
82 #define	IMAGE_FILE_MACHINE_I386		0x14c	/* x86 */
83 #define	IMAGE_FILE_MACHINE_IA64		0x200	/* IA64 */
84 #define IMAGE_FILE_MACHINE_LOONGARCH32  0x6232  /* LoongArch 32-bit */
85 #define IMAGE_FILE_MACHINE_LOONGARCH64  0x6264  /* LoongArch 64-bit */
86 #define	IMAGE_FILE_MACHINE_M32R		0x9041	/* Mitsubishi M32R LE */
87 #define	IMAGE_FILE_MACHINE_MIPS16	0x266	/* MIPS16 */
88 #define	IMAGE_FILE_MACHINE_MIPSFPU	0x366	/* MIPS with FPU */
89 #define	IMAGE_FILE_MACHINE_MIPSFPU16	0x466	/* MIPS16 with FPU */
90 #define	IMAGE_FILE_MACHINE_POWERPC	0x1f0	/* Power PC LE */
91 #define	IMAGE_FILE_MACHINE_POWERPCFP	0x1f1	/* Power PC floating point */
92 #define	IMAGE_FILE_MACHINE_R4000	0x166	/* MIPS R4000 LE */
93 #define	IMAGE_FILE_MACHINE_RISCV32	0x5032	/* RISC-V 32-bit */
94 #define	IMAGE_FILE_MACHINE_RISCV64	0x5064	/* RISC-V 64-bit */
95 #define	IMAGE_FILE_MACHINE_RISCV128	0x5128	/* RISC-V 128-bit */
96 #define	IMAGE_FILE_MACHINE_SH3		0x1a2	/* Hitachi SH3 */
97 #define	IMAGE_FILE_MACHINE_SH3DSP	0x1a3	/* Hitachi SH3 DSP */
98 #define	IMAGE_FILE_MACHINE_SH4		0x1a6	/* Hitachi SH4 */
99 #define	IMAGE_FILE_MACHINE_SH5		0x1a8	/* Hitachi SH5 */
100 #define	IMAGE_FILE_MACHINE_THUMB	0x1c2	/* ARM or Thumb interworking */
101 #define	IMAGE_FILE_MACHINE_WCEMIPSV2	0x169	/* MIPS LE WCE v2 */
102 
103 /*
104  * COFF header: Characteristics
105  */
106 
107 #define	IMAGE_FILE_RELOCS_STRIPPED		0x0001
108 #define	IMAGE_FILE_EXECUTABLE_IMAGE		0x0002
109 #define	IMAGE_FILE_LINE_NUMS_STRIPPED		0x0004
110 #define	IMAGE_FILE_LOCAL_SYMS_STRIPPED		0x0008
111 #define	IMAGE_FILE_AGGRESSIVE_WS_TRIM		0x0010
112 #define	IMAGE_FILE_LARGE_ADDRESS_AWARE		0x0020
113 #define	IMAGE_FILE_BYTES_REVERSED_LO		0x0080
114 #define	IMAGE_FILE_32BIT_MACHINE		0x0100
115 #define	IMAGE_FILE_DEBUG_STRIPPED		0x0200
116 #define	IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP	0x0400
117 #define	IMAGE_FILE_NET_RUN_FROM_SWAP		0x0800
118 #define	IMAGE_FILE_SYSTEM			0x1000
119 #define	IMAGE_FILE_DLL				0x2000
120 #define	IMAGE_FILE_UP_SYSTEM_ONLY		0x4000
121 #define	IMAGE_FILE_BYTES_REVERSED_HI		0x8000
122 
123 /*
124  * COFF Header.
125  */
126 
127 typedef struct _PE_CoffHdr {
128 	uint16_t ch_machine;
129 	uint16_t ch_nsec;
130 	uint32_t ch_timestamp;
131 	uint32_t ch_symptr;
132 	uint32_t ch_nsym;
133 	uint16_t ch_optsize;
134 	uint16_t ch_char;
135 } PE_CoffHdr;
136 
137 
138 /*
139  * Optional Header: Subsystem.
140  */
141 
142 #define	IMAGE_SUBSYSTEM_UNKNOWN			0
143 #define	IMAGE_SUBSYSTEM_NATIVE			1
144 #define	IMAGE_SUBSYSTEM_WINDOWS_GUI		2
145 #define	IMAGE_SUBSYSTEM_WINDOWS_CUI		3
146 #define	IMAGE_SUBSYSTEM_POSIX_CUI		7
147 #define	IMAGE_SUBSYSTEM_WINDOWS_CE_GUI		9
148 #define	IMAGE_SUBSYSTEM_EFI_APPLICATION		10
149 #define	IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER	11
150 #define	IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER	12
151 #define	IMAGE_SUBSYSTEM_EFI_ROM			13
152 #define	IMAGE_SUBSYSTEM_XBOX			14
153 
154 /*
155  * Optional Header: DLL Characteristics
156  */
157 
158 #define	IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE		0x0040
159 #define	IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY	0x0080
160 #define	IMAGE_DLL_CHARACTERISTICS_NX_COMPAT		0x0100
161 #define	IMAGE_DLL_CHARACTERISTICS_NO_ISOLATION		0x0200
162 #define	IMAGE_DLL_CHARACTERISTICS_NO_SEH		0x0400
163 #define	IMAGE_DLL_CHARACTERISTICS_NO_BIND		0x0800
164 #define	IMAGE_DLL_CHARACTERISTICS_WDM_DRIVER		0x2000
165 #define	IMAGE_DLL_CHARACTERISTICS_TERMINAL_SERVER_AWARE	0x8000
166 
167 /*
168  * Optional Header.
169  */
170 
171 #define	PE_FORMAT_ROM		0x107
172 #define	PE_FORMAT_32		0x10b
173 #define	PE_FORMAT_32P		0x20b
174 
175 typedef struct _PE_OptHdr {
176 	uint16_t oh_magic;
177 	uint8_t oh_ldvermajor;
178 	uint8_t oh_ldverminor;
179 	uint32_t oh_textsize;
180 	uint32_t oh_datasize;
181 	uint32_t oh_bsssize;
182 	uint32_t oh_entry;
183 	uint32_t oh_textbase;
184 	uint32_t oh_database;
185 	uint64_t oh_imgbase;
186 	uint32_t oh_secalign;
187 	uint32_t oh_filealign;
188 	uint16_t oh_osvermajor;
189 	uint16_t oh_osverminor;
190 	uint16_t oh_imgvermajor;
191 	uint16_t oh_imgverminor;
192 	uint16_t oh_subvermajor;
193 	uint16_t oh_subverminor;
194 	uint32_t oh_win32ver;
195 	uint32_t oh_imgsize;
196 	uint32_t oh_hdrsize;
197 	uint32_t oh_checksum;
198 	uint16_t oh_subsystem;
199 	uint16_t oh_dllchar;
200 	uint64_t oh_stacksizer;
201 	uint64_t oh_stacksizec;
202 	uint64_t oh_heapsizer;
203 	uint64_t oh_heapsizec;
204 	uint32_t oh_ldrflags;
205 	uint32_t oh_ndatadir;
206 } PE_OptHdr;
207 
208 /*
209  * Optional Header: Data Directories.
210  */
211 
212 #define	PE_DD_EXPORT		0
213 #define	PE_DD_IMPORT		1
214 #define	PE_DD_RESROUCE		2
215 #define	PE_DD_EXCEPTION		3
216 #define	PE_DD_CERTIFICATE	4
217 #define	PE_DD_BASERELOC		5
218 #define	PE_DD_DEBUG		6
219 #define	PE_DD_ARCH		7
220 #define	PE_DD_GLOBALPTR		8
221 #define	PE_DD_TLS		9
222 #define	PE_DD_LOADCONFIG	10
223 #define	PE_DD_BOUNDIMPORT	11
224 #define	PE_DD_IAT		12
225 #define	PE_DD_DELAYIMPORT	13
226 #define	PE_DD_CLRRUNTIME	14
227 #define	PE_DD_RESERVED		15
228 #define	PE_DD_MAX		16
229 
230 typedef struct _PE_DataDirEntry {
231 	uint32_t de_addr;
232 	uint32_t de_size;
233 } PE_DataDirEntry;
234 
235 typedef struct _PE_DataDir {
236 	PE_DataDirEntry dd_e[PE_DD_MAX];
237 	uint32_t dd_total;
238 } PE_DataDir;
239 
240 /*
241  * Section Headers: Section flags.
242  */
243 
244 #define	IMAGE_SCN_TYPE_NO_PAD			0x00000008
245 #define	IMAGE_SCN_CNT_CODE			0x00000020
246 #define	IMAGE_SCN_CNT_INITIALIZED_DATA		0x00000040
247 #define	IMAGE_SCN_CNT_UNINITIALIZED_DATA	0x00000080
248 #define	IMAGE_SCN_LNK_OTHER			0x00000100
249 #define	IMAGE_SCN_LNK_INFO			0x00000200
250 #define	IMAGE_SCN_LNK_REMOVE			0x00000800
251 #define	IMAGE_SCN_LNK_COMDAT			0x00001000
252 #define	IMAGE_SCN_GPREL				0x00008000
253 #define	IMAGE_SCN_MEM_PURGEABLE			0x00020000
254 #define	IMAGE_SCN_MEM_16BIT			0x00020000
255 #define	IMAGE_SCN_MEM_LOCKED			0x00040000
256 #define	IMAGE_SCN_MEM_PRELOAD			0x00080000
257 #define	IMAGE_SCN_ALIGN_1BYTES			0x00100000
258 #define	IMAGE_SCN_ALIGN_2BYTES			0x00200000
259 #define	IMAGE_SCN_ALIGN_4BYTES			0x00300000
260 #define	IMAGE_SCN_ALIGN_8BYTES			0x00400000
261 #define	IMAGE_SCN_ALIGN_16BYTES			0x00500000
262 #define	IMAGE_SCN_ALIGN_32BYTES			0x00600000
263 #define	IMAGE_SCN_ALIGN_64BYTES			0x00700000
264 #define	IMAGE_SCN_ALIGN_128BYTES		0x00800000
265 #define	IMAGE_SCN_ALIGN_256BYTES		0x00900000
266 #define	IMAGE_SCN_ALIGN_512BYTES		0x00A00000
267 #define	IMAGE_SCN_ALIGN_1024BYTES		0x00B00000
268 #define	IMAGE_SCN_ALIGN_2048BYTES		0x00C00000
269 #define	IMAGE_SCN_ALIGN_4096BYTES		0x00D00000
270 #define	IMAGE_SCN_ALIGN_8192BYTES		0x00E00000
271 #define	IMAGE_SCN_LNK_NRELOC_OVFL		0x01000000
272 #define	IMAGE_SCN_MEM_DISCARDABLE		0x02000000
273 #define	IMAGE_SCN_MEM_NOT_CACHED		0x04000000
274 #define	IMAGE_SCN_MEM_NOT_PAGED			0x08000000
275 #define	IMAGE_SCN_MEM_SHARED			0x10000000
276 #define	IMAGE_SCN_MEM_EXECUTE			0x20000000
277 #define	IMAGE_SCN_MEM_READ			0x40000000
278 #define	IMAGE_SCN_MEM_WRITE			0x80000000
279 
280 /*
281  * Section Headers.
282  */
283 
284 typedef struct _PE_SecHdr {
285 	char sh_name[8];
286 	uint32_t sh_virtsize;
287 	uint32_t sh_addr;
288 	uint32_t sh_rawsize;
289 	uint32_t sh_rawptr;
290 	uint32_t sh_relocptr;
291 	uint32_t sh_lineptr;
292 	uint16_t sh_nreloc;
293 	uint16_t sh_nline;
294 	uint32_t sh_char;
295 } PE_SecHdr;
296 
297 #endif	/* !_PE_H_ */
298