msdasql/tests: Test for ITransaction* interfaces on a session.
[wine.git] / tools / winedump / pe.c
blob6ff14f957913985b1e640d966efcfee4a981d80e
1 /*
2 * PE dumping utility
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <fcntl.h>
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winedump.h"
35 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
37 static const IMAGE_NT_HEADERS32* PE_nt_headers;
39 static const char builtin_signature[] = "Wine builtin DLL";
40 static const char fakedll_signature[] = "Wine placeholder DLL";
41 static int is_builtin;
43 const char *get_machine_str(int mach)
45 switch (mach)
47 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
48 case IMAGE_FILE_MACHINE_I860: return "i860";
49 case IMAGE_FILE_MACHINE_I386: return "i386";
50 case IMAGE_FILE_MACHINE_R3000: return "R3000";
51 case IMAGE_FILE_MACHINE_R4000: return "R4000";
52 case IMAGE_FILE_MACHINE_R10000: return "R10000";
53 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
54 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
55 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
56 case IMAGE_FILE_MACHINE_IA64: return "IA64";
57 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
58 case IMAGE_FILE_MACHINE_ARM: return "ARM";
59 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
60 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
62 return "???";
65 static const void* RVA(unsigned long rva, unsigned long len)
67 IMAGE_SECTION_HEADER* sectHead;
68 int i;
70 if (rva == 0) return NULL;
72 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
73 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
75 if (sectHead[i].VirtualAddress <= rva &&
76 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
78 /* return image import directory offset */
79 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
83 return NULL;
86 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
88 const IMAGE_DOS_HEADER *dos;
89 dos = PRD(0, sizeof(*dos));
90 if (!dos) return NULL;
91 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
92 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
93 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
96 void print_fake_dll( void )
98 const IMAGE_DOS_HEADER *dos;
100 dos = PRD(0, sizeof(*dos) + 32);
101 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
103 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
104 printf( "*** This is a Wine builtin DLL ***\n\n" );
105 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
106 printf( "*** This is a Wine fake DLL ***\n\n" );
110 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
112 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
114 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
115 if (idx >= opt->NumberOfRvaAndSizes)
116 return NULL;
117 if(size)
118 *size = opt->DataDirectory[idx].Size;
119 return RVA(opt->DataDirectory[idx].VirtualAddress,
120 opt->DataDirectory[idx].Size);
122 else
124 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
125 if (idx >= opt->NumberOfRvaAndSizes)
126 return NULL;
127 if(size)
128 *size = opt->DataDirectory[idx].Size;
129 return RVA(opt->DataDirectory[idx].VirtualAddress,
130 opt->DataDirectory[idx].Size);
134 static const void* get_dir(unsigned idx)
136 return get_dir_and_size(idx, 0);
139 static const char * const DirectoryNames[16] = {
140 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
141 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
142 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
143 "IAT", "Delay IAT", "CLR Header", ""
146 static const char *get_magic_type(WORD magic)
148 switch(magic) {
149 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
150 return "32bit";
151 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
152 return "64bit";
153 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
154 return "ROM";
156 return "???";
159 static inline void print_word(const char *title, WORD value)
161 printf(" %-34s 0x%-4X %u\n", title, value, value);
164 static inline void print_dword(const char *title, DWORD value)
166 printf(" %-34s 0x%-8x %u\n", title, value, value);
169 static inline void print_longlong(const char *title, ULONGLONG value)
171 printf(" %-34s 0x", title);
172 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
173 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
174 else
175 printf("%lx\n", (unsigned long)value);
178 static inline void print_ver(const char *title, BYTE major, BYTE minor)
180 printf(" %-34s %u.%02u\n", title, major, minor);
183 static inline void print_subsys(const char *title, WORD value)
185 const char *str;
186 switch (value)
188 default:
189 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
190 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
191 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
192 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
193 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
194 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
195 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
196 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
197 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
198 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
199 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
200 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
201 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
202 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
204 printf(" %-34s 0x%X (%s)\n", title, value, str);
207 static inline void print_dllflags(const char *title, WORD value)
209 printf(" %-34s 0x%04X\n", title, value);
210 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
211 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
212 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
213 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
214 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
215 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
216 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
217 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
218 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
219 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
220 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
221 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
222 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
223 #undef X
226 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
228 unsigned i;
229 printf("Data Directory\n");
231 for (i = 0; i < n && i < 16; i++)
233 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
234 DirectoryNames[i], directory[i].VirtualAddress,
235 directory[i].Size);
239 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
241 IMAGE_OPTIONAL_HEADER32 oh;
242 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
244 /* in case optional header is missing or partial */
245 memset(&oh, 0, sizeof(oh));
246 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
247 optionalHeader = &oh;
249 print_word("Magic", optionalHeader->Magic);
250 print_ver("linker version",
251 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
252 print_dword("size of code", optionalHeader->SizeOfCode);
253 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
254 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
255 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
256 print_dword("base of code", optionalHeader->BaseOfCode);
257 print_dword("base of data", optionalHeader->BaseOfData);
258 print_dword("image base", optionalHeader->ImageBase);
259 print_dword("section align", optionalHeader->SectionAlignment);
260 print_dword("file align", optionalHeader->FileAlignment);
261 print_ver("required OS version",
262 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
263 print_ver("image version",
264 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
265 print_ver("subsystem version",
266 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
267 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
268 print_dword("size of image", optionalHeader->SizeOfImage);
269 print_dword("size of headers", optionalHeader->SizeOfHeaders);
270 print_dword("checksum", optionalHeader->CheckSum);
271 print_subsys("Subsystem", optionalHeader->Subsystem);
272 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
273 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
274 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
275 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
276 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
277 print_dword("loader flags", optionalHeader->LoaderFlags);
278 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
279 printf("\n");
280 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
281 printf("\n");
284 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
286 IMAGE_OPTIONAL_HEADER64 oh;
287 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
289 /* in case optional header is missing or partial */
290 memset(&oh, 0, sizeof(oh));
291 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
292 optionalHeader = &oh;
294 print_word("Magic", optionalHeader->Magic);
295 print_ver("linker version",
296 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
297 print_dword("size of code", optionalHeader->SizeOfCode);
298 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
299 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
300 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
301 print_dword("base of code", optionalHeader->BaseOfCode);
302 print_longlong("image base", optionalHeader->ImageBase);
303 print_dword("section align", optionalHeader->SectionAlignment);
304 print_dword("file align", optionalHeader->FileAlignment);
305 print_ver("required OS version",
306 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
307 print_ver("image version",
308 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
309 print_ver("subsystem version",
310 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
311 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
312 print_dword("size of image", optionalHeader->SizeOfImage);
313 print_dword("size of headers", optionalHeader->SizeOfHeaders);
314 print_dword("checksum", optionalHeader->CheckSum);
315 print_subsys("Subsystem", optionalHeader->Subsystem);
316 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
317 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
318 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
319 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
320 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
321 print_dword("loader flags", optionalHeader->LoaderFlags);
322 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
323 printf("\n");
324 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
325 printf("\n");
328 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
330 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
332 switch(optionalHeader->Magic) {
333 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
334 dump_optional_header32(optionalHeader, header_size);
335 break;
336 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
337 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
338 break;
339 default:
340 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
341 break;
345 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
347 printf("File Header\n");
349 printf(" Machine: %04X (%s)\n",
350 fileHeader->Machine, get_machine_str(fileHeader->Machine));
351 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
352 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
353 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
354 Offset(&(fileHeader->TimeDateStamp)));
355 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
356 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
357 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
358 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
359 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
360 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
361 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
362 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
363 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
364 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
365 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
366 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
367 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
368 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
369 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
370 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
371 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
372 X(IMAGE_FILE_SYSTEM, "SYSTEM");
373 X(IMAGE_FILE_DLL, "DLL");
374 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
375 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
376 #undef X
377 printf("\n");
380 static void dump_pe_header(void)
382 dump_file_header(&PE_nt_headers->FileHeader);
383 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
386 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
388 unsigned offset;
390 /* long section name ? */
391 if (strtable && sectHead->Name[0] == '/' &&
392 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
393 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
394 else
395 printf(" %-8.8s", sectHead->Name);
396 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
397 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
398 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
399 sectHead->PointerToRawData, sectHead->SizeOfRawData);
400 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
401 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
402 printf(" line # offs: %-8u line #'s: %-8u\n",
403 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
404 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
405 printf(" ");
406 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
407 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
408 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
409 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
410 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
411 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
412 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
414 X(IMAGE_SCN_CNT_CODE, "CODE");
415 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
416 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
418 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
419 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
420 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
421 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
422 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
424 /* 0x00002000 - Reserved */
425 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
426 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
428 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
429 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
430 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
431 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
432 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
434 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
436 #define X2(b,s) case b: printf(" " s); break
437 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
438 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
439 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
440 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
441 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
442 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
443 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
444 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
445 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
446 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
447 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
448 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
449 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
450 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
451 #undef X2
454 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
456 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
457 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
458 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
459 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
460 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
461 X(IMAGE_SCN_MEM_READ, "MEM_READ");
462 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
463 #undef X
464 printf("\n\n");
467 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
469 const IMAGE_SECTION_HEADER* sectHead = addr;
470 unsigned i;
471 const char* strtable;
473 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
475 strtable = (const char*)base +
476 PE_nt_headers->FileHeader.PointerToSymbolTable +
477 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
479 else strtable = NULL;
481 printf("Section Table\n");
482 for (i = 0; i < num_sect; i++, sectHead++)
484 dump_section(sectHead, strtable);
486 if (globals.do_dump_rawdata)
488 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
489 printf("\n");
494 static void dump_dir_exported_functions(void)
496 unsigned int size = 0;
497 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
498 unsigned int i;
499 const DWORD* pFunc;
500 const DWORD* pName;
501 const WORD* pOrdl;
502 DWORD* funcs;
504 if (!exportDir) return;
506 printf("Exports table:\n");
507 printf("\n");
508 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
509 printf(" Characteristics: %08x\n", exportDir->Characteristics);
510 printf(" TimeDateStamp: %08X %s\n",
511 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
512 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
513 printf(" Ordinal base: %u\n", exportDir->Base);
514 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
515 printf(" # of Names: %u\n", exportDir->NumberOfNames);
516 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
517 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
518 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
519 printf("\n");
520 printf(" Entry Pt Ordn Name\n");
522 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
523 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
524 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
525 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
527 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
528 if (!funcs) fatal("no memory");
530 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
532 for (i = 0; i < exportDir->NumberOfFunctions; i++)
534 if (!pFunc[i]) continue;
535 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
536 if (funcs[i])
537 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
538 else
539 printf("<by ordinal>");
541 /* check for forwarded function */
542 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
543 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
544 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
545 printf("\n");
547 free(funcs);
548 printf("\n");
552 struct runtime_function_x86_64
554 DWORD BeginAddress;
555 DWORD EndAddress;
556 DWORD UnwindData;
559 struct runtime_function_armnt
561 DWORD BeginAddress;
562 union {
563 DWORD UnwindData;
564 struct {
565 DWORD Flag : 2;
566 DWORD FunctionLength : 11;
567 DWORD Ret : 2;
568 DWORD H : 1;
569 DWORD Reg : 3;
570 DWORD R : 1;
571 DWORD L : 1;
572 DWORD C : 1;
573 DWORD StackAdjust : 10;
574 } DUMMYSTRUCTNAME;
575 } DUMMYUNIONNAME;
578 struct runtime_function_arm64
580 DWORD BeginAddress;
581 union
583 DWORD UnwindData;
584 struct
586 DWORD Flag : 2;
587 DWORD FunctionLength : 11;
588 DWORD RegF : 3;
589 DWORD RegI : 4;
590 DWORD H : 1;
591 DWORD CR : 2;
592 DWORD FrameSize : 9;
593 } DUMMYSTRUCTNAME;
594 } DUMMYUNIONNAME;
597 union handler_data
599 struct runtime_function_x86_64 chain;
600 DWORD handler;
603 struct opcode
605 BYTE offset;
606 BYTE code : 4;
607 BYTE info : 4;
610 struct unwind_info_x86_64
612 BYTE version : 3;
613 BYTE flags : 5;
614 BYTE prolog;
615 BYTE count;
616 BYTE frame_reg : 4;
617 BYTE frame_offset : 4;
618 struct opcode opcodes[1]; /* count entries */
619 /* followed by union handler_data */
622 struct unwind_info_armnt
624 DWORD function_length : 18;
625 DWORD version : 2;
626 DWORD x : 1;
627 DWORD e : 1;
628 DWORD f : 1;
629 DWORD count : 5;
630 DWORD words : 4;
633 struct unwind_info_ext_armnt
635 WORD excount;
636 BYTE exwords;
637 BYTE reserved;
640 struct unwind_info_epilogue_armnt
642 DWORD offset : 18;
643 DWORD res : 2;
644 DWORD cond : 4;
645 DWORD index : 8;
648 #define UWOP_PUSH_NONVOL 0
649 #define UWOP_ALLOC_LARGE 1
650 #define UWOP_ALLOC_SMALL 2
651 #define UWOP_SET_FPREG 3
652 #define UWOP_SAVE_NONVOL 4
653 #define UWOP_SAVE_NONVOL_FAR 5
654 #define UWOP_SAVE_XMM128 8
655 #define UWOP_SAVE_XMM128_FAR 9
656 #define UWOP_PUSH_MACHFRAME 10
658 #define UNW_FLAG_EHANDLER 1
659 #define UNW_FLAG_UHANDLER 2
660 #define UNW_FLAG_CHAININFO 4
662 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
664 static const char * const reg_names[16] =
665 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
666 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
668 const union handler_data *handler_data;
669 const struct unwind_info_x86_64 *info;
670 unsigned int i, count;
672 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
673 if (function->UnwindData & 1)
675 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
676 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
677 return;
679 info = RVA( function->UnwindData, sizeof(*info) );
681 printf( " unwind info at %08x\n", function->UnwindData );
682 if (info->version != 1)
684 printf( " *** unknown version %u\n", info->version );
685 return;
687 printf( " flags %x", info->flags );
688 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
689 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
690 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
691 printf( "\n prolog 0x%x bytes\n", info->prolog );
693 if (info->frame_reg)
694 printf( " frame register %s offset 0x%x(%%rsp)\n",
695 reg_names[info->frame_reg], info->frame_offset * 16 );
697 for (i = 0; i < info->count; i++)
699 printf( " 0x%02x: ", info->opcodes[i].offset );
700 switch (info->opcodes[i].code)
702 case UWOP_PUSH_NONVOL:
703 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
704 break;
705 case UWOP_ALLOC_LARGE:
706 if (info->opcodes[i].info)
708 count = *(const DWORD *)&info->opcodes[i+1];
709 i += 2;
711 else
713 count = *(const USHORT *)&info->opcodes[i+1] * 8;
714 i++;
716 printf( "sub $0x%x,%%rsp\n", count );
717 break;
718 case UWOP_ALLOC_SMALL:
719 count = (info->opcodes[i].info + 1) * 8;
720 printf( "sub $0x%x,%%rsp\n", count );
721 break;
722 case UWOP_SET_FPREG:
723 printf( "lea 0x%x(%%rsp),%s\n",
724 info->frame_offset * 16, reg_names[info->frame_reg] );
725 break;
726 case UWOP_SAVE_NONVOL:
727 count = *(const USHORT *)&info->opcodes[i+1] * 8;
728 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
729 i++;
730 break;
731 case UWOP_SAVE_NONVOL_FAR:
732 count = *(const DWORD *)&info->opcodes[i+1];
733 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
734 i += 2;
735 break;
736 case UWOP_SAVE_XMM128:
737 count = *(const USHORT *)&info->opcodes[i+1] * 16;
738 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
739 i++;
740 break;
741 case UWOP_SAVE_XMM128_FAR:
742 count = *(const DWORD *)&info->opcodes[i+1];
743 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
744 i += 2;
745 break;
746 case UWOP_PUSH_MACHFRAME:
747 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
748 break;
749 default:
750 printf( "*** unknown code %u\n", info->opcodes[i].code );
751 break;
755 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
756 if (info->flags & UNW_FLAG_CHAININFO)
758 printf( " -> function %08x-%08x\n",
759 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
760 return;
762 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
763 printf( " handler %08x data at %08x\n", handler_data->handler,
764 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
767 static const BYTE armnt_code_lengths[256] =
769 /* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
770 /* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
771 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
772 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
773 /* 80 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
774 /* a0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
775 /* c0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
776 /* e0 */ 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,4,3,4,1,1,1,1,1
779 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
781 const struct unwind_info_armnt *info;
782 const struct unwind_info_ext_armnt *infoex;
783 const struct unwind_info_epilogue_armnt *infoepi;
784 unsigned int rva;
785 WORD i, count = 0, words = 0;
787 if (fnc->u.s.Flag)
789 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
790 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->u.s.StackAdjust;
792 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
793 (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
794 printf( " Flag %x\n", fnc->u.s.Flag );
795 printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
796 printf( " Ret %x\n", fnc->u.s.Ret );
797 printf( " H %x\n", fnc->u.s.H );
798 printf( " Reg %x\n", fnc->u.s.Reg );
799 printf( " R %x\n", fnc->u.s.R );
800 printf( " L %x\n", fnc->u.s.L );
801 printf( " C %x\n", fnc->u.s.C );
802 printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
804 if (fnc->u.s.StackAdjust >= 0x03f4)
806 pf = fnc->u.s.StackAdjust & 0x04;
807 ef = fnc->u.s.StackAdjust & 0x08;
808 stack = (fnc->u.s.StackAdjust & 3) + 1;
811 if (!fnc->u.s.R || pf)
813 int first = 4, last = fnc->u.s.Reg + 4;
814 if (pf)
816 first = (~fnc->u.s.StackAdjust) & 3;
817 if (fnc->u.s.R)
818 last = 3;
820 if (first == last)
821 sprintf(intregs, "r%u", first);
822 else
823 sprintf(intregs, "r%u-r%u", first, last);
824 fpoffset = last + 1 - first;
827 if (!fnc->u.s.R || ef)
829 int first = 4, last = fnc->u.s.Reg + 4;
830 if (ef)
832 first = (~fnc->u.s.StackAdjust) & 3;
833 if (fnc->u.s.R)
834 last = 3;
836 if (first == last)
837 sprintf(intregspop, "r%u", first);
838 else
839 sprintf(intregspop, "r%u-r%u", first, last);
842 if (fnc->u.s.C)
844 if (intregs[0])
845 strcat(intregs, ", ");
846 if (intregspop[0])
847 strcat(intregspop, ", ");
848 strcat(intregs, "r11");
849 strcat(intregspop, "r11");
851 if (fnc->u.s.L)
853 if (intregs[0])
854 strcat(intregs, ", ");
855 strcat(intregs, "lr");
857 if (intregspop[0] && (fnc->u.s.Ret != 0 || !fnc->u.s.H))
858 strcat(intregspop, ", ");
859 if (fnc->u.s.Ret != 0)
860 strcat(intregspop, "lr");
861 else if (!fnc->u.s.H)
862 strcat(intregspop, "pc");
865 if (fnc->u.s.R)
867 if (fnc->u.s.Reg)
868 sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
869 else
870 strcpy(vfpregs, "d8");
873 if (fnc->u.s.Flag == 1) {
874 if (fnc->u.s.H)
875 printf( " Unwind Code\tpush {r0-r3}\n" );
877 if (intregs[0])
878 printf( " Unwind Code\tpush {%s}\n", intregs );
880 if (fnc->u.s.C && fpoffset == 0)
881 printf( " Unwind Code\tmov r11, sp\n" );
882 else if (fnc->u.s.C)
883 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
885 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
886 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
888 if (stack && !pf)
889 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
892 if (fnc->u.s.Ret == 3)
893 return;
894 printf( "Epilogue:\n" );
896 if (stack && !ef)
897 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
899 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
900 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
902 if (intregspop[0])
903 printf( " Unwind Code\tpop {%s}\n", intregspop );
905 if (fnc->u.s.H && !(fnc->u.s.L && fnc->u.s.Ret == 0))
906 printf( " Unwind Code\tadd sp, sp, #16\n" );
907 else if (fnc->u.s.H && (fnc->u.s.L && fnc->u.s.Ret == 0))
908 printf( " Unwind Code\tldr pc, [sp], #20\n" );
910 if (fnc->u.s.Ret == 1)
911 printf( " Unwind Code\tbx <reg>\n" );
912 else if (fnc->u.s.Ret == 2)
913 printf( " Unwind Code\tb <address>\n" );
915 return;
918 info = RVA( fnc->u.UnwindData, sizeof(*info) );
919 rva = fnc->u.UnwindData + sizeof(*info);
920 count = info->count;
921 words = info->words;
923 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
924 (fnc->BeginAddress & ~1) + info->function_length * 2 );
925 printf( " unwind info at %08x\n", fnc->u.UnwindData );
926 printf( " Flag %x\n", fnc->u.s.Flag );
927 printf( " FunctionLength %x\n", info->function_length );
928 printf( " Version %x\n", info->version );
929 printf( " X %x\n", info->x );
930 printf( " E %x\n", info->e );
931 printf( " F %x\n", info->f );
932 printf( " Count %x\n", count );
933 printf( " Words %x\n", words );
935 if (!info->count && !info->words)
937 infoex = RVA( rva, sizeof(*infoex) );
938 rva = rva + sizeof(*infoex);
939 count = infoex->excount;
940 words = infoex->exwords;
941 printf( " ExtCount %x\n", count );
942 printf( " ExtWords %x\n", words );
945 if (!info->e)
947 infoepi = RVA( rva, count * sizeof(*infoepi) );
948 rva = rva + count * sizeof(*infoepi);
950 for (i = 0; i < count; i++)
952 printf( " Epilogue Scope %x\n", i );
953 printf( " Offset %x\n", infoepi[i].offset );
954 printf( " Reserved %x\n", infoepi[i].res );
955 printf( " Condition %x\n", infoepi[i].cond );
956 printf( " Index %x\n", infoepi[i].index );
959 else
960 infoepi = NULL;
962 if (words)
964 const unsigned int *codes;
965 BYTE b, *bytes;
966 BOOL inepilogue = FALSE;
968 codes = RVA( rva, words * sizeof(*codes) );
969 rva = rva + words * sizeof(*codes);
970 bytes = (BYTE*)codes;
972 for (b = 0; b < words * sizeof(*codes); b++)
974 BYTE code = bytes[b];
975 BYTE len = armnt_code_lengths[code];
977 if (info->e && b == count)
979 printf( "Epilogue:\n" );
980 inepilogue = TRUE;
982 else if (!info->e && infoepi)
984 for (i = 0; i < count; i++)
985 if (b == infoepi[i].index)
987 printf( "Epilogue from Scope %x at %08x:\n", i,
988 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
989 inepilogue = TRUE;
993 printf( " Unwind Code");
994 for (i = 0; i < len; i++)
995 printf( " %02x", bytes[b+i] );
996 printf( "\t" );
998 if (code == 0x00)
999 printf( "\n" );
1000 else if (code <= 0x7f)
1001 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1002 else if (code <= 0xbf)
1004 WORD excode, f;
1005 BOOL first = TRUE;
1006 BYTE excodes = bytes[++b];
1008 excode = (code << 8) | excodes;
1009 printf( "%s {", inepilogue ? "pop" : "push" );
1011 for (f = 0; f <= 12; f++)
1013 if ((excode >> f) & 1)
1015 printf( "%sr%u", first ? "" : ", ", f );
1016 first = FALSE;
1020 if (excode & 0x2000)
1021 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1023 printf( "}\n" );
1025 else if (code <= 0xcf)
1026 if (inepilogue)
1027 printf( "mov sp, r%u\n", code & 0x0f );
1028 else
1029 printf( "mov r%u, sp\n", code & 0x0f );
1030 else if (code <= 0xd7)
1031 if (inepilogue)
1032 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1033 else
1034 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1035 else if (code <= 0xdf)
1036 if (inepilogue)
1037 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1038 else
1039 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1040 else if (code <= 0xe7)
1041 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1042 else if (code <= 0xeb)
1044 WORD excode;
1045 BYTE excodes = bytes[++b];
1047 excode = (code << 8) | excodes;
1048 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1050 else if (code <= 0xed)
1052 WORD excode, f;
1053 BOOL first = TRUE;
1054 BYTE excodes = bytes[++b];
1056 excode = (code << 8) | excodes;
1057 printf( "%s {", inepilogue ? "pop" : "push" );
1059 for (f = 0; f < 8; f++)
1061 if ((excode >> f) & 1)
1063 printf( "%sr%u", first ? "" : ", ", f );
1064 first = FALSE;
1068 if (excode & 0x0100)
1069 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1071 printf( "}\n" );
1073 else if (code == 0xee)
1074 printf( "unknown 16\n" );
1075 else if (code == 0xef)
1077 WORD excode;
1078 BYTE excodes = bytes[++b];
1080 if (excodes <= 0x0f)
1082 excode = (code << 8) | excodes;
1083 if (inepilogue)
1084 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1085 else
1086 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1088 else
1089 printf( "unknown 32\n" );
1091 else if (code <= 0xf4)
1092 printf( "unknown\n" );
1093 else if (code <= 0xf6)
1095 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1096 BYTE excodes = bytes[++b];
1098 excode = (code << 8) | excodes;
1099 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1100 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1102 else if (code <= 0xf7)
1104 unsigned int excode;
1105 BYTE excodes[2];
1107 excodes[0] = bytes[++b];
1108 excodes[1] = bytes[++b];
1109 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1110 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1112 else if (code <= 0xf8)
1114 unsigned int excode;
1115 BYTE excodes[3];
1117 excodes[0] = bytes[++b];
1118 excodes[1] = bytes[++b];
1119 excodes[2] = bytes[++b];
1120 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1121 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1123 else if (code <= 0xf9)
1125 unsigned int excode;
1126 BYTE excodes[2];
1128 excodes[0] = bytes[++b];
1129 excodes[1] = bytes[++b];
1130 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1131 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1133 else if (code <= 0xfa)
1135 unsigned int excode;
1136 BYTE excodes[3];
1138 excodes[0] = bytes[++b];
1139 excodes[1] = bytes[++b];
1140 excodes[2] = bytes[++b];
1141 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1142 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1144 else if (code <= 0xfb)
1145 printf( "nop\n" );
1146 else if (code <= 0xfc)
1147 printf( "nop.w\n" );
1148 else if (code <= 0xfd)
1150 printf( "(end) nop\n" );
1151 inepilogue = TRUE;
1153 else if (code <= 0xfe)
1155 printf( "(end) nop.w\n" );
1156 inepilogue = TRUE;
1158 else
1160 printf( "end\n" );
1161 inepilogue = TRUE;
1166 if (info->x)
1168 const unsigned int *handler;
1170 handler = RVA( rva, sizeof(*handler) );
1171 rva = rva + sizeof(*handler);
1173 printf( " handler %08x data at %08x\n", *handler, rva);
1177 struct unwind_info_arm64
1179 DWORD function_length : 18;
1180 DWORD version : 2;
1181 DWORD x : 1;
1182 DWORD e : 1;
1183 DWORD epilog : 5;
1184 DWORD codes : 5;
1187 struct unwind_info_ext_arm64
1189 WORD epilog;
1190 BYTE codes;
1191 BYTE reserved;
1194 struct unwind_info_epilog_arm64
1196 DWORD offset : 18;
1197 DWORD res : 4;
1198 DWORD index : 10;
1201 static const BYTE code_lengths[256] =
1203 /* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1204 /* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1205 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1206 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1207 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1208 /* a0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1209 /* c0 */ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
1210 /* e0 */ 4,1,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1213 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1215 unsigned int i, j;
1217 for (i = 0; i < count; i += code_lengths[ptr[i]])
1219 BYTE len = code_lengths[ptr[i]];
1220 unsigned int val = ptr[i];
1221 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1222 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1224 printf( " %04x: ", i );
1225 for (j = 0; j < 4; j++)
1226 if (j < len) printf( "%02x ", ptr[i+j] );
1227 else printf( " " );
1229 if (ptr[i] < 0x20) /* alloc_s */
1231 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1233 else if (ptr[i] < 0x40) /* save_r19r20_x */
1235 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1237 else if (ptr[i] < 0x80) /* save_fplr */
1239 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1241 else if (ptr[i] < 0xc0) /* save_fplr_x */
1243 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1245 else if (ptr[i] < 0xc8) /* alloc_m */
1247 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1249 else if (ptr[i] < 0xcc) /* save_regp */
1251 int reg = 19 + ((val >> 6) & 0xf);
1252 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1254 else if (ptr[i] < 0xd0) /* save_regp_x */
1256 int reg = 19 + ((val >> 6) & 0xf);
1257 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1259 else if (ptr[i] < 0xd4) /* save_reg */
1261 int reg = 19 + ((val >> 6) & 0xf);
1262 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1264 else if (ptr[i] < 0xd6) /* save_reg_x */
1266 int reg = 19 + ((val >> 5) & 0xf);
1267 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1269 else if (ptr[i] < 0xd8) /* save_lrpair */
1271 int reg = 19 + 2 * ((val >> 6) & 0x7);
1272 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1274 else if (ptr[i] < 0xda) /* save_fregp */
1276 int reg = 8 + ((val >> 6) & 0x7);
1277 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1279 else if (ptr[i] < 0xdc) /* save_fregp_x */
1281 int reg = 8 + ((val >> 6) & 0x7);
1282 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1284 else if (ptr[i] < 0xde) /* save_freg */
1286 int reg = 8 + ((val >> 6) & 0x7);
1287 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1289 else if (ptr[i] == 0xde) /* save_freg_x */
1291 int reg = 8 + ((val >> 5) & 0x7);
1292 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1294 else if (ptr[i] == 0xe0) /* alloc_l */
1296 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1298 else if (ptr[i] == 0xe1) /* set_fp */
1300 printf( "mov x29,sp\n" );
1302 else if (ptr[i] == 0xe2) /* add_fp */
1304 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1306 else if (ptr[i] == 0xe3) /* nop */
1308 printf( "nop\n" );
1310 else if (ptr[i] == 0xe4) /* end */
1312 printf( "end\n" );
1314 else if (ptr[i] == 0xe5) /* end_c */
1316 printf( "end_c\n" );
1318 else if (ptr[i] == 0xe6) /* save_next */
1320 printf( "save_next\n" );
1322 else if (ptr[i] == 0xe7) /* arithmetic */
1324 switch ((val >> 4) & 0x0f)
1326 case 0: printf( "add lr,lr,x28\n" ); break;
1327 case 1: printf( "add lr,lr,sp\n" ); break;
1328 case 2: printf( "sub lr,lr,x28\n" ); break;
1329 case 3: printf( "sub lr,lr,sp\n" ); break;
1330 case 4: printf( "eor lr,lr,x28\n" ); break;
1331 case 5: printf( "eor lr,lr,sp\n" ); break;
1332 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1333 case 8: printf( "ror lr,lr,x28\n" ); break;
1334 case 9: printf( "ror lr,lr,sp\n" ); break;
1335 default:printf( "unknown op\n" ); break;
1338 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1340 printf( "MSFT_OP_TRAP_FRAME\n" );
1342 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1344 printf( "MSFT_OP_MACHINE_FRAME\n" );
1346 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1348 printf( "MSFT_OP_CONTEXT\n" );
1350 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1352 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1354 else printf( "??\n");
1358 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1360 int i, pos = 0, intsz = func->u.s.RegI * 8, fpsz = func->u.s.RegF * 8, savesz, locsz;
1362 if (func->u.s.CR == 1) intsz += 8;
1363 if (func->u.s.RegF) fpsz += 8;
1365 savesz = ((intsz + fpsz + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
1366 locsz = func->u.s.FrameSize * 16 - savesz;
1368 switch (func->u.s.CR)
1370 case 3:
1371 printf( " %04x: mov x29,sp\n", pos++ );
1372 if (locsz <= 512)
1374 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1375 break;
1377 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1378 /* fall through */
1379 case 0:
1380 case 1:
1381 if (locsz <= 4080)
1383 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1385 else
1387 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1388 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1390 break;
1393 if (func->u.s.H)
1395 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1396 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1397 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1398 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1401 if (func->u.s.RegF)
1403 if (func->u.s.RegF % 2 == 0)
1404 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->u.s.RegF, intsz + fpsz - 8 );
1405 for (i = (func->u.s.RegF - 1)/ 2; i >= 0; i--)
1407 if (!i && !intsz)
1408 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1409 else
1410 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1414 switch (func->u.s.RegI)
1416 case 0:
1417 if (func->u.s.CR == 1)
1418 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1419 break;
1420 case 1:
1421 if (func->u.s.CR == 1)
1422 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1423 else
1424 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1425 break;
1426 default:
1427 if (func->u.s.RegI % 2)
1429 if (func->u.s.CR == 1)
1430 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1431 else
1432 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1434 else if (func->u.s.CR == 1)
1435 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1437 for (i = func->u.s.RegI / 2 - 1; i >= 0; i--)
1438 if (i)
1439 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1440 else
1441 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1442 break;
1444 printf( " %04x: end\n", pos );
1447 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1449 const struct unwind_info_arm64 *info;
1450 const struct unwind_info_ext_arm64 *infoex;
1451 const struct unwind_info_epilog_arm64 *infoepi;
1452 const BYTE *ptr;
1453 unsigned int i, rva, codes, epilogs;
1455 if (func->u.s.Flag)
1457 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1458 func->BeginAddress + func->u.s.FunctionLength * 4 );
1459 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1460 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
1461 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
1462 dump_arm64_packed_info( func );
1463 return;
1466 rva = func->u.UnwindData;
1467 info = RVA( rva, sizeof(*info) );
1468 rva += sizeof(*info);
1469 epilogs = info->epilog;
1470 codes = info->codes;
1472 if (!codes)
1474 infoex = RVA( rva, sizeof(*infoex) );
1475 rva = rva + sizeof(*infoex);
1476 codes = infoex->codes;
1477 epilogs = infoex->epilog;
1479 printf( "\nFunction %08x-%08x:\n",
1480 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1481 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1482 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1483 if (info->e)
1485 printf( " epilog 0: code=%04x\n", info->epilog );
1487 else
1489 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1490 rva += sizeof(*infoepi) * epilogs;
1491 for (i = 0; i < epilogs; i++)
1492 printf( " epilog %u: pc=%08x code=%04x\n", i,
1493 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1495 ptr = RVA( rva, codes * 4);
1496 rva += codes * 4;
1497 if (info->x)
1499 const DWORD *handler = RVA( rva, sizeof(*handler) );
1500 rva += sizeof(*handler);
1501 printf( " handler: %08x data %08x\n", *handler, rva );
1503 dump_arm64_codes( ptr, codes * 4 );
1506 static void dump_dir_exceptions(void)
1508 unsigned int i, size = 0;
1509 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1510 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1512 if (!funcs) return;
1514 switch (file_header->Machine)
1516 case IMAGE_FILE_MACHINE_AMD64:
1517 size /= sizeof(struct runtime_function_x86_64);
1518 printf( "Exception info (%u functions):\n", size );
1519 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1520 break;
1521 case IMAGE_FILE_MACHINE_ARMNT:
1522 size /= sizeof(struct runtime_function_armnt);
1523 printf( "Exception info (%u functions):\n", size );
1524 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1525 break;
1526 case IMAGE_FILE_MACHINE_ARM64:
1527 size /= sizeof(struct runtime_function_arm64);
1528 printf( "Exception info (%u functions):\n", size );
1529 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1530 break;
1531 default:
1532 printf( "Exception information not supported for %s binaries\n",
1533 get_machine_str(file_header->Machine));
1534 break;
1539 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, DWORD thunk_rva)
1541 /* FIXME: This does not properly handle large images */
1542 const IMAGE_IMPORT_BY_NAME* iibn;
1543 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1545 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1546 printf(" %08x %4u <by ordinal>\n", thunk_rva, (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
1547 else
1549 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1550 if (!iibn)
1551 printf("Can't grab import by name info, skipping to next ordinal\n");
1552 else
1553 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1558 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, DWORD thunk_rva)
1560 const IMAGE_IMPORT_BY_NAME* iibn;
1561 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(DWORD))
1563 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1564 printf(" %08x %4u <by ordinal>\n", thunk_rva, IMAGE_ORDINAL32(il->u1.Ordinal));
1565 else
1567 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1568 if (!iibn)
1569 printf("Can't grab import by name info, skipping to next ordinal\n");
1570 else
1571 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1576 static void dump_dir_imported_functions(void)
1578 unsigned directorySize;
1579 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1581 if (!importDesc) return;
1583 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1585 for (;;)
1587 const IMAGE_THUNK_DATA32* il;
1589 if (!importDesc->Name || !importDesc->FirstThunk) break;
1591 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1592 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
1593 printf(" TimeDateStamp: %08X (%s)\n",
1594 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1595 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
1596 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
1598 printf(" Thunk Ordn Name\n");
1600 il = (importDesc->u.OriginalFirstThunk != 0) ?
1601 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
1602 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1604 if (!il)
1605 printf("Can't grab thunk data, going to next imported DLL\n");
1606 else
1608 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1609 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1610 else
1611 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1612 printf("\n");
1614 importDesc++;
1616 printf("\n");
1619 static void dump_dir_loadconfig(void)
1621 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1622 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1624 if (!loadcfg32) return;
1626 printf( "Loadconfig\n" );
1627 print_dword( "Size", loadcfg32->Size );
1628 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1629 print_word( "MajorVersion", loadcfg32->MajorVersion );
1630 print_word( "MinorVersion", loadcfg32->MinorVersion );
1631 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1632 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1633 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1635 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1637 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1638 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1639 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1640 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1641 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1642 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1643 print_word( "CSDVersion", loadcfg64->CSDVersion );
1644 print_word( "Reserved", loadcfg64->Reserved1 );
1645 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1646 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1647 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1649 else
1651 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1652 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1653 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1654 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1655 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1656 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1657 print_word( "CSDVersion", loadcfg32->CSDVersion );
1658 print_word( "Reserved", loadcfg32->Reserved1 );
1659 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1660 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1661 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1665 static void dump_dir_delay_imported_functions(void)
1667 unsigned directorySize;
1668 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1670 if (!importDesc) return;
1672 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1674 for (;;)
1676 const IMAGE_THUNK_DATA32* il;
1677 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1679 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1681 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1682 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1683 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1684 printf(" Address Table: %08x\n", importDesc->ImportAddressTableRVA);
1685 printf(" TimeDateStamp: %08X (%s)\n",
1686 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1688 printf(" Thunk Ordn Name\n");
1690 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1692 if (!il)
1693 printf("Can't grab thunk data, going to next imported DLL\n");
1694 else
1696 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1697 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1698 else
1699 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1700 printf("\n");
1702 importDesc++;
1704 printf("\n");
1707 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1709 const char* str;
1711 printf("Directory %02u\n", idx + 1);
1712 printf(" Characteristics: %08X\n", idd->Characteristics);
1713 printf(" TimeDateStamp: %08X %s\n",
1714 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1715 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1716 switch (idd->Type)
1718 default:
1719 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1720 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1721 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1722 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1723 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1724 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1725 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1726 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1727 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1728 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1729 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1730 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1731 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1732 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1733 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1734 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1735 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
1737 printf(" Type: %u (%s)\n", idd->Type, str);
1738 printf(" SizeOfData: %u\n", idd->SizeOfData);
1739 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1740 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1742 switch (idd->Type)
1744 case IMAGE_DEBUG_TYPE_UNKNOWN:
1745 break;
1746 case IMAGE_DEBUG_TYPE_COFF:
1747 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1748 IMAGE_FIRST_SECTION(PE_nt_headers));
1749 break;
1750 case IMAGE_DEBUG_TYPE_CODEVIEW:
1751 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1752 break;
1753 case IMAGE_DEBUG_TYPE_FPO:
1754 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1755 break;
1756 case IMAGE_DEBUG_TYPE_MISC:
1758 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1759 if (!misc) {printf("Can't get misc debug information\n"); break;}
1760 printf(" DataType: %u (%s)\n",
1761 misc->DataType,
1762 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1763 printf(" Length: %u\n", misc->Length);
1764 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1765 printf(" Data: %s\n", misc->Data);
1767 break;
1768 default: break;
1770 printf("\n");
1773 static void dump_dir_debug(void)
1775 unsigned nb_dbg, i;
1776 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1778 nb_dbg /= sizeof(*debugDir);
1779 if (!debugDir || !nb_dbg) return;
1781 printf("Debug Table (%u directories)\n", nb_dbg);
1783 for (i = 0; i < nb_dbg; i++)
1785 dump_dir_debug_dir(debugDir, i);
1786 debugDir++;
1788 printf("\n");
1791 static inline void print_clrflags(const char *title, DWORD value)
1793 printf(" %-34s 0x%X\n", title, value);
1794 #define X(f,s) if (value & f) printf(" %s\n", s)
1795 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1796 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1797 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1798 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1799 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1800 #undef X
1803 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1805 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1808 static void dump_dir_clr_header(void)
1810 unsigned int size = 0;
1811 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1813 if (!dir) return;
1815 printf( "CLR Header\n" );
1816 print_dword( "Header Size", dir->cb );
1817 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1818 print_clrflags( "Flags", dir->Flags );
1819 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1820 printf("\n");
1821 printf( "CLR Data Directory\n" );
1822 print_clrdirectory( "MetaData", &dir->MetaData );
1823 print_clrdirectory( "Resources", &dir->Resources );
1824 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1825 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1826 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1827 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1828 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1829 printf("\n");
1832 static void dump_dir_reloc(void)
1834 unsigned int i, size = 0;
1835 const USHORT *relocs;
1836 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1837 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1838 static const char * const names[] =
1840 "BASED_ABSOLUTE",
1841 "BASED_HIGH",
1842 "BASED_LOW",
1843 "BASED_HIGHLOW",
1844 "BASED_HIGHADJ",
1845 "BASED_MIPS_JMPADDR",
1846 "BASED_SECTION",
1847 "BASED_REL",
1848 "unknown 8",
1849 "BASED_IA64_IMM64",
1850 "BASED_DIR64",
1851 "BASED_HIGH3ADJ",
1852 "unknown 12",
1853 "unknown 13",
1854 "unknown 14",
1855 "unknown 15"
1858 if (!rel) return;
1860 printf( "Relocations\n" );
1861 while (rel < end - 1 && rel->SizeOfBlock)
1863 printf( " Page %x\n", rel->VirtualAddress );
1864 relocs = (const USHORT *)(rel + 1);
1865 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1866 while (i--)
1868 USHORT offset = *relocs & 0xfff;
1869 int type = *relocs >> 12;
1870 printf( " off %04x type %s\n", offset, names[type] );
1871 relocs++;
1873 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1875 printf("\n");
1878 static void dump_dir_tls(void)
1880 IMAGE_TLS_DIRECTORY64 dir;
1881 const DWORD *callbacks;
1882 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1884 if (!pdir) return;
1886 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1887 memcpy(&dir, pdir, sizeof(dir));
1888 else
1890 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1891 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1892 dir.AddressOfIndex = pdir->AddressOfIndex;
1893 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1894 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1895 dir.Characteristics = pdir->Characteristics;
1898 /* FIXME: This does not properly handle large images */
1899 printf( "Thread Local Storage\n" );
1900 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1901 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1902 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1903 (DWORD)dir.SizeOfZeroFill );
1904 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1905 printf( " Characteristics %08x\n", dir.Characteristics );
1906 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1907 if (dir.AddressOfCallBacks)
1909 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1910 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1912 printf( " %08x", *callbacks );
1913 addr += sizeof(DWORD);
1916 printf(" }\n\n");
1919 enum FileSig get_kind_dbg(void)
1921 const WORD* pw;
1923 pw = PRD(0, sizeof(WORD));
1924 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1926 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1927 return SIG_UNKNOWN;
1930 void dbg_dump(void)
1932 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1933 unsigned nb_dbg;
1934 unsigned i;
1935 const IMAGE_DEBUG_DIRECTORY* debugDir;
1937 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1938 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1940 printf ("Signature: %.2s (0x%4X)\n",
1941 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1942 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1943 printf ("Machine: 0x%04X (%s)\n",
1944 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1945 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1946 printf ("TimeDateStamp: 0x%08X (%s)\n",
1947 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1948 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1949 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1950 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1951 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1952 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1953 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1955 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1956 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1957 {printf("Can't get the sections, aborting\n"); return;}
1959 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1961 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1962 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1963 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1964 separateDebugHead->ExportedNamesSize,
1965 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1966 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1968 printf("Debug Table (%u directories)\n", nb_dbg);
1970 for (i = 0; i < nb_dbg; i++)
1972 dump_dir_debug_dir(debugDir, i);
1973 debugDir++;
1977 static const char *get_resource_type( unsigned int id )
1979 static const char * const types[] =
1981 NULL,
1982 "CURSOR",
1983 "BITMAP",
1984 "ICON",
1985 "MENU",
1986 "DIALOG",
1987 "STRING",
1988 "FONTDIR",
1989 "FONT",
1990 "ACCELERATOR",
1991 "RCDATA",
1992 "MESSAGETABLE",
1993 "GROUP_CURSOR",
1994 NULL,
1995 "GROUP_ICON",
1996 NULL,
1997 "VERSION",
1998 "DLGINCLUDE",
1999 NULL,
2000 "PLUGPLAY",
2001 "VXD",
2002 "ANICURSOR",
2003 "ANIICON",
2004 "HTML",
2005 "RT_MANIFEST"
2008 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2009 return NULL;
2012 /* dump an ASCII string with proper escaping */
2013 static int dump_strA( const unsigned char *str, size_t len )
2015 static const char escapes[32] = ".......abtnvfr.............e....";
2016 char buffer[256];
2017 char *pos = buffer;
2018 int count = 0;
2020 for (; len; str++, len--)
2022 if (pos > buffer + sizeof(buffer) - 8)
2024 fwrite( buffer, pos - buffer, 1, stdout );
2025 count += pos - buffer;
2026 pos = buffer;
2028 if (*str > 127) /* hex escape */
2030 pos += sprintf( pos, "\\x%02x", *str );
2031 continue;
2033 if (*str < 32) /* octal or C escape */
2035 if (!*str && len == 1) continue; /* do not output terminating NULL */
2036 if (escapes[*str] != '.')
2037 pos += sprintf( pos, "\\%c", escapes[*str] );
2038 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2039 pos += sprintf( pos, "\\%03o", *str );
2040 else
2041 pos += sprintf( pos, "\\%o", *str );
2042 continue;
2044 if (*str == '\\') *pos++ = '\\';
2045 *pos++ = *str;
2047 fwrite( buffer, pos - buffer, 1, stdout );
2048 count += pos - buffer;
2049 return count;
2052 /* dump a Unicode string with proper escaping */
2053 static int dump_strW( const WCHAR *str, size_t len )
2055 static const char escapes[32] = ".......abtnvfr.............e....";
2056 char buffer[256];
2057 char *pos = buffer;
2058 int count = 0;
2060 for (; len; str++, len--)
2062 if (pos > buffer + sizeof(buffer) - 8)
2064 fwrite( buffer, pos - buffer, 1, stdout );
2065 count += pos - buffer;
2066 pos = buffer;
2068 if (*str > 127) /* hex escape */
2070 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2071 pos += sprintf( pos, "\\x%04x", *str );
2072 else
2073 pos += sprintf( pos, "\\x%x", *str );
2074 continue;
2076 if (*str < 32) /* octal or C escape */
2078 if (!*str && len == 1) continue; /* do not output terminating NULL */
2079 if (escapes[*str] != '.')
2080 pos += sprintf( pos, "\\%c", escapes[*str] );
2081 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2082 pos += sprintf( pos, "\\%03o", *str );
2083 else
2084 pos += sprintf( pos, "\\%o", *str );
2085 continue;
2087 if (*str == '\\') *pos++ = '\\';
2088 *pos++ = *str;
2090 fwrite( buffer, pos - buffer, 1, stdout );
2091 count += pos - buffer;
2092 return count;
2095 /* dump data for a STRING resource */
2096 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2098 int i;
2100 for (i = 0; i < 16 && size; i++)
2102 unsigned len = *ptr++;
2104 if (len >= size)
2106 len = size;
2107 size = 0;
2109 else size -= len + 1;
2111 if (len)
2113 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2114 dump_strW( ptr, len );
2115 printf( "\"\n" );
2116 ptr += len;
2121 /* dump data for a MESSAGETABLE resource */
2122 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2124 const MESSAGE_RESOURCE_DATA *data = ptr;
2125 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2126 unsigned i, j;
2128 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2130 const MESSAGE_RESOURCE_ENTRY *entry;
2132 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2133 for (j = block->LowId; j <= block->HighId; j++)
2135 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2137 const WCHAR *str = (const WCHAR *)entry->Text;
2138 printf( "%s%08x L\"", prefix, j );
2139 dump_strW( str, strlenW(str) );
2140 printf( "\"\n" );
2142 else
2144 const char *str = (const char *) entry->Text;
2145 printf( "%s%08x \"", prefix, j );
2146 dump_strA( entry->Text, strlen(str) );
2147 printf( "\"\n" );
2149 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2154 static void dump_dir_resource(void)
2156 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2157 const IMAGE_RESOURCE_DIRECTORY *namedir;
2158 const IMAGE_RESOURCE_DIRECTORY *langdir;
2159 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2160 const IMAGE_RESOURCE_DIR_STRING_U *string;
2161 const IMAGE_RESOURCE_DATA_ENTRY *data;
2162 int i, j, k;
2164 if (!root) return;
2166 printf( "Resources:" );
2168 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2170 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2171 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
2172 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2174 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2175 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
2176 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2178 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2180 printf( "\n " );
2181 if (e1->u.s.NameIsString)
2183 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
2184 dump_unicode_str( string->NameString, string->Length );
2186 else
2188 const char *type = get_resource_type( e1->u.Id );
2189 if (type) printf( "%s", type );
2190 else printf( "%04x", e1->u.Id );
2193 printf( " Name=" );
2194 if (e2->u.s.NameIsString)
2196 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
2197 dump_unicode_str( string->NameString, string->Length );
2199 else
2200 printf( "%04x", e2->u.Id );
2202 printf( " Language=%04x:\n", e3->u.Id );
2203 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
2204 if (e1->u.s.NameIsString)
2206 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2208 else switch(e1->u.Id)
2210 case 6:
2211 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
2212 break;
2213 case 11:
2214 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
2215 e2->u.Id, " " );
2216 break;
2217 default:
2218 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2219 break;
2224 printf( "\n\n" );
2227 static void dump_debug(void)
2229 const char* stabs = NULL;
2230 unsigned szstabs = 0;
2231 const char* stabstr = NULL;
2232 unsigned szstr = 0;
2233 unsigned i;
2234 const IMAGE_SECTION_HEADER* sectHead;
2236 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2238 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2240 if (!strcmp((const char *)sectHead->Name, ".stab"))
2242 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2243 szstabs = sectHead->Misc.VirtualSize;
2245 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2247 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2248 szstr = sectHead->Misc.VirtualSize;
2251 if (stabs && stabstr)
2252 dump_stabs(stabs, szstabs, stabstr, szstr);
2255 static void dump_symbol_table(void)
2257 const IMAGE_SYMBOL* sym;
2258 int numsym;
2260 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2261 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2262 return;
2263 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2264 sizeof(*sym) * numsym);
2265 if (!sym) return;
2267 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2270 enum FileSig get_kind_exec(void)
2272 const WORD* pw;
2273 const DWORD* pdw;
2274 const IMAGE_DOS_HEADER* dh;
2276 pw = PRD(0, sizeof(WORD));
2277 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2279 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2281 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2283 /* the signature is the first DWORD */
2284 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2285 if (pdw)
2287 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2288 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2289 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2291 return SIG_DOS;
2293 return SIG_UNKNOWN;
2296 void pe_dump(void)
2298 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
2300 PE_nt_headers = get_nt_header();
2301 print_fake_dll();
2303 if (globals.do_dumpheader)
2305 dump_pe_header();
2306 /* FIXME: should check ptr */
2307 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2308 PE_nt_headers->FileHeader.NumberOfSections);
2310 else if (!globals.dumpsect)
2312 /* show at least something here */
2313 dump_pe_header();
2316 if (globals.dumpsect)
2318 if (all || !strcmp(globals.dumpsect, "import"))
2320 dump_dir_imported_functions();
2321 dump_dir_delay_imported_functions();
2323 if (all || !strcmp(globals.dumpsect, "export"))
2324 dump_dir_exported_functions();
2325 if (all || !strcmp(globals.dumpsect, "debug"))
2326 dump_dir_debug();
2327 if (all || !strcmp(globals.dumpsect, "resource"))
2328 dump_dir_resource();
2329 if (all || !strcmp(globals.dumpsect, "tls"))
2330 dump_dir_tls();
2331 if (all || !strcmp(globals.dumpsect, "loadcfg"))
2332 dump_dir_loadconfig();
2333 if (all || !strcmp(globals.dumpsect, "clr"))
2334 dump_dir_clr_header();
2335 if (all || !strcmp(globals.dumpsect, "reloc"))
2336 dump_dir_reloc();
2337 if (all || !strcmp(globals.dumpsect, "except"))
2338 dump_dir_exceptions();
2340 if (globals.do_symbol_table)
2341 dump_symbol_table();
2342 if (globals.do_debug)
2343 dump_debug();
2346 typedef struct _dll_symbol {
2347 size_t ordinal;
2348 char *symbol;
2349 } dll_symbol;
2351 static dll_symbol *dll_symbols = NULL;
2352 static dll_symbol *dll_current_symbol = NULL;
2354 /* Compare symbols by ordinal for qsort */
2355 static int symbol_cmp(const void *left, const void *right)
2357 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2360 /*******************************************************************
2361 * dll_close
2363 * Free resources used by DLL
2365 /* FIXME: Not used yet
2366 static void dll_close (void)
2368 dll_symbol* ds;
2370 if (!dll_symbols) {
2371 fatal("No symbols");
2373 for (ds = dll_symbols; ds->symbol; ds++)
2374 free(ds->symbol);
2375 free (dll_symbols);
2376 dll_symbols = NULL;
2380 static void do_grab_sym( void )
2382 const IMAGE_EXPORT_DIRECTORY*exportDir;
2383 unsigned i, j;
2384 const DWORD* pName;
2385 const DWORD* pFunc;
2386 const WORD* pOrdl;
2387 const char* ptr;
2388 DWORD* map;
2390 PE_nt_headers = get_nt_header();
2391 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2393 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2394 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2395 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2396 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2397 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2398 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2400 /* dll_close(); */
2402 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
2404 /* bit map of used funcs */
2405 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2406 if (!map) fatal("no memory");
2408 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2410 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2411 ptr = RVA(*pName++, sizeof(DWORD));
2412 if (!ptr) ptr = "cant_get_function";
2413 dll_symbols[j].symbol = xstrdup(ptr);
2414 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2415 assert(dll_symbols[j].symbol);
2418 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2420 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2422 char ordinal_text[256];
2423 /* Ordinal only entry */
2424 sprintf (ordinal_text, "%s_%u",
2425 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2426 exportDir->Base + i);
2427 str_toupper(ordinal_text);
2428 dll_symbols[j].symbol = xstrdup(ordinal_text);
2429 assert(dll_symbols[j].symbol);
2430 dll_symbols[j].ordinal = exportDir->Base + i;
2431 j++;
2432 assert(j <= exportDir->NumberOfFunctions);
2435 free(map);
2437 if (NORMAL)
2438 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2439 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2441 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2443 dll_symbols[j].symbol = NULL;
2445 dll_current_symbol = dll_symbols;
2448 /*******************************************************************
2449 * dll_open
2451 * Open a DLL and read in exported symbols
2453 BOOL dll_open (const char *dll_name)
2455 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2458 /*******************************************************************
2459 * dll_next_symbol
2461 * Get next exported symbol from dll
2463 BOOL dll_next_symbol (parsed_symbol * sym)
2465 if (!dll_current_symbol || !dll_current_symbol->symbol)
2466 return FALSE;
2467 assert (dll_symbols);
2468 sym->symbol = xstrdup (dll_current_symbol->symbol);
2469 sym->ordinal = dll_current_symbol->ordinal;
2470 dll_current_symbol++;
2471 return TRUE;