VirtualBox

source: kStuff/trunk/include/k/kLdrFmts/pe.h@ 31

Last change on this file since 31 was 31, checked in by bird, 16 years ago

4 missing license headers and some missing svn properties.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.6 KB
Line 
1/* $Id: pe.h 31 2009-07-01 21:08:06Z bird $ */
2/** @file
3 * PE structures, types and defines.
4 */
5
6/*
7 * Copyright (c) 2006-2007 Knut St. Osmundsen <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31#ifndef ___k_kLdrFmts_pe_h___
32#define ___k_kLdrFmts_pe_h___
33
34
35/*******************************************************************************
36* Header Files *
37*******************************************************************************/
38#include <k/kTypes.h>
39#include <k/kDefs.h>
40
41
42/*******************************************************************************
43* Defined Constants And Macros *
44*******************************************************************************/
45#ifndef IMAGE_NT_SIGNATURE
46# define IMAGE_NT_SIGNATURE K_LE2H_U32('P' | ('E' << 8))
47#endif
48
49/* file header */
50#define IMAGE_FILE_MACHINE_I386 0x014c
51#define IMAGE_FILE_MACHINE_AMD64 0x8664
52
53#define IMAGE_FILE_RELOCS_STRIPPED 0x0001
54#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
55#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004
56#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008
57#define IMAGE_FILE_AGGRESIVE_WS_TRIM 0x0010
58#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020
59#define IMAGE_FILE_16BIT_MACHINE 0x0040
60#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
61#define IMAGE_FILE_32BIT_MACHINE 0x0100
62#define IMAGE_FILE_DEBUG_STRIPPED 0x0200
63#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400
64#define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800
65#define IMAGE_FILE_SYSTEM 0x1000
66#define IMAGE_FILE_DLL 0x2000
67#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000
68#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
69
70
71/* optional header */
72#define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10B
73#define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20B
74
75#define IMAGE_SUBSYSTEM_UNKNOWN 0x0
76#define IMAGE_SUBSYSTEM_NATIVE 0x1
77#define IMAGE_SUBSYSTEM_WINDOWS_GUI 0x2
78#define IMAGE_SUBSYSTEM_WINDOWS_CUI 0x3
79#define IMAGE_SUBSYSTEM_OS2_GUI 0x4
80#define IMAGE_SUBSYSTEM_OS2_CUI 0x5
81#define IMAGE_SUBSYSTEM_POSIX_CUI 0x7
82
83#define IMAGE_LIBRARY_PROCESS_INIT 0x0001
84#define IMAGE_LIBRARY_PROCESS_TERM 0x0002
85#define IMAGE_LIBRARY_THREAD_INIT 0x0004
86#define IMAGE_LIBRARY_THREAD_TERM 0x0008
87#define IMAGE_DLLCHARACTERISTICS_NO_ISOLATION 0x0200
88#define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x0400
89#define IMAGE_DLLCHARACTERISTICS_NO_BIND 0x0800
90#define IMAGE_DLLCHARACTERISTICS_WDM_DRIVER 0x2000
91#define IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE 0x8000
92
93#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 0x10
94
95#define IMAGE_DIRECTORY_ENTRY_EXPORT 0x0
96#define IMAGE_DIRECTORY_ENTRY_IMPORT 0x1
97#define IMAGE_DIRECTORY_ENTRY_RESOURCE 0x2
98#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 0x3
99#define IMAGE_DIRECTORY_ENTRY_SECURITY 0x4
100#define IMAGE_DIRECTORY_ENTRY_BASERELOC 0x5
101#define IMAGE_DIRECTORY_ENTRY_DEBUG 0x6
102#define IMAGE_DIRECTORY_ENTRY_ARCHITECTURE 0x7
103#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT IMAGE_DIRECTORY_ENTRY_ARCHITECTURE
104#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 0x8
105#define IMAGE_DIRECTORY_ENTRY_TLS 0x9
106#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 0xa
107#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 0xb
108#define IMAGE_DIRECTORY_ENTRY_IAT 0xc
109#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 0xd
110#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 0xe
111
112
113/* section header */
114#define IMAGE_SIZEOF_SHORT_NAME 0x8
115
116#define IMAGE_SCN_TYPE_REG 0x00000000
117#define IMAGE_SCN_TYPE_DSECT 0x00000001
118#define IMAGE_SCN_TYPE_NOLOAD 0x00000002
119#define IMAGE_SCN_TYPE_GROUP 0x00000004
120#define IMAGE_SCN_TYPE_NO_PAD 0x00000008
121#define IMAGE_SCN_TYPE_COPY 0x00000010
122
123#define IMAGE_SCN_CNT_CODE 0x00000020
124#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
125#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
126
127#define IMAGE_SCN_LNK_OTHER 0x00000100
128#define IMAGE_SCN_LNK_INFO 0x00000200
129#define IMAGE_SCN_TYPE_OVER 0x00000400
130#define IMAGE_SCN_LNK_REMOVE 0x00000800
131#define IMAGE_SCN_LNK_COMDAT 0x00001000
132#define IMAGE_SCN_MEM_PROTECTED 0x00004000
133#define IMAGE_SCN_NO_DEFER_SPEC_EXC 0x00004000
134#define IMAGE_SCN_GPREL 0x00008000
135#define IMAGE_SCN_MEM_FARDATA 0x00008000
136#define IMAGE_SCN_MEM_SYSHEAP 0x00010000
137#define IMAGE_SCN_MEM_PURGEABLE 0x00020000
138#define IMAGE_SCN_MEM_16BIT 0x00020000
139#define IMAGE_SCN_MEM_LOCKED 0x00040000
140#define IMAGE_SCN_MEM_PRELOAD 0x00080000
141
142#define IMAGE_SCN_ALIGN_1BYTES 0x00100000
143#define IMAGE_SCN_ALIGN_2BYTES 0x00200000
144#define IMAGE_SCN_ALIGN_4BYTES 0x00300000
145#define IMAGE_SCN_ALIGN_8BYTES 0x00400000
146#define IMAGE_SCN_ALIGN_16BYTES 0x00500000
147#define IMAGE_SCN_ALIGN_32BYTES 0x00600000
148#define IMAGE_SCN_ALIGN_64BYTES 0x00700000
149#define IMAGE_SCN_ALIGN_128BYTES 0x00800000
150#define IMAGE_SCN_ALIGN_256BYTES 0x00900000
151#define IMAGE_SCN_ALIGN_512BYTES 0x00A00000
152#define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000
153#define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000
154#define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000
155#define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000
156#define IMAGE_SCN_ALIGN_MASK 0x00F00000
157
158#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
159#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
160#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000
161#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000
162#define IMAGE_SCN_MEM_SHARED 0x10000000
163#define IMAGE_SCN_MEM_EXECUTE 0x20000000
164#define IMAGE_SCN_MEM_READ 0x40000000
165#define IMAGE_SCN_MEM_WRITE 0x80000000
166
167
168/* relocations */
169#define IMAGE_REL_BASED_ABSOLUTE 0x0
170#define IMAGE_REL_BASED_HIGH 0x1
171#define IMAGE_REL_BASED_LOW 0x2
172#define IMAGE_REL_BASED_HIGHLOW 0x3
173#define IMAGE_REL_BASED_HIGHADJ 0x4
174#define IMAGE_REL_BASED_MIPS_JMPADDR 0x5
175#define IMAGE_REL_BASED_SECTION 0x6
176#define IMAGE_REL_BASED_REL32 0x7
177/*#define IMAGE_REL_BASED_RESERVED1 0x8 */
178#define IMAGE_REL_BASED_MIPS_JMPADDR16 0x9
179#define IMAGE_REL_BASED_IA64_IMM64 0x9
180#define IMAGE_REL_BASED_DIR64 0xa
181#define IMAGE_REL_BASED_HIGH3ADJ 0xb
182
183/* imports */
184#define IMAGE_ORDINAL_FLAG32 0x80000000
185#define IMAGE_ORDINAL32(ord) ((ord) & 0xffff)
186#define IMAGE_SNAP_BY_ORDINAL32(ord) (!!((ord) & IMAGE_ORDINAL_FLAG32))
187
188#define IMAGE_ORDINAL_FLAG64 0x8000000000000000ULL
189#define IMAGE_ORDINAL64(ord) ((ord) & 0xffff)
190#define IMAGE_SNAP_BY_ORDINAL64(ord) (!!((ord) & IMAGE_ORDINAL_FLAG64))
191
192
193/* dll/tls entry points argument */
194#define DLL_PROCESS_DETACH 0
195#define DLL_PROCESS_ATTACH 1
196#define DLL_THREAD_ATTACH 2
197#define DLL_THREAD_DETACH 3
198
199
200/*******************************************************************************
201* Structures and Typedefs *
202*******************************************************************************/
203#pragma pack(4)
204
205typedef struct _IMAGE_FILE_HEADER
206{
207 KU16 Machine;
208 KU16 NumberOfSections;
209 KU32 TimeDateStamp;
210 KU32 PointerToSymbolTable;
211 KU32 NumberOfSymbols;
212 KU16 SizeOfOptionalHeader;
213 KU16 Characteristics;
214} IMAGE_FILE_HEADER;
215typedef IMAGE_FILE_HEADER *PIMAGE_FILE_HEADER;
216
217
218typedef struct _IMAGE_DATA_DIRECTORY
219{
220 KU32 VirtualAddress;
221 KU32 Size;
222} IMAGE_DATA_DIRECTORY;
223typedef IMAGE_DATA_DIRECTORY *PIMAGE_DATA_DIRECTORY;
224
225
226typedef struct _IMAGE_OPTIONAL_HEADER32
227{
228 KU16 Magic;
229 KU8 MajorLinkerVersion;
230 KU8 MinorLinkerVersion;
231 KU32 SizeOfCode;
232 KU32 SizeOfInitializedData;
233 KU32 SizeOfUninitializedData;
234 KU32 AddressOfEntryPoint;
235 KU32 BaseOfCode;
236 KU32 BaseOfData;
237 KU32 ImageBase;
238 KU32 SectionAlignment;
239 KU32 FileAlignment;
240 KU16 MajorOperatingSystemVersion;
241 KU16 MinorOperatingSystemVersion;
242 KU16 MajorImageVersion;
243 KU16 MinorImageVersion;
244 KU16 MajorSubsystemVersion;
245 KU16 MinorSubsystemVersion;
246 KU32 Win32VersionValue;
247 KU32 SizeOfImage;
248 KU32 SizeOfHeaders;
249 KU32 CheckSum;
250 KU16 Subsystem;
251 KU16 DllCharacteristics;
252 KU32 SizeOfStackReserve;
253 KU32 SizeOfStackCommit;
254 KU32 SizeOfHeapReserve;
255 KU32 SizeOfHeapCommit;
256 KU32 LoaderFlags;
257 KU32 NumberOfRvaAndSizes;
258 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
259} IMAGE_OPTIONAL_HEADER32;
260typedef IMAGE_OPTIONAL_HEADER32 *PIMAGE_OPTIONAL_HEADER32;
261
262typedef struct _IMAGE_OPTIONAL_HEADER64
263{
264 KU16 Magic;
265 KU8 MajorLinkerVersion;
266 KU8 MinorLinkerVersion;
267 KU32 SizeOfCode;
268 KU32 SizeOfInitializedData;
269 KU32 SizeOfUninitializedData;
270 KU32 AddressOfEntryPoint;
271 KU32 BaseOfCode;
272 KU64 ImageBase;
273 KU32 SectionAlignment;
274 KU32 FileAlignment;
275 KU16 MajorOperatingSystemVersion;
276 KU16 MinorOperatingSystemVersion;
277 KU16 MajorImageVersion;
278 KU16 MinorImageVersion;
279 KU16 MajorSubsystemVersion;
280 KU16 MinorSubsystemVersion;
281 KU32 Win32VersionValue;
282 KU32 SizeOfImage;
283 KU32 SizeOfHeaders;
284 KU32 CheckSum;
285 KU16 Subsystem;
286 KU16 DllCharacteristics;
287 KU64 SizeOfStackReserve;
288 KU64 SizeOfStackCommit;
289 KU64 SizeOfHeapReserve;
290 KU64 SizeOfHeapCommit;
291 KU32 LoaderFlags;
292 KU32 NumberOfRvaAndSizes;
293 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
294} IMAGE_OPTIONAL_HEADER64;
295typedef IMAGE_OPTIONAL_HEADER64 *PIMAGE_OPTIONAL_HEADER64;
296
297
298typedef struct _IMAGE_NT_HEADERS
299{
300 KU32 Signature;
301 IMAGE_FILE_HEADER FileHeader;
302 IMAGE_OPTIONAL_HEADER32 OptionalHeader;
303} IMAGE_NT_HEADERS32;
304typedef IMAGE_NT_HEADERS32 *PIMAGE_NT_HEADERS32;
305
306typedef struct _IMAGE_NT_HEADERS64
307{
308 KU32 Signature;
309 IMAGE_FILE_HEADER FileHeader;
310 IMAGE_OPTIONAL_HEADER64 OptionalHeader;
311} IMAGE_NT_HEADERS64;
312typedef IMAGE_NT_HEADERS64 *PIMAGE_NT_HEADERS64;
313
314
315typedef struct _IMAGE_SECTION_HEADER
316{
317 KU8 Name[IMAGE_SIZEOF_SHORT_NAME];
318 union
319 {
320 KU32 PhysicalAddress;
321 KU32 VirtualSize;
322 } Misc;
323 KU32 VirtualAddress;
324 KU32 SizeOfRawData;
325 KU32 PointerToRawData;
326 KU32 PointerToRelocations;
327 KU32 PointerToLinenumbers;
328 KU16 NumberOfRelocations;
329 KU16 NumberOfLinenumbers;
330 KU32 Characteristics;
331} IMAGE_SECTION_HEADER;
332typedef IMAGE_SECTION_HEADER *PIMAGE_SECTION_HEADER;
333
334
335typedef struct _IMAGE_BASE_RELOCATION
336{
337 KU32 VirtualAddress;
338 KU32 SizeOfBlock;
339} IMAGE_BASE_RELOCATION;
340typedef IMAGE_BASE_RELOCATION *PIMAGE_BASE_RELOCATION;
341
342
343typedef struct _IMAGE_EXPORT_DIRECTORY
344{
345 KU32 Characteristics;
346 KU32 TimeDateStamp;
347 KU16 MajorVersion;
348 KU16 MinorVersion;
349 KU32 Name;
350 KU32 Base;
351 KU32 NumberOfFunctions;
352 KU32 NumberOfNames;
353 KU32 AddressOfFunctions;
354 KU32 AddressOfNames;
355 KU32 AddressOfNameOrdinals;
356} IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY;
357
358
359typedef struct _IMAGE_IMPORT_DESCRIPTOR
360{
361 union
362 {
363 KU32 Characteristics;
364 KU32 OriginalFirstThunk;
365 } u;
366 KU32 TimeDateStamp;
367 KU32 ForwarderChain;
368 KU32 Name;
369 KU32 FirstThunk;
370} IMAGE_IMPORT_DESCRIPTOR;
371typedef IMAGE_IMPORT_DESCRIPTOR *PIMAGE_IMPORT_DESCRIPTOR;
372
373
374typedef struct _IMAGE_IMPORT_BY_NAME
375{
376 KU16 Hint;
377 KU8 Name[1];
378} IMAGE_IMPORT_BY_NAME;
379typedef IMAGE_IMPORT_BY_NAME *PIMAGE_IMPORT_BY_NAME;
380
381
382/* The image_thunk_data32/64 structures are not very helpful except for getting RSI. keep them around till all the code has been converted. */
383typedef struct _IMAGE_THUNK_DATA64
384{
385 union
386 {
387 KU64 ForwarderString;
388 KU64 Function;
389 KU64 Ordinal;
390 KU64 AddressOfData;
391 } u1;
392} IMAGE_THUNK_DATA64;
393typedef IMAGE_THUNK_DATA64 *PIMAGE_THUNK_DATA64;
394
395typedef struct _IMAGE_THUNK_DATA32
396{
397 union
398 {
399 KU32 ForwarderString;
400 KU32 Function;
401 KU32 Ordinal;
402 KU32 AddressOfData;
403 } u1;
404} IMAGE_THUNK_DATA32;
405typedef IMAGE_THUNK_DATA32 *PIMAGE_THUNK_DATA32;
406
407
408typedef struct _IMAGE_LOAD_CONFIG_DIRECTORY32
409{
410 KU32 Size;
411 KU32 TimeDateStamp;
412 KU16 MajorVersion;
413 KU16 MinorVersion;
414 KU32 GlobalFlagsClear;
415 KU32 GlobalFlagsSet;
416 KU32 CriticalSectionDefaultTimeout;
417 KU32 DeCommitFreeBlockThreshold;
418 KU32 DeCommitTotalFreeThreshold;
419 KU32 LockPrefixTable;
420 KU32 MaximumAllocationSize;
421 KU32 VirtualMemoryThreshold;
422 KU32 ProcessHeapFlags;
423 KU32 ProcessAffinityMask;
424 KU16 CSDVersion;
425 KU16 Reserved1;
426 KU32 EditList;
427 KU32 SecurityCookie;
428 KU32 SEHandlerTable;
429 KU32 SEHandlerCount;
430} IMAGE_LOAD_CONFIG_DIRECTORY32;
431typedef IMAGE_LOAD_CONFIG_DIRECTORY32 PIMAGE_LOAD_CONFIG_DIRECTORY32;
432
433typedef struct _IMAGE_LOAD_CONFIG_DIRECTORY64
434{
435 KU32 Size;
436 KU32 TimeDateStamp;
437 KU16 MajorVersion;
438 KU16 MinorVersion;
439 KU32 GlobalFlagsClear;
440 KU32 GlobalFlagsSet;
441 KU32 CriticalSectionDefaultTimeout;
442 KU64 DeCommitFreeBlockThreshold;
443 KU64 DeCommitTotalFreeThreshold;
444 KU64 LockPrefixTable;
445 KU64 MaximumAllocationSize;
446 KU64 VirtualMemoryThreshold;
447 KU64 ProcessAffinityMask;
448 KU32 ProcessHeapFlags;
449 KU16 CSDVersion;
450 KU16 Reserved1;
451 KU64 EditList;
452 KU64 SecurityCookie;
453 KU64 SEHandlerTable;
454 KU64 SEHandlerCount;
455} IMAGE_LOAD_CONFIG_DIRECTORY64;
456typedef IMAGE_LOAD_CONFIG_DIRECTORY64 *PIMAGE_LOAD_CONFIG_DIRECTORY64;
457
458typedef struct _IMAGE_DEBUG_DIRECTORY
459{
460 KU32 Characteristics;
461 KU32 TimeDateStamp;
462 KU16 MajorVersion;
463 KU16 MinorVersion;
464 KU32 Type;
465 KU32 SizeOfData;
466 KU32 AddressOfRawData;
467 KU32 PointerToRawData;
468} IMAGE_DEBUG_DIRECTORY;
469typedef IMAGE_DEBUG_DIRECTORY *PIMAGE_DEBUG_DIRECTORY;
470
471#define IMAGE_DEBUG_TYPE_UNKNOWN 0
472#define IMAGE_DEBUG_TYPE_COFF 1
473#define IMAGE_DEBUG_TYPE_CODEVIEW 2 /* 4.0 */
474#define IMAGE_DEBUG_TYPE_FPO 3 /* FPO = frame pointer omission */
475#define IMAGE_DEBUG_TYPE_MISC 4
476#define IMAGE_DEBUG_TYPE_EXCEPTION 5
477#define IMAGE_DEBUG_TYPE_FIXUP 6
478#define IMAGE_DEBUG_TYPE_BORLAND 9
479
480typedef struct _IMAGE_TLS_DIRECTORY32
481{
482 KU32 StartAddressOfRawData;
483 KU32 EndAddressOfRawData;
484 KU32 AddressOfIndex;
485 KU32 AddressOfCallBacks;
486 KU32 SizeOfZeroFill;
487 KU32 Characteristics;
488} IMAGE_TLS_DIRECTORY32;
489typedef IMAGE_TLS_DIRECTORY32 *PIMAGE_TLS_DIRECTORY32;
490
491typedef struct _IMAGE_TLS_DIRECTORY64
492{
493 KU64 StartAddressOfRawData;
494 KU64 EndAddressOfRawData;
495 KU64 AddressOfIndex;
496 KU64 AddressOfCallBacks;
497 KU32 SizeOfZeroFill;
498 KU32 Characteristics;
499} IMAGE_TLS_DIRECTORY64;
500typedef IMAGE_TLS_DIRECTORY64 *PIMAGE_TLS_DIRECTORY64;
501
502
503#pragma pack()
504
505#endif
506
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette