dbghelp: Initialize more basic types.
[wine.git] / tools / winedump / pe.c
blob54250e4b32b56495fc14e90471eea2fe48a28692
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"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include <fcntl.h>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winedump.h"
48 static const IMAGE_NT_HEADERS32* PE_nt_headers;
50 const char *get_machine_str(int mach)
52 switch (mach)
54 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
55 case IMAGE_FILE_MACHINE_I860: return "i860";
56 case IMAGE_FILE_MACHINE_I386: return "i386";
57 case IMAGE_FILE_MACHINE_R3000: return "R3000";
58 case IMAGE_FILE_MACHINE_R4000: return "R4000";
59 case IMAGE_FILE_MACHINE_R10000: return "R10000";
60 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
61 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
62 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
63 case IMAGE_FILE_MACHINE_IA64: return "IA64";
64 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
65 case IMAGE_FILE_MACHINE_ARM: return "ARM";
66 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
67 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
69 return "???";
72 static const void* RVA(unsigned long rva, unsigned long len)
74 IMAGE_SECTION_HEADER* sectHead;
75 int i;
77 if (rva == 0) return NULL;
79 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
80 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
82 if (sectHead[i].VirtualAddress <= rva &&
83 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
85 /* return image import directory offset */
86 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
90 return NULL;
93 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
95 const IMAGE_DOS_HEADER *dos;
96 dos = PRD(0, sizeof(*dos));
97 if (!dos) return NULL;
98 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
101 static BOOL is_fake_dll( void )
103 static const char fakedll_signature[] = "Wine placeholder DLL";
104 const IMAGE_DOS_HEADER *dos;
106 dos = PRD(0, sizeof(*dos) + sizeof(fakedll_signature));
108 if (dos && dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
109 !memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return TRUE;
110 return FALSE;
113 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
115 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
117 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
118 if (idx >= opt->NumberOfRvaAndSizes)
119 return NULL;
120 if(size)
121 *size = opt->DataDirectory[idx].Size;
122 return RVA(opt->DataDirectory[idx].VirtualAddress,
123 opt->DataDirectory[idx].Size);
125 else
127 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
128 if (idx >= opt->NumberOfRvaAndSizes)
129 return NULL;
130 if(size)
131 *size = opt->DataDirectory[idx].Size;
132 return RVA(opt->DataDirectory[idx].VirtualAddress,
133 opt->DataDirectory[idx].Size);
137 static const void* get_dir(unsigned idx)
139 return get_dir_and_size(idx, 0);
142 static const char * const DirectoryNames[16] = {
143 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
144 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
145 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
146 "IAT", "Delay IAT", "CLR Header", ""
149 static const char *get_magic_type(WORD magic)
151 switch(magic) {
152 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
153 return "32bit";
154 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
155 return "64bit";
156 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
157 return "ROM";
159 return "???";
162 static inline void print_word(const char *title, WORD value)
164 printf(" %-34s 0x%-4X %u\n", title, value, value);
167 static inline void print_dword(const char *title, DWORD value)
169 printf(" %-34s 0x%-8x %u\n", title, value, value);
172 static inline void print_longlong(const char *title, ULONGLONG value)
174 printf(" %-34s 0x", title);
175 if(value >> 32)
176 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
177 else
178 printf("%lx\n", (unsigned long)value);
181 static inline void print_ver(const char *title, BYTE major, BYTE minor)
183 printf(" %-34s %u.%02u\n", title, major, minor);
186 static inline void print_subsys(const char *title, WORD value)
188 const char *str;
189 switch (value)
191 default:
192 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
193 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
194 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
195 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
196 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
197 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
198 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
199 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
200 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
201 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
202 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
203 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
204 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
205 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
207 printf(" %-34s 0x%X (%s)\n", title, value, str);
210 static inline void print_dllflags(const char *title, WORD value)
212 printf(" %-34s 0x%X\n", title, value);
213 #define X(f,s) if (value & f) printf(" %s\n", s)
214 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
215 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
216 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
217 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
218 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
219 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
220 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
221 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
222 #undef X
225 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
227 unsigned i;
228 printf("Data Directory\n");
230 for (i = 0; i < n && i < 16; i++)
232 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
233 DirectoryNames[i], directory[i].VirtualAddress,
234 directory[i].Size);
238 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
240 IMAGE_OPTIONAL_HEADER32 oh;
241 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
243 /* in case optional header is missing or partial */
244 memset(&oh, 0, sizeof(oh));
245 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
246 optionalHeader = &oh;
248 print_word("Magic", optionalHeader->Magic);
249 print_ver("linker version",
250 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
251 print_dword("size of code", optionalHeader->SizeOfCode);
252 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
253 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
254 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
255 print_dword("base of code", optionalHeader->BaseOfCode);
256 print_dword("base of data", optionalHeader->BaseOfData);
257 print_dword("image base", optionalHeader->ImageBase);
258 print_dword("section align", optionalHeader->SectionAlignment);
259 print_dword("file align", optionalHeader->FileAlignment);
260 print_ver("required OS version",
261 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
262 print_ver("image version",
263 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
264 print_ver("subsystem version",
265 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
266 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
267 print_dword("size of image", optionalHeader->SizeOfImage);
268 print_dword("size of headers", optionalHeader->SizeOfHeaders);
269 print_dword("checksum", optionalHeader->CheckSum);
270 print_subsys("Subsystem", optionalHeader->Subsystem);
271 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
272 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
273 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
274 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
275 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
276 print_dword("loader flags", optionalHeader->LoaderFlags);
277 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
278 printf("\n");
279 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
280 printf("\n");
283 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
285 IMAGE_OPTIONAL_HEADER64 oh;
286 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
288 /* in case optional header is missing or partial */
289 memset(&oh, 0, sizeof(oh));
290 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
291 optionalHeader = &oh;
293 print_word("Magic", optionalHeader->Magic);
294 print_ver("linker version",
295 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
296 print_dword("size of code", optionalHeader->SizeOfCode);
297 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
298 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
299 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
300 print_dword("base of code", optionalHeader->BaseOfCode);
301 print_longlong("image base", optionalHeader->ImageBase);
302 print_dword("section align", optionalHeader->SectionAlignment);
303 print_dword("file align", optionalHeader->FileAlignment);
304 print_ver("required OS version",
305 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
306 print_ver("image version",
307 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
308 print_ver("subsystem version",
309 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
310 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
311 print_dword("size of image", optionalHeader->SizeOfImage);
312 print_dword("size of headers", optionalHeader->SizeOfHeaders);
313 print_dword("checksum", optionalHeader->CheckSum);
314 print_subsys("Subsystem", optionalHeader->Subsystem);
315 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
316 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
317 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
318 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
319 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
320 print_dword("loader flags", optionalHeader->LoaderFlags);
321 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
322 printf("\n");
323 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
324 printf("\n");
327 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
329 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
331 switch(optionalHeader->Magic) {
332 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
333 dump_optional_header32(optionalHeader, header_size);
334 break;
335 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
336 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
337 break;
338 default:
339 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
340 break;
344 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
346 printf("File Header\n");
348 printf(" Machine: %04X (%s)\n",
349 fileHeader->Machine, get_machine_str(fileHeader->Machine));
350 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
351 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
352 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
353 Offset(&(fileHeader->TimeDateStamp)));
354 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
355 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
356 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
357 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
358 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
359 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
360 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
361 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
362 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
363 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
364 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
365 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
366 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
367 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
368 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
369 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
370 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
371 X(IMAGE_FILE_SYSTEM, "SYSTEM");
372 X(IMAGE_FILE_DLL, "DLL");
373 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
374 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
375 #undef X
376 printf("\n");
379 static void dump_pe_header(void)
381 dump_file_header(&PE_nt_headers->FileHeader);
382 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
385 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
387 unsigned offset;
389 /* long section name ? */
390 if (strtable && sectHead->Name[0] == '/' &&
391 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
392 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
393 else
394 printf(" %-8.8s", sectHead->Name);
395 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
396 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
397 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
398 sectHead->PointerToRawData, sectHead->SizeOfRawData);
399 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
400 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
401 printf(" line # offs: %-8u line #'s: %-8u\n",
402 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
403 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
404 printf(" ");
405 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
406 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
407 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
408 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
409 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
410 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
411 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
413 X(IMAGE_SCN_CNT_CODE, "CODE");
414 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
415 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
417 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
418 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
419 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
420 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
421 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
423 /* 0x00002000 - Reserved */
424 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
425 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
427 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
428 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
429 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
430 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
431 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
433 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
435 #define X2(b,s) case b: printf(" " s); break
436 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
437 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
438 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
439 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
440 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
441 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
442 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
443 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
444 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
445 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
446 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
447 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
448 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
449 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
450 #undef X2
453 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
455 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
456 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
457 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
458 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
459 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
460 X(IMAGE_SCN_MEM_READ, "MEM_READ");
461 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
462 #undef X
463 printf("\n\n");
466 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
468 const IMAGE_SECTION_HEADER* sectHead = addr;
469 unsigned i;
470 const char* strtable;
472 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
474 strtable = (const char*)base +
475 PE_nt_headers->FileHeader.PointerToSymbolTable +
476 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
478 else strtable = NULL;
480 printf("Section Table\n");
481 for (i = 0; i < num_sect; i++, sectHead++)
483 dump_section(sectHead, strtable);
485 if (globals.do_dump_rawdata)
487 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
488 printf("\n");
493 static void dump_dir_exported_functions(void)
495 unsigned int size = 0;
496 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
497 unsigned int i;
498 const DWORD* pFunc;
499 const DWORD* pName;
500 const WORD* pOrdl;
501 DWORD* funcs;
503 if (!exportDir) return;
505 printf("Exports table:\n");
506 printf("\n");
507 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
508 printf(" Characteristics: %08x\n", exportDir->Characteristics);
509 printf(" TimeDateStamp: %08X %s\n",
510 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
511 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
512 printf(" Ordinal base: %u\n", exportDir->Base);
513 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
514 printf(" # of Names: %u\n", exportDir->NumberOfNames);
515 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
516 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
517 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
518 printf("\n");
519 printf(" Entry Pt Ordn Name\n");
521 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
522 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
523 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
524 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
526 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
527 if (!funcs) fatal("no memory");
529 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
531 for (i = 0; i < exportDir->NumberOfFunctions; i++)
533 if (!pFunc[i]) continue;
534 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
535 if (funcs[i])
536 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
537 else
538 printf("<by ordinal>");
540 /* check for forwarded function */
541 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
542 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
543 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
544 printf("\n");
546 free(funcs);
547 printf("\n");
551 struct runtime_function_x86_64
553 DWORD BeginAddress;
554 DWORD EndAddress;
555 DWORD UnwindData;
558 struct runtime_function_armnt
560 DWORD BeginAddress;
561 union {
562 DWORD UnwindData;
563 struct {
564 DWORD Flag : 2;
565 DWORD FunctionLength : 11;
566 DWORD Ret : 2;
567 DWORD H : 1;
568 DWORD Reg : 3;
569 DWORD R : 1;
570 DWORD L : 1;
571 DWORD C : 1;
572 DWORD StackAdjust : 10;
573 } DUMMYSTRUCTNAME;
574 } DUMMYUNIONNAME;
577 union handler_data
579 struct runtime_function_x86_64 chain;
580 DWORD handler;
583 struct opcode
585 BYTE offset;
586 BYTE code : 4;
587 BYTE info : 4;
590 struct unwind_info_x86_64
592 BYTE version : 3;
593 BYTE flags : 5;
594 BYTE prolog;
595 BYTE count;
596 BYTE frame_reg : 4;
597 BYTE frame_offset : 4;
598 struct opcode opcodes[1]; /* count entries */
599 /* followed by union handler_data */
602 struct unwind_info_armnt
604 DWORD function_length : 18;
605 DWORD version : 2;
606 DWORD x : 1;
607 DWORD e : 1;
608 DWORD f : 1;
609 DWORD count : 5;
610 DWORD words : 4;
613 struct unwind_info_ext_armnt
615 WORD excount;
616 BYTE exwords;
617 BYTE reserved;
620 struct unwind_info_epilogue_armnt
622 DWORD offset : 18;
623 DWORD res : 2;
624 DWORD cond : 4;
625 DWORD index : 8;
628 #define UWOP_PUSH_NONVOL 0
629 #define UWOP_ALLOC_LARGE 1
630 #define UWOP_ALLOC_SMALL 2
631 #define UWOP_SET_FPREG 3
632 #define UWOP_SAVE_NONVOL 4
633 #define UWOP_SAVE_NONVOL_FAR 5
634 #define UWOP_SAVE_XMM128 8
635 #define UWOP_SAVE_XMM128_FAR 9
636 #define UWOP_PUSH_MACHFRAME 10
638 #define UNW_FLAG_EHANDLER 1
639 #define UNW_FLAG_UHANDLER 2
640 #define UNW_FLAG_CHAININFO 4
642 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
644 static const char * const reg_names[16] =
645 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
646 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
648 const union handler_data *handler_data;
649 const struct unwind_info_x86_64 *info;
650 unsigned int i, count;
652 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
653 if (function->UnwindData & 1)
655 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
656 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
657 return;
659 info = RVA( function->UnwindData, sizeof(*info) );
661 printf( " unwind info at %08x\n", function->UnwindData );
662 if (info->version != 1)
664 printf( " *** unknown version %u\n", info->version );
665 return;
667 printf( " flags %x", info->flags );
668 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
669 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
670 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
671 printf( "\n prolog 0x%x bytes\n", info->prolog );
673 if (info->frame_reg)
674 printf( " frame register %s offset 0x%x(%%rsp)\n",
675 reg_names[info->frame_reg], info->frame_offset * 16 );
677 for (i = 0; i < info->count; i++)
679 printf( " 0x%02x: ", info->opcodes[i].offset );
680 switch (info->opcodes[i].code)
682 case UWOP_PUSH_NONVOL:
683 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
684 break;
685 case UWOP_ALLOC_LARGE:
686 if (info->opcodes[i].info)
688 count = *(const DWORD *)&info->opcodes[i+1];
689 i += 2;
691 else
693 count = *(const USHORT *)&info->opcodes[i+1] * 8;
694 i++;
696 printf( "sub $0x%x,%%rsp\n", count );
697 break;
698 case UWOP_ALLOC_SMALL:
699 count = (info->opcodes[i].info + 1) * 8;
700 printf( "sub $0x%x,%%rsp\n", count );
701 break;
702 case UWOP_SET_FPREG:
703 printf( "lea 0x%x(%%rsp),%s\n",
704 info->frame_offset * 16, reg_names[info->frame_reg] );
705 break;
706 case UWOP_SAVE_NONVOL:
707 count = *(const USHORT *)&info->opcodes[i+1] * 8;
708 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
709 i++;
710 break;
711 case UWOP_SAVE_NONVOL_FAR:
712 count = *(const DWORD *)&info->opcodes[i+1];
713 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
714 i += 2;
715 break;
716 case UWOP_SAVE_XMM128:
717 count = *(const USHORT *)&info->opcodes[i+1] * 16;
718 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
719 i++;
720 break;
721 case UWOP_SAVE_XMM128_FAR:
722 count = *(const DWORD *)&info->opcodes[i+1];
723 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
724 i += 2;
725 break;
726 case UWOP_PUSH_MACHFRAME:
727 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
728 break;
729 default:
730 printf( "*** unknown code %u\n", info->opcodes[i].code );
731 break;
735 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
736 if (info->flags & UNW_FLAG_CHAININFO)
738 printf( " -> function %08x-%08x\n",
739 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
740 return;
742 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
743 printf( " handler %08x data at %08x\n", handler_data->handler,
744 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
747 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
749 const struct unwind_info_armnt *info;
750 const struct unwind_info_ext_armnt *infoex;
751 const struct unwind_info_epilogue_armnt *infoepi;
752 unsigned int rva;
753 WORD i, count = 0, words = 0;
755 if (fnc->u.s.Flag)
757 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
758 WORD pf = 0, ef = 0, sc = 0;
760 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
761 (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
762 printf( " Flag %x\n", fnc->u.s.Flag );
763 printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
764 printf( " Ret %x\n", fnc->u.s.Ret );
765 printf( " H %x\n", fnc->u.s.H );
766 printf( " Reg %x\n", fnc->u.s.Reg );
767 printf( " R %x\n", fnc->u.s.R );
768 printf( " L %x\n", fnc->u.s.L );
769 printf( " C %x\n", fnc->u.s.C );
770 printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
772 if (fnc->u.s.StackAdjust >= 0x03f4)
774 pf = fnc->u.s.StackAdjust & 0x04;
775 ef = fnc->u.s.StackAdjust & 0x08;
778 if (!fnc->u.s.R && !pf)
780 if (fnc->u.s.Reg)
782 sprintf(intregs, "r4-r%u", fnc->u.s.Reg + 4);
783 sprintf(intregspop, "r4-r%u", fnc->u.s.Reg + 4);
785 else
787 strcpy(intregs, "r4");
788 strcpy(intregspop, "r4");
790 sc = fnc->u.s.Reg + 1;
791 if (fnc->u.s.C || fnc->u.s.L)
793 strcat(intregs, ", ");
794 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
795 strcat(intregspop, ", ");
798 else if (fnc->u.s.R && pf)
800 if (((~fnc->u.s.StackAdjust) & 3) != 3)
802 sprintf(intregs, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
803 sprintf(intregspop, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
805 else
807 sprintf(intregs, "r3");
808 sprintf(intregspop, "r3");
810 sc = 4 - ((~fnc->u.s.StackAdjust) & 3);
811 if (fnc->u.s.C || fnc->u.s.L)
813 strcat(intregs, ", ");
814 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
815 strcat(intregspop, ", ");
818 else if (!fnc->u.s.R && pf)
820 sprintf(intregs, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
821 sprintf(intregspop, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
822 sc = fnc->u.s.Reg + 5 - ((~fnc->u.s.StackAdjust) & 3);
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.C && !fnc->u.s.L)
834 strcpy(intregs, "none");
835 strcpy(intregspop, "none");
839 if (fnc->u.s.C && !fnc->u.s.L)
841 strcat(intregs, "r11");
842 strcat(intregspop, "r11");
844 else if (fnc->u.s.C && fnc->u.s.L)
846 strcat(intregs, "r11, lr");
847 if (fnc->u.s.H)
848 strcat(intregspop, "r11");
849 else
850 strcat(intregspop, "r11, pc");
852 else if (!fnc->u.s.C && fnc->u.s.L)
854 strcat(intregs, "lr");
855 if (!fnc->u.s.H)
856 strcat(intregspop, "pc");
859 if (fnc->u.s.R)
861 if (fnc->u.s.Reg)
862 sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
863 else
864 strcpy(vfpregs, "d8");
866 else
867 strcpy(vfpregs, "none");
869 if (fnc->u.s.H)
870 printf( " Unwind Code\tpush {r0-r3}\n" );
872 if (fnc->u.s.R || fnc->u.s.L || fnc->u.s.C || pf)
873 printf( " Unwind Code\tpush {%s}\n", intregs );
875 if (fnc->u.s.C && fnc->u.s.R && !fnc->u.s.L && !pf)
876 printf( " Unwind Code\tmov r11, sp\n" );
877 else if (fnc->u.s.C && (!fnc->u.s.R || fnc->u.s.L || pf))
879 if (fnc->u.s.StackAdjust >= 0x03f4 && !sc)
880 printf( " Unwind Code\tadd r11, sp, #<unknown>\n");
881 else if (fnc->u.s.StackAdjust >= 0x03f4)
882 printf( " Unwind Code\tadd r11, sp, #%d\n", sc * 4 );
883 else
884 printf( " Unwind Code\tadd r11, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
887 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
888 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
890 if (fnc->u.s.StackAdjust < 0x03f4 && !pf)
891 printf( " Unwind Code\tsub sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
894 if (fnc->u.s.StackAdjust < 0x03f4 && !ef)
895 printf( " Unwind Code\tadd sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
897 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
898 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
900 if (fnc->u.s.C || !fnc->u.s.R || ef || (fnc->u.s.L && !fnc->u.s.H))
901 printf( " Unwind Code\tpop {%s}\n", intregspop );
903 if (fnc->u.s.H && !fnc->u.s.L)
904 printf( " Unwind Code\tadd sp, sp, #16\n" );
905 else if (fnc->u.s.H && fnc->u.s.L)
906 printf( " Unwind Code\tldr pc, [sp], #20\n" );
908 if (fnc->u.s.Ret == 1)
909 printf( " Unwind Code\tbx <reg>\n" );
910 else if (fnc->u.s.Ret == 2)
911 printf( " Unwind Code\tb <address>\n" );
913 return;
916 info = RVA( fnc->u.UnwindData, sizeof(*info) );
917 rva = fnc->u.UnwindData + sizeof(*info);
918 count = info->count;
919 words = info->words;
921 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
922 (fnc->BeginAddress & ~1) + info->function_length * 2 );
923 printf( " unwind info at %08x\n", fnc->u.UnwindData );
924 printf( " Flag %x\n", fnc->u.s.Flag );
925 printf( " FunctionLength %x\n", info->function_length );
926 printf( " Version %x\n", info->version );
927 printf( " X %x\n", info->x );
928 printf( " E %x\n", info->e );
929 printf( " F %x\n", info->f );
930 printf( " Count %x\n", count );
931 printf( " Words %x\n", words );
933 if (!info->count && !info->words)
935 infoex = RVA( rva, sizeof(*infoex) );
936 rva = rva + sizeof(*infoex);
937 count = infoex->excount;
938 words = infoex->exwords;
939 printf( " ExtCount %x\n", count );
940 printf( " ExtWords %x\n", words );
943 if (!info->e)
945 infoepi = RVA( rva, count * sizeof(*infoepi) );
946 rva = rva + count * sizeof(*infoepi);
948 for (i = 0; i < count; i++)
950 printf( " Epilogue Scope %x\n", i );
951 printf( " Offset %x\n", infoepi[i].offset );
952 printf( " Reserved %x\n", infoepi[i].res );
953 printf( " Condition %x\n", infoepi[i].cond );
954 printf( " Index %x\n", infoepi[i].index );
957 else
958 infoepi = NULL;
960 if (words)
962 const unsigned int *codes;
963 BYTE b, *bytes;
964 BOOL inepilogue = FALSE;
966 codes = RVA( rva, words * sizeof(*codes) );
967 rva = rva + words * sizeof(*codes);
968 bytes = (BYTE*)codes;
970 for (b = 0; b < words * sizeof(*codes); b++)
972 BYTE code = bytes[b];
974 if (info->e && b == count)
976 printf( "Epilogue:\n" );
977 inepilogue = TRUE;
979 else if (!info->e && infoepi)
981 for (i = 0; i < count; i++)
982 if (b == infoepi[i].index)
984 printf( "Epilogue from Scope %x at %08x:\n", i,
985 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
986 inepilogue = TRUE;
990 printf( " Unwind Code %x\t", code );
992 if (code == 0x00)
993 printf( "\n" );
994 else if (code <= 0x7f)
995 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
996 else if (code <= 0xbf)
998 WORD excode, f;
999 BOOL first = TRUE;
1000 BYTE excodes = bytes[++b];
1002 excode = (code << 8) | excodes;
1003 printf( "%s {", inepilogue ? "pop" : "push" );
1005 for (f = 0; f <= 12; f++)
1007 if ((excode >> f) & 1)
1009 printf( "%sr%u", first ? "" : ", ", f );
1010 first = FALSE;
1014 if (excode & 0x2000)
1015 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1017 printf( "}\n" );
1019 else if (code <= 0xcf)
1020 if (inepilogue)
1021 printf( "mov sp, r%u\n", code & 0x0f );
1022 else
1023 printf( "mov r%u, sp\n", code & 0x0f );
1024 else if (code <= 0xd7)
1025 if (inepilogue)
1026 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1027 else
1028 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1029 else if (code <= 0xdf)
1030 if (inepilogue)
1031 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1032 else
1033 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1034 else if (code <= 0xe7)
1035 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1036 else if (code <= 0xeb)
1038 WORD excode;
1039 BYTE excodes = bytes[++b];
1041 excode = (code << 8) | excodes;
1042 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1044 else if (code <= 0xed)
1046 WORD excode, f;
1047 BOOL first = TRUE;
1048 BYTE excodes = bytes[++b];
1050 excode = (code << 8) | excodes;
1051 printf( "%s {", inepilogue ? "pop" : "push" );
1053 for (f = 0; f < 8; f++)
1055 if ((excode >> f) & 1)
1057 printf( "%sr%u", first ? "" : ", ", f );
1058 first = FALSE;
1062 if (excode & 0x0100)
1063 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1065 printf( "}\n" );
1067 else if (code == 0xee)
1068 printf( "unknown 16\n" );
1069 else if (code == 0xef)
1071 WORD excode;
1072 BYTE excodes = bytes[++b];
1074 if (excodes <= 0x0f)
1076 excode = (code << 8) | excodes;
1077 if (inepilogue)
1078 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1079 else
1080 printf( "unknown 32\n" );
1082 else
1083 printf( "unknown 32\n" );
1085 else if (code <= 0xf4)
1086 printf( "unknown\n" );
1087 else if (code <= 0xf6)
1089 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1090 BYTE excodes = bytes[++b];
1092 excode = (code << 8) | excodes;
1093 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1094 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1096 else if (code <= 0xf7)
1098 unsigned int excode;
1099 BYTE excodes[2];
1101 excodes[0] = bytes[++b];
1102 excodes[1] = bytes[++b];
1103 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1104 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1106 else if (code <= 0xf8)
1108 unsigned int excode;
1109 BYTE excodes[3];
1111 excodes[0] = bytes[++b];
1112 excodes[1] = bytes[++b];
1113 excodes[2] = bytes[++b];
1114 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1115 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1117 else if (code <= 0xf9)
1119 unsigned int excode;
1120 BYTE excodes[2];
1122 excodes[0] = bytes[++b];
1123 excodes[1] = bytes[++b];
1124 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1125 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1127 else if (code <= 0xfa)
1129 unsigned int excode;
1130 BYTE excodes[3];
1132 excodes[0] = bytes[++b];
1133 excodes[1] = bytes[++b];
1134 excodes[2] = bytes[++b];
1135 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1136 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1138 else if (code <= 0xfc)
1139 printf( "nop\n" );
1140 else if (code <= 0xfe)
1142 printf( "(end) nop\n" );
1143 inepilogue = TRUE;
1145 else
1147 printf( "end\n" );
1148 inepilogue = TRUE;
1153 if (info->x)
1155 const unsigned int *handler;
1157 handler = RVA( rva, sizeof(*handler) );
1158 rva = rva + sizeof(*handler);
1160 printf( " handler %08x data at %08x\n", *handler, rva);
1164 static void dump_dir_exceptions(void)
1166 unsigned int i, size = 0;
1167 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1168 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1170 if (!funcs) return;
1172 if (file_header->Machine == IMAGE_FILE_MACHINE_AMD64)
1174 size /= sizeof(struct runtime_function_x86_64);
1175 printf( "Exception info (%u functions):\n", size );
1176 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1178 else if (file_header->Machine == IMAGE_FILE_MACHINE_ARMNT)
1180 size /= sizeof(struct runtime_function_armnt);
1181 printf( "Exception info (%u functions):\n", size );
1182 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1184 else printf( "Exception information not supported for %s binaries\n",
1185 get_machine_str(file_header->Machine));
1189 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il)
1191 /* FIXME: This does not properly handle large images */
1192 const IMAGE_IMPORT_BY_NAME* iibn;
1193 for (; il->u1.Ordinal; il++)
1195 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1196 printf(" %4u <by ordinal>\n", (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
1197 else
1199 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1200 if (!iibn)
1201 printf("Can't grab import by name info, skipping to next ordinal\n");
1202 else
1203 printf(" %4u %s %x\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
1208 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset)
1210 const IMAGE_IMPORT_BY_NAME* iibn;
1211 for (; il->u1.Ordinal; il++)
1213 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1214 printf(" %4u <by ordinal>\n", IMAGE_ORDINAL32(il->u1.Ordinal));
1215 else
1217 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1218 if (!iibn)
1219 printf("Can't grab import by name info, skipping to next ordinal\n");
1220 else
1221 printf(" %4u %s %x\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
1226 static void dump_dir_imported_functions(void)
1228 unsigned directorySize;
1229 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1231 if (!importDesc) return;
1233 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1235 for (;;)
1237 const IMAGE_THUNK_DATA32* il;
1239 if (!importDesc->Name || !importDesc->FirstThunk) break;
1241 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1242 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
1243 printf(" TimeDateStamp: %08X (%s)\n",
1244 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1245 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
1246 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
1248 printf(" Ordn Name\n");
1250 il = (importDesc->u.OriginalFirstThunk != 0) ?
1251 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
1252 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1254 if (!il)
1255 printf("Can't grab thunk data, going to next imported DLL\n");
1256 else
1258 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1259 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il);
1260 else
1261 dump_image_thunk_data32(il, 0);
1262 printf("\n");
1264 importDesc++;
1266 printf("\n");
1269 static void dump_dir_loadconfig(void)
1271 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1272 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1274 if (!loadcfg32) return;
1276 printf( "Loadconfig\n" );
1277 print_dword( "Size", loadcfg32->Size );
1278 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1279 print_word( "MajorVersion", loadcfg32->MajorVersion );
1280 print_word( "MinorVersion", loadcfg32->MinorVersion );
1281 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1282 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1283 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1285 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1287 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1288 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1289 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1290 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1291 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1292 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1293 print_word( "CSDVersion", loadcfg64->CSDVersion );
1294 print_word( "Reserved", loadcfg64->Reserved1 );
1295 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1296 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1297 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1299 else
1301 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1302 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1303 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1304 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1305 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1306 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1307 print_word( "CSDVersion", loadcfg32->CSDVersion );
1308 print_word( "Reserved", loadcfg32->Reserved1 );
1309 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1310 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1311 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1315 static void dump_dir_delay_imported_functions(void)
1317 unsigned directorySize;
1318 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1320 if (!importDesc) return;
1322 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1324 for (;;)
1326 const IMAGE_THUNK_DATA32* il;
1327 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1329 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1331 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1332 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1333 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1334 printf(" TimeDateStamp: %08X (%s)\n",
1335 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1337 printf(" Ordn Name\n");
1339 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1341 if (!il)
1342 printf("Can't grab thunk data, going to next imported DLL\n");
1343 else
1345 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1346 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il);
1347 else
1348 dump_image_thunk_data32(il, offset);
1349 printf("\n");
1351 importDesc++;
1353 printf("\n");
1356 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1358 const char* str;
1360 printf("Directory %02u\n", idx + 1);
1361 printf(" Characteristics: %08X\n", idd->Characteristics);
1362 printf(" TimeDateStamp: %08X %s\n",
1363 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1364 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1365 switch (idd->Type)
1367 default:
1368 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1369 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1370 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1371 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1372 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1373 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1374 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1375 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1376 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1377 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1378 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1379 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1380 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1381 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1382 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1383 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1385 printf(" Type: %u (%s)\n", idd->Type, str);
1386 printf(" SizeOfData: %u\n", idd->SizeOfData);
1387 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1388 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1390 switch (idd->Type)
1392 case IMAGE_DEBUG_TYPE_UNKNOWN:
1393 break;
1394 case IMAGE_DEBUG_TYPE_COFF:
1395 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1396 IMAGE_FIRST_SECTION(PE_nt_headers));
1397 break;
1398 case IMAGE_DEBUG_TYPE_CODEVIEW:
1399 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1400 break;
1401 case IMAGE_DEBUG_TYPE_FPO:
1402 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1403 break;
1404 case IMAGE_DEBUG_TYPE_MISC:
1406 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1407 if (!misc) {printf("Can't get misc debug information\n"); break;}
1408 printf(" DataType: %u (%s)\n",
1409 misc->DataType,
1410 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1411 printf(" Length: %u\n", misc->Length);
1412 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1413 printf(" Data: %s\n", misc->Data);
1415 break;
1416 default: break;
1418 printf("\n");
1421 static void dump_dir_debug(void)
1423 unsigned nb_dbg, i;
1424 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1426 nb_dbg /= sizeof(*debugDir);
1427 if (!debugDir || !nb_dbg) return;
1429 printf("Debug Table (%u directories)\n", nb_dbg);
1431 for (i = 0; i < nb_dbg; i++)
1433 dump_dir_debug_dir(debugDir, i);
1434 debugDir++;
1436 printf("\n");
1439 static inline void print_clrflags(const char *title, DWORD value)
1441 printf(" %-34s 0x%X\n", title, value);
1442 #define X(f,s) if (value & f) printf(" %s\n", s)
1443 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1444 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1445 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1446 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1447 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1448 #undef X
1451 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1453 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1456 static void dump_dir_clr_header(void)
1458 unsigned int size = 0;
1459 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1461 if (!dir) return;
1463 printf( "CLR Header\n" );
1464 print_dword( "Header Size", dir->cb );
1465 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1466 print_clrflags( "Flags", dir->Flags );
1467 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1468 printf("\n");
1469 printf( "CLR Data Directory\n" );
1470 print_clrdirectory( "MetaData", &dir->MetaData );
1471 print_clrdirectory( "Resources", &dir->Resources );
1472 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1473 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1474 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1475 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1476 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1477 printf("\n");
1480 static void dump_dir_reloc(void)
1482 unsigned int i, size = 0;
1483 const USHORT *relocs;
1484 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1485 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1486 static const char * const names[] =
1488 "BASED_ABSOLUTE",
1489 "BASED_HIGH",
1490 "BASED_LOW",
1491 "BASED_HIGHLOW",
1492 "BASED_HIGHADJ",
1493 "BASED_MIPS_JMPADDR",
1494 "BASED_SECTION",
1495 "BASED_REL",
1496 "unknown 8",
1497 "BASED_IA64_IMM64",
1498 "BASED_DIR64",
1499 "BASED_HIGH3ADJ",
1500 "unknown 12",
1501 "unknown 13",
1502 "unknown 14",
1503 "unknown 15"
1506 if (!rel) return;
1508 printf( "Relocations\n" );
1509 while (rel < end - 1 && rel->SizeOfBlock)
1511 printf( " Page %x\n", rel->VirtualAddress );
1512 relocs = (const USHORT *)(rel + 1);
1513 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1514 while (i--)
1516 USHORT offset = *relocs & 0xfff;
1517 int type = *relocs >> 12;
1518 printf( " off %04x type %s\n", offset, names[type] );
1519 relocs++;
1521 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1523 printf("\n");
1526 static void dump_dir_tls(void)
1528 IMAGE_TLS_DIRECTORY64 dir;
1529 const DWORD *callbacks;
1530 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1532 if (!pdir) return;
1534 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1535 memcpy(&dir, pdir, sizeof(dir));
1536 else
1538 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1539 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1540 dir.AddressOfIndex = pdir->AddressOfIndex;
1541 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1542 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1543 dir.Characteristics = pdir->Characteristics;
1546 /* FIXME: This does not properly handle large images */
1547 printf( "Thread Local Storage\n" );
1548 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1549 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1550 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1551 (DWORD)dir.SizeOfZeroFill );
1552 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1553 printf( " Characteristics %08x\n", dir.Characteristics );
1554 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1555 if (dir.AddressOfCallBacks)
1557 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1558 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1560 printf( " %08x", *callbacks );
1561 addr += sizeof(DWORD);
1564 printf(" }\n\n");
1567 enum FileSig get_kind_dbg(void)
1569 const WORD* pw;
1571 pw = PRD(0, sizeof(WORD));
1572 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1574 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1575 return SIG_UNKNOWN;
1578 void dbg_dump(void)
1580 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1581 unsigned nb_dbg;
1582 unsigned i;
1583 const IMAGE_DEBUG_DIRECTORY* debugDir;
1585 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1586 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1588 printf ("Signature: %.2s (0x%4X)\n",
1589 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1590 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1591 printf ("Machine: 0x%04X (%s)\n",
1592 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1593 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1594 printf ("TimeDateStamp: 0x%08X (%s)\n",
1595 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1596 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1597 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1598 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1599 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1600 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1601 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1603 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1604 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1605 {printf("Can't get the sections, aborting\n"); return;}
1607 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1609 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1610 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1611 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1612 separateDebugHead->ExportedNamesSize,
1613 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1614 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1616 printf("Debug Table (%u directories)\n", nb_dbg);
1618 for (i = 0; i < nb_dbg; i++)
1620 dump_dir_debug_dir(debugDir, i);
1621 debugDir++;
1625 static const char *get_resource_type( unsigned int id )
1627 static const char * const types[] =
1629 NULL,
1630 "CURSOR",
1631 "BITMAP",
1632 "ICON",
1633 "MENU",
1634 "DIALOG",
1635 "STRING",
1636 "FONTDIR",
1637 "FONT",
1638 "ACCELERATOR",
1639 "RCDATA",
1640 "MESSAGETABLE",
1641 "GROUP_CURSOR",
1642 NULL,
1643 "GROUP_ICON",
1644 NULL,
1645 "VERSION",
1646 "DLGINCLUDE",
1647 NULL,
1648 "PLUGPLAY",
1649 "VXD",
1650 "ANICURSOR",
1651 "ANIICON",
1652 "HTML",
1653 "RT_MANIFEST"
1656 if ((size_t)id < sizeof(types)/sizeof(types[0])) return types[id];
1657 return NULL;
1660 /* dump an ASCII string with proper escaping */
1661 static int dump_strA( const unsigned char *str, size_t len )
1663 static const char escapes[32] = ".......abtnvfr.............e....";
1664 char buffer[256];
1665 char *pos = buffer;
1666 int count = 0;
1668 for (; len; str++, len--)
1670 if (pos > buffer + sizeof(buffer) - 8)
1672 fwrite( buffer, pos - buffer, 1, stdout );
1673 count += pos - buffer;
1674 pos = buffer;
1676 if (*str > 127) /* hex escape */
1678 pos += sprintf( pos, "\\x%02x", *str );
1679 continue;
1681 if (*str < 32) /* octal or C escape */
1683 if (!*str && len == 1) continue; /* do not output terminating NULL */
1684 if (escapes[*str] != '.')
1685 pos += sprintf( pos, "\\%c", escapes[*str] );
1686 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1687 pos += sprintf( pos, "\\%03o", *str );
1688 else
1689 pos += sprintf( pos, "\\%o", *str );
1690 continue;
1692 if (*str == '\\') *pos++ = '\\';
1693 *pos++ = *str;
1695 fwrite( buffer, pos - buffer, 1, stdout );
1696 count += pos - buffer;
1697 return count;
1700 /* dump a Unicode string with proper escaping */
1701 static int dump_strW( const WCHAR *str, size_t len )
1703 static const char escapes[32] = ".......abtnvfr.............e....";
1704 char buffer[256];
1705 char *pos = buffer;
1706 int count = 0;
1708 for (; len; str++, len--)
1710 if (pos > buffer + sizeof(buffer) - 8)
1712 fwrite( buffer, pos - buffer, 1, stdout );
1713 count += pos - buffer;
1714 pos = buffer;
1716 if (*str > 127) /* hex escape */
1718 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
1719 pos += sprintf( pos, "\\x%04x", *str );
1720 else
1721 pos += sprintf( pos, "\\x%x", *str );
1722 continue;
1724 if (*str < 32) /* octal or C escape */
1726 if (!*str && len == 1) continue; /* do not output terminating NULL */
1727 if (escapes[*str] != '.')
1728 pos += sprintf( pos, "\\%c", escapes[*str] );
1729 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1730 pos += sprintf( pos, "\\%03o", *str );
1731 else
1732 pos += sprintf( pos, "\\%o", *str );
1733 continue;
1735 if (*str == '\\') *pos++ = '\\';
1736 *pos++ = *str;
1738 fwrite( buffer, pos - buffer, 1, stdout );
1739 count += pos - buffer;
1740 return count;
1743 /* dump data for a STRING resource */
1744 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
1746 int i;
1748 for (i = 0; i < 16 && size; i++)
1750 unsigned len = *ptr++;
1752 if (len >= size)
1754 len = size;
1755 size = 0;
1757 else size -= len + 1;
1759 if (len)
1761 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
1762 dump_strW( ptr, len );
1763 printf( "\"\n" );
1764 ptr += len;
1769 /* dump data for a MESSAGETABLE resource */
1770 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
1772 const MESSAGE_RESOURCE_DATA *data = ptr;
1773 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
1774 unsigned i, j;
1776 for (i = 0; i < data->NumberOfBlocks; i++, block++)
1778 const MESSAGE_RESOURCE_ENTRY *entry;
1780 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
1781 for (j = block->LowId; j <= block->HighId; j++)
1783 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
1785 const WCHAR *str = (const WCHAR *)entry->Text;
1786 printf( "%s%08x L\"", prefix, j );
1787 dump_strW( str, strlenW(str) );
1788 printf( "\"\n" );
1790 else
1792 const char *str = (const char *) entry->Text;
1793 printf( "%s%08x \"", prefix, j );
1794 dump_strA( entry->Text, strlen(str) );
1795 printf( "\"\n" );
1797 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
1802 static void dump_dir_resource(void)
1804 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
1805 const IMAGE_RESOURCE_DIRECTORY *namedir;
1806 const IMAGE_RESOURCE_DIRECTORY *langdir;
1807 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1808 const IMAGE_RESOURCE_DIR_STRING_U *string;
1809 const IMAGE_RESOURCE_DATA_ENTRY *data;
1810 int i, j, k;
1812 if (!root) return;
1814 printf( "Resources:" );
1816 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1818 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1819 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
1820 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1822 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1823 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
1824 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1826 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1828 printf( "\n " );
1829 if (e1->u.s.NameIsString)
1831 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
1832 dump_unicode_str( string->NameString, string->Length );
1834 else
1836 const char *type = get_resource_type( e1->u.Id );
1837 if (type) printf( "%s", type );
1838 else printf( "%04x", e1->u.Id );
1841 printf( " Name=" );
1842 if (e2->u.s.NameIsString)
1844 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
1845 dump_unicode_str( string->NameString, string->Length );
1847 else
1848 printf( "%04x", e2->u.Id );
1850 printf( " Language=%04x:\n", e3->u.Id );
1851 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1852 if (e1->u.s.NameIsString)
1854 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1856 else switch(e1->u.Id)
1858 case 6:
1859 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
1860 break;
1861 case 11:
1862 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
1863 e2->u.Id, " " );
1864 break;
1865 default:
1866 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1867 break;
1872 printf( "\n\n" );
1875 static void dump_debug(void)
1877 const char* stabs = NULL;
1878 unsigned szstabs = 0;
1879 const char* stabstr = NULL;
1880 unsigned szstr = 0;
1881 unsigned i;
1882 const IMAGE_SECTION_HEADER* sectHead;
1884 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
1886 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
1888 if (!strcmp((const char *)sectHead->Name, ".stab"))
1890 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1891 szstabs = sectHead->Misc.VirtualSize;
1893 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
1895 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1896 szstr = sectHead->Misc.VirtualSize;
1899 if (stabs && stabstr)
1900 dump_stabs(stabs, szstabs, stabstr, szstr);
1903 static void dump_symbol_table(void)
1905 const IMAGE_SYMBOL* sym;
1906 int numsym;
1908 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
1909 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
1910 return;
1911 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
1912 sizeof(*sym) * numsym);
1913 if (!sym) return;
1915 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
1918 enum FileSig get_kind_exec(void)
1920 const WORD* pw;
1921 const DWORD* pdw;
1922 const IMAGE_DOS_HEADER* dh;
1924 pw = PRD(0, sizeof(WORD));
1925 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1927 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
1929 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
1931 /* the signature is the first DWORD */
1932 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
1933 if (pdw)
1935 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
1936 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
1937 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
1939 return SIG_DOS;
1941 return SIG_UNKNOWN;
1944 void pe_dump(void)
1946 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
1948 PE_nt_headers = get_nt_header();
1949 if (is_fake_dll()) printf( "*** This is a Wine fake DLL ***\n\n" );
1951 if (globals.do_dumpheader)
1953 dump_pe_header();
1954 /* FIXME: should check ptr */
1955 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
1956 PE_nt_headers->FileHeader.NumberOfSections);
1958 else if (!globals.dumpsect)
1960 /* show at least something here */
1961 dump_pe_header();
1964 if (globals.dumpsect)
1966 if (all || !strcmp(globals.dumpsect, "import"))
1968 dump_dir_imported_functions();
1969 dump_dir_delay_imported_functions();
1971 if (all || !strcmp(globals.dumpsect, "export"))
1972 dump_dir_exported_functions();
1973 if (all || !strcmp(globals.dumpsect, "debug"))
1974 dump_dir_debug();
1975 if (all || !strcmp(globals.dumpsect, "resource"))
1976 dump_dir_resource();
1977 if (all || !strcmp(globals.dumpsect, "tls"))
1978 dump_dir_tls();
1979 if (all || !strcmp(globals.dumpsect, "loadcfg"))
1980 dump_dir_loadconfig();
1981 if (all || !strcmp(globals.dumpsect, "clr"))
1982 dump_dir_clr_header();
1983 if (all || !strcmp(globals.dumpsect, "reloc"))
1984 dump_dir_reloc();
1985 if (all || !strcmp(globals.dumpsect, "except"))
1986 dump_dir_exceptions();
1988 if (globals.do_symbol_table)
1989 dump_symbol_table();
1990 if (globals.do_debug)
1991 dump_debug();
1994 typedef struct _dll_symbol {
1995 size_t ordinal;
1996 char *symbol;
1997 } dll_symbol;
1999 static dll_symbol *dll_symbols = NULL;
2000 static dll_symbol *dll_current_symbol = NULL;
2002 /* Compare symbols by ordinal for qsort */
2003 static int symbol_cmp(const void *left, const void *right)
2005 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2008 /*******************************************************************
2009 * dll_close
2011 * Free resources used by DLL
2013 /* FIXME: Not used yet
2014 static void dll_close (void)
2016 dll_symbol* ds;
2018 if (!dll_symbols) {
2019 fatal("No symbols");
2021 for (ds = dll_symbols; ds->symbol; ds++)
2022 free(ds->symbol);
2023 free (dll_symbols);
2024 dll_symbols = NULL;
2028 static void do_grab_sym( void )
2030 const IMAGE_EXPORT_DIRECTORY*exportDir;
2031 unsigned i, j;
2032 const DWORD* pName;
2033 const DWORD* pFunc;
2034 const WORD* pOrdl;
2035 const char* ptr;
2036 DWORD* map;
2038 PE_nt_headers = get_nt_header();
2039 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2041 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2042 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2043 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2044 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2045 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2046 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2048 /* dll_close(); */
2050 if (!(dll_symbols = malloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol))))
2051 fatal ("Out of memory");
2053 /* bit map of used funcs */
2054 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2055 if (!map) fatal("no memory");
2057 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2059 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2060 ptr = RVA(*pName++, sizeof(DWORD));
2061 if (!ptr) ptr = "cant_get_function";
2062 dll_symbols[j].symbol = strdup(ptr);
2063 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2064 assert(dll_symbols[j].symbol);
2067 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2069 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2071 char ordinal_text[256];
2072 /* Ordinal only entry */
2073 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%u",
2074 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2075 exportDir->Base + i);
2076 str_toupper(ordinal_text);
2077 dll_symbols[j].symbol = strdup(ordinal_text);
2078 assert(dll_symbols[j].symbol);
2079 dll_symbols[j].ordinal = exportDir->Base + i;
2080 j++;
2081 assert(j <= exportDir->NumberOfFunctions);
2084 free(map);
2086 if (NORMAL)
2087 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2088 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2090 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2092 dll_symbols[j].symbol = NULL;
2094 dll_current_symbol = dll_symbols;
2097 /*******************************************************************
2098 * dll_open
2100 * Open a DLL and read in exported symbols
2102 BOOL dll_open (const char *dll_name)
2104 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2107 /*******************************************************************
2108 * dll_next_symbol
2110 * Get next exported symbol from dll
2112 BOOL dll_next_symbol (parsed_symbol * sym)
2114 if (!dll_current_symbol || !dll_current_symbol->symbol)
2115 return FALSE;
2116 assert (dll_symbols);
2117 sym->symbol = strdup (dll_current_symbol->symbol);
2118 sym->ordinal = dll_current_symbol->ordinal;
2119 dll_current_symbol++;
2120 return TRUE;