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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
33 #ifdef HAVE_SYS_STAT_H
34 # include <sys/stat.h>
36 #ifdef HAVE_SYS_MMAN_H
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
52 unsigned long PE_total_len
;
53 IMAGE_NT_HEADERS
* PE_nt_headers
;
55 enum FileSig
{SIG_UNKNOWN
, SIG_DOS
, SIG_PE
, SIG_DBG
, SIG_NE
};
57 char* get_time_str(DWORD _t
)
59 time_t t
= (time_t)_t
;
62 /* FIXME: I don't get the same values from MS' pedump running under Wine...
63 * I wonder if Wine isn't broken wrt to GMT settings...
65 strncpy(buf
, ctime(&t
), sizeof(buf
));
66 buf
[sizeof(buf
) - 1] = '\0';
67 if (buf
[strlen(buf
)-1] == '\n')
68 buf
[strlen(buf
)-1] = '\0';
72 static const char* get_machine_str(DWORD mach
)
76 case IMAGE_FILE_MACHINE_UNKNOWN
: return "Unknown";
77 case IMAGE_FILE_MACHINE_I860
: return "i860";
78 case IMAGE_FILE_MACHINE_I386
: return "i386";
79 case IMAGE_FILE_MACHINE_R3000
: return "R3000";
80 case IMAGE_FILE_MACHINE_R4000
: return "R4000";
81 case IMAGE_FILE_MACHINE_R10000
: return "R10000";
82 case IMAGE_FILE_MACHINE_ALPHA
: return "Alpha";
83 case IMAGE_FILE_MACHINE_POWERPC
: return "PowerPC";
88 void* PRD(unsigned long prd
, unsigned long len
)
90 return (prd
+ len
> PE_total_len
) ? NULL
: (char*)PE_base
+ prd
;
93 unsigned long Offset(void* ptr
)
95 if (ptr
< PE_base
) {printf("<<<<<ptr below\n");return 0;}
96 if ((char *)ptr
>= (char*)PE_base
+ PE_total_len
) {printf("<<<<<ptr above\n");return 0;}
97 return (char*)ptr
- (char*)PE_base
;
100 void* RVA(unsigned long rva
, unsigned long len
)
102 IMAGE_SECTION_HEADER
* sectHead
;
105 sectHead
= (IMAGE_SECTION_HEADER
*)((char*)PE_nt_headers
+ sizeof(DWORD
) +
106 sizeof(IMAGE_FILE_HEADER
) +
107 PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
);
109 if (rva
== 0) return NULL
;
111 for (i
= PE_nt_headers
->FileHeader
.NumberOfSections
- 1; i
>= 0; i
--)
113 if (sectHead
[i
].VirtualAddress
<= rva
&&
114 rva
+ len
<= (DWORD
)sectHead
[i
].VirtualAddress
+ sectHead
[i
].SizeOfRawData
)
120 printf("rva not found in any section (%lu)\n", rva
);
124 /* return image import directory offset */
125 return PRD(sectHead
[i
].PointerToRawData
+ rva
- sectHead
[i
].VirtualAddress
, len
);
128 static void* get_dir(unsigned idx
)
130 if (idx
>= PE_nt_headers
->OptionalHeader
.NumberOfRvaAndSizes
)
132 return RVA(PE_nt_headers
->OptionalHeader
.DataDirectory
[idx
].VirtualAddress
,
133 PE_nt_headers
->OptionalHeader
.DataDirectory
[idx
].Size
);
136 static const char* DirectoryNames
[16] = {
137 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
138 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
139 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
140 "IAT", "Delay IAT", "COM Descript", ""
143 static void dump_pe_header(void)
146 IMAGE_FILE_HEADER
*fileHeader
;
147 IMAGE_OPTIONAL_HEADER
*optionalHeader
;
150 printf("File Header\n");
151 fileHeader
= &PE_nt_headers
->FileHeader
;
153 printf(" Machine: %04X (%s)\n",
154 fileHeader
->Machine
, get_machine_str(fileHeader
->Machine
));
155 printf(" Number of Sections: %d\n", fileHeader
->NumberOfSections
);
156 printf(" TimeDateStamp: %08lX (%s) offset %ld\n",
157 fileHeader
->TimeDateStamp
, get_time_str(fileHeader
->TimeDateStamp
),
158 Offset(&(fileHeader
->TimeDateStamp
)));
159 printf(" PointerToSymbolTable: %08lX\n", fileHeader
->PointerToSymbolTable
);
160 printf(" NumberOfSymbols: %08lX\n", fileHeader
->NumberOfSymbols
);
161 printf(" SizeOfOptionalHeader: %04X\n", fileHeader
->SizeOfOptionalHeader
);
162 printf(" Characteristics: %04X\n", fileHeader
->Characteristics
);
163 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
164 X(IMAGE_FILE_RELOCS_STRIPPED
, "RELOCS_STRIPPED");
165 X(IMAGE_FILE_EXECUTABLE_IMAGE
, "EXECUTABLE_IMAGE");
166 X(IMAGE_FILE_LINE_NUMS_STRIPPED
, "LINE_NUMS_STRIPPED");
167 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED
, "LOCAL_SYMS_STRIPPED");
168 X(IMAGE_FILE_16BIT_MACHINE
, "16BIT_MACHINE");
169 X(IMAGE_FILE_BYTES_REVERSED_LO
, "BYTES_REVERSED_LO");
170 X(IMAGE_FILE_32BIT_MACHINE
, "32BIT_MACHINE");
171 X(IMAGE_FILE_DEBUG_STRIPPED
, "DEBUG_STRIPPED");
172 X(IMAGE_FILE_SYSTEM
, "SYSTEM");
173 X(IMAGE_FILE_DLL
, "DLL");
174 X(IMAGE_FILE_BYTES_REVERSED_HI
, "BYTES_REVERSED_HI");
178 /* hope we have the right size */
179 printf("Optional Header\n");
180 optionalHeader
= &PE_nt_headers
->OptionalHeader
;
181 printf(" Magic 0x%-4X %u\n",
182 optionalHeader
->Magic
, optionalHeader
->Magic
);
183 printf(" linker version %u.%02u\n",
184 optionalHeader
->MajorLinkerVersion
, optionalHeader
->MinorLinkerVersion
);
185 printf(" size of code 0x%-8lx %lu\n",
186 optionalHeader
->SizeOfCode
, optionalHeader
->SizeOfCode
);
187 printf(" size of initialized data 0x%-8lx %lu\n",
188 optionalHeader
->SizeOfInitializedData
, optionalHeader
->SizeOfInitializedData
);
189 printf(" size of uninitialized data 0x%-8lx %lu\n",
190 optionalHeader
->SizeOfUninitializedData
, optionalHeader
->SizeOfUninitializedData
);
191 printf(" entrypoint RVA 0x%-8lx %lu\n",
192 optionalHeader
->AddressOfEntryPoint
, optionalHeader
->AddressOfEntryPoint
);
193 printf(" base of code 0x%-8lx %lu\n",
194 optionalHeader
->BaseOfCode
, optionalHeader
->BaseOfCode
);
195 printf(" base of data 0x%-8lX %lu\n",
196 optionalHeader
->BaseOfData
, optionalHeader
->BaseOfData
);
197 printf(" image base 0x%-8lX %lu\n",
198 optionalHeader
->ImageBase
, optionalHeader
->ImageBase
);
199 printf(" section align 0x%-8lx %lu\n",
200 optionalHeader
->SectionAlignment
, optionalHeader
->SectionAlignment
);
201 printf(" file align 0x%-8lx %lu\n",
202 optionalHeader
->FileAlignment
, optionalHeader
->FileAlignment
);
203 printf(" required OS version %u.%02u\n",
204 optionalHeader
->MajorOperatingSystemVersion
, optionalHeader
->MinorOperatingSystemVersion
);
205 printf(" image version %u.%02u\n",
206 optionalHeader
->MajorImageVersion
, optionalHeader
->MinorImageVersion
);
207 printf(" subsystem version %u.%02u\n",
208 optionalHeader
->MajorSubsystemVersion
, optionalHeader
->MinorSubsystemVersion
);
209 printf(" Win32 Version 0x%lX\n", optionalHeader
->Win32VersionValue
);
210 printf(" size of image 0x%-8lx %lu\n",
211 optionalHeader
->SizeOfImage
, optionalHeader
->SizeOfImage
);
212 printf(" size of headers 0x%-8lx %lu\n",
213 optionalHeader
->SizeOfHeaders
, optionalHeader
->SizeOfHeaders
);
214 printf(" checksum 0x%lX\n", optionalHeader
->CheckSum
);
215 switch (optionalHeader
->Subsystem
)
218 case IMAGE_SUBSYSTEM_UNKNOWN
: str
= "Unknown"; break;
219 case IMAGE_SUBSYSTEM_NATIVE
: str
= "Native"; break;
220 case IMAGE_SUBSYSTEM_WINDOWS_GUI
: str
= "Windows GUI"; break;
221 case IMAGE_SUBSYSTEM_WINDOWS_CUI
: str
= "Windows CUI"; break;
222 case IMAGE_SUBSYSTEM_OS2_CUI
: str
= "OS/2 CUI"; break;
223 case IMAGE_SUBSYSTEM_POSIX_CUI
: str
= "Posix CUI"; break;
225 printf(" Subsystem 0x%X (%s)\n", optionalHeader
->Subsystem
, str
);
226 printf(" DLL flags 0x%X\n", optionalHeader
->DllCharacteristics
);
227 printf(" stack reserve size 0x%-8lx %lu\n",
228 optionalHeader
->SizeOfStackReserve
, optionalHeader
->SizeOfStackReserve
);
229 printf(" stack commit size 0x%-8lx %lu\n",
230 optionalHeader
->SizeOfStackCommit
, optionalHeader
->SizeOfStackCommit
);
231 printf(" heap reserve size 0x%-8lx %lu\n",
232 optionalHeader
->SizeOfHeapReserve
, optionalHeader
->SizeOfHeapReserve
);
233 printf(" heap commit size 0x%-8lx %lu\n",
234 optionalHeader
->SizeOfHeapCommit
, optionalHeader
->SizeOfHeapCommit
);
235 printf(" loader flags 0x%lX\n", optionalHeader
->LoaderFlags
);
236 printf(" RVAs & sizes 0x%lX\n", optionalHeader
->NumberOfRvaAndSizes
);
239 printf("Data Directory\n");
240 printf("%ld\n", optionalHeader
->NumberOfRvaAndSizes
* sizeof(IMAGE_DATA_DIRECTORY
));
242 for (i
= 0; i
< optionalHeader
->NumberOfRvaAndSizes
&& i
< 16; i
++)
244 printf(" %-12s rva: 0x%-8lX size: %8lu\n",
245 DirectoryNames
[i
], optionalHeader
->DataDirectory
[i
].VirtualAddress
,
246 optionalHeader
->DataDirectory
[i
].Size
);
251 static void dump_sections(void* addr
, unsigned num_sect
)
253 IMAGE_SECTION_HEADER
* sectHead
= addr
;
256 printf("Section Table\n");
257 for (i
= 0; i
< num_sect
; i
++, sectHead
++)
259 printf(" %02d %-8s VirtSize: %-8lu VirtAddr: %-8lu 0x%08lx\n",
260 i
+ 1, sectHead
->Name
, sectHead
->Misc
.VirtualSize
, sectHead
->VirtualAddress
,
261 sectHead
->VirtualAddress
);
262 printf(" raw data offs: %-8lu raw data size: %-8lu\n",
263 sectHead
->PointerToRawData
, sectHead
->SizeOfRawData
);
264 printf(" relocation offs: %-8lu relocations: %-8u\n",
265 sectHead
->PointerToRelocations
, sectHead
->NumberOfRelocations
);
266 printf(" line # offs: %-8lu line #'s: %-8u\n",
267 sectHead
->PointerToLinenumbers
, sectHead
->NumberOfLinenumbers
);
268 printf(" characteristics: 0x$%08lx\n", sectHead
->Characteristics
);
270 #define X(b,s) if (sectHead->Characteristics & b) printf(s " ")
271 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
272 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
273 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
274 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
275 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
276 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
278 X(IMAGE_SCN_CNT_CODE
, "CODE");
279 X(IMAGE_SCN_CNT_INITIALIZED_DATA
, "INITIALIZED_DATA");
280 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA
, "UNINITIALIZED_DATA");
282 X(IMAGE_SCN_LNK_OTHER
, "LNK_OTHER");
283 X(IMAGE_SCN_LNK_INFO
, "LNK_INFO");
284 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
285 X(IMAGE_SCN_LNK_REMOVE
, "LNK_REMOVE");
286 X(IMAGE_SCN_LNK_COMDAT
, "LNK_COMDAT");
288 /* 0x00002000 - Reserved */
289 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
290 X(IMAGE_SCN_MEM_FARDATA
, "MEM_FARDATA");
292 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
293 X(IMAGE_SCN_MEM_PURGEABLE
, "MEM_PURGEABLE");
294 X(IMAGE_SCN_MEM_16BIT
, "MEM_16BIT");
295 X(IMAGE_SCN_MEM_LOCKED
, "MEM_LOCKED");
296 X(IMAGE_SCN_MEM_PRELOAD
, "MEM_PRELOAD");
298 X(IMAGE_SCN_ALIGN_1BYTES
, "ALIGN_1BYTES");
299 X(IMAGE_SCN_ALIGN_2BYTES
, "ALIGN_2BYTES");
300 X(IMAGE_SCN_ALIGN_4BYTES
, "ALIGN_4BYTES");
301 X(IMAGE_SCN_ALIGN_8BYTES
, "ALIGN_8BYTES");
302 X(IMAGE_SCN_ALIGN_16BYTES
, "ALIGN_16BYTES");
303 X(IMAGE_SCN_ALIGN_32BYTES
, "ALIGN_32BYTES");
304 X(IMAGE_SCN_ALIGN_64BYTES
, "ALIGN_64BYTES");
305 /* 0x00800000 - Unused */
307 X(IMAGE_SCN_LNK_NRELOC_OVFL
, "LNK_NRELOC_OVFL");
309 X(IMAGE_SCN_MEM_DISCARDABLE
, "MEM_DISCARDABLE");
310 X(IMAGE_SCN_MEM_NOT_CACHED
, "MEM_NOT_CACHED");
311 X(IMAGE_SCN_MEM_NOT_PAGED
, "MEM_NOT_PAGED");
312 X(IMAGE_SCN_MEM_SHARED
, "MEM_SHARED");
313 X(IMAGE_SCN_MEM_EXECUTE
, "MEM_EXECUTE");
314 X(IMAGE_SCN_MEM_READ
, "MEM_READ");
315 X(IMAGE_SCN_MEM_WRITE
, "MEM_WRITE");
322 static void dump_dir_exported_functions(void)
324 IMAGE_EXPORT_DIRECTORY
*exportDir
= get_dir(IMAGE_FILE_EXPORT_DIRECTORY
);
330 parsed_symbol symbol
;
332 if (!exportDir
) return;
334 printf("Exports table:\n");
336 printf(" Name: %s\n", (char*)RVA(exportDir
->Name
, sizeof(DWORD
)));
337 printf(" Characteristics: %08lx\n", exportDir
->Characteristics
);
338 printf(" TimeDateStamp: %08lX %s\n",
339 exportDir
->TimeDateStamp
, get_time_str(exportDir
->TimeDateStamp
));
340 printf(" Version: %u.%02u\n", exportDir
->MajorVersion
, exportDir
->MinorVersion
);
341 printf(" Ordinal base: %lu\n", exportDir
->Base
);
342 printf(" # of functions: %lu\n", exportDir
->NumberOfFunctions
);
343 printf(" # of Names: %lu\n", exportDir
->NumberOfNames
);
344 printf("Adresses of functions: %08lX\n", exportDir
->AddressOfFunctions
);
345 printf("Adresses of name ordinals: %08lX\n", exportDir
->AddressOfNameOrdinals
);
346 printf("Adresses of names: %08lX\n", exportDir
->AddressOfNames
);
348 printf(" Entry Pt Ordn Name\n");
350 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
351 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
352 pName
= RVA(exportDir
->AddressOfNames
, exportDir
->NumberOfNames
* sizeof(DWORD
));
353 if (!pName
) {printf("Can't grab functions' name table\n"); return;}
354 pOrdl
= RVA(exportDir
->AddressOfNameOrdinals
, exportDir
->NumberOfNames
* sizeof(WORD
));
355 if (!pOrdl
) {printf("Can't grab functions' ordinal table\n"); return;}
357 /* bit map of used funcs */
358 map
= calloc(((exportDir
->NumberOfFunctions
+ 31) & ~31) / 32, sizeof(DWORD
));
359 if (!map
) fatal("no memory");
361 for (i
= 0; i
< exportDir
->NumberOfNames
; i
++, pName
++, pOrdl
++)
365 map
[*pOrdl
/ 32] |= 1 << (*pOrdl
% 32);
367 name
= (char*)RVA(*pName
, sizeof(DWORD
));
368 if (name
&& globals
.do_demangle
)
370 printf(" %08lX %4lu ", pFunc
[*pOrdl
], exportDir
->Base
+ *pOrdl
);
372 symbol_init(&symbol
, name
);
373 if (symbol_demangle(&symbol
) == -1)
375 else if (symbol
.flags
& SYM_DATA
)
376 printf(symbol
.arg_text
[0]);
378 output_prototype(stdout
, &symbol
);
380 symbol_clear(&symbol
);
384 printf(" %08lX %4lu %s\n", pFunc
[*pOrdl
], exportDir
->Base
+ *pOrdl
, name
);
387 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
388 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
389 for (i
= 0; i
< exportDir
->NumberOfFunctions
; i
++)
391 if (pFunc
[i
] && !(map
[i
/ 32] & (1 << (i
% 32))))
393 printf(" %08lX %4lu <by ordinal>\n", pFunc
[i
], exportDir
->Base
+ i
);
400 static void dump_dir_imported_functions(void)
402 IMAGE_IMPORT_DESCRIPTOR
*importDesc
= get_dir(IMAGE_FILE_IMPORT_DIRECTORY
);
405 if (!importDesc
) return;
406 nb_imp
= PE_nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
].Size
/
410 printf("Import Table size: %lu\n",
411 PE_nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
].Size
);/* FIXME */
413 for (i
= 0; i
< nb_imp
- 1; i
++) /* the last descr is set as 0 as a sentinel */
415 IMAGE_THUNK_DATA
* il
;
416 IMAGE_IMPORT_BY_NAME
* iibn
;
418 if (!importDesc
->Name
||
419 (importDesc
->u
.OriginalFirstThunk
== NULL
&& importDesc
->FirstThunk
== NULL
))
422 printf("<<<<<<<null entry\n");
425 printf(" offset %lu %s\n", Offset(importDesc
), (char*)RVA(importDesc
->Name
, sizeof(DWORD
)));
426 printf(" Hint/Name Table: %08lX\n", (DWORD
)importDesc
->u
.OriginalFirstThunk
);
427 printf(" TimeDataStamp: %08lX (%s)\n",
428 importDesc
->TimeDateStamp
, get_time_str(importDesc
->TimeDateStamp
));
429 printf(" ForwarderChain: %08lX\n", importDesc
->ForwarderChain
);
430 printf(" First thunk RVA: %08lX (delta: %u 0x%x)\n",
431 (DWORD
)importDesc
->FirstThunk
, -1, -1); /* FIXME */
433 printf(" Ordn Name\n");
435 il
= (importDesc
->u
.OriginalFirstThunk
!= 0) ?
436 RVA((DWORD
)importDesc
->u
.OriginalFirstThunk
, sizeof(DWORD
)) :
437 RVA((DWORD
)importDesc
->FirstThunk
, sizeof(DWORD
));
439 if (!il
) {printf("Can't grab thunk data, going to next imported DLL\n"); continue;}
441 for (; il
->u1
.Ordinal
; il
++)
443 if (IMAGE_SNAP_BY_ORDINAL(il
->u1
.Ordinal
))
445 printf(" %4lu <by ordinal>\n", IMAGE_ORDINAL(il
->u1
.Ordinal
));
449 iibn
= RVA((DWORD
)il
->u1
.AddressOfData
, sizeof(DWORD
));
452 printf("Can't grab import by name info, skipping to next ordinal\n");
456 printf(" %4u %s %lx\n", iibn
->Hint
, iibn
->Name
, (DWORD
)il
->u1
.AddressOfData
);
466 static void dump_dir_debug_dir(IMAGE_DEBUG_DIRECTORY
* idd
, int idx
)
470 printf("Directory %02u\n", idx
+ 1);
471 printf(" Characteristics: %08lX\n", idd
->Characteristics
);
472 printf(" TimeDateStamp: %08lX %s\n",
473 idd
->TimeDateStamp
, get_time_str(idd
->TimeDateStamp
));
474 printf(" Version %u.%02u\n", idd
->MajorVersion
, idd
->MinorVersion
);
478 case IMAGE_DEBUG_TYPE_UNKNOWN
: str
= "UNKNOWN"; break;
479 case IMAGE_DEBUG_TYPE_COFF
: str
= "COFF"; break;
480 case IMAGE_DEBUG_TYPE_CODEVIEW
: str
= "CODEVIEW"; break;
481 case IMAGE_DEBUG_TYPE_FPO
: str
= "FPO"; break;
482 case IMAGE_DEBUG_TYPE_MISC
: str
= "MISC"; break;
483 case IMAGE_DEBUG_TYPE_EXCEPTION
: str
= "EXCEPTION"; break;
484 case IMAGE_DEBUG_TYPE_FIXUP
: str
= "FIXUP"; break;
485 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC
: str
= "OMAP_TO_SRC"; break;
486 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
:str
= "OMAP_FROM_SRC"; break;
487 case IMAGE_DEBUG_TYPE_BORLAND
: str
= "BORLAND"; break;
488 case IMAGE_DEBUG_TYPE_RESERVED10
: str
= "RESERVED10"; break;
490 printf(" Type: %lu (%s)\n", idd
->Type
, str
);
491 printf(" SizeOfData: %lu\n", idd
->SizeOfData
);
492 printf(" AddressOfRawData: %08lX\n", idd
->AddressOfRawData
);
493 printf(" PointerToRawData: %08lX\n", idd
->PointerToRawData
);
497 case IMAGE_DEBUG_TYPE_UNKNOWN
:
499 case IMAGE_DEBUG_TYPE_COFF
:
500 dump_coff(idd
->PointerToRawData
, idd
->SizeOfData
);
502 case IMAGE_DEBUG_TYPE_CODEVIEW
:
503 dump_codeview(idd
->PointerToRawData
, idd
->SizeOfData
);
505 case IMAGE_DEBUG_TYPE_FPO
:
506 dump_frame_pointer_omission(idd
->PointerToRawData
, idd
->SizeOfData
);
508 case IMAGE_DEBUG_TYPE_MISC
:
510 IMAGE_DEBUG_MISC
* misc
= PRD(idd
->PointerToRawData
, idd
->SizeOfData
);
511 if (!misc
) {printf("Can't get misc debug information\n"); break;}
512 printf(" DataType: %lu (%s)\n",
514 (misc
->DataType
== IMAGE_DEBUG_MISC_EXENAME
) ? "Exe name" : "Unknown");
515 printf(" Length: %lu\n", misc
->Length
);
516 printf(" Unicode: %s\n", misc
->Unicode
? "Yes" : "No");
517 printf(" Data: %s\n", misc
->Data
);
520 case IMAGE_DEBUG_TYPE_EXCEPTION
:
522 case IMAGE_DEBUG_TYPE_FIXUP
:
524 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC
:
526 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
:
528 case IMAGE_DEBUG_TYPE_BORLAND
:
530 case IMAGE_DEBUG_TYPE_RESERVED10
:
536 static void dump_dir_debug(void)
538 IMAGE_DEBUG_DIRECTORY
* debugDir
= get_dir(IMAGE_FILE_DEBUG_DIRECTORY
);
541 if (!debugDir
) return;
542 nb_dbg
= PE_nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_DEBUG_DIRECTORY
].Size
/
546 printf("Debug Table (%u directories)\n", nb_dbg
);
548 for (i
= 0; i
< nb_dbg
; i
++)
550 dump_dir_debug_dir(debugDir
, i
);
556 static void dump_separate_dbg(void)
558 IMAGE_SEPARATE_DEBUG_HEADER
*separateDebugHead
= PRD(0, sizeof(separateDebugHead
));
561 IMAGE_DEBUG_DIRECTORY
* debugDir
;
563 if (!separateDebugHead
) {printf("Can't grab the separate header, aborting\n"); return;}
565 printf ("Signature: %.2s (0x%4X)\n",
566 (char*)&separateDebugHead
->Signature
, separateDebugHead
->Signature
);
567 printf ("Flags: 0x%04X\n", separateDebugHead
->Flags
);
568 printf ("Machine: 0x%04X (%s)\n",
569 separateDebugHead
->Machine
, get_machine_str(separateDebugHead
->Machine
));
570 printf ("Characteristics: 0x%04X\n", separateDebugHead
->Characteristics
);
571 printf ("TimeDateStamp: 0x%08lX (%s)\n",
572 separateDebugHead
->TimeDateStamp
, get_time_str(separateDebugHead
->TimeDateStamp
));
573 printf ("CheckSum: 0x%08lX\n", separateDebugHead
->CheckSum
);
574 printf ("ImageBase: 0x%08lX\n", separateDebugHead
->ImageBase
);
575 printf ("SizeOfImage: 0x%08lX\n", separateDebugHead
->SizeOfImage
);
576 printf ("NumberOfSections: 0x%08lX\n", separateDebugHead
->NumberOfSections
);
577 printf ("ExportedNamesSize: 0x%08lX\n", separateDebugHead
->ExportedNamesSize
);
578 printf ("DebugDirectorySize: 0x%08lX\n", separateDebugHead
->DebugDirectorySize
);
580 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER
),
581 separateDebugHead
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)))
582 {printf("Can't get the sections, aborting\n"); return;}
584 dump_sections(separateDebugHead
+ 1, separateDebugHead
->NumberOfSections
);
586 nb_dbg
= separateDebugHead
->DebugDirectorySize
/ sizeof(IMAGE_DEBUG_DIRECTORY
);
587 debugDir
= PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER
) +
588 separateDebugHead
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
589 separateDebugHead
->ExportedNamesSize
,
590 nb_dbg
* sizeof(IMAGE_DEBUG_DIRECTORY
));
591 if (!debugDir
) {printf("Couldn't get the debug directory info, aborting\n");return;}
593 printf("Debug Table (%u directories)\n", nb_dbg
);
595 for (i
= 0; i
< nb_dbg
; i
++)
597 dump_dir_debug_dir(debugDir
, i
);
602 static void dump_unicode_str( const WCHAR
*str
, int len
)
604 if (len
== -1) for (len
= 0; str
[len
]; len
++) ;
606 while (len
-- > 0 && *str
)
611 case '\n': printf( "\\n" ); break;
612 case '\r': printf( "\\r" ); break;
613 case '\t': printf( "\\t" ); break;
614 case '"': printf( "\\\"" ); break;
615 case '\\': printf( "\\\\" ); break;
617 if (c
>= ' ' && c
<= 126) putchar(c
);
618 else printf( "\\u%04x",c
);
624 static const char *get_resource_type( int id
)
626 static const char *types
[] =
654 if (id
< sizeof(types
)/sizeof(types
[0])) return types
[id
];
658 void dump_data( const unsigned char *ptr
, unsigned int size
, const char *prefix
)
662 printf( "%s", prefix
);
663 for (i
= 0; i
< size
; i
++)
665 printf( "%02x%c", ptr
[i
], (i
% 16 == 7) ? '-' : ' ' );
669 for (j
= 0; j
< 16; j
++)
670 printf( "%c", isprint(ptr
[i
-15+j
]) ? ptr
[i
-15+j
] : '.' );
671 if (i
< size
-1) printf( "\n%s", prefix
);
676 printf( "%*s ", 3 * (16-(i
%16)), "" );
677 for (j
= 0; j
< i
% 16; j
++)
678 printf( "%c", isprint(ptr
[i
-(i
%16)+j
]) ? ptr
[i
-(i
%16)+j
] : '.' );
683 static void dump_dir_resource(void)
685 const IMAGE_RESOURCE_DIRECTORY
*root
= get_dir(IMAGE_FILE_RESOURCE_DIRECTORY
);
686 const IMAGE_RESOURCE_DIRECTORY
*namedir
;
687 const IMAGE_RESOURCE_DIRECTORY
*langdir
;
688 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*e1
, *e2
, *e3
;
689 const IMAGE_RESOURCE_DIR_STRING_U
*string
;
690 const IMAGE_RESOURCE_DATA_ENTRY
*data
;
695 printf( "Resources:" );
697 for (i
= 0; i
< root
->NumberOfNamedEntries
+ root
->NumberOfIdEntries
; i
++)
699 e1
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(root
+ 1) + i
;
700 namedir
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ e1
->u2
.s3
.OffsetToDirectory
);
701 for (j
= 0; j
< namedir
->NumberOfNamedEntries
+ namedir
->NumberOfIdEntries
; j
++)
703 e2
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(namedir
+ 1) + j
;
704 langdir
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ e2
->u2
.s3
.OffsetToDirectory
);
705 for (k
= 0; k
< langdir
->NumberOfNamedEntries
+ langdir
->NumberOfIdEntries
; k
++)
707 e3
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(langdir
+ 1) + k
;
710 if (e1
->u1
.s1
.NameIsString
)
712 string
= (PIMAGE_RESOURCE_DIR_STRING_U
)((char *)root
+ e1
->u1
.s1
.NameOffset
);
713 dump_unicode_str( string
->NameString
, string
->Length
);
717 const char *type
= get_resource_type( e1
->u1
.s2
.Id
);
718 if (type
) printf( "%s", type
);
719 else printf( "%04x", e1
->u1
.s2
.Id
);
723 if (e2
->u1
.s1
.NameIsString
)
725 string
= (PIMAGE_RESOURCE_DIR_STRING_U
) ((char *)root
+ e2
->u1
.s1
.NameOffset
);
726 dump_unicode_str( string
->NameString
, string
->Length
);
729 printf( "%04x", e2
->u1
.s2
.Id
);
731 printf( " Language=%04x:\n", e3
->u1
.s2
.Id
);
732 data
= (IMAGE_RESOURCE_DATA_ENTRY
*)((char *)root
+ e3
->u2
.OffsetToData
);
733 dump_data( RVA( data
->OffsetToData
, data
->Size
), data
->Size
, " " );
740 static void do_dump( enum FileSig sig
)
742 int all
= (globals
.dumpsect
!= NULL
) && strcmp(globals
.dumpsect
, "ALL") == 0;
746 ne_dump( PE_base
, PE_total_len
);
750 if (globals
.do_dumpheader
)
753 /* FIXME: should check ptr */
754 dump_sections((char*)PE_nt_headers
+ sizeof(DWORD
) +
755 sizeof(IMAGE_FILE_HEADER
) + PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
,
756 PE_nt_headers
->FileHeader
.NumberOfSections
);
758 else if (!globals
.dumpsect
)
760 /* show at least something here */
764 if (globals
.dumpsect
)
766 if (all
|| !strcmp(globals
.dumpsect
, "import"))
767 dump_dir_imported_functions();
768 if (all
|| !strcmp(globals
.dumpsect
, "export"))
769 dump_dir_exported_functions();
770 if (all
|| !strcmp(globals
.dumpsect
, "debug"))
772 if (all
|| !strcmp(globals
.dumpsect
, "resource"))
775 /* FIXME: not implemented yet */
776 if (all
|| !strcmp(globals
.dumpsect
, "reloc"))
782 static enum FileSig
check_headers(void)
786 IMAGE_DOS_HEADER
* dh
;
789 pw
= PRD(0, sizeof(WORD
));
790 if (!pw
) {printf("Can't get main signature, aborting\n"); return 0;}
794 case IMAGE_DOS_SIGNATURE
:
796 dh
= PRD(0, sizeof(IMAGE_DOS_HEADER
));
797 if (dh
&& dh
->e_lfanew
>= sizeof(*dh
)) /* reasonable DOS header ? */
799 /* the signature is the first DWORD */
800 pdw
= PRD(dh
->e_lfanew
, sizeof(DWORD
));
803 if (*pdw
== IMAGE_NT_SIGNATURE
)
805 PE_nt_headers
= PRD(dh
->e_lfanew
, sizeof(DWORD
));
808 else if (*(WORD
*)pdw
== IMAGE_OS2_SIGNATURE
)
814 printf("No PE Signature found\n");
819 printf("Can't get the extented signature, aborting\n");
823 case 0x4944: /* "DI" */
827 printf("No known main signature (%.2s/%x), aborting\n", (char*)pw
, *pw
);
834 int pe_analysis(const char* name
, void (*fn
)(enum FileSig
), enum FileSig wanted_sig
)
837 enum FileSig effective_sig
;
841 setbuf(stdout
, NULL
);
843 fd
= open(name
, O_RDONLY
| O_BINARY
);
844 if (fd
== -1) fatal("Can't open file");
846 if (fstat(fd
, &s
) < 0) fatal("Can't get size");
847 PE_total_len
= s
.st_size
;
850 if ((PE_base
= mmap(NULL
, PE_total_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0)) == (void *)-1)
853 if (!(PE_base
= malloc( PE_total_len
))) fatal( "Out of memory" );
854 if (read( fd
, PE_base
, PE_total_len
) != PE_total_len
) fatal( "Cannot read file" );
857 effective_sig
= check_headers();
859 if (effective_sig
== SIG_UNKNOWN
)
861 printf("Can't get a recognized file signature, aborting\n");
864 else if (wanted_sig
== SIG_UNKNOWN
|| wanted_sig
== effective_sig
)
866 switch (effective_sig
)
868 case SIG_UNKNOWN
: /* shouldn't happen... */
872 printf("Contents of \"%s\": %ld bytes\n\n", name
, PE_total_len
);
873 (*fn
)(effective_sig
);
884 printf("Can't get a suitable file signature, aborting\n");
888 if (ret
) printf("Done dumping %s\n", name
);
890 if (munmap(PE_base
, PE_total_len
) == -1)
900 void dump_file(const char* name
)
902 pe_analysis(name
, do_dump
, SIG_UNKNOWN
);
906 int main(int argc
, char* argv
[])
908 if (argc
!= 2) fatal("usage");
909 pe_analysis(argv
[1], do_dump
);
913 typedef struct _dll_symbol
{
918 static dll_symbol
*dll_symbols
= NULL
;
919 static dll_symbol
*dll_current_symbol
= NULL
;
921 /* Compare symbols by ordinal for qsort */
922 static int symbol_cmp(const void *left
, const void *right
)
924 return ((dll_symbol
*)left
)->ordinal
> ((dll_symbol
*)right
)->ordinal
;
927 /*******************************************************************
930 * Free resources used by DLL
932 /* FIXME: Not used yet
933 static void dll_close (void)
940 for (ds = dll_symbols; ds->symbol; ds++)
947 static void do_grab_sym( enum FileSig sig
)
949 IMAGE_EXPORT_DIRECTORY
*exportDir
= get_dir(IMAGE_FILE_EXPORT_DIRECTORY
);
957 if (!exportDir
) return;
959 pName
= RVA(exportDir
->AddressOfNames
, exportDir
->NumberOfNames
* sizeof(DWORD
));
960 if (!pName
) {printf("Can't grab functions' name table\n"); return;}
961 pOrdl
= RVA(exportDir
->AddressOfNameOrdinals
, exportDir
->NumberOfNames
* sizeof(WORD
));
962 if (!pOrdl
) {printf("Can't grab functions' ordinal table\n"); return;}
966 if (!(dll_symbols
= (dll_symbol
*) malloc((exportDir
->NumberOfFunctions
+ 1) *
967 sizeof (dll_symbol
))))
968 fatal ("Out of memory");
969 if (exportDir
->AddressOfFunctions
!= exportDir
->NumberOfNames
|| exportDir
->Base
> 1)
970 globals
.do_ordinals
= 1;
972 /* bit map of used funcs */
973 map
= calloc(((exportDir
->NumberOfFunctions
+ 31) & ~31) / 32, sizeof(DWORD
));
974 if (!map
) fatal("no memory");
976 for (j
= 0; j
< exportDir
->NumberOfNames
; j
++, pOrdl
++)
978 map
[*pOrdl
/ 32] |= 1 << (*pOrdl
% 32);
979 ptr
= RVA(*pName
++, sizeof(DWORD
));
980 if (!ptr
) ptr
= "cant_get_function";
981 dll_symbols
[j
].symbol
= strdup(ptr
);
982 dll_symbols
[j
].ordinal
= -1; /* indicate non-ordinal symbol */
983 assert(dll_symbols
[j
].symbol
);
985 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
986 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
988 for (i
= 0; i
< exportDir
->NumberOfFunctions
; i
++)
990 if (pFunc
[i
] && !(map
[i
/ 32] & (1 << (i
% 32))))
992 char ordinal_text
[256];
993 /* Ordinal only entry */
994 snprintf (ordinal_text
, sizeof(ordinal_text
), "%s_%lu",
995 globals
.forward_dll
? globals
.forward_dll
: OUTPUT_UC_DLL_NAME
,
996 exportDir
->Base
+ i
);
997 str_toupper(ordinal_text
);
998 dll_symbols
[j
].symbol
= strdup(ordinal_text
);
999 assert(dll_symbols
[j
].symbol
);
1000 dll_symbols
[j
].ordinal
= exportDir
->Base
+ i
;
1002 assert(j
<= exportDir
->NumberOfFunctions
);
1008 printf("%lu named symbols in DLL, %lu total\n",
1009 exportDir
->NumberOfNames
, exportDir
->NumberOfFunctions
);
1011 qsort( dll_symbols
, exportDir
->NumberOfFunctions
, sizeof(dll_symbol
), symbol_cmp
);
1013 dll_symbols
[exportDir
->NumberOfFunctions
].symbol
= NULL
;
1015 dll_current_symbol
= dll_symbols
;
1018 /*******************************************************************
1021 * Open a DLL and read in exported symbols
1023 void dll_open (const char *dll_name
)
1025 pe_analysis(dll_name
, do_grab_sym
, SIG_PE
);
1028 /*******************************************************************
1031 * Get next exported symbol from dll
1033 int dll_next_symbol (parsed_symbol
* sym
)
1035 if (!dll_current_symbol
->symbol
)
1038 assert (dll_symbols
);
1040 sym
->symbol
= strdup (dll_current_symbol
->symbol
);
1041 sym
->ordinal
= dll_current_symbol
->ordinal
;
1042 dll_current_symbol
++;