kernel32/tests: Test hard linking a file to a different casing of itself.
[wine.git] / tools / winedump / pe.c
blobaa679103e59c7b659455bd327e536ecbcfe3baa0
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 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
50 static const IMAGE_NT_HEADERS32* PE_nt_headers;
52 static const char builtin_signature[] = "Wine builtin DLL";
53 static const char fakedll_signature[] = "Wine placeholder DLL";
54 static int is_builtin;
56 const char *get_machine_str(int mach)
58 switch (mach)
60 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
61 case IMAGE_FILE_MACHINE_I860: return "i860";
62 case IMAGE_FILE_MACHINE_I386: return "i386";
63 case IMAGE_FILE_MACHINE_R3000: return "R3000";
64 case IMAGE_FILE_MACHINE_R4000: return "R4000";
65 case IMAGE_FILE_MACHINE_R10000: return "R10000";
66 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
67 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
68 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
69 case IMAGE_FILE_MACHINE_IA64: return "IA64";
70 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
71 case IMAGE_FILE_MACHINE_ARM: return "ARM";
72 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
73 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
75 return "???";
78 static const void* RVA(unsigned long rva, unsigned long len)
80 IMAGE_SECTION_HEADER* sectHead;
81 int i;
83 if (rva == 0) return NULL;
85 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
86 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
88 if (sectHead[i].VirtualAddress <= rva &&
89 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
91 /* return image import directory offset */
92 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
96 return NULL;
99 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
101 const IMAGE_DOS_HEADER *dos;
102 dos = PRD(0, sizeof(*dos));
103 if (!dos) return NULL;
104 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
105 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
106 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
109 void print_fake_dll( void )
111 const IMAGE_DOS_HEADER *dos;
113 dos = PRD(0, sizeof(*dos) + 32);
114 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
116 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
117 printf( "*** This is a Wine builtin DLL ***\n\n" );
118 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
119 printf( "*** This is a Wine fake DLL ***\n\n" );
123 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
125 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
127 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&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);
135 else
137 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
138 if (idx >= opt->NumberOfRvaAndSizes)
139 return NULL;
140 if(size)
141 *size = opt->DataDirectory[idx].Size;
142 return RVA(opt->DataDirectory[idx].VirtualAddress,
143 opt->DataDirectory[idx].Size);
147 static const void* get_dir(unsigned idx)
149 return get_dir_and_size(idx, 0);
152 static const char * const DirectoryNames[16] = {
153 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
154 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
155 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
156 "IAT", "Delay IAT", "CLR Header", ""
159 static const char *get_magic_type(WORD magic)
161 switch(magic) {
162 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
163 return "32bit";
164 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
165 return "64bit";
166 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
167 return "ROM";
169 return "???";
172 static inline void print_word(const char *title, WORD value)
174 printf(" %-34s 0x%-4X %u\n", title, value, value);
177 static inline void print_dword(const char *title, DWORD value)
179 printf(" %-34s 0x%-8x %u\n", title, value, value);
182 static inline void print_longlong(const char *title, ULONGLONG value)
184 printf(" %-34s 0x", title);
185 if(value >> 32)
186 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
187 else
188 printf("%lx\n", (unsigned long)value);
191 static inline void print_ver(const char *title, BYTE major, BYTE minor)
193 printf(" %-34s %u.%02u\n", title, major, minor);
196 static inline void print_subsys(const char *title, WORD value)
198 const char *str;
199 switch (value)
201 default:
202 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
203 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
204 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
205 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
206 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
207 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
208 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
209 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
210 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
211 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
212 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
213 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
214 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
215 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
217 printf(" %-34s 0x%X (%s)\n", title, value, str);
220 static inline void print_dllflags(const char *title, WORD value)
222 printf(" %-34s 0x%04X\n", title, value);
223 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
224 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
225 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
226 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
227 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
228 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
229 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
230 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
231 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
232 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
233 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
234 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
235 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
236 #undef X
239 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
241 unsigned i;
242 printf("Data Directory\n");
244 for (i = 0; i < n && i < 16; i++)
246 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
247 DirectoryNames[i], directory[i].VirtualAddress,
248 directory[i].Size);
252 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
254 IMAGE_OPTIONAL_HEADER32 oh;
255 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
257 /* in case optional header is missing or partial */
258 memset(&oh, 0, sizeof(oh));
259 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
260 optionalHeader = &oh;
262 print_word("Magic", optionalHeader->Magic);
263 print_ver("linker version",
264 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
265 print_dword("size of code", optionalHeader->SizeOfCode);
266 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
267 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
268 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
269 print_dword("base of code", optionalHeader->BaseOfCode);
270 print_dword("base of data", optionalHeader->BaseOfData);
271 print_dword("image base", optionalHeader->ImageBase);
272 print_dword("section align", optionalHeader->SectionAlignment);
273 print_dword("file align", optionalHeader->FileAlignment);
274 print_ver("required OS version",
275 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
276 print_ver("image version",
277 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
278 print_ver("subsystem version",
279 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
280 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
281 print_dword("size of image", optionalHeader->SizeOfImage);
282 print_dword("size of headers", optionalHeader->SizeOfHeaders);
283 print_dword("checksum", optionalHeader->CheckSum);
284 print_subsys("Subsystem", optionalHeader->Subsystem);
285 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
286 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
287 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
288 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
289 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
290 print_dword("loader flags", optionalHeader->LoaderFlags);
291 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
292 printf("\n");
293 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
294 printf("\n");
297 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
299 IMAGE_OPTIONAL_HEADER64 oh;
300 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
302 /* in case optional header is missing or partial */
303 memset(&oh, 0, sizeof(oh));
304 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
305 optionalHeader = &oh;
307 print_word("Magic", optionalHeader->Magic);
308 print_ver("linker version",
309 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
310 print_dword("size of code", optionalHeader->SizeOfCode);
311 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
312 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
313 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
314 print_dword("base of code", optionalHeader->BaseOfCode);
315 print_longlong("image base", optionalHeader->ImageBase);
316 print_dword("section align", optionalHeader->SectionAlignment);
317 print_dword("file align", optionalHeader->FileAlignment);
318 print_ver("required OS version",
319 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
320 print_ver("image version",
321 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
322 print_ver("subsystem version",
323 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
324 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
325 print_dword("size of image", optionalHeader->SizeOfImage);
326 print_dword("size of headers", optionalHeader->SizeOfHeaders);
327 print_dword("checksum", optionalHeader->CheckSum);
328 print_subsys("Subsystem", optionalHeader->Subsystem);
329 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
330 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
331 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
332 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
333 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
334 print_dword("loader flags", optionalHeader->LoaderFlags);
335 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
336 printf("\n");
337 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
338 printf("\n");
341 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
343 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
345 switch(optionalHeader->Magic) {
346 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
347 dump_optional_header32(optionalHeader, header_size);
348 break;
349 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
350 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
351 break;
352 default:
353 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
354 break;
358 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
360 printf("File Header\n");
362 printf(" Machine: %04X (%s)\n",
363 fileHeader->Machine, get_machine_str(fileHeader->Machine));
364 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
365 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
366 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
367 Offset(&(fileHeader->TimeDateStamp)));
368 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
369 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
370 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
371 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
372 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
373 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
374 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
375 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
376 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
377 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
378 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
379 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
380 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
381 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
382 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
383 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
384 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
385 X(IMAGE_FILE_SYSTEM, "SYSTEM");
386 X(IMAGE_FILE_DLL, "DLL");
387 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
388 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
389 #undef X
390 printf("\n");
393 static void dump_pe_header(void)
395 dump_file_header(&PE_nt_headers->FileHeader);
396 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
399 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
401 unsigned offset;
403 /* long section name ? */
404 if (strtable && sectHead->Name[0] == '/' &&
405 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
406 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
407 else
408 printf(" %-8.8s", sectHead->Name);
409 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
410 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
411 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
412 sectHead->PointerToRawData, sectHead->SizeOfRawData);
413 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
414 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
415 printf(" line # offs: %-8u line #'s: %-8u\n",
416 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
417 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
418 printf(" ");
419 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
420 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
421 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
422 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
423 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
424 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
425 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
427 X(IMAGE_SCN_CNT_CODE, "CODE");
428 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
429 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
431 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
432 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
433 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
434 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
435 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
437 /* 0x00002000 - Reserved */
438 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
439 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
441 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
442 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
443 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
444 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
445 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
447 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
449 #define X2(b,s) case b: printf(" " s); break
450 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
451 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
452 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
453 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
454 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
455 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
456 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
457 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
458 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
459 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
460 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
461 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
462 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
463 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
464 #undef X2
467 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
469 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
470 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
471 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
472 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
473 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
474 X(IMAGE_SCN_MEM_READ, "MEM_READ");
475 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
476 #undef X
477 printf("\n\n");
480 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
482 const IMAGE_SECTION_HEADER* sectHead = addr;
483 unsigned i;
484 const char* strtable;
486 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
488 strtable = (const char*)base +
489 PE_nt_headers->FileHeader.PointerToSymbolTable +
490 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
492 else strtable = NULL;
494 printf("Section Table\n");
495 for (i = 0; i < num_sect; i++, sectHead++)
497 dump_section(sectHead, strtable);
499 if (globals.do_dump_rawdata)
501 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
502 printf("\n");
507 static void dump_dir_exported_functions(void)
509 unsigned int size = 0;
510 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
511 unsigned int i;
512 const DWORD* pFunc;
513 const DWORD* pName;
514 const WORD* pOrdl;
515 DWORD* funcs;
517 if (!exportDir) return;
519 printf("Exports table:\n");
520 printf("\n");
521 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
522 printf(" Characteristics: %08x\n", exportDir->Characteristics);
523 printf(" TimeDateStamp: %08X %s\n",
524 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
525 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
526 printf(" Ordinal base: %u\n", exportDir->Base);
527 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
528 printf(" # of Names: %u\n", exportDir->NumberOfNames);
529 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
530 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
531 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
532 printf("\n");
533 printf(" Entry Pt Ordn Name\n");
535 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
536 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
537 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
538 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
540 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
541 if (!funcs) fatal("no memory");
543 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
545 for (i = 0; i < exportDir->NumberOfFunctions; i++)
547 if (!pFunc[i]) continue;
548 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
549 if (funcs[i])
550 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
551 else
552 printf("<by ordinal>");
554 /* check for forwarded function */
555 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
556 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
557 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
558 printf("\n");
560 free(funcs);
561 printf("\n");
565 struct runtime_function_x86_64
567 DWORD BeginAddress;
568 DWORD EndAddress;
569 DWORD UnwindData;
572 struct runtime_function_armnt
574 DWORD BeginAddress;
575 union {
576 DWORD UnwindData;
577 struct {
578 DWORD Flag : 2;
579 DWORD FunctionLength : 11;
580 DWORD Ret : 2;
581 DWORD H : 1;
582 DWORD Reg : 3;
583 DWORD R : 1;
584 DWORD L : 1;
585 DWORD C : 1;
586 DWORD StackAdjust : 10;
587 } DUMMYSTRUCTNAME;
588 } DUMMYUNIONNAME;
591 struct runtime_function_arm64
593 DWORD BeginAddress;
594 union
596 DWORD UnwindData;
597 struct
599 DWORD Flag : 2;
600 DWORD FunctionLength : 11;
601 DWORD RegF : 3;
602 DWORD RegI : 4;
603 DWORD H : 1;
604 DWORD CR : 2;
605 DWORD FrameSize : 9;
606 } DUMMYSTRUCTNAME;
607 } DUMMYUNIONNAME;
610 union handler_data
612 struct runtime_function_x86_64 chain;
613 DWORD handler;
616 struct opcode
618 BYTE offset;
619 BYTE code : 4;
620 BYTE info : 4;
623 struct unwind_info_x86_64
625 BYTE version : 3;
626 BYTE flags : 5;
627 BYTE prolog;
628 BYTE count;
629 BYTE frame_reg : 4;
630 BYTE frame_offset : 4;
631 struct opcode opcodes[1]; /* count entries */
632 /* followed by union handler_data */
635 struct unwind_info_armnt
637 DWORD function_length : 18;
638 DWORD version : 2;
639 DWORD x : 1;
640 DWORD e : 1;
641 DWORD f : 1;
642 DWORD count : 5;
643 DWORD words : 4;
646 struct unwind_info_ext_armnt
648 WORD excount;
649 BYTE exwords;
650 BYTE reserved;
653 struct unwind_info_epilogue_armnt
655 DWORD offset : 18;
656 DWORD res : 2;
657 DWORD cond : 4;
658 DWORD index : 8;
661 #define UWOP_PUSH_NONVOL 0
662 #define UWOP_ALLOC_LARGE 1
663 #define UWOP_ALLOC_SMALL 2
664 #define UWOP_SET_FPREG 3
665 #define UWOP_SAVE_NONVOL 4
666 #define UWOP_SAVE_NONVOL_FAR 5
667 #define UWOP_SAVE_XMM128 8
668 #define UWOP_SAVE_XMM128_FAR 9
669 #define UWOP_PUSH_MACHFRAME 10
671 #define UNW_FLAG_EHANDLER 1
672 #define UNW_FLAG_UHANDLER 2
673 #define UNW_FLAG_CHAININFO 4
675 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
677 static const char * const reg_names[16] =
678 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
679 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
681 const union handler_data *handler_data;
682 const struct unwind_info_x86_64 *info;
683 unsigned int i, count;
685 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
686 if (function->UnwindData & 1)
688 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
689 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
690 return;
692 info = RVA( function->UnwindData, sizeof(*info) );
694 printf( " unwind info at %08x\n", function->UnwindData );
695 if (info->version != 1)
697 printf( " *** unknown version %u\n", info->version );
698 return;
700 printf( " flags %x", info->flags );
701 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
702 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
703 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
704 printf( "\n prolog 0x%x bytes\n", info->prolog );
706 if (info->frame_reg)
707 printf( " frame register %s offset 0x%x(%%rsp)\n",
708 reg_names[info->frame_reg], info->frame_offset * 16 );
710 for (i = 0; i < info->count; i++)
712 printf( " 0x%02x: ", info->opcodes[i].offset );
713 switch (info->opcodes[i].code)
715 case UWOP_PUSH_NONVOL:
716 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
717 break;
718 case UWOP_ALLOC_LARGE:
719 if (info->opcodes[i].info)
721 count = *(const DWORD *)&info->opcodes[i+1];
722 i += 2;
724 else
726 count = *(const USHORT *)&info->opcodes[i+1] * 8;
727 i++;
729 printf( "sub $0x%x,%%rsp\n", count );
730 break;
731 case UWOP_ALLOC_SMALL:
732 count = (info->opcodes[i].info + 1) * 8;
733 printf( "sub $0x%x,%%rsp\n", count );
734 break;
735 case UWOP_SET_FPREG:
736 printf( "lea 0x%x(%%rsp),%s\n",
737 info->frame_offset * 16, reg_names[info->frame_reg] );
738 break;
739 case UWOP_SAVE_NONVOL:
740 count = *(const USHORT *)&info->opcodes[i+1] * 8;
741 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
742 i++;
743 break;
744 case UWOP_SAVE_NONVOL_FAR:
745 count = *(const DWORD *)&info->opcodes[i+1];
746 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
747 i += 2;
748 break;
749 case UWOP_SAVE_XMM128:
750 count = *(const USHORT *)&info->opcodes[i+1] * 16;
751 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
752 i++;
753 break;
754 case UWOP_SAVE_XMM128_FAR:
755 count = *(const DWORD *)&info->opcodes[i+1];
756 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
757 i += 2;
758 break;
759 case UWOP_PUSH_MACHFRAME:
760 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
761 break;
762 default:
763 printf( "*** unknown code %u\n", info->opcodes[i].code );
764 break;
768 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
769 if (info->flags & UNW_FLAG_CHAININFO)
771 printf( " -> function %08x-%08x\n",
772 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
773 return;
775 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
776 printf( " handler %08x data at %08x\n", handler_data->handler,
777 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
780 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
782 const struct unwind_info_armnt *info;
783 const struct unwind_info_ext_armnt *infoex;
784 const struct unwind_info_epilogue_armnt *infoepi;
785 unsigned int rva;
786 WORD i, count = 0, words = 0;
788 if (fnc->u.s.Flag)
790 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
791 WORD pf = 0, ef = 0, sc = 0;
793 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
794 (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
795 printf( " Flag %x\n", fnc->u.s.Flag );
796 printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
797 printf( " Ret %x\n", fnc->u.s.Ret );
798 printf( " H %x\n", fnc->u.s.H );
799 printf( " Reg %x\n", fnc->u.s.Reg );
800 printf( " R %x\n", fnc->u.s.R );
801 printf( " L %x\n", fnc->u.s.L );
802 printf( " C %x\n", fnc->u.s.C );
803 printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
805 if (fnc->u.s.StackAdjust >= 0x03f4)
807 pf = fnc->u.s.StackAdjust & 0x04;
808 ef = fnc->u.s.StackAdjust & 0x08;
811 if (!fnc->u.s.R && !pf)
813 if (fnc->u.s.Reg)
815 sprintf(intregs, "r4-r%u", fnc->u.s.Reg + 4);
816 sprintf(intregspop, "r4-r%u", fnc->u.s.Reg + 4);
818 else
820 strcpy(intregs, "r4");
821 strcpy(intregspop, "r4");
823 sc = fnc->u.s.Reg + 1;
824 if (fnc->u.s.C || fnc->u.s.L)
826 strcat(intregs, ", ");
827 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
828 strcat(intregspop, ", ");
831 else if (fnc->u.s.R && pf)
833 if (((~fnc->u.s.StackAdjust) & 3) != 3)
835 sprintf(intregs, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
836 sprintf(intregspop, "r%u-r3", (~fnc->u.s.StackAdjust) & 3);
838 else
840 sprintf(intregs, "r3");
841 sprintf(intregspop, "r3");
843 sc = 4 - ((~fnc->u.s.StackAdjust) & 3);
844 if (fnc->u.s.C || fnc->u.s.L)
846 strcat(intregs, ", ");
847 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
848 strcat(intregspop, ", ");
851 else if (!fnc->u.s.R && pf)
853 sprintf(intregs, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
854 sprintf(intregspop, "r%u-r%u", (~fnc->u.s.StackAdjust) & 3, fnc->u.s.Reg + 4);
855 sc = fnc->u.s.Reg + 5 - ((~fnc->u.s.StackAdjust) & 3);
856 if (fnc->u.s.C || fnc->u.s.L)
858 strcat(intregs, ", ");
859 if (fnc->u.s.C || (fnc->u.s.L && !fnc->u.s.H))
860 strcat(intregspop, ", ");
863 else if (fnc->u.s.R && !pf)
865 if (!fnc->u.s.C && !fnc->u.s.L)
867 strcpy(intregs, "none");
868 strcpy(intregspop, "none");
872 if (fnc->u.s.C && !fnc->u.s.L)
874 strcat(intregs, "r11");
875 strcat(intregspop, "r11");
877 else if (fnc->u.s.C && fnc->u.s.L)
879 strcat(intregs, "r11, lr");
880 if (fnc->u.s.H)
881 strcat(intregspop, "r11");
882 else
883 strcat(intregspop, "r11, pc");
885 else if (!fnc->u.s.C && fnc->u.s.L)
887 strcat(intregs, "lr");
888 if (!fnc->u.s.H)
889 strcat(intregspop, "pc");
892 if (fnc->u.s.R)
894 if (fnc->u.s.Reg)
895 sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
896 else
897 strcpy(vfpregs, "d8");
899 else
900 strcpy(vfpregs, "none");
902 if (fnc->u.s.H)
903 printf( " Unwind Code\tpush {r0-r3}\n" );
905 if (fnc->u.s.R || fnc->u.s.L || fnc->u.s.C || pf)
906 printf( " Unwind Code\tpush {%s}\n", intregs );
908 if (fnc->u.s.C && fnc->u.s.R && !fnc->u.s.L && !pf)
909 printf( " Unwind Code\tmov r11, sp\n" );
910 else if (fnc->u.s.C && (!fnc->u.s.R || fnc->u.s.L || pf))
912 if (fnc->u.s.StackAdjust >= 0x03f4 && !sc)
913 printf( " Unwind Code\tadd r11, sp, #<unknown>\n");
914 else if (fnc->u.s.StackAdjust >= 0x03f4)
915 printf( " Unwind Code\tadd r11, sp, #%d\n", sc * 4 );
916 else
917 printf( " Unwind Code\tadd r11, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
920 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
921 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
923 if (fnc->u.s.StackAdjust < 0x03f4 && !pf)
924 printf( " Unwind Code\tsub sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
927 if (fnc->u.s.StackAdjust < 0x03f4 && !ef)
928 printf( " Unwind Code\tadd sp, sp, #%d\n", fnc->u.s.StackAdjust * 4 );
930 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
931 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
933 if (fnc->u.s.C || !fnc->u.s.R || ef || (fnc->u.s.L && !fnc->u.s.H))
934 printf( " Unwind Code\tpop {%s}\n", intregspop );
936 if (fnc->u.s.H && !fnc->u.s.L)
937 printf( " Unwind Code\tadd sp, sp, #16\n" );
938 else if (fnc->u.s.H && fnc->u.s.L)
939 printf( " Unwind Code\tldr pc, [sp], #20\n" );
941 if (fnc->u.s.Ret == 1)
942 printf( " Unwind Code\tbx <reg>\n" );
943 else if (fnc->u.s.Ret == 2)
944 printf( " Unwind Code\tb <address>\n" );
946 return;
949 info = RVA( fnc->u.UnwindData, sizeof(*info) );
950 rva = fnc->u.UnwindData + sizeof(*info);
951 count = info->count;
952 words = info->words;
954 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
955 (fnc->BeginAddress & ~1) + info->function_length * 2 );
956 printf( " unwind info at %08x\n", fnc->u.UnwindData );
957 printf( " Flag %x\n", fnc->u.s.Flag );
958 printf( " FunctionLength %x\n", info->function_length );
959 printf( " Version %x\n", info->version );
960 printf( " X %x\n", info->x );
961 printf( " E %x\n", info->e );
962 printf( " F %x\n", info->f );
963 printf( " Count %x\n", count );
964 printf( " Words %x\n", words );
966 if (!info->count && !info->words)
968 infoex = RVA( rva, sizeof(*infoex) );
969 rva = rva + sizeof(*infoex);
970 count = infoex->excount;
971 words = infoex->exwords;
972 printf( " ExtCount %x\n", count );
973 printf( " ExtWords %x\n", words );
976 if (!info->e)
978 infoepi = RVA( rva, count * sizeof(*infoepi) );
979 rva = rva + count * sizeof(*infoepi);
981 for (i = 0; i < count; i++)
983 printf( " Epilogue Scope %x\n", i );
984 printf( " Offset %x\n", infoepi[i].offset );
985 printf( " Reserved %x\n", infoepi[i].res );
986 printf( " Condition %x\n", infoepi[i].cond );
987 printf( " Index %x\n", infoepi[i].index );
990 else
991 infoepi = NULL;
993 if (words)
995 const unsigned int *codes;
996 BYTE b, *bytes;
997 BOOL inepilogue = FALSE;
999 codes = RVA( rva, words * sizeof(*codes) );
1000 rva = rva + words * sizeof(*codes);
1001 bytes = (BYTE*)codes;
1003 for (b = 0; b < words * sizeof(*codes); b++)
1005 BYTE code = bytes[b];
1007 if (info->e && b == count)
1009 printf( "Epilogue:\n" );
1010 inepilogue = TRUE;
1012 else if (!info->e && infoepi)
1014 for (i = 0; i < count; i++)
1015 if (b == infoepi[i].index)
1017 printf( "Epilogue from Scope %x at %08x:\n", i,
1018 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1019 inepilogue = TRUE;
1023 printf( " Unwind Code %x\t", code );
1025 if (code == 0x00)
1026 printf( "\n" );
1027 else if (code <= 0x7f)
1028 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1029 else if (code <= 0xbf)
1031 WORD excode, f;
1032 BOOL first = TRUE;
1033 BYTE excodes = bytes[++b];
1035 excode = (code << 8) | excodes;
1036 printf( "%s {", inepilogue ? "pop" : "push" );
1038 for (f = 0; f <= 12; f++)
1040 if ((excode >> f) & 1)
1042 printf( "%sr%u", first ? "" : ", ", f );
1043 first = FALSE;
1047 if (excode & 0x2000)
1048 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1050 printf( "}\n" );
1052 else if (code <= 0xcf)
1053 if (inepilogue)
1054 printf( "mov sp, r%u\n", code & 0x0f );
1055 else
1056 printf( "mov r%u, sp\n", code & 0x0f );
1057 else if (code <= 0xd7)
1058 if (inepilogue)
1059 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1060 else
1061 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1062 else if (code <= 0xdf)
1063 if (inepilogue)
1064 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1065 else
1066 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1067 else if (code <= 0xe7)
1068 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1069 else if (code <= 0xeb)
1071 WORD excode;
1072 BYTE excodes = bytes[++b];
1074 excode = (code << 8) | excodes;
1075 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1077 else if (code <= 0xed)
1079 WORD excode, f;
1080 BOOL first = TRUE;
1081 BYTE excodes = bytes[++b];
1083 excode = (code << 8) | excodes;
1084 printf( "%s {", inepilogue ? "pop" : "push" );
1086 for (f = 0; f < 8; f++)
1088 if ((excode >> f) & 1)
1090 printf( "%sr%u", first ? "" : ", ", f );
1091 first = FALSE;
1095 if (excode & 0x0100)
1096 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1098 printf( "}\n" );
1100 else if (code == 0xee)
1101 printf( "unknown 16\n" );
1102 else if (code == 0xef)
1104 WORD excode;
1105 BYTE excodes = bytes[++b];
1107 if (excodes <= 0x0f)
1109 excode = (code << 8) | excodes;
1110 if (inepilogue)
1111 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1112 else
1113 printf( "unknown 32\n" );
1115 else
1116 printf( "unknown 32\n" );
1118 else if (code <= 0xf4)
1119 printf( "unknown\n" );
1120 else if (code <= 0xf6)
1122 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1123 BYTE excodes = bytes[++b];
1125 excode = (code << 8) | excodes;
1126 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1127 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1129 else if (code <= 0xf7)
1131 unsigned int excode;
1132 BYTE excodes[2];
1134 excodes[0] = bytes[++b];
1135 excodes[1] = bytes[++b];
1136 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1137 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1139 else if (code <= 0xf8)
1141 unsigned int excode;
1142 BYTE excodes[3];
1144 excodes[0] = bytes[++b];
1145 excodes[1] = bytes[++b];
1146 excodes[2] = bytes[++b];
1147 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1148 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1150 else if (code <= 0xf9)
1152 unsigned int excode;
1153 BYTE excodes[2];
1155 excodes[0] = bytes[++b];
1156 excodes[1] = bytes[++b];
1157 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1158 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1160 else if (code <= 0xfa)
1162 unsigned int excode;
1163 BYTE excodes[3];
1165 excodes[0] = bytes[++b];
1166 excodes[1] = bytes[++b];
1167 excodes[2] = bytes[++b];
1168 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1169 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1171 else if (code <= 0xfc)
1172 printf( "nop\n" );
1173 else if (code <= 0xfe)
1175 printf( "(end) nop\n" );
1176 inepilogue = TRUE;
1178 else
1180 printf( "end\n" );
1181 inepilogue = TRUE;
1186 if (info->x)
1188 const unsigned int *handler;
1190 handler = RVA( rva, sizeof(*handler) );
1191 rva = rva + sizeof(*handler);
1193 printf( " handler %08x data at %08x\n", *handler, rva);
1197 struct unwind_info_arm64
1199 DWORD function_length : 18;
1200 DWORD version : 2;
1201 DWORD x : 1;
1202 DWORD e : 1;
1203 DWORD epilog : 5;
1204 DWORD codes : 5;
1207 struct unwind_info_ext_arm64
1209 WORD epilog;
1210 BYTE codes;
1211 BYTE reserved;
1214 struct unwind_info_epilog_arm64
1216 DWORD offset : 18;
1217 DWORD res : 4;
1218 DWORD index : 10;
1221 static const BYTE code_lengths[256] =
1223 /* 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,
1224 /* 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,
1225 /* 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,
1226 /* 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,
1227 /* 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,
1228 /* 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,
1229 /* 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,
1230 /* 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
1233 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1235 unsigned int i, j;
1237 for (i = 0; i < count; i += code_lengths[ptr[i]])
1239 BYTE len = code_lengths[ptr[i]];
1240 unsigned int val = ptr[i];
1241 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1242 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1244 printf( " %04x: ", i );
1245 for (j = 0; j < 4; j++)
1246 if (j < len) printf( "%02x ", ptr[i+j] );
1247 else printf( " " );
1249 if (ptr[i] < 0x20) /* alloc_s */
1251 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1253 else if (ptr[i] < 0x40) /* save_r19r20_x */
1255 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1257 else if (ptr[i] < 0x80) /* save_fplr */
1259 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1261 else if (ptr[i] < 0xc0) /* save_fplr_x */
1263 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1265 else if (ptr[i] < 0xc8) /* alloc_m */
1267 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1269 else if (ptr[i] < 0xcc) /* save_regp */
1271 int reg = 19 + ((val >> 6) & 0xf);
1272 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1274 else if (ptr[i] < 0xd0) /* save_regp_x */
1276 int reg = 19 + ((val >> 6) & 0xf);
1277 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1279 else if (ptr[i] < 0xd4) /* save_reg */
1281 int reg = 19 + ((val >> 6) & 0xf);
1282 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1284 else if (ptr[i] < 0xd6) /* save_reg_x */
1286 int reg = 19 + ((val >> 5) & 0xf);
1287 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1289 else if (ptr[i] < 0xd8) /* save_lrpair */
1291 int reg = 19 + 2 * ((val >> 6) & 0x7);
1292 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1294 else if (ptr[i] < 0xda) /* save_fregp */
1296 int reg = 8 + ((val >> 6) & 0x7);
1297 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1299 else if (ptr[i] < 0xdc) /* save_fregp_x */
1301 int reg = 8 + ((val >> 6) & 0x7);
1302 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1304 else if (ptr[i] < 0xde) /* save_freg */
1306 int reg = 8 + ((val >> 6) & 0x7);
1307 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1309 else if (ptr[i] == 0xde) /* save_freg_x */
1311 int reg = 8 + ((val >> 5) & 0x7);
1312 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1314 else if (ptr[i] == 0xe0) /* alloc_l */
1316 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1318 else if (ptr[i] == 0xe1) /* set_fp */
1320 printf( "mov x29,sp\n" );
1322 else if (ptr[i] == 0xe2) /* add_fp */
1324 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1326 else if (ptr[i] == 0xe3) /* nop */
1328 printf( "nop\n" );
1330 else if (ptr[i] == 0xe4) /* end */
1332 printf( "end\n" );
1334 else if (ptr[i] == 0xe5) /* end_c */
1336 printf( "end_c\n" );
1338 else if (ptr[i] == 0xe6) /* save_next */
1340 printf( "save_next\n" );
1342 else if (ptr[i] == 0xe7) /* arithmetic */
1344 switch ((val >> 4) & 0x0f)
1346 case 0: printf( "add lr,lr,x28\n" ); break;
1347 case 1: printf( "add lr,lr,sp\n" ); break;
1348 case 2: printf( "sub lr,lr,x28\n" ); break;
1349 case 3: printf( "sub lr,lr,sp\n" ); break;
1350 case 4: printf( "eor lr,lr,x28\n" ); break;
1351 case 5: printf( "eor lr,lr,sp\n" ); break;
1352 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1353 case 8: printf( "ror lr,lr,x28\n" ); break;
1354 case 9: printf( "ror lr,lr,sp\n" ); break;
1355 default:printf( "unknown op\n" ); break;
1358 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1360 printf( "MSFT_OP_TRAP_FRAME\n" );
1362 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1364 printf( "MSFT_OP_MACHINE_FRAME\n" );
1366 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1368 printf( "MSFT_OP_CONTEXT\n" );
1370 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1372 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1374 else printf( "??\n");
1378 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1380 int i, pos = 0, intsz = func->u.s.RegI * 8, fpsz = func->u.s.RegF * 8, savesz, locsz;
1382 if (func->u.s.CR == 1) intsz += 8;
1383 if (func->u.s.RegF) fpsz += 8;
1385 savesz = ((intsz + fpsz + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
1386 locsz = func->u.s.FrameSize * 16 - savesz;
1388 switch (func->u.s.CR)
1390 case 3:
1391 printf( " %04x: mov x29,sp\n", pos++ );
1392 if (locsz <= 512)
1394 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1395 break;
1397 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1398 /* fall through */
1399 case 0:
1400 case 1:
1401 if (locsz <= 4080)
1403 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1405 else
1407 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1408 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1410 break;
1413 if (func->u.s.H)
1415 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1416 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1417 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1418 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1421 if (func->u.s.RegF)
1423 if (func->u.s.RegF % 2 == 0)
1424 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->u.s.RegF, intsz + fpsz - 8 );
1425 for (i = (func->u.s.RegF - 1)/ 2; i >= 0; i--)
1427 if (!i && !intsz)
1428 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1429 else
1430 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1434 switch (func->u.s.RegI)
1436 case 0:
1437 if (func->u.s.CR == 1)
1438 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1439 break;
1440 case 1:
1441 if (func->u.s.CR == 1)
1442 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1443 else
1444 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1445 break;
1446 default:
1447 if (func->u.s.RegI % 2)
1449 if (func->u.s.CR == 1)
1450 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1451 else
1452 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1454 else if (func->u.s.CR == 1)
1455 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1457 for (i = func->u.s.RegI / 2 - 1; i >= 0; i--)
1458 if (i)
1459 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1460 else
1461 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1462 break;
1464 printf( " %04x: end\n", pos );
1467 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1469 const struct unwind_info_arm64 *info;
1470 const struct unwind_info_ext_arm64 *infoex;
1471 const struct unwind_info_epilog_arm64 *infoepi;
1472 const BYTE *ptr;
1473 unsigned int i, rva, codes, epilogs;
1475 if (func->u.s.Flag)
1477 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1478 func->BeginAddress + func->u.s.FunctionLength * 4 );
1479 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1480 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
1481 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
1482 dump_arm64_packed_info( func );
1483 return;
1486 rva = func->u.UnwindData;
1487 info = RVA( rva, sizeof(*info) );
1488 rva += sizeof(*info);
1489 epilogs = info->epilog;
1490 codes = info->codes;
1492 if (!codes)
1494 infoex = RVA( rva, sizeof(*infoex) );
1495 rva = rva + sizeof(*infoex);
1496 codes = infoex->codes;
1497 epilogs = infoex->epilog;
1499 printf( "\nFunction %08x-%08x:\n",
1500 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1501 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1502 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1503 if (info->e)
1505 printf( " epilog 0: code=%04x\n", info->epilog );
1507 else
1509 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1510 rva += sizeof(*infoepi) * epilogs;
1511 for (i = 0; i < epilogs; i++)
1512 printf( " epilog %u: pc=%08x code=%04x\n", i,
1513 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1515 ptr = RVA( rva, codes * 4);
1516 rva += codes * 4;
1517 if (info->x)
1519 const DWORD *handler = RVA( rva, sizeof(*handler) );
1520 rva += sizeof(*handler);
1521 printf( " handler: %08x data %08x\n", *handler, rva );
1523 dump_arm64_codes( ptr, codes * 4 );
1526 static void dump_dir_exceptions(void)
1528 unsigned int i, size = 0;
1529 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1530 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1532 if (!funcs) return;
1534 switch (file_header->Machine)
1536 case IMAGE_FILE_MACHINE_AMD64:
1537 size /= sizeof(struct runtime_function_x86_64);
1538 printf( "Exception info (%u functions):\n", size );
1539 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1540 break;
1541 case IMAGE_FILE_MACHINE_ARMNT:
1542 size /= sizeof(struct runtime_function_armnt);
1543 printf( "Exception info (%u functions):\n", size );
1544 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1545 break;
1546 case IMAGE_FILE_MACHINE_ARM64:
1547 size /= sizeof(struct runtime_function_arm64);
1548 printf( "Exception info (%u functions):\n", size );
1549 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1550 break;
1551 default:
1552 printf( "Exception information not supported for %s binaries\n",
1553 get_machine_str(file_header->Machine));
1554 break;
1559 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, DWORD thunk_rva)
1561 /* FIXME: This does not properly handle large images */
1562 const IMAGE_IMPORT_BY_NAME* iibn;
1563 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1565 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1566 printf(" %08x %4u <by ordinal>\n", thunk_rva, (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
1567 else
1569 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1570 if (!iibn)
1571 printf("Can't grab import by name info, skipping to next ordinal\n");
1572 else
1573 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1578 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, DWORD thunk_rva)
1580 const IMAGE_IMPORT_BY_NAME* iibn;
1581 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(DWORD))
1583 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1584 printf(" %08x %4u <by ordinal>\n", thunk_rva, IMAGE_ORDINAL32(il->u1.Ordinal));
1585 else
1587 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1588 if (!iibn)
1589 printf("Can't grab import by name info, skipping to next ordinal\n");
1590 else
1591 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1596 static void dump_dir_imported_functions(void)
1598 unsigned directorySize;
1599 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1601 if (!importDesc) return;
1603 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1605 for (;;)
1607 const IMAGE_THUNK_DATA32* il;
1609 if (!importDesc->Name || !importDesc->FirstThunk) break;
1611 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1612 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
1613 printf(" TimeDateStamp: %08X (%s)\n",
1614 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1615 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
1616 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
1618 printf(" Thunk Ordn Name\n");
1620 il = (importDesc->u.OriginalFirstThunk != 0) ?
1621 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
1622 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1624 if (!il)
1625 printf("Can't grab thunk data, going to next imported DLL\n");
1626 else
1628 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1629 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1630 else
1631 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1632 printf("\n");
1634 importDesc++;
1636 printf("\n");
1639 static void dump_dir_loadconfig(void)
1641 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1642 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1644 if (!loadcfg32) return;
1646 printf( "Loadconfig\n" );
1647 print_dword( "Size", loadcfg32->Size );
1648 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1649 print_word( "MajorVersion", loadcfg32->MajorVersion );
1650 print_word( "MinorVersion", loadcfg32->MinorVersion );
1651 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1652 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1653 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1655 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1657 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1658 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1659 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1660 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1661 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1662 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1663 print_word( "CSDVersion", loadcfg64->CSDVersion );
1664 print_word( "Reserved", loadcfg64->Reserved1 );
1665 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1666 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1667 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1669 else
1671 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1672 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1673 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1674 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1675 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1676 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1677 print_word( "CSDVersion", loadcfg32->CSDVersion );
1678 print_word( "Reserved", loadcfg32->Reserved1 );
1679 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1680 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1681 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1685 static void dump_dir_delay_imported_functions(void)
1687 unsigned directorySize;
1688 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1690 if (!importDesc) return;
1692 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1694 for (;;)
1696 const IMAGE_THUNK_DATA32* il;
1697 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1699 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1701 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1702 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1703 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1704 printf(" Address Table: %08x\n", importDesc->ImportAddressTableRVA);
1705 printf(" TimeDateStamp: %08X (%s)\n",
1706 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1708 printf(" Thunk Ordn Name\n");
1710 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1712 if (!il)
1713 printf("Can't grab thunk data, going to next imported DLL\n");
1714 else
1716 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1717 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1718 else
1719 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1720 printf("\n");
1722 importDesc++;
1724 printf("\n");
1727 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1729 const char* str;
1731 printf("Directory %02u\n", idx + 1);
1732 printf(" Characteristics: %08X\n", idd->Characteristics);
1733 printf(" TimeDateStamp: %08X %s\n",
1734 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1735 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1736 switch (idd->Type)
1738 default:
1739 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1740 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1741 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1742 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1743 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1744 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1745 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1746 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1747 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1748 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1749 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1750 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1751 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1752 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1753 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1754 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1755 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
1757 printf(" Type: %u (%s)\n", idd->Type, str);
1758 printf(" SizeOfData: %u\n", idd->SizeOfData);
1759 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1760 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1762 switch (idd->Type)
1764 case IMAGE_DEBUG_TYPE_UNKNOWN:
1765 break;
1766 case IMAGE_DEBUG_TYPE_COFF:
1767 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1768 IMAGE_FIRST_SECTION(PE_nt_headers));
1769 break;
1770 case IMAGE_DEBUG_TYPE_CODEVIEW:
1771 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1772 break;
1773 case IMAGE_DEBUG_TYPE_FPO:
1774 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1775 break;
1776 case IMAGE_DEBUG_TYPE_MISC:
1778 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1779 if (!misc) {printf("Can't get misc debug information\n"); break;}
1780 printf(" DataType: %u (%s)\n",
1781 misc->DataType,
1782 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1783 printf(" Length: %u\n", misc->Length);
1784 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1785 printf(" Data: %s\n", misc->Data);
1787 break;
1788 default: break;
1790 printf("\n");
1793 static void dump_dir_debug(void)
1795 unsigned nb_dbg, i;
1796 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1798 nb_dbg /= sizeof(*debugDir);
1799 if (!debugDir || !nb_dbg) return;
1801 printf("Debug Table (%u directories)\n", nb_dbg);
1803 for (i = 0; i < nb_dbg; i++)
1805 dump_dir_debug_dir(debugDir, i);
1806 debugDir++;
1808 printf("\n");
1811 static inline void print_clrflags(const char *title, DWORD value)
1813 printf(" %-34s 0x%X\n", title, value);
1814 #define X(f,s) if (value & f) printf(" %s\n", s)
1815 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1816 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1817 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1818 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1819 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1820 #undef X
1823 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1825 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1828 static void dump_dir_clr_header(void)
1830 unsigned int size = 0;
1831 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1833 if (!dir) return;
1835 printf( "CLR Header\n" );
1836 print_dword( "Header Size", dir->cb );
1837 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1838 print_clrflags( "Flags", dir->Flags );
1839 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1840 printf("\n");
1841 printf( "CLR Data Directory\n" );
1842 print_clrdirectory( "MetaData", &dir->MetaData );
1843 print_clrdirectory( "Resources", &dir->Resources );
1844 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1845 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1846 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1847 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1848 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1849 printf("\n");
1852 static void dump_dir_reloc(void)
1854 unsigned int i, size = 0;
1855 const USHORT *relocs;
1856 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1857 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1858 static const char * const names[] =
1860 "BASED_ABSOLUTE",
1861 "BASED_HIGH",
1862 "BASED_LOW",
1863 "BASED_HIGHLOW",
1864 "BASED_HIGHADJ",
1865 "BASED_MIPS_JMPADDR",
1866 "BASED_SECTION",
1867 "BASED_REL",
1868 "unknown 8",
1869 "BASED_IA64_IMM64",
1870 "BASED_DIR64",
1871 "BASED_HIGH3ADJ",
1872 "unknown 12",
1873 "unknown 13",
1874 "unknown 14",
1875 "unknown 15"
1878 if (!rel) return;
1880 printf( "Relocations\n" );
1881 while (rel < end - 1 && rel->SizeOfBlock)
1883 printf( " Page %x\n", rel->VirtualAddress );
1884 relocs = (const USHORT *)(rel + 1);
1885 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1886 while (i--)
1888 USHORT offset = *relocs & 0xfff;
1889 int type = *relocs >> 12;
1890 printf( " off %04x type %s\n", offset, names[type] );
1891 relocs++;
1893 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1895 printf("\n");
1898 static void dump_dir_tls(void)
1900 IMAGE_TLS_DIRECTORY64 dir;
1901 const DWORD *callbacks;
1902 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1904 if (!pdir) return;
1906 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1907 memcpy(&dir, pdir, sizeof(dir));
1908 else
1910 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1911 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1912 dir.AddressOfIndex = pdir->AddressOfIndex;
1913 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1914 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1915 dir.Characteristics = pdir->Characteristics;
1918 /* FIXME: This does not properly handle large images */
1919 printf( "Thread Local Storage\n" );
1920 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1921 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1922 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1923 (DWORD)dir.SizeOfZeroFill );
1924 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1925 printf( " Characteristics %08x\n", dir.Characteristics );
1926 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1927 if (dir.AddressOfCallBacks)
1929 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1930 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1932 printf( " %08x", *callbacks );
1933 addr += sizeof(DWORD);
1936 printf(" }\n\n");
1939 enum FileSig get_kind_dbg(void)
1941 const WORD* pw;
1943 pw = PRD(0, sizeof(WORD));
1944 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1946 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1947 return SIG_UNKNOWN;
1950 void dbg_dump(void)
1952 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1953 unsigned nb_dbg;
1954 unsigned i;
1955 const IMAGE_DEBUG_DIRECTORY* debugDir;
1957 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1958 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1960 printf ("Signature: %.2s (0x%4X)\n",
1961 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1962 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1963 printf ("Machine: 0x%04X (%s)\n",
1964 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1965 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1966 printf ("TimeDateStamp: 0x%08X (%s)\n",
1967 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1968 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1969 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1970 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1971 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1972 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1973 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1975 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1976 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1977 {printf("Can't get the sections, aborting\n"); return;}
1979 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1981 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1982 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1983 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1984 separateDebugHead->ExportedNamesSize,
1985 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1986 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1988 printf("Debug Table (%u directories)\n", nb_dbg);
1990 for (i = 0; i < nb_dbg; i++)
1992 dump_dir_debug_dir(debugDir, i);
1993 debugDir++;
1997 static const char *get_resource_type( unsigned int id )
1999 static const char * const types[] =
2001 NULL,
2002 "CURSOR",
2003 "BITMAP",
2004 "ICON",
2005 "MENU",
2006 "DIALOG",
2007 "STRING",
2008 "FONTDIR",
2009 "FONT",
2010 "ACCELERATOR",
2011 "RCDATA",
2012 "MESSAGETABLE",
2013 "GROUP_CURSOR",
2014 NULL,
2015 "GROUP_ICON",
2016 NULL,
2017 "VERSION",
2018 "DLGINCLUDE",
2019 NULL,
2020 "PLUGPLAY",
2021 "VXD",
2022 "ANICURSOR",
2023 "ANIICON",
2024 "HTML",
2025 "RT_MANIFEST"
2028 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2029 return NULL;
2032 /* dump an ASCII string with proper escaping */
2033 static int dump_strA( const unsigned char *str, size_t len )
2035 static const char escapes[32] = ".......abtnvfr.............e....";
2036 char buffer[256];
2037 char *pos = buffer;
2038 int count = 0;
2040 for (; len; str++, len--)
2042 if (pos > buffer + sizeof(buffer) - 8)
2044 fwrite( buffer, pos - buffer, 1, stdout );
2045 count += pos - buffer;
2046 pos = buffer;
2048 if (*str > 127) /* hex escape */
2050 pos += sprintf( pos, "\\x%02x", *str );
2051 continue;
2053 if (*str < 32) /* octal or C escape */
2055 if (!*str && len == 1) continue; /* do not output terminating NULL */
2056 if (escapes[*str] != '.')
2057 pos += sprintf( pos, "\\%c", escapes[*str] );
2058 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2059 pos += sprintf( pos, "\\%03o", *str );
2060 else
2061 pos += sprintf( pos, "\\%o", *str );
2062 continue;
2064 if (*str == '\\') *pos++ = '\\';
2065 *pos++ = *str;
2067 fwrite( buffer, pos - buffer, 1, stdout );
2068 count += pos - buffer;
2069 return count;
2072 /* dump a Unicode string with proper escaping */
2073 static int dump_strW( const WCHAR *str, size_t len )
2075 static const char escapes[32] = ".......abtnvfr.............e....";
2076 char buffer[256];
2077 char *pos = buffer;
2078 int count = 0;
2080 for (; len; str++, len--)
2082 if (pos > buffer + sizeof(buffer) - 8)
2084 fwrite( buffer, pos - buffer, 1, stdout );
2085 count += pos - buffer;
2086 pos = buffer;
2088 if (*str > 127) /* hex escape */
2090 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2091 pos += sprintf( pos, "\\x%04x", *str );
2092 else
2093 pos += sprintf( pos, "\\x%x", *str );
2094 continue;
2096 if (*str < 32) /* octal or C escape */
2098 if (!*str && len == 1) continue; /* do not output terminating NULL */
2099 if (escapes[*str] != '.')
2100 pos += sprintf( pos, "\\%c", escapes[*str] );
2101 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2102 pos += sprintf( pos, "\\%03o", *str );
2103 else
2104 pos += sprintf( pos, "\\%o", *str );
2105 continue;
2107 if (*str == '\\') *pos++ = '\\';
2108 *pos++ = *str;
2110 fwrite( buffer, pos - buffer, 1, stdout );
2111 count += pos - buffer;
2112 return count;
2115 /* dump data for a STRING resource */
2116 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2118 int i;
2120 for (i = 0; i < 16 && size; i++)
2122 unsigned len = *ptr++;
2124 if (len >= size)
2126 len = size;
2127 size = 0;
2129 else size -= len + 1;
2131 if (len)
2133 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2134 dump_strW( ptr, len );
2135 printf( "\"\n" );
2136 ptr += len;
2141 /* dump data for a MESSAGETABLE resource */
2142 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2144 const MESSAGE_RESOURCE_DATA *data = ptr;
2145 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2146 unsigned i, j;
2148 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2150 const MESSAGE_RESOURCE_ENTRY *entry;
2152 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2153 for (j = block->LowId; j <= block->HighId; j++)
2155 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2157 const WCHAR *str = (const WCHAR *)entry->Text;
2158 printf( "%s%08x L\"", prefix, j );
2159 dump_strW( str, strlenW(str) );
2160 printf( "\"\n" );
2162 else
2164 const char *str = (const char *) entry->Text;
2165 printf( "%s%08x \"", prefix, j );
2166 dump_strA( entry->Text, strlen(str) );
2167 printf( "\"\n" );
2169 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2174 static void dump_dir_resource(void)
2176 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2177 const IMAGE_RESOURCE_DIRECTORY *namedir;
2178 const IMAGE_RESOURCE_DIRECTORY *langdir;
2179 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2180 const IMAGE_RESOURCE_DIR_STRING_U *string;
2181 const IMAGE_RESOURCE_DATA_ENTRY *data;
2182 int i, j, k;
2184 if (!root) return;
2186 printf( "Resources:" );
2188 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2190 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2191 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
2192 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2194 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2195 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
2196 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2198 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2200 printf( "\n " );
2201 if (e1->u.s.NameIsString)
2203 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
2204 dump_unicode_str( string->NameString, string->Length );
2206 else
2208 const char *type = get_resource_type( e1->u.Id );
2209 if (type) printf( "%s", type );
2210 else printf( "%04x", e1->u.Id );
2213 printf( " Name=" );
2214 if (e2->u.s.NameIsString)
2216 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
2217 dump_unicode_str( string->NameString, string->Length );
2219 else
2220 printf( "%04x", e2->u.Id );
2222 printf( " Language=%04x:\n", e3->u.Id );
2223 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
2224 if (e1->u.s.NameIsString)
2226 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2228 else switch(e1->u.Id)
2230 case 6:
2231 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
2232 break;
2233 case 11:
2234 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
2235 e2->u.Id, " " );
2236 break;
2237 default:
2238 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2239 break;
2244 printf( "\n\n" );
2247 static void dump_debug(void)
2249 const char* stabs = NULL;
2250 unsigned szstabs = 0;
2251 const char* stabstr = NULL;
2252 unsigned szstr = 0;
2253 unsigned i;
2254 const IMAGE_SECTION_HEADER* sectHead;
2256 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2258 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2260 if (!strcmp((const char *)sectHead->Name, ".stab"))
2262 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2263 szstabs = sectHead->Misc.VirtualSize;
2265 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2267 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2268 szstr = sectHead->Misc.VirtualSize;
2271 if (stabs && stabstr)
2272 dump_stabs(stabs, szstabs, stabstr, szstr);
2275 static void dump_symbol_table(void)
2277 const IMAGE_SYMBOL* sym;
2278 int numsym;
2280 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2281 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2282 return;
2283 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2284 sizeof(*sym) * numsym);
2285 if (!sym) return;
2287 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2290 enum FileSig get_kind_exec(void)
2292 const WORD* pw;
2293 const DWORD* pdw;
2294 const IMAGE_DOS_HEADER* dh;
2296 pw = PRD(0, sizeof(WORD));
2297 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2299 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2301 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2303 /* the signature is the first DWORD */
2304 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2305 if (pdw)
2307 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2308 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2309 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2311 return SIG_DOS;
2313 return SIG_UNKNOWN;
2316 void pe_dump(void)
2318 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
2320 PE_nt_headers = get_nt_header();
2321 print_fake_dll();
2323 if (globals.do_dumpheader)
2325 dump_pe_header();
2326 /* FIXME: should check ptr */
2327 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2328 PE_nt_headers->FileHeader.NumberOfSections);
2330 else if (!globals.dumpsect)
2332 /* show at least something here */
2333 dump_pe_header();
2336 if (globals.dumpsect)
2338 if (all || !strcmp(globals.dumpsect, "import"))
2340 dump_dir_imported_functions();
2341 dump_dir_delay_imported_functions();
2343 if (all || !strcmp(globals.dumpsect, "export"))
2344 dump_dir_exported_functions();
2345 if (all || !strcmp(globals.dumpsect, "debug"))
2346 dump_dir_debug();
2347 if (all || !strcmp(globals.dumpsect, "resource"))
2348 dump_dir_resource();
2349 if (all || !strcmp(globals.dumpsect, "tls"))
2350 dump_dir_tls();
2351 if (all || !strcmp(globals.dumpsect, "loadcfg"))
2352 dump_dir_loadconfig();
2353 if (all || !strcmp(globals.dumpsect, "clr"))
2354 dump_dir_clr_header();
2355 if (all || !strcmp(globals.dumpsect, "reloc"))
2356 dump_dir_reloc();
2357 if (all || !strcmp(globals.dumpsect, "except"))
2358 dump_dir_exceptions();
2360 if (globals.do_symbol_table)
2361 dump_symbol_table();
2362 if (globals.do_debug)
2363 dump_debug();
2366 typedef struct _dll_symbol {
2367 size_t ordinal;
2368 char *symbol;
2369 } dll_symbol;
2371 static dll_symbol *dll_symbols = NULL;
2372 static dll_symbol *dll_current_symbol = NULL;
2374 /* Compare symbols by ordinal for qsort */
2375 static int symbol_cmp(const void *left, const void *right)
2377 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2380 /*******************************************************************
2381 * dll_close
2383 * Free resources used by DLL
2385 /* FIXME: Not used yet
2386 static void dll_close (void)
2388 dll_symbol* ds;
2390 if (!dll_symbols) {
2391 fatal("No symbols");
2393 for (ds = dll_symbols; ds->symbol; ds++)
2394 free(ds->symbol);
2395 free (dll_symbols);
2396 dll_symbols = NULL;
2400 static void do_grab_sym( void )
2402 const IMAGE_EXPORT_DIRECTORY*exportDir;
2403 unsigned i, j;
2404 const DWORD* pName;
2405 const DWORD* pFunc;
2406 const WORD* pOrdl;
2407 const char* ptr;
2408 DWORD* map;
2410 PE_nt_headers = get_nt_header();
2411 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2413 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2414 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2415 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2416 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2417 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2418 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2420 /* dll_close(); */
2422 if (!(dll_symbols = malloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol))))
2423 fatal ("Out of memory");
2425 /* bit map of used funcs */
2426 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2427 if (!map) fatal("no memory");
2429 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2431 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2432 ptr = RVA(*pName++, sizeof(DWORD));
2433 if (!ptr) ptr = "cant_get_function";
2434 dll_symbols[j].symbol = strdup(ptr);
2435 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2436 assert(dll_symbols[j].symbol);
2439 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2441 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2443 char ordinal_text[256];
2444 /* Ordinal only entry */
2445 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%u",
2446 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2447 exportDir->Base + i);
2448 str_toupper(ordinal_text);
2449 dll_symbols[j].symbol = strdup(ordinal_text);
2450 assert(dll_symbols[j].symbol);
2451 dll_symbols[j].ordinal = exportDir->Base + i;
2452 j++;
2453 assert(j <= exportDir->NumberOfFunctions);
2456 free(map);
2458 if (NORMAL)
2459 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2460 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2462 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2464 dll_symbols[j].symbol = NULL;
2466 dll_current_symbol = dll_symbols;
2469 /*******************************************************************
2470 * dll_open
2472 * Open a DLL and read in exported symbols
2474 BOOL dll_open (const char *dll_name)
2476 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2479 /*******************************************************************
2480 * dll_next_symbol
2482 * Get next exported symbol from dll
2484 BOOL dll_next_symbol (parsed_symbol * sym)
2486 if (!dll_current_symbol || !dll_current_symbol->symbol)
2487 return FALSE;
2488 assert (dll_symbols);
2489 sym->symbol = strdup (dll_current_symbol->symbol);
2490 sym->ordinal = dll_current_symbol->ordinal;
2491 dll_current_symbol++;
2492 return TRUE;