winedump: Fix compiler warnings.
[wine.git] / tools / winedump / pe.c
blobaedd98e11ffbb0b7276017a317176d348ce7f6ea
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_ARM: return "ARM";
65 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
66 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
68 return "???";
71 static const void* RVA(unsigned long rva, unsigned long len)
73 IMAGE_SECTION_HEADER* sectHead;
74 int i;
76 if (rva == 0) return NULL;
78 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
79 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
81 if (sectHead[i].VirtualAddress <= rva &&
82 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
84 /* return image import directory offset */
85 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
89 return NULL;
92 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
94 const IMAGE_DOS_HEADER *dos;
95 dos = PRD(0, sizeof(*dos));
96 if (!dos) return NULL;
97 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
100 static BOOL is_fake_dll( void )
102 static const char fakedll_signature[] = "Wine placeholder DLL";
103 const IMAGE_DOS_HEADER *dos;
105 dos = PRD(0, sizeof(*dos) + sizeof(fakedll_signature));
107 if (dos && dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
108 !memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return TRUE;
109 return FALSE;
112 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
114 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
116 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
117 if (idx >= opt->NumberOfRvaAndSizes)
118 return NULL;
119 if(size)
120 *size = opt->DataDirectory[idx].Size;
121 return RVA(opt->DataDirectory[idx].VirtualAddress,
122 opt->DataDirectory[idx].Size);
124 else
126 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
127 if (idx >= opt->NumberOfRvaAndSizes)
128 return NULL;
129 if(size)
130 *size = opt->DataDirectory[idx].Size;
131 return RVA(opt->DataDirectory[idx].VirtualAddress,
132 opt->DataDirectory[idx].Size);
136 static const void* get_dir(unsigned idx)
138 return get_dir_and_size(idx, 0);
141 static const char * const DirectoryNames[16] = {
142 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
143 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
144 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
145 "IAT", "Delay IAT", "CLR Header", ""
148 static const char *get_magic_type(WORD magic)
150 switch(magic) {
151 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
152 return "32bit";
153 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
154 return "64bit";
155 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
156 return "ROM";
158 return "???";
161 static inline void print_word(const char *title, WORD value)
163 printf(" %-34s 0x%-4X %u\n", title, value, value);
166 static inline void print_dword(const char *title, DWORD value)
168 printf(" %-34s 0x%-8x %u\n", title, value, value);
171 static inline void print_longlong(const char *title, ULONGLONG value)
173 printf(" %-34s 0x", title);
174 if(value >> 32)
175 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
176 else
177 printf("%lx\n", (unsigned long)value);
180 static inline void print_ver(const char *title, BYTE major, BYTE minor)
182 printf(" %-34s %u.%02u\n", title, major, minor);
185 static inline void print_subsys(const char *title, WORD value)
187 const char *str;
188 switch (value)
190 default:
191 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
192 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
193 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
194 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
195 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
196 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
197 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
198 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
199 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
200 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
201 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
202 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
203 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
204 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
206 printf(" %-34s 0x%X (%s)\n", title, value, str);
209 static inline void print_dllflags(const char *title, WORD value)
211 printf(" %-34s 0x%X\n", title, value);
212 #define X(f,s) if (value & f) printf(" %s\n", s)
213 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
214 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
215 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
216 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
217 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
218 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
219 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
220 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
221 #undef X
224 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
226 unsigned i;
227 printf("Data Directory\n");
229 for (i = 0; i < n && i < 16; i++)
231 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
232 DirectoryNames[i], directory[i].VirtualAddress,
233 directory[i].Size);
237 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
239 IMAGE_OPTIONAL_HEADER32 oh;
240 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
242 /* in case optional header is missing or partial */
243 memset(&oh, 0, sizeof(oh));
244 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
245 optionalHeader = &oh;
247 print_word("Magic", optionalHeader->Magic);
248 print_ver("linker version",
249 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
250 print_dword("size of code", optionalHeader->SizeOfCode);
251 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
252 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
253 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
254 print_dword("base of code", optionalHeader->BaseOfCode);
255 print_dword("base of data", optionalHeader->BaseOfData);
256 print_dword("image base", optionalHeader->ImageBase);
257 print_dword("section align", optionalHeader->SectionAlignment);
258 print_dword("file align", optionalHeader->FileAlignment);
259 print_ver("required OS version",
260 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
261 print_ver("image version",
262 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
263 print_ver("subsystem version",
264 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
265 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
266 print_dword("size of image", optionalHeader->SizeOfImage);
267 print_dword("size of headers", optionalHeader->SizeOfHeaders);
268 print_dword("checksum", optionalHeader->CheckSum);
269 print_subsys("Subsystem", optionalHeader->Subsystem);
270 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
271 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
272 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
273 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
274 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
275 print_dword("loader flags", optionalHeader->LoaderFlags);
276 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
277 printf("\n");
278 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
279 printf("\n");
282 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
284 IMAGE_OPTIONAL_HEADER64 oh;
285 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
287 /* in case optional header is missing or partial */
288 memset(&oh, 0, sizeof(oh));
289 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
290 optionalHeader = &oh;
292 print_word("Magic", optionalHeader->Magic);
293 print_ver("linker version",
294 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
295 print_dword("size of code", optionalHeader->SizeOfCode);
296 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
297 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
298 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
299 print_dword("base of code", optionalHeader->BaseOfCode);
300 print_longlong("image base", optionalHeader->ImageBase);
301 print_dword("section align", optionalHeader->SectionAlignment);
302 print_dword("file align", optionalHeader->FileAlignment);
303 print_ver("required OS version",
304 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
305 print_ver("image version",
306 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
307 print_ver("subsystem version",
308 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
309 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
310 print_dword("size of image", optionalHeader->SizeOfImage);
311 print_dword("size of headers", optionalHeader->SizeOfHeaders);
312 print_dword("checksum", optionalHeader->CheckSum);
313 print_subsys("Subsystem", optionalHeader->Subsystem);
314 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
315 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
316 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
317 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
318 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
319 print_dword("loader flags", optionalHeader->LoaderFlags);
320 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
321 printf("\n");
322 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
323 printf("\n");
326 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
328 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
330 switch(optionalHeader->Magic) {
331 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
332 dump_optional_header32(optionalHeader, header_size);
333 break;
334 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
335 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
336 break;
337 default:
338 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
339 break;
343 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
345 printf("File Header\n");
347 printf(" Machine: %04X (%s)\n",
348 fileHeader->Machine, get_machine_str(fileHeader->Machine));
349 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
350 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
351 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
352 Offset(&(fileHeader->TimeDateStamp)));
353 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
354 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
355 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
356 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
357 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
358 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
359 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
360 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
361 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
362 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
363 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
364 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
365 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
366 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
367 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
368 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
369 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
370 X(IMAGE_FILE_SYSTEM, "SYSTEM");
371 X(IMAGE_FILE_DLL, "DLL");
372 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
373 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
374 #undef X
375 printf("\n");
378 static void dump_pe_header(void)
380 dump_file_header(&PE_nt_headers->FileHeader);
381 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
384 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
386 unsigned offset;
388 /* long section name ? */
389 if (strtable && sectHead->Name[0] == '/' &&
390 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
391 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
392 else
393 printf(" %-8.8s", sectHead->Name);
394 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
395 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
396 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
397 sectHead->PointerToRawData, sectHead->SizeOfRawData);
398 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
399 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
400 printf(" line # offs: %-8u line #'s: %-8u\n",
401 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
402 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
403 printf(" ");
404 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
405 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
406 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
407 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
408 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
409 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
410 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
412 X(IMAGE_SCN_CNT_CODE, "CODE");
413 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
414 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
416 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
417 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
418 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
419 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
420 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
422 /* 0x00002000 - Reserved */
423 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
424 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
426 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
427 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
428 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
429 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
430 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
432 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
434 #define X2(b,s) case b: printf(" " s); break
435 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
436 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
437 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
438 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
439 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
440 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
441 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
442 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
443 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
444 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
445 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
446 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
447 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
448 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
449 #undef X2
452 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
454 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
455 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
456 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
457 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
458 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
459 X(IMAGE_SCN_MEM_READ, "MEM_READ");
460 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
461 #undef X
462 printf("\n\n");
465 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
467 const IMAGE_SECTION_HEADER* sectHead = addr;
468 unsigned i;
469 const char* strtable;
471 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
473 strtable = (const char*)base +
474 PE_nt_headers->FileHeader.PointerToSymbolTable +
475 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
477 else strtable = NULL;
479 printf("Section Table\n");
480 for (i = 0; i < num_sect; i++, sectHead++)
482 dump_section(sectHead, strtable);
484 if (globals.do_dump_rawdata)
486 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
487 printf("\n");
492 static void dump_dir_exported_functions(void)
494 unsigned int size = 0;
495 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
496 unsigned int i;
497 const DWORD* pFunc;
498 const DWORD* pName;
499 const WORD* pOrdl;
500 DWORD* funcs;
502 if (!exportDir) return;
504 printf("Exports table:\n");
505 printf("\n");
506 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
507 printf(" Characteristics: %08x\n", exportDir->Characteristics);
508 printf(" TimeDateStamp: %08X %s\n",
509 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
510 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
511 printf(" Ordinal base: %u\n", exportDir->Base);
512 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
513 printf(" # of Names: %u\n", exportDir->NumberOfNames);
514 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
515 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
516 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
517 printf("\n");
518 printf(" Entry Pt Ordn Name\n");
520 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
521 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
522 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
523 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
525 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
526 if (!funcs) fatal("no memory");
528 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
530 for (i = 0; i < exportDir->NumberOfFunctions; i++)
532 if (!pFunc[i]) continue;
533 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
534 if (funcs[i])
535 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
536 else
537 printf("<by ordinal>");
539 /* check for forwarded function */
540 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
541 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
542 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
543 printf("\n");
545 free(funcs);
546 printf("\n");
550 struct runtime_function_x86_64
552 DWORD BeginAddress;
553 DWORD EndAddress;
554 DWORD UnwindData;
557 struct runtime_function_armnt
559 DWORD BeginAddress;
560 union {
561 DWORD UnwindData;
562 struct {
563 DWORD Flag : 2;
564 DWORD FunctionLength : 11;
565 DWORD Ret : 2;
566 DWORD H : 1;
567 DWORD Reg : 3;
568 DWORD R : 1;
569 DWORD L : 1;
570 DWORD C : 1;
571 DWORD StackAdjust : 10;
572 } DUMMYSTRUCTNAME;
573 } DUMMYUNIONNAME;
576 union handler_data
578 struct runtime_function_x86_64 chain;
579 DWORD handler;
582 struct opcode
584 BYTE offset;
585 BYTE code : 4;
586 BYTE info : 4;
589 struct unwind_info_x86_64
591 BYTE version : 3;
592 BYTE flags : 5;
593 BYTE prolog;
594 BYTE count;
595 BYTE frame_reg : 4;
596 BYTE frame_offset : 4;
597 struct opcode opcodes[1]; /* count entries */
598 /* followed by union handler_data */
601 struct unwind_info_armnt
603 DWORD function_length : 18;
604 DWORD version : 2;
605 DWORD x : 1;
606 DWORD e : 1;
607 DWORD f : 1;
608 DWORD count : 5;
609 DWORD words : 4;
612 struct unwind_info_ext_armnt
614 WORD excount;
615 BYTE exwords;
616 BYTE reserved;
619 struct unwind_info_epilogue_armnt
621 DWORD offset : 18;
622 DWORD res : 2;
623 DWORD cond : 4;
624 DWORD index : 8;
627 #define UWOP_PUSH_NONVOL 0
628 #define UWOP_ALLOC_LARGE 1
629 #define UWOP_ALLOC_SMALL 2
630 #define UWOP_SET_FPREG 3
631 #define UWOP_SAVE_NONVOL 4
632 #define UWOP_SAVE_NONVOL_FAR 5
633 #define UWOP_SAVE_XMM128 8
634 #define UWOP_SAVE_XMM128_FAR 9
635 #define UWOP_PUSH_MACHFRAME 10
637 #define UNW_FLAG_EHANDLER 1
638 #define UNW_FLAG_UHANDLER 2
639 #define UNW_FLAG_CHAININFO 4
641 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
643 static const char * const reg_names[16] =
644 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
645 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
647 const union handler_data *handler_data;
648 const struct unwind_info_x86_64 *info;
649 unsigned int i, count;
651 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
652 if (function->UnwindData & 1)
654 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
655 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
656 return;
658 info = RVA( function->UnwindData, sizeof(*info) );
660 printf( " unwind info at %08x\n", function->UnwindData );
661 if (info->version != 1)
663 printf( " *** unknown version %u\n", info->version );
664 return;
666 printf( " flags %x", info->flags );
667 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
668 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
669 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
670 printf( "\n prolog 0x%x bytes\n", info->prolog );
672 if (info->frame_reg)
673 printf( " frame register %s offset 0x%x(%%rsp)\n",
674 reg_names[info->frame_reg], info->frame_offset * 16 );
676 for (i = 0; i < info->count; i++)
678 printf( " 0x%02x: ", info->opcodes[i].offset );
679 switch (info->opcodes[i].code)
681 case UWOP_PUSH_NONVOL:
682 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
683 break;
684 case UWOP_ALLOC_LARGE:
685 if (info->opcodes[i].info)
687 count = *(const DWORD *)&info->opcodes[i+1];
688 i += 2;
690 else
692 count = *(const USHORT *)&info->opcodes[i+1] * 8;
693 i++;
695 printf( "sub $0x%x,%%rsp\n", count );
696 break;
697 case UWOP_ALLOC_SMALL:
698 count = (info->opcodes[i].info + 1) * 8;
699 printf( "sub $0x%x,%%rsp\n", count );
700 break;
701 case UWOP_SET_FPREG:
702 printf( "lea 0x%x(%%rsp),%s\n",
703 info->frame_offset * 16, reg_names[info->frame_reg] );
704 break;
705 case UWOP_SAVE_NONVOL:
706 count = *(const USHORT *)&info->opcodes[i+1] * 8;
707 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
708 i++;
709 break;
710 case UWOP_SAVE_NONVOL_FAR:
711 count = *(const DWORD *)&info->opcodes[i+1];
712 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
713 i += 2;
714 break;
715 case UWOP_SAVE_XMM128:
716 count = *(const USHORT *)&info->opcodes[i+1] * 16;
717 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
718 i++;
719 break;
720 case UWOP_SAVE_XMM128_FAR:
721 count = *(const DWORD *)&info->opcodes[i+1];
722 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
723 i += 2;
724 break;
725 case UWOP_PUSH_MACHFRAME:
726 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
727 break;
728 default:
729 printf( "*** unknown code %u\n", info->opcodes[i].code );
730 break;
734 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
735 if (info->flags & UNW_FLAG_CHAININFO)
737 printf( " -> function %08x-%08x\n",
738 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
739 return;
741 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
742 printf( " handler %08x data at %08x\n", handler_data->handler,
743 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
746 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
748 const struct unwind_info_armnt *info;
749 const struct unwind_info_ext_armnt *infoex;
750 const struct unwind_info_epilogue_armnt *infoepi;
751 unsigned int rva;
752 WORD i, count = 0, words = 0;
754 if (fnc->u.s.Flag)
756 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
757 WORD pf = 0, ef = 0, sc = 0;
759 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
760 (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
761 printf( " Flag %x\n", fnc->u.s.Flag );
762 printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
763 printf( " Ret %x\n", fnc->u.s.Ret );
764 printf( " H %x\n", fnc->u.s.H );
765 printf( " Reg %x\n", fnc->u.s.Reg );
766 printf( " R %x\n", fnc->u.s.R );
767 printf( " L %x\n", fnc->u.s.L );
768 printf( " C %x\n", fnc->u.s.C );
769 printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
771 if (fnc->u.s.StackAdjust >= 0x03f4)
773 pf = fnc->u.s.StackAdjust & 0x04;
774 ef = fnc->u.s.StackAdjust & 0x08;
777 if (!fnc->u.s.R && !pf)
779 if (fnc->u.s.Reg)
781 sprintf(intregs, "r4-r%u", fnc->u.s.Reg + 4);
782 sprintf(intregspop, "r4-r%u", fnc->u.s.Reg + 4);
784 else
786 strcpy(intregs, "r4");
787 strcpy(intregspop, "r4");
789 sc = fnc->u.s.Reg + 1;
790 if (fnc->u.s.C || fnc->u.s.L)
792 strcat(intregs, ", ");
793 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
794 strcat(intregspop, ", ");
797 else if (fnc->u.s.R && pf)
799 if (((~fnc->u.s.StackAdjust) & 3) != 3)
801 sprintf(intregs, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
802 sprintf(intregspop, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
804 else
806 sprintf(intregs, "r3");
807 sprintf(intregspop, "r3");
809 sc = 4 - ((~fnc->u.s.StackAdjust) & 3);
810 if (fnc->u.s.C || fnc->u.s.L)
812 strcat(intregs, ", ");
813 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
814 strcat(intregspop, ", ");
817 else if (!fnc->u.s.R && pf)
819 sprintf(intregs, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
820 sprintf(intregspop, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
821 sc = fnc->u.s.Reg + 5 - ((~fnc->u.s.StackAdjust) & 3);
822 if (fnc->u.s.C || fnc->u.s.L)
824 strcat(intregs, ", ");
825 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
826 strcat(intregspop, ", ");
829 else if (fnc->u.s.R && !pf)
831 if (!fnc->u.s.C && !fnc->u.s.L)
833 strcpy(intregs, "none");
834 strcpy(intregspop, "none");
838 if (fnc->u.s.C && !fnc->u.s.L)
840 strcat(intregs, "r11");
841 strcat(intregspop, "r11");
843 else if (fnc->u.s.C && fnc->u.s.L)
845 strcat(intregs, "r11, lr");
846 if (fnc->u.s.H)
847 strcat(intregspop, "r11");
848 else
849 strcat(intregspop, "r11, pc");
851 else if (!fnc->u.s.C && fnc->u.s.L)
853 strcat(intregs, "lr");
854 if (!fnc->u.s.H)
855 strcat(intregspop, "pc");
858 if (fnc->u.s.R)
860 if (fnc->u.s.Reg)
861 sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
862 else
863 strcpy(vfpregs, "d8");
865 else
866 strcpy(vfpregs, "none");
868 if (fnc->u.s.H)
869 printf( " Unwind Code\tpush {r0-r3}\n" );
871 if (fnc->u.s.R || fnc->u.s.L || fnc->u.s.C || pf)
872 printf( " Unwind Code\tpush {%s}\n", intregs );
874 if (fnc->u.s.C && fnc->u.s.R && !fnc->u.s.L && !pf)
875 printf( " Unwind Code\tmov r11, sp\n" );
876 else if (fnc->u.s.C && (!fnc->u.s.R || fnc->u.s.L || pf))
878 if (fnc->u.s.StackAdjust >= 0x03f4 && !sc)
879 printf( " Unwind Code\tadd r11, sp, #<unknown>\n");
880 else if (fnc->u.s.StackAdjust >= 0x03f4)
881 printf( " Unwind Code\tadd r11, sp, #%d\n", sc * 4 );
882 else
883 printf( " Unwind Code\tadd r11, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
886 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
887 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
889 if (fnc->u.s.StackAdjust < 0x03f4 && !pf)
890 printf( " Unwind Code\tsub sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
893 if (fnc->u.s.StackAdjust < 0x03f4 && !ef)
894 printf( " Unwind Code\tadd sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
896 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
897 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
899 if (fnc->u.s.C || !fnc->u.s.R || ef || (fnc->u.s.L && !fnc->u.s.H))
900 printf( " Unwind Code\tpop {%s}\n", intregspop );
902 if (fnc->u.s.H && !fnc->u.s.L)
903 printf( " Unwind Code\tadd sp, sp, #16\n" );
904 else if (fnc->u.s.H && fnc->u.s.L)
905 printf( " Unwind Code\tldr pc, [sp], #20\n" );
907 if (fnc->u.s.Ret == 1)
908 printf( " Unwind Code\tbx <reg>\n" );
909 else if (fnc->u.s.Ret == 2)
910 printf( " Unwind Code\tb <address>\n" );
912 return;
915 info = RVA( fnc->u.UnwindData, sizeof(*info) );
916 rva = fnc->u.UnwindData + sizeof(*info);
917 count = info->count;
918 words = info->words;
920 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
921 (fnc->BeginAddress & ~1) + info->function_length * 2 );
922 printf( " unwind info at %08x\n", fnc->u.UnwindData );
923 printf( " Flag %x\n", fnc->u.s.Flag );
924 printf( " FunctionLength %x\n", info->function_length );
925 printf( " Version %x\n", info->version );
926 printf( " X %x\n", info->x );
927 printf( " E %x\n", info->e );
928 printf( " F %x\n", info->f );
929 printf( " Count %x\n", count );
930 printf( " Words %x\n", words );
932 if (!info->count && !info->words)
934 infoex = RVA( rva, sizeof(*infoex) );
935 rva = rva + sizeof(*infoex);
936 count = infoex->excount;
937 words = infoex->exwords;
938 printf( " ExtCount %x\n", count );
939 printf( " ExtWords %x\n", words );
942 if (!info->e)
944 infoepi = RVA( rva, count * sizeof(*infoepi) );
945 rva = rva + count * sizeof(*infoepi);
947 for (i = 0; i < count; i++)
949 printf( " Epilogue Scope %x\n", i );
950 printf( " Offset %x\n", infoepi[i].offset );
951 printf( " Reserved %x\n", infoepi[i].res );
952 printf( " Condition %x\n", infoepi[i].cond );
953 printf( " Index %x\n", infoepi[i].index );
956 else
957 infoepi = NULL;
959 if (words)
961 const unsigned int *codes;
962 BYTE b, *bytes;
963 BOOL inepilogue = FALSE;
965 codes = RVA( rva, words * sizeof(*codes) );
966 rva = rva + words * sizeof(*codes);
967 bytes = (BYTE*)codes;
969 for (b = 0; b < words * sizeof(*codes); b++)
971 BYTE code = bytes[b];
973 if (info->e && b == count)
975 printf( "Epilogue:\n" );
976 inepilogue = TRUE;
978 else if (!info->e && infoepi)
980 for (i = 0; i < count; i++)
981 if (b == infoepi[i].index)
983 printf( "Epilogue from Scope %x at %08x:\n", i,
984 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
985 inepilogue = TRUE;
989 printf( " Unwind Code %x\t", code );
991 if (code == 0x00)
992 printf( "\n" );
993 else if (code <= 0x7f)
994 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
995 else if (code <= 0xbf)
997 WORD excode, f;
998 BOOL first = TRUE;
999 BYTE excodes = bytes[++b];
1001 excode = (code << 8) | excodes;
1002 printf( "%s {", inepilogue ? "pop" : "push" );
1004 for (f = 0; f <= 12; f++)
1006 if ((excode >> f) & 1)
1008 printf( "%sr%u", first ? "" : ", ", f );
1009 first = FALSE;
1013 if (excode & 0x2000)
1014 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1016 printf( "}\n" );
1018 else if (code <= 0xcf)
1019 if (inepilogue)
1020 printf( "mov sp, r%u\n", code & 0x0f );
1021 else
1022 printf( "mov r%u, sp\n", code & 0x0f );
1023 else if (code <= 0xd7)
1024 if (inepilogue)
1025 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1026 else
1027 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1028 else if (code <= 0xdf)
1029 if (inepilogue)
1030 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1031 else
1032 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1033 else if (code <= 0xe7)
1034 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1035 else if (code <= 0xeb)
1037 WORD excode;
1038 BYTE excodes = bytes[++b];
1040 excode = (code << 8) | excodes;
1041 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1043 else if (code <= 0xed)
1045 WORD excode, f;
1046 BOOL first = TRUE;
1047 BYTE excodes = bytes[++b];
1049 excode = (code << 8) | excodes;
1050 printf( "%s {", inepilogue ? "pop" : "push" );
1052 for (f = 0; f < 8; f++)
1054 if ((excode >> f) & 1)
1056 printf( "%sr%u", first ? "" : ", ", f );
1057 first = FALSE;
1061 if (excode & 0x0100)
1062 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1064 printf( "}\n" );
1066 else if (code == 0xee)
1067 printf( "unknown 16\n" );
1068 else if (code == 0xef)
1070 WORD excode;
1071 BYTE excodes = bytes[++b];
1073 if (excodes <= 0x0f)
1075 excode = (code << 8) | excodes;
1076 if (inepilogue)
1077 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1078 else
1079 printf( "unknown 32\n" );
1081 else
1082 printf( "unknown 32\n" );
1084 else if (code <= 0xf4)
1085 printf( "unknown\n" );
1086 else if (code <= 0xf6)
1088 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1089 BYTE excodes = bytes[++b];
1091 excode = (code << 8) | excodes;
1092 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1093 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1095 else if (code <= 0xf7)
1097 unsigned int excode;
1098 BYTE excodes[2];
1100 excodes[0] = bytes[++b];
1101 excodes[1] = bytes[++b];
1102 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1103 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1105 else if (code <= 0xf8)
1107 unsigned int excode;
1108 BYTE excodes[3];
1110 excodes[0] = bytes[++b];
1111 excodes[1] = bytes[++b];
1112 excodes[2] = bytes[++b];
1113 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1114 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1116 else if (code <= 0xf9)
1118 unsigned int excode;
1119 BYTE excodes[2];
1121 excodes[0] = bytes[++b];
1122 excodes[1] = bytes[++b];
1123 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1124 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1126 else if (code <= 0xfa)
1128 unsigned int excode;
1129 BYTE excodes[3];
1131 excodes[0] = bytes[++b];
1132 excodes[1] = bytes[++b];
1133 excodes[2] = bytes[++b];
1134 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1135 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1137 else if (code <= 0xfc)
1138 printf( "nop\n" );
1139 else if (code <= 0xfe)
1141 printf( "(end) nop\n" );
1142 inepilogue = TRUE;
1144 else
1146 printf( "end\n" );
1147 inepilogue = TRUE;
1152 if (info->x)
1154 const unsigned int *handler;
1156 handler = RVA( rva, sizeof(*handler) );
1157 rva = rva + sizeof(*handler);
1159 printf( " handler %08x data at %08x\n", *handler, rva);
1163 static void dump_dir_exceptions(void)
1165 unsigned int i, size = 0;
1166 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1167 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1169 if (!funcs) return;
1171 if (file_header->Machine == IMAGE_FILE_MACHINE_AMD64)
1173 size /= sizeof(struct runtime_function_x86_64);
1174 printf( "Exception info (%u functions):\n", size );
1175 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1177 else if (file_header->Machine == IMAGE_FILE_MACHINE_ARMNT)
1179 size /= sizeof(struct runtime_function_armnt);
1180 printf( "Exception info (%u functions):\n", size );
1181 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1183 else printf( "Exception information not supported for %s binaries\n",
1184 get_machine_str(file_header->Machine));
1188 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il)
1190 /* FIXME: This does not properly handle large images */
1191 const IMAGE_IMPORT_BY_NAME* iibn;
1192 for (; il->u1.Ordinal; il++)
1194 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1195 printf(" %4u <by ordinal>\n", (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
1196 else
1198 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1199 if (!iibn)
1200 printf("Can't grab import by name info, skipping to next ordinal\n");
1201 else
1202 printf(" %4u %s %x\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
1207 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset)
1209 const IMAGE_IMPORT_BY_NAME* iibn;
1210 for (; il->u1.Ordinal; il++)
1212 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1213 printf(" %4u <by ordinal>\n", IMAGE_ORDINAL32(il->u1.Ordinal));
1214 else
1216 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1217 if (!iibn)
1218 printf("Can't grab import by name info, skipping to next ordinal\n");
1219 else
1220 printf(" %4u %s %x\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
1225 static void dump_dir_imported_functions(void)
1227 unsigned directorySize;
1228 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1230 if (!importDesc) return;
1232 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1234 for (;;)
1236 const IMAGE_THUNK_DATA32* il;
1238 if (!importDesc->Name || !importDesc->FirstThunk) break;
1240 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1241 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
1242 printf(" TimeDateStamp: %08X (%s)\n",
1243 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1244 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
1245 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
1247 printf(" Ordn Name\n");
1249 il = (importDesc->u.OriginalFirstThunk != 0) ?
1250 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
1251 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1253 if (!il)
1254 printf("Can't grab thunk data, going to next imported DLL\n");
1255 else
1257 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1258 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il);
1259 else
1260 dump_image_thunk_data32(il, 0);
1261 printf("\n");
1263 importDesc++;
1265 printf("\n");
1268 static void dump_dir_delay_imported_functions(void)
1270 unsigned directorySize;
1271 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1273 if (!importDesc) return;
1275 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1277 for (;;)
1279 const IMAGE_THUNK_DATA32* il;
1280 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1282 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1284 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1285 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1286 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1287 printf(" TimeDateStamp: %08X (%s)\n",
1288 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1290 printf(" Ordn Name\n");
1292 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1294 if (!il)
1295 printf("Can't grab thunk data, going to next imported DLL\n");
1296 else
1298 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1299 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il);
1300 else
1301 dump_image_thunk_data32(il, offset);
1302 printf("\n");
1304 importDesc++;
1306 printf("\n");
1309 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1311 const char* str;
1313 printf("Directory %02u\n", idx + 1);
1314 printf(" Characteristics: %08X\n", idd->Characteristics);
1315 printf(" TimeDateStamp: %08X %s\n",
1316 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1317 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1318 switch (idd->Type)
1320 default:
1321 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1322 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1323 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1324 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1325 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1326 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1327 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1328 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1329 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1330 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1331 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1332 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1334 printf(" Type: %u (%s)\n", idd->Type, str);
1335 printf(" SizeOfData: %u\n", idd->SizeOfData);
1336 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1337 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1339 switch (idd->Type)
1341 case IMAGE_DEBUG_TYPE_UNKNOWN:
1342 break;
1343 case IMAGE_DEBUG_TYPE_COFF:
1344 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1345 IMAGE_FIRST_SECTION(PE_nt_headers));
1346 break;
1347 case IMAGE_DEBUG_TYPE_CODEVIEW:
1348 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1349 break;
1350 case IMAGE_DEBUG_TYPE_FPO:
1351 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1352 break;
1353 case IMAGE_DEBUG_TYPE_MISC:
1355 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1356 if (!misc) {printf("Can't get misc debug information\n"); break;}
1357 printf(" DataType: %u (%s)\n",
1358 misc->DataType,
1359 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1360 printf(" Length: %u\n", misc->Length);
1361 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1362 printf(" Data: %s\n", misc->Data);
1364 break;
1365 case IMAGE_DEBUG_TYPE_EXCEPTION:
1366 break;
1367 case IMAGE_DEBUG_TYPE_FIXUP:
1368 break;
1369 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC:
1370 break;
1371 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:
1372 break;
1373 case IMAGE_DEBUG_TYPE_BORLAND:
1374 break;
1375 case IMAGE_DEBUG_TYPE_RESERVED10:
1376 break;
1377 case IMAGE_DEBUG_TYPE_CLSID:
1378 break;
1380 printf("\n");
1383 static void dump_dir_debug(void)
1385 unsigned nb_dbg, i;
1386 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1388 nb_dbg /= sizeof(*debugDir);
1389 if (!debugDir || !nb_dbg) return;
1391 printf("Debug Table (%u directories)\n", nb_dbg);
1393 for (i = 0; i < nb_dbg; i++)
1395 dump_dir_debug_dir(debugDir, i);
1396 debugDir++;
1398 printf("\n");
1401 static inline void print_clrflags(const char *title, DWORD value)
1403 printf(" %-34s 0x%X\n", title, value);
1404 #define X(f,s) if (value & f) printf(" %s\n", s)
1405 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1406 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1407 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1408 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1409 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1410 #undef X
1413 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1415 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1418 static void dump_dir_clr_header(void)
1420 unsigned int size = 0;
1421 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1423 if (!dir) return;
1425 printf( "CLR Header\n" );
1426 print_dword( "Header Size", dir->cb );
1427 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1428 print_clrflags( "Flags", dir->Flags );
1429 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1430 printf("\n");
1431 printf( "CLR Data Directory\n" );
1432 print_clrdirectory( "MetaData", &dir->MetaData );
1433 print_clrdirectory( "Resources", &dir->Resources );
1434 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1435 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1436 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1437 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1438 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1439 printf("\n");
1442 static void dump_dir_reloc(void)
1444 unsigned int i, size = 0;
1445 const USHORT *relocs;
1446 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1447 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1448 static const char * const names[] =
1450 "BASED_ABSOLUTE",
1451 "BASED_HIGH",
1452 "BASED_LOW",
1453 "BASED_HIGHLOW",
1454 "BASED_HIGHADJ",
1455 "BASED_MIPS_JMPADDR",
1456 "BASED_SECTION",
1457 "BASED_REL",
1458 "unknown 8",
1459 "BASED_IA64_IMM64",
1460 "BASED_DIR64",
1461 "BASED_HIGH3ADJ",
1462 "unknown 12",
1463 "unknown 13",
1464 "unknown 14",
1465 "unknown 15"
1468 if (!rel) return;
1470 printf( "Relocations\n" );
1471 while (rel < end - 1 && rel->SizeOfBlock)
1473 printf( " Page %x\n", rel->VirtualAddress );
1474 relocs = (const USHORT *)(rel + 1);
1475 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1476 while (i--)
1478 USHORT offset = *relocs & 0xfff;
1479 int type = *relocs >> 12;
1480 printf( " off %04x type %s\n", offset, names[type] );
1481 relocs++;
1483 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1485 printf("\n");
1488 static void dump_dir_tls(void)
1490 IMAGE_TLS_DIRECTORY64 dir;
1491 const DWORD *callbacks;
1492 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1494 if (!pdir) return;
1496 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1497 memcpy(&dir, pdir, sizeof(dir));
1498 else
1500 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1501 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1502 dir.AddressOfIndex = pdir->AddressOfIndex;
1503 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1504 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1505 dir.Characteristics = pdir->Characteristics;
1508 /* FIXME: This does not properly handle large images */
1509 printf( "Thread Local Storage\n" );
1510 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1511 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1512 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1513 (DWORD)dir.SizeOfZeroFill );
1514 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1515 printf( " Characteristics %08x\n", dir.Characteristics );
1516 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1517 if (dir.AddressOfCallBacks)
1519 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1520 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1522 printf( " %08x", *callbacks );
1523 addr += sizeof(DWORD);
1526 printf(" }\n\n");
1529 enum FileSig get_kind_dbg(void)
1531 const WORD* pw;
1533 pw = PRD(0, sizeof(WORD));
1534 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1536 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1537 return SIG_UNKNOWN;
1540 void dbg_dump(void)
1542 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1543 unsigned nb_dbg;
1544 unsigned i;
1545 const IMAGE_DEBUG_DIRECTORY* debugDir;
1547 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1548 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1550 printf ("Signature: %.2s (0x%4X)\n",
1551 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1552 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1553 printf ("Machine: 0x%04X (%s)\n",
1554 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1555 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1556 printf ("TimeDateStamp: 0x%08X (%s)\n",
1557 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1558 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1559 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1560 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1561 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1562 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1563 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1565 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1566 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1567 {printf("Can't get the sections, aborting\n"); return;}
1569 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1571 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1572 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1573 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1574 separateDebugHead->ExportedNamesSize,
1575 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1576 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1578 printf("Debug Table (%u directories)\n", nb_dbg);
1580 for (i = 0; i < nb_dbg; i++)
1582 dump_dir_debug_dir(debugDir, i);
1583 debugDir++;
1587 static const char *get_resource_type( unsigned int id )
1589 static const char * const types[] =
1591 NULL,
1592 "CURSOR",
1593 "BITMAP",
1594 "ICON",
1595 "MENU",
1596 "DIALOG",
1597 "STRING",
1598 "FONTDIR",
1599 "FONT",
1600 "ACCELERATOR",
1601 "RCDATA",
1602 "MESSAGETABLE",
1603 "GROUP_CURSOR",
1604 NULL,
1605 "GROUP_ICON",
1606 NULL,
1607 "VERSION",
1608 "DLGINCLUDE",
1609 NULL,
1610 "PLUGPLAY",
1611 "VXD",
1612 "ANICURSOR",
1613 "ANIICON",
1614 "HTML",
1615 "RT_MANIFEST"
1618 if ((size_t)id < sizeof(types)/sizeof(types[0])) return types[id];
1619 return NULL;
1622 /* dump an ASCII string with proper escaping */
1623 static int dump_strA( const unsigned char *str, size_t len )
1625 static const char escapes[32] = ".......abtnvfr.............e....";
1626 char buffer[256];
1627 char *pos = buffer;
1628 int count = 0;
1630 for (; len; str++, len--)
1632 if (pos > buffer + sizeof(buffer) - 8)
1634 fwrite( buffer, pos - buffer, 1, stdout );
1635 count += pos - buffer;
1636 pos = buffer;
1638 if (*str > 127) /* hex escape */
1640 pos += sprintf( pos, "\\x%02x", *str );
1641 continue;
1643 if (*str < 32) /* octal or C escape */
1645 if (!*str && len == 1) continue; /* do not output terminating NULL */
1646 if (escapes[*str] != '.')
1647 pos += sprintf( pos, "\\%c", escapes[*str] );
1648 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1649 pos += sprintf( pos, "\\%03o", *str );
1650 else
1651 pos += sprintf( pos, "\\%o", *str );
1652 continue;
1654 if (*str == '\\') *pos++ = '\\';
1655 *pos++ = *str;
1657 fwrite( buffer, pos - buffer, 1, stdout );
1658 count += pos - buffer;
1659 return count;
1662 /* dump a Unicode string with proper escaping */
1663 static int dump_strW( const WCHAR *str, size_t len )
1665 static const char escapes[32] = ".......abtnvfr.............e....";
1666 char buffer[256];
1667 char *pos = buffer;
1668 int count = 0;
1670 for (; len; str++, len--)
1672 if (pos > buffer + sizeof(buffer) - 8)
1674 fwrite( buffer, pos - buffer, 1, stdout );
1675 count += pos - buffer;
1676 pos = buffer;
1678 if (*str > 127) /* hex escape */
1680 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
1681 pos += sprintf( pos, "\\x%04x", *str );
1682 else
1683 pos += sprintf( pos, "\\x%x", *str );
1684 continue;
1686 if (*str < 32) /* octal or C escape */
1688 if (!*str && len == 1) continue; /* do not output terminating NULL */
1689 if (escapes[*str] != '.')
1690 pos += sprintf( pos, "\\%c", escapes[*str] );
1691 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1692 pos += sprintf( pos, "\\%03o", *str );
1693 else
1694 pos += sprintf( pos, "\\%o", *str );
1695 continue;
1697 if (*str == '\\') *pos++ = '\\';
1698 *pos++ = *str;
1700 fwrite( buffer, pos - buffer, 1, stdout );
1701 count += pos - buffer;
1702 return count;
1705 /* dump data for a STRING resource */
1706 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
1708 int i;
1710 for (i = 0; i < 16 && size; i++)
1712 unsigned len = *ptr++;
1714 if (len >= size)
1716 len = size;
1717 size = 0;
1719 else size -= len + 1;
1721 if (len)
1723 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
1724 dump_strW( ptr, len );
1725 printf( "\"\n" );
1726 ptr += len;
1731 /* dump data for a MESSAGETABLE resource */
1732 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
1734 const MESSAGE_RESOURCE_DATA *data = ptr;
1735 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
1736 unsigned i, j;
1738 for (i = 0; i < data->NumberOfBlocks; i++, block++)
1740 const MESSAGE_RESOURCE_ENTRY *entry;
1742 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
1743 for (j = block->LowId; j <= block->HighId; j++)
1745 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
1747 const WCHAR *str = (const WCHAR *)entry->Text;
1748 printf( "%s%08x L\"", prefix, j );
1749 dump_strW( str, strlenW(str) );
1750 printf( "\"\n" );
1752 else
1754 const char *str = (const char *) entry->Text;
1755 printf( "%s%08x \"", prefix, j );
1756 dump_strA( entry->Text, strlen(str) );
1757 printf( "\"\n" );
1759 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
1764 static void dump_dir_resource(void)
1766 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
1767 const IMAGE_RESOURCE_DIRECTORY *namedir;
1768 const IMAGE_RESOURCE_DIRECTORY *langdir;
1769 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1770 const IMAGE_RESOURCE_DIR_STRING_U *string;
1771 const IMAGE_RESOURCE_DATA_ENTRY *data;
1772 int i, j, k;
1774 if (!root) return;
1776 printf( "Resources:" );
1778 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1780 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1781 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
1782 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1784 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1785 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
1786 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1788 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1790 printf( "\n " );
1791 if (e1->u.s.NameIsString)
1793 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
1794 dump_unicode_str( string->NameString, string->Length );
1796 else
1798 const char *type = get_resource_type( e1->u.Id );
1799 if (type) printf( "%s", type );
1800 else printf( "%04x", e1->u.Id );
1803 printf( " Name=" );
1804 if (e2->u.s.NameIsString)
1806 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
1807 dump_unicode_str( string->NameString, string->Length );
1809 else
1810 printf( "%04x", e2->u.Id );
1812 printf( " Language=%04x:\n", e3->u.Id );
1813 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1814 if (e1->u.s.NameIsString)
1816 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1818 else switch(e1->u.Id)
1820 case 6:
1821 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
1822 break;
1823 case 11:
1824 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
1825 e2->u.Id, " " );
1826 break;
1827 default:
1828 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1829 break;
1834 printf( "\n\n" );
1837 static void dump_debug(void)
1839 const char* stabs = NULL;
1840 unsigned szstabs = 0;
1841 const char* stabstr = NULL;
1842 unsigned szstr = 0;
1843 unsigned i;
1844 const IMAGE_SECTION_HEADER* sectHead;
1846 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
1848 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
1850 if (!strcmp((const char *)sectHead->Name, ".stab"))
1852 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1853 szstabs = sectHead->Misc.VirtualSize;
1855 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
1857 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1858 szstr = sectHead->Misc.VirtualSize;
1861 if (stabs && stabstr)
1862 dump_stabs(stabs, szstabs, stabstr, szstr);
1865 static void dump_symbol_table(void)
1867 const IMAGE_SYMBOL* sym;
1868 int numsym;
1870 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
1871 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
1872 return;
1873 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
1874 sizeof(*sym) * numsym);
1875 if (!sym) return;
1877 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
1880 enum FileSig get_kind_exec(void)
1882 const WORD* pw;
1883 const DWORD* pdw;
1884 const IMAGE_DOS_HEADER* dh;
1886 pw = PRD(0, sizeof(WORD));
1887 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1889 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
1891 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
1893 /* the signature is the first DWORD */
1894 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
1895 if (pdw)
1897 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
1898 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
1899 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
1901 return SIG_DOS;
1903 return SIG_UNKNOWN;
1906 void pe_dump(void)
1908 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
1910 PE_nt_headers = get_nt_header();
1911 if (is_fake_dll()) printf( "*** This is a Wine fake DLL ***\n\n" );
1913 if (globals.do_dumpheader)
1915 dump_pe_header();
1916 /* FIXME: should check ptr */
1917 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
1918 PE_nt_headers->FileHeader.NumberOfSections);
1920 else if (!globals.dumpsect)
1922 /* show at least something here */
1923 dump_pe_header();
1926 if (globals.dumpsect)
1928 if (all || !strcmp(globals.dumpsect, "import"))
1930 dump_dir_imported_functions();
1931 dump_dir_delay_imported_functions();
1933 if (all || !strcmp(globals.dumpsect, "export"))
1934 dump_dir_exported_functions();
1935 if (all || !strcmp(globals.dumpsect, "debug"))
1936 dump_dir_debug();
1937 if (all || !strcmp(globals.dumpsect, "resource"))
1938 dump_dir_resource();
1939 if (all || !strcmp(globals.dumpsect, "tls"))
1940 dump_dir_tls();
1941 if (all || !strcmp(globals.dumpsect, "clr"))
1942 dump_dir_clr_header();
1943 if (all || !strcmp(globals.dumpsect, "reloc"))
1944 dump_dir_reloc();
1945 if (all || !strcmp(globals.dumpsect, "except"))
1946 dump_dir_exceptions();
1948 if (globals.do_symbol_table)
1949 dump_symbol_table();
1950 if (globals.do_debug)
1951 dump_debug();
1954 typedef struct _dll_symbol {
1955 size_t ordinal;
1956 char *symbol;
1957 } dll_symbol;
1959 static dll_symbol *dll_symbols = NULL;
1960 static dll_symbol *dll_current_symbol = NULL;
1962 /* Compare symbols by ordinal for qsort */
1963 static int symbol_cmp(const void *left, const void *right)
1965 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
1968 /*******************************************************************
1969 * dll_close
1971 * Free resources used by DLL
1973 /* FIXME: Not used yet
1974 static void dll_close (void)
1976 dll_symbol* ds;
1978 if (!dll_symbols) {
1979 fatal("No symbols");
1981 for (ds = dll_symbols; ds->symbol; ds++)
1982 free(ds->symbol);
1983 free (dll_symbols);
1984 dll_symbols = NULL;
1988 static void do_grab_sym( void )
1990 const IMAGE_EXPORT_DIRECTORY*exportDir;
1991 unsigned i, j;
1992 const DWORD* pName;
1993 const DWORD* pFunc;
1994 const WORD* pOrdl;
1995 const char* ptr;
1996 DWORD* map;
1998 PE_nt_headers = get_nt_header();
1999 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2001 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2002 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2003 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2004 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2005 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2006 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2008 /* dll_close(); */
2010 if (!(dll_symbols = malloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol))))
2011 fatal ("Out of memory");
2013 /* bit map of used funcs */
2014 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2015 if (!map) fatal("no memory");
2017 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2019 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2020 ptr = RVA(*pName++, sizeof(DWORD));
2021 if (!ptr) ptr = "cant_get_function";
2022 dll_symbols[j].symbol = strdup(ptr);
2023 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2024 assert(dll_symbols[j].symbol);
2027 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2029 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2031 char ordinal_text[256];
2032 /* Ordinal only entry */
2033 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%u",
2034 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2035 exportDir->Base + i);
2036 str_toupper(ordinal_text);
2037 dll_symbols[j].symbol = strdup(ordinal_text);
2038 assert(dll_symbols[j].symbol);
2039 dll_symbols[j].ordinal = exportDir->Base + i;
2040 j++;
2041 assert(j <= exportDir->NumberOfFunctions);
2044 free(map);
2046 if (NORMAL)
2047 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2048 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2050 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2052 dll_symbols[j].symbol = NULL;
2054 dll_current_symbol = dll_symbols;
2057 /*******************************************************************
2058 * dll_open
2060 * Open a DLL and read in exported symbols
2062 BOOL dll_open (const char *dll_name)
2064 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2067 /*******************************************************************
2068 * dll_next_symbol
2070 * Get next exported symbol from dll
2072 BOOL dll_next_symbol (parsed_symbol * sym)
2074 if (!dll_current_symbol || !dll_current_symbol->symbol)
2075 return FALSE;
2076 assert (dll_symbols);
2077 sym->symbol = strdup (dll_current_symbol->symbol);
2078 sym->ordinal = dll_current_symbol->ordinal;
2079 dll_current_symbol++;
2080 return TRUE;