winedump: Add ARM64 string.
[wine/multimedia.git] / tools / winedump / pe.c
blob51ceba4ff6941c746d3ed2a10b29bb97a2a33ccd
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_delay_imported_functions(void)
1271 unsigned directorySize;
1272 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1274 if (!importDesc) return;
1276 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1278 for (;;)
1280 const IMAGE_THUNK_DATA32* il;
1281 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1283 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1285 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1286 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1287 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1288 printf(" TimeDateStamp: %08X (%s)\n",
1289 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1291 printf(" Ordn Name\n");
1293 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1295 if (!il)
1296 printf("Can't grab thunk data, going to next imported DLL\n");
1297 else
1299 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1300 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il);
1301 else
1302 dump_image_thunk_data32(il, offset);
1303 printf("\n");
1305 importDesc++;
1307 printf("\n");
1310 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1312 const char* str;
1314 printf("Directory %02u\n", idx + 1);
1315 printf(" Characteristics: %08X\n", idd->Characteristics);
1316 printf(" TimeDateStamp: %08X %s\n",
1317 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1318 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1319 switch (idd->Type)
1321 default:
1322 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1323 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1324 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1325 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1326 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1327 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1328 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1329 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1330 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1331 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1332 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1333 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1335 printf(" Type: %u (%s)\n", idd->Type, str);
1336 printf(" SizeOfData: %u\n", idd->SizeOfData);
1337 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1338 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1340 switch (idd->Type)
1342 case IMAGE_DEBUG_TYPE_UNKNOWN:
1343 break;
1344 case IMAGE_DEBUG_TYPE_COFF:
1345 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1346 IMAGE_FIRST_SECTION(PE_nt_headers));
1347 break;
1348 case IMAGE_DEBUG_TYPE_CODEVIEW:
1349 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1350 break;
1351 case IMAGE_DEBUG_TYPE_FPO:
1352 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1353 break;
1354 case IMAGE_DEBUG_TYPE_MISC:
1356 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1357 if (!misc) {printf("Can't get misc debug information\n"); break;}
1358 printf(" DataType: %u (%s)\n",
1359 misc->DataType,
1360 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1361 printf(" Length: %u\n", misc->Length);
1362 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1363 printf(" Data: %s\n", misc->Data);
1365 break;
1366 case IMAGE_DEBUG_TYPE_EXCEPTION:
1367 break;
1368 case IMAGE_DEBUG_TYPE_FIXUP:
1369 break;
1370 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC:
1371 break;
1372 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:
1373 break;
1374 case IMAGE_DEBUG_TYPE_BORLAND:
1375 break;
1376 case IMAGE_DEBUG_TYPE_RESERVED10:
1377 break;
1378 case IMAGE_DEBUG_TYPE_CLSID:
1379 break;
1381 printf("\n");
1384 static void dump_dir_debug(void)
1386 unsigned nb_dbg, i;
1387 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1389 nb_dbg /= sizeof(*debugDir);
1390 if (!debugDir || !nb_dbg) return;
1392 printf("Debug Table (%u directories)\n", nb_dbg);
1394 for (i = 0; i < nb_dbg; i++)
1396 dump_dir_debug_dir(debugDir, i);
1397 debugDir++;
1399 printf("\n");
1402 static inline void print_clrflags(const char *title, DWORD value)
1404 printf(" %-34s 0x%X\n", title, value);
1405 #define X(f,s) if (value & f) printf(" %s\n", s)
1406 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1407 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1408 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1409 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1410 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1411 #undef X
1414 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1416 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1419 static void dump_dir_clr_header(void)
1421 unsigned int size = 0;
1422 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1424 if (!dir) return;
1426 printf( "CLR Header\n" );
1427 print_dword( "Header Size", dir->cb );
1428 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1429 print_clrflags( "Flags", dir->Flags );
1430 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1431 printf("\n");
1432 printf( "CLR Data Directory\n" );
1433 print_clrdirectory( "MetaData", &dir->MetaData );
1434 print_clrdirectory( "Resources", &dir->Resources );
1435 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1436 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1437 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1438 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1439 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1440 printf("\n");
1443 static void dump_dir_reloc(void)
1445 unsigned int i, size = 0;
1446 const USHORT *relocs;
1447 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1448 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1449 static const char * const names[] =
1451 "BASED_ABSOLUTE",
1452 "BASED_HIGH",
1453 "BASED_LOW",
1454 "BASED_HIGHLOW",
1455 "BASED_HIGHADJ",
1456 "BASED_MIPS_JMPADDR",
1457 "BASED_SECTION",
1458 "BASED_REL",
1459 "unknown 8",
1460 "BASED_IA64_IMM64",
1461 "BASED_DIR64",
1462 "BASED_HIGH3ADJ",
1463 "unknown 12",
1464 "unknown 13",
1465 "unknown 14",
1466 "unknown 15"
1469 if (!rel) return;
1471 printf( "Relocations\n" );
1472 while (rel < end - 1 && rel->SizeOfBlock)
1474 printf( " Page %x\n", rel->VirtualAddress );
1475 relocs = (const USHORT *)(rel + 1);
1476 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1477 while (i--)
1479 USHORT offset = *relocs & 0xfff;
1480 int type = *relocs >> 12;
1481 printf( " off %04x type %s\n", offset, names[type] );
1482 relocs++;
1484 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1486 printf("\n");
1489 static void dump_dir_tls(void)
1491 IMAGE_TLS_DIRECTORY64 dir;
1492 const DWORD *callbacks;
1493 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1495 if (!pdir) return;
1497 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1498 memcpy(&dir, pdir, sizeof(dir));
1499 else
1501 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1502 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1503 dir.AddressOfIndex = pdir->AddressOfIndex;
1504 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1505 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1506 dir.Characteristics = pdir->Characteristics;
1509 /* FIXME: This does not properly handle large images */
1510 printf( "Thread Local Storage\n" );
1511 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1512 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1513 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1514 (DWORD)dir.SizeOfZeroFill );
1515 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1516 printf( " Characteristics %08x\n", dir.Characteristics );
1517 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1518 if (dir.AddressOfCallBacks)
1520 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1521 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1523 printf( " %08x", *callbacks );
1524 addr += sizeof(DWORD);
1527 printf(" }\n\n");
1530 enum FileSig get_kind_dbg(void)
1532 const WORD* pw;
1534 pw = PRD(0, sizeof(WORD));
1535 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1537 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1538 return SIG_UNKNOWN;
1541 void dbg_dump(void)
1543 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1544 unsigned nb_dbg;
1545 unsigned i;
1546 const IMAGE_DEBUG_DIRECTORY* debugDir;
1548 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1549 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1551 printf ("Signature: %.2s (0x%4X)\n",
1552 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1553 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1554 printf ("Machine: 0x%04X (%s)\n",
1555 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1556 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1557 printf ("TimeDateStamp: 0x%08X (%s)\n",
1558 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1559 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1560 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1561 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1562 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1563 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1564 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1566 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1567 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1568 {printf("Can't get the sections, aborting\n"); return;}
1570 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1572 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1573 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1574 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1575 separateDebugHead->ExportedNamesSize,
1576 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1577 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1579 printf("Debug Table (%u directories)\n", nb_dbg);
1581 for (i = 0; i < nb_dbg; i++)
1583 dump_dir_debug_dir(debugDir, i);
1584 debugDir++;
1588 static const char *get_resource_type( unsigned int id )
1590 static const char * const types[] =
1592 NULL,
1593 "CURSOR",
1594 "BITMAP",
1595 "ICON",
1596 "MENU",
1597 "DIALOG",
1598 "STRING",
1599 "FONTDIR",
1600 "FONT",
1601 "ACCELERATOR",
1602 "RCDATA",
1603 "MESSAGETABLE",
1604 "GROUP_CURSOR",
1605 NULL,
1606 "GROUP_ICON",
1607 NULL,
1608 "VERSION",
1609 "DLGINCLUDE",
1610 NULL,
1611 "PLUGPLAY",
1612 "VXD",
1613 "ANICURSOR",
1614 "ANIICON",
1615 "HTML",
1616 "RT_MANIFEST"
1619 if ((size_t)id < sizeof(types)/sizeof(types[0])) return types[id];
1620 return NULL;
1623 /* dump an ASCII string with proper escaping */
1624 static int dump_strA( const unsigned char *str, size_t len )
1626 static const char escapes[32] = ".......abtnvfr.............e....";
1627 char buffer[256];
1628 char *pos = buffer;
1629 int count = 0;
1631 for (; len; str++, len--)
1633 if (pos > buffer + sizeof(buffer) - 8)
1635 fwrite( buffer, pos - buffer, 1, stdout );
1636 count += pos - buffer;
1637 pos = buffer;
1639 if (*str > 127) /* hex escape */
1641 pos += sprintf( pos, "\\x%02x", *str );
1642 continue;
1644 if (*str < 32) /* octal or C escape */
1646 if (!*str && len == 1) continue; /* do not output terminating NULL */
1647 if (escapes[*str] != '.')
1648 pos += sprintf( pos, "\\%c", escapes[*str] );
1649 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1650 pos += sprintf( pos, "\\%03o", *str );
1651 else
1652 pos += sprintf( pos, "\\%o", *str );
1653 continue;
1655 if (*str == '\\') *pos++ = '\\';
1656 *pos++ = *str;
1658 fwrite( buffer, pos - buffer, 1, stdout );
1659 count += pos - buffer;
1660 return count;
1663 /* dump a Unicode string with proper escaping */
1664 static int dump_strW( const WCHAR *str, size_t len )
1666 static const char escapes[32] = ".......abtnvfr.............e....";
1667 char buffer[256];
1668 char *pos = buffer;
1669 int count = 0;
1671 for (; len; str++, len--)
1673 if (pos > buffer + sizeof(buffer) - 8)
1675 fwrite( buffer, pos - buffer, 1, stdout );
1676 count += pos - buffer;
1677 pos = buffer;
1679 if (*str > 127) /* hex escape */
1681 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
1682 pos += sprintf( pos, "\\x%04x", *str );
1683 else
1684 pos += sprintf( pos, "\\x%x", *str );
1685 continue;
1687 if (*str < 32) /* octal or C escape */
1689 if (!*str && len == 1) continue; /* do not output terminating NULL */
1690 if (escapes[*str] != '.')
1691 pos += sprintf( pos, "\\%c", escapes[*str] );
1692 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1693 pos += sprintf( pos, "\\%03o", *str );
1694 else
1695 pos += sprintf( pos, "\\%o", *str );
1696 continue;
1698 if (*str == '\\') *pos++ = '\\';
1699 *pos++ = *str;
1701 fwrite( buffer, pos - buffer, 1, stdout );
1702 count += pos - buffer;
1703 return count;
1706 /* dump data for a STRING resource */
1707 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
1709 int i;
1711 for (i = 0; i < 16 && size; i++)
1713 unsigned len = *ptr++;
1715 if (len >= size)
1717 len = size;
1718 size = 0;
1720 else size -= len + 1;
1722 if (len)
1724 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
1725 dump_strW( ptr, len );
1726 printf( "\"\n" );
1727 ptr += len;
1732 /* dump data for a MESSAGETABLE resource */
1733 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
1735 const MESSAGE_RESOURCE_DATA *data = ptr;
1736 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
1737 unsigned i, j;
1739 for (i = 0; i < data->NumberOfBlocks; i++, block++)
1741 const MESSAGE_RESOURCE_ENTRY *entry;
1743 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
1744 for (j = block->LowId; j <= block->HighId; j++)
1746 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
1748 const WCHAR *str = (const WCHAR *)entry->Text;
1749 printf( "%s%08x L\"", prefix, j );
1750 dump_strW( str, strlenW(str) );
1751 printf( "\"\n" );
1753 else
1755 const char *str = (const char *) entry->Text;
1756 printf( "%s%08x \"", prefix, j );
1757 dump_strA( entry->Text, strlen(str) );
1758 printf( "\"\n" );
1760 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
1765 static void dump_dir_resource(void)
1767 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
1768 const IMAGE_RESOURCE_DIRECTORY *namedir;
1769 const IMAGE_RESOURCE_DIRECTORY *langdir;
1770 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1771 const IMAGE_RESOURCE_DIR_STRING_U *string;
1772 const IMAGE_RESOURCE_DATA_ENTRY *data;
1773 int i, j, k;
1775 if (!root) return;
1777 printf( "Resources:" );
1779 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1781 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1782 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
1783 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1785 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1786 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
1787 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1789 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1791 printf( "\n " );
1792 if (e1->u.s.NameIsString)
1794 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
1795 dump_unicode_str( string->NameString, string->Length );
1797 else
1799 const char *type = get_resource_type( e1->u.Id );
1800 if (type) printf( "%s", type );
1801 else printf( "%04x", e1->u.Id );
1804 printf( " Name=" );
1805 if (e2->u.s.NameIsString)
1807 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
1808 dump_unicode_str( string->NameString, string->Length );
1810 else
1811 printf( "%04x", e2->u.Id );
1813 printf( " Language=%04x:\n", e3->u.Id );
1814 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1815 if (e1->u.s.NameIsString)
1817 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1819 else switch(e1->u.Id)
1821 case 6:
1822 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
1823 break;
1824 case 11:
1825 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
1826 e2->u.Id, " " );
1827 break;
1828 default:
1829 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1830 break;
1835 printf( "\n\n" );
1838 static void dump_debug(void)
1840 const char* stabs = NULL;
1841 unsigned szstabs = 0;
1842 const char* stabstr = NULL;
1843 unsigned szstr = 0;
1844 unsigned i;
1845 const IMAGE_SECTION_HEADER* sectHead;
1847 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
1849 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
1851 if (!strcmp((const char *)sectHead->Name, ".stab"))
1853 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1854 szstabs = sectHead->Misc.VirtualSize;
1856 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
1858 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1859 szstr = sectHead->Misc.VirtualSize;
1862 if (stabs && stabstr)
1863 dump_stabs(stabs, szstabs, stabstr, szstr);
1866 static void dump_symbol_table(void)
1868 const IMAGE_SYMBOL* sym;
1869 int numsym;
1871 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
1872 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
1873 return;
1874 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
1875 sizeof(*sym) * numsym);
1876 if (!sym) return;
1878 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
1881 enum FileSig get_kind_exec(void)
1883 const WORD* pw;
1884 const DWORD* pdw;
1885 const IMAGE_DOS_HEADER* dh;
1887 pw = PRD(0, sizeof(WORD));
1888 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1890 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
1892 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
1894 /* the signature is the first DWORD */
1895 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
1896 if (pdw)
1898 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
1899 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
1900 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
1902 return SIG_DOS;
1904 return SIG_UNKNOWN;
1907 void pe_dump(void)
1909 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
1911 PE_nt_headers = get_nt_header();
1912 if (is_fake_dll()) printf( "*** This is a Wine fake DLL ***\n\n" );
1914 if (globals.do_dumpheader)
1916 dump_pe_header();
1917 /* FIXME: should check ptr */
1918 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
1919 PE_nt_headers->FileHeader.NumberOfSections);
1921 else if (!globals.dumpsect)
1923 /* show at least something here */
1924 dump_pe_header();
1927 if (globals.dumpsect)
1929 if (all || !strcmp(globals.dumpsect, "import"))
1931 dump_dir_imported_functions();
1932 dump_dir_delay_imported_functions();
1934 if (all || !strcmp(globals.dumpsect, "export"))
1935 dump_dir_exported_functions();
1936 if (all || !strcmp(globals.dumpsect, "debug"))
1937 dump_dir_debug();
1938 if (all || !strcmp(globals.dumpsect, "resource"))
1939 dump_dir_resource();
1940 if (all || !strcmp(globals.dumpsect, "tls"))
1941 dump_dir_tls();
1942 if (all || !strcmp(globals.dumpsect, "clr"))
1943 dump_dir_clr_header();
1944 if (all || !strcmp(globals.dumpsect, "reloc"))
1945 dump_dir_reloc();
1946 if (all || !strcmp(globals.dumpsect, "except"))
1947 dump_dir_exceptions();
1949 if (globals.do_symbol_table)
1950 dump_symbol_table();
1951 if (globals.do_debug)
1952 dump_debug();
1955 typedef struct _dll_symbol {
1956 size_t ordinal;
1957 char *symbol;
1958 } dll_symbol;
1960 static dll_symbol *dll_symbols = NULL;
1961 static dll_symbol *dll_current_symbol = NULL;
1963 /* Compare symbols by ordinal for qsort */
1964 static int symbol_cmp(const void *left, const void *right)
1966 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
1969 /*******************************************************************
1970 * dll_close
1972 * Free resources used by DLL
1974 /* FIXME: Not used yet
1975 static void dll_close (void)
1977 dll_symbol* ds;
1979 if (!dll_symbols) {
1980 fatal("No symbols");
1982 for (ds = dll_symbols; ds->symbol; ds++)
1983 free(ds->symbol);
1984 free (dll_symbols);
1985 dll_symbols = NULL;
1989 static void do_grab_sym( void )
1991 const IMAGE_EXPORT_DIRECTORY*exportDir;
1992 unsigned i, j;
1993 const DWORD* pName;
1994 const DWORD* pFunc;
1995 const WORD* pOrdl;
1996 const char* ptr;
1997 DWORD* map;
1999 PE_nt_headers = get_nt_header();
2000 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2002 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2003 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2004 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2005 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2006 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2007 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2009 /* dll_close(); */
2011 if (!(dll_symbols = malloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol))))
2012 fatal ("Out of memory");
2014 /* bit map of used funcs */
2015 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2016 if (!map) fatal("no memory");
2018 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2020 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2021 ptr = RVA(*pName++, sizeof(DWORD));
2022 if (!ptr) ptr = "cant_get_function";
2023 dll_symbols[j].symbol = strdup(ptr);
2024 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2025 assert(dll_symbols[j].symbol);
2028 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2030 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2032 char ordinal_text[256];
2033 /* Ordinal only entry */
2034 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%u",
2035 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2036 exportDir->Base + i);
2037 str_toupper(ordinal_text);
2038 dll_symbols[j].symbol = strdup(ordinal_text);
2039 assert(dll_symbols[j].symbol);
2040 dll_symbols[j].ordinal = exportDir->Base + i;
2041 j++;
2042 assert(j <= exportDir->NumberOfFunctions);
2045 free(map);
2047 if (NORMAL)
2048 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2049 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2051 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2053 dll_symbols[j].symbol = NULL;
2055 dll_current_symbol = dll_symbols;
2058 /*******************************************************************
2059 * dll_open
2061 * Open a DLL and read in exported symbols
2063 BOOL dll_open (const char *dll_name)
2065 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2068 /*******************************************************************
2069 * dll_next_symbol
2071 * Get next exported symbol from dll
2073 BOOL dll_next_symbol (parsed_symbol * sym)
2075 if (!dll_current_symbol || !dll_current_symbol->symbol)
2076 return FALSE;
2077 assert (dll_symbols);
2078 sym->symbol = strdup (dll_current_symbol->symbol);
2079 sym->ordinal = dll_current_symbol->ordinal;
2080 dll_current_symbol++;
2081 return TRUE;