msi/tests: Fixed a typo.
[wine.git] / tools / winedump / pe.c
blob6ef0607545e78356a353c9564f625c5dc39aa474
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"
47 #include "pe.h"
49 static const IMAGE_NT_HEADERS32* PE_nt_headers;
51 static const char* get_machine_str(DWORD mach)
53 switch (mach)
55 case IMAGE_FILE_MACHINE_UNKNOWN: return "Unknown";
56 case IMAGE_FILE_MACHINE_I860: return "i860";
57 case IMAGE_FILE_MACHINE_I386: return "i386";
58 case IMAGE_FILE_MACHINE_R3000: return "R3000";
59 case IMAGE_FILE_MACHINE_R4000: return "R4000";
60 case IMAGE_FILE_MACHINE_R10000: return "R10000";
61 case IMAGE_FILE_MACHINE_ALPHA: return "Alpha";
62 case IMAGE_FILE_MACHINE_POWERPC: return "PowerPC";
63 case IMAGE_FILE_MACHINE_AMD64: return "AMD64";
64 case IMAGE_FILE_MACHINE_IA64: return "IA64";
66 return "???";
69 static const void* RVA(unsigned long rva, unsigned long len)
71 IMAGE_SECTION_HEADER* sectHead;
72 int i;
74 if (rva == 0) return NULL;
76 sectHead = IMAGE_FIRST_SECTION(PE_nt_headers);
77 for (i = PE_nt_headers->FileHeader.NumberOfSections - 1; i >= 0; i--)
79 if (sectHead[i].VirtualAddress <= rva &&
80 rva + len <= (DWORD)sectHead[i].VirtualAddress + sectHead[i].SizeOfRawData)
82 /* return image import directory offset */
83 return PRD(sectHead[i].PointerToRawData + rva - sectHead[i].VirtualAddress, len);
87 return NULL;
90 static const IMAGE_NT_HEADERS32 *get_nt_header( const void *pmt )
92 const IMAGE_DOS_HEADER *dos = pmt;
93 return (const IMAGE_NT_HEADERS32 *)((const BYTE *)dos + dos->e_lfanew);
96 static int is_fake_dll( const void *base )
98 static const char fakedll_signature[] = "Wine placeholder DLL";
99 const IMAGE_DOS_HEADER *dos = base;
101 if (dos->e_lfanew >= sizeof(*dos) + sizeof(fakedll_signature) &&
102 !memcmp( dos + 1, fakedll_signature, sizeof(fakedll_signature) )) return TRUE;
103 return FALSE;
106 static const void *get_dir_and_size(unsigned int idx, unsigned int *size)
108 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
110 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
111 if (idx >= opt->NumberOfRvaAndSizes)
112 return NULL;
113 if(size)
114 *size = opt->DataDirectory[idx].Size;
115 return RVA(opt->DataDirectory[idx].VirtualAddress,
116 opt->DataDirectory[idx].Size);
118 else
120 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
121 if (idx >= opt->NumberOfRvaAndSizes)
122 return NULL;
123 if(size)
124 *size = opt->DataDirectory[idx].Size;
125 return RVA(opt->DataDirectory[idx].VirtualAddress,
126 opt->DataDirectory[idx].Size);
130 static const void* get_dir(unsigned idx)
132 return get_dir_and_size(idx, 0);
135 static const char * const DirectoryNames[16] = {
136 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
137 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
138 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
139 "IAT", "Delay IAT", "COM Descript", ""
142 static char *get_magic_type(WORD magic)
144 switch(magic) {
145 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
146 return "32bit";
147 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
148 return "64bit";
149 case IMAGE_ROM_OPTIONAL_HDR_MAGIC:
150 return "ROM";
152 return "???";
155 static inline void print_word(const char *title, WORD value)
157 printf(" %-34s 0x%-4X %u\n", title, value, value);
160 static inline void print_dword(const char *title, DWORD value)
162 printf(" %-34s 0x%-8lx %lu\n", title, value, value);
165 static inline void print_longlong(const char *title, ULONGLONG value)
167 printf(" %-34s 0x", title);
168 if(value >> 32)
169 printf("%lx%08lx\n", (unsigned long)(value >> 32), (unsigned long)value);
170 else
171 printf("%lx\n", (unsigned long)value);
174 static inline void print_ver(const char *title, BYTE major, BYTE minor)
176 printf(" %-34s %u.%02u\n", title, major, minor);
179 static inline void print_subsys(const char *title, WORD value)
181 const char *str;
182 switch (value)
184 default:
185 case IMAGE_SUBSYSTEM_UNKNOWN: str = "Unknown"; break;
186 case IMAGE_SUBSYSTEM_NATIVE: str = "Native"; break;
187 case IMAGE_SUBSYSTEM_WINDOWS_GUI: str = "Windows GUI"; break;
188 case IMAGE_SUBSYSTEM_WINDOWS_CUI: str = "Windows CUI"; break;
189 case IMAGE_SUBSYSTEM_OS2_CUI: str = "OS/2 CUI"; break;
190 case IMAGE_SUBSYSTEM_POSIX_CUI: str = "Posix CUI"; break;
192 printf(" %-34s 0x%X (%s)\n", title, value, str);
195 static inline void print_dllflags(const char *title, WORD value)
197 printf(" %-34s 0x%X\n", title, value);
198 #define X(f,s) if (value & f) printf(" %s\n", s)
199 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION, "NO_ISOLATION");
200 X(IMAGE_DLLCHARACTERISTICS_NO_SEH, "NO_SEH");
201 X(IMAGE_DLLCHARACTERISTICS_NO_BIND, "NO_BIND");
202 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER, "WDM_DRIVER");
203 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE, "TERMINAL_SERVER_AWARE");
204 #undef X
207 static inline void print_datadirectory(DWORD n, const IMAGE_DATA_DIRECTORY *directory)
209 unsigned i;
210 printf("Data Directory\n");
211 printf("%ld\n", n * sizeof(IMAGE_DATA_DIRECTORY));
213 for (i = 0; i < n && i < 16; i++)
215 printf(" %-12s rva: 0x%-8lX size: %8lu\n",
216 DirectoryNames[i], directory[i].VirtualAddress,
217 directory[i].Size);
221 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32 *optionalHeader)
223 print_word("Magic", optionalHeader->Magic);
224 print_ver("linker version",
225 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
226 print_dword("size of code", optionalHeader->SizeOfCode);
227 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
228 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
229 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
230 print_dword("base of code", optionalHeader->BaseOfCode);
231 print_dword("base of data", optionalHeader->BaseOfData);
232 print_dword("image base", optionalHeader->ImageBase);
233 print_dword("section align", optionalHeader->SectionAlignment);
234 print_dword("file align", optionalHeader->FileAlignment);
235 print_ver("required OS version",
236 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
237 print_ver("image version",
238 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
239 print_ver("subsystem version",
240 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
241 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
242 print_dword("size of image", optionalHeader->SizeOfImage);
243 print_dword("size of headers", optionalHeader->SizeOfHeaders);
244 print_dword("checksum", optionalHeader->CheckSum);
245 print_subsys("Subsystem", optionalHeader->Subsystem);
246 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
247 print_dword("stack reserve size", optionalHeader->SizeOfStackReserve);
248 print_dword("stack commit size", optionalHeader->SizeOfStackCommit);
249 print_dword("heap reserve size", optionalHeader->SizeOfHeapReserve);
250 print_dword("heap commit size", optionalHeader->SizeOfHeapCommit);
251 print_dword("loader flags", optionalHeader->LoaderFlags);
252 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
253 printf("\n");
254 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
257 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64 *optionalHeader)
259 print_word("Magic", optionalHeader->Magic);
260 print_ver("linker version",
261 optionalHeader->MajorLinkerVersion, optionalHeader->MinorLinkerVersion);
262 print_dword("size of code", optionalHeader->SizeOfCode);
263 print_dword("size of initialized data", optionalHeader->SizeOfInitializedData);
264 print_dword("size of uninitialized data", optionalHeader->SizeOfUninitializedData);
265 print_dword("entrypoint RVA", optionalHeader->AddressOfEntryPoint);
266 print_dword("base of code", optionalHeader->BaseOfCode);
267 print_longlong("image base", optionalHeader->ImageBase);
268 print_dword("section align", optionalHeader->SectionAlignment);
269 print_dword("file align", optionalHeader->FileAlignment);
270 print_ver("required OS version",
271 optionalHeader->MajorOperatingSystemVersion, optionalHeader->MinorOperatingSystemVersion);
272 print_ver("image version",
273 optionalHeader->MajorImageVersion, optionalHeader->MinorImageVersion);
274 print_ver("subsystem version",
275 optionalHeader->MajorSubsystemVersion, optionalHeader->MinorSubsystemVersion);
276 print_dword("Win32 Version", optionalHeader->Win32VersionValue);
277 print_dword("size of image", optionalHeader->SizeOfImage);
278 print_dword("size of headers", optionalHeader->SizeOfHeaders);
279 print_dword("checksum", optionalHeader->CheckSum);
280 print_subsys("Subsystem", optionalHeader->Subsystem);
281 print_dllflags("DLL characteristics:", optionalHeader->DllCharacteristics);
282 print_longlong("stack reserve size", optionalHeader->SizeOfStackReserve);
283 print_longlong("stack commit size", optionalHeader->SizeOfStackCommit);
284 print_longlong("heap reserve size", optionalHeader->SizeOfHeapReserve);
285 print_longlong("heap commit size", optionalHeader->SizeOfHeapCommit);
286 print_dword("loader flags", optionalHeader->LoaderFlags);
287 print_dword("RVAs & sizes", optionalHeader->NumberOfRvaAndSizes);
288 printf("\n");
289 print_datadirectory(optionalHeader->NumberOfRvaAndSizes, optionalHeader->DataDirectory);
292 static void dump_pe_header(void)
294 const IMAGE_FILE_HEADER *fileHeader;
296 printf("File Header\n");
297 fileHeader = &PE_nt_headers->FileHeader;
299 printf(" Machine: %04X (%s)\n",
300 fileHeader->Machine, get_machine_str(fileHeader->Machine));
301 printf(" Number of Sections: %d\n", fileHeader->NumberOfSections);
302 printf(" TimeDateStamp: %08lX (%s) offset %lu\n",
303 fileHeader->TimeDateStamp, get_time_str(fileHeader->TimeDateStamp),
304 Offset(&(fileHeader->TimeDateStamp)));
305 printf(" PointerToSymbolTable: %08lX\n", fileHeader->PointerToSymbolTable);
306 printf(" NumberOfSymbols: %08lX\n", fileHeader->NumberOfSymbols);
307 printf(" SizeOfOptionalHeader: %04X\n", fileHeader->SizeOfOptionalHeader);
308 printf(" Characteristics: %04X\n", fileHeader->Characteristics);
309 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
310 X(IMAGE_FILE_RELOCS_STRIPPED, "RELOCS_STRIPPED");
311 X(IMAGE_FILE_EXECUTABLE_IMAGE, "EXECUTABLE_IMAGE");
312 X(IMAGE_FILE_LINE_NUMS_STRIPPED, "LINE_NUMS_STRIPPED");
313 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED, "LOCAL_SYMS_STRIPPED");
314 X(IMAGE_FILE_AGGRESIVE_WS_TRIM, "AGGRESIVE_WS_TRIM");
315 X(IMAGE_FILE_LARGE_ADDRESS_AWARE, "LARGE_ADDRESS_AWARE");
316 X(IMAGE_FILE_16BIT_MACHINE, "16BIT_MACHINE");
317 X(IMAGE_FILE_BYTES_REVERSED_LO, "BYTES_REVERSED_LO");
318 X(IMAGE_FILE_32BIT_MACHINE, "32BIT_MACHINE");
319 X(IMAGE_FILE_DEBUG_STRIPPED, "DEBUG_STRIPPED");
320 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "REMOVABLE_RUN_FROM_SWAP");
321 X(IMAGE_FILE_NET_RUN_FROM_SWAP, "NET_RUN_FROM_SWAP");
322 X(IMAGE_FILE_SYSTEM, "SYSTEM");
323 X(IMAGE_FILE_DLL, "DLL");
324 X(IMAGE_FILE_UP_SYSTEM_ONLY, "UP_SYSTEM_ONLY");
325 X(IMAGE_FILE_BYTES_REVERSED_HI, "BYTES_REVERSED_HI");
326 #undef X
327 printf("\n");
329 /* hope we have the right size */
330 printf("Optional Header (%s)\n", get_magic_type(PE_nt_headers->OptionalHeader.Magic));
331 switch(PE_nt_headers->OptionalHeader.Magic) {
332 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
333 dump_optional_header32((const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader);
334 break;
335 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
336 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader);
337 break;
338 default:
339 printf(" Unknown header magic: 0x%-4X\n", PE_nt_headers->OptionalHeader.Magic);
340 break;
342 printf("\n");
345 static void dump_sections(const void* addr, unsigned num_sect)
347 const IMAGE_SECTION_HEADER* sectHead = addr;
348 unsigned i;
350 printf("Section Table\n");
351 for (i = 0; i < num_sect; i++, sectHead++)
353 printf(" %02d %-8.8s VirtSize: %-8lu VirtAddr: %-8lu 0x%08lx\n",
354 i + 1, sectHead->Name, sectHead->Misc.VirtualSize, sectHead->VirtualAddress,
355 sectHead->VirtualAddress);
356 printf(" raw data offs: %-8lu raw data size: %-8lu\n",
357 sectHead->PointerToRawData, sectHead->SizeOfRawData);
358 printf(" relocation offs: %-8lu relocations: %-8u\n",
359 sectHead->PointerToRelocations, sectHead->NumberOfRelocations);
360 printf(" line # offs: %-8lu line #'s: %-8u\n",
361 sectHead->PointerToLinenumbers, sectHead->NumberOfLinenumbers);
362 printf(" characteristics: 0x%08lx\n", sectHead->Characteristics);
363 printf(" ");
364 #define X(b,s) if (sectHead->Characteristics & b) printf(s " ")
365 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
366 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
367 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
368 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
369 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
370 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
372 X(IMAGE_SCN_CNT_CODE, "CODE");
373 X(IMAGE_SCN_CNT_INITIALIZED_DATA, "INITIALIZED_DATA");
374 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA, "UNINITIALIZED_DATA");
376 X(IMAGE_SCN_LNK_OTHER, "LNK_OTHER");
377 X(IMAGE_SCN_LNK_INFO, "LNK_INFO");
378 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
379 X(IMAGE_SCN_LNK_REMOVE, "LNK_REMOVE");
380 X(IMAGE_SCN_LNK_COMDAT, "LNK_COMDAT");
382 /* 0x00002000 - Reserved */
383 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
384 X(IMAGE_SCN_MEM_FARDATA, "MEM_FARDATA");
386 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
387 X(IMAGE_SCN_MEM_PURGEABLE, "MEM_PURGEABLE");
388 X(IMAGE_SCN_MEM_16BIT, "MEM_16BIT");
389 X(IMAGE_SCN_MEM_LOCKED, "MEM_LOCKED");
390 X(IMAGE_SCN_MEM_PRELOAD, "MEM_PRELOAD");
392 X(IMAGE_SCN_ALIGN_1BYTES, "ALIGN_1BYTES");
393 X(IMAGE_SCN_ALIGN_2BYTES, "ALIGN_2BYTES");
394 X(IMAGE_SCN_ALIGN_4BYTES, "ALIGN_4BYTES");
395 X(IMAGE_SCN_ALIGN_8BYTES, "ALIGN_8BYTES");
396 X(IMAGE_SCN_ALIGN_16BYTES, "ALIGN_16BYTES");
397 X(IMAGE_SCN_ALIGN_32BYTES, "ALIGN_32BYTES");
398 X(IMAGE_SCN_ALIGN_64BYTES, "ALIGN_64BYTES");
399 /* 0x00800000 - Unused */
401 X(IMAGE_SCN_LNK_NRELOC_OVFL, "LNK_NRELOC_OVFL");
403 X(IMAGE_SCN_MEM_DISCARDABLE, "MEM_DISCARDABLE");
404 X(IMAGE_SCN_MEM_NOT_CACHED, "MEM_NOT_CACHED");
405 X(IMAGE_SCN_MEM_NOT_PAGED, "MEM_NOT_PAGED");
406 X(IMAGE_SCN_MEM_SHARED, "MEM_SHARED");
407 X(IMAGE_SCN_MEM_EXECUTE, "MEM_EXECUTE");
408 X(IMAGE_SCN_MEM_READ, "MEM_READ");
409 X(IMAGE_SCN_MEM_WRITE, "MEM_WRITE");
410 #undef X
411 printf("\n\n");
413 printf("\n");
416 static void dump_dir_exported_functions(void)
418 unsigned int size = 0;
419 const IMAGE_EXPORT_DIRECTORY*exportDir = get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY, &size);
420 unsigned int i;
421 const DWORD* pFunc;
422 const DWORD* pName;
423 const WORD* pOrdl;
424 DWORD* map;
425 parsed_symbol symbol;
427 if (!exportDir) return;
429 printf("Exports table:\n");
430 printf("\n");
431 printf(" Name: %s\n", (const char*)RVA(exportDir->Name, sizeof(DWORD)));
432 printf(" Characteristics: %08lx\n", exportDir->Characteristics);
433 printf(" TimeDateStamp: %08lX %s\n",
434 exportDir->TimeDateStamp, get_time_str(exportDir->TimeDateStamp));
435 printf(" Version: %u.%02u\n", exportDir->MajorVersion, exportDir->MinorVersion);
436 printf(" Ordinal base: %lu\n", exportDir->Base);
437 printf(" # of functions: %lu\n", exportDir->NumberOfFunctions);
438 printf(" # of Names: %lu\n", exportDir->NumberOfNames);
439 printf("Addresses of functions: %08lX\n", exportDir->AddressOfFunctions);
440 printf("Addresses of name ordinals: %08lX\n", exportDir->AddressOfNameOrdinals);
441 printf("Addresses of names: %08lX\n", exportDir->AddressOfNames);
442 printf("\n");
443 printf(" Entry Pt Ordn Name\n");
445 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
446 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
447 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
448 if (!pName) {printf("Can't grab functions' name table\n"); return;}
449 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
450 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
452 /* bit map of used funcs */
453 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
454 if (!map) fatal("no memory");
456 for (i = 0; i < exportDir->NumberOfNames; i++, pName++, pOrdl++)
458 const char* name;
460 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
462 name = (const char*)RVA(*pName, sizeof(DWORD));
463 if (name && globals.do_demangle)
465 printf(" %08lX %4lu ", pFunc[*pOrdl], exportDir->Base + *pOrdl);
467 symbol_init(&symbol, name);
468 if (symbol_demangle(&symbol) == -1)
469 printf(name);
470 else if (symbol.flags & SYM_DATA)
471 printf(symbol.arg_text[0]);
472 else
473 output_prototype(stdout, &symbol);
474 symbol_clear(&symbol);
476 else
478 printf(" %08lX %4lu %s", pFunc[*pOrdl], exportDir->Base + *pOrdl, name);
480 /* check for forwarded function */
481 if ((const char *)RVA(pFunc[*pOrdl],sizeof(void*)) >= (const char *)exportDir &&
482 (const char *)RVA(pFunc[*pOrdl],sizeof(void*)) < (const char *)exportDir + size)
483 printf( " (-> %s)", (const char *)RVA(pFunc[*pOrdl],1));
484 printf("\n");
486 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
487 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
488 for (i = 0; i < exportDir->NumberOfFunctions; i++)
490 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
492 printf(" %08lX %4lu <by ordinal>\n", pFunc[i], exportDir->Base + i);
495 free(map);
496 printf("\n");
499 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64 *il)
501 /* FIXME: This does not properly handle large images */
502 const IMAGE_IMPORT_BY_NAME* iibn;
503 for (; il->u1.Ordinal; il++)
505 if (IMAGE_SNAP_BY_ORDINAL64(il->u1.Ordinal))
506 printf(" %4lu <by ordinal>\n", (DWORD)IMAGE_ORDINAL64(il->u1.Ordinal));
507 else
509 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
510 if (!iibn)
511 printf("Can't grab import by name info, skipping to next ordinal\n");
512 else
513 printf(" %4u %s %lx\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
518 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32 *il)
520 const IMAGE_IMPORT_BY_NAME* iibn;
521 for (; il->u1.Ordinal; il++)
523 if (IMAGE_SNAP_BY_ORDINAL32(il->u1.Ordinal))
524 printf(" %4lu <by ordinal>\n", IMAGE_ORDINAL32(il->u1.Ordinal));
525 else
527 iibn = RVA((DWORD)il->u1.AddressOfData, sizeof(DWORD));
528 if (!iibn)
529 printf("Can't grab import by name info, skipping to next ordinal\n");
530 else
531 printf(" %4u %s %lx\n", iibn->Hint, iibn->Name, (DWORD)il->u1.AddressOfData);
536 static void dump_dir_imported_functions(void)
538 const IMAGE_IMPORT_DESCRIPTOR *importDesc = get_dir(IMAGE_FILE_IMPORT_DIRECTORY);
539 DWORD directorySize;
541 if (!importDesc) return;
542 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
544 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64*)&PE_nt_headers->OptionalHeader;
545 directorySize = opt->DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY].Size;
547 else
549 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32*)&PE_nt_headers->OptionalHeader;
550 directorySize = opt->DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY].Size;
553 printf("Import Table size: %08lx\n", directorySize);/* FIXME */
555 for (;;)
557 const IMAGE_THUNK_DATA32* il;
559 if (!importDesc->Name || !importDesc->FirstThunk) break;
561 printf(" offset %08lx %s\n", Offset(importDesc), (const char*)RVA(importDesc->Name, sizeof(DWORD)));
562 printf(" Hint/Name Table: %08lX\n", (DWORD)importDesc->u.OriginalFirstThunk);
563 printf(" TimeDataStamp: %08lX (%s)\n",
564 importDesc->TimeDateStamp, get_time_str(importDesc->TimeDateStamp));
565 printf(" ForwarderChain: %08lX\n", importDesc->ForwarderChain);
566 printf(" First thunk RVA: %08lX\n", (DWORD)importDesc->FirstThunk);
568 printf(" Ordn Name\n");
570 il = (importDesc->u.OriginalFirstThunk != 0) ?
571 RVA((DWORD)importDesc->u.OriginalFirstThunk, sizeof(DWORD)) :
572 RVA((DWORD)importDesc->FirstThunk, sizeof(DWORD));
574 if (!il)
575 printf("Can't grab thunk data, going to next imported DLL\n");
576 else
578 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
579 dump_image_thunk_data64((const IMAGE_THUNK_DATA64*)il);
580 else
581 dump_image_thunk_data32(il);
582 printf("\n");
584 importDesc++;
586 printf("\n");
589 static void dump_dir_delay_imported_functions(void)
591 const struct ImgDelayDescr
593 DWORD grAttrs;
594 DWORD szName;
595 DWORD phmod;
596 DWORD pIAT;
597 DWORD pINT;
598 DWORD pBoundIAT;
599 DWORD pUnloadIAT;
600 DWORD dwTimeStamp;
601 } *importDesc = get_dir(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT);
602 DWORD directorySize;
604 if (!importDesc) return;
605 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
607 const IMAGE_OPTIONAL_HEADER64 *opt = (const IMAGE_OPTIONAL_HEADER64 *)&PE_nt_headers->OptionalHeader;
608 directorySize = opt->DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size;
610 else
612 const IMAGE_OPTIONAL_HEADER32 *opt = (const IMAGE_OPTIONAL_HEADER32 *)&PE_nt_headers->OptionalHeader;
613 directorySize = opt->DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size;
616 printf("Delay Import Table size: %08lx\n", directorySize); /* FIXME */
618 for (;;)
620 BOOL use_rva = importDesc->grAttrs & 1;
621 const IMAGE_THUNK_DATA32* il;
623 if (!importDesc->szName || !importDesc->pIAT || !importDesc->pINT) break;
625 printf(" grAttrs %08lx offset %08lx %s\n", importDesc->grAttrs, Offset(importDesc),
626 use_rva ? (const char *)RVA(importDesc->szName, sizeof(DWORD)) : (char *)importDesc->szName);
627 printf(" Hint/Name Table: %08lx\n", importDesc->pINT);
628 printf(" TimeDataStamp: %08lX (%s)\n",
629 importDesc->dwTimeStamp, get_time_str(importDesc->dwTimeStamp));
631 printf(" Ordn Name\n");
633 il = use_rva ? (const IMAGE_THUNK_DATA32 *)RVA(importDesc->pINT, sizeof(DWORD)) : (const IMAGE_THUNK_DATA32 *)importDesc->pINT;
635 if (!il)
636 printf("Can't grab thunk data, going to next imported DLL\n");
637 else
639 if (PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
640 dump_image_thunk_data64((const IMAGE_THUNK_DATA64 *)il);
641 else
642 dump_image_thunk_data32(il);
643 printf("\n");
645 importDesc++;
647 printf("\n");
650 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY* idd, int idx)
652 const char* str;
654 printf("Directory %02u\n", idx + 1);
655 printf(" Characteristics: %08lX\n", idd->Characteristics);
656 printf(" TimeDateStamp: %08lX %s\n",
657 idd->TimeDateStamp, get_time_str(idd->TimeDateStamp));
658 printf(" Version %u.%02u\n", idd->MajorVersion, idd->MinorVersion);
659 switch (idd->Type)
661 default:
662 case IMAGE_DEBUG_TYPE_UNKNOWN: str = "UNKNOWN"; break;
663 case IMAGE_DEBUG_TYPE_COFF: str = "COFF"; break;
664 case IMAGE_DEBUG_TYPE_CODEVIEW: str = "CODEVIEW"; break;
665 case IMAGE_DEBUG_TYPE_FPO: str = "FPO"; break;
666 case IMAGE_DEBUG_TYPE_MISC: str = "MISC"; break;
667 case IMAGE_DEBUG_TYPE_EXCEPTION: str = "EXCEPTION"; break;
668 case IMAGE_DEBUG_TYPE_FIXUP: str = "FIXUP"; break;
669 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC: str = "OMAP_TO_SRC"; break;
670 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:str = "OMAP_FROM_SRC"; break;
671 case IMAGE_DEBUG_TYPE_BORLAND: str = "BORLAND"; break;
672 case IMAGE_DEBUG_TYPE_RESERVED10: str = "RESERVED10"; break;
674 printf(" Type: %lu (%s)\n", idd->Type, str);
675 printf(" SizeOfData: %lu\n", idd->SizeOfData);
676 printf(" AddressOfRawData: %08lX\n", idd->AddressOfRawData);
677 printf(" PointerToRawData: %08lX\n", idd->PointerToRawData);
679 switch (idd->Type)
681 case IMAGE_DEBUG_TYPE_UNKNOWN:
682 break;
683 case IMAGE_DEBUG_TYPE_COFF:
684 dump_coff(idd->PointerToRawData, idd->SizeOfData,
685 (const char*)PE_nt_headers + sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + PE_nt_headers->FileHeader.SizeOfOptionalHeader);
686 break;
687 case IMAGE_DEBUG_TYPE_CODEVIEW:
688 dump_codeview(idd->PointerToRawData, idd->SizeOfData);
689 break;
690 case IMAGE_DEBUG_TYPE_FPO:
691 dump_frame_pointer_omission(idd->PointerToRawData, idd->SizeOfData);
692 break;
693 case IMAGE_DEBUG_TYPE_MISC:
695 const IMAGE_DEBUG_MISC* misc = PRD(idd->PointerToRawData, idd->SizeOfData);
696 if (!misc) {printf("Can't get misc debug information\n"); break;}
697 printf(" DataType: %lu (%s)\n",
698 misc->DataType,
699 (misc->DataType == IMAGE_DEBUG_MISC_EXENAME) ? "Exe name" : "Unknown");
700 printf(" Length: %lu\n", misc->Length);
701 printf(" Unicode: %s\n", misc->Unicode ? "Yes" : "No");
702 printf(" Data: %s\n", misc->Data);
704 break;
705 case IMAGE_DEBUG_TYPE_EXCEPTION:
706 break;
707 case IMAGE_DEBUG_TYPE_FIXUP:
708 break;
709 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC:
710 break;
711 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC:
712 break;
713 case IMAGE_DEBUG_TYPE_BORLAND:
714 break;
715 case IMAGE_DEBUG_TYPE_RESERVED10:
716 break;
718 printf("\n");
721 static void dump_dir_debug(void)
723 const IMAGE_DEBUG_DIRECTORY*debugDir = get_dir(IMAGE_FILE_DEBUG_DIRECTORY);
724 unsigned nb_dbg, i;
726 if (!debugDir) return;
727 nb_dbg = PE_nt_headers->OptionalHeader.DataDirectory[IMAGE_FILE_DEBUG_DIRECTORY].Size /
728 sizeof(*debugDir);
729 if (!nb_dbg) return;
731 printf("Debug Table (%u directories)\n", nb_dbg);
733 for (i = 0; i < nb_dbg; i++)
735 dump_dir_debug_dir(debugDir, i);
736 debugDir++;
738 printf("\n");
741 static void dump_dir_tls(void)
743 IMAGE_TLS_DIRECTORY64 dir;
744 const DWORD *callbacks;
745 const IMAGE_TLS_DIRECTORY32 *pdir = get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE);
747 if (!pdir) return;
749 if(PE_nt_headers->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
750 memcpy(&dir, pdir, sizeof(dir));
751 else
753 dir.StartAddressOfRawData = pdir->StartAddressOfRawData;
754 dir.EndAddressOfRawData = pdir->EndAddressOfRawData;
755 dir.AddressOfIndex = pdir->AddressOfIndex;
756 dir.AddressOfCallBacks = pdir->AddressOfCallBacks;
757 dir.SizeOfZeroFill = pdir->SizeOfZeroFill;
758 dir.Characteristics = pdir->Characteristics;
761 /* FIXME: This does not properly handle large images */
762 printf( "Thread Local Storage\n" );
763 printf( " Raw data %08lx-%08lx (data size %lx zero fill size %lx)\n",
764 (DWORD)dir.StartAddressOfRawData, (DWORD)dir.EndAddressOfRawData,
765 (DWORD)(dir.EndAddressOfRawData - dir.StartAddressOfRawData),
766 (DWORD)dir.SizeOfZeroFill );
767 printf( " Index address %08lx\n", (DWORD)dir.AddressOfIndex );
768 printf( " Characteristics %08lx\n", dir.Characteristics );
769 printf( " Callbacks %08lx -> {", (DWORD)dir.AddressOfCallBacks );
770 if (dir.AddressOfCallBacks)
772 DWORD addr = (DWORD)dir.AddressOfCallBacks - PE_nt_headers->OptionalHeader.ImageBase;
773 while ((callbacks = RVA(addr, sizeof(DWORD))) && *callbacks)
775 printf( " %08lx", *callbacks );
776 addr += sizeof(DWORD);
779 printf(" }\n\n");
782 void dump_separate_dbg(void)
784 const IMAGE_SEPARATE_DEBUG_HEADER* separateDebugHead;
785 unsigned nb_dbg;
786 unsigned i;
787 const IMAGE_DEBUG_DIRECTORY* debugDir;
789 separateDebugHead = PRD(0, sizeof(separateDebugHead));
790 if (!separateDebugHead) {printf("Can't grab the separate header, aborting\n"); return;}
792 printf ("Signature: %.2s (0x%4X)\n",
793 (const char*)&separateDebugHead->Signature, separateDebugHead->Signature);
794 printf ("Flags: 0x%04X\n", separateDebugHead->Flags);
795 printf ("Machine: 0x%04X (%s)\n",
796 separateDebugHead->Machine, get_machine_str(separateDebugHead->Machine));
797 printf ("Characteristics: 0x%04X\n", separateDebugHead->Characteristics);
798 printf ("TimeDateStamp: 0x%08lX (%s)\n",
799 separateDebugHead->TimeDateStamp, get_time_str(separateDebugHead->TimeDateStamp));
800 printf ("CheckSum: 0x%08lX\n", separateDebugHead->CheckSum);
801 printf ("ImageBase: 0x%08lX\n", separateDebugHead->ImageBase);
802 printf ("SizeOfImage: 0x%08lX\n", separateDebugHead->SizeOfImage);
803 printf ("NumberOfSections: 0x%08lX\n", separateDebugHead->NumberOfSections);
804 printf ("ExportedNamesSize: 0x%08lX\n", separateDebugHead->ExportedNamesSize);
805 printf ("DebugDirectorySize: 0x%08lX\n", separateDebugHead->DebugDirectorySize);
807 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER),
808 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)))
809 {printf("Can't get the sections, aborting\n"); return;}
811 dump_sections(separateDebugHead + 1, separateDebugHead->NumberOfSections);
813 nb_dbg = separateDebugHead->DebugDirectorySize / sizeof(IMAGE_DEBUG_DIRECTORY);
814 debugDir = PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER) +
815 separateDebugHead->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) +
816 separateDebugHead->ExportedNamesSize,
817 nb_dbg * sizeof(IMAGE_DEBUG_DIRECTORY));
818 if (!debugDir) {printf("Couldn't get the debug directory info, aborting\n");return;}
820 printf("Debug Table (%u directories)\n", nb_dbg);
822 for (i = 0; i < nb_dbg; i++)
824 dump_dir_debug_dir(debugDir, i);
825 debugDir++;
829 static const char *get_resource_type( unsigned int id )
831 static const char *types[] =
833 NULL,
834 "CURSOR",
835 "BITMAP",
836 "ICON",
837 "MENU",
838 "DIALOG",
839 "STRING",
840 "FONTDIR",
841 "FONT",
842 "ACCELERATOR",
843 "RCDATA",
844 "MESSAGETABLE",
845 "GROUP_CURSOR",
846 NULL,
847 "GROUP_ICON",
848 NULL,
849 "VERSION",
850 "DLGINCLUDE",
851 NULL,
852 "PLUGPLAY",
853 "VXD",
854 "ANICURSOR",
855 "ANIICON",
856 "HTML"
859 if ((size_t)id < sizeof(types)/sizeof(types[0])) return types[id];
860 return NULL;
863 /* dump an ASCII string with proper escaping */
864 static int dump_strA( const unsigned char *str, size_t len )
866 static const char escapes[32] = ".......abtnvfr.............e....";
867 char buffer[256];
868 char *pos = buffer;
869 int count = 0;
871 for (; len; str++, len--)
873 if (pos > buffer + sizeof(buffer) - 8)
875 fwrite( buffer, pos - buffer, 1, stdout );
876 count += pos - buffer;
877 pos = buffer;
879 if (*str > 127) /* hex escape */
881 pos += sprintf( pos, "\\x%02x", *str );
882 continue;
884 if (*str < 32) /* octal or C escape */
886 if (!*str && len == 1) continue; /* do not output terminating NULL */
887 if (escapes[*str] != '.')
888 pos += sprintf( pos, "\\%c", escapes[*str] );
889 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
890 pos += sprintf( pos, "\\%03o", *str );
891 else
892 pos += sprintf( pos, "\\%o", *str );
893 continue;
895 if (*str == '\\') *pos++ = '\\';
896 *pos++ = *str;
898 fwrite( buffer, pos - buffer, 1, stdout );
899 count += pos - buffer;
900 return count;
903 /* dump a Unicode string with proper escaping */
904 static int dump_strW( const WCHAR *str, size_t len )
906 static const char escapes[32] = ".......abtnvfr.............e....";
907 char buffer[256];
908 char *pos = buffer;
909 int count = 0;
911 for (; len; str++, len--)
913 if (pos > buffer + sizeof(buffer) - 8)
915 fwrite( buffer, pos - buffer, 1, stdout );
916 count += pos - buffer;
917 pos = buffer;
919 if (*str > 127) /* hex escape */
921 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
922 pos += sprintf( pos, "\\x%04x", *str );
923 else
924 pos += sprintf( pos, "\\x%x", *str );
925 continue;
927 if (*str < 32) /* octal or C escape */
929 if (!*str && len == 1) continue; /* do not output terminating NULL */
930 if (escapes[*str] != '.')
931 pos += sprintf( pos, "\\%c", escapes[*str] );
932 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
933 pos += sprintf( pos, "\\%03o", *str );
934 else
935 pos += sprintf( pos, "\\%o", *str );
936 continue;
938 if (*str == '\\') *pos++ = '\\';
939 *pos++ = *str;
941 fwrite( buffer, pos - buffer, 1, stdout );
942 count += pos - buffer;
943 return count;
946 /* dump data for a STRING resource */
947 static void dump_string_data( const WCHAR *ptr, unsigned int size, unsigned int id, const char *prefix )
949 int i;
951 for (i = 0; i < 16 && size; i++)
953 unsigned len = *ptr++;
955 if (len >= size)
957 len = size;
958 size = 0;
960 else size -= len + 1;
962 if (len)
964 printf( "%s%04x \"", prefix, (id - 1) * 16 + i );
965 dump_strW( ptr, len );
966 printf( "\"\n" );
967 ptr += len;
972 /* dump data for a MESSAGETABLE resource */
973 static void dump_msgtable_data( const void *ptr, unsigned int size, unsigned int id, const char *prefix )
975 const MESSAGE_RESOURCE_DATA *data = ptr;
976 const MESSAGE_RESOURCE_BLOCK *block = data->Blocks;
977 unsigned i, j;
979 for (i = 0; i < data->NumberOfBlocks; i++, block++)
981 const MESSAGE_RESOURCE_ENTRY *entry;
983 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)data + block->OffsetToEntries);
984 for (j = block->LowId; j <= block->HighId; j++)
986 if (entry->Flags & MESSAGE_RESOURCE_UNICODE)
988 const WCHAR *str = (const WCHAR *)entry->Text;
989 printf( "%s%08x L\"", prefix, j );
990 dump_strW( str, strlenW(str) );
991 printf( "\"\n" );
993 else
995 const char *str = (const char *) entry->Text;
996 printf( "%s%08x \"", prefix, j );
997 dump_strA( entry->Text, strlen(str) );
998 printf( "\"\n" );
1000 entry = (const MESSAGE_RESOURCE_ENTRY *)((const char *)entry + entry->Length);
1005 static void dump_dir_resource(void)
1007 const IMAGE_RESOURCE_DIRECTORY *root = get_dir(IMAGE_FILE_RESOURCE_DIRECTORY);
1008 const IMAGE_RESOURCE_DIRECTORY *namedir;
1009 const IMAGE_RESOURCE_DIRECTORY *langdir;
1010 const IMAGE_RESOURCE_DIRECTORY_ENTRY *e1, *e2, *e3;
1011 const IMAGE_RESOURCE_DIR_STRING_U *string;
1012 const IMAGE_RESOURCE_DATA_ENTRY *data;
1013 int i, j, k;
1015 if (!root) return;
1017 printf( "Resources:" );
1019 for (i = 0; i< root->NumberOfNamedEntries + root->NumberOfIdEntries; i++)
1021 e1 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(root + 1) + i;
1022 namedir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e1->u2.s3.OffsetToDirectory);
1023 for (j = 0; j < namedir->NumberOfNamedEntries + namedir->NumberOfIdEntries; j++)
1025 e2 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(namedir + 1) + j;
1026 langdir = (const IMAGE_RESOURCE_DIRECTORY *)((const char *)root + e2->u2.s3.OffsetToDirectory);
1027 for (k = 0; k < langdir->NumberOfNamedEntries + langdir->NumberOfIdEntries; k++)
1029 e3 = (const IMAGE_RESOURCE_DIRECTORY_ENTRY*)(langdir + 1) + k;
1031 printf( "\n " );
1032 if (e1->u1.s1.NameIsString)
1034 string = (const IMAGE_RESOURCE_DIR_STRING_U*)((const char *)root + e1->u1.s1.NameOffset);
1035 dump_unicode_str( string->NameString, string->Length );
1037 else
1039 const char *type = get_resource_type( e1->u1.s2.Id );
1040 if (type) printf( "%s", type );
1041 else printf( "%04x", e1->u1.s2.Id );
1044 printf( " Name=" );
1045 if (e2->u1.s1.NameIsString)
1047 string = (const IMAGE_RESOURCE_DIR_STRING_U*) ((const char *)root + e2->u1.s1.NameOffset);
1048 dump_unicode_str( string->NameString, string->Length );
1050 else
1051 printf( "%04x", e2->u1.s2.Id );
1053 printf( " Language=%04x:\n", e3->u1.s2.Id );
1054 data = (const IMAGE_RESOURCE_DATA_ENTRY *)((const char *)root + e3->u2.OffsetToData);
1055 if (e1->u1.s1.NameIsString)
1057 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1059 else switch(e1->u1.s2.Id)
1061 case 6:
1062 dump_string_data( RVA( data->OffsetToData, data->Size ), data->Size,
1063 e2->u1.s2.Id, " " );
1064 break;
1065 case 11:
1066 dump_msgtable_data( RVA( data->OffsetToData, data->Size ), data->Size,
1067 e2->u1.s2.Id, " " );
1068 break;
1069 default:
1070 dump_data( RVA( data->OffsetToData, data->Size ), data->Size, " " );
1071 break;
1076 printf( "\n\n" );
1079 void pe_dump(const void* pmt)
1081 int all = (globals.dumpsect != NULL) && strcmp(globals.dumpsect, "ALL") == 0;
1083 PE_nt_headers = get_nt_header(pmt);
1084 if (is_fake_dll(pmt)) printf( "*** This is a Wine fake DLL ***\n\n" );
1086 if (globals.do_dumpheader)
1088 dump_pe_header();
1089 /* FIXME: should check ptr */
1090 dump_sections((const char*)PE_nt_headers + sizeof(DWORD) +
1091 sizeof(IMAGE_FILE_HEADER) + PE_nt_headers->FileHeader.SizeOfOptionalHeader,
1092 PE_nt_headers->FileHeader.NumberOfSections);
1094 else if (!globals.dumpsect)
1096 /* show at least something here */
1097 dump_pe_header();
1100 if (globals.dumpsect)
1102 if (all || !strcmp(globals.dumpsect, "import"))
1104 dump_dir_imported_functions();
1105 dump_dir_delay_imported_functions();
1107 if (all || !strcmp(globals.dumpsect, "export"))
1108 dump_dir_exported_functions();
1109 if (all || !strcmp(globals.dumpsect, "debug"))
1110 dump_dir_debug();
1111 if (all || !strcmp(globals.dumpsect, "resource"))
1112 dump_dir_resource();
1113 if (all || !strcmp(globals.dumpsect, "tls"))
1114 dump_dir_tls();
1115 #if 0
1116 /* FIXME: not implemented yet */
1117 if (all || !strcmp(globals.dumpsect, "reloc"))
1118 dump_dir_reloc();
1119 #endif
1123 typedef struct _dll_symbol {
1124 size_t ordinal;
1125 char *symbol;
1126 } dll_symbol;
1128 static dll_symbol *dll_symbols = NULL;
1129 static dll_symbol *dll_current_symbol = NULL;
1131 /* Compare symbols by ordinal for qsort */
1132 static int symbol_cmp(const void *left, const void *right)
1134 return ((const dll_symbol *)left)->ordinal > ((const dll_symbol *)right)->ordinal;
1137 /*******************************************************************
1138 * dll_close
1140 * Free resources used by DLL
1142 /* FIXME: Not used yet
1143 static void dll_close (void)
1145 dll_symbol* ds;
1147 if (!dll_symbols) {
1148 fatal("No symbols");
1150 for (ds = dll_symbols; ds->symbol; ds++)
1151 free(ds->symbol);
1152 free (dll_symbols);
1153 dll_symbols = NULL;
1157 static void do_grab_sym( enum FileSig sig, const void* pmt )
1159 const IMAGE_EXPORT_DIRECTORY*exportDir;
1160 unsigned i, j;
1161 const DWORD* pName;
1162 const DWORD* pFunc;
1163 const WORD* pOrdl;
1164 const char* ptr;
1165 DWORD* map;
1167 PE_nt_headers = get_nt_header(pmt);
1168 if (!(exportDir = get_dir(IMAGE_FILE_EXPORT_DIRECTORY))) return;
1170 pName = RVA(exportDir->AddressOfNames, exportDir->NumberOfNames * sizeof(DWORD));
1171 if (!pName) {printf("Can't grab functions' name table\n"); return;}
1172 pOrdl = RVA(exportDir->AddressOfNameOrdinals, exportDir->NumberOfNames * sizeof(WORD));
1173 if (!pOrdl) {printf("Can't grab functions' ordinal table\n"); return;}
1175 /* dll_close(); */
1177 if (!(dll_symbols = (dll_symbol *) malloc((exportDir->NumberOfFunctions + 1) *
1178 sizeof (dll_symbol))))
1179 fatal ("Out of memory");
1180 if (exportDir->AddressOfFunctions != exportDir->NumberOfNames || exportDir->Base > 1)
1181 globals.do_ordinals = 1;
1183 /* bit map of used funcs */
1184 map = calloc(((exportDir->NumberOfFunctions + 31) & ~31) / 32, sizeof(DWORD));
1185 if (!map) fatal("no memory");
1187 for (j = 0; j < exportDir->NumberOfNames; j++, pOrdl++)
1189 map[*pOrdl / 32] |= 1 << (*pOrdl % 32);
1190 ptr = RVA(*pName++, sizeof(DWORD));
1191 if (!ptr) ptr = "cant_get_function";
1192 dll_symbols[j].symbol = strdup(ptr);
1193 dll_symbols[j].ordinal = exportDir->Base + *pOrdl;
1194 assert(dll_symbols[j].symbol);
1196 pFunc = RVA(exportDir->AddressOfFunctions, exportDir->NumberOfFunctions * sizeof(DWORD));
1197 if (!pFunc) {printf("Can't grab functions' address table\n"); return;}
1199 for (i = 0; i < exportDir->NumberOfFunctions; i++)
1201 if (pFunc[i] && !(map[i / 32] & (1 << (i % 32))))
1203 char ordinal_text[256];
1204 /* Ordinal only entry */
1205 snprintf (ordinal_text, sizeof(ordinal_text), "%s_%lu",
1206 globals.forward_dll ? globals.forward_dll : OUTPUT_UC_DLL_NAME,
1207 exportDir->Base + i);
1208 str_toupper(ordinal_text);
1209 dll_symbols[j].symbol = strdup(ordinal_text);
1210 assert(dll_symbols[j].symbol);
1211 dll_symbols[j].ordinal = exportDir->Base + i;
1212 j++;
1213 assert(j <= exportDir->NumberOfFunctions);
1216 free(map);
1218 if (NORMAL)
1219 printf("%lu named symbols in DLL, %lu total, %d unique (ordinal base = %ld)\n",
1220 exportDir->NumberOfNames, exportDir->NumberOfFunctions, j, exportDir->Base);
1222 qsort( dll_symbols, j, sizeof(dll_symbol), symbol_cmp );
1224 dll_symbols[j].symbol = NULL;
1226 dll_current_symbol = dll_symbols;
1229 /*******************************************************************
1230 * dll_open
1232 * Open a DLL and read in exported symbols
1234 int dll_open (const char *dll_name)
1236 return dump_analysis(dll_name, do_grab_sym, SIG_PE);
1239 /*******************************************************************
1240 * dll_next_symbol
1242 * Get next exported symbol from dll
1244 int dll_next_symbol (parsed_symbol * sym)
1246 if (!dll_current_symbol->symbol)
1247 return 1;
1249 assert (dll_symbols);
1251 sym->symbol = strdup (dll_current_symbol->symbol);
1252 sym->ordinal = dll_current_symbol->ordinal;
1253 dll_current_symbol++;
1254 return 0;