wined3d: Move struct wined3d_sampler_gl to wined3d_gl.h.
[wine.git] / tools / winedump / pe.c
blobfbcd9f5c6a5106dd4a18251b083259c1c5d7005e
1 /*
2 * PE dumping utility
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <fcntl.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "verrsrc.h"
32 #include "winedump.h"
34 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
36 static const IMAGE_NT_HEADERS32* PE_nt_headers;
38 static const char builtin_signature[] = "Wine builtin DLL";
39 static const char fakedll_signature[] = "Wine placeholder DLL";
40 static int is_builtin;
42 const char *get_machine_str(int mach)
44 switch (mach)
46 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
47 case IMAGE_FILE_MACHINE_I386: return "i386";
48 case IMAGE_FILE_MACHINE_R3000: return "R3000";
49 case IMAGE_FILE_MACHINE_R4000: return "R4000";
50 case IMAGE_FILE_MACHINE_R10000: return "R10000";
51 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
52 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
53 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
54 case IMAGE_FILE_MACHINE_IA64: return "IA64";
55 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
56 case IMAGE_FILE_MACHINE_ARM: return "ARM";
57 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
58 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
59 case IMAGE_FILE_MACHINE_ALPHA64: return "Alpha64";
60 case IMAGE_FILE_MACHINE_CHPE_X86: return "CHPE-x86";
61 case IMAGE_FILE_MACHINE_ARM64EC: return "ARM64EC";
62 case IMAGE_FILE_MACHINE_ARM64X: return "ARM64X";
63 case IMAGE_FILE_MACHINE_RISCV32: return "RISC-V 32-bit";
64 case IMAGE_FILE_MACHINE_RISCV64: return "RISC-V 64-bit";
65 case IMAGE_FILE_MACHINE_RISCV128: return "RISC-V 128-bit";
67 return "???";
70 static const void* RVA(unsigned long rva, unsigned long len)
72 IMAGE_SECTION_HEADER* sectHead;
73 int i;
75 if (rva == 0) return NULL;
77 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
78 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
80 if (sectHead[i].VirtualAddress <= rva &&
81 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
83 /* return image import directory offset */
84 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
88 return NULL;
91 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
93 const IMAGE_DOS_HEADER *dos;
94 dos = PRD(0, sizeof(*dos));
95 if (!dos) return NULL;
96 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
97 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
98 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
101 void print_fake_dll( void )
103 const IMAGE_DOS_HEADER *dos;
105 dos = PRD(0, sizeof(*dos) + 32);
106 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
108 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
109 printf( "*** This is a Wine builtin DLL ***\n\n" );
110 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
111 printf( "*** This is a Wine fake DLL ***\n\n" );
115 static const void *get_data_dir(const IMAGE_NT_HEADERS32 *hdr, unsigned int idx, unsigned int *size)
117 if(hdr->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
119 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&hdr->OptionalHeader;
120 if (idx >= opt->NumberOfRvaAndSizes)
121 return NULL;
122 if(size)
123 *size = opt->DataDirectory[idx].Size;
124 return RVA(opt->DataDirectory[idx].VirtualAddress,
125 opt->DataDirectory[idx].Size);
127 else
129 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&hdr->OptionalHeader;
130 if (idx >= opt->NumberOfRvaAndSizes)
131 return NULL;
132 if(size)
133 *size = opt->DataDirectory[idx].Size;
134 return RVA(opt->DataDirectory[idx].VirtualAddress,
135 opt->DataDirectory[idx].Size);
139 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
141 return get_data_dir( PE_nt_headers, idx, size );
144 static const void* get_dir(unsigned idx)
146 return get_dir_and_size(idx, 0);
149 static const char * const DirectoryNames[16] = {
150 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
151 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
152 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
153 "IAT", "Delay IAT", "CLR Header", ""
156 static const char *get_magic_type(WORD magic)
158 switch(magic) {
159 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
160 return "32bit";
161 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
162 return "64bit";
163 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
164 return "ROM";
166 return "???";
169 static const void *get_hybrid_metadata(void)
171 unsigned int size;
173 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
175 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
176 if (!cfg) return 0;
177 size = min( size, cfg->Size );
178 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CHPEMetadataPointer )) return 0;
179 return RVA( cfg->CHPEMetadataPointer - ((const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader)->ImageBase, 1 );
181 else
183 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
184 if (!cfg) return 0;
185 size = min( size, cfg->Size );
186 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CHPEMetadataPointer )) return 0;
187 return RVA( cfg->CHPEMetadataPointer - PE_nt_headers->OptionalHeader.ImageBase, 1 );
191 static inline const char *longlong_str( ULONGLONG value )
193 static char buffer[20];
195 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
196 sprintf(buffer, "%lx%08lx", (unsigned long)(value >> 32), (unsigned long)value);
197 else
198 sprintf(buffer, "%lx", (unsigned long)value);
199 return buffer;
202 static inline void print_word(const char *title, WORD value)
204 printf(" %-34s 0x%-4X %u\n", title, value, value);
207 static inline void print_dword(const char *title, UINT value)
209 printf(" %-34s 0x%-8x %u\n", title, value, value);
212 static inline void print_longlong(const char *title, ULONGLONG value)
214 printf(" %-34s 0x%s\n", title, longlong_str(value));
217 static inline void print_ver(const char *title, BYTE major, BYTE minor)
219 printf(" %-34s %u.%02u\n", title, major, minor);
222 static inline void print_subsys(const char *title, WORD value)
224 const char *str;
225 switch (value)
227 default:
228 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
229 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
230 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
231 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
232 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
233 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
234 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
235 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
236 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
237 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
238 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
239 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
240 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
241 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
243 printf(" %-34s 0x%X (%s)\n", title, value, str);
246 static inline void print_dllflags(const char *title, WORD value)
248 printf(" %-34s 0x%04X\n", title, value);
249 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
250 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
251 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
252 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
253 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
254 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
255 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
256 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
257 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
258 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
259 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
260 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
261 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
262 #undef X
265 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
267 unsigned i;
268 printf("Data Directory\n");
270 for (i = 0; i < n && i < 16; i++)
272 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
273 DirectoryNames[i], (UINT)directory[i].VirtualAddress,
274 (UINT)directory[i].Size);
278 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
280 IMAGE_OPTIONAL_HEADER32 oh;
281 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
283 /* in case optional header is missing or partial */
284 memset(&oh, 0, sizeof(oh));
285 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
286 optionalHeader = &oh;
288 print_word("Magic", optionalHeader->Magic);
289 print_ver("linker version",
290 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
291 print_dword("size of code", optionalHeader->SizeOfCode);
292 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
293 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
294 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
295 print_dword("base of code", optionalHeader->BaseOfCode);
296 print_dword("base of data", optionalHeader->BaseOfData);
297 print_dword("image base", optionalHeader->ImageBase);
298 print_dword("section align", optionalHeader->SectionAlignment);
299 print_dword("file align", optionalHeader->FileAlignment);
300 print_ver("required OS version",
301 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
302 print_ver("image version",
303 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
304 print_ver("subsystem version",
305 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
306 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
307 print_dword("size of image", optionalHeader->SizeOfImage);
308 print_dword("size of headers", optionalHeader->SizeOfHeaders);
309 print_dword("checksum", optionalHeader->CheckSum);
310 print_subsys("Subsystem", optionalHeader->Subsystem);
311 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
312 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
313 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
314 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
315 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
316 print_dword("loader flags", optionalHeader->LoaderFlags);
317 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
318 printf("\n");
319 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
320 printf("\n");
323 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
325 IMAGE_OPTIONAL_HEADER64 oh;
326 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
328 /* in case optional header is missing or partial */
329 memset(&oh, 0, sizeof(oh));
330 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
331 optionalHeader = &oh;
333 print_word("Magic", optionalHeader->Magic);
334 print_ver("linker version",
335 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
336 print_dword("size of code", optionalHeader->SizeOfCode);
337 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
338 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
339 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
340 print_dword("base of code", optionalHeader->BaseOfCode);
341 print_longlong("image base", optionalHeader->ImageBase);
342 print_dword("section align", optionalHeader->SectionAlignment);
343 print_dword("file align", optionalHeader->FileAlignment);
344 print_ver("required OS version",
345 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
346 print_ver("image version",
347 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
348 print_ver("subsystem version",
349 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
350 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
351 print_dword("size of image", optionalHeader->SizeOfImage);
352 print_dword("size of headers", optionalHeader->SizeOfHeaders);
353 print_dword("checksum", optionalHeader->CheckSum);
354 print_subsys("Subsystem", optionalHeader->Subsystem);
355 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
356 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
357 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
358 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
359 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
360 print_dword("loader flags", optionalHeader->LoaderFlags);
361 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
362 printf("\n");
363 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
364 printf("\n");
367 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
369 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
371 switch(optionalHeader->Magic) {
372 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
373 dump_optional_header32(optionalHeader, header_size);
374 break;
375 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
376 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
377 break;
378 default:
379 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
380 break;
384 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader, BOOL is_hybrid)
386 const char *name = get_machine_str(fileHeader->Machine);
388 printf("File Header\n");
390 if (is_hybrid)
392 switch (fileHeader->Machine)
394 case IMAGE_FILE_MACHINE_I386: name = "CHPE"; break;
395 case IMAGE_FILE_MACHINE_AMD64: name = "ARM64EC"; break;
396 case IMAGE_FILE_MACHINE_ARM64: name = "ARM64X"; break;
399 printf(" Machine: %04X (%s)\n", fileHeader->Machine, name);
400 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
401 printf(" TimeDateStamp: %08X (%s)\n",
402 (UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp));
403 printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
404 printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
405 printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
406 printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
407 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
408 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
409 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
410 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
411 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
412 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
413 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
414 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
415 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
416 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
417 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
418 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
419 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
420 X(IMAGE_FILE_SYSTEM, "SYSTEM");
421 X(IMAGE_FILE_DLL, "DLL");
422 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
423 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
424 #undef X
425 printf("\n");
428 static void dump_pe_header(void)
430 dump_file_header(&PE_nt_headers->FileHeader, get_hybrid_metadata() != NULL);
431 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader,
432 PE_nt_headers->FileHeader.SizeOfOptionalHeader);
435 void dump_section_characteristics(DWORD characteristics, const char* sep)
437 #define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
438 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
439 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
440 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
441 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
442 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
443 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
445 X(IMAGE_SCN_CNT_CODE, "CODE");
446 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
447 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
449 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
450 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
451 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
452 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
453 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
455 /* 0x00002000 - Reserved */
456 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
457 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
459 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
460 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
461 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
462 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
463 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
465 switch (characteristics & IMAGE_SCN_ALIGN_MASK)
467 #define X2(b,s) case b: printf("%s%s", sep, s); break
468 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
469 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
470 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
471 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
472 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
473 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
474 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
475 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
476 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
477 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
478 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
479 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
480 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
481 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
482 #undef X2
485 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
487 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
488 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
489 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
490 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
491 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
492 X(IMAGE_SCN_MEM_READ, "MEM_READ");
493 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
494 #undef X
497 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
499 unsigned offset;
501 /* long section name ? */
502 if (strtable && sectHead->Name[0] == '/' &&
503 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
504 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
505 else
506 printf(" %-8.8s", sectHead->Name);
507 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
508 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
509 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
510 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
511 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
512 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
513 printf(" line # offs: %-8u line #'s: %-8u\n",
514 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
515 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
516 printf(" ");
517 dump_section_characteristics(sectHead->Characteristics, " ");
519 printf("\n\n");
522 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
524 const IMAGE_SECTION_HEADER* sectHead = addr;
525 unsigned i;
526 const char* strtable;
528 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
530 strtable = (const char*)base +
531 PE_nt_headers->FileHeader.PointerToSymbolTable +
532 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
534 else strtable = NULL;
536 printf("Section Table\n");
537 for (i = 0; i < num_sect; i++, sectHead++)
539 dump_section(sectHead, strtable);
541 if (globals.do_dump_rawdata)
543 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
544 printf("\n");
549 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
551 const WCHAR *wstr = PRD( rva, len );
552 char *ret = buffer;
554 len /= sizeof(WCHAR);
555 while (len--) *buffer++ = *wstr++;
556 *buffer = 0;
557 return ret;
560 static void dump_section_apiset(void)
562 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
563 const UINT *ptr, *entry, *value, *hash;
564 unsigned int i, j, count, val_count, rva;
565 char buffer[128];
567 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
569 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
570 rva = sect->PointerToRawData;
571 ptr = PRD( rva, sizeof(*ptr) );
572 printf( "ApiSet section:\n" );
573 switch (ptr[0]) /* version */
575 case 2:
576 printf( " Version: %u\n", ptr[0] );
577 printf( " Count: %08x\n", ptr[1] );
578 count = ptr[1];
579 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
580 for (i = 0; i < count; i++, entry += 3)
582 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
583 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
584 val_count = *value++;
585 for (j = 0; j < val_count; j++, value += 4)
587 putchar( ' ' );
588 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
589 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
591 printf( "\n");
593 break;
594 case 4:
595 printf( " Version: %u\n", ptr[0] );
596 printf( " Size: %08x\n", ptr[1] );
597 printf( " Flags: %08x\n", ptr[2] );
598 printf( " Count: %08x\n", ptr[3] );
599 count = ptr[3];
600 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
601 for (i = 0; i < count; i++, entry += 6)
603 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
604 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
605 value++; /* flags */
606 val_count = *value++;
607 for (j = 0; j < val_count; j++, value += 5)
609 putchar( ' ' );
610 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
611 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
613 printf( "\n");
615 break;
616 case 6:
617 printf( " Version: %u\n", ptr[0] );
618 printf( " Size: %08x\n", ptr[1] );
619 printf( " Flags: %08x\n", ptr[2] );
620 printf( " Count: %08x\n", ptr[3] );
621 printf( " EntryOffset: %08x\n", ptr[4] );
622 printf( " HashOffset: %08x\n", ptr[5] );
623 printf( " HashFactor: %08x\n", ptr[6] );
624 count = ptr[3];
625 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
626 for (i = 0; i < count; i++, entry += 6)
628 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
629 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
630 for (j = 0; j < entry[5]; j++, value += 5)
632 putchar( ' ' );
633 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
634 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
636 printf( "\n" );
638 printf( " Hash table:\n" );
639 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
640 for (i = 0; i < count; i++, hash += 2)
642 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
643 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
645 break;
646 default:
647 printf( "*** Unknown version %u\n", ptr[0] );
648 break;
650 break;
654 static const char *find_export_from_rva( UINT rva )
656 UINT i, *func_names;
657 const UINT *funcs;
658 const UINT *names;
659 const WORD *ordinals;
660 const IMAGE_EXPORT_DIRECTORY *dir;
661 const char *ret = NULL;
663 if (!(dir = get_dir( IMAGE_FILE_EXPORT_DIRECTORY ))) return NULL;
664 if (!(funcs = RVA( dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD) ))) return NULL;
665 names = RVA( dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD) );
666 ordinals = RVA( dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD) );
667 func_names = calloc( dir->NumberOfFunctions, sizeof(*func_names) );
669 for (i = 0; i < dir->NumberOfNames; i++) func_names[ordinals[i]] = names[i];
670 for (i = 0; i < dir->NumberOfFunctions && !ret; i++)
671 if (funcs[i] == rva) ret = get_symbol_str( RVA( func_names[i], sizeof(DWORD) ));
673 free( func_names );
674 if (!ret && rva == PE_nt_headers->OptionalHeader.AddressOfEntryPoint) return "<EntryPoint>";
675 return ret;
678 static void dump_dir_exported_functions(void)
680 unsigned int size;
681 const IMAGE_EXPORT_DIRECTORY *dir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
682 UINT i, *funcs;
683 const UINT *pFunc;
684 const UINT *pName;
685 const WORD *pOrdl;
687 if (!dir) return;
689 printf("\n");
690 printf(" Name: %s\n", (const char*)RVA(dir->Name, sizeof(DWORD)));
691 printf(" Characteristics: %08x\n", (UINT)dir->Characteristics);
692 printf(" TimeDateStamp: %08X %s\n",
693 (UINT)dir->TimeDateStamp, get_time_str(dir->TimeDateStamp));
694 printf(" Version: %u.%02u\n", dir->MajorVersion, dir->MinorVersion);
695 printf(" Ordinal base: %u\n", (UINT)dir->Base);
696 printf(" # of functions: %u\n", (UINT)dir->NumberOfFunctions);
697 printf(" # of Names: %u\n", (UINT)dir->NumberOfNames);
698 printf("Addresses of functions: %08X\n", (UINT)dir->AddressOfFunctions);
699 printf("Addresses of name ordinals: %08X\n", (UINT)dir->AddressOfNameOrdinals);
700 printf("Addresses of names: %08X\n", (UINT)dir->AddressOfNames);
701 printf("\n");
702 printf(" Entry Pt Ordn Name\n");
704 pFunc = RVA(dir->AddressOfFunctions, dir->NumberOfFunctions * sizeof(DWORD));
705 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
706 pName = RVA(dir->AddressOfNames, dir->NumberOfNames * sizeof(DWORD));
707 pOrdl = RVA(dir->AddressOfNameOrdinals, dir->NumberOfNames * sizeof(WORD));
709 funcs = calloc( dir->NumberOfFunctions, sizeof(*funcs) );
710 if (!funcs) fatal("no memory");
712 for (i = 0; i < dir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
714 for (i = 0; i < dir->NumberOfFunctions; i++)
716 if (!pFunc[i]) continue;
717 printf(" %08X %5u ", pFunc[i], (UINT)dir->Base + i);
718 if (funcs[i])
719 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
720 else
721 printf("<by ordinal>");
723 /* check for forwarded function */
724 if ((const char *)RVA(pFunc[i],1) >= (const char *)dir &&
725 (const char *)RVA(pFunc[i],1) < (const char *)dir + size)
726 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
727 printf("\n");
729 free(funcs);
730 printf("\n");
734 struct runtime_function_x86_64
736 UINT BeginAddress;
737 UINT EndAddress;
738 UINT UnwindData;
741 struct runtime_function_armnt
743 UINT BeginAddress;
744 union {
745 UINT UnwindData;
746 struct {
747 UINT Flag : 2;
748 UINT FunctionLength : 11;
749 UINT Ret : 2;
750 UINT H : 1;
751 UINT Reg : 3;
752 UINT R : 1;
753 UINT L : 1;
754 UINT C : 1;
755 UINT StackAdjust : 10;
760 struct runtime_function_arm64
762 UINT BeginAddress;
763 union
765 UINT UnwindData;
766 struct
768 UINT Flag : 2;
769 UINT FunctionLength : 11;
770 UINT RegF : 3;
771 UINT RegI : 4;
772 UINT H : 1;
773 UINT CR : 2;
774 UINT FrameSize : 9;
779 union handler_data
781 struct runtime_function_x86_64 chain;
782 UINT handler;
785 struct opcode
787 BYTE offset;
788 BYTE code : 4;
789 BYTE info : 4;
792 struct unwind_info_x86_64
794 BYTE version : 3;
795 BYTE flags : 5;
796 BYTE prolog;
797 BYTE count;
798 BYTE frame_reg : 4;
799 BYTE frame_offset : 4;
800 struct opcode opcodes[1]; /* count entries */
801 /* followed by union handler_data */
804 struct unwind_info_armnt
806 UINT function_length : 18;
807 UINT version : 2;
808 UINT x : 1;
809 UINT e : 1;
810 UINT f : 1;
811 UINT count : 5;
812 UINT words : 4;
815 struct unwind_info_ext_armnt
817 WORD excount;
818 BYTE exwords;
819 BYTE reserved;
822 struct unwind_info_epilogue_armnt
824 UINT offset : 18;
825 UINT res : 2;
826 UINT cond : 4;
827 UINT index : 8;
830 #define UWOP_PUSH_NONVOL 0
831 #define UWOP_ALLOC_LARGE 1
832 #define UWOP_ALLOC_SMALL 2
833 #define UWOP_SET_FPREG 3
834 #define UWOP_SAVE_NONVOL 4
835 #define UWOP_SAVE_NONVOL_FAR 5
836 #define UWOP_SAVE_XMM128 8
837 #define UWOP_SAVE_XMM128_FAR 9
838 #define UWOP_PUSH_MACHFRAME 10
840 #define UNW_FLAG_EHANDLER 1
841 #define UNW_FLAG_UHANDLER 2
842 #define UNW_FLAG_CHAININFO 4
844 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
846 static const char * const reg_names[16] =
847 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
848 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
850 const union handler_data *handler_data;
851 const struct unwind_info_x86_64 *info;
852 unsigned int i, count;
854 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
855 if (function->UnwindData & 1)
857 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
858 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
859 return;
861 info = RVA( function->UnwindData, sizeof(*info) );
863 printf( " unwind info at %08x\n", function->UnwindData );
864 if (info->version != 1)
866 printf( " *** unknown version %u\n", info->version );
867 return;
869 printf( " flags %x", info->flags );
870 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
871 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
872 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
873 printf( "\n prolog 0x%x bytes\n", info->prolog );
875 if (info->frame_reg)
876 printf( " frame register %s offset 0x%x(%%rsp)\n",
877 reg_names[info->frame_reg], info->frame_offset * 16 );
879 for (i = 0; i < info->count; i++)
881 printf( " 0x%02x: ", info->opcodes[i].offset );
882 switch (info->opcodes[i].code)
884 case UWOP_PUSH_NONVOL:
885 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
886 break;
887 case UWOP_ALLOC_LARGE:
888 if (info->opcodes[i].info)
890 count = *(const UINT *)&info->opcodes[i+1];
891 i += 2;
893 else
895 count = *(const USHORT *)&info->opcodes[i+1] * 8;
896 i++;
898 printf( "sub $0x%x,%%rsp\n", count );
899 break;
900 case UWOP_ALLOC_SMALL:
901 count = (info->opcodes[i].info + 1) * 8;
902 printf( "sub $0x%x,%%rsp\n", count );
903 break;
904 case UWOP_SET_FPREG:
905 printf( "lea 0x%x(%%rsp),%s\n",
906 info->frame_offset * 16, reg_names[info->frame_reg] );
907 break;
908 case UWOP_SAVE_NONVOL:
909 count = *(const USHORT *)&info->opcodes[i+1] * 8;
910 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
911 i++;
912 break;
913 case UWOP_SAVE_NONVOL_FAR:
914 count = *(const UINT *)&info->opcodes[i+1];
915 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
916 i += 2;
917 break;
918 case UWOP_SAVE_XMM128:
919 count = *(const USHORT *)&info->opcodes[i+1] * 16;
920 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
921 i++;
922 break;
923 case UWOP_SAVE_XMM128_FAR:
924 count = *(const UINT *)&info->opcodes[i+1];
925 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
926 i += 2;
927 break;
928 case UWOP_PUSH_MACHFRAME:
929 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
930 break;
931 default:
932 printf( "*** unknown code %u\n", info->opcodes[i].code );
933 break;
937 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
938 if (info->flags & UNW_FLAG_CHAININFO)
940 printf( " -> function %08x-%08x\n",
941 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
942 return;
944 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
945 printf( " handler %08x data at %08x\n", handler_data->handler,
946 (UINT)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
949 static const BYTE armnt_code_lengths[256] =
951 /* 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,
952 /* 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,
953 /* 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,
954 /* 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,
955 /* 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,
956 /* 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,
957 /* 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,
958 /* 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
961 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
963 const struct unwind_info_armnt *info;
964 const struct unwind_info_ext_armnt *infoex;
965 const struct unwind_info_epilogue_armnt *infoepi;
966 unsigned int rva;
967 WORD i, count = 0, words = 0;
969 if (fnc->Flag)
971 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
972 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
974 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
975 (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2 );
976 printf( " Flag %x\n", fnc->Flag );
977 printf( " FunctionLength %x\n", fnc->FunctionLength );
978 printf( " Ret %x\n", fnc->Ret );
979 printf( " H %x\n", fnc->H );
980 printf( " Reg %x\n", fnc->Reg );
981 printf( " R %x\n", fnc->R );
982 printf( " L %x\n", fnc->L );
983 printf( " C %x\n", fnc->C );
984 printf( " StackAdjust %x\n", fnc->StackAdjust );
986 if (fnc->StackAdjust >= 0x03f4)
988 pf = fnc->StackAdjust & 0x04;
989 ef = fnc->StackAdjust & 0x08;
990 stack = (fnc->StackAdjust & 3) + 1;
993 if (!fnc->R || pf)
995 int first = 4, last = fnc->Reg + 4;
996 if (pf)
998 first = (~fnc->StackAdjust) & 3;
999 if (fnc->R)
1000 last = 3;
1002 if (first == last)
1003 sprintf(intregs, "r%u", first);
1004 else
1005 sprintf(intregs, "r%u-r%u", first, last);
1006 fpoffset = last + 1 - first;
1009 if (!fnc->R || ef)
1011 int first = 4, last = fnc->Reg + 4;
1012 if (ef)
1014 first = (~fnc->StackAdjust) & 3;
1015 if (fnc->R)
1016 last = 3;
1018 if (first == last)
1019 sprintf(intregspop, "r%u", first);
1020 else
1021 sprintf(intregspop, "r%u-r%u", first, last);
1024 if (fnc->C)
1026 if (intregs[0])
1027 strcat(intregs, ", ");
1028 if (intregspop[0])
1029 strcat(intregspop, ", ");
1030 strcat(intregs, "r11");
1031 strcat(intregspop, "r11");
1033 if (fnc->L)
1035 if (intregs[0])
1036 strcat(intregs, ", ");
1037 strcat(intregs, "lr");
1039 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
1040 strcat(intregspop, ", ");
1041 if (fnc->Ret != 0)
1042 strcat(intregspop, "lr");
1043 else if (!fnc->H)
1044 strcat(intregspop, "pc");
1047 if (fnc->R)
1049 if (fnc->Reg)
1050 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
1051 else
1052 strcpy(vfpregs, "d8");
1055 if (fnc->Flag == 1) {
1056 if (fnc->H)
1057 printf( " Unwind Code\tpush {r0-r3}\n" );
1059 if (intregs[0])
1060 printf( " Unwind Code\tpush {%s}\n", intregs );
1062 if (fnc->C && fpoffset == 0)
1063 printf( " Unwind Code\tmov r11, sp\n" );
1064 else if (fnc->C)
1065 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
1067 if (fnc->R && fnc->Reg != 0x07)
1068 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
1070 if (stack && !pf)
1071 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
1074 if (fnc->Ret == 3)
1075 return;
1076 printf( "Epilogue:\n" );
1078 if (stack && !ef)
1079 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1081 if (fnc->R && fnc->Reg != 0x07)
1082 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1084 if (intregspop[0])
1085 printf( " Unwind Code\tpop {%s}\n", intregspop );
1087 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1088 printf( " Unwind Code\tadd sp, sp, #16\n" );
1089 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1090 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1092 if (fnc->Ret == 1)
1093 printf( " Unwind Code\tbx <reg>\n" );
1094 else if (fnc->Ret == 2)
1095 printf( " Unwind Code\tb <address>\n" );
1097 return;
1100 info = RVA( fnc->UnwindData, sizeof(*info) );
1101 rva = fnc->UnwindData + sizeof(*info);
1102 count = info->count;
1103 words = info->words;
1105 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1106 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1107 printf( " unwind info at %08x\n", fnc->UnwindData );
1108 printf( " Flag %x\n", fnc->Flag );
1109 printf( " FunctionLength %x\n", info->function_length );
1110 printf( " Version %x\n", info->version );
1111 printf( " X %x\n", info->x );
1112 printf( " E %x\n", info->e );
1113 printf( " F %x\n", info->f );
1114 printf( " Count %x\n", count );
1115 printf( " Words %x\n", words );
1117 if (!info->count && !info->words)
1119 infoex = RVA( rva, sizeof(*infoex) );
1120 rva = rva + sizeof(*infoex);
1121 count = infoex->excount;
1122 words = infoex->exwords;
1123 printf( " ExtCount %x\n", count );
1124 printf( " ExtWords %x\n", words );
1127 if (!info->e)
1129 infoepi = RVA( rva, count * sizeof(*infoepi) );
1130 rva = rva + count * sizeof(*infoepi);
1132 for (i = 0; i < count; i++)
1134 printf( " Epilogue Scope %x\n", i );
1135 printf( " Offset %x\n", infoepi[i].offset );
1136 printf( " Reserved %x\n", infoepi[i].res );
1137 printf( " Condition %x\n", infoepi[i].cond );
1138 printf( " Index %x\n", infoepi[i].index );
1141 else
1142 infoepi = NULL;
1144 if (words)
1146 const unsigned int *codes;
1147 BYTE b, *bytes;
1148 BOOL inepilogue = FALSE;
1150 codes = RVA( rva, words * sizeof(*codes) );
1151 rva = rva + words * sizeof(*codes);
1152 bytes = (BYTE*)codes;
1154 for (b = 0; b < words * sizeof(*codes); b++)
1156 BYTE code = bytes[b];
1157 BYTE len = armnt_code_lengths[code];
1159 if (info->e && b == count)
1161 printf( "Epilogue:\n" );
1162 inepilogue = TRUE;
1164 else if (!info->e && infoepi)
1166 for (i = 0; i < count; i++)
1167 if (b == infoepi[i].index)
1169 printf( "Epilogue from Scope %x at %08x:\n", i,
1170 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1171 inepilogue = TRUE;
1175 printf( " Unwind Code");
1176 for (i = 0; i < len; i++)
1177 printf( " %02x", bytes[b+i] );
1178 printf( "\t" );
1180 if (code == 0x00)
1181 printf( "\n" );
1182 else if (code <= 0x7f)
1183 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1184 else if (code <= 0xbf)
1186 WORD excode, f;
1187 BOOL first = TRUE;
1188 BYTE excodes = bytes[++b];
1190 excode = (code << 8) | excodes;
1191 printf( "%s {", inepilogue ? "pop" : "push" );
1193 for (f = 0; f <= 12; f++)
1195 if ((excode >> f) & 1)
1197 printf( "%sr%u", first ? "" : ", ", f );
1198 first = FALSE;
1202 if (excode & 0x2000)
1203 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1205 printf( "}\n" );
1207 else if (code <= 0xcf)
1208 if (inepilogue)
1209 printf( "mov sp, r%u\n", code & 0x0f );
1210 else
1211 printf( "mov r%u, sp\n", code & 0x0f );
1212 else if (code <= 0xd7)
1213 if (inepilogue)
1214 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1215 else
1216 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1217 else if (code <= 0xdf)
1218 if (inepilogue)
1219 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1220 else
1221 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1222 else if (code <= 0xe7)
1223 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1224 else if (code <= 0xeb)
1226 WORD excode;
1227 BYTE excodes = bytes[++b];
1229 excode = (code << 8) | excodes;
1230 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1232 else if (code <= 0xed)
1234 WORD excode, f;
1235 BOOL first = TRUE;
1236 BYTE excodes = bytes[++b];
1238 excode = (code << 8) | excodes;
1239 printf( "%s {", inepilogue ? "pop" : "push" );
1241 for (f = 0; f < 8; f++)
1243 if ((excode >> f) & 1)
1245 printf( "%sr%u", first ? "" : ", ", f );
1246 first = FALSE;
1250 if (excode & 0x0100)
1251 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1253 printf( "}\n" );
1255 else if (code == 0xee)
1256 printf( "unknown 16\n" );
1257 else if (code == 0xef)
1259 WORD excode;
1260 BYTE excodes = bytes[++b];
1262 if (excodes <= 0x0f)
1264 excode = (code << 8) | excodes;
1265 if (inepilogue)
1266 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1267 else
1268 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1270 else
1271 printf( "unknown 32\n" );
1273 else if (code <= 0xf4)
1274 printf( "unknown\n" );
1275 else if (code <= 0xf6)
1277 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1278 BYTE excodes = bytes[++b];
1280 excode = (code << 8) | excodes;
1281 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1282 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1284 else if (code <= 0xf7)
1286 unsigned int excode;
1287 BYTE excodes[2];
1289 excodes[0] = bytes[++b];
1290 excodes[1] = bytes[++b];
1291 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1292 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1294 else if (code <= 0xf8)
1296 unsigned int excode;
1297 BYTE excodes[3];
1299 excodes[0] = bytes[++b];
1300 excodes[1] = bytes[++b];
1301 excodes[2] = bytes[++b];
1302 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1303 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1305 else if (code <= 0xf9)
1307 unsigned int excode;
1308 BYTE excodes[2];
1310 excodes[0] = bytes[++b];
1311 excodes[1] = bytes[++b];
1312 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1313 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1315 else if (code <= 0xfa)
1317 unsigned int excode;
1318 BYTE excodes[3];
1320 excodes[0] = bytes[++b];
1321 excodes[1] = bytes[++b];
1322 excodes[2] = bytes[++b];
1323 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1324 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1326 else if (code <= 0xfb)
1327 printf( "nop\n" );
1328 else if (code <= 0xfc)
1329 printf( "nop.w\n" );
1330 else if (code <= 0xfd)
1332 printf( "(end) nop\n" );
1333 inepilogue = TRUE;
1335 else if (code <= 0xfe)
1337 printf( "(end) nop.w\n" );
1338 inepilogue = TRUE;
1340 else
1342 printf( "end\n" );
1343 inepilogue = TRUE;
1348 if (info->x)
1350 const unsigned int *handler;
1352 handler = RVA( rva, sizeof(*handler) );
1353 rva = rva + sizeof(*handler);
1355 printf( " handler %08x data at %08x\n", *handler, rva);
1359 struct unwind_info_arm64
1361 UINT function_length : 18;
1362 UINT version : 2;
1363 UINT x : 1;
1364 UINT e : 1;
1365 UINT epilog : 5;
1366 UINT codes : 5;
1369 struct unwind_info_ext_arm64
1371 WORD epilog;
1372 BYTE codes;
1373 BYTE reserved;
1376 struct unwind_info_epilog_arm64
1378 UINT offset : 18;
1379 UINT res : 4;
1380 UINT index : 10;
1383 static const BYTE code_lengths[256] =
1385 /* 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,
1386 /* 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,
1387 /* 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,
1388 /* 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,
1389 /* 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,
1390 /* 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,
1391 /* 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,
1392 /* 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
1395 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1397 unsigned int i, j;
1399 for (i = 0; i < count; i += code_lengths[ptr[i]])
1401 BYTE len = code_lengths[ptr[i]];
1402 unsigned int val = ptr[i];
1403 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1404 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1406 printf( " %04x: ", i );
1407 for (j = 0; j < 4; j++)
1408 if (j < len) printf( "%02x ", ptr[i+j] );
1409 else printf( " " );
1411 if (ptr[i] < 0x20) /* alloc_s */
1413 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1415 else if (ptr[i] < 0x40) /* save_r19r20_x */
1417 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1419 else if (ptr[i] < 0x80) /* save_fplr */
1421 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1423 else if (ptr[i] < 0xc0) /* save_fplr_x */
1425 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1427 else if (ptr[i] < 0xc8) /* alloc_m */
1429 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1431 else if (ptr[i] < 0xcc) /* save_regp */
1433 int reg = 19 + ((val >> 6) & 0xf);
1434 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1436 else if (ptr[i] < 0xd0) /* save_regp_x */
1438 int reg = 19 + ((val >> 6) & 0xf);
1439 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1441 else if (ptr[i] < 0xd4) /* save_reg */
1443 int reg = 19 + ((val >> 6) & 0xf);
1444 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1446 else if (ptr[i] < 0xd6) /* save_reg_x */
1448 int reg = 19 + ((val >> 5) & 0xf);
1449 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1451 else if (ptr[i] < 0xd8) /* save_lrpair */
1453 int reg = 19 + 2 * ((val >> 6) & 0x7);
1454 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1456 else if (ptr[i] < 0xda) /* save_fregp */
1458 int reg = 8 + ((val >> 6) & 0x7);
1459 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1461 else if (ptr[i] < 0xdc) /* save_fregp_x */
1463 int reg = 8 + ((val >> 6) & 0x7);
1464 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1466 else if (ptr[i] < 0xde) /* save_freg */
1468 int reg = 8 + ((val >> 6) & 0x7);
1469 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1471 else if (ptr[i] == 0xde) /* save_freg_x */
1473 int reg = 8 + ((val >> 5) & 0x7);
1474 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1476 else if (ptr[i] == 0xe0) /* alloc_l */
1478 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1480 else if (ptr[i] == 0xe1) /* set_fp */
1482 printf( "mov x29,sp\n" );
1484 else if (ptr[i] == 0xe2) /* add_fp */
1486 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1488 else if (ptr[i] == 0xe3) /* nop */
1490 printf( "nop\n" );
1492 else if (ptr[i] == 0xe4) /* end */
1494 printf( "end\n" );
1496 else if (ptr[i] == 0xe5) /* end_c */
1498 printf( "end_c\n" );
1500 else if (ptr[i] == 0xe6) /* save_next */
1502 printf( "save_next\n" );
1504 else if (ptr[i] == 0xe7) /* arithmetic */
1506 switch ((val >> 4) & 0x0f)
1508 case 0: printf( "add lr,lr,x28\n" ); break;
1509 case 1: printf( "add lr,lr,sp\n" ); break;
1510 case 2: printf( "sub lr,lr,x28\n" ); break;
1511 case 3: printf( "sub lr,lr,sp\n" ); break;
1512 case 4: printf( "eor lr,lr,x28\n" ); break;
1513 case 5: printf( "eor lr,lr,sp\n" ); break;
1514 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1515 case 8: printf( "ror lr,lr,x28\n" ); break;
1516 case 9: printf( "ror lr,lr,sp\n" ); break;
1517 default:printf( "unknown op\n" ); break;
1520 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1522 printf( "MSFT_OP_TRAP_FRAME\n" );
1524 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1526 printf( "MSFT_OP_MACHINE_FRAME\n" );
1528 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1530 printf( "MSFT_OP_CONTEXT\n" );
1532 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1534 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1536 else printf( "??\n");
1540 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1542 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
1544 if (func->CR == 1) intsz += 8;
1545 if (func->RegF) fpsz += 8;
1547 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
1548 locsz = func->FrameSize * 16 - savesz;
1550 switch (func->CR)
1552 case 3:
1553 printf( " %04x: mov x29,sp\n", pos++ );
1554 if (locsz <= 512)
1556 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1557 break;
1559 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1560 /* fall through */
1561 case 0:
1562 case 1:
1563 if (locsz <= 4080)
1565 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1567 else
1569 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1570 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1572 break;
1575 if (func->H)
1577 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1578 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1579 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1580 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1583 if (func->RegF)
1585 if (func->RegF % 2 == 0)
1586 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
1587 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
1589 if (!i && !intsz)
1590 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1591 else
1592 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1596 switch (func->RegI)
1598 case 0:
1599 if (func->CR == 1)
1600 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1601 break;
1602 case 1:
1603 if (func->CR == 1)
1604 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1605 else
1606 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1607 break;
1608 default:
1609 if (func->RegI % 2)
1611 if (func->CR == 1)
1612 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1613 else
1614 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1616 else if (func->CR == 1)
1617 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1619 for (i = func->RegI / 2 - 1; i >= 0; i--)
1620 if (i)
1621 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1622 else
1623 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1624 break;
1626 printf( " %04x: end\n", pos );
1629 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1631 const struct unwind_info_arm64 *info;
1632 const struct unwind_info_ext_arm64 *infoex;
1633 const struct unwind_info_epilog_arm64 *infoepi;
1634 const BYTE *ptr;
1635 unsigned int i, rva, codes, epilogs;
1637 if (func->Flag)
1639 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1640 func->BeginAddress + func->FunctionLength * 4 );
1641 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1642 func->FunctionLength, func->Flag, func->RegF, func->RegI,
1643 func->H, func->CR, func->FrameSize );
1644 dump_arm64_packed_info( func );
1645 return;
1648 rva = func->UnwindData;
1649 info = RVA( rva, sizeof(*info) );
1650 rva += sizeof(*info);
1651 epilogs = info->epilog;
1652 codes = info->codes;
1654 if (!codes)
1656 infoex = RVA( rva, sizeof(*infoex) );
1657 rva = rva + sizeof(*infoex);
1658 codes = infoex->codes;
1659 epilogs = infoex->epilog;
1661 printf( "\nFunction %08x-%08x:\n",
1662 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1663 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1664 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1665 if (info->e)
1667 printf( " epilog 0: code=%04x\n", info->epilog );
1669 else
1671 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1672 rva += sizeof(*infoepi) * epilogs;
1673 for (i = 0; i < epilogs; i++)
1674 printf( " epilog %u: pc=%08x code=%04x\n", i,
1675 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1677 ptr = RVA( rva, codes * 4);
1678 rva += codes * 4;
1679 if (info->x)
1681 const UINT *handler = RVA( rva, sizeof(*handler) );
1682 rva += sizeof(*handler);
1683 printf( " handler: %08x data %08x\n", *handler, rva );
1685 dump_arm64_codes( ptr, codes * 4 );
1688 static void dump_dir_exceptions(void)
1690 unsigned int i, size;
1691 const void *funcs;
1692 const IMAGE_FILE_HEADER *file_header;
1694 funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1695 if (!funcs) return;
1697 file_header = &PE_nt_headers->FileHeader;
1699 switch (file_header->Machine)
1701 case IMAGE_FILE_MACHINE_AMD64:
1702 size /= sizeof(struct runtime_function_x86_64);
1703 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1704 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1705 break;
1706 case IMAGE_FILE_MACHINE_ARMNT:
1707 size /= sizeof(struct runtime_function_armnt);
1708 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1709 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1710 break;
1711 case IMAGE_FILE_MACHINE_ARM64:
1712 size /= sizeof(struct runtime_function_arm64);
1713 printf( "%s exception info (%u functions):\n", get_machine_str( file_header->Machine ), size );
1714 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1715 break;
1716 default:
1717 printf( "Exception information not supported for %s binaries\n",
1718 get_machine_str(file_header->Machine));
1719 break;
1721 printf( "\n" );
1725 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
1727 /* FIXME: This does not properly handle large images */
1728 const IMAGE_IMPORT_BY_NAME* iibn;
1729 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1731 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1732 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
1733 else
1735 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1736 if (!iibn)
1737 printf("Can't grab import by name info, skipping to next ordinal\n");
1738 else
1739 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1744 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
1746 const IMAGE_IMPORT_BY_NAME* iibn;
1747 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
1749 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1750 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
1751 else
1753 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1754 if (!iibn)
1755 printf("Can't grab import by name info, skipping to next ordinal\n");
1756 else
1757 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1762 static void dump_dir_imported_functions(void)
1764 unsigned directorySize;
1765 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1767 if (!importDesc) return;
1769 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1771 for (;;)
1773 const IMAGE_THUNK_DATA32* il;
1775 if (!importDesc->Name || !importDesc->FirstThunk) break;
1777 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1778 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
1779 printf(" TimeDateStamp: %08X (%s)\n",
1780 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1781 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
1782 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
1784 printf(" Thunk Ordn Name\n");
1786 il = (importDesc->OriginalFirstThunk != 0) ?
1787 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
1788 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1790 if (!il)
1791 printf("Can't grab thunk data, going to next imported DLL\n");
1792 else
1794 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1795 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1796 else
1797 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1798 printf("\n");
1800 importDesc++;
1802 printf("\n");
1805 static void dump_hybrid_metadata(void)
1807 unsigned int i;
1808 const void *metadata = get_hybrid_metadata();
1810 if (!metadata) return;
1811 printf( "Hybrid metadata\n" );
1813 switch (PE_nt_headers->FileHeader.Machine)
1815 case IMAGE_FILE_MACHINE_I386:
1817 const IMAGE_CHPE_METADATA_X86 *data = metadata;
1819 printf( " Version %#x\n", (int)data->Version );
1820 printf( " CHPECodeAddressRangeOffset %#x\n", (int)data->CHPECodeAddressRangeOffset );
1821 printf( " CHPECodeAddressRangeCount %#x\n", (int)data->CHPECodeAddressRangeCount );
1822 printf( " WowA64ExceptionHandlerFunctionPointer %#x\n", (int)data->WowA64ExceptionHandlerFunctionPointer );
1823 printf( " WowA64DispatchCallFunctionPointer %#x\n", (int)data->WowA64DispatchCallFunctionPointer );
1824 printf( " WowA64DispatchIndirectCallFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallFunctionPointer );
1825 printf( " WowA64DispatchIndirectCallCfgFunctionPointer %#x\n", (int)data->WowA64DispatchIndirectCallCfgFunctionPointer );
1826 printf( " WowA64DispatchRetFunctionPointer %#x\n", (int)data->WowA64DispatchRetFunctionPointer );
1827 printf( " WowA64DispatchRetLeafFunctionPointer %#x\n", (int)data->WowA64DispatchRetLeafFunctionPointer );
1828 printf( " WowA64DispatchJumpFunctionPointer %#x\n", (int)data->WowA64DispatchJumpFunctionPointer );
1829 if (data->Version >= 2)
1830 printf( " CompilerIATPointer %#x\n", (int)data->CompilerIATPointer );
1831 if (data->Version >= 3)
1832 printf( " WowA64RdtscFunctionPointer %#x\n", (int)data->WowA64RdtscFunctionPointer );
1833 if (data->Version >= 4)
1835 printf( " unknown[0] %#x\n", (int)data->unknown[0] );
1836 printf( " unknown[1] %#x\n", (int)data->unknown[1] );
1837 printf( " unknown[2] %#x\n", (int)data->unknown[2] );
1838 printf( " unknown[3] %#x\n", (int)data->unknown[3] );
1841 if (data->CHPECodeAddressRangeOffset)
1843 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CHPECodeAddressRangeOffset,
1844 data->CHPECodeAddressRangeCount * sizeof(*map) );
1846 printf( "\nCode ranges\n" );
1847 for (i = 0; i < data->CHPECodeAddressRangeCount; i++)
1849 static const char *types[] = { "x86", "ARM64" };
1850 unsigned int start = map[i].StartOffset & ~1;
1851 unsigned int type = map[i].StartOffset & 1;
1852 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
1856 break;
1859 case IMAGE_FILE_MACHINE_AMD64:
1860 case IMAGE_FILE_MACHINE_ARM64:
1862 const IMAGE_ARM64EC_METADATA *data = metadata;
1864 printf( " Version %#x\n", (int)data->Version );
1865 printf( " CodeMap %#x\n", (int)data->CodeMap );
1866 printf( " CodeMapCount %#x\n", (int)data->CodeMapCount );
1867 printf( " CodeRangesToEntryPoints %#x\n", (int)data->CodeRangesToEntryPoints );
1868 printf( " RedirectionMetadata %#x\n", (int)data->RedirectionMetadata );
1869 printf( " __os_arm64x_dispatch_call_no_redirect %#x\n", (int)data->__os_arm64x_dispatch_call_no_redirect );
1870 printf( " __os_arm64x_dispatch_ret %#x\n", (int)data->__os_arm64x_dispatch_ret );
1871 printf( " __os_arm64x_dispatch_call %#x\n", (int)data->__os_arm64x_dispatch_call );
1872 printf( " __os_arm64x_dispatch_icall %#x\n", (int)data->__os_arm64x_dispatch_icall );
1873 printf( " __os_arm64x_dispatch_icall_cfg %#x\n", (int)data->__os_arm64x_dispatch_icall_cfg );
1874 printf( " AlternateEntryPoint %#x\n", (int)data->AlternateEntryPoint );
1875 printf( " AuxiliaryIAT %#x\n", (int)data->AuxiliaryIAT );
1876 printf( " CodeRangesToEntryPointsCount %#x\n", (int)data->CodeRangesToEntryPointsCount );
1877 printf( " RedirectionMetadataCount %#x\n", (int)data->RedirectionMetadataCount );
1878 printf( " GetX64InformationFunctionPointer %#x\n", (int)data->GetX64InformationFunctionPointer );
1879 printf( " SetX64InformationFunctionPointer %#x\n", (int)data->SetX64InformationFunctionPointer );
1880 printf( " ExtraRFETable %#x\n", (int)data->ExtraRFETable );
1881 printf( " ExtraRFETableSize %#x\n", (int)data->ExtraRFETableSize );
1882 printf( " __os_arm64x_dispatch_fptr %#x\n", (int)data->__os_arm64x_dispatch_fptr );
1883 printf( " AuxiliaryIATCopy %#x\n", (int)data->AuxiliaryIATCopy );
1885 if (data->CodeMap)
1887 const IMAGE_CHPE_RANGE_ENTRY *map = RVA( data->CodeMap, data->CodeMapCount * sizeof(*map) );
1889 printf( "\nCode ranges\n" );
1890 for (i = 0; i < data->CodeMapCount; i++)
1892 static const char *types[] = { "ARM64", "ARM64EC", "x64", "??" };
1893 unsigned int start = map[i].StartOffset & ~0x3;
1894 unsigned int type = map[i].StartOffset & 0x3;
1895 printf( " %08x - %08x %s\n", start, start + (int)map[i].Length, types[type] );
1899 if (PE_nt_headers->FileHeader.Machine == IMAGE_FILE_MACHINE_ARM64) break;
1901 if (data->CodeRangesToEntryPoints)
1903 const IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT *map = RVA( data->CodeRangesToEntryPoints,
1904 data->CodeRangesToEntryPointsCount * sizeof(*map) );
1906 printf( "\nCode ranges to entry points\n" );
1907 printf( " Start - End Entry point\n" );
1908 for (i = 0; i < data->CodeRangesToEntryPointsCount; i++)
1910 const char *name = find_export_from_rva( map[i].EntryPoint );
1911 printf( " %08x - %08x %08x",
1912 (int)map[i].StartRva, (int)map[i].EndRva, (int)map[i].EntryPoint );
1913 if (name) printf( " %s", name );
1914 printf( "\n" );
1918 if (data->RedirectionMetadata)
1920 const IMAGE_ARM64EC_REDIRECTION_ENTRY *map = RVA( data->RedirectionMetadata,
1921 data->RedirectionMetadataCount * sizeof(*map) );
1923 printf( "\nEntry point redirection\n" );
1924 for (i = 0; i < data->RedirectionMetadataCount; i++)
1926 const char *name = find_export_from_rva( map[i].Source );
1927 printf( " %08x -> %08x", (int)map[i].Source, (int)map[i].Destination );
1928 if (name) printf( " (%s)", name );
1929 printf( "\n" );
1932 break;
1935 printf( "\n" );
1938 static void dump_dir_loadconfig(void)
1940 unsigned int size;
1941 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32;
1943 loadcfg32 = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
1944 if (!loadcfg32) return;
1945 size = min( size, loadcfg32->Size );
1947 printf( "Loadconfig\n" );
1948 print_dword( "Size", loadcfg32->Size );
1949 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1950 print_word( "MajorVersion", loadcfg32->MajorVersion );
1951 print_word( "MinorVersion", loadcfg32->MinorVersion );
1952 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1953 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1954 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1956 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1958 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void *)loadcfg32;
1960 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1961 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1962 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1963 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1964 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1965 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1966 print_word( "CSDVersion", loadcfg64->CSDVersion );
1967 print_word( "DependentLoadFlags", loadcfg64->DependentLoadFlags );
1968 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1969 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, SEHandlerTable )) goto done;
1970 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1971 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1972 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardCFCheckFunctionPointer )) goto done;
1973 print_longlong( "GuardCFCheckFunctionPointer", loadcfg64->GuardCFCheckFunctionPointer );
1974 print_longlong( "GuardCFDispatchFunctionPointer", loadcfg64->GuardCFDispatchFunctionPointer );
1975 print_longlong( "GuardCFFunctionTable", loadcfg64->GuardCFFunctionTable );
1976 print_longlong( "GuardCFFunctionCount", loadcfg64->GuardCFFunctionCount );
1977 print_dword( "GuardFlags", loadcfg64->GuardFlags );
1978 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CodeIntegrity )) goto done;
1979 print_word( "CodeIntegrity.Flags", loadcfg64->CodeIntegrity.Flags );
1980 print_word( "CodeIntegrity.Catalog", loadcfg64->CodeIntegrity.Catalog );
1981 print_dword( "CodeIntegrity.CatalogOffset", loadcfg64->CodeIntegrity.CatalogOffset );
1982 print_dword( "CodeIntegrity.Reserved", loadcfg64->CodeIntegrity.Reserved );
1983 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardAddressTakenIatEntryTable )) goto done;
1984 print_longlong( "GuardAddressTakenIatEntryTable", loadcfg64->GuardAddressTakenIatEntryTable );
1985 print_longlong( "GuardAddressTakenIatEntryCount", loadcfg64->GuardAddressTakenIatEntryCount );
1986 print_longlong( "GuardLongJumpTargetTable", loadcfg64->GuardLongJumpTargetTable );
1987 print_longlong( "GuardLongJumpTargetCount", loadcfg64->GuardLongJumpTargetCount );
1988 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTable )) goto done;
1989 print_longlong( "DynamicValueRelocTable", loadcfg64->DynamicValueRelocTable );
1990 print_longlong( "CHPEMetadataPointer", loadcfg64->CHPEMetadataPointer );
1991 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFFailureRoutine )) goto done;
1992 print_longlong( "GuardRFFailureRoutine", loadcfg64->GuardRFFailureRoutine );
1993 print_longlong( "GuardRFFailureRoutineFuncPtr", loadcfg64->GuardRFFailureRoutineFunctionPointer );
1994 print_dword( "DynamicValueRelocTableOffset", loadcfg64->DynamicValueRelocTableOffset );
1995 print_word( "DynamicValueRelocTableSection",loadcfg64->DynamicValueRelocTableSection );
1996 print_word( "Reserved2", loadcfg64->Reserved2 );
1997 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardRFVerifyStackPointerFunctionPointer )) goto done;
1998 print_longlong( "GuardRFVerifyStackPointerFuncPtr", loadcfg64->GuardRFVerifyStackPointerFunctionPointer );
1999 print_dword( "HotPatchTableOffset", loadcfg64->HotPatchTableOffset );
2000 print_dword( "Reserved3", loadcfg64->Reserved3 );
2001 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, EnclaveConfigurationPointer )) goto done;
2002 print_longlong( "EnclaveConfigurationPointer", loadcfg64->EnclaveConfigurationPointer );
2003 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, VolatileMetadataPointer )) goto done;
2004 print_longlong( "VolatileMetadataPointer", loadcfg64->VolatileMetadataPointer );
2005 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardEHContinuationTable )) goto done;
2006 print_longlong( "GuardEHContinuationTable", loadcfg64->GuardEHContinuationTable );
2007 print_longlong( "GuardEHContinuationCount", loadcfg64->GuardEHContinuationCount );
2008 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardXFGCheckFunctionPointer )) goto done;
2009 print_longlong( "GuardXFGCheckFunctionPointer", loadcfg64->GuardXFGCheckFunctionPointer );
2010 print_longlong( "GuardXFGDispatchFunctionPointer", loadcfg64->GuardXFGDispatchFunctionPointer );
2011 print_longlong( "GuardXFGTableDispatchFuncPtr", loadcfg64->GuardXFGTableDispatchFunctionPointer );
2012 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, CastGuardOsDeterminedFailureMode )) goto done;
2013 print_longlong( "CastGuardOsDeterminedFailureMode", loadcfg64->CastGuardOsDeterminedFailureMode );
2014 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, GuardMemcpyFunctionPointer )) goto done;
2015 print_longlong( "GuardMemcpyFunctionPointer", loadcfg64->GuardMemcpyFunctionPointer );
2017 else
2019 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
2020 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
2021 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
2022 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
2023 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
2024 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
2025 print_word( "CSDVersion", loadcfg32->CSDVersion );
2026 print_word( "DependentLoadFlags", loadcfg32->DependentLoadFlags );
2027 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
2028 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, SEHandlerTable )) goto done;
2029 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
2030 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
2031 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardCFCheckFunctionPointer )) goto done;
2032 print_dword( "GuardCFCheckFunctionPointer", loadcfg32->GuardCFCheckFunctionPointer );
2033 print_dword( "GuardCFDispatchFunctionPointer", loadcfg32->GuardCFDispatchFunctionPointer );
2034 print_dword( "GuardCFFunctionTable", loadcfg32->GuardCFFunctionTable );
2035 print_dword( "GuardCFFunctionCount", loadcfg32->GuardCFFunctionCount );
2036 print_dword( "GuardFlags", loadcfg32->GuardFlags );
2037 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CodeIntegrity )) goto done;
2038 print_word( "CodeIntegrity.Flags", loadcfg32->CodeIntegrity.Flags );
2039 print_word( "CodeIntegrity.Catalog", loadcfg32->CodeIntegrity.Catalog );
2040 print_dword( "CodeIntegrity.CatalogOffset", loadcfg32->CodeIntegrity.CatalogOffset );
2041 print_dword( "CodeIntegrity.Reserved", loadcfg32->CodeIntegrity.Reserved );
2042 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardAddressTakenIatEntryTable )) goto done;
2043 print_dword( "GuardAddressTakenIatEntryTable", loadcfg32->GuardAddressTakenIatEntryTable );
2044 print_dword( "GuardAddressTakenIatEntryCount", loadcfg32->GuardAddressTakenIatEntryCount );
2045 print_dword( "GuardLongJumpTargetTable", loadcfg32->GuardLongJumpTargetTable );
2046 print_dword( "GuardLongJumpTargetCount", loadcfg32->GuardLongJumpTargetCount );
2047 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTable )) goto done;
2048 print_dword( "DynamicValueRelocTable", loadcfg32->DynamicValueRelocTable );
2049 print_dword( "CHPEMetadataPointer", loadcfg32->CHPEMetadataPointer );
2050 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFFailureRoutine )) goto done;
2051 print_dword( "GuardRFFailureRoutine", loadcfg32->GuardRFFailureRoutine );
2052 print_dword( "GuardRFFailureRoutineFuncPtr", loadcfg32->GuardRFFailureRoutineFunctionPointer );
2053 print_dword( "DynamicValueRelocTableOffset", loadcfg32->DynamicValueRelocTableOffset );
2054 print_word( "DynamicValueRelocTableSection", loadcfg32->DynamicValueRelocTableSection );
2055 print_word( "Reserved2", loadcfg32->Reserved2 );
2056 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardRFVerifyStackPointerFunctionPointer )) goto done;
2057 print_dword( "GuardRFVerifyStackPointerFuncPtr", loadcfg32->GuardRFVerifyStackPointerFunctionPointer );
2058 print_dword( "HotPatchTableOffset", loadcfg32->HotPatchTableOffset );
2059 print_dword( "Reserved3", loadcfg32->Reserved3 );
2060 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, EnclaveConfigurationPointer )) goto done;
2061 print_dword( "EnclaveConfigurationPointer", loadcfg32->EnclaveConfigurationPointer );
2062 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, VolatileMetadataPointer )) goto done;
2063 print_dword( "VolatileMetadataPointer", loadcfg32->VolatileMetadataPointer );
2064 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardEHContinuationTable )) goto done;
2065 print_dword( "GuardEHContinuationTable", loadcfg32->GuardEHContinuationTable );
2066 print_dword( "GuardEHContinuationCount", loadcfg32->GuardEHContinuationCount );
2067 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardXFGCheckFunctionPointer )) goto done;
2068 print_dword( "GuardXFGCheckFunctionPointer", loadcfg32->GuardXFGCheckFunctionPointer );
2069 print_dword( "GuardXFGDispatchFunctionPointer", loadcfg32->GuardXFGDispatchFunctionPointer );
2070 print_dword( "GuardXFGTableDispatchFuncPtr", loadcfg32->GuardXFGTableDispatchFunctionPointer );
2071 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, CastGuardOsDeterminedFailureMode )) goto done;
2072 print_dword( "CastGuardOsDeterminedFailureMode", loadcfg32->CastGuardOsDeterminedFailureMode );
2073 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, GuardMemcpyFunctionPointer )) goto done;
2074 print_dword( "GuardMemcpyFunctionPointer", loadcfg32->GuardMemcpyFunctionPointer );
2076 done:
2077 printf( "\n" );
2078 dump_hybrid_metadata();
2081 static void dump_dir_delay_imported_functions(void)
2083 unsigned directorySize;
2084 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
2086 if (!importDesc) return;
2088 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
2090 for (;;)
2092 const IMAGE_THUNK_DATA32* il;
2093 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
2095 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
2097 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
2098 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
2099 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
2100 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
2101 printf(" TimeDateStamp: %08X (%s)\n",
2102 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
2104 printf(" Thunk Ordn Name\n");
2106 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
2108 if (!il)
2109 printf("Can't grab thunk data, going to next imported DLL\n");
2110 else
2112 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2113 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
2114 else
2115 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
2116 printf("\n");
2118 importDesc++;
2120 printf("\n");
2123 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
2125 const char* str;
2127 printf("Directory %02u\n", idx + 1);
2128 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
2129 printf(" TimeDateStamp: %08X %s\n",
2130 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
2131 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
2132 switch (idd->Type)
2134 default:
2135 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
2136 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
2137 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
2138 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
2139 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
2140 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
2141 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
2142 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
2143 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
2144 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
2145 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
2146 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
2147 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
2148 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
2149 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
2150 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
2151 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
2153 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
2154 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
2155 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
2156 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
2158 switch (idd->Type)
2160 case IMAGE_DEBUG_TYPE_UNKNOWN:
2161 break;
2162 case IMAGE_DEBUG_TYPE_COFF:
2163 dump_coff(idd->PointerToRawData, idd->SizeOfData,
2164 IMAGE_FIRST_SECTION(PE_nt_headers));
2165 break;
2166 case IMAGE_DEBUG_TYPE_CODEVIEW:
2167 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
2168 break;
2169 case IMAGE_DEBUG_TYPE_FPO:
2170 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
2171 break;
2172 case IMAGE_DEBUG_TYPE_MISC:
2174 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
2175 if (!misc) {printf("Can't get misc debug information\n"); break;}
2176 printf(" DataType: %u (%s)\n",
2177 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
2178 printf(" Length: %u\n", (UINT)misc->Length);
2179 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
2180 printf(" Data: %s\n", misc->Data);
2182 break;
2183 default: break;
2185 printf("\n");
2188 static void dump_dir_debug(void)
2190 unsigned nb_dbg, i;
2191 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
2193 nb_dbg /= sizeof(*debugDir);
2194 if (!debugDir || !nb_dbg) return;
2196 printf("Debug Table (%u directories)\n", nb_dbg);
2198 for (i = 0; i < nb_dbg; i++)
2200 dump_dir_debug_dir(debugDir, i);
2201 debugDir++;
2203 printf("\n");
2206 static inline void print_clrflags(const char *title, UINT value)
2208 printf(" %-34s 0x%X\n", title, value);
2209 #define X(f,s) if (value & f) printf(" %s\n", s)
2210 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
2211 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
2212 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
2213 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
2214 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
2215 #undef X
2218 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
2220 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
2223 static void dump_dir_clr_header(void)
2225 unsigned int size = 0;
2226 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
2228 if (!dir) return;
2230 printf( "CLR Header\n" );
2231 print_dword( "Header Size", dir->cb );
2232 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
2233 print_clrflags( "Flags", dir->Flags );
2234 print_dword( "EntryPointToken", dir->EntryPointToken );
2235 printf("\n");
2236 printf( "CLR Data Directory\n" );
2237 print_clrdirectory( "MetaData", &dir->MetaData );
2238 print_clrdirectory( "Resources", &dir->Resources );
2239 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
2240 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
2241 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
2242 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
2243 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
2244 printf("\n");
2247 static void dump_dynamic_relocs_arm64x( const IMAGE_BASE_RELOCATION *base_reloc, unsigned int size )
2249 unsigned int i;
2250 const IMAGE_BASE_RELOCATION *base_end = (const IMAGE_BASE_RELOCATION *)((const char *)base_reloc + size);
2252 printf( "Relocations ARM64X\n" );
2253 while (base_reloc < base_end - 1 && base_reloc->SizeOfBlock)
2255 const USHORT *rel = (const USHORT *)(base_reloc + 1);
2256 const USHORT *end = (const USHORT *)base_reloc + base_reloc->SizeOfBlock / sizeof(USHORT);
2257 printf( " Page %x\n", (UINT)base_reloc->VirtualAddress );
2258 while (rel < end && *rel)
2260 USHORT offset = *rel & 0xfff;
2261 USHORT type = (*rel >> 12) & 3;
2262 USHORT arg = *rel >> 14;
2263 rel++;
2264 switch (type)
2266 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2267 printf( " off %04x zero-fill %u bytes\n", offset, 1 << arg );
2268 break;
2269 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2270 printf( " off %04x set %u bytes value ", offset, 1 << arg );
2271 for (i = (1 << arg ) / sizeof(USHORT); i > 0; i--) printf( "%04x", rel[i - 1] );
2272 rel += (1 << arg) / sizeof(USHORT);
2273 printf( "\n" );
2274 break;
2275 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2276 printf( " off %04x add offset ", offset );
2277 if (arg & 1) printf( "-" );
2278 printf( "%08x\n", (UINT)*rel++ * ((arg & 2) ? 8 : 4) );
2279 break;
2280 default:
2281 printf( " off %04x unknown (arg %x)\n", offset, arg );
2282 break;
2285 base_reloc = (const IMAGE_BASE_RELOCATION *)end;
2289 static void dump_dynamic_relocs( const char *ptr, unsigned int size, ULONGLONG symbol )
2291 switch (symbol)
2293 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE:
2294 printf( "Relocations GUARD_RF_PROLOGUE\n" );
2295 break;
2296 case IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE:
2297 printf( "Relocations GUARD_RF_EPILOGUE\n" );
2298 break;
2299 case IMAGE_DYNAMIC_RELOCATION_GUARD_IMPORT_CONTROL_TRANSFER:
2300 printf( "Relocations GUARD_IMPORT_CONTROL_TRANSFER\n" );
2301 break;
2302 case IMAGE_DYNAMIC_RELOCATION_GUARD_INDIR_CONTROL_TRANSFER:
2303 printf( "Relocations GUARD_INDIR_CONTROL_TRANSFER\n" );
2304 break;
2305 case IMAGE_DYNAMIC_RELOCATION_GUARD_SWITCHTABLE_BRANCH:
2306 printf( "Relocations GUARD_SWITCHTABLE_BRANCH\n" );
2307 break;
2308 case IMAGE_DYNAMIC_RELOCATION_ARM64X:
2309 dump_dynamic_relocs_arm64x( (const IMAGE_BASE_RELOCATION *)ptr, size );
2310 break;
2311 default:
2312 printf( "Unknown relocation symbol %s\n", longlong_str(symbol) );
2313 break;
2317 static const IMAGE_DYNAMIC_RELOCATION_TABLE *get_dyn_reloc_table(void)
2319 unsigned int size, section, offset;
2320 const IMAGE_SECTION_HEADER *sec;
2321 const IMAGE_DYNAMIC_RELOCATION_TABLE *table;
2323 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2325 const IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2326 if (!cfg) return NULL;
2327 size = min( size, cfg->Size );
2328 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, DynamicValueRelocTableSection )) return NULL;
2329 offset = cfg->DynamicValueRelocTableOffset;
2330 section = cfg->DynamicValueRelocTableSection;
2332 else
2334 const IMAGE_LOAD_CONFIG_DIRECTORY32 *cfg = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, &size);
2335 if (!cfg) return NULL;
2336 size = min( size, cfg->Size );
2337 if (size <= offsetof( IMAGE_LOAD_CONFIG_DIRECTORY32, DynamicValueRelocTableSection )) return NULL;
2338 offset = cfg->DynamicValueRelocTableOffset;
2339 section = cfg->DynamicValueRelocTableSection;
2341 if (!section || section > PE_nt_headers->FileHeader.NumberOfSections) return NULL;
2342 sec = IMAGE_FIRST_SECTION( PE_nt_headers ) + section - 1;
2343 if (offset >= sec->SizeOfRawData) return NULL;
2344 return PRD( sec->PointerToRawData + offset, sizeof(*table) );
2347 static void dump_dir_dynamic_reloc(void)
2349 const char *ptr, *end;
2350 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2352 if (!table) return;
2354 printf( "Dynamic relocations (version %u)\n\n", (UINT)table->Version );
2355 ptr = (const char *)(table + 1);
2356 end = ptr + table->Size;
2357 while (ptr < end)
2359 switch (table->Version)
2361 case 1:
2362 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2364 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2365 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2366 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2368 else
2370 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2371 dump_dynamic_relocs( (const char *)(reloc + 1), reloc->BaseRelocSize, reloc->Symbol );
2372 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2374 break;
2375 case 2:
2376 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2378 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2379 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2380 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2382 else
2384 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2385 dump_dynamic_relocs( ptr + reloc->HeaderSize, reloc->FixupInfoSize, reloc->Symbol );
2386 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2388 break;
2391 printf( "\n" );
2394 static const IMAGE_BASE_RELOCATION *get_armx_relocs( unsigned int *size )
2396 const char *ptr, *end;
2397 const IMAGE_DYNAMIC_RELOCATION_TABLE *table = get_dyn_reloc_table();
2399 if (!table) return NULL;
2400 ptr = (const char *)(table + 1);
2401 end = ptr + table->Size;
2402 while (ptr < end)
2404 switch (table->Version)
2406 case 1:
2407 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2409 const IMAGE_DYNAMIC_RELOCATION64 *reloc = (const IMAGE_DYNAMIC_RELOCATION64 *)ptr;
2410 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2412 *size = reloc->BaseRelocSize;
2413 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2415 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2417 else
2419 const IMAGE_DYNAMIC_RELOCATION32 *reloc = (const IMAGE_DYNAMIC_RELOCATION32 *)ptr;
2420 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2422 *size = reloc->BaseRelocSize;
2423 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2425 ptr += sizeof(*reloc) + reloc->BaseRelocSize;
2427 break;
2428 case 2:
2429 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2431 const IMAGE_DYNAMIC_RELOCATION64_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION64_V2 *)ptr;
2432 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2434 *size = reloc->FixupInfoSize;
2435 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2437 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2439 else
2441 const IMAGE_DYNAMIC_RELOCATION32_V2 *reloc = (const IMAGE_DYNAMIC_RELOCATION32_V2 *)ptr;
2442 if (reloc->Symbol == IMAGE_DYNAMIC_RELOCATION_ARM64X)
2444 *size = reloc->FixupInfoSize;
2445 return (const IMAGE_BASE_RELOCATION *)(reloc + 1);
2447 ptr += reloc->HeaderSize + reloc->FixupInfoSize;
2449 break;
2452 return NULL;
2455 static BOOL get_alt_header( void )
2457 unsigned int size;
2458 const IMAGE_BASE_RELOCATION *end, *reloc = get_armx_relocs( &size );
2460 if (!reloc) return FALSE;
2461 end = (const IMAGE_BASE_RELOCATION *)((const char *)reloc + size);
2463 while (reloc < end - 1 && reloc->SizeOfBlock)
2465 const USHORT *rel = (const USHORT *)(reloc + 1);
2466 const USHORT *rel_end = (const USHORT *)reloc + reloc->SizeOfBlock / sizeof(USHORT);
2467 char *page = reloc->VirtualAddress ? (char *)RVA(reloc->VirtualAddress,1) : dump_base;
2469 while (rel < rel_end && *rel)
2471 USHORT offset = *rel & 0xfff;
2472 USHORT type = (*rel >> 12) & 3;
2473 USHORT arg = *rel >> 14;
2474 int val;
2475 rel++;
2476 switch (type)
2478 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL:
2479 memset( page + offset, 0, 1 << arg );
2480 break;
2481 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE:
2482 memcpy( page + offset, rel, 1 << arg );
2483 rel += (1 << arg) / sizeof(USHORT);
2484 break;
2485 case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA:
2486 val = (unsigned int)*rel++ * ((arg & 2) ? 8 : 4);
2487 if (arg & 1) val = -val;
2488 *(int *)(page + offset) += val;
2489 break;
2492 reloc = (const IMAGE_BASE_RELOCATION *)rel_end;
2494 return TRUE;
2497 static void dump_dir_reloc(void)
2499 unsigned int i, size = 0;
2500 const USHORT *relocs;
2501 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
2502 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
2503 static const char * const names[] =
2505 "BASED_ABSOLUTE",
2506 "BASED_HIGH",
2507 "BASED_LOW",
2508 "BASED_HIGHLOW",
2509 "BASED_HIGHADJ",
2510 "BASED_MIPS_JMPADDR",
2511 "BASED_SECTION",
2512 "BASED_REL",
2513 "unknown 8",
2514 "BASED_IA64_IMM64",
2515 "BASED_DIR64",
2516 "BASED_HIGH3ADJ",
2517 "unknown 12",
2518 "unknown 13",
2519 "unknown 14",
2520 "unknown 15"
2523 if (!rel) return;
2525 printf( "Relocations\n" );
2526 while (rel < end - 1 && rel->SizeOfBlock)
2528 printf( " Page %x\n", (UINT)rel->VirtualAddress );
2529 relocs = (const USHORT *)(rel + 1);
2530 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
2531 while (i--)
2533 USHORT offset = *relocs & 0xfff;
2534 int type = *relocs >> 12;
2535 printf( " off %04x type %s\n", offset, names[type] );
2536 relocs++;
2538 rel = (const IMAGE_BASE_RELOCATION *)relocs;
2540 printf("\n");
2543 static void dump_dir_tls(void)
2545 IMAGE_TLS_DIRECTORY64 dir;
2546 const UINT *callbacks;
2547 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
2549 if (!pdir) return;
2551 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
2552 memcpy(&dir, pdir, sizeof(dir));
2553 else
2555 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
2556 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
2557 dir.AddressOfIndex = pdir->AddressOfIndex;
2558 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
2559 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
2560 dir.Characteristics = pdir->Characteristics;
2563 /* FIXME: This does not properly handle large images */
2564 printf( "Thread Local Storage\n" );
2565 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2566 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
2567 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2568 (UINT)dir.SizeOfZeroFill );
2569 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
2570 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
2571 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
2572 if (dir.AddressOfCallBacks)
2574 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2575 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
2577 printf( " %08x", *callbacks );
2578 addr += sizeof(UINT);
2581 printf(" }\n\n");
2584 enum FileSig get_kind_dbg(void)
2586 const WORD* pw;
2588 pw = PRD(0, sizeof(WORD));
2589 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2591 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2592 return SIG_UNKNOWN;
2595 void dbg_dump(void)
2597 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2598 unsigned nb_dbg;
2599 unsigned i;
2600 const IMAGE_DEBUG_DIRECTORY* debugDir;
2602 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2603 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2605 printf ("Signature: %.2s (0x%4X)\n",
2606 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2607 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2608 printf ("Machine: 0x%04X (%s)\n",
2609 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2610 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2611 printf ("TimeDateStamp: 0x%08X (%s)\n",
2612 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2613 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
2614 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
2615 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
2616 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
2617 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
2618 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
2620 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2621 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2622 {printf("Can't get the sections, aborting\n"); return;}
2624 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2626 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2627 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2628 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2629 separateDebugHead->ExportedNamesSize,
2630 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2631 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2633 printf("Debug Table (%u directories)\n", nb_dbg);
2635 for (i = 0; i < nb_dbg; i++)
2637 dump_dir_debug_dir(debugDir, i);
2638 debugDir++;
2642 static const char *get_resource_type( unsigned int id )
2644 static const char * const types[] =
2646 NULL,
2647 "CURSOR",
2648 "BITMAP",
2649 "ICON",
2650 "MENU",
2651 "DIALOG",
2652 "STRING",
2653 "FONTDIR",
2654 "FONT",
2655 "ACCELERATOR",
2656 "RCDATA",
2657 "MESSAGETABLE",
2658 "GROUP_CURSOR",
2659 NULL,
2660 "GROUP_ICON",
2661 NULL,
2662 "VERSION",
2663 "DLGINCLUDE",
2664 NULL,
2665 "PLUGPLAY",
2666 "VXD",
2667 "ANICURSOR",
2668 "ANIICON",
2669 "HTML",
2670 "MANIFEST"
2673 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2674 return NULL;
2677 /* dump an ASCII string with proper escaping */
2678 static int dump_strA( const unsigned char *str, size_t len )
2680 static const char escapes[32] = ".......abtnvfr.............e....";
2681 char buffer[256];
2682 char *pos = buffer;
2683 int count = 0;
2685 for (; len; str++, len--)
2687 if (pos > buffer + sizeof(buffer) - 8)
2689 fwrite( buffer, pos - buffer, 1, stdout );
2690 count += pos - buffer;
2691 pos = buffer;
2693 if (*str > 127) /* hex escape */
2695 pos += sprintf( pos, "\\x%02x", *str );
2696 continue;
2698 if (*str < 32) /* octal or C escape */
2700 if (!*str && len == 1) continue; /* do not output terminating NULL */
2701 if (escapes[*str] != '.')
2702 pos += sprintf( pos, "\\%c", escapes[*str] );
2703 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2704 pos += sprintf( pos, "\\%03o", *str );
2705 else
2706 pos += sprintf( pos, "\\%o", *str );
2707 continue;
2709 if (*str == '\\') *pos++ = '\\';
2710 *pos++ = *str;
2712 fwrite( buffer, pos - buffer, 1, stdout );
2713 count += pos - buffer;
2714 return count;
2717 /* dump a Unicode string with proper escaping */
2718 static int dump_strW( const WCHAR *str, size_t len )
2720 static const char escapes[32] = ".......abtnvfr.............e....";
2721 char buffer[256];
2722 char *pos = buffer;
2723 int count = 0;
2725 for (; len; str++, len--)
2727 if (pos > buffer + sizeof(buffer) - 8)
2729 fwrite( buffer, pos - buffer, 1, stdout );
2730 count += pos - buffer;
2731 pos = buffer;
2733 if (*str > 127) /* hex escape */
2735 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2736 pos += sprintf( pos, "\\x%04x", *str );
2737 else
2738 pos += sprintf( pos, "\\x%x", *str );
2739 continue;
2741 if (*str < 32) /* octal or C escape */
2743 if (!*str && len == 1) continue; /* do not output terminating NULL */
2744 if (escapes[*str] != '.')
2745 pos += sprintf( pos, "\\%c", escapes[*str] );
2746 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2747 pos += sprintf( pos, "\\%03o", *str );
2748 else
2749 pos += sprintf( pos, "\\%o", *str );
2750 continue;
2752 if (*str == '\\') *pos++ = '\\';
2753 *pos++ = *str;
2755 fwrite( buffer, pos - buffer, 1, stdout );
2756 count += pos - buffer;
2757 return count;
2760 /* dump data for a STRING resource */
2761 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2763 int i;
2765 for (i = 0; i < 16 && size; i++)
2767 unsigned len = *ptr++;
2769 if (len >= size)
2771 len = size;
2772 size = 0;
2774 else size -= len + 1;
2776 if (len)
2778 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2779 dump_strW( ptr, len );
2780 printf( "\"\n" );
2781 ptr += len;
2786 /* dump data for a MESSAGETABLE resource */
2787 static void dump_msgtable_data( const void *ptr, unsigned int size, const char *prefix )
2789 const MESSAGE_RESOURCE_DATA *data = ptr;
2790 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2791 unsigned i, j;
2793 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2795 const MESSAGE_RESOURCE_ENTRY *entry;
2797 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2798 for (j = block->LowId; j <= block->HighId; j++)
2800 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2802 const WCHAR *str = (const WCHAR *)entry->Text;
2803 printf( "%s%08x L\"", prefix, j );
2804 dump_strW( str, strlenW(str) );
2805 printf( "\"\n" );
2807 else
2809 const char *str = (const char *) entry->Text;
2810 printf( "%s%08x \"", prefix, j );
2811 dump_strA( entry->Text, strlen(str) );
2812 printf( "\"\n" );
2814 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2819 struct version_info
2821 WORD len;
2822 WORD val_len;
2823 WORD type;
2824 WCHAR key[1];
2826 #define GET_VALUE(info) ((void *)((char *)info + ((offsetof(struct version_info, key[strlenW(info->key) + 1]) + 3) & ~3)))
2827 #define GET_CHILD(info) ((void *)((char *)GET_VALUE(info) + ((info->val_len * (info->type ? 2 : 1) + 3) & ~3)))
2828 #define GET_NEXT(info) ((void *)((char *)info + ((info->len + 3) & ~3)))
2830 static void dump_version_children( const struct version_info *info, const char *prefix, int indent )
2832 const struct version_info *child = GET_CHILD( info );
2834 while ((char *)child < (char *)info + info->len)
2836 printf( "%s%*s", prefix, indent * 2, "" );
2837 if (child->val_len)
2839 printf( "VALUE \"" );
2840 dump_strW( child->key, strlenW(child->key) );
2841 if (child->type)
2843 printf( "\", \"" );
2844 dump_strW( GET_VALUE(child), child->val_len );
2845 printf( "\"\n" );
2847 else
2849 const WORD *data = GET_VALUE(child);
2850 unsigned int i;
2851 printf( "\"," );
2852 for (i = 0; i < child->val_len / sizeof(WORD); i++) printf( " %#x", data[i] );
2853 printf( "\n" );
2856 else
2858 printf( "BLOCK \"" );
2859 dump_strW( child->key, strlenW(child->key) );
2860 printf( "\"\n" );
2862 dump_version_children( child, prefix, indent + 1 );
2863 child = GET_NEXT( child );
2867 /* dump data for a VERSION resource */
2868 static void dump_version_data( const void *ptr, unsigned int size, const char *prefix )
2870 const struct version_info *info = ptr;
2871 const VS_FIXEDFILEINFO *fileinfo = GET_VALUE( info );
2873 printf( "%sSIGNATURE %08x\n", prefix, (UINT)fileinfo->dwSignature );
2874 printf( "%sVERSION %u.%u\n", prefix,
2875 HIWORD(fileinfo->dwStrucVersion), LOWORD(fileinfo->dwStrucVersion) );
2876 printf( "%sFILEVERSION %u.%u.%u.%u\n", prefix,
2877 HIWORD(fileinfo->dwFileVersionMS), LOWORD(fileinfo->dwFileVersionMS),
2878 HIWORD(fileinfo->dwFileVersionLS), LOWORD(fileinfo->dwFileVersionLS) );
2879 printf( "%sPRODUCTVERSION %u.%u.%u.%u\n", prefix,
2880 HIWORD(fileinfo->dwProductVersionMS), LOWORD(fileinfo->dwProductVersionMS),
2881 HIWORD(fileinfo->dwProductVersionLS), LOWORD(fileinfo->dwProductVersionLS) );
2882 printf( "%sFILEFLAGSMASK %08x\n", prefix, (UINT)fileinfo->dwFileFlagsMask );
2883 printf( "%sFILEFLAGS %08x\n", prefix, (UINT)fileinfo->dwFileFlags );
2885 switch (fileinfo->dwFileOS)
2887 #define CASE(x) case x: printf( "%sFILEOS %s\n", prefix, #x ); break
2888 CASE(VOS_UNKNOWN);
2889 CASE(VOS_DOS_WINDOWS16);
2890 CASE(VOS_DOS_WINDOWS32);
2891 CASE(VOS_OS216_PM16);
2892 CASE(VOS_OS232_PM32);
2893 CASE(VOS_NT_WINDOWS32);
2894 #undef CASE
2895 default:
2896 printf( "%sFILEOS %u.%u\n", prefix,
2897 (WORD)(fileinfo->dwFileOS >> 16), (WORD)fileinfo->dwFileOS );
2898 break;
2901 switch (fileinfo->dwFileType)
2903 #define CASE(x) case x: printf( "%sFILETYPE %s\n", prefix, #x ); break
2904 CASE(VFT_UNKNOWN);
2905 CASE(VFT_APP);
2906 CASE(VFT_DLL);
2907 CASE(VFT_DRV);
2908 CASE(VFT_FONT);
2909 CASE(VFT_VXD);
2910 CASE(VFT_STATIC_LIB);
2911 #undef CASE
2912 default:
2913 printf( "%sFILETYPE %08x\n", prefix, (UINT)fileinfo->dwFileType );
2914 break;
2917 switch (((ULONGLONG)fileinfo->dwFileType << 32) + fileinfo->dwFileSubtype)
2919 #define CASE(t,x) case (((ULONGLONG)t << 32) + x): printf( "%sFILESUBTYPE %s\n", prefix, #x ); break
2920 CASE(VFT_DRV, VFT2_UNKNOWN);
2921 CASE(VFT_DRV, VFT2_DRV_PRINTER);
2922 CASE(VFT_DRV, VFT2_DRV_KEYBOARD);
2923 CASE(VFT_DRV, VFT2_DRV_LANGUAGE);
2924 CASE(VFT_DRV, VFT2_DRV_DISPLAY);
2925 CASE(VFT_DRV, VFT2_DRV_MOUSE);
2926 CASE(VFT_DRV, VFT2_DRV_NETWORK);
2927 CASE(VFT_DRV, VFT2_DRV_SYSTEM);
2928 CASE(VFT_DRV, VFT2_DRV_INSTALLABLE);
2929 CASE(VFT_DRV, VFT2_DRV_SOUND);
2930 CASE(VFT_DRV, VFT2_DRV_COMM);
2931 CASE(VFT_DRV, VFT2_DRV_INPUTMETHOD);
2932 CASE(VFT_DRV, VFT2_DRV_VERSIONED_PRINTER);
2933 CASE(VFT_FONT, VFT2_FONT_RASTER);
2934 CASE(VFT_FONT, VFT2_FONT_VECTOR);
2935 CASE(VFT_FONT, VFT2_FONT_TRUETYPE);
2936 #undef CASE
2937 default:
2938 printf( "%sFILESUBTYPE %08x\n", prefix, (UINT)fileinfo->dwFileSubtype );
2939 break;
2942 printf( "%sFILEDATE %08x.%08x\n", prefix,
2943 (UINT)fileinfo->dwFileDateMS, (UINT)fileinfo->dwFileDateLS );
2944 dump_version_children( info, prefix, 0 );
2947 /* dump data for a HTML/MANIFEST resource */
2948 static void dump_xml_data( const void *ptr, unsigned int size, const char *prefix )
2950 const char *p = ptr, *end = p + size;
2952 while (p < end)
2954 const char *start = p;
2955 while (p < end && *p != '\r' && *p != '\n') p++;
2956 printf( "%s%.*s\n", prefix, (int)(p - start), start );
2957 while (p < end && (*p == '\r' || *p == '\n')) p++;
2961 static void dump_dir_resource(void)
2963 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2964 const IMAGE_RESOURCE_DIRECTORY *namedir;
2965 const IMAGE_RESOURCE_DIRECTORY *langdir;
2966 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2967 const IMAGE_RESOURCE_DIR_STRING_U *string;
2968 const IMAGE_RESOURCE_DATA_ENTRY *data;
2969 int i, j, k;
2971 if (!root) return;
2973 printf( "Resources:" );
2975 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2977 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2978 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
2979 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2981 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2982 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
2983 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2985 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2987 printf( "\n " );
2988 if (e1->NameIsString)
2990 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
2991 dump_unicode_str( string->NameString, string->Length );
2993 else
2995 const char *type = get_resource_type( e1->Id );
2996 if (type) printf( "%s", type );
2997 else printf( "%04x", e1->Id );
3000 printf( " Name=" );
3001 if (e2->NameIsString)
3003 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
3004 dump_unicode_str( string->NameString, string->Length );
3006 else
3007 printf( "%04x", e2->Id );
3009 printf( " Language=%04x:\n", e3->Id );
3010 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
3011 if (e1->NameIsString)
3013 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
3015 else switch(e1->Id)
3017 case 6: /* RT_STRING */
3018 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
3019 break;
3020 case 11: /* RT_MESSAGETABLE */
3021 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
3022 break;
3023 case 16: /* RT_VERSION */
3024 dump_version_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
3025 break;
3026 case 23: /* RT_HTML */
3027 case 24: /* RT_MANIFEST */
3028 dump_xml_data( RVA( data->OffsetToData, data->Size ), data->Size, " | " );
3029 break;
3030 default:
3031 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
3032 break;
3037 printf( "\n\n" );
3040 static void dump_debug(void)
3042 const char* stabs = NULL;
3043 unsigned szstabs = 0;
3044 const char* stabstr = NULL;
3045 unsigned szstr = 0;
3046 unsigned i;
3047 const IMAGE_SECTION_HEADER* sectHead;
3049 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
3051 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
3053 if (!strcmp((const char *)sectHead->Name, ".stab"))
3055 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
3056 szstabs = sectHead->Misc.VirtualSize;
3058 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
3060 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
3061 szstr = sectHead->Misc.VirtualSize;
3064 if (stabs && stabstr)
3065 dump_stabs(stabs, szstabs, stabstr, szstr);
3068 static void dump_symbol_table(void)
3070 const IMAGE_SYMBOL* sym;
3071 int numsym;
3073 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
3074 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
3075 return;
3076 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
3077 sizeof(*sym) * numsym);
3078 if (!sym) return;
3080 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
3083 enum FileSig get_kind_exec(void)
3085 const WORD* pw;
3086 const DWORD* pdw;
3087 const IMAGE_DOS_HEADER* dh;
3089 pw = PRD(0, sizeof(WORD));
3090 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
3092 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
3094 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
3096 /* the signature is the first DWORD */
3097 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
3098 if (pdw)
3100 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
3101 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
3102 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
3104 return SIG_DOS;
3106 return SIG_UNKNOWN;
3109 void pe_dump(void)
3111 int alt = 0;
3113 PE_nt_headers = get_nt_header();
3114 print_fake_dll();
3116 for (;;)
3118 if (alt)
3119 printf( "\n**** Alternate (%s) data ****\n\n",
3120 get_machine_str(PE_nt_headers->FileHeader.Machine));
3121 else if (get_dyn_reloc_table())
3122 printf( "**** Native (%s) data ****\n\n",
3123 get_machine_str(PE_nt_headers->FileHeader.Machine));
3125 if (globals.do_dumpheader)
3127 dump_pe_header();
3128 /* FIXME: should check ptr */
3129 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
3130 PE_nt_headers->FileHeader.NumberOfSections);
3132 else if (!globals.dumpsect)
3134 /* show at least something here */
3135 dump_pe_header();
3138 if (globals_dump_sect("import"))
3140 dump_dir_imported_functions();
3141 dump_dir_delay_imported_functions();
3143 if (globals_dump_sect("export"))
3144 dump_dir_exported_functions();
3145 if (globals_dump_sect("debug"))
3146 dump_dir_debug();
3147 if (globals_dump_sect("resource"))
3148 dump_dir_resource();
3149 if (globals_dump_sect("tls"))
3150 dump_dir_tls();
3151 if (globals_dump_sect("loadcfg"))
3152 dump_dir_loadconfig();
3153 if (globals_dump_sect("clr"))
3154 dump_dir_clr_header();
3155 if (globals_dump_sect("reloc"))
3156 dump_dir_reloc();
3157 if (globals_dump_sect("dynreloc"))
3158 dump_dir_dynamic_reloc();
3159 if (globals_dump_sect("except"))
3160 dump_dir_exceptions();
3161 if (globals_dump_sect("apiset"))
3162 dump_section_apiset();
3164 if (globals.do_symbol_table)
3165 dump_symbol_table();
3166 if (globals.do_debug)
3167 dump_debug();
3168 if (alt++) break;
3169 if (!get_alt_header()) break;
3173 typedef struct _dll_symbol {
3174 size_t ordinal;
3175 char *symbol;
3176 } dll_symbol;
3178 static dll_symbol *dll_symbols = NULL;
3179 static dll_symbol *dll_current_symbol = NULL;
3181 /* Compare symbols by ordinal for qsort */
3182 static int symbol_cmp(const void *left, const void *right)
3184 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
3187 /*******************************************************************
3188 * dll_close
3190 * Free resources used by DLL
3192 /* FIXME: Not used yet
3193 static void dll_close (void)
3195 dll_symbol* ds;
3197 if (!dll_symbols) {
3198 fatal("No symbols");
3200 for (ds = dll_symbols; ds->symbol; ds++)
3201 free(ds->symbol);
3202 free (dll_symbols);
3203 dll_symbols = NULL;
3207 static void do_grab_sym( void )
3209 const IMAGE_EXPORT_DIRECTORY*exportDir;
3210 UINT i, j, *map;
3211 const UINT *pName;
3212 const UINT *pFunc;
3213 const WORD *pOrdl;
3214 const char *ptr;
3216 PE_nt_headers = get_nt_header();
3217 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
3219 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
3220 if (!pName) {printf("Can't grab functions' name table\n"); return;}
3221 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
3222 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
3223 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
3224 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
3226 /* dll_close(); */
3228 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
3230 /* bit map of used funcs */
3231 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
3232 if (!map) fatal("no memory");
3234 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
3236 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
3237 ptr = RVA(*pName++, sizeof(DWORD));
3238 if (!ptr) ptr = "cant_get_function";
3239 dll_symbols[j].symbol = xstrdup(ptr);
3240 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
3241 assert(dll_symbols[j].symbol);
3244 for (i = 0; i < exportDir->NumberOfFunctions; i++)
3246 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
3248 char ordinal_text[256];
3249 /* Ordinal only entry */
3250 sprintf (ordinal_text, "%s_%u",
3251 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
3252 (UINT)exportDir->Base + i);
3253 str_toupper(ordinal_text);
3254 dll_symbols[j].symbol = xstrdup(ordinal_text);
3255 assert(dll_symbols[j].symbol);
3256 dll_symbols[j].ordinal = exportDir->Base + i;
3257 j++;
3258 assert(j <= exportDir->NumberOfFunctions);
3261 free(map);
3263 if (NORMAL)
3264 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
3265 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
3266 j, (UINT)exportDir->Base);
3268 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
3270 dll_symbols[j].symbol = NULL;
3272 dll_current_symbol = dll_symbols;
3275 /*******************************************************************
3276 * dll_open
3278 * Open a DLL and read in exported symbols
3280 BOOL dll_open (const char *dll_name)
3282 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
3285 /*******************************************************************
3286 * dll_next_symbol
3288 * Get next exported symbol from dll
3290 BOOL dll_next_symbol (parsed_symbol * sym)
3292 if (!dll_current_symbol || !dll_current_symbol->symbol)
3293 return FALSE;
3294 assert (dll_symbols);
3295 sym->symbol = xstrdup (dll_current_symbol->symbol);
3296 sym->ordinal = dll_current_symbol->ordinal;
3297 dll_current_symbol++;
3298 return TRUE;