include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / tools / winedump / pe.c
blob6acab7e47739c46f89a07baa17a142882c7d948b
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 "verrsrc.h"
32 #include "winedump.h"
34 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
36 static const IMAGE_NT_HEADERS32* PE_nt_headers;
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_I386: return "i386";
48 case IMAGE_FILE_MACHINE_R3000: return "R3000";
49 case IMAGE_FILE_MACHINE_R4000: return "R4000";
50 case IMAGE_FILE_MACHINE_R10000: return "R10000";
51 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
52 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
53 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
54 case IMAGE_FILE_MACHINE_IA64: return "IA64";
55 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
56 case IMAGE_FILE_MACHINE_ARM: return "ARM";
57 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
58 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
59 case IMAGE_FILE_MACHINE_ALPHA64: return "Alpha64";
60 case IMAGE_FILE_MACHINE_CHPE_X86: return "CHPE-x86";
61 case IMAGE_FILE_MACHINE_ARM64EC: return "ARM64EC";
62 case IMAGE_FILE_MACHINE_ARM64X: return "ARM64X";
63 case IMAGE_FILE_MACHINE_RISCV32: return "RISC-V 32-bit";
64 case IMAGE_FILE_MACHINE_RISCV64: return "RISC-V 64-bit";
65 case IMAGE_FILE_MACHINE_RISCV128: return "RISC-V 128-bit";
67 return "???";
70 static const void* RVA(unsigned long rva, unsigned long len)
72 IMAGE_SECTION_HEADER* sectHead;
73 int i;
75 if (rva == 0) return NULL;
77 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
78 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
80 if (sectHead[i].VirtualAddress <= rva &&
81 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
83 /* return image import directory offset */
84 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
88 return NULL;
91 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
93 const IMAGE_DOS_HEADER *dos;
94 dos = PRD(0, sizeof(*dos));
95 if (!dos) return NULL;
96 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
97 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
98 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
101 void print_fake_dll( void )
103 const IMAGE_DOS_HEADER *dos;
105 dos = PRD(0, sizeof(*dos) + 32);
106 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
108 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
109 printf( "*** This is a Wine builtin DLL ***\n\n" );
110 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
111 printf( "*** This is a Wine fake DLL ***\n\n" );
115 static const void *get_data_dir(const IMAGE_NT_HEADERS32 *hdr, unsigned int idx, unsigned int *size)
117 if(hdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
119 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&hdr->OptionalHeader;
120 if (idx >= opt->NumberOfRvaAndSizes)
121 return NULL;
122 if(size)
123 *size = opt->DataDirectory[idx].Size;
124 return RVA(opt->DataDirectory[idx].VirtualAddress,
125 opt->DataDirectory[idx].Size);
127 else
129 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&hdr->OptionalHeader;
130 if (idx >= opt->NumberOfRvaAndSizes)
131 return NULL;
132 if(size)
133 *size = opt->DataDirectory[idx].Size;
134 return RVA(opt->DataDirectory[idx].VirtualAddress,
135 opt->DataDirectory[idx].Size);
139 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
141 return get_data_dir( PE_nt_headers, idx, size );
144 static const void* get_dir(unsigned idx)
146 return get_dir_and_size(idx, 0);
149 static const char * const DirectoryNames[16] = {
150 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
151 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
152 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
153 "IAT", "Delay IAT", "CLR Header", ""
156 static const char *get_magic_type(WORD magic)
158 switch(magic) {
159 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
160 return "32bit";
161 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
162 return "64bit";
163 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
164 return "ROM";
166 return "???";
169 static const void *get_hybrid_metadata(void)
171 unsigned int size;
173 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
175 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
176 if (!cfg) return 0;
177 size = min( size, cfg->Size );
178 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CHPEMetadataPointer )) return 0;
179 if (!cfg->CHPEMetadataPointer) return 0;
180 return RVA( cfg->CHPEMetadataPointer - ((const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader)->ImageBase, 1 );
182 else
184 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
185 if (!cfg) return 0;
186 size = min( size, cfg->Size );
187 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CHPEMetadataPointer )) return 0;
188 if (!cfg->CHPEMetadataPointer) return 0;
189 return RVA( cfg->CHPEMetadataPointer - PE_nt_headers->OptionalHeader.ImageBase, 1 );
193 static inline const char *longlong_str( ULONGLONG value )
195 static char buffer[20];
197 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
198 sprintf(buffer, "%lx%08lx", (unsigned long)(value >> 32), (unsigned long)value);
199 else
200 sprintf(buffer, "%lx", (unsigned long)value);
201 return buffer;
204 static inline void print_word(const char *title, WORD value)
206 printf(" %-34s 0x%-4X %u\n", title, value, value);
209 static inline void print_dword(const char *title, UINT value)
211 printf(" %-34s 0x%-8x %u\n", title, value, value);
214 static inline void print_longlong(const char *title, ULONGLONG value)
216 printf(" %-34s 0x%s\n", title, longlong_str(value));
219 static inline void print_ver(const char *title, BYTE major, BYTE minor)
221 printf(" %-34s %u.%02u\n", title, major, minor);
224 static inline void print_subsys(const char *title, WORD value)
226 const char *str;
227 switch (value)
229 default:
230 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
231 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
232 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
233 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
234 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
235 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
236 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
237 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
238 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
239 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
240 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
241 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
242 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
243 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
245 printf(" %-34s 0x%X (%s)\n", title, value, str);
248 static inline void print_dllflags(const char *title, WORD value)
250 printf(" %-34s 0x%04X\n", title, value);
251 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
252 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
253 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
254 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
255 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
256 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
257 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
258 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
259 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
260 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
261 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
262 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
263 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
264 #undef X
267 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
269 unsigned i;
270 printf("Data Directory\n");
272 for (i = 0; i < n && i < 16; i++)
274 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
275 DirectoryNames[i], (UINT)directory[i].VirtualAddress,
276 (UINT)directory[i].Size);
280 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh)
282 IMAGE_OPTIONAL_HEADER32 oh;
283 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
285 /* in case optional header is missing or partial */
286 memset(&oh, 0, sizeof(oh));
287 memcpy(&oh, image_oh, min(dump_total_len - ((char *)image_oh - (char *)dump_base), sizeof(oh)));
288 optionalHeader = &oh;
290 print_word("Magic", optionalHeader->Magic);
291 print_ver("linker version",
292 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
293 print_dword("size of code", optionalHeader->SizeOfCode);
294 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
295 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
296 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
297 print_dword("base of code", optionalHeader->BaseOfCode);
298 print_dword("base of data", optionalHeader->BaseOfData);
299 print_dword("image base", optionalHeader->ImageBase);
300 print_dword("section align", optionalHeader->SectionAlignment);
301 print_dword("file align", optionalHeader->FileAlignment);
302 print_ver("required OS version",
303 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
304 print_ver("image version",
305 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
306 print_ver("subsystem version",
307 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
308 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
309 print_dword("size of image", optionalHeader->SizeOfImage);
310 print_dword("size of headers", optionalHeader->SizeOfHeaders);
311 print_dword("checksum", optionalHeader->CheckSum);
312 print_subsys("Subsystem", optionalHeader->Subsystem);
313 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
314 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
315 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
316 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
317 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
318 print_dword("loader flags", optionalHeader->LoaderFlags);
319 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
320 printf("\n");
321 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
322 printf("\n");
325 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh)
327 IMAGE_OPTIONAL_HEADER64 oh;
328 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
330 /* in case optional header is missing or partial */
331 memset(&oh, 0, sizeof(oh));
332 memcpy(&oh, image_oh, min(dump_total_len - ((char *)image_oh - (char *)dump_base), sizeof(oh)));
333 optionalHeader = &oh;
335 print_word("Magic", optionalHeader->Magic);
336 print_ver("linker version",
337 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
338 print_dword("size of code", optionalHeader->SizeOfCode);
339 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
340 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
341 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
342 print_dword("base of code", optionalHeader->BaseOfCode);
343 print_longlong("image base", optionalHeader->ImageBase);
344 print_dword("section align", optionalHeader->SectionAlignment);
345 print_dword("file align", optionalHeader->FileAlignment);
346 print_ver("required OS version",
347 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
348 print_ver("image version",
349 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
350 print_ver("subsystem version",
351 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
352 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
353 print_dword("size of image", optionalHeader->SizeOfImage);
354 print_dword("size of headers", optionalHeader->SizeOfHeaders);
355 print_dword("checksum", optionalHeader->CheckSum);
356 print_subsys("Subsystem", optionalHeader->Subsystem);
357 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
358 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
359 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
360 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
361 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
362 print_dword("loader flags", optionalHeader->LoaderFlags);
363 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
364 printf("\n");
365 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
366 printf("\n");
369 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader)
371 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
373 switch(optionalHeader->Magic) {
374 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
375 dump_optional_header32(optionalHeader);
376 break;
377 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
378 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader);
379 break;
380 default:
381 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
382 break;
386 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader, BOOL is_hybrid)
388 const char *name = get_machine_str(fileHeader->Machine);
390 printf("File Header\n");
392 if (is_hybrid)
394 switch (fileHeader->Machine)
396 case IMAGE_FILE_MACHINE_I386: name = "CHPE"; break;
397 case IMAGE_FILE_MACHINE_AMD64: name = "ARM64EC"; break;
398 case IMAGE_FILE_MACHINE_ARM64: name = "ARM64X"; break;
401 printf(" Machine: %04X (%s)\n", fileHeader->Machine, name);
402 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
403 printf(" TimeDateStamp: %08X (%s)\n",
404 (UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp));
405 printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
406 printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
407 printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
408 printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
409 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
410 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
411 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
412 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
413 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
414 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
415 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
416 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
417 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
418 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
419 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
420 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
421 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
422 X(IMAGE_FILE_SYSTEM, "SYSTEM");
423 X(IMAGE_FILE_DLL, "DLL");
424 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
425 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
426 #undef X
427 printf("\n");
430 static void dump_pe_header(void)
432 dump_file_header(&PE_nt_headers->FileHeader, get_hybrid_metadata() != NULL);
433 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader);
436 void dump_section_characteristics(DWORD characteristics, const char* sep)
438 #define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
439 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
440 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
441 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
442 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
443 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
444 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
446 X(IMAGE_SCN_CNT_CODE, "CODE");
447 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
448 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
450 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
451 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
452 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
453 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
454 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
456 /* 0x00002000 - Reserved */
457 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
458 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
460 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
461 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
462 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
463 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
464 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
466 switch (characteristics & IMAGE_SCN_ALIGN_MASK)
468 #define X2(b,s) case b: printf("%s%s", sep, s); break
469 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
470 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
471 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
472 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
473 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
474 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
475 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
476 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
477 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
478 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
479 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
480 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
481 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
482 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
483 #undef X2
486 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
488 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
489 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
490 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
491 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
492 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
493 X(IMAGE_SCN_MEM_READ, "MEM_READ");
494 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
495 #undef X
498 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
500 unsigned offset;
502 /* long section name ? */
503 if (strtable && sectHead->Name[0] == '/' &&
504 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
505 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
506 else
507 printf(" %-8.8s", sectHead->Name);
508 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
509 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
510 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
511 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
512 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
513 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
514 printf(" line # offs: %-8u line #'s: %-8u\n",
515 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
516 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
517 printf(" ");
518 dump_section_characteristics(sectHead->Characteristics, " ");
520 printf("\n\n");
523 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
525 const IMAGE_SECTION_HEADER* sectHead = addr;
526 unsigned i;
527 const char* strtable;
529 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
531 strtable = (const char*)base +
532 PE_nt_headers->FileHeader.PointerToSymbolTable +
533 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
535 else strtable = NULL;
537 printf("Section Table\n");
538 for (i = 0; i < num_sect; i++, sectHead++)
540 dump_section(sectHead, strtable);
542 if (globals.do_dump_rawdata)
544 dump_data_offset((const unsigned char *)base + sectHead->PointerToRawData,
545 sectHead->SizeOfRawData, sectHead->VirtualAddress, " " );
546 printf("\n");
551 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
553 const WCHAR *wstr = PRD( rva, len );
554 char *ret = buffer;
556 len /= sizeof(WCHAR);
557 while (len--) *buffer++ = *wstr++;
558 *buffer = 0;
559 return ret;
562 static void dump_section_apiset(void)
564 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
565 const UINT *ptr, *entry, *value, *hash;
566 unsigned int i, j, count, val_count, rva;
567 char buffer[128];
569 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
571 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
572 rva = sect->PointerToRawData;
573 ptr = PRD( rva, sizeof(*ptr) );
574 printf( "ApiSet section:\n" );
575 switch (ptr[0]) /* version */
577 case 2:
578 printf( " Version: %u\n", ptr[0] );
579 printf( " Count: %08x\n", ptr[1] );
580 count = ptr[1];
581 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
582 for (i = 0; i < count; i++, entry += 3)
584 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
585 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
586 val_count = *value++;
587 for (j = 0; j < val_count; j++, value += 4)
589 putchar( ' ' );
590 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
591 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
593 printf( "\n");
595 break;
596 case 4:
597 printf( " Version: %u\n", ptr[0] );
598 printf( " Size: %08x\n", ptr[1] );
599 printf( " Flags: %08x\n", ptr[2] );
600 printf( " Count: %08x\n", ptr[3] );
601 count = ptr[3];
602 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
603 for (i = 0; i < count; i++, entry += 6)
605 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
606 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
607 value++; /* flags */
608 val_count = *value++;
609 for (j = 0; j < val_count; j++, value += 5)
611 putchar( ' ' );
612 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
613 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
615 printf( "\n");
617 break;
618 case 6:
619 printf( " Version: %u\n", ptr[0] );
620 printf( " Size: %08x\n", ptr[1] );
621 printf( " Flags: %08x\n", ptr[2] );
622 printf( " Count: %08x\n", ptr[3] );
623 printf( " EntryOffset: %08x\n", ptr[4] );
624 printf( " HashOffset: %08x\n", ptr[5] );
625 printf( " HashFactor: %08x\n", ptr[6] );
626 count = ptr[3];
627 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
628 for (i = 0; i < count; i++, entry += 6)
630 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
631 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
632 for (j = 0; j < entry[5]; j++, value += 5)
634 putchar( ' ' );
635 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
636 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
638 printf( "\n" );
640 printf( " Hash table:\n" );
641 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
642 for (i = 0; i < count; i++, hash += 2)
644 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
645 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
647 break;
648 default:
649 printf( "*** Unknown version %u\n", ptr[0] );
650 break;
652 break;
656 static const char *find_export_from_rva( UINT rva )
658 UINT i, *func_names;
659 const UINT *funcs;
660 const UINT *names;
661 const WORD *ordinals;
662 const IMAGE_EXPORT_DIRECTORY *dir;
663 const char *ret = NULL;
665 if (!(dir = get_dir( IMAGE_FILE_EXPORT_DIRECTORY ))) return "";
666 if (!(funcs = RVA( dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD) ))) return "";
667 names = RVA( dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD) );
668 ordinals = RVA( dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD) );
669 func_names = calloc( dir->NumberOfFunctions, sizeof(*func_names) );
671 for (i = 0; i < dir->NumberOfNames; i++) func_names[ordinals[i]] = names[i];
672 for (i = 0; i < dir->NumberOfFunctions; i++)
674 if (funcs[i] != rva) continue;
675 if (func_names[i]) ret = get_symbol_str( RVA( func_names[i], sizeof(DWORD) ));
676 break;
679 free( func_names );
680 if (!ret && rva == PE_nt_headers->OptionalHeader.AddressOfEntryPoint) return " <EntryPoint>";
681 return ret ? strmake( " (%s)", ret ) : "";
684 static const char *find_import_from_rva( UINT rva )
686 const IMAGE_IMPORT_DESCRIPTOR *imp;
688 if (!(imp = get_dir( IMAGE_FILE_IMPORT_DIRECTORY ))) return "";
690 /* check for import thunk */
691 switch (PE_nt_headers->FileHeader.Machine)
693 case IMAGE_FILE_MACHINE_AMD64:
695 const BYTE *ptr = RVA( rva, 6 );
696 if (!ptr) return "";
697 if (ptr[0] == 0xff && ptr[1] == 0x25)
699 rva += 6 + *(int *)(ptr + 2);
700 break;
702 if (ptr[0] == 0x48 && ptr[1] == 0xff && ptr[2] == 0x25)
704 rva += 7 + *(int *)(ptr + 3);
705 break;
707 return "";
709 case IMAGE_FILE_MACHINE_ARM64:
711 const UINT *ptr = RVA( rva, sizeof(DWORD) );
712 if ((ptr[0] & 0x9f00001f) == 0x90000010 && /* adrp x16, page */
713 (ptr[1] & 0xffc003ff) == 0xf9400210 && /* ldr x16, [x16, #off] */
714 ptr[2] == 0xd61f0200) /* br x16 */
716 rva &= ~0xfff;
717 rva += ((ptr[0] & 0x00ffffe0) << 9) + ((ptr[0] & 0x60000000) >> 17);
718 rva += (ptr[1] & 0x003ffc00) >> 7;
719 break;
721 return "";
723 default:
724 return "";
727 for ( ; imp->Name && imp->FirstThunk; imp++)
729 UINT imp_rva = imp->OriginalFirstThunk ? imp->OriginalFirstThunk : imp->FirstThunk;
730 UINT thunk_rva = imp->FirstThunk;
732 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
734 const IMAGE_THUNK_DATA64 *il = RVA( imp_rva, sizeof(DWORD) );
735 for ( ; il && il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
737 if (thunk_rva != rva) continue;
738 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
739 return strmake( " (-> #%u)", (WORD)IMAGE_ORDINAL64( il->u1.Ordinal ));
740 else
741 return strmake( " (-> %s)", get_symbol_str( RVA( (DWORD)il->u1.AddressOfData + 2, 1 )));
744 else
746 const IMAGE_THUNK_DATA32 *il = RVA( imp_rva, sizeof(DWORD) );
747 for ( ; il && il->u1.Ordinal; il++, thunk_rva += sizeof(LONG))
749 if (thunk_rva != rva) continue;
750 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
751 return strmake( " (-> #%u)", (WORD)IMAGE_ORDINAL32( il->u1.Ordinal ));
752 else
753 return strmake( " (-> %s)", get_symbol_str( RVA( (DWORD)il->u1.AddressOfData + 2, 1 )));
757 return "";
760 static const char *get_function_name( UINT rva )
762 const char *name = find_export_from_rva( rva );
763 if (!*name) name = find_import_from_rva( rva );
764 return name;
767 static void dump_dir_exported_functions(void)
769 unsigned int size;
770 const IMAGE_EXPORT_DIRECTORY *dir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
771 UINT i, *funcs;
772 const UINT *pFunc;
773 const UINT *pName;
774 const WORD *pOrdl;
776 if (!dir) return;
778 printf("\n");
779 printf(" Name: %s\n", (const char*)RVA(dir->Name, sizeof(DWORD)));
780 printf(" Characteristics: %08x\n", (UINT)dir->Characteristics);
781 printf(" TimeDateStamp: %08X %s\n",
782 (UINT)dir->TimeDateStamp, get_time_str(dir->TimeDateStamp));
783 printf(" Version: %u.%02u\n", dir->MajorVersion, dir->MinorVersion);
784 printf(" Ordinal base: %u\n", (UINT)dir->Base);
785 printf(" # of functions: %u\n", (UINT)dir->NumberOfFunctions);
786 printf(" # of Names: %u\n", (UINT)dir->NumberOfNames);
787 printf(" Functions RVA: %08X\n", (UINT)dir->AddressOfFunctions);
788 printf(" Ordinals RVA: %08X\n", (UINT)dir->AddressOfNameOrdinals);
789 printf(" Names RVA: %08X\n", (UINT)dir->AddressOfNames);
790 printf("\n");
791 printf(" Entry Pt Ordn Name\n");
793 pFunc = RVA(dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD));
794 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
795 pName = RVA(dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD));
796 pOrdl = RVA(dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD));
798 funcs = calloc( dir->NumberOfFunctions, sizeof(*funcs) );
799 if (!funcs) fatal("no memory");
801 for (i = 0; i < dir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
803 for (i = 0; i < dir->NumberOfFunctions; i++)
805 if (!pFunc[i]) continue;
806 printf(" %08X %5u ", pFunc[i], (UINT)dir->Base + i);
807 if (funcs[i])
808 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
809 else
810 printf("<by ordinal>");
812 /* check for forwarded function */
813 if ((const char *)RVA(pFunc[i],1) >= (const char *)dir &&
814 (const char *)RVA(pFunc[i],1) < (const char *)dir + size)
815 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
816 printf("\n");
818 free(funcs);
819 printf("\n");
823 struct runtime_function_x86_64
825 UINT BeginAddress;
826 UINT EndAddress;
827 UINT UnwindData;
830 struct runtime_function_armnt
832 UINT BeginAddress;
833 union {
834 UINT UnwindData;
835 struct {
836 UINT Flag : 2;
837 UINT FunctionLength : 11;
838 UINT Ret : 2;
839 UINT H : 1;
840 UINT Reg : 3;
841 UINT R : 1;
842 UINT L : 1;
843 UINT C : 1;
844 UINT StackAdjust : 10;
849 struct runtime_function_arm64
851 UINT BeginAddress;
852 union
854 UINT UnwindData;
855 struct
857 UINT Flag : 2;
858 UINT FunctionLength : 11;
859 UINT RegF : 3;
860 UINT RegI : 4;
861 UINT H : 1;
862 UINT CR : 2;
863 UINT FrameSize : 9;
868 union handler_data
870 struct runtime_function_x86_64 chain;
871 UINT handler;
874 struct opcode
876 BYTE offset;
877 BYTE code : 4;
878 BYTE info : 4;
881 struct unwind_info_x86_64
883 BYTE version : 3;
884 BYTE flags : 5;
885 BYTE prolog;
886 BYTE count;
887 BYTE frame_reg : 4;
888 BYTE frame_offset : 4;
889 struct opcode opcodes[1]; /* count entries */
890 /* followed by union handler_data */
893 struct unwind_info_armnt
895 UINT function_length : 18;
896 UINT version : 2;
897 UINT x : 1;
898 UINT e : 1;
899 UINT f : 1;
900 UINT count : 5;
901 UINT words : 4;
904 struct unwind_info_ext_armnt
906 WORD excount;
907 BYTE exwords;
908 BYTE reserved;
911 struct unwind_info_epilogue_armnt
913 UINT offset : 18;
914 UINT res : 2;
915 UINT cond : 4;
916 UINT index : 8;
919 #define UWOP_PUSH_NONVOL 0
920 #define UWOP_ALLOC_LARGE 1
921 #define UWOP_ALLOC_SMALL 2
922 #define UWOP_SET_FPREG 3
923 #define UWOP_SAVE_NONVOL 4
924 #define UWOP_SAVE_NONVOL_FAR 5
925 #define UWOP_EPILOG 6
926 #define UWOP_SAVE_XMM128 8
927 #define UWOP_SAVE_XMM128_FAR 9
928 #define UWOP_PUSH_MACHFRAME 10
930 #define UNW_FLAG_EHANDLER 1
931 #define UNW_FLAG_UHANDLER 2
932 #define UNW_FLAG_CHAININFO 4
934 static void dump_c_exception_data( unsigned int rva )
936 unsigned int i;
937 const struct
939 UINT count;
940 struct
942 UINT begin;
943 UINT end;
944 UINT handler;
945 UINT target;
946 } rec[];
947 } *table = RVA( rva, sizeof(*table) );
949 if (!table) return;
950 printf( " C exception data at %08x count %u\n", rva, table->count );
951 for (i = 0; i < table->count; i++)
952 printf( " %u: %08x-%08x handler %08x target %08x\n", i,
953 table->rec[i].begin, table->rec[i].end, table->rec[i].handler, table->rec[i].target );
956 static void dump_cxx_exception_data( unsigned int rva, unsigned int func_rva )
958 unsigned int i, j, flags = 0;
959 const unsigned int *ptr = RVA( rva, sizeof(*ptr) );
961 const struct
963 int prev;
964 UINT handler;
965 } *unwind_info;
967 const struct
969 int start;
970 int end;
971 int catch;
972 int catchblock_count;
973 UINT catchblock;
974 } *tryblock;
976 const struct
978 UINT flags;
979 UINT type_info;
980 int offset;
981 UINT handler;
982 UINT frame;
983 } *catchblock;
985 const struct
987 UINT flags;
988 UINT type_info;
989 int offset;
990 UINT handler;
991 } *catchblock32;
993 const struct
995 UINT ip;
996 int state;
997 } *ipmap;
999 const struct
1001 UINT magic;
1002 UINT unwind_count;
1003 UINT unwind_table;
1004 UINT tryblock_count;
1005 UINT tryblock;
1006 UINT ipmap_count;
1007 UINT ipmap;
1008 int unwind_help;
1009 UINT expect_list;
1010 UINT flags;
1011 } *func;
1013 if (!ptr || !*ptr) return;
1014 rva = *ptr;
1015 if (!(func = RVA( rva, sizeof(*func) ))) return;
1016 if (func->magic < 0x19930520 || func->magic > 0x19930522) return;
1017 if (func->magic > 0x19930521) flags = func->flags;
1018 printf( " C++ exception data at %08x magic %08x", rva, func->magic );
1019 if (flags & 1) printf( " sync" );
1020 if (flags & 4) printf( " noexcept" );
1021 printf( "\n" );
1022 printf( " unwind help %+d\n", func->unwind_help );
1023 if (func->magic > 0x19930520 && func->expect_list)
1024 printf( " expect_list %08x\n", func->expect_list );
1025 if (func->unwind_count)
1027 printf( " unwind table at %08x count %u\n", func->unwind_table, func->unwind_count );
1028 if ((unwind_info = RVA( func->unwind_table, func->unwind_count * sizeof(*unwind_info) )))
1030 for (i = 0; i < func->unwind_count; i++)
1031 printf( " %u: prev %d func %08x\n", i,
1032 unwind_info[i].prev, unwind_info[i].handler );
1035 if (func->tryblock_count)
1037 printf( " try table at %08x count %u\n", func->tryblock, func->tryblock_count );
1038 if ((tryblock = RVA( func->tryblock, func->tryblock_count * sizeof(*tryblock) )))
1040 for (i = 0; i < func->tryblock_count; i++)
1042 catchblock = RVA( tryblock[i].catchblock, sizeof(*catchblock) );
1043 catchblock32 = RVA( tryblock[i].catchblock, sizeof(*catchblock32) );
1044 printf( " %d: start %d end %d catch %d count %u\n", i,
1045 tryblock[i].start, tryblock[i].end, tryblock[i].catch,
1046 tryblock[i].catchblock_count );
1047 for (j = 0; j < tryblock[i].catchblock_count; j++)
1049 const char *type = "<none>";
1051 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1053 if (catchblock[j].type_info) type = RVA( catchblock[j].type_info + 16, 1 );
1054 printf( " %d: flags %x offset %+d handler %08x frame %x type %s\n", j,
1055 catchblock[j].flags, catchblock[j].offset,
1056 catchblock[j].handler, catchblock[j].frame, type );
1058 else
1060 if (catchblock32[j].type_info) type = RVA( catchblock32[j].type_info + 8, 1 );
1061 printf( " %d: flags %x offset %+d handler %08x type %s\n", j,
1062 catchblock32[j].flags, catchblock32[j].offset,
1063 catchblock32[j].handler, type );
1069 if (func->ipmap_count)
1071 printf( " ip map at %08x count %u\n", func->ipmap, func->ipmap_count );
1072 if ((ipmap = RVA( func->ipmap, func->ipmap_count * sizeof(*ipmap) )))
1074 for (i = 0; i < func->ipmap_count; i++)
1075 printf( " %u: ip %08x state %d\n", i, ipmap[i].ip, ipmap[i].state );
1080 static UINT v4_decode_uint( const BYTE **b )
1082 UINT ret;
1083 const BYTE *p = *b;
1085 if ((*p & 1) == 0)
1087 ret = p[0] >> 1;
1088 p += 1;
1090 else if ((*p & 3) == 1)
1092 ret = (p[0] >> 2) + (p[1] << 6);
1093 p += 2;
1095 else if ((*p & 7) == 3)
1097 ret = (p[0] >> 3) + (p[1] << 5) + (p[2] << 13);
1098 p += 3;
1100 else if ((*p & 15) == 7)
1102 ret = (p[0] >> 4) + (p[1] << 4) + (p[2] << 12) + (p[3] << 20);
1103 p += 4;
1105 else
1107 ret = 0;
1108 p += 5;
1111 *b = p;
1112 return ret;
1115 static UINT v4_read_rva( const BYTE **b )
1117 UINT ret = *(UINT *)*b;
1118 *b += sizeof(UINT);
1119 return ret;
1122 #define FUNC_DESCR_IS_CATCH 0x01
1123 #define FUNC_DESCR_IS_SEPARATED 0x02
1124 #define FUNC_DESCR_BBT 0x04
1125 #define FUNC_DESCR_UNWIND_MAP 0x08
1126 #define FUNC_DESCR_TRYBLOCK_MAP 0x10
1127 #define FUNC_DESCR_EHS 0x20
1128 #define FUNC_DESCR_NO_EXCEPT 0x40
1129 #define FUNC_DESCR_RESERVED 0x80
1131 #define CATCHBLOCK_FLAGS 0x01
1132 #define CATCHBLOCK_TYPE_INFO 0x02
1133 #define CATCHBLOCK_OFFSET 0x04
1134 #define CATCHBLOCK_SEPARATED 0x08
1135 #define CATCHBLOCK_RET_ADDR_MASK 0x30
1136 #define CATCHBLOCK_RET_ADDR 0x10
1137 #define CATCHBLOCK_TWO_RET_ADDRS 0x20
1139 static void dump_cxx_exception_data_v4( unsigned int rva, unsigned int func_rva )
1141 const unsigned int *ptr = RVA( rva, sizeof(*ptr) );
1142 const BYTE *p;
1143 BYTE flags;
1144 UINT unwind_map = 0, tryblock_map = 0, ip_map, count, i, j, k;
1146 if (!ptr || !*ptr) return;
1147 rva = *ptr;
1148 if (!(p = RVA( rva, 1 ))) return;
1149 flags = *p++;
1150 printf( " C++ v4 exception data at %08x", rva );
1151 if (flags & FUNC_DESCR_BBT) printf( " bbt %08x", v4_decode_uint( &p ));
1152 if (flags & FUNC_DESCR_UNWIND_MAP) unwind_map = v4_read_rva( &p );
1153 if (flags & FUNC_DESCR_TRYBLOCK_MAP) tryblock_map = v4_read_rva( &p );
1154 ip_map = v4_read_rva(&p);
1155 if (flags & FUNC_DESCR_IS_CATCH) printf( " frame %08x", v4_decode_uint( &p ));
1156 if (flags & FUNC_DESCR_EHS) printf( " sync" );
1157 if (flags & FUNC_DESCR_NO_EXCEPT) printf( " noexcept" );
1158 printf( "\n" );
1160 if (unwind_map)
1162 int *offsets;
1163 const BYTE *start, *p = RVA( unwind_map, 1 );
1164 count = v4_decode_uint( &p );
1165 printf( " unwind map at %08x count %u\n", unwind_map, count );
1166 offsets = calloc( count, sizeof(*offsets) );
1167 start = p;
1168 for (i = 0; i < count; i++)
1170 UINT handler, object, type;
1171 int offset = p - start, off, prev = -2;
1173 offsets[i] = offset;
1174 type = v4_decode_uint( &p );
1175 off = (type >> 2);
1176 if (off > offset) prev = -1;
1177 else for (j = 0; j < i; j++) if (offsets[j] == offset - off) prev = j;
1179 switch (type & 3)
1181 case 0:
1182 printf( " %u: prev %d no handler\n", i, prev );
1183 break;
1184 case 1:
1185 handler = v4_read_rva( &p );
1186 object = v4_decode_uint( &p );
1187 printf( " %u: prev %d dtor obj handler %08x obj %+d\n",
1188 i, prev, handler, (int)object );
1189 break;
1190 case 2:
1191 handler = v4_read_rva( &p );
1192 object = v4_decode_uint( &p );
1193 printf( " %u: prev %d dtor ptr handler %08x obj %+d\n",
1194 i, prev, handler, (int)object );
1195 break;
1196 case 3:
1197 handler = v4_read_rva( &p );
1198 printf( " %u: prev %d handler %08x\n", i, prev, handler );
1199 break;
1202 free( offsets );
1205 if (tryblock_map)
1207 const BYTE *p = RVA( tryblock_map, 1 );
1208 count = v4_decode_uint( &p );
1209 printf( " tryblock map at %08x count %u\n", tryblock_map, count );
1210 for (i = 0; i < count; i++)
1212 int start = v4_decode_uint( &p );
1213 int end = v4_decode_uint( &p );
1214 int catch = v4_decode_uint( &p );
1215 UINT catchblock = v4_read_rva( &p );
1217 printf( " %u: start %d end %d catch %d\n", i, start, end, catch );
1218 if (catchblock)
1220 UINT cont[3];
1221 const BYTE *p = RVA( catchblock, 1 );
1222 count = v4_decode_uint( &p );
1223 for (j = 0; j < count; j++)
1225 BYTE flags = *p++;
1226 printf( " %u:", j );
1227 if (flags & CATCHBLOCK_FLAGS)
1228 printf( " flags %08x", v4_decode_uint( &p ));
1229 if (flags & CATCHBLOCK_TYPE_INFO)
1230 printf( " type %08x", v4_read_rva( &p ));
1231 if (flags & CATCHBLOCK_OFFSET)
1232 printf( " offset %08x", v4_decode_uint( &p ));
1233 printf( " handler %08x", v4_read_rva( &p ));
1234 for (k = 0; k < ((flags >> 4) & 3); k++)
1235 if (flags & CATCHBLOCK_SEPARATED) cont[k] = v4_read_rva( &p );
1236 else cont[k] = func_rva + v4_decode_uint( &p );
1237 if (k == 1) printf( " cont %08x", cont[0] );
1238 else if (k == 2) printf( " cont %08x,%08x", cont[0], cont[1] );
1239 printf( "\n" );
1245 if (ip_map && (flags & FUNC_DESCR_IS_SEPARATED))
1247 const BYTE *p = RVA( ip_map, 1 );
1249 count = v4_decode_uint( &p );
1250 printf( " separated ip map at %08x count %u\n", ip_map, count );
1251 for (i = 0; i < count; i++)
1253 UINT ip = v4_read_rva( &p );
1254 UINT map = v4_read_rva( &p );
1256 if (map)
1258 UINT state, count2;
1259 const BYTE *p = RVA( map, 1 );
1261 count2 = v4_decode_uint( &p );
1262 printf( " %u: start %08x map %08x\n", i, ip, map );
1263 for (j = 0; j < count2; j++)
1265 ip += v4_decode_uint( &p );
1266 state = v4_decode_uint( &p );
1267 printf( " %u: ip %08x state %d\n", i, ip, state );
1272 else if (ip_map)
1274 UINT ip = func_rva, state;
1275 const BYTE *p = RVA( ip_map, 1 );
1277 count = v4_decode_uint( &p );
1278 printf( " ip map at %08x count %u\n", ip_map, count );
1279 for (i = 0; i < count; i++)
1281 ip += v4_decode_uint( &p );
1282 state = v4_decode_uint( &p );
1283 printf( " %u: ip %08x state %d\n", i, ip, state );
1288 static void dump_exception_data( unsigned int rva, unsigned int func_rva, const char *name )
1290 if (strstr( name, "__C_specific_handler" )) dump_c_exception_data( rva );
1291 if (strstr( name, "__CxxFrameHandler4" )) dump_cxx_exception_data_v4( rva, func_rva );
1292 else dump_cxx_exception_data( rva, func_rva );
1295 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
1297 static const char * const reg_names[16] =
1298 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1299 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
1301 const union handler_data *handler_data;
1302 const struct unwind_info_x86_64 *info;
1303 unsigned int i, count;
1305 printf( "\nFunction %08x-%08x:%s\n", function->BeginAddress, function->EndAddress,
1306 find_export_from_rva( function->BeginAddress ));
1307 if (function->UnwindData & 1)
1309 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
1310 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
1311 return;
1313 info = RVA( function->UnwindData, sizeof(*info) );
1315 if (!info)
1317 printf( " no unwind info (%x)\n", function->UnwindData );
1318 return;
1320 printf( " unwind info at %08x\n", function->UnwindData );
1321 if (info->version > 2)
1323 printf( " *** unknown version %u\n", info->version );
1324 return;
1326 printf( " flags %x", info->flags );
1327 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
1328 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
1329 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
1330 printf( "\n prolog 0x%x bytes\n", info->prolog );
1332 if (info->frame_reg)
1333 printf( " frame register %s offset 0x%x(%%rsp)\n",
1334 reg_names[info->frame_reg], info->frame_offset * 16 );
1336 for (i = 0; i < info->count; i++)
1338 if (info->opcodes[i].code == UWOP_EPILOG)
1340 i++;
1341 continue;
1343 printf( " 0x%02x: ", info->opcodes[i].offset );
1344 switch (info->opcodes[i].code)
1346 case UWOP_PUSH_NONVOL:
1347 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
1348 break;
1349 case UWOP_ALLOC_LARGE:
1350 if (info->opcodes[i].info)
1352 count = *(const UINT *)&info->opcodes[i+1];
1353 i += 2;
1355 else
1357 count = *(const USHORT *)&info->opcodes[i+1] * 8;
1358 i++;
1360 printf( "sub $0x%x,%%rsp\n", count );
1361 break;
1362 case UWOP_ALLOC_SMALL:
1363 count = (info->opcodes[i].info + 1) * 8;
1364 printf( "sub $0x%x,%%rsp\n", count );
1365 break;
1366 case UWOP_SET_FPREG:
1367 printf( "lea 0x%x(%%rsp),%s\n",
1368 info->frame_offset * 16, reg_names[info->frame_reg] );
1369 break;
1370 case UWOP_SAVE_NONVOL:
1371 count = *(const USHORT *)&info->opcodes[i+1] * 8;
1372 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
1373 i++;
1374 break;
1375 case UWOP_SAVE_NONVOL_FAR:
1376 count = *(const UINT *)&info->opcodes[i+1];
1377 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
1378 i += 2;
1379 break;
1380 case UWOP_SAVE_XMM128:
1381 count = *(const USHORT *)&info->opcodes[i+1] * 16;
1382 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
1383 i++;
1384 break;
1385 case UWOP_SAVE_XMM128_FAR:
1386 count = *(const UINT *)&info->opcodes[i+1];
1387 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
1388 i += 2;
1389 break;
1390 case UWOP_PUSH_MACHFRAME:
1391 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
1392 break;
1393 default:
1394 printf( "*** unknown code %u\n", info->opcodes[i].code );
1395 break;
1399 if (info->version == 2 && info->opcodes[0].code == UWOP_EPILOG) /* print the epilogs */
1401 unsigned int end = function->EndAddress;
1402 unsigned int size = info->opcodes[0].offset;
1404 printf( " epilog 0x%x bytes\n", size );
1405 if (info->opcodes[0].info) printf( " at %08x-%08x\n", end - size, end );
1406 for (i = 1; i < info->count && info->opcodes[i].code == UWOP_EPILOG; i++)
1408 unsigned int offset = (info->opcodes[i].info << 8) + info->opcodes[i].offset;
1409 if (!offset) break;
1410 printf( " at %08x-%08x\n", end - offset, end - offset + size );
1414 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
1415 if (info->flags & UNW_FLAG_CHAININFO)
1417 printf( " -> function %08x-%08x\n",
1418 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
1419 return;
1421 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
1423 UINT rva = function->UnwindData + ((const char *)(&handler_data->handler + 1) - (const char *)info);
1424 const char *name = get_function_name( handler_data->handler );
1426 printf( " handler %08x%s data at %08x\n", handler_data->handler, name, rva );
1427 dump_exception_data( rva, function->BeginAddress, name );
1431 static const BYTE armnt_code_lengths[256] =
1433 /* 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,
1434 /* 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,
1435 /* 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,
1436 /* 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,
1437 /* 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,
1438 /* 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,
1439 /* 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,
1440 /* 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
1443 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
1445 const struct unwind_info_armnt *info;
1446 const struct unwind_info_ext_armnt *infoex;
1447 const struct unwind_info_epilogue_armnt *infoepi = NULL;
1448 unsigned int rva;
1449 WORD i, count = 0, words = 0;
1451 if (fnc->Flag)
1453 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
1454 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
1455 const char *pfx = " ... ";
1457 printf( "\nFunction %08x-%08x: flag=%u ret=%u H=%u reg=%u R=%u L=%u C=%u%s\n",
1458 fnc->BeginAddress & ~1, (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2,
1459 fnc->Flag, fnc->Ret, fnc->H, fnc->Reg, fnc->R, fnc->L, fnc->C,
1460 find_export_from_rva( fnc->BeginAddress ));
1462 if (fnc->StackAdjust >= 0x03f4)
1464 pf = fnc->StackAdjust & 0x04;
1465 ef = fnc->StackAdjust & 0x08;
1466 stack = (fnc->StackAdjust & 3) + 1;
1469 if (!fnc->R || pf)
1471 int first = 4, last = fnc->Reg + 4;
1472 if (pf)
1474 first = (~fnc->StackAdjust) & 3;
1475 if (fnc->R)
1476 last = 3;
1478 if (first == last)
1479 sprintf(intregs, "r%u", first);
1480 else
1481 sprintf(intregs, "r%u-r%u", first, last);
1482 fpoffset = last + 1 - first;
1485 if (!fnc->R || ef)
1487 int first = 4, last = fnc->Reg + 4;
1488 if (ef)
1490 first = (~fnc->StackAdjust) & 3;
1491 if (fnc->R)
1492 last = 3;
1494 if (first == last)
1495 sprintf(intregspop, "r%u", first);
1496 else
1497 sprintf(intregspop, "r%u-r%u", first, last);
1500 if (fnc->C)
1502 if (intregs[0])
1503 strcat(intregs, ", ");
1504 if (intregspop[0])
1505 strcat(intregspop, ", ");
1506 strcat(intregs, "r11");
1507 strcat(intregspop, "r11");
1509 if (fnc->L)
1511 if (intregs[0])
1512 strcat(intregs, ", ");
1513 strcat(intregs, "lr");
1515 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
1516 strcat(intregspop, ", ");
1517 if (fnc->Ret != 0)
1518 strcat(intregspop, "lr");
1519 else if (!fnc->H)
1520 strcat(intregspop, "pc");
1523 if (fnc->R)
1525 if (fnc->Reg)
1526 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
1527 else
1528 strcpy(vfpregs, "d8");
1531 printf( " Prologue:\n" );
1532 if (fnc->Flag == 1) {
1533 if (fnc->H)
1534 printf( "%s push {r0-r3}\n", pfx );
1536 if (intregs[0])
1537 printf( "%s push {%s}\n", pfx, intregs );
1539 if (fnc->C && fpoffset == 0)
1540 printf( "%s mov r11, sp\n", pfx );
1541 else if (fnc->C)
1542 printf( "%s add r11, sp, #%d\n", pfx, fpoffset * 4 );
1544 if (fnc->R && fnc->Reg != 0x07)
1545 printf( "%s vpush {%s}\n", pfx, vfpregs );
1547 if (stack && !pf)
1548 printf( "%s sub sp, sp, #%d\n", pfx, stack * 4 );
1551 if (fnc->Ret == 3)
1552 return;
1553 printf( " Epilogue:\n" );
1555 if (stack && !ef)
1556 printf( "%s add sp, sp, #%d\n", pfx, stack * 4 );
1558 if (fnc->R && fnc->Reg != 0x07)
1559 printf( "%s vpop {%s}\n", pfx, vfpregs );
1561 if (intregspop[0])
1562 printf( "%s pop {%s}\n", pfx, intregspop );
1564 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1565 printf( "%s add sp, sp, #16\n", pfx );
1566 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1567 printf( "%s ldr pc, [sp], #20\n", pfx );
1569 if (fnc->Ret == 1)
1570 printf( "%s bx <reg>\n", pfx );
1571 else if (fnc->Ret == 2)
1572 printf( "%s b <address>\n", pfx );
1574 return;
1577 info = RVA( fnc->UnwindData, sizeof(*info) );
1578 rva = fnc->UnwindData + sizeof(*info);
1579 count = info->count;
1580 words = info->words;
1582 printf( "\nFunction %08x-%08x: ver=%u X=%u E=%u F=%u%s\n", fnc->BeginAddress & ~1,
1583 (fnc->BeginAddress & ~1) + info->function_length * 2,
1584 info->version, info->x, info->e, info->f, find_export_from_rva( fnc->BeginAddress | 1 ));
1586 if (!info->count && !info->words)
1588 infoex = RVA( rva, sizeof(*infoex) );
1589 rva = rva + sizeof(*infoex);
1590 count = infoex->excount;
1591 words = infoex->exwords;
1594 if (!info->e)
1596 infoepi = RVA( rva, count * sizeof(*infoepi) );
1597 rva = rva + count * sizeof(*infoepi);
1600 if (words)
1602 const unsigned int *codes;
1603 BYTE b, *bytes;
1604 BOOL inepilogue = FALSE;
1606 codes = RVA( rva, words * sizeof(*codes) );
1607 rva = rva + words * sizeof(*codes);
1608 bytes = (BYTE*)codes;
1610 printf( " Prologue:\n" );
1611 for (b = 0; b < words * sizeof(*codes); b++)
1613 BYTE code = bytes[b];
1614 BYTE len = armnt_code_lengths[code];
1616 if (info->e && b == count)
1618 printf( " Epilogue:\n" );
1619 inepilogue = TRUE;
1621 else if (!info->e && infoepi)
1623 for (i = 0; i < count; i++)
1624 if (b == infoepi[i].index)
1626 printf( " Epilogue %u at %08x: (res=%x cond=%x)\n", i,
1627 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2,
1628 infoepi[i].res, infoepi[i].cond );
1629 inepilogue = TRUE;
1633 printf( " ");
1634 for (i = 0; i < len; i++)
1635 printf( " %02x", bytes[b+i] );
1636 printf( " %*s", 3 * (3 - len), "" );
1638 if (code == 0x00)
1639 printf( "\n" );
1640 else if (code <= 0x7f)
1641 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1642 else if (code <= 0xbf)
1644 WORD excode, f;
1645 BOOL first = TRUE;
1646 BYTE excodes = bytes[++b];
1648 excode = (code << 8) | excodes;
1649 printf( "%s {", inepilogue ? "pop" : "push" );
1651 for (f = 0; f <= 12; f++)
1653 if ((excode >> f) & 1)
1655 printf( "%sr%u", first ? "" : ", ", f );
1656 first = FALSE;
1660 if (excode & 0x2000)
1661 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1663 printf( "}\n" );
1665 else if (code <= 0xcf)
1666 if (inepilogue)
1667 printf( "mov sp, r%u\n", code & 0x0f );
1668 else
1669 printf( "mov r%u, sp\n", code & 0x0f );
1670 else if (code <= 0xd3)
1671 printf( "%s {r4-r%u}\n", inepilogue ? "pop" : "push", (code & 0x03) + 4 );
1672 else if (code <= 0xd4)
1673 printf( "%s {r4, %s}\n", inepilogue ? "pop" : "push", inepilogue ? "pc" : "lr" );
1674 else if (code <= 0xd7)
1675 printf( "%s {r4-r%u, %s}\n", inepilogue ? "pop" : "push", (code & 0x03) + 4, inepilogue ? "pc" : "lr" );
1676 else if (code <= 0xdb)
1677 printf( "%s {r4-r%u}\n", inepilogue ? "pop" : "push", (code & 0x03) + 8 );
1678 else if (code <= 0xdf)
1679 printf( "%s {r4-r%u, %s}\n", inepilogue ? "pop" : "push", (code & 0x03) + 8, inepilogue ? "pc" : "lr" );
1680 else if (code <= 0xe7)
1681 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1682 else if (code <= 0xeb)
1684 WORD excode;
1685 BYTE excodes = bytes[++b];
1687 excode = (code << 8) | excodes;
1688 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1690 else if (code <= 0xed)
1692 WORD excode, f;
1693 BOOL first = TRUE;
1694 BYTE excodes = bytes[++b];
1696 excode = (code << 8) | excodes;
1697 printf( "%s {", inepilogue ? "pop" : "push" );
1699 for (f = 0; f < 8; f++)
1701 if ((excode >> f) & 1)
1703 printf( "%sr%u", first ? "" : ", ", f );
1704 first = FALSE;
1708 if (excode & 0x0100)
1709 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1711 printf( "}\n" );
1713 else if (code == 0xee)
1715 BYTE excodes = bytes[++b];
1716 if (excodes == 0x01)
1717 printf( "MSFT_OP_MACHINE_FRAME\n");
1718 else if (excodes == 0x02)
1719 printf( "MSFT_OP_CONTEXT\n");
1720 else
1721 printf( "MSFT opcode %u\n", excodes );
1723 else if (code == 0xef)
1725 WORD excode;
1726 BYTE excodes = bytes[++b];
1728 if (excodes <= 0x0f)
1730 excode = (code << 8) | excodes;
1731 if (inepilogue)
1732 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1733 else
1734 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1736 else
1737 printf( "unknown 32\n" );
1739 else if (code <= 0xf4)
1740 printf( "unknown\n" );
1741 else if (code <= 0xf6)
1743 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1744 BYTE excodes = bytes[++b];
1746 excode = (code << 8) | excodes;
1747 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1748 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1750 else if (code <= 0xf7)
1752 unsigned int excode;
1753 BYTE excodes[2];
1755 excodes[0] = bytes[++b];
1756 excodes[1] = bytes[++b];
1757 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1758 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1760 else if (code <= 0xf8)
1762 unsigned int excode;
1763 BYTE excodes[3];
1765 excodes[0] = bytes[++b];
1766 excodes[1] = bytes[++b];
1767 excodes[2] = bytes[++b];
1768 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1769 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1771 else if (code <= 0xf9)
1773 unsigned int excode;
1774 BYTE excodes[2];
1776 excodes[0] = bytes[++b];
1777 excodes[1] = bytes[++b];
1778 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1779 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1781 else if (code <= 0xfa)
1783 unsigned int excode;
1784 BYTE excodes[3];
1786 excodes[0] = bytes[++b];
1787 excodes[1] = bytes[++b];
1788 excodes[2] = bytes[++b];
1789 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1790 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1792 else if (code <= 0xfb)
1793 printf( "nop\n" );
1794 else if (code <= 0xfc)
1795 printf( "nop.w\n" );
1796 else if (code <= 0xfd)
1798 printf( "(end) nop\n" );
1799 inepilogue = TRUE;
1801 else if (code <= 0xfe)
1803 printf( "(end) nop.w\n" );
1804 inepilogue = TRUE;
1806 else
1808 printf( "end\n" );
1809 inepilogue = TRUE;
1814 if (info->x)
1816 const unsigned int *handler;
1817 const char *name;
1819 handler = RVA( rva, sizeof(*handler) );
1820 rva = rva + sizeof(*handler);
1821 name = get_function_name( *handler );
1822 printf( " handler %08x%s data at %08x\n", *handler, name, rva );
1823 dump_exception_data( rva, fnc->BeginAddress & ~1, name );
1827 struct unwind_info_arm64
1829 UINT function_length : 18;
1830 UINT version : 2;
1831 UINT x : 1;
1832 UINT e : 1;
1833 UINT epilog : 5;
1834 UINT codes : 5;
1837 struct unwind_info_ext_arm64
1839 WORD epilog;
1840 BYTE codes;
1841 BYTE reserved;
1844 struct unwind_info_epilog_arm64
1846 UINT offset : 18;
1847 UINT res : 4;
1848 UINT index : 10;
1851 static const BYTE code_lengths[256] =
1853 /* 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,
1854 /* 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,
1855 /* 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,
1856 /* 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,
1857 /* 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,
1858 /* 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,
1859 /* 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,
1860 /* e0 */ 4,1,2,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1863 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1865 unsigned int i, j;
1867 for (i = 0; i < count; i += code_lengths[ptr[i]])
1869 BYTE len = code_lengths[ptr[i]];
1870 unsigned int val = ptr[i];
1871 for (j = 1; j < len; j++) val = val * 0x100 + ptr[i + j];
1873 printf( " %04x: ", i );
1874 for (j = 0; j < 4; j++)
1875 if (j < len) printf( "%02x ", ptr[i+j] );
1876 else printf( " " );
1878 if (ptr[i] < 0x20) /* alloc_s */
1880 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1882 else if (ptr[i] < 0x40) /* save_r19r20_x */
1884 printf( "stp x19,x20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1886 else if (ptr[i] < 0x80) /* save_fplr */
1888 printf( "stp x29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1890 else if (ptr[i] < 0xc0) /* save_fplr_x */
1892 printf( "stp x29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1894 else if (ptr[i] < 0xc8) /* alloc_m */
1896 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1898 else if (ptr[i] < 0xcc) /* save_regp */
1900 int reg = 19 + ((val >> 6) & 0xf);
1901 printf( "stp x%u,x%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1903 else if (ptr[i] < 0xd0) /* save_regp_x */
1905 int reg = 19 + ((val >> 6) & 0xf);
1906 printf( "stp x%u,x%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1908 else if (ptr[i] < 0xd4) /* save_reg */
1910 int reg = 19 + ((val >> 6) & 0xf);
1911 printf( "str x%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1913 else if (ptr[i] < 0xd6) /* save_reg_x */
1915 int reg = 19 + ((val >> 5) & 0xf);
1916 printf( "str x%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1918 else if (ptr[i] < 0xd8) /* save_lrpair */
1920 int reg = 19 + 2 * ((val >> 6) & 0x7);
1921 printf( "stp x%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1923 else if (ptr[i] < 0xda) /* save_fregp */
1925 int reg = 8 + ((val >> 6) & 0x7);
1926 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1928 else if (ptr[i] < 0xdc) /* save_fregp_x */
1930 int reg = 8 + ((val >> 6) & 0x7);
1931 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1933 else if (ptr[i] < 0xde) /* save_freg */
1935 int reg = 8 + ((val >> 6) & 0x7);
1936 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1938 else if (ptr[i] == 0xde) /* save_freg_x */
1940 int reg = 8 + ((val >> 5) & 0x7);
1941 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1943 else if (ptr[i] == 0xe0) /* alloc_l */
1945 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1947 else if (ptr[i] == 0xe1) /* set_fp */
1949 printf( "mov x29,sp\n" );
1951 else if (ptr[i] == 0xe2) /* add_fp */
1953 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1955 else if (ptr[i] == 0xe3) /* nop */
1957 printf( "nop\n" );
1959 else if (ptr[i] == 0xe4) /* end */
1961 printf( "end\n" );
1963 else if (ptr[i] == 0xe5) /* end_c */
1965 printf( "end_c\n" );
1967 else if (ptr[i] == 0xe6) /* save_next */
1969 printf( "save_next\n" );
1971 else if (ptr[i] == 0xe7) /* save_any_reg */
1973 char reg = "xdq?"[(val >> 6) & 3];
1974 int num = (val >> 8) & 0x1f;
1975 if (val & 0x4000)
1976 printf( "stp %c%u,%c%u", reg, num, reg, num + 1 );
1977 else
1978 printf( "str %c%u,", reg, num );
1979 if (val & 0x2000)
1980 printf( "[sp,#-%#x]!\n", 16 * (val & 0x3f) + 16 );
1981 else
1982 printf( "[sp,#%#x]\n", (val & 0x3f) * ((val & 0x4080) ? 16 : 8) );
1984 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1986 printf( "MSFT_OP_TRAP_FRAME\n" );
1988 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1990 printf( "MSFT_OP_MACHINE_FRAME\n" );
1992 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1994 printf( "MSFT_OP_CONTEXT\n" );
1996 else if (ptr[i] == 0xeb) /* MSFT_OP_EC_CONTEXT */
1998 printf( "MSFT_OP_EC_CONTEXT\n" );
2000 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
2002 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
2004 else if (ptr[i] == 0xfc) /* pac_sign_lr */
2006 printf( "pac_sign_lr\n" );
2008 else printf( "??\n");
2012 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
2014 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
2016 if (func->CR == 1) intsz += 8;
2017 if (func->RegF) fpsz += 8;
2019 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
2020 locsz = func->FrameSize * 16 - savesz;
2022 switch (func->CR)
2024 case 3:
2025 printf( " %04x: mov x29,sp\n", pos++ );
2026 if (locsz <= 512)
2028 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
2029 break;
2031 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
2032 /* fall through */
2033 case 0:
2034 case 1:
2035 if (locsz <= 4080)
2037 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
2039 else
2041 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
2042 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
2044 break;
2047 if (func->H)
2049 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
2050 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
2051 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
2052 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
2055 if (func->RegF)
2057 if (func->RegF % 2 == 0)
2058 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
2059 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
2061 if (!i && !intsz)
2062 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
2063 else
2064 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
2068 switch (func->RegI)
2070 case 0:
2071 if (func->CR == 1)
2072 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
2073 break;
2074 case 1:
2075 if (func->CR == 1)
2076 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
2077 else
2078 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
2079 break;
2080 default:
2081 if (func->RegI % 2)
2083 if (func->CR == 1)
2084 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
2085 else
2086 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
2088 else if (func->CR == 1)
2089 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
2091 for (i = func->RegI / 2 - 1; i >= 0; i--)
2092 if (i)
2093 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
2094 else
2095 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
2096 break;
2098 printf( " %04x: end\n", pos );
2101 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
2103 const struct unwind_info_arm64 *info;
2104 const struct unwind_info_ext_arm64 *infoex;
2105 const struct unwind_info_epilog_arm64 *infoepi;
2106 const struct runtime_function_arm64 *parent_func;
2107 const BYTE *ptr;
2108 unsigned int i, rva, codes, epilogs;
2110 switch (func->Flag)
2112 case 1:
2113 case 2:
2114 printf( "\nFunction %08x-%08x:%s\n", func->BeginAddress,
2115 func->BeginAddress + func->FunctionLength * 4, find_export_from_rva( func->BeginAddress ));
2116 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
2117 func->FunctionLength, func->Flag, func->RegF, func->RegI,
2118 func->H, func->CR, func->FrameSize );
2119 dump_arm64_packed_info( func );
2120 return;
2121 case 3:
2122 rva = func->UnwindData & ~3;
2123 parent_func = RVA( rva, sizeof(*parent_func) );
2124 printf( "\nFunction %08x-%08x:%s\n", func->BeginAddress,
2125 func->BeginAddress + 12 /* adrl x16, <dest>; br x16 */,
2126 find_export_from_rva( func->BeginAddress ));
2127 printf( " forward to parent %08x\n", parent_func->BeginAddress );
2128 return;
2131 rva = func->UnwindData;
2132 info = RVA( rva, sizeof(*info) );
2133 rva += sizeof(*info);
2134 epilogs = info->epilog;
2135 codes = info->codes;
2137 if (!codes)
2139 infoex = RVA( rva, sizeof(*infoex) );
2140 rva = rva + sizeof(*infoex);
2141 codes = infoex->codes;
2142 epilogs = infoex->epilog;
2144 printf( "\nFunction %08x-%08x:\n",
2145 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
2146 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
2147 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
2148 if (info->e)
2150 printf( " epilog 0: code=%04x\n", info->epilog );
2152 else
2154 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
2155 rva += sizeof(*infoepi) * epilogs;
2156 for (i = 0; i < epilogs; i++)
2157 printf( " epilog %u: pc=%08x code=%04x\n", i,
2158 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
2160 ptr = RVA( rva, codes * 4);
2161 rva += codes * 4;
2162 dump_arm64_codes( ptr, codes * 4 );
2163 if (info->x)
2165 const UINT *handler = RVA( rva, sizeof(*handler) );
2166 const char *name = get_function_name( *handler );
2168 rva += sizeof(*handler);
2169 printf( " handler: %08x%s data %08x\n", *handler, name, rva );
2170 dump_exception_data( rva, func->BeginAddress, name );
2174 static void dump_dir_exceptions(void)
2176 static const void *arm64_funcs;
2177 unsigned int i, size;
2178 const void *funcs;
2179 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
2180 const IMAGE_ARM64EC_METADATA *metadata;
2182 funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
2184 switch (file_header->Machine)
2186 case IMAGE_FILE_MACHINE_AMD64:
2187 size /= sizeof(struct runtime_function_x86_64);
2188 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
2189 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
2190 if (!(metadata = get_hybrid_metadata())) break;
2191 if (!(size = metadata->ExtraRFETableSize)) break;
2192 if (!(funcs = RVA( metadata->ExtraRFETable, size ))) break;
2193 if (funcs == arm64_funcs) break; /* already dumped */
2194 printf( "\n" );
2195 /* fall through */
2196 case IMAGE_FILE_MACHINE_ARM64:
2197 arm64_funcs = funcs;
2198 size /= sizeof(struct runtime_function_arm64);
2199 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
2200 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
2201 break;
2202 case IMAGE_FILE_MACHINE_ARMNT:
2203 size /= sizeof(struct runtime_function_armnt);
2204 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
2205 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
2206 break;
2207 default:
2208 printf( "Exception information not supported for %s binaries\n",
2209 get_machine_str(file_header->Machine));
2210 break;
2212 printf( "\n" );
2216 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
2218 /* FIXME: This does not properly handle large images */
2219 const IMAGE_IMPORT_BY_NAME* iibn;
2220 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
2222 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
2223 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
2224 else
2226 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
2227 if (!iibn)
2228 printf("Can't grab import by name info, skipping to next ordinal\n");
2229 else
2230 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
2235 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
2237 const IMAGE_IMPORT_BY_NAME* iibn;
2238 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
2240 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
2241 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
2242 else
2244 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
2245 if (!iibn)
2246 printf("Can't grab import by name info, skipping to next ordinal\n");
2247 else
2248 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
2253 static void dump_dir_imported_functions(void)
2255 unsigned directorySize;
2256 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
2258 if (!importDesc) return;
2260 printf("Import Table size: %08x\n", directorySize);/* FIXME */
2262 for (;;)
2264 const IMAGE_THUNK_DATA32* il;
2266 if (!importDesc->Name || !importDesc->FirstThunk) break;
2268 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
2269 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
2270 printf(" TimeDateStamp: %08X (%s)\n",
2271 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2272 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
2273 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
2275 printf(" Thunk Ordn Name\n");
2277 il = (importDesc->OriginalFirstThunk != 0) ?
2278 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
2279 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
2281 if (!il)
2282 printf("Can't grab thunk data, going to next imported DLL\n");
2283 else
2285 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2286 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
2287 else
2288 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
2289 printf("\n");
2291 importDesc++;
2293 printf("\n");
2296 static void dump_hybrid_metadata(void)
2298 unsigned int i;
2299 const void *metadata = get_hybrid_metadata();
2301 if (!metadata) return;
2302 printf( "Hybrid metadata\n" );
2304 switch (PE_nt_headers->FileHeader.Machine)
2306 case IMAGE_FILE_MACHINE_I386:
2308 const IMAGE_CHPE_METADATA_X86 *data = metadata;
2310 printf( " Version %#x\n", (int)data->Version );
2311 printf( " CHPECodeAddressRangeOffset %#x\n", (int)data->CHPECodeAddressRangeOffset );
2312 printf( " CHPECodeAddressRangeCount %#x\n", (int)data->CHPECodeAddressRangeCount );
2313 printf( " WowA64ExceptionHandlerFunctionPointer %#x\n", (int)data->WowA64ExceptionHandlerFunctionPointer );
2314 printf( " WowA64DispatchCallFunctionPointer %#x\n", (int)data->WowA64DispatchCallFunctionPointer );
2315 printf( " WowA64DispatchIndirectCallFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallFunctionPointer );
2316 printf( " WowA64DispatchIndirectCallCfgFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallCfgFunctionPointer );
2317 printf( " WowA64DispatchRetFunctionPointer %#x\n", (int)data->WowA64DispatchRetFunctionPointer );
2318 printf( " WowA64DispatchRetLeafFunctionPointer %#x\n", (int)data->WowA64DispatchRetLeafFunctionPointer );
2319 printf( " WowA64DispatchJumpFunctionPointer %#x\n", (int)data->WowA64DispatchJumpFunctionPointer );
2320 if (data->Version >= 2)
2321 printf( " CompilerIATPointer %#x\n", (int)data->CompilerIATPointer );
2322 if (data->Version >= 3)
2323 printf( " WowA64RdtscFunctionPointer %#x\n", (int)data->WowA64RdtscFunctionPointer );
2324 if (data->Version >= 4)
2326 printf( " unknown[0] %#x\n", (int)data->unknown[0] );
2327 printf( " unknown[1] %#x\n", (int)data->unknown[1] );
2328 printf( " unknown[2] %#x\n", (int)data->unknown[2] );
2329 printf( " unknown[3] %#x\n", (int)data->unknown[3] );
2332 if (data->CHPECodeAddressRangeOffset)
2334 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CHPECodeAddressRangeOffset,
2335 data->CHPECodeAddressRangeCount * sizeof(*map) );
2337 printf( "\nCode ranges\n" );
2338 for (i = 0; i < data->CHPECodeAddressRangeCount; i++)
2340 static const char *types[] = { "x86", "ARM64" };
2341 unsigned int start = map[i].StartOffset & ~1;
2342 unsigned int type = map[i].StartOffset & 1;
2343 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
2347 break;
2350 case IMAGE_FILE_MACHINE_AMD64:
2351 case IMAGE_FILE_MACHINE_ARM64:
2353 const IMAGE_ARM64EC_METADATA *data = metadata;
2355 printf( " Version %#x\n", (int)data->Version );
2356 printf( " CodeMap %#x\n", (int)data->CodeMap );
2357 printf( " CodeMapCount %#x\n", (int)data->CodeMapCount );
2358 printf( " CodeRangesToEntryPoints %#x\n", (int)data->CodeRangesToEntryPoints );
2359 printf( " RedirectionMetadata %#x\n", (int)data->RedirectionMetadata );
2360 printf( " __os_arm64x_dispatch_call_no_redirect %#x\n", (int)data->__os_arm64x_dispatch_call_no_redirect );
2361 printf( " __os_arm64x_dispatch_ret %#x\n", (int)data->__os_arm64x_dispatch_ret );
2362 printf( " __os_arm64x_dispatch_call %#x\n", (int)data->__os_arm64x_dispatch_call );
2363 printf( " __os_arm64x_dispatch_icall %#x\n", (int)data->__os_arm64x_dispatch_icall );
2364 printf( " __os_arm64x_dispatch_icall_cfg %#x\n", (int)data->__os_arm64x_dispatch_icall_cfg );
2365 printf( " AlternateEntryPoint %#x\n", (int)data->AlternateEntryPoint );
2366 printf( " AuxiliaryIAT %#x\n", (int)data->AuxiliaryIAT );
2367 printf( " CodeRangesToEntryPointsCount %#x\n", (int)data->CodeRangesToEntryPointsCount );
2368 printf( " RedirectionMetadataCount %#x\n", (int)data->RedirectionMetadataCount );
2369 printf( " GetX64InformationFunctionPointer %#x\n", (int)data->GetX64InformationFunctionPointer );
2370 printf( " SetX64InformationFunctionPointer %#x\n", (int)data->SetX64InformationFunctionPointer );
2371 printf( " ExtraRFETable %#x\n", (int)data->ExtraRFETable );
2372 printf( " ExtraRFETableSize %#x\n", (int)data->ExtraRFETableSize );
2373 printf( " __os_arm64x_dispatch_fptr %#x\n", (int)data->__os_arm64x_dispatch_fptr );
2374 printf( " AuxiliaryIATCopy %#x\n", (int)data->AuxiliaryIATCopy );
2375 printf( " __os_arm64x_helper0 %#x\n", (int)data->__os_arm64x_helper0 );
2376 printf( " __os_arm64x_helper1 %#x\n", (int)data->__os_arm64x_helper1 );
2377 printf( " __os_arm64x_helper2 %#x\n", (int)data->__os_arm64x_helper2 );
2378 printf( " __os_arm64x_helper3 %#x\n", (int)data->__os_arm64x_helper3 );
2379 printf( " __os_arm64x_helper4 %#x\n", (int)data->__os_arm64x_helper4 );
2380 printf( " __os_arm64x_helper5 %#x\n", (int)data->__os_arm64x_helper5 );
2381 printf( " __os_arm64x_helper6 %#x\n", (int)data->__os_arm64x_helper6 );
2382 printf( " __os_arm64x_helper7 %#x\n", (int)data->__os_arm64x_helper7 );
2383 printf( " __os_arm64x_helper8 %#x\n", (int)data->__os_arm64x_helper8 );
2385 if (data->CodeMap)
2387 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CodeMap, data->CodeMapCount * sizeof(*map) );
2389 printf( "\nCode ranges\n" );
2390 for (i = 0; i < data->CodeMapCount; i++)
2392 static const char *types[] = { "ARM64", "ARM64EC", "x64", "??" };
2393 unsigned int start = map[i].StartOffset & ~0x3;
2394 unsigned int type = map[i].StartOffset & 0x3;
2395 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
2399 if (PE_nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_ARM64) break;
2401 if (data->CodeRangesToEntryPoints)
2403 const IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT *map = RVA( data->CodeRangesToEntryPoints,
2404 data->CodeRangesToEntryPointsCount * sizeof(*map) );
2406 printf( "\nCode ranges to entry points\n" );
2407 printf( " Start - End Entry point\n" );
2408 for (i = 0; i < data->CodeRangesToEntryPointsCount; i++)
2410 const char *name = find_export_from_rva( map[i].EntryPoint );
2411 printf( " %08x - %08x %08x%s\n",
2412 (int)map[i].StartRva, (int)map[i].EndRva, (int)map[i].EntryPoint, name );
2416 if (data->RedirectionMetadata)
2418 const IMAGE_ARM64EC_REDIRECTION_ENTRY *map = RVA( data->RedirectionMetadata,
2419 data->RedirectionMetadataCount * sizeof(*map) );
2421 printf( "\nEntry point redirection\n" );
2422 for (i = 0; i < data->RedirectionMetadataCount; i++)
2424 const char *name = find_export_from_rva( map[i].Source );
2425 printf( " %08x -> %08x%s\n", (int)map[i].Source, (int)map[i].Destination, name );
2428 break;
2431 printf( "\n" );
2434 static void dump_dir_loadconfig(void)
2436 unsigned int size;
2437 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32;
2439 loadcfg32 = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2440 if (!loadcfg32) return;
2441 size = min( size, loadcfg32->Size );
2443 printf( "Loadconfig\n" );
2444 print_dword( "Size", loadcfg32->Size );
2445 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
2446 print_word( "MajorVersion", loadcfg32->MajorVersion );
2447 print_word( "MinorVersion", loadcfg32->MinorVersion );
2448 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
2449 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
2450 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
2452 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2454 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void *)loadcfg32;
2456 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
2457 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
2458 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
2459 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
2460 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
2461 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
2462 print_word( "CSDVersion", loadcfg64->CSDVersion );
2463 print_word( "DependentLoadFlags", loadcfg64->DependentLoadFlags );
2464 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
2465 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, SEHandlerTable )) goto done;
2466 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
2467 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
2468 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardCFCheckFunctionPointer )) goto done;
2469 print_longlong( "GuardCFCheckFunctionPointer", loadcfg64->GuardCFCheckFunctionPointer );
2470 print_longlong( "GuardCFDispatchFunctionPointer", loadcfg64->GuardCFDispatchFunctionPointer );
2471 print_longlong( "GuardCFFunctionTable", loadcfg64->GuardCFFunctionTable );
2472 print_longlong( "GuardCFFunctionCount", loadcfg64->GuardCFFunctionCount );
2473 print_dword( "GuardFlags", loadcfg64->GuardFlags );
2474 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CodeIntegrity )) goto done;
2475 print_word( "CodeIntegrity.Flags", loadcfg64->CodeIntegrity.Flags );
2476 print_word( "CodeIntegrity.Catalog", loadcfg64->CodeIntegrity.Catalog );
2477 print_dword( "CodeIntegrity.CatalogOffset", loadcfg64->CodeIntegrity.CatalogOffset );
2478 print_dword( "CodeIntegrity.Reserved", loadcfg64->CodeIntegrity.Reserved );
2479 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardAddressTakenIatEntryTable )) goto done;
2480 print_longlong( "GuardAddressTakenIatEntryTable", loadcfg64->GuardAddressTakenIatEntryTable );
2481 print_longlong( "GuardAddressTakenIatEntryCount", loadcfg64->GuardAddressTakenIatEntryCount );
2482 print_longlong( "GuardLongJumpTargetTable", loadcfg64->GuardLongJumpTargetTable );
2483 print_longlong( "GuardLongJumpTargetCount", loadcfg64->GuardLongJumpTargetCount );
2484 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTable )) goto done;
2485 print_longlong( "DynamicValueRelocTable", loadcfg64->DynamicValueRelocTable );
2486 print_longlong( "CHPEMetadataPointer", loadcfg64->CHPEMetadataPointer );
2487 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFFailureRoutine )) goto done;
2488 print_longlong( "GuardRFFailureRoutine", loadcfg64->GuardRFFailureRoutine );
2489 print_longlong( "GuardRFFailureRoutineFuncPtr", loadcfg64->GuardRFFailureRoutineFunctionPointer );
2490 print_dword( "DynamicValueRelocTableOffset", loadcfg64->DynamicValueRelocTableOffset );
2491 print_word( "DynamicValueRelocTableSection",loadcfg64->DynamicValueRelocTableSection );
2492 print_word( "Reserved2", loadcfg64->Reserved2 );
2493 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2494 print_longlong( "GuardRFVerifyStackPointerFuncPtr", loadcfg64->GuardRFVerifyStackPointerFunctionPointer );
2495 print_dword( "HotPatchTableOffset", loadcfg64->HotPatchTableOffset );
2496 print_dword( "Reserved3", loadcfg64->Reserved3 );
2497 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, EnclaveConfigurationPointer )) goto done;
2498 print_longlong( "EnclaveConfigurationPointer", loadcfg64->EnclaveConfigurationPointer );
2499 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, VolatileMetadataPointer )) goto done;
2500 print_longlong( "VolatileMetadataPointer", loadcfg64->VolatileMetadataPointer );
2501 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardEHContinuationTable )) goto done;
2502 print_longlong( "GuardEHContinuationTable", loadcfg64->GuardEHContinuationTable );
2503 print_longlong( "GuardEHContinuationCount", loadcfg64->GuardEHContinuationCount );
2504 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardXFGCheckFunctionPointer )) goto done;
2505 print_longlong( "GuardXFGCheckFunctionPointer", loadcfg64->GuardXFGCheckFunctionPointer );
2506 print_longlong( "GuardXFGDispatchFunctionPointer", loadcfg64->GuardXFGDispatchFunctionPointer );
2507 print_longlong( "GuardXFGTableDispatchFuncPtr", loadcfg64->GuardXFGTableDispatchFunctionPointer );
2508 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CastGuardOsDeterminedFailureMode )) goto done;
2509 print_longlong( "CastGuardOsDeterminedFailureMode", loadcfg64->CastGuardOsDeterminedFailureMode );
2510 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardMemcpyFunctionPointer )) goto done;
2511 print_longlong( "GuardMemcpyFunctionPointer", loadcfg64->GuardMemcpyFunctionPointer );
2513 else
2515 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
2516 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
2517 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
2518 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
2519 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
2520 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
2521 print_word( "CSDVersion", loadcfg32->CSDVersion );
2522 print_word( "DependentLoadFlags", loadcfg32->DependentLoadFlags );
2523 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
2524 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, SEHandlerTable )) goto done;
2525 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
2526 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
2527 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardCFCheckFunctionPointer )) goto done;
2528 print_dword( "GuardCFCheckFunctionPointer", loadcfg32->GuardCFCheckFunctionPointer );
2529 print_dword( "GuardCFDispatchFunctionPointer", loadcfg32->GuardCFDispatchFunctionPointer );
2530 print_dword( "GuardCFFunctionTable", loadcfg32->GuardCFFunctionTable );
2531 print_dword( "GuardCFFunctionCount", loadcfg32->GuardCFFunctionCount );
2532 print_dword( "GuardFlags", loadcfg32->GuardFlags );
2533 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CodeIntegrity )) goto done;
2534 print_word( "CodeIntegrity.Flags", loadcfg32->CodeIntegrity.Flags );
2535 print_word( "CodeIntegrity.Catalog", loadcfg32->CodeIntegrity.Catalog );
2536 print_dword( "CodeIntegrity.CatalogOffset", loadcfg32->CodeIntegrity.CatalogOffset );
2537 print_dword( "CodeIntegrity.Reserved", loadcfg32->CodeIntegrity.Reserved );
2538 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardAddressTakenIatEntryTable )) goto done;
2539 print_dword( "GuardAddressTakenIatEntryTable", loadcfg32->GuardAddressTakenIatEntryTable );
2540 print_dword( "GuardAddressTakenIatEntryCount", loadcfg32->GuardAddressTakenIatEntryCount );
2541 print_dword( "GuardLongJumpTargetTable", loadcfg32->GuardLongJumpTargetTable );
2542 print_dword( "GuardLongJumpTargetCount", loadcfg32->GuardLongJumpTargetCount );
2543 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTable )) goto done;
2544 print_dword( "DynamicValueRelocTable", loadcfg32->DynamicValueRelocTable );
2545 print_dword( "CHPEMetadataPointer", loadcfg32->CHPEMetadataPointer );
2546 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFFailureRoutine )) goto done;
2547 print_dword( "GuardRFFailureRoutine", loadcfg32->GuardRFFailureRoutine );
2548 print_dword( "GuardRFFailureRoutineFuncPtr", loadcfg32->GuardRFFailureRoutineFunctionPointer );
2549 print_dword( "DynamicValueRelocTableOffset", loadcfg32->DynamicValueRelocTableOffset );
2550 print_word( "DynamicValueRelocTableSection", loadcfg32->DynamicValueRelocTableSection );
2551 print_word( "Reserved2", loadcfg32->Reserved2 );
2552 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2553 print_dword( "GuardRFVerifyStackPointerFuncPtr", loadcfg32->GuardRFVerifyStackPointerFunctionPointer );
2554 print_dword( "HotPatchTableOffset", loadcfg32->HotPatchTableOffset );
2555 print_dword( "Reserved3", loadcfg32->Reserved3 );
2556 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, EnclaveConfigurationPointer )) goto done;
2557 print_dword( "EnclaveConfigurationPointer", loadcfg32->EnclaveConfigurationPointer );
2558 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, VolatileMetadataPointer )) goto done;
2559 print_dword( "VolatileMetadataPointer", loadcfg32->VolatileMetadataPointer );
2560 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardEHContinuationTable )) goto done;
2561 print_dword( "GuardEHContinuationTable", loadcfg32->GuardEHContinuationTable );
2562 print_dword( "GuardEHContinuationCount", loadcfg32->GuardEHContinuationCount );
2563 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardXFGCheckFunctionPointer )) goto done;
2564 print_dword( "GuardXFGCheckFunctionPointer", loadcfg32->GuardXFGCheckFunctionPointer );
2565 print_dword( "GuardXFGDispatchFunctionPointer", loadcfg32->GuardXFGDispatchFunctionPointer );
2566 print_dword( "GuardXFGTableDispatchFuncPtr", loadcfg32->GuardXFGTableDispatchFunctionPointer );
2567 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CastGuardOsDeterminedFailureMode )) goto done;
2568 print_dword( "CastGuardOsDeterminedFailureMode", loadcfg32->CastGuardOsDeterminedFailureMode );
2569 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardMemcpyFunctionPointer )) goto done;
2570 print_dword( "GuardMemcpyFunctionPointer", loadcfg32->GuardMemcpyFunctionPointer );
2572 done:
2573 printf( "\n" );
2574 dump_hybrid_metadata();
2577 static void dump_dir_delay_imported_functions(void)
2579 unsigned directorySize;
2580 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
2582 if (!importDesc) return;
2584 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
2586 for (;;)
2588 const IMAGE_THUNK_DATA32* il;
2589 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
2591 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
2593 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
2594 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
2595 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
2596 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
2597 printf(" TimeDateStamp: %08X (%s)\n",
2598 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2600 printf(" Thunk Ordn Name\n");
2602 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
2604 if (!il)
2605 printf("Can't grab thunk data, going to next imported DLL\n");
2606 else
2608 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2609 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
2610 else
2611 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
2612 printf("\n");
2614 importDesc++;
2616 printf("\n");
2619 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
2621 const char* str;
2623 printf("Directory %02u\n", idx + 1);
2624 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
2625 printf(" TimeDateStamp: %08X %s\n",
2626 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
2627 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
2628 switch (idd->Type)
2630 default:
2631 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
2632 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
2633 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
2634 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
2635 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
2636 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
2637 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
2638 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
2639 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
2640 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
2641 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
2642 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
2643 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
2644 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
2645 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
2646 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
2647 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
2649 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
2650 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
2651 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
2652 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
2654 switch (idd->Type)
2656 case IMAGE_DEBUG_TYPE_UNKNOWN:
2657 break;
2658 case IMAGE_DEBUG_TYPE_COFF:
2659 dump_coff(idd->PointerToRawData, idd->SizeOfData,
2660 IMAGE_FIRST_SECTION(PE_nt_headers));
2661 break;
2662 case IMAGE_DEBUG_TYPE_CODEVIEW:
2663 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
2664 break;
2665 case IMAGE_DEBUG_TYPE_FPO:
2666 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
2667 break;
2668 case IMAGE_DEBUG_TYPE_MISC:
2670 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
2671 if (!misc || idd->SizeOfData < sizeof(*misc)) {printf("Can't get MISC debug information\n"); break;}
2672 printf(" DataType: %u (%s)\n",
2673 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
2674 printf(" Length: %u\n", (UINT)misc->Length);
2675 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
2676 printf(" Data: %s\n", misc->Data);
2678 break;
2679 case IMAGE_DEBUG_TYPE_POGO:
2681 const unsigned* data = PRD(idd->PointerToRawData, idd->SizeOfData);
2682 const unsigned* end = (const unsigned*)((const unsigned char*)data + idd->SizeOfData);
2683 unsigned idx = 0;
2685 if (!data || idd->SizeOfData < sizeof(unsigned)) {printf("Can't get PODO debug information\n"); break;}
2686 printf(" Header: %08x\n", *(const unsigned*)data);
2687 data++;
2688 printf(" Index Name Offset Size\n");
2689 while (data + 2 < end)
2691 const char* ptr;
2692 ptrdiff_t s;
2694 if (!(ptr = memchr(&data[2], '\0', (const char*)end - (const char*)&data[2]))) break;
2695 printf(" %-5u %-16s %08x %08x\n", idx, (const char*)&data[2], data[0], data[1]);
2696 s = ptr - (const char*)&data[2] + 1;
2697 data += 2 + ((s + sizeof(unsigned) - 1) / sizeof(unsigned));
2698 idx++;
2701 break;
2702 case IMAGE_DEBUG_TYPE_REPRO:
2704 const IMAGE_DEBUG_REPRO* repro = PRD(idd->PointerToRawData, idd->SizeOfData);
2705 if (!repro || idd->SizeOfData < sizeof(*repro)) {printf("Can't get REPRO debug information\n"); break;}
2706 printf(" Flags: %08X\n", repro->flags);
2707 printf(" Guid: %s\n", get_guid_str(&repro->guid));
2708 printf(" _unk0: %08X %u\n", repro->unk[0], repro->unk[0]);
2709 printf(" _unk1: %08X %u\n", repro->unk[1], repro->unk[1]);
2710 printf(" _unk2: %08X %u\n", repro->unk[2], repro->unk[2]);
2711 printf(" Timestamp: %08X\n", repro->debug_timestamp);
2713 break;
2714 default:
2716 const unsigned char* data = PRD(idd->PointerToRawData, idd->SizeOfData);
2717 if (!data) {printf("Can't get debug information for %s\n", str); break;}
2718 dump_data(data, idd->SizeOfData, " ");
2720 break;
2722 printf("\n");
2725 static void dump_dir_debug(void)
2727 unsigned nb_dbg, i;
2728 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
2730 nb_dbg /= sizeof(*debugDir);
2731 if (!debugDir || !nb_dbg) return;
2733 printf("Debug Table (%u directories)\n", nb_dbg);
2735 for (i = 0; i < nb_dbg; i++)
2737 dump_dir_debug_dir(debugDir, i);
2738 debugDir++;
2740 printf("\n");
2743 static inline void print_clrflags(const char *title, UINT value)
2745 printf(" %-34s 0x%X\n", title, value);
2746 #define X(f,s) if (value & f) printf(" %s\n", s)
2747 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
2748 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
2749 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
2750 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
2751 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
2752 #undef X
2755 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
2757 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
2760 static void dump_dir_clr_header(void)
2762 unsigned int size = 0;
2763 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
2765 if (!dir) return;
2767 printf( "CLR Header\n" );
2768 print_dword( "Header Size", dir->cb );
2769 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
2770 print_clrflags( "Flags", dir->Flags );
2771 print_dword( "EntryPointToken", dir->EntryPointToken );
2772 printf("\n");
2773 printf( "CLR Data Directory\n" );
2774 print_clrdirectory( "MetaData", &dir->MetaData );
2775 print_clrdirectory( "Resources", &dir->Resources );
2776 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
2777 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
2778 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
2779 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
2780 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
2781 printf("\n");
2784 static void dump_dynamic_relocs_arm64x( const IMAGE_BASE_RELOCATION *base_reloc, unsigned int size )
2786 unsigned int i;
2787 const IMAGE_BASE_RELOCATION *base_end = (const IMAGE_BASE_RELOCATION *)((const char *)base_reloc + size);
2789 printf( "Relocations ARM64X\n" );
2790 while (base_reloc < base_end - 1 && base_reloc->SizeOfBlock)
2792 const USHORT *rel = (const USHORT *)(base_reloc + 1);
2793 const USHORT *end = (const USHORT *)base_reloc + base_reloc->SizeOfBlock / sizeof(USHORT);
2794 printf( " Page %x\n", (UINT)base_reloc->VirtualAddress );
2795 while (rel < end && *rel)
2797 USHORT offset = *rel & 0xfff;
2798 USHORT type = (*rel >> 12) & 3;
2799 USHORT arg = *rel >> 14;
2800 rel++;
2801 switch (type)
2803 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2804 printf( " off %04x zero-fill %u bytes\n", offset, 1 << arg );
2805 break;
2806 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2807 printf( " off %04x set %u bytes value ", offset, 1 << arg );
2808 for (i = (1 << arg ) / sizeof(USHORT); i > 0; i--) printf( "%04x", rel[i - 1] );
2809 rel += (1 << arg) / sizeof(USHORT);
2810 printf( "\n" );
2811 break;
2812 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2813 printf( " off %04x add offset ", offset );
2814 if (arg & 1) printf( "-" );
2815 printf( "%08x\n", (UINT)*rel++ * ((arg & 2) ? 8 : 4) );
2816 break;
2817 default:
2818 printf( " off %04x unknown (arg %x)\n", offset, arg );
2819 break;
2822 base_reloc = (const IMAGE_BASE_RELOCATION *)end;
2826 static void dump_dynamic_relocs( const char *ptr, unsigned int size, ULONGLONG symbol )
2828 switch (symbol)
2830 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE:
2831 printf( "Relocations GUARD_RF_PROLOGUE\n" );
2832 break;
2833 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE:
2834 printf( "Relocations GUARD_RF_EPILOGUE\n" );
2835 break;
2836 case IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER:
2837 printf( "Relocations GUARD_IMPORT_CONTROL_TRANSFER\n" );
2838 break;
2839 case IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER:
2840 printf( "Relocations GUARD_INDIR_CONTROL_TRANSFER\n" );
2841 break;
2842 case IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH:
2843 printf( "Relocations GUARD_SWITCHTABLE_BRANCH\n" );
2844 break;
2845 case IMAGE_DYNAMIC_RELOCATION_ARM64X:
2846 dump_dynamic_relocs_arm64x( (const IMAGE_BASE_RELOCATION *)ptr, size );
2847 break;
2848 default:
2849 printf( "Unknown relocation symbol %s\n", longlong_str(symbol) );
2850 break;
2854 static const IMAGE_DYNAMIC_RELOCATION_TABLE *get_dyn_reloc_table(void)
2856 unsigned int size, section, offset;
2857 const IMAGE_SECTION_HEADER *sec;
2858 const IMAGE_DYNAMIC_RELOCATION_TABLE *table;
2860 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2862 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2863 if (!cfg) return NULL;
2864 size = min( size, cfg->Size );
2865 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTableSection )) return NULL;
2866 offset = cfg->DynamicValueRelocTableOffset;
2867 section = cfg->DynamicValueRelocTableSection;
2869 else
2871 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2872 if (!cfg) return NULL;
2873 size = min( size, cfg->Size );
2874 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTableSection )) return NULL;
2875 offset = cfg->DynamicValueRelocTableOffset;
2876 section = cfg->DynamicValueRelocTableSection;
2878 if (!section || section > PE_nt_headers->FileHeader.NumberOfSections) return NULL;
2879 sec = IMAGE_FIRST_SECTION( PE_nt_headers ) + section - 1;
2880 if (offset >= sec->SizeOfRawData) return NULL;
2881 return PRD( sec->PointerToRawData + offset, sizeof(*table) );
2884 static void dump_dir_dynamic_reloc(void)
2886 const char *ptr, *end;
2887 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2889 if (!table) return;
2891 printf( "Dynamic relocations (version %u)\n\n", (UINT)table->Version );
2892 ptr = (const char *)(table + 1);
2893 end = ptr + table->Size;
2894 while (ptr < end)
2896 switch (table->Version)
2898 case 1:
2899 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2901 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2902 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2903 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2905 else
2907 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2908 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2909 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2911 break;
2912 case 2:
2913 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2915 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2916 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2917 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2919 else
2921 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2922 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2923 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2925 break;
2928 printf( "\n" );
2931 static const IMAGE_BASE_RELOCATION *get_armx_relocs( unsigned int *size )
2933 const char *ptr, *end;
2934 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2936 if (!table) return NULL;
2937 ptr = (const char *)(table + 1);
2938 end = ptr + table->Size;
2939 while (ptr < end)
2941 switch (table->Version)
2943 case 1:
2944 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2946 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2947 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2949 *size = reloc->BaseRelocSize;
2950 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2952 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2954 else
2956 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2957 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2959 *size = reloc->BaseRelocSize;
2960 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2962 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2964 break;
2965 case 2:
2966 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2968 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2969 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2971 *size = reloc->FixupInfoSize;
2972 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2974 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2976 else
2978 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2979 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2981 *size = reloc->FixupInfoSize;
2982 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2984 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2986 break;
2989 return NULL;
2992 static BOOL get_alt_header( void )
2994 unsigned int size;
2995 const IMAGE_BASE_RELOCATION *end, *reloc = get_armx_relocs( &size );
2997 if (!reloc) return FALSE;
2998 end = (const IMAGE_BASE_RELOCATION *)((const char *)reloc + size);
3000 while (reloc < end - 1 && reloc->SizeOfBlock)
3002 const USHORT *rel = (const USHORT *)(reloc + 1);
3003 const USHORT *rel_end = (const USHORT *)reloc + reloc->SizeOfBlock / sizeof(USHORT);
3004 char *page = reloc->VirtualAddress ? (char *)RVA(reloc->VirtualAddress,1) : dump_base;
3006 while (rel < rel_end && *rel)
3008 USHORT offset = *rel & 0xfff;
3009 USHORT type = (*rel >> 12) & 3;
3010 USHORT arg = *rel >> 14;
3011 int val;
3012 rel++;
3013 switch (type)
3015 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
3016 memset( page + offset, 0, 1 << arg );
3017 break;
3018 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
3019 memcpy( page + offset, rel, 1 << arg );
3020 rel += (1 << arg) / sizeof(USHORT);
3021 break;
3022 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
3023 val = (unsigned int)*rel++ * ((arg & 2) ? 8 : 4);
3024 if (arg & 1) val = -val;
3025 *(int *)(page + offset) += val;
3026 break;
3029 reloc = (const IMAGE_BASE_RELOCATION *)rel_end;
3031 return TRUE;
3034 static void dump_dir_reloc(void)
3036 unsigned int i, size = 0;
3037 const USHORT *relocs;
3038 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
3039 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
3040 static const char * const names[] =
3042 "BASED_ABSOLUTE",
3043 "BASED_HIGH",
3044 "BASED_LOW",
3045 "BASED_HIGHLOW",
3046 "BASED_HIGHADJ",
3047 "BASED_MIPS_JMPADDR",
3048 "BASED_SECTION",
3049 "BASED_REL",
3050 "unknown 8",
3051 "BASED_IA64_IMM64",
3052 "BASED_DIR64",
3053 "BASED_HIGH3ADJ",
3054 "unknown 12",
3055 "unknown 13",
3056 "unknown 14",
3057 "unknown 15"
3060 if (!rel) return;
3062 printf( "Relocations\n" );
3063 while (rel < end - 1 && rel->SizeOfBlock)
3065 printf( " Page %x\n", (UINT)rel->VirtualAddress );
3066 relocs = (const USHORT *)(rel + 1);
3067 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
3068 while (i--)
3070 USHORT offset = *relocs & 0xfff;
3071 int type = *relocs >> 12;
3072 printf( " off %04x type %s\n", offset, names[type] );
3073 relocs++;
3075 rel = (const IMAGE_BASE_RELOCATION *)relocs;
3077 printf("\n");
3080 static void dump_dir_tls(void)
3082 IMAGE_TLS_DIRECTORY64 dir;
3083 const UINT *callbacks;
3084 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
3086 if (!pdir) return;
3088 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
3089 memcpy(&dir, pdir, sizeof(dir));
3090 else
3092 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
3093 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
3094 dir.AddressOfIndex = pdir->AddressOfIndex;
3095 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
3096 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
3097 dir.Characteristics = pdir->Characteristics;
3100 /* FIXME: This does not properly handle large images */
3101 printf( "Thread Local Storage\n" );
3102 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
3103 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
3104 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
3105 (UINT)dir.SizeOfZeroFill );
3106 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
3107 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
3108 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
3109 if (dir.AddressOfCallBacks)
3111 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
3112 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
3114 printf( " %08x", *callbacks );
3115 addr += sizeof(UINT);
3118 printf(" }\n\n");
3121 enum FileSig get_kind_dbg(void)
3123 const WORD* pw;
3125 pw = PRD(0, sizeof(WORD));
3126 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
3128 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
3129 return SIG_UNKNOWN;
3132 void dbg_dump(void)
3134 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
3135 unsigned nb_dbg;
3136 unsigned i;
3137 const IMAGE_DEBUG_DIRECTORY* debugDir;
3139 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
3140 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
3142 printf ("Signature: %.2s (0x%4X)\n",
3143 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
3144 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
3145 printf ("Machine: 0x%04X (%s)\n",
3146 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
3147 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
3148 printf ("TimeDateStamp: 0x%08X (%s)\n",
3149 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
3150 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
3151 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
3152 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
3153 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
3154 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
3155 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
3157 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
3158 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
3159 {printf("Can't get the sections, aborting\n"); return;}
3161 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
3163 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
3164 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
3165 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
3166 separateDebugHead->ExportedNamesSize,
3167 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
3168 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
3170 printf("Debug Table (%u directories)\n", nb_dbg);
3172 for (i = 0; i < nb_dbg; i++)
3174 dump_dir_debug_dir(debugDir, i);
3175 debugDir++;
3179 static const char *get_resource_type( unsigned int id )
3181 static const char * const types[] =
3183 NULL,
3184 "CURSOR",
3185 "BITMAP",
3186 "ICON",
3187 "MENU",
3188 "DIALOG",
3189 "STRING",
3190 "FONTDIR",
3191 "FONT",
3192 "ACCELERATOR",
3193 "RCDATA",
3194 "MESSAGETABLE",
3195 "GROUP_CURSOR",
3196 NULL,
3197 "GROUP_ICON",
3198 NULL,
3199 "VERSION",
3200 "DLGINCLUDE",
3201 NULL,
3202 "PLUGPLAY",
3203 "VXD",
3204 "ANICURSOR",
3205 "ANIICON",
3206 "HTML",
3207 "MANIFEST"
3210 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
3211 return NULL;
3214 /* dump an ASCII string with proper escaping */
3215 static int dump_strA( const unsigned char *str, size_t len )
3217 static const char escapes[32] = ".......abtnvfr.............e....";
3218 char buffer[256];
3219 char *pos = buffer;
3220 int count = 0;
3222 for (; len; str++, len--)
3224 if (pos > buffer + sizeof(buffer) - 8)
3226 fwrite( buffer, pos - buffer, 1, stdout );
3227 count += pos - buffer;
3228 pos = buffer;
3230 if (*str > 127) /* hex escape */
3232 pos += sprintf( pos, "\\x%02x", *str );
3233 continue;
3235 if (*str < 32) /* octal or C escape */
3237 if (!*str && len == 1) continue; /* do not output terminating NULL */
3238 if (escapes[*str] != '.')
3239 pos += sprintf( pos, "\\%c", escapes[*str] );
3240 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
3241 pos += sprintf( pos, "\\%03o", *str );
3242 else
3243 pos += sprintf( pos, "\\%o", *str );
3244 continue;
3246 if (*str == '\\') *pos++ = '\\';
3247 *pos++ = *str;
3249 fwrite( buffer, pos - buffer, 1, stdout );
3250 count += pos - buffer;
3251 return count;
3254 /* dump a Unicode string with proper escaping */
3255 static int dump_strW( const WCHAR *str, size_t len )
3257 static const char escapes[32] = ".......abtnvfr.............e....";
3258 char buffer[256];
3259 char *pos = buffer;
3260 int count = 0;
3262 for (; len; str++, len--)
3264 if (pos > buffer + sizeof(buffer) - 8)
3266 fwrite( buffer, pos - buffer, 1, stdout );
3267 count += pos - buffer;
3268 pos = buffer;
3270 if (*str > 127) /* hex escape */
3272 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
3273 pos += sprintf( pos, "\\x%04x", *str );
3274 else
3275 pos += sprintf( pos, "\\x%x", *str );
3276 continue;
3278 if (*str < 32) /* octal or C escape */
3280 if (!*str && len == 1) continue; /* do not output terminating NULL */
3281 if (escapes[*str] != '.')
3282 pos += sprintf( pos, "\\%c", escapes[*str] );
3283 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
3284 pos += sprintf( pos, "\\%03o", *str );
3285 else
3286 pos += sprintf( pos, "\\%o", *str );
3287 continue;
3289 if (*str == '\\') *pos++ = '\\';
3290 *pos++ = *str;
3292 fwrite( buffer, pos - buffer, 1, stdout );
3293 count += pos - buffer;
3294 return count;
3297 /* dump data for a STRING resource */
3298 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
3300 int i;
3302 for (i = 0; i < 16 && size; i++)
3304 unsigned len = *ptr++;
3306 if (len >= size)
3308 len = size;
3309 size = 0;
3311 else size -= len + 1;
3313 if (len)
3315 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
3316 dump_strW( ptr, len );
3317 printf( "\"\n" );
3318 ptr += len;
3323 /* dump data for a MESSAGETABLE resource */
3324 static void dump_msgtable_data( const void *ptr, unsigned int size, const char *prefix )
3326 const MESSAGE_RESOURCE_DATA *data = ptr;
3327 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
3328 unsigned i, j;
3330 for (i = 0; i < data->NumberOfBlocks; i++, block++)
3332 const MESSAGE_RESOURCE_ENTRY *entry;
3334 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
3335 for (j = block->LowId; j <= block->HighId; j++)
3337 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
3339 const WCHAR *str = (const WCHAR *)entry->Text;
3340 printf( "%s%08x L\"", prefix, j );
3341 dump_strW( str, strlenW(str) );
3342 printf( "\"\n" );
3344 else
3346 const char *str = (const char *) entry->Text;
3347 printf( "%s%08x \"", prefix, j );
3348 dump_strA( entry->Text, strlen(str) );
3349 printf( "\"\n" );
3351 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
3356 struct version_info
3358 WORD len;
3359 WORD val_len;
3360 WORD type;
3361 WCHAR key[1];
3363 #define GET_VALUE(info) ((void *)((char *)info + ((offsetof(struct version_info, key[strlenW(info->key) + 1]) + 3) & ~3)))
3364 #define GET_CHILD(info) ((void *)((char *)GET_VALUE(info) + ((info->val_len * (info->type ? 2 : 1) + 3) & ~3)))
3365 #define GET_NEXT(info) ((void *)((char *)info + ((info->len + 3) & ~3)))
3367 static void dump_version_children( const struct version_info *info, const char *prefix, int indent )
3369 const struct version_info *child = GET_CHILD( info );
3371 while ((char *)child < (char *)info + info->len)
3373 printf( "%s%*s", prefix, indent * 2, "" );
3374 if (child->val_len)
3376 printf( "VALUE \"" );
3377 dump_strW( child->key, strlenW(child->key) );
3378 if (child->type)
3380 printf( "\", \"" );
3381 dump_strW( GET_VALUE(child), child->val_len );
3382 printf( "\"\n" );
3384 else
3386 const WORD *data = GET_VALUE(child);
3387 unsigned int i;
3388 printf( "\"," );
3389 for (i = 0; i < child->val_len / sizeof(WORD); i++) printf( " %#x", data[i] );
3390 printf( "\n" );
3393 else
3395 printf( "BLOCK \"" );
3396 dump_strW( child->key, strlenW(child->key) );
3397 printf( "\"\n" );
3399 dump_version_children( child, prefix, indent + 1 );
3400 child = GET_NEXT( child );
3404 /* dump data for a VERSION resource */
3405 static void dump_version_data( const void *ptr, unsigned int size, const char *prefix )
3407 const struct version_info *info = ptr;
3408 const VS_FIXEDFILEINFO *fileinfo = GET_VALUE( info );
3410 printf( "%sSIGNATURE %08x\n", prefix, (UINT)fileinfo->dwSignature );
3411 printf( "%sVERSION %u.%u\n", prefix,
3412 HIWORD(fileinfo->dwStrucVersion), LOWORD(fileinfo->dwStrucVersion) );
3413 printf( "%sFILEVERSION %u.%u.%u.%u\n", prefix,
3414 HIWORD(fileinfo->dwFileVersionMS), LOWORD(fileinfo->dwFileVersionMS),
3415 HIWORD(fileinfo->dwFileVersionLS), LOWORD(fileinfo->dwFileVersionLS) );
3416 printf( "%sPRODUCTVERSION %u.%u.%u.%u\n", prefix,
3417 HIWORD(fileinfo->dwProductVersionMS), LOWORD(fileinfo->dwProductVersionMS),
3418 HIWORD(fileinfo->dwProductVersionLS), LOWORD(fileinfo->dwProductVersionLS) );
3419 printf( "%sFILEFLAGSMASK %08x\n", prefix, (UINT)fileinfo->dwFileFlagsMask );
3420 printf( "%sFILEFLAGS %08x\n", prefix, (UINT)fileinfo->dwFileFlags );
3422 switch (fileinfo->dwFileOS)
3424 #define CASE(x) case x: printf( "%sFILEOS %s\n", prefix, #x ); break
3425 CASE(VOS_UNKNOWN);
3426 CASE(VOS_DOS_WINDOWS16);
3427 CASE(VOS_DOS_WINDOWS32);
3428 CASE(VOS_OS216_PM16);
3429 CASE(VOS_OS232_PM32);
3430 CASE(VOS_NT_WINDOWS32);
3431 #undef CASE
3432 default:
3433 printf( "%sFILEOS %u.%u\n", prefix,
3434 (WORD)(fileinfo->dwFileOS >> 16), (WORD)fileinfo->dwFileOS );
3435 break;
3438 switch (fileinfo->dwFileType)
3440 #define CASE(x) case x: printf( "%sFILETYPE %s\n", prefix, #x ); break
3441 CASE(VFT_UNKNOWN);
3442 CASE(VFT_APP);
3443 CASE(VFT_DLL);
3444 CASE(VFT_DRV);
3445 CASE(VFT_FONT);
3446 CASE(VFT_VXD);
3447 CASE(VFT_STATIC_LIB);
3448 #undef CASE
3449 default:
3450 printf( "%sFILETYPE %08x\n", prefix, (UINT)fileinfo->dwFileType );
3451 break;
3454 switch (((ULONGLONG)fileinfo->dwFileType << 32) + fileinfo->dwFileSubtype)
3456 #define CASE(t,x) case (((ULONGLONG)t << 32) + x): printf( "%sFILESUBTYPE %s\n", prefix, #x ); break
3457 CASE(VFT_DRV, VFT2_UNKNOWN);
3458 CASE(VFT_DRV, VFT2_DRV_PRINTER);
3459 CASE(VFT_DRV, VFT2_DRV_KEYBOARD);
3460 CASE(VFT_DRV, VFT2_DRV_LANGUAGE);
3461 CASE(VFT_DRV, VFT2_DRV_DISPLAY);
3462 CASE(VFT_DRV, VFT2_DRV_MOUSE);
3463 CASE(VFT_DRV, VFT2_DRV_NETWORK);
3464 CASE(VFT_DRV, VFT2_DRV_SYSTEM);
3465 CASE(VFT_DRV, VFT2_DRV_INSTALLABLE);
3466 CASE(VFT_DRV, VFT2_DRV_SOUND);
3467 CASE(VFT_DRV, VFT2_DRV_COMM);
3468 CASE(VFT_DRV, VFT2_DRV_INPUTMETHOD);
3469 CASE(VFT_DRV, VFT2_DRV_VERSIONED_PRINTER);
3470 CASE(VFT_FONT, VFT2_FONT_RASTER);
3471 CASE(VFT_FONT, VFT2_FONT_VECTOR);
3472 CASE(VFT_FONT, VFT2_FONT_TRUETYPE);
3473 #undef CASE
3474 default:
3475 printf( "%sFILESUBTYPE %08x\n", prefix, (UINT)fileinfo->dwFileSubtype );
3476 break;
3479 printf( "%sFILEDATE %08x.%08x\n", prefix,
3480 (UINT)fileinfo->dwFileDateMS, (UINT)fileinfo->dwFileDateLS );
3481 dump_version_children( info, prefix, 0 );
3484 /* dump data for a HTML/MANIFEST resource */
3485 static void dump_text_data( const void *ptr, unsigned int size, const char *prefix )
3487 const char *p = ptr, *end = p + size;
3489 while (p < end)
3491 const char *start = p;
3492 while (p < end && *p != '\r' && *p != '\n') p++;
3493 printf( "%s%.*s\n", prefix, (int)(p - start), start );
3494 while (p < end && (*p == '\r' || *p == '\n')) p++;
3498 static int cmp_resource_name( const IMAGE_RESOURCE_DIR_STRING_U *str_res, const char *str )
3500 unsigned int i;
3502 for (i = 0; i < str_res->Length; i++)
3504 int res = str_res->NameString[i] - (WCHAR)str[i];
3505 if (res || !str[i]) return res;
3507 return -(WCHAR)str[i];
3510 static void dump_dir_resource(void)
3512 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
3513 const IMAGE_RESOURCE_DIRECTORY *namedir;
3514 const IMAGE_RESOURCE_DIRECTORY *langdir;
3515 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
3516 const IMAGE_RESOURCE_DIR_STRING_U *string;
3517 const IMAGE_RESOURCE_DATA_ENTRY *data;
3518 int i, j, k;
3520 if (!root) return;
3522 printf( "Resources:" );
3524 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
3526 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
3527 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
3528 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
3530 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
3531 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
3532 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
3534 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
3536 printf( "\n " );
3537 if (e1->NameIsString)
3539 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
3540 dump_unicode_str( string->NameString, string->Length );
3542 else
3544 const char *type = get_resource_type( e1->Id );
3545 if (type) printf( "%s", type );
3546 else printf( "%04x", e1->Id );
3549 printf( " Name=" );
3550 if (e2->NameIsString)
3552 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
3553 dump_unicode_str( string->NameString, string->Length );
3555 else
3556 printf( "%04x", e2->Id );
3558 printf( " Language=%04x:\n", e3->Id );
3559 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
3560 if (e1->NameIsString)
3562 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
3563 if (!cmp_resource_name( string, "TYPELIB" ))
3564 tlb_dump_resource( (void *)RVA( data->OffsetToData, data->Size ), data->Size, " | " );
3565 else if (!cmp_resource_name( string, "WINE_REGISTRY" ))
3566 dump_text_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
3567 else
3568 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
3570 else switch(e1->Id)
3572 case 6: /* RT_STRING */
3573 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
3574 break;
3575 case 11: /* RT_MESSAGETABLE */
3576 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
3577 break;
3578 case 16: /* RT_VERSION */
3579 dump_version_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
3580 break;
3581 case 23: /* RT_HTML */
3582 case 24: /* RT_MANIFEST */
3583 dump_text_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
3584 break;
3585 default:
3586 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
3587 break;
3592 printf( "\n\n" );
3595 static void dump_debug(void)
3597 const char* stabs = NULL;
3598 unsigned szstabs = 0;
3599 const char* stabstr = NULL;
3600 unsigned szstr = 0;
3601 unsigned i;
3602 const IMAGE_SECTION_HEADER* sectHead;
3604 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
3606 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
3608 if (!strcmp((const char *)sectHead->Name, ".stab"))
3610 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
3611 szstabs = sectHead->Misc.VirtualSize;
3613 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
3615 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
3616 szstr = sectHead->Misc.VirtualSize;
3619 if (stabs && stabstr)
3620 dump_stabs(stabs, szstabs, stabstr, szstr);
3623 static void dump_symbol_table(void)
3625 const IMAGE_SYMBOL* sym;
3626 int numsym;
3628 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
3629 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
3630 return;
3631 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
3632 sizeof(*sym) * numsym);
3633 if (!sym) return;
3635 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
3638 enum FileSig get_kind_exec(void)
3640 const WORD* pw;
3641 const DWORD* pdw;
3642 const IMAGE_DOS_HEADER* dh;
3644 pw = PRD(0, sizeof(WORD));
3645 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
3647 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
3649 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
3651 /* the signature is the first DWORD */
3652 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
3653 if (pdw)
3655 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
3656 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
3657 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
3659 return SIG_DOS;
3661 return SIG_UNKNOWN;
3664 void pe_dump(void)
3666 int alt = 0;
3668 PE_nt_headers = get_nt_header();
3669 print_fake_dll();
3671 for (;;)
3673 if (alt)
3674 printf( "\n**** Alternate (%s) data ****\n\n",
3675 get_machine_str(PE_nt_headers->FileHeader.Machine));
3676 else if (get_dyn_reloc_table())
3677 printf( "**** Native (%s) data ****\n\n",
3678 get_machine_str(PE_nt_headers->FileHeader.Machine));
3680 if (globals.do_dumpheader)
3682 dump_pe_header();
3683 /* FIXME: should check ptr */
3684 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
3685 PE_nt_headers->FileHeader.NumberOfSections);
3687 else if (!globals.dumpsect)
3689 /* show at least something here */
3690 dump_pe_header();
3693 if (globals_dump_sect("import"))
3695 dump_dir_imported_functions();
3696 dump_dir_delay_imported_functions();
3698 if (globals_dump_sect("export"))
3699 dump_dir_exported_functions();
3700 if (globals_dump_sect("debug"))
3701 dump_dir_debug();
3702 if (globals_dump_sect("resource"))
3703 dump_dir_resource();
3704 if (globals_dump_sect("tls"))
3705 dump_dir_tls();
3706 if (globals_dump_sect("loadcfg"))
3707 dump_dir_loadconfig();
3708 if (globals_dump_sect("clr"))
3709 dump_dir_clr_header();
3710 if (globals_dump_sect("reloc"))
3711 dump_dir_reloc();
3712 if (globals_dump_sect("dynreloc"))
3713 dump_dir_dynamic_reloc();
3714 if (globals_dump_sect("except"))
3715 dump_dir_exceptions();
3716 if (globals_dump_sect("apiset"))
3717 dump_section_apiset();
3719 if (globals.do_symbol_table)
3720 dump_symbol_table();
3721 if (globals.do_debug)
3722 dump_debug();
3723 if (alt++) break;
3724 if (!get_alt_header()) break;
3728 typedef struct _dll_symbol {
3729 size_t ordinal;
3730 char *symbol;
3731 } dll_symbol;
3733 static dll_symbol *dll_symbols = NULL;
3734 static dll_symbol *dll_current_symbol = NULL;
3736 /* Compare symbols by ordinal for qsort */
3737 static int symbol_cmp(const void *left, const void *right)
3739 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
3742 /*******************************************************************
3743 * dll_close
3745 * Free resources used by DLL
3747 /* FIXME: Not used yet
3748 static void dll_close (void)
3750 dll_symbol* ds;
3752 if (!dll_symbols) {
3753 fatal("No symbols");
3755 for (ds = dll_symbols; ds->symbol; ds++)
3756 free(ds->symbol);
3757 free (dll_symbols);
3758 dll_symbols = NULL;
3762 static void do_grab_sym( void )
3764 const IMAGE_EXPORT_DIRECTORY*exportDir;
3765 UINT i, j, *map;
3766 const UINT *pName;
3767 const UINT *pFunc;
3768 const WORD *pOrdl;
3769 const char *ptr;
3771 PE_nt_headers = get_nt_header();
3772 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
3774 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
3775 if (!pName) {printf("Can't grab functions' name table\n"); return;}
3776 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
3777 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
3778 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
3779 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
3781 /* dll_close(); */
3783 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
3785 /* bit map of used funcs */
3786 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
3787 if (!map) fatal("no memory");
3789 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
3791 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
3792 ptr = RVA(*pName++, sizeof(DWORD));
3793 if (!ptr) ptr = "cant_get_function";
3794 dll_symbols[j].symbol = xstrdup(ptr);
3795 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
3796 assert(dll_symbols[j].symbol);
3799 for (i = 0; i < exportDir->NumberOfFunctions; i++)
3801 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
3803 char ordinal_text[256];
3804 /* Ordinal only entry */
3805 sprintf (ordinal_text, "%s_%u",
3806 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
3807 (UINT)exportDir->Base + i);
3808 str_toupper(ordinal_text);
3809 dll_symbols[j].symbol = xstrdup(ordinal_text);
3810 assert(dll_symbols[j].symbol);
3811 dll_symbols[j].ordinal = exportDir->Base + i;
3812 j++;
3813 assert(j <= exportDir->NumberOfFunctions);
3816 free(map);
3818 if (NORMAL)
3819 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
3820 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
3821 j, (UINT)exportDir->Base);
3823 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
3825 dll_symbols[j].symbol = NULL;
3827 dll_current_symbol = dll_symbols;
3830 /*******************************************************************
3831 * dll_open
3833 * Open a DLL and read in exported symbols
3835 BOOL dll_open (const char *dll_name)
3837 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
3840 /*******************************************************************
3841 * dll_next_symbol
3843 * Get next exported symbol from dll
3845 BOOL dll_next_symbol (parsed_symbol * sym)
3847 if (!dll_current_symbol || !dll_current_symbol->symbol)
3848 return FALSE;
3849 assert (dll_symbols);
3850 sym->symbol = xstrdup (dll_current_symbol->symbol);
3851 sym->ordinal = dll_current_symbol->ordinal;
3852 dll_current_symbol++;
3853 return TRUE;