mshtml/tests: Actually test a return value.
[wine.git] / tools / winedump / pe.c
blobed30d54cece31a934cfb28f6c21aca68f3968f08
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_characteristics(DWORD characteristics, const char* sep)
386 #define X(b,s) if (characteristics & b) printf("%s%s", sep, s)
387 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
388 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
389 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
390 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
391 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
392 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
394 X(IMAGE_SCN_CNT_CODE, "CODE");
395 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
396 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
398 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
399 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
400 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
401 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
402 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
404 /* 0x00002000 - Reserved */
405 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
406 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
408 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
409 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
410 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
411 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
412 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
414 switch (characteristics & IMAGE_SCN_ALIGN_MASK)
416 #define X2(b,s) case b: printf("%s%s", sep, s); break
417 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
418 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
419 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
420 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
421 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
422 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
423 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
424 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
425 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
426 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
427 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
428 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
429 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
430 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
431 #undef X2
434 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
436 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
437 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
438 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
439 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
440 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
441 X(IMAGE_SCN_MEM_READ, "MEM_READ");
442 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
443 #undef X
446 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
448 unsigned offset;
450 /* long section name ? */
451 if (strtable && sectHead->Name[0] == '/' &&
452 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
453 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
454 else
455 printf(" %-8.8s", sectHead->Name);
456 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
457 (UINT)sectHead->Misc.VirtualSize, (UINT)sectHead->VirtualAddress);
458 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
459 (UINT)sectHead->PointerToRawData, (UINT)sectHead->SizeOfRawData);
460 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
461 (UINT)sectHead->PointerToRelocations, (UINT)sectHead->NumberOfRelocations);
462 printf(" line # offs: %-8u line #'s: %-8u\n",
463 (UINT)sectHead->PointerToLinenumbers, (UINT)sectHead->NumberOfLinenumbers);
464 printf(" characteristics: 0x%08x\n", (UINT)sectHead->Characteristics);
465 printf(" ");
466 dump_section_characteristics(sectHead->Characteristics, " ");
468 printf("\n\n");
471 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
473 const IMAGE_SECTION_HEADER* sectHead = addr;
474 unsigned i;
475 const char* strtable;
477 if (PE_nt_headers && PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
479 strtable = (const char*)base +
480 PE_nt_headers->FileHeader.PointerToSymbolTable +
481 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
483 else strtable = NULL;
485 printf("Section Table\n");
486 for (i = 0; i < num_sect; i++, sectHead++)
488 dump_section(sectHead, strtable);
490 if (globals.do_dump_rawdata)
492 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
493 printf("\n");
498 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
500 const WCHAR *wstr = PRD( rva, len );
501 char *ret = buffer;
503 len /= sizeof(WCHAR);
504 while (len--) *buffer++ = *wstr++;
505 *buffer = 0;
506 return ret;
509 static void dump_section_apiset(void)
511 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
512 const UINT *ptr, *entry, *value, *hash;
513 unsigned int i, j, count, val_count, rva;
514 char buffer[128];
516 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
518 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
519 rva = sect->PointerToRawData;
520 ptr = PRD( rva, sizeof(*ptr) );
521 printf( "ApiSet section:\n" );
522 switch (ptr[0]) /* version */
524 case 2:
525 printf( " Version: %u\n", ptr[0] );
526 printf( " Count: %08x\n", ptr[1] );
527 count = ptr[1];
528 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
529 for (i = 0; i < count; i++, entry += 3)
531 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
532 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
533 val_count = *value++;
534 for (j = 0; j < val_count; j++, value += 4)
536 putchar( ' ' );
537 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
538 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
540 printf( "\n");
542 break;
543 case 4:
544 printf( " Version: %u\n", ptr[0] );
545 printf( " Size: %08x\n", ptr[1] );
546 printf( " Flags: %08x\n", ptr[2] );
547 printf( " Count: %08x\n", ptr[3] );
548 count = ptr[3];
549 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
550 for (i = 0; i < count; i++, entry += 6)
552 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
553 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
554 value++; /* flags */
555 val_count = *value++;
556 for (j = 0; j < val_count; j++, value += 5)
558 putchar( ' ' );
559 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
560 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
562 printf( "\n");
564 break;
565 case 6:
566 printf( " Version: %u\n", ptr[0] );
567 printf( " Size: %08x\n", ptr[1] );
568 printf( " Flags: %08x\n", ptr[2] );
569 printf( " Count: %08x\n", ptr[3] );
570 printf( " EntryOffset: %08x\n", ptr[4] );
571 printf( " HashOffset: %08x\n", ptr[5] );
572 printf( " HashFactor: %08x\n", ptr[6] );
573 count = ptr[3];
574 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
575 for (i = 0; i < count; i++, entry += 6)
577 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
578 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
579 for (j = 0; j < entry[5]; j++, value += 5)
581 putchar( ' ' );
582 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
583 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
585 printf( "\n" );
587 printf( " Hash table:\n" );
588 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
589 for (i = 0; i < count; i++, hash += 2)
591 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
592 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
594 break;
595 default:
596 printf( "*** Unknown version %u\n", ptr[0] );
597 break;
599 break;
603 static void dump_dir_exported_functions(void)
605 unsigned int size = 0;
606 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
607 UINT i, *funcs;
608 const UINT *pFunc;
609 const UINT *pName;
610 const WORD *pOrdl;
612 if (!exportDir) return;
614 printf("Exports table:\n");
615 printf("\n");
616 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
617 printf(" Characteristics: %08x\n", (UINT)exportDir->Characteristics);
618 printf(" TimeDateStamp: %08X %s\n",
619 (UINT)exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
620 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
621 printf(" Ordinal base: %u\n", (UINT)exportDir->Base);
622 printf(" # of functions: %u\n", (UINT)exportDir->NumberOfFunctions);
623 printf(" # of Names: %u\n", (UINT)exportDir->NumberOfNames);
624 printf("Addresses of functions: %08X\n", (UINT)exportDir->AddressOfFunctions);
625 printf("Addresses of name ordinals: %08X\n", (UINT)exportDir->AddressOfNameOrdinals);
626 printf("Addresses of names: %08X\n", (UINT)exportDir->AddressOfNames);
627 printf("\n");
628 printf(" Entry Pt Ordn Name\n");
630 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
631 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
632 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
633 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
635 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
636 if (!funcs) fatal("no memory");
638 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
640 for (i = 0; i < exportDir->NumberOfFunctions; i++)
642 if (!pFunc[i]) continue;
643 printf(" %08X %5u ", pFunc[i], (UINT)exportDir->Base + i);
644 if (funcs[i])
645 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
646 else
647 printf("<by ordinal>");
649 /* check for forwarded function */
650 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
651 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
652 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
653 printf("\n");
655 free(funcs);
656 printf("\n");
660 struct runtime_function_x86_64
662 UINT BeginAddress;
663 UINT EndAddress;
664 UINT UnwindData;
667 struct runtime_function_armnt
669 UINT BeginAddress;
670 union {
671 UINT UnwindData;
672 struct {
673 UINT Flag : 2;
674 UINT FunctionLength : 11;
675 UINT Ret : 2;
676 UINT H : 1;
677 UINT Reg : 3;
678 UINT R : 1;
679 UINT L : 1;
680 UINT C : 1;
681 UINT StackAdjust : 10;
686 struct runtime_function_arm64
688 UINT BeginAddress;
689 union
691 UINT UnwindData;
692 struct
694 UINT Flag : 2;
695 UINT FunctionLength : 11;
696 UINT RegF : 3;
697 UINT RegI : 4;
698 UINT H : 1;
699 UINT CR : 2;
700 UINT FrameSize : 9;
705 union handler_data
707 struct runtime_function_x86_64 chain;
708 UINT handler;
711 struct opcode
713 BYTE offset;
714 BYTE code : 4;
715 BYTE info : 4;
718 struct unwind_info_x86_64
720 BYTE version : 3;
721 BYTE flags : 5;
722 BYTE prolog;
723 BYTE count;
724 BYTE frame_reg : 4;
725 BYTE frame_offset : 4;
726 struct opcode opcodes[1]; /* count entries */
727 /* followed by union handler_data */
730 struct unwind_info_armnt
732 UINT function_length : 18;
733 UINT version : 2;
734 UINT x : 1;
735 UINT e : 1;
736 UINT f : 1;
737 UINT count : 5;
738 UINT words : 4;
741 struct unwind_info_ext_armnt
743 WORD excount;
744 BYTE exwords;
745 BYTE reserved;
748 struct unwind_info_epilogue_armnt
750 UINT offset : 18;
751 UINT res : 2;
752 UINT cond : 4;
753 UINT index : 8;
756 #define UWOP_PUSH_NONVOL 0
757 #define UWOP_ALLOC_LARGE 1
758 #define UWOP_ALLOC_SMALL 2
759 #define UWOP_SET_FPREG 3
760 #define UWOP_SAVE_NONVOL 4
761 #define UWOP_SAVE_NONVOL_FAR 5
762 #define UWOP_SAVE_XMM128 8
763 #define UWOP_SAVE_XMM128_FAR 9
764 #define UWOP_PUSH_MACHFRAME 10
766 #define UNW_FLAG_EHANDLER 1
767 #define UNW_FLAG_UHANDLER 2
768 #define UNW_FLAG_CHAININFO 4
770 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
772 static const char * const reg_names[16] =
773 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
774 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
776 const union handler_data *handler_data;
777 const struct unwind_info_x86_64 *info;
778 unsigned int i, count;
780 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
781 if (function->UnwindData & 1)
783 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
784 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
785 return;
787 info = RVA( function->UnwindData, sizeof(*info) );
789 printf( " unwind info at %08x\n", function->UnwindData );
790 if (info->version != 1)
792 printf( " *** unknown version %u\n", info->version );
793 return;
795 printf( " flags %x", info->flags );
796 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
797 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
798 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
799 printf( "\n prolog 0x%x bytes\n", info->prolog );
801 if (info->frame_reg)
802 printf( " frame register %s offset 0x%x(%%rsp)\n",
803 reg_names[info->frame_reg], info->frame_offset * 16 );
805 for (i = 0; i < info->count; i++)
807 printf( " 0x%02x: ", info->opcodes[i].offset );
808 switch (info->opcodes[i].code)
810 case UWOP_PUSH_NONVOL:
811 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
812 break;
813 case UWOP_ALLOC_LARGE:
814 if (info->opcodes[i].info)
816 count = *(const UINT *)&info->opcodes[i+1];
817 i += 2;
819 else
821 count = *(const USHORT *)&info->opcodes[i+1] * 8;
822 i++;
824 printf( "sub $0x%x,%%rsp\n", count );
825 break;
826 case UWOP_ALLOC_SMALL:
827 count = (info->opcodes[i].info + 1) * 8;
828 printf( "sub $0x%x,%%rsp\n", count );
829 break;
830 case UWOP_SET_FPREG:
831 printf( "lea 0x%x(%%rsp),%s\n",
832 info->frame_offset * 16, reg_names[info->frame_reg] );
833 break;
834 case UWOP_SAVE_NONVOL:
835 count = *(const USHORT *)&info->opcodes[i+1] * 8;
836 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
837 i++;
838 break;
839 case UWOP_SAVE_NONVOL_FAR:
840 count = *(const UINT *)&info->opcodes[i+1];
841 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
842 i += 2;
843 break;
844 case UWOP_SAVE_XMM128:
845 count = *(const USHORT *)&info->opcodes[i+1] * 16;
846 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
847 i++;
848 break;
849 case UWOP_SAVE_XMM128_FAR:
850 count = *(const UINT *)&info->opcodes[i+1];
851 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
852 i += 2;
853 break;
854 case UWOP_PUSH_MACHFRAME:
855 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
856 break;
857 default:
858 printf( "*** unknown code %u\n", info->opcodes[i].code );
859 break;
863 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
864 if (info->flags & UNW_FLAG_CHAININFO)
866 printf( " -> function %08x-%08x\n",
867 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
868 return;
870 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
871 printf( " handler %08x data at %08x\n", handler_data->handler,
872 (UINT)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
875 static const BYTE armnt_code_lengths[256] =
877 /* 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,
878 /* 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,
879 /* 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,
880 /* 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,
881 /* 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,
882 /* 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,
883 /* 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,
884 /* 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
887 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
889 const struct unwind_info_armnt *info;
890 const struct unwind_info_ext_armnt *infoex;
891 const struct unwind_info_epilogue_armnt *infoepi;
892 unsigned int rva;
893 WORD i, count = 0, words = 0;
895 if (fnc->Flag)
897 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
898 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->StackAdjust;
900 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
901 (fnc->BeginAddress & ~1) + fnc->FunctionLength * 2 );
902 printf( " Flag %x\n", fnc->Flag );
903 printf( " FunctionLength %x\n", fnc->FunctionLength );
904 printf( " Ret %x\n", fnc->Ret );
905 printf( " H %x\n", fnc->H );
906 printf( " Reg %x\n", fnc->Reg );
907 printf( " R %x\n", fnc->R );
908 printf( " L %x\n", fnc->L );
909 printf( " C %x\n", fnc->C );
910 printf( " StackAdjust %x\n", fnc->StackAdjust );
912 if (fnc->StackAdjust >= 0x03f4)
914 pf = fnc->StackAdjust & 0x04;
915 ef = fnc->StackAdjust & 0x08;
916 stack = (fnc->StackAdjust & 3) + 1;
919 if (!fnc->R || pf)
921 int first = 4, last = fnc->Reg + 4;
922 if (pf)
924 first = (~fnc->StackAdjust) & 3;
925 if (fnc->R)
926 last = 3;
928 if (first == last)
929 sprintf(intregs, "r%u", first);
930 else
931 sprintf(intregs, "r%u-r%u", first, last);
932 fpoffset = last + 1 - first;
935 if (!fnc->R || ef)
937 int first = 4, last = fnc->Reg + 4;
938 if (ef)
940 first = (~fnc->StackAdjust) & 3;
941 if (fnc->R)
942 last = 3;
944 if (first == last)
945 sprintf(intregspop, "r%u", first);
946 else
947 sprintf(intregspop, "r%u-r%u", first, last);
950 if (fnc->C)
952 if (intregs[0])
953 strcat(intregs, ", ");
954 if (intregspop[0])
955 strcat(intregspop, ", ");
956 strcat(intregs, "r11");
957 strcat(intregspop, "r11");
959 if (fnc->L)
961 if (intregs[0])
962 strcat(intregs, ", ");
963 strcat(intregs, "lr");
965 if (intregspop[0] && (fnc->Ret != 0 || !fnc->H))
966 strcat(intregspop, ", ");
967 if (fnc->Ret != 0)
968 strcat(intregspop, "lr");
969 else if (!fnc->H)
970 strcat(intregspop, "pc");
973 if (fnc->R)
975 if (fnc->Reg)
976 sprintf(vfpregs, "d8-d%u", fnc->Reg + 8);
977 else
978 strcpy(vfpregs, "d8");
981 if (fnc->Flag == 1) {
982 if (fnc->H)
983 printf( " Unwind Code\tpush {r0-r3}\n" );
985 if (intregs[0])
986 printf( " Unwind Code\tpush {%s}\n", intregs );
988 if (fnc->C && fpoffset == 0)
989 printf( " Unwind Code\tmov r11, sp\n" );
990 else if (fnc->C)
991 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
993 if (fnc->R && fnc->Reg != 0x07)
994 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
996 if (stack && !pf)
997 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
1000 if (fnc->Ret == 3)
1001 return;
1002 printf( "Epilogue:\n" );
1004 if (stack && !ef)
1005 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1007 if (fnc->R && fnc->Reg != 0x07)
1008 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1010 if (intregspop[0])
1011 printf( " Unwind Code\tpop {%s}\n", intregspop );
1013 if (fnc->H && !(fnc->L && fnc->Ret == 0))
1014 printf( " Unwind Code\tadd sp, sp, #16\n" );
1015 else if (fnc->H && (fnc->L && fnc->Ret == 0))
1016 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1018 if (fnc->Ret == 1)
1019 printf( " Unwind Code\tbx <reg>\n" );
1020 else if (fnc->Ret == 2)
1021 printf( " Unwind Code\tb <address>\n" );
1023 return;
1026 info = RVA( fnc->UnwindData, sizeof(*info) );
1027 rva = fnc->UnwindData + sizeof(*info);
1028 count = info->count;
1029 words = info->words;
1031 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1032 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1033 printf( " unwind info at %08x\n", fnc->UnwindData );
1034 printf( " Flag %x\n", fnc->Flag );
1035 printf( " FunctionLength %x\n", info->function_length );
1036 printf( " Version %x\n", info->version );
1037 printf( " X %x\n", info->x );
1038 printf( " E %x\n", info->e );
1039 printf( " F %x\n", info->f );
1040 printf( " Count %x\n", count );
1041 printf( " Words %x\n", words );
1043 if (!info->count && !info->words)
1045 infoex = RVA( rva, sizeof(*infoex) );
1046 rva = rva + sizeof(*infoex);
1047 count = infoex->excount;
1048 words = infoex->exwords;
1049 printf( " ExtCount %x\n", count );
1050 printf( " ExtWords %x\n", words );
1053 if (!info->e)
1055 infoepi = RVA( rva, count * sizeof(*infoepi) );
1056 rva = rva + count * sizeof(*infoepi);
1058 for (i = 0; i < count; i++)
1060 printf( " Epilogue Scope %x\n", i );
1061 printf( " Offset %x\n", infoepi[i].offset );
1062 printf( " Reserved %x\n", infoepi[i].res );
1063 printf( " Condition %x\n", infoepi[i].cond );
1064 printf( " Index %x\n", infoepi[i].index );
1067 else
1068 infoepi = NULL;
1070 if (words)
1072 const unsigned int *codes;
1073 BYTE b, *bytes;
1074 BOOL inepilogue = FALSE;
1076 codes = RVA( rva, words * sizeof(*codes) );
1077 rva = rva + words * sizeof(*codes);
1078 bytes = (BYTE*)codes;
1080 for (b = 0; b < words * sizeof(*codes); b++)
1082 BYTE code = bytes[b];
1083 BYTE len = armnt_code_lengths[code];
1085 if (info->e && b == count)
1087 printf( "Epilogue:\n" );
1088 inepilogue = TRUE;
1090 else if (!info->e && infoepi)
1092 for (i = 0; i < count; i++)
1093 if (b == infoepi[i].index)
1095 printf( "Epilogue from Scope %x at %08x:\n", i,
1096 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1097 inepilogue = TRUE;
1101 printf( " Unwind Code");
1102 for (i = 0; i < len; i++)
1103 printf( " %02x", bytes[b+i] );
1104 printf( "\t" );
1106 if (code == 0x00)
1107 printf( "\n" );
1108 else if (code <= 0x7f)
1109 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1110 else if (code <= 0xbf)
1112 WORD excode, f;
1113 BOOL first = TRUE;
1114 BYTE excodes = bytes[++b];
1116 excode = (code << 8) | excodes;
1117 printf( "%s {", inepilogue ? "pop" : "push" );
1119 for (f = 0; f <= 12; f++)
1121 if ((excode >> f) & 1)
1123 printf( "%sr%u", first ? "" : ", ", f );
1124 first = FALSE;
1128 if (excode & 0x2000)
1129 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1131 printf( "}\n" );
1133 else if (code <= 0xcf)
1134 if (inepilogue)
1135 printf( "mov sp, r%u\n", code & 0x0f );
1136 else
1137 printf( "mov r%u, sp\n", code & 0x0f );
1138 else if (code <= 0xd7)
1139 if (inepilogue)
1140 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1141 else
1142 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1143 else if (code <= 0xdf)
1144 if (inepilogue)
1145 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1146 else
1147 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1148 else if (code <= 0xe7)
1149 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1150 else if (code <= 0xeb)
1152 WORD excode;
1153 BYTE excodes = bytes[++b];
1155 excode = (code << 8) | excodes;
1156 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1158 else if (code <= 0xed)
1160 WORD excode, f;
1161 BOOL first = TRUE;
1162 BYTE excodes = bytes[++b];
1164 excode = (code << 8) | excodes;
1165 printf( "%s {", inepilogue ? "pop" : "push" );
1167 for (f = 0; f < 8; f++)
1169 if ((excode >> f) & 1)
1171 printf( "%sr%u", first ? "" : ", ", f );
1172 first = FALSE;
1176 if (excode & 0x0100)
1177 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1179 printf( "}\n" );
1181 else if (code == 0xee)
1182 printf( "unknown 16\n" );
1183 else if (code == 0xef)
1185 WORD excode;
1186 BYTE excodes = bytes[++b];
1188 if (excodes <= 0x0f)
1190 excode = (code << 8) | excodes;
1191 if (inepilogue)
1192 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1193 else
1194 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1196 else
1197 printf( "unknown 32\n" );
1199 else if (code <= 0xf4)
1200 printf( "unknown\n" );
1201 else if (code <= 0xf6)
1203 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1204 BYTE excodes = bytes[++b];
1206 excode = (code << 8) | excodes;
1207 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1208 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1210 else if (code <= 0xf7)
1212 unsigned int excode;
1213 BYTE excodes[2];
1215 excodes[0] = bytes[++b];
1216 excodes[1] = bytes[++b];
1217 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1218 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1220 else if (code <= 0xf8)
1222 unsigned int excode;
1223 BYTE excodes[3];
1225 excodes[0] = bytes[++b];
1226 excodes[1] = bytes[++b];
1227 excodes[2] = bytes[++b];
1228 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1229 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1231 else if (code <= 0xf9)
1233 unsigned int excode;
1234 BYTE excodes[2];
1236 excodes[0] = bytes[++b];
1237 excodes[1] = bytes[++b];
1238 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1239 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1241 else if (code <= 0xfa)
1243 unsigned int excode;
1244 BYTE excodes[3];
1246 excodes[0] = bytes[++b];
1247 excodes[1] = bytes[++b];
1248 excodes[2] = bytes[++b];
1249 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1250 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1252 else if (code <= 0xfb)
1253 printf( "nop\n" );
1254 else if (code <= 0xfc)
1255 printf( "nop.w\n" );
1256 else if (code <= 0xfd)
1258 printf( "(end) nop\n" );
1259 inepilogue = TRUE;
1261 else if (code <= 0xfe)
1263 printf( "(end) nop.w\n" );
1264 inepilogue = TRUE;
1266 else
1268 printf( "end\n" );
1269 inepilogue = TRUE;
1274 if (info->x)
1276 const unsigned int *handler;
1278 handler = RVA( rva, sizeof(*handler) );
1279 rva = rva + sizeof(*handler);
1281 printf( " handler %08x data at %08x\n", *handler, rva);
1285 struct unwind_info_arm64
1287 UINT function_length : 18;
1288 UINT version : 2;
1289 UINT x : 1;
1290 UINT e : 1;
1291 UINT epilog : 5;
1292 UINT codes : 5;
1295 struct unwind_info_ext_arm64
1297 WORD epilog;
1298 BYTE codes;
1299 BYTE reserved;
1302 struct unwind_info_epilog_arm64
1304 UINT offset : 18;
1305 UINT res : 4;
1306 UINT index : 10;
1309 static const BYTE code_lengths[256] =
1311 /* 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,
1312 /* 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,
1313 /* 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,
1314 /* 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,
1315 /* 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,
1316 /* 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,
1317 /* 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,
1318 /* 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
1321 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1323 unsigned int i, j;
1325 for (i = 0; i < count; i += code_lengths[ptr[i]])
1327 BYTE len = code_lengths[ptr[i]];
1328 unsigned int val = ptr[i];
1329 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1330 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1332 printf( " %04x: ", i );
1333 for (j = 0; j < 4; j++)
1334 if (j < len) printf( "%02x ", ptr[i+j] );
1335 else printf( " " );
1337 if (ptr[i] < 0x20) /* alloc_s */
1339 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1341 else if (ptr[i] < 0x40) /* save_r19r20_x */
1343 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1345 else if (ptr[i] < 0x80) /* save_fplr */
1347 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1349 else if (ptr[i] < 0xc0) /* save_fplr_x */
1351 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1353 else if (ptr[i] < 0xc8) /* alloc_m */
1355 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1357 else if (ptr[i] < 0xcc) /* save_regp */
1359 int reg = 19 + ((val >> 6) & 0xf);
1360 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1362 else if (ptr[i] < 0xd0) /* save_regp_x */
1364 int reg = 19 + ((val >> 6) & 0xf);
1365 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1367 else if (ptr[i] < 0xd4) /* save_reg */
1369 int reg = 19 + ((val >> 6) & 0xf);
1370 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1372 else if (ptr[i] < 0xd6) /* save_reg_x */
1374 int reg = 19 + ((val >> 5) & 0xf);
1375 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1377 else if (ptr[i] < 0xd8) /* save_lrpair */
1379 int reg = 19 + 2 * ((val >> 6) & 0x7);
1380 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1382 else if (ptr[i] < 0xda) /* save_fregp */
1384 int reg = 8 + ((val >> 6) & 0x7);
1385 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1387 else if (ptr[i] < 0xdc) /* save_fregp_x */
1389 int reg = 8 + ((val >> 6) & 0x7);
1390 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1392 else if (ptr[i] < 0xde) /* save_freg */
1394 int reg = 8 + ((val >> 6) & 0x7);
1395 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1397 else if (ptr[i] == 0xde) /* save_freg_x */
1399 int reg = 8 + ((val >> 5) & 0x7);
1400 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1402 else if (ptr[i] == 0xe0) /* alloc_l */
1404 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1406 else if (ptr[i] == 0xe1) /* set_fp */
1408 printf( "mov x29,sp\n" );
1410 else if (ptr[i] == 0xe2) /* add_fp */
1412 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1414 else if (ptr[i] == 0xe3) /* nop */
1416 printf( "nop\n" );
1418 else if (ptr[i] == 0xe4) /* end */
1420 printf( "end\n" );
1422 else if (ptr[i] == 0xe5) /* end_c */
1424 printf( "end_c\n" );
1426 else if (ptr[i] == 0xe6) /* save_next */
1428 printf( "save_next\n" );
1430 else if (ptr[i] == 0xe7) /* arithmetic */
1432 switch ((val >> 4) & 0x0f)
1434 case 0: printf( "add lr,lr,x28\n" ); break;
1435 case 1: printf( "add lr,lr,sp\n" ); break;
1436 case 2: printf( "sub lr,lr,x28\n" ); break;
1437 case 3: printf( "sub lr,lr,sp\n" ); break;
1438 case 4: printf( "eor lr,lr,x28\n" ); break;
1439 case 5: printf( "eor lr,lr,sp\n" ); break;
1440 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1441 case 8: printf( "ror lr,lr,x28\n" ); break;
1442 case 9: printf( "ror lr,lr,sp\n" ); break;
1443 default:printf( "unknown op\n" ); break;
1446 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1448 printf( "MSFT_OP_TRAP_FRAME\n" );
1450 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1452 printf( "MSFT_OP_MACHINE_FRAME\n" );
1454 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1456 printf( "MSFT_OP_CONTEXT\n" );
1458 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1460 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1462 else printf( "??\n");
1466 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1468 int i, pos = 0, intsz = func->RegI * 8, fpsz = func->RegF * 8, savesz, locsz;
1470 if (func->CR == 1) intsz += 8;
1471 if (func->RegF) fpsz += 8;
1473 savesz = ((intsz + fpsz + 8 * 8 * func->H) + 0xf) & ~0xf;
1474 locsz = func->FrameSize * 16 - savesz;
1476 switch (func->CR)
1478 case 3:
1479 printf( " %04x: mov x29,sp\n", pos++ );
1480 if (locsz <= 512)
1482 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1483 break;
1485 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1486 /* fall through */
1487 case 0:
1488 case 1:
1489 if (locsz <= 4080)
1491 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1493 else
1495 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1496 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1498 break;
1501 if (func->H)
1503 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1504 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1505 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1506 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1509 if (func->RegF)
1511 if (func->RegF % 2 == 0)
1512 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->RegF, intsz + fpsz - 8 );
1513 for (i = (func->RegF - 1)/ 2; i >= 0; i--)
1515 if (!i && !intsz)
1516 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1517 else
1518 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1522 switch (func->RegI)
1524 case 0:
1525 if (func->CR == 1)
1526 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1527 break;
1528 case 1:
1529 if (func->CR == 1)
1530 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1531 else
1532 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1533 break;
1534 default:
1535 if (func->RegI % 2)
1537 if (func->CR == 1)
1538 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1539 else
1540 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->RegI, 8 * func->RegI - 8 );
1542 else if (func->CR == 1)
1543 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1545 for (i = func->RegI / 2 - 1; i >= 0; i--)
1546 if (i)
1547 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1548 else
1549 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1550 break;
1552 printf( " %04x: end\n", pos );
1555 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1557 const struct unwind_info_arm64 *info;
1558 const struct unwind_info_ext_arm64 *infoex;
1559 const struct unwind_info_epilog_arm64 *infoepi;
1560 const BYTE *ptr;
1561 unsigned int i, rva, codes, epilogs;
1563 if (func->Flag)
1565 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1566 func->BeginAddress + func->FunctionLength * 4 );
1567 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1568 func->FunctionLength, func->Flag, func->RegF, func->RegI,
1569 func->H, func->CR, func->FrameSize );
1570 dump_arm64_packed_info( func );
1571 return;
1574 rva = func->UnwindData;
1575 info = RVA( rva, sizeof(*info) );
1576 rva += sizeof(*info);
1577 epilogs = info->epilog;
1578 codes = info->codes;
1580 if (!codes)
1582 infoex = RVA( rva, sizeof(*infoex) );
1583 rva = rva + sizeof(*infoex);
1584 codes = infoex->codes;
1585 epilogs = infoex->epilog;
1587 printf( "\nFunction %08x-%08x:\n",
1588 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1589 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1590 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1591 if (info->e)
1593 printf( " epilog 0: code=%04x\n", info->epilog );
1595 else
1597 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1598 rva += sizeof(*infoepi) * epilogs;
1599 for (i = 0; i < epilogs; i++)
1600 printf( " epilog %u: pc=%08x code=%04x\n", i,
1601 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1603 ptr = RVA( rva, codes * 4);
1604 rva += codes * 4;
1605 if (info->x)
1607 const UINT *handler = RVA( rva, sizeof(*handler) );
1608 rva += sizeof(*handler);
1609 printf( " handler: %08x data %08x\n", *handler, rva );
1611 dump_arm64_codes( ptr, codes * 4 );
1614 static void dump_dir_exceptions(void)
1616 unsigned int i, size = 0;
1617 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1618 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1620 if (!funcs) return;
1622 switch (file_header->Machine)
1624 case IMAGE_FILE_MACHINE_AMD64:
1625 size /= sizeof(struct runtime_function_x86_64);
1626 printf( "Exception info (%u functions):\n", size );
1627 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1628 break;
1629 case IMAGE_FILE_MACHINE_ARMNT:
1630 size /= sizeof(struct runtime_function_armnt);
1631 printf( "Exception info (%u functions):\n", size );
1632 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1633 break;
1634 case IMAGE_FILE_MACHINE_ARM64:
1635 size /= sizeof(struct runtime_function_arm64);
1636 printf( "Exception info (%u functions):\n", size );
1637 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1638 break;
1639 default:
1640 printf( "Exception information not supported for %s binaries\n",
1641 get_machine_str(file_header->Machine));
1642 break;
1647 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, UINT thunk_rva)
1649 /* FIXME: This does not properly handle large images */
1650 const IMAGE_IMPORT_BY_NAME* iibn;
1651 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1653 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1654 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL64(il->u1.Ordinal));
1655 else
1657 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1658 if (!iibn)
1659 printf("Can't grab import by name info, skipping to next ordinal\n");
1660 else
1661 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1666 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, UINT thunk_rva)
1668 const IMAGE_IMPORT_BY_NAME* iibn;
1669 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(UINT))
1671 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1672 printf(" %08x %4u <by ordinal>\n", thunk_rva, (UINT)IMAGE_ORDINAL32(il->u1.Ordinal));
1673 else
1675 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1676 if (!iibn)
1677 printf("Can't grab import by name info, skipping to next ordinal\n");
1678 else
1679 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1684 static void dump_dir_imported_functions(void)
1686 unsigned directorySize;
1687 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1689 if (!importDesc) return;
1691 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1693 for (;;)
1695 const IMAGE_THUNK_DATA32* il;
1697 if (!importDesc->Name || !importDesc->FirstThunk) break;
1699 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1700 printf(" Hint/Name Table: %08X\n", (UINT)importDesc->OriginalFirstThunk);
1701 printf(" TimeDateStamp: %08X (%s)\n",
1702 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1703 printf(" ForwarderChain: %08X\n", (UINT)importDesc->ForwarderChain);
1704 printf(" First thunk RVA: %08X\n", (UINT)importDesc->FirstThunk);
1706 printf(" Thunk Ordn Name\n");
1708 il = (importDesc->OriginalFirstThunk != 0) ?
1709 RVA((DWORD)importDesc->OriginalFirstThunk, sizeof(DWORD)) :
1710 RVA((DWORD)importDesc->FirstThunk, 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->FirstThunk);
1718 else
1719 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1720 printf("\n");
1722 importDesc++;
1724 printf("\n");
1727 static void dump_dir_loadconfig(void)
1729 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1730 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1732 if (!loadcfg32) return;
1734 printf( "Loadconfig\n" );
1735 print_dword( "Size", loadcfg32->Size );
1736 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1737 print_word( "MajorVersion", loadcfg32->MajorVersion );
1738 print_word( "MinorVersion", loadcfg32->MinorVersion );
1739 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1740 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1741 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1743 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1745 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1746 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1747 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1748 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1749 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1750 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1751 print_word( "CSDVersion", loadcfg64->CSDVersion );
1752 print_word( "Reserved", loadcfg64->Reserved1 );
1753 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1754 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1755 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1757 else
1759 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1760 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1761 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1762 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1763 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1764 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1765 print_word( "CSDVersion", loadcfg32->CSDVersion );
1766 print_word( "Reserved", loadcfg32->Reserved1 );
1767 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1768 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1769 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1773 static void dump_dir_delay_imported_functions(void)
1775 unsigned directorySize;
1776 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1778 if (!importDesc) return;
1780 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1782 for (;;)
1784 const IMAGE_THUNK_DATA32* il;
1785 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1787 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1789 printf(" grAttrs %08x offset %08lx %s\n", (UINT)importDesc->Attributes.AllAttributes,
1790 Offset(importDesc), (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1791 printf(" Hint/Name Table: %08x\n", (UINT)importDesc->ImportNameTableRVA);
1792 printf(" Address Table: %08x\n", (UINT)importDesc->ImportAddressTableRVA);
1793 printf(" TimeDateStamp: %08X (%s)\n",
1794 (UINT)importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1796 printf(" Thunk Ordn Name\n");
1798 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1800 if (!il)
1801 printf("Can't grab thunk data, going to next imported DLL\n");
1802 else
1804 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1805 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1806 else
1807 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1808 printf("\n");
1810 importDesc++;
1812 printf("\n");
1815 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1817 const char* str;
1819 printf("Directory %02u\n", idx + 1);
1820 printf(" Characteristics: %08X\n", (UINT)idd->Characteristics);
1821 printf(" TimeDateStamp: %08X %s\n",
1822 (UINT)idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1823 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1824 switch (idd->Type)
1826 default:
1827 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1828 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1829 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1830 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1831 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1832 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1833 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1834 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1835 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1836 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1837 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1838 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1839 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1840 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1841 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1842 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1843 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
1845 printf(" Type: %u (%s)\n", (UINT)idd->Type, str);
1846 printf(" SizeOfData: %u\n", (UINT)idd->SizeOfData);
1847 printf(" AddressOfRawData: %08X\n", (UINT)idd->AddressOfRawData);
1848 printf(" PointerToRawData: %08X\n", (UINT)idd->PointerToRawData);
1850 switch (idd->Type)
1852 case IMAGE_DEBUG_TYPE_UNKNOWN:
1853 break;
1854 case IMAGE_DEBUG_TYPE_COFF:
1855 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1856 IMAGE_FIRST_SECTION(PE_nt_headers));
1857 break;
1858 case IMAGE_DEBUG_TYPE_CODEVIEW:
1859 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1860 break;
1861 case IMAGE_DEBUG_TYPE_FPO:
1862 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1863 break;
1864 case IMAGE_DEBUG_TYPE_MISC:
1866 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1867 if (!misc) {printf("Can't get misc debug information\n"); break;}
1868 printf(" DataType: %u (%s)\n",
1869 (UINT)misc->DataType, (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1870 printf(" Length: %u\n", (UINT)misc->Length);
1871 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1872 printf(" Data: %s\n", misc->Data);
1874 break;
1875 default: break;
1877 printf("\n");
1880 static void dump_dir_debug(void)
1882 unsigned nb_dbg, i;
1883 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1885 nb_dbg /= sizeof(*debugDir);
1886 if (!debugDir || !nb_dbg) return;
1888 printf("Debug Table (%u directories)\n", nb_dbg);
1890 for (i = 0; i < nb_dbg; i++)
1892 dump_dir_debug_dir(debugDir, i);
1893 debugDir++;
1895 printf("\n");
1898 static inline void print_clrflags(const char *title, UINT value)
1900 printf(" %-34s 0x%X\n", title, value);
1901 #define X(f,s) if (value & f) printf(" %s\n", s)
1902 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1903 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1904 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1905 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1906 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1907 #undef X
1910 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1912 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, (UINT)dir->VirtualAddress, (UINT)dir->Size);
1915 static void dump_dir_clr_header(void)
1917 unsigned int size = 0;
1918 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1920 if (!dir) return;
1922 printf( "CLR Header\n" );
1923 print_dword( "Header Size", dir->cb );
1924 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1925 print_clrflags( "Flags", dir->Flags );
1926 print_dword( "EntryPointToken", dir->EntryPointToken );
1927 printf("\n");
1928 printf( "CLR Data Directory\n" );
1929 print_clrdirectory( "MetaData", &dir->MetaData );
1930 print_clrdirectory( "Resources", &dir->Resources );
1931 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1932 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1933 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1934 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1935 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1936 printf("\n");
1939 static void dump_dir_reloc(void)
1941 unsigned int i, size = 0;
1942 const USHORT *relocs;
1943 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1944 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1945 static const char * const names[] =
1947 "BASED_ABSOLUTE",
1948 "BASED_HIGH",
1949 "BASED_LOW",
1950 "BASED_HIGHLOW",
1951 "BASED_HIGHADJ",
1952 "BASED_MIPS_JMPADDR",
1953 "BASED_SECTION",
1954 "BASED_REL",
1955 "unknown 8",
1956 "BASED_IA64_IMM64",
1957 "BASED_DIR64",
1958 "BASED_HIGH3ADJ",
1959 "unknown 12",
1960 "unknown 13",
1961 "unknown 14",
1962 "unknown 15"
1965 if (!rel) return;
1967 printf( "Relocations\n" );
1968 while (rel < end - 1 && rel->SizeOfBlock)
1970 printf( " Page %x\n", (UINT)rel->VirtualAddress );
1971 relocs = (const USHORT *)(rel + 1);
1972 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1973 while (i--)
1975 USHORT offset = *relocs & 0xfff;
1976 int type = *relocs >> 12;
1977 printf( " off %04x type %s\n", offset, names[type] );
1978 relocs++;
1980 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1982 printf("\n");
1985 static void dump_dir_tls(void)
1987 IMAGE_TLS_DIRECTORY64 dir;
1988 const UINT *callbacks;
1989 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1991 if (!pdir) return;
1993 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1994 memcpy(&dir, pdir, sizeof(dir));
1995 else
1997 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1998 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1999 dir.AddressOfIndex = pdir->AddressOfIndex;
2000 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
2001 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
2002 dir.Characteristics = pdir->Characteristics;
2005 /* FIXME: This does not properly handle large images */
2006 printf( "Thread Local Storage\n" );
2007 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2008 (UINT)dir.StartAddressOfRawData, (UINT)dir.EndAddressOfRawData,
2009 (UINT)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2010 (UINT)dir.SizeOfZeroFill );
2011 printf( " Index address %08x\n", (UINT)dir.AddressOfIndex );
2012 printf( " Characteristics %08x\n", (UINT)dir.Characteristics );
2013 printf( " Callbacks %08x -> {", (UINT)dir.AddressOfCallBacks );
2014 if (dir.AddressOfCallBacks)
2016 UINT addr = (UINT)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2017 while ((callbacks = RVA(addr, sizeof(UINT))) && *callbacks)
2019 printf( " %08x", *callbacks );
2020 addr += sizeof(UINT);
2023 printf(" }\n\n");
2026 enum FileSig get_kind_dbg(void)
2028 const WORD* pw;
2030 pw = PRD(0, sizeof(WORD));
2031 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2033 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2034 return SIG_UNKNOWN;
2037 void dbg_dump(void)
2039 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2040 unsigned nb_dbg;
2041 unsigned i;
2042 const IMAGE_DEBUG_DIRECTORY* debugDir;
2044 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2045 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2047 printf ("Signature: %.2s (0x%4X)\n",
2048 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2049 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2050 printf ("Machine: 0x%04X (%s)\n",
2051 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2052 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2053 printf ("TimeDateStamp: 0x%08X (%s)\n",
2054 (UINT)separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2055 printf ("CheckSum: 0x%08X\n", (UINT)separateDebugHead->CheckSum);
2056 printf ("ImageBase: 0x%08X\n", (UINT)separateDebugHead->ImageBase);
2057 printf ("SizeOfImage: 0x%08X\n", (UINT)separateDebugHead->SizeOfImage);
2058 printf ("NumberOfSections: 0x%08X\n", (UINT)separateDebugHead->NumberOfSections);
2059 printf ("ExportedNamesSize: 0x%08X\n", (UINT)separateDebugHead->ExportedNamesSize);
2060 printf ("DebugDirectorySize: 0x%08X\n", (UINT)separateDebugHead->DebugDirectorySize);
2062 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2063 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2064 {printf("Can't get the sections, aborting\n"); return;}
2066 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2068 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2069 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2070 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2071 separateDebugHead->ExportedNamesSize,
2072 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2073 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2075 printf("Debug Table (%u directories)\n", nb_dbg);
2077 for (i = 0; i < nb_dbg; i++)
2079 dump_dir_debug_dir(debugDir, i);
2080 debugDir++;
2084 static const char *get_resource_type( unsigned int id )
2086 static const char * const types[] =
2088 NULL,
2089 "CURSOR",
2090 "BITMAP",
2091 "ICON",
2092 "MENU",
2093 "DIALOG",
2094 "STRING",
2095 "FONTDIR",
2096 "FONT",
2097 "ACCELERATOR",
2098 "RCDATA",
2099 "MESSAGETABLE",
2100 "GROUP_CURSOR",
2101 NULL,
2102 "GROUP_ICON",
2103 NULL,
2104 "VERSION",
2105 "DLGINCLUDE",
2106 NULL,
2107 "PLUGPLAY",
2108 "VXD",
2109 "ANICURSOR",
2110 "ANIICON",
2111 "HTML",
2112 "RT_MANIFEST"
2115 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2116 return NULL;
2119 /* dump an ASCII string with proper escaping */
2120 static int dump_strA( const unsigned char *str, size_t len )
2122 static const char escapes[32] = ".......abtnvfr.............e....";
2123 char buffer[256];
2124 char *pos = buffer;
2125 int count = 0;
2127 for (; len; str++, len--)
2129 if (pos > buffer + sizeof(buffer) - 8)
2131 fwrite( buffer, pos - buffer, 1, stdout );
2132 count += pos - buffer;
2133 pos = buffer;
2135 if (*str > 127) /* hex escape */
2137 pos += sprintf( pos, "\\x%02x", *str );
2138 continue;
2140 if (*str < 32) /* octal or C escape */
2142 if (!*str && len == 1) continue; /* do not output terminating NULL */
2143 if (escapes[*str] != '.')
2144 pos += sprintf( pos, "\\%c", escapes[*str] );
2145 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2146 pos += sprintf( pos, "\\%03o", *str );
2147 else
2148 pos += sprintf( pos, "\\%o", *str );
2149 continue;
2151 if (*str == '\\') *pos++ = '\\';
2152 *pos++ = *str;
2154 fwrite( buffer, pos - buffer, 1, stdout );
2155 count += pos - buffer;
2156 return count;
2159 /* dump a Unicode string with proper escaping */
2160 static int dump_strW( const WCHAR *str, size_t len )
2162 static const char escapes[32] = ".......abtnvfr.............e....";
2163 char buffer[256];
2164 char *pos = buffer;
2165 int count = 0;
2167 for (; len; str++, len--)
2169 if (pos > buffer + sizeof(buffer) - 8)
2171 fwrite( buffer, pos - buffer, 1, stdout );
2172 count += pos - buffer;
2173 pos = buffer;
2175 if (*str > 127) /* hex escape */
2177 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2178 pos += sprintf( pos, "\\x%04x", *str );
2179 else
2180 pos += sprintf( pos, "\\x%x", *str );
2181 continue;
2183 if (*str < 32) /* octal or C escape */
2185 if (!*str && len == 1) continue; /* do not output terminating NULL */
2186 if (escapes[*str] != '.')
2187 pos += sprintf( pos, "\\%c", escapes[*str] );
2188 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2189 pos += sprintf( pos, "\\%03o", *str );
2190 else
2191 pos += sprintf( pos, "\\%o", *str );
2192 continue;
2194 if (*str == '\\') *pos++ = '\\';
2195 *pos++ = *str;
2197 fwrite( buffer, pos - buffer, 1, stdout );
2198 count += pos - buffer;
2199 return count;
2202 /* dump data for a STRING resource */
2203 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2205 int i;
2207 for (i = 0; i < 16 && size; i++)
2209 unsigned len = *ptr++;
2211 if (len >= size)
2213 len = size;
2214 size = 0;
2216 else size -= len + 1;
2218 if (len)
2220 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2221 dump_strW( ptr, len );
2222 printf( "\"\n" );
2223 ptr += len;
2228 /* dump data for a MESSAGETABLE resource */
2229 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2231 const MESSAGE_RESOURCE_DATA *data = ptr;
2232 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2233 unsigned i, j;
2235 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2237 const MESSAGE_RESOURCE_ENTRY *entry;
2239 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2240 for (j = block->LowId; j <= block->HighId; j++)
2242 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2244 const WCHAR *str = (const WCHAR *)entry->Text;
2245 printf( "%s%08x L\"", prefix, j );
2246 dump_strW( str, strlenW(str) );
2247 printf( "\"\n" );
2249 else
2251 const char *str = (const char *) entry->Text;
2252 printf( "%s%08x \"", prefix, j );
2253 dump_strA( entry->Text, strlen(str) );
2254 printf( "\"\n" );
2256 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2261 static void dump_dir_resource(void)
2263 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2264 const IMAGE_RESOURCE_DIRECTORY *namedir;
2265 const IMAGE_RESOURCE_DIRECTORY *langdir;
2266 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2267 const IMAGE_RESOURCE_DIR_STRING_U *string;
2268 const IMAGE_RESOURCE_DATA_ENTRY *data;
2269 int i, j, k;
2271 if (!root) return;
2273 printf( "Resources:" );
2275 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2277 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2278 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->OffsetToDirectory);
2279 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2281 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2282 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->OffsetToDirectory);
2283 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2285 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2287 printf( "\n " );
2288 if (e1->NameIsString)
2290 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->NameOffset);
2291 dump_unicode_str( string->NameString, string->Length );
2293 else
2295 const char *type = get_resource_type( e1->Id );
2296 if (type) printf( "%s", type );
2297 else printf( "%04x", e1->Id );
2300 printf( " Name=" );
2301 if (e2->NameIsString)
2303 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->NameOffset);
2304 dump_unicode_str( string->NameString, string->Length );
2306 else
2307 printf( "%04x", e2->Id );
2309 printf( " Language=%04x:\n", e3->Id );
2310 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->OffsetToData);
2311 if (e1->NameIsString)
2313 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2315 else switch(e1->Id)
2317 case 6:
2318 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2319 break;
2320 case 11:
2321 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->Id, " " );
2322 break;
2323 default:
2324 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2325 break;
2330 printf( "\n\n" );
2333 static void dump_debug(void)
2335 const char* stabs = NULL;
2336 unsigned szstabs = 0;
2337 const char* stabstr = NULL;
2338 unsigned szstr = 0;
2339 unsigned i;
2340 const IMAGE_SECTION_HEADER* sectHead;
2342 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2344 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2346 if (!strcmp((const char *)sectHead->Name, ".stab"))
2348 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2349 szstabs = sectHead->Misc.VirtualSize;
2351 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2353 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2354 szstr = sectHead->Misc.VirtualSize;
2357 if (stabs && stabstr)
2358 dump_stabs(stabs, szstabs, stabstr, szstr);
2361 static void dump_symbol_table(void)
2363 const IMAGE_SYMBOL* sym;
2364 int numsym;
2366 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2367 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2368 return;
2369 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2370 sizeof(*sym) * numsym);
2371 if (!sym) return;
2373 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2376 enum FileSig get_kind_exec(void)
2378 const WORD* pw;
2379 const DWORD* pdw;
2380 const IMAGE_DOS_HEADER* dh;
2382 pw = PRD(0, sizeof(WORD));
2383 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2385 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2387 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2389 /* the signature is the first DWORD */
2390 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2391 if (pdw)
2393 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2394 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2395 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2397 return SIG_DOS;
2399 return SIG_UNKNOWN;
2402 void pe_dump(void)
2404 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
2406 PE_nt_headers = get_nt_header();
2407 print_fake_dll();
2409 if (globals.do_dumpheader)
2411 dump_pe_header();
2412 /* FIXME: should check ptr */
2413 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2414 PE_nt_headers->FileHeader.NumberOfSections);
2416 else if (!globals.dumpsect)
2418 /* show at least something here */
2419 dump_pe_header();
2422 if (globals.dumpsect)
2424 if (all || !strcmp(globals.dumpsect, "import"))
2426 dump_dir_imported_functions();
2427 dump_dir_delay_imported_functions();
2429 if (all || !strcmp(globals.dumpsect, "export"))
2430 dump_dir_exported_functions();
2431 if (all || !strcmp(globals.dumpsect, "debug"))
2432 dump_dir_debug();
2433 if (all || !strcmp(globals.dumpsect, "resource"))
2434 dump_dir_resource();
2435 if (all || !strcmp(globals.dumpsect, "tls"))
2436 dump_dir_tls();
2437 if (all || !strcmp(globals.dumpsect, "loadcfg"))
2438 dump_dir_loadconfig();
2439 if (all || !strcmp(globals.dumpsect, "clr"))
2440 dump_dir_clr_header();
2441 if (all || !strcmp(globals.dumpsect, "reloc"))
2442 dump_dir_reloc();
2443 if (all || !strcmp(globals.dumpsect, "except"))
2444 dump_dir_exceptions();
2445 if (all || !strcmp(globals.dumpsect, "apiset"))
2446 dump_section_apiset();
2448 if (globals.do_symbol_table)
2449 dump_symbol_table();
2450 if (globals.do_debug)
2451 dump_debug();
2454 typedef struct _dll_symbol {
2455 size_t ordinal;
2456 char *symbol;
2457 } dll_symbol;
2459 static dll_symbol *dll_symbols = NULL;
2460 static dll_symbol *dll_current_symbol = NULL;
2462 /* Compare symbols by ordinal for qsort */
2463 static int symbol_cmp(const void *left, const void *right)
2465 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2468 /*******************************************************************
2469 * dll_close
2471 * Free resources used by DLL
2473 /* FIXME: Not used yet
2474 static void dll_close (void)
2476 dll_symbol* ds;
2478 if (!dll_symbols) {
2479 fatal("No symbols");
2481 for (ds = dll_symbols; ds->symbol; ds++)
2482 free(ds->symbol);
2483 free (dll_symbols);
2484 dll_symbols = NULL;
2488 static void do_grab_sym( void )
2490 const IMAGE_EXPORT_DIRECTORY*exportDir;
2491 UINT i, j, *map;
2492 const UINT *pName;
2493 const UINT *pFunc;
2494 const WORD *pOrdl;
2495 const char *ptr;
2497 PE_nt_headers = get_nt_header();
2498 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
2500 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
2501 if (!pName) {printf("Can't grab functions' name table\n"); return;}
2502 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
2503 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
2504 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
2505 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
2507 /* dll_close(); */
2509 dll_symbols = xmalloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol));
2511 /* bit map of used funcs */
2512 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
2513 if (!map) fatal("no memory");
2515 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
2517 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
2518 ptr = RVA(*pName++, sizeof(DWORD));
2519 if (!ptr) ptr = "cant_get_function";
2520 dll_symbols[j].symbol = xstrdup(ptr);
2521 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
2522 assert(dll_symbols[j].symbol);
2525 for (i = 0; i < exportDir->NumberOfFunctions; i++)
2527 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
2529 char ordinal_text[256];
2530 /* Ordinal only entry */
2531 sprintf (ordinal_text, "%s_%u",
2532 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
2533 (UINT)exportDir->Base + i);
2534 str_toupper(ordinal_text);
2535 dll_symbols[j].symbol = xstrdup(ordinal_text);
2536 assert(dll_symbols[j].symbol);
2537 dll_symbols[j].ordinal = exportDir->Base + i;
2538 j++;
2539 assert(j <= exportDir->NumberOfFunctions);
2542 free(map);
2544 if (NORMAL)
2545 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
2546 (UINT)exportDir->NumberOfNames, (UINT)exportDir->NumberOfFunctions,
2547 j, (UINT)exportDir->Base);
2549 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2551 dll_symbols[j].symbol = NULL;
2553 dll_current_symbol = dll_symbols;
2556 /*******************************************************************
2557 * dll_open
2559 * Open a DLL and read in exported symbols
2561 BOOL dll_open (const char *dll_name)
2563 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2566 /*******************************************************************
2567 * dll_next_symbol
2569 * Get next exported symbol from dll
2571 BOOL dll_next_symbol (parsed_symbol * sym)
2573 if (!dll_current_symbol || !dll_current_symbol->symbol)
2574 return FALSE;
2575 assert (dll_symbols);
2576 sym->symbol = xstrdup (dll_current_symbol->symbol);
2577 sym->ordinal = dll_current_symbol->ordinal;
2578 dll_current_symbol++;
2579 return TRUE;