d3dcompiler: D3DAssemble stub.
[wine/multimedia.git] / tools / winedump / pe.c
blob48578d68a3933d85650f5bdaf1c582e4a7e07d3a
1 /*
2 * PE dumping utility
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include <fcntl.h>
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winedump.h"
48 static const IMAGE_NT_HEADERS32* PE_nt_headers;
50 const char *get_machine_str(int mach)
52 switch (mach)
54 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
55 case IMAGE_FILE_MACHINE_I860: return "i860";
56 case IMAGE_FILE_MACHINE_I386: return "i386";
57 case IMAGE_FILE_MACHINE_R3000: return "R3000";
58 case IMAGE_FILE_MACHINE_R4000: return "R4000";
59 case IMAGE_FILE_MACHINE_R10000: return "R10000";
60 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
61 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
62 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
63 case IMAGE_FILE_MACHINE_IA64: return "IA64";
64 case IMAGE_FILE_MACHINE_ARM: return "ARM";
65 case IMAGE_FILE_MACHINE_SPARC: return "SPARC";
67 return "???";
70 static const void* RVA(unsigned long rva, unsigned long len)
72 IMAGE_SECTION_HEADER* sectHead;
73 int i;
75 if (rva == 0) return NULL;
77 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
78 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
80 if (sectHead[i].VirtualAddress <= rva &&
81 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
83 /* return image import directory offset */
84 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
88 return NULL;
91 static const IMAGE_NT_HEADERS32 *get_nt_header( void )
93 const IMAGE_DOS_HEADER *dos;
94 dos = PRD(0, sizeof(*dos));
95 if (!dos) return NULL;
96 return PRD(dos->e_lfanew, sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER));
99 static int is_fake_dll( void )
101 static const char fakedll_signature[] = "Wine placeholder DLL";
102 const IMAGE_DOS_HEADER *dos;
104 dos = PRD(0, sizeof(*dos) + sizeof(fakedll_signature));
106 if (dos && dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
107 !memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return TRUE;
108 return FALSE;
111 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
113 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
115 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
116 if (idx >= opt->NumberOfRvaAndSizes)
117 return NULL;
118 if(size)
119 *size = opt->DataDirectory[idx].Size;
120 return RVA(opt->DataDirectory[idx].VirtualAddress,
121 opt->DataDirectory[idx].Size);
123 else
125 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
126 if (idx >= opt->NumberOfRvaAndSizes)
127 return NULL;
128 if(size)
129 *size = opt->DataDirectory[idx].Size;
130 return RVA(opt->DataDirectory[idx].VirtualAddress,
131 opt->DataDirectory[idx].Size);
135 static const void* get_dir(unsigned idx)
137 return get_dir_and_size(idx, 0);
140 static const char * const DirectoryNames[16] = {
141 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
142 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
143 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
144 "IAT", "Delay IAT", "CLR Header", ""
147 static const char *get_magic_type(WORD magic)
149 switch(magic) {
150 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
151 return "32bit";
152 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
153 return "64bit";
154 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
155 return "ROM";
157 return "???";
160 static inline void print_word(const char *title, WORD value)
162 printf(" %-34s 0x%-4X %u\n", title, value, value);
165 static inline void print_dword(const char *title, DWORD value)
167 printf(" %-34s 0x%-8x %u\n", title, value, value);
170 static inline void print_longlong(const char *title, ULONGLONG value)
172 printf(" %-34s 0x", title);
173 if(value >> 32)
174 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
175 else
176 printf("%lx\n", (unsigned long)value);
179 static inline void print_ver(const char *title, BYTE major, BYTE minor)
181 printf(" %-34s %u.%02u\n", title, major, minor);
184 static inline void print_subsys(const char *title, WORD value)
186 const char *str;
187 switch (value)
189 default:
190 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
191 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
192 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
193 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
194 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
195 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
197 printf(" %-34s 0x%X (%s)\n", title, value, str);
200 static inline void print_dllflags(const char *title, WORD value)
202 printf(" %-34s 0x%X\n", title, value);
203 #define X(f,s) if (value & f) printf(" %s\n", s)
204 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE, "DYNAMIC_BASE");
205 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY, "FORCE_INTEGRITY");
206 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT, "NX_COMPAT");
207 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
208 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
209 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
210 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
211 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
212 #undef X
215 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
217 unsigned i;
218 printf("Data Directory\n");
220 for (i = 0; i < n && i < 16; i++)
222 printf(" %-12s rva: 0x%-8x size: 0x%-8x\n",
223 DirectoryNames[i], directory[i].VirtualAddress,
224 directory[i].Size);
228 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *image_oh, UINT header_size)
230 IMAGE_OPTIONAL_HEADER32 oh;
231 const IMAGE_OPTIONAL_HEADER32 *optionalHeader;
233 /* in case optional header is missing or partial */
234 memset(&oh, 0, sizeof(oh));
235 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
236 optionalHeader = &oh;
238 print_word("Magic", optionalHeader->Magic);
239 print_ver("linker version",
240 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
241 print_dword("size of code", optionalHeader->SizeOfCode);
242 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
243 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
244 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
245 print_dword("base of code", optionalHeader->BaseOfCode);
246 print_dword("base of data", optionalHeader->BaseOfData);
247 print_dword("image base", optionalHeader->ImageBase);
248 print_dword("section align", optionalHeader->SectionAlignment);
249 print_dword("file align", optionalHeader->FileAlignment);
250 print_ver("required OS version",
251 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
252 print_ver("image version",
253 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
254 print_ver("subsystem version",
255 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
256 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
257 print_dword("size of image", optionalHeader->SizeOfImage);
258 print_dword("size of headers", optionalHeader->SizeOfHeaders);
259 print_dword("checksum", optionalHeader->CheckSum);
260 print_subsys("Subsystem", optionalHeader->Subsystem);
261 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
262 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
263 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
264 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
265 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
266 print_dword("loader flags", optionalHeader->LoaderFlags);
267 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
268 printf("\n");
269 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
270 printf("\n");
273 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *image_oh, UINT header_size)
275 IMAGE_OPTIONAL_HEADER64 oh;
276 const IMAGE_OPTIONAL_HEADER64 *optionalHeader;
278 /* in case optional header is missing or partial */
279 memset(&oh, 0, sizeof(oh));
280 memcpy(&oh, image_oh, min(header_size, sizeof(oh)));
281 optionalHeader = &oh;
283 print_word("Magic", optionalHeader->Magic);
284 print_ver("linker version",
285 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
286 print_dword("size of code", optionalHeader->SizeOfCode);
287 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
288 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
289 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
290 print_dword("base of code", optionalHeader->BaseOfCode);
291 print_longlong("image base", optionalHeader->ImageBase);
292 print_dword("section align", optionalHeader->SectionAlignment);
293 print_dword("file align", optionalHeader->FileAlignment);
294 print_ver("required OS version",
295 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
296 print_ver("image version",
297 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
298 print_ver("subsystem version",
299 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
300 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
301 print_dword("size of image", optionalHeader->SizeOfImage);
302 print_dword("size of headers", optionalHeader->SizeOfHeaders);
303 print_dword("checksum", optionalHeader->CheckSum);
304 print_subsys("Subsystem", optionalHeader->Subsystem);
305 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
306 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
307 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
308 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
309 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
310 print_dword("loader flags", optionalHeader->LoaderFlags);
311 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
312 printf("\n");
313 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
314 printf("\n");
317 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32 *optionalHeader, UINT header_size)
319 printf("Optional Header (%s)\n", get_magic_type(optionalHeader->Magic));
321 switch(optionalHeader->Magic) {
322 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
323 dump_optional_header32(optionalHeader, header_size);
324 break;
325 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
326 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64 *)optionalHeader, header_size);
327 break;
328 default:
329 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader->Magic);
330 break;
334 void dump_file_header(const IMAGE_FILE_HEADER *fileHeader)
336 printf("File Header\n");
338 printf(" Machine: %04X (%s)\n",
339 fileHeader->Machine, get_machine_str(fileHeader->Machine));
340 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
341 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
342 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
343 Offset(&(fileHeader->TimeDateStamp)));
344 printf(" PointerToSymbolTable: %08X\n", fileHeader->PointerToSymbolTable);
345 printf(" NumberOfSymbols: %08X\n", fileHeader->NumberOfSymbols);
346 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
347 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
348 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
349 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
350 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
351 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
352 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
353 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
354 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
355 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
356 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
357 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
358 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
359 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
360 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
361 X(IMAGE_FILE_SYSTEM, "SYSTEM");
362 X(IMAGE_FILE_DLL, "DLL");
363 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
364 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
365 #undef X
366 printf("\n");
369 static void dump_pe_header(void)
371 dump_file_header(&PE_nt_headers->FileHeader);
372 dump_optional_header((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader, PE_nt_headers->FileHeader.SizeOfOptionalHeader);
375 void dump_section(const IMAGE_SECTION_HEADER *sectHead, const char* strtable)
377 unsigned offset;
379 /* long section name ? */
380 if (strtable && sectHead->Name[0] == '/' &&
381 ((offset = atoi((const char*)sectHead->Name + 1)) < *(const DWORD*)strtable))
382 printf(" %.8s (%s)", sectHead->Name, strtable + offset);
383 else
384 printf(" %-8.8s", sectHead->Name);
385 printf(" VirtSize: 0x%08x VirtAddr: 0x%08x\n",
386 sectHead->Misc.VirtualSize, sectHead->VirtualAddress);
387 printf(" raw data offs: 0x%08x raw data size: 0x%08x\n",
388 sectHead->PointerToRawData, sectHead->SizeOfRawData);
389 printf(" relocation offs: 0x%08x relocations: 0x%08x\n",
390 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
391 printf(" line # offs: %-8u line #'s: %-8u\n",
392 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
393 printf(" characteristics: 0x%08x\n", sectHead->Characteristics);
394 printf(" ");
395 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
396 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
397 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
398 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
399 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
400 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
401 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
403 X(IMAGE_SCN_CNT_CODE, "CODE");
404 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
405 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
407 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
408 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
409 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
410 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
411 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
413 /* 0x00002000 - Reserved */
414 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
415 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
417 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
418 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
419 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
420 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
421 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
423 switch (sectHead->Characteristics & IMAGE_SCN_ALIGN_MASK)
425 #define X2(b,s) case b: printf(" " s); break
426 X2(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
427 X2(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
428 X2(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
429 X2(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
430 X2(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
431 X2(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
432 X2(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
433 X2(IMAGE_SCN_ALIGN_128BYTES, "ALIGN_128BYTES");
434 X2(IMAGE_SCN_ALIGN_256BYTES, "ALIGN_256BYTES");
435 X2(IMAGE_SCN_ALIGN_512BYTES, "ALIGN_512BYTES");
436 X2(IMAGE_SCN_ALIGN_1024BYTES, "ALIGN_1024BYTES");
437 X2(IMAGE_SCN_ALIGN_2048BYTES, "ALIGN_2048BYTES");
438 X2(IMAGE_SCN_ALIGN_4096BYTES, "ALIGN_4096BYTES");
439 X2(IMAGE_SCN_ALIGN_8192BYTES, "ALIGN_8192BYTES");
440 #undef X2
443 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
445 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
446 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
447 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
448 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
449 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
450 X(IMAGE_SCN_MEM_READ, "MEM_READ");
451 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
452 #undef X
453 printf("\n\n");
456 static void dump_sections(const void *base, const void* addr, unsigned num_sect)
458 const IMAGE_SECTION_HEADER* sectHead = addr;
459 unsigned i;
460 const char* strtable;
462 if (PE_nt_headers->FileHeader.PointerToSymbolTable && PE_nt_headers->FileHeader.NumberOfSymbols)
464 strtable = (const char*)base +
465 PE_nt_headers->FileHeader.PointerToSymbolTable +
466 PE_nt_headers->FileHeader.NumberOfSymbols * sizeof(IMAGE_SYMBOL);
468 else strtable = NULL;
470 printf("Section Table\n");
471 for (i = 0; i < num_sect; i++, sectHead++)
473 dump_section(sectHead, strtable);
475 if (globals.do_dump_rawdata)
477 dump_data((const unsigned char *)base + sectHead->PointerToRawData, sectHead->SizeOfRawData, " " );
478 printf("\n");
483 static void dump_dir_exported_functions(void)
485 unsigned int size = 0;
486 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
487 unsigned int i;
488 const DWORD* pFunc;
489 const DWORD* pName;
490 const WORD* pOrdl;
491 DWORD* funcs;
493 if (!exportDir) return;
495 printf("Exports table:\n");
496 printf("\n");
497 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
498 printf(" Characteristics: %08x\n", exportDir->Characteristics);
499 printf(" TimeDateStamp: %08X %s\n",
500 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
501 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
502 printf(" Ordinal base: %u\n", exportDir->Base);
503 printf(" # of functions: %u\n", exportDir->NumberOfFunctions);
504 printf(" # of Names: %u\n", exportDir->NumberOfNames);
505 printf("Addresses of functions: %08X\n", exportDir->AddressOfFunctions);
506 printf("Addresses of name ordinals: %08X\n", exportDir->AddressOfNameOrdinals);
507 printf("Addresses of names: %08X\n", exportDir->AddressOfNames);
508 printf("\n");
509 printf(" Entry Pt Ordn Name\n");
511 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
512 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
513 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
514 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
516 funcs = calloc( exportDir->NumberOfFunctions, sizeof(*funcs) );
517 if (!funcs) fatal("no memory");
519 for (i = 0; i < exportDir->NumberOfNames; i++) funcs[pOrdl[i]] = pName[i];
521 for (i = 0; i < exportDir->NumberOfFunctions; i++)
523 if (!pFunc[i]) continue;
524 printf(" %08X %5u ", pFunc[i], exportDir->Base + i);
525 if (funcs[i])
526 printf("%s", get_symbol_str((const char*)RVA(funcs[i], sizeof(DWORD))));
527 else
528 printf("<by ordinal>");
530 /* check for forwarded function */
531 if ((const char *)RVA(pFunc[i],1) >= (const char *)exportDir &&
532 (const char *)RVA(pFunc[i],1) < (const char *)exportDir + size)
533 printf(" (-> %s)", (const char *)RVA(pFunc[i],1));
534 printf("\n");
536 free(funcs);
537 printf("\n");
541 struct runtime_function
543 DWORD BeginAddress;
544 DWORD EndAddress;
545 DWORD UnwindData;
548 union handler_data
550 struct runtime_function chain;
551 DWORD handler;
554 struct opcode
556 BYTE offset;
557 BYTE code : 4;
558 BYTE info : 4;
561 struct unwind_info
563 BYTE version : 3;
564 BYTE flags : 5;
565 BYTE prolog;
566 BYTE count;
567 BYTE frame_reg : 4;
568 BYTE frame_offset : 4;
569 struct opcode opcodes[1]; /* count entries */
570 /* followed by union handler_data */
573 #define UWOP_PUSH_NONVOL 0
574 #define UWOP_ALLOC_LARGE 1
575 #define UWOP_ALLOC_SMALL 2
576 #define UWOP_SET_FPREG 3
577 #define UWOP_SAVE_NONVOL 4
578 #define UWOP_SAVE_NONVOL_FAR 5
579 #define UWOP_SAVE_XMM128 8
580 #define UWOP_SAVE_XMM128_FAR 9
581 #define UWOP_PUSH_MACHFRAME 10
583 #define UNW_FLAG_EHANDLER 1
584 #define UNW_FLAG_UHANDLER 2
585 #define UNW_FLAG_CHAININFO 4
587 static void dump_x86_64_unwind_info( const struct runtime_function *function )
589 static const char * const reg_names[16] =
590 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
591 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
593 const union handler_data *handler_data;
594 const struct unwind_info *info;
595 unsigned int i, count;
597 printf( "\nFunction %08x-%08x:\n", function->BeginAddress, function->EndAddress );
598 if (function->UnwindData & 1)
600 const struct runtime_function *next = RVA( function->UnwindData & ~1, sizeof(*next) );
601 printf( " -> function %08x-%08x\n", next->BeginAddress, next->EndAddress );
602 return;
604 info = RVA( function->UnwindData, sizeof(*info) );
606 printf( " unwind info at %08x\n", function->UnwindData );
607 if (info->version != 1)
609 printf( " *** unknown version %u\n", info->version );
610 return;
612 printf( " flags %x", info->flags );
613 if (info->flags & UNW_FLAG_EHANDLER) printf( " EHANDLER" );
614 if (info->flags & UNW_FLAG_UHANDLER) printf( " UHANDLER" );
615 if (info->flags & UNW_FLAG_CHAININFO) printf( " CHAININFO" );
616 printf( "\n prolog 0x%x bytes\n", info->prolog );
618 if (info->frame_reg)
619 printf( " frame register %s offset 0x%x(%%rsp)\n",
620 reg_names[info->frame_reg], info->frame_offset * 16 );
622 for (i = 0; i < info->count; i++)
624 printf( " 0x%02x: ", info->opcodes[i].offset );
625 switch (info->opcodes[i].code)
627 case UWOP_PUSH_NONVOL:
628 printf( "push %%%s\n", reg_names[info->opcodes[i].info] );
629 break;
630 case UWOP_ALLOC_LARGE:
631 if (info->opcodes[i].info)
633 count = *(const DWORD *)&info->opcodes[i+1];
634 i += 2;
636 else
638 count = *(const USHORT *)&info->opcodes[i+1] * 8;
639 i++;
641 printf( "sub $0x%x,%%rsp\n", count );
642 break;
643 case UWOP_ALLOC_SMALL:
644 count = (info->opcodes[i].info + 1) * 8;
645 printf( "sub $0x%x,%%rsp\n", count );
646 break;
647 case UWOP_SET_FPREG:
648 printf( "lea 0x%x(%%rsp),%s\n",
649 info->frame_offset * 16, reg_names[info->frame_reg] );
650 break;
651 case UWOP_SAVE_NONVOL:
652 count = *(const USHORT *)&info->opcodes[i+1] * 8;
653 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
654 i++;
655 break;
656 case UWOP_SAVE_NONVOL_FAR:
657 count = *(const DWORD *)&info->opcodes[i+1];
658 printf( "mov %%%s,0x%x(%%rsp)\n", reg_names[info->opcodes[i].info], count );
659 i += 2;
660 break;
661 case UWOP_SAVE_XMM128:
662 count = *(const USHORT *)&info->opcodes[i+1] * 16;
663 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
664 i++;
665 break;
666 case UWOP_SAVE_XMM128_FAR:
667 count = *(const DWORD *)&info->opcodes[i+1];
668 printf( "movaps %%xmm%u,0x%x(%%rsp)\n", info->opcodes[i].info, count );
669 i += 2;
670 break;
671 case UWOP_PUSH_MACHFRAME:
672 printf( "PUSH_MACHFRAME %u\n", info->opcodes[i].info );
673 break;
674 default:
675 printf( "*** unknown code %u\n", info->opcodes[i].code );
676 break;
680 handler_data = (const union handler_data *)&info->opcodes[(info->count + 1) & ~1];
681 if (info->flags & UNW_FLAG_CHAININFO)
683 printf( " -> function %08x-%08x\n",
684 handler_data->chain.BeginAddress, handler_data->chain.EndAddress );
685 return;
687 if (info->flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
688 printf( " handler %08x data at %08x\n", handler_data->handler,
689 (ULONG)(function->UnwindData + (const char *)(&handler_data->handler + 1) - (const char *)info ));
692 static void dump_dir_exceptions(void)
694 unsigned int i, size = 0;
695 const struct runtime_function *funcs = get_dir_and_size(IMAGE_FILE_EXCEPTION_DIRECTORY, &size);
696 const IMAGE_FILE_HEADER *file_header = &PE_nt_headers->FileHeader;
698 if (!funcs) return;
700 if (file_header->Machine == IMAGE_FILE_MACHINE_AMD64)
702 size /= sizeof(*funcs);
703 printf( "Exception info (%u functions):\n", size );
704 for (i = 0; i < size; i++) dump_x86_64_unwind_info( funcs + i );
706 else printf( "Exception information not supported for %s binaries\n",
707 get_machine_str(file_header->Machine));
711 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il)
713 /* FIXME: This does not properly handle large images */
714 const IMAGE_IMPORT_BY_NAME* iibn;
715 for (; il->u1.Ordinal; il++)
717 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
718 printf(" %4u <by ordinal>\n", (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
719 else
721 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
722 if (!iibn)
723 printf("Can't grab import by name info, skipping to next ordinal\n");
724 else
725 printf(" %4u %s %x\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
730 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il, int offset)
732 const IMAGE_IMPORT_BY_NAME* iibn;
733 for (; il->u1.Ordinal; il++)
735 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
736 printf(" %4u <by ordinal>\n", IMAGE_ORDINAL32(il->u1.Ordinal));
737 else
739 iibn = RVA((DWORD)il->u1.AddressOfData - offset, sizeof(DWORD));
740 if (!iibn)
741 printf("Can't grab import by name info, skipping to next ordinal\n");
742 else
743 printf(" %4u %s %x\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
748 static void dump_dir_imported_functions(void)
750 const IMAGE_IMPORT_DESCRIPTOR *importDesc = get_dir(IMAGE_FILE_IMPORT_DIRECTORY);
751 DWORD directorySize;
753 if (!importDesc) return;
754 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
756 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
757 directorySize = opt->DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY].Size;
759 else
761 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
762 directorySize = opt->DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY].Size;
765 printf("Import Table size: %08x\n", directorySize);/* FIXME */
767 for (;;)
769 const IMAGE_THUNK_DATA32* il;
771 if (!importDesc->Name || !importDesc->FirstThunk) break;
773 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
774 printf(" Hint/Name Table: %08X\n", (DWORD)importDesc->u.OriginalFirstThunk);
775 printf(" TimeDateStamp: %08X (%s)\n",
776 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
777 printf(" ForwarderChain: %08X\n", importDesc->ForwarderChain);
778 printf(" First thunk RVA: %08X\n", (DWORD)importDesc->FirstThunk);
780 printf(" Ordn Name\n");
782 il = (importDesc->u.OriginalFirstThunk != 0) ?
783 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
784 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
786 if (!il)
787 printf("Can't grab thunk data, going to next imported DLL\n");
788 else
790 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
791 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il);
792 else
793 dump_image_thunk_data32(il, 0);
794 printf("\n");
796 importDesc++;
798 printf("\n");
801 static void dump_dir_delay_imported_functions(void)
803 const struct ImgDelayDescr
805 DWORD grAttrs;
806 DWORD szName;
807 DWORD phmod;
808 DWORD pIAT;
809 DWORD pINT;
810 DWORD pBoundIAT;
811 DWORD pUnloadIAT;
812 DWORD dwTimeStamp;
813 } *importDesc = get_dir(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT);
814 DWORD directorySize;
816 if (!importDesc) return;
817 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
819 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader;
820 directorySize = opt->DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size;
822 else
824 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32 *)&PE_nt_headers->OptionalHeader;
825 directorySize = opt->DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size;
828 printf("Delay Import Table size: %08x\n", directorySize); /* FIXME */
830 for (;;)
832 const IMAGE_THUNK_DATA32* il;
833 int offset = (importDesc->grAttrs & 1) ? 0 : PE_nt_headers->OptionalHeader.ImageBase;
835 if (!importDesc->szName || !importDesc->pIAT || !importDesc->pINT) break;
837 printf(" grAttrs %08x offset %08lx %s\n", importDesc->grAttrs, Offset(importDesc),
838 (const char *)RVA(importDesc->szName - offset, sizeof(DWORD)));
839 printf(" Hint/Name Table: %08x\n", importDesc->pINT);
840 printf(" TimeDateStamp: %08X (%s)\n",
841 importDesc->dwTimeStamp, get_time_str(importDesc->dwTimeStamp));
843 printf(" Ordn Name\n");
845 il = RVA(importDesc->pINT - offset, sizeof(DWORD));
847 if (!il)
848 printf("Can't grab thunk data, going to next imported DLL\n");
849 else
851 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
852 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il);
853 else
854 dump_image_thunk_data32(il, offset);
855 printf("\n");
857 importDesc++;
859 printf("\n");
862 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
864 const char* str;
866 printf("Directory %02u\n", idx + 1);
867 printf(" Characteristics: %08X\n", idd->Characteristics);
868 printf(" TimeDateStamp: %08X %s\n",
869 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
870 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
871 switch (idd->Type)
873 default:
874 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
875 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
876 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
877 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
878 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
879 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
880 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
881 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
882 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
883 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
884 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
886 printf(" Type: %u (%s)\n", idd->Type, str);
887 printf(" SizeOfData: %u\n", idd->SizeOfData);
888 printf(" AddressOfRawData: %08X\n", idd->AddressOfRawData);
889 printf(" PointerToRawData: %08X\n", idd->PointerToRawData);
891 switch (idd->Type)
893 case IMAGE_DEBUG_TYPE_UNKNOWN:
894 break;
895 case IMAGE_DEBUG_TYPE_COFF:
896 dump_coff(idd->PointerToRawData, idd->SizeOfData,
897 (const char*)PE_nt_headers + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + PE_nt_headers->FileHeader.SizeOfOptionalHeader);
898 break;
899 case IMAGE_DEBUG_TYPE_CODEVIEW:
900 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
901 break;
902 case IMAGE_DEBUG_TYPE_FPO:
903 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
904 break;
905 case IMAGE_DEBUG_TYPE_MISC:
907 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
908 if (!misc) {printf("Can't get misc debug information\n"); break;}
909 printf(" DataType: %u (%s)\n",
910 misc->DataType,
911 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
912 printf(" Length: %u\n", misc->Length);
913 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
914 printf(" Data: %s\n", misc->Data);
916 break;
917 case IMAGE_DEBUG_TYPE_EXCEPTION:
918 break;
919 case IMAGE_DEBUG_TYPE_FIXUP:
920 break;
921 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC:
922 break;
923 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:
924 break;
925 case IMAGE_DEBUG_TYPE_BORLAND:
926 break;
927 case IMAGE_DEBUG_TYPE_RESERVED10:
928 break;
930 printf("\n");
933 static void dump_dir_debug(void)
935 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir(IMAGE_FILE_DEBUG_DIRECTORY);
936 unsigned nb_dbg, i;
938 if (!debugDir) return;
939 nb_dbg = PE_nt_headers->OptionalHeader.DataDirectory[IMAGE_FILE_DEBUG_DIRECTORY].Size /
940 sizeof(*debugDir);
941 if (!nb_dbg) return;
943 printf("Debug Table (%u directories)\n", nb_dbg);
945 for (i = 0; i < nb_dbg; i++)
947 dump_dir_debug_dir(debugDir, i);
948 debugDir++;
950 printf("\n");
953 static inline void print_clrflags(const char *title, WORD value)
955 printf(" %-34s 0x%X\n", title, value);
956 #define X(f,s) if (value & f) printf(" %s\n", s)
957 X(COMIMAGE_FLAGS_ILONLY, "ILONLY");
958 X(COMIMAGE_FLAGS_32BITREQUIRED, "32BITREQUIRED");
959 X(COMIMAGE_FLAGS_IL_LIBRARY, "IL_LIBRARY");
960 X(COMIMAGE_FLAGS_STRONGNAMESIGNED, "STRONGNAMESIGNED");
961 X(COMIMAGE_FLAGS_TRACKDEBUGDATA, "TRACKDEBUGDATA");
962 #undef X
965 static inline void print_clrdirectory(const char *title, const IMAGE_DATA_DIRECTORY *dir)
967 printf(" %-23s rva: 0x%-8x size: 0x%-8x\n", title, dir->VirtualAddress, dir->Size);
970 static void dump_dir_clr_header(void)
972 unsigned int size = 0;
973 const IMAGE_COR20_HEADER *dir = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR, &size);
975 if (!dir) return;
977 printf( "CLR Header\n" );
978 print_dword( "Header Size", dir->cb );
979 print_ver( "Required runtime version", dir->MajorRuntimeVersion, dir->MinorRuntimeVersion );
980 print_clrflags( "Flags", dir->Flags );
981 print_dword( "EntryPointToken", dir->EntryPointToken );
982 printf("\n");
983 printf( "CLR Data Directory\n" );
984 print_clrdirectory( "MetaData", &dir->MetaData );
985 print_clrdirectory( "Resources", &dir->Resources );
986 print_clrdirectory( "StrongNameSignature", &dir->StrongNameSignature );
987 print_clrdirectory( "CodeManagerTable", &dir->CodeManagerTable );
988 print_clrdirectory( "VTableFixups", &dir->VTableFixups );
989 print_clrdirectory( "ExportAddressTableJumps", &dir->ExportAddressTableJumps );
990 print_clrdirectory( "ManagedNativeHeader", &dir->ManagedNativeHeader );
991 printf("\n");
994 static void dump_dir_reloc(void)
996 unsigned int i, size = 0;
997 const USHORT *relocs;
998 const IMAGE_BASE_RELOCATION *rel = get_dir_and_size(IMAGE_DIRECTORY_ENTRY_BASERELOC, &size);
999 const IMAGE_BASE_RELOCATION *end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + size);
1000 static const char * const names[] =
1002 "BASED_ABSOLUTE",
1003 "BASED_HIGH",
1004 "BASED_LOW",
1005 "BASED_HIGHLOW",
1006 "BASED_HIGHADJ",
1007 "BASED_MIPS_JMPADDR",
1008 "BASED_SECTION",
1009 "BASED_REL",
1010 "unknown 8",
1011 "BASED_IA64_IMM64",
1012 "BASED_DIR64",
1013 "BASED_HIGH3ADJ",
1014 "unknown 12",
1015 "unknown 13",
1016 "unknown 14",
1017 "unknown 15"
1020 if (!rel) return;
1022 printf( "Relocations\n" );
1023 while (rel < end - 1 && rel->SizeOfBlock)
1025 printf( " Page %x\n", rel->VirtualAddress );
1026 relocs = (const USHORT *)(rel + 1);
1027 i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(USHORT);
1028 while (i--)
1030 USHORT offset = *relocs & 0xfff;
1031 int type = *relocs >> 12;
1032 printf( " off %04x type %s\n", offset, names[type] );
1033 relocs++;
1035 rel = (const IMAGE_BASE_RELOCATION *)relocs;
1037 printf("\n");
1040 static void dump_dir_tls(void)
1042 IMAGE_TLS_DIRECTORY64 dir;
1043 const DWORD *callbacks;
1044 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
1046 if (!pdir) return;
1048 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
1049 memcpy(&dir, pdir, sizeof(dir));
1050 else
1052 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
1053 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
1054 dir.AddressOfIndex = pdir->AddressOfIndex;
1055 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
1056 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
1057 dir.Characteristics = pdir->Characteristics;
1060 /* FIXME: This does not properly handle large images */
1061 printf( "Thread Local Storage\n" );
1062 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
1063 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
1064 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
1065 (DWORD)dir.SizeOfZeroFill );
1066 printf( " Index address %08x\n", (DWORD)dir.AddressOfIndex );
1067 printf( " Characteristics %08x\n", dir.Characteristics );
1068 printf( " Callbacks %08x -> {", (DWORD)dir.AddressOfCallBacks );
1069 if (dir.AddressOfCallBacks)
1071 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
1072 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
1074 printf( " %08x", *callbacks );
1075 addr += sizeof(DWORD);
1078 printf(" }\n\n");
1081 enum FileSig get_kind_dbg(void)
1083 const WORD* pw;
1085 pw = PRD(0, sizeof(WORD));
1086 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1088 if (*pw == 0x4944 /* "DI" */) return SIG_DBG;
1089 return SIG_UNKNOWN;
1092 void dbg_dump(void)
1094 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
1095 unsigned nb_dbg;
1096 unsigned i;
1097 const IMAGE_DEBUG_DIRECTORY* debugDir;
1099 separateDebugHead = PRD(0, sizeof(*separateDebugHead));
1100 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
1102 printf ("Signature: %.2s (0x%4X)\n",
1103 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
1104 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
1105 printf ("Machine: 0x%04X (%s)\n",
1106 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
1107 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
1108 printf ("TimeDateStamp: 0x%08X (%s)\n",
1109 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
1110 printf ("CheckSum: 0x%08X\n", separateDebugHead->CheckSum);
1111 printf ("ImageBase: 0x%08X\n", separateDebugHead->ImageBase);
1112 printf ("SizeOfImage: 0x%08X\n", separateDebugHead->SizeOfImage);
1113 printf ("NumberOfSections: 0x%08X\n", separateDebugHead->NumberOfSections);
1114 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead->ExportedNamesSize);
1115 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead->DebugDirectorySize);
1117 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
1118 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
1119 {printf("Can't get the sections, aborting\n"); return;}
1121 dump_sections(separateDebugHead, separateDebugHead + 1, separateDebugHead->NumberOfSections);
1123 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
1124 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
1125 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
1126 separateDebugHead->ExportedNamesSize,
1127 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
1128 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
1130 printf("Debug Table (%u directories)\n", nb_dbg);
1132 for (i = 0; i < nb_dbg; i++)
1134 dump_dir_debug_dir(debugDir, i);
1135 debugDir++;
1139 static const char *get_resource_type( unsigned int id )
1141 static const char * const types[] =
1143 NULL,
1144 "CURSOR",
1145 "BITMAP",
1146 "ICON",
1147 "MENU",
1148 "DIALOG",
1149 "STRING",
1150 "FONTDIR",
1151 "FONT",
1152 "ACCELERATOR",
1153 "RCDATA",
1154 "MESSAGETABLE",
1155 "GROUP_CURSOR",
1156 NULL,
1157 "GROUP_ICON",
1158 NULL,
1159 "VERSION",
1160 "DLGINCLUDE",
1161 NULL,
1162 "PLUGPLAY",
1163 "VXD",
1164 "ANICURSOR",
1165 "ANIICON",
1166 "HTML",
1167 "RT_MANIFEST"
1170 if ((size_t)id < sizeof(types)/sizeof(types[0])) return types[id];
1171 return NULL;
1174 /* dump an ASCII string with proper escaping */
1175 static int dump_strA( const unsigned char *str, size_t len )
1177 static const char escapes[32] = ".......abtnvfr.............e....";
1178 char buffer[256];
1179 char *pos = buffer;
1180 int count = 0;
1182 for (; len; str++, len--)
1184 if (pos > buffer + sizeof(buffer) - 8)
1186 fwrite( buffer, pos - buffer, 1, stdout );
1187 count += pos - buffer;
1188 pos = buffer;
1190 if (*str > 127) /* hex escape */
1192 pos += sprintf( pos, "\\x%02x", *str );
1193 continue;
1195 if (*str < 32) /* octal or C escape */
1197 if (!*str && len == 1) continue; /* do not output terminating NULL */
1198 if (escapes[*str] != '.')
1199 pos += sprintf( pos, "\\%c", escapes[*str] );
1200 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1201 pos += sprintf( pos, "\\%03o", *str );
1202 else
1203 pos += sprintf( pos, "\\%o", *str );
1204 continue;
1206 if (*str == '\\') *pos++ = '\\';
1207 *pos++ = *str;
1209 fwrite( buffer, pos - buffer, 1, stdout );
1210 count += pos - buffer;
1211 return count;
1214 /* dump a Unicode string with proper escaping */
1215 static int dump_strW( const WCHAR *str, size_t len )
1217 static const char escapes[32] = ".......abtnvfr.............e....";
1218 char buffer[256];
1219 char *pos = buffer;
1220 int count = 0;
1222 for (; len; str++, len--)
1224 if (pos > buffer + sizeof(buffer) - 8)
1226 fwrite( buffer, pos - buffer, 1, stdout );
1227 count += pos - buffer;
1228 pos = buffer;
1230 if (*str > 127) /* hex escape */
1232 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
1233 pos += sprintf( pos, "\\x%04x", *str );
1234 else
1235 pos += sprintf( pos, "\\x%x", *str );
1236 continue;
1238 if (*str < 32) /* octal or C escape */
1240 if (!*str && len == 1) continue; /* do not output terminating NULL */
1241 if (escapes[*str] != '.')
1242 pos += sprintf( pos, "\\%c", escapes[*str] );
1243 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
1244 pos += sprintf( pos, "\\%03o", *str );
1245 else
1246 pos += sprintf( pos, "\\%o", *str );
1247 continue;
1249 if (*str == '\\') *pos++ = '\\';
1250 *pos++ = *str;
1252 fwrite( buffer, pos - buffer, 1, stdout );
1253 count += pos - buffer;
1254 return count;
1257 /* dump data for a STRING resource */
1258 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
1260 int i;
1262 for (i = 0; i < 16 && size; i++)
1264 unsigned len = *ptr++;
1266 if (len >= size)
1268 len = size;
1269 size = 0;
1271 else size -= len + 1;
1273 if (len)
1275 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
1276 dump_strW( ptr, len );
1277 printf( "\"\n" );
1278 ptr += len;
1283 /* dump data for a MESSAGETABLE resource */
1284 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
1286 const MESSAGE_RESOURCE_DATA *data = ptr;
1287 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
1288 unsigned i, j;
1290 for (i = 0; i < data->NumberOfBlocks; i++, block++)
1292 const MESSAGE_RESOURCE_ENTRY *entry;
1294 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
1295 for (j = block->LowId; j <= block->HighId; j++)
1297 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
1299 const WCHAR *str = (const WCHAR *)entry->Text;
1300 printf( "%s%08x L\"", prefix, j );
1301 dump_strW( str, strlenW(str) );
1302 printf( "\"\n" );
1304 else
1306 const char *str = (const char *) entry->Text;
1307 printf( "%s%08x \"", prefix, j );
1308 dump_strA( entry->Text, strlen(str) );
1309 printf( "\"\n" );
1311 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
1316 static void dump_dir_resource(void)
1318 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
1319 const IMAGE_RESOURCE_DIRECTORY *namedir;
1320 const IMAGE_RESOURCE_DIRECTORY *langdir;
1321 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1322 const IMAGE_RESOURCE_DIR_STRING_U *string;
1323 const IMAGE_RESOURCE_DATA_ENTRY *data;
1324 int i, j, k;
1326 if (!root) return;
1328 printf( "Resources:" );
1330 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1332 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1333 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s3.OffsetToDirectory);
1334 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1336 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1337 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s3.OffsetToDirectory);
1338 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1340 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1342 printf( "\n " );
1343 if (e1->u1.s1.NameIsString)
1345 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u1.s1.NameOffset);
1346 dump_unicode_str( string->NameString, string->Length );
1348 else
1350 const char *type = get_resource_type( e1->u1.s2.Id );
1351 if (type) printf( "%s", type );
1352 else printf( "%04x", e1->u1.s2.Id );
1355 printf( " Name=" );
1356 if (e2->u1.s1.NameIsString)
1358 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u1.s1.NameOffset);
1359 dump_unicode_str( string->NameString, string->Length );
1361 else
1362 printf( "%04x", e2->u1.s2.Id );
1364 printf( " Language=%04x:\n", e3->u1.s2.Id );
1365 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1366 if (e1->u1.s1.NameIsString)
1368 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1370 else switch(e1->u1.s2.Id)
1372 case 6:
1373 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size,
1374 e2->u1.s2.Id, " " );
1375 break;
1376 case 11:
1377 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
1378 e2->u1.s2.Id, " " );
1379 break;
1380 default:
1381 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1382 break;
1387 printf( "\n\n" );
1390 static void dump_debug(void)
1392 const char* stabs = NULL;
1393 unsigned szstabs = 0;
1394 const char* stabstr = NULL;
1395 unsigned szstr = 0;
1396 unsigned i;
1397 const IMAGE_SECTION_HEADER* sectHead;
1399 sectHead = (const IMAGE_SECTION_HEADER*)
1400 ((const char*)PE_nt_headers + sizeof(DWORD) +
1401 sizeof(IMAGE_FILE_HEADER) + PE_nt_headers->FileHeader.SizeOfOptionalHeader);
1403 for (i = 0; i < PE_nt_headers->FileHeader.NumberOfSections; i++, sectHead++)
1405 if (!strcmp((const char *)sectHead->Name, ".stab"))
1407 stabs = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1408 szstabs = sectHead->Misc.VirtualSize;
1410 if (!strncmp((const char *)sectHead->Name, ".stabstr", 8))
1412 stabstr = RVA(sectHead->VirtualAddress, sectHead->Misc.VirtualSize);
1413 szstr = sectHead->Misc.VirtualSize;
1416 if (stabs && stabstr)
1417 dump_stabs(stabs, szstabs, stabstr, szstr);
1420 static void dump_symbol_table(void)
1422 const IMAGE_SYMBOL* sym;
1423 int numsym;
1425 numsym = PE_nt_headers->FileHeader.NumberOfSymbols;
1426 if (!PE_nt_headers->FileHeader.PointerToSymbolTable || !numsym)
1427 return;
1428 sym = PRD(PE_nt_headers->FileHeader.PointerToSymbolTable,
1429 sizeof(*sym) * numsym);
1430 if (!sym) return;
1432 dump_coff_symbol_table(sym, numsym, IMAGE_FIRST_SECTION(PE_nt_headers));
1435 enum FileSig get_kind_exec(void)
1437 const WORD* pw;
1438 const DWORD* pdw;
1439 const IMAGE_DOS_HEADER* dh;
1441 pw = PRD(0, sizeof(WORD));
1442 if (!pw) {printf("Can't get main signature, aborting\n"); return 0;}
1444 if (*pw != IMAGE_DOS_SIGNATURE) return SIG_UNKNOWN;
1446 if ((dh = PRD(0, sizeof(IMAGE_DOS_HEADER))))
1448 /* the signature is the first DWORD */
1449 pdw = PRD(dh->e_lfanew, sizeof(DWORD));
1450 if (pdw)
1452 if (*pdw == IMAGE_NT_SIGNATURE) return SIG_PE;
1453 if (*(const WORD *)pdw == IMAGE_OS2_SIGNATURE) return SIG_NE;
1454 if (*(const WORD *)pdw == IMAGE_VXD_SIGNATURE) return SIG_LE;
1455 return SIG_DOS;
1458 return 0;
1461 void pe_dump(void)
1463 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
1465 PE_nt_headers = get_nt_header();
1466 if (is_fake_dll()) printf( "*** This is a Wine fake DLL ***\n\n" );
1468 if (globals.do_dumpheader)
1470 dump_pe_header();
1471 /* FIXME: should check ptr */
1472 dump_sections(PRD(0, 1), (const char*)PE_nt_headers + sizeof(DWORD) +
1473 sizeof(IMAGE_FILE_HEADER) + PE_nt_headers->FileHeader.SizeOfOptionalHeader,
1474 PE_nt_headers->FileHeader.NumberOfSections);
1476 else if (!globals.dumpsect)
1478 /* show at least something here */
1479 dump_pe_header();
1482 if (globals.dumpsect)
1484 if (all || !strcmp(globals.dumpsect, "import"))
1486 dump_dir_imported_functions();
1487 dump_dir_delay_imported_functions();
1489 if (all || !strcmp(globals.dumpsect, "export"))
1490 dump_dir_exported_functions();
1491 if (all || !strcmp(globals.dumpsect, "debug"))
1492 dump_dir_debug();
1493 if (all || !strcmp(globals.dumpsect, "resource"))
1494 dump_dir_resource();
1495 if (all || !strcmp(globals.dumpsect, "tls"))
1496 dump_dir_tls();
1497 if (all || !strcmp(globals.dumpsect, "clr"))
1498 dump_dir_clr_header();
1499 if (all || !strcmp(globals.dumpsect, "reloc"))
1500 dump_dir_reloc();
1501 if (all || !strcmp(globals.dumpsect, "except"))
1502 dump_dir_exceptions();
1504 if (globals.do_symbol_table)
1505 dump_symbol_table();
1506 if (globals.do_debug)
1507 dump_debug();
1510 typedef struct _dll_symbol {
1511 size_t ordinal;
1512 char *symbol;
1513 } dll_symbol;
1515 static dll_symbol *dll_symbols = NULL;
1516 static dll_symbol *dll_current_symbol = NULL;
1518 /* Compare symbols by ordinal for qsort */
1519 static int symbol_cmp(const void *left, const void *right)
1521 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
1524 /*******************************************************************
1525 * dll_close
1527 * Free resources used by DLL
1529 /* FIXME: Not used yet
1530 static void dll_close (void)
1532 dll_symbol* ds;
1534 if (!dll_symbols) {
1535 fatal("No symbols");
1537 for (ds = dll_symbols; ds->symbol; ds++)
1538 free(ds->symbol);
1539 free (dll_symbols);
1540 dll_symbols = NULL;
1544 static void do_grab_sym( void )
1546 const IMAGE_EXPORT_DIRECTORY*exportDir;
1547 unsigned i, j;
1548 const DWORD* pName;
1549 const DWORD* pFunc;
1550 const WORD* pOrdl;
1551 const char* ptr;
1552 DWORD* map;
1554 PE_nt_headers = get_nt_header();
1555 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
1557 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
1558 if (!pName) {printf("Can't grab functions' name table\n"); return;}
1559 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
1560 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
1561 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
1562 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
1564 /* dll_close(); */
1566 if (!(dll_symbols = malloc((exportDir->NumberOfFunctions + 1) * sizeof(dll_symbol))))
1567 fatal ("Out of memory");
1569 /* bit map of used funcs */
1570 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
1571 if (!map) fatal("no memory");
1573 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
1575 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
1576 ptr = RVA(*pName++, sizeof(DWORD));
1577 if (!ptr) ptr = "cant_get_function";
1578 dll_symbols[j].symbol = strdup(ptr);
1579 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
1580 assert(dll_symbols[j].symbol);
1583 for (i = 0; i < exportDir->NumberOfFunctions; i++)
1585 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
1587 char ordinal_text[256];
1588 /* Ordinal only entry */
1589 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%u",
1590 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
1591 exportDir->Base + i);
1592 str_toupper(ordinal_text);
1593 dll_symbols[j].symbol = strdup(ordinal_text);
1594 assert(dll_symbols[j].symbol);
1595 dll_symbols[j].ordinal = exportDir->Base + i;
1596 j++;
1597 assert(j <= exportDir->NumberOfFunctions);
1600 free(map);
1602 if (NORMAL)
1603 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
1604 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
1606 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
1608 dll_symbols[j].symbol = NULL;
1610 dll_current_symbol = dll_symbols;
1613 /*******************************************************************
1614 * dll_open
1616 * Open a DLL and read in exported symbols
1618 int dll_open (const char *dll_name)
1620 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
1623 /*******************************************************************
1624 * dll_next_symbol
1626 * Get next exported symbol from dll
1628 int dll_next_symbol (parsed_symbol * sym)
1630 if (!dll_current_symbol || !dll_current_symbol->symbol)
1631 return 1;
1632 assert (dll_symbols);
1633 sym->symbol = strdup (dll_current_symbol->symbol);
1634 sym->ordinal = dll_current_symbol->ordinal;
1635 dll_current_symbol++;
1636 return 0;