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
22 #include "wine/port.h"
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
37 #ifdef HAVE_SYS_MMAN_H
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
48 static const IMAGE_NT_HEADERS32
* PE_nt_headers
;
50 const char *get_machine_str(int mach
)
54 case IMAGE_FILE_MACHINE_UNKNOWN
: return "Unknown";
55 case IMAGE_FILE_MACHINE_I860
: return "i860";
56 case IMAGE_FILE_MACHINE_I386
: return "i386";
57 case IMAGE_FILE_MACHINE_R3000
: return "R3000";
58 case IMAGE_FILE_MACHINE_R4000
: return "R4000";
59 case IMAGE_FILE_MACHINE_R10000
: return "R10000";
60 case IMAGE_FILE_MACHINE_ALPHA
: return "Alpha";
61 case IMAGE_FILE_MACHINE_POWERPC
: return "PowerPC";
62 case IMAGE_FILE_MACHINE_AMD64
: return "AMD64";
63 case IMAGE_FILE_MACHINE_IA64
: return "IA64";
68 static const void* RVA(unsigned long rva
, unsigned long len
)
70 IMAGE_SECTION_HEADER
* sectHead
;
73 if (rva
== 0) return NULL
;
75 sectHead
= IMAGE_FIRST_SECTION(PE_nt_headers
);
76 for (i
= PE_nt_headers
->FileHeader
.NumberOfSections
- 1; i
>= 0; i
--)
78 if (sectHead
[i
].VirtualAddress
<= rva
&&
79 rva
+ len
<= (DWORD
)sectHead
[i
].VirtualAddress
+ sectHead
[i
].SizeOfRawData
)
81 /* return image import directory offset */
82 return PRD(sectHead
[i
].PointerToRawData
+ rva
- sectHead
[i
].VirtualAddress
, len
);
89 static const IMAGE_NT_HEADERS32
*get_nt_header( void )
91 const IMAGE_DOS_HEADER
*dos
;
92 dos
= PRD(0, sizeof(*dos
));
93 if (!dos
) return NULL
;
94 return PRD(dos
->e_lfanew
, sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
));
97 static int is_fake_dll( void )
99 static const char fakedll_signature
[] = "Wine placeholder DLL";
100 const IMAGE_DOS_HEADER
*dos
;
102 dos
= PRD(0, sizeof(*dos
) + sizeof(fakedll_signature
));
104 if (dos
&& dos
->e_lfanew
>= sizeof(*dos
) + sizeof(fakedll_signature
) &&
105 !memcmp( dos
+ 1, fakedll_signature
, sizeof(fakedll_signature
) )) return TRUE
;
109 static const void *get_dir_and_size(unsigned int idx
, unsigned int *size
)
111 if(PE_nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
113 const IMAGE_OPTIONAL_HEADER64
*opt
= (const IMAGE_OPTIONAL_HEADER64
*)&PE_nt_headers
->OptionalHeader
;
114 if (idx
>= opt
->NumberOfRvaAndSizes
)
117 *size
= opt
->DataDirectory
[idx
].Size
;
118 return RVA(opt
->DataDirectory
[idx
].VirtualAddress
,
119 opt
->DataDirectory
[idx
].Size
);
123 const IMAGE_OPTIONAL_HEADER32
*opt
= (const IMAGE_OPTIONAL_HEADER32
*)&PE_nt_headers
->OptionalHeader
;
124 if (idx
>= opt
->NumberOfRvaAndSizes
)
127 *size
= opt
->DataDirectory
[idx
].Size
;
128 return RVA(opt
->DataDirectory
[idx
].VirtualAddress
,
129 opt
->DataDirectory
[idx
].Size
);
133 static const void* get_dir(unsigned idx
)
135 return get_dir_and_size(idx
, 0);
138 static const char * const DirectoryNames
[16] = {
139 "EXPORT", "IMPORT", "RESOURCE", "EXCEPTION",
140 "SECURITY", "BASERELOC", "DEBUG", "ARCHITECTURE",
141 "GLOBALPTR", "TLS", "LOAD_CONFIG", "Bound IAT",
142 "IAT", "Delay IAT", "COM Descript", ""
145 static const char *get_magic_type(WORD magic
)
148 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
150 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
152 case IMAGE_ROM_OPTIONAL_HDR_MAGIC
:
158 static inline void print_word(const char *title
, WORD value
)
160 printf(" %-34s 0x%-4X %u\n", title
, value
, value
);
163 static inline void print_dword(const char *title
, DWORD value
)
165 printf(" %-34s 0x%-8x %u\n", title
, value
, value
);
168 static inline void print_longlong(const char *title
, ULONGLONG value
)
170 printf(" %-34s 0x", title
);
172 printf("%lx%08lx\n", (unsigned long)(value
>> 32), (unsigned long)value
);
174 printf("%lx\n", (unsigned long)value
);
177 static inline void print_ver(const char *title
, BYTE major
, BYTE minor
)
179 printf(" %-34s %u.%02u\n", title
, major
, minor
);
182 static inline void print_subsys(const char *title
, WORD value
)
188 case IMAGE_SUBSYSTEM_UNKNOWN
: str
= "Unknown"; break;
189 case IMAGE_SUBSYSTEM_NATIVE
: str
= "Native"; break;
190 case IMAGE_SUBSYSTEM_WINDOWS_GUI
: str
= "Windows GUI"; break;
191 case IMAGE_SUBSYSTEM_WINDOWS_CUI
: str
= "Windows CUI"; break;
192 case IMAGE_SUBSYSTEM_OS2_CUI
: str
= "OS/2 CUI"; break;
193 case IMAGE_SUBSYSTEM_POSIX_CUI
: str
= "Posix CUI"; break;
195 printf(" %-34s 0x%X (%s)\n", title
, value
, str
);
198 static inline void print_dllflags(const char *title
, WORD value
)
200 printf(" %-34s 0x%X\n", title
, value
);
201 #define X(f,s) if (value & f) printf(" %s\n", s)
202 X(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
, "DYNAMIC_BASE");
203 X(IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY
, "FORCE_INTEGRITY");
204 X(IMAGE_DLLCHARACTERISTICS_NX_COMPAT
, "NX_COMPAT");
205 X(IMAGE_DLLCHARACTERISTICS_NO_ISOLATION
, "NO_ISOLATION");
206 X(IMAGE_DLLCHARACTERISTICS_NO_SEH
, "NO_SEH");
207 X(IMAGE_DLLCHARACTERISTICS_NO_BIND
, "NO_BIND");
208 X(IMAGE_DLLCHARACTERISTICS_WDM_DRIVER
, "WDM_DRIVER");
209 X(IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE
, "TERMINAL_SERVER_AWARE");
213 static inline void print_datadirectory(DWORD n
, const IMAGE_DATA_DIRECTORY
*directory
)
216 printf("Data Directory\n");
218 for (i
= 0; i
< n
&& i
< 16; i
++)
220 printf(" %-12s rva: 0x%-8X size: %8u\n",
221 DirectoryNames
[i
], directory
[i
].VirtualAddress
,
226 static void dump_optional_header32(const IMAGE_OPTIONAL_HEADER32
*image_oh
, UINT header_size
)
228 IMAGE_OPTIONAL_HEADER32 oh
;
229 const IMAGE_OPTIONAL_HEADER32
*optionalHeader
;
231 /* in case optional header is missing or partial */
232 memset(&oh
, 0, sizeof(oh
));
233 memcpy(&oh
, image_oh
, min(header_size
, sizeof(oh
)));
234 optionalHeader
= &oh
;
236 print_word("Magic", optionalHeader
->Magic
);
237 print_ver("linker version",
238 optionalHeader
->MajorLinkerVersion
, optionalHeader
->MinorLinkerVersion
);
239 print_dword("size of code", optionalHeader
->SizeOfCode
);
240 print_dword("size of initialized data", optionalHeader
->SizeOfInitializedData
);
241 print_dword("size of uninitialized data", optionalHeader
->SizeOfUninitializedData
);
242 print_dword("entrypoint RVA", optionalHeader
->AddressOfEntryPoint
);
243 print_dword("base of code", optionalHeader
->BaseOfCode
);
244 print_dword("base of data", optionalHeader
->BaseOfData
);
245 print_dword("image base", optionalHeader
->ImageBase
);
246 print_dword("section align", optionalHeader
->SectionAlignment
);
247 print_dword("file align", optionalHeader
->FileAlignment
);
248 print_ver("required OS version",
249 optionalHeader
->MajorOperatingSystemVersion
, optionalHeader
->MinorOperatingSystemVersion
);
250 print_ver("image version",
251 optionalHeader
->MajorImageVersion
, optionalHeader
->MinorImageVersion
);
252 print_ver("subsystem version",
253 optionalHeader
->MajorSubsystemVersion
, optionalHeader
->MinorSubsystemVersion
);
254 print_dword("Win32 Version", optionalHeader
->Win32VersionValue
);
255 print_dword("size of image", optionalHeader
->SizeOfImage
);
256 print_dword("size of headers", optionalHeader
->SizeOfHeaders
);
257 print_dword("checksum", optionalHeader
->CheckSum
);
258 print_subsys("Subsystem", optionalHeader
->Subsystem
);
259 print_dllflags("DLL characteristics:", optionalHeader
->DllCharacteristics
);
260 print_dword("stack reserve size", optionalHeader
->SizeOfStackReserve
);
261 print_dword("stack commit size", optionalHeader
->SizeOfStackCommit
);
262 print_dword("heap reserve size", optionalHeader
->SizeOfHeapReserve
);
263 print_dword("heap commit size", optionalHeader
->SizeOfHeapCommit
);
264 print_dword("loader flags", optionalHeader
->LoaderFlags
);
265 print_dword("RVAs & sizes", optionalHeader
->NumberOfRvaAndSizes
);
267 print_datadirectory(optionalHeader
->NumberOfRvaAndSizes
, optionalHeader
->DataDirectory
);
271 static void dump_optional_header64(const IMAGE_OPTIONAL_HEADER64
*image_oh
, UINT header_size
)
273 IMAGE_OPTIONAL_HEADER64 oh
;
274 const IMAGE_OPTIONAL_HEADER64
*optionalHeader
;
276 /* in case optional header is missing or partial */
277 memset(&oh
, 0, sizeof(oh
));
278 memcpy(&oh
, image_oh
, min(header_size
, sizeof(oh
)));
279 optionalHeader
= &oh
;
281 print_word("Magic", optionalHeader
->Magic
);
282 print_ver("linker version",
283 optionalHeader
->MajorLinkerVersion
, optionalHeader
->MinorLinkerVersion
);
284 print_dword("size of code", optionalHeader
->SizeOfCode
);
285 print_dword("size of initialized data", optionalHeader
->SizeOfInitializedData
);
286 print_dword("size of uninitialized data", optionalHeader
->SizeOfUninitializedData
);
287 print_dword("entrypoint RVA", optionalHeader
->AddressOfEntryPoint
);
288 print_dword("base of code", optionalHeader
->BaseOfCode
);
289 print_longlong("image base", optionalHeader
->ImageBase
);
290 print_dword("section align", optionalHeader
->SectionAlignment
);
291 print_dword("file align", optionalHeader
->FileAlignment
);
292 print_ver("required OS version",
293 optionalHeader
->MajorOperatingSystemVersion
, optionalHeader
->MinorOperatingSystemVersion
);
294 print_ver("image version",
295 optionalHeader
->MajorImageVersion
, optionalHeader
->MinorImageVersion
);
296 print_ver("subsystem version",
297 optionalHeader
->MajorSubsystemVersion
, optionalHeader
->MinorSubsystemVersion
);
298 print_dword("Win32 Version", optionalHeader
->Win32VersionValue
);
299 print_dword("size of image", optionalHeader
->SizeOfImage
);
300 print_dword("size of headers", optionalHeader
->SizeOfHeaders
);
301 print_dword("checksum", optionalHeader
->CheckSum
);
302 print_subsys("Subsystem", optionalHeader
->Subsystem
);
303 print_dllflags("DLL characteristics:", optionalHeader
->DllCharacteristics
);
304 print_longlong("stack reserve size", optionalHeader
->SizeOfStackReserve
);
305 print_longlong("stack commit size", optionalHeader
->SizeOfStackCommit
);
306 print_longlong("heap reserve size", optionalHeader
->SizeOfHeapReserve
);
307 print_longlong("heap commit size", optionalHeader
->SizeOfHeapCommit
);
308 print_dword("loader flags", optionalHeader
->LoaderFlags
);
309 print_dword("RVAs & sizes", optionalHeader
->NumberOfRvaAndSizes
);
311 print_datadirectory(optionalHeader
->NumberOfRvaAndSizes
, optionalHeader
->DataDirectory
);
315 void dump_optional_header(const IMAGE_OPTIONAL_HEADER32
*optionalHeader
, UINT header_size
)
317 printf("Optional Header (%s)\n", get_magic_type(optionalHeader
->Magic
));
319 switch(optionalHeader
->Magic
) {
320 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
321 dump_optional_header32(optionalHeader
, header_size
);
323 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
324 dump_optional_header64((const IMAGE_OPTIONAL_HEADER64
*)optionalHeader
, header_size
);
327 printf(" Unknown optional header magic: 0x%-4X\n", optionalHeader
->Magic
);
332 void dump_file_header(const IMAGE_FILE_HEADER
*fileHeader
)
334 printf("File Header\n");
336 printf(" Machine: %04X (%s)\n",
337 fileHeader
->Machine
, get_machine_str(fileHeader
->Machine
));
338 printf(" Number of Sections: %d\n", fileHeader
->NumberOfSections
);
339 printf(" TimeDateStamp: %08X (%s) offset %lu\n",
340 fileHeader
->TimeDateStamp
, get_time_str(fileHeader
->TimeDateStamp
),
341 Offset(&(fileHeader
->TimeDateStamp
)));
342 printf(" PointerToSymbolTable: %08X\n", fileHeader
->PointerToSymbolTable
);
343 printf(" NumberOfSymbols: %08X\n", fileHeader
->NumberOfSymbols
);
344 printf(" SizeOfOptionalHeader: %04X\n", fileHeader
->SizeOfOptionalHeader
);
345 printf(" Characteristics: %04X\n", fileHeader
->Characteristics
);
346 #define X(f,s) if (fileHeader->Characteristics & f) printf(" %s\n", s)
347 X(IMAGE_FILE_RELOCS_STRIPPED
, "RELOCS_STRIPPED");
348 X(IMAGE_FILE_EXECUTABLE_IMAGE
, "EXECUTABLE_IMAGE");
349 X(IMAGE_FILE_LINE_NUMS_STRIPPED
, "LINE_NUMS_STRIPPED");
350 X(IMAGE_FILE_LOCAL_SYMS_STRIPPED
, "LOCAL_SYMS_STRIPPED");
351 X(IMAGE_FILE_AGGRESIVE_WS_TRIM
, "AGGRESIVE_WS_TRIM");
352 X(IMAGE_FILE_LARGE_ADDRESS_AWARE
, "LARGE_ADDRESS_AWARE");
353 X(IMAGE_FILE_16BIT_MACHINE
, "16BIT_MACHINE");
354 X(IMAGE_FILE_BYTES_REVERSED_LO
, "BYTES_REVERSED_LO");
355 X(IMAGE_FILE_32BIT_MACHINE
, "32BIT_MACHINE");
356 X(IMAGE_FILE_DEBUG_STRIPPED
, "DEBUG_STRIPPED");
357 X(IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP
, "REMOVABLE_RUN_FROM_SWAP");
358 X(IMAGE_FILE_NET_RUN_FROM_SWAP
, "NET_RUN_FROM_SWAP");
359 X(IMAGE_FILE_SYSTEM
, "SYSTEM");
360 X(IMAGE_FILE_DLL
, "DLL");
361 X(IMAGE_FILE_UP_SYSTEM_ONLY
, "UP_SYSTEM_ONLY");
362 X(IMAGE_FILE_BYTES_REVERSED_HI
, "BYTES_REVERSED_HI");
367 static void dump_pe_header(void)
369 dump_file_header(&PE_nt_headers
->FileHeader
);
370 dump_optional_header((const IMAGE_OPTIONAL_HEADER32
*)&PE_nt_headers
->OptionalHeader
, PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
);
373 void dump_section(const IMAGE_SECTION_HEADER
*sectHead
)
375 printf(" %-8.8s VirtSize: %-8u VirtAddr: %-8u 0x%08x\n",
376 sectHead
->Name
, sectHead
->Misc
.VirtualSize
, sectHead
->VirtualAddress
,
377 sectHead
->VirtualAddress
);
378 printf(" raw data offs: %-8u raw data size: %-8u\n",
379 sectHead
->PointerToRawData
, sectHead
->SizeOfRawData
);
380 printf(" relocation offs: %-8u relocations: %-8u\n",
381 sectHead
->PointerToRelocations
, sectHead
->NumberOfRelocations
);
382 printf(" line # offs: %-8u line #'s: %-8u\n",
383 sectHead
->PointerToLinenumbers
, sectHead
->NumberOfLinenumbers
);
384 printf(" characteristics: 0x%08x\n", sectHead
->Characteristics
);
386 #define X(b,s) if (sectHead->Characteristics & b) printf(" " s)
387 /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */
388 /* #define IMAGE_SCN_TYPE_DSECT 0x00000001 - Reserved */
389 /* #define IMAGE_SCN_TYPE_NOLOAD 0x00000002 - Reserved */
390 /* #define IMAGE_SCN_TYPE_GROUP 0x00000004 - Reserved */
391 /* #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 - Reserved */
392 /* #define IMAGE_SCN_TYPE_COPY 0x00000010 - Reserved */
394 X(IMAGE_SCN_CNT_CODE
, "CODE");
395 X(IMAGE_SCN_CNT_INITIALIZED_DATA
, "INITIALIZED_DATA");
396 X(IMAGE_SCN_CNT_UNINITIALIZED_DATA
, "UNINITIALIZED_DATA");
398 X(IMAGE_SCN_LNK_OTHER
, "LNK_OTHER");
399 X(IMAGE_SCN_LNK_INFO
, "LNK_INFO");
400 /* #define IMAGE_SCN_TYPE_OVER 0x00000400 - Reserved */
401 X(IMAGE_SCN_LNK_REMOVE
, "LNK_REMOVE");
402 X(IMAGE_SCN_LNK_COMDAT
, "LNK_COMDAT");
404 /* 0x00002000 - Reserved */
405 /* #define IMAGE_SCN_MEM_PROTECTED 0x00004000 - Obsolete */
406 X(IMAGE_SCN_MEM_FARDATA
, "MEM_FARDATA");
408 /* #define IMAGE_SCN_MEM_SYSHEAP 0x00010000 - Obsolete */
409 X(IMAGE_SCN_MEM_PURGEABLE
, "MEM_PURGEABLE");
410 X(IMAGE_SCN_MEM_16BIT
, "MEM_16BIT");
411 X(IMAGE_SCN_MEM_LOCKED
, "MEM_LOCKED");
412 X(IMAGE_SCN_MEM_PRELOAD
, "MEM_PRELOAD");
414 switch (sectHead
->Characteristics
& IMAGE_SCN_ALIGN_MASK
)
416 #define X2(b,s) case b: printf(" " s); break;
417 X2(IMAGE_SCN_ALIGN_1BYTES
, "ALIGN_1BYTES");
418 X2(IMAGE_SCN_ALIGN_2BYTES
, "ALIGN_2BYTES");
419 X2(IMAGE_SCN_ALIGN_4BYTES
, "ALIGN_4BYTES");
420 X2(IMAGE_SCN_ALIGN_8BYTES
, "ALIGN_8BYTES");
421 X2(IMAGE_SCN_ALIGN_16BYTES
, "ALIGN_16BYTES");
422 X2(IMAGE_SCN_ALIGN_32BYTES
, "ALIGN_32BYTES");
423 X2(IMAGE_SCN_ALIGN_64BYTES
, "ALIGN_64BYTES");
424 X2(IMAGE_SCN_ALIGN_128BYTES
, "ALIGN_128BYTES");
425 X2(IMAGE_SCN_ALIGN_256BYTES
, "ALIGN_256BYTES");
426 X2(IMAGE_SCN_ALIGN_512BYTES
, "ALIGN_512BYTES");
427 X2(IMAGE_SCN_ALIGN_1024BYTES
, "ALIGN_1024BYTES");
428 X2(IMAGE_SCN_ALIGN_2048BYTES
, "ALIGN_2048BYTES");
429 X2(IMAGE_SCN_ALIGN_4096BYTES
, "ALIGN_4096BYTES");
430 X2(IMAGE_SCN_ALIGN_8192BYTES
, "ALIGN_8192BYTES");
434 X(IMAGE_SCN_LNK_NRELOC_OVFL
, "LNK_NRELOC_OVFL");
436 X(IMAGE_SCN_MEM_DISCARDABLE
, "MEM_DISCARDABLE");
437 X(IMAGE_SCN_MEM_NOT_CACHED
, "MEM_NOT_CACHED");
438 X(IMAGE_SCN_MEM_NOT_PAGED
, "MEM_NOT_PAGED");
439 X(IMAGE_SCN_MEM_SHARED
, "MEM_SHARED");
440 X(IMAGE_SCN_MEM_EXECUTE
, "MEM_EXECUTE");
441 X(IMAGE_SCN_MEM_READ
, "MEM_READ");
442 X(IMAGE_SCN_MEM_WRITE
, "MEM_WRITE");
447 static void dump_sections(const void *base
, const void* addr
, unsigned num_sect
)
449 const IMAGE_SECTION_HEADER
* sectHead
= addr
;
452 printf("Section Table\n");
453 for (i
= 0; i
< num_sect
; i
++, sectHead
++)
455 dump_section(sectHead
);
457 if (globals
.do_dump_rawdata
)
459 dump_data((const unsigned char *)base
+ sectHead
->PointerToRawData
, sectHead
->SizeOfRawData
, " " );
465 static void dump_dir_exported_functions(void)
467 unsigned int size
= 0;
468 const IMAGE_EXPORT_DIRECTORY
*exportDir
= get_dir_and_size(IMAGE_FILE_EXPORT_DIRECTORY
, &size
);
474 parsed_symbol symbol
;
476 if (!exportDir
) return;
478 printf("Exports table:\n");
480 printf(" Name: %s\n", (const char*)RVA(exportDir
->Name
, sizeof(DWORD
)));
481 printf(" Characteristics: %08x\n", exportDir
->Characteristics
);
482 printf(" TimeDateStamp: %08X %s\n",
483 exportDir
->TimeDateStamp
, get_time_str(exportDir
->TimeDateStamp
));
484 printf(" Version: %u.%02u\n", exportDir
->MajorVersion
, exportDir
->MinorVersion
);
485 printf(" Ordinal base: %u\n", exportDir
->Base
);
486 printf(" # of functions: %u\n", exportDir
->NumberOfFunctions
);
487 printf(" # of Names: %u\n", exportDir
->NumberOfNames
);
488 printf("Addresses of functions: %08X\n", exportDir
->AddressOfFunctions
);
489 printf("Addresses of name ordinals: %08X\n", exportDir
->AddressOfNameOrdinals
);
490 printf("Addresses of names: %08X\n", exportDir
->AddressOfNames
);
492 printf(" Entry Pt Ordn Name\n");
494 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
495 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
496 pName
= RVA(exportDir
->AddressOfNames
, exportDir
->NumberOfNames
* sizeof(DWORD
));
497 if (!pName
) {printf("Can't grab functions' name table\n"); return;}
498 pOrdl
= RVA(exportDir
->AddressOfNameOrdinals
, exportDir
->NumberOfNames
* sizeof(WORD
));
499 if (!pOrdl
) {printf("Can't grab functions' ordinal table\n"); return;}
501 /* bit map of used funcs */
502 map
= calloc(((exportDir
->NumberOfFunctions
+ 31) & ~31) / 32, sizeof(DWORD
));
503 if (!map
) fatal("no memory");
505 for (i
= 0; i
< exportDir
->NumberOfNames
; i
++, pName
++, pOrdl
++)
509 map
[*pOrdl
/ 32] |= 1 << (*pOrdl
% 32);
511 name
= (const char*)RVA(*pName
, sizeof(DWORD
));
512 if (name
&& globals
.do_demangle
)
514 printf(" %08X %4u ", pFunc
[*pOrdl
], exportDir
->Base
+ *pOrdl
);
516 symbol_init(&symbol
, name
);
517 if (symbol_demangle(&symbol
) == -1)
519 else if (symbol
.flags
& SYM_DATA
)
520 printf(symbol
.arg_text
[0]);
522 output_prototype(stdout
, &symbol
);
523 symbol_clear(&symbol
);
527 printf(" %08X %4u %s", pFunc
[*pOrdl
], exportDir
->Base
+ *pOrdl
, name
);
529 /* check for forwarded function */
530 if ((const char *)RVA(pFunc
[*pOrdl
],sizeof(void*)) >= (const char *)exportDir
&&
531 (const char *)RVA(pFunc
[*pOrdl
],sizeof(void*)) < (const char *)exportDir
+ size
)
532 printf( " (-> %s)", (const char *)RVA(pFunc
[*pOrdl
],1));
535 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
536 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
537 for (i
= 0; i
< exportDir
->NumberOfFunctions
; i
++)
539 if (pFunc
[i
] && !(map
[i
/ 32] & (1 << (i
% 32))))
541 printf(" %08X %4u <by ordinal>\n", pFunc
[i
], exportDir
->Base
+ i
);
548 static void dump_image_thunk_data64(const IMAGE_THUNK_DATA64
*il
)
550 /* FIXME: This does not properly handle large images */
551 const IMAGE_IMPORT_BY_NAME
* iibn
;
552 for (; il
->u1
.Ordinal
; il
++)
554 if (IMAGE_SNAP_BY_ORDINAL64(il
->u1
.Ordinal
))
555 printf(" %4u <by ordinal>\n", (DWORD
)IMAGE_ORDINAL64(il
->u1
.Ordinal
));
558 iibn
= RVA((DWORD
)il
->u1
.AddressOfData
, sizeof(DWORD
));
560 printf("Can't grab import by name info, skipping to next ordinal\n");
562 printf(" %4u %s %x\n", iibn
->Hint
, iibn
->Name
, (DWORD
)il
->u1
.AddressOfData
);
567 static void dump_image_thunk_data32(const IMAGE_THUNK_DATA32
*il
)
569 const IMAGE_IMPORT_BY_NAME
* iibn
;
570 for (; il
->u1
.Ordinal
; il
++)
572 if (IMAGE_SNAP_BY_ORDINAL32(il
->u1
.Ordinal
))
573 printf(" %4u <by ordinal>\n", IMAGE_ORDINAL32(il
->u1
.Ordinal
));
576 iibn
= RVA((DWORD
)il
->u1
.AddressOfData
, sizeof(DWORD
));
578 printf("Can't grab import by name info, skipping to next ordinal\n");
580 printf(" %4u %s %x\n", iibn
->Hint
, iibn
->Name
, (DWORD
)il
->u1
.AddressOfData
);
585 static void dump_dir_imported_functions(void)
587 const IMAGE_IMPORT_DESCRIPTOR
*importDesc
= get_dir(IMAGE_FILE_IMPORT_DIRECTORY
);
590 if (!importDesc
) return;
591 if(PE_nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
593 const IMAGE_OPTIONAL_HEADER64
*opt
= (const IMAGE_OPTIONAL_HEADER64
*)&PE_nt_headers
->OptionalHeader
;
594 directorySize
= opt
->DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
].Size
;
598 const IMAGE_OPTIONAL_HEADER32
*opt
= (const IMAGE_OPTIONAL_HEADER32
*)&PE_nt_headers
->OptionalHeader
;
599 directorySize
= opt
->DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
].Size
;
602 printf("Import Table size: %08x\n", directorySize
);/* FIXME */
606 const IMAGE_THUNK_DATA32
* il
;
608 if (!importDesc
->Name
|| !importDesc
->FirstThunk
) break;
610 printf(" offset %08lx %s\n", Offset(importDesc
), (const char*)RVA(importDesc
->Name
, sizeof(DWORD
)));
611 printf(" Hint/Name Table: %08X\n", (DWORD
)importDesc
->u
.OriginalFirstThunk
);
612 printf(" TimeDataStamp: %08X (%s)\n",
613 importDesc
->TimeDateStamp
, get_time_str(importDesc
->TimeDateStamp
));
614 printf(" ForwarderChain: %08X\n", importDesc
->ForwarderChain
);
615 printf(" First thunk RVA: %08X\n", (DWORD
)importDesc
->FirstThunk
);
617 printf(" Ordn Name\n");
619 il
= (importDesc
->u
.OriginalFirstThunk
!= 0) ?
620 RVA((DWORD
)importDesc
->u
.OriginalFirstThunk
, sizeof(DWORD
)) :
621 RVA((DWORD
)importDesc
->FirstThunk
, sizeof(DWORD
));
624 printf("Can't grab thunk data, going to next imported DLL\n");
627 if(PE_nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
628 dump_image_thunk_data64((const IMAGE_THUNK_DATA64
*)il
);
630 dump_image_thunk_data32(il
);
638 static void dump_dir_delay_imported_functions(void)
640 const struct ImgDelayDescr
650 } *importDesc
= get_dir(IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
);
653 if (!importDesc
) return;
654 if (PE_nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
656 const IMAGE_OPTIONAL_HEADER64
*opt
= (const IMAGE_OPTIONAL_HEADER64
*)&PE_nt_headers
->OptionalHeader
;
657 directorySize
= opt
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].Size
;
661 const IMAGE_OPTIONAL_HEADER32
*opt
= (const IMAGE_OPTIONAL_HEADER32
*)&PE_nt_headers
->OptionalHeader
;
662 directorySize
= opt
->DataDirectory
[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
].Size
;
665 printf("Delay Import Table size: %08x\n", directorySize
); /* FIXME */
669 BOOL use_rva
= importDesc
->grAttrs
& 1;
670 const IMAGE_THUNK_DATA32
* il
;
672 if (!importDesc
->szName
|| !importDesc
->pIAT
|| !importDesc
->pINT
) break;
674 printf(" grAttrs %08x offset %08lx %s\n", importDesc
->grAttrs
, Offset(importDesc
),
675 use_rva
? (const char *)RVA(importDesc
->szName
, sizeof(DWORD
)) : (char *)importDesc
->szName
);
676 printf(" Hint/Name Table: %08x\n", importDesc
->pINT
);
677 printf(" TimeDataStamp: %08X (%s)\n",
678 importDesc
->dwTimeStamp
, get_time_str(importDesc
->dwTimeStamp
));
680 printf(" Ordn Name\n");
682 il
= use_rva
? (const IMAGE_THUNK_DATA32
*)RVA(importDesc
->pINT
, sizeof(DWORD
)) : (const IMAGE_THUNK_DATA32
*)importDesc
->pINT
;
685 printf("Can't grab thunk data, going to next imported DLL\n");
688 if (PE_nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
689 dump_image_thunk_data64((const IMAGE_THUNK_DATA64
*)il
);
691 dump_image_thunk_data32(il
);
699 static void dump_dir_debug_dir(const IMAGE_DEBUG_DIRECTORY
* idd
, int idx
)
703 printf("Directory %02u\n", idx
+ 1);
704 printf(" Characteristics: %08X\n", idd
->Characteristics
);
705 printf(" TimeDateStamp: %08X %s\n",
706 idd
->TimeDateStamp
, get_time_str(idd
->TimeDateStamp
));
707 printf(" Version %u.%02u\n", idd
->MajorVersion
, idd
->MinorVersion
);
711 case IMAGE_DEBUG_TYPE_UNKNOWN
: str
= "UNKNOWN"; break;
712 case IMAGE_DEBUG_TYPE_COFF
: str
= "COFF"; break;
713 case IMAGE_DEBUG_TYPE_CODEVIEW
: str
= "CODEVIEW"; break;
714 case IMAGE_DEBUG_TYPE_FPO
: str
= "FPO"; break;
715 case IMAGE_DEBUG_TYPE_MISC
: str
= "MISC"; break;
716 case IMAGE_DEBUG_TYPE_EXCEPTION
: str
= "EXCEPTION"; break;
717 case IMAGE_DEBUG_TYPE_FIXUP
: str
= "FIXUP"; break;
718 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC
: str
= "OMAP_TO_SRC"; break;
719 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
:str
= "OMAP_FROM_SRC"; break;
720 case IMAGE_DEBUG_TYPE_BORLAND
: str
= "BORLAND"; break;
721 case IMAGE_DEBUG_TYPE_RESERVED10
: str
= "RESERVED10"; break;
723 printf(" Type: %u (%s)\n", idd
->Type
, str
);
724 printf(" SizeOfData: %u\n", idd
->SizeOfData
);
725 printf(" AddressOfRawData: %08X\n", idd
->AddressOfRawData
);
726 printf(" PointerToRawData: %08X\n", idd
->PointerToRawData
);
730 case IMAGE_DEBUG_TYPE_UNKNOWN
:
732 case IMAGE_DEBUG_TYPE_COFF
:
733 dump_coff(idd
->PointerToRawData
, idd
->SizeOfData
,
734 (const char*)PE_nt_headers
+ sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
) + PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
);
736 case IMAGE_DEBUG_TYPE_CODEVIEW
:
737 dump_codeview(idd
->PointerToRawData
, idd
->SizeOfData
);
739 case IMAGE_DEBUG_TYPE_FPO
:
740 dump_frame_pointer_omission(idd
->PointerToRawData
, idd
->SizeOfData
);
742 case IMAGE_DEBUG_TYPE_MISC
:
744 const IMAGE_DEBUG_MISC
* misc
= PRD(idd
->PointerToRawData
, idd
->SizeOfData
);
745 if (!misc
) {printf("Can't get misc debug information\n"); break;}
746 printf(" DataType: %u (%s)\n",
748 (misc
->DataType
== IMAGE_DEBUG_MISC_EXENAME
) ? "Exe name" : "Unknown");
749 printf(" Length: %u\n", misc
->Length
);
750 printf(" Unicode: %s\n", misc
->Unicode
? "Yes" : "No");
751 printf(" Data: %s\n", misc
->Data
);
754 case IMAGE_DEBUG_TYPE_EXCEPTION
:
756 case IMAGE_DEBUG_TYPE_FIXUP
:
758 case IMAGE_DEBUG_TYPE_OMAP_TO_SRC
:
760 case IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
:
762 case IMAGE_DEBUG_TYPE_BORLAND
:
764 case IMAGE_DEBUG_TYPE_RESERVED10
:
770 static void dump_dir_debug(void)
772 const IMAGE_DEBUG_DIRECTORY
*debugDir
= get_dir(IMAGE_FILE_DEBUG_DIRECTORY
);
775 if (!debugDir
) return;
776 nb_dbg
= PE_nt_headers
->OptionalHeader
.DataDirectory
[IMAGE_FILE_DEBUG_DIRECTORY
].Size
/
780 printf("Debug Table (%u directories)\n", nb_dbg
);
782 for (i
= 0; i
< nb_dbg
; i
++)
784 dump_dir_debug_dir(debugDir
, i
);
790 static void dump_dir_tls(void)
792 IMAGE_TLS_DIRECTORY64 dir
;
793 const DWORD
*callbacks
;
794 const IMAGE_TLS_DIRECTORY32
*pdir
= get_dir(IMAGE_FILE_THREAD_LOCAL_STORAGE
);
798 if(PE_nt_headers
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
799 memcpy(&dir
, pdir
, sizeof(dir
));
802 dir
.StartAddressOfRawData
= pdir
->StartAddressOfRawData
;
803 dir
.EndAddressOfRawData
= pdir
->EndAddressOfRawData
;
804 dir
.AddressOfIndex
= pdir
->AddressOfIndex
;
805 dir
.AddressOfCallBacks
= pdir
->AddressOfCallBacks
;
806 dir
.SizeOfZeroFill
= pdir
->SizeOfZeroFill
;
807 dir
.Characteristics
= pdir
->Characteristics
;
810 /* FIXME: This does not properly handle large images */
811 printf( "Thread Local Storage\n" );
812 printf( " Raw data %08x-%08x (data size %x zero fill size %x)\n",
813 (DWORD
)dir
.StartAddressOfRawData
, (DWORD
)dir
.EndAddressOfRawData
,
814 (DWORD
)(dir
.EndAddressOfRawData
- dir
.StartAddressOfRawData
),
815 (DWORD
)dir
.SizeOfZeroFill
);
816 printf( " Index address %08x\n", (DWORD
)dir
.AddressOfIndex
);
817 printf( " Characteristics %08x\n", dir
.Characteristics
);
818 printf( " Callbacks %08x -> {", (DWORD
)dir
.AddressOfCallBacks
);
819 if (dir
.AddressOfCallBacks
)
821 DWORD addr
= (DWORD
)dir
.AddressOfCallBacks
- PE_nt_headers
->OptionalHeader
.ImageBase
;
822 while ((callbacks
= RVA(addr
, sizeof(DWORD
))) && *callbacks
)
824 printf( " %08x", *callbacks
);
825 addr
+= sizeof(DWORD
);
831 enum FileSig
get_kind_dbg(void)
835 pw
= PRD(0, sizeof(WORD
));
836 if (!pw
) {printf("Can't get main signature, aborting\n"); return 0;}
838 if (*pw
== 0x4944 /* "DI" */) return SIG_DBG
;
844 const IMAGE_SEPARATE_DEBUG_HEADER
* separateDebugHead
;
847 const IMAGE_DEBUG_DIRECTORY
* debugDir
;
849 separateDebugHead
= PRD(0, sizeof(separateDebugHead
));
850 if (!separateDebugHead
) {printf("Can't grab the separate header, aborting\n"); return;}
852 printf ("Signature: %.2s (0x%4X)\n",
853 (const char*)&separateDebugHead
->Signature
, separateDebugHead
->Signature
);
854 printf ("Flags: 0x%04X\n", separateDebugHead
->Flags
);
855 printf ("Machine: 0x%04X (%s)\n",
856 separateDebugHead
->Machine
, get_machine_str(separateDebugHead
->Machine
));
857 printf ("Characteristics: 0x%04X\n", separateDebugHead
->Characteristics
);
858 printf ("TimeDateStamp: 0x%08X (%s)\n",
859 separateDebugHead
->TimeDateStamp
, get_time_str(separateDebugHead
->TimeDateStamp
));
860 printf ("CheckSum: 0x%08X\n", separateDebugHead
->CheckSum
);
861 printf ("ImageBase: 0x%08X\n", separateDebugHead
->ImageBase
);
862 printf ("SizeOfImage: 0x%08X\n", separateDebugHead
->SizeOfImage
);
863 printf ("NumberOfSections: 0x%08X\n", separateDebugHead
->NumberOfSections
);
864 printf ("ExportedNamesSize: 0x%08X\n", separateDebugHead
->ExportedNamesSize
);
865 printf ("DebugDirectorySize: 0x%08X\n", separateDebugHead
->DebugDirectorySize
);
867 if (!PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER
),
868 separateDebugHead
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)))
869 {printf("Can't get the sections, aborting\n"); return;}
871 dump_sections(separateDebugHead
, separateDebugHead
+ 1, separateDebugHead
->NumberOfSections
);
873 nb_dbg
= separateDebugHead
->DebugDirectorySize
/ sizeof(IMAGE_DEBUG_DIRECTORY
);
874 debugDir
= PRD(sizeof(IMAGE_SEPARATE_DEBUG_HEADER
) +
875 separateDebugHead
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
) +
876 separateDebugHead
->ExportedNamesSize
,
877 nb_dbg
* sizeof(IMAGE_DEBUG_DIRECTORY
));
878 if (!debugDir
) {printf("Couldn't get the debug directory info, aborting\n");return;}
880 printf("Debug Table (%u directories)\n", nb_dbg
);
882 for (i
= 0; i
< nb_dbg
; i
++)
884 dump_dir_debug_dir(debugDir
, i
);
889 static const char *get_resource_type( unsigned int id
)
891 static const char * const types
[] =
919 if ((size_t)id
< sizeof(types
)/sizeof(types
[0])) return types
[id
];
923 /* dump an ASCII string with proper escaping */
924 static int dump_strA( const unsigned char *str
, size_t len
)
926 static const char escapes
[32] = ".......abtnvfr.............e....";
931 for (; len
; str
++, len
--)
933 if (pos
> buffer
+ sizeof(buffer
) - 8)
935 fwrite( buffer
, pos
- buffer
, 1, stdout
);
936 count
+= pos
- buffer
;
939 if (*str
> 127) /* hex escape */
941 pos
+= sprintf( pos
, "\\x%02x", *str
);
944 if (*str
< 32) /* octal or C escape */
946 if (!*str
&& len
== 1) continue; /* do not output terminating NULL */
947 if (escapes
[*str
] != '.')
948 pos
+= sprintf( pos
, "\\%c", escapes
[*str
] );
949 else if (len
> 1 && str
[1] >= '0' && str
[1] <= '7')
950 pos
+= sprintf( pos
, "\\%03o", *str
);
952 pos
+= sprintf( pos
, "\\%o", *str
);
955 if (*str
== '\\') *pos
++ = '\\';
958 fwrite( buffer
, pos
- buffer
, 1, stdout
);
959 count
+= pos
- buffer
;
963 /* dump a Unicode string with proper escaping */
964 static int dump_strW( const WCHAR
*str
, size_t len
)
966 static const char escapes
[32] = ".......abtnvfr.............e....";
971 for (; len
; str
++, len
--)
973 if (pos
> buffer
+ sizeof(buffer
) - 8)
975 fwrite( buffer
, pos
- buffer
, 1, stdout
);
976 count
+= pos
- buffer
;
979 if (*str
> 127) /* hex escape */
981 if (len
> 1 && str
[1] < 128 && isxdigit((char)str
[1]))
982 pos
+= sprintf( pos
, "\\x%04x", *str
);
984 pos
+= sprintf( pos
, "\\x%x", *str
);
987 if (*str
< 32) /* octal or C escape */
989 if (!*str
&& len
== 1) continue; /* do not output terminating NULL */
990 if (escapes
[*str
] != '.')
991 pos
+= sprintf( pos
, "\\%c", escapes
[*str
] );
992 else if (len
> 1 && str
[1] >= '0' && str
[1] <= '7')
993 pos
+= sprintf( pos
, "\\%03o", *str
);
995 pos
+= sprintf( pos
, "\\%o", *str
);
998 if (*str
== '\\') *pos
++ = '\\';
1001 fwrite( buffer
, pos
- buffer
, 1, stdout
);
1002 count
+= pos
- buffer
;
1006 /* dump data for a STRING resource */
1007 static void dump_string_data( const WCHAR
*ptr
, unsigned int size
, unsigned int id
, const char *prefix
)
1011 for (i
= 0; i
< 16 && size
; i
++)
1013 unsigned len
= *ptr
++;
1020 else size
-= len
+ 1;
1024 printf( "%s%04x \"", prefix
, (id
- 1) * 16 + i
);
1025 dump_strW( ptr
, len
);
1032 /* dump data for a MESSAGETABLE resource */
1033 static void dump_msgtable_data( const void *ptr
, unsigned int size
, unsigned int id
, const char *prefix
)
1035 const MESSAGE_RESOURCE_DATA
*data
= ptr
;
1036 const MESSAGE_RESOURCE_BLOCK
*block
= data
->Blocks
;
1039 for (i
= 0; i
< data
->NumberOfBlocks
; i
++, block
++)
1041 const MESSAGE_RESOURCE_ENTRY
*entry
;
1043 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)data
+ block
->OffsetToEntries
);
1044 for (j
= block
->LowId
; j
<= block
->HighId
; j
++)
1046 if (entry
->Flags
& MESSAGE_RESOURCE_UNICODE
)
1048 const WCHAR
*str
= (const WCHAR
*)entry
->Text
;
1049 printf( "%s%08x L\"", prefix
, j
);
1050 dump_strW( str
, strlenW(str
) );
1055 const char *str
= (const char *) entry
->Text
;
1056 printf( "%s%08x \"", prefix
, j
);
1057 dump_strA( entry
->Text
, strlen(str
) );
1060 entry
= (const MESSAGE_RESOURCE_ENTRY
*)((const char *)entry
+ entry
->Length
);
1065 static void dump_dir_resource(void)
1067 const IMAGE_RESOURCE_DIRECTORY
*root
= get_dir(IMAGE_FILE_RESOURCE_DIRECTORY
);
1068 const IMAGE_RESOURCE_DIRECTORY
*namedir
;
1069 const IMAGE_RESOURCE_DIRECTORY
*langdir
;
1070 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*e1
, *e2
, *e3
;
1071 const IMAGE_RESOURCE_DIR_STRING_U
*string
;
1072 const IMAGE_RESOURCE_DATA_ENTRY
*data
;
1077 printf( "Resources:" );
1079 for (i
= 0; i
< root
->NumberOfNamedEntries
+ root
->NumberOfIdEntries
; i
++)
1081 e1
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(root
+ 1) + i
;
1082 namedir
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ e1
->u2
.s3
.OffsetToDirectory
);
1083 for (j
= 0; j
< namedir
->NumberOfNamedEntries
+ namedir
->NumberOfIdEntries
; j
++)
1085 e2
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(namedir
+ 1) + j
;
1086 langdir
= (const IMAGE_RESOURCE_DIRECTORY
*)((const char *)root
+ e2
->u2
.s3
.OffsetToDirectory
);
1087 for (k
= 0; k
< langdir
->NumberOfNamedEntries
+ langdir
->NumberOfIdEntries
; k
++)
1089 e3
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(langdir
+ 1) + k
;
1092 if (e1
->u1
.s1
.NameIsString
)
1094 string
= (const IMAGE_RESOURCE_DIR_STRING_U
*)((const char *)root
+ e1
->u1
.s1
.NameOffset
);
1095 dump_unicode_str( string
->NameString
, string
->Length
);
1099 const char *type
= get_resource_type( e1
->u1
.s2
.Id
);
1100 if (type
) printf( "%s", type
);
1101 else printf( "%04x", e1
->u1
.s2
.Id
);
1105 if (e2
->u1
.s1
.NameIsString
)
1107 string
= (const IMAGE_RESOURCE_DIR_STRING_U
*) ((const char *)root
+ e2
->u1
.s1
.NameOffset
);
1108 dump_unicode_str( string
->NameString
, string
->Length
);
1111 printf( "%04x", e2
->u1
.s2
.Id
);
1113 printf( " Language=%04x:\n", e3
->u1
.s2
.Id
);
1114 data
= (const IMAGE_RESOURCE_DATA_ENTRY
*)((const char *)root
+ e3
->u2
.OffsetToData
);
1115 if (e1
->u1
.s1
.NameIsString
)
1117 dump_data( RVA( data
->OffsetToData
, data
->Size
), data
->Size
, " " );
1119 else switch(e1
->u1
.s2
.Id
)
1122 dump_string_data( RVA( data
->OffsetToData
, data
->Size
), data
->Size
,
1123 e2
->u1
.s2
.Id
, " " );
1126 dump_msgtable_data( RVA( data
->OffsetToData
, data
->Size
), data
->Size
,
1127 e2
->u1
.s2
.Id
, " " );
1130 dump_data( RVA( data
->OffsetToData
, data
->Size
), data
->Size
, " " );
1139 static void dump_debug(void)
1141 const char* stabs
= NULL
;
1142 unsigned szstabs
= 0;
1143 const char* stabstr
= NULL
;
1146 const IMAGE_SECTION_HEADER
* sectHead
;
1148 sectHead
= (const IMAGE_SECTION_HEADER
*)
1149 ((const char*)PE_nt_headers
+ sizeof(DWORD
) +
1150 sizeof(IMAGE_FILE_HEADER
) + PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
);
1152 for (i
= 0; i
< PE_nt_headers
->FileHeader
.NumberOfSections
; i
++, sectHead
++)
1154 if (!strcmp((const char *)sectHead
->Name
, ".stab"))
1156 stabs
= RVA(sectHead
->VirtualAddress
, sectHead
->Misc
.VirtualSize
);
1157 szstabs
= sectHead
->Misc
.VirtualSize
;
1159 if (!strncmp((const char *)sectHead
->Name
, ".stabstr", 8))
1161 stabstr
= RVA(sectHead
->VirtualAddress
, sectHead
->Misc
.VirtualSize
);
1162 szstr
= sectHead
->Misc
.VirtualSize
;
1165 if (stabs
&& stabstr
)
1166 dump_stabs(stabs
, szstabs
, stabstr
, szstr
);
1169 enum FileSig
get_kind_exec(void)
1173 const IMAGE_DOS_HEADER
* dh
;
1175 pw
= PRD(0, sizeof(WORD
));
1176 if (!pw
) {printf("Can't get main signature, aborting\n"); return 0;}
1178 if (*pw
!= IMAGE_DOS_SIGNATURE
) return SIG_UNKNOWN
;
1180 if ((dh
= PRD(0, sizeof(IMAGE_DOS_HEADER
))))
1182 /* the signature is the first DWORD */
1183 pdw
= PRD(dh
->e_lfanew
, sizeof(DWORD
));
1186 if (*pdw
== IMAGE_NT_SIGNATURE
) return SIG_PE
;
1187 if (*(const WORD
*)pdw
== IMAGE_OS2_SIGNATURE
) return SIG_NE
;
1188 if (*(const WORD
*)pdw
== IMAGE_VXD_SIGNATURE
) return SIG_LE
;
1197 int all
= (globals
.dumpsect
!= NULL
) && strcmp(globals
.dumpsect
, "ALL") == 0;
1199 PE_nt_headers
= get_nt_header();
1200 if (is_fake_dll()) printf( "*** This is a Wine fake DLL ***\n\n" );
1202 if (globals
.do_dumpheader
)
1205 /* FIXME: should check ptr */
1206 dump_sections(PRD(0, 1), (const char*)PE_nt_headers
+ sizeof(DWORD
) +
1207 sizeof(IMAGE_FILE_HEADER
) + PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
,
1208 PE_nt_headers
->FileHeader
.NumberOfSections
);
1210 else if (!globals
.dumpsect
)
1212 /* show at least something here */
1216 if (globals
.dumpsect
)
1218 if (all
|| !strcmp(globals
.dumpsect
, "import"))
1220 dump_dir_imported_functions();
1221 dump_dir_delay_imported_functions();
1223 if (all
|| !strcmp(globals
.dumpsect
, "export"))
1224 dump_dir_exported_functions();
1225 if (all
|| !strcmp(globals
.dumpsect
, "debug"))
1227 if (all
|| !strcmp(globals
.dumpsect
, "resource"))
1228 dump_dir_resource();
1229 if (all
|| !strcmp(globals
.dumpsect
, "tls"))
1232 /* FIXME: not implemented yet */
1233 if (all
|| !strcmp(globals
.dumpsect
, "reloc"))
1237 if (globals
.do_debug
)
1241 typedef struct _dll_symbol
{
1246 static dll_symbol
*dll_symbols
= NULL
;
1247 static dll_symbol
*dll_current_symbol
= NULL
;
1249 /* Compare symbols by ordinal for qsort */
1250 static int symbol_cmp(const void *left
, const void *right
)
1252 return ((const dll_symbol
*)left
)->ordinal
> ((const dll_symbol
*)right
)->ordinal
;
1255 /*******************************************************************
1258 * Free resources used by DLL
1260 /* FIXME: Not used yet
1261 static void dll_close (void)
1266 fatal("No symbols");
1268 for (ds = dll_symbols; ds->symbol; ds++)
1275 static void do_grab_sym( void )
1277 const IMAGE_EXPORT_DIRECTORY
*exportDir
;
1285 PE_nt_headers
= get_nt_header();
1286 if (!(exportDir
= get_dir(IMAGE_FILE_EXPORT_DIRECTORY
))) return;
1288 pName
= RVA(exportDir
->AddressOfNames
, exportDir
->NumberOfNames
* sizeof(DWORD
));
1289 if (!pName
) {printf("Can't grab functions' name table\n"); return;}
1290 pOrdl
= RVA(exportDir
->AddressOfNameOrdinals
, exportDir
->NumberOfNames
* sizeof(WORD
));
1291 if (!pOrdl
) {printf("Can't grab functions' ordinal table\n"); return;}
1295 if (!(dll_symbols
= (dll_symbol
*) malloc((exportDir
->NumberOfFunctions
+ 1) *
1296 sizeof (dll_symbol
))))
1297 fatal ("Out of memory");
1298 if (exportDir
->AddressOfFunctions
!= exportDir
->NumberOfNames
|| exportDir
->Base
> 1)
1299 globals
.do_ordinals
= 1;
1301 /* bit map of used funcs */
1302 map
= calloc(((exportDir
->NumberOfFunctions
+ 31) & ~31) / 32, sizeof(DWORD
));
1303 if (!map
) fatal("no memory");
1305 for (j
= 0; j
< exportDir
->NumberOfNames
; j
++, pOrdl
++)
1307 map
[*pOrdl
/ 32] |= 1 << (*pOrdl
% 32);
1308 ptr
= RVA(*pName
++, sizeof(DWORD
));
1309 if (!ptr
) ptr
= "cant_get_function";
1310 dll_symbols
[j
].symbol
= strdup(ptr
);
1311 dll_symbols
[j
].ordinal
= exportDir
->Base
+ *pOrdl
;
1312 assert(dll_symbols
[j
].symbol
);
1314 pFunc
= RVA(exportDir
->AddressOfFunctions
, exportDir
->NumberOfFunctions
* sizeof(DWORD
));
1315 if (!pFunc
) {printf("Can't grab functions' address table\n"); return;}
1317 for (i
= 0; i
< exportDir
->NumberOfFunctions
; i
++)
1319 if (pFunc
[i
] && !(map
[i
/ 32] & (1 << (i
% 32))))
1321 char ordinal_text
[256];
1322 /* Ordinal only entry */
1323 snprintf (ordinal_text
, sizeof(ordinal_text
), "%s_%u",
1324 globals
.forward_dll
? globals
.forward_dll
: OUTPUT_UC_DLL_NAME
,
1325 exportDir
->Base
+ i
);
1326 str_toupper(ordinal_text
);
1327 dll_symbols
[j
].symbol
= strdup(ordinal_text
);
1328 assert(dll_symbols
[j
].symbol
);
1329 dll_symbols
[j
].ordinal
= exportDir
->Base
+ i
;
1331 assert(j
<= exportDir
->NumberOfFunctions
);
1337 printf("%u named symbols in DLL, %u total, %d unique (ordinal base = %d)\n",
1338 exportDir
->NumberOfNames
, exportDir
->NumberOfFunctions
, j
, exportDir
->Base
);
1340 qsort( dll_symbols
, j
, sizeof(dll_symbol
), symbol_cmp
);
1342 dll_symbols
[j
].symbol
= NULL
;
1344 dll_current_symbol
= dll_symbols
;
1347 /*******************************************************************
1350 * Open a DLL and read in exported symbols
1352 int dll_open (const char *dll_name
)
1354 return dump_analysis(dll_name
, do_grab_sym
, SIG_PE
);
1357 /*******************************************************************
1360 * Get next exported symbol from dll
1362 int dll_next_symbol (parsed_symbol
* sym
)
1364 if (!dll_current_symbol
->symbol
)
1367 assert (dll_symbols
);
1369 sym
->symbol
= strdup (dll_current_symbol
->symbol
);
1370 sym
->ordinal
= dll_current_symbol
->ordinal
;
1371 dll_current_symbol
++;