Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winedump/pe.c
8742 views
1
/*
2
* PE dumping utility
3
*
4
* Copyright 2001 Eric Pouech
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
*/
20
21
#include "config.h"
22
23
#include <stdlib.h>
24
#include <stdarg.h>
25
#include <stdio.h>
26
#include <time.h>
27
#include <fcntl.h>
28
29
#include "windef.h"
30
#include "winbase.h"
31
#include "verrsrc.h"
32
#include "winedump.h"
33
34
#define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
35
36
static const IMAGE_NT_HEADERS32* PE_nt_headers;
37
38
static const char builtin_signature[] = "Wine builtin DLL";
39
static const char fakedll_signature[] = "Wine placeholder DLL";
40
static int is_builtin;
41
42
const char *get_machine_str(int mach)
43
{
44
switch (mach)
45
{
46
case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
47
case IMAGE_FILE_MACHINE_I386: return "i386";
48
case IMAGE_FILE_MACHINE_R3000: return "R3000";
49
case IMAGE_FILE_MACHINE_R4000: return "R4000";
50
case IMAGE_FILE_MACHINE_R10000: return "R10000";
51
case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
52
case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
53
case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
54
case IMAGE_FILE_MACHINE_IA64: return "IA64";
55
case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
56
case IMAGE_FILE_MACHINE_ARM: return "ARM";
57
case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
58
case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
59
case IMAGE_FILE_MACHINE_ALPHA64: return "Alpha64";
60
case IMAGE_FILE_MACHINE_CHPE_X86: return "CHPE-x86";
61
case IMAGE_FILE_MACHINE_ARM64EC: return "ARM64EC";
62
case IMAGE_FILE_MACHINE_ARM64X: return "ARM64X";
63
case IMAGE_FILE_MACHINE_RISCV32: return "RISC-V 32-bit";
64
case IMAGE_FILE_MACHINE_RISCV64: return "RISC-V 64-bit";
65
case IMAGE_FILE_MACHINE_RISCV128: return "RISC-V 128-bit";
66
}
67
return "???";
68
}
69
70
static const void* RVA(unsigned long rva, unsigned long len)
71
{
72
IMAGE_SECTION_HEADER* sectHead;
73
int i;
74
75
if (rva == 0) return NULL;
76
77
sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
78
for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
79
{
80
if (sectHead[i].VirtualAddress <= rva &&
81
rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
82
{
83
/* return image import directory offset */
84
return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
85
}
86
}
87
88
return NULL;
89
}
90
91
static const IMAGE_NT_HEADERS32 *get_nt_header( void )
92
{
93
const IMAGE_DOS_HEADER *dos;
94
dos = PRD(0, sizeof(*dos));
95
if (!dos) return NULL;
96
is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
97
!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
98
return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
99
}
100
101
void print_fake_dll( void )
102
{
103
const IMAGE_DOS_HEADER *dos;
104
105
dos = PRD(0, sizeof(*dos) + 32);
106
if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
107
{
108
if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
109
printf( "*** This is a Wine builtin DLL ***\n\n" );
110
else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
111
printf( "*** This is a Wine fake DLL ***\n\n" );
112
}
113
}
114
115
static const void *get_data_dir(const IMAGE_NT_HEADERS32 *hdr, unsigned int idx, unsigned int *size)
116
{
117
if(hdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
118
{
119
const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&hdr->OptionalHeader;
120
if (idx >= opt->NumberOfRvaAndSizes)
121
return NULL;
122
if(size)
123
*size = opt->DataDirectory[idx].Size;
124
return RVA(opt->DataDirectory[idx].VirtualAddress,
125
opt->DataDirectory[idx].Size);
126
}
127
else
128
{
129
const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&hdr->OptionalHeader;
130
if (idx >= opt->NumberOfRvaAndSizes)
131
return NULL;
132
if(size)
133
*size = opt->DataDirectory[idx].Size;
134
return RVA(opt->DataDirectory[idx].VirtualAddress,
135
opt->DataDirectory[idx].Size);
136
}
137
}
138
139
static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
140
{
141
return get_data_dir( PE_nt_headers, idx, size );
142
}
143
144
static const void* get_dir(unsigned idx)
145
{
146
return get_dir_and_size(idx, 0);
147
}
148
149
static const char * const DirectoryNames[16] = {
150
"EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
151
"SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
152
"GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
153
"IAT", "Delay IAT", "CLR Header", ""
154
};
155
156
static const char *get_magic_type(WORD magic)
157
{
158
switch(magic) {
159
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
160
return "32bit";
161
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
162
return "64bit";
163
case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
164
return "ROM";
165
}
166
return "???";
167
}
168
169
static const void *get_hybrid_metadata(void)
170
{
171
unsigned int size;
172
173
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
174
{
175
const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
176
if (!cfg) return 0;
177
size = min( size, cfg->Size );
178
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CHPEMetadataPointer )) return 0;
179
if (!cfg->CHPEMetadataPointer) return 0;
180
return RVA( cfg->CHPEMetadataPointer - ((const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader)->ImageBase, 1 );
181
}
182
else
183
{
184
const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
185
if (!cfg) return 0;
186
size = min( size, cfg->Size );
187
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CHPEMetadataPointer )) return 0;
188
if (!cfg->CHPEMetadataPointer) return 0;
189
return RVA( cfg->CHPEMetadataPointer - PE_nt_headers->OptionalHeader.ImageBase, 1 );
190
}
191
}
192
193
static inline const char *longlong_str( ULONGLONG value )
194
{
195
static char buffer[20];
196
197
if (sizeof(value) > sizeof(unsigned long) && value >> 32)
198
sprintf(buffer, "%lx%08lx", (unsigned long)(value >> 32), (unsigned long)value);
199
else
200
sprintf(buffer, "%lx", (unsigned long)value);
201
return buffer;
202
}
203
204
static inline void print_word(const char *title, WORD value)
205
{
206
printf(" %-34s 0x%-4X %u\n", title, value, value);
207
}
208
209
static inline void print_dword(const char *title, UINT value)
210
{
211
printf(" %-34s 0x%-8x %u\n", title, value, value);
212
}
213
214
static inline void print_longlong(const char *title, ULONGLONG value)
215
{
216
printf(" %-34s 0x%s\n", title, longlong_str(value));
217
}
218
219
static inline void print_ver(const char *title, BYTE major, BYTE minor)
220
{
221
printf(" %-34s %u.%02u\n", title, major, minor);
222
}
223
224
static inline void print_subsys(const char *title, WORD value)
225
{
226
const char *str;
227
switch (value)
228
{
229
default:
230
case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
231
case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
232
case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
233
case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
234
case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
235
case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
236
case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
237
case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
238
case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
239
case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
240
case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
241
case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
242
case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
243
case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
244
}
245
printf(" %-34s 0x%X (%s)\n", title, value, str);
246
}
247
248
static inline void print_dllflags(const char *title, WORD value)
249
{
250
printf(" %-34s 0x%04X\n", title, value);
251
#define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
252
if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
253
X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
254
X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
255
X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
256
X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
257
X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
258
X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
259
X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
260
X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
261
X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
262
X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
263
X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
264
#undef X
265
}
266
267
static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
268
{
269
unsigned i;
270
printf("Data Directory\n");
271
272
for (i = 0; i < n && i < 16; i++)
273
{
274
printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
275
DirectoryNames[i], (UINT)directory[i].VirtualAddress,
276
(UINT)directory[i].Size);
277
}
278
}
279
280
static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh)
281
{
282
IMAGE_OPTIONAL_HEADER32 oh;
283
const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
284
285
/* in case optional header is missing or partial */
286
memset(&oh, 0, sizeof(oh));
287
memcpy(&oh, image_oh, min(dump_total_len - ((char *)image_oh - (char *)dump_base), sizeof(oh)));
288
optionalHeader = &oh;
289
290
print_word("Magic", optionalHeader->Magic);
291
print_ver("linker version",
292
optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
293
print_dword("size of code", optionalHeader->SizeOfCode);
294
print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
295
print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
296
print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
297
print_dword("base of code", optionalHeader->BaseOfCode);
298
print_dword("base of data", optionalHeader->BaseOfData);
299
print_dword("image base", optionalHeader->ImageBase);
300
print_dword("section align", optionalHeader->SectionAlignment);
301
print_dword("file align", optionalHeader->FileAlignment);
302
print_ver("required OS version",
303
optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
304
print_ver("image version",
305
optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
306
print_ver("subsystem version",
307
optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
308
print_dword("Win32 Version", optionalHeader->Win32VersionValue);
309
print_dword("size of image", optionalHeader->SizeOfImage);
310
print_dword("size of headers", optionalHeader->SizeOfHeaders);
311
print_dword("checksum", optionalHeader->CheckSum);
312
print_subsys("Subsystem", optionalHeader->Subsystem);
313
print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
314
print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
315
print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
316
print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
317
print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
318
print_dword("loader flags", optionalHeader->LoaderFlags);
319
print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
320
printf("\n");
321
print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
322
printf("\n");
323
}
324
325
static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh)
326
{
327
IMAGE_OPTIONAL_HEADER64 oh;
328
const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
329
330
/* in case optional header is missing or partial */
331
memset(&oh, 0, sizeof(oh));
332
memcpy(&oh, image_oh, min(dump_total_len - ((char *)image_oh - (char *)dump_base), sizeof(oh)));
333
optionalHeader = &oh;
334
335
print_word("Magic", optionalHeader->Magic);
336
print_ver("linker version",
337
optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
338
print_dword("size of code", optionalHeader->SizeOfCode);
339
print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
340
print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
341
print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
342
print_dword("base of code", optionalHeader->BaseOfCode);
343
print_longlong("image base", optionalHeader->ImageBase);
344
print_dword("section align", optionalHeader->SectionAlignment);
345
print_dword("file align", optionalHeader->FileAlignment);
346
print_ver("required OS version",
347
optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
348
print_ver("image version",
349
optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
350
print_ver("subsystem version",
351
optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
352
print_dword("Win32 Version", optionalHeader->Win32VersionValue);
353
print_dword("size of image", optionalHeader->SizeOfImage);
354
print_dword("size of headers", optionalHeader->SizeOfHeaders);
355
print_dword("checksum", optionalHeader->CheckSum);
356
print_subsys("Subsystem", optionalHeader->Subsystem);
357
print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
358
print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
359
print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
360
print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
361
print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
362
print_dword("loader flags", optionalHeader->LoaderFlags);
363
print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
364
printf("\n");
365
print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
366
printf("\n");
367
}
368
369
void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader)
370
{
371
printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
372
373
switch(optionalHeader->Magic) {
374
case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
375
dump_optional_header32(optionalHeader);
376
break;
377
case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
378
dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader);
379
break;
380
default:
381
printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
382
break;
383
}
384
}
385
386
void dump_file_header(const IMAGE_FILE_HEADER *fileHeader, BOOL is_hybrid)
387
{
388
const char *name = get_machine_str(fileHeader->Machine);
389
390
printf("File Header\n");
391
392
if (is_hybrid)
393
{
394
switch (fileHeader->Machine)
395
{
396
case IMAGE_FILE_MACHINE_I386: name = "CHPE"; break;
397
case IMAGE_FILE_MACHINE_AMD64: name = "ARM64EC"; break;
398
case IMAGE_FILE_MACHINE_ARM64: name = "ARM64X"; break;
399
}
400
}
401
printf(" Machine: %04X (%s)\n", fileHeader->Machine, name);
402
printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
403
printf(" TimeDateStamp: %08X (%s)\n",
404
(UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp));
405
printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
406
printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
407
printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
408
printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
409
#define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
410
X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
411
X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
412
X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
413
X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
414
X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
415
X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
416
X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
417
X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
418
X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
419
X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
420
X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
421
X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
422
X(IMAGE_FILE_SYSTEM, "SYSTEM");
423
X(IMAGE_FILE_DLL, "DLL");
424
X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
425
X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
426
#undef X
427
printf("\n");
428
}
429
430
static void dump_pe_header(void)
431
{
432
dump_file_header(&PE_nt_headers->FileHeader, get_hybrid_metadata() != NULL);
433
dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader);
434
}
435
436
void dump_section_characteristics(DWORD characteristics, const char* sep)
437
{
438
#define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
439
/* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
440
/* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
441
/* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
442
/* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
443
/* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
444
/* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
445
446
X(IMAGE_SCN_CNT_CODE, "CODE");
447
X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
448
X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
449
450
X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
451
X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
452
/* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
453
X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
454
X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
455
456
/* 0x00002000 - Reserved */
457
/* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
458
X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
459
460
/* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
461
X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
462
X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
463
X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
464
X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
465
466
switch (characteristics & IMAGE_SCN_ALIGN_MASK)
467
{
468
#define X2(b,s) case b: printf("%s%s", sep, s); break
469
X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
470
X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
471
X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
472
X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
473
X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
474
X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
475
X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
476
X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
477
X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
478
X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
479
X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
480
X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
481
X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
482
X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
483
#undef X2
484
}
485
486
X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
487
488
X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
489
X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
490
X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
491
X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
492
X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
493
X(IMAGE_SCN_MEM_READ, "MEM_READ");
494
X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
495
#undef X
496
}
497
498
void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
499
{
500
unsigned offset;
501
502
/* long section name ? */
503
if (strtable && sectHead->Name[0] == '/' &&
504
((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
505
printf(" %.8s (%s)", sectHead->Name, strtable + offset);
506
else
507
printf(" %-8.8s", sectHead->Name);
508
printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
509
(UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
510
printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
511
(UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
512
printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
513
(UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
514
printf(" line # offs: %-8u line #'s: %-8u\n",
515
(UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
516
printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
517
printf(" ");
518
dump_section_characteristics(sectHead->Characteristics, " ");
519
520
printf("\n\n");
521
}
522
523
static void dump_sections(const void *base, const void* addr, unsigned num_sect)
524
{
525
const IMAGE_SECTION_HEADER* sectHead = addr;
526
unsigned i;
527
const char* strtable;
528
529
if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
530
{
531
strtable = (const char*)base +
532
PE_nt_headers->FileHeader.PointerToSymbolTable +
533
PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
534
}
535
else strtable = NULL;
536
537
printf("Section Table\n");
538
for (i = 0; i < num_sect; i++, sectHead++)
539
{
540
dump_section(sectHead, strtable);
541
542
if (globals.do_dump_rawdata)
543
{
544
dump_data_offset((const unsigned char *)base + sectHead->PointerToRawData,
545
sectHead->SizeOfRawData, sectHead->VirtualAddress, " " );
546
printf("\n");
547
}
548
}
549
}
550
551
static char *get_str( char *buffer, unsigned int rva, unsigned int len )
552
{
553
const WCHAR *wstr = PRD( rva, len );
554
char *ret = buffer;
555
556
len /= sizeof(WCHAR);
557
while (len--) *buffer++ = *wstr++;
558
*buffer = 0;
559
return ret;
560
}
561
562
static void dump_section_apiset(void)
563
{
564
const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
565
const UINT *ptr, *entry, *value, *hash;
566
unsigned int i, j, count, val_count, rva;
567
char buffer[128];
568
569
for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
570
{
571
if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
572
rva = sect->PointerToRawData;
573
ptr = PRD( rva, sizeof(*ptr) );
574
printf( "ApiSet section:\n" );
575
switch (ptr[0]) /* version */
576
{
577
case 2:
578
printf( " Version: %u\n", ptr[0] );
579
printf( " Count: %08x\n", ptr[1] );
580
count = ptr[1];
581
if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
582
for (i = 0; i < count; i++, entry += 3)
583
{
584
printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
585
if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
586
val_count = *value++;
587
for (j = 0; j < val_count; j++, value += 4)
588
{
589
putchar( ' ' );
590
if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
591
printf( "%s", get_str( buffer, rva + value[2], value[3] ));
592
}
593
printf( "\n");
594
}
595
break;
596
case 4:
597
printf( " Version: %u\n", ptr[0] );
598
printf( " Size: %08x\n", ptr[1] );
599
printf( " Flags: %08x\n", ptr[2] );
600
printf( " Count: %08x\n", ptr[3] );
601
count = ptr[3];
602
if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
603
for (i = 0; i < count; i++, entry += 6)
604
{
605
printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
606
if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
607
value++; /* flags */
608
val_count = *value++;
609
for (j = 0; j < val_count; j++, value += 5)
610
{
611
putchar( ' ' );
612
if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
613
printf( "%s", get_str( buffer, rva + value[3], value[4] ));
614
}
615
printf( "\n");
616
}
617
break;
618
case 6:
619
printf( " Version: %u\n", ptr[0] );
620
printf( " Size: %08x\n", ptr[1] );
621
printf( " Flags: %08x\n", ptr[2] );
622
printf( " Count: %08x\n", ptr[3] );
623
printf( " EntryOffset: %08x\n", ptr[4] );
624
printf( " HashOffset: %08x\n", ptr[5] );
625
printf( " HashFactor: %08x\n", ptr[6] );
626
count = ptr[3];
627
if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
628
for (i = 0; i < count; i++, entry += 6)
629
{
630
printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
631
if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
632
for (j = 0; j < entry[5]; j++, value += 5)
633
{
634
putchar( ' ' );
635
if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
636
printf( "%s", get_str( buffer, rva + value[3], value[4] ));
637
}
638
printf( "\n" );
639
}
640
printf( " Hash table:\n" );
641
if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
642
for (i = 0; i < count; i++, hash += 2)
643
{
644
entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
645
printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
646
}
647
break;
648
default:
649
printf( "*** Unknown version %u\n", ptr[0] );
650
break;
651
}
652
break;
653
}
654
}
655
656
static UINT get_export_rva( const char *name )
657
{
658
UINT i;
659
const UINT *funcs;
660
const UINT *names;
661
const WORD *ordinals;
662
const IMAGE_EXPORT_DIRECTORY *dir;
663
664
if (!(dir = get_dir( IMAGE_FILE_EXPORT_DIRECTORY ))) return 0;
665
if (!(funcs = RVA( dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD) ))) return 0;
666
names = RVA( dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD) );
667
ordinals = RVA( dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD) );
668
for (i = 0; i < dir->NumberOfNames; i++)
669
if (!strcmp( name, RVA( names[i], 1 )))
670
return funcs[ordinals[i]];
671
return 0;
672
}
673
674
static const char *find_export_from_rva( UINT rva )
675
{
676
UINT i, *func_names;
677
const UINT *funcs;
678
const UINT *names;
679
const WORD *ordinals;
680
const IMAGE_EXPORT_DIRECTORY *dir;
681
const char *ret = NULL;
682
683
if (!(dir = get_dir( IMAGE_FILE_EXPORT_DIRECTORY ))) return "";
684
if (!(funcs = RVA( dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD) ))) return "";
685
names = RVA( dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD) );
686
ordinals = RVA( dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD) );
687
func_names = calloc( dir->NumberOfFunctions, sizeof(*func_names) );
688
689
for (i = 0; i < dir->NumberOfNames; i++) func_names[ordinals[i]] = names[i];
690
for (i = 0; i < dir->NumberOfFunctions; i++)
691
{
692
if (funcs[i] != rva) continue;
693
if (func_names[i]) ret = get_symbol_str( RVA( func_names[i], sizeof(DWORD) ));
694
break;
695
}
696
697
free( func_names );
698
if (!ret && rva == PE_nt_headers->OptionalHeader.AddressOfEntryPoint) return " <EntryPoint>";
699
return ret ? strmake( " (%s)", ret ) : "";
700
}
701
702
static const char *find_import_from_rva( UINT rva )
703
{
704
const IMAGE_IMPORT_DESCRIPTOR *imp;
705
706
if (!(imp = get_dir( IMAGE_FILE_IMPORT_DIRECTORY ))) return "";
707
708
/* check for import thunk */
709
switch (PE_nt_headers->FileHeader.Machine)
710
{
711
case IMAGE_FILE_MACHINE_AMD64:
712
{
713
const BYTE *ptr = RVA( rva, 6 );
714
if (!ptr) return "";
715
if (ptr[0] == 0xff && ptr[1] == 0x25)
716
{
717
rva += 6 + *(int *)(ptr + 2);
718
break;
719
}
720
if (ptr[0] == 0x48 && ptr[1] == 0xff && ptr[2] == 0x25)
721
{
722
rva += 7 + *(int *)(ptr + 3);
723
break;
724
}
725
return "";
726
}
727
case IMAGE_FILE_MACHINE_ARM64:
728
{
729
const UINT *ptr = RVA( rva, sizeof(DWORD) );
730
if ((ptr[0] & 0x9f00001f) == 0x90000010 && /* adrp x16, page */
731
(ptr[1] & 0xffc003ff) == 0xf9400210 && /* ldr x16, [x16, #off] */
732
ptr[2] == 0xd61f0200) /* br x16 */
733
{
734
rva &= ~0xfff;
735
rva += ((ptr[0] & 0x00ffffe0) << 9) + ((ptr[0] & 0x60000000) >> 17);
736
rva += (ptr[1] & 0x003ffc00) >> 7;
737
break;
738
}
739
return "";
740
}
741
default:
742
return "";
743
}
744
745
for ( ; imp->Name && imp->FirstThunk; imp++)
746
{
747
UINT imp_rva = imp->OriginalFirstThunk ? imp->OriginalFirstThunk : imp->FirstThunk;
748
UINT thunk_rva = imp->FirstThunk;
749
750
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
751
{
752
const IMAGE_THUNK_DATA64 *il = RVA( imp_rva, sizeof(DWORD) );
753
for ( ; il && il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
754
{
755
if (thunk_rva != rva) continue;
756
if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
757
return strmake( " (-> #%u)", (WORD)IMAGE_ORDINAL64( il->u1.Ordinal ));
758
else
759
return strmake( " (-> %s)", get_symbol_str( RVA( (DWORD)il->u1.AddressOfData + 2, 1 )));
760
}
761
}
762
else
763
{
764
const IMAGE_THUNK_DATA32 *il = RVA( imp_rva, sizeof(DWORD) );
765
for ( ; il && il->u1.Ordinal; il++, thunk_rva += sizeof(LONG))
766
{
767
if (thunk_rva != rva) continue;
768
if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
769
return strmake( " (-> #%u)", (WORD)IMAGE_ORDINAL32( il->u1.Ordinal ));
770
else
771
return strmake( " (-> %s)", get_symbol_str( RVA( (DWORD)il->u1.AddressOfData + 2, 1 )));
772
}
773
}
774
}
775
return "";
776
}
777
778
static const char *get_function_name( UINT rva )
779
{
780
const char *name = find_export_from_rva( rva );
781
if (!*name) name = find_import_from_rva( rva );
782
return name;
783
}
784
785
static int match_pattern( const BYTE *instr, const BYTE *pattern, int len )
786
{
787
for (int i = 0; i < len; i++) if (pattern[i] && pattern[i] != instr[i]) return 0;
788
return 1;
789
}
790
791
static void dump_export_flags( UINT rva )
792
{
793
switch (PE_nt_headers->FileHeader.Machine)
794
{
795
case IMAGE_FILE_MACHINE_I386:
796
{
797
static const BYTE patterns[][18] =
798
{
799
{ 0xb8, 0, 0, 0, 0, 0xba, 0, 0, 0, 0, 0xff, 0xd2 }, /* >= win10 */
800
{ 0xb8, 0, 0, 0, 0, 0xba, 0, 0, 0, 0, 0xff, 0x12 }, /* winxp */
801
{ 0xb8, 0, 0, 0, 0, 0x64, 0xff, 0x15, 0xc0, 0, 0, 0 }, /* nt */
802
{ 0xb8, 0, 0, 0, 0, 0x8d, 0x54, 0x24, 0x04, 0xcd, 0x2e }, /* nt */
803
{ 0xb8, 0, 0, 0, 0, 0xb9, 0, 0, 0, 0, 0x8d, 0x54, 0x24, 0x04, 0x64, 0xff, 0x15, 0xc0 }, /* vista */
804
{ 0xb8, 0, 0, 0, 0, 0x33, 0xc9, 0x8d, 0x54, 0x24, 0x04, 0x64, 0xff, 0x15, 0xc0 }, /* vista */
805
{ 0xb8, 0, 0, 0, 0, 0xe8, 0, 0, 0, 0, 0x8d, 0x54, 0x24, 0x04, 0x64, 0xff, 0x15, 0xc0 }, /* win8 */
806
{ 0xb8, 0, 0, 0, 0, 0xe8, 0x01, 0, 0, 0, 0xc3, 0x8b, 0xd4, 0x0f, 0x34, 0xc3 }, /* win8 */
807
{ 0xb8, 0, 0, 0, 0, 0xe8, 0x03, 0, 0, 0, 0xc2, 0, 0, 0x8b, 0xd4, 0x0f, 0x34, 0xc3 }, /* win8 */
808
};
809
const BYTE *instr = RVA( rva, 32 );
810
811
if (!instr) break;
812
for (UINT i = 0; i < ARRAY_SIZE(patterns); i++)
813
{
814
if (match_pattern( instr, patterns[i], ARRAY_SIZE(patterns[0]) ))
815
{
816
printf( " [syscall=%04x]", *(UINT *)(instr + 1) );
817
return;
818
}
819
}
820
break;
821
}
822
case IMAGE_FILE_MACHINE_AMD64:
823
{
824
static const BYTE patterns[][20] =
825
{
826
{ 0x4c, 0x8b, 0xd1, 0xb8, 0, 0, 0, 0, 0xf6, 0x04, 0x25, 0x08, 0x03, 0xfe,
827
0x7f, 0x01, 0x75, 0x03, 0x0f, 0x05 }, /* >= win10 */
828
{ 0x4c, 0x8b, 0xd1, 0xb8, 0, 0, 0, 0, 0x0f, 0x05, 0xc3 }, /* < win10 */
829
};
830
const BYTE *instr = RVA( rva, 32 );
831
832
if (!instr) break;
833
for (UINT i = 0; i < ARRAY_SIZE(patterns); i++)
834
{
835
if (match_pattern( instr, patterns[i], ARRAY_SIZE(patterns[0]) ))
836
{
837
printf( " [syscall=%04x]", ((UINT *)instr)[1] );
838
return;
839
}
840
}
841
break;
842
}
843
case IMAGE_FILE_MACHINE_ARM64:
844
{
845
const UINT *instr = RVA( rva, 32 );
846
847
if (!instr) break;
848
if (((instr[0] & 0xffe0001f) == 0xd4000001 && instr[1] == 0xd65f03c0) || /* windows */
849
((instr[0] & 0xffe0001f) == 0xd2800008 && instr[1] == 0xaa1e03e9 && /* wine */
850
instr[3] == 0xf9400210 && instr[4] == 0xd63f0200 && instr[5] == 0xd65f03c0))
851
printf( " [syscall=%04x]", (instr[0] >> 5) & 0xffff );
852
break;
853
}
854
case IMAGE_FILE_MACHINE_ARMNT:
855
{
856
const USHORT *instr = RVA( rva & ~1, 16 );
857
if (!instr) break;
858
if (instr[0] == 0xb40f && (instr[2] & 0x0f00) == 0x0c00 &&
859
((instr[3] == 0xdef8 && instr[4] == 0xb004 && instr[5] == 0x4770) || /* windows */
860
(instr[3] == 0x4673 && instr[6] == 0xb004 && instr[7] == 0x4770))) /* wine */
861
{
862
USHORT imm = ((instr[1] & 0x400) << 1) | (instr[2] & 0xff) | ((instr[2] >> 4) & 0x0700);
863
if ((instr[1] & 0xfbf0) == 0xf240) /* T3 */
864
{
865
imm |= (instr[1] & 0x0f) << 12;
866
printf( " [syscall=%04x]", imm );
867
}
868
else if ((instr[1] & 0xfbf0) == 0xf040) /* T2 */
869
{
870
switch (imm >> 8)
871
{
872
case 0: break;
873
case 1: imm = (imm & 0xff); break;
874
case 2: imm = (imm & 0xff) << 8; break;
875
case 3: imm = (imm & 0xff) | ((imm & 0xff) << 8); break;
876
default: imm = (0x80 | (imm & 0x7f)) << (32 - (imm >> 7)); break;
877
}
878
printf( " [syscall=%04x]", imm );
879
}
880
}
881
break;
882
}
883
default:
884
break;
885
}
886
}
887
888
static void dump_dir_exported_functions(void)
889
{
890
unsigned int size;
891
const IMAGE_EXPORT_DIRECTORY *dir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
892
UINT i, *funcs;
893
const UINT *pFunc;
894
const UINT *pName;
895
const WORD *pOrdl;
896
897
if (!dir) return;
898
899
printf("\n");
900
printf(" Name: %s\n", (const char*)RVA(dir->Name, sizeof(DWORD)));
901
printf(" Characteristics: %08x\n", (UINT)dir->Characteristics);
902
printf(" TimeDateStamp: %08X %s\n",
903
(UINT)dir->TimeDateStamp, get_time_str(dir->TimeDateStamp));
904
printf(" Version: %u.%02u\n", dir->MajorVersion, dir->MinorVersion);
905
printf(" Ordinal base: %u\n", (UINT)dir->Base);
906
printf(" # of functions: %u\n", (UINT)dir->NumberOfFunctions);
907
printf(" # of Names: %u\n", (UINT)dir->NumberOfNames);
908
printf(" Functions RVA: %08X\n", (UINT)dir->AddressOfFunctions);
909
printf(" Ordinals RVA: %08X\n", (UINT)dir->AddressOfNameOrdinals);
910
printf(" Names RVA: %08X\n", (UINT)dir->AddressOfNames);
911
printf("\n");
912
printf(" Entry Pt Ordn Name\n");
913
914
pFunc = RVA(dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD));
915
if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
916
pName = RVA(dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD));
917
pOrdl = RVA(dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD));
918
919
funcs = calloc( dir->NumberOfFunctions, sizeof(*funcs) );
920
if (!funcs) fatal("no memory");
921
922
for (i = 0; i < dir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
923
924
for (i = 0; i < dir->NumberOfFunctions; i++)
925
{
926
if (!pFunc[i]) continue;
927
printf(" %08X %5u ", pFunc[i], (UINT)dir->Base + i);
928
if (funcs[i])
929
printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
930
else
931
printf("<by ordinal>");
932
933
/* check for forwarded function */
934
if ((const char *)RVA(pFunc[i],1) >= (const char *)dir &&
935
(const char *)RVA(pFunc[i],1) < (const char *)dir + size)
936
printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
937
dump_export_flags( pFunc[i] );
938
printf("\n");
939
}
940
free(funcs);
941
printf("\n");
942
}
943
944
945
struct runtime_function_x86_64
946
{
947
UINT BeginAddress;
948
UINT EndAddress;
949
UINT UnwindData;
950
};
951
952
struct runtime_function_armnt
953
{
954
UINT BeginAddress;
955
union {
956
UINT UnwindData;
957
struct {
958
UINT Flag : 2;
959
UINT FunctionLength : 11;
960
UINT Ret : 2;
961
UINT H : 1;
962
UINT Reg : 3;
963
UINT R : 1;
964
UINT L : 1;
965
UINT C : 1;
966
UINT StackAdjust : 10;
967
};
968
};
969
};
970
971
struct runtime_function_arm64
972
{
973
UINT BeginAddress;
974
union
975
{
976
UINT UnwindData;
977
struct
978
{
979
UINT Flag : 2;
980
UINT FunctionLength : 11;
981
UINT RegF : 3;
982
UINT RegI : 4;
983
UINT H : 1;
984
UINT CR : 2;
985
UINT FrameSize : 9;
986
};
987
};
988
};
989
990
union handler_data
991
{
992
struct runtime_function_x86_64 chain;
993
UINT handler;
994
};
995
996
struct opcode
997
{
998
BYTE offset;
999
BYTE code : 4;
1000
BYTE info : 4;
1001
};
1002
1003
struct unwind_info_x86_64
1004
{
1005
BYTE version : 3;
1006
BYTE flags : 5;
1007
BYTE prolog;
1008
BYTE count;
1009
BYTE frame_reg : 4;
1010
BYTE frame_offset : 4;
1011
struct opcode opcodes[1]; /* count entries */
1012
/* followed by union handler_data */
1013
};
1014
1015
struct unwind_info_armnt
1016
{
1017
UINT function_length : 18;
1018
UINT version : 2;
1019
UINT x : 1;
1020
UINT e : 1;
1021
UINT f : 1;
1022
UINT count : 5;
1023
UINT words : 4;
1024
};
1025
1026
struct unwind_info_ext_armnt
1027
{
1028
WORD excount;
1029
BYTE exwords;
1030
BYTE reserved;
1031
};
1032
1033
struct unwind_info_epilogue_armnt
1034
{
1035
UINT offset : 18;
1036
UINT res : 2;
1037
UINT cond : 4;
1038
UINT index : 8;
1039
};
1040
1041
#define UWOP_PUSH_NONVOL 0
1042
#define UWOP_ALLOC_LARGE 1
1043
#define UWOP_ALLOC_SMALL 2
1044
#define UWOP_SET_FPREG 3
1045
#define UWOP_SAVE_NONVOL 4
1046
#define UWOP_SAVE_NONVOL_FAR 5
1047
#define UWOP_EPILOG 6
1048
#define UWOP_SAVE_XMM128 8
1049
#define UWOP_SAVE_XMM128_FAR 9
1050
#define UWOP_PUSH_MACHFRAME 10
1051
1052
#define UNW_FLAG_EHANDLER 1
1053
#define UNW_FLAG_UHANDLER 2
1054
#define UNW_FLAG_CHAININFO 4
1055
1056
static void dump_c_exception_data( unsigned int rva )
1057
{
1058
unsigned int i;
1059
const struct
1060
{
1061
UINT count;
1062
struct
1063
{
1064
UINT begin;
1065
UINT end;
1066
UINT handler;
1067
UINT target;
1068
} rec[];
1069
} *table = RVA( rva, sizeof(*table) );
1070
1071
if (!table) return;
1072
printf( " C exception data at %08x count %u\n", rva, table->count );
1073
for (i = 0; i < table->count; i++)
1074
printf( " %u: %08x-%08x handler %08x target %08x\n", i,
1075
table->rec[i].begin, table->rec[i].end, table->rec[i].handler, table->rec[i].target );
1076
}
1077
1078
static void dump_cxx_exception_data( unsigned int rva, unsigned int func_rva )
1079
{
1080
unsigned int i, j, flags = 0;
1081
const unsigned int *ptr = RVA( rva, sizeof(*ptr) );
1082
1083
const struct
1084
{
1085
int prev;
1086
UINT handler;
1087
} *unwind_info;
1088
1089
const struct
1090
{
1091
int start;
1092
int end;
1093
int catch;
1094
int catchblock_count;
1095
UINT catchblock;
1096
} *tryblock;
1097
1098
const struct
1099
{
1100
UINT flags;
1101
UINT type_info;
1102
int offset;
1103
UINT handler;
1104
UINT frame;
1105
} *catchblock;
1106
1107
const struct
1108
{
1109
UINT flags;
1110
UINT type_info;
1111
int offset;
1112
UINT handler;
1113
} *catchblock32;
1114
1115
const struct
1116
{
1117
UINT ip;
1118
int state;
1119
} *ipmap;
1120
1121
const struct
1122
{
1123
UINT magic;
1124
UINT unwind_count;
1125
UINT unwind_table;
1126
UINT tryblock_count;
1127
UINT tryblock;
1128
UINT ipmap_count;
1129
UINT ipmap;
1130
int unwind_help;
1131
UINT expect_list;
1132
UINT flags;
1133
} *func;
1134
1135
if (!ptr || !*ptr) return;
1136
rva = *ptr;
1137
if (!(func = RVA( rva, sizeof(*func) ))) return;
1138
if (func->magic < 0x19930520 || func->magic > 0x19930522) return;
1139
if (func->magic > 0x19930521) flags = func->flags;
1140
printf( " C++ exception data at %08x magic %08x", rva, func->magic );
1141
if (flags & 1) printf( " sync" );
1142
if (flags & 4) printf( " noexcept" );
1143
printf( "\n" );
1144
printf( " unwind help %+d\n", func->unwind_help );
1145
if (func->magic > 0x19930520 && func->expect_list)
1146
printf( " expect_list %08x\n", func->expect_list );
1147
if (func->unwind_count)
1148
{
1149
printf( " unwind table at %08x count %u\n", func->unwind_table, func->unwind_count );
1150
if ((unwind_info = RVA( func->unwind_table, func->unwind_count * sizeof(*unwind_info) )))
1151
{
1152
for (i = 0; i < func->unwind_count; i++)
1153
printf( " %u: prev %d func %08x\n", i,
1154
unwind_info[i].prev, unwind_info[i].handler );
1155
}
1156
}
1157
if (func->tryblock_count)
1158
{
1159
printf( " try table at %08x count %u\n", func->tryblock, func->tryblock_count );
1160
if ((tryblock = RVA( func->tryblock, func->tryblock_count * sizeof(*tryblock) )))
1161
{
1162
for (i = 0; i < func->tryblock_count; i++)
1163
{
1164
catchblock = RVA( tryblock[i].catchblock, sizeof(*catchblock) );
1165
catchblock32 = RVA( tryblock[i].catchblock, sizeof(*catchblock32) );
1166
printf( " %d: start %d end %d catch %d count %u\n", i,
1167
tryblock[i].start, tryblock[i].end, tryblock[i].catch,
1168
tryblock[i].catchblock_count );
1169
for (j = 0; j < tryblock[i].catchblock_count; j++)
1170
{
1171
const char *type = "<none>";
1172
1173
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1174
{
1175
if (catchblock[j].type_info) type = RVA( catchblock[j].type_info + 16, 1 );
1176
printf( " %d: flags %x offset %+d handler %08x frame %x type %s\n", j,
1177
catchblock[j].flags, catchblock[j].offset,
1178
catchblock[j].handler, catchblock[j].frame, type );
1179
}
1180
else
1181
{
1182
if (catchblock32[j].type_info) type = RVA( catchblock32[j].type_info + 8, 1 );
1183
printf( " %d: flags %x offset %+d handler %08x type %s\n", j,
1184
catchblock32[j].flags, catchblock32[j].offset,
1185
catchblock32[j].handler, type );
1186
}
1187
}
1188
}
1189
}
1190
}
1191
if (func->ipmap_count)
1192
{
1193
printf( " ip map at %08x count %u\n", func->ipmap, func->ipmap_count );
1194
if ((ipmap = RVA( func->ipmap, func->ipmap_count * sizeof(*ipmap) )))
1195
{
1196
for (i = 0; i < func->ipmap_count; i++)
1197
printf( " %u: ip %08x state %d\n", i, ipmap[i].ip, ipmap[i].state );
1198
}
1199
}
1200
}
1201
1202
static UINT v4_decode_uint( const BYTE **b )
1203
{
1204
UINT ret;
1205
const BYTE *p = *b;
1206
1207
if ((*p & 1) == 0)
1208
{
1209
ret = p[0] >> 1;
1210
p += 1;
1211
}
1212
else if ((*p & 3) == 1)
1213
{
1214
ret = (p[0] >> 2) + (p[1] << 6);
1215
p += 2;
1216
}
1217
else if ((*p & 7) == 3)
1218
{
1219
ret = (p[0] >> 3) + (p[1] << 5) + (p[2] << 13);
1220
p += 3;
1221
}
1222
else if ((*p & 15) == 7)
1223
{
1224
ret = (p[0] >> 4) + (p[1] << 4) + (p[2] << 12) + (p[3] << 20);
1225
p += 4;
1226
}
1227
else
1228
{
1229
ret = 0;
1230
p += 5;
1231
}
1232
1233
*b = p;
1234
return ret;
1235
}
1236
1237
static UINT v4_read_rva( const BYTE **b )
1238
{
1239
UINT ret = *(UINT *)*b;
1240
*b += sizeof(UINT);
1241
return ret;
1242
}
1243
1244
#define FUNC_DESCR_IS_CATCH 0x01
1245
#define FUNC_DESCR_IS_SEPARATED 0x02
1246
#define FUNC_DESCR_BBT 0x04
1247
#define FUNC_DESCR_UNWIND_MAP 0x08
1248
#define FUNC_DESCR_TRYBLOCK_MAP 0x10
1249
#define FUNC_DESCR_EHS 0x20
1250
#define FUNC_DESCR_NO_EXCEPT 0x40
1251
#define FUNC_DESCR_RESERVED 0x80
1252
1253
#define CATCHBLOCK_FLAGS 0x01
1254
#define CATCHBLOCK_TYPE_INFO 0x02
1255
#define CATCHBLOCK_OFFSET 0x04
1256
#define CATCHBLOCK_SEPARATED 0x08
1257
#define CATCHBLOCK_RET_ADDR_MASK 0x30
1258
#define CATCHBLOCK_RET_ADDR 0x10
1259
#define CATCHBLOCK_TWO_RET_ADDRS 0x20
1260
1261
static void dump_cxx_exception_data_v4( unsigned int rva, unsigned int func_rva )
1262
{
1263
const unsigned int *ptr = RVA( rva, sizeof(*ptr) );
1264
const BYTE *p;
1265
BYTE flags;
1266
UINT unwind_map = 0, tryblock_map = 0, ip_map, count, i, j, k;
1267
1268
if (!ptr || !*ptr) return;
1269
rva = *ptr;
1270
if (!(p = RVA( rva, 1 ))) return;
1271
flags = *p++;
1272
printf( " C++ v4 exception data at %08x", rva );
1273
if (flags & FUNC_DESCR_BBT) printf( " bbt %08x", v4_decode_uint( &p ));
1274
if (flags & FUNC_DESCR_UNWIND_MAP) unwind_map = v4_read_rva( &p );
1275
if (flags & FUNC_DESCR_TRYBLOCK_MAP) tryblock_map = v4_read_rva( &p );
1276
ip_map = v4_read_rva(&p);
1277
if (flags & FUNC_DESCR_IS_CATCH) printf( " frame %08x", v4_decode_uint( &p ));
1278
if (flags & FUNC_DESCR_EHS) printf( " sync" );
1279
if (flags & FUNC_DESCR_NO_EXCEPT) printf( " noexcept" );
1280
printf( "\n" );
1281
1282
if (unwind_map)
1283
{
1284
int *offsets;
1285
const BYTE *start, *p = RVA( unwind_map, 1 );
1286
count = v4_decode_uint( &p );
1287
printf( " unwind map at %08x count %u\n", unwind_map, count );
1288
offsets = calloc( count, sizeof(*offsets) );
1289
start = p;
1290
for (i = 0; i < count; i++)
1291
{
1292
UINT handler, object, type;
1293
int offset = p - start, off, prev = -2;
1294
1295
offsets[i] = offset;
1296
type = v4_decode_uint( &p );
1297
off = (type >> 2);
1298
if (off > offset) prev = -1;
1299
else for (j = 0; j < i; j++) if (offsets[j] == offset - off) prev = j;
1300
1301
switch (type & 3)
1302
{
1303
case 0:
1304
printf( " %u: prev %d no handler\n", i, prev );
1305
break;
1306
case 1:
1307
handler = v4_read_rva( &p );
1308
object = v4_decode_uint( &p );
1309
printf( " %u: prev %d dtor obj handler %08x obj %+d\n",
1310
i, prev, handler, (int)object );
1311
break;
1312
case 2:
1313
handler = v4_read_rva( &p );
1314
object = v4_decode_uint( &p );
1315
printf( " %u: prev %d dtor ptr handler %08x obj %+d\n",
1316
i, prev, handler, (int)object );
1317
break;
1318
case 3:
1319
handler = v4_read_rva( &p );
1320
printf( " %u: prev %d handler %08x\n", i, prev, handler );
1321
break;
1322
}
1323
}
1324
free( offsets );
1325
}
1326
1327
if (tryblock_map)
1328
{
1329
const BYTE *p = RVA( tryblock_map, 1 );
1330
count = v4_decode_uint( &p );
1331
printf( " tryblock map at %08x count %u\n", tryblock_map, count );
1332
for (i = 0; i < count; i++)
1333
{
1334
int start = v4_decode_uint( &p );
1335
int end = v4_decode_uint( &p );
1336
int catch = v4_decode_uint( &p );
1337
UINT catchblock = v4_read_rva( &p );
1338
1339
printf( " %u: start %d end %d catch %d\n", i, start, end, catch );
1340
if (catchblock)
1341
{
1342
UINT cont[3];
1343
const BYTE *p = RVA( catchblock, 1 );
1344
UINT count2 = v4_decode_uint( &p );
1345
for (j = 0; j < count2; j++)
1346
{
1347
BYTE flags = *p++;
1348
printf( " %u:", j );
1349
if (flags & CATCHBLOCK_FLAGS)
1350
printf( " flags %08x", v4_decode_uint( &p ));
1351
if (flags & CATCHBLOCK_TYPE_INFO)
1352
printf( " type %08x", v4_read_rva( &p ));
1353
if (flags & CATCHBLOCK_OFFSET)
1354
printf( " offset %08x", v4_decode_uint( &p ));
1355
printf( " handler %08x", v4_read_rva( &p ));
1356
for (k = 0; k < ((flags >> 4) & 3); k++)
1357
if (flags & CATCHBLOCK_SEPARATED) cont[k] = v4_read_rva( &p );
1358
else cont[k] = func_rva + v4_decode_uint( &p );
1359
if (k == 1) printf( " cont %08x", cont[0] );
1360
else if (k == 2) printf( " cont %08x,%08x", cont[0], cont[1] );
1361
printf( "\n" );
1362
}
1363
}
1364
}
1365
}
1366
1367
if (ip_map && (flags & FUNC_DESCR_IS_SEPARATED))
1368
{
1369
const BYTE *p = RVA( ip_map, 1 );
1370
1371
count = v4_decode_uint( &p );
1372
printf( " separated ip map at %08x count %u\n", ip_map, count );
1373
for (i = 0; i < count; i++)
1374
{
1375
UINT ip = v4_read_rva( &p );
1376
UINT map = v4_read_rva( &p );
1377
1378
if (map)
1379
{
1380
UINT state, count2;
1381
const BYTE *p = RVA( map, 1 );
1382
1383
count2 = v4_decode_uint( &p );
1384
printf( " %u: start %08x map %08x\n", i, ip, map );
1385
for (j = 0; j < count2; j++)
1386
{
1387
ip += v4_decode_uint( &p );
1388
state = v4_decode_uint( &p );
1389
printf( " %u: ip %08x state %d\n", i, ip, state );
1390
}
1391
}
1392
}
1393
}
1394
else if (ip_map)
1395
{
1396
UINT ip = func_rva, state;
1397
const BYTE *p = RVA( ip_map, 1 );
1398
1399
count = v4_decode_uint( &p );
1400
printf( " ip map at %08x count %u\n", ip_map, count );
1401
for (i = 0; i < count; i++)
1402
{
1403
ip += v4_decode_uint( &p );
1404
state = v4_decode_uint( &p );
1405
printf( " %u: ip %08x state %d\n", i, ip, state );
1406
}
1407
}
1408
}
1409
1410
static void dump_exception_data( unsigned int rva, unsigned int func_rva, const char *name )
1411
{
1412
if (strstr( name, "__C_specific_handler" )) dump_c_exception_data( rva );
1413
if (strstr( name, "__CxxFrameHandler4" )) dump_cxx_exception_data_v4( rva, func_rva );
1414
else dump_cxx_exception_data( rva, func_rva );
1415
}
1416
1417
static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
1418
{
1419
static const char * const reg_names[16] =
1420
{ "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1421
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
1422
1423
const union handler_data *handler_data;
1424
const struct unwind_info_x86_64 *info;
1425
unsigned int i, count;
1426
1427
printf( "\nFunction %08x-%08x:%s\n", function->BeginAddress, function->EndAddress,
1428
find_export_from_rva( function->BeginAddress ));
1429
if (function->UnwindData & 1)
1430
{
1431
const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
1432
printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
1433
return;
1434
}
1435
info = RVA( function->UnwindData, sizeof(*info) );
1436
1437
if (!info)
1438
{
1439
printf( " no unwind info (%x)\n", function->UnwindData );
1440
return;
1441
}
1442
printf( " unwind info at %08x\n", function->UnwindData );
1443
if (info->version > 2)
1444
{
1445
printf( " *** unknown version %u\n", info->version );
1446
return;
1447
}
1448
printf( " flags %x", info->flags );
1449
if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
1450
if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
1451
if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
1452
printf( "\n prolog 0x%x bytes\n", info->prolog );
1453
1454
if (info->frame_reg)
1455
printf( " frame register %s offset 0x%x(%%rsp)\n",
1456
reg_names[info->frame_reg], info->frame_offset * 16 );
1457
1458
for (i = 0; i < info->count; i++)
1459
{
1460
if (info->opcodes[i].code == UWOP_EPILOG)
1461
{
1462
i++;
1463
continue;
1464
}
1465
printf( " 0x%02x: ", info->opcodes[i].offset );
1466
switch (info->opcodes[i].code)
1467
{
1468
case UWOP_PUSH_NONVOL:
1469
printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
1470
break;
1471
case UWOP_ALLOC_LARGE:
1472
if (info->opcodes[i].info)
1473
{
1474
count = *(const UINT *)&info->opcodes[i+1];
1475
i += 2;
1476
}
1477
else
1478
{
1479
count = *(const USHORT *)&info->opcodes[i+1] * 8;
1480
i++;
1481
}
1482
printf( "sub $0x%x,%%rsp\n", count );
1483
break;
1484
case UWOP_ALLOC_SMALL:
1485
count = (info->opcodes[i].info + 1) * 8;
1486
printf( "sub $0x%x,%%rsp\n", count );
1487
break;
1488
case UWOP_SET_FPREG:
1489
printf( "lea 0x%x(%%rsp),%s\n",
1490
info->frame_offset * 16, reg_names[info->frame_reg] );
1491
break;
1492
case UWOP_SAVE_NONVOL:
1493
count = *(const USHORT *)&info->opcodes[i+1] * 8;
1494
printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
1495
i++;
1496
break;
1497
case UWOP_SAVE_NONVOL_FAR:
1498
count = *(const UINT *)&info->opcodes[i+1];
1499
printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
1500
i += 2;
1501
break;
1502
case UWOP_SAVE_XMM128:
1503
count = *(const USHORT *)&info->opcodes[i+1] * 16;
1504
printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
1505
i++;
1506
break;
1507
case UWOP_SAVE_XMM128_FAR:
1508
count = *(const UINT *)&info->opcodes[i+1];
1509
printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
1510
i += 2;
1511
break;
1512
case UWOP_PUSH_MACHFRAME:
1513
printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
1514
break;
1515
default:
1516
printf( "*** unknown code %u\n", info->opcodes[i].code );
1517
break;
1518
}
1519
}
1520
1521
if (info->version == 2 && info->opcodes[0].code == UWOP_EPILOG) /* print the epilogs */
1522
{
1523
unsigned int end = function->EndAddress;
1524
unsigned int size = info->opcodes[0].offset;
1525
1526
printf( " epilog 0x%x bytes\n", size );
1527
if (info->opcodes[0].info) printf( " at %08x-%08x\n", end - size, end );
1528
for (i = 1; i < info->count && info->opcodes[i].code == UWOP_EPILOG; i++)
1529
{
1530
unsigned int offset = (info->opcodes[i].info << 8) + info->opcodes[i].offset;
1531
if (!offset) break;
1532
printf( " at %08x-%08x\n", end - offset, end - offset + size );
1533
}
1534
}
1535
1536
handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
1537
if (info->flags & UNW_FLAG_CHAININFO)
1538
{
1539
printf( " -> function %08x-%08x\n",
1540
handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
1541
return;
1542
}
1543
if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
1544
{
1545
UINT rva = function->UnwindData + ((const char *)(&handler_data->handler + 1) - (const char *)info);
1546
const char *name = get_function_name( handler_data->handler );
1547
1548
printf( " handler %08x%s data at %08x\n", handler_data->handler, name, rva );
1549
dump_exception_data( rva, function->BeginAddress, name );
1550
}
1551
}
1552
1553
static const BYTE armnt_code_lengths[256] =
1554
{
1555
/* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1556
/* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1557
/* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1558
/* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1559
/* 80 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1560
/* a0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1561
/* c0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1562
/* e0 */ 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,4,3,4,1,1,1,1,1
1563
};
1564
1565
static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
1566
{
1567
const struct unwind_info_armnt *info;
1568
const struct unwind_info_ext_armnt *infoex;
1569
const struct unwind_info_epilogue_armnt *infoepi = NULL;
1570
unsigned int rva;
1571
WORD i, count = 0, words = 0;
1572
1573
if (fnc->Flag)
1574
{
1575
char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
1576
WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
1577
const char *pfx = " ... ";
1578
1579
printf( "\nFunction %08x-%08x: flag=%u ret=%u H=%u reg=%u R=%u L=%u C=%u%s\n",
1580
fnc->BeginAddress & ~1, (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2,
1581
fnc->Flag, fnc->Ret, fnc->H, fnc->Reg, fnc->R, fnc->L, fnc->C,
1582
find_export_from_rva( fnc->BeginAddress ));
1583
1584
if (fnc->StackAdjust >= 0x03f4)
1585
{
1586
pf = fnc->StackAdjust & 0x04;
1587
ef = fnc->StackAdjust & 0x08;
1588
stack = (fnc->StackAdjust & 3) + 1;
1589
}
1590
1591
if (!fnc->R || pf)
1592
{
1593
int first = 4, last = fnc->Reg + 4;
1594
if (pf)
1595
{
1596
first = (~fnc->StackAdjust) & 3;
1597
if (fnc->R)
1598
last = 3;
1599
}
1600
if (first == last)
1601
sprintf(intregs, "r%u", first);
1602
else
1603
sprintf(intregs, "r%u-r%u", first, last);
1604
fpoffset = last + 1 - first;
1605
}
1606
1607
if (!fnc->R || ef)
1608
{
1609
int first = 4, last = fnc->Reg + 4;
1610
if (ef)
1611
{
1612
first = (~fnc->StackAdjust) & 3;
1613
if (fnc->R)
1614
last = 3;
1615
}
1616
if (first == last)
1617
sprintf(intregspop, "r%u", first);
1618
else
1619
sprintf(intregspop, "r%u-r%u", first, last);
1620
}
1621
1622
if (fnc->C)
1623
{
1624
if (intregs[0])
1625
strcat(intregs, ", ");
1626
if (intregspop[0])
1627
strcat(intregspop, ", ");
1628
strcat(intregs, "r11");
1629
strcat(intregspop, "r11");
1630
}
1631
if (fnc->L)
1632
{
1633
if (intregs[0])
1634
strcat(intregs, ", ");
1635
strcat(intregs, "lr");
1636
1637
if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
1638
strcat(intregspop, ", ");
1639
if (fnc->Ret != 0)
1640
strcat(intregspop, "lr");
1641
else if (!fnc->H)
1642
strcat(intregspop, "pc");
1643
}
1644
1645
if (fnc->R)
1646
{
1647
if (fnc->Reg)
1648
sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
1649
else
1650
strcpy(vfpregs, "d8");
1651
}
1652
1653
printf( " Prologue:\n" );
1654
if (fnc->Flag == 1) {
1655
if (fnc->H)
1656
printf( "%s push {r0-r3}\n", pfx );
1657
1658
if (intregs[0])
1659
printf( "%s push {%s}\n", pfx, intregs );
1660
1661
if (fnc->C && fpoffset == 0)
1662
printf( "%s mov r11, sp\n", pfx );
1663
else if (fnc->C)
1664
printf( "%s add r11, sp, #%d\n", pfx, fpoffset * 4 );
1665
1666
if (fnc->R && fnc->Reg != 0x07)
1667
printf( "%s vpush {%s}\n", pfx, vfpregs );
1668
1669
if (stack && !pf)
1670
printf( "%s sub sp, sp, #%d\n", pfx, stack * 4 );
1671
}
1672
1673
if (fnc->Ret == 3)
1674
return;
1675
printf( " Epilogue:\n" );
1676
1677
if (stack && !ef)
1678
printf( "%s add sp, sp, #%d\n", pfx, stack * 4 );
1679
1680
if (fnc->R && fnc->Reg != 0x07)
1681
printf( "%s vpop {%s}\n", pfx, vfpregs );
1682
1683
if (intregspop[0])
1684
printf( "%s pop {%s}\n", pfx, intregspop );
1685
1686
if (fnc->H && !(fnc->L && fnc->Ret == 0))
1687
printf( "%s add sp, sp, #16\n", pfx );
1688
else if (fnc->H && (fnc->L && fnc->Ret == 0))
1689
printf( "%s ldr pc, [sp], #20\n", pfx );
1690
1691
if (fnc->Ret == 1)
1692
printf( "%s bx <reg>\n", pfx );
1693
else if (fnc->Ret == 2)
1694
printf( "%s b <address>\n", pfx );
1695
1696
return;
1697
}
1698
1699
info = RVA( fnc->UnwindData, sizeof(*info) );
1700
rva = fnc->UnwindData + sizeof(*info);
1701
count = info->count;
1702
words = info->words;
1703
1704
printf( "\nFunction %08x-%08x: ver=%u X=%u E=%u F=%u%s\n", fnc->BeginAddress & ~1,
1705
(fnc->BeginAddress & ~1) + info->function_length * 2,
1706
info->version, info->x, info->e, info->f, find_export_from_rva( fnc->BeginAddress | 1 ));
1707
1708
if (!info->count && !info->words)
1709
{
1710
infoex = RVA( rva, sizeof(*infoex) );
1711
rva = rva + sizeof(*infoex);
1712
count = infoex->excount;
1713
words = infoex->exwords;
1714
}
1715
1716
if (!info->e)
1717
{
1718
infoepi = RVA( rva, count * sizeof(*infoepi) );
1719
rva = rva + count * sizeof(*infoepi);
1720
}
1721
1722
if (words)
1723
{
1724
const unsigned int *codes;
1725
BYTE b, *bytes;
1726
BOOL inepilogue = FALSE;
1727
1728
codes = RVA( rva, words * sizeof(*codes) );
1729
rva = rva + words * sizeof(*codes);
1730
bytes = (BYTE*)codes;
1731
1732
printf( " Prologue:\n" );
1733
for (b = 0; b < words * sizeof(*codes); b++)
1734
{
1735
BYTE code = bytes[b];
1736
BYTE len = armnt_code_lengths[code];
1737
1738
if (info->e && b == count)
1739
{
1740
printf( " Epilogue:\n" );
1741
inepilogue = TRUE;
1742
}
1743
else if (!info->e && infoepi)
1744
{
1745
for (i = 0; i < count; i++)
1746
if (b == infoepi[i].index)
1747
{
1748
printf( " Epilogue %u at %08x: (res=%x cond=%x)\n", i,
1749
(fnc->BeginAddress & ~1) + infoepi[i].offset * 2,
1750
infoepi[i].res, infoepi[i].cond );
1751
inepilogue = TRUE;
1752
}
1753
}
1754
1755
printf( " ");
1756
for (i = 0; i < len; i++)
1757
printf( " %02x", bytes[b+i] );
1758
printf( " %*s", 3 * (3 - len), "" );
1759
1760
if (code == 0x00)
1761
printf( "\n" );
1762
else if (code <= 0x7f)
1763
printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1764
else if (code <= 0xbf)
1765
{
1766
WORD excode, f;
1767
BOOL first = TRUE;
1768
BYTE excodes = bytes[++b];
1769
1770
excode = (code << 8) | excodes;
1771
printf( "%s {", inepilogue ? "pop" : "push" );
1772
1773
for (f = 0; f <= 12; f++)
1774
{
1775
if ((excode >> f) & 1)
1776
{
1777
printf( "%sr%u", first ? "" : ", ", f );
1778
first = FALSE;
1779
}
1780
}
1781
1782
if (excode & 0x2000)
1783
printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1784
1785
printf( "}\n" );
1786
}
1787
else if (code <= 0xcf)
1788
if (inepilogue)
1789
printf( "mov sp, r%u\n", code & 0x0f );
1790
else
1791
printf( "mov r%u, sp\n", code & 0x0f );
1792
else if (code <= 0xd3)
1793
printf( "%s {r4-r%u}\n", inepilogue ? "pop" : "push", (code & 0x03) + 4 );
1794
else if (code <= 0xd4)
1795
printf( "%s {r4, %s}\n", inepilogue ? "pop" : "push", inepilogue ? "pc" : "lr" );
1796
else if (code <= 0xd7)
1797
printf( "%s {r4-r%u, %s}\n", inepilogue ? "pop" : "push", (code & 0x03) + 4, inepilogue ? "pc" : "lr" );
1798
else if (code <= 0xdb)
1799
printf( "%s {r4-r%u}\n", inepilogue ? "pop" : "push", (code & 0x03) + 8 );
1800
else if (code <= 0xdf)
1801
printf( "%s {r4-r%u, %s}\n", inepilogue ? "pop" : "push", (code & 0x03) + 8, inepilogue ? "pc" : "lr" );
1802
else if (code <= 0xe7)
1803
printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1804
else if (code <= 0xeb)
1805
{
1806
WORD excode;
1807
BYTE excodes = bytes[++b];
1808
1809
excode = (code << 8) | excodes;
1810
printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1811
}
1812
else if (code <= 0xed)
1813
{
1814
WORD excode, f;
1815
BOOL first = TRUE;
1816
BYTE excodes = bytes[++b];
1817
1818
excode = (code << 8) | excodes;
1819
printf( "%s {", inepilogue ? "pop" : "push" );
1820
1821
for (f = 0; f < 8; f++)
1822
{
1823
if ((excode >> f) & 1)
1824
{
1825
printf( "%sr%u", first ? "" : ", ", f );
1826
first = FALSE;
1827
}
1828
}
1829
1830
if (excode & 0x0100)
1831
printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1832
1833
printf( "}\n" );
1834
}
1835
else if (code == 0xee)
1836
{
1837
BYTE excodes = bytes[++b];
1838
if (excodes == 0x01)
1839
printf( "MSFT_OP_MACHINE_FRAME\n");
1840
else if (excodes == 0x02)
1841
printf( "MSFT_OP_CONTEXT\n");
1842
else
1843
printf( "MSFT opcode %u\n", excodes );
1844
}
1845
else if (code == 0xef)
1846
{
1847
WORD excode;
1848
BYTE excodes = bytes[++b];
1849
1850
if (excodes <= 0x0f)
1851
{
1852
excode = (code << 8) | excodes;
1853
if (inepilogue)
1854
printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1855
else
1856
printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1857
}
1858
else
1859
printf( "unknown 32\n" );
1860
}
1861
else if (code <= 0xf4)
1862
printf( "unknown\n" );
1863
else if (code <= 0xf6)
1864
{
1865
WORD excode, offset = (code == 0xf6) ? 16 : 0;
1866
BYTE excodes = bytes[++b];
1867
1868
excode = (code << 8) | excodes;
1869
printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1870
((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1871
}
1872
else if (code <= 0xf7)
1873
{
1874
unsigned int excode;
1875
BYTE excodes[2];
1876
1877
excodes[0] = bytes[++b];
1878
excodes[1] = bytes[++b];
1879
excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1880
printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1881
}
1882
else if (code <= 0xf8)
1883
{
1884
unsigned int excode;
1885
BYTE excodes[3];
1886
1887
excodes[0] = bytes[++b];
1888
excodes[1] = bytes[++b];
1889
excodes[2] = bytes[++b];
1890
excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1891
printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1892
}
1893
else if (code <= 0xf9)
1894
{
1895
unsigned int excode;
1896
BYTE excodes[2];
1897
1898
excodes[0] = bytes[++b];
1899
excodes[1] = bytes[++b];
1900
excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1901
printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1902
}
1903
else if (code <= 0xfa)
1904
{
1905
unsigned int excode;
1906
BYTE excodes[3];
1907
1908
excodes[0] = bytes[++b];
1909
excodes[1] = bytes[++b];
1910
excodes[2] = bytes[++b];
1911
excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1912
printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1913
}
1914
else if (code <= 0xfb)
1915
printf( "nop\n" );
1916
else if (code <= 0xfc)
1917
printf( "nop.w\n" );
1918
else if (code <= 0xfd)
1919
{
1920
printf( "(end) nop\n" );
1921
inepilogue = TRUE;
1922
}
1923
else if (code <= 0xfe)
1924
{
1925
printf( "(end) nop.w\n" );
1926
inepilogue = TRUE;
1927
}
1928
else
1929
{
1930
printf( "end\n" );
1931
inepilogue = TRUE;
1932
}
1933
}
1934
}
1935
1936
if (info->x)
1937
{
1938
const unsigned int *handler;
1939
const char *name;
1940
1941
handler = RVA( rva, sizeof(*handler) );
1942
rva = rva + sizeof(*handler);
1943
name = get_function_name( *handler );
1944
printf( " handler %08x%s data at %08x\n", *handler, name, rva );
1945
dump_exception_data( rva, fnc->BeginAddress & ~1, name );
1946
}
1947
}
1948
1949
struct unwind_info_arm64
1950
{
1951
UINT function_length : 18;
1952
UINT version : 2;
1953
UINT x : 1;
1954
UINT e : 1;
1955
UINT epilog : 5;
1956
UINT codes : 5;
1957
};
1958
1959
struct unwind_info_ext_arm64
1960
{
1961
WORD epilog;
1962
BYTE codes;
1963
BYTE reserved;
1964
};
1965
1966
struct unwind_info_epilog_arm64
1967
{
1968
UINT offset : 18;
1969
UINT res : 4;
1970
UINT index : 10;
1971
};
1972
1973
static const BYTE code_lengths[256] =
1974
{
1975
/* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1976
/* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1977
/* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1978
/* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1979
/* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1980
/* a0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1981
/* c0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1982
/* e0 */ 4,1,2,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1983
};
1984
1985
static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1986
{
1987
unsigned int i, j;
1988
1989
for (i = 0; i < count; i += code_lengths[ptr[i]])
1990
{
1991
BYTE len = code_lengths[ptr[i]];
1992
unsigned int val = ptr[i];
1993
for (j = 1; j < len; j++) val = val * 0x100 + ptr[i + j];
1994
1995
printf( " %04x: ", i );
1996
for (j = 0; j < 4; j++)
1997
if (j < len) printf( "%02x ", ptr[i+j] );
1998
else printf( " " );
1999
2000
if (ptr[i] < 0x20) /* alloc_s */
2001
{
2002
printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
2003
}
2004
else if (ptr[i] < 0x40) /* save_r19r20_x */
2005
{
2006
printf( "stp x19,x20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
2007
}
2008
else if (ptr[i] < 0x80) /* save_fplr */
2009
{
2010
printf( "stp x29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
2011
}
2012
else if (ptr[i] < 0xc0) /* save_fplr_x */
2013
{
2014
printf( "stp x29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
2015
}
2016
else if (ptr[i] < 0xc8) /* alloc_m */
2017
{
2018
printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
2019
}
2020
else if (ptr[i] < 0xcc) /* save_regp */
2021
{
2022
int reg = 19 + ((val >> 6) & 0xf);
2023
printf( "stp x%u,x%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
2024
}
2025
else if (ptr[i] < 0xd0) /* save_regp_x */
2026
{
2027
int reg = 19 + ((val >> 6) & 0xf);
2028
printf( "stp x%u,x%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
2029
}
2030
else if (ptr[i] < 0xd4) /* save_reg */
2031
{
2032
int reg = 19 + ((val >> 6) & 0xf);
2033
printf( "str x%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
2034
}
2035
else if (ptr[i] < 0xd6) /* save_reg_x */
2036
{
2037
int reg = 19 + ((val >> 5) & 0xf);
2038
printf( "str x%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
2039
}
2040
else if (ptr[i] < 0xd8) /* save_lrpair */
2041
{
2042
int reg = 19 + 2 * ((val >> 6) & 0x7);
2043
printf( "stp x%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
2044
}
2045
else if (ptr[i] < 0xda) /* save_fregp */
2046
{
2047
int reg = 8 + ((val >> 6) & 0x7);
2048
printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
2049
}
2050
else if (ptr[i] < 0xdc) /* save_fregp_x */
2051
{
2052
int reg = 8 + ((val >> 6) & 0x7);
2053
printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
2054
}
2055
else if (ptr[i] < 0xde) /* save_freg */
2056
{
2057
int reg = 8 + ((val >> 6) & 0x7);
2058
printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
2059
}
2060
else if (ptr[i] == 0xde) /* save_freg_x */
2061
{
2062
int reg = 8 + ((val >> 5) & 0x7);
2063
printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
2064
}
2065
else if (ptr[i] == 0xe0) /* alloc_l */
2066
{
2067
printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
2068
}
2069
else if (ptr[i] == 0xe1) /* set_fp */
2070
{
2071
printf( "mov x29,sp\n" );
2072
}
2073
else if (ptr[i] == 0xe2) /* add_fp */
2074
{
2075
printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
2076
}
2077
else if (ptr[i] == 0xe3) /* nop */
2078
{
2079
printf( "nop\n" );
2080
}
2081
else if (ptr[i] == 0xe4) /* end */
2082
{
2083
printf( "end\n" );
2084
}
2085
else if (ptr[i] == 0xe5) /* end_c */
2086
{
2087
printf( "end_c\n" );
2088
}
2089
else if (ptr[i] == 0xe6) /* save_next */
2090
{
2091
printf( "save_next\n" );
2092
}
2093
else if (ptr[i] == 0xe7) /* save_any_reg */
2094
{
2095
char reg = "xdq?"[(val >> 6) & 3];
2096
int num = (val >> 8) & 0x1f;
2097
if (val & 0x4000)
2098
printf( "stp %c%u,%c%u", reg, num, reg, num + 1 );
2099
else
2100
printf( "str %c%u,", reg, num );
2101
if (val & 0x2000)
2102
printf( "[sp,#-%#x]!\n", 16 * (val & 0x3f) + 16 );
2103
else
2104
printf( "[sp,#%#x]\n", (val & 0x3f) * ((val & 0x4080) ? 16 : 8) );
2105
}
2106
else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
2107
{
2108
printf( "MSFT_OP_TRAP_FRAME\n" );
2109
}
2110
else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
2111
{
2112
printf( "MSFT_OP_MACHINE_FRAME\n" );
2113
}
2114
else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
2115
{
2116
printf( "MSFT_OP_CONTEXT\n" );
2117
}
2118
else if (ptr[i] == 0xeb) /* MSFT_OP_EC_CONTEXT */
2119
{
2120
printf( "MSFT_OP_EC_CONTEXT\n" );
2121
}
2122
else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
2123
{
2124
printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
2125
}
2126
else if (ptr[i] == 0xfc) /* pac_sign_lr */
2127
{
2128
printf( "pac_sign_lr\n" );
2129
}
2130
else printf( "??\n");
2131
}
2132
}
2133
2134
static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
2135
{
2136
int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
2137
int homing = func->H;
2138
2139
if (func->CR == 1) intsz += 8;
2140
if (func->RegF) fpsz += 8;
2141
2142
savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
2143
locsz = func->FrameSize * 16 - savesz;
2144
2145
if (func->H && func->RegI == 0 && func->RegF == 0 && func->CR != 1)
2146
{
2147
locsz += savesz;
2148
homing = 0;
2149
}
2150
2151
switch (func->CR)
2152
{
2153
case 3:
2154
printf( " %04x: mov x29,sp\n", pos++ );
2155
if (locsz <= 512)
2156
{
2157
printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
2158
break;
2159
}
2160
printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
2161
/* fall through */
2162
case 0:
2163
case 1:
2164
if (locsz <= 4080)
2165
{
2166
printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
2167
}
2168
else
2169
{
2170
printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
2171
printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
2172
}
2173
break;
2174
}
2175
2176
if (homing)
2177
{
2178
printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
2179
printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
2180
printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
2181
printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
2182
}
2183
2184
if (func->RegF)
2185
{
2186
if (func->RegF % 2 == 0)
2187
printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
2188
for (i = (func->RegF - 1)/ 2; i >= 0; i--)
2189
{
2190
if (!i && !intsz)
2191
printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
2192
else
2193
printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
2194
}
2195
}
2196
2197
switch (func->RegI)
2198
{
2199
case 0:
2200
if (func->CR == 1)
2201
printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
2202
break;
2203
case 1:
2204
if (func->CR == 1) {
2205
printf( " %04x: sub sp, sp, %#x\n", pos++, savesz );
2206
printf( " %04x: stp x19,lr,[sp]\n", pos++ );
2207
} else
2208
printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
2209
break;
2210
default:
2211
if (func->RegI % 2)
2212
{
2213
if (func->CR == 1)
2214
printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
2215
else
2216
printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
2217
}
2218
else if (func->CR == 1)
2219
printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
2220
2221
for (i = func->RegI / 2 - 1; i >= 0; i--)
2222
if (i)
2223
printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
2224
else
2225
printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
2226
break;
2227
}
2228
printf( " %04x: end\n", pos );
2229
}
2230
2231
static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
2232
{
2233
const struct unwind_info_arm64 *info;
2234
const struct unwind_info_ext_arm64 *infoex;
2235
const struct unwind_info_epilog_arm64 *infoepi;
2236
const struct runtime_function_arm64 *parent_func;
2237
const BYTE *ptr;
2238
unsigned int i, rva, codes, epilogs;
2239
2240
switch (func->Flag)
2241
{
2242
case 1:
2243
case 2:
2244
printf( "\nFunction %08x-%08x:%s\n", func->BeginAddress,
2245
func->BeginAddress + func->FunctionLength * 4, find_export_from_rva( func->BeginAddress ));
2246
printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
2247
func->FunctionLength, func->Flag, func->RegF, func->RegI,
2248
func->H, func->CR, func->FrameSize );
2249
dump_arm64_packed_info( func );
2250
return;
2251
case 3:
2252
rva = func->UnwindData & ~3;
2253
parent_func = RVA( rva, sizeof(*parent_func) );
2254
printf( "\nFunction %08x-%08x:%s\n", func->BeginAddress,
2255
func->BeginAddress + 12 /* adrl x16, <dest>; br x16 */,
2256
find_export_from_rva( func->BeginAddress ));
2257
printf( " forward to parent %08x\n", parent_func->BeginAddress );
2258
return;
2259
}
2260
2261
rva = func->UnwindData;
2262
info = RVA( rva, sizeof(*info) );
2263
rva += sizeof(*info);
2264
epilogs = info->epilog;
2265
codes = info->codes;
2266
2267
if (!codes)
2268
{
2269
infoex = RVA( rva, sizeof(*infoex) );
2270
rva = rva + sizeof(*infoex);
2271
codes = infoex->codes;
2272
epilogs = infoex->epilog;
2273
}
2274
printf( "\nFunction %08x-%08x:\n",
2275
func->BeginAddress, func->BeginAddress + info->function_length * 4 );
2276
printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
2277
info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
2278
if (info->e)
2279
{
2280
printf( " epilog 0: code=%04x\n", info->epilog );
2281
}
2282
else
2283
{
2284
infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
2285
rva += sizeof(*infoepi) * epilogs;
2286
for (i = 0; i < epilogs; i++)
2287
printf( " epilog %u: pc=%08x code=%04x\n", i,
2288
func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
2289
}
2290
ptr = RVA( rva, codes * 4);
2291
rva += codes * 4;
2292
dump_arm64_codes( ptr, codes * 4 );
2293
if (info->x)
2294
{
2295
const UINT *handler = RVA( rva, sizeof(*handler) );
2296
const char *name = get_function_name( *handler );
2297
2298
rva += sizeof(*handler);
2299
printf( " handler: %08x%s data %08x\n", *handler, name, rva );
2300
dump_exception_data( rva, func->BeginAddress, name );
2301
}
2302
}
2303
2304
static void dump_dir_exceptions(void)
2305
{
2306
static const void *arm64_funcs;
2307
unsigned int i, size;
2308
const void *funcs;
2309
const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
2310
const IMAGE_ARM64EC_METADATA *metadata;
2311
2312
funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
2313
if (!funcs) return;
2314
2315
switch (file_header->Machine)
2316
{
2317
case IMAGE_FILE_MACHINE_AMD64:
2318
size /= sizeof(struct runtime_function_x86_64);
2319
printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
2320
for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
2321
if (!(metadata = get_hybrid_metadata())) break;
2322
if (!(size = metadata->ExtraRFETableSize)) break;
2323
if (!(funcs = RVA( metadata->ExtraRFETable, size ))) break;
2324
if (funcs == arm64_funcs) break; /* already dumped */
2325
printf( "\n" );
2326
/* fall through */
2327
case IMAGE_FILE_MACHINE_ARM64:
2328
arm64_funcs = funcs;
2329
size /= sizeof(struct runtime_function_arm64);
2330
printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
2331
for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
2332
break;
2333
case IMAGE_FILE_MACHINE_ARMNT:
2334
size /= sizeof(struct runtime_function_armnt);
2335
printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
2336
for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
2337
break;
2338
default:
2339
printf( "Exception information not supported for %s binaries\n",
2340
get_machine_str(file_header->Machine));
2341
break;
2342
}
2343
printf( "\n" );
2344
}
2345
2346
2347
static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
2348
{
2349
/* FIXME: This does not properly handle large images */
2350
const IMAGE_IMPORT_BY_NAME* iibn;
2351
for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
2352
{
2353
if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
2354
printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
2355
else
2356
{
2357
iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
2358
if (!iibn)
2359
printf("Can't grab import by name info, skipping to next ordinal\n");
2360
else
2361
printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
2362
}
2363
}
2364
}
2365
2366
static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
2367
{
2368
const IMAGE_IMPORT_BY_NAME* iibn;
2369
for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
2370
{
2371
if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
2372
printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
2373
else
2374
{
2375
iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
2376
if (!iibn)
2377
printf("Can't grab import by name info, skipping to next ordinal\n");
2378
else
2379
printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
2380
}
2381
}
2382
}
2383
2384
static void dump_dir_imported_functions(void)
2385
{
2386
unsigned directorySize;
2387
const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
2388
2389
if (!importDesc) return;
2390
2391
printf("Import Table size: %08x\n", directorySize);/* FIXME */
2392
2393
for (;;)
2394
{
2395
const IMAGE_THUNK_DATA32* il;
2396
2397
if (!importDesc->Name || !importDesc->FirstThunk) break;
2398
2399
printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
2400
printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
2401
printf(" TimeDateStamp: %08X (%s)\n",
2402
(UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2403
printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
2404
printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
2405
2406
printf(" Thunk Ordn Name\n");
2407
2408
il = (importDesc->OriginalFirstThunk != 0) ?
2409
RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
2410
RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
2411
2412
if (!il)
2413
printf("Can't grab thunk data, going to next imported DLL\n");
2414
else
2415
{
2416
if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2417
dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
2418
else
2419
dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
2420
printf("\n");
2421
}
2422
importDesc++;
2423
}
2424
printf("\n");
2425
}
2426
2427
static void dump_hybrid_metadata(void)
2428
{
2429
unsigned int i;
2430
const void *metadata = get_hybrid_metadata();
2431
2432
if (!metadata) return;
2433
printf( "Hybrid metadata\n" );
2434
2435
switch (PE_nt_headers->FileHeader.Machine)
2436
{
2437
case IMAGE_FILE_MACHINE_I386:
2438
{
2439
const IMAGE_CHPE_METADATA_X86 *data = metadata;
2440
2441
printf( " Version %#x\n", (int)data->Version );
2442
printf( " CHPECodeAddressRangeOffset %#x\n", (int)data->CHPECodeAddressRangeOffset );
2443
printf( " CHPECodeAddressRangeCount %#x\n", (int)data->CHPECodeAddressRangeCount );
2444
printf( " WowA64ExceptionHandlerFunctionPointer %#x\n", (int)data->WowA64ExceptionHandlerFunctionPointer );
2445
printf( " WowA64DispatchCallFunctionPointer %#x\n", (int)data->WowA64DispatchCallFunctionPointer );
2446
printf( " WowA64DispatchIndirectCallFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallFunctionPointer );
2447
printf( " WowA64DispatchIndirectCallCfgFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallCfgFunctionPointer );
2448
printf( " WowA64DispatchRetFunctionPointer %#x\n", (int)data->WowA64DispatchRetFunctionPointer );
2449
printf( " WowA64DispatchRetLeafFunctionPointer %#x\n", (int)data->WowA64DispatchRetLeafFunctionPointer );
2450
printf( " WowA64DispatchJumpFunctionPointer %#x\n", (int)data->WowA64DispatchJumpFunctionPointer );
2451
if (data->Version >= 2)
2452
printf( " CompilerIATPointer %#x\n", (int)data->CompilerIATPointer );
2453
if (data->Version >= 3)
2454
printf( " WowA64RdtscFunctionPointer %#x\n", (int)data->WowA64RdtscFunctionPointer );
2455
if (data->Version >= 4)
2456
{
2457
printf( " unknown[0] %#x\n", (int)data->unknown[0] );
2458
printf( " unknown[1] %#x\n", (int)data->unknown[1] );
2459
printf( " unknown[2] %#x\n", (int)data->unknown[2] );
2460
printf( " unknown[3] %#x\n", (int)data->unknown[3] );
2461
}
2462
2463
if (data->CHPECodeAddressRangeOffset)
2464
{
2465
const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CHPECodeAddressRangeOffset,
2466
data->CHPECodeAddressRangeCount * sizeof(*map) );
2467
2468
printf( "\nCode ranges\n" );
2469
for (i = 0; i < data->CHPECodeAddressRangeCount; i++)
2470
{
2471
static const char *types[] = { "x86", "ARM64" };
2472
unsigned int start = map[i].StartOffset & ~1;
2473
unsigned int type = map[i].StartOffset & 1;
2474
printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
2475
}
2476
}
2477
2478
break;
2479
}
2480
2481
case IMAGE_FILE_MACHINE_AMD64:
2482
case IMAGE_FILE_MACHINE_ARM64:
2483
{
2484
const IMAGE_ARM64EC_METADATA *data = metadata;
2485
2486
printf( " Version %#x\n", (int)data->Version );
2487
printf( " CodeMap %#x\n", (int)data->CodeMap );
2488
printf( " CodeMapCount %#x\n", (int)data->CodeMapCount );
2489
printf( " CodeRangesToEntryPoints %#x\n", (int)data->CodeRangesToEntryPoints );
2490
printf( " RedirectionMetadata %#x\n", (int)data->RedirectionMetadata );
2491
printf( " __os_arm64x_dispatch_call_no_redirect %#x\n", (int)data->__os_arm64x_dispatch_call_no_redirect );
2492
printf( " __os_arm64x_dispatch_ret %#x\n", (int)data->__os_arm64x_dispatch_ret );
2493
printf( " __os_arm64x_dispatch_call %#x\n", (int)data->__os_arm64x_dispatch_call );
2494
printf( " __os_arm64x_dispatch_icall %#x\n", (int)data->__os_arm64x_dispatch_icall );
2495
printf( " __os_arm64x_dispatch_icall_cfg %#x\n", (int)data->__os_arm64x_dispatch_icall_cfg );
2496
printf( " AlternateEntryPoint %#x\n", (int)data->AlternateEntryPoint );
2497
printf( " AuxiliaryIAT %#x\n", (int)data->AuxiliaryIAT );
2498
printf( " CodeRangesToEntryPointsCount %#x\n", (int)data->CodeRangesToEntryPointsCount );
2499
printf( " RedirectionMetadataCount %#x\n", (int)data->RedirectionMetadataCount );
2500
printf( " GetX64InformationFunctionPointer %#x\n", (int)data->GetX64InformationFunctionPointer );
2501
printf( " SetX64InformationFunctionPointer %#x\n", (int)data->SetX64InformationFunctionPointer );
2502
printf( " ExtraRFETable %#x\n", (int)data->ExtraRFETable );
2503
printf( " ExtraRFETableSize %#x\n", (int)data->ExtraRFETableSize );
2504
printf( " __os_arm64x_dispatch_fptr %#x\n", (int)data->__os_arm64x_dispatch_fptr );
2505
printf( " AuxiliaryIATCopy %#x\n", (int)data->AuxiliaryIATCopy );
2506
printf( " __os_arm64x_helper0 %#x\n", (int)data->__os_arm64x_helper0 );
2507
printf( " __os_arm64x_helper1 %#x\n", (int)data->__os_arm64x_helper1 );
2508
printf( " __os_arm64x_helper2 %#x\n", (int)data->__os_arm64x_helper2 );
2509
printf( " __os_arm64x_helper3 %#x\n", (int)data->__os_arm64x_helper3 );
2510
printf( " __os_arm64x_helper4 %#x\n", (int)data->__os_arm64x_helper4 );
2511
printf( " __os_arm64x_helper5 %#x\n", (int)data->__os_arm64x_helper5 );
2512
printf( " __os_arm64x_helper6 %#x\n", (int)data->__os_arm64x_helper6 );
2513
printf( " __os_arm64x_helper7 %#x\n", (int)data->__os_arm64x_helper7 );
2514
printf( " __os_arm64x_helper8 %#x\n", (int)data->__os_arm64x_helper8 );
2515
2516
if (data->CodeMap)
2517
{
2518
const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CodeMap, data->CodeMapCount * sizeof(*map) );
2519
2520
printf( "\nCode ranges\n" );
2521
for (i = 0; i < data->CodeMapCount; i++)
2522
{
2523
static const char *types[] = { "ARM64", "ARM64EC", "x64", "??" };
2524
unsigned int start = map[i].StartOffset & ~0x3;
2525
unsigned int type = map[i].StartOffset & 0x3;
2526
printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
2527
}
2528
}
2529
2530
if (PE_nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_ARM64) break;
2531
2532
if (data->CodeRangesToEntryPoints)
2533
{
2534
const IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT *map = RVA( data->CodeRangesToEntryPoints,
2535
data->CodeRangesToEntryPointsCount * sizeof(*map) );
2536
2537
printf( "\nCode ranges to entry points\n" );
2538
printf( " Start - End Entry point\n" );
2539
for (i = 0; i < data->CodeRangesToEntryPointsCount; i++)
2540
{
2541
const char *name = find_export_from_rva( map[i].EntryPoint );
2542
printf( " %08x - %08x %08x%s\n",
2543
(int)map[i].StartRva, (int)map[i].EndRva, (int)map[i].EntryPoint, name );
2544
}
2545
}
2546
2547
if (data->RedirectionMetadata)
2548
{
2549
const IMAGE_ARM64EC_REDIRECTION_ENTRY *map = RVA( data->RedirectionMetadata,
2550
data->RedirectionMetadataCount * sizeof(*map) );
2551
2552
printf( "\nEntry point redirection\n" );
2553
for (i = 0; i < data->RedirectionMetadataCount; i++)
2554
{
2555
const char *name = find_export_from_rva( map[i].Source );
2556
printf( " %08x -> %08x%s\n", (int)map[i].Source, (int)map[i].Destination, name );
2557
}
2558
}
2559
break;
2560
}
2561
}
2562
printf( "\n" );
2563
}
2564
2565
static void dump_dir_loadconfig(void)
2566
{
2567
unsigned int size;
2568
const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32;
2569
2570
loadcfg32 = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2571
if (!loadcfg32) return;
2572
size = min( size, loadcfg32->Size );
2573
2574
printf( "Loadconfig\n" );
2575
print_dword( "Size", loadcfg32->Size );
2576
print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
2577
print_word( "MajorVersion", loadcfg32->MajorVersion );
2578
print_word( "MinorVersion", loadcfg32->MinorVersion );
2579
print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
2580
print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
2581
print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
2582
2583
if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2584
{
2585
const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void *)loadcfg32;
2586
2587
print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
2588
print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
2589
print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
2590
print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
2591
print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
2592
print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
2593
print_word( "CSDVersion", loadcfg64->CSDVersion );
2594
print_word( "DependentLoadFlags", loadcfg64->DependentLoadFlags );
2595
print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
2596
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, SEHandlerTable )) goto done;
2597
print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
2598
print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
2599
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardCFCheckFunctionPointer )) goto done;
2600
print_longlong( "GuardCFCheckFunctionPointer", loadcfg64->GuardCFCheckFunctionPointer );
2601
print_longlong( "GuardCFDispatchFunctionPointer", loadcfg64->GuardCFDispatchFunctionPointer );
2602
print_longlong( "GuardCFFunctionTable", loadcfg64->GuardCFFunctionTable );
2603
print_longlong( "GuardCFFunctionCount", loadcfg64->GuardCFFunctionCount );
2604
print_dword( "GuardFlags", loadcfg64->GuardFlags );
2605
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CodeIntegrity )) goto done;
2606
print_word( "CodeIntegrity.Flags", loadcfg64->CodeIntegrity.Flags );
2607
print_word( "CodeIntegrity.Catalog", loadcfg64->CodeIntegrity.Catalog );
2608
print_dword( "CodeIntegrity.CatalogOffset", loadcfg64->CodeIntegrity.CatalogOffset );
2609
print_dword( "CodeIntegrity.Reserved", loadcfg64->CodeIntegrity.Reserved );
2610
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardAddressTakenIatEntryTable )) goto done;
2611
print_longlong( "GuardAddressTakenIatEntryTable", loadcfg64->GuardAddressTakenIatEntryTable );
2612
print_longlong( "GuardAddressTakenIatEntryCount", loadcfg64->GuardAddressTakenIatEntryCount );
2613
print_longlong( "GuardLongJumpTargetTable", loadcfg64->GuardLongJumpTargetTable );
2614
print_longlong( "GuardLongJumpTargetCount", loadcfg64->GuardLongJumpTargetCount );
2615
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTable )) goto done;
2616
print_longlong( "DynamicValueRelocTable", loadcfg64->DynamicValueRelocTable );
2617
print_longlong( "CHPEMetadataPointer", loadcfg64->CHPEMetadataPointer );
2618
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFFailureRoutine )) goto done;
2619
print_longlong( "GuardRFFailureRoutine", loadcfg64->GuardRFFailureRoutine );
2620
print_longlong( "GuardRFFailureRoutineFuncPtr", loadcfg64->GuardRFFailureRoutineFunctionPointer );
2621
print_dword( "DynamicValueRelocTableOffset", loadcfg64->DynamicValueRelocTableOffset );
2622
print_word( "DynamicValueRelocTableSection",loadcfg64->DynamicValueRelocTableSection );
2623
print_word( "Reserved2", loadcfg64->Reserved2 );
2624
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2625
print_longlong( "GuardRFVerifyStackPointerFuncPtr", loadcfg64->GuardRFVerifyStackPointerFunctionPointer );
2626
print_dword( "HotPatchTableOffset", loadcfg64->HotPatchTableOffset );
2627
print_dword( "Reserved3", loadcfg64->Reserved3 );
2628
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, EnclaveConfigurationPointer )) goto done;
2629
print_longlong( "EnclaveConfigurationPointer", loadcfg64->EnclaveConfigurationPointer );
2630
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, VolatileMetadataPointer )) goto done;
2631
print_longlong( "VolatileMetadataPointer", loadcfg64->VolatileMetadataPointer );
2632
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardEHContinuationTable )) goto done;
2633
print_longlong( "GuardEHContinuationTable", loadcfg64->GuardEHContinuationTable );
2634
print_longlong( "GuardEHContinuationCount", loadcfg64->GuardEHContinuationCount );
2635
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardXFGCheckFunctionPointer )) goto done;
2636
print_longlong( "GuardXFGCheckFunctionPointer", loadcfg64->GuardXFGCheckFunctionPointer );
2637
print_longlong( "GuardXFGDispatchFunctionPointer", loadcfg64->GuardXFGDispatchFunctionPointer );
2638
print_longlong( "GuardXFGTableDispatchFuncPtr", loadcfg64->GuardXFGTableDispatchFunctionPointer );
2639
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CastGuardOsDeterminedFailureMode )) goto done;
2640
print_longlong( "CastGuardOsDeterminedFailureMode", loadcfg64->CastGuardOsDeterminedFailureMode );
2641
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardMemcpyFunctionPointer )) goto done;
2642
print_longlong( "GuardMemcpyFunctionPointer", loadcfg64->GuardMemcpyFunctionPointer );
2643
}
2644
else
2645
{
2646
print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
2647
print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
2648
print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
2649
print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
2650
print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
2651
print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
2652
print_word( "CSDVersion", loadcfg32->CSDVersion );
2653
print_word( "DependentLoadFlags", loadcfg32->DependentLoadFlags );
2654
print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
2655
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, SEHandlerTable )) goto done;
2656
print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
2657
print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
2658
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardCFCheckFunctionPointer )) goto done;
2659
print_dword( "GuardCFCheckFunctionPointer", loadcfg32->GuardCFCheckFunctionPointer );
2660
print_dword( "GuardCFDispatchFunctionPointer", loadcfg32->GuardCFDispatchFunctionPointer );
2661
print_dword( "GuardCFFunctionTable", loadcfg32->GuardCFFunctionTable );
2662
print_dword( "GuardCFFunctionCount", loadcfg32->GuardCFFunctionCount );
2663
print_dword( "GuardFlags", loadcfg32->GuardFlags );
2664
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CodeIntegrity )) goto done;
2665
print_word( "CodeIntegrity.Flags", loadcfg32->CodeIntegrity.Flags );
2666
print_word( "CodeIntegrity.Catalog", loadcfg32->CodeIntegrity.Catalog );
2667
print_dword( "CodeIntegrity.CatalogOffset", loadcfg32->CodeIntegrity.CatalogOffset );
2668
print_dword( "CodeIntegrity.Reserved", loadcfg32->CodeIntegrity.Reserved );
2669
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardAddressTakenIatEntryTable )) goto done;
2670
print_dword( "GuardAddressTakenIatEntryTable", loadcfg32->GuardAddressTakenIatEntryTable );
2671
print_dword( "GuardAddressTakenIatEntryCount", loadcfg32->GuardAddressTakenIatEntryCount );
2672
print_dword( "GuardLongJumpTargetTable", loadcfg32->GuardLongJumpTargetTable );
2673
print_dword( "GuardLongJumpTargetCount", loadcfg32->GuardLongJumpTargetCount );
2674
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTable )) goto done;
2675
print_dword( "DynamicValueRelocTable", loadcfg32->DynamicValueRelocTable );
2676
print_dword( "CHPEMetadataPointer", loadcfg32->CHPEMetadataPointer );
2677
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFFailureRoutine )) goto done;
2678
print_dword( "GuardRFFailureRoutine", loadcfg32->GuardRFFailureRoutine );
2679
print_dword( "GuardRFFailureRoutineFuncPtr", loadcfg32->GuardRFFailureRoutineFunctionPointer );
2680
print_dword( "DynamicValueRelocTableOffset", loadcfg32->DynamicValueRelocTableOffset );
2681
print_word( "DynamicValueRelocTableSection", loadcfg32->DynamicValueRelocTableSection );
2682
print_word( "Reserved2", loadcfg32->Reserved2 );
2683
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2684
print_dword( "GuardRFVerifyStackPointerFuncPtr", loadcfg32->GuardRFVerifyStackPointerFunctionPointer );
2685
print_dword( "HotPatchTableOffset", loadcfg32->HotPatchTableOffset );
2686
print_dword( "Reserved3", loadcfg32->Reserved3 );
2687
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, EnclaveConfigurationPointer )) goto done;
2688
print_dword( "EnclaveConfigurationPointer", loadcfg32->EnclaveConfigurationPointer );
2689
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, VolatileMetadataPointer )) goto done;
2690
print_dword( "VolatileMetadataPointer", loadcfg32->VolatileMetadataPointer );
2691
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardEHContinuationTable )) goto done;
2692
print_dword( "GuardEHContinuationTable", loadcfg32->GuardEHContinuationTable );
2693
print_dword( "GuardEHContinuationCount", loadcfg32->GuardEHContinuationCount );
2694
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardXFGCheckFunctionPointer )) goto done;
2695
print_dword( "GuardXFGCheckFunctionPointer", loadcfg32->GuardXFGCheckFunctionPointer );
2696
print_dword( "GuardXFGDispatchFunctionPointer", loadcfg32->GuardXFGDispatchFunctionPointer );
2697
print_dword( "GuardXFGTableDispatchFuncPtr", loadcfg32->GuardXFGTableDispatchFunctionPointer );
2698
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CastGuardOsDeterminedFailureMode )) goto done;
2699
print_dword( "CastGuardOsDeterminedFailureMode", loadcfg32->CastGuardOsDeterminedFailureMode );
2700
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardMemcpyFunctionPointer )) goto done;
2701
print_dword( "GuardMemcpyFunctionPointer", loadcfg32->GuardMemcpyFunctionPointer );
2702
}
2703
done:
2704
printf( "\n" );
2705
dump_hybrid_metadata();
2706
}
2707
2708
static void dump_dir_delay_imported_functions(void)
2709
{
2710
unsigned directorySize;
2711
const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
2712
2713
if (!importDesc) return;
2714
2715
printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
2716
2717
for (;;)
2718
{
2719
const IMAGE_THUNK_DATA32* il;
2720
int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
2721
2722
if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
2723
2724
printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
2725
Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
2726
printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
2727
printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
2728
printf(" TimeDateStamp: %08X (%s)\n",
2729
(UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2730
2731
printf(" Thunk Ordn Name\n");
2732
2733
il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
2734
2735
if (!il)
2736
printf("Can't grab thunk data, going to next imported DLL\n");
2737
else
2738
{
2739
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2740
dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
2741
else
2742
dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
2743
printf("\n");
2744
}
2745
importDesc++;
2746
}
2747
printf("\n");
2748
}
2749
2750
static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx, const IMAGE_SECTION_HEADER *first_section)
2751
{
2752
const char* str;
2753
2754
printf("Directory %02u\n", idx + 1);
2755
printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
2756
printf(" TimeDateStamp: %08X %s\n",
2757
(UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
2758
printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
2759
switch (idd->Type)
2760
{
2761
default:
2762
case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
2763
case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
2764
case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
2765
case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
2766
case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
2767
case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
2768
case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
2769
case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
2770
case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
2771
case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
2772
case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
2773
case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
2774
case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
2775
case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
2776
case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
2777
case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
2778
case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
2779
}
2780
printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
2781
printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
2782
printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
2783
printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
2784
2785
switch (idd->Type)
2786
{
2787
case IMAGE_DEBUG_TYPE_UNKNOWN:
2788
break;
2789
case IMAGE_DEBUG_TYPE_COFF:
2790
dump_coff(idd->PointerToRawData, idd->SizeOfData, first_section);
2791
break;
2792
case IMAGE_DEBUG_TYPE_CODEVIEW:
2793
dump_codeview(idd->PointerToRawData, idd->SizeOfData);
2794
break;
2795
case IMAGE_DEBUG_TYPE_FPO:
2796
dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
2797
break;
2798
case IMAGE_DEBUG_TYPE_MISC:
2799
{
2800
const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
2801
if (!misc || idd->SizeOfData < sizeof(*misc)) {printf("Can't get MISC debug information\n"); break;}
2802
printf(" DataType: %u (%s)\n",
2803
(UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
2804
printf(" Length: %u\n", (UINT)misc->Length);
2805
printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
2806
printf(" Data: %s\n", misc->Data);
2807
}
2808
break;
2809
case IMAGE_DEBUG_TYPE_POGO:
2810
{
2811
const unsigned* data = PRD(idd->PointerToRawData, idd->SizeOfData);
2812
const unsigned* end = (const unsigned*)((const unsigned char*)data + idd->SizeOfData);
2813
unsigned idx = 0;
2814
2815
if (!data || idd->SizeOfData < sizeof(unsigned)) {printf("Can't get PODO debug information\n"); break;}
2816
printf(" Header: %08x\n", *(const unsigned*)data);
2817
data++;
2818
printf(" Index Name Offset Size\n");
2819
while (data + 2 < end)
2820
{
2821
const char* ptr;
2822
ptrdiff_t s;
2823
2824
if (!(ptr = memchr(&data[2], '\0', (const char*)end - (const char*)&data[2]))) break;
2825
printf(" %-5u %-16s %08x %08x\n", idx, (const char*)&data[2], data[0], data[1]);
2826
s = ptr - (const char*)&data[2] + 1;
2827
data += 2 + ((s + sizeof(unsigned) - 1) / sizeof(unsigned));
2828
idx++;
2829
}
2830
}
2831
break;
2832
case IMAGE_DEBUG_TYPE_REPRO:
2833
{
2834
const IMAGE_DEBUG_REPRO* repro = PRD(idd->PointerToRawData, idd->SizeOfData);
2835
if (!repro || idd->SizeOfData < sizeof(*repro)) {printf("Can't get REPRO debug information\n"); break;}
2836
printf(" Flags: %08X\n", repro->flags);
2837
printf(" Guid: %s\n", get_guid_str(&repro->guid));
2838
printf(" _unk0: %08X %u\n", repro->unk[0], repro->unk[0]);
2839
printf(" _unk1: %08X %u\n", repro->unk[1], repro->unk[1]);
2840
printf(" _unk2: %08X %u\n", repro->unk[2], repro->unk[2]);
2841
printf(" Timestamp: %08X\n", repro->debug_timestamp);
2842
}
2843
break;
2844
default:
2845
{
2846
const unsigned char* data = PRD(idd->PointerToRawData, idd->SizeOfData);
2847
if (!data) {printf("Can't get debug information for %s\n", str); break;}
2848
dump_data(data, idd->SizeOfData, " ");
2849
}
2850
break;
2851
}
2852
printf("\n");
2853
}
2854
2855
static void dump_dir_debug(void)
2856
{
2857
unsigned nb_dbg, i;
2858
const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
2859
2860
nb_dbg /= sizeof(*debugDir);
2861
if (!debugDir || !nb_dbg) return;
2862
2863
printf("Debug Table (%u directories)\n", nb_dbg);
2864
2865
for (i = 0; i < nb_dbg; i++)
2866
{
2867
dump_dir_debug_dir(debugDir, i, IMAGE_FIRST_SECTION(PE_nt_headers));
2868
debugDir++;
2869
}
2870
printf("\n");
2871
}
2872
2873
static struct
2874
{
2875
const BYTE *data;
2876
UINT size;
2877
} strings, userstrings, blobs, guids, tables;
2878
2879
static void print_clr_flags( const char *title, UINT value )
2880
{
2881
printf(" %-34s 0x%X\n", title, value);
2882
#define X(f,s) if (value & f) printf(" %s\n", s)
2883
X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
2884
X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
2885
X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
2886
X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
2887
X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
2888
#undef X
2889
}
2890
2891
static void print_clr_directory( const char *title, const IMAGE_DATA_DIRECTORY *dir )
2892
{
2893
printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
2894
}
2895
2896
static UINT clr_indent = 4;
2897
2898
static void print_clr_indent( void )
2899
{
2900
UINT i;
2901
for (i = 0; i < clr_indent; i++) printf( " " );
2902
}
2903
2904
static void print_clr( const char *str )
2905
{
2906
print_clr_indent();
2907
printf( "%s", str );
2908
}
2909
2910
static void print_clr_strings( const BYTE *data, UINT data_size )
2911
{
2912
const char *beg = (const char *)data, *end;
2913
2914
if (!data_size) return;
2915
clr_indent += 4;
2916
for (;;)
2917
{
2918
if (!(end = memchr( beg, '\0', data_size - (beg - (const char *)data) ))) break;
2919
print_clr_indent();
2920
printf( "%-10u\"%s\"\n", (UINT)(beg - (const char *)data), beg );
2921
beg = end + 1;
2922
}
2923
clr_indent -= 4;
2924
}
2925
2926
static UINT clr_blob_size( const BYTE *data, UINT data_size, UINT *skip )
2927
{
2928
if (!data_size) return 0;
2929
if (!(data[0] & 0x80))
2930
{
2931
*skip = 1;
2932
return data[0];
2933
}
2934
if (data_size < 2) return 0;
2935
if (!(data[0] & 0x40))
2936
{
2937
*skip = 2;
2938
return ((data[0] & ~0xc0) << 8) + data[1];
2939
}
2940
if (data_size < 4) return 0;
2941
if (!(data[0] & 0x20))
2942
{
2943
*skip = 4;
2944
return ((data[0] & ~0xe0) << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
2945
}
2946
return 0;
2947
}
2948
2949
static void print_clr_blobs( const BYTE *data, UINT data_size )
2950
{
2951
const BYTE *blob = data + 1;
2952
UINT i, size, skip;
2953
2954
if (!data_size) return;
2955
2956
clr_indent += 4;
2957
print_clr_indent();
2958
printf( "%-10u %02x\n", 0, data[0] );
2959
data_size--;
2960
for (;;)
2961
{
2962
if (!(size = clr_blob_size( blob, data_size, &skip ))) break;
2963
print_clr_indent();
2964
printf( "%-10u ", (UINT)(blob - data) );
2965
for (i = 0; i < size; i++)
2966
{
2967
printf( "%02x ", blob[i + skip] );
2968
if (i && i < size - 1 && !((i + 1) % 30))
2969
{
2970
printf( "\n" );
2971
print_clr( " " );
2972
}
2973
}
2974
printf( "\n" );
2975
data_size -= size + skip;
2976
blob += size + skip;
2977
}
2978
clr_indent -= 4;
2979
}
2980
2981
static void print_clr_guids( const BYTE *data, UINT data_size )
2982
{
2983
const BYTE *guid = data;
2984
UINT i, j, count = data_size / 16;
2985
2986
clr_indent += 4;
2987
for (i = 0; i < count; i++)
2988
{
2989
print_clr_indent();
2990
printf( "%-10u ", count );
2991
printf( "%08x", *(UINT *)guid );
2992
printf( " %04x", *(UINT *)guid + 4 );
2993
printf( " %04x", *(USHORT *)guid + 6 );
2994
for (j = 0; j < 8; j++) printf( " %02x", guid[j + 8] );
2995
printf( "\n" );
2996
guid += 16;
2997
}
2998
clr_indent -= 4;
2999
}
3000
3001
static UINT str_idx_size = 2;
3002
static UINT guid_idx_size = 2;
3003
static UINT blob_idx_size = 2;
3004
3005
static void print_clr_byte( const BYTE **ptr, UINT *size, BOOL flags )
3006
{
3007
if (*size < 1) return;
3008
printf( flags ? "0x%02x " : "%-16u", **ptr );
3009
(*ptr)++; (*size)--;
3010
}
3011
3012
static void print_clr_ushort( const BYTE **ptr, UINT *size, BOOL flags )
3013
{
3014
if (*size < sizeof(USHORT)) return;
3015
printf( flags ? "0x%04x " : "%-16u", *(const USHORT *)*ptr );
3016
*ptr += sizeof(USHORT); *size -= sizeof(USHORT);
3017
}
3018
3019
static void print_clr_uint( const BYTE **ptr, UINT *size, BOOL flags )
3020
{
3021
if (*size < sizeof(UINT)) return;
3022
printf( flags ? "0x%08x " : "%-16u", *(const UINT *)*ptr );
3023
*ptr += sizeof(UINT); *size -= sizeof(UINT);
3024
}
3025
3026
static void print_clr_uint64( const BYTE **ptr, UINT *size )
3027
{
3028
if (*size < sizeof(UINT64)) return;
3029
printf( "0x%08x%08x", (UINT)(*(const UINT64 *)*ptr >> 32), (UINT)*(const UINT64 *)*ptr );
3030
*ptr += sizeof(UINT64); *size -= sizeof(UINT64);
3031
}
3032
3033
static void print_clr_string_idx( const BYTE **ptr, UINT *size )
3034
{
3035
if (*size < str_idx_size) return;
3036
if (str_idx_size == 2)
3037
{
3038
USHORT idx = *(const USHORT *)*ptr;
3039
printf( "%-16u", idx );
3040
}
3041
else
3042
{
3043
UINT idx = *(const UINT *)*ptr;
3044
printf( "%-16u", idx );
3045
}
3046
*ptr += str_idx_size; *size -= str_idx_size;
3047
}
3048
3049
static void print_clr_blob_idx( const BYTE **ptr, UINT *size )
3050
{
3051
if (*size < blob_idx_size) return;
3052
if (blob_idx_size == 2)
3053
{
3054
USHORT idx = *(const USHORT *)*ptr;
3055
printf( "%-16u", idx );
3056
}
3057
else
3058
{
3059
UINT idx = *(const UINT *)*ptr;
3060
printf( "%-16u", idx );
3061
}
3062
*ptr += blob_idx_size; *size -= blob_idx_size;
3063
}
3064
3065
static void print_clr_guid_idx( const BYTE **ptr, UINT *size )
3066
{
3067
if (*size < guid_idx_size) return;
3068
if (guid_idx_size == 2)
3069
{
3070
USHORT idx = *(const USHORT *)*ptr;
3071
printf( "%-16u", idx );
3072
}
3073
else
3074
{
3075
UINT idx = *(const UINT *)*ptr;
3076
printf( "%-16u", idx );
3077
}
3078
*ptr += guid_idx_size; *size -= guid_idx_size;
3079
}
3080
3081
enum
3082
{
3083
CLR_TABLE_MODULE = 0x00,
3084
CLR_TABLE_TYPEREF = 0x01,
3085
CLR_TABLE_TYPEDEF = 0x02,
3086
CLR_TABLE_FIELD = 0x04,
3087
CLR_TABLE_METHODDEF = 0x06,
3088
CLR_TABLE_PARAM = 0x08,
3089
CLR_TABLE_INTERFACEIMPL = 0x09,
3090
CLR_TABLE_MEMBERREF = 0x0a,
3091
CLR_TABLE_CONSTANT = 0x0b,
3092
CLR_TABLE_CUSTOMATTRIBUTE = 0x0c,
3093
CLR_TABLE_FIELDMARSHAL = 0x0d,
3094
CLR_TABLE_DECLSECURITY = 0x0e,
3095
CLR_TABLE_CLASSLAYOUT = 0x0f,
3096
CLR_TABLE_FIELDLAYOUT = 0x10,
3097
CLR_TABLE_STANDALONESIG = 0x11,
3098
CLR_TABLE_EVENTMAP = 0x12,
3099
CLR_TABLE_EVENT = 0x14,
3100
CLR_TABLE_PROPERTYMAP = 0x15,
3101
CLR_TABLE_PROPERTY = 0x17,
3102
CLR_TABLE_METHODSEMANTICS = 0x18,
3103
CLR_TABLE_METHODIMPL = 0x19,
3104
CLR_TABLE_MODULEREF = 0x1a,
3105
CLR_TABLE_TYPESPEC = 0x1b,
3106
CLR_TABLE_IMPLMAP = 0x1c,
3107
CLR_TABLE_FIELDRVA = 0x1d,
3108
CLR_TABLE_ASSEMBLY = 0x20,
3109
CLR_TABLE_ASSEMBLYPROCESSOR = 0x21,
3110
CLR_TABLE_ASSEMBLYOS = 0x22,
3111
CLR_TABLE_ASSEMBLYREF = 0x23,
3112
CLR_TABLE_ASSEMBLYREFPROCESSOR = 0x24,
3113
CLR_TABLE_ASSEMBLYREFOS = 0x25,
3114
CLR_TABLE_FILE = 0x26,
3115
CLR_TABLE_EXPORTEDTYPE = 0x27,
3116
CLR_TABLE_MANIFESTRESOURCE = 0x28,
3117
CLR_TABLE_NESTEDCLASS = 0x29,
3118
CLR_TABLE_GENERICPARAM = 0x2a,
3119
CLR_TABLE_METHODSPEC = 0x2b,
3120
CLR_TABLE_GENERICPARAMCONSTRAINT = 0x2c,
3121
CLR_TABLE_MAX = 0x2d,
3122
};
3123
3124
static void print_clr_table_idx( const BYTE **ptr, UINT *size )
3125
{
3126
USHORT idx;
3127
if (*size < sizeof(USHORT)) return; /* FIXME should be 4 bytes if the number of rows exceeds 2^16 */
3128
idx = *(const USHORT *)*ptr;
3129
printf( "0x%04x ", idx );
3130
*ptr += sizeof(USHORT); *size -= sizeof(USHORT);
3131
}
3132
3133
static void print_clr_row( UINT row )
3134
{
3135
print_clr_indent();
3136
printf( "%-10u ", row );
3137
}
3138
3139
static void print_clr_table_header( const char * const *header )
3140
{
3141
const char *str;
3142
3143
print_clr_indent();
3144
printf( "%-11s", "#" );
3145
while ((str = *header++)) printf( "%-16s", str );
3146
printf( "\n" );
3147
}
3148
3149
static void print_clr_table_module( const BYTE **ptr, UINT *size, UINT row_count )
3150
{
3151
static const char * const header[] = { "Generation", "Name", "Mvid", "EncId", "EncBaseId", NULL };
3152
UINT i, row = 1;
3153
3154
print_clr( "Module table\n" );
3155
clr_indent += 4;
3156
print_clr_table_header( header );
3157
for (i = 0; i < row_count; i++)
3158
{
3159
print_clr_row( row++ );
3160
print_clr_ushort( ptr, size, FALSE );
3161
print_clr_string_idx( ptr, size );
3162
print_clr_guid_idx( ptr, size );
3163
print_clr_guid_idx( ptr, size );
3164
print_clr_guid_idx( ptr, size );
3165
printf( "\n" );
3166
}
3167
clr_indent -= 4;
3168
}
3169
3170
static void print_clr_table_typeref( const BYTE **ptr, UINT *size, UINT row_count )
3171
{
3172
static const char * const header[] = { "ResolutionScope", "TypeName", "TypeNamespace", NULL };
3173
UINT i, row = 1;
3174
3175
print_clr( "TypeRef table\n" );
3176
clr_indent += 4;
3177
print_clr_table_header( header );
3178
for (i = 0; i < row_count; i++)
3179
{
3180
print_clr_row( row++ );
3181
print_clr_table_idx( ptr, size );
3182
print_clr_string_idx( ptr, size );
3183
print_clr_string_idx( ptr, size );
3184
printf( "\n" );
3185
}
3186
clr_indent -= 4;
3187
}
3188
3189
static void print_clr_table_typedef( const BYTE **ptr, UINT *size, UINT row_count )
3190
{
3191
static const char * const header[] = { "Flags", "TypeName", "TypeNamespace", "Extends", "FieldList",
3192
"MethodList", NULL };
3193
UINT i, row = 1;
3194
3195
print_clr( "TypeDef table\n" );
3196
clr_indent += 4;
3197
print_clr_table_header( header );
3198
for (i = 0; i < row_count; i++)
3199
{
3200
print_clr_row( row++ );
3201
print_clr_uint( ptr, size, TRUE );
3202
print_clr_string_idx( ptr, size );
3203
print_clr_string_idx( ptr, size );
3204
print_clr_table_idx( ptr, size );
3205
print_clr_table_idx( ptr, size );
3206
print_clr_table_idx( ptr, size );
3207
printf( "\n" );
3208
}
3209
clr_indent -= 4;
3210
}
3211
3212
static void print_clr_table_field( const BYTE **ptr, UINT *size, UINT row_count )
3213
{
3214
static const char * const header[] = { "Flags", "Name", "Signature", NULL };
3215
UINT i, row = 1;
3216
3217
print_clr( "Field table\n" );
3218
clr_indent += 4;
3219
print_clr_table_header( header );
3220
for (i = 0; i < row_count; i++)
3221
{
3222
print_clr_row( row++ );
3223
print_clr_ushort( ptr, size, TRUE );
3224
print_clr_string_idx( ptr, size );
3225
print_clr_blob_idx( ptr, size );
3226
printf( "\n" );
3227
}
3228
clr_indent -= 4;
3229
}
3230
3231
static void print_clr_table_methoddef( const BYTE **ptr, UINT *size, UINT row_count )
3232
{
3233
static const char * const header[] = { "RVA", "ImplFlags", "Flags", "Name", "Signature", "ParamList", NULL };
3234
UINT i, row = 1;
3235
3236
print_clr( "MethodDef table\n" );
3237
clr_indent += 4;
3238
print_clr_table_header( header );
3239
for (i = 0; i < row_count; i++)
3240
{
3241
print_clr_row( row++ );
3242
print_clr_uint( ptr, size, TRUE );
3243
print_clr_ushort( ptr, size, TRUE );
3244
print_clr_ushort( ptr, size, TRUE );
3245
print_clr_string_idx( ptr, size );
3246
print_clr_blob_idx( ptr, size );
3247
print_clr_table_idx( ptr, size );
3248
printf( "\n" );
3249
}
3250
clr_indent -= 4;
3251
}
3252
3253
static void print_clr_table_param( const BYTE **ptr, UINT *size, UINT row_count )
3254
{
3255
static const char * const header[] = { "Flags", "Sequence", "Name", NULL };
3256
UINT i, row = 1;
3257
3258
print_clr( "Param table\n" );
3259
clr_indent += 4;
3260
print_clr_table_header( header );
3261
for (i = 0; i < row_count; i++)
3262
{
3263
print_clr_row( row++ );
3264
print_clr_ushort( ptr, size, TRUE );
3265
print_clr_ushort( ptr, size, FALSE );
3266
print_clr_string_idx( ptr, size );
3267
printf( "\n" );
3268
}
3269
clr_indent -= 4;
3270
}
3271
3272
static void print_clr_table_interfaceimpl( const BYTE **ptr, UINT *size, UINT row_count )
3273
{
3274
static const char * const header[] = { "Class", "Interface", NULL };
3275
UINT i, row = 1;
3276
3277
print_clr( "InterfaceImpl table\n" );
3278
clr_indent += 4;
3279
print_clr_table_header( header );
3280
for (i = 0; i < row_count; i++)
3281
{
3282
print_clr_row( row++ );
3283
print_clr_table_idx( ptr, size );
3284
print_clr_table_idx( ptr, size );
3285
printf( "\n" );
3286
}
3287
clr_indent -= 4;
3288
}
3289
3290
static void print_clr_table_memberref( const BYTE **ptr, UINT *size, UINT row_count )
3291
{
3292
static const char * const header[] = { "Class", "Name", "Signature", NULL };
3293
UINT i, row = 1;
3294
3295
print_clr( "MemberRef table\n" );
3296
clr_indent += 4;
3297
print_clr_table_header( header );
3298
for (i = 0; i < row_count; i++)
3299
{
3300
print_clr_row( row++ );
3301
print_clr_table_idx( ptr, size );
3302
print_clr_string_idx( ptr, size );
3303
print_clr_blob_idx( ptr, size );
3304
printf( "\n" );
3305
}
3306
clr_indent -= 4;
3307
}
3308
3309
static void print_clr_table_constant( const BYTE **ptr, UINT *size, UINT row_count )
3310
{
3311
static const char * const header[] = { "Type", "Padding", "Parent", "Value", NULL };
3312
UINT i, row = 1;
3313
3314
print_clr( "Constant table\n" );
3315
clr_indent += 4;
3316
print_clr_table_header( header );
3317
for (i = 0; i < row_count; i++)
3318
{
3319
print_clr_row( row++ );
3320
print_clr_byte( ptr, size, FALSE );
3321
print_clr_byte( ptr, size, FALSE );
3322
print_clr_table_idx( ptr, size );
3323
print_clr_blob_idx( ptr, size );
3324
printf( "\n" );
3325
}
3326
clr_indent -= 4;
3327
}
3328
3329
static void print_clr_table_customattribute( const BYTE **ptr, UINT *size, UINT row_count )
3330
{
3331
static const char * const header[] = { "Parent", "Type", "Value", NULL };
3332
UINT i, row = 1;
3333
3334
print_clr( "CustomAttribute table\n" );
3335
clr_indent += 4;
3336
print_clr_table_header( header );
3337
for (i = 0; i < row_count; i++)
3338
{
3339
print_clr_row( row++ );
3340
print_clr_table_idx( ptr, size );
3341
print_clr_table_idx( ptr, size );
3342
print_clr_blob_idx( ptr, size );
3343
printf( "\n" );
3344
}
3345
clr_indent -= 4;
3346
}
3347
3348
static void print_clr_table_fieldmarshal( const BYTE **ptr, UINT *size, UINT row_count )
3349
{
3350
static const char * const header[] = { "Parent", "NativeType", NULL };
3351
UINT i, row = 1;
3352
3353
print_clr( "FieldMarshal table\n" );
3354
clr_indent += 4;
3355
print_clr_table_header( header );
3356
for (i = 0; i < row_count; i++)
3357
{
3358
print_clr_row( row++ );
3359
print_clr_table_idx( ptr, size );
3360
print_clr_blob_idx( ptr, size );
3361
printf( "\n" );
3362
}
3363
clr_indent -= 4;
3364
}
3365
3366
static void print_clr_table_declsecurity( const BYTE **ptr, UINT *size, UINT row_count )
3367
{
3368
static const char * const header[] = { "Action", "Parent", "PermissionSet", NULL };
3369
UINT i, row = 1;
3370
3371
print_clr( "DeclSecurity table\n" );
3372
clr_indent += 4;
3373
print_clr_table_header( header );
3374
for (i = 0; i < row_count; i++)
3375
{
3376
print_clr_row( row++ );
3377
print_clr_ushort( ptr, size, TRUE );
3378
print_clr_table_idx( ptr, size );
3379
print_clr_blob_idx( ptr, size );
3380
printf( "\n" );
3381
}
3382
clr_indent -= 4;
3383
}
3384
3385
static void print_clr_table_classlayout( const BYTE **ptr, UINT *size, UINT row_count )
3386
{
3387
static const char * const header[] = { "PackingSize", "ClassSize", "Parent", NULL };
3388
UINT i, row = 1;
3389
3390
print_clr( "ClassLayout table\n" );
3391
clr_indent += 4;
3392
print_clr_table_header( header );
3393
for (i = 0; i < row_count; i++)
3394
{
3395
print_clr_row( row++ );
3396
print_clr_ushort( ptr, size, FALSE );
3397
print_clr_uint( ptr, size, FALSE );
3398
print_clr_table_idx( ptr, size );
3399
printf( "\n" );
3400
}
3401
clr_indent -= 4;
3402
}
3403
3404
static void print_clr_table_fieldlayout( const BYTE **ptr, UINT *size, UINT row_count )
3405
{
3406
static const char * const header[] = { "Offset", "Field", NULL };
3407
UINT i, row = 1;
3408
3409
print_clr( "FieldLayout table\n" );
3410
clr_indent += 4;
3411
print_clr_table_header( header );
3412
for (i = 0; i < row_count; i++)
3413
{
3414
print_clr_row( row++ );
3415
print_clr_uint( ptr, size, FALSE );
3416
print_clr_table_idx( ptr, size );
3417
printf( "\n" );
3418
}
3419
clr_indent -= 4;
3420
}
3421
3422
static void print_clr_table_standalonesig( const BYTE **ptr, UINT *size, UINT row_count )
3423
{
3424
static const char * const header[] = { "Signature", NULL };
3425
UINT i, row = 1;
3426
3427
print_clr( "StandAloneSig table\n" );
3428
clr_indent += 4;
3429
print_clr_table_header( header );
3430
for (i = 0; i < row_count; i++)
3431
{
3432
print_clr_row( row++ );
3433
print_clr_blob_idx( ptr, size );
3434
printf( "\n" );
3435
}
3436
clr_indent -= 4;
3437
}
3438
3439
static void print_clr_table_eventmap( const BYTE **ptr, UINT *size, UINT row_count )
3440
{
3441
static const char * const header[] = { "Parent", "EventList", NULL };
3442
UINT i, row = 1;
3443
3444
print_clr( "EventMap table\n" );
3445
clr_indent += 4;
3446
print_clr_table_header( header );
3447
for (i = 0; i < row_count; i++)
3448
{
3449
print_clr_row( row++ );
3450
print_clr_table_idx( ptr, size );
3451
print_clr_table_idx( ptr, size );
3452
printf( "\n" );
3453
}
3454
clr_indent -= 4;
3455
}
3456
3457
static void print_clr_table_event( const BYTE **ptr, UINT *size, UINT row_count )
3458
{
3459
static const char * const header[] = { "EventFlags", "Name", "EventType", NULL };
3460
UINT i, row = 1;
3461
3462
print_clr( "Event table\n" );
3463
clr_indent += 4;
3464
print_clr_table_header( header );
3465
for (i = 0; i < row_count; i++)
3466
{
3467
print_clr_row( row++ );
3468
print_clr_ushort( ptr, size, TRUE );
3469
print_clr_string_idx( ptr, size );
3470
print_clr_table_idx( ptr, size );
3471
printf( "\n" );
3472
}
3473
clr_indent -= 4;
3474
}
3475
3476
static void print_clr_table_propertymap( const BYTE **ptr, UINT *size, UINT row_count )
3477
{
3478
static const char * const header[] = { "Parent", "PropertyList", NULL };
3479
UINT i, row = 1;
3480
3481
print_clr( "PropertyMap table\n" );
3482
clr_indent += 4;
3483
print_clr_table_header( header );
3484
for (i = 0; i < row_count; i++)
3485
{
3486
print_clr_row( row++ );
3487
print_clr_table_idx( ptr, size );
3488
print_clr_table_idx( ptr, size );
3489
printf( "\n" );
3490
}
3491
clr_indent -= 4;
3492
}
3493
3494
static void print_clr_table_property( const BYTE **ptr, UINT *size, UINT row_count )
3495
{
3496
static const char * const header[] = { "Flags", "Name", "Type", NULL };
3497
UINT i, row = 1;
3498
3499
print_clr( "Property table\n" );
3500
clr_indent += 4;
3501
print_clr_table_header( header );
3502
for (i = 0; i < row_count; i++)
3503
{
3504
print_clr_row( row++ );
3505
print_clr_ushort( ptr, size, TRUE );
3506
print_clr_string_idx( ptr, size );
3507
print_clr_blob_idx( ptr, size );
3508
printf( "\n" );
3509
}
3510
clr_indent -= 4;
3511
}
3512
3513
static void print_clr_table_methodsemantics( const BYTE **ptr, UINT *size, UINT row_count )
3514
{
3515
static const char * const header[] = { "Semantics", "Method", "Association", NULL };
3516
UINT i, row = 1;
3517
3518
print_clr( "MethodSemantics table\n" );
3519
clr_indent += 4;
3520
print_clr_table_header( header );
3521
for (i = 0; i < row_count; i++)
3522
{
3523
print_clr_row( row++ );
3524
print_clr_ushort( ptr, size, TRUE );
3525
print_clr_table_idx( ptr, size );
3526
print_clr_table_idx( ptr, size );
3527
printf( "\n" );
3528
}
3529
clr_indent -= 4;
3530
}
3531
3532
static void print_clr_table_methodimpl( const BYTE **ptr, UINT *size, UINT row_count )
3533
{
3534
static const char * const header[] = { "Class", "Body", "Declaration", NULL };
3535
UINT i, row = 1;
3536
3537
print_clr( "MethodImpl table\n" );
3538
clr_indent += 4;
3539
print_clr_table_header( header );
3540
for (i = 0; i < row_count; i++)
3541
{
3542
print_clr_row( row++ );
3543
print_clr_table_idx( ptr, size );
3544
print_clr_table_idx( ptr, size );
3545
print_clr_table_idx( ptr, size );
3546
printf( "\n" );
3547
}
3548
clr_indent -= 4;
3549
}
3550
3551
static void print_clr_table_moduleref( const BYTE **ptr, UINT *size, UINT row_count )
3552
{
3553
static const char * const header[] = { "Name", NULL };
3554
UINT i, row = 1;
3555
3556
print_clr( "ModuleRef table\n" );
3557
clr_indent += 4;
3558
print_clr_table_header( header );
3559
for (i = 0; i < row_count; i++)
3560
{
3561
print_clr_row( row++ );
3562
print_clr_string_idx( ptr, size );
3563
printf( "\n" );
3564
}
3565
clr_indent -= 4;
3566
}
3567
3568
static void print_clr_table_typespec( const BYTE **ptr, UINT *size, UINT row_count )
3569
{
3570
static const char * const header[] = { "Signature", NULL };
3571
UINT i, row = 1;
3572
3573
print_clr( "TypeSpec table\n" );
3574
clr_indent += 4;
3575
print_clr_table_header( header );
3576
for (i = 0; i < row_count; i++)
3577
{
3578
print_clr_row( row++ );
3579
print_clr_blob_idx( ptr, size );
3580
printf( "\n" );
3581
}
3582
clr_indent -= 4;
3583
}
3584
3585
static void print_clr_table_implmap( const BYTE **ptr, UINT *size, UINT row_count )
3586
{
3587
static const char * const header[] = { "MappingFlags", "MemberForwarded", "ImportName", "ImportScope", NULL };
3588
UINT i, row = 1;
3589
3590
print_clr( "ImplMap table\n" );
3591
clr_indent += 4;
3592
print_clr_table_header( header );
3593
for (i = 0; i < row_count; i++)
3594
{
3595
print_clr_row( row++ );
3596
print_clr_ushort( ptr, size, TRUE );
3597
print_clr_table_idx( ptr, size );
3598
print_clr_string_idx( ptr, size );
3599
print_clr_table_idx( ptr, size );
3600
printf( "\n" );
3601
}
3602
clr_indent -= 4;
3603
}
3604
3605
static void print_clr_table_fieldrva( const BYTE **ptr, UINT *size, UINT row_count )
3606
{
3607
static const char * const header[] = { "RVA", "Field", NULL };
3608
UINT i, row = 1;
3609
3610
print_clr( "FieldRVA table\n" );
3611
clr_indent += 4;
3612
print_clr_table_header( header );
3613
for (i = 0; i < row_count; i++)
3614
{
3615
print_clr_row( row++ );
3616
print_clr_uint( ptr, size, TRUE );
3617
print_clr_table_idx( ptr, size );
3618
printf( "\n" );
3619
}
3620
clr_indent -= 4;
3621
}
3622
3623
static void print_clr_table_assembly( const BYTE **ptr, UINT *size, UINT row_count )
3624
{
3625
static const char * const header[] = { "HashAlgId", "MajorVersion", "MinorVersion", "BuildNumber",
3626
"RevisionNumber", "Flags", "PublicKey", "Name", "Culture", NULL };
3627
UINT i, row = 1;
3628
3629
print_clr( "Assembly table\n" );
3630
clr_indent += 4;
3631
print_clr_table_header( header );
3632
for (i = 0; i < row_count; i++)
3633
{
3634
print_clr_row( row++ );
3635
print_clr_uint( ptr, size, TRUE );
3636
print_clr_ushort( ptr, size, FALSE );
3637
print_clr_ushort( ptr, size, FALSE );
3638
print_clr_ushort( ptr, size, FALSE );
3639
print_clr_ushort( ptr, size, FALSE );
3640
print_clr_uint( ptr, size, TRUE );
3641
print_clr_blob_idx( ptr, size );
3642
print_clr_string_idx( ptr, size );
3643
print_clr_string_idx( ptr, size );
3644
printf( "\n" );
3645
}
3646
clr_indent -= 4;
3647
}
3648
3649
static void print_clr_table_assemblyprocessor( const BYTE **ptr, UINT *size, UINT row_count )
3650
{
3651
static const char * const header[] = { "Processor", NULL };
3652
UINT i, row = 1;
3653
3654
print_clr( "AssemblyProcessor table\n" );
3655
clr_indent += 4;
3656
print_clr_table_header( header );
3657
for (i = 0; i < row_count; i++)
3658
{
3659
print_clr_row( row++ );
3660
print_clr_uint( ptr, size, TRUE );
3661
printf( "\n" );
3662
}
3663
clr_indent -= 4;
3664
}
3665
3666
static void print_clr_table_assemblyos( const BYTE **ptr, UINT *size, UINT row_count )
3667
{
3668
static const char * const header[] = { "OSPlatformID", "OSMajorVersion", "OSMinorVersion", NULL };
3669
UINT i, row = 1;
3670
3671
print_clr( "AssemblyOS table\n" );
3672
clr_indent += 4;
3673
print_clr_table_header( header );
3674
for (i = 0; i < row_count; i++)
3675
{
3676
print_clr_row( row++ );
3677
print_clr_uint( ptr, size, TRUE );
3678
print_clr_uint( ptr, size, FALSE );
3679
print_clr_uint( ptr, size, FALSE );
3680
printf( "\n" );
3681
}
3682
clr_indent -= 4;
3683
}
3684
3685
static void print_clr_table_assemblyref( const BYTE **ptr, UINT *size, UINT row_count )
3686
{
3687
static const char * const header[] = { "MajorVersion", "MinorVersion", "BuildNumber", "RevisionNumber", "Flags",
3688
"PublicKey", "Name", "Culture", "HashValue", NULL };
3689
UINT i, row = 1;
3690
3691
print_clr( "AssemblyRef table\n" );
3692
clr_indent += 4;
3693
print_clr_table_header( header );
3694
for (i = 0; i < row_count; i++)
3695
{
3696
print_clr_row( row++ );
3697
print_clr_ushort( ptr, size, FALSE );
3698
print_clr_ushort( ptr, size, FALSE );
3699
print_clr_ushort( ptr, size, FALSE );
3700
print_clr_ushort( ptr, size, FALSE );
3701
print_clr_uint( ptr, size, TRUE );
3702
print_clr_blob_idx( ptr, size );
3703
print_clr_string_idx( ptr, size );
3704
print_clr_string_idx( ptr, size );
3705
print_clr_blob_idx( ptr, size );
3706
printf( "\n" );
3707
}
3708
clr_indent -= 4;
3709
}
3710
3711
static void print_clr_table_assemblyrefprocessor( const BYTE **ptr, UINT *size, UINT row_count )
3712
{
3713
static const char * const header[] = { "Processor", "AssemblyRef", NULL };
3714
UINT i, row = 1;
3715
3716
print_clr( "AssemblyRefProcessor table\n" );
3717
clr_indent += 4;
3718
print_clr_table_header( header );
3719
for (i = 0; i < row_count; i++)
3720
{
3721
print_clr_row( row++ );
3722
print_clr_uint( ptr, size, TRUE );
3723
print_clr_table_idx( ptr, size );
3724
printf( "\n" );
3725
}
3726
clr_indent -= 4;
3727
}
3728
3729
static void print_clr_table_assemblyrefos( const BYTE **ptr, UINT *size, UINT row_count )
3730
{
3731
static const char * const header[] = { "OSPlatformId", "OSMajorVersion", "OSMinorVersion", "AssemblyRef", NULL };
3732
UINT i, row = 1;
3733
3734
print_clr( "AssemblyRefOS table\n" );
3735
clr_indent += 4;
3736
print_clr_table_header( header );
3737
for (i = 0; i < row_count; i++)
3738
{
3739
print_clr_row( row++ );
3740
print_clr_uint( ptr, size, TRUE );
3741
print_clr_uint( ptr, size, FALSE );
3742
print_clr_uint( ptr, size, FALSE );
3743
print_clr_table_idx( ptr, size );
3744
printf( "\n" );
3745
}
3746
clr_indent -= 4;
3747
}
3748
3749
static void print_clr_table_file( const BYTE **ptr, UINT *size, UINT row_count )
3750
{
3751
static const char * const header[] = { "Flags", "Name", "HashValue", NULL };
3752
UINT i, row = 1;
3753
3754
print_clr( "File table\n" );
3755
clr_indent += 4;
3756
print_clr_table_header( header );
3757
for (i = 0; i < row_count; i++)
3758
{
3759
print_clr_row( row++ );
3760
print_clr_uint( ptr, size, TRUE );
3761
print_clr_string_idx( ptr, size );
3762
print_clr_blob_idx( ptr, size );
3763
printf( "\n" );
3764
}
3765
clr_indent -= 4;
3766
}
3767
3768
static void print_clr_table_exportedtype( const BYTE **ptr, UINT *size, UINT row_count )
3769
{
3770
static const char * const header[] = { "Flags", "TypeDefId", "TypeName", "TypeNamespace", "Implementation", NULL };
3771
UINT i, row = 1;
3772
3773
print_clr( "ExportedType table\n" );
3774
clr_indent += 4;
3775
print_clr_table_header( header );
3776
for (i = 0; i < row_count; i++)
3777
{
3778
print_clr_row( row++ );
3779
print_clr_uint( ptr, size, TRUE );
3780
print_clr_uint( ptr, size, TRUE );
3781
print_clr_string_idx( ptr, size );
3782
print_clr_string_idx( ptr, size );
3783
print_clr_table_idx( ptr, size );
3784
printf( "\n" );
3785
}
3786
clr_indent -= 4;
3787
}
3788
3789
static void print_clr_table_manifestresource( const BYTE **ptr, UINT *size, UINT row_count )
3790
{
3791
static const char * const header[] = { "Offset", "Flags", "Name", "Implementation", NULL };
3792
UINT i, row = 1;
3793
3794
print_clr( "ManifestResource table\n" );
3795
clr_indent += 4;
3796
print_clr_table_header( header );
3797
for (i = 0; i < row_count; i++)
3798
{
3799
print_clr_row( row++ );
3800
print_clr_uint( ptr, size, FALSE );
3801
print_clr_uint( ptr, size, TRUE );
3802
print_clr_string_idx( ptr, size );
3803
print_clr_table_idx( ptr, size );
3804
printf( "\n" );
3805
}
3806
clr_indent -= 4;
3807
}
3808
3809
static void print_clr_table_nestedclass( const BYTE **ptr, UINT *size, UINT row_count )
3810
{
3811
static const char * const header[] = { "NestedClass", "EnclosingClass", NULL };
3812
UINT i, row = 1;
3813
3814
print_clr( "NestedClass table\n" );
3815
clr_indent += 4;
3816
print_clr_table_header( header );
3817
for (i = 0; i < row_count; i++)
3818
{
3819
print_clr_row( row++ );
3820
print_clr_table_idx( ptr, size );
3821
print_clr_table_idx( ptr, size );
3822
printf( "\n" );
3823
}
3824
clr_indent -= 4;
3825
}
3826
3827
static void print_clr_table_genericparam( const BYTE **ptr, UINT *size, UINT row_count )
3828
{
3829
static const char * const header[] = { "Number", "Flags", "Owner", "Name", NULL };
3830
UINT i, row = 1;
3831
3832
print_clr( "GenericParam table\n" );
3833
clr_indent += 4;
3834
print_clr_table_header( header );
3835
for (i = 0; i < row_count; i++)
3836
{
3837
print_clr_row( row++ );
3838
print_clr_ushort( ptr, size, FALSE );
3839
print_clr_ushort( ptr, size, TRUE );
3840
print_clr_table_idx( ptr, size );
3841
print_clr_string_idx( ptr, size );
3842
printf( "\n" );
3843
}
3844
clr_indent -= 4;
3845
}
3846
3847
static void print_clr_table_methodspec( const BYTE **ptr, UINT *size, UINT row_count )
3848
{
3849
static const char * const header[] = { "Method", "Instantiation", NULL };
3850
UINT i, row = 1;
3851
3852
print_clr( "MethodSpec table\n" );
3853
clr_indent += 4;
3854
print_clr_table_header( header );
3855
for (i = 0; i < row_count; i++)
3856
{
3857
print_clr_row( row++ );
3858
print_clr_table_idx( ptr, size );
3859
print_clr_blob_idx( ptr, size );
3860
printf( "\n" );
3861
}
3862
clr_indent -= 4;
3863
}
3864
3865
static void print_clr_table_genericparamconstraint( const BYTE **ptr, UINT *size, UINT row_count )
3866
{
3867
static const char * const header[] = { "Owner", "Constraint", NULL };
3868
UINT i, row = 1;
3869
3870
print_clr( "GenericParamConstraint table\n" );
3871
clr_indent += 4;
3872
print_clr_table_header( header );
3873
for (i = 0; i < row_count; i++)
3874
{
3875
print_clr_row( row++ );
3876
print_clr_table_idx( ptr, size );
3877
print_clr_blob_idx( ptr, size );
3878
printf( "\n" );
3879
}
3880
clr_indent -= 4;
3881
}
3882
3883
enum
3884
{
3885
HEAP_SIZE_FLAG_STRING = 0x01,
3886
HEAP_SIZE_FLAG_GUID = 0x02,
3887
HEAP_SIZE_FLAG_BLOB = 0x04,
3888
};
3889
3890
static void print_clr_tables( const BYTE *data, UINT data_size )
3891
{
3892
const BYTE *ptr = data;
3893
BYTE heap_sizes;
3894
UINT i, size = data_size, count = 0;
3895
const UINT *row_count;
3896
UINT64 valid, j;
3897
3898
if (!data_size) return;
3899
3900
clr_indent += 4;
3901
print_clr( "Reserved " );
3902
print_clr_uint( &ptr, &size, FALSE );
3903
printf( "\n" );
3904
print_clr( "MajorVersion " );
3905
print_clr_byte( &ptr, &size, FALSE );
3906
printf( "\n" );
3907
print_clr( "MinorVersion " );
3908
print_clr_byte( &ptr, &size, FALSE );
3909
printf( "\n" );
3910
3911
if (size < 1) return;
3912
heap_sizes = *ptr;
3913
print_clr( "HeapSizes " );
3914
print_clr_byte( &ptr, &size, TRUE );
3915
printf( "\n" );
3916
if (heap_sizes & HEAP_SIZE_FLAG_STRING) str_idx_size = 4;
3917
if (heap_sizes & HEAP_SIZE_FLAG_GUID) guid_idx_size = 4;
3918
if (heap_sizes & HEAP_SIZE_FLAG_BLOB) blob_idx_size = 4;
3919
3920
print_clr( "Reserved " );
3921
print_clr_byte( &ptr, &size, FALSE );
3922
printf( "\n" );
3923
3924
if (size < sizeof(UINT64)) return;
3925
valid = *(UINT64 *)ptr;
3926
print_clr( "Valid " );
3927
print_clr_uint64( &ptr, &size );
3928
printf( "\n" );
3929
for (j = 0; j < sizeof(valid) * 8; j++) if (valid & (1ul << j)) count++;
3930
3931
print_clr( "Sorted " );
3932
print_clr_uint64( &ptr, &size );
3933
printf( "\n" );
3934
clr_indent -= 4;
3935
3936
row_count = (const UINT *)ptr;
3937
if (count) print_clr( "Row counts\n" );
3938
for (i = 0; i < count; i++)
3939
{
3940
clr_indent += 4;
3941
print_clr_indent();
3942
print_clr_uint( &ptr, &size, FALSE );
3943
clr_indent -= 4;
3944
printf( "\n" );
3945
}
3946
3947
for (i = 0, j = 0; j < sizeof(valid) * 8; j++)
3948
{
3949
if (!(valid & (1ul << j))) continue;
3950
3951
if (row_count[i] > 65536)
3952
{
3953
printf( "can't handle number of rows > 65536\n" );
3954
break;
3955
}
3956
3957
switch (j)
3958
{
3959
case CLR_TABLE_MODULE:
3960
print_clr_table_module( &ptr, &size, row_count[i++] );
3961
break;
3962
case CLR_TABLE_TYPEREF:
3963
print_clr_table_typeref( &ptr, &size, row_count[i++] );
3964
break;
3965
case CLR_TABLE_TYPEDEF:
3966
print_clr_table_typedef( &ptr, &size, row_count[i++] );
3967
break;
3968
case CLR_TABLE_FIELD:
3969
print_clr_table_field( &ptr, &size, row_count[i++] );
3970
break;
3971
case CLR_TABLE_METHODDEF:
3972
print_clr_table_methoddef( &ptr, &size, row_count[i++] );
3973
break;
3974
case CLR_TABLE_PARAM:
3975
print_clr_table_param( &ptr, &size, row_count[i++] );
3976
break;
3977
case CLR_TABLE_INTERFACEIMPL:
3978
print_clr_table_interfaceimpl( &ptr, &size, row_count[i++] );
3979
break;
3980
case CLR_TABLE_MEMBERREF:
3981
print_clr_table_memberref( &ptr, &size, row_count[i++] );
3982
break;
3983
case CLR_TABLE_CONSTANT:
3984
print_clr_table_constant( &ptr, &size, row_count[i++] );
3985
break;
3986
case CLR_TABLE_CUSTOMATTRIBUTE:
3987
print_clr_table_customattribute( &ptr, &size, row_count[i++] );
3988
break;
3989
case CLR_TABLE_FIELDMARSHAL:
3990
print_clr_table_fieldmarshal( &ptr, &size, row_count[i++] );
3991
break;
3992
case CLR_TABLE_DECLSECURITY:
3993
print_clr_table_declsecurity( &ptr, &size, row_count[i++] );
3994
break;
3995
case CLR_TABLE_CLASSLAYOUT:
3996
print_clr_table_classlayout( &ptr, &size, row_count[i++] );
3997
break;
3998
case CLR_TABLE_FIELDLAYOUT:
3999
print_clr_table_fieldlayout( &ptr, &size, row_count[i++] );
4000
break;
4001
case CLR_TABLE_STANDALONESIG:
4002
print_clr_table_standalonesig( &ptr, &size, row_count[i++] );
4003
break;
4004
case CLR_TABLE_EVENTMAP:
4005
print_clr_table_eventmap( &ptr, &size, row_count[i++] );
4006
break;
4007
case CLR_TABLE_EVENT:
4008
print_clr_table_event( &ptr, &size, row_count[i++] );
4009
break;
4010
case CLR_TABLE_PROPERTYMAP:
4011
print_clr_table_propertymap( &ptr, &size, row_count[i++] );
4012
break;
4013
case CLR_TABLE_PROPERTY:
4014
print_clr_table_property( &ptr, &size, row_count[i++] );
4015
break;
4016
case CLR_TABLE_METHODSEMANTICS:
4017
print_clr_table_methodsemantics( &ptr, &size, row_count[i++] );
4018
break;
4019
case CLR_TABLE_METHODIMPL:
4020
print_clr_table_methodimpl( &ptr, &size, row_count[i++] );
4021
break;
4022
case CLR_TABLE_MODULEREF:
4023
print_clr_table_moduleref( &ptr, &size, row_count[i++] );
4024
break;
4025
case CLR_TABLE_TYPESPEC:
4026
print_clr_table_typespec( &ptr, &size, row_count[i++] );
4027
break;
4028
case CLR_TABLE_IMPLMAP:
4029
print_clr_table_implmap( &ptr, &size, row_count[i++] );
4030
break;
4031
case CLR_TABLE_FIELDRVA:
4032
print_clr_table_fieldrva( &ptr, &size, row_count[i++] );
4033
break;
4034
case CLR_TABLE_ASSEMBLY:
4035
print_clr_table_assembly( &ptr, &size, row_count[i++] );
4036
break;
4037
case CLR_TABLE_ASSEMBLYPROCESSOR:
4038
print_clr_table_assemblyprocessor( &ptr, &size, row_count[i++] );
4039
break;
4040
case CLR_TABLE_ASSEMBLYOS:
4041
print_clr_table_assemblyos( &ptr, &size, row_count[i++] );
4042
break;
4043
case CLR_TABLE_ASSEMBLYREF:
4044
print_clr_table_assemblyref( &ptr, &size, row_count[i++] );
4045
break;
4046
case CLR_TABLE_ASSEMBLYREFPROCESSOR:
4047
print_clr_table_assemblyrefprocessor( &ptr, &size, row_count[i++] );
4048
break;
4049
case CLR_TABLE_ASSEMBLYREFOS:
4050
print_clr_table_assemblyrefos( &ptr, &size, row_count[i++] );
4051
break;
4052
case CLR_TABLE_FILE:
4053
print_clr_table_file( &ptr, &size, row_count[i++] );
4054
break;
4055
case CLR_TABLE_EXPORTEDTYPE:
4056
print_clr_table_exportedtype( &ptr, &size, row_count[i++] );
4057
break;
4058
case CLR_TABLE_MANIFESTRESOURCE:
4059
print_clr_table_manifestresource( &ptr, &size, row_count[i++] );
4060
break;
4061
case CLR_TABLE_NESTEDCLASS:
4062
print_clr_table_nestedclass( &ptr, &size, row_count[i++] );
4063
break;
4064
case CLR_TABLE_GENERICPARAM:
4065
print_clr_table_genericparam( &ptr, &size, row_count[i++] );
4066
break;
4067
case CLR_TABLE_METHODSPEC:
4068
print_clr_table_methodspec( &ptr, &size, row_count[i++] );
4069
break;
4070
case CLR_TABLE_GENERICPARAMCONSTRAINT:
4071
print_clr_table_genericparamconstraint( &ptr, &size, row_count[i++] );
4072
break;
4073
default:
4074
printf( "unknown table 0x%02x\n", (BYTE)j );
4075
break;
4076
}
4077
}
4078
}
4079
4080
static void print_clr_stream( const BYTE *base, const BYTE **ptr, UINT *size )
4081
{
4082
const char *name;
4083
UINT len, stream_offset, stream_size;
4084
4085
if (*size < sizeof(UINT)) return;
4086
stream_offset = *(const UINT *)*ptr;
4087
print_clr( "Offset " );
4088
print_clr_uint( ptr, size, FALSE );
4089
printf( "\n" );
4090
4091
if (*size < sizeof(UINT)) return;
4092
stream_size = *(const UINT *)*ptr;
4093
print_clr( "Size " );
4094
print_clr_uint( ptr, size, FALSE );
4095
printf( "\n" );
4096
4097
if (!memchr( *ptr, '\0', *size )) return;
4098
name = (const char *)*ptr;
4099
print_clr( "Name " );
4100
printf( "%s\n", name );
4101
len = ((strlen( name ) + 1) + 3) & ~3;
4102
if (*size < len) return;
4103
*ptr += len; *size -= len;
4104
4105
if (!strcmp( name, "#Strings" ))
4106
{
4107
strings.data = base + stream_offset;
4108
strings.size = stream_size;
4109
/* remove padding */
4110
while (strings.size > 1 && !strings.data[strings.size -2] && !strings.data[strings.size - 1])
4111
strings.size--;
4112
}
4113
else if (!strcmp( name, "#US" ))
4114
{
4115
userstrings.data = base + stream_offset;
4116
userstrings.size = stream_size;
4117
}
4118
else if (!strcmp( name, "#Blob" ))
4119
{
4120
blobs.data = base + stream_offset;
4121
blobs.size = stream_size;
4122
}
4123
else if (!strcmp( name, "#GUID" ))
4124
{
4125
guids.data = base + stream_offset;
4126
guids.size = stream_size;
4127
}
4128
else if (!strcmp( name, "#~" ))
4129
{
4130
tables.data = base + stream_offset;
4131
tables.size = stream_size;
4132
}
4133
}
4134
4135
static void print_clr_metadata( const BYTE *data, UINT data_size )
4136
{
4137
const BYTE *ptr = data;
4138
UINT size = data_size, len, nb_streams, i;
4139
const char *version;
4140
4141
print_clr( "Signature " );
4142
print_clr_uint( &ptr, &size, TRUE );
4143
printf( "\n" );
4144
print_clr( "MajorVersion " );
4145
print_clr_ushort( &ptr, &size, FALSE );
4146
printf( "\n" );
4147
print_clr( "MinorVersion " );
4148
print_clr_ushort( &ptr, &size, FALSE );
4149
printf( "\n" );
4150
print_clr( "Reserved " );
4151
print_clr_uint( &ptr, &size, FALSE );
4152
printf( "\n" );
4153
4154
if (size < sizeof(UINT)) return;
4155
len = *(const UINT *)ptr;
4156
print_clr( "Length " );
4157
print_clr_uint( &ptr, &size, FALSE );
4158
printf( "\n" );
4159
4160
if (size < len || !memchr( ptr, '\0', len )) return;
4161
version = (const char *)ptr;
4162
print_clr( "Version " );
4163
printf( "%s\n", version );
4164
len = ((strlen( version ) + 1) + 3) & ~3;
4165
if (size < len) return;
4166
ptr += len; size -= len;
4167
4168
print_clr( "Flags " );
4169
print_clr_ushort( &ptr, &size, TRUE );
4170
printf( "\n" );
4171
4172
if (size < sizeof(USHORT)) return;
4173
nb_streams = *(const USHORT *)ptr;
4174
print_clr( "Streams " );
4175
print_clr_ushort( &ptr, &size, FALSE );
4176
printf( "\n" );
4177
4178
clr_indent += 4;
4179
for (i = 0; i < nb_streams; i++) print_clr_stream( data, &ptr, &size );
4180
clr_indent -= 4;
4181
4182
print_clr( "Tables\n" );
4183
print_clr_tables( tables.data, tables.size );
4184
print_clr( "Strings\n" );
4185
print_clr_strings( strings.data, strings.size );
4186
print_clr( "Userstrings\n" );
4187
print_clr_blobs( userstrings.data, userstrings.size );
4188
print_clr( "GUIDs\n" );
4189
print_clr_guids( guids.data, guids.size );
4190
print_clr( "Blobs\n" );
4191
print_clr_blobs( blobs.data, blobs.size );
4192
}
4193
4194
static void dump_dir_clr_header(void)
4195
{
4196
unsigned int size = 0;
4197
const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
4198
4199
if (!dir) return;
4200
4201
printf( "CLR Header\n" );
4202
print_dword( "Header Size", dir->cb );
4203
print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
4204
print_clr_flags( "Flags", dir->Flags );
4205
print_dword( "EntryPointToken", dir->EntryPointToken );
4206
printf("\n");
4207
printf( "CLR Data Directory\n" );
4208
print_clr_directory( "MetaData", &dir->MetaData );
4209
print_clr_metadata( RVA(dir->MetaData.VirtualAddress, dir->MetaData.Size), dir->MetaData.Size );
4210
print_clr_directory( "Resources", &dir->Resources );
4211
print_clr_directory( "StrongNameSignature", &dir->StrongNameSignature );
4212
print_clr_directory( "CodeManagerTable", &dir->CodeManagerTable );
4213
print_clr_directory( "VTableFixups", &dir->VTableFixups );
4214
print_clr_directory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
4215
print_clr_directory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
4216
printf("\n");
4217
}
4218
4219
static void dump_dynamic_relocs_arm64x( const IMAGE_BASE_RELOCATION *base_reloc, unsigned int size )
4220
{
4221
unsigned int i;
4222
const IMAGE_BASE_RELOCATION *base_end = (const IMAGE_BASE_RELOCATION *)((const char *)base_reloc + size);
4223
4224
printf( "Relocations ARM64X\n" );
4225
while (base_reloc < base_end - 1 && base_reloc->SizeOfBlock)
4226
{
4227
const USHORT *rel = (const USHORT *)(base_reloc + 1);
4228
const USHORT *end = (const USHORT *)base_reloc + base_reloc->SizeOfBlock / sizeof(USHORT);
4229
printf( " Page %x\n", (UINT)base_reloc->VirtualAddress );
4230
while (rel < end && *rel)
4231
{
4232
USHORT offset = *rel & 0xfff;
4233
USHORT type = (*rel >> 12) & 3;
4234
USHORT arg = *rel >> 14;
4235
rel++;
4236
switch (type)
4237
{
4238
case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
4239
printf( " off %04x zero-fill %u bytes\n", offset, 1 << arg );
4240
break;
4241
case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
4242
printf( " off %04x set %u bytes value ", offset, 1 << arg );
4243
for (i = (1 << arg ) / sizeof(USHORT); i > 0; i--) printf( "%04x", rel[i - 1] );
4244
rel += (1 << arg) / sizeof(USHORT);
4245
printf( "\n" );
4246
break;
4247
case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
4248
printf( " off %04x add offset ", offset );
4249
if (arg & 1) printf( "-" );
4250
printf( "%08x\n", (UINT)*rel++ * ((arg & 2) ? 8 : 4) );
4251
break;
4252
default:
4253
printf( " off %04x unknown (arg %x)\n", offset, arg );
4254
break;
4255
}
4256
}
4257
base_reloc = (const IMAGE_BASE_RELOCATION *)end;
4258
}
4259
}
4260
4261
static void dump_dynamic_relocs( const char *ptr, unsigned int size, ULONGLONG symbol )
4262
{
4263
switch (symbol)
4264
{
4265
case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE:
4266
printf( "Relocations GUARD_RF_PROLOGUE\n" );
4267
break;
4268
case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE:
4269
printf( "Relocations GUARD_RF_EPILOGUE\n" );
4270
break;
4271
case IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER:
4272
printf( "Relocations GUARD_IMPORT_CONTROL_TRANSFER\n" );
4273
break;
4274
case IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER:
4275
printf( "Relocations GUARD_INDIR_CONTROL_TRANSFER\n" );
4276
break;
4277
case IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH:
4278
printf( "Relocations GUARD_SWITCHTABLE_BRANCH\n" );
4279
break;
4280
case IMAGE_DYNAMIC_RELOCATION_ARM64X:
4281
dump_dynamic_relocs_arm64x( (const IMAGE_BASE_RELOCATION *)ptr, size );
4282
break;
4283
default:
4284
printf( "Unknown relocation symbol %s\n", longlong_str(symbol) );
4285
break;
4286
}
4287
}
4288
4289
static const IMAGE_DYNAMIC_RELOCATION_TABLE *get_dyn_reloc_table(void)
4290
{
4291
unsigned int size, section, offset;
4292
const IMAGE_SECTION_HEADER *sec;
4293
const IMAGE_DYNAMIC_RELOCATION_TABLE *table;
4294
4295
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
4296
{
4297
const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
4298
if (!cfg) return NULL;
4299
size = min( size, cfg->Size );
4300
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTableSection )) return NULL;
4301
offset = cfg->DynamicValueRelocTableOffset;
4302
section = cfg->DynamicValueRelocTableSection;
4303
}
4304
else
4305
{
4306
const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
4307
if (!cfg) return NULL;
4308
size = min( size, cfg->Size );
4309
if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTableSection )) return NULL;
4310
offset = cfg->DynamicValueRelocTableOffset;
4311
section = cfg->DynamicValueRelocTableSection;
4312
}
4313
if (!section || section > PE_nt_headers->FileHeader.NumberOfSections) return NULL;
4314
sec = IMAGE_FIRST_SECTION( PE_nt_headers ) + section - 1;
4315
if (offset >= sec->SizeOfRawData) return NULL;
4316
return PRD( sec->PointerToRawData + offset, sizeof(*table) );
4317
}
4318
4319
static void dump_dir_dynamic_reloc(void)
4320
{
4321
const char *ptr, *end;
4322
const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
4323
4324
if (!table) return;
4325
4326
printf( "Dynamic relocations (version %u)\n\n", (UINT)table->Version );
4327
ptr = (const char *)(table + 1);
4328
end = ptr + table->Size;
4329
while (ptr < end)
4330
{
4331
switch (table->Version)
4332
{
4333
case 1:
4334
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
4335
{
4336
const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
4337
dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
4338
ptr += sizeof(*reloc) + reloc->BaseRelocSize;
4339
}
4340
else
4341
{
4342
const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
4343
dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
4344
ptr += sizeof(*reloc) + reloc->BaseRelocSize;
4345
}
4346
break;
4347
case 2:
4348
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
4349
{
4350
const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
4351
dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
4352
ptr += reloc->HeaderSize + reloc->FixupInfoSize;
4353
}
4354
else
4355
{
4356
const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
4357
dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
4358
ptr += reloc->HeaderSize + reloc->FixupInfoSize;
4359
}
4360
break;
4361
}
4362
}
4363
printf( "\n" );
4364
}
4365
4366
static const IMAGE_BASE_RELOCATION *get_armx_relocs( unsigned int *size )
4367
{
4368
const char *ptr, *end;
4369
const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
4370
4371
if (!table) return NULL;
4372
ptr = (const char *)(table + 1);
4373
end = ptr + table->Size;
4374
while (ptr < end)
4375
{
4376
switch (table->Version)
4377
{
4378
case 1:
4379
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
4380
{
4381
const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
4382
if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
4383
{
4384
*size = reloc->BaseRelocSize;
4385
return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
4386
}
4387
ptr += sizeof(*reloc) + reloc->BaseRelocSize;
4388
}
4389
else
4390
{
4391
const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
4392
if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
4393
{
4394
*size = reloc->BaseRelocSize;
4395
return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
4396
}
4397
ptr += sizeof(*reloc) + reloc->BaseRelocSize;
4398
}
4399
break;
4400
case 2:
4401
if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
4402
{
4403
const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
4404
if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
4405
{
4406
*size = reloc->FixupInfoSize;
4407
return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
4408
}
4409
ptr += reloc->HeaderSize + reloc->FixupInfoSize;
4410
}
4411
else
4412
{
4413
const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
4414
if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
4415
{
4416
*size = reloc->FixupInfoSize;
4417
return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
4418
}
4419
ptr += reloc->HeaderSize + reloc->FixupInfoSize;
4420
}
4421
break;
4422
}
4423
}
4424
return NULL;
4425
}
4426
4427
static BOOL get_alt_header( void )
4428
{
4429
unsigned int size;
4430
const IMAGE_BASE_RELOCATION *end, *reloc = get_armx_relocs( &size );
4431
4432
if (!reloc) return FALSE;
4433
end = (const IMAGE_BASE_RELOCATION *)((const char *)reloc + size);
4434
4435
while (reloc < end - 1 && reloc->SizeOfBlock)
4436
{
4437
const USHORT *rel = (const USHORT *)(reloc + 1);
4438
const USHORT *rel_end = (const USHORT *)reloc + reloc->SizeOfBlock / sizeof(USHORT);
4439
char *page = reloc->VirtualAddress ? (char *)RVA(reloc->VirtualAddress,1) : dump_base;
4440
4441
while (rel < rel_end && *rel)
4442
{
4443
USHORT offset = *rel & 0xfff;
4444
USHORT type = (*rel >> 12) & 3;
4445
USHORT arg = *rel >> 14;
4446
int val;
4447
rel++;
4448
switch (type)
4449
{
4450
case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
4451
memset( page + offset, 0, 1 << arg );
4452
break;
4453
case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
4454
memcpy( page + offset, rel, 1 << arg );
4455
rel += (1 << arg) / sizeof(USHORT);
4456
break;
4457
case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
4458
val = (unsigned int)*rel++ * ((arg & 2) ? 8 : 4);
4459
if (arg & 1) val = -val;
4460
*(int *)(page + offset) += val;
4461
break;
4462
}
4463
}
4464
reloc = (const IMAGE_BASE_RELOCATION *)rel_end;
4465
}
4466
return TRUE;
4467
}
4468
4469
static void dump_dir_reloc(void)
4470
{
4471
unsigned int i, size = 0;
4472
const USHORT *relocs;
4473
const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
4474
const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
4475
static const char * const names[] =
4476
{
4477
"BASED_ABSOLUTE",
4478
"BASED_HIGH",
4479
"BASED_LOW",
4480
"BASED_HIGHLOW",
4481
"BASED_HIGHADJ",
4482
"BASED_MIPS_JMPADDR",
4483
"BASED_SECTION",
4484
"BASED_REL",
4485
"unknown 8",
4486
"BASED_IA64_IMM64",
4487
"BASED_DIR64",
4488
"BASED_HIGH3ADJ",
4489
"unknown 12",
4490
"unknown 13",
4491
"unknown 14",
4492
"unknown 15"
4493
};
4494
4495
if (!rel) return;
4496
4497
printf( "Relocations\n" );
4498
while (rel < end - 1 && rel->SizeOfBlock)
4499
{
4500
printf( " Page %x\n", (UINT)rel->VirtualAddress );
4501
relocs = (const USHORT *)(rel + 1);
4502
i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
4503
while (i--)
4504
{
4505
USHORT offset = *relocs & 0xfff;
4506
int type = *relocs >> 12;
4507
printf( " off %04x type %s\n", offset, names[type] );
4508
relocs++;
4509
}
4510
rel = (const IMAGE_BASE_RELOCATION *)relocs;
4511
}
4512
printf("\n");
4513
}
4514
4515
static void dump_dir_tls(void)
4516
{
4517
IMAGE_TLS_DIRECTORY64 dir;
4518
const UINT *callbacks;
4519
const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
4520
4521
if (!pdir) return;
4522
4523
if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
4524
memcpy(&dir, pdir, sizeof(dir));
4525
else
4526
{
4527
dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
4528
dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
4529
dir.AddressOfIndex = pdir->AddressOfIndex;
4530
dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
4531
dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
4532
dir.Characteristics = pdir->Characteristics;
4533
}
4534
4535
/* FIXME: This does not properly handle large images */
4536
printf( "Thread Local Storage\n" );
4537
printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
4538
(UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
4539
(UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
4540
(UINT)dir.SizeOfZeroFill );
4541
printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
4542
printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
4543
printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
4544
if (dir.AddressOfCallBacks)
4545
{
4546
UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
4547
while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
4548
{
4549
printf( " %08x", *callbacks );
4550
addr += sizeof(UINT);
4551
}
4552
}
4553
printf(" }\n\n");
4554
}
4555
4556
enum FileSig get_kind_dbg(void)
4557
{
4558
const WORD* pw;
4559
4560
pw = PRD(0, sizeof(WORD));
4561
if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
4562
4563
if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
4564
return SIG_UNKNOWN;
4565
}
4566
4567
void dbg_dump(void)
4568
{
4569
const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
4570
unsigned nb_dbg;
4571
unsigned i;
4572
const IMAGE_DEBUG_DIRECTORY* debugDir;
4573
4574
separateDebugHead = PRD(0, sizeof(*separateDebugHead));
4575
if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
4576
4577
printf ("Signature: %.2s (0x%4X)\n",
4578
(const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
4579
printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
4580
printf ("Machine: 0x%04X (%s)\n",
4581
separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
4582
printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
4583
printf ("TimeDateStamp: 0x%08X (%s)\n",
4584
(UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
4585
printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
4586
printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
4587
printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
4588
printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
4589
printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
4590
printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
4591
4592
if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
4593
separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
4594
{printf("Can't get the sections, aborting\n"); return;}
4595
4596
dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
4597
4598
nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
4599
debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
4600
separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
4601
separateDebugHead->ExportedNamesSize,
4602
nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
4603
if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
4604
4605
printf("Debug Table (%u directories)\n", nb_dbg);
4606
4607
for (i = 0; i < nb_dbg; i++)
4608
{
4609
dump_dir_debug_dir(debugDir, i, (const IMAGE_SECTION_HEADER*)(separateDebugHead + 1));
4610
debugDir++;
4611
}
4612
}
4613
4614
static const char *get_resource_type( unsigned int id )
4615
{
4616
static const char * const types[] =
4617
{
4618
NULL,
4619
"CURSOR",
4620
"BITMAP",
4621
"ICON",
4622
"MENU",
4623
"DIALOG",
4624
"STRING",
4625
"FONTDIR",
4626
"FONT",
4627
"ACCELERATOR",
4628
"RCDATA",
4629
"MESSAGETABLE",
4630
"GROUP_CURSOR",
4631
NULL,
4632
"GROUP_ICON",
4633
NULL,
4634
"VERSION",
4635
"DLGINCLUDE",
4636
NULL,
4637
"PLUGPLAY",
4638
"VXD",
4639
"ANICURSOR",
4640
"ANIICON",
4641
"HTML",
4642
"MANIFEST"
4643
};
4644
4645
if ((size_t)id < ARRAY_SIZE(types)) return types[id];
4646
return NULL;
4647
}
4648
4649
/* dump data for a STRING resource */
4650
static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
4651
{
4652
int i;
4653
4654
for (i = 0; i < 16 && size; i++)
4655
{
4656
unsigned len = *ptr++;
4657
4658
if (len >= size)
4659
{
4660
len = size;
4661
size = 0;
4662
}
4663
else size -= len + 1;
4664
4665
if (len)
4666
{
4667
printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
4668
dump_strW( ptr, len );
4669
printf( "\"\n" );
4670
ptr += len;
4671
}
4672
}
4673
}
4674
4675
/* dump data for a MESSAGETABLE resource */
4676
static void dump_msgtable_data( const void *ptr, unsigned int size, const char *prefix )
4677
{
4678
const MESSAGE_RESOURCE_DATA *data = ptr;
4679
const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
4680
unsigned i, j;
4681
4682
for (i = 0; i < data->NumberOfBlocks; i++, block++)
4683
{
4684
const MESSAGE_RESOURCE_ENTRY *entry;
4685
4686
entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
4687
for (j = block->LowId; j <= block->HighId; j++)
4688
{
4689
if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
4690
{
4691
const WCHAR *str = (const WCHAR *)entry->Text;
4692
printf( "%s%08x L\"", prefix, j );
4693
dump_strW( str, strlenW(str) );
4694
printf( "\"\n" );
4695
}
4696
else
4697
{
4698
const char *str = (const char *) entry->Text;
4699
printf( "%s%08x \"", prefix, j );
4700
dump_strA( str, strlen(str) );
4701
printf( "\"\n" );
4702
}
4703
entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
4704
}
4705
}
4706
}
4707
4708
struct version_info
4709
{
4710
WORD len;
4711
WORD val_len;
4712
WORD type;
4713
WCHAR key[1];
4714
};
4715
#define GET_VALUE(info) ((void *)((char *)info + ((offsetof(struct version_info, key[strlenW(info->key) + 1]) + 3) & ~3)))
4716
#define GET_CHILD(info) ((void *)((char *)GET_VALUE(info) + ((info->val_len * (info->type ? 2 : 1) + 3) & ~3)))
4717
#define GET_NEXT(info) ((void *)((char *)info + ((info->len + 3) & ~3)))
4718
4719
static void dump_version_children( const struct version_info *info, const char *prefix, int indent )
4720
{
4721
const struct version_info *next, *child = GET_CHILD( info );
4722
4723
for ( ; (char *)child < (char *)info + info->len; child = next)
4724
{
4725
next = GET_NEXT( child );
4726
printf( "%s%*s", prefix, indent * 2, "" );
4727
if (child->val_len || GET_VALUE( child ) == next)
4728
{
4729
printf( "VALUE \"" );
4730
dump_strW( child->key, strlenW(child->key) );
4731
if (child->type)
4732
{
4733
printf( "\", \"" );
4734
dump_strW( GET_VALUE(child), child->val_len );
4735
printf( "\"\n" );
4736
}
4737
else
4738
{
4739
const WORD *data = GET_VALUE(child);
4740
unsigned int i;
4741
printf( "\"," );
4742
for (i = 0; i < child->val_len / sizeof(WORD); i++) printf( " %#x", data[i] );
4743
printf( "\n" );
4744
}
4745
}
4746
else
4747
{
4748
printf( "BLOCK \"" );
4749
dump_strW( child->key, strlenW(child->key) );
4750
printf( "\"\n" );
4751
}
4752
dump_version_children( child, prefix, indent + 1 );
4753
}
4754
}
4755
4756
/* dump data for a VERSION resource */
4757
static void dump_version_data( const void *ptr, unsigned int size, const char *prefix )
4758
{
4759
const struct version_info *info = ptr;
4760
const VS_FIXEDFILEINFO *fileinfo = GET_VALUE( info );
4761
4762
printf( "%sSIGNATURE %08x\n", prefix, (UINT)fileinfo->dwSignature );
4763
printf( "%sVERSION %u.%u\n", prefix,
4764
HIWORD(fileinfo->dwStrucVersion), LOWORD(fileinfo->dwStrucVersion) );
4765
printf( "%sFILEVERSION %u.%u.%u.%u\n", prefix,
4766
HIWORD(fileinfo->dwFileVersionMS), LOWORD(fileinfo->dwFileVersionMS),
4767
HIWORD(fileinfo->dwFileVersionLS), LOWORD(fileinfo->dwFileVersionLS) );
4768
printf( "%sPRODUCTVERSION %u.%u.%u.%u\n", prefix,
4769
HIWORD(fileinfo->dwProductVersionMS), LOWORD(fileinfo->dwProductVersionMS),
4770
HIWORD(fileinfo->dwProductVersionLS), LOWORD(fileinfo->dwProductVersionLS) );
4771
printf( "%sFILEFLAGSMASK %08x\n", prefix, (UINT)fileinfo->dwFileFlagsMask );
4772
printf( "%sFILEFLAGS %08x\n", prefix, (UINT)fileinfo->dwFileFlags );
4773
4774
switch (fileinfo->dwFileOS)
4775
{
4776
#define CASE(x) case x: printf( "%sFILEOS %s\n", prefix, #x ); break
4777
CASE(VOS_UNKNOWN);
4778
CASE(VOS_DOS_WINDOWS16);
4779
CASE(VOS_DOS_WINDOWS32);
4780
CASE(VOS_OS216_PM16);
4781
CASE(VOS_OS232_PM32);
4782
CASE(VOS_NT_WINDOWS32);
4783
#undef CASE
4784
default:
4785
printf( "%sFILEOS %u.%u\n", prefix,
4786
(WORD)(fileinfo->dwFileOS >> 16), (WORD)fileinfo->dwFileOS );
4787
break;
4788
}
4789
4790
switch (fileinfo->dwFileType)
4791
{
4792
#define CASE(x) case x: printf( "%sFILETYPE %s\n", prefix, #x ); break
4793
CASE(VFT_UNKNOWN);
4794
CASE(VFT_APP);
4795
CASE(VFT_DLL);
4796
CASE(VFT_DRV);
4797
CASE(VFT_FONT);
4798
CASE(VFT_VXD);
4799
CASE(VFT_STATIC_LIB);
4800
#undef CASE
4801
default:
4802
printf( "%sFILETYPE %08x\n", prefix, (UINT)fileinfo->dwFileType );
4803
break;
4804
}
4805
4806
switch (((ULONGLONG)fileinfo->dwFileType << 32) + fileinfo->dwFileSubtype)
4807
{
4808
#define CASE(t,x) case (((ULONGLONG)t << 32) + x): printf( "%sFILESUBTYPE %s\n", prefix, #x ); break
4809
CASE(VFT_DRV, VFT2_UNKNOWN);
4810
CASE(VFT_DRV, VFT2_DRV_PRINTER);
4811
CASE(VFT_DRV, VFT2_DRV_KEYBOARD);
4812
CASE(VFT_DRV, VFT2_DRV_LANGUAGE);
4813
CASE(VFT_DRV, VFT2_DRV_DISPLAY);
4814
CASE(VFT_DRV, VFT2_DRV_MOUSE);
4815
CASE(VFT_DRV, VFT2_DRV_NETWORK);
4816
CASE(VFT_DRV, VFT2_DRV_SYSTEM);
4817
CASE(VFT_DRV, VFT2_DRV_INSTALLABLE);
4818
CASE(VFT_DRV, VFT2_DRV_SOUND);
4819
CASE(VFT_DRV, VFT2_DRV_COMM);
4820
CASE(VFT_DRV, VFT2_DRV_INPUTMETHOD);
4821
CASE(VFT_DRV, VFT2_DRV_VERSIONED_PRINTER);
4822
CASE(VFT_FONT, VFT2_FONT_RASTER);
4823
CASE(VFT_FONT, VFT2_FONT_VECTOR);
4824
CASE(VFT_FONT, VFT2_FONT_TRUETYPE);
4825
#undef CASE
4826
default:
4827
printf( "%sFILESUBTYPE %08x\n", prefix, (UINT)fileinfo->dwFileSubtype );
4828
break;
4829
}
4830
4831
printf( "%sFILEDATE %08x.%08x\n", prefix,
4832
(UINT)fileinfo->dwFileDateMS, (UINT)fileinfo->dwFileDateLS );
4833
dump_version_children( info, prefix, 0 );
4834
}
4835
4836
/* dump data for a HTML/MANIFEST resource */
4837
static void dump_text_data( const void *ptr, unsigned int size, const char *prefix )
4838
{
4839
const char *p = ptr, *end = p + size;
4840
4841
while (p < end)
4842
{
4843
const char *start = p;
4844
while (p < end && *p != '\r' && *p != '\n') p++;
4845
printf( "%s%.*s\n", prefix, (int)(p - start), start );
4846
while (p < end && (*p == '\r' || *p == '\n')) p++;
4847
}
4848
}
4849
4850
/* dump data for an MUI resource */
4851
static void dump_mui_data( const void *ptr, unsigned int size, const char *prefix )
4852
{
4853
UINT i;
4854
4855
const struct mui_resource
4856
{
4857
UINT signature;
4858
UINT size;
4859
UINT version;
4860
UINT path_type;
4861
UINT file_type;
4862
UINT system_attributes;
4863
UINT fallback_location;
4864
BYTE service_checksum[16];
4865
BYTE checksum[16];
4866
UINT unk1[2];
4867
UINT mui_path_off;
4868
UINT mui_path_size;
4869
UINT unk2[2];
4870
UINT ln_type_name_off;
4871
UINT ln_type_name_size;
4872
UINT ln_type_id_off;
4873
UINT ln_type_id_size;
4874
UINT mui_type_name_off;
4875
UINT mui_type_name_size;
4876
UINT mui_type_id_off;
4877
UINT mui_type_id_size;
4878
UINT lang_off;
4879
UINT lang_size;
4880
UINT fallback_lang_off;
4881
UINT fallback_lang_size;
4882
} *data = ptr;
4883
4884
printf( "%sSignature: %x\n", prefix, data->signature );
4885
printf( "%sVersion: %u.%u\n", prefix, HIWORD(data->version), LOWORD(data->version) );
4886
printf( "%sPath type: %x\n", prefix, data->path_type );
4887
printf( "%sFile type: %x\n", prefix, data->file_type );
4888
printf( "%sAttributes: %x\n", prefix, data->system_attributes );
4889
printf( "%sFallback: %x\n", prefix, data->fallback_location );
4890
printf( "%sChecksum: ", prefix );
4891
for (i = 0; i < sizeof(data->checksum); i++) printf( "%02x", data->checksum[i] );
4892
printf( "\n%sSvc checksum: ", prefix );
4893
for (i = 0; i < sizeof(data->service_checksum); i++) printf( "%02x", data->service_checksum[i] );
4894
printf( "\n" );
4895
printf( "%sUnknown1: %08x %08x\n", prefix, data->unk1[0], data->unk1[1] );
4896
printf( "%sUnknown2: %08x %08x\n", prefix, data->unk2[0], data->unk2[1] );
4897
4898
if (data->mui_path_off)
4899
{
4900
const WCHAR *name = (WCHAR *)((char *)data + data->mui_path_off);
4901
printf( "%sMUI path: ", prefix );
4902
dump_strW( name, strlenW(name) );
4903
printf( "\n" );
4904
}
4905
4906
printf( "%sResources: {", prefix );
4907
if (data->ln_type_name_off)
4908
{
4909
const WCHAR *names = (WCHAR *)((char *)data + data->ln_type_name_off);
4910
while (*names)
4911
{
4912
printf( " " );
4913
dump_strW( names, strlenW(names) );
4914
names += strlenW( names ) + 1;
4915
}
4916
}
4917
if (data->ln_type_id_off)
4918
{
4919
const UINT *types = (UINT *)((char *)data + data->ln_type_id_off);
4920
for (i = 0; i < data->ln_type_id_size / sizeof(*types); i++)
4921
{
4922
const char *type = get_resource_type( types[i] );
4923
if (type) printf( " %s", type );
4924
else printf( " %04x", types[i] );
4925
}
4926
}
4927
printf( " }\n" );
4928
4929
printf( "%sMUI resources: {", prefix );
4930
if (data->mui_type_name_off)
4931
{
4932
const WCHAR *names = (WCHAR *)((char *)data + data->mui_type_name_off);
4933
while (*names)
4934
{
4935
printf( " " );
4936
dump_strW( names, strlenW(names) );
4937
names += strlenW( names ) + 1;
4938
}
4939
}
4940
if (data->mui_type_id_off)
4941
{
4942
const UINT *types = (UINT *)((char *)data + data->mui_type_id_off);
4943
for (i = 0; i < data->mui_type_id_size / sizeof(*types); i++)
4944
{
4945
const char *type = get_resource_type( types[i] );
4946
if (type) printf( " %s", type );
4947
else printf( " %04x", types[i] );
4948
}
4949
}
4950
printf( " }\n" );
4951
4952
if (data->lang_off)
4953
{
4954
const WCHAR *name = (WCHAR *)((char *)data + data->lang_off);
4955
printf( "%sLanguage: ", prefix );
4956
dump_strW( name, strlenW(name) );
4957
printf( "\n" );
4958
}
4959
4960
if (data->fallback_lang_off)
4961
{
4962
const WCHAR *name = (WCHAR *)((char *)data + data->fallback_lang_off);
4963
printf( "%sFallback lang: ", prefix );
4964
dump_strW( name, strlenW(name) );
4965
printf( "\n" );
4966
}
4967
dump_data( ptr, size, prefix );
4968
}
4969
4970
static int cmp_resource_name( const IMAGE_RESOURCE_DIR_STRING_U *str_res, const char *str )
4971
{
4972
unsigned int i;
4973
4974
for (i = 0; i < str_res->Length; i++)
4975
{
4976
int res = str_res->NameString[i] - (WCHAR)str[i];
4977
if (res || !str[i]) return res;
4978
}
4979
return -(WCHAR)str[i];
4980
}
4981
4982
static void dump_dir_resource(void)
4983
{
4984
const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
4985
const IMAGE_RESOURCE_DIRECTORY *namedir;
4986
const IMAGE_RESOURCE_DIRECTORY *langdir;
4987
const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
4988
const IMAGE_RESOURCE_DIR_STRING_U *string;
4989
const IMAGE_RESOURCE_DATA_ENTRY *data;
4990
int i, j, k;
4991
4992
if (!root) return;
4993
4994
printf( "Resources:" );
4995
4996
for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
4997
{
4998
e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
4999
namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
5000
for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
5001
{
5002
e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
5003
langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
5004
for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
5005
{
5006
e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
5007
5008
printf( "\n " );
5009
if (e1->NameIsString)
5010
{
5011
string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
5012
dump_strW( string->NameString, string->Length );
5013
}
5014
else
5015
{
5016
const char *type = get_resource_type( e1->Id );
5017
if (type) printf( "%s", type );
5018
else printf( "%04x", e1->Id );
5019
}
5020
5021
printf( " Name=" );
5022
if (e2->NameIsString)
5023
{
5024
string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
5025
dump_strW( string->NameString, string->Length );
5026
}
5027
else
5028
printf( "%04x", e2->Id );
5029
5030
printf( " Language=%04x:\n", e3->Id );
5031
data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
5032
if (e1->NameIsString)
5033
{
5034
string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
5035
if (!cmp_resource_name( string, "TYPELIB" ))
5036
tlb_dump_resource( (void *)RVA( data->OffsetToData, data->Size ), data->Size, " | " );
5037
else if (!cmp_resource_name( string, "MUI" ))
5038
dump_mui_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
5039
else if (!cmp_resource_name( string, "REGISTRY" ) ||
5040
!cmp_resource_name( string, "WINE_REGISTRY" ))
5041
dump_text_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
5042
else
5043
dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
5044
}
5045
else switch(e1->Id)
5046
{
5047
case 6: /* RT_STRING */
5048
dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
5049
break;
5050
case 11: /* RT_MESSAGETABLE */
5051
dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
5052
break;
5053
case 16: /* RT_VERSION */
5054
dump_version_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
5055
break;
5056
case 23: /* RT_HTML */
5057
case 24: /* RT_MANIFEST */
5058
dump_text_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
5059
break;
5060
default:
5061
dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
5062
break;
5063
}
5064
}
5065
}
5066
}
5067
printf( "\n\n" );
5068
}
5069
5070
static void dump_debug(void)
5071
{
5072
const char* stabs = NULL;
5073
unsigned szstabs = 0;
5074
const char* stabstr = NULL;
5075
unsigned szstr = 0;
5076
unsigned i;
5077
const IMAGE_SECTION_HEADER* sectHead;
5078
5079
sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
5080
5081
for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
5082
{
5083
if (!strcmp((const char *)sectHead->Name, ".stab"))
5084
{
5085
stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
5086
szstabs = sectHead->Misc.VirtualSize;
5087
}
5088
if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
5089
{
5090
stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
5091
szstr = sectHead->Misc.VirtualSize;
5092
}
5093
}
5094
if (stabs && stabstr)
5095
dump_stabs(stabs, szstabs, stabstr, szstr);
5096
}
5097
5098
static void dump_symbol_table(void)
5099
{
5100
const IMAGE_SYMBOL* sym;
5101
int numsym;
5102
5103
numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
5104
if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
5105
return;
5106
sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
5107
sizeof(*sym) * numsym);
5108
if (!sym) return;
5109
5110
dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
5111
}
5112
5113
static void dump_embedded_ne_module( UINT rva )
5114
{
5115
const void *ne_module = RVA( rva, 1 );
5116
void *new_base;
5117
5118
dump_total_len -= (char *)ne_module - (char *)dump_base;
5119
new_base = xmalloc( dump_total_len );
5120
memcpy( new_base, ne_module, dump_total_len );
5121
free( dump_base );
5122
dump_base = new_base;
5123
ne_dump();
5124
}
5125
5126
enum FileSig get_kind_exec(void)
5127
{
5128
const WORD* pw;
5129
const DWORD* pdw;
5130
const IMAGE_DOS_HEADER* dh;
5131
5132
pw = PRD(0, sizeof(WORD));
5133
if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
5134
5135
if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
5136
5137
if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
5138
{
5139
/* the signature is the first DWORD */
5140
pdw = PRD(dh->e_lfanew, sizeof(DWORD));
5141
if (pdw)
5142
{
5143
if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
5144
if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
5145
if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
5146
}
5147
return SIG_DOS;
5148
}
5149
return SIG_UNKNOWN;
5150
}
5151
5152
void pe_dump(void)
5153
{
5154
int alt = 0;
5155
UINT rva;
5156
5157
PE_nt_headers = get_nt_header();
5158
print_fake_dll();
5159
5160
for (;;)
5161
{
5162
if (alt)
5163
printf( "\n**** Alternate (%s) data ****\n\n",
5164
get_machine_str(PE_nt_headers->FileHeader.Machine));
5165
else if (get_dyn_reloc_table())
5166
printf( "**** Native (%s) data ****\n\n",
5167
get_machine_str(PE_nt_headers->FileHeader.Machine));
5168
5169
if (globals.do_dumpheader)
5170
{
5171
dump_pe_header();
5172
/* FIXME: should check ptr */
5173
dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
5174
PE_nt_headers->FileHeader.NumberOfSections);
5175
}
5176
else if (!globals.dumpsect)
5177
{
5178
/* show at least something here */
5179
dump_pe_header();
5180
}
5181
5182
if (globals_dump_sect("import"))
5183
{
5184
dump_dir_imported_functions();
5185
dump_dir_delay_imported_functions();
5186
}
5187
if (globals_dump_sect("export"))
5188
dump_dir_exported_functions();
5189
if (globals_dump_sect("debug"))
5190
dump_dir_debug();
5191
if (globals_dump_sect("resource"))
5192
dump_dir_resource();
5193
if (globals_dump_sect("tls"))
5194
dump_dir_tls();
5195
if (globals_dump_sect("loadcfg"))
5196
dump_dir_loadconfig();
5197
if (globals_dump_sect("clr"))
5198
dump_dir_clr_header();
5199
if (globals_dump_sect("reloc"))
5200
dump_dir_reloc();
5201
if (globals_dump_sect("dynreloc"))
5202
dump_dir_dynamic_reloc();
5203
if (globals_dump_sect("except"))
5204
dump_dir_exceptions();
5205
if (globals_dump_sect("apiset"))
5206
dump_section_apiset();
5207
5208
if (globals.do_symbol_table)
5209
dump_symbol_table();
5210
if (globals.do_debug)
5211
dump_debug();
5212
if (PE_nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_I386 &&
5213
(rva = get_export_rva( "__wine_spec_dos_header" )))
5214
{
5215
printf( "\n*** 16-bit Wine dll, dumping embedded NE module ***\n\n" );
5216
dump_embedded_ne_module( rva );
5217
break;
5218
}
5219
if (alt++) break;
5220
if (!get_alt_header()) break;
5221
}
5222
}
5223
5224
typedef struct _dll_symbol {
5225
size_t ordinal;
5226
char *symbol;
5227
} dll_symbol;
5228
5229
static dll_symbol *dll_symbols = NULL;
5230
static dll_symbol *dll_current_symbol = NULL;
5231
5232
/* Compare symbols by ordinal for qsort */
5233
static int symbol_cmp(const void *left, const void *right)
5234
{
5235
return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
5236
}
5237
5238
/*******************************************************************
5239
* dll_close
5240
*
5241
* Free resources used by DLL
5242
*/
5243
/* FIXME: Not used yet
5244
static void dll_close (void)
5245
{
5246
dll_symbol* ds;
5247
5248
if (!dll_symbols) {
5249
fatal("No symbols");
5250
}
5251
for (ds = dll_symbols; ds->symbol; ds++)
5252
free(ds->symbol);
5253
free (dll_symbols);
5254
dll_symbols = NULL;
5255
}
5256
*/
5257
5258
static void do_grab_sym( void )
5259
{
5260
const IMAGE_EXPORT_DIRECTORY*exportDir;
5261
UINT i, j, *map;
5262
const UINT *pName;
5263
const UINT *pFunc;
5264
const WORD *pOrdl;
5265
const char *ptr;
5266
5267
PE_nt_headers = get_nt_header();
5268
if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
5269
5270
pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
5271
if (!pName) {printf("Can't grab functions' name table\n"); return;}
5272
pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
5273
if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
5274
pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
5275
if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
5276
5277
/* dll_close(); */
5278
5279
dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
5280
5281
/* bit map of used funcs */
5282
map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
5283
if (!map) fatal("no memory");
5284
5285
for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
5286
{
5287
map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
5288
ptr = RVA(*pName++, sizeof(DWORD));
5289
if (!ptr) ptr = "cant_get_function";
5290
dll_symbols[j].symbol = xstrdup(ptr);
5291
dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
5292
assert(dll_symbols[j].symbol);
5293
}
5294
5295
for (i = 0; i < exportDir->NumberOfFunctions; i++)
5296
{
5297
if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
5298
{
5299
char ordinal_text[256];
5300
/* Ordinal only entry */
5301
sprintf (ordinal_text, "%s_%u",
5302
globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
5303
(UINT)exportDir->Base + i);
5304
str_toupper(ordinal_text);
5305
dll_symbols[j].symbol = xstrdup(ordinal_text);
5306
assert(dll_symbols[j].symbol);
5307
dll_symbols[j].ordinal = exportDir->Base + i;
5308
j++;
5309
assert(j <= exportDir->NumberOfFunctions);
5310
}
5311
}
5312
free(map);
5313
5314
if (NORMAL)
5315
printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
5316
(UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
5317
j, (UINT)exportDir->Base);
5318
5319
qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
5320
5321
dll_symbols[j].symbol = NULL;
5322
5323
dll_current_symbol = dll_symbols;
5324
}
5325
5326
/*******************************************************************
5327
* dll_open
5328
*
5329
* Open a DLL and read in exported symbols
5330
*/
5331
BOOL dll_open (const char *dll_name)
5332
{
5333
return dump_analysis(dll_name, do_grab_sym, SIG_PE);
5334
}
5335
5336
/*******************************************************************
5337
* dll_next_symbol
5338
*
5339
* Get next exported symbol from dll
5340
*/
5341
BOOL dll_next_symbol (parsed_symbol * sym)
5342
{
5343
if (!dll_current_symbol || !dll_current_symbol->symbol)
5344
return FALSE;
5345
assert (dll_symbols);
5346
sym->symbol = xstrdup (dll_current_symbol->symbol);
5347
sym->ordinal = dll_current_symbol->ordinal;
5348
dll_current_symbol++;
5349
return TRUE;
5350
}
5351
5352