winedump: Print the whole multibyte unwind opcode for arm.
[wine.git] / tools / winedump / pe.c
blob4629c3082f903de59aa47d2f3512c4075b823492
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 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winedump.h"
35 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
37 static const IMAGE_NT_HEADERS32* PE_nt_headers;
39 static const char builtin_signature[] = "Wine builtin DLL";
40 static const char fakedll_signature[] = "Wine placeholder DLL";
41 static int is_builtin;
43 const char *get_machine_str(int mach)
45 switch (mach)
47 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
48 case IMAGE_FILE_MACHINE_I860: return "i860";
49 case IMAGE_FILE_MACHINE_I386: return "i386";
50 case IMAGE_FILE_MACHINE_R3000: return "R3000";
51 case IMAGE_FILE_MACHINE_R4000: return "R4000";
52 case IMAGE_FILE_MACHINE_R10000: return "R10000";
53 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
54 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
55 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
56 case IMAGE_FILE_MACHINE_IA64: return "IA64";
57 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
58 case IMAGE_FILE_MACHINE_ARM: return "ARM";
59 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
60 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
62 return "???";
65 static const void* RVA(unsigned long rva, unsigned long len)
67 IMAGE_SECTION_HEADER* sectHead;
68 int i;
70 if (rva == 0) return NULL;
72 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
73 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
75 if (sectHead[i].VirtualAddress <= rva &&
76 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
78 /* return image import directory offset */
79 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
83 return NULL;
86 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
88 const IMAGE_DOS_HEADER *dos;
89 dos = PRD(0, sizeof(*dos));
90 if (!dos) return NULL;
91 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
92 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
93 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
96 void print_fake_dll( void )
98 const IMAGE_DOS_HEADER *dos;
100 dos = PRD(0, sizeof(*dos) + 32);
101 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
103 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
104 printf( "*** This is a Wine builtin DLL ***\n\n" );
105 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
106 printf( "*** This is a Wine fake DLL ***\n\n" );
110 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
112 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
114 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
115 if (idx >= opt->NumberOfRvaAndSizes)
116 return NULL;
117 if(size)
118 *size = opt->DataDirectory[idx].Size;
119 return RVA(opt->DataDirectory[idx].VirtualAddress,
120 opt->DataDirectory[idx].Size);
122 else
124 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
125 if (idx >= opt->NumberOfRvaAndSizes)
126 return NULL;
127 if(size)
128 *size = opt->DataDirectory[idx].Size;
129 return RVA(opt->DataDirectory[idx].VirtualAddress,
130 opt->DataDirectory[idx].Size);
134 static const void* get_dir(unsigned idx)
136 return get_dir_and_size(idx, 0);
139 static const char * const DirectoryNames[16] = {
140 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
141 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
142 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
143 "IAT", "Delay IAT", "CLR Header", ""
146 static const char *get_magic_type(WORD magic)
148 switch(magic) {
149 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
150 return "32bit";
151 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
152 return "64bit";
153 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
154 return "ROM";
156 return "???";
159 static inline void print_word(const char *title, WORD value)
161 printf(" %-34s 0x%-4X %u\n", title, value, value);
164 static inline void print_dword(const char *title, DWORD value)
166 printf(" %-34s 0x%-8x %u\n", title, value, value);
169 static inline void print_longlong(const char *title, ULONGLONG value)
171 printf(" %-34s 0x", title);
172 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
173 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
174 else
175 printf("%lx\n", (unsigned long)value);
178 static inline void print_ver(const char *title, BYTE major, BYTE minor)
180 printf(" %-34s %u.%02u\n", title, major, minor);
183 static inline void print_subsys(const char *title, WORD value)
185 const char *str;
186 switch (value)
188 default:
189 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
190 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
191 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
192 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
193 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
194 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
195 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
196 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
197 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
198 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
199 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
200 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
201 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
202 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
204 printf(" %-34s 0x%X (%s)\n", title, value, str);
207 static inline void print_dllflags(const char *title, WORD value)
209 printf(" %-34s 0x%04X\n", title, value);
210 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
211 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
212 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
213 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
214 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
215 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
216 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
217 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
218 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
219 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
220 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
221 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
222 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
223 #undef X
226 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
228 unsigned i;
229 printf("Data Directory\n");
231 for (i = 0; i < n && i < 16; i++)
233 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
234 DirectoryNames[i], directory[i].VirtualAddress,
235 directory[i].Size);
239 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
241 IMAGE_OPTIONAL_HEADER32 oh;
242 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
244 /* in case optional header is missing or partial */
245 memset(&oh, 0, sizeof(oh));
246 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
247 optionalHeader = &oh;
249 print_word("Magic", optionalHeader->Magic);
250 print_ver("linker version",
251 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
252 print_dword("size of code", optionalHeader->SizeOfCode);
253 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
254 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
255 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
256 print_dword("base of code", optionalHeader->BaseOfCode);
257 print_dword("base of data", optionalHeader->BaseOfData);
258 print_dword("image base", optionalHeader->ImageBase);
259 print_dword("section align", optionalHeader->SectionAlignment);
260 print_dword("file align", optionalHeader->FileAlignment);
261 print_ver("required OS version",
262 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
263 print_ver("image version",
264 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
265 print_ver("subsystem version",
266 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
267 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
268 print_dword("size of image", optionalHeader->SizeOfImage);
269 print_dword("size of headers", optionalHeader->SizeOfHeaders);
270 print_dword("checksum", optionalHeader->CheckSum);
271 print_subsys("Subsystem", optionalHeader->Subsystem);
272 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
273 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
274 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
275 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
276 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
277 print_dword("loader flags", optionalHeader->LoaderFlags);
278 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
279 printf("\n");
280 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
281 printf("\n");
284 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
286 IMAGE_OPTIONAL_HEADER64 oh;
287 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
289 /* in case optional header is missing or partial */
290 memset(&oh, 0, sizeof(oh));
291 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
292 optionalHeader = &oh;
294 print_word("Magic", optionalHeader->Magic);
295 print_ver("linker version",
296 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
297 print_dword("size of code", optionalHeader->SizeOfCode);
298 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
299 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
300 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
301 print_dword("base of code", optionalHeader->BaseOfCode);
302 print_longlong("image base", optionalHeader->ImageBase);
303 print_dword("section align", optionalHeader->SectionAlignment);
304 print_dword("file align", optionalHeader->FileAlignment);
305 print_ver("required OS version",
306 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
307 print_ver("image version",
308 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
309 print_ver("subsystem version",
310 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
311 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
312 print_dword("size of image", optionalHeader->SizeOfImage);
313 print_dword("size of headers", optionalHeader->SizeOfHeaders);
314 print_dword("checksum", optionalHeader->CheckSum);
315 print_subsys("Subsystem", optionalHeader->Subsystem);
316 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
317 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
318 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
319 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
320 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
321 print_dword("loader flags", optionalHeader->LoaderFlags);
322 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
323 printf("\n");
324 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
325 printf("\n");
328 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
330 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
332 switch(optionalHeader->Magic) {
333 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
334 dump_optional_header32(optionalHeader, header_size);
335 break;
336 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
337 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
338 break;
339 default:
340 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
341 break;
345 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
347 printf("File Header\n");
349 printf(" Machine: %04X (%s)\n",
350 fileHeader->Machine, get_machine_str(fileHeader->Machine));
351 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
352 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
353 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
354 Offset(&(fileHeader->TimeDateStamp)));
355 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
356 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
357 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
358 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
359 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
360 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
361 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
362 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
363 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
364 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
365 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
366 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
367 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
368 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
369 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
370 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
371 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
372 X(IMAGE_FILE_SYSTEM, "SYSTEM");
373 X(IMAGE_FILE_DLL, "DLL");
374 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
375 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
376 #undef X
377 printf("\n");
380 static void dump_pe_header(void)
382 dump_file_header(&PE_nt_headers->FileHeader);
383 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
386 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
388 unsigned offset;
390 /* long section name ? */
391 if (strtable && sectHead->Name[0] == '/' &&
392 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
393 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
394 else
395 printf(" %-8.8s", sectHead->Name);
396 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
397 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
398 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
399 sectHead->PointerToRawData, sectHead->SizeOfRawData);
400 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
401 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
402 printf(" line # offs: %-8u line #'s: %-8u\n",
403 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
404 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
405 printf(" ");
406 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
407 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
408 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
409 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
410 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
411 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
412 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
414 X(IMAGE_SCN_CNT_CODE, "CODE");
415 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
416 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
418 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
419 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
420 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
421 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
422 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
424 /* 0x00002000 - Reserved */
425 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
426 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
428 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
429 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
430 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
431 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
432 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
434 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
436 #define X2(b,s) case b: printf(" " s); break
437 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
438 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
439 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
440 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
441 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
442 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
443 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
444 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
445 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
446 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
447 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
448 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
449 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
450 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
451 #undef X2
454 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
456 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
457 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
458 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
459 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
460 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
461 X(IMAGE_SCN_MEM_READ, "MEM_READ");
462 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
463 #undef X
464 printf("\n\n");
467 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
469 const IMAGE_SECTION_HEADER* sectHead = addr;
470 unsigned i;
471 const char* strtable;
473 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
475 strtable = (const char*)base +
476 PE_nt_headers->FileHeader.PointerToSymbolTable +
477 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
479 else strtable = NULL;
481 printf("Section Table\n");
482 for (i = 0; i < num_sect; i++, sectHead++)
484 dump_section(sectHead, strtable);
486 if (globals.do_dump_rawdata)
488 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
489 printf("\n");
494 static void dump_dir_exported_functions(void)
496 unsigned int size = 0;
497 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
498 unsigned int i;
499 const DWORD* pFunc;
500 const DWORD* pName;
501 const WORD* pOrdl;
502 DWORD* funcs;
504 if (!exportDir) return;
506 printf("Exports table:\n");
507 printf("\n");
508 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
509 printf(" Characteristics: %08x\n", exportDir->Characteristics);
510 printf(" TimeDateStamp: %08X %s\n",
511 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
512 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
513 printf(" Ordinal base: %u\n", exportDir->Base);
514 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
515 printf(" # of Names: %u\n", exportDir->NumberOfNames);
516 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
517 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
518 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
519 printf("\n");
520 printf(" Entry Pt Ordn Name\n");
522 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
523 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
524 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
525 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
527 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
528 if (!funcs) fatal("no memory");
530 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
532 for (i = 0; i < exportDir->NumberOfFunctions; i++)
534 if (!pFunc[i]) continue;
535 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
536 if (funcs[i])
537 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
538 else
539 printf("<by ordinal>");
541 /* check for forwarded function */
542 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
543 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
544 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
545 printf("\n");
547 free(funcs);
548 printf("\n");
552 struct runtime_function_x86_64
554 DWORD BeginAddress;
555 DWORD EndAddress;
556 DWORD UnwindData;
559 struct runtime_function_armnt
561 DWORD BeginAddress;
562 union {
563 DWORD UnwindData;
564 struct {
565 DWORD Flag : 2;
566 DWORD FunctionLength : 11;
567 DWORD Ret : 2;
568 DWORD H : 1;
569 DWORD Reg : 3;
570 DWORD R : 1;
571 DWORD L : 1;
572 DWORD C : 1;
573 DWORD StackAdjust : 10;
574 } DUMMYSTRUCTNAME;
575 } DUMMYUNIONNAME;
578 struct runtime_function_arm64
580 DWORD BeginAddress;
581 union
583 DWORD UnwindData;
584 struct
586 DWORD Flag : 2;
587 DWORD FunctionLength : 11;
588 DWORD RegF : 3;
589 DWORD RegI : 4;
590 DWORD H : 1;
591 DWORD CR : 2;
592 DWORD FrameSize : 9;
593 } DUMMYSTRUCTNAME;
594 } DUMMYUNIONNAME;
597 union handler_data
599 struct runtime_function_x86_64 chain;
600 DWORD handler;
603 struct opcode
605 BYTE offset;
606 BYTE code : 4;
607 BYTE info : 4;
610 struct unwind_info_x86_64
612 BYTE version : 3;
613 BYTE flags : 5;
614 BYTE prolog;
615 BYTE count;
616 BYTE frame_reg : 4;
617 BYTE frame_offset : 4;
618 struct opcode opcodes[1]; /* count entries */
619 /* followed by union handler_data */
622 struct unwind_info_armnt
624 DWORD function_length : 18;
625 DWORD version : 2;
626 DWORD x : 1;
627 DWORD e : 1;
628 DWORD f : 1;
629 DWORD count : 5;
630 DWORD words : 4;
633 struct unwind_info_ext_armnt
635 WORD excount;
636 BYTE exwords;
637 BYTE reserved;
640 struct unwind_info_epilogue_armnt
642 DWORD offset : 18;
643 DWORD res : 2;
644 DWORD cond : 4;
645 DWORD index : 8;
648 #define UWOP_PUSH_NONVOL 0
649 #define UWOP_ALLOC_LARGE 1
650 #define UWOP_ALLOC_SMALL 2
651 #define UWOP_SET_FPREG 3
652 #define UWOP_SAVE_NONVOL 4
653 #define UWOP_SAVE_NONVOL_FAR 5
654 #define UWOP_SAVE_XMM128 8
655 #define UWOP_SAVE_XMM128_FAR 9
656 #define UWOP_PUSH_MACHFRAME 10
658 #define UNW_FLAG_EHANDLER 1
659 #define UNW_FLAG_UHANDLER 2
660 #define UNW_FLAG_CHAININFO 4
662 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
664 static const char * const reg_names[16] =
665 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
666 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
668 const union handler_data *handler_data;
669 const struct unwind_info_x86_64 *info;
670 unsigned int i, count;
672 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
673 if (function->UnwindData & 1)
675 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
676 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
677 return;
679 info = RVA( function->UnwindData, sizeof(*info) );
681 printf( " unwind info at %08x\n", function->UnwindData );
682 if (info->version != 1)
684 printf( " *** unknown version %u\n", info->version );
685 return;
687 printf( " flags %x", info->flags );
688 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
689 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
690 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
691 printf( "\n prolog 0x%x bytes\n", info->prolog );
693 if (info->frame_reg)
694 printf( " frame register %s offset 0x%x(%%rsp)\n",
695 reg_names[info->frame_reg], info->frame_offset * 16 );
697 for (i = 0; i < info->count; i++)
699 printf( " 0x%02x: ", info->opcodes[i].offset );
700 switch (info->opcodes[i].code)
702 case UWOP_PUSH_NONVOL:
703 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
704 break;
705 case UWOP_ALLOC_LARGE:
706 if (info->opcodes[i].info)
708 count = *(const DWORD *)&info->opcodes[i+1];
709 i += 2;
711 else
713 count = *(const USHORT *)&info->opcodes[i+1] * 8;
714 i++;
716 printf( "sub $0x%x,%%rsp\n", count );
717 break;
718 case UWOP_ALLOC_SMALL:
719 count = (info->opcodes[i].info + 1) * 8;
720 printf( "sub $0x%x,%%rsp\n", count );
721 break;
722 case UWOP_SET_FPREG:
723 printf( "lea 0x%x(%%rsp),%s\n",
724 info->frame_offset * 16, reg_names[info->frame_reg] );
725 break;
726 case UWOP_SAVE_NONVOL:
727 count = *(const USHORT *)&info->opcodes[i+1] * 8;
728 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
729 i++;
730 break;
731 case UWOP_SAVE_NONVOL_FAR:
732 count = *(const DWORD *)&info->opcodes[i+1];
733 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
734 i += 2;
735 break;
736 case UWOP_SAVE_XMM128:
737 count = *(const USHORT *)&info->opcodes[i+1] * 16;
738 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
739 i++;
740 break;
741 case UWOP_SAVE_XMM128_FAR:
742 count = *(const DWORD *)&info->opcodes[i+1];
743 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
744 i += 2;
745 break;
746 case UWOP_PUSH_MACHFRAME:
747 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
748 break;
749 default:
750 printf( "*** unknown code %u\n", info->opcodes[i].code );
751 break;
755 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
756 if (info->flags & UNW_FLAG_CHAININFO)
758 printf( " -> function %08x-%08x\n",
759 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
760 return;
762 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
763 printf( " handler %08x data at %08x\n", handler_data->handler,
764 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
767 static const BYTE armnt_code_lengths[256] =
769 /* 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,
770 /* 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,
771 /* 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,
772 /* 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,
773 /* 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,
774 /* 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,
775 /* 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,
776 /* 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
779 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
781 const struct unwind_info_armnt *info;
782 const struct unwind_info_ext_armnt *infoex;
783 const struct unwind_info_epilogue_armnt *infoepi;
784 unsigned int rva;
785 WORD i, count = 0, words = 0;
787 if (fnc->u.s.Flag)
789 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
790 WORD pf = 0, ef = 0, sc = 0;
792 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
793 (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
794 printf( " Flag %x\n", fnc->u.s.Flag );
795 printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
796 printf( " Ret %x\n", fnc->u.s.Ret );
797 printf( " H %x\n", fnc->u.s.H );
798 printf( " Reg %x\n", fnc->u.s.Reg );
799 printf( " R %x\n", fnc->u.s.R );
800 printf( " L %x\n", fnc->u.s.L );
801 printf( " C %x\n", fnc->u.s.C );
802 printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
804 if (fnc->u.s.StackAdjust >= 0x03f4)
806 pf = fnc->u.s.StackAdjust & 0x04;
807 ef = fnc->u.s.StackAdjust & 0x08;
810 if (!fnc->u.s.R && !pf)
812 if (fnc->u.s.Reg)
814 sprintf(intregs, "r4-r%u", fnc->u.s.Reg + 4);
815 sprintf(intregspop, "r4-r%u", fnc->u.s.Reg + 4);
817 else
819 strcpy(intregs, "r4");
820 strcpy(intregspop, "r4");
822 sc = fnc->u.s.Reg + 1;
823 if (fnc->u.s.C || fnc->u.s.L)
825 strcat(intregs, ", ");
826 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
827 strcat(intregspop, ", ");
830 else if (fnc->u.s.R && pf)
832 if (((~fnc->u.s.StackAdjust) & 3) != 3)
834 sprintf(intregs, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
835 sprintf(intregspop, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
837 else
839 sprintf(intregs, "r3");
840 sprintf(intregspop, "r3");
842 sc = 4 - ((~fnc->u.s.StackAdjust) & 3);
843 if (fnc->u.s.C || fnc->u.s.L)
845 strcat(intregs, ", ");
846 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
847 strcat(intregspop, ", ");
850 else if (!fnc->u.s.R && pf)
852 sprintf(intregs, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
853 sprintf(intregspop, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
854 sc = fnc->u.s.Reg + 5 - ((~fnc->u.s.StackAdjust) & 3);
855 if (fnc->u.s.C || fnc->u.s.L)
857 strcat(intregs, ", ");
858 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
859 strcat(intregspop, ", ");
862 else if (fnc->u.s.R && !pf)
864 if (!fnc->u.s.C && !fnc->u.s.L)
866 strcpy(intregs, "none");
867 strcpy(intregspop, "none");
871 if (fnc->u.s.C && !fnc->u.s.L)
873 strcat(intregs, "r11");
874 strcat(intregspop, "r11");
876 else if (fnc->u.s.C && fnc->u.s.L)
878 strcat(intregs, "r11, lr");
879 if (fnc->u.s.H)
880 strcat(intregspop, "r11");
881 else
882 strcat(intregspop, "r11, pc");
884 else if (!fnc->u.s.C && fnc->u.s.L)
886 strcat(intregs, "lr");
887 if (!fnc->u.s.H)
888 strcat(intregspop, "pc");
891 if (fnc->u.s.R)
893 if (fnc->u.s.Reg)
894 sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
895 else
896 strcpy(vfpregs, "d8");
898 else
899 strcpy(vfpregs, "none");
901 if (fnc->u.s.H)
902 printf( " Unwind Code\tpush {r0-r3}\n" );
904 if (fnc->u.s.R || fnc->u.s.L || fnc->u.s.C || pf)
905 printf( " Unwind Code\tpush {%s}\n", intregs );
907 if (fnc->u.s.C && fnc->u.s.R && !fnc->u.s.L && !pf)
908 printf( " Unwind Code\tmov r11, sp\n" );
909 else if (fnc->u.s.C && (!fnc->u.s.R || fnc->u.s.L || pf))
911 if (fnc->u.s.StackAdjust >= 0x03f4 && !sc)
912 printf( " Unwind Code\tadd r11, sp, #<unknown>\n");
913 else if (fnc->u.s.StackAdjust >= 0x03f4)
914 printf( " Unwind Code\tadd r11, sp, #%d\n", sc * 4 );
915 else
916 printf( " Unwind Code\tadd r11, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
919 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
920 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
922 if (fnc->u.s.StackAdjust < 0x03f4 && !pf)
923 printf( " Unwind Code\tsub sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
926 if (fnc->u.s.StackAdjust < 0x03f4 && !ef)
927 printf( " Unwind Code\tadd sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
929 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
930 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
932 if (fnc->u.s.C || !fnc->u.s.R || ef || (fnc->u.s.L && !fnc->u.s.H))
933 printf( " Unwind Code\tpop {%s}\n", intregspop );
935 if (fnc->u.s.H && !fnc->u.s.L)
936 printf( " Unwind Code\tadd sp, sp, #16\n" );
937 else if (fnc->u.s.H && fnc->u.s.L)
938 printf( " Unwind Code\tldr pc, [sp], #20\n" );
940 if (fnc->u.s.Ret == 1)
941 printf( " Unwind Code\tbx <reg>\n" );
942 else if (fnc->u.s.Ret == 2)
943 printf( " Unwind Code\tb <address>\n" );
945 return;
948 info = RVA( fnc->u.UnwindData, sizeof(*info) );
949 rva = fnc->u.UnwindData + sizeof(*info);
950 count = info->count;
951 words = info->words;
953 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
954 (fnc->BeginAddress & ~1) + info->function_length * 2 );
955 printf( " unwind info at %08x\n", fnc->u.UnwindData );
956 printf( " Flag %x\n", fnc->u.s.Flag );
957 printf( " FunctionLength %x\n", info->function_length );
958 printf( " Version %x\n", info->version );
959 printf( " X %x\n", info->x );
960 printf( " E %x\n", info->e );
961 printf( " F %x\n", info->f );
962 printf( " Count %x\n", count );
963 printf( " Words %x\n", words );
965 if (!info->count && !info->words)
967 infoex = RVA( rva, sizeof(*infoex) );
968 rva = rva + sizeof(*infoex);
969 count = infoex->excount;
970 words = infoex->exwords;
971 printf( " ExtCount %x\n", count );
972 printf( " ExtWords %x\n", words );
975 if (!info->e)
977 infoepi = RVA( rva, count * sizeof(*infoepi) );
978 rva = rva + count * sizeof(*infoepi);
980 for (i = 0; i < count; i++)
982 printf( " Epilogue Scope %x\n", i );
983 printf( " Offset %x\n", infoepi[i].offset );
984 printf( " Reserved %x\n", infoepi[i].res );
985 printf( " Condition %x\n", infoepi[i].cond );
986 printf( " Index %x\n", infoepi[i].index );
989 else
990 infoepi = NULL;
992 if (words)
994 const unsigned int *codes;
995 BYTE b, *bytes;
996 BOOL inepilogue = FALSE;
998 codes = RVA( rva, words * sizeof(*codes) );
999 rva = rva + words * sizeof(*codes);
1000 bytes = (BYTE*)codes;
1002 for (b = 0; b < words * sizeof(*codes); b++)
1004 BYTE code = bytes[b];
1005 BYTE len = armnt_code_lengths[code];
1007 if (info->e && b == count)
1009 printf( "Epilogue:\n" );
1010 inepilogue = TRUE;
1012 else if (!info->e && infoepi)
1014 for (i = 0; i < count; i++)
1015 if (b == infoepi[i].index)
1017 printf( "Epilogue from Scope %x at %08x:\n", i,
1018 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1019 inepilogue = TRUE;
1023 printf( " Unwind Code");
1024 for (i = 0; i < len; i++)
1025 printf( " %02x", bytes[b+i] );
1026 printf( "\t" );
1028 if (code == 0x00)
1029 printf( "\n" );
1030 else if (code <= 0x7f)
1031 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1032 else if (code <= 0xbf)
1034 WORD excode, f;
1035 BOOL first = TRUE;
1036 BYTE excodes = bytes[++b];
1038 excode = (code << 8) | excodes;
1039 printf( "%s {", inepilogue ? "pop" : "push" );
1041 for (f = 0; f <= 12; f++)
1043 if ((excode >> f) & 1)
1045 printf( "%sr%u", first ? "" : ", ", f );
1046 first = FALSE;
1050 if (excode & 0x2000)
1051 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1053 printf( "}\n" );
1055 else if (code <= 0xcf)
1056 if (inepilogue)
1057 printf( "mov sp, r%u\n", code & 0x0f );
1058 else
1059 printf( "mov r%u, sp\n", code & 0x0f );
1060 else if (code <= 0xd7)
1061 if (inepilogue)
1062 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1063 else
1064 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1065 else if (code <= 0xdf)
1066 if (inepilogue)
1067 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1068 else
1069 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1070 else if (code <= 0xe7)
1071 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1072 else if (code <= 0xeb)
1074 WORD excode;
1075 BYTE excodes = bytes[++b];
1077 excode = (code << 8) | excodes;
1078 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1080 else if (code <= 0xed)
1082 WORD excode, f;
1083 BOOL first = TRUE;
1084 BYTE excodes = bytes[++b];
1086 excode = (code << 8) | excodes;
1087 printf( "%s {", inepilogue ? "pop" : "push" );
1089 for (f = 0; f < 8; f++)
1091 if ((excode >> f) & 1)
1093 printf( "%sr%u", first ? "" : ", ", f );
1094 first = FALSE;
1098 if (excode & 0x0100)
1099 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1101 printf( "}\n" );
1103 else if (code == 0xee)
1104 printf( "unknown 16\n" );
1105 else if (code == 0xef)
1107 WORD excode;
1108 BYTE excodes = bytes[++b];
1110 if (excodes <= 0x0f)
1112 excode = (code << 8) | excodes;
1113 if (inepilogue)
1114 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1115 else
1116 printf( "unknown 32\n" );
1118 else
1119 printf( "unknown 32\n" );
1121 else if (code <= 0xf4)
1122 printf( "unknown\n" );
1123 else if (code <= 0xf6)
1125 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1126 BYTE excodes = bytes[++b];
1128 excode = (code << 8) | excodes;
1129 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1130 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1132 else if (code <= 0xf7)
1134 unsigned int excode;
1135 BYTE excodes[2];
1137 excodes[0] = bytes[++b];
1138 excodes[1] = bytes[++b];
1139 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1140 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1142 else if (code <= 0xf8)
1144 unsigned int excode;
1145 BYTE excodes[3];
1147 excodes[0] = bytes[++b];
1148 excodes[1] = bytes[++b];
1149 excodes[2] = bytes[++b];
1150 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1151 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1153 else if (code <= 0xf9)
1155 unsigned int excode;
1156 BYTE excodes[2];
1158 excodes[0] = bytes[++b];
1159 excodes[1] = bytes[++b];
1160 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1161 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1163 else if (code <= 0xfa)
1165 unsigned int excode;
1166 BYTE excodes[3];
1168 excodes[0] = bytes[++b];
1169 excodes[1] = bytes[++b];
1170 excodes[2] = bytes[++b];
1171 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1172 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1174 else if (code <= 0xfc)
1175 printf( "nop\n" );
1176 else if (code <= 0xfe)
1178 printf( "(end) nop\n" );
1179 inepilogue = TRUE;
1181 else
1183 printf( "end\n" );
1184 inepilogue = TRUE;
1189 if (info->x)
1191 const unsigned int *handler;
1193 handler = RVA( rva, sizeof(*handler) );
1194 rva = rva + sizeof(*handler);
1196 printf( " handler %08x data at %08x\n", *handler, rva);
1200 struct unwind_info_arm64
1202 DWORD function_length : 18;
1203 DWORD version : 2;
1204 DWORD x : 1;
1205 DWORD e : 1;
1206 DWORD epilog : 5;
1207 DWORD codes : 5;
1210 struct unwind_info_ext_arm64
1212 WORD epilog;
1213 BYTE codes;
1214 BYTE reserved;
1217 struct unwind_info_epilog_arm64
1219 DWORD offset : 18;
1220 DWORD res : 4;
1221 DWORD index : 10;
1224 static const BYTE code_lengths[256] =
1226 /* 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,
1227 /* 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,
1228 /* 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,
1229 /* 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,
1230 /* 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,
1231 /* 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,
1232 /* 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,
1233 /* 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
1236 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1238 unsigned int i, j;
1240 for (i = 0; i < count; i += code_lengths[ptr[i]])
1242 BYTE len = code_lengths[ptr[i]];
1243 unsigned int val = ptr[i];
1244 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1245 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1247 printf( " %04x: ", i );
1248 for (j = 0; j < 4; j++)
1249 if (j < len) printf( "%02x ", ptr[i+j] );
1250 else printf( " " );
1252 if (ptr[i] < 0x20) /* alloc_s */
1254 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1256 else if (ptr[i] < 0x40) /* save_r19r20_x */
1258 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1260 else if (ptr[i] < 0x80) /* save_fplr */
1262 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1264 else if (ptr[i] < 0xc0) /* save_fplr_x */
1266 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1268 else if (ptr[i] < 0xc8) /* alloc_m */
1270 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1272 else if (ptr[i] < 0xcc) /* save_regp */
1274 int reg = 19 + ((val >> 6) & 0xf);
1275 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1277 else if (ptr[i] < 0xd0) /* save_regp_x */
1279 int reg = 19 + ((val >> 6) & 0xf);
1280 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1282 else if (ptr[i] < 0xd4) /* save_reg */
1284 int reg = 19 + ((val >> 6) & 0xf);
1285 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1287 else if (ptr[i] < 0xd6) /* save_reg_x */
1289 int reg = 19 + ((val >> 5) & 0xf);
1290 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1292 else if (ptr[i] < 0xd8) /* save_lrpair */
1294 int reg = 19 + 2 * ((val >> 6) & 0x7);
1295 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1297 else if (ptr[i] < 0xda) /* save_fregp */
1299 int reg = 8 + ((val >> 6) & 0x7);
1300 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1302 else if (ptr[i] < 0xdc) /* save_fregp_x */
1304 int reg = 8 + ((val >> 6) & 0x7);
1305 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1307 else if (ptr[i] < 0xde) /* save_freg */
1309 int reg = 8 + ((val >> 6) & 0x7);
1310 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1312 else if (ptr[i] == 0xde) /* save_freg_x */
1314 int reg = 8 + ((val >> 5) & 0x7);
1315 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1317 else if (ptr[i] == 0xe0) /* alloc_l */
1319 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1321 else if (ptr[i] == 0xe1) /* set_fp */
1323 printf( "mov x29,sp\n" );
1325 else if (ptr[i] == 0xe2) /* add_fp */
1327 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1329 else if (ptr[i] == 0xe3) /* nop */
1331 printf( "nop\n" );
1333 else if (ptr[i] == 0xe4) /* end */
1335 printf( "end\n" );
1337 else if (ptr[i] == 0xe5) /* end_c */
1339 printf( "end_c\n" );
1341 else if (ptr[i] == 0xe6) /* save_next */
1343 printf( "save_next\n" );
1345 else if (ptr[i] == 0xe7) /* arithmetic */
1347 switch ((val >> 4) & 0x0f)
1349 case 0: printf( "add lr,lr,x28\n" ); break;
1350 case 1: printf( "add lr,lr,sp\n" ); break;
1351 case 2: printf( "sub lr,lr,x28\n" ); break;
1352 case 3: printf( "sub lr,lr,sp\n" ); break;
1353 case 4: printf( "eor lr,lr,x28\n" ); break;
1354 case 5: printf( "eor lr,lr,sp\n" ); break;
1355 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1356 case 8: printf( "ror lr,lr,x28\n" ); break;
1357 case 9: printf( "ror lr,lr,sp\n" ); break;
1358 default:printf( "unknown op\n" ); break;
1361 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1363 printf( "MSFT_OP_TRAP_FRAME\n" );
1365 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1367 printf( "MSFT_OP_MACHINE_FRAME\n" );
1369 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1371 printf( "MSFT_OP_CONTEXT\n" );
1373 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1375 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1377 else printf( "??\n");
1381 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1383 int i, pos = 0, intsz = func->u.s.RegI * 8, fpsz = func->u.s.RegF * 8, savesz, locsz;
1385 if (func->u.s.CR == 1) intsz += 8;
1386 if (func->u.s.RegF) fpsz += 8;
1388 savesz = ((intsz + fpsz + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
1389 locsz = func->u.s.FrameSize * 16 - savesz;
1391 switch (func->u.s.CR)
1393 case 3:
1394 printf( " %04x: mov x29,sp\n", pos++ );
1395 if (locsz <= 512)
1397 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1398 break;
1400 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1401 /* fall through */
1402 case 0:
1403 case 1:
1404 if (locsz <= 4080)
1406 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1408 else
1410 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1411 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1413 break;
1416 if (func->u.s.H)
1418 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1419 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1420 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1421 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1424 if (func->u.s.RegF)
1426 if (func->u.s.RegF % 2 == 0)
1427 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->u.s.RegF, intsz + fpsz - 8 );
1428 for (i = (func->u.s.RegF - 1)/ 2; i >= 0; i--)
1430 if (!i && !intsz)
1431 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1432 else
1433 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1437 switch (func->u.s.RegI)
1439 case 0:
1440 if (func->u.s.CR == 1)
1441 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1442 break;
1443 case 1:
1444 if (func->u.s.CR == 1)
1445 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1446 else
1447 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1448 break;
1449 default:
1450 if (func->u.s.RegI % 2)
1452 if (func->u.s.CR == 1)
1453 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1454 else
1455 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1457 else if (func->u.s.CR == 1)
1458 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1460 for (i = func->u.s.RegI / 2 - 1; i >= 0; i--)
1461 if (i)
1462 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1463 else
1464 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1465 break;
1467 printf( " %04x: end\n", pos );
1470 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1472 const struct unwind_info_arm64 *info;
1473 const struct unwind_info_ext_arm64 *infoex;
1474 const struct unwind_info_epilog_arm64 *infoepi;
1475 const BYTE *ptr;
1476 unsigned int i, rva, codes, epilogs;
1478 if (func->u.s.Flag)
1480 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1481 func->BeginAddress + func->u.s.FunctionLength * 4 );
1482 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1483 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
1484 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
1485 dump_arm64_packed_info( func );
1486 return;
1489 rva = func->u.UnwindData;
1490 info = RVA( rva, sizeof(*info) );
1491 rva += sizeof(*info);
1492 epilogs = info->epilog;
1493 codes = info->codes;
1495 if (!codes)
1497 infoex = RVA( rva, sizeof(*infoex) );
1498 rva = rva + sizeof(*infoex);
1499 codes = infoex->codes;
1500 epilogs = infoex->epilog;
1502 printf( "\nFunction %08x-%08x:\n",
1503 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1504 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1505 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1506 if (info->e)
1508 printf( " epilog 0: code=%04x\n", info->epilog );
1510 else
1512 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1513 rva += sizeof(*infoepi) * epilogs;
1514 for (i = 0; i < epilogs; i++)
1515 printf( " epilog %u: pc=%08x code=%04x\n", i,
1516 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1518 ptr = RVA( rva, codes * 4);
1519 rva += codes * 4;
1520 if (info->x)
1522 const DWORD *handler = RVA( rva, sizeof(*handler) );
1523 rva += sizeof(*handler);
1524 printf( " handler: %08x data %08x\n", *handler, rva );
1526 dump_arm64_codes( ptr, codes * 4 );
1529 static void dump_dir_exceptions(void)
1531 unsigned int i, size = 0;
1532 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1533 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1535 if (!funcs) return;
1537 switch (file_header->Machine)
1539 case IMAGE_FILE_MACHINE_AMD64:
1540 size /= sizeof(struct runtime_function_x86_64);
1541 printf( "Exception info (%u functions):\n", size );
1542 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1543 break;
1544 case IMAGE_FILE_MACHINE_ARMNT:
1545 size /= sizeof(struct runtime_function_armnt);
1546 printf( "Exception info (%u functions):\n", size );
1547 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1548 break;
1549 case IMAGE_FILE_MACHINE_ARM64:
1550 size /= sizeof(struct runtime_function_arm64);
1551 printf( "Exception info (%u functions):\n", size );
1552 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1553 break;
1554 default:
1555 printf( "Exception information not supported for %s binaries\n",
1556 get_machine_str(file_header->Machine));
1557 break;
1562 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, DWORD thunk_rva)
1564 /* FIXME: This does not properly handle large images */
1565 const IMAGE_IMPORT_BY_NAME* iibn;
1566 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1568 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1569 printf(" %08x %4u <by ordinal>\n", thunk_rva, (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
1570 else
1572 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1573 if (!iibn)
1574 printf("Can't grab import by name info, skipping to next ordinal\n");
1575 else
1576 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1581 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, DWORD thunk_rva)
1583 const IMAGE_IMPORT_BY_NAME* iibn;
1584 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(DWORD))
1586 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1587 printf(" %08x %4u <by ordinal>\n", thunk_rva, IMAGE_ORDINAL32(il->u1.Ordinal));
1588 else
1590 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1591 if (!iibn)
1592 printf("Can't grab import by name info, skipping to next ordinal\n");
1593 else
1594 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1599 static void dump_dir_imported_functions(void)
1601 unsigned directorySize;
1602 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1604 if (!importDesc) return;
1606 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1608 for (;;)
1610 const IMAGE_THUNK_DATA32* il;
1612 if (!importDesc->Name || !importDesc->FirstThunk) break;
1614 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1615 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
1616 printf(" TimeDateStamp: %08X (%s)\n",
1617 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1618 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
1619 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
1621 printf(" Thunk Ordn Name\n");
1623 il = (importDesc->u.OriginalFirstThunk != 0) ?
1624 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
1625 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1627 if (!il)
1628 printf("Can't grab thunk data, going to next imported DLL\n");
1629 else
1631 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1632 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1633 else
1634 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1635 printf("\n");
1637 importDesc++;
1639 printf("\n");
1642 static void dump_dir_loadconfig(void)
1644 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1645 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1647 if (!loadcfg32) return;
1649 printf( "Loadconfig\n" );
1650 print_dword( "Size", loadcfg32->Size );
1651 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1652 print_word( "MajorVersion", loadcfg32->MajorVersion );
1653 print_word( "MinorVersion", loadcfg32->MinorVersion );
1654 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1655 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1656 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1658 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1660 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1661 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1662 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1663 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1664 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1665 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1666 print_word( "CSDVersion", loadcfg64->CSDVersion );
1667 print_word( "Reserved", loadcfg64->Reserved1 );
1668 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1669 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1670 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1672 else
1674 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1675 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1676 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1677 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1678 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1679 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1680 print_word( "CSDVersion", loadcfg32->CSDVersion );
1681 print_word( "Reserved", loadcfg32->Reserved1 );
1682 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1683 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1684 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1688 static void dump_dir_delay_imported_functions(void)
1690 unsigned directorySize;
1691 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1693 if (!importDesc) return;
1695 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1697 for (;;)
1699 const IMAGE_THUNK_DATA32* il;
1700 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1702 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1704 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1705 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1706 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1707 printf(" Address Table: %08x\n", importDesc->ImportAddressTableRVA);
1708 printf(" TimeDateStamp: %08X (%s)\n",
1709 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1711 printf(" Thunk Ordn Name\n");
1713 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1715 if (!il)
1716 printf("Can't grab thunk data, going to next imported DLL\n");
1717 else
1719 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1720 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1721 else
1722 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1723 printf("\n");
1725 importDesc++;
1727 printf("\n");
1730 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1732 const char* str;
1734 printf("Directory %02u\n", idx + 1);
1735 printf(" Characteristics: %08X\n", idd->Characteristics);
1736 printf(" TimeDateStamp: %08X %s\n",
1737 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1738 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1739 switch (idd->Type)
1741 default:
1742 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1743 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1744 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1745 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1746 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1747 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1748 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1749 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1750 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1751 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1752 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1753 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1754 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1755 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1756 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1757 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1758 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
1760 printf(" Type: %u (%s)\n", idd->Type, str);
1761 printf(" SizeOfData: %u\n", idd->SizeOfData);
1762 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1763 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1765 switch (idd->Type)
1767 case IMAGE_DEBUG_TYPE_UNKNOWN:
1768 break;
1769 case IMAGE_DEBUG_TYPE_COFF:
1770 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1771 IMAGE_FIRST_SECTION(PE_nt_headers));
1772 break;
1773 case IMAGE_DEBUG_TYPE_CODEVIEW:
1774 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1775 break;
1776 case IMAGE_DEBUG_TYPE_FPO:
1777 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1778 break;
1779 case IMAGE_DEBUG_TYPE_MISC:
1781 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1782 if (!misc) {printf("Can't get misc debug information\n"); break;}
1783 printf(" DataType: %u (%s)\n",
1784 misc->DataType,
1785 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1786 printf(" Length: %u\n", misc->Length);
1787 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1788 printf(" Data: %s\n", misc->Data);
1790 break;
1791 default: break;
1793 printf("\n");
1796 static void dump_dir_debug(void)
1798 unsigned nb_dbg, i;
1799 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1801 nb_dbg /= sizeof(*debugDir);
1802 if (!debugDir || !nb_dbg) return;
1804 printf("Debug Table (%u directories)\n", nb_dbg);
1806 for (i = 0; i < nb_dbg; i++)
1808 dump_dir_debug_dir(debugDir, i);
1809 debugDir++;
1811 printf("\n");
1814 static inline void print_clrflags(const char *title, DWORD value)
1816 printf(" %-34s 0x%X\n", title, value);
1817 #define X(f,s) if (value & f) printf(" %s\n", s)
1818 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1819 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1820 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1821 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1822 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1823 #undef X
1826 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1828 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1831 static void dump_dir_clr_header(void)
1833 unsigned int size = 0;
1834 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1836 if (!dir) return;
1838 printf( "CLR Header\n" );
1839 print_dword( "Header Size", dir->cb );
1840 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1841 print_clrflags( "Flags", dir->Flags );
1842 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1843 printf("\n");
1844 printf( "CLR Data Directory\n" );
1845 print_clrdirectory( "MetaData", &dir->MetaData );
1846 print_clrdirectory( "Resources", &dir->Resources );
1847 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1848 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1849 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1850 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1851 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1852 printf("\n");
1855 static void dump_dir_reloc(void)
1857 unsigned int i, size = 0;
1858 const USHORT *relocs;
1859 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1860 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1861 static const char * const names[] =
1863 "BASED_ABSOLUTE",
1864 "BASED_HIGH",
1865 "BASED_LOW",
1866 "BASED_HIGHLOW",
1867 "BASED_HIGHADJ",
1868 "BASED_MIPS_JMPADDR",
1869 "BASED_SECTION",
1870 "BASED_REL",
1871 "unknown 8",
1872 "BASED_IA64_IMM64",
1873 "BASED_DIR64",
1874 "BASED_HIGH3ADJ",
1875 "unknown 12",
1876 "unknown 13",
1877 "unknown 14",
1878 "unknown 15"
1881 if (!rel) return;
1883 printf( "Relocations\n" );
1884 while (rel < end - 1 && rel->SizeOfBlock)
1886 printf( " Page %x\n", rel->VirtualAddress );
1887 relocs = (const USHORT *)(rel + 1);
1888 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1889 while (i--)
1891 USHORT offset = *relocs & 0xfff;
1892 int type = *relocs >> 12;
1893 printf( " off %04x type %s\n", offset, names[type] );
1894 relocs++;
1896 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1898 printf("\n");
1901 static void dump_dir_tls(void)
1903 IMAGE_TLS_DIRECTORY64 dir;
1904 const DWORD *callbacks;
1905 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1907 if (!pdir) return;
1909 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1910 memcpy(&dir, pdir, sizeof(dir));
1911 else
1913 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1914 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1915 dir.AddressOfIndex = pdir->AddressOfIndex;
1916 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1917 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1918 dir.Characteristics = pdir->Characteristics;
1921 /* FIXME: This does not properly handle large images */
1922 printf( "Thread Local Storage\n" );
1923 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1924 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1925 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1926 (DWORD)dir.SizeOfZeroFill );
1927 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1928 printf( " Characteristics %08x\n", dir.Characteristics );
1929 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1930 if (dir.AddressOfCallBacks)
1932 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1933 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1935 printf( " %08x", *callbacks );
1936 addr += sizeof(DWORD);
1939 printf(" }\n\n");
1942 enum FileSig get_kind_dbg(void)
1944 const WORD* pw;
1946 pw = PRD(0, sizeof(WORD));
1947 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1949 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1950 return SIG_UNKNOWN;
1953 void dbg_dump(void)
1955 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1956 unsigned nb_dbg;
1957 unsigned i;
1958 const IMAGE_DEBUG_DIRECTORY* debugDir;
1960 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1961 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1963 printf ("Signature: %.2s (0x%4X)\n",
1964 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1965 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1966 printf ("Machine: 0x%04X (%s)\n",
1967 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1968 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1969 printf ("TimeDateStamp: 0x%08X (%s)\n",
1970 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1971 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1972 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1973 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1974 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1975 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1976 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1978 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1979 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1980 {printf("Can't get the sections, aborting\n"); return;}
1982 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1984 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1985 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1986 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1987 separateDebugHead->ExportedNamesSize,
1988 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1989 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1991 printf("Debug Table (%u directories)\n", nb_dbg);
1993 for (i = 0; i < nb_dbg; i++)
1995 dump_dir_debug_dir(debugDir, i);
1996 debugDir++;
2000 static const char *get_resource_type( unsigned int id )
2002 static const char * const types[] =
2004 NULL,
2005 "CURSOR",
2006 "BITMAP",
2007 "ICON",
2008 "MENU",
2009 "DIALOG",
2010 "STRING",
2011 "FONTDIR",
2012 "FONT",
2013 "ACCELERATOR",
2014 "RCDATA",
2015 "MESSAGETABLE",
2016 "GROUP_CURSOR",
2017 NULL,
2018 "GROUP_ICON",
2019 NULL,
2020 "VERSION",
2021 "DLGINCLUDE",
2022 NULL,
2023 "PLUGPLAY",
2024 "VXD",
2025 "ANICURSOR",
2026 "ANIICON",
2027 "HTML",
2028 "RT_MANIFEST"
2031 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2032 return NULL;
2035 /* dump an ASCII string with proper escaping */
2036 static int dump_strA( const unsigned char *str, size_t len )
2038 static const char escapes[32] = ".......abtnvfr.............e....";
2039 char buffer[256];
2040 char *pos = buffer;
2041 int count = 0;
2043 for (; len; str++, len--)
2045 if (pos > buffer + sizeof(buffer) - 8)
2047 fwrite( buffer, pos - buffer, 1, stdout );
2048 count += pos - buffer;
2049 pos = buffer;
2051 if (*str > 127) /* hex escape */
2053 pos += sprintf( pos, "\\x%02x", *str );
2054 continue;
2056 if (*str < 32) /* octal or C escape */
2058 if (!*str && len == 1) continue; /* do not output terminating NULL */
2059 if (escapes[*str] != '.')
2060 pos += sprintf( pos, "\\%c", escapes[*str] );
2061 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2062 pos += sprintf( pos, "\\%03o", *str );
2063 else
2064 pos += sprintf( pos, "\\%o", *str );
2065 continue;
2067 if (*str == '\\') *pos++ = '\\';
2068 *pos++ = *str;
2070 fwrite( buffer, pos - buffer, 1, stdout );
2071 count += pos - buffer;
2072 return count;
2075 /* dump a Unicode string with proper escaping */
2076 static int dump_strW( const WCHAR *str, size_t len )
2078 static const char escapes[32] = ".......abtnvfr.............e....";
2079 char buffer[256];
2080 char *pos = buffer;
2081 int count = 0;
2083 for (; len; str++, len--)
2085 if (pos > buffer + sizeof(buffer) - 8)
2087 fwrite( buffer, pos - buffer, 1, stdout );
2088 count += pos - buffer;
2089 pos = buffer;
2091 if (*str > 127) /* hex escape */
2093 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2094 pos += sprintf( pos, "\\x%04x", *str );
2095 else
2096 pos += sprintf( pos, "\\x%x", *str );
2097 continue;
2099 if (*str < 32) /* octal or C escape */
2101 if (!*str && len == 1) continue; /* do not output terminating NULL */
2102 if (escapes[*str] != '.')
2103 pos += sprintf( pos, "\\%c", escapes[*str] );
2104 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2105 pos += sprintf( pos, "\\%03o", *str );
2106 else
2107 pos += sprintf( pos, "\\%o", *str );
2108 continue;
2110 if (*str == '\\') *pos++ = '\\';
2111 *pos++ = *str;
2113 fwrite( buffer, pos - buffer, 1, stdout );
2114 count += pos - buffer;
2115 return count;
2118 /* dump data for a STRING resource */
2119 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2121 int i;
2123 for (i = 0; i < 16 && size; i++)
2125 unsigned len = *ptr++;
2127 if (len >= size)
2129 len = size;
2130 size = 0;
2132 else size -= len + 1;
2134 if (len)
2136 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2137 dump_strW( ptr, len );
2138 printf( "\"\n" );
2139 ptr += len;
2144 /* dump data for a MESSAGETABLE resource */
2145 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2147 const MESSAGE_RESOURCE_DATA *data = ptr;
2148 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2149 unsigned i, j;
2151 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2153 const MESSAGE_RESOURCE_ENTRY *entry;
2155 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2156 for (j = block->LowId; j <= block->HighId; j++)
2158 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2160 const WCHAR *str = (const WCHAR *)entry->Text;
2161 printf( "%s%08x L\"", prefix, j );
2162 dump_strW( str, strlenW(str) );
2163 printf( "\"\n" );
2165 else
2167 const char *str = (const char *) entry->Text;
2168 printf( "%s%08x \"", prefix, j );
2169 dump_strA( entry->Text, strlen(str) );
2170 printf( "\"\n" );
2172 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2177 static void dump_dir_resource(void)
2179 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2180 const IMAGE_RESOURCE_DIRECTORY *namedir;
2181 const IMAGE_RESOURCE_DIRECTORY *langdir;
2182 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2183 const IMAGE_RESOURCE_DIR_STRING_U *string;
2184 const IMAGE_RESOURCE_DATA_ENTRY *data;
2185 int i, j, k;
2187 if (!root) return;
2189 printf( "Resources:" );
2191 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2193 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2194 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
2195 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2197 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2198 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
2199 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2201 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2203 printf( "\n " );
2204 if (e1->u.s.NameIsString)
2206 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
2207 dump_unicode_str( string->NameString, string->Length );
2209 else
2211 const char *type = get_resource_type( e1->u.Id );
2212 if (type) printf( "%s", type );
2213 else printf( "%04x", e1->u.Id );
2216 printf( " Name=" );
2217 if (e2->u.s.NameIsString)
2219 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
2220 dump_unicode_str( string->NameString, string->Length );
2222 else
2223 printf( "%04x", e2->u.Id );
2225 printf( " Language=%04x:\n", e3->u.Id );
2226 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
2227 if (e1->u.s.NameIsString)
2229 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2231 else switch(e1->u.Id)
2233 case 6:
2234 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
2235 break;
2236 case 11:
2237 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
2238 e2->u.Id, " " );
2239 break;
2240 default:
2241 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2242 break;
2247 printf( "\n\n" );
2250 static void dump_debug(void)
2252 const char* stabs = NULL;
2253 unsigned szstabs = 0;
2254 const char* stabstr = NULL;
2255 unsigned szstr = 0;
2256 unsigned i;
2257 const IMAGE_SECTION_HEADER* sectHead;
2259 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2261 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2263 if (!strcmp((const char *)sectHead->Name, ".stab"))
2265 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2266 szstabs = sectHead->Misc.VirtualSize;
2268 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2270 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2271 szstr = sectHead->Misc.VirtualSize;
2274 if (stabs && stabstr)
2275 dump_stabs(stabs, szstabs, stabstr, szstr);
2278 static void dump_symbol_table(void)
2280 const IMAGE_SYMBOL* sym;
2281 int numsym;
2283 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2284 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2285 return;
2286 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2287 sizeof(*sym) * numsym);
2288 if (!sym) return;
2290 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2293 enum FileSig get_kind_exec(void)
2295 const WORD* pw;
2296 const DWORD* pdw;
2297 const IMAGE_DOS_HEADER* dh;
2299 pw = PRD(0, sizeof(WORD));
2300 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2302 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2304 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2306 /* the signature is the first DWORD */
2307 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2308 if (pdw)
2310 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2311 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2312 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2314 return SIG_DOS;
2316 return SIG_UNKNOWN;
2319 void pe_dump(void)
2321 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
2323 PE_nt_headers = get_nt_header();
2324 print_fake_dll();
2326 if (globals.do_dumpheader)
2328 dump_pe_header();
2329 /* FIXME: should check ptr */
2330 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2331 PE_nt_headers->FileHeader.NumberOfSections);
2333 else if (!globals.dumpsect)
2335 /* show at least something here */
2336 dump_pe_header();
2339 if (globals.dumpsect)
2341 if (all || !strcmp(globals.dumpsect, "import"))
2343 dump_dir_imported_functions();
2344 dump_dir_delay_imported_functions();
2346 if (all || !strcmp(globals.dumpsect, "export"))
2347 dump_dir_exported_functions();
2348 if (all || !strcmp(globals.dumpsect, "debug"))
2349 dump_dir_debug();
2350 if (all || !strcmp(globals.dumpsect, "resource"))
2351 dump_dir_resource();
2352 if (all || !strcmp(globals.dumpsect, "tls"))
2353 dump_dir_tls();
2354 if (all || !strcmp(globals.dumpsect, "loadcfg"))
2355 dump_dir_loadconfig();
2356 if (all || !strcmp(globals.dumpsect, "clr"))
2357 dump_dir_clr_header();
2358 if (all || !strcmp(globals.dumpsect, "reloc"))
2359 dump_dir_reloc();
2360 if (all || !strcmp(globals.dumpsect, "except"))
2361 dump_dir_exceptions();
2363 if (globals.do_symbol_table)
2364 dump_symbol_table();
2365 if (globals.do_debug)
2366 dump_debug();
2369 typedef struct _dll_symbol {
2370 size_t ordinal;
2371 char *symbol;
2372 } dll_symbol;
2374 static dll_symbol *dll_symbols = NULL;
2375 static dll_symbol *dll_current_symbol = NULL;
2377 /* Compare symbols by ordinal for qsort */
2378 static int symbol_cmp(const void *left, const void *right)
2380 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2383 /*******************************************************************
2384 * dll_close
2386 * Free resources used by DLL
2388 /* FIXME: Not used yet
2389 static void dll_close (void)
2391 dll_symbol* ds;
2393 if (!dll_symbols) {
2394 fatal("No symbols");
2396 for (ds = dll_symbols; ds->symbol; ds++)
2397 free(ds->symbol);
2398 free (dll_symbols);
2399 dll_symbols = NULL;
2403 static void do_grab_sym( void )
2405 const IMAGE_EXPORT_DIRECTORY*exportDir;
2406 unsigned i, j;
2407 const DWORD* pName;
2408 const DWORD* pFunc;
2409 const WORD* pOrdl;
2410 const char* ptr;
2411 DWORD* map;
2413 PE_nt_headers = get_nt_header();
2414 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2416 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2417 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2418 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2419 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2420 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2421 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2423 /* dll_close(); */
2425 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
2427 /* bit map of used funcs */
2428 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2429 if (!map) fatal("no memory");
2431 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2433 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2434 ptr = RVA(*pName++, sizeof(DWORD));
2435 if (!ptr) ptr = "cant_get_function";
2436 dll_symbols[j].symbol = xstrdup(ptr);
2437 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2438 assert(dll_symbols[j].symbol);
2441 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2443 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2445 char ordinal_text[256];
2446 /* Ordinal only entry */
2447 sprintf (ordinal_text, "%s_%u",
2448 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2449 exportDir->Base + i);
2450 str_toupper(ordinal_text);
2451 dll_symbols[j].symbol = xstrdup(ordinal_text);
2452 assert(dll_symbols[j].symbol);
2453 dll_symbols[j].ordinal = exportDir->Base + i;
2454 j++;
2455 assert(j <= exportDir->NumberOfFunctions);
2458 free(map);
2460 if (NORMAL)
2461 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2462 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2464 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2466 dll_symbols[j].symbol = NULL;
2468 dll_current_symbol = dll_symbols;
2471 /*******************************************************************
2472 * dll_open
2474 * Open a DLL and read in exported symbols
2476 BOOL dll_open (const char *dll_name)
2478 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2481 /*******************************************************************
2482 * dll_next_symbol
2484 * Get next exported symbol from dll
2486 BOOL dll_next_symbol (parsed_symbol * sym)
2488 if (!dll_current_symbol || !dll_current_symbol->symbol)
2489 return FALSE;
2490 assert (dll_symbols);
2491 sym->symbol = xstrdup (dll_current_symbol->symbol);
2492 sym->ordinal = dll_current_symbol->ordinal;
2493 dll_current_symbol++;
2494 return TRUE;