localspl: Add unixname port extension.
[wine.git] / tools / winedump / pe.c
blob7ec378a19b8a055dce2eb0fcb49044f7b4defd6e
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 #include "windef.h"
30 #include "winbase.h"
31 #include "winedump.h"
33 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
35 static const IMAGE_NT_HEADERS32* PE_nt_headers;
37 static const char builtin_signature[] = "Wine builtin DLL";
38 static const char fakedll_signature[] = "Wine placeholder DLL";
39 static int is_builtin;
41 const char *get_machine_str(int mach)
43 switch (mach)
45 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
46 case IMAGE_FILE_MACHINE_I860: return "i860";
47 case IMAGE_FILE_MACHINE_I386: return "i386";
48 case IMAGE_FILE_MACHINE_R3000: return "R3000";
49 case IMAGE_FILE_MACHINE_R4000: return "R4000";
50 case IMAGE_FILE_MACHINE_R10000: return "R10000";
51 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
52 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
53 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
54 case IMAGE_FILE_MACHINE_IA64: return "IA64";
55 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
56 case IMAGE_FILE_MACHINE_ARM: return "ARM";
57 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
58 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
60 return "???";
63 static const void* RVA(unsigned long rva, unsigned long len)
65 IMAGE_SECTION_HEADER* sectHead;
66 int i;
68 if (rva == 0) return NULL;
70 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
71 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
73 if (sectHead[i].VirtualAddress <= rva &&
74 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
76 /* return image import directory offset */
77 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
81 return NULL;
84 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
86 const IMAGE_DOS_HEADER *dos;
87 dos = PRD(0, sizeof(*dos));
88 if (!dos) return NULL;
89 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
90 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
91 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
94 void print_fake_dll( void )
96 const IMAGE_DOS_HEADER *dos;
98 dos = PRD(0, sizeof(*dos) + 32);
99 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
101 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
102 printf( "*** This is a Wine builtin DLL ***\n\n" );
103 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
104 printf( "*** This is a Wine fake DLL ***\n\n" );
108 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
110 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
112 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
113 if (idx >= opt->NumberOfRvaAndSizes)
114 return NULL;
115 if(size)
116 *size = opt->DataDirectory[idx].Size;
117 return RVA(opt->DataDirectory[idx].VirtualAddress,
118 opt->DataDirectory[idx].Size);
120 else
122 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
123 if (idx >= opt->NumberOfRvaAndSizes)
124 return NULL;
125 if(size)
126 *size = opt->DataDirectory[idx].Size;
127 return RVA(opt->DataDirectory[idx].VirtualAddress,
128 opt->DataDirectory[idx].Size);
132 static const void* get_dir(unsigned idx)
134 return get_dir_and_size(idx, 0);
137 static const char * const DirectoryNames[16] = {
138 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
139 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
140 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
141 "IAT", "Delay IAT", "CLR Header", ""
144 static const char *get_magic_type(WORD magic)
146 switch(magic) {
147 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
148 return "32bit";
149 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
150 return "64bit";
151 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
152 return "ROM";
154 return "???";
157 static inline void print_word(const char *title, WORD value)
159 printf(" %-34s 0x%-4X %u\n", title, value, value);
162 static inline void print_dword(const char *title, UINT value)
164 printf(" %-34s 0x%-8x %u\n", title, value, value);
167 static inline void print_longlong(const char *title, ULONGLONG value)
169 printf(" %-34s 0x", title);
170 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
171 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
172 else
173 printf("%lx\n", (unsigned long)value);
176 static inline void print_ver(const char *title, BYTE major, BYTE minor)
178 printf(" %-34s %u.%02u\n", title, major, minor);
181 static inline void print_subsys(const char *title, WORD value)
183 const char *str;
184 switch (value)
186 default:
187 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
188 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
189 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
190 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
191 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
192 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
193 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
194 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
195 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
196 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
197 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
198 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
199 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
200 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
202 printf(" %-34s 0x%X (%s)\n", title, value, str);
205 static inline void print_dllflags(const char *title, WORD value)
207 printf(" %-34s 0x%04X\n", title, value);
208 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
209 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
210 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
211 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
212 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
213 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
214 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
215 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
216 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
217 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
218 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
219 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
220 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
221 #undef X
224 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
226 unsigned i;
227 printf("Data Directory\n");
229 for (i = 0; i < n && i < 16; i++)
231 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
232 DirectoryNames[i], (UINT)directory[i].VirtualAddress,
233 (UINT)directory[i].Size);
237 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
239 IMAGE_OPTIONAL_HEADER32 oh;
240 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
242 /* in case optional header is missing or partial */
243 memset(&oh, 0, sizeof(oh));
244 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
245 optionalHeader = &oh;
247 print_word("Magic", optionalHeader->Magic);
248 print_ver("linker version",
249 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
250 print_dword("size of code", optionalHeader->SizeOfCode);
251 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
252 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
253 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
254 print_dword("base of code", optionalHeader->BaseOfCode);
255 print_dword("base of data", optionalHeader->BaseOfData);
256 print_dword("image base", optionalHeader->ImageBase);
257 print_dword("section align", optionalHeader->SectionAlignment);
258 print_dword("file align", optionalHeader->FileAlignment);
259 print_ver("required OS version",
260 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
261 print_ver("image version",
262 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
263 print_ver("subsystem version",
264 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
265 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
266 print_dword("size of image", optionalHeader->SizeOfImage);
267 print_dword("size of headers", optionalHeader->SizeOfHeaders);
268 print_dword("checksum", optionalHeader->CheckSum);
269 print_subsys("Subsystem", optionalHeader->Subsystem);
270 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
271 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
272 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
273 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
274 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
275 print_dword("loader flags", optionalHeader->LoaderFlags);
276 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
277 printf("\n");
278 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
279 printf("\n");
282 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
284 IMAGE_OPTIONAL_HEADER64 oh;
285 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
287 /* in case optional header is missing or partial */
288 memset(&oh, 0, sizeof(oh));
289 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
290 optionalHeader = &oh;
292 print_word("Magic", optionalHeader->Magic);
293 print_ver("linker version",
294 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
295 print_dword("size of code", optionalHeader->SizeOfCode);
296 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
297 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
298 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
299 print_dword("base of code", optionalHeader->BaseOfCode);
300 print_longlong("image base", optionalHeader->ImageBase);
301 print_dword("section align", optionalHeader->SectionAlignment);
302 print_dword("file align", optionalHeader->FileAlignment);
303 print_ver("required OS version",
304 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
305 print_ver("image version",
306 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
307 print_ver("subsystem version",
308 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
309 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
310 print_dword("size of image", optionalHeader->SizeOfImage);
311 print_dword("size of headers", optionalHeader->SizeOfHeaders);
312 print_dword("checksum", optionalHeader->CheckSum);
313 print_subsys("Subsystem", optionalHeader->Subsystem);
314 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
315 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
316 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
317 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
318 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
319 print_dword("loader flags", optionalHeader->LoaderFlags);
320 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
321 printf("\n");
322 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
323 printf("\n");
326 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
328 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
330 switch(optionalHeader->Magic) {
331 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
332 dump_optional_header32(optionalHeader, header_size);
333 break;
334 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
335 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
336 break;
337 default:
338 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
339 break;
343 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
345 printf("File Header\n");
347 printf(" Machine: %04X (%s)\n",
348 fileHeader->Machine, get_machine_str(fileHeader->Machine));
349 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
350 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
351 (UINT)fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
352 Offset(&(fileHeader->TimeDateStamp)));
353 printf(" PointerToSymbolTable: %08X\n", (UINT)fileHeader->PointerToSymbolTable);
354 printf(" NumberOfSymbols: %08X\n", (UINT)fileHeader->NumberOfSymbols);
355 printf(" SizeOfOptionalHeader: %04X\n", (UINT)fileHeader->SizeOfOptionalHeader);
356 printf(" Characteristics: %04X\n", (UINT)fileHeader->Characteristics);
357 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
358 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
359 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
360 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
361 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
362 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
363 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
364 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
365 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
366 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
367 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
368 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
369 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
370 X(IMAGE_FILE_SYSTEM, "SYSTEM");
371 X(IMAGE_FILE_DLL, "DLL");
372 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
373 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
374 #undef X
375 printf("\n");
378 static void dump_pe_header(void)
380 dump_file_header(&PE_nt_headers->FileHeader);
381 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
384 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
386 unsigned offset;
388 /* long section name ? */
389 if (strtable && sectHead->Name[0] == '/' &&
390 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
391 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
392 else
393 printf(" %-8.8s", sectHead->Name);
394 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
395 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
396 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
397 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
398 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
399 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
400 printf(" line # offs: %-8u line #'s: %-8u\n",
401 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
402 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
403 printf(" ");
404 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
405 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
406 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
407 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
408 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
409 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
410 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
412 X(IMAGE_SCN_CNT_CODE, "CODE");
413 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
414 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
416 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
417 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
418 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
419 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
420 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
422 /* 0x00002000 - Reserved */
423 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
424 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
426 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
427 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
428 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
429 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
430 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
432 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
434 #define X2(b,s) case b: printf(" " s); break
435 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
436 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
437 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
438 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
439 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
440 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
441 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
442 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
443 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
444 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
445 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
446 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
447 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
448 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
449 #undef X2
452 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
454 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
455 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
456 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
457 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
458 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
459 X(IMAGE_SCN_MEM_READ, "MEM_READ");
460 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
461 #undef X
462 printf("\n\n");
465 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
467 const IMAGE_SECTION_HEADER* sectHead = addr;
468 unsigned i;
469 const char* strtable;
471 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
473 strtable = (const char*)base +
474 PE_nt_headers->FileHeader.PointerToSymbolTable +
475 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
477 else strtable = NULL;
479 printf("Section Table\n");
480 for (i = 0; i < num_sect; i++, sectHead++)
482 dump_section(sectHead, strtable);
484 if (globals.do_dump_rawdata)
486 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
487 printf("\n");
492 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
494 const WCHAR *wstr = PRD( rva, len );
495 char *ret = buffer;
497 len /= sizeof(WCHAR);
498 while (len--) *buffer++ = *wstr++;
499 *buffer = 0;
500 return ret;
503 static void dump_section_apiset(void)
505 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
506 const UINT *ptr, *entry, *value, *hash;
507 unsigned int i, j, count, val_count, rva;
508 char buffer[128];
510 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
512 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
513 rva = sect->PointerToRawData;
514 ptr = PRD( rva, sizeof(*ptr) );
515 printf( "ApiSet section:\n" );
516 switch (ptr[0]) /* version */
518 case 2:
519 printf( " Version: %u\n", ptr[0] );
520 printf( " Count: %08x\n", ptr[1] );
521 count = ptr[1];
522 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
523 for (i = 0; i < count; i++, entry += 3)
525 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
526 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
527 val_count = *value++;
528 for (j = 0; j < val_count; j++, value += 4)
530 putchar( ' ' );
531 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
532 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
534 printf( "\n");
536 break;
537 case 4:
538 printf( " Version: %u\n", ptr[0] );
539 printf( " Size: %08x\n", ptr[1] );
540 printf( " Flags: %08x\n", ptr[2] );
541 printf( " Count: %08x\n", ptr[3] );
542 count = ptr[3];
543 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
544 for (i = 0; i < count; i++, entry += 6)
546 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
547 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
548 value++; /* flags */
549 val_count = *value++;
550 for (j = 0; j < val_count; j++, value += 5)
552 putchar( ' ' );
553 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
554 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
556 printf( "\n");
558 break;
559 case 6:
560 printf( " Version: %u\n", ptr[0] );
561 printf( " Size: %08x\n", ptr[1] );
562 printf( " Flags: %08x\n", ptr[2] );
563 printf( " Count: %08x\n", ptr[3] );
564 printf( " EntryOffset: %08x\n", ptr[4] );
565 printf( " HashOffset: %08x\n", ptr[5] );
566 printf( " HashFactor: %08x\n", ptr[6] );
567 count = ptr[3];
568 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
569 for (i = 0; i < count; i++, entry += 6)
571 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
572 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
573 for (j = 0; j < entry[5]; j++, value += 5)
575 putchar( ' ' );
576 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
577 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
579 printf( "\n" );
581 printf( " Hash table:\n" );
582 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
583 for (i = 0; i < count; i++, hash += 2)
585 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
586 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
588 break;
589 default:
590 printf( "*** Unknown version %u\n", ptr[0] );
591 break;
593 break;
597 static void dump_dir_exported_functions(void)
599 unsigned int size = 0;
600 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
601 UINT i, *funcs;
602 const UINT *pFunc;
603 const UINT *pName;
604 const WORD *pOrdl;
606 if (!exportDir) return;
608 printf("Exports table:\n");
609 printf("\n");
610 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
611 printf(" Characteristics: %08x\n", (UINT)exportDir->Characteristics);
612 printf(" TimeDateStamp: %08X %s\n",
613 (UINT)exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
614 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
615 printf(" Ordinal base: %u\n", (UINT)exportDir->Base);
616 printf(" # of functions: %u\n", (UINT)exportDir->NumberOfFunctions);
617 printf(" # of Names: %u\n", (UINT)exportDir->NumberOfNames);
618 printf("Addresses of functions: %08X\n", (UINT)exportDir->AddressOfFunctions);
619 printf("Addresses of name ordinals: %08X\n", (UINT)exportDir->AddressOfNameOrdinals);
620 printf("Addresses of names: %08X\n", (UINT)exportDir->AddressOfNames);
621 printf("\n");
622 printf(" Entry Pt Ordn Name\n");
624 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
625 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
626 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
627 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
629 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
630 if (!funcs) fatal("no memory");
632 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
634 for (i = 0; i < exportDir->NumberOfFunctions; i++)
636 if (!pFunc[i]) continue;
637 printf(" %08X %5u ", pFunc[i], (UINT)exportDir->Base + i);
638 if (funcs[i])
639 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
640 else
641 printf("<by ordinal>");
643 /* check for forwarded function */
644 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
645 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
646 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
647 printf("\n");
649 free(funcs);
650 printf("\n");
654 struct runtime_function_x86_64
656 UINT BeginAddress;
657 UINT EndAddress;
658 UINT UnwindData;
661 struct runtime_function_armnt
663 UINT BeginAddress;
664 union {
665 UINT UnwindData;
666 struct {
667 UINT Flag : 2;
668 UINT FunctionLength : 11;
669 UINT Ret : 2;
670 UINT H : 1;
671 UINT Reg : 3;
672 UINT R : 1;
673 UINT L : 1;
674 UINT C : 1;
675 UINT StackAdjust : 10;
680 struct runtime_function_arm64
682 UINT BeginAddress;
683 union
685 UINT UnwindData;
686 struct
688 UINT Flag : 2;
689 UINT FunctionLength : 11;
690 UINT RegF : 3;
691 UINT RegI : 4;
692 UINT H : 1;
693 UINT CR : 2;
694 UINT FrameSize : 9;
699 union handler_data
701 struct runtime_function_x86_64 chain;
702 UINT handler;
705 struct opcode
707 BYTE offset;
708 BYTE code : 4;
709 BYTE info : 4;
712 struct unwind_info_x86_64
714 BYTE version : 3;
715 BYTE flags : 5;
716 BYTE prolog;
717 BYTE count;
718 BYTE frame_reg : 4;
719 BYTE frame_offset : 4;
720 struct opcode opcodes[1]; /* count entries */
721 /* followed by union handler_data */
724 struct unwind_info_armnt
726 UINT function_length : 18;
727 UINT version : 2;
728 UINT x : 1;
729 UINT e : 1;
730 UINT f : 1;
731 UINT count : 5;
732 UINT words : 4;
735 struct unwind_info_ext_armnt
737 WORD excount;
738 BYTE exwords;
739 BYTE reserved;
742 struct unwind_info_epilogue_armnt
744 UINT offset : 18;
745 UINT res : 2;
746 UINT cond : 4;
747 UINT index : 8;
750 #define UWOP_PUSH_NONVOL 0
751 #define UWOP_ALLOC_LARGE 1
752 #define UWOP_ALLOC_SMALL 2
753 #define UWOP_SET_FPREG 3
754 #define UWOP_SAVE_NONVOL 4
755 #define UWOP_SAVE_NONVOL_FAR 5
756 #define UWOP_SAVE_XMM128 8
757 #define UWOP_SAVE_XMM128_FAR 9
758 #define UWOP_PUSH_MACHFRAME 10
760 #define UNW_FLAG_EHANDLER 1
761 #define UNW_FLAG_UHANDLER 2
762 #define UNW_FLAG_CHAININFO 4
764 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
766 static const char * const reg_names[16] =
767 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
768 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
770 const union handler_data *handler_data;
771 const struct unwind_info_x86_64 *info;
772 unsigned int i, count;
774 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
775 if (function->UnwindData & 1)
777 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
778 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
779 return;
781 info = RVA( function->UnwindData, sizeof(*info) );
783 printf( " unwind info at %08x\n", function->UnwindData );
784 if (info->version != 1)
786 printf( " *** unknown version %u\n", info->version );
787 return;
789 printf( " flags %x", info->flags );
790 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
791 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
792 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
793 printf( "\n prolog 0x%x bytes\n", info->prolog );
795 if (info->frame_reg)
796 printf( " frame register %s offset 0x%x(%%rsp)\n",
797 reg_names[info->frame_reg], info->frame_offset * 16 );
799 for (i = 0; i < info->count; i++)
801 printf( " 0x%02x: ", info->opcodes[i].offset );
802 switch (info->opcodes[i].code)
804 case UWOP_PUSH_NONVOL:
805 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
806 break;
807 case UWOP_ALLOC_LARGE:
808 if (info->opcodes[i].info)
810 count = *(const UINT *)&info->opcodes[i+1];
811 i += 2;
813 else
815 count = *(const USHORT *)&info->opcodes[i+1] * 8;
816 i++;
818 printf( "sub $0x%x,%%rsp\n", count );
819 break;
820 case UWOP_ALLOC_SMALL:
821 count = (info->opcodes[i].info + 1) * 8;
822 printf( "sub $0x%x,%%rsp\n", count );
823 break;
824 case UWOP_SET_FPREG:
825 printf( "lea 0x%x(%%rsp),%s\n",
826 info->frame_offset * 16, reg_names[info->frame_reg] );
827 break;
828 case UWOP_SAVE_NONVOL:
829 count = *(const USHORT *)&info->opcodes[i+1] * 8;
830 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
831 i++;
832 break;
833 case UWOP_SAVE_NONVOL_FAR:
834 count = *(const UINT *)&info->opcodes[i+1];
835 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
836 i += 2;
837 break;
838 case UWOP_SAVE_XMM128:
839 count = *(const USHORT *)&info->opcodes[i+1] * 16;
840 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
841 i++;
842 break;
843 case UWOP_SAVE_XMM128_FAR:
844 count = *(const UINT *)&info->opcodes[i+1];
845 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
846 i += 2;
847 break;
848 case UWOP_PUSH_MACHFRAME:
849 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
850 break;
851 default:
852 printf( "*** unknown code %u\n", info->opcodes[i].code );
853 break;
857 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
858 if (info->flags & UNW_FLAG_CHAININFO)
860 printf( " -> function %08x-%08x\n",
861 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
862 return;
864 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
865 printf( " handler %08x data at %08x\n", handler_data->handler,
866 (UINT)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
869 static const BYTE armnt_code_lengths[256] =
871 /* 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,
872 /* 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,
873 /* 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,
874 /* 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,
875 /* 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,
876 /* 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,
877 /* 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,
878 /* 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
881 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
883 const struct unwind_info_armnt *info;
884 const struct unwind_info_ext_armnt *infoex;
885 const struct unwind_info_epilogue_armnt *infoepi;
886 unsigned int rva;
887 WORD i, count = 0, words = 0;
889 if (fnc->Flag)
891 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
892 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
894 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
895 (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2 );
896 printf( " Flag %x\n", fnc->Flag );
897 printf( " FunctionLength %x\n", fnc->FunctionLength );
898 printf( " Ret %x\n", fnc->Ret );
899 printf( " H %x\n", fnc->H );
900 printf( " Reg %x\n", fnc->Reg );
901 printf( " R %x\n", fnc->R );
902 printf( " L %x\n", fnc->L );
903 printf( " C %x\n", fnc->C );
904 printf( " StackAdjust %x\n", fnc->StackAdjust );
906 if (fnc->StackAdjust >= 0x03f4)
908 pf = fnc->StackAdjust & 0x04;
909 ef = fnc->StackAdjust & 0x08;
910 stack = (fnc->StackAdjust & 3) + 1;
913 if (!fnc->R || pf)
915 int first = 4, last = fnc->Reg + 4;
916 if (pf)
918 first = (~fnc->StackAdjust) & 3;
919 if (fnc->R)
920 last = 3;
922 if (first == last)
923 sprintf(intregs, "r%u", first);
924 else
925 sprintf(intregs, "r%u-r%u", first, last);
926 fpoffset = last + 1 - first;
929 if (!fnc->R || ef)
931 int first = 4, last = fnc->Reg + 4;
932 if (ef)
934 first = (~fnc->StackAdjust) & 3;
935 if (fnc->R)
936 last = 3;
938 if (first == last)
939 sprintf(intregspop, "r%u", first);
940 else
941 sprintf(intregspop, "r%u-r%u", first, last);
944 if (fnc->C)
946 if (intregs[0])
947 strcat(intregs, ", ");
948 if (intregspop[0])
949 strcat(intregspop, ", ");
950 strcat(intregs, "r11");
951 strcat(intregspop, "r11");
953 if (fnc->L)
955 if (intregs[0])
956 strcat(intregs, ", ");
957 strcat(intregs, "lr");
959 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
960 strcat(intregspop, ", ");
961 if (fnc->Ret != 0)
962 strcat(intregspop, "lr");
963 else if (!fnc->H)
964 strcat(intregspop, "pc");
967 if (fnc->R)
969 if (fnc->Reg)
970 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
971 else
972 strcpy(vfpregs, "d8");
975 if (fnc->Flag == 1) {
976 if (fnc->H)
977 printf( " Unwind Code\tpush {r0-r3}\n" );
979 if (intregs[0])
980 printf( " Unwind Code\tpush {%s}\n", intregs );
982 if (fnc->C && fpoffset == 0)
983 printf( " Unwind Code\tmov r11, sp\n" );
984 else if (fnc->C)
985 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
987 if (fnc->R && fnc->Reg != 0x07)
988 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
990 if (stack && !pf)
991 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
994 if (fnc->Ret == 3)
995 return;
996 printf( "Epilogue:\n" );
998 if (stack && !ef)
999 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1001 if (fnc->R && fnc->Reg != 0x07)
1002 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1004 if (intregspop[0])
1005 printf( " Unwind Code\tpop {%s}\n", intregspop );
1007 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1008 printf( " Unwind Code\tadd sp, sp, #16\n" );
1009 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1010 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1012 if (fnc->Ret == 1)
1013 printf( " Unwind Code\tbx <reg>\n" );
1014 else if (fnc->Ret == 2)
1015 printf( " Unwind Code\tb <address>\n" );
1017 return;
1020 info = RVA( fnc->UnwindData, sizeof(*info) );
1021 rva = fnc->UnwindData + sizeof(*info);
1022 count = info->count;
1023 words = info->words;
1025 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1026 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1027 printf( " unwind info at %08x\n", fnc->UnwindData );
1028 printf( " Flag %x\n", fnc->Flag );
1029 printf( " FunctionLength %x\n", info->function_length );
1030 printf( " Version %x\n", info->version );
1031 printf( " X %x\n", info->x );
1032 printf( " E %x\n", info->e );
1033 printf( " F %x\n", info->f );
1034 printf( " Count %x\n", count );
1035 printf( " Words %x\n", words );
1037 if (!info->count && !info->words)
1039 infoex = RVA( rva, sizeof(*infoex) );
1040 rva = rva + sizeof(*infoex);
1041 count = infoex->excount;
1042 words = infoex->exwords;
1043 printf( " ExtCount %x\n", count );
1044 printf( " ExtWords %x\n", words );
1047 if (!info->e)
1049 infoepi = RVA( rva, count * sizeof(*infoepi) );
1050 rva = rva + count * sizeof(*infoepi);
1052 for (i = 0; i < count; i++)
1054 printf( " Epilogue Scope %x\n", i );
1055 printf( " Offset %x\n", infoepi[i].offset );
1056 printf( " Reserved %x\n", infoepi[i].res );
1057 printf( " Condition %x\n", infoepi[i].cond );
1058 printf( " Index %x\n", infoepi[i].index );
1061 else
1062 infoepi = NULL;
1064 if (words)
1066 const unsigned int *codes;
1067 BYTE b, *bytes;
1068 BOOL inepilogue = FALSE;
1070 codes = RVA( rva, words * sizeof(*codes) );
1071 rva = rva + words * sizeof(*codes);
1072 bytes = (BYTE*)codes;
1074 for (b = 0; b < words * sizeof(*codes); b++)
1076 BYTE code = bytes[b];
1077 BYTE len = armnt_code_lengths[code];
1079 if (info->e && b == count)
1081 printf( "Epilogue:\n" );
1082 inepilogue = TRUE;
1084 else if (!info->e && infoepi)
1086 for (i = 0; i < count; i++)
1087 if (b == infoepi[i].index)
1089 printf( "Epilogue from Scope %x at %08x:\n", i,
1090 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1091 inepilogue = TRUE;
1095 printf( " Unwind Code");
1096 for (i = 0; i < len; i++)
1097 printf( " %02x", bytes[b+i] );
1098 printf( "\t" );
1100 if (code == 0x00)
1101 printf( "\n" );
1102 else if (code <= 0x7f)
1103 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1104 else if (code <= 0xbf)
1106 WORD excode, f;
1107 BOOL first = TRUE;
1108 BYTE excodes = bytes[++b];
1110 excode = (code << 8) | excodes;
1111 printf( "%s {", inepilogue ? "pop" : "push" );
1113 for (f = 0; f <= 12; f++)
1115 if ((excode >> f) & 1)
1117 printf( "%sr%u", first ? "" : ", ", f );
1118 first = FALSE;
1122 if (excode & 0x2000)
1123 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1125 printf( "}\n" );
1127 else if (code <= 0xcf)
1128 if (inepilogue)
1129 printf( "mov sp, r%u\n", code & 0x0f );
1130 else
1131 printf( "mov r%u, sp\n", code & 0x0f );
1132 else if (code <= 0xd7)
1133 if (inepilogue)
1134 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1135 else
1136 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1137 else if (code <= 0xdf)
1138 if (inepilogue)
1139 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1140 else
1141 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1142 else if (code <= 0xe7)
1143 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1144 else if (code <= 0xeb)
1146 WORD excode;
1147 BYTE excodes = bytes[++b];
1149 excode = (code << 8) | excodes;
1150 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1152 else if (code <= 0xed)
1154 WORD excode, f;
1155 BOOL first = TRUE;
1156 BYTE excodes = bytes[++b];
1158 excode = (code << 8) | excodes;
1159 printf( "%s {", inepilogue ? "pop" : "push" );
1161 for (f = 0; f < 8; f++)
1163 if ((excode >> f) & 1)
1165 printf( "%sr%u", first ? "" : ", ", f );
1166 first = FALSE;
1170 if (excode & 0x0100)
1171 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1173 printf( "}\n" );
1175 else if (code == 0xee)
1176 printf( "unknown 16\n" );
1177 else if (code == 0xef)
1179 WORD excode;
1180 BYTE excodes = bytes[++b];
1182 if (excodes <= 0x0f)
1184 excode = (code << 8) | excodes;
1185 if (inepilogue)
1186 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1187 else
1188 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1190 else
1191 printf( "unknown 32\n" );
1193 else if (code <= 0xf4)
1194 printf( "unknown\n" );
1195 else if (code <= 0xf6)
1197 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1198 BYTE excodes = bytes[++b];
1200 excode = (code << 8) | excodes;
1201 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1202 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1204 else if (code <= 0xf7)
1206 unsigned int excode;
1207 BYTE excodes[2];
1209 excodes[0] = bytes[++b];
1210 excodes[1] = bytes[++b];
1211 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1212 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1214 else if (code <= 0xf8)
1216 unsigned int excode;
1217 BYTE excodes[3];
1219 excodes[0] = bytes[++b];
1220 excodes[1] = bytes[++b];
1221 excodes[2] = bytes[++b];
1222 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1223 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1225 else if (code <= 0xf9)
1227 unsigned int excode;
1228 BYTE excodes[2];
1230 excodes[0] = bytes[++b];
1231 excodes[1] = bytes[++b];
1232 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1233 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1235 else if (code <= 0xfa)
1237 unsigned int excode;
1238 BYTE excodes[3];
1240 excodes[0] = bytes[++b];
1241 excodes[1] = bytes[++b];
1242 excodes[2] = bytes[++b];
1243 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1244 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1246 else if (code <= 0xfb)
1247 printf( "nop\n" );
1248 else if (code <= 0xfc)
1249 printf( "nop.w\n" );
1250 else if (code <= 0xfd)
1252 printf( "(end) nop\n" );
1253 inepilogue = TRUE;
1255 else if (code <= 0xfe)
1257 printf( "(end) nop.w\n" );
1258 inepilogue = TRUE;
1260 else
1262 printf( "end\n" );
1263 inepilogue = TRUE;
1268 if (info->x)
1270 const unsigned int *handler;
1272 handler = RVA( rva, sizeof(*handler) );
1273 rva = rva + sizeof(*handler);
1275 printf( " handler %08x data at %08x\n", *handler, rva);
1279 struct unwind_info_arm64
1281 UINT function_length : 18;
1282 UINT version : 2;
1283 UINT x : 1;
1284 UINT e : 1;
1285 UINT epilog : 5;
1286 UINT codes : 5;
1289 struct unwind_info_ext_arm64
1291 WORD epilog;
1292 BYTE codes;
1293 BYTE reserved;
1296 struct unwind_info_epilog_arm64
1298 UINT offset : 18;
1299 UINT res : 4;
1300 UINT index : 10;
1303 static const BYTE code_lengths[256] =
1305 /* 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,
1306 /* 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,
1307 /* 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,
1308 /* 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,
1309 /* 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,
1310 /* 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,
1311 /* 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,
1312 /* 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
1315 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1317 unsigned int i, j;
1319 for (i = 0; i < count; i += code_lengths[ptr[i]])
1321 BYTE len = code_lengths[ptr[i]];
1322 unsigned int val = ptr[i];
1323 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1324 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1326 printf( " %04x: ", i );
1327 for (j = 0; j < 4; j++)
1328 if (j < len) printf( "%02x ", ptr[i+j] );
1329 else printf( " " );
1331 if (ptr[i] < 0x20) /* alloc_s */
1333 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1335 else if (ptr[i] < 0x40) /* save_r19r20_x */
1337 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1339 else if (ptr[i] < 0x80) /* save_fplr */
1341 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1343 else if (ptr[i] < 0xc0) /* save_fplr_x */
1345 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1347 else if (ptr[i] < 0xc8) /* alloc_m */
1349 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1351 else if (ptr[i] < 0xcc) /* save_regp */
1353 int reg = 19 + ((val >> 6) & 0xf);
1354 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1356 else if (ptr[i] < 0xd0) /* save_regp_x */
1358 int reg = 19 + ((val >> 6) & 0xf);
1359 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1361 else if (ptr[i] < 0xd4) /* save_reg */
1363 int reg = 19 + ((val >> 6) & 0xf);
1364 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1366 else if (ptr[i] < 0xd6) /* save_reg_x */
1368 int reg = 19 + ((val >> 5) & 0xf);
1369 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1371 else if (ptr[i] < 0xd8) /* save_lrpair */
1373 int reg = 19 + 2 * ((val >> 6) & 0x7);
1374 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1376 else if (ptr[i] < 0xda) /* save_fregp */
1378 int reg = 8 + ((val >> 6) & 0x7);
1379 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1381 else if (ptr[i] < 0xdc) /* save_fregp_x */
1383 int reg = 8 + ((val >> 6) & 0x7);
1384 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1386 else if (ptr[i] < 0xde) /* save_freg */
1388 int reg = 8 + ((val >> 6) & 0x7);
1389 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1391 else if (ptr[i] == 0xde) /* save_freg_x */
1393 int reg = 8 + ((val >> 5) & 0x7);
1394 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1396 else if (ptr[i] == 0xe0) /* alloc_l */
1398 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1400 else if (ptr[i] == 0xe1) /* set_fp */
1402 printf( "mov x29,sp\n" );
1404 else if (ptr[i] == 0xe2) /* add_fp */
1406 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1408 else if (ptr[i] == 0xe3) /* nop */
1410 printf( "nop\n" );
1412 else if (ptr[i] == 0xe4) /* end */
1414 printf( "end\n" );
1416 else if (ptr[i] == 0xe5) /* end_c */
1418 printf( "end_c\n" );
1420 else if (ptr[i] == 0xe6) /* save_next */
1422 printf( "save_next\n" );
1424 else if (ptr[i] == 0xe7) /* arithmetic */
1426 switch ((val >> 4) & 0x0f)
1428 case 0: printf( "add lr,lr,x28\n" ); break;
1429 case 1: printf( "add lr,lr,sp\n" ); break;
1430 case 2: printf( "sub lr,lr,x28\n" ); break;
1431 case 3: printf( "sub lr,lr,sp\n" ); break;
1432 case 4: printf( "eor lr,lr,x28\n" ); break;
1433 case 5: printf( "eor lr,lr,sp\n" ); break;
1434 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1435 case 8: printf( "ror lr,lr,x28\n" ); break;
1436 case 9: printf( "ror lr,lr,sp\n" ); break;
1437 default:printf( "unknown op\n" ); break;
1440 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1442 printf( "MSFT_OP_TRAP_FRAME\n" );
1444 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1446 printf( "MSFT_OP_MACHINE_FRAME\n" );
1448 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1450 printf( "MSFT_OP_CONTEXT\n" );
1452 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1454 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1456 else printf( "??\n");
1460 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1462 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
1464 if (func->CR == 1) intsz += 8;
1465 if (func->RegF) fpsz += 8;
1467 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
1468 locsz = func->FrameSize * 16 - savesz;
1470 switch (func->CR)
1472 case 3:
1473 printf( " %04x: mov x29,sp\n", pos++ );
1474 if (locsz <= 512)
1476 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1477 break;
1479 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1480 /* fall through */
1481 case 0:
1482 case 1:
1483 if (locsz <= 4080)
1485 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1487 else
1489 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1490 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1492 break;
1495 if (func->H)
1497 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1498 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1499 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1500 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1503 if (func->RegF)
1505 if (func->RegF % 2 == 0)
1506 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
1507 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
1509 if (!i && !intsz)
1510 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1511 else
1512 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1516 switch (func->RegI)
1518 case 0:
1519 if (func->CR == 1)
1520 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1521 break;
1522 case 1:
1523 if (func->CR == 1)
1524 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1525 else
1526 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1527 break;
1528 default:
1529 if (func->RegI % 2)
1531 if (func->CR == 1)
1532 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1533 else
1534 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1536 else if (func->CR == 1)
1537 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1539 for (i = func->RegI / 2 - 1; i >= 0; i--)
1540 if (i)
1541 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1542 else
1543 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1544 break;
1546 printf( " %04x: end\n", pos );
1549 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1551 const struct unwind_info_arm64 *info;
1552 const struct unwind_info_ext_arm64 *infoex;
1553 const struct unwind_info_epilog_arm64 *infoepi;
1554 const BYTE *ptr;
1555 unsigned int i, rva, codes, epilogs;
1557 if (func->Flag)
1559 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1560 func->BeginAddress + func->FunctionLength * 4 );
1561 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1562 func->FunctionLength, func->Flag, func->RegF, func->RegI,
1563 func->H, func->CR, func->FrameSize );
1564 dump_arm64_packed_info( func );
1565 return;
1568 rva = func->UnwindData;
1569 info = RVA( rva, sizeof(*info) );
1570 rva += sizeof(*info);
1571 epilogs = info->epilog;
1572 codes = info->codes;
1574 if (!codes)
1576 infoex = RVA( rva, sizeof(*infoex) );
1577 rva = rva + sizeof(*infoex);
1578 codes = infoex->codes;
1579 epilogs = infoex->epilog;
1581 printf( "\nFunction %08x-%08x:\n",
1582 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1583 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1584 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1585 if (info->e)
1587 printf( " epilog 0: code=%04x\n", info->epilog );
1589 else
1591 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1592 rva += sizeof(*infoepi) * epilogs;
1593 for (i = 0; i < epilogs; i++)
1594 printf( " epilog %u: pc=%08x code=%04x\n", i,
1595 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1597 ptr = RVA( rva, codes * 4);
1598 rva += codes * 4;
1599 if (info->x)
1601 const UINT *handler = RVA( rva, sizeof(*handler) );
1602 rva += sizeof(*handler);
1603 printf( " handler: %08x data %08x\n", *handler, rva );
1605 dump_arm64_codes( ptr, codes * 4 );
1608 static void dump_dir_exceptions(void)
1610 unsigned int i, size = 0;
1611 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1612 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1614 if (!funcs) return;
1616 switch (file_header->Machine)
1618 case IMAGE_FILE_MACHINE_AMD64:
1619 size /= sizeof(struct runtime_function_x86_64);
1620 printf( "Exception info (%u functions):\n", size );
1621 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1622 break;
1623 case IMAGE_FILE_MACHINE_ARMNT:
1624 size /= sizeof(struct runtime_function_armnt);
1625 printf( "Exception info (%u functions):\n", size );
1626 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1627 break;
1628 case IMAGE_FILE_MACHINE_ARM64:
1629 size /= sizeof(struct runtime_function_arm64);
1630 printf( "Exception info (%u functions):\n", size );
1631 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1632 break;
1633 default:
1634 printf( "Exception information not supported for %s binaries\n",
1635 get_machine_str(file_header->Machine));
1636 break;
1641 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
1643 /* FIXME: This does not properly handle large images */
1644 const IMAGE_IMPORT_BY_NAME* iibn;
1645 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1647 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1648 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
1649 else
1651 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1652 if (!iibn)
1653 printf("Can't grab import by name info, skipping to next ordinal\n");
1654 else
1655 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1660 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
1662 const IMAGE_IMPORT_BY_NAME* iibn;
1663 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
1665 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1666 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
1667 else
1669 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1670 if (!iibn)
1671 printf("Can't grab import by name info, skipping to next ordinal\n");
1672 else
1673 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1678 static void dump_dir_imported_functions(void)
1680 unsigned directorySize;
1681 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1683 if (!importDesc) return;
1685 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1687 for (;;)
1689 const IMAGE_THUNK_DATA32* il;
1691 if (!importDesc->Name || !importDesc->FirstThunk) break;
1693 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1694 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
1695 printf(" TimeDateStamp: %08X (%s)\n",
1696 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1697 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
1698 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
1700 printf(" Thunk Ordn Name\n");
1702 il = (importDesc->OriginalFirstThunk != 0) ?
1703 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
1704 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1706 if (!il)
1707 printf("Can't grab thunk data, going to next imported DLL\n");
1708 else
1710 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1711 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1712 else
1713 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1714 printf("\n");
1716 importDesc++;
1718 printf("\n");
1721 static void dump_dir_loadconfig(void)
1723 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1724 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1726 if (!loadcfg32) return;
1728 printf( "Loadconfig\n" );
1729 print_dword( "Size", loadcfg32->Size );
1730 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1731 print_word( "MajorVersion", loadcfg32->MajorVersion );
1732 print_word( "MinorVersion", loadcfg32->MinorVersion );
1733 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1734 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1735 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1737 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1739 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1740 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1741 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1742 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1743 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1744 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1745 print_word( "CSDVersion", loadcfg64->CSDVersion );
1746 print_word( "Reserved", loadcfg64->Reserved1 );
1747 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1748 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1749 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1751 else
1753 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1754 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1755 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1756 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1757 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1758 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1759 print_word( "CSDVersion", loadcfg32->CSDVersion );
1760 print_word( "Reserved", loadcfg32->Reserved1 );
1761 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1762 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1763 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1767 static void dump_dir_delay_imported_functions(void)
1769 unsigned directorySize;
1770 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1772 if (!importDesc) return;
1774 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1776 for (;;)
1778 const IMAGE_THUNK_DATA32* il;
1779 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1781 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1783 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
1784 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1785 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
1786 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
1787 printf(" TimeDateStamp: %08X (%s)\n",
1788 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1790 printf(" Thunk Ordn Name\n");
1792 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1794 if (!il)
1795 printf("Can't grab thunk data, going to next imported DLL\n");
1796 else
1798 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1799 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1800 else
1801 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1802 printf("\n");
1804 importDesc++;
1806 printf("\n");
1809 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1811 const char* str;
1813 printf("Directory %02u\n", idx + 1);
1814 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
1815 printf(" TimeDateStamp: %08X %s\n",
1816 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1817 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1818 switch (idd->Type)
1820 default:
1821 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1822 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1823 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1824 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1825 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1826 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1827 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1828 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1829 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1830 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1831 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1832 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1833 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1834 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1835 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1836 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1837 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
1839 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
1840 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
1841 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
1842 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
1844 switch (idd->Type)
1846 case IMAGE_DEBUG_TYPE_UNKNOWN:
1847 break;
1848 case IMAGE_DEBUG_TYPE_COFF:
1849 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1850 IMAGE_FIRST_SECTION(PE_nt_headers));
1851 break;
1852 case IMAGE_DEBUG_TYPE_CODEVIEW:
1853 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1854 break;
1855 case IMAGE_DEBUG_TYPE_FPO:
1856 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1857 break;
1858 case IMAGE_DEBUG_TYPE_MISC:
1860 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1861 if (!misc) {printf("Can't get misc debug information\n"); break;}
1862 printf(" DataType: %u (%s)\n",
1863 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1864 printf(" Length: %u\n", (UINT)misc->Length);
1865 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1866 printf(" Data: %s\n", misc->Data);
1868 break;
1869 default: break;
1871 printf("\n");
1874 static void dump_dir_debug(void)
1876 unsigned nb_dbg, i;
1877 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1879 nb_dbg /= sizeof(*debugDir);
1880 if (!debugDir || !nb_dbg) return;
1882 printf("Debug Table (%u directories)\n", nb_dbg);
1884 for (i = 0; i < nb_dbg; i++)
1886 dump_dir_debug_dir(debugDir, i);
1887 debugDir++;
1889 printf("\n");
1892 static inline void print_clrflags(const char *title, UINT value)
1894 printf(" %-34s 0x%X\n", title, value);
1895 #define X(f,s) if (value & f) printf(" %s\n", s)
1896 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1897 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1898 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1899 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1900 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1901 #undef X
1904 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1906 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
1909 static void dump_dir_clr_header(void)
1911 unsigned int size = 0;
1912 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1914 if (!dir) return;
1916 printf( "CLR Header\n" );
1917 print_dword( "Header Size", dir->cb );
1918 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1919 print_clrflags( "Flags", dir->Flags );
1920 print_dword( "EntryPointToken", dir->EntryPointToken );
1921 printf("\n");
1922 printf( "CLR Data Directory\n" );
1923 print_clrdirectory( "MetaData", &dir->MetaData );
1924 print_clrdirectory( "Resources", &dir->Resources );
1925 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1926 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1927 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1928 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1929 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1930 printf("\n");
1933 static void dump_dir_reloc(void)
1935 unsigned int i, size = 0;
1936 const USHORT *relocs;
1937 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1938 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1939 static const char * const names[] =
1941 "BASED_ABSOLUTE",
1942 "BASED_HIGH",
1943 "BASED_LOW",
1944 "BASED_HIGHLOW",
1945 "BASED_HIGHADJ",
1946 "BASED_MIPS_JMPADDR",
1947 "BASED_SECTION",
1948 "BASED_REL",
1949 "unknown 8",
1950 "BASED_IA64_IMM64",
1951 "BASED_DIR64",
1952 "BASED_HIGH3ADJ",
1953 "unknown 12",
1954 "unknown 13",
1955 "unknown 14",
1956 "unknown 15"
1959 if (!rel) return;
1961 printf( "Relocations\n" );
1962 while (rel < end - 1 && rel->SizeOfBlock)
1964 printf( " Page %x\n", (UINT)rel->VirtualAddress );
1965 relocs = (const USHORT *)(rel + 1);
1966 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1967 while (i--)
1969 USHORT offset = *relocs & 0xfff;
1970 int type = *relocs >> 12;
1971 printf( " off %04x type %s\n", offset, names[type] );
1972 relocs++;
1974 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1976 printf("\n");
1979 static void dump_dir_tls(void)
1981 IMAGE_TLS_DIRECTORY64 dir;
1982 const UINT *callbacks;
1983 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1985 if (!pdir) return;
1987 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1988 memcpy(&dir, pdir, sizeof(dir));
1989 else
1991 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1992 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1993 dir.AddressOfIndex = pdir->AddressOfIndex;
1994 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1995 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1996 dir.Characteristics = pdir->Characteristics;
1999 /* FIXME: This does not properly handle large images */
2000 printf( "Thread Local Storage\n" );
2001 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2002 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
2003 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2004 (UINT)dir.SizeOfZeroFill );
2005 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
2006 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
2007 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
2008 if (dir.AddressOfCallBacks)
2010 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2011 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
2013 printf( " %08x", *callbacks );
2014 addr += sizeof(UINT);
2017 printf(" }\n\n");
2020 enum FileSig get_kind_dbg(void)
2022 const WORD* pw;
2024 pw = PRD(0, sizeof(WORD));
2025 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2027 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2028 return SIG_UNKNOWN;
2031 void dbg_dump(void)
2033 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2034 unsigned nb_dbg;
2035 unsigned i;
2036 const IMAGE_DEBUG_DIRECTORY* debugDir;
2038 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2039 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2041 printf ("Signature: %.2s (0x%4X)\n",
2042 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2043 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2044 printf ("Machine: 0x%04X (%s)\n",
2045 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2046 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2047 printf ("TimeDateStamp: 0x%08X (%s)\n",
2048 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2049 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
2050 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
2051 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
2052 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
2053 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
2054 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
2056 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2057 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2058 {printf("Can't get the sections, aborting\n"); return;}
2060 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2062 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2063 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2064 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2065 separateDebugHead->ExportedNamesSize,
2066 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2067 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2069 printf("Debug Table (%u directories)\n", nb_dbg);
2071 for (i = 0; i < nb_dbg; i++)
2073 dump_dir_debug_dir(debugDir, i);
2074 debugDir++;
2078 static const char *get_resource_type( unsigned int id )
2080 static const char * const types[] =
2082 NULL,
2083 "CURSOR",
2084 "BITMAP",
2085 "ICON",
2086 "MENU",
2087 "DIALOG",
2088 "STRING",
2089 "FONTDIR",
2090 "FONT",
2091 "ACCELERATOR",
2092 "RCDATA",
2093 "MESSAGETABLE",
2094 "GROUP_CURSOR",
2095 NULL,
2096 "GROUP_ICON",
2097 NULL,
2098 "VERSION",
2099 "DLGINCLUDE",
2100 NULL,
2101 "PLUGPLAY",
2102 "VXD",
2103 "ANICURSOR",
2104 "ANIICON",
2105 "HTML",
2106 "RT_MANIFEST"
2109 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2110 return NULL;
2113 /* dump an ASCII string with proper escaping */
2114 static int dump_strA( const unsigned char *str, size_t len )
2116 static const char escapes[32] = ".......abtnvfr.............e....";
2117 char buffer[256];
2118 char *pos = buffer;
2119 int count = 0;
2121 for (; len; str++, len--)
2123 if (pos > buffer + sizeof(buffer) - 8)
2125 fwrite( buffer, pos - buffer, 1, stdout );
2126 count += pos - buffer;
2127 pos = buffer;
2129 if (*str > 127) /* hex escape */
2131 pos += sprintf( pos, "\\x%02x", *str );
2132 continue;
2134 if (*str < 32) /* octal or C escape */
2136 if (!*str && len == 1) continue; /* do not output terminating NULL */
2137 if (escapes[*str] != '.')
2138 pos += sprintf( pos, "\\%c", escapes[*str] );
2139 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2140 pos += sprintf( pos, "\\%03o", *str );
2141 else
2142 pos += sprintf( pos, "\\%o", *str );
2143 continue;
2145 if (*str == '\\') *pos++ = '\\';
2146 *pos++ = *str;
2148 fwrite( buffer, pos - buffer, 1, stdout );
2149 count += pos - buffer;
2150 return count;
2153 /* dump a Unicode string with proper escaping */
2154 static int dump_strW( const WCHAR *str, size_t len )
2156 static const char escapes[32] = ".......abtnvfr.............e....";
2157 char buffer[256];
2158 char *pos = buffer;
2159 int count = 0;
2161 for (; len; str++, len--)
2163 if (pos > buffer + sizeof(buffer) - 8)
2165 fwrite( buffer, pos - buffer, 1, stdout );
2166 count += pos - buffer;
2167 pos = buffer;
2169 if (*str > 127) /* hex escape */
2171 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2172 pos += sprintf( pos, "\\x%04x", *str );
2173 else
2174 pos += sprintf( pos, "\\x%x", *str );
2175 continue;
2177 if (*str < 32) /* octal or C escape */
2179 if (!*str && len == 1) continue; /* do not output terminating NULL */
2180 if (escapes[*str] != '.')
2181 pos += sprintf( pos, "\\%c", escapes[*str] );
2182 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2183 pos += sprintf( pos, "\\%03o", *str );
2184 else
2185 pos += sprintf( pos, "\\%o", *str );
2186 continue;
2188 if (*str == '\\') *pos++ = '\\';
2189 *pos++ = *str;
2191 fwrite( buffer, pos - buffer, 1, stdout );
2192 count += pos - buffer;
2193 return count;
2196 /* dump data for a STRING resource */
2197 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2199 int i;
2201 for (i = 0; i < 16 && size; i++)
2203 unsigned len = *ptr++;
2205 if (len >= size)
2207 len = size;
2208 size = 0;
2210 else size -= len + 1;
2212 if (len)
2214 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2215 dump_strW( ptr, len );
2216 printf( "\"\n" );
2217 ptr += len;
2222 /* dump data for a MESSAGETABLE resource */
2223 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2225 const MESSAGE_RESOURCE_DATA *data = ptr;
2226 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2227 unsigned i, j;
2229 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2231 const MESSAGE_RESOURCE_ENTRY *entry;
2233 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2234 for (j = block->LowId; j <= block->HighId; j++)
2236 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2238 const WCHAR *str = (const WCHAR *)entry->Text;
2239 printf( "%s%08x L\"", prefix, j );
2240 dump_strW( str, strlenW(str) );
2241 printf( "\"\n" );
2243 else
2245 const char *str = (const char *) entry->Text;
2246 printf( "%s%08x \"", prefix, j );
2247 dump_strA( entry->Text, strlen(str) );
2248 printf( "\"\n" );
2250 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2255 static void dump_dir_resource(void)
2257 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2258 const IMAGE_RESOURCE_DIRECTORY *namedir;
2259 const IMAGE_RESOURCE_DIRECTORY *langdir;
2260 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2261 const IMAGE_RESOURCE_DIR_STRING_U *string;
2262 const IMAGE_RESOURCE_DATA_ENTRY *data;
2263 int i, j, k;
2265 if (!root) return;
2267 printf( "Resources:" );
2269 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2271 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2272 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
2273 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2275 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2276 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
2277 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2279 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2281 printf( "\n " );
2282 if (e1->NameIsString)
2284 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
2285 dump_unicode_str( string->NameString, string->Length );
2287 else
2289 const char *type = get_resource_type( e1->Id );
2290 if (type) printf( "%s", type );
2291 else printf( "%04x", e1->Id );
2294 printf( " Name=" );
2295 if (e2->NameIsString)
2297 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
2298 dump_unicode_str( string->NameString, string->Length );
2300 else
2301 printf( "%04x", e2->Id );
2303 printf( " Language=%04x:\n", e3->Id );
2304 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
2305 if (e1->NameIsString)
2307 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2309 else switch(e1->Id)
2311 case 6:
2312 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2313 break;
2314 case 11:
2315 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2316 break;
2317 default:
2318 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2319 break;
2324 printf( "\n\n" );
2327 static void dump_debug(void)
2329 const char* stabs = NULL;
2330 unsigned szstabs = 0;
2331 const char* stabstr = NULL;
2332 unsigned szstr = 0;
2333 unsigned i;
2334 const IMAGE_SECTION_HEADER* sectHead;
2336 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2338 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2340 if (!strcmp((const char *)sectHead->Name, ".stab"))
2342 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2343 szstabs = sectHead->Misc.VirtualSize;
2345 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2347 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2348 szstr = sectHead->Misc.VirtualSize;
2351 if (stabs && stabstr)
2352 dump_stabs(stabs, szstabs, stabstr, szstr);
2355 static void dump_symbol_table(void)
2357 const IMAGE_SYMBOL* sym;
2358 int numsym;
2360 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2361 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2362 return;
2363 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2364 sizeof(*sym) * numsym);
2365 if (!sym) return;
2367 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2370 enum FileSig get_kind_exec(void)
2372 const WORD* pw;
2373 const DWORD* pdw;
2374 const IMAGE_DOS_HEADER* dh;
2376 pw = PRD(0, sizeof(WORD));
2377 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2379 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2381 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2383 /* the signature is the first DWORD */
2384 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2385 if (pdw)
2387 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2388 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2389 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2391 return SIG_DOS;
2393 return SIG_UNKNOWN;
2396 void pe_dump(void)
2398 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
2400 PE_nt_headers = get_nt_header();
2401 print_fake_dll();
2403 if (globals.do_dumpheader)
2405 dump_pe_header();
2406 /* FIXME: should check ptr */
2407 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2408 PE_nt_headers->FileHeader.NumberOfSections);
2410 else if (!globals.dumpsect)
2412 /* show at least something here */
2413 dump_pe_header();
2416 if (globals.dumpsect)
2418 if (all || !strcmp(globals.dumpsect, "import"))
2420 dump_dir_imported_functions();
2421 dump_dir_delay_imported_functions();
2423 if (all || !strcmp(globals.dumpsect, "export"))
2424 dump_dir_exported_functions();
2425 if (all || !strcmp(globals.dumpsect, "debug"))
2426 dump_dir_debug();
2427 if (all || !strcmp(globals.dumpsect, "resource"))
2428 dump_dir_resource();
2429 if (all || !strcmp(globals.dumpsect, "tls"))
2430 dump_dir_tls();
2431 if (all || !strcmp(globals.dumpsect, "loadcfg"))
2432 dump_dir_loadconfig();
2433 if (all || !strcmp(globals.dumpsect, "clr"))
2434 dump_dir_clr_header();
2435 if (all || !strcmp(globals.dumpsect, "reloc"))
2436 dump_dir_reloc();
2437 if (all || !strcmp(globals.dumpsect, "except"))
2438 dump_dir_exceptions();
2439 if (all || !strcmp(globals.dumpsect, "apiset"))
2440 dump_section_apiset();
2442 if (globals.do_symbol_table)
2443 dump_symbol_table();
2444 if (globals.do_debug)
2445 dump_debug();
2448 typedef struct _dll_symbol {
2449 size_t ordinal;
2450 char *symbol;
2451 } dll_symbol;
2453 static dll_symbol *dll_symbols = NULL;
2454 static dll_symbol *dll_current_symbol = NULL;
2456 /* Compare symbols by ordinal for qsort */
2457 static int symbol_cmp(const void *left, const void *right)
2459 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2462 /*******************************************************************
2463 * dll_close
2465 * Free resources used by DLL
2467 /* FIXME: Not used yet
2468 static void dll_close (void)
2470 dll_symbol* ds;
2472 if (!dll_symbols) {
2473 fatal("No symbols");
2475 for (ds = dll_symbols; ds->symbol; ds++)
2476 free(ds->symbol);
2477 free (dll_symbols);
2478 dll_symbols = NULL;
2482 static void do_grab_sym( void )
2484 const IMAGE_EXPORT_DIRECTORY*exportDir;
2485 UINT i, j, *map;
2486 const UINT *pName;
2487 const UINT *pFunc;
2488 const WORD *pOrdl;
2489 const char *ptr;
2491 PE_nt_headers = get_nt_header();
2492 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2494 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2495 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2496 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2497 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2498 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2499 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2501 /* dll_close(); */
2503 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
2505 /* bit map of used funcs */
2506 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2507 if (!map) fatal("no memory");
2509 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2511 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2512 ptr = RVA(*pName++, sizeof(DWORD));
2513 if (!ptr) ptr = "cant_get_function";
2514 dll_symbols[j].symbol = xstrdup(ptr);
2515 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2516 assert(dll_symbols[j].symbol);
2519 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2521 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2523 char ordinal_text[256];
2524 /* Ordinal only entry */
2525 sprintf (ordinal_text, "%s_%u",
2526 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2527 (UINT)exportDir->Base + i);
2528 str_toupper(ordinal_text);
2529 dll_symbols[j].symbol = xstrdup(ordinal_text);
2530 assert(dll_symbols[j].symbol);
2531 dll_symbols[j].ordinal = exportDir->Base + i;
2532 j++;
2533 assert(j <= exportDir->NumberOfFunctions);
2536 free(map);
2538 if (NORMAL)
2539 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2540 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
2541 j, (UINT)exportDir->Base);
2543 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2545 dll_symbols[j].symbol = NULL;
2547 dll_current_symbol = dll_symbols;
2550 /*******************************************************************
2551 * dll_open
2553 * Open a DLL and read in exported symbols
2555 BOOL dll_open (const char *dll_name)
2557 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2560 /*******************************************************************
2561 * dll_next_symbol
2563 * Get next exported symbol from dll
2565 BOOL dll_next_symbol (parsed_symbol * sym)
2567 if (!dll_current_symbol || !dll_current_symbol->symbol)
2568 return FALSE;
2569 assert (dll_symbols);
2570 sym->symbol = xstrdup (dll_current_symbol->symbol);
2571 sym->ordinal = dll_current_symbol->ordinal;
2572 dll_current_symbol++;
2573 return TRUE;