winex11: Directly use win32u for user functions in mouse.c.
[wine.git] / tools / winedump / pe.c
blob2207ed9accc90bdb07fcf4d4ca821d0c52d2c5ca
1 /*
2 * PE dumping utility
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <fcntl.h>
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winedump.h"
35 #define IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE 0x0010 /* Wine extension */
37 static const IMAGE_NT_HEADERS32* PE_nt_headers;
39 static const char builtin_signature[] = "Wine builtin DLL";
40 static const char fakedll_signature[] = "Wine placeholder DLL";
41 static int is_builtin;
43 const char *get_machine_str(int mach)
45 switch (mach)
47 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
48 case IMAGE_FILE_MACHINE_I860: return "i860";
49 case IMAGE_FILE_MACHINE_I386: return "i386";
50 case IMAGE_FILE_MACHINE_R3000: return "R3000";
51 case IMAGE_FILE_MACHINE_R4000: return "R4000";
52 case IMAGE_FILE_MACHINE_R10000: return "R10000";
53 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
54 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
55 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
56 case IMAGE_FILE_MACHINE_IA64: return "IA64";
57 case IMAGE_FILE_MACHINE_ARM64: return "ARM64";
58 case IMAGE_FILE_MACHINE_ARM: return "ARM";
59 case IMAGE_FILE_MACHINE_ARMNT: return "ARMNT";
60 case IMAGE_FILE_MACHINE_THUMB: return "ARM Thumb";
62 return "???";
65 static const void* RVA(unsigned long rva, unsigned long len)
67 IMAGE_SECTION_HEADER* sectHead;
68 int i;
70 if (rva == 0) return NULL;
72 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
73 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
75 if (sectHead[i].VirtualAddress <= rva &&
76 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
78 /* return image import directory offset */
79 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
83 return NULL;
86 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
88 const IMAGE_DOS_HEADER *dos;
89 dos = PRD(0, sizeof(*dos));
90 if (!dos) return NULL;
91 is_builtin = (dos->e_lfanew >= sizeof(*dos) + 32 &&
92 !memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ));
93 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
96 void print_fake_dll( void )
98 const IMAGE_DOS_HEADER *dos;
100 dos = PRD(0, sizeof(*dos) + 32);
101 if (dos && dos->e_lfanew >= sizeof(*dos) + 32)
103 if (!memcmp( dos + 1, builtin_signature, sizeof(builtin_signature) ))
104 printf( "*** This is a Wine builtin DLL ***\n\n" );
105 else if (!memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) ))
106 printf( "*** This is a Wine fake DLL ***\n\n" );
110 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
112 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
114 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
115 if (idx >= opt->NumberOfRvaAndSizes)
116 return NULL;
117 if(size)
118 *size = opt->DataDirectory[idx].Size;
119 return RVA(opt->DataDirectory[idx].VirtualAddress,
120 opt->DataDirectory[idx].Size);
122 else
124 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
125 if (idx >= opt->NumberOfRvaAndSizes)
126 return NULL;
127 if(size)
128 *size = opt->DataDirectory[idx].Size;
129 return RVA(opt->DataDirectory[idx].VirtualAddress,
130 opt->DataDirectory[idx].Size);
134 static const void* get_dir(unsigned idx)
136 return get_dir_and_size(idx, 0);
139 static const char * const DirectoryNames[16] = {
140 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
141 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
142 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
143 "IAT", "Delay IAT", "CLR Header", ""
146 static const char *get_magic_type(WORD magic)
148 switch(magic) {
149 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
150 return "32bit";
151 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
152 return "64bit";
153 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
154 return "ROM";
156 return "???";
159 static inline void print_word(const char *title, WORD value)
161 printf(" %-34s 0x%-4X %u\n", title, value, value);
164 static inline void print_dword(const char *title, DWORD value)
166 printf(" %-34s 0x%-8x %u\n", title, value, value);
169 static inline void print_longlong(const char *title, ULONGLONG value)
171 printf(" %-34s 0x", title);
172 if (sizeof(value) > sizeof(unsigned long) && value >> 32)
173 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
174 else
175 printf("%lx\n", (unsigned long)value);
178 static inline void print_ver(const char *title, BYTE major, BYTE minor)
180 printf(" %-34s %u.%02u\n", title, major, minor);
183 static inline void print_subsys(const char *title, WORD value)
185 const char *str;
186 switch (value)
188 default:
189 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
190 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
191 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
192 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
193 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
194 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
195 case IMAGE_SUBSYSTEM_NATIVE_WINDOWS: str = "native Win9x driver"; break;
196 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI: str = "Windows CE GUI"; break;
197 case IMAGE_SUBSYSTEM_EFI_APPLICATION: str = "EFI application"; break;
198 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: str = "EFI driver (boot)"; break;
199 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: str = "EFI driver (runtime)"; break;
200 case IMAGE_SUBSYSTEM_EFI_ROM: str = "EFI ROM"; break;
201 case IMAGE_SUBSYSTEM_XBOX: str = "Xbox application"; break;
202 case IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION: str = "Boot application"; break;
204 printf(" %-34s 0x%X (%s)\n", title, value, str);
207 static inline void print_dllflags(const char *title, WORD value)
209 printf(" %-34s 0x%04X\n", title, value);
210 #define X(f,s) do { if (value & f) printf(" %s\n", s); } while(0)
211 if (is_builtin) X(IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE, "PREFER_NATIVE (Wine extension)");
212 X(IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA, "HIGH_ENTROPY_VA");
213 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
214 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
215 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
216 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
217 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
218 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
219 X(IMAGE_DLLCHARACTERISTICS_APPCONTAINER, "APPCONTAINER");
220 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
221 X(IMAGE_DLLCHARACTERISTICS_GUARD_CF, "GUARD_CF");
222 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
223 #undef X
226 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
228 unsigned i;
229 printf("Data Directory\n");
231 for (i = 0; i < n && i < 16; i++)
233 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
234 DirectoryNames[i], directory[i].VirtualAddress,
235 directory[i].Size);
239 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
241 IMAGE_OPTIONAL_HEADER32 oh;
242 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
244 /* in case optional header is missing or partial */
245 memset(&oh, 0, sizeof(oh));
246 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
247 optionalHeader = &oh;
249 print_word("Magic", optionalHeader->Magic);
250 print_ver("linker version",
251 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
252 print_dword("size of code", optionalHeader->SizeOfCode);
253 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
254 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
255 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
256 print_dword("base of code", optionalHeader->BaseOfCode);
257 print_dword("base of data", optionalHeader->BaseOfData);
258 print_dword("image base", optionalHeader->ImageBase);
259 print_dword("section align", optionalHeader->SectionAlignment);
260 print_dword("file align", optionalHeader->FileAlignment);
261 print_ver("required OS version",
262 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
263 print_ver("image version",
264 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
265 print_ver("subsystem version",
266 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
267 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
268 print_dword("size of image", optionalHeader->SizeOfImage);
269 print_dword("size of headers", optionalHeader->SizeOfHeaders);
270 print_dword("checksum", optionalHeader->CheckSum);
271 print_subsys("Subsystem", optionalHeader->Subsystem);
272 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
273 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
274 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
275 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
276 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
277 print_dword("loader flags", optionalHeader->LoaderFlags);
278 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
279 printf("\n");
280 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
281 printf("\n");
284 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
286 IMAGE_OPTIONAL_HEADER64 oh;
287 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
289 /* in case optional header is missing or partial */
290 memset(&oh, 0, sizeof(oh));
291 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
292 optionalHeader = &oh;
294 print_word("Magic", optionalHeader->Magic);
295 print_ver("linker version",
296 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
297 print_dword("size of code", optionalHeader->SizeOfCode);
298 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
299 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
300 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
301 print_dword("base of code", optionalHeader->BaseOfCode);
302 print_longlong("image base", optionalHeader->ImageBase);
303 print_dword("section align", optionalHeader->SectionAlignment);
304 print_dword("file align", optionalHeader->FileAlignment);
305 print_ver("required OS version",
306 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
307 print_ver("image version",
308 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
309 print_ver("subsystem version",
310 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
311 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
312 print_dword("size of image", optionalHeader->SizeOfImage);
313 print_dword("size of headers", optionalHeader->SizeOfHeaders);
314 print_dword("checksum", optionalHeader->CheckSum);
315 print_subsys("Subsystem", optionalHeader->Subsystem);
316 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
317 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
318 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
319 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
320 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
321 print_dword("loader flags", optionalHeader->LoaderFlags);
322 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
323 printf("\n");
324 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
325 printf("\n");
328 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
330 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
332 switch(optionalHeader->Magic) {
333 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
334 dump_optional_header32(optionalHeader, header_size);
335 break;
336 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
337 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
338 break;
339 default:
340 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
341 break;
345 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
347 printf("File Header\n");
349 printf(" Machine: %04X (%s)\n",
350 fileHeader->Machine, get_machine_str(fileHeader->Machine));
351 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
352 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
353 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
354 Offset(&(fileHeader->TimeDateStamp)));
355 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
356 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
357 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
358 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
359 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
360 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
361 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
362 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
363 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
364 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
365 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
366 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
367 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
368 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
369 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
370 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
371 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
372 X(IMAGE_FILE_SYSTEM, "SYSTEM");
373 X(IMAGE_FILE_DLL, "DLL");
374 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
375 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
376 #undef X
377 printf("\n");
380 static void dump_pe_header(void)
382 dump_file_header(&PE_nt_headers->FileHeader);
383 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
386 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
388 unsigned offset;
390 /* long section name ? */
391 if (strtable && sectHead->Name[0] == '/' &&
392 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
393 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
394 else
395 printf(" %-8.8s", sectHead->Name);
396 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
397 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
398 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
399 sectHead->PointerToRawData, sectHead->SizeOfRawData);
400 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
401 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
402 printf(" line # offs: %-8u line #'s: %-8u\n",
403 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
404 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
405 printf(" ");
406 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
407 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
408 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
409 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
410 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
411 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
412 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
414 X(IMAGE_SCN_CNT_CODE, "CODE");
415 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
416 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
418 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
419 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
420 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
421 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
422 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
424 /* 0x00002000 - Reserved */
425 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
426 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
428 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
429 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
430 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
431 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
432 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
434 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
436 #define X2(b,s) case b: printf(" " s); break
437 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
438 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
439 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
440 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
441 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
442 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
443 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
444 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
445 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
446 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
447 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
448 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
449 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
450 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
451 #undef X2
454 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
456 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
457 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
458 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
459 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
460 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
461 X(IMAGE_SCN_MEM_READ, "MEM_READ");
462 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
463 #undef X
464 printf("\n\n");
467 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
469 const IMAGE_SECTION_HEADER* sectHead = addr;
470 unsigned i;
471 const char* strtable;
473 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
475 strtable = (const char*)base +
476 PE_nt_headers->FileHeader.PointerToSymbolTable +
477 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
479 else strtable = NULL;
481 printf("Section Table\n");
482 for (i = 0; i < num_sect; i++, sectHead++)
484 dump_section(sectHead, strtable);
486 if (globals.do_dump_rawdata)
488 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
489 printf("\n");
494 static char *get_str( char *buffer, unsigned int rva, unsigned int len )
496 const WCHAR *wstr = PRD( rva, len );
497 char *ret = buffer;
499 len /= sizeof(WCHAR);
500 while (len--) *buffer++ = *wstr++;
501 *buffer = 0;
502 return ret;
505 static void dump_section_apiset(void)
507 const IMAGE_SECTION_HEADER *sect = IMAGE_FIRST_SECTION(PE_nt_headers);
508 const UINT *ptr, *entry, *value, *hash;
509 unsigned int i, j, count, val_count, rva;
510 char buffer[128];
512 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sect++)
514 if (strncmp( (const char *)sect->Name, ".apiset", 8 )) continue;
515 rva = sect->PointerToRawData;
516 ptr = PRD( rva, sizeof(*ptr) );
517 printf( "ApiSet section:\n" );
518 switch (ptr[0]) /* version */
520 case 2:
521 printf( " Version: %u\n", ptr[0] );
522 printf( " Count: %08x\n", ptr[1] );
523 count = ptr[1];
524 if (!(entry = PRD( rva + 2 * sizeof(*ptr), count * 3 * sizeof(*entry) ))) break;
525 for (i = 0; i < count; i++, entry += 3)
527 printf( " %s ->", get_str( buffer, rva + entry[0], entry[1] ));
528 if (!(value = PRD( rva + entry[2], sizeof(*value) ))) break;
529 val_count = *value++;
530 for (j = 0; j < val_count; j++, value += 4)
532 putchar( ' ' );
533 if (value[1]) printf( "%s:", get_str( buffer, rva + value[0], value[1] ));
534 printf( "%s", get_str( buffer, rva + value[2], value[3] ));
536 printf( "\n");
538 break;
539 case 4:
540 printf( " Version: %u\n", ptr[0] );
541 printf( " Size: %08x\n", ptr[1] );
542 printf( " Flags: %08x\n", ptr[2] );
543 printf( " Count: %08x\n", ptr[3] );
544 count = ptr[3];
545 if (!(entry = PRD( rva + 4 * sizeof(*ptr), count * 6 * sizeof(*entry) ))) break;
546 for (i = 0; i < count; i++, entry += 6)
548 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
549 if (!(value = PRD( rva + entry[5], sizeof(*value) ))) break;
550 value++; /* flags */
551 val_count = *value++;
552 for (j = 0; j < val_count; j++, value += 5)
554 putchar( ' ' );
555 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
556 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
558 printf( "\n");
560 break;
561 case 6:
562 printf( " Version: %u\n", ptr[0] );
563 printf( " Size: %08x\n", ptr[1] );
564 printf( " Flags: %08x\n", ptr[2] );
565 printf( " Count: %08x\n", ptr[3] );
566 printf( " EntryOffset: %08x\n", ptr[4] );
567 printf( " HashOffset: %08x\n", ptr[5] );
568 printf( " HashFactor: %08x\n", ptr[6] );
569 count = ptr[3];
570 if (!(entry = PRD( rva + ptr[4], count * 6 * sizeof(*entry) ))) break;
571 for (i = 0; i < count; i++, entry += 6)
573 printf( " %08x %s ->", entry[0], get_str( buffer, rva + entry[1], entry[2] ));
574 if (!(value = PRD( rva + entry[4], entry[5] * 5 * sizeof(*value) ))) break;
575 for (j = 0; j < entry[5]; j++, value += 5)
577 putchar( ' ' );
578 if (value[1]) printf( "%s:", get_str( buffer, rva + value[1], value[2] ));
579 printf( "%s", get_str( buffer, rva + value[3], value[4] ));
581 printf( "\n" );
583 printf( " Hash table:\n" );
584 if (!(hash = PRD( rva + ptr[5], count * 2 * sizeof(*hash) ))) break;
585 for (i = 0; i < count; i++, hash += 2)
587 entry = PRD( rva + ptr[4] + hash[1] * 6 * sizeof(*entry), 6 * sizeof(*entry) );
588 printf( " %08x -> %s\n", hash[0], get_str( buffer, rva + entry[1], entry[3] ));
590 break;
591 default:
592 printf( "*** Unknown version %u\n", ptr[0] );
593 break;
595 break;
599 static void dump_dir_exported_functions(void)
601 unsigned int size = 0;
602 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
603 unsigned int i;
604 const DWORD* pFunc;
605 const DWORD* pName;
606 const WORD* pOrdl;
607 DWORD* funcs;
609 if (!exportDir) return;
611 printf("Exports table:\n");
612 printf("\n");
613 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
614 printf(" Characteristics: %08x\n", exportDir->Characteristics);
615 printf(" TimeDateStamp: %08X %s\n",
616 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
617 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
618 printf(" Ordinal base: %u\n", exportDir->Base);
619 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
620 printf(" # of Names: %u\n", exportDir->NumberOfNames);
621 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
622 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
623 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
624 printf("\n");
625 printf(" Entry Pt Ordn Name\n");
627 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
628 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
629 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
630 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
632 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
633 if (!funcs) fatal("no memory");
635 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
637 for (i = 0; i < exportDir->NumberOfFunctions; i++)
639 if (!pFunc[i]) continue;
640 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
641 if (funcs[i])
642 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
643 else
644 printf("<by ordinal>");
646 /* check for forwarded function */
647 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
648 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
649 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
650 printf("\n");
652 free(funcs);
653 printf("\n");
657 struct runtime_function_x86_64
659 DWORD BeginAddress;
660 DWORD EndAddress;
661 DWORD UnwindData;
664 struct runtime_function_armnt
666 DWORD BeginAddress;
667 union {
668 DWORD UnwindData;
669 struct {
670 DWORD Flag : 2;
671 DWORD FunctionLength : 11;
672 DWORD Ret : 2;
673 DWORD H : 1;
674 DWORD Reg : 3;
675 DWORD R : 1;
676 DWORD L : 1;
677 DWORD C : 1;
678 DWORD StackAdjust : 10;
679 } DUMMYSTRUCTNAME;
680 } DUMMYUNIONNAME;
683 struct runtime_function_arm64
685 DWORD BeginAddress;
686 union
688 DWORD UnwindData;
689 struct
691 DWORD Flag : 2;
692 DWORD FunctionLength : 11;
693 DWORD RegF : 3;
694 DWORD RegI : 4;
695 DWORD H : 1;
696 DWORD CR : 2;
697 DWORD FrameSize : 9;
698 } DUMMYSTRUCTNAME;
699 } DUMMYUNIONNAME;
702 union handler_data
704 struct runtime_function_x86_64 chain;
705 DWORD handler;
708 struct opcode
710 BYTE offset;
711 BYTE code : 4;
712 BYTE info : 4;
715 struct unwind_info_x86_64
717 BYTE version : 3;
718 BYTE flags : 5;
719 BYTE prolog;
720 BYTE count;
721 BYTE frame_reg : 4;
722 BYTE frame_offset : 4;
723 struct opcode opcodes[1]; /* count entries */
724 /* followed by union handler_data */
727 struct unwind_info_armnt
729 DWORD function_length : 18;
730 DWORD version : 2;
731 DWORD x : 1;
732 DWORD e : 1;
733 DWORD f : 1;
734 DWORD count : 5;
735 DWORD words : 4;
738 struct unwind_info_ext_armnt
740 WORD excount;
741 BYTE exwords;
742 BYTE reserved;
745 struct unwind_info_epilogue_armnt
747 DWORD offset : 18;
748 DWORD res : 2;
749 DWORD cond : 4;
750 DWORD index : 8;
753 #define UWOP_PUSH_NONVOL 0
754 #define UWOP_ALLOC_LARGE 1
755 #define UWOP_ALLOC_SMALL 2
756 #define UWOP_SET_FPREG 3
757 #define UWOP_SAVE_NONVOL 4
758 #define UWOP_SAVE_NONVOL_FAR 5
759 #define UWOP_SAVE_XMM128 8
760 #define UWOP_SAVE_XMM128_FAR 9
761 #define UWOP_PUSH_MACHFRAME 10
763 #define UNW_FLAG_EHANDLER 1
764 #define UNW_FLAG_UHANDLER 2
765 #define UNW_FLAG_CHAININFO 4
767 static void dump_x86_64_unwind_info( const struct runtime_function_x86_64 *function )
769 static const char * const reg_names[16] =
770 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
771 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
773 const union handler_data *handler_data;
774 const struct unwind_info_x86_64 *info;
775 unsigned int i, count;
777 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
778 if (function->UnwindData & 1)
780 const struct runtime_function_x86_64 *next = RVA( function->UnwindData & ~1, sizeof(*next) );
781 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
782 return;
784 info = RVA( function->UnwindData, sizeof(*info) );
786 printf( " unwind info at %08x\n", function->UnwindData );
787 if (info->version != 1)
789 printf( " *** unknown version %u\n", info->version );
790 return;
792 printf( " flags %x", info->flags );
793 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
794 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
795 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
796 printf( "\n prolog 0x%x bytes\n", info->prolog );
798 if (info->frame_reg)
799 printf( " frame register %s offset 0x%x(%%rsp)\n",
800 reg_names[info->frame_reg], info->frame_offset * 16 );
802 for (i = 0; i < info->count; i++)
804 printf( " 0x%02x: ", info->opcodes[i].offset );
805 switch (info->opcodes[i].code)
807 case UWOP_PUSH_NONVOL:
808 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
809 break;
810 case UWOP_ALLOC_LARGE:
811 if (info->opcodes[i].info)
813 count = *(const DWORD *)&info->opcodes[i+1];
814 i += 2;
816 else
818 count = *(const USHORT *)&info->opcodes[i+1] * 8;
819 i++;
821 printf( "sub $0x%x,%%rsp\n", count );
822 break;
823 case UWOP_ALLOC_SMALL:
824 count = (info->opcodes[i].info + 1) * 8;
825 printf( "sub $0x%x,%%rsp\n", count );
826 break;
827 case UWOP_SET_FPREG:
828 printf( "lea 0x%x(%%rsp),%s\n",
829 info->frame_offset * 16, reg_names[info->frame_reg] );
830 break;
831 case UWOP_SAVE_NONVOL:
832 count = *(const USHORT *)&info->opcodes[i+1] * 8;
833 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
834 i++;
835 break;
836 case UWOP_SAVE_NONVOL_FAR:
837 count = *(const DWORD *)&info->opcodes[i+1];
838 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
839 i += 2;
840 break;
841 case UWOP_SAVE_XMM128:
842 count = *(const USHORT *)&info->opcodes[i+1] * 16;
843 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
844 i++;
845 break;
846 case UWOP_SAVE_XMM128_FAR:
847 count = *(const DWORD *)&info->opcodes[i+1];
848 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
849 i += 2;
850 break;
851 case UWOP_PUSH_MACHFRAME:
852 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
853 break;
854 default:
855 printf( "*** unknown code %u\n", info->opcodes[i].code );
856 break;
860 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
861 if (info->flags & UNW_FLAG_CHAININFO)
863 printf( " -> function %08x-%08x\n",
864 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
865 return;
867 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
868 printf( " handler %08x data at %08x\n", handler_data->handler,
869 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
872 static const BYTE armnt_code_lengths[256] =
874 /* 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,
875 /* 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,
876 /* 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,
877 /* 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,
878 /* 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,
879 /* 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,
880 /* 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,
881 /* 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
884 static void dump_armnt_unwind_info( const struct runtime_function_armnt *fnc )
886 const struct unwind_info_armnt *info;
887 const struct unwind_info_ext_armnt *infoex;
888 const struct unwind_info_epilogue_armnt *infoepi;
889 unsigned int rva;
890 WORD i, count = 0, words = 0;
892 if (fnc->u.s.Flag)
894 char intregs[32] = {0}, intregspop[32] = {0}, vfpregs[32] = {0};
895 WORD pf = 0, ef = 0, fpoffset = 0, stack = fnc->u.s.StackAdjust;
897 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
898 (fnc->BeginAddress & ~1) + fnc->u.s.FunctionLength * 2 );
899 printf( " Flag %x\n", fnc->u.s.Flag );
900 printf( " FunctionLength %x\n", fnc->u.s.FunctionLength );
901 printf( " Ret %x\n", fnc->u.s.Ret );
902 printf( " H %x\n", fnc->u.s.H );
903 printf( " Reg %x\n", fnc->u.s.Reg );
904 printf( " R %x\n", fnc->u.s.R );
905 printf( " L %x\n", fnc->u.s.L );
906 printf( " C %x\n", fnc->u.s.C );
907 printf( " StackAdjust %x\n", fnc->u.s.StackAdjust );
909 if (fnc->u.s.StackAdjust >= 0x03f4)
911 pf = fnc->u.s.StackAdjust & 0x04;
912 ef = fnc->u.s.StackAdjust & 0x08;
913 stack = (fnc->u.s.StackAdjust & 3) + 1;
916 if (!fnc->u.s.R || pf)
918 int first = 4, last = fnc->u.s.Reg + 4;
919 if (pf)
921 first = (~fnc->u.s.StackAdjust) & 3;
922 if (fnc->u.s.R)
923 last = 3;
925 if (first == last)
926 sprintf(intregs, "r%u", first);
927 else
928 sprintf(intregs, "r%u-r%u", first, last);
929 fpoffset = last + 1 - first;
932 if (!fnc->u.s.R || ef)
934 int first = 4, last = fnc->u.s.Reg + 4;
935 if (ef)
937 first = (~fnc->u.s.StackAdjust) & 3;
938 if (fnc->u.s.R)
939 last = 3;
941 if (first == last)
942 sprintf(intregspop, "r%u", first);
943 else
944 sprintf(intregspop, "r%u-r%u", first, last);
947 if (fnc->u.s.C)
949 if (intregs[0])
950 strcat(intregs, ", ");
951 if (intregspop[0])
952 strcat(intregspop, ", ");
953 strcat(intregs, "r11");
954 strcat(intregspop, "r11");
956 if (fnc->u.s.L)
958 if (intregs[0])
959 strcat(intregs, ", ");
960 strcat(intregs, "lr");
962 if (intregspop[0] && (fnc->u.s.Ret != 0 || !fnc->u.s.H))
963 strcat(intregspop, ", ");
964 if (fnc->u.s.Ret != 0)
965 strcat(intregspop, "lr");
966 else if (!fnc->u.s.H)
967 strcat(intregspop, "pc");
970 if (fnc->u.s.R)
972 if (fnc->u.s.Reg)
973 sprintf(vfpregs, "d8-d%u", fnc->u.s.Reg + 8);
974 else
975 strcpy(vfpregs, "d8");
978 if (fnc->u.s.Flag == 1) {
979 if (fnc->u.s.H)
980 printf( " Unwind Code\tpush {r0-r3}\n" );
982 if (intregs[0])
983 printf( " Unwind Code\tpush {%s}\n", intregs );
985 if (fnc->u.s.C && fpoffset == 0)
986 printf( " Unwind Code\tmov r11, sp\n" );
987 else if (fnc->u.s.C)
988 printf( " Unwind Code\tadd r11, sp, #%d\n", fpoffset * 4 );
990 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
991 printf( " Unwind Code\tvpush {%s}\n", vfpregs );
993 if (stack && !pf)
994 printf( " Unwind Code\tsub sp, sp, #%d\n", stack * 4 );
997 if (fnc->u.s.Ret == 3)
998 return;
999 printf( "Epilogue:\n" );
1001 if (stack && !ef)
1002 printf( " Unwind Code\tadd sp, sp, #%d\n", stack * 4 );
1004 if (fnc->u.s.R && fnc->u.s.Reg != 0x07)
1005 printf( " Unwind Code\tvpop {%s}\n", vfpregs );
1007 if (intregspop[0])
1008 printf( " Unwind Code\tpop {%s}\n", intregspop );
1010 if (fnc->u.s.H && !(fnc->u.s.L && fnc->u.s.Ret == 0))
1011 printf( " Unwind Code\tadd sp, sp, #16\n" );
1012 else if (fnc->u.s.H && (fnc->u.s.L && fnc->u.s.Ret == 0))
1013 printf( " Unwind Code\tldr pc, [sp], #20\n" );
1015 if (fnc->u.s.Ret == 1)
1016 printf( " Unwind Code\tbx <reg>\n" );
1017 else if (fnc->u.s.Ret == 2)
1018 printf( " Unwind Code\tb <address>\n" );
1020 return;
1023 info = RVA( fnc->u.UnwindData, sizeof(*info) );
1024 rva = fnc->u.UnwindData + sizeof(*info);
1025 count = info->count;
1026 words = info->words;
1028 printf( "\nFunction %08x-%08x:\n", fnc->BeginAddress & ~1,
1029 (fnc->BeginAddress & ~1) + info->function_length * 2 );
1030 printf( " unwind info at %08x\n", fnc->u.UnwindData );
1031 printf( " Flag %x\n", fnc->u.s.Flag );
1032 printf( " FunctionLength %x\n", info->function_length );
1033 printf( " Version %x\n", info->version );
1034 printf( " X %x\n", info->x );
1035 printf( " E %x\n", info->e );
1036 printf( " F %x\n", info->f );
1037 printf( " Count %x\n", count );
1038 printf( " Words %x\n", words );
1040 if (!info->count && !info->words)
1042 infoex = RVA( rva, sizeof(*infoex) );
1043 rva = rva + sizeof(*infoex);
1044 count = infoex->excount;
1045 words = infoex->exwords;
1046 printf( " ExtCount %x\n", count );
1047 printf( " ExtWords %x\n", words );
1050 if (!info->e)
1052 infoepi = RVA( rva, count * sizeof(*infoepi) );
1053 rva = rva + count * sizeof(*infoepi);
1055 for (i = 0; i < count; i++)
1057 printf( " Epilogue Scope %x\n", i );
1058 printf( " Offset %x\n", infoepi[i].offset );
1059 printf( " Reserved %x\n", infoepi[i].res );
1060 printf( " Condition %x\n", infoepi[i].cond );
1061 printf( " Index %x\n", infoepi[i].index );
1064 else
1065 infoepi = NULL;
1067 if (words)
1069 const unsigned int *codes;
1070 BYTE b, *bytes;
1071 BOOL inepilogue = FALSE;
1073 codes = RVA( rva, words * sizeof(*codes) );
1074 rva = rva + words * sizeof(*codes);
1075 bytes = (BYTE*)codes;
1077 for (b = 0; b < words * sizeof(*codes); b++)
1079 BYTE code = bytes[b];
1080 BYTE len = armnt_code_lengths[code];
1082 if (info->e && b == count)
1084 printf( "Epilogue:\n" );
1085 inepilogue = TRUE;
1087 else if (!info->e && infoepi)
1089 for (i = 0; i < count; i++)
1090 if (b == infoepi[i].index)
1092 printf( "Epilogue from Scope %x at %08x:\n", i,
1093 (fnc->BeginAddress & ~1) + infoepi[i].offset * 2 );
1094 inepilogue = TRUE;
1098 printf( " Unwind Code");
1099 for (i = 0; i < len; i++)
1100 printf( " %02x", bytes[b+i] );
1101 printf( "\t" );
1103 if (code == 0x00)
1104 printf( "\n" );
1105 else if (code <= 0x7f)
1106 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", code * 4 );
1107 else if (code <= 0xbf)
1109 WORD excode, f;
1110 BOOL first = TRUE;
1111 BYTE excodes = bytes[++b];
1113 excode = (code << 8) | excodes;
1114 printf( "%s {", inepilogue ? "pop" : "push" );
1116 for (f = 0; f <= 12; f++)
1118 if ((excode >> f) & 1)
1120 printf( "%sr%u", first ? "" : ", ", f );
1121 first = FALSE;
1125 if (excode & 0x2000)
1126 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1128 printf( "}\n" );
1130 else if (code <= 0xcf)
1131 if (inepilogue)
1132 printf( "mov sp, r%u\n", code & 0x0f );
1133 else
1134 printf( "mov r%u, sp\n", code & 0x0f );
1135 else if (code <= 0xd7)
1136 if (inepilogue)
1137 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", pc" : "" );
1138 else
1139 printf( "push {r4-r%u%s}\n", (code & 0x03) + 4, (code & 0x04) ? ", lr" : "" );
1140 else if (code <= 0xdf)
1141 if (inepilogue)
1142 printf( "pop {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", pc" : "" );
1143 else
1144 printf( "push {r4-r%u%s}\n", (code & 0x03) + 8, (code & 0x04) ? ", lr" : "" );
1145 else if (code <= 0xe7)
1146 printf( "%s {d8-d%u}\n", inepilogue ? "vpop" : "vpush", (code & 0x07) + 8 );
1147 else if (code <= 0xeb)
1149 WORD excode;
1150 BYTE excodes = bytes[++b];
1152 excode = (code << 8) | excodes;
1153 printf( "%s sp, sp, #%u\n", inepilogue ? "addw" : "subw", (excode & 0x03ff) *4 );
1155 else if (code <= 0xed)
1157 WORD excode, f;
1158 BOOL first = TRUE;
1159 BYTE excodes = bytes[++b];
1161 excode = (code << 8) | excodes;
1162 printf( "%s {", inepilogue ? "pop" : "push" );
1164 for (f = 0; f < 8; f++)
1166 if ((excode >> f) & 1)
1168 printf( "%sr%u", first ? "" : ", ", f );
1169 first = FALSE;
1173 if (excode & 0x0100)
1174 printf( "%s%s", first ? "" : ", ", inepilogue ? "pc" : "lr" );
1176 printf( "}\n" );
1178 else if (code == 0xee)
1179 printf( "unknown 16\n" );
1180 else if (code == 0xef)
1182 WORD excode;
1183 BYTE excodes = bytes[++b];
1185 if (excodes <= 0x0f)
1187 excode = (code << 8) | excodes;
1188 if (inepilogue)
1189 printf( "ldr lr, [sp], #%u\n", (excode & 0x0f) * 4 );
1190 else
1191 printf( "str lr, [sp, #-%u]!\n", (excode & 0x0f) * 4 );
1193 else
1194 printf( "unknown 32\n" );
1196 else if (code <= 0xf4)
1197 printf( "unknown\n" );
1198 else if (code <= 0xf6)
1200 WORD excode, offset = (code == 0xf6) ? 16 : 0;
1201 BYTE excodes = bytes[++b];
1203 excode = (code << 8) | excodes;
1204 printf( "%s {d%u-d%u}\n", inepilogue ? "vpop" : "vpush",
1205 ((excode & 0x00f0) >> 4) + offset, (excode & 0x0f) + offset );
1207 else if (code <= 0xf7)
1209 unsigned int excode;
1210 BYTE excodes[2];
1212 excodes[0] = bytes[++b];
1213 excodes[1] = bytes[++b];
1214 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1215 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1217 else if (code <= 0xf8)
1219 unsigned int excode;
1220 BYTE excodes[3];
1222 excodes[0] = bytes[++b];
1223 excodes[1] = bytes[++b];
1224 excodes[2] = bytes[++b];
1225 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1226 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1228 else if (code <= 0xf9)
1230 unsigned int excode;
1231 BYTE excodes[2];
1233 excodes[0] = bytes[++b];
1234 excodes[1] = bytes[++b];
1235 excode = (code << 16) | (excodes[0] << 8) | excodes[1];
1236 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffff) *4 );
1238 else if (code <= 0xfa)
1240 unsigned int excode;
1241 BYTE excodes[3];
1243 excodes[0] = bytes[++b];
1244 excodes[1] = bytes[++b];
1245 excodes[2] = bytes[++b];
1246 excode = (code << 24) | (excodes[0] << 16) | (excodes[1] << 8) | excodes[2];
1247 printf( "%s sp, sp, #%u\n", inepilogue ? "add" : "sub", (excode & 0xffffff) * 4 );
1249 else if (code <= 0xfb)
1250 printf( "nop\n" );
1251 else if (code <= 0xfc)
1252 printf( "nop.w\n" );
1253 else if (code <= 0xfd)
1255 printf( "(end) nop\n" );
1256 inepilogue = TRUE;
1258 else if (code <= 0xfe)
1260 printf( "(end) nop.w\n" );
1261 inepilogue = TRUE;
1263 else
1265 printf( "end\n" );
1266 inepilogue = TRUE;
1271 if (info->x)
1273 const unsigned int *handler;
1275 handler = RVA( rva, sizeof(*handler) );
1276 rva = rva + sizeof(*handler);
1278 printf( " handler %08x data at %08x\n", *handler, rva);
1282 struct unwind_info_arm64
1284 DWORD function_length : 18;
1285 DWORD version : 2;
1286 DWORD x : 1;
1287 DWORD e : 1;
1288 DWORD epilog : 5;
1289 DWORD codes : 5;
1292 struct unwind_info_ext_arm64
1294 WORD epilog;
1295 BYTE codes;
1296 BYTE reserved;
1299 struct unwind_info_epilog_arm64
1301 DWORD offset : 18;
1302 DWORD res : 4;
1303 DWORD index : 10;
1306 static const BYTE code_lengths[256] =
1308 /* 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,
1309 /* 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,
1310 /* 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,
1311 /* 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,
1312 /* 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,
1313 /* 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,
1314 /* 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,
1315 /* 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
1318 static void dump_arm64_codes( const BYTE *ptr, unsigned int count )
1320 unsigned int i, j;
1322 for (i = 0; i < count; i += code_lengths[ptr[i]])
1324 BYTE len = code_lengths[ptr[i]];
1325 unsigned int val = ptr[i];
1326 if (len == 2) val = ptr[i] * 0x100 + ptr[i+1];
1327 else if (len == 4) val = ptr[i] * 0x1000000 + ptr[i+1] * 0x10000 + ptr[i+2] * 0x100 + ptr[i+3];
1329 printf( " %04x: ", i );
1330 for (j = 0; j < 4; j++)
1331 if (j < len) printf( "%02x ", ptr[i+j] );
1332 else printf( " " );
1334 if (ptr[i] < 0x20) /* alloc_s */
1336 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x1f) );
1338 else if (ptr[i] < 0x40) /* save_r19r20_x */
1340 printf( "stp r19,r20,[sp,-#%#x]!\n", 8 * (val & 0x1f) );
1342 else if (ptr[i] < 0x80) /* save_fplr */
1344 printf( "stp r29,lr,[sp,#%#x]\n", 8 * (val & 0x3f) );
1346 else if (ptr[i] < 0xc0) /* save_fplr_x */
1348 printf( "stp r29,lr,[sp,-#%#x]!\n", 8 * (val & 0x3f) + 8 );
1350 else if (ptr[i] < 0xc8) /* alloc_m */
1352 printf( "sub sp,sp,#%#x\n", 16 * (val & 0x7ff) );
1354 else if (ptr[i] < 0xcc) /* save_regp */
1356 int reg = 19 + ((val >> 6) & 0xf);
1357 printf( "stp r%u,r%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1359 else if (ptr[i] < 0xd0) /* save_regp_x */
1361 int reg = 19 + ((val >> 6) & 0xf);
1362 printf( "stp r%u,r%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1364 else if (ptr[i] < 0xd4) /* save_reg */
1366 int reg = 19 + ((val >> 6) & 0xf);
1367 printf( "str r%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1369 else if (ptr[i] < 0xd6) /* save_reg_x */
1371 int reg = 19 + ((val >> 5) & 0xf);
1372 printf( "str r%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x1f) + 8 );
1374 else if (ptr[i] < 0xd8) /* save_lrpair */
1376 int reg = 19 + 2 * ((val >> 6) & 0x7);
1377 printf( "stp r%u,lr,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1379 else if (ptr[i] < 0xda) /* save_fregp */
1381 int reg = 8 + ((val >> 6) & 0x7);
1382 printf( "stp d%u,d%u,[sp,#%#x]\n", reg, reg + 1, 8 * (val & 0x3f) );
1384 else if (ptr[i] < 0xdc) /* save_fregp_x */
1386 int reg = 8 + ((val >> 6) & 0x7);
1387 printf( "stp d%u,d%u,[sp,-#%#x]!\n", reg, reg + 1, 8 * (val & 0x3f) + 8 );
1389 else if (ptr[i] < 0xde) /* save_freg */
1391 int reg = 8 + ((val >> 6) & 0x7);
1392 printf( "str d%u,[sp,#%#x]\n", reg, 8 * (val & 0x3f) );
1394 else if (ptr[i] == 0xde) /* save_freg_x */
1396 int reg = 8 + ((val >> 5) & 0x7);
1397 printf( "str d%u,[sp,-#%#x]!\n", reg, 8 * (val & 0x3f) + 8 );
1399 else if (ptr[i] == 0xe0) /* alloc_l */
1401 printf( "sub sp,sp,#%#x\n", 16 * (val & 0xffffff) );
1403 else if (ptr[i] == 0xe1) /* set_fp */
1405 printf( "mov x29,sp\n" );
1407 else if (ptr[i] == 0xe2) /* add_fp */
1409 printf( "add x29,sp,#%#x\n", 8 * (val & 0xff) );
1411 else if (ptr[i] == 0xe3) /* nop */
1413 printf( "nop\n" );
1415 else if (ptr[i] == 0xe4) /* end */
1417 printf( "end\n" );
1419 else if (ptr[i] == 0xe5) /* end_c */
1421 printf( "end_c\n" );
1423 else if (ptr[i] == 0xe6) /* save_next */
1425 printf( "save_next\n" );
1427 else if (ptr[i] == 0xe7) /* arithmetic */
1429 switch ((val >> 4) & 0x0f)
1431 case 0: printf( "add lr,lr,x28\n" ); break;
1432 case 1: printf( "add lr,lr,sp\n" ); break;
1433 case 2: printf( "sub lr,lr,x28\n" ); break;
1434 case 3: printf( "sub lr,lr,sp\n" ); break;
1435 case 4: printf( "eor lr,lr,x28\n" ); break;
1436 case 5: printf( "eor lr,lr,sp\n" ); break;
1437 case 6: printf( "rol lr,lr,neg x28\n" ); break;
1438 case 8: printf( "ror lr,lr,x28\n" ); break;
1439 case 9: printf( "ror lr,lr,sp\n" ); break;
1440 default:printf( "unknown op\n" ); break;
1443 else if (ptr[i] == 0xe8) /* MSFT_OP_TRAP_FRAME */
1445 printf( "MSFT_OP_TRAP_FRAME\n" );
1447 else if (ptr[i] == 0xe9) /* MSFT_OP_MACHINE_FRAME */
1449 printf( "MSFT_OP_MACHINE_FRAME\n" );
1451 else if (ptr[i] == 0xea) /* MSFT_OP_CONTEXT */
1453 printf( "MSFT_OP_CONTEXT\n" );
1455 else if (ptr[i] == 0xec) /* MSFT_OP_CLEAR_UNWOUND_TO_CALL */
1457 printf( "MSFT_OP_CLEAR_UNWOUND_TO_CALL\n" );
1459 else printf( "??\n");
1463 static void dump_arm64_packed_info( const struct runtime_function_arm64 *func )
1465 int i, pos = 0, intsz = func->u.s.RegI * 8, fpsz = func->u.s.RegF * 8, savesz, locsz;
1467 if (func->u.s.CR == 1) intsz += 8;
1468 if (func->u.s.RegF) fpsz += 8;
1470 savesz = ((intsz + fpsz + 8 * 8 * func->u.s.H) + 0xf) & ~0xf;
1471 locsz = func->u.s.FrameSize * 16 - savesz;
1473 switch (func->u.s.CR)
1475 case 3:
1476 printf( " %04x: mov x29,sp\n", pos++ );
1477 if (locsz <= 512)
1479 printf( " %04x: stp x29,lr,[sp,-#%#x]!\n", pos++, locsz );
1480 break;
1482 printf( " %04x: stp x29,lr,[sp,0]\n", pos++ );
1483 /* fall through */
1484 case 0:
1485 case 1:
1486 if (locsz <= 4080)
1488 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz );
1490 else
1492 printf( " %04x: sub sp,sp,#%#x\n", pos++, locsz - 4080 );
1493 printf( " %04x: sub sp,sp,#%#x\n", pos++, 4080 );
1495 break;
1498 if (func->u.s.H)
1500 printf( " %04x: stp x6,x7,[sp,#%#x]\n", pos++, intsz + fpsz + 48 );
1501 printf( " %04x: stp x4,x5,[sp,#%#x]\n", pos++, intsz + fpsz + 32 );
1502 printf( " %04x: stp x2,x3,[sp,#%#x]\n", pos++, intsz + fpsz + 16 );
1503 printf( " %04x: stp x0,x1,[sp,#%#x]\n", pos++, intsz + fpsz );
1506 if (func->u.s.RegF)
1508 if (func->u.s.RegF % 2 == 0)
1509 printf( " %04x: str d%u,[sp,#%#x]\n", pos++, 8 + func->u.s.RegF, intsz + fpsz - 8 );
1510 for (i = (func->u.s.RegF - 1)/ 2; i >= 0; i--)
1512 if (!i && !intsz)
1513 printf( " %04x: stp d8,d9,[sp,-#%#x]!\n", pos++, savesz );
1514 else
1515 printf( " %04x: stp d%u,d%u,[sp,#%#x]\n", pos++, 8 + 2 * i, 9 + 2 * i, intsz + 16 * i );
1519 switch (func->u.s.RegI)
1521 case 0:
1522 if (func->u.s.CR == 1)
1523 printf( " %04x: str lr,[sp,-#%#x]!\n", pos++, savesz );
1524 break;
1525 case 1:
1526 if (func->u.s.CR == 1)
1527 printf( " %04x: stp x19,lr,[sp,-#%#x]!\n", pos++, savesz );
1528 else
1529 printf( " %04x: str x19,[sp,-#%#x]!\n", pos++, savesz );
1530 break;
1531 default:
1532 if (func->u.s.RegI % 2)
1534 if (func->u.s.CR == 1)
1535 printf( " %04x: stp x%u,lr,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1536 else
1537 printf( " %04x: str x%u,[sp,#%#x]\n", pos++, 18 + func->u.s.RegI, 8 * func->u.s.RegI - 8 );
1539 else if (func->u.s.CR == 1)
1540 printf( " %04x: str lr,[sp,#%#x]\n", pos++, intsz - 8 );
1542 for (i = func->u.s.RegI / 2 - 1; i >= 0; i--)
1543 if (i)
1544 printf( " %04x: stp x%u,x%u,[sp,#%#x]\n", pos++, 19 + 2 * i, 20 + 2 * i, 16 * i );
1545 else
1546 printf( " %04x: stp x19,x20,[sp,-#%#x]!\n", pos++, savesz );
1547 break;
1549 printf( " %04x: end\n", pos );
1552 static void dump_arm64_unwind_info( const struct runtime_function_arm64 *func )
1554 const struct unwind_info_arm64 *info;
1555 const struct unwind_info_ext_arm64 *infoex;
1556 const struct unwind_info_epilog_arm64 *infoepi;
1557 const BYTE *ptr;
1558 unsigned int i, rva, codes, epilogs;
1560 if (func->u.s.Flag)
1562 printf( "\nFunction %08x-%08x:\n", func->BeginAddress,
1563 func->BeginAddress + func->u.s.FunctionLength * 4 );
1564 printf( " len=%#x flag=%x regF=%u regI=%u H=%u CR=%u frame=%x\n",
1565 func->u.s.FunctionLength, func->u.s.Flag, func->u.s.RegF, func->u.s.RegI,
1566 func->u.s.H, func->u.s.CR, func->u.s.FrameSize );
1567 dump_arm64_packed_info( func );
1568 return;
1571 rva = func->u.UnwindData;
1572 info = RVA( rva, sizeof(*info) );
1573 rva += sizeof(*info);
1574 epilogs = info->epilog;
1575 codes = info->codes;
1577 if (!codes)
1579 infoex = RVA( rva, sizeof(*infoex) );
1580 rva = rva + sizeof(*infoex);
1581 codes = infoex->codes;
1582 epilogs = infoex->epilog;
1584 printf( "\nFunction %08x-%08x:\n",
1585 func->BeginAddress, func->BeginAddress + info->function_length * 4 );
1586 printf( " len=%#x ver=%u X=%u E=%u epilogs=%u codes=%u\n",
1587 info->function_length, info->version, info->x, info->e, epilogs, codes * 4 );
1588 if (info->e)
1590 printf( " epilog 0: code=%04x\n", info->epilog );
1592 else
1594 infoepi = RVA( rva, sizeof(*infoepi) * epilogs );
1595 rva += sizeof(*infoepi) * epilogs;
1596 for (i = 0; i < epilogs; i++)
1597 printf( " epilog %u: pc=%08x code=%04x\n", i,
1598 func->BeginAddress + infoepi[i].offset * 4, infoepi[i].index );
1600 ptr = RVA( rva, codes * 4);
1601 rva += codes * 4;
1602 if (info->x)
1604 const DWORD *handler = RVA( rva, sizeof(*handler) );
1605 rva += sizeof(*handler);
1606 printf( " handler: %08x data %08x\n", *handler, rva );
1608 dump_arm64_codes( ptr, codes * 4 );
1611 static void dump_dir_exceptions(void)
1613 unsigned int i, size = 0;
1614 const void *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
1615 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
1617 if (!funcs) return;
1619 switch (file_header->Machine)
1621 case IMAGE_FILE_MACHINE_AMD64:
1622 size /= sizeof(struct runtime_function_x86_64);
1623 printf( "Exception info (%u functions):\n", size );
1624 for (i = 0; i < size; i++) dump_x86_64_unwind_info( (struct runtime_function_x86_64*)funcs + i );
1625 break;
1626 case IMAGE_FILE_MACHINE_ARMNT:
1627 size /= sizeof(struct runtime_function_armnt);
1628 printf( "Exception info (%u functions):\n", size );
1629 for (i = 0; i < size; i++) dump_armnt_unwind_info( (struct runtime_function_armnt*)funcs + i );
1630 break;
1631 case IMAGE_FILE_MACHINE_ARM64:
1632 size /= sizeof(struct runtime_function_arm64);
1633 printf( "Exception info (%u functions):\n", size );
1634 for (i = 0; i < size; i++) dump_arm64_unwind_info( (struct runtime_function_arm64*)funcs + i );
1635 break;
1636 default:
1637 printf( "Exception information not supported for %s binaries\n",
1638 get_machine_str(file_header->Machine));
1639 break;
1644 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il, DWORD thunk_rva)
1646 /* FIXME: This does not properly handle large images */
1647 const IMAGE_IMPORT_BY_NAME* iibn;
1648 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(LONGLONG))
1650 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
1651 printf(" %08x %4u <by ordinal>\n", thunk_rva, (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
1652 else
1654 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
1655 if (!iibn)
1656 printf("Can't grab import by name info, skipping to next ordinal\n");
1657 else
1658 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1663 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset, DWORD thunk_rva)
1665 const IMAGE_IMPORT_BY_NAME* iibn;
1666 for (; il->u1.Ordinal; il++, thunk_rva += sizeof(DWORD))
1668 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
1669 printf(" %08x %4u <by ordinal>\n", thunk_rva, IMAGE_ORDINAL32(il->u1.Ordinal));
1670 else
1672 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
1673 if (!iibn)
1674 printf("Can't grab import by name info, skipping to next ordinal\n");
1675 else
1676 printf(" %08x %4u %s\n", thunk_rva, iibn->Hint, iibn->Name);
1681 static void dump_dir_imported_functions(void)
1683 unsigned directorySize;
1684 const IMAGE_IMPORT_DESCRIPTOR* importDesc = get_dir_and_size(IMAGE_FILE_IMPORT_DIRECTORY, &directorySize);
1686 if (!importDesc) return;
1688 printf("Import Table size: %08x\n", directorySize);/* FIXME */
1690 for (;;)
1692 const IMAGE_THUNK_DATA32* il;
1694 if (!importDesc->Name || !importDesc->FirstThunk) break;
1696 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
1697 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
1698 printf(" TimeDateStamp: %08X (%s)\n",
1699 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1700 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
1701 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
1703 printf(" Thunk Ordn Name\n");
1705 il = (importDesc->u.OriginalFirstThunk != 0) ?
1706 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
1707 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
1709 if (!il)
1710 printf("Can't grab thunk data, going to next imported DLL\n");
1711 else
1713 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1714 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il, importDesc->FirstThunk);
1715 else
1716 dump_image_thunk_data32(il, 0, importDesc->FirstThunk);
1717 printf("\n");
1719 importDesc++;
1721 printf("\n");
1724 static void dump_dir_loadconfig(void)
1726 const IMAGE_LOAD_CONFIG_DIRECTORY32 *loadcfg32 = get_dir(IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG);
1727 const IMAGE_LOAD_CONFIG_DIRECTORY64 *loadcfg64 = (void*)loadcfg32;
1729 if (!loadcfg32) return;
1731 printf( "Loadconfig\n" );
1732 print_dword( "Size", loadcfg32->Size );
1733 print_dword( "TimeDateStamp", loadcfg32->TimeDateStamp );
1734 print_word( "MajorVersion", loadcfg32->MajorVersion );
1735 print_word( "MinorVersion", loadcfg32->MinorVersion );
1736 print_dword( "GlobalFlagsClear", loadcfg32->GlobalFlagsClear );
1737 print_dword( "GlobalFlagsSet", loadcfg32->GlobalFlagsSet );
1738 print_dword( "CriticalSectionDefaultTimeout", loadcfg32->CriticalSectionDefaultTimeout );
1740 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1742 print_longlong( "DeCommitFreeBlockThreshold", loadcfg64->DeCommitFreeBlockThreshold );
1743 print_longlong( "DeCommitTotalFreeThreshold", loadcfg64->DeCommitTotalFreeThreshold );
1744 print_longlong( "MaximumAllocationSize", loadcfg64->MaximumAllocationSize );
1745 print_longlong( "VirtualMemoryThreshold", loadcfg64->VirtualMemoryThreshold );
1746 print_dword( "ProcessHeapFlags", loadcfg64->ProcessHeapFlags );
1747 print_longlong( "ProcessAffinityMask", loadcfg64->ProcessAffinityMask );
1748 print_word( "CSDVersion", loadcfg64->CSDVersion );
1749 print_word( "Reserved", loadcfg64->Reserved1 );
1750 print_longlong( "SecurityCookie", loadcfg64->SecurityCookie );
1751 print_longlong( "SEHandlerTable", loadcfg64->SEHandlerTable );
1752 print_longlong( "SEHandlerCount", loadcfg64->SEHandlerCount );
1754 else
1756 print_dword( "DeCommitFreeBlockThreshold", loadcfg32->DeCommitFreeBlockThreshold );
1757 print_dword( "DeCommitTotalFreeThreshold", loadcfg32->DeCommitTotalFreeThreshold );
1758 print_dword( "MaximumAllocationSize", loadcfg32->MaximumAllocationSize );
1759 print_dword( "VirtualMemoryThreshold", loadcfg32->VirtualMemoryThreshold );
1760 print_dword( "ProcessHeapFlags", loadcfg32->ProcessHeapFlags );
1761 print_dword( "ProcessAffinityMask", loadcfg32->ProcessAffinityMask );
1762 print_word( "CSDVersion", loadcfg32->CSDVersion );
1763 print_word( "Reserved", loadcfg32->Reserved1 );
1764 print_dword( "SecurityCookie", loadcfg32->SecurityCookie );
1765 print_dword( "SEHandlerTable", loadcfg32->SEHandlerTable );
1766 print_dword( "SEHandlerCount", loadcfg32->SEHandlerCount );
1770 static void dump_dir_delay_imported_functions(void)
1772 unsigned directorySize;
1773 const IMAGE_DELAYLOAD_DESCRIPTOR *importDesc = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT, &directorySize);
1775 if (!importDesc) return;
1777 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
1779 for (;;)
1781 const IMAGE_THUNK_DATA32* il;
1782 int offset = (importDesc->Attributes.AllAttributes & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
1784 if (!importDesc->DllNameRVA || !importDesc->ImportAddressTableRVA || !importDesc->ImportNameTableRVA) break;
1786 printf(" grAttrs %08x offset %08lx %s\n", importDesc->Attributes.AllAttributes, Offset(importDesc),
1787 (const char *)RVA(importDesc->DllNameRVA - offset, sizeof(DWORD)));
1788 printf(" Hint/Name Table: %08x\n", importDesc->ImportNameTableRVA);
1789 printf(" Address Table: %08x\n", importDesc->ImportAddressTableRVA);
1790 printf(" TimeDateStamp: %08X (%s)\n",
1791 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
1793 printf(" Thunk Ordn Name\n");
1795 il = RVA(importDesc->ImportNameTableRVA - offset, sizeof(DWORD));
1797 if (!il)
1798 printf("Can't grab thunk data, going to next imported DLL\n");
1799 else
1801 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1802 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il, importDesc->ImportAddressTableRVA);
1803 else
1804 dump_image_thunk_data32(il, offset, importDesc->ImportAddressTableRVA);
1805 printf("\n");
1807 importDesc++;
1809 printf("\n");
1812 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
1814 const char* str;
1816 printf("Directory %02u\n", idx + 1);
1817 printf(" Characteristics: %08X\n", idd->Characteristics);
1818 printf(" TimeDateStamp: %08X %s\n",
1819 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
1820 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
1821 switch (idd->Type)
1823 default:
1824 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
1825 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
1826 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
1827 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
1828 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
1829 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
1830 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
1831 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
1832 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
1833 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
1834 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
1835 case IMAGE_DEBUG_TYPE_CLSID: str = "CLSID"; break;
1836 case IMAGE_DEBUG_TYPE_VC_FEATURE: str = "VC_FEATURE"; break;
1837 case IMAGE_DEBUG_TYPE_POGO: str = "POGO"; break;
1838 case IMAGE_DEBUG_TYPE_ILTCG: str = "ILTCG"; break;
1839 case IMAGE_DEBUG_TYPE_MPX: str = "MPX"; break;
1840 case IMAGE_DEBUG_TYPE_REPRO: str = "REPRO"; break;
1842 printf(" Type: %u (%s)\n", idd->Type, str);
1843 printf(" SizeOfData: %u\n", idd->SizeOfData);
1844 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
1845 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
1847 switch (idd->Type)
1849 case IMAGE_DEBUG_TYPE_UNKNOWN:
1850 break;
1851 case IMAGE_DEBUG_TYPE_COFF:
1852 dump_coff(idd->PointerToRawData, idd->SizeOfData,
1853 IMAGE_FIRST_SECTION(PE_nt_headers));
1854 break;
1855 case IMAGE_DEBUG_TYPE_CODEVIEW:
1856 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
1857 break;
1858 case IMAGE_DEBUG_TYPE_FPO:
1859 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
1860 break;
1861 case IMAGE_DEBUG_TYPE_MISC:
1863 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
1864 if (!misc) {printf("Can't get misc debug information\n"); break;}
1865 printf(" DataType: %u (%s)\n",
1866 misc->DataType,
1867 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
1868 printf(" Length: %u\n", misc->Length);
1869 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
1870 printf(" Data: %s\n", misc->Data);
1872 break;
1873 default: break;
1875 printf("\n");
1878 static void dump_dir_debug(void)
1880 unsigned nb_dbg, i;
1881 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir_and_size(IMAGE_FILE_DEBUG_DIRECTORY, &nb_dbg);
1883 nb_dbg /= sizeof(*debugDir);
1884 if (!debugDir || !nb_dbg) return;
1886 printf("Debug Table (%u directories)\n", nb_dbg);
1888 for (i = 0; i < nb_dbg; i++)
1890 dump_dir_debug_dir(debugDir, i);
1891 debugDir++;
1893 printf("\n");
1896 static inline void print_clrflags(const char *title, DWORD value)
1898 printf(" %-34s 0x%X\n", title, value);
1899 #define X(f,s) if (value & f) printf(" %s\n", s)
1900 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
1901 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
1902 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
1903 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
1904 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
1905 #undef X
1908 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
1910 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
1913 static void dump_dir_clr_header(void)
1915 unsigned int size = 0;
1916 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
1918 if (!dir) return;
1920 printf( "CLR Header\n" );
1921 print_dword( "Header Size", dir->cb );
1922 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
1923 print_clrflags( "Flags", dir->Flags );
1924 print_dword( "EntryPointToken", dir->u.EntryPointToken );
1925 printf("\n");
1926 printf( "CLR Data Directory\n" );
1927 print_clrdirectory( "MetaData", &dir->MetaData );
1928 print_clrdirectory( "Resources", &dir->Resources );
1929 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
1930 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
1931 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
1932 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
1933 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
1934 printf("\n");
1937 static void dump_dir_reloc(void)
1939 unsigned int i, size = 0;
1940 const USHORT *relocs;
1941 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
1942 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1943 static const char * const names[] =
1945 "BASED_ABSOLUTE",
1946 "BASED_HIGH",
1947 "BASED_LOW",
1948 "BASED_HIGHLOW",
1949 "BASED_HIGHADJ",
1950 "BASED_MIPS_JMPADDR",
1951 "BASED_SECTION",
1952 "BASED_REL",
1953 "unknown 8",
1954 "BASED_IA64_IMM64",
1955 "BASED_DIR64",
1956 "BASED_HIGH3ADJ",
1957 "unknown 12",
1958 "unknown 13",
1959 "unknown 14",
1960 "unknown 15"
1963 if (!rel) return;
1965 printf( "Relocations\n" );
1966 while (rel < end - 1 && rel->SizeOfBlock)
1968 printf( " Page %x\n", rel->VirtualAddress );
1969 relocs = (const USHORT *)(rel + 1);
1970 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1971 while (i--)
1973 USHORT offset = *relocs & 0xfff;
1974 int type = *relocs >> 12;
1975 printf( " off %04x type %s\n", offset, names[type] );
1976 relocs++;
1978 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1980 printf("\n");
1983 static void dump_dir_tls(void)
1985 IMAGE_TLS_DIRECTORY64 dir;
1986 const DWORD *callbacks;
1987 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1989 if (!pdir) return;
1991 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1992 memcpy(&dir, pdir, sizeof(dir));
1993 else
1995 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1996 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1997 dir.AddressOfIndex = pdir->AddressOfIndex;
1998 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1999 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
2000 dir.Characteristics = pdir->Characteristics;
2003 /* FIXME: This does not properly handle large images */
2004 printf( "Thread Local Storage\n" );
2005 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
2006 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
2007 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
2008 (DWORD)dir.SizeOfZeroFill );
2009 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
2010 printf( " Characteristics %08x\n", dir.Characteristics );
2011 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
2012 if (dir.AddressOfCallBacks)
2014 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
2015 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
2017 printf( " %08x", *callbacks );
2018 addr += sizeof(DWORD);
2021 printf(" }\n\n");
2024 enum FileSig get_kind_dbg(void)
2026 const WORD* pw;
2028 pw = PRD(0, sizeof(WORD));
2029 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2031 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
2032 return SIG_UNKNOWN;
2035 void dbg_dump(void)
2037 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
2038 unsigned nb_dbg;
2039 unsigned i;
2040 const IMAGE_DEBUG_DIRECTORY* debugDir;
2042 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
2043 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
2045 printf ("Signature: %.2s (0x%4X)\n",
2046 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
2047 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
2048 printf ("Machine: 0x%04X (%s)\n",
2049 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
2050 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
2051 printf ("TimeDateStamp: 0x%08X (%s)\n",
2052 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
2053 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
2054 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
2055 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
2056 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
2057 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
2058 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
2060 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
2061 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
2062 {printf("Can't get the sections, aborting\n"); return;}
2064 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
2066 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
2067 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
2068 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
2069 separateDebugHead->ExportedNamesSize,
2070 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
2071 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
2073 printf("Debug Table (%u directories)\n", nb_dbg);
2075 for (i = 0; i < nb_dbg; i++)
2077 dump_dir_debug_dir(debugDir, i);
2078 debugDir++;
2082 static const char *get_resource_type( unsigned int id )
2084 static const char * const types[] =
2086 NULL,
2087 "CURSOR",
2088 "BITMAP",
2089 "ICON",
2090 "MENU",
2091 "DIALOG",
2092 "STRING",
2093 "FONTDIR",
2094 "FONT",
2095 "ACCELERATOR",
2096 "RCDATA",
2097 "MESSAGETABLE",
2098 "GROUP_CURSOR",
2099 NULL,
2100 "GROUP_ICON",
2101 NULL,
2102 "VERSION",
2103 "DLGINCLUDE",
2104 NULL,
2105 "PLUGPLAY",
2106 "VXD",
2107 "ANICURSOR",
2108 "ANIICON",
2109 "HTML",
2110 "RT_MANIFEST"
2113 if ((size_t)id < ARRAY_SIZE(types)) return types[id];
2114 return NULL;
2117 /* dump an ASCII string with proper escaping */
2118 static int dump_strA( const unsigned char *str, size_t len )
2120 static const char escapes[32] = ".......abtnvfr.............e....";
2121 char buffer[256];
2122 char *pos = buffer;
2123 int count = 0;
2125 for (; len; str++, len--)
2127 if (pos > buffer + sizeof(buffer) - 8)
2129 fwrite( buffer, pos - buffer, 1, stdout );
2130 count += pos - buffer;
2131 pos = buffer;
2133 if (*str > 127) /* hex escape */
2135 pos += sprintf( pos, "\\x%02x", *str );
2136 continue;
2138 if (*str < 32) /* octal or C escape */
2140 if (!*str && len == 1) continue; /* do not output terminating NULL */
2141 if (escapes[*str] != '.')
2142 pos += sprintf( pos, "\\%c", escapes[*str] );
2143 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2144 pos += sprintf( pos, "\\%03o", *str );
2145 else
2146 pos += sprintf( pos, "\\%o", *str );
2147 continue;
2149 if (*str == '\\') *pos++ = '\\';
2150 *pos++ = *str;
2152 fwrite( buffer, pos - buffer, 1, stdout );
2153 count += pos - buffer;
2154 return count;
2157 /* dump a Unicode string with proper escaping */
2158 static int dump_strW( const WCHAR *str, size_t len )
2160 static const char escapes[32] = ".......abtnvfr.............e....";
2161 char buffer[256];
2162 char *pos = buffer;
2163 int count = 0;
2165 for (; len; str++, len--)
2167 if (pos > buffer + sizeof(buffer) - 8)
2169 fwrite( buffer, pos - buffer, 1, stdout );
2170 count += pos - buffer;
2171 pos = buffer;
2173 if (*str > 127) /* hex escape */
2175 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
2176 pos += sprintf( pos, "\\x%04x", *str );
2177 else
2178 pos += sprintf( pos, "\\x%x", *str );
2179 continue;
2181 if (*str < 32) /* octal or C escape */
2183 if (!*str && len == 1) continue; /* do not output terminating NULL */
2184 if (escapes[*str] != '.')
2185 pos += sprintf( pos, "\\%c", escapes[*str] );
2186 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
2187 pos += sprintf( pos, "\\%03o", *str );
2188 else
2189 pos += sprintf( pos, "\\%o", *str );
2190 continue;
2192 if (*str == '\\') *pos++ = '\\';
2193 *pos++ = *str;
2195 fwrite( buffer, pos - buffer, 1, stdout );
2196 count += pos - buffer;
2197 return count;
2200 /* dump data for a STRING resource */
2201 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
2203 int i;
2205 for (i = 0; i < 16 && size; i++)
2207 unsigned len = *ptr++;
2209 if (len >= size)
2211 len = size;
2212 size = 0;
2214 else size -= len + 1;
2216 if (len)
2218 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
2219 dump_strW( ptr, len );
2220 printf( "\"\n" );
2221 ptr += len;
2226 /* dump data for a MESSAGETABLE resource */
2227 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
2229 const MESSAGE_RESOURCE_DATA *data = ptr;
2230 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
2231 unsigned i, j;
2233 for (i = 0; i < data->NumberOfBlocks; i++, block++)
2235 const MESSAGE_RESOURCE_ENTRY *entry;
2237 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
2238 for (j = block->LowId; j <= block->HighId; j++)
2240 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
2242 const WCHAR *str = (const WCHAR *)entry->Text;
2243 printf( "%s%08x L\"", prefix, j );
2244 dump_strW( str, strlenW(str) );
2245 printf( "\"\n" );
2247 else
2249 const char *str = (const char *) entry->Text;
2250 printf( "%s%08x \"", prefix, j );
2251 dump_strA( entry->Text, strlen(str) );
2252 printf( "\"\n" );
2254 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
2259 static void dump_dir_resource(void)
2261 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
2262 const IMAGE_RESOURCE_DIRECTORY *namedir;
2263 const IMAGE_RESOURCE_DIRECTORY *langdir;
2264 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
2265 const IMAGE_RESOURCE_DIR_STRING_U *string;
2266 const IMAGE_RESOURCE_DATA_ENTRY *data;
2267 int i, j, k;
2269 if (!root) return;
2271 printf( "Resources:" );
2273 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
2275 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
2276 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s2.OffsetToDirectory);
2277 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
2279 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
2280 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s2.OffsetToDirectory);
2281 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
2283 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
2285 printf( "\n " );
2286 if (e1->u.s.NameIsString)
2288 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u.s.NameOffset);
2289 dump_unicode_str( string->NameString, string->Length );
2291 else
2293 const char *type = get_resource_type( e1->u.Id );
2294 if (type) printf( "%s", type );
2295 else printf( "%04x", e1->u.Id );
2298 printf( " Name=" );
2299 if (e2->u.s.NameIsString)
2301 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u.s.NameOffset);
2302 dump_unicode_str( string->NameString, string->Length );
2304 else
2305 printf( "%04x", e2->u.Id );
2307 printf( " Language=%04x:\n", e3->u.Id );
2308 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
2309 if (e1->u.s.NameIsString)
2311 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2313 else switch(e1->u.Id)
2315 case 6:
2316 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size, e2->u.Id, " " );
2317 break;
2318 case 11:
2319 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
2320 e2->u.Id, " " );
2321 break;
2322 default:
2323 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
2324 break;
2329 printf( "\n\n" );
2332 static void dump_debug(void)
2334 const char* stabs = NULL;
2335 unsigned szstabs = 0;
2336 const char* stabstr = NULL;
2337 unsigned szstr = 0;
2338 unsigned i;
2339 const IMAGE_SECTION_HEADER* sectHead;
2341 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
2343 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
2345 if (!strcmp((const char *)sectHead->Name, ".stab"))
2347 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2348 szstabs = sectHead->Misc.VirtualSize;
2350 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
2352 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
2353 szstr = sectHead->Misc.VirtualSize;
2356 if (stabs && stabstr)
2357 dump_stabs(stabs, szstabs, stabstr, szstr);
2360 static void dump_symbol_table(void)
2362 const IMAGE_SYMBOL* sym;
2363 int numsym;
2365 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
2366 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
2367 return;
2368 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
2369 sizeof(*sym) * numsym);
2370 if (!sym) return;
2372 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
2375 enum FileSig get_kind_exec(void)
2377 const WORD* pw;
2378 const DWORD* pdw;
2379 const IMAGE_DOS_HEADER* dh;
2381 pw = PRD(0, sizeof(WORD));
2382 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
2384 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
2386 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
2388 /* the signature is the first DWORD */
2389 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
2390 if (pdw)
2392 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
2393 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
2394 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
2396 return SIG_DOS;
2398 return SIG_UNKNOWN;
2401 void pe_dump(void)
2403 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
2405 PE_nt_headers = get_nt_header();
2406 print_fake_dll();
2408 if (globals.do_dumpheader)
2410 dump_pe_header();
2411 /* FIXME: should check ptr */
2412 dump_sections(PRD(0, 1), IMAGE_FIRST_SECTION(PE_nt_headers),
2413 PE_nt_headers->FileHeader.NumberOfSections);
2415 else if (!globals.dumpsect)
2417 /* show at least something here */
2418 dump_pe_header();
2421 if (globals.dumpsect)
2423 if (all || !strcmp(globals.dumpsect, "import"))
2425 dump_dir_imported_functions();
2426 dump_dir_delay_imported_functions();
2428 if (all || !strcmp(globals.dumpsect, "export"))
2429 dump_dir_exported_functions();
2430 if (all || !strcmp(globals.dumpsect, "debug"))
2431 dump_dir_debug();
2432 if (all || !strcmp(globals.dumpsect, "resource"))
2433 dump_dir_resource();
2434 if (all || !strcmp(globals.dumpsect, "tls"))
2435 dump_dir_tls();
2436 if (all || !strcmp(globals.dumpsect, "loadcfg"))
2437 dump_dir_loadconfig();
2438 if (all || !strcmp(globals.dumpsect, "clr"))
2439 dump_dir_clr_header();
2440 if (all || !strcmp(globals.dumpsect, "reloc"))
2441 dump_dir_reloc();
2442 if (all || !strcmp(globals.dumpsect, "except"))
2443 dump_dir_exceptions();
2444 if (all || !strcmp(globals.dumpsect, "apiset"))
2445 dump_section_apiset();
2447 if (globals.do_symbol_table)
2448 dump_symbol_table();
2449 if (globals.do_debug)
2450 dump_debug();
2453 typedef struct _dll_symbol {
2454 size_t ordinal;
2455 char *symbol;
2456 } dll_symbol;
2458 static dll_symbol *dll_symbols = NULL;
2459 static dll_symbol *dll_current_symbol = NULL;
2461 /* Compare symbols by ordinal for qsort */
2462 static int symbol_cmp(const void *left, const void *right)
2464 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
2467 /*******************************************************************
2468 * dll_close
2470 * Free resources used by DLL
2472 /* FIXME: Not used yet
2473 static void dll_close (void)
2475 dll_symbol* ds;
2477 if (!dll_symbols) {
2478 fatal("No symbols");
2480 for (ds = dll_symbols; ds->symbol; ds++)
2481 free(ds->symbol);
2482 free (dll_symbols);
2483 dll_symbols = NULL;
2487 static void do_grab_sym( void )
2489 const IMAGE_EXPORT_DIRECTORY*exportDir;
2490 unsigned i, j;
2491 const DWORD* pName;
2492 const DWORD* pFunc;
2493 const WORD* pOrdl;
2494 const char* ptr;
2495 DWORD* map;
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 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 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
2548 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
2550 dll_symbols[j].symbol = NULL;
2552 dll_current_symbol = dll_symbols;
2555 /*******************************************************************
2556 * dll_open
2558 * Open a DLL and read in exported symbols
2560 BOOL dll_open (const char *dll_name)
2562 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
2565 /*******************************************************************
2566 * dll_next_symbol
2568 * Get next exported symbol from dll
2570 BOOL dll_next_symbol (parsed_symbol * sym)
2572 if (!dll_current_symbol || !dll_current_symbol->symbol)
2573 return FALSE;
2574 assert (dll_symbols);
2575 sym->symbol = xstrdup (dll_current_symbol->symbol);
2576 sym->ordinal = dll_current_symbol->ordinal;
2577 dll_current_symbol++;
2578 return TRUE;