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
25 #include <sys/types.h>
35 static unsigned long total_len
;
36 static IMAGE_NT_HEADERS
* nt_headers
;
38 enum FileSig
{SIG_UNKNOWN
, SIG_DOS
, SIG_PE
, SIG_DBG
};
40 char* get_time_str(DWORD _t
)
42 time_t t
= (time_t)_t
;
45 /* FIXME: I don't get the same values from MS' pedump running under Wine...
46 * I wonder if Wine isn't broken wrt to GMT settings...
48 strncpy(buf
, ctime(&t
), sizeof(buf
));
49 buf
[sizeof(buf
) - 1] = '\0';
50 if (buf
[strlen(buf
)-1] == '\n')
51 buf
[strlen(buf
)-1] = '\0';
55 static const char* get_machine_str(DWORD mach
)
59 case IMAGE_FILE_MACHINE_UNKNOWN
: return "Unknown";
60 case IMAGE_FILE_MACHINE_I860
: return "i860";
61 case IMAGE_FILE_MACHINE_I386
: return "i386";
62 case IMAGE_FILE_MACHINE_R3000
: return "R3000";
63 case IMAGE_FILE_MACHINE_R4000
: return "R4000";
64 case IMAGE_FILE_MACHINE_R10000
: return "R10000";
65 case IMAGE_FILE_MACHINE_ALPHA
: return "Alpha";
66 case IMAGE_FILE_MACHINE_POWERPC
: return "PowerPC";
71 void* PRD(unsigned long prd
, unsigned long len
)
73 return (prd
+ len
> total_len
) ? NULL
: (char*)base
+ prd
;
76 unsigned long Offset(void* ptr
)
78 if (ptr
< base
) {printf("<<<<<ptr below\n");return 0;}
79 if (ptr
>= base
+ total_len
) {printf("<<<<<ptr above\n");return 0;}
83 void* RVA(unsigned long rva
, unsigned long len
)
85 IMAGE_SECTION_HEADER
* sectHead
;
88 sectHead
= (IMAGE_SECTION_HEADER
*)((char*)nt_headers
+ sizeof(DWORD
) +
89 sizeof(IMAGE_FILE_HEADER
) +
90 nt_headers
->FileHeader
.SizeOfOptionalHeader
);
92 if (rva
== 0) return NULL
;
94 for (i
= nt_headers
->FileHeader
.NumberOfSections
- 1; i
>= 0; i
--)
96 if (sectHead
[i
].VirtualAddress
<= rva
&&
97 rva
+ len
<= (DWORD
)sectHead
[i
].VirtualAddress
+ sectHead
[i
].SizeOfRawData
)
103 printf("rva not found in any section (%lu)\n", rva
);
107 /* return image import directory offset */
108 return PRD(sectHead
[i
].PointerToRawData
+ rva
- sectHead
[i
].VirtualAddress
, len
);
111 static void* get_dir(unsigned idx
)
113 if (idx
>= nt_headers
->OptionalHeader
.NumberOfRvaAndSizes
)
115 return RVA(nt_headers
->OptionalHeader
.DataDirectory
[idx
].VirtualAddress
,
116 nt_headers
->OptionalHeader
.DataDirectory
[idx
].Size
);
119 static const char* DirectoryNames
[16] = {
120 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
121 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
122 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
123 "IAT", "Delay IAT", "COM Descript", ""
126 static void dump_pe_header(void)
129 IMAGE_FILE_HEADER
*fileHeader
;
130 IMAGE_OPTIONAL_HEADER
*optionalHeader
;
133 printf("File Header\n");
134 fileHeader
= &nt_headers
->FileHeader
;
136 printf(" Machine: %04X (%s)\n",
137 fileHeader
->Machine
, get_machine_str(fileHeader
->Machine
));
138 printf(" Number of Sections: %d\n", fileHeader
->NumberOfSections
);
139 printf(" TimeDateStamp: %08lX (%s) offset %ld\n",
140 fileHeader
->TimeDateStamp
, get_time_str(fileHeader
->TimeDateStamp
),
141 Offset(&(fileHeader
->TimeDateStamp
)));
142 printf(" PointerToSymbolTable: %08lX\n", fileHeader
->PointerToSymbolTable
);
143 printf(" NumberOfSymbols: %08lX\n", fileHeader
->NumberOfSymbols
);
144 printf(" SizeOfOptionalHeader: %04X\n", fileHeader
->SizeOfOptionalHeader
);
145 printf(" Characteristics: %04X\n", fileHeader
->Characteristics
);
146 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
147 X(IMAGE_FILE_RELOCS_STRIPPED
, "RELOCS_STRIPPED");
148 X(IMAGE_FILE_EXECUTABLE_IMAGE
, "EXECUTABLE_IMAGE");
149 X(IMAGE_FILE_LINE_NUMS_STRIPPED
, "LINE_NUMS_STRIPPED");
150 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED
, "LOCAL_SYMS_STRIPPED");
151 X(IMAGE_FILE_16BIT_MACHINE
, "16BIT_MACHINE");
152 X(IMAGE_FILE_BYTES_REVERSED_LO
, "BYTES_REVERSED_LO");
153 X(IMAGE_FILE_32BIT_MACHINE
, "32BIT_MACHINE");
154 X(IMAGE_FILE_DEBUG_STRIPPED
, "DEBUG_STRIPPED");
155 X(IMAGE_FILE_SYSTEM
, "SYSTEM");
156 X(IMAGE_FILE_DLL
, "DLL");
157 X(IMAGE_FILE_BYTES_REVERSED_HI
, "BYTES_REVERSED_HI");
161 /* hope we have the right size */
162 printf("Optional Header\n");
163 optionalHeader
= &nt_headers
->OptionalHeader
;
164 printf(" Magic 0x%-4X %u\n",
165 optionalHeader
->Magic
, optionalHeader
->Magic
);
166 printf(" linker version %u.%02u\n",
167 optionalHeader
->MajorLinkerVersion
, optionalHeader
->MinorLinkerVersion
);
168 printf(" size of code 0x%-8lx %lu\n",
169 optionalHeader
->SizeOfCode
, optionalHeader
->SizeOfCode
);
170 printf(" size of initialized data 0x%-8lx %lu\n",
171 optionalHeader
->SizeOfInitializedData
, optionalHeader
->SizeOfInitializedData
);
172 printf(" size of uninitialized data 0x%-8lx %lu\n",
173 optionalHeader
->SizeOfUninitializedData
, optionalHeader
->SizeOfUninitializedData
);
174 printf(" entrypoint RVA 0x%-8lx %lu\n",
175 optionalHeader
->AddressOfEntryPoint
, optionalHeader
->AddressOfEntryPoint
);
176 printf(" base of code 0x%-8lx %lu\n",
177 optionalHeader
->BaseOfCode
, optionalHeader
->BaseOfCode
);
178 printf(" base of data 0x%-8lX %lu\n",
179 optionalHeader
->BaseOfData
, optionalHeader
->BaseOfData
);
180 printf(" image base 0x%-8lX %lu\n",
181 optionalHeader
->ImageBase
, optionalHeader
->ImageBase
);
182 printf(" section align 0x%-8lx %lu\n",
183 optionalHeader
->SectionAlignment
, optionalHeader
->SectionAlignment
);
184 printf(" file align 0x%-8lx %lu\n",
185 optionalHeader
->FileAlignment
, optionalHeader
->FileAlignment
);
186 printf(" required OS version %u.%02u\n",
187 optionalHeader
->MajorOperatingSystemVersion
, optionalHeader
->MinorOperatingSystemVersion
);
188 printf(" image version %u.%02u\n",
189 optionalHeader
->MajorImageVersion
, optionalHeader
->MinorImageVersion
);
190 printf(" subsystem version %u.%02u\n",
191 optionalHeader
->MajorSubsystemVersion
, optionalHeader
->MinorSubsystemVersion
);
192 printf(" Win32 Version 0x%lX\n", optionalHeader
->Win32VersionValue
);
193 printf(" size of image 0x%-8lx %lu\n",
194 optionalHeader
->SizeOfImage
, optionalHeader
->SizeOfImage
);
195 printf(" size of headers 0x%-8lx %lu\n",
196 optionalHeader
->SizeOfHeaders
, optionalHeader
->SizeOfHeaders
);
197 printf(" checksum 0x%lX\n", optionalHeader
->CheckSum
);
198 switch (optionalHeader
->Subsystem
)
201 case IMAGE_SUBSYSTEM_UNKNOWN
: str
= "Unknown"; break;
202 case IMAGE_SUBSYSTEM_NATIVE
: str
= "Native"; break;
203 case IMAGE_SUBSYSTEM_WINDOWS_GUI
: str
= "Windows GUI"; break;
204 case IMAGE_SUBSYSTEM_WINDOWS_CUI
: str
= "Windows CUI"; break;
205 case IMAGE_SUBSYSTEM_OS2_CUI
: str
= "OS/2 CUI"; break;
206 case IMAGE_SUBSYSTEM_POSIX_CUI
: str
= "Posix CUI"; break;
208 printf(" Subsystem 0x%X (%s)\n", optionalHeader
->Subsystem
, str
);
209 printf(" DLL flags 0x%X\n", optionalHeader
->DllCharacteristics
);
210 printf(" stack reserve size 0x%-8lx %lu\n",
211 optionalHeader
->SizeOfStackReserve
, optionalHeader
->SizeOfStackReserve
);
212 printf(" stack commit size 0x%-8lx %lu\n",
213 optionalHeader
->SizeOfStackCommit
, optionalHeader
->SizeOfStackCommit
);
214 printf(" heap reserve size 0x%-8lx %lu\n",
215 optionalHeader
->SizeOfHeapReserve
, optionalHeader
->SizeOfHeapReserve
);
216 printf(" heap commit size 0x%-8lx %lu\n",
217 optionalHeader
->SizeOfHeapCommit
, optionalHeader
->SizeOfHeapCommit
);
218 printf(" loader flags 0x%lX\n", optionalHeader
->LoaderFlags
);
219 printf(" RVAs & sizes 0x%lX\n", optionalHeader
->NumberOfRvaAndSizes
);
222 printf("Data Directory\n");
223 printf("%ld\n", optionalHeader
->NumberOfRvaAndSizes
* sizeof(IMAGE_DATA_DIRECTORY
));
225 for (i
= 0; i
< optionalHeader
->NumberOfRvaAndSizes
&& i
< 16; i
++)
227 printf(" %-12s rva: 0x%-8lX size: %8lu\n",
228 DirectoryNames
[i
], optionalHeader
->DataDirectory
[i
].VirtualAddress
,
229 optionalHeader
->DataDirectory
[i
].Size
);
234 static void dump_sections(void* addr
, unsigned num_sect
)
236 IMAGE_SECTION_HEADER
* sectHead
= addr
;
239 printf("Section Table\n");
240 for (i
= 0; i
< num_sect
; i
++, sectHead
++)
242 printf(" %02d %-8s VirtSize: %-8lu VirtAddr: %-8lu 0x%08lx\n",
243 i
+ 1, sectHead
->Name
, sectHead
->Misc
.VirtualSize
, sectHead
->VirtualAddress
,
244 sectHead
->VirtualAddress
);
245 printf(" raw data offs: %-8lu raw data size: %-8lu\n",
246 sectHead
->PointerToRawData
, sectHead
->SizeOfRawData
);
247 printf(" relocation offs: %-8lu relocations: %-8u\n",
248 sectHead
->PointerToRelocations
, sectHead
->NumberOfRelocations
);
249 printf(" line # offs: %-8lu line #'s: %-8u\n",
250 sectHead
->PointerToLinenumbers
, sectHead
->NumberOfLinenumbers
);
251 printf(" characteristics: 0x$%08lx\n", sectHead
->Characteristics
);
253 #define X(b,s) if (sectHead->Characteristics & b) printf(s " ")
254 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
255 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
256 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
257 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
258 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
259 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
261 X(IMAGE_SCN_CNT_CODE
, "CODE");
262 X(IMAGE_SCN_CNT_INITIALIZED_DATA
, "INITIALIZED_DATA");
263 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA
, "UNINITIALIZED_DATA");
265 X(IMAGE_SCN_LNK_OTHER
, "LNK_OTHER");
266 X(IMAGE_SCN_LNK_INFO
, "LNK_INFO");
267 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
268 X(IMAGE_SCN_LNK_REMOVE
, "LNK_REMOVE");
269 X(IMAGE_SCN_LNK_COMDAT
, "LNK_COMDAT");
271 /* 0x00002000 - Reserved */
272 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
273 X(IMAGE_SCN_MEM_FARDATA
, "MEM_FARDATA");
275 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
276 X(IMAGE_SCN_MEM_PURGEABLE
, "MEM_PURGEABLE");
277 X(IMAGE_SCN_MEM_16BIT
, "MEM_16BIT");
278 X(IMAGE_SCN_MEM_LOCKED
, "MEM_LOCKED");
279 X(IMAGE_SCN_MEM_PRELOAD
, "MEM_PRELOAD");
281 X(IMAGE_SCN_ALIGN_1BYTES
, "ALIGN_1BYTES");
282 X(IMAGE_SCN_ALIGN_2BYTES
, "ALIGN_2BYTES");
283 X(IMAGE_SCN_ALIGN_4BYTES
, "ALIGN_4BYTES");
284 X(IMAGE_SCN_ALIGN_8BYTES
, "ALIGN_8BYTES");
285 X(IMAGE_SCN_ALIGN_16BYTES
, "ALIGN_16BYTES");
286 X(IMAGE_SCN_ALIGN_32BYTES
, "ALIGN_32BYTES");
287 X(IMAGE_SCN_ALIGN_64BYTES
, "ALIGN_64BYTES");
288 /* 0x00800000 - Unused */
290 X(IMAGE_SCN_LNK_NRELOC_OVFL
, "LNK_NRELOC_OVFL");
292 X(IMAGE_SCN_MEM_DISCARDABLE
, "MEM_DISCARDABLE");
293 X(IMAGE_SCN_MEM_NOT_CACHED
, "MEM_NOT_CACHED");
294 X(IMAGE_SCN_MEM_NOT_PAGED
, "MEM_NOT_PAGED");
295 X(IMAGE_SCN_MEM_SHARED
, "MEM_SHARED");
296 X(IMAGE_SCN_MEM_EXECUTE
, "MEM_EXECUTE");
297 X(IMAGE_SCN_MEM_READ
, "MEM_READ");
298 X(IMAGE_SCN_MEM_WRITE
, "MEM_WRITE");
305 static void dump_dir_exported_functions(void)
307 IMAGE_EXPORT_DIRECTORY
*exportDir
= get_dir(IMAGE_FILE_EXPORT_DIRECTORY
);
313 parsed_symbol symbol
;
315 if (!exportDir
) return;
317 printf("Exports table:\n");
319 printf(" Name: %s\n", (char*)RVA(exportDir
->Name
, sizeof(DWORD
)));
320 printf(" Characteristics: %08lx\n", exportDir
->Characteristics
);
321 printf(" TimeDateStamp: %08lX %s\n",
322 exportDir
->TimeDateStamp
, get_time_str(exportDir
->TimeDateStamp
));
323 printf(" Version: %u.%02u\n", exportDir
->MajorVersion
, exportDir
->MinorVersion
);
324 printf(" Ordinal base: %lu\n", exportDir
->Base
);
325 printf(" # of functions: %lu\n", exportDir
->NumberOfFunctions
);
326 printf(" # of Names: %lu\n", exportDir
->NumberOfNames
);
327 printf("Adresses of functions: %08lX\n", exportDir
->AddressOfFunctions
);
328 printf("Adresses of name ordinals: %08lX\n", exportDir
->AddressOfNameOrdinals
);
329 printf("Adresses of names: %08lX\n", exportDir
->AddressOfNames
);
331 printf(" Entry Pt Ordn Name\n");
333 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
334 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
335 pName
= RVA(exportDir
->AddressOfNames
, exportDir
->NumberOfNames
* sizeof(DWORD
));
336 if (!pName
) {printf("Can't grab functions' name table\n"); return;}
337 pOrdl
= RVA(exportDir
->AddressOfNameOrdinals
, exportDir
->NumberOfNames
* sizeof(WORD
));
338 if (!pOrdl
) {printf("Can't grab functions' ordinal table\n"); return;}
340 /* bit map of used funcs */
341 map
= calloc(((exportDir
->NumberOfFunctions
+ 31) & ~31) / 32, sizeof(DWORD
));
342 if (!map
) fatal("no memory");
344 for (i
= 0; i
< exportDir
->NumberOfNames
; i
++, pName
++, pOrdl
++)
348 map
[*pOrdl
/ 32] |= 1 << (*pOrdl
% 32);
350 name
= (char*)RVA(*pName
, sizeof(DWORD
));
351 if (name
&& globals
.do_demangle
)
353 printf(" %08lX %4lu ", pFunc
[*pOrdl
], exportDir
->Base
+ *pOrdl
);
355 symbol_init(&symbol
, name
);
356 if (symbol_demangle(&symbol
) == -1)
358 else if (symbol
.flags
& SYM_DATA
)
359 printf(symbol
.arg_text
[0]);
361 output_prototype(stdout
, &symbol
);
363 symbol_clear(&symbol
);
367 printf(" %08lX %4lu %s\n", pFunc
[*pOrdl
], exportDir
->Base
+ *pOrdl
, name
);
370 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
371 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
372 for (i
= 0; i
< exportDir
->NumberOfFunctions
; i
++)
374 if (pFunc
[i
] && !(map
[i
/ 32] & (1 << (i
% 32))))
376 printf(" %08lX %4lu <by ordinal>\n", pFunc
[i
], exportDir
->Base
+ i
);
383 static void dump_dir_imported_functions(void)
385 IMAGE_IMPORT_DESCRIPTOR
*importDesc
= get_dir(IMAGE_FILE_IMPORT_DIRECTORY
);
388 if (!importDesc
) return;
389 nb_imp
= nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
].Size
/
393 printf("Import Table size: %lu\n",
394 nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
].Size
);/* FIXME */
396 for (i
= 0; i
< nb_imp
- 1; i
++) /* the last descr is set as 0 as a sentinel */
398 IMAGE_THUNK_DATA
* il
;
399 IMAGE_IMPORT_BY_NAME
* iibn
;
401 if (!importDesc
->Name
||
402 (importDesc
->u
.OriginalFirstThunk
== NULL
&& importDesc
->FirstThunk
== NULL
))
405 printf("<<<<<<<null entry\n");
408 printf(" offset %lu %s\n", Offset(importDesc
), (char*)RVA(importDesc
->Name
, sizeof(DWORD
)));
409 printf(" Hint/Name Table: %08lX\n", (DWORD
)importDesc
->u
.OriginalFirstThunk
);
410 printf(" TimeDataStamp: %08lX (%s)\n",
411 importDesc
->TimeDateStamp
, get_time_str(importDesc
->TimeDateStamp
));
412 printf(" ForwarderChain: %08lX\n", importDesc
->ForwarderChain
);
413 printf(" First thunk RVA: %08lX (delta: %u 0x%x)\n",
414 (DWORD
)importDesc
->FirstThunk
, -1, -1); /* FIXME */
416 printf(" Ordn Name\n");
418 il
= (importDesc
->u
.OriginalFirstThunk
!= 0) ?
419 RVA((DWORD
)importDesc
->u
.OriginalFirstThunk
, sizeof(DWORD
)) :
420 RVA((DWORD
)importDesc
->FirstThunk
, sizeof(DWORD
));
422 if (!il
) {printf("Can't grab thunk data, going to next imported DLL\n"); continue;}
424 for (; il
->u1
.Ordinal
; il
++)
426 if (IMAGE_SNAP_BY_ORDINAL(il
->u1
.Ordinal
))
428 printf(" %4lu <by ordinal>\n", IMAGE_ORDINAL(il
->u1
.Ordinal
));
432 iibn
= RVA((DWORD
)il
->u1
.AddressOfData
, sizeof(DWORD
));
435 printf("Can't grab import by name info, skipping to next ordinal\n");
439 printf(" %4u %s %lx\n", iibn
->Hint
, iibn
->Name
, (DWORD
)il
->u1
.AddressOfData
);
449 static void dump_dir_debug_dir(IMAGE_DEBUG_DIRECTORY
* idd
, int idx
)
453 printf("Directory %02u\n", idx
+ 1);
454 printf(" Characteristics: %08lX\n", idd
->Characteristics
);
455 printf(" TimeDateStamp: %08lX %s\n",
456 idd
->TimeDateStamp
, get_time_str(idd
->TimeDateStamp
));
457 printf(" Version %u.%02u\n", idd
->MajorVersion
, idd
->MinorVersion
);
461 case IMAGE_DEBUG_TYPE_UNKNOWN
: str
= "UNKNOWN"; break;
462 case IMAGE_DEBUG_TYPE_COFF
: str
= "COFF"; break;
463 case IMAGE_DEBUG_TYPE_CODEVIEW
: str
= "CODEVIEW"; break;
464 case IMAGE_DEBUG_TYPE_FPO
: str
= "FPO"; break;
465 case IMAGE_DEBUG_TYPE_MISC
: str
= "MISC"; break;
466 case IMAGE_DEBUG_TYPE_EXCEPTION
: str
= "EXCEPTION"; break;
467 case IMAGE_DEBUG_TYPE_FIXUP
: str
= "FIXUP"; break;
468 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC
: str
= "OMAP_TO_SRC"; break;
469 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
:str
= "OMAP_FROM_SRC"; break;
470 case IMAGE_DEBUG_TYPE_BORLAND
: str
= "BORLAND"; break;
471 case IMAGE_DEBUG_TYPE_RESERVED10
: str
= "RESERVED10"; break;
473 printf(" Type: %lu (%s)\n", idd
->Type
, str
);
474 printf(" SizeOfData: %lu\n", idd
->SizeOfData
);
475 printf(" AddressOfRawData: %08lX\n", idd
->AddressOfRawData
);
476 printf(" PointerToRawData: %08lX\n", idd
->PointerToRawData
);
480 case IMAGE_DEBUG_TYPE_UNKNOWN
:
482 case IMAGE_DEBUG_TYPE_COFF
:
484 case IMAGE_DEBUG_TYPE_CODEVIEW
:
485 dump_codeview(idd
->PointerToRawData
, idd
->SizeOfData
);
487 case IMAGE_DEBUG_TYPE_FPO
:
489 case IMAGE_DEBUG_TYPE_MISC
:
491 IMAGE_DEBUG_MISC
* misc
= PRD(idd
->PointerToRawData
, idd
->SizeOfData
);
492 if (!misc
) {printf("Can't get misc debug information\n"); break;}
493 printf(" DataType: %lu (%s)\n",
495 (misc
->DataType
== IMAGE_DEBUG_MISC_EXENAME
) ? "Exe name" : "Unknown");
496 printf(" Length: %lu\n", misc
->Length
);
497 printf(" Unicode: %s\n", misc
->Unicode
? "Yes" : "No");
498 printf(" Data: %s\n", misc
->Data
);
501 case IMAGE_DEBUG_TYPE_EXCEPTION
:
503 case IMAGE_DEBUG_TYPE_FIXUP
:
505 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC
:
507 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
:
509 case IMAGE_DEBUG_TYPE_BORLAND
:
511 case IMAGE_DEBUG_TYPE_RESERVED10
:
517 static void dump_dir_debug(void)
519 IMAGE_DEBUG_DIRECTORY
* debugDir
= get_dir(IMAGE_FILE_DEBUG_DIRECTORY
);
522 if (!debugDir
) return;
523 nb_dbg
= nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_DEBUG_DIRECTORY
].Size
/
527 printf("Debug Table (%u directories)\n", nb_dbg
);
529 for (i
= 0; i
< nb_dbg
; i
++)
531 dump_dir_debug_dir(debugDir
, i
);
537 static void dump_separate_dbg(void)
539 IMAGE_SEPARATE_DEBUG_HEADER
*separateDebugHead
= PRD(0, sizeof(separateDebugHead
));
542 IMAGE_DEBUG_DIRECTORY
* debugDir
;
544 if (!separateDebugHead
) {printf("Can't grab the separate header, aborting\n"); return;}
546 printf ("Signature: %.2s (0x%4X)\n",
547 (char*)&separateDebugHead
->Signature
, separateDebugHead
->Signature
);
548 printf ("Flags: 0x%04X\n", separateDebugHead
->Flags
);
549 printf ("Machine: 0x%04X (%s)\n",
550 separateDebugHead
->Machine
, get_machine_str(separateDebugHead
->Machine
));
551 printf ("Characteristics: 0x%04X\n", separateDebugHead
->Characteristics
);
552 printf ("TimeDateStamp: 0x%08lX (%s)\n",
553 separateDebugHead
->TimeDateStamp
, get_time_str(separateDebugHead
->TimeDateStamp
));
554 printf ("CheckSum: 0x%08lX\n", separateDebugHead
->CheckSum
);
555 printf ("ImageBase: 0x%08lX\n", separateDebugHead
->ImageBase
);
556 printf ("SizeOfImage: 0x%08lX\n", separateDebugHead
->SizeOfImage
);
557 printf ("NumberOfSections: 0x%08lX\n", separateDebugHead
->NumberOfSections
);
558 printf ("ExportedNamesSize: 0x%08lX\n", separateDebugHead
->ExportedNamesSize
);
559 printf ("DebugDirectorySize: 0x%08lX\n", separateDebugHead
->DebugDirectorySize
);
561 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER
),
562 separateDebugHead
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)))
563 {printf("Can't get the sections, aborting\n"); return;}
565 dump_sections(separateDebugHead
+ 1, separateDebugHead
->NumberOfSections
);
567 nb_dbg
= separateDebugHead
->DebugDirectorySize
/ sizeof(IMAGE_DEBUG_DIRECTORY
);
568 debugDir
= PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER
) +
569 separateDebugHead
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
570 separateDebugHead
->ExportedNamesSize
,
571 nb_dbg
* sizeof(IMAGE_DEBUG_DIRECTORY
));
572 if (!debugDir
) {printf("Couldn't get the debug directory info, aborting\n");return;}
574 printf("Debug Table (%u directories)\n", nb_dbg
);
576 for (i
= 0; i
< nb_dbg
; i
++)
578 dump_dir_debug_dir(debugDir
, i
);
583 static void dump_unicode_str( const WCHAR
*str
, int len
)
585 if (len
== -1) for (len
= 0; str
[len
]; len
++) ;
587 while (len
-- > 0 && *str
)
592 case '\n': printf( "\\n" ); break;
593 case '\r': printf( "\\r" ); break;
594 case '\t': printf( "\\t" ); break;
595 case '"': printf( "\\\"" ); break;
596 case '\\': printf( "\\\\" ); break;
598 if (c
>= ' ' && c
<= 126) putchar(c
);
599 else printf( "\\u%04x",c
);
605 static const char *get_resource_type( int id
)
607 static const char *types
[] =
635 if (id
< sizeof(types
)/sizeof(types
[0])) return types
[id
];
639 static void dump_data( const unsigned char *ptr
, unsigned int size
, const char *prefix
)
643 printf( "%s", prefix
);
644 for (i
= 0; i
< size
; i
++)
646 printf( "%02x%c", ptr
[i
], (i
% 16 == 7) ? '-' : ' ' );
650 for (j
= 0; j
< 16; j
++)
651 printf( "%c", isprint(ptr
[i
-15+j
]) ? ptr
[i
-15+j
] : '.' );
652 if (i
< size
-1) printf( "\n%s", prefix
);
657 printf( "%*s ", 3 * (16-(i
%16)), "" );
658 for (j
= 0; j
< i
% 16; j
++)
659 printf( "%c", isprint(ptr
[i
-(i
%16)+j
]) ? ptr
[i
-(i
%16)+j
] : '.' );
664 static void dump_dir_resource(void)
666 const IMAGE_RESOURCE_DIRECTORY
*root
= get_dir(IMAGE_FILE_RESOURCE_DIRECTORY
);
667 const IMAGE_RESOURCE_DIRECTORY
*namedir
;
668 const IMAGE_RESOURCE_DIRECTORY
*langdir
;
669 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*e1
, *e2
, *e3
;
670 const IMAGE_RESOURCE_DIR_STRING_U
*string
;
671 const IMAGE_RESOURCE_DATA_ENTRY
*data
;
676 printf( "Resources:" );
678 for (i
= 0; i
< root
->NumberOfNamedEntries
+ root
->NumberOfIdEntries
; i
++)
680 e1
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(root
+ 1) + i
;
681 namedir
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ e1
->u2
.s3
.OffsetToDirectory
);
682 for (j
= 0; j
< namedir
->NumberOfNamedEntries
+ namedir
->NumberOfIdEntries
; j
++)
684 e2
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(namedir
+ 1) + j
;
685 langdir
= (IMAGE_RESOURCE_DIRECTORY
*)((char *)root
+ e2
->u2
.s3
.OffsetToDirectory
);
686 for (k
= 0; k
< langdir
->NumberOfNamedEntries
+ langdir
->NumberOfIdEntries
; k
++)
688 e3
= (PIMAGE_RESOURCE_DIRECTORY_ENTRY
)(langdir
+ 1) + k
;
691 if (e1
->u1
.s1
.NameIsString
)
693 string
= (PIMAGE_RESOURCE_DIR_STRING_U
)((char *)root
+ e1
->u1
.s1
.NameOffset
);
694 dump_unicode_str( string
->NameString
, string
->Length
);
698 const char *type
= get_resource_type( e1
->u1
.s2
.Id
);
699 if (type
) printf( "%s", type
);
700 else printf( "%04x", e1
->u1
.s2
.Id
);
704 if (e2
->u1
.s1
.NameIsString
)
706 string
= (PIMAGE_RESOURCE_DIR_STRING_U
) ((char *)root
+ e2
->u1
.s1
.NameOffset
);
707 dump_unicode_str( string
->NameString
, string
->Length
);
710 printf( "%04x", e2
->u1
.s2
.Id
);
712 printf( " Language=%04x:\n", e3
->u1
.s2
.Id
);
713 data
= (IMAGE_RESOURCE_DATA_ENTRY
*)((char *)root
+ e3
->u2
.OffsetToData
);
714 dump_data( RVA( data
->OffsetToData
, data
->Size
), data
->Size
, " " );
721 static void do_dump(void)
723 int all
= (globals
.dumpsect
!= NULL
) && strcmp(globals
.dumpsect
, "ALL") == 0;
725 if (globals
.do_dumpheader
)
728 /* FIX%E: should check ptr */
729 dump_sections((char*)nt_headers
+ sizeof(DWORD
) +
730 sizeof(IMAGE_FILE_HEADER
) + nt_headers
->FileHeader
.SizeOfOptionalHeader
,
731 nt_headers
->FileHeader
.NumberOfSections
);
733 else if (!globals
.dumpsect
)
735 /* show at least something here */
739 if (globals
.dumpsect
)
741 if (all
|| !strcmp(globals
.dumpsect
, "import"))
742 dump_dir_imported_functions();
743 if (all
|| !strcmp(globals
.dumpsect
, "export"))
744 dump_dir_exported_functions();
745 if (all
|| !strcmp(globals
.dumpsect
, "debug"))
747 if (all
|| !strcmp(globals
.dumpsect
, "resource"))
750 /* FIXME: not implemented yet */
751 if (all
|| !strcmp(globals
.dumpsect
, "reloc"))
757 static enum FileSig
check_headers(void)
761 IMAGE_DOS_HEADER
* dh
;
764 pw
= PRD(0, sizeof(WORD
));
765 if (!pw
) {printf("Can't get main signature, aborting\n"); return 0;}
769 case IMAGE_DOS_SIGNATURE
:
771 dh
= PRD(0, sizeof(IMAGE_DOS_HEADER
));
772 if (dh
&& dh
->e_lfanew
>= sizeof(*dh
)) /* reasonable DOS header ? */
774 /* the signature is the first DWORD */
775 pdw
= PRD(dh
->e_lfanew
, sizeof(DWORD
));
778 if (*pdw
== IMAGE_NT_SIGNATURE
)
780 nt_headers
= PRD(dh
->e_lfanew
, sizeof(DWORD
));
785 printf("No PE Signature found\n");
790 printf("Can't get the extented signature, aborting\n");
794 case 0x4944: /* "DI" */
798 printf("No known main signature (%.2s/%x), aborting\n", (char*)pw
, *pw
);
805 int pe_analysis(const char* name
, void (*fn
)(void), enum FileSig wanted_sig
)
808 enum FileSig effective_sig
;
812 setbuf(stdout
, NULL
);
814 fd
= open(name
, O_RDONLY
);
815 if (fd
== -1) fatal("Can't open file");
817 if (fstat(fd
, &s
) < 0) fatal("Can't get size");
818 total_len
= s
.st_size
;
820 base
= mmap(NULL
, total_len
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
821 if (base
== (void*)-1) fatal("Can't map file");
823 effective_sig
= check_headers();
825 if (effective_sig
== SIG_UNKNOWN
)
827 printf("Can't get a recognized file signature, aborting\n");
830 else if (wanted_sig
== SIG_UNKNOWN
|| wanted_sig
== effective_sig
)
832 switch (effective_sig
)
834 case SIG_UNKNOWN
: /* shouldn't happen... */
837 printf("Contents of \"%s\": %ld bytes\n\n", name
, total_len
);
849 printf("Can't get a suitable file signature, aborting\n");
853 if (ret
) printf("Done dumping %s\n", name
);
854 munmap(base
, total_len
);
860 void dump_file(const char* name
)
862 pe_analysis(name
, do_dump
, SIG_UNKNOWN
);
866 int main(int argc
, char* argv
[])
868 if (argc
!= 2) fatal("usage");
869 pe_analysis(argv
[1], do_dump
);
873 typedef struct _dll_symbol
{
878 static dll_symbol
*dll_symbols
= NULL
;
879 static dll_symbol
*dll_current_symbol
= NULL
;
881 /* Compare symbols by ordinal for qsort */
882 static int symbol_cmp(const void *left
, const void *right
)
884 return ((dll_symbol
*)left
)->ordinal
> ((dll_symbol
*)right
)->ordinal
;
887 /*******************************************************************
890 * Free resources used by DLL
892 /* FIXME: Not used yet
893 static void dll_close (void)
900 for (ds = dll_symbols; ds->symbol; ds++)
907 static void do_grab_sym(void)
909 IMAGE_EXPORT_DIRECTORY
*exportDir
= get_dir(IMAGE_FILE_EXPORT_DIRECTORY
);
917 if (!exportDir
) return;
919 pName
= RVA(exportDir
->AddressOfNames
, exportDir
->NumberOfNames
* sizeof(DWORD
));
920 if (!pName
) {printf("Can't grab functions' name table\n"); return;}
921 pOrdl
= RVA(exportDir
->AddressOfNameOrdinals
, exportDir
->NumberOfNames
* sizeof(WORD
));
922 if (!pOrdl
) {printf("Can't grab functions' ordinal table\n"); return;}
926 if (!(dll_symbols
= (dll_symbol
*) malloc((exportDir
->NumberOfFunctions
+ 1) *
927 sizeof (dll_symbol
))))
928 fatal ("Out of memory");
929 if (exportDir
->AddressOfFunctions
!= exportDir
->NumberOfNames
|| exportDir
->Base
> 1)
930 globals
.do_ordinals
= 1;
932 /* bit map of used funcs */
933 map
= calloc(((exportDir
->NumberOfFunctions
+ 31) & ~31) / 32, sizeof(DWORD
));
934 if (!map
) fatal("no memory");
936 for (j
= 0; j
< exportDir
->NumberOfNames
; j
++, pOrdl
++)
938 map
[*pOrdl
/ 32] |= 1 << (*pOrdl
% 32);
939 ptr
= RVA(*pName
++, sizeof(DWORD
));
940 if (!ptr
) ptr
= "cant_get_function";
941 dll_symbols
[j
].symbol
= strdup(ptr
);
942 dll_symbols
[j
].ordinal
= -1; /* indicate non-ordinal symbol */
943 assert(dll_symbols
[j
].symbol
);
945 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
946 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
948 for (i
= 0; i
< exportDir
->NumberOfFunctions
; i
++)
950 if (pFunc
[i
] && !(map
[i
/ 32] & (1 << (i
% 32))))
952 char ordinal_text
[256];
953 /* Ordinal only entry */
954 snprintf (ordinal_text
, sizeof(ordinal_text
), "%s_%lu",
955 globals
.forward_dll
? globals
.forward_dll
: OUTPUT_UC_DLL_NAME
,
956 exportDir
->Base
+ i
);
957 str_toupper(ordinal_text
);
958 dll_symbols
[j
].symbol
= strdup(ordinal_text
);
959 assert(dll_symbols
[j
].symbol
);
960 dll_symbols
[j
].ordinal
= exportDir
->Base
+ i
;
962 assert(j
<= exportDir
->NumberOfFunctions
);
968 printf("%lu named symbols in DLL, %lu total\n",
969 exportDir
->NumberOfNames
, exportDir
->NumberOfFunctions
);
971 qsort( dll_symbols
, exportDir
->NumberOfFunctions
, sizeof(dll_symbol
), symbol_cmp
);
973 dll_symbols
[exportDir
->NumberOfFunctions
].symbol
= NULL
;
975 dll_current_symbol
= dll_symbols
;
978 /*******************************************************************
981 * Open a DLL and read in exported symbols
983 void dll_open (const char *dll_name
)
985 pe_analysis(dll_name
, do_grab_sym
, SIG_PE
);
988 /*******************************************************************
991 * Get next exported symbol from dll
993 int dll_next_symbol (parsed_symbol
* sym
)
995 if (!dll_current_symbol
->symbol
)
998 assert (dll_symbols
);
1000 sym
->symbol
= strdup (dll_current_symbol
->symbol
);
1001 sym
->ordinal
= dll_current_symbol
->ordinal
;
1002 dll_current_symbol
++;