forked from tecknicaltom/HT-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpestruct.h
271 lines (235 loc) · 7.2 KB
/
pestruct.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* HT Editor
* pestruct.h
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __PESTRUCT_H_
#define __PESTRUCT_H_
#include "io/types.h"
#include "tools.h"
#include "coff_s.h"
#define PE_MAGIC0 'P'
#define PE_MAGIC1 'E'
#define PE_MAGIC2 0
#define PE_MAGIC3 0
//
// Directory format.
//
struct PE_DATA_DIRECTORY {
uint32 address;
uint32 size;
} PACKED;
#define PE_NUMBEROF_DIRECTORY_ENTRIES 16
//
// Optional header format.
//
struct PE_OPTIONAL_HEADER32_NT {
// NT additional fields.
uint32 image_base;
uint32 section_alignment;
uint32 file_alignment;
uint16 major_os_version;
uint16 minor_os_version;
uint16 major_image_version;
uint16 minor_image_version;
uint16 major_subsystem_version;
uint16 minor_subsystem_version;
uint32 win32_version;
uint32 image_size;
uint32 header_size;
uint32 checksum;
uint16 subsystem;
uint16 dll_characteristics;
uint32 stack_reserve_size;
uint32 stack_commit_size;
uint32 heap_reserve_size;
uint32 heap_commit_size;
uint32 loader_flags;
uint32 directory_count;
PE_DATA_DIRECTORY directory[PE_NUMBEROF_DIRECTORY_ENTRIES];
} PACKED;
struct PE_OPTIONAL_HEADER64_NT {
// NT additional fields.
uint64 image_base;
uint32 section_alignment;
uint32 file_alignment;
uint16 major_os_version;
uint16 minor_os_version;
uint16 major_image_version;
uint16 minor_image_version;
uint16 major_subsystem_version;
uint16 minor_subsystem_version;
uint32 win32_version;
uint32 image_size;
uint32 header_size;
uint32 checksum;
uint16 subsystem;
uint16 dll_characteristics;
uint64 stack_reserve_size;
uint64 stack_commit_size;
uint64 heap_reserve_size;
uint64 heap_commit_size;
uint32 loader_flags;
uint32 directory_count;
PE_DATA_DIRECTORY directory[PE_NUMBEROF_DIRECTORY_ENTRIES];
} PACKED;
// Subsystem Values
#define PE_SUBSYSTEM_NATIVE 1 // image doesn't require a subsystem.
#define PE_SUBSYSTEM_WINDOWS_GUI 2 // image runs in the Windows GUI subsystem.
#define PE_SUBSYSTEM_WINDOWS_CUI 3 // image runs in the Windows character subsystem.
#define PE_SUBSYSTEM_OS2_CUI 5 // image runs in the OS/2 character subsystem.
#define PE_SUBSYSTEM_POSIX_CUI 7 // image run in the Posix character subsystem.
#define PE_SUBSYSTEM_RESERVED8 8 // image run in the 8 subsystem.
#define PE_SUBSYSTEM_CE_GUI 9 // image runs in the Windows CE subsystem.
#define PE_SUBSYSTEM_EFI_APPLICATION 10 // image is an EFI application.
#define PE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11 // image is an EFI driver that provides boot services.
#define PE_SUBSYSTEM_EFI_RUNTIME_DRIVER 12 // image is an EFI driver that provides runtime services.
// Directory Entries
#define PE_DIRECTORY_ENTRY_EXPORT 0 // Export Directory
#define PE_DIRECTORY_ENTRY_IMPORT 1 // Import Directory
#define PE_DIRECTORY_ENTRY_RESOURCE 2 // Resource Directory
#define PE_DIRECTORY_ENTRY_EXCEPTION 3 // Exception Directory
#define PE_DIRECTORY_ENTRY_SECURITY 4 // Security Directory
#define PE_DIRECTORY_ENTRY_BASERELOC 5 // Base Relocation Table
#define PE_DIRECTORY_ENTRY_DEBUG 6 // Debug Directory
#define PE_DIRECTORY_ENTRY_COPYRIGHT 7 // Description String
#define PE_DIRECTORY_ENTRY_GLOBALPTR 8 // Machine Value (MIPS GP)
#define PE_DIRECTORY_ENTRY_TLS 9 // TLS Directory
#define PE_DIRECTORY_ENTRY_LOAD_CONFIG 10 // Load Configuration Directory
#define PE_DIRECTORY_ENTRY_BOUND_IMPORT 11 // Bound Import Directory in headers
#define PE_DIRECTORY_ENTRY_IAT 12 // Import Address Table
#define PE_DIRECTORY_ENTRY_DELAY_IMPORT 13 // Delay Import Directory
#define PE_DIRECTORY_ENTRY_IL 14 // IL (e.g. MS .NET)
/*
* Export
*/
struct PE_EXPORT_DIRECTORY {
uint32 characteristics;
uint32 timestamp;
uint16 major_version;
uint16 minor_version;
uint32 name_address;
uint32 ordinal_base;
uint32 function_count;
uint32 name_count;
uint32 function_table_address;
uint32 name_table_address;
uint32 ordinal_table_address;
} PACKED;
/*
* Import
*/
struct PE_THUNK_DATA {
union {
uint32 forwarder_string;
uint32 function_desc_address;
uint32 ordinal;
uint32 data_address;
};
} PACKED;
struct PE_THUNK_DATA_64 {
union {
uint64 forwarder_string;
uint64 function_desc_address;
uint64 ordinal;
uint64 data_address;
};
} PACKED;
struct PE_IMPORT_DESCRIPTOR {
union {
uint32 characteristics; // 0 for terminating null import descriptor
uint32 original_first_thunk; // rva to original unbound IAT
};
uint32 timestamp; // 0 if not bound,
// -1 if bound, and real date\time stamp
// in IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT (new BIND)
// O.W. date/time stamp of DLL bound to (Old BIND)
uint32 forwarder_chain; // -1 if no forwarders
uint32 name;
uint32 first_thunk; // rva to IAT (if bound this IAT has actual addresses)
} PACKED;
/*
* Delay Import
*/
struct PE_DELAY_IMPORT_DESCRIPTOR {
uint32 attributes;
uint32 name;
uint32 module_handle;
uint32 delay_iat;
uint32 delay_int;
uint32 bound_delay_import_table;
uint32 unload_delay_import_table;
uint32 timestamp;
} PACKED;
/*
* Resource
*/
struct PE_RESOURCE_DIRECTORY {
uint32 characteristics;
uint32 timedate_stamp;
uint16 major_version;
uint16 minor_version;
uint16 name_count;
uint16 id_count;
// PE_RESOURCE_DIRECTORY_ENTRY DirectoryEntries[];
} PACKED;
#define PE_RESOURCE_NAME_IS_STRING 0x80000000
#define PE_RESOURCE_DATA_IS_DIRECTORY 0x80000000
struct PE_RESOURCE_DIRECTORY_ENTRY {
uint32 name; // or id
uint32 offset_to_directory; // or data
} PACKED;
/* struct PE_RESOURCE_DIRECTORY_STRING {
uint16 Length;
CHAR NameString[ 1 ];
} PACKED;
struct PE_RESOURCE_DIR_STRING_U {
uint16 Length;
WCHAR NameString[ 1 ];
} PACKED;*/
struct PE_RESOURCE_DATA_ENTRY {
uint32 offset_to_data;
uint32 size;
uint32 codepage;
uint32 reserved;
} PACKED;
/*
* IL
*/
#define PE_IL_DIRECTORY_ATTRIBUTES_HAD_NATIVE 0x1
#define PE_IL_DIRECTORY_ATTRIBUTES_INT64 0x2
struct PE_IL_DIRECTORY {
uint32 size;
uint16 major_version;
uint16 minor_version;
uint32 metadata_section_rva;
uint32 metadata_section_size;
uint32 attributes;
} PACKED;
extern byte PE_DATA_DIRECTORY_struct[];
extern byte PE_OPTIONAL_HEADER32_NT_struct[];
extern byte PE_OPTIONAL_HEADER64_NT_struct[];
extern byte PE_EXPORT_DIRECTORY_struct[];
extern byte PE_THUNK_DATA_struct[];
extern byte PE_THUNK_DATA_64_struct[];
extern byte PE_IMPORT_DESCRIPTOR_struct[];
extern byte PE_DELAY_IMPORT_DESCRIPTOR_struct[];
extern byte PE_RESOURCE_DIRECTORY_struct[];
extern byte PE_RESOURCE_DIRECTORY_ENTRY_struct[];
extern byte PE_RESOURCE_DATA_ENTRY_struct[];
extern byte PE_IL_DIRECTORY_struct[];
#endif