-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdosheader.pas
276 lines (240 loc) · 9.14 KB
/
dosheader.pas
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
272
273
274
275
276
unit dosheader;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
const
IMAGE_SIZEOF_SHORT_NAME = 8;
IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
IMAGE_FILE_RELOCS_STRIPPED = $0001;
IMAGE_FILE_EXECUTABLE_IMAGE = $0002;
IMAGE_FILE_LINE_NUMS_STRIPPED = $0004;
IMAGE_FILE_LOCAL_SYMS_STRIPPED = $0008;
IMAGE_FILE_AGGRESIVE_WS_TRIM = $0010;
IMAGE_FILE_LARGE_ADDRESS_AWARE = $0020;
IMAGE_FILE_BYTES_REVERSED_LO = $0080;
IMAGE_FILE_32BIT_MACHINE = $0100;
IMAGE_FILE_DEBUG_STRIPPED = $0200;
IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = $0400;
IMAGE_FILE_NET_RUN_FROM_SWAP = $0800;
IMAGE_FILE_SYSTEM = $1000;
IMAGE_FILE_DLL = $2000;
IMAGE_FILE_UP_SYSTEM_ONLY = $4000;
IMAGE_FILE_BYTES_REVERSED_HI = $8000;
IMAGE_ORDINAL_FLAG32 = DWORD($80000000);
IMAGE_ORDINAL_FLAG64 = QWORD($8000000000000000);
IMAGE_SIZEOF_BASE_RELOCATION = 8;
IMAGE_REL_BASED_HIGHLOW = 3;
IMAGE_SCN_MEM_DISCARDABLE = $02000000;
IMAGE_SCN_MEM_NOT_CACHED = $04000000;
IMAGE_SCN_CNT_INITIALIZED_DATA = $00000040;
IMAGE_SCN_CNT_UNINITIALIZED_DATA = $00000080;
IMAGE_SCN_MEM_EXECUTE = $20000000;
IMAGE_SCN_MEM_READ = $40000000;
IMAGE_SCN_MEM_WRITE = DWORD($80000000);
cDOSRelocOffset = $18; // offset of "pointer" to DOS relocation table
cWinHeaderOffset = $3C; // offset of "pointer" to windows header in file
cNEAppTypeOffset = $0D; // offset in NE windows header of app type field
cDOSMagic = $5A4D; // magic number for a DOS executable
cNEMagic = $454E; // magic number for a NE executable (Win 16)
cPEMagic = $4550; // magic nunber for a PE executable (Win 32)
cLEMagic = $454C; // magic number for a Virtual Device Driver
cNEDLLFlag = $80; // flag in NE app type field indicating a DLL
type
LIST_ENTRY = record
Flink : ^_LIST_ENTRY;
Blink : ^_LIST_ENTRY;
end;
_LIST_ENTRY = LIST_ENTRY;
TLISTENTRY = LIST_ENTRY;
PLISTENTRY = ^LIST_ENTRY;
IMAGE_BASE_RELOCATION = packed record
VirtualAddress: DWORD;
SizeOfBlock: DWORD;
end;
PIMAGE_BASE_RELOCATION = ^IMAGE_BASE_RELOCATION;
IMAGE_EXPORT_DIRECTORY = packed record
Characteristics: DWORD;
TimeDateStamp: DWORD;
MajorVersion: WORD;
MinorVersion: WORD;
Name: DWORD;
Base: DWORD;
NumberOfFunctions: DWORD;
NumberOfNames: DWORD;
AddressOfFunctions: DWORD;
AddressOfNames: DWORD;
AddressOfNameOrdinals: DWORD;
end;
PIMAGE_EXPORT_DIRECTORY = ^IMAGE_EXPORT_DIRECTORY;
IMAGE_EXPORT_DIRECTORY_ARRAY = array[0..0] of IMAGE_EXPORT_DIRECTORY;
PIMAGE_EXPORT_DIRECTORY_ARRAY = ^IMAGE_EXPORT_DIRECTORY_ARRAY;
IMAGE_IMPORT_DESCRIPTOR = packed record
OriginalFirstThunk: DWORD;
TimeDateStamp: DWORD;
ForwarderChain: DWORD;
Name: DWORD;
FirstThunk: DWORD;
end;
PIMAGE_IMPORT_DESCRIPTOR = ^IMAGE_IMPORT_DESCRIPTOR;
TImageImportDescriptor = IMAGE_IMPORT_DESCRIPTOR;
IMAGE_IMPORT_DESCRIPTOR_ARRAY = packed array[0..0] of IMAGE_IMPORT_DESCRIPTOR;
PIMAGE_IMPORT_DESCRIPTOR_ARRAY = ^IMAGE_IMPORT_DESCRIPTOR_ARRAY;
PImageImportByName = ^TImageImportByName;
IMAGE_IMPORT_BY_NAME = packed record
Hint: Word;
Name: array [0 .. 255] of Byte; // original: "Name: array [0..0] of Byte;"
end;
TImageImportByName = IMAGE_IMPORT_BY_NAME;
PIMAGE_IMPORT_BY_NAME = ^IMAGE_IMPORT_BY_NAME;
IMAGE_DATA_DIRECTORY = packed record
VirtualAddress : DWORD;
Size : DWORD;
end;
PIMAGE_DATA_DIRECTORY = ^IMAGE_DATA_DIRECTORY;
IMAGE_SECTION_HEADER = packed record
Name : packed array [0..IMAGE_SIZEOF_SHORT_NAME-1] of Char;
PhysicalAddress : DWORD; // or VirtualSize (union);
VirtualAddress : DWORD;
SizeOfRawData : DWORD;
PointerToRawData : DWORD;
PointerToRelocations : DWORD;
PointerToLinenumbers : DWORD;
NumberOfRelocations : WORD;
NumberOfLinenumbers : WORD;
Characteristics : DWORD;
end;
PIMAGE_SECTION_HEADER = ^IMAGE_SECTION_HEADER;
IMAGE_OPTIONAL_HEADER = packed record
{ Standard fields. }
Magic : WORD;
MajorLinkerVersion : Byte;
MinorLinkerVersion : Byte;
SizeOfCode : DWORD;
SizeOfInitializedData : DWORD;
SizeOfUninitializedData : DWORD;
AddressOfEntryPoint : DWORD;
BaseOfCode : DWORD;
BaseOfData : DWORD;
{ NT additional fields. }
ImageBase : DWORD;
SectionAlignment : DWORD;
FileAlignment : DWORD;
MajorOperatingSystemVersion : WORD;
MinorOperatingSystemVersion : WORD;
MajorImageVersion : WORD;
MinorImageVersion : WORD;
MajorSubsystemVersion : WORD;
MinorSubsystemVersion : WORD;
Reserved1 : DWORD;
SizeOfImage : DWORD;
SizeOfHeaders : DWORD;
CheckSum : DWORD;
Subsystem : WORD;
DllCharacteristics : WORD;
SizeOfStackReserve : DWORD;
SizeOfStackCommit : DWORD;
SizeOfHeapReserve : DWORD;
SizeOfHeapCommit : DWORD;
LoaderFlags : DWORD;
NumberOfRvaAndSizes : DWORD;
DataDirectory: packed array[0..IMAGE_NUMBEROF_DIRECTORY_ENTRIES-1] of IMAGE_DATA_DIRECTORY;
end;
PIMAGE_OPTIONAL_HEADER = ^IMAGE_OPTIONAL_HEADER;
IMAGE_OPTIONAL_HEADER64 = packed record
{ Standard fields. }
Magic : WORD;
MajorLinkerVersion : Byte;
MinorLinkerVersion : Byte;
SizeOfCode : DWORD;
SizeOfInitializedData : DWORD;
SizeOfUninitializedData : DWORD;
AddressOfEntryPoint : DWORD;
BaseOfCode : DWORD;
// BaseOfData : DWORD;
{ NT additional fields. }
ImageBase : UInt64;
SectionAlignment : DWORD;
FileAlignment : DWORD;
MajorOperatingSystemVersion : WORD;
MinorOperatingSystemVersion : WORD;
MajorImageVersion : WORD;
MinorImageVersion : WORD;
MajorSubsystemVersion : WORD;
MinorSubsystemVersion : WORD;
Win32VersionValue : DWORD;
SizeOfImage : DWORD;
SizeOfHeaders : DWORD;
CheckSum : DWORD;
Subsystem : WORD;
DllCharacteristics : WORD;
SizeOfStackReserve : UInt64;
SizeOfStackCommit : UInt64;
SizeOfHeapReserve : UInt64;
SizeOfHeapCommit : UInt64;
LoaderFlags : DWORD;
NumberOfRvaAndSizes : DWORD;
DataDirectory: packed array[0..IMAGE_NUMBEROF_DIRECTORY_ENTRIES-1] of IMAGE_DATA_DIRECTORY;
end;
PIMAGE_OPTIONAL_HEADER64 = ^IMAGE_OPTIONAL_HEADER64;
IMAGE_FILE_HEADER = packed record
Machine : WORD;
NumberOfSections : WORD;
TimeDateStamp : DWORD;
PointerToSymbolTable : DWORD;
NumberOfSymbols : DWORD;
SizeOfOptionalHeader : WORD;
Characteristics : WORD;
end;
PIMAGE_FILE_HEADER = ^IMAGE_FILE_HEADER;
IMAGE_NT_HEADERS = packed record
Signature : DWORD;
FileHeader : IMAGE_FILE_HEADER;
OptionalHeader : IMAGE_OPTIONAL_HEADER;
end;
PIMAGE_NT_HEADERS = ^IMAGE_NT_HEADERS;
IMAGE_NT_HEADERS64 = packed record
Signature : DWORD;
FileHeader : IMAGE_FILE_HEADER;
OptionalHeader : IMAGE_OPTIONAL_HEADER64;
end;
PIMAGE_NT_HEADERS64 = ^IMAGE_NT_HEADERS64;
LOADED_IMAGE = record
ModuleName:pchar;//name of module
hFile:thandle;//handle of file
MappedAddress:pchar;// the base address of mapped file
FileHeader:PIMAGE_NT_HEADERS;//The Header of the file.
LastRvaSection:PIMAGE_SECTION_HEADER;
NumberOfSections:integer;
Sections:PIMAGE_SECTION_HEADER ;
Characteristics:integer;
fSystemImage:boolean;
fDOSImage:boolean;
Links:LIST_ENTRY;
SizeOfImage:integer;
end;
PLOADED_IMAGE= ^LOADED_IMAGE;
IMAGE_DOS_HEADER = packed record
e_magic : Word; // Magic number ("MZ")
e_cblp : Word; // Bytes on last page of file
e_cp : Word; // Pages in file
e_crlc : Word; // Relocations
e_cparhdr : Word; // Size of header in paragraphs
e_minalloc: Word; // Minimum extra paragraphs needed
e_maxalloc: Word; // Maximum extra paragraphs needed
e_ss : Word; // Initial (relative) SS value
e_sp : Word; // Initial SP value
e_csum : Word; // Checksum
e_ip : Word; // Initial IP value
e_cs : Word; // Initial (relative) CS value
e_lfarlc : Word; // Address of relocation table
e_ovno : Word; // Overlay number
e_res : packed array [0..3] of Word; // Reserved words
e_oemid : Word; // OEM identifier (for e_oeminfo)
e_oeminfo : Word; // OEM info; e_oemid specific
e_res2 : packed array [0..9] of Word; // Reserved words
e_lfanew : Longint; // File address of new exe header
end;
PIMAGE_DOS_HEADER = ^IMAGE_DOS_HEADER;
implementation
end.