mfplat: Implement MFCreatePathFromURL().
[wine.git] / tools / winedump / pe.c
blobbf3aa6b4d697e4b11714034bc45c808f45c12d14
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";
63 return "???";
66 static const void* RVA(unsigned long rva, unsigned long len)
68 IMAGE_SECTION_HEADER* sectHead;
69 int i;
71 if (rva == 0) return NULL;
73 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
74 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
76 if (sectHead[i].VirtualAddress <= rva &&
77 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
79 /* return image import directory offset */
80 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
84 return NULL;
87 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
89 const IMAGE_DOS_HEADER *dos;
90 dos = PRD(0, sizeof(*dos));
91 if (!dos) return NULL;
92 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
93 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
94 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
97 void print_fake_dll( void )
99 const IMAGE_DOS_HEADER *dos;
101 dos = PRD(0, sizeof(*dos) + 32);
102 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
104 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
105 printf( "*** This is a Wine builtin DLL ***\n\n" );
106 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
107 printf( "*** This is a Wine fake DLL ***\n\n" );
111 static const void *get_data_dir(const IMAGE_NT_HEADERS32 *hdr, unsigned int idx, unsigned int *size)
113 if(hdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
115 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&hdr->OptionalHeader;
116 if (idx >= opt->NumberOfRvaAndSizes)
117 return NULL;
118 if(size)
119 *size = opt->DataDirectory[idx].Size;
120 return RVA(opt->DataDirectory[idx].VirtualAddress,
121 opt->DataDirectory[idx].Size);
123 else
125 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&hdr->OptionalHeader;
126 if (idx >= opt->NumberOfRvaAndSizes)
127 return NULL;
128 if(size)
129 *size = opt->DataDirectory[idx].Size;
130 return RVA(opt->DataDirectory[idx].VirtualAddress,
131 opt->DataDirectory[idx].Size);
135 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
137 return get_data_dir( PE_nt_headers, idx, size );
140 static const void* get_dir(unsigned idx)
142 return get_dir_and_size(idx, 0);
145 static const char * const DirectoryNames[16] = {
146 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
147 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
148 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
149 "IAT", "Delay IAT", "CLR Header", ""
152 static const char *get_magic_type(WORD magic)
154 switch(magic) {
155 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
156 return "32bit";
157 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
158 return "64bit";
159 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
160 return "ROM";
162 return "???";
165 static const void *get_hybrid_metadata(void)
167 unsigned int size;
169 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
171 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
172 if (!cfg) return 0;
173 size = min( size, cfg->Size );
174 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CHPEMetadataPointer )) return 0;
175 return RVA( cfg->CHPEMetadataPointer - ((const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader)->ImageBase, 1 );
177 else
179 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
180 if (!cfg) return 0;
181 size = min( size, cfg->Size );
182 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CHPEMetadataPointer )) return 0;
183 return RVA( cfg->CHPEMetadataPointer - PE_nt_headers->OptionalHeader.ImageBase, 1 );
187 static inline const char *longlong_str( ULONGLONG value )
189 static char buffer[20];
191 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
192 sprintf(buffer, "%lx%08lx", (unsigned long)(value >> 32), (unsigned long)value);
193 else
194 sprintf(buffer, "%lx", (unsigned long)value);
195 return buffer;
198 static inline void print_word(const char *title, WORD value)
200 printf(" %-34s 0x%-4X %u\n", title, value, value);
203 static inline void print_dword(const char *title, UINT value)
205 printf(" %-34s 0x%-8x %u\n", title, value, value);
208 static inline void print_longlong(const char *title, ULONGLONG value)
210 printf(" %-34s 0x%s\n", title, longlong_str(value));
213 static inline void print_ver(const char *title, BYTE major, BYTE minor)
215 printf(" %-34s %u.%02u\n", title, major, minor);
218 static inline void print_subsys(const char *title, WORD value)
220 const char *str;
221 switch (value)
223 default:
224 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
225 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
226 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
227 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
228 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
229 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
230 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
231 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
232 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
233 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
234 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
235 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
236 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
237 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
239 printf(" %-34s 0x%X (%s)\n", title, value, str);
242 static inline void print_dllflags(const char *title, WORD value)
244 printf(" %-34s 0x%04X\n", title, value);
245 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
246 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
247 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
248 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
249 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
250 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
251 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
252 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
253 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
254 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
255 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
256 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
257 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
258 #undef X
261 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
263 unsigned i;
264 printf("Data Directory\n");
266 for (i = 0; i < n && i < 16; i++)
268 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
269 DirectoryNames[i], (UINT)directory[i].VirtualAddress,
270 (UINT)directory[i].Size);
274 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
276 IMAGE_OPTIONAL_HEADER32 oh;
277 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
279 /* in case optional header is missing or partial */
280 memset(&oh, 0, sizeof(oh));
281 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
282 optionalHeader = &oh;
284 print_word("Magic", optionalHeader->Magic);
285 print_ver("linker version",
286 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
287 print_dword("size of code", optionalHeader->SizeOfCode);
288 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
289 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
290 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
291 print_dword("base of code", optionalHeader->BaseOfCode);
292 print_dword("base of data", optionalHeader->BaseOfData);
293 print_dword("image base", optionalHeader->ImageBase);
294 print_dword("section align", optionalHeader->SectionAlignment);
295 print_dword("file align", optionalHeader->FileAlignment);
296 print_ver("required OS version",
297 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
298 print_ver("image version",
299 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
300 print_ver("subsystem version",
301 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
302 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
303 print_dword("size of image", optionalHeader->SizeOfImage);
304 print_dword("size of headers", optionalHeader->SizeOfHeaders);
305 print_dword("checksum", optionalHeader->CheckSum);
306 print_subsys("Subsystem", optionalHeader->Subsystem);
307 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
308 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
309 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
310 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
311 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
312 print_dword("loader flags", optionalHeader->LoaderFlags);
313 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
314 printf("\n");
315 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
316 printf("\n");
319 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
321 IMAGE_OPTIONAL_HEADER64 oh;
322 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
324 /* in case optional header is missing or partial */
325 memset(&oh, 0, sizeof(oh));
326 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
327 optionalHeader = &oh;
329 print_word("Magic", optionalHeader->Magic);
330 print_ver("linker version",
331 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
332 print_dword("size of code", optionalHeader->SizeOfCode);
333 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
334 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
335 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
336 print_dword("base of code", optionalHeader->BaseOfCode);
337 print_longlong("image base", optionalHeader->ImageBase);
338 print_dword("section align", optionalHeader->SectionAlignment);
339 print_dword("file align", optionalHeader->FileAlignment);
340 print_ver("required OS version",
341 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
342 print_ver("image version",
343 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
344 print_ver("subsystem version",
345 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
346 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
347 print_dword("size of image", optionalHeader->SizeOfImage);
348 print_dword("size of headers", optionalHeader->SizeOfHeaders);
349 print_dword("checksum", optionalHeader->CheckSum);
350 print_subsys("Subsystem", optionalHeader->Subsystem);
351 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
352 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
353 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
354 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
355 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
356 print_dword("loader flags", optionalHeader->LoaderFlags);
357 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
358 printf("\n");
359 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
360 printf("\n");
363 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
365 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
367 switch(optionalHeader->Magic) {
368 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
369 dump_optional_header32(optionalHeader, header_size);
370 break;
371 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
372 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
373 break;
374 default:
375 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
376 break;
380 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader, BOOL is_hybrid)
382 const char *name = get_machine_str(fileHeader->Machine);
384 printf("File Header\n");
386 if (is_hybrid)
388 switch (fileHeader->Machine)
390 case IMAGE_FILE_MACHINE_I386: name = "CHPE"; break;
391 case IMAGE_FILE_MACHINE_AMD64: name = "ARM64EC"; break;
392 case IMAGE_FILE_MACHINE_ARM64: name = "ARM64X"; break;
395 printf(" Machine: %04X (%s)\n", fileHeader->Machine, name);
396 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
397 printf(" TimeDateStamp: %08X (%s)\n",
398 (UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp));
399 printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
400 printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
401 printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
402 printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
403 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
404 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
405 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
406 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
407 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
408 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
409 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
410 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
411 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
412 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
413 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
414 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
415 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
416 X(IMAGE_FILE_SYSTEM, "SYSTEM");
417 X(IMAGE_FILE_DLL, "DLL");
418 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
419 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
420 #undef X
421 printf("\n");
424 static void dump_pe_header(void)
426 dump_file_header(&PE_nt_headers->FileHeader, get_hybrid_metadata() != NULL);
427 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader,
428 PE_nt_headers->FileHeader.SizeOfOptionalHeader);
431 void dump_section_characteristics(DWORD characteristics, const char* sep)
433 #define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
434 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
435 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
436 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
437 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
438 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
439 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
441 X(IMAGE_SCN_CNT_CODE, "CODE");
442 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
443 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
445 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
446 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
447 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
448 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
449 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
451 /* 0x00002000 - Reserved */
452 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
453 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
455 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
456 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
457 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
458 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
459 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
461 switch (characteristics & IMAGE_SCN_ALIGN_MASK)
463 #define X2(b,s) case b: printf("%s%s", sep, s); break
464 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
465 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
466 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
467 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
468 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
469 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
470 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
471 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
472 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
473 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
474 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
475 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
476 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
477 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
478 #undef X2
481 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
483 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
484 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
485 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
486 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
487 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
488 X(IMAGE_SCN_MEM_READ, "MEM_READ");
489 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
490 #undef X
493 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
495 unsigned offset;
497 /* long section name ? */
498 if (strtable && sectHead->Name[0] == '/' &&
499 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
500 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
501 else
502 printf(" %-8.8s", sectHead->Name);
503 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
504 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
505 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
506 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
507 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
508 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
509 printf(" line # offs: %-8u line #'s: %-8u\n",
510 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
511 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
512 printf(" ");
513 dump_section_characteristics(sectHead->Characteristics, " ");
515 printf("\n\n");
518 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
520 const IMAGE_SECTION_HEADER* sectHead = addr;
521 unsigned i;
522 const char* strtable;
524 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
526 strtable = (const char*)base +
527 PE_nt_headers->FileHeader.PointerToSymbolTable +
528 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
530 else strtable = NULL;
532 printf("Section Table\n");
533 for (i = 0; i < num_sect; i++, sectHead++)
535 dump_section(sectHead, strtable);
537 if (globals.do_dump_rawdata)
539 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
540 printf("\n");
545 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
547 const WCHAR *wstr = PRD( rva, len );
548 char *ret = buffer;
550 len /= sizeof(WCHAR);
551 while (len--) *buffer++ = *wstr++;
552 *buffer = 0;
553 return ret;
556 static void dump_section_apiset(void)
558 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
559 const UINT *ptr, *entry, *value, *hash;
560 unsigned int i, j, count, val_count, rva;
561 char buffer[128];
563 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
565 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
566 rva = sect->PointerToRawData;
567 ptr = PRD( rva, sizeof(*ptr) );
568 printf( "ApiSet section:\n" );
569 switch (ptr[0]) /* version */
571 case 2:
572 printf( " Version: %u\n", ptr[0] );
573 printf( " Count: %08x\n", ptr[1] );
574 count = ptr[1];
575 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
576 for (i = 0; i < count; i++, entry += 3)
578 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
579 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
580 val_count = *value++;
581 for (j = 0; j < val_count; j++, value += 4)
583 putchar( ' ' );
584 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
585 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
587 printf( "\n");
589 break;
590 case 4:
591 printf( " Version: %u\n", ptr[0] );
592 printf( " Size: %08x\n", ptr[1] );
593 printf( " Flags: %08x\n", ptr[2] );
594 printf( " Count: %08x\n", ptr[3] );
595 count = ptr[3];
596 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
597 for (i = 0; i < count; i++, entry += 6)
599 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
600 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
601 value++; /* flags */
602 val_count = *value++;
603 for (j = 0; j < val_count; j++, value += 5)
605 putchar( ' ' );
606 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
607 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
609 printf( "\n");
611 break;
612 case 6:
613 printf( " Version: %u\n", ptr[0] );
614 printf( " Size: %08x\n", ptr[1] );
615 printf( " Flags: %08x\n", ptr[2] );
616 printf( " Count: %08x\n", ptr[3] );
617 printf( " EntryOffset: %08x\n", ptr[4] );
618 printf( " HashOffset: %08x\n", ptr[5] );
619 printf( " HashFactor: %08x\n", ptr[6] );
620 count = ptr[3];
621 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
622 for (i = 0; i < count; i++, entry += 6)
624 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
625 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
626 for (j = 0; j < entry[5]; j++, value += 5)
628 putchar( ' ' );
629 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
630 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
632 printf( "\n" );
634 printf( " Hash table:\n" );
635 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
636 for (i = 0; i < count; i++, hash += 2)
638 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
639 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
641 break;
642 default:
643 printf( "*** Unknown version %u\n", ptr[0] );
644 break;
646 break;
650 static const char *find_export_from_rva( UINT rva )
652 UINT i, *func_names;
653 const UINT *funcs;
654 const UINT *names;
655 const WORD *ordinals;
656 const IMAGE_EXPORT_DIRECTORY *dir;
657 const char *ret = NULL;
659 if (!(dir = get_dir( IMAGE_FILE_EXPORT_DIRECTORY ))) return NULL;
660 if (!(funcs = RVA( dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD) ))) return NULL;
661 names = RVA( dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD) );
662 ordinals = RVA( dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD) );
663 func_names = calloc( dir->NumberOfFunctions, sizeof(*func_names) );
665 for (i = 0; i < dir->NumberOfNames; i++) func_names[ordinals[i]] = names[i];
666 for (i = 0; i < dir->NumberOfFunctions && !ret; i++)
667 if (funcs[i] == rva) ret = get_symbol_str( RVA( func_names[i], sizeof(DWORD) ));
669 free( func_names );
670 if (!ret && rva == PE_nt_headers->OptionalHeader.AddressOfEntryPoint) return "<EntryPoint>";
671 return ret;
674 static void dump_dir_exported_functions(void)
676 unsigned int size;
677 const IMAGE_EXPORT_DIRECTORY *dir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
678 UINT i, *funcs;
679 const UINT *pFunc;
680 const UINT *pName;
681 const WORD *pOrdl;
683 if (!dir) return;
685 printf("\n");
686 printf(" Name: %s\n", (const char*)RVA(dir->Name, sizeof(DWORD)));
687 printf(" Characteristics: %08x\n", (UINT)dir->Characteristics);
688 printf(" TimeDateStamp: %08X %s\n",
689 (UINT)dir->TimeDateStamp, get_time_str(dir->TimeDateStamp));
690 printf(" Version: %u.%02u\n", dir->MajorVersion, dir->MinorVersion);
691 printf(" Ordinal base: %u\n", (UINT)dir->Base);
692 printf(" # of functions: %u\n", (UINT)dir->NumberOfFunctions);
693 printf(" # of Names: %u\n", (UINT)dir->NumberOfNames);
694 printf("Addresses of functions: %08X\n", (UINT)dir->AddressOfFunctions);
695 printf("Addresses of name ordinals: %08X\n", (UINT)dir->AddressOfNameOrdinals);
696 printf("Addresses of names: %08X\n", (UINT)dir->AddressOfNames);
697 printf("\n");
698 printf(" Entry Pt Ordn Name\n");
700 pFunc = RVA(dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD));
701 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
702 pName = RVA(dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD));
703 pOrdl = RVA(dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD));
705 funcs = calloc( dir->NumberOfFunctions, sizeof(*funcs) );
706 if (!funcs) fatal("no memory");
708 for (i = 0; i < dir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
710 for (i = 0; i < dir->NumberOfFunctions; i++)
712 if (!pFunc[i]) continue;
713 printf(" %08X %5u ", pFunc[i], (UINT)dir->Base + i);
714 if (funcs[i])
715 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
716 else
717 printf("<by ordinal>");
719 /* check for forwarded function */
720 if ((const char *)RVA(pFunc[i],1) >= (const char *)dir &&
721 (const char *)RVA(pFunc[i],1) < (const char *)dir + size)
722 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
723 printf("\n");
725 free(funcs);
726 printf("\n");
730 struct runtime_function_x86_64
732 UINT BeginAddress;
733 UINT EndAddress;
734 UINT UnwindData;
737 struct runtime_function_armnt
739 UINT BeginAddress;
740 union {
741 UINT UnwindData;
742 struct {
743 UINT Flag : 2;
744 UINT FunctionLength : 11;
745 UINT Ret : 2;
746 UINT H : 1;
747 UINT Reg : 3;
748 UINT R : 1;
749 UINT L : 1;
750 UINT C : 1;
751 UINT StackAdjust : 10;
756 struct runtime_function_arm64
758 UINT BeginAddress;
759 union
761 UINT UnwindData;
762 struct
764 UINT Flag : 2;
765 UINT FunctionLength : 11;
766 UINT RegF : 3;
767 UINT RegI : 4;
768 UINT H : 1;
769 UINT CR : 2;
770 UINT FrameSize : 9;
775 union handler_data
777 struct runtime_function_x86_64 chain;
778 UINT handler;
781 struct opcode
783 BYTE offset;
784 BYTE code : 4;
785 BYTE info : 4;
788 struct unwind_info_x86_64
790 BYTE version : 3;
791 BYTE flags : 5;
792 BYTE prolog;
793 BYTE count;
794 BYTE frame_reg : 4;
795 BYTE frame_offset : 4;
796 struct opcode opcodes[1]; /* count entries */
797 /* followed by union handler_data */
800 struct unwind_info_armnt
802 UINT function_length : 18;
803 UINT version : 2;
804 UINT x : 1;
805 UINT e : 1;
806 UINT f : 1;
807 UINT count : 5;
808 UINT words : 4;
811 struct unwind_info_ext_armnt
813 WORD excount;
814 BYTE exwords;
815 BYTE reserved;
818 struct unwind_info_epilogue_armnt
820 UINT offset : 18;
821 UINT res : 2;
822 UINT cond : 4;
823 UINT index : 8;
826 #define UWOP_PUSH_NONVOL 0
827 #define UWOP_ALLOC_LARGE 1
828 #define UWOP_ALLOC_SMALL 2
829 #define UWOP_SET_FPREG 3
830 #define UWOP_SAVE_NONVOL 4
831 #define UWOP_SAVE_NONVOL_FAR 5
832 #define UWOP_SAVE_XMM128 8
833 #define UWOP_SAVE_XMM128_FAR 9
834 #define UWOP_PUSH_MACHFRAME 10
836 #define UNW_FLAG_EHANDLER 1
837 #define UNW_FLAG_UHANDLER 2
838 #define UNW_FLAG_CHAININFO 4
840 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
842 static const char * const reg_names[16] =
843 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
844 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
846 const union handler_data *handler_data;
847 const struct unwind_info_x86_64 *info;
848 unsigned int i, count;
850 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
851 if (function->UnwindData & 1)
853 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
854 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
855 return;
857 info = RVA( function->UnwindData, sizeof(*info) );
859 printf( " unwind info at %08x\n", function->UnwindData );
860 if (info->version != 1)
862 printf( " *** unknown version %u\n", info->version );
863 return;
865 printf( " flags %x", info->flags );
866 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
867 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
868 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
869 printf( "\n prolog 0x%x bytes\n", info->prolog );
871 if (info->frame_reg)
872 printf( " frame register %s offset 0x%x(%%rsp)\n",
873 reg_names[info->frame_reg], info->frame_offset * 16 );
875 for (i = 0; i < info->count; i++)
877 printf( " 0x%02x: ", info->opcodes[i].offset );
878 switch (info->opcodes[i].code)
880 case UWOP_PUSH_NONVOL:
881 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
882 break;
883 case UWOP_ALLOC_LARGE:
884 if (info->opcodes[i].info)
886 count = *(const UINT *)&info->opcodes[i+1];
887 i += 2;
889 else
891 count = *(const USHORT *)&info->opcodes[i+1] * 8;
892 i++;
894 printf( "sub $0x%x,%%rsp\n", count );
895 break;
896 case UWOP_ALLOC_SMALL:
897 count = (info->opcodes[i].info + 1) * 8;
898 printf( "sub $0x%x,%%rsp\n", count );
899 break;
900 case UWOP_SET_FPREG:
901 printf( "lea 0x%x(%%rsp),%s\n",
902 info->frame_offset * 16, reg_names[info->frame_reg] );
903 break;
904 case UWOP_SAVE_NONVOL:
905 count = *(const USHORT *)&info->opcodes[i+1] * 8;
906 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
907 i++;
908 break;
909 case UWOP_SAVE_NONVOL_FAR:
910 count = *(const UINT *)&info->opcodes[i+1];
911 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
912 i += 2;
913 break;
914 case UWOP_SAVE_XMM128:
915 count = *(const USHORT *)&info->opcodes[i+1] * 16;
916 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
917 i++;
918 break;
919 case UWOP_SAVE_XMM128_FAR:
920 count = *(const UINT *)&info->opcodes[i+1];
921 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
922 i += 2;
923 break;
924 case UWOP_PUSH_MACHFRAME:
925 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
926 break;
927 default:
928 printf( "*** unknown code %u\n", info->opcodes[i].code );
929 break;
933 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
934 if (info->flags & UNW_FLAG_CHAININFO)
936 printf( " -> function %08x-%08x\n",
937 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
938 return;
940 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
941 printf( " handler %08x data at %08x\n", handler_data->handler,
942 (UINT)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
945 static const BYTE armnt_code_lengths[256] =
947 /* 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,
948 /* 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,
949 /* 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,
950 /* 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,
951 /* 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,
952 /* 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,
953 /* 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,
954 /* 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
957 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
959 const struct unwind_info_armnt *info;
960 const struct unwind_info_ext_armnt *infoex;
961 const struct unwind_info_epilogue_armnt *infoepi;
962 unsigned int rva;
963 WORD i, count = 0, words = 0;
965 if (fnc->Flag)
967 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
968 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
970 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
971 (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2 );
972 printf( " Flag %x\n", fnc->Flag );
973 printf( " FunctionLength %x\n", fnc->FunctionLength );
974 printf( " Ret %x\n", fnc->Ret );
975 printf( " H %x\n", fnc->H );
976 printf( " Reg %x\n", fnc->Reg );
977 printf( " R %x\n", fnc->R );
978 printf( " L %x\n", fnc->L );
979 printf( " C %x\n", fnc->C );
980 printf( " StackAdjust %x\n", fnc->StackAdjust );
982 if (fnc->StackAdjust >= 0x03f4)
984 pf = fnc->StackAdjust & 0x04;
985 ef = fnc->StackAdjust & 0x08;
986 stack = (fnc->StackAdjust & 3) + 1;
989 if (!fnc->R || pf)
991 int first = 4, last = fnc->Reg + 4;
992 if (pf)
994 first = (~fnc->StackAdjust) & 3;
995 if (fnc->R)
996 last = 3;
998 if (first == last)
999 sprintf(intregs, "r%u", first);
1000 else
1001 sprintf(intregs, "r%u-r%u", first, last);
1002 fpoffset = last + 1 - first;
1005 if (!fnc->R || ef)
1007 int first = 4, last = fnc->Reg + 4;
1008 if (ef)
1010 first = (~fnc->StackAdjust) & 3;
1011 if (fnc->R)
1012 last = 3;
1014 if (first == last)
1015 sprintf(intregspop, "r%u", first);
1016 else
1017 sprintf(intregspop, "r%u-r%u", first, last);
1020 if (fnc->C)
1022 if (intregs[0])
1023 strcat(intregs, ", ");
1024 if (intregspop[0])
1025 strcat(intregspop, ", ");
1026 strcat(intregs, "r11");
1027 strcat(intregspop, "r11");
1029 if (fnc->L)
1031 if (intregs[0])
1032 strcat(intregs, ", ");
1033 strcat(intregs, "lr");
1035 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
1036 strcat(intregspop, ", ");
1037 if (fnc->Ret != 0)
1038 strcat(intregspop, "lr");
1039 else if (!fnc->H)
1040 strcat(intregspop, "pc");
1043 if (fnc->R)
1045 if (fnc->Reg)
1046 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
1047 else
1048 strcpy(vfpregs, "d8");
1051 if (fnc->Flag == 1) {
1052 if (fnc->H)
1053 printf( " Unwind Code\tpush {r0-r3}\n" );
1055 if (intregs[0])
1056 printf( " Unwind Code\tpush {%s}\n", intregs );
1058 if (fnc->C && fpoffset == 0)
1059 printf( " Unwind Code\tmov r11, sp\n" );
1060 else if (fnc->C)
1061 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
1063 if (fnc->R && fnc->Reg != 0x07)
1064 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
1066 if (stack && !pf)
1067 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
1070 if (fnc->Ret == 3)
1071 return;
1072 printf( "Epilogue:\n" );
1074 if (stack && !ef)
1075 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1077 if (fnc->R && fnc->Reg != 0x07)
1078 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1080 if (intregspop[0])
1081 printf( " Unwind Code\tpop {%s}\n", intregspop );
1083 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1084 printf( " Unwind Code\tadd sp, sp, #16\n" );
1085 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1086 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1088 if (fnc->Ret == 1)
1089 printf( " Unwind Code\tbx <reg>\n" );
1090 else if (fnc->Ret == 2)
1091 printf( " Unwind Code\tb <address>\n" );
1093 return;
1096 info = RVA( fnc->UnwindData, sizeof(*info) );
1097 rva = fnc->UnwindData + sizeof(*info);
1098 count = info->count;
1099 words = info->words;
1101 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1102 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1103 printf( " unwind info at %08x\n", fnc->UnwindData );
1104 printf( " Flag %x\n", fnc->Flag );
1105 printf( " FunctionLength %x\n", info->function_length );
1106 printf( " Version %x\n", info->version );
1107 printf( " X %x\n", info->x );
1108 printf( " E %x\n", info->e );
1109 printf( " F %x\n", info->f );
1110 printf( " Count %x\n", count );
1111 printf( " Words %x\n", words );
1113 if (!info->count && !info->words)
1115 infoex = RVA( rva, sizeof(*infoex) );
1116 rva = rva + sizeof(*infoex);
1117 count = infoex->excount;
1118 words = infoex->exwords;
1119 printf( " ExtCount %x\n", count );
1120 printf( " ExtWords %x\n", words );
1123 if (!info->e)
1125 infoepi = RVA( rva, count * sizeof(*infoepi) );
1126 rva = rva + count * sizeof(*infoepi);
1128 for (i = 0; i < count; i++)
1130 printf( " Epilogue Scope %x\n", i );
1131 printf( " Offset %x\n", infoepi[i].offset );
1132 printf( " Reserved %x\n", infoepi[i].res );
1133 printf( " Condition %x\n", infoepi[i].cond );
1134 printf( " Index %x\n", infoepi[i].index );
1137 else
1138 infoepi = NULL;
1140 if (words)
1142 const unsigned int *codes;
1143 BYTE b, *bytes;
1144 BOOL inepilogue = FALSE;
1146 codes = RVA( rva, words * sizeof(*codes) );
1147 rva = rva + words * sizeof(*codes);
1148 bytes = (BYTE*)codes;
1150 for (b = 0; b < words * sizeof(*codes); b++)
1152 BYTE code = bytes[b];
1153 BYTE len = armnt_code_lengths[code];
1155 if (info->e && b == count)
1157 printf( "Epilogue:\n" );
1158 inepilogue = TRUE;
1160 else if (!info->e && infoepi)
1162 for (i = 0; i < count; i++)
1163 if (b == infoepi[i].index)
1165 printf( "Epilogue from Scope %x at %08x:\n", i,
1166 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1167 inepilogue = TRUE;
1171 printf( " Unwind Code");
1172 for (i = 0; i < len; i++)
1173 printf( " %02x", bytes[b+i] );
1174 printf( "\t" );
1176 if (code == 0x00)
1177 printf( "\n" );
1178 else if (code <= 0x7f)
1179 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1180 else if (code <= 0xbf)
1182 WORD excode, f;
1183 BOOL first = TRUE;
1184 BYTE excodes = bytes[++b];
1186 excode = (code << 8) | excodes;
1187 printf( "%s {", inepilogue ? "pop" : "push" );
1189 for (f = 0; f <= 12; f++)
1191 if ((excode >> f) & 1)
1193 printf( "%sr%u", first ? "" : ", ", f );
1194 first = FALSE;
1198 if (excode & 0x2000)
1199 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1201 printf( "}\n" );
1203 else if (code <= 0xcf)
1204 if (inepilogue)
1205 printf( "mov sp, r%u\n", code & 0x0f );
1206 else
1207 printf( "mov r%u, sp\n", code & 0x0f );
1208 else if (code <= 0xd7)
1209 if (inepilogue)
1210 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1211 else
1212 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1213 else if (code <= 0xdf)
1214 if (inepilogue)
1215 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1216 else
1217 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1218 else if (code <= 0xe7)
1219 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1220 else if (code <= 0xeb)
1222 WORD excode;
1223 BYTE excodes = bytes[++b];
1225 excode = (code << 8) | excodes;
1226 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1228 else if (code <= 0xed)
1230 WORD excode, f;
1231 BOOL first = TRUE;
1232 BYTE excodes = bytes[++b];
1234 excode = (code << 8) | excodes;
1235 printf( "%s {", inepilogue ? "pop" : "push" );
1237 for (f = 0; f < 8; f++)
1239 if ((excode >> f) & 1)
1241 printf( "%sr%u", first ? "" : ", ", f );
1242 first = FALSE;
1246 if (excode & 0x0100)
1247 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1249 printf( "}\n" );
1251 else if (code == 0xee)
1252 printf( "unknown 16\n" );
1253 else if (code == 0xef)
1255 WORD excode;
1256 BYTE excodes = bytes[++b];
1258 if (excodes <= 0x0f)
1260 excode = (code << 8) | excodes;
1261 if (inepilogue)
1262 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1263 else
1264 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1266 else
1267 printf( "unknown 32\n" );
1269 else if (code <= 0xf4)
1270 printf( "unknown\n" );
1271 else if (code <= 0xf6)
1273 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1274 BYTE excodes = bytes[++b];
1276 excode = (code << 8) | excodes;
1277 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1278 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1280 else if (code <= 0xf7)
1282 unsigned int excode;
1283 BYTE excodes[2];
1285 excodes[0] = bytes[++b];
1286 excodes[1] = bytes[++b];
1287 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1288 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1290 else if (code <= 0xf8)
1292 unsigned int excode;
1293 BYTE excodes[3];
1295 excodes[0] = bytes[++b];
1296 excodes[1] = bytes[++b];
1297 excodes[2] = bytes[++b];
1298 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1299 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1301 else if (code <= 0xf9)
1303 unsigned int excode;
1304 BYTE excodes[2];
1306 excodes[0] = bytes[++b];
1307 excodes[1] = bytes[++b];
1308 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1309 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1311 else if (code <= 0xfa)
1313 unsigned int excode;
1314 BYTE excodes[3];
1316 excodes[0] = bytes[++b];
1317 excodes[1] = bytes[++b];
1318 excodes[2] = bytes[++b];
1319 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1320 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1322 else if (code <= 0xfb)
1323 printf( "nop\n" );
1324 else if (code <= 0xfc)
1325 printf( "nop.w\n" );
1326 else if (code <= 0xfd)
1328 printf( "(end) nop\n" );
1329 inepilogue = TRUE;
1331 else if (code <= 0xfe)
1333 printf( "(end) nop.w\n" );
1334 inepilogue = TRUE;
1336 else
1338 printf( "end\n" );
1339 inepilogue = TRUE;
1344 if (info->x)
1346 const unsigned int *handler;
1348 handler = RVA( rva, sizeof(*handler) );
1349 rva = rva + sizeof(*handler);
1351 printf( " handler %08x data at %08x\n", *handler, rva);
1355 struct unwind_info_arm64
1357 UINT function_length : 18;
1358 UINT version : 2;
1359 UINT x : 1;
1360 UINT e : 1;
1361 UINT epilog : 5;
1362 UINT codes : 5;
1365 struct unwind_info_ext_arm64
1367 WORD epilog;
1368 BYTE codes;
1369 BYTE reserved;
1372 struct unwind_info_epilog_arm64
1374 UINT offset : 18;
1375 UINT res : 4;
1376 UINT index : 10;
1379 static const BYTE code_lengths[256] =
1381 /* 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,
1382 /* 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,
1383 /* 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,
1384 /* 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,
1385 /* 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,
1386 /* 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,
1387 /* 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,
1388 /* 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
1391 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1393 unsigned int i, j;
1395 for (i = 0; i < count; i += code_lengths[ptr[i]])
1397 BYTE len = code_lengths[ptr[i]];
1398 unsigned int val = ptr[i];
1399 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1400 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1402 printf( " %04x: ", i );
1403 for (j = 0; j < 4; j++)
1404 if (j < len) printf( "%02x ", ptr[i+j] );
1405 else printf( " " );
1407 if (ptr[i] < 0x20) /* alloc_s */
1409 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1411 else if (ptr[i] < 0x40) /* save_r19r20_x */
1413 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1415 else if (ptr[i] < 0x80) /* save_fplr */
1417 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1419 else if (ptr[i] < 0xc0) /* save_fplr_x */
1421 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1423 else if (ptr[i] < 0xc8) /* alloc_m */
1425 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1427 else if (ptr[i] < 0xcc) /* save_regp */
1429 int reg = 19 + ((val >> 6) & 0xf);
1430 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1432 else if (ptr[i] < 0xd0) /* save_regp_x */
1434 int reg = 19 + ((val >> 6) & 0xf);
1435 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1437 else if (ptr[i] < 0xd4) /* save_reg */
1439 int reg = 19 + ((val >> 6) & 0xf);
1440 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1442 else if (ptr[i] < 0xd6) /* save_reg_x */
1444 int reg = 19 + ((val >> 5) & 0xf);
1445 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1447 else if (ptr[i] < 0xd8) /* save_lrpair */
1449 int reg = 19 + 2 * ((val >> 6) & 0x7);
1450 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1452 else if (ptr[i] < 0xda) /* save_fregp */
1454 int reg = 8 + ((val >> 6) & 0x7);
1455 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1457 else if (ptr[i] < 0xdc) /* save_fregp_x */
1459 int reg = 8 + ((val >> 6) & 0x7);
1460 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1462 else if (ptr[i] < 0xde) /* save_freg */
1464 int reg = 8 + ((val >> 6) & 0x7);
1465 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1467 else if (ptr[i] == 0xde) /* save_freg_x */
1469 int reg = 8 + ((val >> 5) & 0x7);
1470 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1472 else if (ptr[i] == 0xe0) /* alloc_l */
1474 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1476 else if (ptr[i] == 0xe1) /* set_fp */
1478 printf( "mov x29,sp\n" );
1480 else if (ptr[i] == 0xe2) /* add_fp */
1482 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1484 else if (ptr[i] == 0xe3) /* nop */
1486 printf( "nop\n" );
1488 else if (ptr[i] == 0xe4) /* end */
1490 printf( "end\n" );
1492 else if (ptr[i] == 0xe5) /* end_c */
1494 printf( "end_c\n" );
1496 else if (ptr[i] == 0xe6) /* save_next */
1498 printf( "save_next\n" );
1500 else if (ptr[i] == 0xe7) /* arithmetic */
1502 switch ((val >> 4) & 0x0f)
1504 case 0: printf( "add lr,lr,x28\n" ); break;
1505 case 1: printf( "add lr,lr,sp\n" ); break;
1506 case 2: printf( "sub lr,lr,x28\n" ); break;
1507 case 3: printf( "sub lr,lr,sp\n" ); break;
1508 case 4: printf( "eor lr,lr,x28\n" ); break;
1509 case 5: printf( "eor lr,lr,sp\n" ); break;
1510 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1511 case 8: printf( "ror lr,lr,x28\n" ); break;
1512 case 9: printf( "ror lr,lr,sp\n" ); break;
1513 default:printf( "unknown op\n" ); break;
1516 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1518 printf( "MSFT_OP_TRAP_FRAME\n" );
1520 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1522 printf( "MSFT_OP_MACHINE_FRAME\n" );
1524 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1526 printf( "MSFT_OP_CONTEXT\n" );
1528 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1530 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1532 else printf( "??\n");
1536 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1538 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
1540 if (func->CR == 1) intsz += 8;
1541 if (func->RegF) fpsz += 8;
1543 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
1544 locsz = func->FrameSize * 16 - savesz;
1546 switch (func->CR)
1548 case 3:
1549 printf( " %04x: mov x29,sp\n", pos++ );
1550 if (locsz <= 512)
1552 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1553 break;
1555 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1556 /* fall through */
1557 case 0:
1558 case 1:
1559 if (locsz <= 4080)
1561 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1563 else
1565 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1566 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1568 break;
1571 if (func->H)
1573 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1574 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1575 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1576 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1579 if (func->RegF)
1581 if (func->RegF % 2 == 0)
1582 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
1583 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
1585 if (!i && !intsz)
1586 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1587 else
1588 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1592 switch (func->RegI)
1594 case 0:
1595 if (func->CR == 1)
1596 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1597 break;
1598 case 1:
1599 if (func->CR == 1)
1600 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1601 else
1602 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1603 break;
1604 default:
1605 if (func->RegI % 2)
1607 if (func->CR == 1)
1608 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1609 else
1610 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1612 else if (func->CR == 1)
1613 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1615 for (i = func->RegI / 2 - 1; i >= 0; i--)
1616 if (i)
1617 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1618 else
1619 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1620 break;
1622 printf( " %04x: end\n", pos );
1625 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1627 const struct unwind_info_arm64 *info;
1628 const struct unwind_info_ext_arm64 *infoex;
1629 const struct unwind_info_epilog_arm64 *infoepi;
1630 const BYTE *ptr;
1631 unsigned int i, rva, codes, epilogs;
1633 if (func->Flag)
1635 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1636 func->BeginAddress + func->FunctionLength * 4 );
1637 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1638 func->FunctionLength, func->Flag, func->RegF, func->RegI,
1639 func->H, func->CR, func->FrameSize );
1640 dump_arm64_packed_info( func );
1641 return;
1644 rva = func->UnwindData;
1645 info = RVA( rva, sizeof(*info) );
1646 rva += sizeof(*info);
1647 epilogs = info->epilog;
1648 codes = info->codes;
1650 if (!codes)
1652 infoex = RVA( rva, sizeof(*infoex) );
1653 rva = rva + sizeof(*infoex);
1654 codes = infoex->codes;
1655 epilogs = infoex->epilog;
1657 printf( "\nFunction %08x-%08x:\n",
1658 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1659 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1660 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1661 if (info->e)
1663 printf( " epilog 0: code=%04x\n", info->epilog );
1665 else
1667 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1668 rva += sizeof(*infoepi) * epilogs;
1669 for (i = 0; i < epilogs; i++)
1670 printf( " epilog %u: pc=%08x code=%04x\n", i,
1671 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1673 ptr = RVA( rva, codes * 4);
1674 rva += codes * 4;
1675 if (info->x)
1677 const UINT *handler = RVA( rva, sizeof(*handler) );
1678 rva += sizeof(*handler);
1679 printf( " handler: %08x data %08x\n", *handler, rva );
1681 dump_arm64_codes( ptr, codes * 4 );
1684 static void dump_dir_exceptions(void)
1686 unsigned int i, size;
1687 const void *funcs;
1688 const IMAGE_FILE_HEADER *file_header;
1690 funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1691 if (!funcs) return;
1693 file_header = &PE_nt_headers->FileHeader;
1695 switch (file_header->Machine)
1697 case IMAGE_FILE_MACHINE_AMD64:
1698 size /= sizeof(struct runtime_function_x86_64);
1699 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1700 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1701 break;
1702 case IMAGE_FILE_MACHINE_ARMNT:
1703 size /= sizeof(struct runtime_function_armnt);
1704 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1705 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1706 break;
1707 case IMAGE_FILE_MACHINE_ARM64:
1708 size /= sizeof(struct runtime_function_arm64);
1709 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1710 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1711 break;
1712 default:
1713 printf( "Exception information not supported for %s binaries\n",
1714 get_machine_str(file_header->Machine));
1715 break;
1717 printf( "\n" );
1721 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
1723 /* FIXME: This does not properly handle large images */
1724 const IMAGE_IMPORT_BY_NAME* iibn;
1725 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1727 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1728 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
1729 else
1731 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1732 if (!iibn)
1733 printf("Can't grab import by name info, skipping to next ordinal\n");
1734 else
1735 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1740 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
1742 const IMAGE_IMPORT_BY_NAME* iibn;
1743 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
1745 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1746 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
1747 else
1749 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1750 if (!iibn)
1751 printf("Can't grab import by name info, skipping to next ordinal\n");
1752 else
1753 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1758 static void dump_dir_imported_functions(void)
1760 unsigned directorySize;
1761 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1763 if (!importDesc) return;
1765 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1767 for (;;)
1769 const IMAGE_THUNK_DATA32* il;
1771 if (!importDesc->Name || !importDesc->FirstThunk) break;
1773 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1774 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
1775 printf(" TimeDateStamp: %08X (%s)\n",
1776 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1777 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
1778 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
1780 printf(" Thunk Ordn Name\n");
1782 il = (importDesc->OriginalFirstThunk != 0) ?
1783 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
1784 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1786 if (!il)
1787 printf("Can't grab thunk data, going to next imported DLL\n");
1788 else
1790 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1791 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1792 else
1793 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1794 printf("\n");
1796 importDesc++;
1798 printf("\n");
1801 static void dump_hybrid_metadata(void)
1803 unsigned int i;
1804 const void *metadata = get_hybrid_metadata();
1806 if (!metadata) return;
1807 printf( "Hybrid metadata\n" );
1809 switch (PE_nt_headers->FileHeader.Machine)
1811 case IMAGE_FILE_MACHINE_I386:
1813 const IMAGE_CHPE_METADATA_X86 *data = metadata;
1815 printf( " Version %#x\n", (int)data->Version );
1816 printf( " CHPECodeAddressRangeOffset %#x\n", (int)data->CHPECodeAddressRangeOffset );
1817 printf( " CHPECodeAddressRangeCount %#x\n", (int)data->CHPECodeAddressRangeCount );
1818 printf( " WowA64ExceptionHandlerFunctionPointer %#x\n", (int)data->WowA64ExceptionHandlerFunctionPointer );
1819 printf( " WowA64DispatchCallFunctionPointer %#x\n", (int)data->WowA64DispatchCallFunctionPointer );
1820 printf( " WowA64DispatchIndirectCallFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallFunctionPointer );
1821 printf( " WowA64DispatchIndirectCallCfgFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallCfgFunctionPointer );
1822 printf( " WowA64DispatchRetFunctionPointer %#x\n", (int)data->WowA64DispatchRetFunctionPointer );
1823 printf( " WowA64DispatchRetLeafFunctionPointer %#x\n", (int)data->WowA64DispatchRetLeafFunctionPointer );
1824 printf( " WowA64DispatchJumpFunctionPointer %#x\n", (int)data->WowA64DispatchJumpFunctionPointer );
1825 if (data->Version >= 2)
1826 printf( " CompilerIATPointer %#x\n", (int)data->CompilerIATPointer );
1827 if (data->Version >= 3)
1828 printf( " WowA64RdtscFunctionPointer %#x\n", (int)data->WowA64RdtscFunctionPointer );
1829 if (data->Version >= 4)
1831 printf( " unknown[0] %#x\n", (int)data->unknown[0] );
1832 printf( " unknown[1] %#x\n", (int)data->unknown[1] );
1833 printf( " unknown[2] %#x\n", (int)data->unknown[2] );
1834 printf( " unknown[3] %#x\n", (int)data->unknown[3] );
1837 if (data->CHPECodeAddressRangeOffset)
1839 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CHPECodeAddressRangeOffset,
1840 data->CHPECodeAddressRangeCount * sizeof(*map) );
1842 printf( "\nCode ranges\n" );
1843 for (i = 0; i < data->CHPECodeAddressRangeCount; i++)
1845 static const char *types[] = { "x86", "ARM64" };
1846 unsigned int start = map[i].StartOffset & ~1;
1847 unsigned int type = map[i].StartOffset & 1;
1848 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
1852 break;
1855 case IMAGE_FILE_MACHINE_AMD64:
1856 case IMAGE_FILE_MACHINE_ARM64:
1858 const IMAGE_ARM64EC_METADATA *data = metadata;
1860 printf( " Version %#x\n", (int)data->Version );
1861 printf( " CodeMap %#x\n", (int)data->CodeMap );
1862 printf( " CodeMapCount %#x\n", (int)data->CodeMapCount );
1863 printf( " CodeRangesToEntryPoints %#x\n", (int)data->CodeRangesToEntryPoints );
1864 printf( " RedirectionMetadata %#x\n", (int)data->RedirectionMetadata );
1865 printf( " __os_arm64x_dispatch_call_no_redirect %#x\n", (int)data->__os_arm64x_dispatch_call_no_redirect );
1866 printf( " __os_arm64x_dispatch_ret %#x\n", (int)data->__os_arm64x_dispatch_ret );
1867 printf( " __os_arm64x_dispatch_call %#x\n", (int)data->__os_arm64x_dispatch_call );
1868 printf( " __os_arm64x_dispatch_icall %#x\n", (int)data->__os_arm64x_dispatch_icall );
1869 printf( " __os_arm64x_dispatch_icall_cfg %#x\n", (int)data->__os_arm64x_dispatch_icall_cfg );
1870 printf( " AlternateEntryPoint %#x\n", (int)data->AlternateEntryPoint );
1871 printf( " AuxiliaryIAT %#x\n", (int)data->AuxiliaryIAT );
1872 printf( " CodeRangesToEntryPointsCount %#x\n", (int)data->CodeRangesToEntryPointsCount );
1873 printf( " RedirectionMetadataCount %#x\n", (int)data->RedirectionMetadataCount );
1874 printf( " GetX64InformationFunctionPointer %#x\n", (int)data->GetX64InformationFunctionPointer );
1875 printf( " SetX64InformationFunctionPointer %#x\n", (int)data->SetX64InformationFunctionPointer );
1876 printf( " ExtraRFETable %#x\n", (int)data->ExtraRFETable );
1877 printf( " ExtraRFETableSize %#x\n", (int)data->ExtraRFETableSize );
1878 printf( " __os_arm64x_dispatch_fptr %#x\n", (int)data->__os_arm64x_dispatch_fptr );
1879 printf( " AuxiliaryIATCopy %#x\n", (int)data->AuxiliaryIATCopy );
1881 if (data->CodeMap)
1883 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CodeMap, data->CodeMapCount * sizeof(*map) );
1885 printf( "\nCode ranges\n" );
1886 for (i = 0; i < data->CodeMapCount; i++)
1888 static const char *types[] = { "ARM64", "ARM64EC", "x64", "??" };
1889 unsigned int start = map[i].StartOffset & ~0x3;
1890 unsigned int type = map[i].StartOffset & 0x3;
1891 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
1895 if (PE_nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_ARM64) break;
1897 if (data->CodeRangesToEntryPoints)
1899 const IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT *map = RVA( data->CodeRangesToEntryPoints,
1900 data->CodeRangesToEntryPointsCount * sizeof(*map) );
1902 printf( "\nCode ranges to entry points\n" );
1903 printf( " Start - End Entry point\n" );
1904 for (i = 0; i < data->CodeRangesToEntryPointsCount; i++)
1906 const char *name = find_export_from_rva( map[i].EntryPoint );
1907 printf( " %08x - %08x %08x",
1908 (int)map[i].StartRva, (int)map[i].EndRva, (int)map[i].EntryPoint );
1909 if (name) printf( " %s", name );
1910 printf( "\n" );
1914 if (data->RedirectionMetadata)
1916 const IMAGE_ARM64EC_REDIRECTION_ENTRY *map = RVA( data->RedirectionMetadata,
1917 data->RedirectionMetadataCount * sizeof(*map) );
1919 printf( "\nEntry point redirection\n" );
1920 for (i = 0; i < data->RedirectionMetadataCount; i++)
1922 const char *name = find_export_from_rva( map[i].Source );
1923 printf( " %08x -> %08x", (int)map[i].Source, (int)map[i].Destination );
1924 if (name) printf( " (%s)", name );
1925 printf( "\n" );
1928 break;
1931 printf( "\n" );
1934 static void dump_dir_loadconfig(void)
1936 unsigned int size;
1937 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32;
1939 loadcfg32 = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
1940 if (!loadcfg32) return;
1941 size = min( size, loadcfg32->Size );
1943 printf( "Loadconfig\n" );
1944 print_dword( "Size", loadcfg32->Size );
1945 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1946 print_word( "MajorVersion", loadcfg32->MajorVersion );
1947 print_word( "MinorVersion", loadcfg32->MinorVersion );
1948 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1949 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1950 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1952 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1954 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void *)loadcfg32;
1956 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1957 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1958 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1959 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1960 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1961 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1962 print_word( "CSDVersion", loadcfg64->CSDVersion );
1963 print_word( "DependentLoadFlags", loadcfg64->DependentLoadFlags );
1964 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1965 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, SEHandlerTable )) goto done;
1966 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1967 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1968 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardCFCheckFunctionPointer )) goto done;
1969 print_longlong( "GuardCFCheckFunctionPointer", loadcfg64->GuardCFCheckFunctionPointer );
1970 print_longlong( "GuardCFDispatchFunctionPointer", loadcfg64->GuardCFDispatchFunctionPointer );
1971 print_longlong( "GuardCFFunctionTable", loadcfg64->GuardCFFunctionTable );
1972 print_longlong( "GuardCFFunctionCount", loadcfg64->GuardCFFunctionCount );
1973 print_dword( "GuardFlags", loadcfg64->GuardFlags );
1974 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CodeIntegrity )) goto done;
1975 print_word( "CodeIntegrity.Flags", loadcfg64->CodeIntegrity.Flags );
1976 print_word( "CodeIntegrity.Catalog", loadcfg64->CodeIntegrity.Catalog );
1977 print_dword( "CodeIntegrity.CatalogOffset", loadcfg64->CodeIntegrity.CatalogOffset );
1978 print_dword( "CodeIntegrity.Reserved", loadcfg64->CodeIntegrity.Reserved );
1979 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardAddressTakenIatEntryTable )) goto done;
1980 print_longlong( "GuardAddressTakenIatEntryTable", loadcfg64->GuardAddressTakenIatEntryTable );
1981 print_longlong( "GuardAddressTakenIatEntryCount", loadcfg64->GuardAddressTakenIatEntryCount );
1982 print_longlong( "GuardLongJumpTargetTable", loadcfg64->GuardLongJumpTargetTable );
1983 print_longlong( "GuardLongJumpTargetCount", loadcfg64->GuardLongJumpTargetCount );
1984 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTable )) goto done;
1985 print_longlong( "DynamicValueRelocTable", loadcfg64->DynamicValueRelocTable );
1986 print_longlong( "CHPEMetadataPointer", loadcfg64->CHPEMetadataPointer );
1987 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFFailureRoutine )) goto done;
1988 print_longlong( "GuardRFFailureRoutine", loadcfg64->GuardRFFailureRoutine );
1989 print_longlong( "GuardRFFailureRoutineFuncPtr", loadcfg64->GuardRFFailureRoutineFunctionPointer );
1990 print_dword( "DynamicValueRelocTableOffset", loadcfg64->DynamicValueRelocTableOffset );
1991 print_word( "DynamicValueRelocTableSection",loadcfg64->DynamicValueRelocTableSection );
1992 print_word( "Reserved2", loadcfg64->Reserved2 );
1993 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFVerifyStackPointerFunctionPointer )) goto done;
1994 print_longlong( "GuardRFVerifyStackPointerFuncPtr", loadcfg64->GuardRFVerifyStackPointerFunctionPointer );
1995 print_dword( "HotPatchTableOffset", loadcfg64->HotPatchTableOffset );
1996 print_dword( "Reserved3", loadcfg64->Reserved3 );
1997 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, EnclaveConfigurationPointer )) goto done;
1998 print_longlong( "EnclaveConfigurationPointer", loadcfg64->EnclaveConfigurationPointer );
1999 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, VolatileMetadataPointer )) goto done;
2000 print_longlong( "VolatileMetadataPointer", loadcfg64->VolatileMetadataPointer );
2001 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardEHContinuationTable )) goto done;
2002 print_longlong( "GuardEHContinuationTable", loadcfg64->GuardEHContinuationTable );
2003 print_longlong( "GuardEHContinuationCount", loadcfg64->GuardEHContinuationCount );
2004 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardXFGCheckFunctionPointer )) goto done;
2005 print_longlong( "GuardXFGCheckFunctionPointer", loadcfg64->GuardXFGCheckFunctionPointer );
2006 print_longlong( "GuardXFGDispatchFunctionPointer", loadcfg64->GuardXFGDispatchFunctionPointer );
2007 print_longlong( "GuardXFGTableDispatchFuncPtr", loadcfg64->GuardXFGTableDispatchFunctionPointer );
2008 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CastGuardOsDeterminedFailureMode )) goto done;
2009 print_longlong( "CastGuardOsDeterminedFailureMode", loadcfg64->CastGuardOsDeterminedFailureMode );
2010 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardMemcpyFunctionPointer )) goto done;
2011 print_longlong( "GuardMemcpyFunctionPointer", loadcfg64->GuardMemcpyFunctionPointer );
2013 else
2015 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
2016 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
2017 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
2018 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
2019 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
2020 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
2021 print_word( "CSDVersion", loadcfg32->CSDVersion );
2022 print_word( "DependentLoadFlags", loadcfg32->DependentLoadFlags );
2023 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
2024 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, SEHandlerTable )) goto done;
2025 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
2026 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
2027 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardCFCheckFunctionPointer )) goto done;
2028 print_dword( "GuardCFCheckFunctionPointer", loadcfg32->GuardCFCheckFunctionPointer );
2029 print_dword( "GuardCFDispatchFunctionPointer", loadcfg32->GuardCFDispatchFunctionPointer );
2030 print_dword( "GuardCFFunctionTable", loadcfg32->GuardCFFunctionTable );
2031 print_dword( "GuardCFFunctionCount", loadcfg32->GuardCFFunctionCount );
2032 print_dword( "GuardFlags", loadcfg32->GuardFlags );
2033 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CodeIntegrity )) goto done;
2034 print_word( "CodeIntegrity.Flags", loadcfg32->CodeIntegrity.Flags );
2035 print_word( "CodeIntegrity.Catalog", loadcfg32->CodeIntegrity.Catalog );
2036 print_dword( "CodeIntegrity.CatalogOffset", loadcfg32->CodeIntegrity.CatalogOffset );
2037 print_dword( "CodeIntegrity.Reserved", loadcfg32->CodeIntegrity.Reserved );
2038 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardAddressTakenIatEntryTable )) goto done;
2039 print_dword( "GuardAddressTakenIatEntryTable", loadcfg32->GuardAddressTakenIatEntryTable );
2040 print_dword( "GuardAddressTakenIatEntryCount", loadcfg32->GuardAddressTakenIatEntryCount );
2041 print_dword( "GuardLongJumpTargetTable", loadcfg32->GuardLongJumpTargetTable );
2042 print_dword( "GuardLongJumpTargetCount", loadcfg32->GuardLongJumpTargetCount );
2043 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTable )) goto done;
2044 print_dword( "DynamicValueRelocTable", loadcfg32->DynamicValueRelocTable );
2045 print_dword( "CHPEMetadataPointer", loadcfg32->CHPEMetadataPointer );
2046 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFFailureRoutine )) goto done;
2047 print_dword( "GuardRFFailureRoutine", loadcfg32->GuardRFFailureRoutine );
2048 print_dword( "GuardRFFailureRoutineFuncPtr", loadcfg32->GuardRFFailureRoutineFunctionPointer );
2049 print_dword( "DynamicValueRelocTableOffset", loadcfg32->DynamicValueRelocTableOffset );
2050 print_word( "DynamicValueRelocTableSection", loadcfg32->DynamicValueRelocTableSection );
2051 print_word( "Reserved2", loadcfg32->Reserved2 );
2052 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2053 print_dword( "GuardRFVerifyStackPointerFuncPtr", loadcfg32->GuardRFVerifyStackPointerFunctionPointer );
2054 print_dword( "HotPatchTableOffset", loadcfg32->HotPatchTableOffset );
2055 print_dword( "Reserved3", loadcfg32->Reserved3 );
2056 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, EnclaveConfigurationPointer )) goto done;
2057 print_dword( "EnclaveConfigurationPointer", loadcfg32->EnclaveConfigurationPointer );
2058 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, VolatileMetadataPointer )) goto done;
2059 print_dword( "VolatileMetadataPointer", loadcfg32->VolatileMetadataPointer );
2060 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardEHContinuationTable )) goto done;
2061 print_dword( "GuardEHContinuationTable", loadcfg32->GuardEHContinuationTable );
2062 print_dword( "GuardEHContinuationCount", loadcfg32->GuardEHContinuationCount );
2063 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardXFGCheckFunctionPointer )) goto done;
2064 print_dword( "GuardXFGCheckFunctionPointer", loadcfg32->GuardXFGCheckFunctionPointer );
2065 print_dword( "GuardXFGDispatchFunctionPointer", loadcfg32->GuardXFGDispatchFunctionPointer );
2066 print_dword( "GuardXFGTableDispatchFuncPtr", loadcfg32->GuardXFGTableDispatchFunctionPointer );
2067 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CastGuardOsDeterminedFailureMode )) goto done;
2068 print_dword( "CastGuardOsDeterminedFailureMode", loadcfg32->CastGuardOsDeterminedFailureMode );
2069 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardMemcpyFunctionPointer )) goto done;
2070 print_dword( "GuardMemcpyFunctionPointer", loadcfg32->GuardMemcpyFunctionPointer );
2072 done:
2073 printf( "\n" );
2074 dump_hybrid_metadata();
2077 static void dump_dir_delay_imported_functions(void)
2079 unsigned directorySize;
2080 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
2082 if (!importDesc) return;
2084 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
2086 for (;;)
2088 const IMAGE_THUNK_DATA32* il;
2089 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
2091 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
2093 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
2094 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
2095 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
2096 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
2097 printf(" TimeDateStamp: %08X (%s)\n",
2098 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2100 printf(" Thunk Ordn Name\n");
2102 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
2104 if (!il)
2105 printf("Can't grab thunk data, going to next imported DLL\n");
2106 else
2108 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2109 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
2110 else
2111 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
2112 printf("\n");
2114 importDesc++;
2116 printf("\n");
2119 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
2121 const char* str;
2123 printf("Directory %02u\n", idx + 1);
2124 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
2125 printf(" TimeDateStamp: %08X %s\n",
2126 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
2127 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
2128 switch (idd->Type)
2130 default:
2131 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
2132 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
2133 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
2134 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
2135 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
2136 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
2137 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
2138 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
2139 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
2140 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
2141 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
2142 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
2143 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
2144 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
2145 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
2146 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
2147 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
2149 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
2150 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
2151 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
2152 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
2154 switch (idd->Type)
2156 case IMAGE_DEBUG_TYPE_UNKNOWN:
2157 break;
2158 case IMAGE_DEBUG_TYPE_COFF:
2159 dump_coff(idd->PointerToRawData, idd->SizeOfData,
2160 IMAGE_FIRST_SECTION(PE_nt_headers));
2161 break;
2162 case IMAGE_DEBUG_TYPE_CODEVIEW:
2163 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
2164 break;
2165 case IMAGE_DEBUG_TYPE_FPO:
2166 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
2167 break;
2168 case IMAGE_DEBUG_TYPE_MISC:
2170 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
2171 if (!misc) {printf("Can't get misc debug information\n"); break;}
2172 printf(" DataType: %u (%s)\n",
2173 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
2174 printf(" Length: %u\n", (UINT)misc->Length);
2175 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
2176 printf(" Data: %s\n", misc->Data);
2178 break;
2179 default: break;
2181 printf("\n");
2184 static void dump_dir_debug(void)
2186 unsigned nb_dbg, i;
2187 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
2189 nb_dbg /= sizeof(*debugDir);
2190 if (!debugDir || !nb_dbg) return;
2192 printf("Debug Table (%u directories)\n", nb_dbg);
2194 for (i = 0; i < nb_dbg; i++)
2196 dump_dir_debug_dir(debugDir, i);
2197 debugDir++;
2199 printf("\n");
2202 static inline void print_clrflags(const char *title, UINT value)
2204 printf(" %-34s 0x%X\n", title, value);
2205 #define X(f,s) if (value & f) printf(" %s\n", s)
2206 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
2207 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
2208 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
2209 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
2210 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
2211 #undef X
2214 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
2216 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
2219 static void dump_dir_clr_header(void)
2221 unsigned int size = 0;
2222 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
2224 if (!dir) return;
2226 printf( "CLR Header\n" );
2227 print_dword( "Header Size", dir->cb );
2228 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
2229 print_clrflags( "Flags", dir->Flags );
2230 print_dword( "EntryPointToken", dir->EntryPointToken );
2231 printf("\n");
2232 printf( "CLR Data Directory\n" );
2233 print_clrdirectory( "MetaData", &dir->MetaData );
2234 print_clrdirectory( "Resources", &dir->Resources );
2235 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
2236 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
2237 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
2238 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
2239 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
2240 printf("\n");
2243 static void dump_dynamic_relocs_arm64x( const IMAGE_BASE_RELOCATION *base_reloc, unsigned int size )
2245 unsigned int i;
2246 const IMAGE_BASE_RELOCATION *base_end = (const IMAGE_BASE_RELOCATION *)((const char *)base_reloc + size);
2248 printf( "Relocations ARM64X\n" );
2249 while (base_reloc < base_end - 1 && base_reloc->SizeOfBlock)
2251 const USHORT *rel = (const USHORT *)(base_reloc + 1);
2252 const USHORT *end = (const USHORT *)base_reloc + base_reloc->SizeOfBlock / sizeof(USHORT);
2253 printf( " Page %x\n", (UINT)base_reloc->VirtualAddress );
2254 while (rel < end && *rel)
2256 USHORT offset = *rel & 0xfff;
2257 USHORT type = (*rel >> 12) & 3;
2258 USHORT arg = *rel >> 14;
2259 rel++;
2260 switch (type)
2262 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2263 printf( " off %04x zero-fill %u bytes\n", offset, 1 << arg );
2264 break;
2265 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2266 printf( " off %04x set %u bytes value ", offset, 1 << arg );
2267 for (i = (1 << arg ) / sizeof(USHORT); i > 0; i--) printf( "%04x", rel[i - 1] );
2268 rel += (1 << arg) / sizeof(USHORT);
2269 printf( "\n" );
2270 break;
2271 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2272 printf( " off %04x add offset ", offset );
2273 if (arg & 1) printf( "-" );
2274 printf( "%08x\n", (UINT)*rel++ * ((arg & 2) ? 8 : 4) );
2275 break;
2276 default:
2277 printf( " off %04x unknown (arg %x)\n", offset, arg );
2278 break;
2281 base_reloc = (const IMAGE_BASE_RELOCATION *)end;
2285 static void dump_dynamic_relocs( const char *ptr, unsigned int size, ULONGLONG symbol )
2287 switch (symbol)
2289 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE:
2290 printf( "Relocations GUARD_RF_PROLOGUE\n" );
2291 break;
2292 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE:
2293 printf( "Relocations GUARD_RF_EPILOGUE\n" );
2294 break;
2295 case IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER:
2296 printf( "Relocations GUARD_IMPORT_CONTROL_TRANSFER\n" );
2297 break;
2298 case IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER:
2299 printf( "Relocations GUARD_INDIR_CONTROL_TRANSFER\n" );
2300 break;
2301 case IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH:
2302 printf( "Relocations GUARD_SWITCHTABLE_BRANCH\n" );
2303 break;
2304 case IMAGE_DYNAMIC_RELOCATION_ARM64X:
2305 dump_dynamic_relocs_arm64x( (const IMAGE_BASE_RELOCATION *)ptr, size );
2306 break;
2307 default:
2308 printf( "Unknown relocation symbol %s\n", longlong_str(symbol) );
2309 break;
2313 static const IMAGE_DYNAMIC_RELOCATION_TABLE *get_dyn_reloc_table(void)
2315 unsigned int size, section, offset;
2316 const IMAGE_SECTION_HEADER *sec;
2317 const IMAGE_DYNAMIC_RELOCATION_TABLE *table;
2319 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2321 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2322 if (!cfg) return NULL;
2323 size = min( size, cfg->Size );
2324 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTableSection )) return NULL;
2325 offset = cfg->DynamicValueRelocTableOffset;
2326 section = cfg->DynamicValueRelocTableSection;
2328 else
2330 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2331 if (!cfg) return NULL;
2332 size = min( size, cfg->Size );
2333 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTableSection )) return NULL;
2334 offset = cfg->DynamicValueRelocTableOffset;
2335 section = cfg->DynamicValueRelocTableSection;
2337 if (!section || section > PE_nt_headers->FileHeader.NumberOfSections) return NULL;
2338 sec = IMAGE_FIRST_SECTION( PE_nt_headers ) + section - 1;
2339 if (offset >= sec->SizeOfRawData) return NULL;
2340 return PRD( sec->PointerToRawData + offset, sizeof(*table) );
2343 static void dump_dir_dynamic_reloc(void)
2345 const char *ptr, *end;
2346 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2348 if (!table) return;
2350 printf( "Dynamic relocations (version %u)\n\n", (UINT)table->Version );
2351 ptr = (const char *)(table + 1);
2352 end = ptr + table->Size;
2353 while (ptr < end)
2355 switch (table->Version)
2357 case 1:
2358 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2360 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2361 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2362 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2364 else
2366 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2367 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2368 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2370 break;
2371 case 2:
2372 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2374 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2375 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2376 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2378 else
2380 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2381 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2382 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2384 break;
2387 printf( "\n" );
2390 static const IMAGE_BASE_RELOCATION *get_armx_relocs( unsigned int *size )
2392 const char *ptr, *end;
2393 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2395 if (!table) return NULL;
2396 ptr = (const char *)(table + 1);
2397 end = ptr + table->Size;
2398 while (ptr < end)
2400 switch (table->Version)
2402 case 1:
2403 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2405 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2406 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2408 *size = reloc->BaseRelocSize;
2409 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2411 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2413 else
2415 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2416 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2418 *size = reloc->BaseRelocSize;
2419 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2421 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2423 break;
2424 case 2:
2425 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2427 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2428 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2430 *size = reloc->FixupInfoSize;
2431 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2433 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2435 else
2437 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2438 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2440 *size = reloc->FixupInfoSize;
2441 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2443 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2445 break;
2448 return NULL;
2451 static BOOL get_alt_header( void )
2453 unsigned int size;
2454 const IMAGE_BASE_RELOCATION *end, *reloc = get_armx_relocs( &size );
2456 if (!reloc) return FALSE;
2457 end = (const IMAGE_BASE_RELOCATION *)((const char *)reloc + size);
2459 while (reloc < end - 1 && reloc->SizeOfBlock)
2461 const USHORT *rel = (const USHORT *)(reloc + 1);
2462 const USHORT *rel_end = (const USHORT *)reloc + reloc->SizeOfBlock / sizeof(USHORT);
2463 char *page = reloc->VirtualAddress ? (char *)RVA(reloc->VirtualAddress,1) : dump_base;
2465 while (rel < rel_end && *rel)
2467 USHORT offset = *rel & 0xfff;
2468 USHORT type = (*rel >> 12) & 3;
2469 USHORT arg = *rel >> 14;
2470 int val;
2471 rel++;
2472 switch (type)
2474 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2475 memset( page + offset, 0, 1 << arg );
2476 break;
2477 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2478 memcpy( page + offset, rel, 1 << arg );
2479 rel += (1 << arg) / sizeof(USHORT);
2480 break;
2481 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2482 val = (unsigned int)*rel++ * ((arg & 2) ? 8 : 4);
2483 if (arg & 1) val = -val;
2484 *(int *)(page + offset) += val;
2485 break;
2488 reloc = (const IMAGE_BASE_RELOCATION *)rel_end;
2490 return TRUE;
2493 static void dump_dir_reloc(void)
2495 unsigned int i, size = 0;
2496 const USHORT *relocs;
2497 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
2498 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
2499 static const char * const names[] =
2501 "BASED_ABSOLUTE",
2502 "BASED_HIGH",
2503 "BASED_LOW",
2504 "BASED_HIGHLOW",
2505 "BASED_HIGHADJ",
2506 "BASED_MIPS_JMPADDR",
2507 "BASED_SECTION",
2508 "BASED_REL",
2509 "unknown 8",
2510 "BASED_IA64_IMM64",
2511 "BASED_DIR64",
2512 "BASED_HIGH3ADJ",
2513 "unknown 12",
2514 "unknown 13",
2515 "unknown 14",
2516 "unknown 15"
2519 if (!rel) return;
2521 printf( "Relocations\n" );
2522 while (rel < end - 1 && rel->SizeOfBlock)
2524 printf( " Page %x\n", (UINT)rel->VirtualAddress );
2525 relocs = (const USHORT *)(rel + 1);
2526 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
2527 while (i--)
2529 USHORT offset = *relocs & 0xfff;
2530 int type = *relocs >> 12;
2531 printf( " off %04x type %s\n", offset, names[type] );
2532 relocs++;
2534 rel = (const IMAGE_BASE_RELOCATION *)relocs;
2536 printf("\n");
2539 static void dump_dir_tls(void)
2541 IMAGE_TLS_DIRECTORY64 dir;
2542 const UINT *callbacks;
2543 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
2545 if (!pdir) return;
2547 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2548 memcpy(&dir, pdir, sizeof(dir));
2549 else
2551 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
2552 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
2553 dir.AddressOfIndex = pdir->AddressOfIndex;
2554 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
2555 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
2556 dir.Characteristics = pdir->Characteristics;
2559 /* FIXME: This does not properly handle large images */
2560 printf( "Thread Local Storage\n" );
2561 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2562 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
2563 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2564 (UINT)dir.SizeOfZeroFill );
2565 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
2566 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
2567 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
2568 if (dir.AddressOfCallBacks)
2570 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2571 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
2573 printf( " %08x", *callbacks );
2574 addr += sizeof(UINT);
2577 printf(" }\n\n");
2580 enum FileSig get_kind_dbg(void)
2582 const WORD* pw;
2584 pw = PRD(0, sizeof(WORD));
2585 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2587 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2588 return SIG_UNKNOWN;
2591 void dbg_dump(void)
2593 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2594 unsigned nb_dbg;
2595 unsigned i;
2596 const IMAGE_DEBUG_DIRECTORY* debugDir;
2598 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2599 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2601 printf ("Signature: %.2s (0x%4X)\n",
2602 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2603 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2604 printf ("Machine: 0x%04X (%s)\n",
2605 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2606 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2607 printf ("TimeDateStamp: 0x%08X (%s)\n",
2608 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2609 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
2610 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
2611 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
2612 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
2613 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
2614 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
2616 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2617 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2618 {printf("Can't get the sections, aborting\n"); return;}
2620 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2622 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2623 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2624 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2625 separateDebugHead->ExportedNamesSize,
2626 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2627 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2629 printf("Debug Table (%u directories)\n", nb_dbg);
2631 for (i = 0; i < nb_dbg; i++)
2633 dump_dir_debug_dir(debugDir, i);
2634 debugDir++;
2638 static const char *get_resource_type( unsigned int id )
2640 static const char * const types[] =
2642 NULL,
2643 "CURSOR",
2644 "BITMAP",
2645 "ICON",
2646 "MENU",
2647 "DIALOG",
2648 "STRING",
2649 "FONTDIR",
2650 "FONT",
2651 "ACCELERATOR",
2652 "RCDATA",
2653 "MESSAGETABLE",
2654 "GROUP_CURSOR",
2655 NULL,
2656 "GROUP_ICON",
2657 NULL,
2658 "VERSION",
2659 "DLGINCLUDE",
2660 NULL,
2661 "PLUGPLAY",
2662 "VXD",
2663 "ANICURSOR",
2664 "ANIICON",
2665 "HTML",
2666 "RT_MANIFEST"
2669 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2670 return NULL;
2673 /* dump an ASCII string with proper escaping */
2674 static int dump_strA( const unsigned char *str, size_t len )
2676 static const char escapes[32] = ".......abtnvfr.............e....";
2677 char buffer[256];
2678 char *pos = buffer;
2679 int count = 0;
2681 for (; len; str++, len--)
2683 if (pos > buffer + sizeof(buffer) - 8)
2685 fwrite( buffer, pos - buffer, 1, stdout );
2686 count += pos - buffer;
2687 pos = buffer;
2689 if (*str > 127) /* hex escape */
2691 pos += sprintf( pos, "\\x%02x", *str );
2692 continue;
2694 if (*str < 32) /* octal or C escape */
2696 if (!*str && len == 1) continue; /* do not output terminating NULL */
2697 if (escapes[*str] != '.')
2698 pos += sprintf( pos, "\\%c", escapes[*str] );
2699 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2700 pos += sprintf( pos, "\\%03o", *str );
2701 else
2702 pos += sprintf( pos, "\\%o", *str );
2703 continue;
2705 if (*str == '\\') *pos++ = '\\';
2706 *pos++ = *str;
2708 fwrite( buffer, pos - buffer, 1, stdout );
2709 count += pos - buffer;
2710 return count;
2713 /* dump a Unicode string with proper escaping */
2714 static int dump_strW( const WCHAR *str, size_t len )
2716 static const char escapes[32] = ".......abtnvfr.............e....";
2717 char buffer[256];
2718 char *pos = buffer;
2719 int count = 0;
2721 for (; len; str++, len--)
2723 if (pos > buffer + sizeof(buffer) - 8)
2725 fwrite( buffer, pos - buffer, 1, stdout );
2726 count += pos - buffer;
2727 pos = buffer;
2729 if (*str > 127) /* hex escape */
2731 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2732 pos += sprintf( pos, "\\x%04x", *str );
2733 else
2734 pos += sprintf( pos, "\\x%x", *str );
2735 continue;
2737 if (*str < 32) /* octal or C escape */
2739 if (!*str && len == 1) continue; /* do not output terminating NULL */
2740 if (escapes[*str] != '.')
2741 pos += sprintf( pos, "\\%c", escapes[*str] );
2742 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2743 pos += sprintf( pos, "\\%03o", *str );
2744 else
2745 pos += sprintf( pos, "\\%o", *str );
2746 continue;
2748 if (*str == '\\') *pos++ = '\\';
2749 *pos++ = *str;
2751 fwrite( buffer, pos - buffer, 1, stdout );
2752 count += pos - buffer;
2753 return count;
2756 /* dump data for a STRING resource */
2757 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2759 int i;
2761 for (i = 0; i < 16 && size; i++)
2763 unsigned len = *ptr++;
2765 if (len >= size)
2767 len = size;
2768 size = 0;
2770 else size -= len + 1;
2772 if (len)
2774 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2775 dump_strW( ptr, len );
2776 printf( "\"\n" );
2777 ptr += len;
2782 /* dump data for a MESSAGETABLE resource */
2783 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2785 const MESSAGE_RESOURCE_DATA *data = ptr;
2786 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2787 unsigned i, j;
2789 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2791 const MESSAGE_RESOURCE_ENTRY *entry;
2793 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2794 for (j = block->LowId; j <= block->HighId; j++)
2796 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2798 const WCHAR *str = (const WCHAR *)entry->Text;
2799 printf( "%s%08x L\"", prefix, j );
2800 dump_strW( str, strlenW(str) );
2801 printf( "\"\n" );
2803 else
2805 const char *str = (const char *) entry->Text;
2806 printf( "%s%08x \"", prefix, j );
2807 dump_strA( entry->Text, strlen(str) );
2808 printf( "\"\n" );
2810 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2815 static void dump_dir_resource(void)
2817 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2818 const IMAGE_RESOURCE_DIRECTORY *namedir;
2819 const IMAGE_RESOURCE_DIRECTORY *langdir;
2820 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2821 const IMAGE_RESOURCE_DIR_STRING_U *string;
2822 const IMAGE_RESOURCE_DATA_ENTRY *data;
2823 int i, j, k;
2825 if (!root) return;
2827 printf( "Resources:" );
2829 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2831 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2832 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
2833 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2835 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2836 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
2837 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2839 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2841 printf( "\n " );
2842 if (e1->NameIsString)
2844 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
2845 dump_unicode_str( string->NameString, string->Length );
2847 else
2849 const char *type = get_resource_type( e1->Id );
2850 if (type) printf( "%s", type );
2851 else printf( "%04x", e1->Id );
2854 printf( " Name=" );
2855 if (e2->NameIsString)
2857 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
2858 dump_unicode_str( string->NameString, string->Length );
2860 else
2861 printf( "%04x", e2->Id );
2863 printf( " Language=%04x:\n", e3->Id );
2864 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
2865 if (e1->NameIsString)
2867 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2869 else switch(e1->Id)
2871 case 6:
2872 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2873 break;
2874 case 11:
2875 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2876 break;
2877 default:
2878 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2879 break;
2884 printf( "\n\n" );
2887 static void dump_debug(void)
2889 const char* stabs = NULL;
2890 unsigned szstabs = 0;
2891 const char* stabstr = NULL;
2892 unsigned szstr = 0;
2893 unsigned i;
2894 const IMAGE_SECTION_HEADER* sectHead;
2896 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2898 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2900 if (!strcmp((const char *)sectHead->Name, ".stab"))
2902 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2903 szstabs = sectHead->Misc.VirtualSize;
2905 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2907 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2908 szstr = sectHead->Misc.VirtualSize;
2911 if (stabs && stabstr)
2912 dump_stabs(stabs, szstabs, stabstr, szstr);
2915 static void dump_symbol_table(void)
2917 const IMAGE_SYMBOL* sym;
2918 int numsym;
2920 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2921 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2922 return;
2923 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2924 sizeof(*sym) * numsym);
2925 if (!sym) return;
2927 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2930 enum FileSig get_kind_exec(void)
2932 const WORD* pw;
2933 const DWORD* pdw;
2934 const IMAGE_DOS_HEADER* dh;
2936 pw = PRD(0, sizeof(WORD));
2937 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2939 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2941 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2943 /* the signature is the first DWORD */
2944 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2945 if (pdw)
2947 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2948 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2949 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2951 return SIG_DOS;
2953 return SIG_UNKNOWN;
2956 void pe_dump(void)
2958 int alt = 0;
2960 PE_nt_headers = get_nt_header();
2961 print_fake_dll();
2963 for (;;)
2965 if (alt)
2966 printf( "\n**** Alternate (%s) data ****\n\n",
2967 get_machine_str(PE_nt_headers->FileHeader.Machine));
2968 else if (get_dyn_reloc_table())
2969 printf( "**** Native (%s) data ****\n\n",
2970 get_machine_str(PE_nt_headers->FileHeader.Machine));
2972 if (globals.do_dumpheader)
2974 dump_pe_header();
2975 /* FIXME: should check ptr */
2976 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2977 PE_nt_headers->FileHeader.NumberOfSections);
2979 else if (!globals.dumpsect)
2981 /* show at least something here */
2982 dump_pe_header();
2985 if (globals_dump_sect("import"))
2987 dump_dir_imported_functions();
2988 dump_dir_delay_imported_functions();
2990 if (globals_dump_sect("export"))
2991 dump_dir_exported_functions();
2992 if (globals_dump_sect("debug"))
2993 dump_dir_debug();
2994 if (globals_dump_sect("resource"))
2995 dump_dir_resource();
2996 if (globals_dump_sect("tls"))
2997 dump_dir_tls();
2998 if (globals_dump_sect("loadcfg"))
2999 dump_dir_loadconfig();
3000 if (globals_dump_sect("clr"))
3001 dump_dir_clr_header();
3002 if (globals_dump_sect("reloc"))
3003 dump_dir_reloc();
3004 if (globals_dump_sect("dynreloc"))
3005 dump_dir_dynamic_reloc();
3006 if (globals_dump_sect("except"))
3007 dump_dir_exceptions();
3008 if (globals_dump_sect("apiset"))
3009 dump_section_apiset();
3011 if (globals.do_symbol_table)
3012 dump_symbol_table();
3013 if (globals.do_debug)
3014 dump_debug();
3015 if (alt++) break;
3016 if (!get_alt_header()) break;
3020 typedef struct _dll_symbol {
3021 size_t ordinal;
3022 char *symbol;
3023 } dll_symbol;
3025 static dll_symbol *dll_symbols = NULL;
3026 static dll_symbol *dll_current_symbol = NULL;
3028 /* Compare symbols by ordinal for qsort */
3029 static int symbol_cmp(const void *left, const void *right)
3031 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
3034 /*******************************************************************
3035 * dll_close
3037 * Free resources used by DLL
3039 /* FIXME: Not used yet
3040 static void dll_close (void)
3042 dll_symbol* ds;
3044 if (!dll_symbols) {
3045 fatal("No symbols");
3047 for (ds = dll_symbols; ds->symbol; ds++)
3048 free(ds->symbol);
3049 free (dll_symbols);
3050 dll_symbols = NULL;
3054 static void do_grab_sym( void )
3056 const IMAGE_EXPORT_DIRECTORY*exportDir;
3057 UINT i, j, *map;
3058 const UINT *pName;
3059 const UINT *pFunc;
3060 const WORD *pOrdl;
3061 const char *ptr;
3063 PE_nt_headers = get_nt_header();
3064 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
3066 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
3067 if (!pName) {printf("Can't grab functions' name table\n"); return;}
3068 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
3069 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
3070 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
3071 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
3073 /* dll_close(); */
3075 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
3077 /* bit map of used funcs */
3078 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
3079 if (!map) fatal("no memory");
3081 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
3083 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
3084 ptr = RVA(*pName++, sizeof(DWORD));
3085 if (!ptr) ptr = "cant_get_function";
3086 dll_symbols[j].symbol = xstrdup(ptr);
3087 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
3088 assert(dll_symbols[j].symbol);
3091 for (i = 0; i < exportDir->NumberOfFunctions; i++)
3093 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
3095 char ordinal_text[256];
3096 /* Ordinal only entry */
3097 sprintf (ordinal_text, "%s_%u",
3098 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
3099 (UINT)exportDir->Base + i);
3100 str_toupper(ordinal_text);
3101 dll_symbols[j].symbol = xstrdup(ordinal_text);
3102 assert(dll_symbols[j].symbol);
3103 dll_symbols[j].ordinal = exportDir->Base + i;
3104 j++;
3105 assert(j <= exportDir->NumberOfFunctions);
3108 free(map);
3110 if (NORMAL)
3111 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
3112 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
3113 j, (UINT)exportDir->Base);
3115 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
3117 dll_symbols[j].symbol = NULL;
3119 dll_current_symbol = dll_symbols;
3122 /*******************************************************************
3123 * dll_open
3125 * Open a DLL and read in exported symbols
3127 BOOL dll_open (const char *dll_name)
3129 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
3132 /*******************************************************************
3133 * dll_next_symbol
3135 * Get next exported symbol from dll
3137 BOOL dll_next_symbol (parsed_symbol * sym)
3139 if (!dll_current_symbol || !dll_current_symbol->symbol)
3140 return FALSE;
3141 assert (dll_symbols);
3142 sym->symbol = xstrdup (dll_current_symbol->symbol);
3143 sym->ordinal = dll_current_symbol->ordinal;
3144 dll_current_symbol++;
3145 return TRUE;