mmdevapi: Query MemoryWineUnixFuncs virtual memory and store the resulting handle.
[wine.git] / tools / winedump / pe.c
blob34b34217d175f80cf4ac8cc34259abcb9a8d3943
1 /*
2 * PE dumping utility
4 * Copyright 2001 Eric Pouech
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.
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.
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <fcntl.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winedump.h"
33 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
35 static const IMAGE_NT_HEADERS32* PE_nt_headers;
36 static const IMAGE_NT_HEADERS32* PE_alt_headers; /* alternative headers for hybrid dlls */
38 static const char builtin_signature[] = "Wine builtin DLL";
39 static const char fakedll_signature[] = "Wine placeholder DLL";
40 static int is_builtin;
42 const char *get_machine_str(int mach)
44 switch (mach)
46 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
47 case IMAGE_FILE_MACHINE_I860: return "i860";
48 case IMAGE_FILE_MACHINE_I386: return "i386";
49 case IMAGE_FILE_MACHINE_R3000: return "R3000";
50 case IMAGE_FILE_MACHINE_R4000: return "R4000";
51 case IMAGE_FILE_MACHINE_R10000: return "R10000";
52 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
53 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
54 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
55 case IMAGE_FILE_MACHINE_IA64: return "IA64";
56 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
57 case IMAGE_FILE_MACHINE_ARM: return "ARM";
58 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
59 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
61 return "???";
64 static const void* RVA(unsigned long rva, unsigned long len)
66 IMAGE_SECTION_HEADER* sectHead;
67 int i;
69 if (rva == 0) return NULL;
71 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
72 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
74 if (sectHead[i].VirtualAddress <= rva &&
75 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
77 /* return image import directory offset */
78 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
82 return NULL;
85 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
87 const IMAGE_DOS_HEADER *dos;
88 dos = PRD(0, sizeof(*dos));
89 if (!dos) return NULL;
90 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
91 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
92 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
95 void print_fake_dll( void )
97 const IMAGE_DOS_HEADER *dos;
99 dos = PRD(0, sizeof(*dos) + 32);
100 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
102 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
103 printf( "*** This is a Wine builtin DLL ***\n\n" );
104 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
105 printf( "*** This is a Wine fake DLL ***\n\n" );
109 static const void *get_data_dir(const IMAGE_NT_HEADERS32 *hdr, unsigned int idx, unsigned int *size)
111 if(hdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
113 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&hdr->OptionalHeader;
114 if (idx >= opt->NumberOfRvaAndSizes)
115 return NULL;
116 if(size)
117 *size = opt->DataDirectory[idx].Size;
118 return RVA(opt->DataDirectory[idx].VirtualAddress,
119 opt->DataDirectory[idx].Size);
121 else
123 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&hdr->OptionalHeader;
124 if (idx >= opt->NumberOfRvaAndSizes)
125 return NULL;
126 if(size)
127 *size = opt->DataDirectory[idx].Size;
128 return RVA(opt->DataDirectory[idx].VirtualAddress,
129 opt->DataDirectory[idx].Size);
133 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
135 return get_data_dir( PE_nt_headers, idx, size );
138 static const void* get_dir(unsigned idx)
140 return get_dir_and_size(idx, 0);
143 static const void *get_alt_dir_and_size(unsigned int idx, unsigned int *size)
145 const void *dir;
146 if (!PE_alt_headers) return NULL;
147 dir = get_data_dir( PE_alt_headers, idx, size );
148 if (dir == get_dir(idx)) return NULL;
149 return dir;
152 static const char * const DirectoryNames[16] = {
153 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
154 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
155 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
156 "IAT", "Delay IAT", "CLR Header", ""
159 static const char *get_magic_type(WORD magic)
161 switch(magic) {
162 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
163 return "32bit";
164 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
165 return "64bit";
166 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
167 return "ROM";
169 return "???";
172 static const void *get_hybrid_metadata(void)
174 unsigned int size;
176 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
178 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
179 if (!cfg) return 0;
180 size = min( size, cfg->Size );
181 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CHPEMetadataPointer )) return 0;
182 return RVA( cfg->CHPEMetadataPointer - ((const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader)->ImageBase, 1 );
184 else
186 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
187 if (!cfg) return 0;
188 size = min( size, cfg->Size );
189 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CHPEMetadataPointer )) return 0;
190 return RVA( cfg->CHPEMetadataPointer - PE_nt_headers->OptionalHeader.ImageBase, 1 );
194 static inline const char *longlong_str( ULONGLONG value )
196 static char buffer[20];
198 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
199 sprintf(buffer, "%lx%08lx", (unsigned long)(value >> 32), (unsigned long)value);
200 else
201 sprintf(buffer, "%lx", (unsigned long)value);
202 return buffer;
205 static inline void print_word(const char *title, WORD value)
207 printf(" %-34s 0x%-4X %u\n", title, value, value);
210 static inline void print_dword(const char *title, UINT value)
212 printf(" %-34s 0x%-8x %u\n", title, value, value);
215 static inline void print_longlong(const char *title, ULONGLONG value)
217 printf(" %-34s 0x%s\n", title, longlong_str(value));
220 static inline void print_ver(const char *title, BYTE major, BYTE minor)
222 printf(" %-34s %u.%02u\n", title, major, minor);
225 static inline void print_subsys(const char *title, WORD value)
227 const char *str;
228 switch (value)
230 default:
231 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
232 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
233 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
234 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
235 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
236 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
237 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
238 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
239 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
240 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
241 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
242 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
243 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
244 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
246 printf(" %-34s 0x%X (%s)\n", title, value, str);
249 static inline void print_dllflags(const char *title, WORD value)
251 printf(" %-34s 0x%04X\n", title, value);
252 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
253 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
254 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
255 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
256 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
257 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
258 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
259 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
260 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
261 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
262 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
263 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
264 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
265 #undef X
268 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
270 unsigned i;
271 printf("Data Directory\n");
273 for (i = 0; i < n && i < 16; i++)
275 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
276 DirectoryNames[i], (UINT)directory[i].VirtualAddress,
277 (UINT)directory[i].Size);
281 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
283 IMAGE_OPTIONAL_HEADER32 oh;
284 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
286 /* in case optional header is missing or partial */
287 memset(&oh, 0, sizeof(oh));
288 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
289 optionalHeader = &oh;
291 print_word("Magic", optionalHeader->Magic);
292 print_ver("linker version",
293 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
294 print_dword("size of code", optionalHeader->SizeOfCode);
295 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
296 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
297 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
298 print_dword("base of code", optionalHeader->BaseOfCode);
299 print_dword("base of data", optionalHeader->BaseOfData);
300 print_dword("image base", optionalHeader->ImageBase);
301 print_dword("section align", optionalHeader->SectionAlignment);
302 print_dword("file align", optionalHeader->FileAlignment);
303 print_ver("required OS version",
304 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
305 print_ver("image version",
306 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
307 print_ver("subsystem version",
308 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
309 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
310 print_dword("size of image", optionalHeader->SizeOfImage);
311 print_dword("size of headers", optionalHeader->SizeOfHeaders);
312 print_dword("checksum", optionalHeader->CheckSum);
313 print_subsys("Subsystem", optionalHeader->Subsystem);
314 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
315 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
316 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
317 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
318 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
319 print_dword("loader flags", optionalHeader->LoaderFlags);
320 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
321 printf("\n");
322 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
323 printf("\n");
326 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
328 IMAGE_OPTIONAL_HEADER64 oh;
329 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
331 /* in case optional header is missing or partial */
332 memset(&oh, 0, sizeof(oh));
333 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
334 optionalHeader = &oh;
336 print_word("Magic", optionalHeader->Magic);
337 print_ver("linker version",
338 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
339 print_dword("size of code", optionalHeader->SizeOfCode);
340 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
341 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
342 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
343 print_dword("base of code", optionalHeader->BaseOfCode);
344 print_longlong("image base", optionalHeader->ImageBase);
345 print_dword("section align", optionalHeader->SectionAlignment);
346 print_dword("file align", optionalHeader->FileAlignment);
347 print_ver("required OS version",
348 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
349 print_ver("image version",
350 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
351 print_ver("subsystem version",
352 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
353 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
354 print_dword("size of image", optionalHeader->SizeOfImage);
355 print_dword("size of headers", optionalHeader->SizeOfHeaders);
356 print_dword("checksum", optionalHeader->CheckSum);
357 print_subsys("Subsystem", optionalHeader->Subsystem);
358 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
359 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
360 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
361 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
362 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
363 print_dword("loader flags", optionalHeader->LoaderFlags);
364 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
365 printf("\n");
366 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
367 printf("\n");
370 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
372 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
374 switch(optionalHeader->Magic) {
375 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
376 dump_optional_header32(optionalHeader, header_size);
377 break;
378 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
379 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
380 break;
381 default:
382 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
383 break;
387 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader, BOOL is_hybrid)
389 const char *name = get_machine_str(fileHeader->Machine);
391 printf("File Header\n");
393 if (is_hybrid)
395 switch (fileHeader->Machine)
397 case IMAGE_FILE_MACHINE_I386: name = "CHPE"; break;
398 case IMAGE_FILE_MACHINE_AMD64: name = "ARM64EC"; break;
399 case IMAGE_FILE_MACHINE_ARM64: name = "ARM64X"; break;
402 printf(" Machine: %04X (%s)\n", fileHeader->Machine, name);
403 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
404 printf(" TimeDateStamp: %08X (%s)\n",
405 (UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp));
406 printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
407 printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
408 printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
409 printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
410 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
411 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
412 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
413 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
414 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
415 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
416 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
417 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
418 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
419 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
420 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
421 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
422 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
423 X(IMAGE_FILE_SYSTEM, "SYSTEM");
424 X(IMAGE_FILE_DLL, "DLL");
425 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
426 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
427 #undef X
428 printf("\n");
431 static void dump_pe_header(void)
433 dump_file_header(&PE_nt_headers->FileHeader, get_hybrid_metadata() != NULL);
434 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader,
435 PE_nt_headers->FileHeader.SizeOfOptionalHeader);
436 if (!PE_alt_headers) return;
437 printf( "Alternate Headers\n\n");
438 dump_file_header(&PE_alt_headers->FileHeader, FALSE);
439 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_alt_headers->OptionalHeader,
440 PE_alt_headers->FileHeader.SizeOfOptionalHeader);
443 void dump_section_characteristics(DWORD characteristics, const char* sep)
445 #define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
446 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
447 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
448 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
449 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
450 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
451 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
453 X(IMAGE_SCN_CNT_CODE, "CODE");
454 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
455 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
457 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
458 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
459 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
460 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
461 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
463 /* 0x00002000 - Reserved */
464 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
465 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
467 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
468 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
469 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
470 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
471 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
473 switch (characteristics & IMAGE_SCN_ALIGN_MASK)
475 #define X2(b,s) case b: printf("%s%s", sep, s); break
476 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
477 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
478 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
479 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
480 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
481 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
482 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
483 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
484 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
485 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
486 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
487 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
488 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
489 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
490 #undef X2
493 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
495 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
496 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
497 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
498 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
499 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
500 X(IMAGE_SCN_MEM_READ, "MEM_READ");
501 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
502 #undef X
505 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
507 unsigned offset;
509 /* long section name ? */
510 if (strtable && sectHead->Name[0] == '/' &&
511 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
512 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
513 else
514 printf(" %-8.8s", sectHead->Name);
515 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
516 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
517 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
518 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
519 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
520 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
521 printf(" line # offs: %-8u line #'s: %-8u\n",
522 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
523 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
524 printf(" ");
525 dump_section_characteristics(sectHead->Characteristics, " ");
527 printf("\n\n");
530 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
532 const IMAGE_SECTION_HEADER* sectHead = addr;
533 unsigned i;
534 const char* strtable;
536 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
538 strtable = (const char*)base +
539 PE_nt_headers->FileHeader.PointerToSymbolTable +
540 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
542 else strtable = NULL;
544 printf("Section Table\n");
545 for (i = 0; i < num_sect; i++, sectHead++)
547 dump_section(sectHead, strtable);
549 if (globals.do_dump_rawdata)
551 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
552 printf("\n");
557 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
559 const WCHAR *wstr = PRD( rva, len );
560 char *ret = buffer;
562 len /= sizeof(WCHAR);
563 while (len--) *buffer++ = *wstr++;
564 *buffer = 0;
565 return ret;
568 static void dump_section_apiset(void)
570 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
571 const UINT *ptr, *entry, *value, *hash;
572 unsigned int i, j, count, val_count, rva;
573 char buffer[128];
575 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
577 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
578 rva = sect->PointerToRawData;
579 ptr = PRD( rva, sizeof(*ptr) );
580 printf( "ApiSet section:\n" );
581 switch (ptr[0]) /* version */
583 case 2:
584 printf( " Version: %u\n", ptr[0] );
585 printf( " Count: %08x\n", ptr[1] );
586 count = ptr[1];
587 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
588 for (i = 0; i < count; i++, entry += 3)
590 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
591 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
592 val_count = *value++;
593 for (j = 0; j < val_count; j++, value += 4)
595 putchar( ' ' );
596 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
597 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
599 printf( "\n");
601 break;
602 case 4:
603 printf( " Version: %u\n", ptr[0] );
604 printf( " Size: %08x\n", ptr[1] );
605 printf( " Flags: %08x\n", ptr[2] );
606 printf( " Count: %08x\n", ptr[3] );
607 count = ptr[3];
608 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
609 for (i = 0; i < count; i++, entry += 6)
611 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
612 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
613 value++; /* flags */
614 val_count = *value++;
615 for (j = 0; j < val_count; j++, value += 5)
617 putchar( ' ' );
618 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
619 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
621 printf( "\n");
623 break;
624 case 6:
625 printf( " Version: %u\n", ptr[0] );
626 printf( " Size: %08x\n", ptr[1] );
627 printf( " Flags: %08x\n", ptr[2] );
628 printf( " Count: %08x\n", ptr[3] );
629 printf( " EntryOffset: %08x\n", ptr[4] );
630 printf( " HashOffset: %08x\n", ptr[5] );
631 printf( " HashFactor: %08x\n", ptr[6] );
632 count = ptr[3];
633 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
634 for (i = 0; i < count; i++, entry += 6)
636 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
637 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
638 for (j = 0; j < entry[5]; j++, value += 5)
640 putchar( ' ' );
641 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
642 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
644 printf( "\n" );
646 printf( " Hash table:\n" );
647 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
648 for (i = 0; i < count; i++, hash += 2)
650 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
651 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
653 break;
654 default:
655 printf( "*** Unknown version %u\n", ptr[0] );
656 break;
658 break;
662 static void dump_dir_exported_functions(void)
664 unsigned int size[2] = { 0 };
665 const IMAGE_EXPORT_DIRECTORY *dirs[2];
666 UINT dir, i, *funcs;
667 const UINT *pFunc;
668 const UINT *pName;
669 const WORD *pOrdl;
671 dirs[0] = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size[0]);
672 if (!dirs[0]) return;
673 dirs[1] = get_alt_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size[1]);
675 for (dir = 0; dir < 2 && dirs[dir]; dir++)
677 if (dir) printf("Alternate (%s) exports table:\n",
678 get_machine_str(PE_alt_headers->FileHeader.Machine));
679 else printf("Exports table:\n");
680 printf("\n");
681 printf(" Name: %s\n", (const char*)RVA(dirs[dir]->Name, sizeof(DWORD)));
682 printf(" Characteristics: %08x\n", (UINT)dirs[dir]->Characteristics);
683 printf(" TimeDateStamp: %08X %s\n",
684 (UINT)dirs[dir]->TimeDateStamp, get_time_str(dirs[dir]->TimeDateStamp));
685 printf(" Version: %u.%02u\n", dirs[dir]->MajorVersion, dirs[dir]->MinorVersion);
686 printf(" Ordinal base: %u\n", (UINT)dirs[dir]->Base);
687 printf(" # of functions: %u\n", (UINT)dirs[dir]->NumberOfFunctions);
688 printf(" # of Names: %u\n", (UINT)dirs[dir]->NumberOfNames);
689 printf("Addresses of functions: %08X\n", (UINT)dirs[dir]->AddressOfFunctions);
690 printf("Addresses of name ordinals: %08X\n", (UINT)dirs[dir]->AddressOfNameOrdinals);
691 printf("Addresses of names: %08X\n", (UINT)dirs[dir]->AddressOfNames);
692 printf("\n");
693 printf(" Entry Pt Ordn Name\n");
695 pFunc = RVA(dirs[dir]->AddressOfFunctions, dirs[dir]->NumberOfFunctions * sizeof(DWORD));
696 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
697 pName = RVA(dirs[dir]->AddressOfNames, dirs[dir]->NumberOfNames * sizeof(DWORD));
698 pOrdl = RVA(dirs[dir]->AddressOfNameOrdinals, dirs[dir]->NumberOfNames * sizeof(WORD));
700 funcs = calloc( dirs[dir]->NumberOfFunctions, sizeof(*funcs) );
701 if (!funcs) fatal("no memory");
703 for (i = 0; i < dirs[dir]->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
705 for (i = 0; i < dirs[dir]->NumberOfFunctions; i++)
707 if (!pFunc[i]) continue;
708 printf(" %08X %5u ", pFunc[i], (UINT)dirs[dir]->Base + i);
709 if (funcs[i])
710 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
711 else
712 printf("<by ordinal>");
714 /* check for forwarded function */
715 if ((const char *)RVA(pFunc[i],1) >= (const char *)dirs[dir] &&
716 (const char *)RVA(pFunc[i],1) < (const char *)dirs[dir] + size[dir])
717 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
718 printf("\n");
720 free(funcs);
721 printf("\n");
726 struct runtime_function_x86_64
728 UINT BeginAddress;
729 UINT EndAddress;
730 UINT UnwindData;
733 struct runtime_function_armnt
735 UINT BeginAddress;
736 union {
737 UINT UnwindData;
738 struct {
739 UINT Flag : 2;
740 UINT FunctionLength : 11;
741 UINT Ret : 2;
742 UINT H : 1;
743 UINT Reg : 3;
744 UINT R : 1;
745 UINT L : 1;
746 UINT C : 1;
747 UINT StackAdjust : 10;
752 struct runtime_function_arm64
754 UINT BeginAddress;
755 union
757 UINT UnwindData;
758 struct
760 UINT Flag : 2;
761 UINT FunctionLength : 11;
762 UINT RegF : 3;
763 UINT RegI : 4;
764 UINT H : 1;
765 UINT CR : 2;
766 UINT FrameSize : 9;
771 union handler_data
773 struct runtime_function_x86_64 chain;
774 UINT handler;
777 struct opcode
779 BYTE offset;
780 BYTE code : 4;
781 BYTE info : 4;
784 struct unwind_info_x86_64
786 BYTE version : 3;
787 BYTE flags : 5;
788 BYTE prolog;
789 BYTE count;
790 BYTE frame_reg : 4;
791 BYTE frame_offset : 4;
792 struct opcode opcodes[1]; /* count entries */
793 /* followed by union handler_data */
796 struct unwind_info_armnt
798 UINT function_length : 18;
799 UINT version : 2;
800 UINT x : 1;
801 UINT e : 1;
802 UINT f : 1;
803 UINT count : 5;
804 UINT words : 4;
807 struct unwind_info_ext_armnt
809 WORD excount;
810 BYTE exwords;
811 BYTE reserved;
814 struct unwind_info_epilogue_armnt
816 UINT offset : 18;
817 UINT res : 2;
818 UINT cond : 4;
819 UINT index : 8;
822 #define UWOP_PUSH_NONVOL 0
823 #define UWOP_ALLOC_LARGE 1
824 #define UWOP_ALLOC_SMALL 2
825 #define UWOP_SET_FPREG 3
826 #define UWOP_SAVE_NONVOL 4
827 #define UWOP_SAVE_NONVOL_FAR 5
828 #define UWOP_SAVE_XMM128 8
829 #define UWOP_SAVE_XMM128_FAR 9
830 #define UWOP_PUSH_MACHFRAME 10
832 #define UNW_FLAG_EHANDLER 1
833 #define UNW_FLAG_UHANDLER 2
834 #define UNW_FLAG_CHAININFO 4
836 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
838 static const char * const reg_names[16] =
839 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
840 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
842 const union handler_data *handler_data;
843 const struct unwind_info_x86_64 *info;
844 unsigned int i, count;
846 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
847 if (function->UnwindData & 1)
849 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
850 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
851 return;
853 info = RVA( function->UnwindData, sizeof(*info) );
855 printf( " unwind info at %08x\n", function->UnwindData );
856 if (info->version != 1)
858 printf( " *** unknown version %u\n", info->version );
859 return;
861 printf( " flags %x", info->flags );
862 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
863 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
864 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
865 printf( "\n prolog 0x%x bytes\n", info->prolog );
867 if (info->frame_reg)
868 printf( " frame register %s offset 0x%x(%%rsp)\n",
869 reg_names[info->frame_reg], info->frame_offset * 16 );
871 for (i = 0; i < info->count; i++)
873 printf( " 0x%02x: ", info->opcodes[i].offset );
874 switch (info->opcodes[i].code)
876 case UWOP_PUSH_NONVOL:
877 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
878 break;
879 case UWOP_ALLOC_LARGE:
880 if (info->opcodes[i].info)
882 count = *(const UINT *)&info->opcodes[i+1];
883 i += 2;
885 else
887 count = *(const USHORT *)&info->opcodes[i+1] * 8;
888 i++;
890 printf( "sub $0x%x,%%rsp\n", count );
891 break;
892 case UWOP_ALLOC_SMALL:
893 count = (info->opcodes[i].info + 1) * 8;
894 printf( "sub $0x%x,%%rsp\n", count );
895 break;
896 case UWOP_SET_FPREG:
897 printf( "lea 0x%x(%%rsp),%s\n",
898 info->frame_offset * 16, reg_names[info->frame_reg] );
899 break;
900 case UWOP_SAVE_NONVOL:
901 count = *(const USHORT *)&info->opcodes[i+1] * 8;
902 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
903 i++;
904 break;
905 case UWOP_SAVE_NONVOL_FAR:
906 count = *(const UINT *)&info->opcodes[i+1];
907 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
908 i += 2;
909 break;
910 case UWOP_SAVE_XMM128:
911 count = *(const USHORT *)&info->opcodes[i+1] * 16;
912 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
913 i++;
914 break;
915 case UWOP_SAVE_XMM128_FAR:
916 count = *(const UINT *)&info->opcodes[i+1];
917 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
918 i += 2;
919 break;
920 case UWOP_PUSH_MACHFRAME:
921 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
922 break;
923 default:
924 printf( "*** unknown code %u\n", info->opcodes[i].code );
925 break;
929 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
930 if (info->flags & UNW_FLAG_CHAININFO)
932 printf( " -> function %08x-%08x\n",
933 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
934 return;
936 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
937 printf( " handler %08x data at %08x\n", handler_data->handler,
938 (UINT)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
941 static const BYTE armnt_code_lengths[256] =
943 /* 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,
944 /* 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,
945 /* 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,
946 /* 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,
947 /* 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,
948 /* 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,
949 /* 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,
950 /* 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
953 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
955 const struct unwind_info_armnt *info;
956 const struct unwind_info_ext_armnt *infoex;
957 const struct unwind_info_epilogue_armnt *infoepi;
958 unsigned int rva;
959 WORD i, count = 0, words = 0;
961 if (fnc->Flag)
963 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
964 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
966 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
967 (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2 );
968 printf( " Flag %x\n", fnc->Flag );
969 printf( " FunctionLength %x\n", fnc->FunctionLength );
970 printf( " Ret %x\n", fnc->Ret );
971 printf( " H %x\n", fnc->H );
972 printf( " Reg %x\n", fnc->Reg );
973 printf( " R %x\n", fnc->R );
974 printf( " L %x\n", fnc->L );
975 printf( " C %x\n", fnc->C );
976 printf( " StackAdjust %x\n", fnc->StackAdjust );
978 if (fnc->StackAdjust >= 0x03f4)
980 pf = fnc->StackAdjust & 0x04;
981 ef = fnc->StackAdjust & 0x08;
982 stack = (fnc->StackAdjust & 3) + 1;
985 if (!fnc->R || pf)
987 int first = 4, last = fnc->Reg + 4;
988 if (pf)
990 first = (~fnc->StackAdjust) & 3;
991 if (fnc->R)
992 last = 3;
994 if (first == last)
995 sprintf(intregs, "r%u", first);
996 else
997 sprintf(intregs, "r%u-r%u", first, last);
998 fpoffset = last + 1 - first;
1001 if (!fnc->R || ef)
1003 int first = 4, last = fnc->Reg + 4;
1004 if (ef)
1006 first = (~fnc->StackAdjust) & 3;
1007 if (fnc->R)
1008 last = 3;
1010 if (first == last)
1011 sprintf(intregspop, "r%u", first);
1012 else
1013 sprintf(intregspop, "r%u-r%u", first, last);
1016 if (fnc->C)
1018 if (intregs[0])
1019 strcat(intregs, ", ");
1020 if (intregspop[0])
1021 strcat(intregspop, ", ");
1022 strcat(intregs, "r11");
1023 strcat(intregspop, "r11");
1025 if (fnc->L)
1027 if (intregs[0])
1028 strcat(intregs, ", ");
1029 strcat(intregs, "lr");
1031 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
1032 strcat(intregspop, ", ");
1033 if (fnc->Ret != 0)
1034 strcat(intregspop, "lr");
1035 else if (!fnc->H)
1036 strcat(intregspop, "pc");
1039 if (fnc->R)
1041 if (fnc->Reg)
1042 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
1043 else
1044 strcpy(vfpregs, "d8");
1047 if (fnc->Flag == 1) {
1048 if (fnc->H)
1049 printf( " Unwind Code\tpush {r0-r3}\n" );
1051 if (intregs[0])
1052 printf( " Unwind Code\tpush {%s}\n", intregs );
1054 if (fnc->C && fpoffset == 0)
1055 printf( " Unwind Code\tmov r11, sp\n" );
1056 else if (fnc->C)
1057 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
1059 if (fnc->R && fnc->Reg != 0x07)
1060 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
1062 if (stack && !pf)
1063 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
1066 if (fnc->Ret == 3)
1067 return;
1068 printf( "Epilogue:\n" );
1070 if (stack && !ef)
1071 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1073 if (fnc->R && fnc->Reg != 0x07)
1074 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1076 if (intregspop[0])
1077 printf( " Unwind Code\tpop {%s}\n", intregspop );
1079 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1080 printf( " Unwind Code\tadd sp, sp, #16\n" );
1081 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1082 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1084 if (fnc->Ret == 1)
1085 printf( " Unwind Code\tbx <reg>\n" );
1086 else if (fnc->Ret == 2)
1087 printf( " Unwind Code\tb <address>\n" );
1089 return;
1092 info = RVA( fnc->UnwindData, sizeof(*info) );
1093 rva = fnc->UnwindData + sizeof(*info);
1094 count = info->count;
1095 words = info->words;
1097 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1098 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1099 printf( " unwind info at %08x\n", fnc->UnwindData );
1100 printf( " Flag %x\n", fnc->Flag );
1101 printf( " FunctionLength %x\n", info->function_length );
1102 printf( " Version %x\n", info->version );
1103 printf( " X %x\n", info->x );
1104 printf( " E %x\n", info->e );
1105 printf( " F %x\n", info->f );
1106 printf( " Count %x\n", count );
1107 printf( " Words %x\n", words );
1109 if (!info->count && !info->words)
1111 infoex = RVA( rva, sizeof(*infoex) );
1112 rva = rva + sizeof(*infoex);
1113 count = infoex->excount;
1114 words = infoex->exwords;
1115 printf( " ExtCount %x\n", count );
1116 printf( " ExtWords %x\n", words );
1119 if (!info->e)
1121 infoepi = RVA( rva, count * sizeof(*infoepi) );
1122 rva = rva + count * sizeof(*infoepi);
1124 for (i = 0; i < count; i++)
1126 printf( " Epilogue Scope %x\n", i );
1127 printf( " Offset %x\n", infoepi[i].offset );
1128 printf( " Reserved %x\n", infoepi[i].res );
1129 printf( " Condition %x\n", infoepi[i].cond );
1130 printf( " Index %x\n", infoepi[i].index );
1133 else
1134 infoepi = NULL;
1136 if (words)
1138 const unsigned int *codes;
1139 BYTE b, *bytes;
1140 BOOL inepilogue = FALSE;
1142 codes = RVA( rva, words * sizeof(*codes) );
1143 rva = rva + words * sizeof(*codes);
1144 bytes = (BYTE*)codes;
1146 for (b = 0; b < words * sizeof(*codes); b++)
1148 BYTE code = bytes[b];
1149 BYTE len = armnt_code_lengths[code];
1151 if (info->e && b == count)
1153 printf( "Epilogue:\n" );
1154 inepilogue = TRUE;
1156 else if (!info->e && infoepi)
1158 for (i = 0; i < count; i++)
1159 if (b == infoepi[i].index)
1161 printf( "Epilogue from Scope %x at %08x:\n", i,
1162 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1163 inepilogue = TRUE;
1167 printf( " Unwind Code");
1168 for (i = 0; i < len; i++)
1169 printf( " %02x", bytes[b+i] );
1170 printf( "\t" );
1172 if (code == 0x00)
1173 printf( "\n" );
1174 else if (code <= 0x7f)
1175 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1176 else if (code <= 0xbf)
1178 WORD excode, f;
1179 BOOL first = TRUE;
1180 BYTE excodes = bytes[++b];
1182 excode = (code << 8) | excodes;
1183 printf( "%s {", inepilogue ? "pop" : "push" );
1185 for (f = 0; f <= 12; f++)
1187 if ((excode >> f) & 1)
1189 printf( "%sr%u", first ? "" : ", ", f );
1190 first = FALSE;
1194 if (excode & 0x2000)
1195 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1197 printf( "}\n" );
1199 else if (code <= 0xcf)
1200 if (inepilogue)
1201 printf( "mov sp, r%u\n", code & 0x0f );
1202 else
1203 printf( "mov r%u, sp\n", code & 0x0f );
1204 else if (code <= 0xd7)
1205 if (inepilogue)
1206 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1207 else
1208 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1209 else if (code <= 0xdf)
1210 if (inepilogue)
1211 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1212 else
1213 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1214 else if (code <= 0xe7)
1215 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1216 else if (code <= 0xeb)
1218 WORD excode;
1219 BYTE excodes = bytes[++b];
1221 excode = (code << 8) | excodes;
1222 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1224 else if (code <= 0xed)
1226 WORD excode, f;
1227 BOOL first = TRUE;
1228 BYTE excodes = bytes[++b];
1230 excode = (code << 8) | excodes;
1231 printf( "%s {", inepilogue ? "pop" : "push" );
1233 for (f = 0; f < 8; f++)
1235 if ((excode >> f) & 1)
1237 printf( "%sr%u", first ? "" : ", ", f );
1238 first = FALSE;
1242 if (excode & 0x0100)
1243 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1245 printf( "}\n" );
1247 else if (code == 0xee)
1248 printf( "unknown 16\n" );
1249 else if (code == 0xef)
1251 WORD excode;
1252 BYTE excodes = bytes[++b];
1254 if (excodes <= 0x0f)
1256 excode = (code << 8) | excodes;
1257 if (inepilogue)
1258 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1259 else
1260 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1262 else
1263 printf( "unknown 32\n" );
1265 else if (code <= 0xf4)
1266 printf( "unknown\n" );
1267 else if (code <= 0xf6)
1269 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1270 BYTE excodes = bytes[++b];
1272 excode = (code << 8) | excodes;
1273 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1274 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1276 else if (code <= 0xf7)
1278 unsigned int excode;
1279 BYTE excodes[2];
1281 excodes[0] = bytes[++b];
1282 excodes[1] = bytes[++b];
1283 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1284 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1286 else if (code <= 0xf8)
1288 unsigned int excode;
1289 BYTE excodes[3];
1291 excodes[0] = bytes[++b];
1292 excodes[1] = bytes[++b];
1293 excodes[2] = bytes[++b];
1294 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1295 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1297 else if (code <= 0xf9)
1299 unsigned int excode;
1300 BYTE excodes[2];
1302 excodes[0] = bytes[++b];
1303 excodes[1] = bytes[++b];
1304 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1305 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1307 else if (code <= 0xfa)
1309 unsigned int excode;
1310 BYTE excodes[3];
1312 excodes[0] = bytes[++b];
1313 excodes[1] = bytes[++b];
1314 excodes[2] = bytes[++b];
1315 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1316 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1318 else if (code <= 0xfb)
1319 printf( "nop\n" );
1320 else if (code <= 0xfc)
1321 printf( "nop.w\n" );
1322 else if (code <= 0xfd)
1324 printf( "(end) nop\n" );
1325 inepilogue = TRUE;
1327 else if (code <= 0xfe)
1329 printf( "(end) nop.w\n" );
1330 inepilogue = TRUE;
1332 else
1334 printf( "end\n" );
1335 inepilogue = TRUE;
1340 if (info->x)
1342 const unsigned int *handler;
1344 handler = RVA( rva, sizeof(*handler) );
1345 rva = rva + sizeof(*handler);
1347 printf( " handler %08x data at %08x\n", *handler, rva);
1351 struct unwind_info_arm64
1353 UINT function_length : 18;
1354 UINT version : 2;
1355 UINT x : 1;
1356 UINT e : 1;
1357 UINT epilog : 5;
1358 UINT codes : 5;
1361 struct unwind_info_ext_arm64
1363 WORD epilog;
1364 BYTE codes;
1365 BYTE reserved;
1368 struct unwind_info_epilog_arm64
1370 UINT offset : 18;
1371 UINT res : 4;
1372 UINT index : 10;
1375 static const BYTE code_lengths[256] =
1377 /* 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,
1378 /* 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,
1379 /* 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,
1380 /* 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,
1381 /* 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,
1382 /* 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,
1383 /* 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,
1384 /* e0 */ 4,1,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1387 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1389 unsigned int i, j;
1391 for (i = 0; i < count; i += code_lengths[ptr[i]])
1393 BYTE len = code_lengths[ptr[i]];
1394 unsigned int val = ptr[i];
1395 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1396 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1398 printf( " %04x: ", i );
1399 for (j = 0; j < 4; j++)
1400 if (j < len) printf( "%02x ", ptr[i+j] );
1401 else printf( " " );
1403 if (ptr[i] < 0x20) /* alloc_s */
1405 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1407 else if (ptr[i] < 0x40) /* save_r19r20_x */
1409 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1411 else if (ptr[i] < 0x80) /* save_fplr */
1413 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1415 else if (ptr[i] < 0xc0) /* save_fplr_x */
1417 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1419 else if (ptr[i] < 0xc8) /* alloc_m */
1421 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1423 else if (ptr[i] < 0xcc) /* save_regp */
1425 int reg = 19 + ((val >> 6) & 0xf);
1426 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1428 else if (ptr[i] < 0xd0) /* save_regp_x */
1430 int reg = 19 + ((val >> 6) & 0xf);
1431 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1433 else if (ptr[i] < 0xd4) /* save_reg */
1435 int reg = 19 + ((val >> 6) & 0xf);
1436 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1438 else if (ptr[i] < 0xd6) /* save_reg_x */
1440 int reg = 19 + ((val >> 5) & 0xf);
1441 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1443 else if (ptr[i] < 0xd8) /* save_lrpair */
1445 int reg = 19 + 2 * ((val >> 6) & 0x7);
1446 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1448 else if (ptr[i] < 0xda) /* save_fregp */
1450 int reg = 8 + ((val >> 6) & 0x7);
1451 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1453 else if (ptr[i] < 0xdc) /* save_fregp_x */
1455 int reg = 8 + ((val >> 6) & 0x7);
1456 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1458 else if (ptr[i] < 0xde) /* save_freg */
1460 int reg = 8 + ((val >> 6) & 0x7);
1461 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1463 else if (ptr[i] == 0xde) /* save_freg_x */
1465 int reg = 8 + ((val >> 5) & 0x7);
1466 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1468 else if (ptr[i] == 0xe0) /* alloc_l */
1470 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1472 else if (ptr[i] == 0xe1) /* set_fp */
1474 printf( "mov x29,sp\n" );
1476 else if (ptr[i] == 0xe2) /* add_fp */
1478 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1480 else if (ptr[i] == 0xe3) /* nop */
1482 printf( "nop\n" );
1484 else if (ptr[i] == 0xe4) /* end */
1486 printf( "end\n" );
1488 else if (ptr[i] == 0xe5) /* end_c */
1490 printf( "end_c\n" );
1492 else if (ptr[i] == 0xe6) /* save_next */
1494 printf( "save_next\n" );
1496 else if (ptr[i] == 0xe7) /* arithmetic */
1498 switch ((val >> 4) & 0x0f)
1500 case 0: printf( "add lr,lr,x28\n" ); break;
1501 case 1: printf( "add lr,lr,sp\n" ); break;
1502 case 2: printf( "sub lr,lr,x28\n" ); break;
1503 case 3: printf( "sub lr,lr,sp\n" ); break;
1504 case 4: printf( "eor lr,lr,x28\n" ); break;
1505 case 5: printf( "eor lr,lr,sp\n" ); break;
1506 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1507 case 8: printf( "ror lr,lr,x28\n" ); break;
1508 case 9: printf( "ror lr,lr,sp\n" ); break;
1509 default:printf( "unknown op\n" ); break;
1512 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1514 printf( "MSFT_OP_TRAP_FRAME\n" );
1516 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1518 printf( "MSFT_OP_MACHINE_FRAME\n" );
1520 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1522 printf( "MSFT_OP_CONTEXT\n" );
1524 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1526 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1528 else printf( "??\n");
1532 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1534 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
1536 if (func->CR == 1) intsz += 8;
1537 if (func->RegF) fpsz += 8;
1539 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
1540 locsz = func->FrameSize * 16 - savesz;
1542 switch (func->CR)
1544 case 3:
1545 printf( " %04x: mov x29,sp\n", pos++ );
1546 if (locsz <= 512)
1548 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1549 break;
1551 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1552 /* fall through */
1553 case 0:
1554 case 1:
1555 if (locsz <= 4080)
1557 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1559 else
1561 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1562 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1564 break;
1567 if (func->H)
1569 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1570 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1571 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1572 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1575 if (func->RegF)
1577 if (func->RegF % 2 == 0)
1578 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
1579 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
1581 if (!i && !intsz)
1582 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1583 else
1584 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1588 switch (func->RegI)
1590 case 0:
1591 if (func->CR == 1)
1592 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1593 break;
1594 case 1:
1595 if (func->CR == 1)
1596 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1597 else
1598 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1599 break;
1600 default:
1601 if (func->RegI % 2)
1603 if (func->CR == 1)
1604 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1605 else
1606 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1608 else if (func->CR == 1)
1609 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1611 for (i = func->RegI / 2 - 1; i >= 0; i--)
1612 if (i)
1613 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1614 else
1615 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1616 break;
1618 printf( " %04x: end\n", pos );
1621 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1623 const struct unwind_info_arm64 *info;
1624 const struct unwind_info_ext_arm64 *infoex;
1625 const struct unwind_info_epilog_arm64 *infoepi;
1626 const BYTE *ptr;
1627 unsigned int i, rva, codes, epilogs;
1629 if (func->Flag)
1631 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1632 func->BeginAddress + func->FunctionLength * 4 );
1633 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1634 func->FunctionLength, func->Flag, func->RegF, func->RegI,
1635 func->H, func->CR, func->FrameSize );
1636 dump_arm64_packed_info( func );
1637 return;
1640 rva = func->UnwindData;
1641 info = RVA( rva, sizeof(*info) );
1642 rva += sizeof(*info);
1643 epilogs = info->epilog;
1644 codes = info->codes;
1646 if (!codes)
1648 infoex = RVA( rva, sizeof(*infoex) );
1649 rva = rva + sizeof(*infoex);
1650 codes = infoex->codes;
1651 epilogs = infoex->epilog;
1653 printf( "\nFunction %08x-%08x:\n",
1654 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1655 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1656 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1657 if (info->e)
1659 printf( " epilog 0: code=%04x\n", info->epilog );
1661 else
1663 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1664 rva += sizeof(*infoepi) * epilogs;
1665 for (i = 0; i < epilogs; i++)
1666 printf( " epilog %u: pc=%08x code=%04x\n", i,
1667 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1669 ptr = RVA( rva, codes * 4);
1670 rva += codes * 4;
1671 if (info->x)
1673 const UINT *handler = RVA( rva, sizeof(*handler) );
1674 rva += sizeof(*handler);
1675 printf( " handler: %08x data %08x\n", *handler, rva );
1677 dump_arm64_codes( ptr, codes * 4 );
1680 static void dump_dir_exceptions(void)
1682 unsigned int i, dir, size, sizes[2] = { 0 };
1683 const void *funcs[2];
1684 const IMAGE_FILE_HEADER *file_header;
1686 funcs[0] = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &sizes[0]);
1687 if (!funcs[0]) return;
1688 funcs[1] = get_alt_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &sizes[1]);
1690 for (dir = 0; dir < 2 && funcs[dir]; dir++)
1692 size = sizes[dir];
1693 file_header = dir ? &PE_alt_headers->FileHeader : &PE_nt_headers->FileHeader;
1695 switch (file_header->Machine)
1697 case IMAGE_FILE_MACHINE_AMD64:
1698 size /= sizeof(struct runtime_function_x86_64);
1699 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1700 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs[dir] + i );
1701 break;
1702 case IMAGE_FILE_MACHINE_ARMNT:
1703 size /= sizeof(struct runtime_function_armnt);
1704 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1705 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs[dir] + i );
1706 break;
1707 case IMAGE_FILE_MACHINE_ARM64:
1708 size /= sizeof(struct runtime_function_arm64);
1709 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1710 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs[dir] + i );
1711 break;
1712 default:
1713 printf( "Exception information not supported for %s binaries\n",
1714 get_machine_str(file_header->Machine));
1715 break;
1717 printf( "\n" );
1722 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
1724 /* FIXME: This does not properly handle large images */
1725 const IMAGE_IMPORT_BY_NAME* iibn;
1726 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1728 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1729 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
1730 else
1732 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1733 if (!iibn)
1734 printf("Can't grab import by name info, skipping to next ordinal\n");
1735 else
1736 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1741 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
1743 const IMAGE_IMPORT_BY_NAME* iibn;
1744 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
1746 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1747 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
1748 else
1750 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1751 if (!iibn)
1752 printf("Can't grab import by name info, skipping to next ordinal\n");
1753 else
1754 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1759 static void dump_dir_imported_functions(void)
1761 unsigned directorySize;
1762 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1764 if (!importDesc) return;
1766 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1768 for (;;)
1770 const IMAGE_THUNK_DATA32* il;
1772 if (!importDesc->Name || !importDesc->FirstThunk) break;
1774 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1775 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
1776 printf(" TimeDateStamp: %08X (%s)\n",
1777 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1778 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
1779 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
1781 printf(" Thunk Ordn Name\n");
1783 il = (importDesc->OriginalFirstThunk != 0) ?
1784 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
1785 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1787 if (!il)
1788 printf("Can't grab thunk data, going to next imported DLL\n");
1789 else
1791 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1792 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1793 else
1794 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1795 printf("\n");
1797 importDesc++;
1799 printf("\n");
1802 static void dump_hybrid_metadata(void)
1804 unsigned int i;
1805 const struct
1807 unsigned int version;
1808 unsigned int code_map;
1809 unsigned int code_map_count;
1810 unsigned int code_ranges_to_ep;
1811 unsigned int redir_metadata;
1812 unsigned int __os_arm64x_dispatch_call_no_redirect;
1813 unsigned int __os_arm64x_dispatch_ret;
1814 unsigned int __os_arm64x_check_call;
1815 unsigned int __os_arm64x_check_icall;
1816 unsigned int __os_arm64x_check_icall_cfg;
1817 unsigned int alt_entry;
1818 unsigned int aux_iat;
1819 unsigned int code_ranges_to_ep_count;
1820 unsigned int redir_metadata_count;
1821 unsigned int __os_arm64x_get_x64_information;
1822 unsigned int __os_arm64x_set_x64_information;
1823 unsigned int except_data;
1824 unsigned int except_data_size;
1825 unsigned int __os_arm64x_jump;
1826 unsigned int aux_iat_copy;
1827 } *data = get_hybrid_metadata();
1829 if (!data) return;
1830 printf( "Hybrid metadata\n" );
1831 print_dword( "Version", data->version );
1832 print_dword( "Code map", data->code_map );
1833 print_dword( "Code map count", data->code_map_count );
1834 print_dword( "Code ranges to entry points", data->code_ranges_to_ep );
1835 print_dword( "Redirection metadata", data->redir_metadata );
1836 print_dword( "__os_arm64x_dispatch_call_no_redirect", data->__os_arm64x_dispatch_call_no_redirect );
1837 print_dword( "__os_arm64x_dispatch_ret", data->__os_arm64x_dispatch_ret );
1838 print_dword( "__os_arm64x_check_call", data->__os_arm64x_check_call );
1839 print_dword( "__os_arm64x_check_icall", data->__os_arm64x_check_icall );
1840 print_dword( "__os_arm64x_check_icall_cfg", data->__os_arm64x_check_icall_cfg );
1841 print_dword( "Alternate entry point", data->alt_entry );
1842 print_dword( "Auxiliary IAT", data->aux_iat );
1843 print_dword( "Code ranges to entry points count", data->code_ranges_to_ep_count );
1844 print_dword( "Redirection metadata count", data->redir_metadata_count );
1845 print_dword( "__os_arm64x_get_x64_information", data->__os_arm64x_get_x64_information );
1846 print_dword( "__os_arm64x_set_x64_information", data->__os_arm64x_set_x64_information );
1847 print_dword( "Exception data", data->except_data );
1848 print_dword( "Exception data size", data->except_data_size );
1849 print_dword( "__os_arm64x_jump", data->__os_arm64x_jump );
1850 print_dword( "Auxiliary IAT copy", data->aux_iat_copy );
1852 if (data->code_map)
1854 const struct
1856 unsigned int start : 30;
1857 unsigned int type : 2;
1858 unsigned int len;
1859 } *map = RVA( data->code_map, data->code_map_count * sizeof(*map) );
1861 printf( "\nCode ranges\n" );
1862 for (i = 0; i < data->code_map_count; i++)
1864 static const char *types[] = { "ARM64", "ARM64EC", "x64", "??" };
1865 unsigned int start = map[i].start & ~0x3;
1866 unsigned int type = map[i].start & 0x3;
1867 printf( " %08x - %08x %s\n", start, start + map[i].len, types[type] );
1871 if (data->code_ranges_to_ep)
1873 const struct
1875 unsigned int start;
1876 unsigned int end;
1877 unsigned int func;
1878 } *map = RVA( data->code_ranges_to_ep, data->code_ranges_to_ep_count * sizeof(*map) );
1880 printf( "\nCode ranges to entry points\n" );
1881 printf( " Start - End Entry point\n" );
1882 for (i = 0; i < data->code_ranges_to_ep_count; i++)
1883 printf( " %08x - %08x %08x\n", map[i].start, map[i].end, map[i].func );
1886 if (data->redir_metadata)
1888 const struct
1890 unsigned int func;
1891 unsigned int redir;
1892 } *map = RVA( data->redir_metadata, data->redir_metadata_count * sizeof(*map) );
1894 printf( "\nEntry point redirection\n" );
1895 for (i = 0; i < data->redir_metadata_count; i++)
1896 printf( " %08x -> %08x\n", map[i].func, map[i].redir );
1900 static void dump_dir_loadconfig(void)
1902 unsigned int dir, size, sizes[2];
1903 const IMAGE_LOAD_CONFIG_DIRECTORY32 *dirs[2];
1905 dirs[0] = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &sizes[0]);
1906 if (!dirs[0]) return;
1907 dirs[1] = get_alt_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &sizes[1]);
1909 for (dir = 0; dir < 2 && dirs[dir]; dir++)
1911 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = dirs[dir];
1912 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1913 size = min( sizes[dir], loadcfg32->Size );
1915 if (dir)
1916 printf( "\nAlternate (%s) loadconfig\n",
1917 get_machine_str( PE_alt_headers->FileHeader.Machine ));
1918 else
1919 printf( "Loadconfig\n" );
1920 print_dword( "Size", loadcfg32->Size );
1921 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1922 print_word( "MajorVersion", loadcfg32->MajorVersion );
1923 print_word( "MinorVersion", loadcfg32->MinorVersion );
1924 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1925 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1926 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1928 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1930 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1931 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1932 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1933 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1934 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1935 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1936 print_word( "CSDVersion", loadcfg64->CSDVersion );
1937 print_word( "DependentLoadFlags", loadcfg64->DependentLoadFlags );
1938 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1939 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, SEHandlerTable )) return;
1940 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1941 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1942 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardCFCheckFunctionPointer )) return;
1943 print_longlong( "GuardCFCheckFunctionPointer", loadcfg64->GuardCFCheckFunctionPointer );
1944 print_longlong( "GuardCFDispatchFunctionPointer", loadcfg64->GuardCFDispatchFunctionPointer );
1945 print_longlong( "GuardCFFunctionTable", loadcfg64->GuardCFFunctionTable );
1946 print_longlong( "GuardCFFunctionCount", loadcfg64->GuardCFFunctionCount );
1947 print_dword( "GuardFlags", loadcfg64->GuardFlags );
1948 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CodeIntegrity )) return;
1949 print_word( "CodeIntegrity.Flags", loadcfg64->CodeIntegrity.Flags );
1950 print_word( "CodeIntegrity.Catalog", loadcfg64->CodeIntegrity.Catalog );
1951 print_dword( "CodeIntegrity.CatalogOffset", loadcfg64->CodeIntegrity.CatalogOffset );
1952 print_dword( "CodeIntegrity.Reserved", loadcfg64->CodeIntegrity.Reserved );
1953 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardAddressTakenIatEntryTable )) return;
1954 print_longlong( "GuardAddressTakenIatEntryTable", loadcfg64->GuardAddressTakenIatEntryTable );
1955 print_longlong( "GuardAddressTakenIatEntryCount", loadcfg64->GuardAddressTakenIatEntryCount );
1956 print_longlong( "GuardLongJumpTargetTable", loadcfg64->GuardLongJumpTargetTable );
1957 print_longlong( "GuardLongJumpTargetCount", loadcfg64->GuardLongJumpTargetCount );
1958 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTable )) return;
1959 print_longlong( "DynamicValueRelocTable", loadcfg64->DynamicValueRelocTable );
1960 print_longlong( "CHPEMetadataPointer", loadcfg64->CHPEMetadataPointer );
1961 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFFailureRoutine )) return;
1962 print_longlong( "GuardRFFailureRoutine", loadcfg64->GuardRFFailureRoutine );
1963 print_longlong( "GuardRFFailureRoutineFunctionPointer", loadcfg64->GuardRFFailureRoutineFunctionPointer );
1964 print_dword( "DynamicValueRelocTableOffset", loadcfg64->DynamicValueRelocTableOffset );
1965 print_word( "DynamicValueRelocTableSection",loadcfg64->DynamicValueRelocTableSection );
1966 print_word( "Reserved2", loadcfg64->Reserved2 );
1967 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFVerifyStackPointerFunctionPointer )) return;
1968 print_longlong( "GuardRFVerifyStackPointerFunctionPointer", loadcfg64->GuardRFVerifyStackPointerFunctionPointer );
1969 print_dword( "HotPatchTableOffset", loadcfg64->HotPatchTableOffset );
1970 print_dword( "Reserved3", loadcfg64->Reserved3 );
1971 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, EnclaveConfigurationPointer )) return;
1972 print_longlong( "EnclaveConfigurationPointer", loadcfg64->EnclaveConfigurationPointer );
1973 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, VolatileMetadataPointer )) return;
1974 print_longlong( "VolatileMetadataPointer", loadcfg64->VolatileMetadataPointer );
1975 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardEHContinuationTable )) return;
1976 print_longlong( "GuardEHContinuationTable", loadcfg64->GuardEHContinuationTable );
1977 print_longlong( "GuardEHContinuationCount", loadcfg64->GuardEHContinuationCount );
1978 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardXFGCheckFunctionPointer )) return;
1979 print_longlong( "GuardXFGCheckFunctionPointer", loadcfg64->GuardXFGCheckFunctionPointer );
1980 print_longlong( "GuardXFGDispatchFunctionPointer", loadcfg64->GuardXFGDispatchFunctionPointer );
1981 print_longlong( "GuardXFGTableDispatchFunctionPointer", loadcfg64->GuardXFGTableDispatchFunctionPointer );
1982 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CastGuardOsDeterminedFailureMode )) return;
1983 print_longlong( "CastGuardOsDeterminedFailureMode", loadcfg64->CastGuardOsDeterminedFailureMode );
1984 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardMemcpyFunctionPointer )) return;
1985 print_longlong( "GuardMemcpyFunctionPointer", loadcfg64->GuardMemcpyFunctionPointer );
1987 else
1989 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1990 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1991 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1992 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1993 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1994 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1995 print_word( "CSDVersion", loadcfg32->CSDVersion );
1996 print_word( "DependentLoadFlags", loadcfg32->DependentLoadFlags );
1997 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1998 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, SEHandlerTable )) return;
1999 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
2000 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
2001 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardCFCheckFunctionPointer )) return;
2002 print_dword( "GuardCFCheckFunctionPointer", loadcfg32->GuardCFCheckFunctionPointer );
2003 print_dword( "GuardCFDispatchFunctionPointer", loadcfg32->GuardCFDispatchFunctionPointer );
2004 print_dword( "GuardCFFunctionTable", loadcfg32->GuardCFFunctionTable );
2005 print_dword( "GuardCFFunctionCount", loadcfg32->GuardCFFunctionCount );
2006 print_dword( "GuardFlags", loadcfg32->GuardFlags );
2007 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CodeIntegrity )) return;
2008 print_word( "CodeIntegrity.Flags", loadcfg32->CodeIntegrity.Flags );
2009 print_word( "CodeIntegrity.Catalog", loadcfg32->CodeIntegrity.Catalog );
2010 print_dword( "CodeIntegrity.CatalogOffset", loadcfg32->CodeIntegrity.CatalogOffset );
2011 print_dword( "CodeIntegrity.Reserved", loadcfg32->CodeIntegrity.Reserved );
2012 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardAddressTakenIatEntryTable )) return;
2013 print_dword( "GuardAddressTakenIatEntryTable", loadcfg32->GuardAddressTakenIatEntryTable );
2014 print_dword( "GuardAddressTakenIatEntryCount", loadcfg32->GuardAddressTakenIatEntryCount );
2015 print_dword( "GuardLongJumpTargetTable", loadcfg32->GuardLongJumpTargetTable );
2016 print_dword( "GuardLongJumpTargetCount", loadcfg32->GuardLongJumpTargetCount );
2017 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTable )) return;
2018 print_dword( "DynamicValueRelocTable", loadcfg32->DynamicValueRelocTable );
2019 print_dword( "CHPEMetadataPointer", loadcfg32->CHPEMetadataPointer );
2020 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFFailureRoutine )) return;
2021 print_dword( "GuardRFFailureRoutine", loadcfg32->GuardRFFailureRoutine );
2022 print_dword( "GuardRFFailureRoutineFunctionPointer", loadcfg32->GuardRFFailureRoutineFunctionPointer );
2023 print_dword( "DynamicValueRelocTableOffset", loadcfg32->DynamicValueRelocTableOffset );
2024 print_word( "DynamicValueRelocTableSection", loadcfg32->DynamicValueRelocTableSection );
2025 print_word( "Reserved2", loadcfg32->Reserved2 );
2026 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFVerifyStackPointerFunctionPointer )) return;
2027 print_dword( "GuardRFVerifyStackPointerFunctionPointer", loadcfg32->GuardRFVerifyStackPointerFunctionPointer );
2028 print_dword( "HotPatchTableOffset", loadcfg32->HotPatchTableOffset );
2029 print_dword( "Reserved3", loadcfg32->Reserved3 );
2030 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, EnclaveConfigurationPointer )) return;
2031 print_dword( "EnclaveConfigurationPointer", loadcfg32->EnclaveConfigurationPointer );
2032 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, VolatileMetadataPointer )) return;
2033 print_dword( "VolatileMetadataPointer", loadcfg32->VolatileMetadataPointer );
2034 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardEHContinuationTable )) return;
2035 print_dword( "GuardEHContinuationTable", loadcfg32->GuardEHContinuationTable );
2036 print_dword( "GuardEHContinuationCount", loadcfg32->GuardEHContinuationCount );
2037 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardXFGCheckFunctionPointer )) return;
2038 print_dword( "GuardXFGCheckFunctionPointer", loadcfg32->GuardXFGCheckFunctionPointer );
2039 print_dword( "GuardXFGDispatchFunctionPointer", loadcfg32->GuardXFGDispatchFunctionPointer );
2040 print_dword( "GuardXFGTableDispatchFunctionPointer", loadcfg32->GuardXFGTableDispatchFunctionPointer );
2041 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CastGuardOsDeterminedFailureMode )) return;
2042 print_dword( "CastGuardOsDeterminedFailureMode", loadcfg32->CastGuardOsDeterminedFailureMode );
2043 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardMemcpyFunctionPointer )) return;
2044 print_dword( "GuardMemcpyFunctionPointer", loadcfg32->GuardMemcpyFunctionPointer );
2047 printf( "\n" );
2048 dump_hybrid_metadata();
2051 static void dump_dir_delay_imported_functions(void)
2053 unsigned directorySize;
2054 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
2056 if (!importDesc) return;
2058 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
2060 for (;;)
2062 const IMAGE_THUNK_DATA32* il;
2063 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
2065 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
2067 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
2068 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
2069 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
2070 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
2071 printf(" TimeDateStamp: %08X (%s)\n",
2072 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2074 printf(" Thunk Ordn Name\n");
2076 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
2078 if (!il)
2079 printf("Can't grab thunk data, going to next imported DLL\n");
2080 else
2082 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2083 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
2084 else
2085 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
2086 printf("\n");
2088 importDesc++;
2090 printf("\n");
2093 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
2095 const char* str;
2097 printf("Directory %02u\n", idx + 1);
2098 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
2099 printf(" TimeDateStamp: %08X %s\n",
2100 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
2101 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
2102 switch (idd->Type)
2104 default:
2105 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
2106 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
2107 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
2108 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
2109 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
2110 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
2111 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
2112 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
2113 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
2114 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
2115 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
2116 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
2117 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
2118 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
2119 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
2120 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
2121 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
2123 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
2124 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
2125 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
2126 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
2128 switch (idd->Type)
2130 case IMAGE_DEBUG_TYPE_UNKNOWN:
2131 break;
2132 case IMAGE_DEBUG_TYPE_COFF:
2133 dump_coff(idd->PointerToRawData, idd->SizeOfData,
2134 IMAGE_FIRST_SECTION(PE_nt_headers));
2135 break;
2136 case IMAGE_DEBUG_TYPE_CODEVIEW:
2137 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
2138 break;
2139 case IMAGE_DEBUG_TYPE_FPO:
2140 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
2141 break;
2142 case IMAGE_DEBUG_TYPE_MISC:
2144 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
2145 if (!misc) {printf("Can't get misc debug information\n"); break;}
2146 printf(" DataType: %u (%s)\n",
2147 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
2148 printf(" Length: %u\n", (UINT)misc->Length);
2149 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
2150 printf(" Data: %s\n", misc->Data);
2152 break;
2153 default: break;
2155 printf("\n");
2158 static void dump_dir_debug(void)
2160 unsigned nb_dbg, i;
2161 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
2163 nb_dbg /= sizeof(*debugDir);
2164 if (!debugDir || !nb_dbg) return;
2166 printf("Debug Table (%u directories)\n", nb_dbg);
2168 for (i = 0; i < nb_dbg; i++)
2170 dump_dir_debug_dir(debugDir, i);
2171 debugDir++;
2173 printf("\n");
2176 static inline void print_clrflags(const char *title, UINT value)
2178 printf(" %-34s 0x%X\n", title, value);
2179 #define X(f,s) if (value & f) printf(" %s\n", s)
2180 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
2181 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
2182 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
2183 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
2184 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
2185 #undef X
2188 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
2190 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
2193 static void dump_dir_clr_header(void)
2195 unsigned int size = 0;
2196 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
2198 if (!dir) return;
2200 printf( "CLR Header\n" );
2201 print_dword( "Header Size", dir->cb );
2202 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
2203 print_clrflags( "Flags", dir->Flags );
2204 print_dword( "EntryPointToken", dir->EntryPointToken );
2205 printf("\n");
2206 printf( "CLR Data Directory\n" );
2207 print_clrdirectory( "MetaData", &dir->MetaData );
2208 print_clrdirectory( "Resources", &dir->Resources );
2209 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
2210 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
2211 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
2212 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
2213 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
2214 printf("\n");
2217 static void dump_dynamic_relocs_arm64x( const IMAGE_BASE_RELOCATION *base_reloc, unsigned int size )
2219 unsigned int i;
2220 const IMAGE_BASE_RELOCATION *base_end = (const IMAGE_BASE_RELOCATION *)((const char *)base_reloc + size);
2222 printf( "Relocations ARM64X\n" );
2223 while (base_reloc < base_end - 1 && base_reloc->SizeOfBlock)
2225 const USHORT *rel = (const USHORT *)(base_reloc + 1);
2226 const USHORT *end = (const USHORT *)base_reloc + base_reloc->SizeOfBlock / sizeof(USHORT);
2227 printf( " Page %x\n", (UINT)base_reloc->VirtualAddress );
2228 while (rel < end && *rel)
2230 USHORT offset = *rel & 0xfff;
2231 USHORT type = (*rel >> 12) & 3;
2232 USHORT arg = *rel >> 14;
2233 rel++;
2234 switch (type)
2236 case 0: /* zero-fill */
2237 printf( " off %04x zero-fill %u bytes\n", offset, 1 << arg );
2238 break;
2239 case 1: /* set value */
2240 printf( " off %04x set %u bytes value ", offset, 1 << arg );
2241 for (i = (1 << arg ) / sizeof(USHORT); i > 0; i--) printf( "%04x", rel[i - 1] );
2242 rel += (1 << arg) / sizeof(USHORT);
2243 printf( "\n" );
2244 break;
2245 case 2: /* add value */
2246 printf( " off %04x add offset ", offset );
2247 if (arg & 1) printf( "-" );
2248 printf( "%08x\n", (UINT)*rel++ * ((arg & 2) ? 8 : 4) );
2249 break;
2250 default:
2251 printf( " off %04x unknown (arg %x)\n", offset, arg );
2252 break;
2255 base_reloc = (const IMAGE_BASE_RELOCATION *)end;
2259 static void dump_dynamic_relocs( const char *ptr, unsigned int size, ULONGLONG symbol )
2261 switch (symbol)
2263 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE:
2264 printf( "Relocations GUARD_RF_PROLOGUE\n" );
2265 break;
2266 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE:
2267 printf( "Relocations GUARD_RF_EPILOGUE\n" );
2268 break;
2269 case IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER:
2270 printf( "Relocations GUARD_IMPORT_CONTROL_TRANSFER\n" );
2271 break;
2272 case IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER:
2273 printf( "Relocations GUARD_INDIR_CONTROL_TRANSFER\n" );
2274 break;
2275 case IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH:
2276 printf( "Relocations GUARD_SWITCHTABLE_BRANCH\n" );
2277 break;
2278 case IMAGE_DYNAMIC_RELOCATION_ARM64X:
2279 dump_dynamic_relocs_arm64x( (const IMAGE_BASE_RELOCATION *)ptr, size );
2280 break;
2281 default:
2282 printf( "Unknown relocation symbol %s\n", longlong_str(symbol) );
2283 break;
2287 static const IMAGE_DYNAMIC_RELOCATION_TABLE *get_dyn_reloc_table(void)
2289 unsigned int size, section, offset;
2290 const IMAGE_SECTION_HEADER *sec;
2291 const IMAGE_DYNAMIC_RELOCATION_TABLE *table;
2293 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2295 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2296 if (!cfg) return NULL;
2297 size = min( size, cfg->Size );
2298 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTableSection )) return NULL;
2299 offset = cfg->DynamicValueRelocTableOffset;
2300 section = cfg->DynamicValueRelocTableSection;
2302 else
2304 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2305 if (!cfg) return NULL;
2306 size = min( size, cfg->Size );
2307 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTableSection )) return NULL;
2308 offset = cfg->DynamicValueRelocTableOffset;
2309 section = cfg->DynamicValueRelocTableSection;
2311 if (!section || section > PE_nt_headers->FileHeader.NumberOfSections) return NULL;
2312 sec = IMAGE_FIRST_SECTION( PE_nt_headers ) + section - 1;
2313 if (offset >= sec->SizeOfRawData) return NULL;
2314 return PRD( sec->PointerToRawData + offset, sizeof(*table) );
2317 static void dump_dir_dynamic_reloc(void)
2319 const char *ptr, *end;
2320 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2322 if (!table) return;
2324 printf( "Dynamic relocations (version %u)\n\n", (UINT)table->Version );
2325 ptr = (const char *)(table + 1);
2326 end = ptr + table->Size;
2327 while (ptr < end)
2329 switch (table->Version)
2331 case 1:
2332 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2334 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2335 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2336 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2338 else
2340 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2341 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2342 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2344 break;
2345 case 2:
2346 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2348 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2349 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2350 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2352 else
2354 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2355 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2356 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2358 break;
2361 printf( "\n" );
2364 static const IMAGE_BASE_RELOCATION *get_armx_relocs( unsigned int *size )
2366 const char *ptr, *end;
2367 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2369 if (!table) return NULL;
2370 ptr = (const char *)(table + 1);
2371 end = ptr + table->Size;
2372 while (ptr < end)
2374 switch (table->Version)
2376 case 1:
2377 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2379 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2380 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2382 *size = reloc->BaseRelocSize;
2383 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2385 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2387 else
2389 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2390 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2392 *size = reloc->BaseRelocSize;
2393 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2395 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2397 break;
2398 case 2:
2399 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2401 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2402 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2404 *size = reloc->FixupInfoSize;
2405 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2407 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2409 else
2411 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2412 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2414 *size = reloc->FixupInfoSize;
2415 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2417 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2419 break;
2422 return NULL;
2425 static const IMAGE_NT_HEADERS32 *get_alt_header( void )
2427 unsigned int page_size, size;
2428 const IMAGE_BASE_RELOCATION *end, *reloc = get_armx_relocs( &size );
2429 char *alt_dos;
2430 const IMAGE_NT_HEADERS32 *hdr;
2432 if (!reloc) return NULL;
2433 page_size = PE_nt_headers->OptionalHeader.SectionAlignment;
2434 alt_dos = malloc( page_size );
2435 memcpy( alt_dos, PRD(0, page_size), page_size );
2436 end = (const IMAGE_BASE_RELOCATION *)((const char *)reloc + size);
2437 hdr = (const IMAGE_NT_HEADERS32 *)(alt_dos + ((IMAGE_DOS_HEADER *)alt_dos)->e_lfanew);
2439 while (reloc < end - 1 && reloc->SizeOfBlock)
2441 const USHORT *rel = (const USHORT *)(reloc + 1);
2442 const USHORT *rel_end = (const USHORT *)reloc + reloc->SizeOfBlock / sizeof(USHORT);
2444 if (!reloc->VirtualAddress) /* only apply relocs to page 0 */
2446 while (rel < rel_end && *rel)
2448 USHORT offset = *rel & 0xfff;
2449 USHORT type = (*rel >> 12) & 3;
2450 USHORT arg = *rel >> 14;
2451 int val;
2452 rel++;
2453 switch (type)
2455 case 0: /* zero-fill */
2456 memset( alt_dos + offset, 0, 1 << arg );
2457 break;
2458 case 1: /* set value */
2459 memcpy( alt_dos + offset, rel, 1 << arg );
2460 rel += (1 << arg) / sizeof(USHORT);
2461 break;
2462 case 2: /* add value */
2463 val = (unsigned int)*rel++ * ((arg & 2) ? 8 : 4);
2464 if (arg & 1) val = -val;
2465 *(int *)(alt_dos + offset) += val;
2466 break;
2470 reloc = (const IMAGE_BASE_RELOCATION *)rel_end;
2472 return hdr;
2475 static void dump_dir_reloc(void)
2477 unsigned int i, size = 0;
2478 const USHORT *relocs;
2479 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
2480 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
2481 static const char * const names[] =
2483 "BASED_ABSOLUTE",
2484 "BASED_HIGH",
2485 "BASED_LOW",
2486 "BASED_HIGHLOW",
2487 "BASED_HIGHADJ",
2488 "BASED_MIPS_JMPADDR",
2489 "BASED_SECTION",
2490 "BASED_REL",
2491 "unknown 8",
2492 "BASED_IA64_IMM64",
2493 "BASED_DIR64",
2494 "BASED_HIGH3ADJ",
2495 "unknown 12",
2496 "unknown 13",
2497 "unknown 14",
2498 "unknown 15"
2501 if (!rel) return;
2503 printf( "Relocations\n" );
2504 while (rel < end - 1 && rel->SizeOfBlock)
2506 printf( " Page %x\n", (UINT)rel->VirtualAddress );
2507 relocs = (const USHORT *)(rel + 1);
2508 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
2509 while (i--)
2511 USHORT offset = *relocs & 0xfff;
2512 int type = *relocs >> 12;
2513 printf( " off %04x type %s\n", offset, names[type] );
2514 relocs++;
2516 rel = (const IMAGE_BASE_RELOCATION *)relocs;
2518 printf("\n");
2521 static void dump_dir_tls(void)
2523 IMAGE_TLS_DIRECTORY64 dir;
2524 const UINT *callbacks;
2525 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
2527 if (!pdir) return;
2529 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2530 memcpy(&dir, pdir, sizeof(dir));
2531 else
2533 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
2534 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
2535 dir.AddressOfIndex = pdir->AddressOfIndex;
2536 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
2537 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
2538 dir.Characteristics = pdir->Characteristics;
2541 /* FIXME: This does not properly handle large images */
2542 printf( "Thread Local Storage\n" );
2543 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2544 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
2545 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2546 (UINT)dir.SizeOfZeroFill );
2547 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
2548 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
2549 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
2550 if (dir.AddressOfCallBacks)
2552 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2553 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
2555 printf( " %08x", *callbacks );
2556 addr += sizeof(UINT);
2559 printf(" }\n\n");
2562 enum FileSig get_kind_dbg(void)
2564 const WORD* pw;
2566 pw = PRD(0, sizeof(WORD));
2567 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2569 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2570 return SIG_UNKNOWN;
2573 void dbg_dump(void)
2575 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2576 unsigned nb_dbg;
2577 unsigned i;
2578 const IMAGE_DEBUG_DIRECTORY* debugDir;
2580 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2581 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2583 printf ("Signature: %.2s (0x%4X)\n",
2584 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2585 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2586 printf ("Machine: 0x%04X (%s)\n",
2587 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2588 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2589 printf ("TimeDateStamp: 0x%08X (%s)\n",
2590 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2591 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
2592 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
2593 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
2594 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
2595 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
2596 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
2598 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2599 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2600 {printf("Can't get the sections, aborting\n"); return;}
2602 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2604 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2605 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2606 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2607 separateDebugHead->ExportedNamesSize,
2608 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2609 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2611 printf("Debug Table (%u directories)\n", nb_dbg);
2613 for (i = 0; i < nb_dbg; i++)
2615 dump_dir_debug_dir(debugDir, i);
2616 debugDir++;
2620 static const char *get_resource_type( unsigned int id )
2622 static const char * const types[] =
2624 NULL,
2625 "CURSOR",
2626 "BITMAP",
2627 "ICON",
2628 "MENU",
2629 "DIALOG",
2630 "STRING",
2631 "FONTDIR",
2632 "FONT",
2633 "ACCELERATOR",
2634 "RCDATA",
2635 "MESSAGETABLE",
2636 "GROUP_CURSOR",
2637 NULL,
2638 "GROUP_ICON",
2639 NULL,
2640 "VERSION",
2641 "DLGINCLUDE",
2642 NULL,
2643 "PLUGPLAY",
2644 "VXD",
2645 "ANICURSOR",
2646 "ANIICON",
2647 "HTML",
2648 "RT_MANIFEST"
2651 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2652 return NULL;
2655 /* dump an ASCII string with proper escaping */
2656 static int dump_strA( const unsigned char *str, size_t len )
2658 static const char escapes[32] = ".......abtnvfr.............e....";
2659 char buffer[256];
2660 char *pos = buffer;
2661 int count = 0;
2663 for (; len; str++, len--)
2665 if (pos > buffer + sizeof(buffer) - 8)
2667 fwrite( buffer, pos - buffer, 1, stdout );
2668 count += pos - buffer;
2669 pos = buffer;
2671 if (*str > 127) /* hex escape */
2673 pos += sprintf( pos, "\\x%02x", *str );
2674 continue;
2676 if (*str < 32) /* octal or C escape */
2678 if (!*str && len == 1) continue; /* do not output terminating NULL */
2679 if (escapes[*str] != '.')
2680 pos += sprintf( pos, "\\%c", escapes[*str] );
2681 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2682 pos += sprintf( pos, "\\%03o", *str );
2683 else
2684 pos += sprintf( pos, "\\%o", *str );
2685 continue;
2687 if (*str == '\\') *pos++ = '\\';
2688 *pos++ = *str;
2690 fwrite( buffer, pos - buffer, 1, stdout );
2691 count += pos - buffer;
2692 return count;
2695 /* dump a Unicode string with proper escaping */
2696 static int dump_strW( const WCHAR *str, size_t len )
2698 static const char escapes[32] = ".......abtnvfr.............e....";
2699 char buffer[256];
2700 char *pos = buffer;
2701 int count = 0;
2703 for (; len; str++, len--)
2705 if (pos > buffer + sizeof(buffer) - 8)
2707 fwrite( buffer, pos - buffer, 1, stdout );
2708 count += pos - buffer;
2709 pos = buffer;
2711 if (*str > 127) /* hex escape */
2713 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2714 pos += sprintf( pos, "\\x%04x", *str );
2715 else
2716 pos += sprintf( pos, "\\x%x", *str );
2717 continue;
2719 if (*str < 32) /* octal or C escape */
2721 if (!*str && len == 1) continue; /* do not output terminating NULL */
2722 if (escapes[*str] != '.')
2723 pos += sprintf( pos, "\\%c", escapes[*str] );
2724 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2725 pos += sprintf( pos, "\\%03o", *str );
2726 else
2727 pos += sprintf( pos, "\\%o", *str );
2728 continue;
2730 if (*str == '\\') *pos++ = '\\';
2731 *pos++ = *str;
2733 fwrite( buffer, pos - buffer, 1, stdout );
2734 count += pos - buffer;
2735 return count;
2738 /* dump data for a STRING resource */
2739 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2741 int i;
2743 for (i = 0; i < 16 && size; i++)
2745 unsigned len = *ptr++;
2747 if (len >= size)
2749 len = size;
2750 size = 0;
2752 else size -= len + 1;
2754 if (len)
2756 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2757 dump_strW( ptr, len );
2758 printf( "\"\n" );
2759 ptr += len;
2764 /* dump data for a MESSAGETABLE resource */
2765 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2767 const MESSAGE_RESOURCE_DATA *data = ptr;
2768 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2769 unsigned i, j;
2771 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2773 const MESSAGE_RESOURCE_ENTRY *entry;
2775 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2776 for (j = block->LowId; j <= block->HighId; j++)
2778 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2780 const WCHAR *str = (const WCHAR *)entry->Text;
2781 printf( "%s%08x L\"", prefix, j );
2782 dump_strW( str, strlenW(str) );
2783 printf( "\"\n" );
2785 else
2787 const char *str = (const char *) entry->Text;
2788 printf( "%s%08x \"", prefix, j );
2789 dump_strA( entry->Text, strlen(str) );
2790 printf( "\"\n" );
2792 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2797 static void dump_dir_resource(void)
2799 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2800 const IMAGE_RESOURCE_DIRECTORY *namedir;
2801 const IMAGE_RESOURCE_DIRECTORY *langdir;
2802 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2803 const IMAGE_RESOURCE_DIR_STRING_U *string;
2804 const IMAGE_RESOURCE_DATA_ENTRY *data;
2805 int i, j, k;
2807 if (!root) return;
2809 printf( "Resources:" );
2811 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2813 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2814 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
2815 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2817 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2818 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
2819 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2821 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2823 printf( "\n " );
2824 if (e1->NameIsString)
2826 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
2827 dump_unicode_str( string->NameString, string->Length );
2829 else
2831 const char *type = get_resource_type( e1->Id );
2832 if (type) printf( "%s", type );
2833 else printf( "%04x", e1->Id );
2836 printf( " Name=" );
2837 if (e2->NameIsString)
2839 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
2840 dump_unicode_str( string->NameString, string->Length );
2842 else
2843 printf( "%04x", e2->Id );
2845 printf( " Language=%04x:\n", e3->Id );
2846 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
2847 if (e1->NameIsString)
2849 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2851 else switch(e1->Id)
2853 case 6:
2854 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2855 break;
2856 case 11:
2857 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2858 break;
2859 default:
2860 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2861 break;
2866 printf( "\n\n" );
2869 static void dump_debug(void)
2871 const char* stabs = NULL;
2872 unsigned szstabs = 0;
2873 const char* stabstr = NULL;
2874 unsigned szstr = 0;
2875 unsigned i;
2876 const IMAGE_SECTION_HEADER* sectHead;
2878 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2880 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2882 if (!strcmp((const char *)sectHead->Name, ".stab"))
2884 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2885 szstabs = sectHead->Misc.VirtualSize;
2887 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2889 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2890 szstr = sectHead->Misc.VirtualSize;
2893 if (stabs && stabstr)
2894 dump_stabs(stabs, szstabs, stabstr, szstr);
2897 static void dump_symbol_table(void)
2899 const IMAGE_SYMBOL* sym;
2900 int numsym;
2902 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2903 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2904 return;
2905 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2906 sizeof(*sym) * numsym);
2907 if (!sym) return;
2909 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2912 enum FileSig get_kind_exec(void)
2914 const WORD* pw;
2915 const DWORD* pdw;
2916 const IMAGE_DOS_HEADER* dh;
2918 pw = PRD(0, sizeof(WORD));
2919 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2921 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2923 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2925 /* the signature is the first DWORD */
2926 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2927 if (pdw)
2929 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2930 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2931 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2933 return SIG_DOS;
2935 return SIG_UNKNOWN;
2938 void pe_dump(void)
2940 PE_nt_headers = get_nt_header();
2941 PE_alt_headers = get_alt_header();
2942 print_fake_dll();
2944 if (globals.do_dumpheader)
2946 dump_pe_header();
2947 /* FIXME: should check ptr */
2948 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2949 PE_nt_headers->FileHeader.NumberOfSections);
2951 else if (!globals.dumpsect)
2953 /* show at least something here */
2954 dump_pe_header();
2957 if (globals_dump_sect("import"))
2959 dump_dir_imported_functions();
2960 dump_dir_delay_imported_functions();
2962 if (globals_dump_sect("export"))
2963 dump_dir_exported_functions();
2964 if (globals_dump_sect("debug"))
2965 dump_dir_debug();
2966 if (globals_dump_sect("resource"))
2967 dump_dir_resource();
2968 if (globals_dump_sect("tls"))
2969 dump_dir_tls();
2970 if (globals_dump_sect("loadcfg"))
2971 dump_dir_loadconfig();
2972 if (globals_dump_sect("clr"))
2973 dump_dir_clr_header();
2974 if (globals_dump_sect("reloc"))
2975 dump_dir_reloc();
2976 if (globals_dump_sect("dynreloc"))
2977 dump_dir_dynamic_reloc();
2978 if (globals_dump_sect("except"))
2979 dump_dir_exceptions();
2980 if (globals_dump_sect("apiset"))
2981 dump_section_apiset();
2983 if (globals.do_symbol_table)
2984 dump_symbol_table();
2985 if (globals.do_debug)
2986 dump_debug();
2989 typedef struct _dll_symbol {
2990 size_t ordinal;
2991 char *symbol;
2992 } dll_symbol;
2994 static dll_symbol *dll_symbols = NULL;
2995 static dll_symbol *dll_current_symbol = NULL;
2997 /* Compare symbols by ordinal for qsort */
2998 static int symbol_cmp(const void *left, const void *right)
3000 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
3003 /*******************************************************************
3004 * dll_close
3006 * Free resources used by DLL
3008 /* FIXME: Not used yet
3009 static void dll_close (void)
3011 dll_symbol* ds;
3013 if (!dll_symbols) {
3014 fatal("No symbols");
3016 for (ds = dll_symbols; ds->symbol; ds++)
3017 free(ds->symbol);
3018 free (dll_symbols);
3019 dll_symbols = NULL;
3023 static void do_grab_sym( void )
3025 const IMAGE_EXPORT_DIRECTORY*exportDir;
3026 UINT i, j, *map;
3027 const UINT *pName;
3028 const UINT *pFunc;
3029 const WORD *pOrdl;
3030 const char *ptr;
3032 PE_nt_headers = get_nt_header();
3033 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
3035 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
3036 if (!pName) {printf("Can't grab functions' name table\n"); return;}
3037 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
3038 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
3039 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
3040 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
3042 /* dll_close(); */
3044 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
3046 /* bit map of used funcs */
3047 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
3048 if (!map) fatal("no memory");
3050 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
3052 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
3053 ptr = RVA(*pName++, sizeof(DWORD));
3054 if (!ptr) ptr = "cant_get_function";
3055 dll_symbols[j].symbol = xstrdup(ptr);
3056 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
3057 assert(dll_symbols[j].symbol);
3060 for (i = 0; i < exportDir->NumberOfFunctions; i++)
3062 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
3064 char ordinal_text[256];
3065 /* Ordinal only entry */
3066 sprintf (ordinal_text, "%s_%u",
3067 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
3068 (UINT)exportDir->Base + i);
3069 str_toupper(ordinal_text);
3070 dll_symbols[j].symbol = xstrdup(ordinal_text);
3071 assert(dll_symbols[j].symbol);
3072 dll_symbols[j].ordinal = exportDir->Base + i;
3073 j++;
3074 assert(j <= exportDir->NumberOfFunctions);
3077 free(map);
3079 if (NORMAL)
3080 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
3081 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
3082 j, (UINT)exportDir->Base);
3084 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
3086 dll_symbols[j].symbol = NULL;
3088 dll_current_symbol = dll_symbols;
3091 /*******************************************************************
3092 * dll_open
3094 * Open a DLL and read in exported symbols
3096 BOOL dll_open (const char *dll_name)
3098 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
3101 /*******************************************************************
3102 * dll_next_symbol
3104 * Get next exported symbol from dll
3106 BOOL dll_next_symbol (parsed_symbol * sym)
3108 if (!dll_current_symbol || !dll_current_symbol->symbol)
3109 return FALSE;
3110 assert (dll_symbols);
3111 sym->symbol = xstrdup (dll_current_symbol->symbol);
3112 sym->ordinal = dll_current_symbol->ordinal;
3113 dll_current_symbol++;
3114 return TRUE;