advapi32/tests: Allow ERROR_ACCESS_DENIED for newer Win10.
[wine.git] / tools / winedump / pe.c
blob7bbb576104b355b7e7046c606aebfd941a593100
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, DWORD thunk_rva)
1191 /* FIXME: This does not properly handle large images */
1192 const IMAGE_IMPORT_BY_NAME* iibn;
1193 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
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, thunk_rva);
1208 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, DWORD thunk_rva)
1210 const IMAGE_IMPORT_BY_NAME* iibn;
1211 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(DWORD))
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, thunk_rva);
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, importDesc->FirstThunk);
1260 else
1261 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1262 printf("\n");
1264 importDesc++;
1266 printf("\n");
1269 static void dump_dir_loadconfig(void)
1271 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1272 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1274 if (!loadcfg32) return;
1276 printf( "Loadconfig\n" );
1277 print_dword( "Size", loadcfg32->Size );
1278 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1279 print_word( "MajorVersion", loadcfg32->MajorVersion );
1280 print_word( "MinorVersion", loadcfg32->MinorVersion );
1281 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1282 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1283 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1285 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1287 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1288 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1289 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1290 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1291 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1292 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1293 print_word( "CSDVersion", loadcfg64->CSDVersion );
1294 print_word( "Reserved", loadcfg64->Reserved1 );
1295 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1296 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1297 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1299 else
1301 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1302 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1303 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1304 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1305 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1306 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1307 print_word( "CSDVersion", loadcfg32->CSDVersion );
1308 print_word( "Reserved", loadcfg32->Reserved1 );
1309 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1310 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1311 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1315 static void dump_dir_delay_imported_functions(void)
1317 unsigned directorySize;
1318 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1320 if (!importDesc) return;
1322 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1324 for (;;)
1326 const IMAGE_THUNK_DATA32* il;
1327 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1329 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1331 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1332 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1333 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1334 printf(" Address Table: %08x\n", importDesc->ImportAddressTableRVA);
1335 printf(" TimeDateStamp: %08X (%s)\n",
1336 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1338 printf(" Ordn Name\n");
1340 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1342 if (!il)
1343 printf("Can't grab thunk data, going to next imported DLL\n");
1344 else
1346 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1347 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1348 else
1349 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1350 printf("\n");
1352 importDesc++;
1354 printf("\n");
1357 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1359 const char* str;
1361 printf("Directory %02u\n", idx + 1);
1362 printf(" Characteristics: %08X\n", idd->Characteristics);
1363 printf(" TimeDateStamp: %08X %s\n",
1364 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1365 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1366 switch (idd->Type)
1368 default:
1369 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1370 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1371 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1372 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1373 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1374 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1375 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1376 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1377 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1378 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1379 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1380 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1381 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1382 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1383 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1384 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1386 printf(" Type: %u (%s)\n", idd->Type, str);
1387 printf(" SizeOfData: %u\n", idd->SizeOfData);
1388 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1389 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1391 switch (idd->Type)
1393 case IMAGE_DEBUG_TYPE_UNKNOWN:
1394 break;
1395 case IMAGE_DEBUG_TYPE_COFF:
1396 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1397 IMAGE_FIRST_SECTION(PE_nt_headers));
1398 break;
1399 case IMAGE_DEBUG_TYPE_CODEVIEW:
1400 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1401 break;
1402 case IMAGE_DEBUG_TYPE_FPO:
1403 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1404 break;
1405 case IMAGE_DEBUG_TYPE_MISC:
1407 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1408 if (!misc) {printf("Can't get misc debug information\n"); break;}
1409 printf(" DataType: %u (%s)\n",
1410 misc->DataType,
1411 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1412 printf(" Length: %u\n", misc->Length);
1413 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1414 printf(" Data: %s\n", misc->Data);
1416 break;
1417 default: break;
1419 printf("\n");
1422 static void dump_dir_debug(void)
1424 unsigned nb_dbg, i;
1425 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1427 nb_dbg /= sizeof(*debugDir);
1428 if (!debugDir || !nb_dbg) return;
1430 printf("Debug Table (%u directories)\n", nb_dbg);
1432 for (i = 0; i < nb_dbg; i++)
1434 dump_dir_debug_dir(debugDir, i);
1435 debugDir++;
1437 printf("\n");
1440 static inline void print_clrflags(const char *title, DWORD value)
1442 printf(" %-34s 0x%X\n", title, value);
1443 #define X(f,s) if (value & f) printf(" %s\n", s)
1444 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1445 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1446 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1447 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1448 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1449 #undef X
1452 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1454 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1457 static void dump_dir_clr_header(void)
1459 unsigned int size = 0;
1460 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1462 if (!dir) return;
1464 printf( "CLR Header\n" );
1465 print_dword( "Header Size", dir->cb );
1466 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1467 print_clrflags( "Flags", dir->Flags );
1468 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1469 printf("\n");
1470 printf( "CLR Data Directory\n" );
1471 print_clrdirectory( "MetaData", &dir->MetaData );
1472 print_clrdirectory( "Resources", &dir->Resources );
1473 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1474 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1475 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1476 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1477 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1478 printf("\n");
1481 static void dump_dir_reloc(void)
1483 unsigned int i, size = 0;
1484 const USHORT *relocs;
1485 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1486 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1487 static const char * const names[] =
1489 "BASED_ABSOLUTE",
1490 "BASED_HIGH",
1491 "BASED_LOW",
1492 "BASED_HIGHLOW",
1493 "BASED_HIGHADJ",
1494 "BASED_MIPS_JMPADDR",
1495 "BASED_SECTION",
1496 "BASED_REL",
1497 "unknown 8",
1498 "BASED_IA64_IMM64",
1499 "BASED_DIR64",
1500 "BASED_HIGH3ADJ",
1501 "unknown 12",
1502 "unknown 13",
1503 "unknown 14",
1504 "unknown 15"
1507 if (!rel) return;
1509 printf( "Relocations\n" );
1510 while (rel < end - 1 && rel->SizeOfBlock)
1512 printf( " Page %x\n", rel->VirtualAddress );
1513 relocs = (const USHORT *)(rel + 1);
1514 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1515 while (i--)
1517 USHORT offset = *relocs & 0xfff;
1518 int type = *relocs >> 12;
1519 printf( " off %04x type %s\n", offset, names[type] );
1520 relocs++;
1522 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1524 printf("\n");
1527 static void dump_dir_tls(void)
1529 IMAGE_TLS_DIRECTORY64 dir;
1530 const DWORD *callbacks;
1531 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1533 if (!pdir) return;
1535 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1536 memcpy(&dir, pdir, sizeof(dir));
1537 else
1539 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1540 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1541 dir.AddressOfIndex = pdir->AddressOfIndex;
1542 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1543 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1544 dir.Characteristics = pdir->Characteristics;
1547 /* FIXME: This does not properly handle large images */
1548 printf( "Thread Local Storage\n" );
1549 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1550 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1551 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1552 (DWORD)dir.SizeOfZeroFill );
1553 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1554 printf( " Characteristics %08x\n", dir.Characteristics );
1555 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1556 if (dir.AddressOfCallBacks)
1558 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1559 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1561 printf( " %08x", *callbacks );
1562 addr += sizeof(DWORD);
1565 printf(" }\n\n");
1568 enum FileSig get_kind_dbg(void)
1570 const WORD* pw;
1572 pw = PRD(0, sizeof(WORD));
1573 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1575 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1576 return SIG_UNKNOWN;
1579 void dbg_dump(void)
1581 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1582 unsigned nb_dbg;
1583 unsigned i;
1584 const IMAGE_DEBUG_DIRECTORY* debugDir;
1586 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1587 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1589 printf ("Signature: %.2s (0x%4X)\n",
1590 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1591 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1592 printf ("Machine: 0x%04X (%s)\n",
1593 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1594 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1595 printf ("TimeDateStamp: 0x%08X (%s)\n",
1596 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1597 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1598 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1599 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1600 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1601 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1602 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1604 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1605 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1606 {printf("Can't get the sections, aborting\n"); return;}
1608 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1610 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1611 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1612 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1613 separateDebugHead->ExportedNamesSize,
1614 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1615 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1617 printf("Debug Table (%u directories)\n", nb_dbg);
1619 for (i = 0; i < nb_dbg; i++)
1621 dump_dir_debug_dir(debugDir, i);
1622 debugDir++;
1626 static const char *get_resource_type( unsigned int id )
1628 static const char * const types[] =
1630 NULL,
1631 "CURSOR",
1632 "BITMAP",
1633 "ICON",
1634 "MENU",
1635 "DIALOG",
1636 "STRING",
1637 "FONTDIR",
1638 "FONT",
1639 "ACCELERATOR",
1640 "RCDATA",
1641 "MESSAGETABLE",
1642 "GROUP_CURSOR",
1643 NULL,
1644 "GROUP_ICON",
1645 NULL,
1646 "VERSION",
1647 "DLGINCLUDE",
1648 NULL,
1649 "PLUGPLAY",
1650 "VXD",
1651 "ANICURSOR",
1652 "ANIICON",
1653 "HTML",
1654 "RT_MANIFEST"
1657 if ((size_t)id < sizeof(types)/sizeof(types[0])) return types[id];
1658 return NULL;
1661 /* dump an ASCII string with proper escaping */
1662 static int dump_strA( const unsigned char *str, size_t len )
1664 static const char escapes[32] = ".......abtnvfr.............e....";
1665 char buffer[256];
1666 char *pos = buffer;
1667 int count = 0;
1669 for (; len; str++, len--)
1671 if (pos > buffer + sizeof(buffer) - 8)
1673 fwrite( buffer, pos - buffer, 1, stdout );
1674 count += pos - buffer;
1675 pos = buffer;
1677 if (*str > 127) /* hex escape */
1679 pos += sprintf( pos, "\\x%02x", *str );
1680 continue;
1682 if (*str < 32) /* octal or C escape */
1684 if (!*str && len == 1) continue; /* do not output terminating NULL */
1685 if (escapes[*str] != '.')
1686 pos += sprintf( pos, "\\%c", escapes[*str] );
1687 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1688 pos += sprintf( pos, "\\%03o", *str );
1689 else
1690 pos += sprintf( pos, "\\%o", *str );
1691 continue;
1693 if (*str == '\\') *pos++ = '\\';
1694 *pos++ = *str;
1696 fwrite( buffer, pos - buffer, 1, stdout );
1697 count += pos - buffer;
1698 return count;
1701 /* dump a Unicode string with proper escaping */
1702 static int dump_strW( const WCHAR *str, size_t len )
1704 static const char escapes[32] = ".......abtnvfr.............e....";
1705 char buffer[256];
1706 char *pos = buffer;
1707 int count = 0;
1709 for (; len; str++, len--)
1711 if (pos > buffer + sizeof(buffer) - 8)
1713 fwrite( buffer, pos - buffer, 1, stdout );
1714 count += pos - buffer;
1715 pos = buffer;
1717 if (*str > 127) /* hex escape */
1719 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
1720 pos += sprintf( pos, "\\x%04x", *str );
1721 else
1722 pos += sprintf( pos, "\\x%x", *str );
1723 continue;
1725 if (*str < 32) /* octal or C escape */
1727 if (!*str && len == 1) continue; /* do not output terminating NULL */
1728 if (escapes[*str] != '.')
1729 pos += sprintf( pos, "\\%c", escapes[*str] );
1730 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1731 pos += sprintf( pos, "\\%03o", *str );
1732 else
1733 pos += sprintf( pos, "\\%o", *str );
1734 continue;
1736 if (*str == '\\') *pos++ = '\\';
1737 *pos++ = *str;
1739 fwrite( buffer, pos - buffer, 1, stdout );
1740 count += pos - buffer;
1741 return count;
1744 /* dump data for a STRING resource */
1745 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
1747 int i;
1749 for (i = 0; i < 16 && size; i++)
1751 unsigned len = *ptr++;
1753 if (len >= size)
1755 len = size;
1756 size = 0;
1758 else size -= len + 1;
1760 if (len)
1762 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
1763 dump_strW( ptr, len );
1764 printf( "\"\n" );
1765 ptr += len;
1770 /* dump data for a MESSAGETABLE resource */
1771 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
1773 const MESSAGE_RESOURCE_DATA *data = ptr;
1774 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
1775 unsigned i, j;
1777 for (i = 0; i < data->NumberOfBlocks; i++, block++)
1779 const MESSAGE_RESOURCE_ENTRY *entry;
1781 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
1782 for (j = block->LowId; j <= block->HighId; j++)
1784 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
1786 const WCHAR *str = (const WCHAR *)entry->Text;
1787 printf( "%s%08x L\"", prefix, j );
1788 dump_strW( str, strlenW(str) );
1789 printf( "\"\n" );
1791 else
1793 const char *str = (const char *) entry->Text;
1794 printf( "%s%08x \"", prefix, j );
1795 dump_strA( entry->Text, strlen(str) );
1796 printf( "\"\n" );
1798 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
1803 static void dump_dir_resource(void)
1805 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
1806 const IMAGE_RESOURCE_DIRECTORY *namedir;
1807 const IMAGE_RESOURCE_DIRECTORY *langdir;
1808 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1809 const IMAGE_RESOURCE_DIR_STRING_U *string;
1810 const IMAGE_RESOURCE_DATA_ENTRY *data;
1811 int i, j, k;
1813 if (!root) return;
1815 printf( "Resources:" );
1817 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1819 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1820 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
1821 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1823 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1824 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
1825 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1827 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1829 printf( "\n " );
1830 if (e1->u.s.NameIsString)
1832 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
1833 dump_unicode_str( string->NameString, string->Length );
1835 else
1837 const char *type = get_resource_type( e1->u.Id );
1838 if (type) printf( "%s", type );
1839 else printf( "%04x", e1->u.Id );
1842 printf( " Name=" );
1843 if (e2->u.s.NameIsString)
1845 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
1846 dump_unicode_str( string->NameString, string->Length );
1848 else
1849 printf( "%04x", e2->u.Id );
1851 printf( " Language=%04x:\n", e3->u.Id );
1852 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1853 if (e1->u.s.NameIsString)
1855 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1857 else switch(e1->u.Id)
1859 case 6:
1860 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
1861 break;
1862 case 11:
1863 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
1864 e2->u.Id, " " );
1865 break;
1866 default:
1867 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1868 break;
1873 printf( "\n\n" );
1876 static void dump_debug(void)
1878 const char* stabs = NULL;
1879 unsigned szstabs = 0;
1880 const char* stabstr = NULL;
1881 unsigned szstr = 0;
1882 unsigned i;
1883 const IMAGE_SECTION_HEADER* sectHead;
1885 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
1887 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
1889 if (!strcmp((const char *)sectHead->Name, ".stab"))
1891 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1892 szstabs = sectHead->Misc.VirtualSize;
1894 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
1896 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1897 szstr = sectHead->Misc.VirtualSize;
1900 if (stabs && stabstr)
1901 dump_stabs(stabs, szstabs, stabstr, szstr);
1904 static void dump_symbol_table(void)
1906 const IMAGE_SYMBOL* sym;
1907 int numsym;
1909 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
1910 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
1911 return;
1912 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
1913 sizeof(*sym) * numsym);
1914 if (!sym) return;
1916 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
1919 enum FileSig get_kind_exec(void)
1921 const WORD* pw;
1922 const DWORD* pdw;
1923 const IMAGE_DOS_HEADER* dh;
1925 pw = PRD(0, sizeof(WORD));
1926 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1928 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
1930 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
1932 /* the signature is the first DWORD */
1933 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
1934 if (pdw)
1936 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
1937 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
1938 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
1940 return SIG_DOS;
1942 return SIG_UNKNOWN;
1945 void pe_dump(void)
1947 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
1949 PE_nt_headers = get_nt_header();
1950 if (is_fake_dll()) printf( "*** This is a Wine fake DLL ***\n\n" );
1952 if (globals.do_dumpheader)
1954 dump_pe_header();
1955 /* FIXME: should check ptr */
1956 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
1957 PE_nt_headers->FileHeader.NumberOfSections);
1959 else if (!globals.dumpsect)
1961 /* show at least something here */
1962 dump_pe_header();
1965 if (globals.dumpsect)
1967 if (all || !strcmp(globals.dumpsect, "import"))
1969 dump_dir_imported_functions();
1970 dump_dir_delay_imported_functions();
1972 if (all || !strcmp(globals.dumpsect, "export"))
1973 dump_dir_exported_functions();
1974 if (all || !strcmp(globals.dumpsect, "debug"))
1975 dump_dir_debug();
1976 if (all || !strcmp(globals.dumpsect, "resource"))
1977 dump_dir_resource();
1978 if (all || !strcmp(globals.dumpsect, "tls"))
1979 dump_dir_tls();
1980 if (all || !strcmp(globals.dumpsect, "loadcfg"))
1981 dump_dir_loadconfig();
1982 if (all || !strcmp(globals.dumpsect, "clr"))
1983 dump_dir_clr_header();
1984 if (all || !strcmp(globals.dumpsect, "reloc"))
1985 dump_dir_reloc();
1986 if (all || !strcmp(globals.dumpsect, "except"))
1987 dump_dir_exceptions();
1989 if (globals.do_symbol_table)
1990 dump_symbol_table();
1991 if (globals.do_debug)
1992 dump_debug();
1995 typedef struct _dll_symbol {
1996 size_t ordinal;
1997 char *symbol;
1998 } dll_symbol;
2000 static dll_symbol *dll_symbols = NULL;
2001 static dll_symbol *dll_current_symbol = NULL;
2003 /* Compare symbols by ordinal for qsort */
2004 static int symbol_cmp(const void *left, const void *right)
2006 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2009 /*******************************************************************
2010 * dll_close
2012 * Free resources used by DLL
2014 /* FIXME: Not used yet
2015 static void dll_close (void)
2017 dll_symbol* ds;
2019 if (!dll_symbols) {
2020 fatal("No symbols");
2022 for (ds = dll_symbols; ds->symbol; ds++)
2023 free(ds->symbol);
2024 free (dll_symbols);
2025 dll_symbols = NULL;
2029 static void do_grab_sym( void )
2031 const IMAGE_EXPORT_DIRECTORY*exportDir;
2032 unsigned i, j;
2033 const DWORD* pName;
2034 const DWORD* pFunc;
2035 const WORD* pOrdl;
2036 const char* ptr;
2037 DWORD* map;
2039 PE_nt_headers = get_nt_header();
2040 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2042 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2043 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2044 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2045 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2046 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2047 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2049 /* dll_close(); */
2051 if (!(dll_symbols = malloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol))))
2052 fatal ("Out of memory");
2054 /* bit map of used funcs */
2055 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2056 if (!map) fatal("no memory");
2058 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2060 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2061 ptr = RVA(*pName++, sizeof(DWORD));
2062 if (!ptr) ptr = "cant_get_function";
2063 dll_symbols[j].symbol = strdup(ptr);
2064 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2065 assert(dll_symbols[j].symbol);
2068 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2070 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2072 char ordinal_text[256];
2073 /* Ordinal only entry */
2074 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%u",
2075 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2076 exportDir->Base + i);
2077 str_toupper(ordinal_text);
2078 dll_symbols[j].symbol = strdup(ordinal_text);
2079 assert(dll_symbols[j].symbol);
2080 dll_symbols[j].ordinal = exportDir->Base + i;
2081 j++;
2082 assert(j <= exportDir->NumberOfFunctions);
2085 free(map);
2087 if (NORMAL)
2088 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2089 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2091 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2093 dll_symbols[j].symbol = NULL;
2095 dll_current_symbol = dll_symbols;
2098 /*******************************************************************
2099 * dll_open
2101 * Open a DLL and read in exported symbols
2103 BOOL dll_open (const char *dll_name)
2105 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2108 /*******************************************************************
2109 * dll_next_symbol
2111 * Get next exported symbol from dll
2113 BOOL dll_next_symbol (parsed_symbol * sym)
2115 if (!dll_current_symbol || !dll_current_symbol->symbol)
2116 return FALSE;
2117 assert (dll_symbols);
2118 sym->symbol = strdup (dll_current_symbol->symbol);
2119 sym->ordinal = dll_current_symbol->ordinal;
2120 dll_current_symbol++;
2121 return TRUE;