msi: Make TransformView_Create static.
[wine.git] / tools / winedump / pe.c
blob5c2898b6dbc41de57f4029a6539e44eb0c4dd572
1 /*
2 * PE dumping utility
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <fcntl.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winedump.h"
33 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
35 static const IMAGE_NT_HEADERS32* PE_nt_headers;
37 static const char builtin_signature[] = "Wine builtin DLL";
38 static const char fakedll_signature[] = "Wine placeholder DLL";
39 static int is_builtin;
41 const char *get_machine_str(int mach)
43 switch (mach)
45 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
46 case IMAGE_FILE_MACHINE_I386: return "i386";
47 case IMAGE_FILE_MACHINE_R3000: return "R3000";
48 case IMAGE_FILE_MACHINE_R4000: return "R4000";
49 case IMAGE_FILE_MACHINE_R10000: return "R10000";
50 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
51 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
52 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
53 case IMAGE_FILE_MACHINE_IA64: return "IA64";
54 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
55 case IMAGE_FILE_MACHINE_ARM: return "ARM";
56 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
57 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
58 case IMAGE_FILE_MACHINE_ALPHA64: return "Alpha64";
59 case IMAGE_FILE_MACHINE_CHPE_X86: return "CHPE-x86";
60 case IMAGE_FILE_MACHINE_ARM64EC: return "ARM64EC";
61 case IMAGE_FILE_MACHINE_ARM64X: return "ARM64X";
62 case IMAGE_FILE_MACHINE_RISCV32: return "RISC-V 32-bit";
63 case IMAGE_FILE_MACHINE_RISCV64: return "RISC-V 64-bit";
64 case IMAGE_FILE_MACHINE_RISCV128: return "RISC-V 128-bit";
66 return "???";
69 static const void* RVA(unsigned long rva, unsigned long len)
71 IMAGE_SECTION_HEADER* sectHead;
72 int i;
74 if (rva == 0) return NULL;
76 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
77 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
79 if (sectHead[i].VirtualAddress <= rva &&
80 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
82 /* return image import directory offset */
83 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
87 return NULL;
90 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
92 const IMAGE_DOS_HEADER *dos;
93 dos = PRD(0, sizeof(*dos));
94 if (!dos) return NULL;
95 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
96 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
97 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
100 void print_fake_dll( void )
102 const IMAGE_DOS_HEADER *dos;
104 dos = PRD(0, sizeof(*dos) + 32);
105 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
107 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
108 printf( "*** This is a Wine builtin DLL ***\n\n" );
109 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
110 printf( "*** This is a Wine fake DLL ***\n\n" );
114 static const void *get_data_dir(const IMAGE_NT_HEADERS32 *hdr, unsigned int idx, unsigned int *size)
116 if(hdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
118 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&hdr->OptionalHeader;
119 if (idx >= opt->NumberOfRvaAndSizes)
120 return NULL;
121 if(size)
122 *size = opt->DataDirectory[idx].Size;
123 return RVA(opt->DataDirectory[idx].VirtualAddress,
124 opt->DataDirectory[idx].Size);
126 else
128 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&hdr->OptionalHeader;
129 if (idx >= opt->NumberOfRvaAndSizes)
130 return NULL;
131 if(size)
132 *size = opt->DataDirectory[idx].Size;
133 return RVA(opt->DataDirectory[idx].VirtualAddress,
134 opt->DataDirectory[idx].Size);
138 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
140 return get_data_dir( PE_nt_headers, idx, size );
143 static const void* get_dir(unsigned idx)
145 return get_dir_and_size(idx, 0);
148 static const char * const DirectoryNames[16] = {
149 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
150 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
151 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
152 "IAT", "Delay IAT", "CLR Header", ""
155 static const char *get_magic_type(WORD magic)
157 switch(magic) {
158 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
159 return "32bit";
160 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
161 return "64bit";
162 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
163 return "ROM";
165 return "???";
168 static const void *get_hybrid_metadata(void)
170 unsigned int size;
172 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
174 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
175 if (!cfg) return 0;
176 size = min( size, cfg->Size );
177 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CHPEMetadataPointer )) return 0;
178 return RVA( cfg->CHPEMetadataPointer - ((const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader)->ImageBase, 1 );
180 else
182 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
183 if (!cfg) return 0;
184 size = min( size, cfg->Size );
185 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CHPEMetadataPointer )) return 0;
186 return RVA( cfg->CHPEMetadataPointer - PE_nt_headers->OptionalHeader.ImageBase, 1 );
190 static inline const char *longlong_str( ULONGLONG value )
192 static char buffer[20];
194 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
195 sprintf(buffer, "%lx%08lx", (unsigned long)(value >> 32), (unsigned long)value);
196 else
197 sprintf(buffer, "%lx", (unsigned long)value);
198 return buffer;
201 static inline void print_word(const char *title, WORD value)
203 printf(" %-34s 0x%-4X %u\n", title, value, value);
206 static inline void print_dword(const char *title, UINT value)
208 printf(" %-34s 0x%-8x %u\n", title, value, value);
211 static inline void print_longlong(const char *title, ULONGLONG value)
213 printf(" %-34s 0x%s\n", title, longlong_str(value));
216 static inline void print_ver(const char *title, BYTE major, BYTE minor)
218 printf(" %-34s %u.%02u\n", title, major, minor);
221 static inline void print_subsys(const char *title, WORD value)
223 const char *str;
224 switch (value)
226 default:
227 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
228 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
229 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
230 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
231 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
232 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
233 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
234 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
235 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
236 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
237 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
238 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
239 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
240 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
242 printf(" %-34s 0x%X (%s)\n", title, value, str);
245 static inline void print_dllflags(const char *title, WORD value)
247 printf(" %-34s 0x%04X\n", title, value);
248 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
249 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
250 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
251 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
252 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
253 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
254 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
255 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
256 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
257 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
258 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
259 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
260 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
261 #undef X
264 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
266 unsigned i;
267 printf("Data Directory\n");
269 for (i = 0; i < n && i < 16; i++)
271 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
272 DirectoryNames[i], (UINT)directory[i].VirtualAddress,
273 (UINT)directory[i].Size);
277 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
279 IMAGE_OPTIONAL_HEADER32 oh;
280 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
282 /* in case optional header is missing or partial */
283 memset(&oh, 0, sizeof(oh));
284 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
285 optionalHeader = &oh;
287 print_word("Magic", optionalHeader->Magic);
288 print_ver("linker version",
289 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
290 print_dword("size of code", optionalHeader->SizeOfCode);
291 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
292 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
293 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
294 print_dword("base of code", optionalHeader->BaseOfCode);
295 print_dword("base of data", optionalHeader->BaseOfData);
296 print_dword("image base", optionalHeader->ImageBase);
297 print_dword("section align", optionalHeader->SectionAlignment);
298 print_dword("file align", optionalHeader->FileAlignment);
299 print_ver("required OS version",
300 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
301 print_ver("image version",
302 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
303 print_ver("subsystem version",
304 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
305 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
306 print_dword("size of image", optionalHeader->SizeOfImage);
307 print_dword("size of headers", optionalHeader->SizeOfHeaders);
308 print_dword("checksum", optionalHeader->CheckSum);
309 print_subsys("Subsystem", optionalHeader->Subsystem);
310 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
311 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
312 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
313 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
314 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
315 print_dword("loader flags", optionalHeader->LoaderFlags);
316 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
317 printf("\n");
318 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
319 printf("\n");
322 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
324 IMAGE_OPTIONAL_HEADER64 oh;
325 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
327 /* in case optional header is missing or partial */
328 memset(&oh, 0, sizeof(oh));
329 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
330 optionalHeader = &oh;
332 print_word("Magic", optionalHeader->Magic);
333 print_ver("linker version",
334 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
335 print_dword("size of code", optionalHeader->SizeOfCode);
336 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
337 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
338 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
339 print_dword("base of code", optionalHeader->BaseOfCode);
340 print_longlong("image base", optionalHeader->ImageBase);
341 print_dword("section align", optionalHeader->SectionAlignment);
342 print_dword("file align", optionalHeader->FileAlignment);
343 print_ver("required OS version",
344 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
345 print_ver("image version",
346 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
347 print_ver("subsystem version",
348 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
349 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
350 print_dword("size of image", optionalHeader->SizeOfImage);
351 print_dword("size of headers", optionalHeader->SizeOfHeaders);
352 print_dword("checksum", optionalHeader->CheckSum);
353 print_subsys("Subsystem", optionalHeader->Subsystem);
354 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
355 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
356 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
357 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
358 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
359 print_dword("loader flags", optionalHeader->LoaderFlags);
360 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
361 printf("\n");
362 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
363 printf("\n");
366 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
368 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
370 switch(optionalHeader->Magic) {
371 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
372 dump_optional_header32(optionalHeader, header_size);
373 break;
374 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
375 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
376 break;
377 default:
378 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
379 break;
383 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader, BOOL is_hybrid)
385 const char *name = get_machine_str(fileHeader->Machine);
387 printf("File Header\n");
389 if (is_hybrid)
391 switch (fileHeader->Machine)
393 case IMAGE_FILE_MACHINE_I386: name = "CHPE"; break;
394 case IMAGE_FILE_MACHINE_AMD64: name = "ARM64EC"; break;
395 case IMAGE_FILE_MACHINE_ARM64: name = "ARM64X"; break;
398 printf(" Machine: %04X (%s)\n", fileHeader->Machine, name);
399 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
400 printf(" TimeDateStamp: %08X (%s)\n",
401 (UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp));
402 printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
403 printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
404 printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
405 printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
406 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
407 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
408 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
409 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
410 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
411 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
412 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
413 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
414 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
415 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
416 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
417 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
418 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
419 X(IMAGE_FILE_SYSTEM, "SYSTEM");
420 X(IMAGE_FILE_DLL, "DLL");
421 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
422 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
423 #undef X
424 printf("\n");
427 static void dump_pe_header(void)
429 dump_file_header(&PE_nt_headers->FileHeader, get_hybrid_metadata() != NULL);
430 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader,
431 PE_nt_headers->FileHeader.SizeOfOptionalHeader);
434 void dump_section_characteristics(DWORD characteristics, const char* sep)
436 #define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
437 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
438 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
439 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
440 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
441 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
442 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
444 X(IMAGE_SCN_CNT_CODE, "CODE");
445 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
446 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
448 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
449 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
450 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
451 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
452 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
454 /* 0x00002000 - Reserved */
455 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
456 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
458 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
459 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
460 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
461 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
462 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
464 switch (characteristics & IMAGE_SCN_ALIGN_MASK)
466 #define X2(b,s) case b: printf("%s%s", sep, s); break
467 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
468 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
469 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
470 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
471 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
472 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
473 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
474 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
475 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
476 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
477 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
478 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
479 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
480 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
481 #undef X2
484 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
486 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
487 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
488 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
489 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
490 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
491 X(IMAGE_SCN_MEM_READ, "MEM_READ");
492 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
493 #undef X
496 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
498 unsigned offset;
500 /* long section name ? */
501 if (strtable && sectHead->Name[0] == '/' &&
502 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
503 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
504 else
505 printf(" %-8.8s", sectHead->Name);
506 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
507 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
508 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
509 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
510 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
511 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
512 printf(" line # offs: %-8u line #'s: %-8u\n",
513 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
514 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
515 printf(" ");
516 dump_section_characteristics(sectHead->Characteristics, " ");
518 printf("\n\n");
521 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
523 const IMAGE_SECTION_HEADER* sectHead = addr;
524 unsigned i;
525 const char* strtable;
527 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
529 strtable = (const char*)base +
530 PE_nt_headers->FileHeader.PointerToSymbolTable +
531 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
533 else strtable = NULL;
535 printf("Section Table\n");
536 for (i = 0; i < num_sect; i++, sectHead++)
538 dump_section(sectHead, strtable);
540 if (globals.do_dump_rawdata)
542 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
543 printf("\n");
548 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
550 const WCHAR *wstr = PRD( rva, len );
551 char *ret = buffer;
553 len /= sizeof(WCHAR);
554 while (len--) *buffer++ = *wstr++;
555 *buffer = 0;
556 return ret;
559 static void dump_section_apiset(void)
561 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
562 const UINT *ptr, *entry, *value, *hash;
563 unsigned int i, j, count, val_count, rva;
564 char buffer[128];
566 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
568 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
569 rva = sect->PointerToRawData;
570 ptr = PRD( rva, sizeof(*ptr) );
571 printf( "ApiSet section:\n" );
572 switch (ptr[0]) /* version */
574 case 2:
575 printf( " Version: %u\n", ptr[0] );
576 printf( " Count: %08x\n", ptr[1] );
577 count = ptr[1];
578 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
579 for (i = 0; i < count; i++, entry += 3)
581 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
582 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
583 val_count = *value++;
584 for (j = 0; j < val_count; j++, value += 4)
586 putchar( ' ' );
587 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
588 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
590 printf( "\n");
592 break;
593 case 4:
594 printf( " Version: %u\n", ptr[0] );
595 printf( " Size: %08x\n", ptr[1] );
596 printf( " Flags: %08x\n", ptr[2] );
597 printf( " Count: %08x\n", ptr[3] );
598 count = ptr[3];
599 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
600 for (i = 0; i < count; i++, entry += 6)
602 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
603 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
604 value++; /* flags */
605 val_count = *value++;
606 for (j = 0; j < val_count; j++, value += 5)
608 putchar( ' ' );
609 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
610 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
612 printf( "\n");
614 break;
615 case 6:
616 printf( " Version: %u\n", ptr[0] );
617 printf( " Size: %08x\n", ptr[1] );
618 printf( " Flags: %08x\n", ptr[2] );
619 printf( " Count: %08x\n", ptr[3] );
620 printf( " EntryOffset: %08x\n", ptr[4] );
621 printf( " HashOffset: %08x\n", ptr[5] );
622 printf( " HashFactor: %08x\n", ptr[6] );
623 count = ptr[3];
624 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
625 for (i = 0; i < count; i++, entry += 6)
627 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
628 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
629 for (j = 0; j < entry[5]; j++, value += 5)
631 putchar( ' ' );
632 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
633 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
635 printf( "\n" );
637 printf( " Hash table:\n" );
638 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
639 for (i = 0; i < count; i++, hash += 2)
641 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
642 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
644 break;
645 default:
646 printf( "*** Unknown version %u\n", ptr[0] );
647 break;
649 break;
653 static const char *find_export_from_rva( UINT rva )
655 UINT i, *func_names;
656 const UINT *funcs;
657 const UINT *names;
658 const WORD *ordinals;
659 const IMAGE_EXPORT_DIRECTORY *dir;
660 const char *ret = NULL;
662 if (!(dir = get_dir( IMAGE_FILE_EXPORT_DIRECTORY ))) return NULL;
663 if (!(funcs = RVA( dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD) ))) return NULL;
664 names = RVA( dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD) );
665 ordinals = RVA( dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD) );
666 func_names = calloc( dir->NumberOfFunctions, sizeof(*func_names) );
668 for (i = 0; i < dir->NumberOfNames; i++) func_names[ordinals[i]] = names[i];
669 for (i = 0; i < dir->NumberOfFunctions && !ret; i++)
670 if (funcs[i] == rva) ret = get_symbol_str( RVA( func_names[i], sizeof(DWORD) ));
672 free( func_names );
673 if (!ret && rva == PE_nt_headers->OptionalHeader.AddressOfEntryPoint) return "<EntryPoint>";
674 return ret;
677 static void dump_dir_exported_functions(void)
679 unsigned int size;
680 const IMAGE_EXPORT_DIRECTORY *dir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
681 UINT i, *funcs;
682 const UINT *pFunc;
683 const UINT *pName;
684 const WORD *pOrdl;
686 if (!dir) return;
688 printf("\n");
689 printf(" Name: %s\n", (const char*)RVA(dir->Name, sizeof(DWORD)));
690 printf(" Characteristics: %08x\n", (UINT)dir->Characteristics);
691 printf(" TimeDateStamp: %08X %s\n",
692 (UINT)dir->TimeDateStamp, get_time_str(dir->TimeDateStamp));
693 printf(" Version: %u.%02u\n", dir->MajorVersion, dir->MinorVersion);
694 printf(" Ordinal base: %u\n", (UINT)dir->Base);
695 printf(" # of functions: %u\n", (UINT)dir->NumberOfFunctions);
696 printf(" # of Names: %u\n", (UINT)dir->NumberOfNames);
697 printf("Addresses of functions: %08X\n", (UINT)dir->AddressOfFunctions);
698 printf("Addresses of name ordinals: %08X\n", (UINT)dir->AddressOfNameOrdinals);
699 printf("Addresses of names: %08X\n", (UINT)dir->AddressOfNames);
700 printf("\n");
701 printf(" Entry Pt Ordn Name\n");
703 pFunc = RVA(dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD));
704 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
705 pName = RVA(dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD));
706 pOrdl = RVA(dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD));
708 funcs = calloc( dir->NumberOfFunctions, sizeof(*funcs) );
709 if (!funcs) fatal("no memory");
711 for (i = 0; i < dir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
713 for (i = 0; i < dir->NumberOfFunctions; i++)
715 if (!pFunc[i]) continue;
716 printf(" %08X %5u ", pFunc[i], (UINT)dir->Base + i);
717 if (funcs[i])
718 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
719 else
720 printf("<by ordinal>");
722 /* check for forwarded function */
723 if ((const char *)RVA(pFunc[i],1) >= (const char *)dir &&
724 (const char *)RVA(pFunc[i],1) < (const char *)dir + size)
725 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
726 printf("\n");
728 free(funcs);
729 printf("\n");
733 struct runtime_function_x86_64
735 UINT BeginAddress;
736 UINT EndAddress;
737 UINT UnwindData;
740 struct runtime_function_armnt
742 UINT BeginAddress;
743 union {
744 UINT UnwindData;
745 struct {
746 UINT Flag : 2;
747 UINT FunctionLength : 11;
748 UINT Ret : 2;
749 UINT H : 1;
750 UINT Reg : 3;
751 UINT R : 1;
752 UINT L : 1;
753 UINT C : 1;
754 UINT StackAdjust : 10;
759 struct runtime_function_arm64
761 UINT BeginAddress;
762 union
764 UINT UnwindData;
765 struct
767 UINT Flag : 2;
768 UINT FunctionLength : 11;
769 UINT RegF : 3;
770 UINT RegI : 4;
771 UINT H : 1;
772 UINT CR : 2;
773 UINT FrameSize : 9;
778 union handler_data
780 struct runtime_function_x86_64 chain;
781 UINT handler;
784 struct opcode
786 BYTE offset;
787 BYTE code : 4;
788 BYTE info : 4;
791 struct unwind_info_x86_64
793 BYTE version : 3;
794 BYTE flags : 5;
795 BYTE prolog;
796 BYTE count;
797 BYTE frame_reg : 4;
798 BYTE frame_offset : 4;
799 struct opcode opcodes[1]; /* count entries */
800 /* followed by union handler_data */
803 struct unwind_info_armnt
805 UINT function_length : 18;
806 UINT version : 2;
807 UINT x : 1;
808 UINT e : 1;
809 UINT f : 1;
810 UINT count : 5;
811 UINT words : 4;
814 struct unwind_info_ext_armnt
816 WORD excount;
817 BYTE exwords;
818 BYTE reserved;
821 struct unwind_info_epilogue_armnt
823 UINT offset : 18;
824 UINT res : 2;
825 UINT cond : 4;
826 UINT index : 8;
829 #define UWOP_PUSH_NONVOL 0
830 #define UWOP_ALLOC_LARGE 1
831 #define UWOP_ALLOC_SMALL 2
832 #define UWOP_SET_FPREG 3
833 #define UWOP_SAVE_NONVOL 4
834 #define UWOP_SAVE_NONVOL_FAR 5
835 #define UWOP_SAVE_XMM128 8
836 #define UWOP_SAVE_XMM128_FAR 9
837 #define UWOP_PUSH_MACHFRAME 10
839 #define UNW_FLAG_EHANDLER 1
840 #define UNW_FLAG_UHANDLER 2
841 #define UNW_FLAG_CHAININFO 4
843 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
845 static const char * const reg_names[16] =
846 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
847 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
849 const union handler_data *handler_data;
850 const struct unwind_info_x86_64 *info;
851 unsigned int i, count;
853 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
854 if (function->UnwindData & 1)
856 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
857 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
858 return;
860 info = RVA( function->UnwindData, sizeof(*info) );
862 printf( " unwind info at %08x\n", function->UnwindData );
863 if (info->version != 1)
865 printf( " *** unknown version %u\n", info->version );
866 return;
868 printf( " flags %x", info->flags );
869 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
870 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
871 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
872 printf( "\n prolog 0x%x bytes\n", info->prolog );
874 if (info->frame_reg)
875 printf( " frame register %s offset 0x%x(%%rsp)\n",
876 reg_names[info->frame_reg], info->frame_offset * 16 );
878 for (i = 0; i < info->count; i++)
880 printf( " 0x%02x: ", info->opcodes[i].offset );
881 switch (info->opcodes[i].code)
883 case UWOP_PUSH_NONVOL:
884 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
885 break;
886 case UWOP_ALLOC_LARGE:
887 if (info->opcodes[i].info)
889 count = *(const UINT *)&info->opcodes[i+1];
890 i += 2;
892 else
894 count = *(const USHORT *)&info->opcodes[i+1] * 8;
895 i++;
897 printf( "sub $0x%x,%%rsp\n", count );
898 break;
899 case UWOP_ALLOC_SMALL:
900 count = (info->opcodes[i].info + 1) * 8;
901 printf( "sub $0x%x,%%rsp\n", count );
902 break;
903 case UWOP_SET_FPREG:
904 printf( "lea 0x%x(%%rsp),%s\n",
905 info->frame_offset * 16, reg_names[info->frame_reg] );
906 break;
907 case UWOP_SAVE_NONVOL:
908 count = *(const USHORT *)&info->opcodes[i+1] * 8;
909 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
910 i++;
911 break;
912 case UWOP_SAVE_NONVOL_FAR:
913 count = *(const UINT *)&info->opcodes[i+1];
914 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
915 i += 2;
916 break;
917 case UWOP_SAVE_XMM128:
918 count = *(const USHORT *)&info->opcodes[i+1] * 16;
919 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
920 i++;
921 break;
922 case UWOP_SAVE_XMM128_FAR:
923 count = *(const UINT *)&info->opcodes[i+1];
924 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
925 i += 2;
926 break;
927 case UWOP_PUSH_MACHFRAME:
928 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
929 break;
930 default:
931 printf( "*** unknown code %u\n", info->opcodes[i].code );
932 break;
936 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
937 if (info->flags & UNW_FLAG_CHAININFO)
939 printf( " -> function %08x-%08x\n",
940 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
941 return;
943 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
944 printf( " handler %08x data at %08x\n", handler_data->handler,
945 (UINT)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
948 static const BYTE armnt_code_lengths[256] =
950 /* 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,
951 /* 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,
952 /* 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,
953 /* 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,
954 /* 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,
955 /* 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,
956 /* 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,
957 /* 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
960 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
962 const struct unwind_info_armnt *info;
963 const struct unwind_info_ext_armnt *infoex;
964 const struct unwind_info_epilogue_armnt *infoepi;
965 unsigned int rva;
966 WORD i, count = 0, words = 0;
968 if (fnc->Flag)
970 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
971 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
973 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
974 (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2 );
975 printf( " Flag %x\n", fnc->Flag );
976 printf( " FunctionLength %x\n", fnc->FunctionLength );
977 printf( " Ret %x\n", fnc->Ret );
978 printf( " H %x\n", fnc->H );
979 printf( " Reg %x\n", fnc->Reg );
980 printf( " R %x\n", fnc->R );
981 printf( " L %x\n", fnc->L );
982 printf( " C %x\n", fnc->C );
983 printf( " StackAdjust %x\n", fnc->StackAdjust );
985 if (fnc->StackAdjust >= 0x03f4)
987 pf = fnc->StackAdjust & 0x04;
988 ef = fnc->StackAdjust & 0x08;
989 stack = (fnc->StackAdjust & 3) + 1;
992 if (!fnc->R || pf)
994 int first = 4, last = fnc->Reg + 4;
995 if (pf)
997 first = (~fnc->StackAdjust) & 3;
998 if (fnc->R)
999 last = 3;
1001 if (first == last)
1002 sprintf(intregs, "r%u", first);
1003 else
1004 sprintf(intregs, "r%u-r%u", first, last);
1005 fpoffset = last + 1 - first;
1008 if (!fnc->R || ef)
1010 int first = 4, last = fnc->Reg + 4;
1011 if (ef)
1013 first = (~fnc->StackAdjust) & 3;
1014 if (fnc->R)
1015 last = 3;
1017 if (first == last)
1018 sprintf(intregspop, "r%u", first);
1019 else
1020 sprintf(intregspop, "r%u-r%u", first, last);
1023 if (fnc->C)
1025 if (intregs[0])
1026 strcat(intregs, ", ");
1027 if (intregspop[0])
1028 strcat(intregspop, ", ");
1029 strcat(intregs, "r11");
1030 strcat(intregspop, "r11");
1032 if (fnc->L)
1034 if (intregs[0])
1035 strcat(intregs, ", ");
1036 strcat(intregs, "lr");
1038 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
1039 strcat(intregspop, ", ");
1040 if (fnc->Ret != 0)
1041 strcat(intregspop, "lr");
1042 else if (!fnc->H)
1043 strcat(intregspop, "pc");
1046 if (fnc->R)
1048 if (fnc->Reg)
1049 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
1050 else
1051 strcpy(vfpregs, "d8");
1054 if (fnc->Flag == 1) {
1055 if (fnc->H)
1056 printf( " Unwind Code\tpush {r0-r3}\n" );
1058 if (intregs[0])
1059 printf( " Unwind Code\tpush {%s}\n", intregs );
1061 if (fnc->C && fpoffset == 0)
1062 printf( " Unwind Code\tmov r11, sp\n" );
1063 else if (fnc->C)
1064 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
1066 if (fnc->R && fnc->Reg != 0x07)
1067 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
1069 if (stack && !pf)
1070 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
1073 if (fnc->Ret == 3)
1074 return;
1075 printf( "Epilogue:\n" );
1077 if (stack && !ef)
1078 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1080 if (fnc->R && fnc->Reg != 0x07)
1081 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1083 if (intregspop[0])
1084 printf( " Unwind Code\tpop {%s}\n", intregspop );
1086 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1087 printf( " Unwind Code\tadd sp, sp, #16\n" );
1088 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1089 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1091 if (fnc->Ret == 1)
1092 printf( " Unwind Code\tbx <reg>\n" );
1093 else if (fnc->Ret == 2)
1094 printf( " Unwind Code\tb <address>\n" );
1096 return;
1099 info = RVA( fnc->UnwindData, sizeof(*info) );
1100 rva = fnc->UnwindData + sizeof(*info);
1101 count = info->count;
1102 words = info->words;
1104 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1105 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1106 printf( " unwind info at %08x\n", fnc->UnwindData );
1107 printf( " Flag %x\n", fnc->Flag );
1108 printf( " FunctionLength %x\n", info->function_length );
1109 printf( " Version %x\n", info->version );
1110 printf( " X %x\n", info->x );
1111 printf( " E %x\n", info->e );
1112 printf( " F %x\n", info->f );
1113 printf( " Count %x\n", count );
1114 printf( " Words %x\n", words );
1116 if (!info->count && !info->words)
1118 infoex = RVA( rva, sizeof(*infoex) );
1119 rva = rva + sizeof(*infoex);
1120 count = infoex->excount;
1121 words = infoex->exwords;
1122 printf( " ExtCount %x\n", count );
1123 printf( " ExtWords %x\n", words );
1126 if (!info->e)
1128 infoepi = RVA( rva, count * sizeof(*infoepi) );
1129 rva = rva + count * sizeof(*infoepi);
1131 for (i = 0; i < count; i++)
1133 printf( " Epilogue Scope %x\n", i );
1134 printf( " Offset %x\n", infoepi[i].offset );
1135 printf( " Reserved %x\n", infoepi[i].res );
1136 printf( " Condition %x\n", infoepi[i].cond );
1137 printf( " Index %x\n", infoepi[i].index );
1140 else
1141 infoepi = NULL;
1143 if (words)
1145 const unsigned int *codes;
1146 BYTE b, *bytes;
1147 BOOL inepilogue = FALSE;
1149 codes = RVA( rva, words * sizeof(*codes) );
1150 rva = rva + words * sizeof(*codes);
1151 bytes = (BYTE*)codes;
1153 for (b = 0; b < words * sizeof(*codes); b++)
1155 BYTE code = bytes[b];
1156 BYTE len = armnt_code_lengths[code];
1158 if (info->e && b == count)
1160 printf( "Epilogue:\n" );
1161 inepilogue = TRUE;
1163 else if (!info->e && infoepi)
1165 for (i = 0; i < count; i++)
1166 if (b == infoepi[i].index)
1168 printf( "Epilogue from Scope %x at %08x:\n", i,
1169 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1170 inepilogue = TRUE;
1174 printf( " Unwind Code");
1175 for (i = 0; i < len; i++)
1176 printf( " %02x", bytes[b+i] );
1177 printf( "\t" );
1179 if (code == 0x00)
1180 printf( "\n" );
1181 else if (code <= 0x7f)
1182 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1183 else if (code <= 0xbf)
1185 WORD excode, f;
1186 BOOL first = TRUE;
1187 BYTE excodes = bytes[++b];
1189 excode = (code << 8) | excodes;
1190 printf( "%s {", inepilogue ? "pop" : "push" );
1192 for (f = 0; f <= 12; f++)
1194 if ((excode >> f) & 1)
1196 printf( "%sr%u", first ? "" : ", ", f );
1197 first = FALSE;
1201 if (excode & 0x2000)
1202 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1204 printf( "}\n" );
1206 else if (code <= 0xcf)
1207 if (inepilogue)
1208 printf( "mov sp, r%u\n", code & 0x0f );
1209 else
1210 printf( "mov r%u, sp\n", code & 0x0f );
1211 else if (code <= 0xd7)
1212 if (inepilogue)
1213 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1214 else
1215 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1216 else if (code <= 0xdf)
1217 if (inepilogue)
1218 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1219 else
1220 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1221 else if (code <= 0xe7)
1222 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1223 else if (code <= 0xeb)
1225 WORD excode;
1226 BYTE excodes = bytes[++b];
1228 excode = (code << 8) | excodes;
1229 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1231 else if (code <= 0xed)
1233 WORD excode, f;
1234 BOOL first = TRUE;
1235 BYTE excodes = bytes[++b];
1237 excode = (code << 8) | excodes;
1238 printf( "%s {", inepilogue ? "pop" : "push" );
1240 for (f = 0; f < 8; f++)
1242 if ((excode >> f) & 1)
1244 printf( "%sr%u", first ? "" : ", ", f );
1245 first = FALSE;
1249 if (excode & 0x0100)
1250 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1252 printf( "}\n" );
1254 else if (code == 0xee)
1255 printf( "unknown 16\n" );
1256 else if (code == 0xef)
1258 WORD excode;
1259 BYTE excodes = bytes[++b];
1261 if (excodes <= 0x0f)
1263 excode = (code << 8) | excodes;
1264 if (inepilogue)
1265 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1266 else
1267 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1269 else
1270 printf( "unknown 32\n" );
1272 else if (code <= 0xf4)
1273 printf( "unknown\n" );
1274 else if (code <= 0xf6)
1276 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1277 BYTE excodes = bytes[++b];
1279 excode = (code << 8) | excodes;
1280 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1281 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1283 else if (code <= 0xf7)
1285 unsigned int excode;
1286 BYTE excodes[2];
1288 excodes[0] = bytes[++b];
1289 excodes[1] = bytes[++b];
1290 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1291 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1293 else if (code <= 0xf8)
1295 unsigned int excode;
1296 BYTE excodes[3];
1298 excodes[0] = bytes[++b];
1299 excodes[1] = bytes[++b];
1300 excodes[2] = bytes[++b];
1301 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1302 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1304 else if (code <= 0xf9)
1306 unsigned int excode;
1307 BYTE excodes[2];
1309 excodes[0] = bytes[++b];
1310 excodes[1] = bytes[++b];
1311 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1312 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1314 else if (code <= 0xfa)
1316 unsigned int excode;
1317 BYTE excodes[3];
1319 excodes[0] = bytes[++b];
1320 excodes[1] = bytes[++b];
1321 excodes[2] = bytes[++b];
1322 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1323 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1325 else if (code <= 0xfb)
1326 printf( "nop\n" );
1327 else if (code <= 0xfc)
1328 printf( "nop.w\n" );
1329 else if (code <= 0xfd)
1331 printf( "(end) nop\n" );
1332 inepilogue = TRUE;
1334 else if (code <= 0xfe)
1336 printf( "(end) nop.w\n" );
1337 inepilogue = TRUE;
1339 else
1341 printf( "end\n" );
1342 inepilogue = TRUE;
1347 if (info->x)
1349 const unsigned int *handler;
1351 handler = RVA( rva, sizeof(*handler) );
1352 rva = rva + sizeof(*handler);
1354 printf( " handler %08x data at %08x\n", *handler, rva);
1358 struct unwind_info_arm64
1360 UINT function_length : 18;
1361 UINT version : 2;
1362 UINT x : 1;
1363 UINT e : 1;
1364 UINT epilog : 5;
1365 UINT codes : 5;
1368 struct unwind_info_ext_arm64
1370 WORD epilog;
1371 BYTE codes;
1372 BYTE reserved;
1375 struct unwind_info_epilog_arm64
1377 UINT offset : 18;
1378 UINT res : 4;
1379 UINT index : 10;
1382 static const BYTE code_lengths[256] =
1384 /* 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,
1385 /* 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,
1386 /* 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,
1387 /* 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,
1388 /* 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,
1389 /* 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,
1390 /* 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,
1391 /* e0 */ 4,1,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1394 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1396 unsigned int i, j;
1398 for (i = 0; i < count; i += code_lengths[ptr[i]])
1400 BYTE len = code_lengths[ptr[i]];
1401 unsigned int val = ptr[i];
1402 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1403 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1405 printf( " %04x: ", i );
1406 for (j = 0; j < 4; j++)
1407 if (j < len) printf( "%02x ", ptr[i+j] );
1408 else printf( " " );
1410 if (ptr[i] < 0x20) /* alloc_s */
1412 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1414 else if (ptr[i] < 0x40) /* save_r19r20_x */
1416 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1418 else if (ptr[i] < 0x80) /* save_fplr */
1420 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1422 else if (ptr[i] < 0xc0) /* save_fplr_x */
1424 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1426 else if (ptr[i] < 0xc8) /* alloc_m */
1428 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1430 else if (ptr[i] < 0xcc) /* save_regp */
1432 int reg = 19 + ((val >> 6) & 0xf);
1433 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1435 else if (ptr[i] < 0xd0) /* save_regp_x */
1437 int reg = 19 + ((val >> 6) & 0xf);
1438 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1440 else if (ptr[i] < 0xd4) /* save_reg */
1442 int reg = 19 + ((val >> 6) & 0xf);
1443 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1445 else if (ptr[i] < 0xd6) /* save_reg_x */
1447 int reg = 19 + ((val >> 5) & 0xf);
1448 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1450 else if (ptr[i] < 0xd8) /* save_lrpair */
1452 int reg = 19 + 2 * ((val >> 6) & 0x7);
1453 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1455 else if (ptr[i] < 0xda) /* save_fregp */
1457 int reg = 8 + ((val >> 6) & 0x7);
1458 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1460 else if (ptr[i] < 0xdc) /* save_fregp_x */
1462 int reg = 8 + ((val >> 6) & 0x7);
1463 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1465 else if (ptr[i] < 0xde) /* save_freg */
1467 int reg = 8 + ((val >> 6) & 0x7);
1468 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1470 else if (ptr[i] == 0xde) /* save_freg_x */
1472 int reg = 8 + ((val >> 5) & 0x7);
1473 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1475 else if (ptr[i] == 0xe0) /* alloc_l */
1477 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1479 else if (ptr[i] == 0xe1) /* set_fp */
1481 printf( "mov x29,sp\n" );
1483 else if (ptr[i] == 0xe2) /* add_fp */
1485 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1487 else if (ptr[i] == 0xe3) /* nop */
1489 printf( "nop\n" );
1491 else if (ptr[i] == 0xe4) /* end */
1493 printf( "end\n" );
1495 else if (ptr[i] == 0xe5) /* end_c */
1497 printf( "end_c\n" );
1499 else if (ptr[i] == 0xe6) /* save_next */
1501 printf( "save_next\n" );
1503 else if (ptr[i] == 0xe7) /* arithmetic */
1505 switch ((val >> 4) & 0x0f)
1507 case 0: printf( "add lr,lr,x28\n" ); break;
1508 case 1: printf( "add lr,lr,sp\n" ); break;
1509 case 2: printf( "sub lr,lr,x28\n" ); break;
1510 case 3: printf( "sub lr,lr,sp\n" ); break;
1511 case 4: printf( "eor lr,lr,x28\n" ); break;
1512 case 5: printf( "eor lr,lr,sp\n" ); break;
1513 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1514 case 8: printf( "ror lr,lr,x28\n" ); break;
1515 case 9: printf( "ror lr,lr,sp\n" ); break;
1516 default:printf( "unknown op\n" ); break;
1519 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1521 printf( "MSFT_OP_TRAP_FRAME\n" );
1523 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1525 printf( "MSFT_OP_MACHINE_FRAME\n" );
1527 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1529 printf( "MSFT_OP_CONTEXT\n" );
1531 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1533 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1535 else printf( "??\n");
1539 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1541 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
1543 if (func->CR == 1) intsz += 8;
1544 if (func->RegF) fpsz += 8;
1546 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
1547 locsz = func->FrameSize * 16 - savesz;
1549 switch (func->CR)
1551 case 3:
1552 printf( " %04x: mov x29,sp\n", pos++ );
1553 if (locsz <= 512)
1555 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1556 break;
1558 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1559 /* fall through */
1560 case 0:
1561 case 1:
1562 if (locsz <= 4080)
1564 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1566 else
1568 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1569 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1571 break;
1574 if (func->H)
1576 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1577 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1578 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1579 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1582 if (func->RegF)
1584 if (func->RegF % 2 == 0)
1585 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
1586 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
1588 if (!i && !intsz)
1589 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1590 else
1591 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1595 switch (func->RegI)
1597 case 0:
1598 if (func->CR == 1)
1599 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1600 break;
1601 case 1:
1602 if (func->CR == 1)
1603 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1604 else
1605 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1606 break;
1607 default:
1608 if (func->RegI % 2)
1610 if (func->CR == 1)
1611 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1612 else
1613 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1615 else if (func->CR == 1)
1616 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1618 for (i = func->RegI / 2 - 1; i >= 0; i--)
1619 if (i)
1620 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1621 else
1622 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1623 break;
1625 printf( " %04x: end\n", pos );
1628 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1630 const struct unwind_info_arm64 *info;
1631 const struct unwind_info_ext_arm64 *infoex;
1632 const struct unwind_info_epilog_arm64 *infoepi;
1633 const BYTE *ptr;
1634 unsigned int i, rva, codes, epilogs;
1636 if (func->Flag)
1638 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1639 func->BeginAddress + func->FunctionLength * 4 );
1640 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1641 func->FunctionLength, func->Flag, func->RegF, func->RegI,
1642 func->H, func->CR, func->FrameSize );
1643 dump_arm64_packed_info( func );
1644 return;
1647 rva = func->UnwindData;
1648 info = RVA( rva, sizeof(*info) );
1649 rva += sizeof(*info);
1650 epilogs = info->epilog;
1651 codes = info->codes;
1653 if (!codes)
1655 infoex = RVA( rva, sizeof(*infoex) );
1656 rva = rva + sizeof(*infoex);
1657 codes = infoex->codes;
1658 epilogs = infoex->epilog;
1660 printf( "\nFunction %08x-%08x:\n",
1661 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1662 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1663 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1664 if (info->e)
1666 printf( " epilog 0: code=%04x\n", info->epilog );
1668 else
1670 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1671 rva += sizeof(*infoepi) * epilogs;
1672 for (i = 0; i < epilogs; i++)
1673 printf( " epilog %u: pc=%08x code=%04x\n", i,
1674 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1676 ptr = RVA( rva, codes * 4);
1677 rva += codes * 4;
1678 if (info->x)
1680 const UINT *handler = RVA( rva, sizeof(*handler) );
1681 rva += sizeof(*handler);
1682 printf( " handler: %08x data %08x\n", *handler, rva );
1684 dump_arm64_codes( ptr, codes * 4 );
1687 static void dump_dir_exceptions(void)
1689 unsigned int i, size;
1690 const void *funcs;
1691 const IMAGE_FILE_HEADER *file_header;
1693 funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1694 if (!funcs) return;
1696 file_header = &PE_nt_headers->FileHeader;
1698 switch (file_header->Machine)
1700 case IMAGE_FILE_MACHINE_AMD64:
1701 size /= sizeof(struct runtime_function_x86_64);
1702 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1703 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1704 break;
1705 case IMAGE_FILE_MACHINE_ARMNT:
1706 size /= sizeof(struct runtime_function_armnt);
1707 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1708 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1709 break;
1710 case IMAGE_FILE_MACHINE_ARM64:
1711 size /= sizeof(struct runtime_function_arm64);
1712 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1713 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1714 break;
1715 default:
1716 printf( "Exception information not supported for %s binaries\n",
1717 get_machine_str(file_header->Machine));
1718 break;
1720 printf( "\n" );
1724 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
1726 /* FIXME: This does not properly handle large images */
1727 const IMAGE_IMPORT_BY_NAME* iibn;
1728 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1730 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1731 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
1732 else
1734 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1735 if (!iibn)
1736 printf("Can't grab import by name info, skipping to next ordinal\n");
1737 else
1738 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1743 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
1745 const IMAGE_IMPORT_BY_NAME* iibn;
1746 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
1748 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1749 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
1750 else
1752 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1753 if (!iibn)
1754 printf("Can't grab import by name info, skipping to next ordinal\n");
1755 else
1756 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1761 static void dump_dir_imported_functions(void)
1763 unsigned directorySize;
1764 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1766 if (!importDesc) return;
1768 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1770 for (;;)
1772 const IMAGE_THUNK_DATA32* il;
1774 if (!importDesc->Name || !importDesc->FirstThunk) break;
1776 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1777 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
1778 printf(" TimeDateStamp: %08X (%s)\n",
1779 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1780 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
1781 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
1783 printf(" Thunk Ordn Name\n");
1785 il = (importDesc->OriginalFirstThunk != 0) ?
1786 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
1787 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1789 if (!il)
1790 printf("Can't grab thunk data, going to next imported DLL\n");
1791 else
1793 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1794 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1795 else
1796 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1797 printf("\n");
1799 importDesc++;
1801 printf("\n");
1804 static void dump_hybrid_metadata(void)
1806 unsigned int i;
1807 const void *metadata = get_hybrid_metadata();
1809 if (!metadata) return;
1810 printf( "Hybrid metadata\n" );
1812 switch (PE_nt_headers->FileHeader.Machine)
1814 case IMAGE_FILE_MACHINE_I386:
1816 const IMAGE_CHPE_METADATA_X86 *data = metadata;
1818 printf( " Version %#x\n", (int)data->Version );
1819 printf( " CHPECodeAddressRangeOffset %#x\n", (int)data->CHPECodeAddressRangeOffset );
1820 printf( " CHPECodeAddressRangeCount %#x\n", (int)data->CHPECodeAddressRangeCount );
1821 printf( " WowA64ExceptionHandlerFunctionPointer %#x\n", (int)data->WowA64ExceptionHandlerFunctionPointer );
1822 printf( " WowA64DispatchCallFunctionPointer %#x\n", (int)data->WowA64DispatchCallFunctionPointer );
1823 printf( " WowA64DispatchIndirectCallFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallFunctionPointer );
1824 printf( " WowA64DispatchIndirectCallCfgFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallCfgFunctionPointer );
1825 printf( " WowA64DispatchRetFunctionPointer %#x\n", (int)data->WowA64DispatchRetFunctionPointer );
1826 printf( " WowA64DispatchRetLeafFunctionPointer %#x\n", (int)data->WowA64DispatchRetLeafFunctionPointer );
1827 printf( " WowA64DispatchJumpFunctionPointer %#x\n", (int)data->WowA64DispatchJumpFunctionPointer );
1828 if (data->Version >= 2)
1829 printf( " CompilerIATPointer %#x\n", (int)data->CompilerIATPointer );
1830 if (data->Version >= 3)
1831 printf( " WowA64RdtscFunctionPointer %#x\n", (int)data->WowA64RdtscFunctionPointer );
1832 if (data->Version >= 4)
1834 printf( " unknown[0] %#x\n", (int)data->unknown[0] );
1835 printf( " unknown[1] %#x\n", (int)data->unknown[1] );
1836 printf( " unknown[2] %#x\n", (int)data->unknown[2] );
1837 printf( " unknown[3] %#x\n", (int)data->unknown[3] );
1840 if (data->CHPECodeAddressRangeOffset)
1842 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CHPECodeAddressRangeOffset,
1843 data->CHPECodeAddressRangeCount * sizeof(*map) );
1845 printf( "\nCode ranges\n" );
1846 for (i = 0; i < data->CHPECodeAddressRangeCount; i++)
1848 static const char *types[] = { "x86", "ARM64" };
1849 unsigned int start = map[i].StartOffset & ~1;
1850 unsigned int type = map[i].StartOffset & 1;
1851 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
1855 break;
1858 case IMAGE_FILE_MACHINE_AMD64:
1859 case IMAGE_FILE_MACHINE_ARM64:
1861 const IMAGE_ARM64EC_METADATA *data = metadata;
1863 printf( " Version %#x\n", (int)data->Version );
1864 printf( " CodeMap %#x\n", (int)data->CodeMap );
1865 printf( " CodeMapCount %#x\n", (int)data->CodeMapCount );
1866 printf( " CodeRangesToEntryPoints %#x\n", (int)data->CodeRangesToEntryPoints );
1867 printf( " RedirectionMetadata %#x\n", (int)data->RedirectionMetadata );
1868 printf( " __os_arm64x_dispatch_call_no_redirect %#x\n", (int)data->__os_arm64x_dispatch_call_no_redirect );
1869 printf( " __os_arm64x_dispatch_ret %#x\n", (int)data->__os_arm64x_dispatch_ret );
1870 printf( " __os_arm64x_dispatch_call %#x\n", (int)data->__os_arm64x_dispatch_call );
1871 printf( " __os_arm64x_dispatch_icall %#x\n", (int)data->__os_arm64x_dispatch_icall );
1872 printf( " __os_arm64x_dispatch_icall_cfg %#x\n", (int)data->__os_arm64x_dispatch_icall_cfg );
1873 printf( " AlternateEntryPoint %#x\n", (int)data->AlternateEntryPoint );
1874 printf( " AuxiliaryIAT %#x\n", (int)data->AuxiliaryIAT );
1875 printf( " CodeRangesToEntryPointsCount %#x\n", (int)data->CodeRangesToEntryPointsCount );
1876 printf( " RedirectionMetadataCount %#x\n", (int)data->RedirectionMetadataCount );
1877 printf( " GetX64InformationFunctionPointer %#x\n", (int)data->GetX64InformationFunctionPointer );
1878 printf( " SetX64InformationFunctionPointer %#x\n", (int)data->SetX64InformationFunctionPointer );
1879 printf( " ExtraRFETable %#x\n", (int)data->ExtraRFETable );
1880 printf( " ExtraRFETableSize %#x\n", (int)data->ExtraRFETableSize );
1881 printf( " __os_arm64x_dispatch_fptr %#x\n", (int)data->__os_arm64x_dispatch_fptr );
1882 printf( " AuxiliaryIATCopy %#x\n", (int)data->AuxiliaryIATCopy );
1884 if (data->CodeMap)
1886 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CodeMap, data->CodeMapCount * sizeof(*map) );
1888 printf( "\nCode ranges\n" );
1889 for (i = 0; i < data->CodeMapCount; i++)
1891 static const char *types[] = { "ARM64", "ARM64EC", "x64", "??" };
1892 unsigned int start = map[i].StartOffset & ~0x3;
1893 unsigned int type = map[i].StartOffset & 0x3;
1894 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
1898 if (PE_nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_ARM64) break;
1900 if (data->CodeRangesToEntryPoints)
1902 const IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT *map = RVA( data->CodeRangesToEntryPoints,
1903 data->CodeRangesToEntryPointsCount * sizeof(*map) );
1905 printf( "\nCode ranges to entry points\n" );
1906 printf( " Start - End Entry point\n" );
1907 for (i = 0; i < data->CodeRangesToEntryPointsCount; i++)
1909 const char *name = find_export_from_rva( map[i].EntryPoint );
1910 printf( " %08x - %08x %08x",
1911 (int)map[i].StartRva, (int)map[i].EndRva, (int)map[i].EntryPoint );
1912 if (name) printf( " %s", name );
1913 printf( "\n" );
1917 if (data->RedirectionMetadata)
1919 const IMAGE_ARM64EC_REDIRECTION_ENTRY *map = RVA( data->RedirectionMetadata,
1920 data->RedirectionMetadataCount * sizeof(*map) );
1922 printf( "\nEntry point redirection\n" );
1923 for (i = 0; i < data->RedirectionMetadataCount; i++)
1925 const char *name = find_export_from_rva( map[i].Source );
1926 printf( " %08x -> %08x", (int)map[i].Source, (int)map[i].Destination );
1927 if (name) printf( " (%s)", name );
1928 printf( "\n" );
1931 break;
1934 printf( "\n" );
1937 static void dump_dir_loadconfig(void)
1939 unsigned int size;
1940 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32;
1942 loadcfg32 = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
1943 if (!loadcfg32) return;
1944 size = min( size, loadcfg32->Size );
1946 printf( "Loadconfig\n" );
1947 print_dword( "Size", loadcfg32->Size );
1948 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1949 print_word( "MajorVersion", loadcfg32->MajorVersion );
1950 print_word( "MinorVersion", loadcfg32->MinorVersion );
1951 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1952 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1953 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1955 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1957 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void *)loadcfg32;
1959 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1960 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1961 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1962 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1963 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1964 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1965 print_word( "CSDVersion", loadcfg64->CSDVersion );
1966 print_word( "DependentLoadFlags", loadcfg64->DependentLoadFlags );
1967 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1968 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, SEHandlerTable )) goto done;
1969 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1970 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1971 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardCFCheckFunctionPointer )) goto done;
1972 print_longlong( "GuardCFCheckFunctionPointer", loadcfg64->GuardCFCheckFunctionPointer );
1973 print_longlong( "GuardCFDispatchFunctionPointer", loadcfg64->GuardCFDispatchFunctionPointer );
1974 print_longlong( "GuardCFFunctionTable", loadcfg64->GuardCFFunctionTable );
1975 print_longlong( "GuardCFFunctionCount", loadcfg64->GuardCFFunctionCount );
1976 print_dword( "GuardFlags", loadcfg64->GuardFlags );
1977 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CodeIntegrity )) goto done;
1978 print_word( "CodeIntegrity.Flags", loadcfg64->CodeIntegrity.Flags );
1979 print_word( "CodeIntegrity.Catalog", loadcfg64->CodeIntegrity.Catalog );
1980 print_dword( "CodeIntegrity.CatalogOffset", loadcfg64->CodeIntegrity.CatalogOffset );
1981 print_dword( "CodeIntegrity.Reserved", loadcfg64->CodeIntegrity.Reserved );
1982 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardAddressTakenIatEntryTable )) goto done;
1983 print_longlong( "GuardAddressTakenIatEntryTable", loadcfg64->GuardAddressTakenIatEntryTable );
1984 print_longlong( "GuardAddressTakenIatEntryCount", loadcfg64->GuardAddressTakenIatEntryCount );
1985 print_longlong( "GuardLongJumpTargetTable", loadcfg64->GuardLongJumpTargetTable );
1986 print_longlong( "GuardLongJumpTargetCount", loadcfg64->GuardLongJumpTargetCount );
1987 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTable )) goto done;
1988 print_longlong( "DynamicValueRelocTable", loadcfg64->DynamicValueRelocTable );
1989 print_longlong( "CHPEMetadataPointer", loadcfg64->CHPEMetadataPointer );
1990 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFFailureRoutine )) goto done;
1991 print_longlong( "GuardRFFailureRoutine", loadcfg64->GuardRFFailureRoutine );
1992 print_longlong( "GuardRFFailureRoutineFuncPtr", loadcfg64->GuardRFFailureRoutineFunctionPointer );
1993 print_dword( "DynamicValueRelocTableOffset", loadcfg64->DynamicValueRelocTableOffset );
1994 print_word( "DynamicValueRelocTableSection",loadcfg64->DynamicValueRelocTableSection );
1995 print_word( "Reserved2", loadcfg64->Reserved2 );
1996 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFVerifyStackPointerFunctionPointer )) goto done;
1997 print_longlong( "GuardRFVerifyStackPointerFuncPtr", loadcfg64->GuardRFVerifyStackPointerFunctionPointer );
1998 print_dword( "HotPatchTableOffset", loadcfg64->HotPatchTableOffset );
1999 print_dword( "Reserved3", loadcfg64->Reserved3 );
2000 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, EnclaveConfigurationPointer )) goto done;
2001 print_longlong( "EnclaveConfigurationPointer", loadcfg64->EnclaveConfigurationPointer );
2002 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, VolatileMetadataPointer )) goto done;
2003 print_longlong( "VolatileMetadataPointer", loadcfg64->VolatileMetadataPointer );
2004 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardEHContinuationTable )) goto done;
2005 print_longlong( "GuardEHContinuationTable", loadcfg64->GuardEHContinuationTable );
2006 print_longlong( "GuardEHContinuationCount", loadcfg64->GuardEHContinuationCount );
2007 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardXFGCheckFunctionPointer )) goto done;
2008 print_longlong( "GuardXFGCheckFunctionPointer", loadcfg64->GuardXFGCheckFunctionPointer );
2009 print_longlong( "GuardXFGDispatchFunctionPointer", loadcfg64->GuardXFGDispatchFunctionPointer );
2010 print_longlong( "GuardXFGTableDispatchFuncPtr", loadcfg64->GuardXFGTableDispatchFunctionPointer );
2011 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CastGuardOsDeterminedFailureMode )) goto done;
2012 print_longlong( "CastGuardOsDeterminedFailureMode", loadcfg64->CastGuardOsDeterminedFailureMode );
2013 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardMemcpyFunctionPointer )) goto done;
2014 print_longlong( "GuardMemcpyFunctionPointer", loadcfg64->GuardMemcpyFunctionPointer );
2016 else
2018 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
2019 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
2020 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
2021 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
2022 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
2023 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
2024 print_word( "CSDVersion", loadcfg32->CSDVersion );
2025 print_word( "DependentLoadFlags", loadcfg32->DependentLoadFlags );
2026 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
2027 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, SEHandlerTable )) goto done;
2028 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
2029 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
2030 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardCFCheckFunctionPointer )) goto done;
2031 print_dword( "GuardCFCheckFunctionPointer", loadcfg32->GuardCFCheckFunctionPointer );
2032 print_dword( "GuardCFDispatchFunctionPointer", loadcfg32->GuardCFDispatchFunctionPointer );
2033 print_dword( "GuardCFFunctionTable", loadcfg32->GuardCFFunctionTable );
2034 print_dword( "GuardCFFunctionCount", loadcfg32->GuardCFFunctionCount );
2035 print_dword( "GuardFlags", loadcfg32->GuardFlags );
2036 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CodeIntegrity )) goto done;
2037 print_word( "CodeIntegrity.Flags", loadcfg32->CodeIntegrity.Flags );
2038 print_word( "CodeIntegrity.Catalog", loadcfg32->CodeIntegrity.Catalog );
2039 print_dword( "CodeIntegrity.CatalogOffset", loadcfg32->CodeIntegrity.CatalogOffset );
2040 print_dword( "CodeIntegrity.Reserved", loadcfg32->CodeIntegrity.Reserved );
2041 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardAddressTakenIatEntryTable )) goto done;
2042 print_dword( "GuardAddressTakenIatEntryTable", loadcfg32->GuardAddressTakenIatEntryTable );
2043 print_dword( "GuardAddressTakenIatEntryCount", loadcfg32->GuardAddressTakenIatEntryCount );
2044 print_dword( "GuardLongJumpTargetTable", loadcfg32->GuardLongJumpTargetTable );
2045 print_dword( "GuardLongJumpTargetCount", loadcfg32->GuardLongJumpTargetCount );
2046 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTable )) goto done;
2047 print_dword( "DynamicValueRelocTable", loadcfg32->DynamicValueRelocTable );
2048 print_dword( "CHPEMetadataPointer", loadcfg32->CHPEMetadataPointer );
2049 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFFailureRoutine )) goto done;
2050 print_dword( "GuardRFFailureRoutine", loadcfg32->GuardRFFailureRoutine );
2051 print_dword( "GuardRFFailureRoutineFuncPtr", loadcfg32->GuardRFFailureRoutineFunctionPointer );
2052 print_dword( "DynamicValueRelocTableOffset", loadcfg32->DynamicValueRelocTableOffset );
2053 print_word( "DynamicValueRelocTableSection", loadcfg32->DynamicValueRelocTableSection );
2054 print_word( "Reserved2", loadcfg32->Reserved2 );
2055 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2056 print_dword( "GuardRFVerifyStackPointerFuncPtr", loadcfg32->GuardRFVerifyStackPointerFunctionPointer );
2057 print_dword( "HotPatchTableOffset", loadcfg32->HotPatchTableOffset );
2058 print_dword( "Reserved3", loadcfg32->Reserved3 );
2059 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, EnclaveConfigurationPointer )) goto done;
2060 print_dword( "EnclaveConfigurationPointer", loadcfg32->EnclaveConfigurationPointer );
2061 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, VolatileMetadataPointer )) goto done;
2062 print_dword( "VolatileMetadataPointer", loadcfg32->VolatileMetadataPointer );
2063 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardEHContinuationTable )) goto done;
2064 print_dword( "GuardEHContinuationTable", loadcfg32->GuardEHContinuationTable );
2065 print_dword( "GuardEHContinuationCount", loadcfg32->GuardEHContinuationCount );
2066 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardXFGCheckFunctionPointer )) goto done;
2067 print_dword( "GuardXFGCheckFunctionPointer", loadcfg32->GuardXFGCheckFunctionPointer );
2068 print_dword( "GuardXFGDispatchFunctionPointer", loadcfg32->GuardXFGDispatchFunctionPointer );
2069 print_dword( "GuardXFGTableDispatchFuncPtr", loadcfg32->GuardXFGTableDispatchFunctionPointer );
2070 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CastGuardOsDeterminedFailureMode )) goto done;
2071 print_dword( "CastGuardOsDeterminedFailureMode", loadcfg32->CastGuardOsDeterminedFailureMode );
2072 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardMemcpyFunctionPointer )) goto done;
2073 print_dword( "GuardMemcpyFunctionPointer", loadcfg32->GuardMemcpyFunctionPointer );
2075 done:
2076 printf( "\n" );
2077 dump_hybrid_metadata();
2080 static void dump_dir_delay_imported_functions(void)
2082 unsigned directorySize;
2083 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
2085 if (!importDesc) return;
2087 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
2089 for (;;)
2091 const IMAGE_THUNK_DATA32* il;
2092 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
2094 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
2096 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
2097 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
2098 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
2099 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
2100 printf(" TimeDateStamp: %08X (%s)\n",
2101 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2103 printf(" Thunk Ordn Name\n");
2105 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
2107 if (!il)
2108 printf("Can't grab thunk data, going to next imported DLL\n");
2109 else
2111 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2112 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
2113 else
2114 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
2115 printf("\n");
2117 importDesc++;
2119 printf("\n");
2122 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
2124 const char* str;
2126 printf("Directory %02u\n", idx + 1);
2127 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
2128 printf(" TimeDateStamp: %08X %s\n",
2129 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
2130 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
2131 switch (idd->Type)
2133 default:
2134 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
2135 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
2136 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
2137 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
2138 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
2139 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
2140 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
2141 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
2142 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
2143 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
2144 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
2145 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
2146 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
2147 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
2148 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
2149 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
2150 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
2152 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
2153 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
2154 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
2155 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
2157 switch (idd->Type)
2159 case IMAGE_DEBUG_TYPE_UNKNOWN:
2160 break;
2161 case IMAGE_DEBUG_TYPE_COFF:
2162 dump_coff(idd->PointerToRawData, idd->SizeOfData,
2163 IMAGE_FIRST_SECTION(PE_nt_headers));
2164 break;
2165 case IMAGE_DEBUG_TYPE_CODEVIEW:
2166 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
2167 break;
2168 case IMAGE_DEBUG_TYPE_FPO:
2169 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
2170 break;
2171 case IMAGE_DEBUG_TYPE_MISC:
2173 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
2174 if (!misc) {printf("Can't get misc debug information\n"); break;}
2175 printf(" DataType: %u (%s)\n",
2176 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
2177 printf(" Length: %u\n", (UINT)misc->Length);
2178 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
2179 printf(" Data: %s\n", misc->Data);
2181 break;
2182 default: break;
2184 printf("\n");
2187 static void dump_dir_debug(void)
2189 unsigned nb_dbg, i;
2190 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
2192 nb_dbg /= sizeof(*debugDir);
2193 if (!debugDir || !nb_dbg) return;
2195 printf("Debug Table (%u directories)\n", nb_dbg);
2197 for (i = 0; i < nb_dbg; i++)
2199 dump_dir_debug_dir(debugDir, i);
2200 debugDir++;
2202 printf("\n");
2205 static inline void print_clrflags(const char *title, UINT value)
2207 printf(" %-34s 0x%X\n", title, value);
2208 #define X(f,s) if (value & f) printf(" %s\n", s)
2209 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
2210 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
2211 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
2212 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
2213 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
2214 #undef X
2217 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
2219 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
2222 static void dump_dir_clr_header(void)
2224 unsigned int size = 0;
2225 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
2227 if (!dir) return;
2229 printf( "CLR Header\n" );
2230 print_dword( "Header Size", dir->cb );
2231 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
2232 print_clrflags( "Flags", dir->Flags );
2233 print_dword( "EntryPointToken", dir->EntryPointToken );
2234 printf("\n");
2235 printf( "CLR Data Directory\n" );
2236 print_clrdirectory( "MetaData", &dir->MetaData );
2237 print_clrdirectory( "Resources", &dir->Resources );
2238 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
2239 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
2240 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
2241 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
2242 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
2243 printf("\n");
2246 static void dump_dynamic_relocs_arm64x( const IMAGE_BASE_RELOCATION *base_reloc, unsigned int size )
2248 unsigned int i;
2249 const IMAGE_BASE_RELOCATION *base_end = (const IMAGE_BASE_RELOCATION *)((const char *)base_reloc + size);
2251 printf( "Relocations ARM64X\n" );
2252 while (base_reloc < base_end - 1 && base_reloc->SizeOfBlock)
2254 const USHORT *rel = (const USHORT *)(base_reloc + 1);
2255 const USHORT *end = (const USHORT *)base_reloc + base_reloc->SizeOfBlock / sizeof(USHORT);
2256 printf( " Page %x\n", (UINT)base_reloc->VirtualAddress );
2257 while (rel < end && *rel)
2259 USHORT offset = *rel & 0xfff;
2260 USHORT type = (*rel >> 12) & 3;
2261 USHORT arg = *rel >> 14;
2262 rel++;
2263 switch (type)
2265 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2266 printf( " off %04x zero-fill %u bytes\n", offset, 1 << arg );
2267 break;
2268 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2269 printf( " off %04x set %u bytes value ", offset, 1 << arg );
2270 for (i = (1 << arg ) / sizeof(USHORT); i > 0; i--) printf( "%04x", rel[i - 1] );
2271 rel += (1 << arg) / sizeof(USHORT);
2272 printf( "\n" );
2273 break;
2274 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2275 printf( " off %04x add offset ", offset );
2276 if (arg & 1) printf( "-" );
2277 printf( "%08x\n", (UINT)*rel++ * ((arg & 2) ? 8 : 4) );
2278 break;
2279 default:
2280 printf( " off %04x unknown (arg %x)\n", offset, arg );
2281 break;
2284 base_reloc = (const IMAGE_BASE_RELOCATION *)end;
2288 static void dump_dynamic_relocs( const char *ptr, unsigned int size, ULONGLONG symbol )
2290 switch (symbol)
2292 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE:
2293 printf( "Relocations GUARD_RF_PROLOGUE\n" );
2294 break;
2295 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE:
2296 printf( "Relocations GUARD_RF_EPILOGUE\n" );
2297 break;
2298 case IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER:
2299 printf( "Relocations GUARD_IMPORT_CONTROL_TRANSFER\n" );
2300 break;
2301 case IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER:
2302 printf( "Relocations GUARD_INDIR_CONTROL_TRANSFER\n" );
2303 break;
2304 case IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH:
2305 printf( "Relocations GUARD_SWITCHTABLE_BRANCH\n" );
2306 break;
2307 case IMAGE_DYNAMIC_RELOCATION_ARM64X:
2308 dump_dynamic_relocs_arm64x( (const IMAGE_BASE_RELOCATION *)ptr, size );
2309 break;
2310 default:
2311 printf( "Unknown relocation symbol %s\n", longlong_str(symbol) );
2312 break;
2316 static const IMAGE_DYNAMIC_RELOCATION_TABLE *get_dyn_reloc_table(void)
2318 unsigned int size, section, offset;
2319 const IMAGE_SECTION_HEADER *sec;
2320 const IMAGE_DYNAMIC_RELOCATION_TABLE *table;
2322 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2324 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2325 if (!cfg) return NULL;
2326 size = min( size, cfg->Size );
2327 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTableSection )) return NULL;
2328 offset = cfg->DynamicValueRelocTableOffset;
2329 section = cfg->DynamicValueRelocTableSection;
2331 else
2333 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2334 if (!cfg) return NULL;
2335 size = min( size, cfg->Size );
2336 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTableSection )) return NULL;
2337 offset = cfg->DynamicValueRelocTableOffset;
2338 section = cfg->DynamicValueRelocTableSection;
2340 if (!section || section > PE_nt_headers->FileHeader.NumberOfSections) return NULL;
2341 sec = IMAGE_FIRST_SECTION( PE_nt_headers ) + section - 1;
2342 if (offset >= sec->SizeOfRawData) return NULL;
2343 return PRD( sec->PointerToRawData + offset, sizeof(*table) );
2346 static void dump_dir_dynamic_reloc(void)
2348 const char *ptr, *end;
2349 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2351 if (!table) return;
2353 printf( "Dynamic relocations (version %u)\n\n", (UINT)table->Version );
2354 ptr = (const char *)(table + 1);
2355 end = ptr + table->Size;
2356 while (ptr < end)
2358 switch (table->Version)
2360 case 1:
2361 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2363 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2364 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2365 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2367 else
2369 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2370 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2371 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2373 break;
2374 case 2:
2375 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2377 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2378 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2379 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2381 else
2383 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2384 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2385 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2387 break;
2390 printf( "\n" );
2393 static const IMAGE_BASE_RELOCATION *get_armx_relocs( unsigned int *size )
2395 const char *ptr, *end;
2396 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2398 if (!table) return NULL;
2399 ptr = (const char *)(table + 1);
2400 end = ptr + table->Size;
2401 while (ptr < end)
2403 switch (table->Version)
2405 case 1:
2406 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2408 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2409 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2411 *size = reloc->BaseRelocSize;
2412 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2414 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2416 else
2418 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2419 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2421 *size = reloc->BaseRelocSize;
2422 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2424 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2426 break;
2427 case 2:
2428 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2430 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2431 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2433 *size = reloc->FixupInfoSize;
2434 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2436 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2438 else
2440 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2441 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2443 *size = reloc->FixupInfoSize;
2444 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2446 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2448 break;
2451 return NULL;
2454 static BOOL get_alt_header( void )
2456 unsigned int size;
2457 const IMAGE_BASE_RELOCATION *end, *reloc = get_armx_relocs( &size );
2459 if (!reloc) return FALSE;
2460 end = (const IMAGE_BASE_RELOCATION *)((const char *)reloc + size);
2462 while (reloc < end - 1 && reloc->SizeOfBlock)
2464 const USHORT *rel = (const USHORT *)(reloc + 1);
2465 const USHORT *rel_end = (const USHORT *)reloc + reloc->SizeOfBlock / sizeof(USHORT);
2466 char *page = reloc->VirtualAddress ? (char *)RVA(reloc->VirtualAddress,1) : dump_base;
2468 while (rel < rel_end && *rel)
2470 USHORT offset = *rel & 0xfff;
2471 USHORT type = (*rel >> 12) & 3;
2472 USHORT arg = *rel >> 14;
2473 int val;
2474 rel++;
2475 switch (type)
2477 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2478 memset( page + offset, 0, 1 << arg );
2479 break;
2480 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2481 memcpy( page + offset, rel, 1 << arg );
2482 rel += (1 << arg) / sizeof(USHORT);
2483 break;
2484 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2485 val = (unsigned int)*rel++ * ((arg & 2) ? 8 : 4);
2486 if (arg & 1) val = -val;
2487 *(int *)(page + offset) += val;
2488 break;
2491 reloc = (const IMAGE_BASE_RELOCATION *)rel_end;
2493 return TRUE;
2496 static void dump_dir_reloc(void)
2498 unsigned int i, size = 0;
2499 const USHORT *relocs;
2500 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
2501 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
2502 static const char * const names[] =
2504 "BASED_ABSOLUTE",
2505 "BASED_HIGH",
2506 "BASED_LOW",
2507 "BASED_HIGHLOW",
2508 "BASED_HIGHADJ",
2509 "BASED_MIPS_JMPADDR",
2510 "BASED_SECTION",
2511 "BASED_REL",
2512 "unknown 8",
2513 "BASED_IA64_IMM64",
2514 "BASED_DIR64",
2515 "BASED_HIGH3ADJ",
2516 "unknown 12",
2517 "unknown 13",
2518 "unknown 14",
2519 "unknown 15"
2522 if (!rel) return;
2524 printf( "Relocations\n" );
2525 while (rel < end - 1 && rel->SizeOfBlock)
2527 printf( " Page %x\n", (UINT)rel->VirtualAddress );
2528 relocs = (const USHORT *)(rel + 1);
2529 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
2530 while (i--)
2532 USHORT offset = *relocs & 0xfff;
2533 int type = *relocs >> 12;
2534 printf( " off %04x type %s\n", offset, names[type] );
2535 relocs++;
2537 rel = (const IMAGE_BASE_RELOCATION *)relocs;
2539 printf("\n");
2542 static void dump_dir_tls(void)
2544 IMAGE_TLS_DIRECTORY64 dir;
2545 const UINT *callbacks;
2546 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
2548 if (!pdir) return;
2550 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2551 memcpy(&dir, pdir, sizeof(dir));
2552 else
2554 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
2555 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
2556 dir.AddressOfIndex = pdir->AddressOfIndex;
2557 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
2558 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
2559 dir.Characteristics = pdir->Characteristics;
2562 /* FIXME: This does not properly handle large images */
2563 printf( "Thread Local Storage\n" );
2564 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2565 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
2566 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2567 (UINT)dir.SizeOfZeroFill );
2568 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
2569 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
2570 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
2571 if (dir.AddressOfCallBacks)
2573 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2574 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
2576 printf( " %08x", *callbacks );
2577 addr += sizeof(UINT);
2580 printf(" }\n\n");
2583 enum FileSig get_kind_dbg(void)
2585 const WORD* pw;
2587 pw = PRD(0, sizeof(WORD));
2588 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2590 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2591 return SIG_UNKNOWN;
2594 void dbg_dump(void)
2596 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2597 unsigned nb_dbg;
2598 unsigned i;
2599 const IMAGE_DEBUG_DIRECTORY* debugDir;
2601 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2602 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2604 printf ("Signature: %.2s (0x%4X)\n",
2605 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2606 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2607 printf ("Machine: 0x%04X (%s)\n",
2608 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2609 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2610 printf ("TimeDateStamp: 0x%08X (%s)\n",
2611 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2612 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
2613 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
2614 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
2615 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
2616 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
2617 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
2619 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2620 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2621 {printf("Can't get the sections, aborting\n"); return;}
2623 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2625 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2626 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2627 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2628 separateDebugHead->ExportedNamesSize,
2629 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2630 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2632 printf("Debug Table (%u directories)\n", nb_dbg);
2634 for (i = 0; i < nb_dbg; i++)
2636 dump_dir_debug_dir(debugDir, i);
2637 debugDir++;
2641 static const char *get_resource_type( unsigned int id )
2643 static const char * const types[] =
2645 NULL,
2646 "CURSOR",
2647 "BITMAP",
2648 "ICON",
2649 "MENU",
2650 "DIALOG",
2651 "STRING",
2652 "FONTDIR",
2653 "FONT",
2654 "ACCELERATOR",
2655 "RCDATA",
2656 "MESSAGETABLE",
2657 "GROUP_CURSOR",
2658 NULL,
2659 "GROUP_ICON",
2660 NULL,
2661 "VERSION",
2662 "DLGINCLUDE",
2663 NULL,
2664 "PLUGPLAY",
2665 "VXD",
2666 "ANICURSOR",
2667 "ANIICON",
2668 "HTML",
2669 "RT_MANIFEST"
2672 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2673 return NULL;
2676 /* dump an ASCII string with proper escaping */
2677 static int dump_strA( const unsigned char *str, size_t len )
2679 static const char escapes[32] = ".......abtnvfr.............e....";
2680 char buffer[256];
2681 char *pos = buffer;
2682 int count = 0;
2684 for (; len; str++, len--)
2686 if (pos > buffer + sizeof(buffer) - 8)
2688 fwrite( buffer, pos - buffer, 1, stdout );
2689 count += pos - buffer;
2690 pos = buffer;
2692 if (*str > 127) /* hex escape */
2694 pos += sprintf( pos, "\\x%02x", *str );
2695 continue;
2697 if (*str < 32) /* octal or C escape */
2699 if (!*str && len == 1) continue; /* do not output terminating NULL */
2700 if (escapes[*str] != '.')
2701 pos += sprintf( pos, "\\%c", escapes[*str] );
2702 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2703 pos += sprintf( pos, "\\%03o", *str );
2704 else
2705 pos += sprintf( pos, "\\%o", *str );
2706 continue;
2708 if (*str == '\\') *pos++ = '\\';
2709 *pos++ = *str;
2711 fwrite( buffer, pos - buffer, 1, stdout );
2712 count += pos - buffer;
2713 return count;
2716 /* dump a Unicode string with proper escaping */
2717 static int dump_strW( const WCHAR *str, size_t len )
2719 static const char escapes[32] = ".......abtnvfr.............e....";
2720 char buffer[256];
2721 char *pos = buffer;
2722 int count = 0;
2724 for (; len; str++, len--)
2726 if (pos > buffer + sizeof(buffer) - 8)
2728 fwrite( buffer, pos - buffer, 1, stdout );
2729 count += pos - buffer;
2730 pos = buffer;
2732 if (*str > 127) /* hex escape */
2734 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2735 pos += sprintf( pos, "\\x%04x", *str );
2736 else
2737 pos += sprintf( pos, "\\x%x", *str );
2738 continue;
2740 if (*str < 32) /* octal or C escape */
2742 if (!*str && len == 1) continue; /* do not output terminating NULL */
2743 if (escapes[*str] != '.')
2744 pos += sprintf( pos, "\\%c", escapes[*str] );
2745 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2746 pos += sprintf( pos, "\\%03o", *str );
2747 else
2748 pos += sprintf( pos, "\\%o", *str );
2749 continue;
2751 if (*str == '\\') *pos++ = '\\';
2752 *pos++ = *str;
2754 fwrite( buffer, pos - buffer, 1, stdout );
2755 count += pos - buffer;
2756 return count;
2759 /* dump data for a STRING resource */
2760 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2762 int i;
2764 for (i = 0; i < 16 && size; i++)
2766 unsigned len = *ptr++;
2768 if (len >= size)
2770 len = size;
2771 size = 0;
2773 else size -= len + 1;
2775 if (len)
2777 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2778 dump_strW( ptr, len );
2779 printf( "\"\n" );
2780 ptr += len;
2785 /* dump data for a MESSAGETABLE resource */
2786 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2788 const MESSAGE_RESOURCE_DATA *data = ptr;
2789 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2790 unsigned i, j;
2792 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2794 const MESSAGE_RESOURCE_ENTRY *entry;
2796 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2797 for (j = block->LowId; j <= block->HighId; j++)
2799 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2801 const WCHAR *str = (const WCHAR *)entry->Text;
2802 printf( "%s%08x L\"", prefix, j );
2803 dump_strW( str, strlenW(str) );
2804 printf( "\"\n" );
2806 else
2808 const char *str = (const char *) entry->Text;
2809 printf( "%s%08x \"", prefix, j );
2810 dump_strA( entry->Text, strlen(str) );
2811 printf( "\"\n" );
2813 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2818 static void dump_dir_resource(void)
2820 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2821 const IMAGE_RESOURCE_DIRECTORY *namedir;
2822 const IMAGE_RESOURCE_DIRECTORY *langdir;
2823 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2824 const IMAGE_RESOURCE_DIR_STRING_U *string;
2825 const IMAGE_RESOURCE_DATA_ENTRY *data;
2826 int i, j, k;
2828 if (!root) return;
2830 printf( "Resources:" );
2832 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2834 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2835 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
2836 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2838 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2839 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
2840 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2842 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2844 printf( "\n " );
2845 if (e1->NameIsString)
2847 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
2848 dump_unicode_str( string->NameString, string->Length );
2850 else
2852 const char *type = get_resource_type( e1->Id );
2853 if (type) printf( "%s", type );
2854 else printf( "%04x", e1->Id );
2857 printf( " Name=" );
2858 if (e2->NameIsString)
2860 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
2861 dump_unicode_str( string->NameString, string->Length );
2863 else
2864 printf( "%04x", e2->Id );
2866 printf( " Language=%04x:\n", e3->Id );
2867 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
2868 if (e1->NameIsString)
2870 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2872 else switch(e1->Id)
2874 case 6:
2875 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2876 break;
2877 case 11:
2878 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2879 break;
2880 default:
2881 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2882 break;
2887 printf( "\n\n" );
2890 static void dump_debug(void)
2892 const char* stabs = NULL;
2893 unsigned szstabs = 0;
2894 const char* stabstr = NULL;
2895 unsigned szstr = 0;
2896 unsigned i;
2897 const IMAGE_SECTION_HEADER* sectHead;
2899 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2901 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2903 if (!strcmp((const char *)sectHead->Name, ".stab"))
2905 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2906 szstabs = sectHead->Misc.VirtualSize;
2908 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2910 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2911 szstr = sectHead->Misc.VirtualSize;
2914 if (stabs && stabstr)
2915 dump_stabs(stabs, szstabs, stabstr, szstr);
2918 static void dump_symbol_table(void)
2920 const IMAGE_SYMBOL* sym;
2921 int numsym;
2923 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2924 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2925 return;
2926 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2927 sizeof(*sym) * numsym);
2928 if (!sym) return;
2930 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2933 enum FileSig get_kind_exec(void)
2935 const WORD* pw;
2936 const DWORD* pdw;
2937 const IMAGE_DOS_HEADER* dh;
2939 pw = PRD(0, sizeof(WORD));
2940 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2942 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2944 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2946 /* the signature is the first DWORD */
2947 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2948 if (pdw)
2950 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2951 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2952 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2954 return SIG_DOS;
2956 return SIG_UNKNOWN;
2959 void pe_dump(void)
2961 int alt = 0;
2963 PE_nt_headers = get_nt_header();
2964 print_fake_dll();
2966 for (;;)
2968 if (alt)
2969 printf( "\n**** Alternate (%s) data ****\n\n",
2970 get_machine_str(PE_nt_headers->FileHeader.Machine));
2971 else if (get_dyn_reloc_table())
2972 printf( "**** Native (%s) data ****\n\n",
2973 get_machine_str(PE_nt_headers->FileHeader.Machine));
2975 if (globals.do_dumpheader)
2977 dump_pe_header();
2978 /* FIXME: should check ptr */
2979 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2980 PE_nt_headers->FileHeader.NumberOfSections);
2982 else if (!globals.dumpsect)
2984 /* show at least something here */
2985 dump_pe_header();
2988 if (globals_dump_sect("import"))
2990 dump_dir_imported_functions();
2991 dump_dir_delay_imported_functions();
2993 if (globals_dump_sect("export"))
2994 dump_dir_exported_functions();
2995 if (globals_dump_sect("debug"))
2996 dump_dir_debug();
2997 if (globals_dump_sect("resource"))
2998 dump_dir_resource();
2999 if (globals_dump_sect("tls"))
3000 dump_dir_tls();
3001 if (globals_dump_sect("loadcfg"))
3002 dump_dir_loadconfig();
3003 if (globals_dump_sect("clr"))
3004 dump_dir_clr_header();
3005 if (globals_dump_sect("reloc"))
3006 dump_dir_reloc();
3007 if (globals_dump_sect("dynreloc"))
3008 dump_dir_dynamic_reloc();
3009 if (globals_dump_sect("except"))
3010 dump_dir_exceptions();
3011 if (globals_dump_sect("apiset"))
3012 dump_section_apiset();
3014 if (globals.do_symbol_table)
3015 dump_symbol_table();
3016 if (globals.do_debug)
3017 dump_debug();
3018 if (alt++) break;
3019 if (!get_alt_header()) break;
3023 typedef struct _dll_symbol {
3024 size_t ordinal;
3025 char *symbol;
3026 } dll_symbol;
3028 static dll_symbol *dll_symbols = NULL;
3029 static dll_symbol *dll_current_symbol = NULL;
3031 /* Compare symbols by ordinal for qsort */
3032 static int symbol_cmp(const void *left, const void *right)
3034 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
3037 /*******************************************************************
3038 * dll_close
3040 * Free resources used by DLL
3042 /* FIXME: Not used yet
3043 static void dll_close (void)
3045 dll_symbol* ds;
3047 if (!dll_symbols) {
3048 fatal("No symbols");
3050 for (ds = dll_symbols; ds->symbol; ds++)
3051 free(ds->symbol);
3052 free (dll_symbols);
3053 dll_symbols = NULL;
3057 static void do_grab_sym( void )
3059 const IMAGE_EXPORT_DIRECTORY*exportDir;
3060 UINT i, j, *map;
3061 const UINT *pName;
3062 const UINT *pFunc;
3063 const WORD *pOrdl;
3064 const char *ptr;
3066 PE_nt_headers = get_nt_header();
3067 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
3069 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
3070 if (!pName) {printf("Can't grab functions' name table\n"); return;}
3071 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
3072 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
3073 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
3074 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
3076 /* dll_close(); */
3078 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
3080 /* bit map of used funcs */
3081 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
3082 if (!map) fatal("no memory");
3084 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
3086 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
3087 ptr = RVA(*pName++, sizeof(DWORD));
3088 if (!ptr) ptr = "cant_get_function";
3089 dll_symbols[j].symbol = xstrdup(ptr);
3090 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
3091 assert(dll_symbols[j].symbol);
3094 for (i = 0; i < exportDir->NumberOfFunctions; i++)
3096 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
3098 char ordinal_text[256];
3099 /* Ordinal only entry */
3100 sprintf (ordinal_text, "%s_%u",
3101 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
3102 (UINT)exportDir->Base + i);
3103 str_toupper(ordinal_text);
3104 dll_symbols[j].symbol = xstrdup(ordinal_text);
3105 assert(dll_symbols[j].symbol);
3106 dll_symbols[j].ordinal = exportDir->Base + i;
3107 j++;
3108 assert(j <= exportDir->NumberOfFunctions);
3111 free(map);
3113 if (NORMAL)
3114 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
3115 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
3116 j, (UINT)exportDir->Base);
3118 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
3120 dll_symbols[j].symbol = NULL;
3122 dll_current_symbol = dll_symbols;
3125 /*******************************************************************
3126 * dll_open
3128 * Open a DLL and read in exported symbols
3130 BOOL dll_open (const char *dll_name)
3132 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
3135 /*******************************************************************
3136 * dll_next_symbol
3138 * Get next exported symbol from dll
3140 BOOL dll_next_symbol (parsed_symbol * sym)
3142 if (!dll_current_symbol || !dll_current_symbol->symbol)
3143 return FALSE;
3144 assert (dll_symbols);
3145 sym->symbol = xstrdup (dll_current_symbol->symbol);
3146 sym->ordinal = dll_current_symbol->ordinal;
3147 dll_current_symbol++;
3148 return TRUE;