4 * Copyright 2006 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
39 const struct PDB_JG_HEADER
* header
;
40 const struct PDB_JG_TOC
* toc
;
41 const struct PDB_JG_ROOT
* root
;
45 const struct PDB_DS_HEADER
* header
;
46 const struct PDB_DS_TOC
* toc
;
47 const struct PDB_DS_ROOT
* root
;
50 void* (*read_stream
)(struct pdb_reader
*, DWORD
);
51 DWORD stream_used
[1024];
54 static inline BOOL
has_stream_been_read(struct pdb_reader
* reader
, unsigned stream_nr
)
56 return reader
->stream_used
[stream_nr
/ 32] & (1 << (stream_nr
% 32));
59 static inline void mark_stream_been_read(struct pdb_reader
* reader
, unsigned stream_nr
)
61 reader
->stream_used
[stream_nr
/ 32] |= 1 << (stream_nr
% 32);
64 static inline void clear_stream_been_read(struct pdb_reader
* reader
, unsigned stream_nr
)
66 reader
->stream_used
[stream_nr
/ 32] &= ~(1 << (stream_nr
% 32));
69 static void* pdb_jg_read(const struct PDB_JG_HEADER
* pdb
, const WORD
* block_list
, int size
)
74 if (!size
) return NULL
;
76 nBlocks
= (size
+ pdb
->block_size
- 1) / pdb
->block_size
;
77 buffer
= xmalloc(nBlocks
* pdb
->block_size
);
79 for (i
= 0; i
< nBlocks
; i
++)
80 memcpy(buffer
+ i
* pdb
->block_size
,
81 (const char*)pdb
+ block_list
[i
] * pdb
->block_size
, pdb
->block_size
);
86 static void* pdb_jg_read_stream(struct pdb_reader
* reader
, DWORD stream_nr
)
88 const WORD
* block_list
;
91 if (!reader
->u
.jg
.toc
|| stream_nr
>= reader
->u
.jg
.toc
->num_streams
) return NULL
;
93 mark_stream_been_read(reader
, stream_nr
);
94 if (reader
->u
.jg
.toc
->streams
[stream_nr
].size
== 0 ||
95 reader
->u
.jg
.toc
->streams
[stream_nr
].size
== 0xFFFFFFFF)
97 block_list
= (const WORD
*) &reader
->u
.jg
.toc
->streams
[reader
->u
.jg
.toc
->num_streams
];
98 for (i
= 0; i
< stream_nr
; i
++)
99 block_list
+= (reader
->u
.jg
.toc
->streams
[i
].size
+
100 reader
->u
.jg
.header
->block_size
- 1) / reader
->u
.jg
.header
->block_size
;
102 return pdb_jg_read(reader
->u
.jg
.header
, block_list
,
103 reader
->u
.jg
.toc
->streams
[stream_nr
].size
);
106 static void pdb_jg_init(struct pdb_reader
* reader
)
108 reader
->u
.jg
.header
= PRD(0, sizeof(struct PDB_JG_HEADER
));
109 reader
->read_stream
= pdb_jg_read_stream
;
110 reader
->u
.jg
.toc
= pdb_jg_read(reader
->u
.jg
.header
,
111 reader
->u
.jg
.header
->toc_block
,
112 reader
->u
.jg
.header
->toc
.size
);
113 memset(reader
->stream_used
, 0, sizeof(reader
->stream_used
));
116 static DWORD
pdb_get_num_streams(const struct pdb_reader
* reader
)
118 if (reader
->read_stream
== pdb_jg_read_stream
)
119 return reader
->u
.jg
.toc
->num_streams
;
121 return reader
->u
.ds
.toc
->num_streams
;
124 static DWORD
pdb_get_stream_size(const struct pdb_reader
* reader
, unsigned idx
)
126 if (reader
->read_stream
== pdb_jg_read_stream
)
127 return reader
->u
.jg
.toc
->streams
[idx
].size
;
129 return reader
->u
.ds
.toc
->stream_size
[idx
];
132 static void pdb_exit(struct pdb_reader
* reader
)
135 unsigned char* stream
;
137 for (i
= 0; i
< pdb_get_num_streams(reader
); i
++)
139 if (has_stream_been_read(reader
, i
)) continue;
141 stream
= reader
->read_stream(reader
, i
);
142 if (!stream
) continue;
144 size
= pdb_get_stream_size(reader
, i
);
146 printf("Stream --unused-- #%d (%x)\n", i
, size
);
147 dump_data(stream
, size
, " ");
151 if (reader
->read_stream
== pdb_jg_read_stream
)
153 free((char*)reader
->u
.jg
.root
);
154 free((char*)reader
->u
.jg
.toc
);
158 free((char*)reader
->u
.ds
.root
);
159 free((char*)reader
->u
.ds
.toc
);
163 static unsigned get_stream_by_name(struct pdb_reader
* reader
, const char* name
)
168 DWORD string_idx
, stream_idx
;
172 if (reader
->read_stream
== pdb_jg_read_stream
)
174 str
= reader
->u
.jg
.root
->names
;
175 cbstr
= reader
->u
.jg
.root
->cbNames
;
179 str
= reader
->u
.ds
.root
->names
;
180 cbstr
= reader
->u
.ds
.root
->cbNames
;
183 pdw
= (DWORD
*)(str
+ cbstr
);
184 pdw
++; /* number of ok entries */
187 /* bitfield: first dword is len (in dword), then data */
189 pdw
+= *ok_bits
++ + 1;
192 printf("unexpected value\n");
196 for (i
= 0; i
< count
; i
++)
198 if (ok_bits
[i
/ 32] & (1 << (i
% 32)))
202 if (!strcmp(name
, &str
[string_idx
])) return stream_idx
;
208 static PDB_STRING_TABLE
* read_string_table(struct pdb_reader
* reader
)
211 PDB_STRING_TABLE
* ret
;
212 unsigned stream_size
;
214 stream_idx
= get_stream_by_name(reader
, "/names");
215 if (stream_idx
== -1) return NULL
;
216 ret
= reader
->read_stream(reader
, stream_idx
);
217 if (!ret
) return NULL
;
218 stream_size
= pdb_get_stream_size(reader
, stream_idx
);
219 if (ret
->magic
== 0xeffeeffe && sizeof(*ret
) + ret
->length
< stream_size
) return ret
;
220 printf("Improper string table header (magic=%x)\n", ret
->magic
);
221 dump_data((const unsigned char*)ret
, stream_size
, " ");
226 const char* pdb_get_string_table_entry(const PDB_STRING_TABLE
* table
, unsigned ofs
)
228 if (!table
) return "<<no string table>>";
229 if (ofs
>= table
->length
) return "<<invalid string table offset>>";
230 /* strings start after header */
231 return (char*)(table
+ 1) + ofs
;
234 static void dump_dbi_hash_table(const BYTE
* root
, unsigned size
, const char* name
, const char* pfx
)
236 if (size
>= sizeof(DBI_HASH_HEADER
))
238 const DBI_HASH_HEADER
* hdr
= (const DBI_HASH_HEADER
*)root
;
240 printf("%s%s symbols hash:\n", pfx
, name
);
241 printf("%s\tSignature: 0x%x\n", pfx
, hdr
->signature
);
242 printf("%s\tVersion: 0x%x (%u)\n", pfx
, hdr
->version
, hdr
->version
- 0xeffe0000);
243 printf("%s\tSize of hash records: %u\n", pfx
, hdr
->hash_records_size
);
244 printf("%s\tUnknown: %u\n", pfx
, hdr
->unknown
);
246 if (hdr
->signature
!= 0xFFFFFFFF ||
247 hdr
->version
!= 0xeffe0000 + 19990810 ||
248 (hdr
->hash_records_size
% sizeof(DBI_HASH_RECORD
)) != 0 ||
249 sizeof(DBI_HASH_HEADER
) + hdr
->hash_records_size
+ DBI_BITMAP_HASH_SIZE
> size
||
250 (size
- (sizeof(DBI_HASH_HEADER
) + hdr
->hash_records_size
+ DBI_BITMAP_HASH_SIZE
)) % sizeof(unsigned))
252 if (size
>= sizeof(DBI_HASH_HEADER
) && !hdr
->hash_records_size
)
253 printf("%s\t\tEmpty hash structure\n", pfx
);
255 printf("%s\t\tIncorrect hash structure\n", pfx
);
260 unsigned num_hash_records
= hdr
->hash_records_size
/ sizeof(DBI_HASH_RECORD
);
261 const DBI_HASH_RECORD
* hr
= (const DBI_HASH_RECORD
*)(hdr
+ 1);
262 unsigned* bitmap
= (unsigned*)((char*)(hdr
+ 1) + hdr
->hash_records_size
);
263 unsigned* buckets
= (unsigned*)((char*)(hdr
+ 1) + hdr
->hash_records_size
+ DBI_BITMAP_HASH_SIZE
);
264 unsigned index
, last_index
= (size
- (sizeof(DBI_HASH_HEADER
) + hdr
->hash_records_size
+ DBI_BITMAP_HASH_SIZE
)) / sizeof(unsigned);
266 /* Yes, offsets for accessiong hr[] are stored as multiple of 12; and not
267 * as multiple of sizeof(*hr) = 8 as one might expect.
268 * Perhaps, native implementation likes to keep the same offsets between
269 * in memory representation vs on file representations.
271 for (index
= 0, i
= 0; i
<= DBI_MAX_HASH
; i
++)
273 if (bitmap
[i
/ 32] & (1u << (i
% 32)))
276 printf("%s\t[%u]\n", pfx
, i
);
277 for (j
= buckets
[index
] / 12; j
< (index
+ 1 < last_index
? buckets
[index
+ 1] / 12 : num_hash_records
); j
++)
278 printf("%s\t\t[%u] offset=%08x unk=%x\n", pfx
, j
, hr
[j
].offset
- 1, hr
[j
].unknown
);
282 printf("%s\t[%u] <<empty>>\n", pfx
, i
);
284 /* shouldn't happen */
285 if (sizeof(DBI_HASH_HEADER
) + hdr
->hash_records_size
+ DBI_BITMAP_HASH_SIZE
+ index
* sizeof(unsigned) > size
)
287 printf("%s-- left over %u bytes\n", pfx
,
288 size
- (unsigned)(sizeof(DBI_HASH_HEADER
) + hdr
->hash_records_size
+ DBI_BITMAP_HASH_SIZE
+ index
* sizeof(unsigned)));
293 printf("%sNo header in symbols hash\n", pfx
);
296 static void dump_global_symbol(struct pdb_reader
* reader
, unsigned stream
)
301 global
= reader
->read_stream(reader
, stream
);
304 size
= pdb_get_stream_size(reader
, stream
);
306 dump_dbi_hash_table(global
, size
, "Global", "");
310 static void dump_public_symbol(struct pdb_reader
* reader
, unsigned stream
)
313 DBI_PUBLIC_HEADER
* hdr
;
315 hdr
= reader
->read_stream(reader
, stream
);
318 size
= pdb_get_stream_size(reader
, stream
);
320 printf("Public symbols table: (%u)\n", size
);
322 printf("\tHash size: %u\n", hdr
->hash_size
);
323 printf("\tAddress map size: %u\n", hdr
->address_map_size
);
324 printf("\tNumber of thunks: %u\n", hdr
->num_thunks
);
325 printf("\tSize of thunk: %u\n", hdr
->size_thunk
);
326 printf("\tSection of thunk table: %u\n", hdr
->section_thunk_table
);
327 printf("\tOffset of thunk table: %u\n", hdr
->offset_thunk_table
);
328 printf("\tNumber of sections: %u\n", hdr
->num_sects
);
330 dump_dbi_hash_table((const BYTE
*)(hdr
+ 1), hdr
->hash_size
, "Public", "\t");
334 static void pdb_dump_symbols(struct pdb_reader
* reader
, PDB_STREAM_INDEXES
* sidx
)
336 PDB_SYMBOLS
* symbols
;
337 unsigned char* modimage
;
339 PDB_STRING_TABLE
* filesimage
;
342 sidx
->FPO
= sidx
->unk0
= sidx
->unk1
= sidx
->unk2
= sidx
->unk3
= sidx
->sections_stream
=
343 sidx
->unk4
= sidx
->unk5
= sidx
->unk6
= sidx
->FPO_EXT
= sidx
->unk7
= -1;
345 symbols
= reader
->read_stream(reader
, 3);
346 if (!symbols
) return;
348 switch (symbols
->version
)
351 case 19960307: /* VC 5.0 */
352 case 19970606: /* VC 6.0 */
353 case 19990903: /* VC 7.0 */
356 printf("-Unknown symbol info version %d\n", symbols
->version
);
358 if (symbols
->flags
& 0x8000) /* new */
359 sprintf(tcver
, "%u.%u", (symbols
->flags
>> 8) & 0x7f, symbols
->flags
& 0xff);
361 sprintf(tcver
, "old-%x", symbols
->flags
);
363 "\tsignature: %08x\n"
366 "\tglobal_hash_stream: %u\n"
368 "\tpublic_stream: %u\n"
370 "\tgsym_stream: %u\n"
372 "\tmodule_size: %08x\n"
373 "\tsectcontrib_size: %08x\n"
374 "\thash_size: %08x\n"
375 "\tsrc_module_size: %08x\n"
376 "\tpdbimport_size: %08x\n"
378 "\tstream_idx_size: %08x\n"
379 "\tunknown2_size: %08x\n"
386 symbols
->global_hash_stream
,
387 tcver
, /* from symbols->flags */
388 symbols
->public_stream
,
390 symbols
->gsym_stream
,
392 symbols
->module_size
,
393 symbols
->sectcontrib_size
,
395 symbols
->srcmodule_size
,
396 symbols
->pdbimport_size
,
398 symbols
->stream_index_size
,
399 symbols
->unknown2_size
,
401 get_machine_str( symbols
->machine
),
404 if (symbols
->sectcontrib_size
)
406 const BYTE
* src
= (const BYTE
*)symbols
+ sizeof(PDB_SYMBOLS
) + symbols
->module_size
;
407 const BYTE
* last
= src
+ symbols
->sectcontrib_size
;
408 unsigned version
, size
;
410 printf("\t----------section contrib------------\n");
411 version
= *(unsigned*)src
;
412 printf("\tVersion: %#x (%d)\n", version
, version
- 0xeffe0000);
415 case 0xeffe0000 + 19970605: size
= sizeof(PDB_SYMBOL_RANGE_EX
); break;
416 case 0xeffe0000 + 20140516: size
= sizeof(PDB_SYMBOL_RANGE_EX
) + sizeof(unsigned); break;
417 default: printf("\t\tUnsupported version number\n"); size
= 0;
421 const PDB_SYMBOL_RANGE_EX
* range
;
423 if ((symbols
->sectcontrib_size
- sizeof(unsigned)) % size
)
424 printf("Incoherent size: %zu = %zu * %u + %zu\n",
425 symbols
->sectcontrib_size
- sizeof(unsigned),
426 (symbols
->sectcontrib_size
- sizeof(unsigned)) / size
,
428 (symbols
->sectcontrib_size
- sizeof(unsigned)) % size
);
429 if ((symbols
->sectcontrib_size
- sizeof(unsigned)) % size
)
430 if ((symbols
->sectcontrib_size
- sizeof(unsigned)) % size
)
431 src
+= sizeof(unsigned);
432 while (src
+ size
<= last
)
434 range
= (const PDB_SYMBOL_RANGE_EX
*)(src
+ sizeof(unsigned));
435 printf("\tRange #%tu\n",
436 ((const BYTE
*)range
- ((const BYTE
*)symbols
+ sizeof(PDB_SYMBOLS
) + symbols
->module_size
)) / size
);
437 printf("\t\tsegment: %04x\n"
441 "\t\tcharacteristics: %08x",
446 range
->characteristics
);
447 dump_section_characteristics(range
->characteristics
, " ");
451 "\t\ttimestamp: %08x\n"
452 "\t\tunknown: %08x\n",
457 if (version
== 0xeffe0000 + 20140516)
458 printf("\t\tcoff_section: %08x\n", *(unsigned*)(range
+ 1));
464 if (!(filesimage
= read_string_table(reader
))) printf("string table not found\n");
466 if (symbols
->srcmodule_size
)
468 const PDB_SYMBOL_SOURCE
*src
;
472 const char* start_cstr
;
475 printf("\t----------src module------------\n");
476 src
= (const PDB_SYMBOL_SOURCE
*)((const char*)symbols
+ sizeof(PDB_SYMBOLS
) +
477 symbols
->module_size
+ symbols
->sectcontrib_size
+ symbols
->hash_size
);
478 printf("\tSource Modules\n"
480 "\t\tnSrcFiles: %u\n",
481 src
->nModules
, src
->nSrcFiles
);
483 /* usage of table seems to be as follows:
484 * two arrays of WORD (src->nModules as size)
485 * - first array contains index into files for "module" compilation
486 * (module = compilation unit ??)
487 * - second array contains the number of source files in module
488 * an array of DWORD (src->nSrcFiles as size)
489 * - contains offset (in following string table) of the source file name
491 * - each string is a pascal string (ie. with its length as first BYTE) or
492 * 0-terminated string (depending on version)
494 indx
= &src
->table
[src
->nModules
];
495 offset
= (const DWORD
*)&src
->table
[2 * src
->nModules
];
496 cstr
= (const char*)&src
->table
[2 * (src
->nModules
+ src
->nSrcFiles
)];
499 for (i
= cfile
= 0; i
< src
->nModules
; i
++)
501 printf("\t\tModule[%2d]:\n", i
);
502 cfile
= src
->table
[i
];
503 for (j
= cfile
; j
< src
->nSrcFiles
&& j
< cfile
+ indx
[i
]; j
++)
505 /* FIXME: in some cases, it's a p_string but WHEN ? */
506 if (cstr
+ offset
[j
] >= start_cstr
/* wrap around */ &&
507 cstr
+ offset
[j
] < (const char*)src
+ symbols
->srcmodule_size
)
508 printf("\t\t\tSource file: %s\n", cstr
+ offset
[j
]);
510 printf("\t\t\tSource file: <<out of bounds>>\n");
514 if (symbols
->pdbimport_size
)
516 const PDB_SYMBOL_IMPORT
* imp
;
521 printf("\t------------import--------------\n");
522 imp
= (const PDB_SYMBOL_IMPORT
*)((const char*)symbols
+ sizeof(PDB_SYMBOLS
) +
523 symbols
->module_size
+ symbols
->sectcontrib_size
+
524 symbols
->hash_size
+ symbols
->srcmodule_size
);
525 first
= (const char*)imp
;
526 last
= (const char*)imp
+ symbols
->pdbimport_size
;
527 while (imp
< (const PDB_SYMBOL_IMPORT
*)last
)
529 ptr
= (const char*)imp
+ sizeof(*imp
) + strlen(imp
->filename
);
530 printf("\tImport: %lx\n"
531 "\t\tUnknown1: %08x\n"
532 "\t\tUnknown2: %08x\n"
533 "\t\tTimeDateStamp: %08x\n"
537 (ULONG_PTR
)((const char*)imp
- first
),
544 imp
= (const PDB_SYMBOL_IMPORT
*)(first
+ ((ptr
- first
+ strlen(ptr
) + 1 + 3) & ~3));
547 if (symbols
->stream_index_size
)
549 printf("\t------------stream indexes--------------\n");
550 switch (symbols
->stream_index_size
)
552 case sizeof(PDB_STREAM_INDEXES_OLD
):
553 /* PDB_STREAM_INDEXES is a superset of PDB_STREAM_INDEX_OLD
554 * FIXME: to be confirmed when all fields are fully understood
557 (const char*)symbols
+ sizeof(PDB_SYMBOLS
) + symbols
->module_size
+
558 symbols
->sectcontrib_size
+ symbols
->hash_size
+ symbols
->srcmodule_size
+
559 symbols
->pdbimport_size
+ symbols
->unknown2_size
,
560 sizeof(PDB_STREAM_INDEXES_OLD
));
561 printf("\tFPO: %04x\n"
566 "\tSections stream: %04x\n",
567 sidx
->FPO
, sidx
->unk0
, sidx
->unk1
, sidx
->unk2
, sidx
->unk3
,
568 sidx
->sections_stream
);
570 case sizeof(PDB_STREAM_INDEXES
):
572 (const char*)symbols
+ sizeof(PDB_SYMBOLS
) + symbols
->module_size
+
573 symbols
->sectcontrib_size
+ symbols
->hash_size
+ symbols
->srcmodule_size
+
574 symbols
->pdbimport_size
+ symbols
->unknown2_size
,
576 printf("\tFPO: %04x\n"
581 "\tSection stream: %04x\n"
587 sidx
->FPO
, sidx
->unk0
, sidx
->unk1
, sidx
->unk2
, sidx
->unk3
,
588 sidx
->sections_stream
, sidx
->unk4
, sidx
->unk5
, sidx
->unk6
, sidx
->FPO_EXT
,
592 printf("unexpected size for stream index %d\n", symbols
->stream_index_size
);
597 /* Read global symbol table */
598 modimage
= reader
->read_stream(reader
, symbols
->gsym_stream
);
601 printf("\t------------globals-------------\n");
602 codeview_dump_symbols(modimage
, 0, pdb_get_stream_size(reader
, symbols
->gsym_stream
));
606 /* Read per-module symbol / linenumber tables */
607 file
= (const char*)symbols
+ sizeof(PDB_SYMBOLS
);
608 while (file
- (const char*)symbols
< sizeof(PDB_SYMBOLS
) + symbols
->module_size
)
610 int stream_nr
, symbol_size
, lineno_size
, lineno2_size
;
611 const char* file_name
;
612 const char* lib_name
;
614 if (symbols
->version
< 19970000)
616 const PDB_SYMBOL_FILE
* sym_file
= (const PDB_SYMBOL_FILE
*) file
;
617 stream_nr
= sym_file
->stream
;
618 file_name
= sym_file
->filename
;
619 lib_name
= file_name
+ strlen(file_name
) + 1;
620 symbol_size
= sym_file
->symbol_size
;
621 lineno_size
= sym_file
->lineno_size
;
622 lineno2_size
= sym_file
->lineno2_size
;
623 printf("\t--------symbol file-----------\n");
624 printf("\tName: %s\n", file_name
);
625 if (strcmp(file_name
, lib_name
)) printf("\tLibrary: %s\n", lib_name
);
626 printf("\t\tunknown1: %08x\n"
628 "\t\t\tsegment: %04x\n"
630 "\t\t\toffset: %08x\n"
632 "\t\t\tcharacteristics: %08x",
634 sym_file
->range
.segment
,
635 sym_file
->range
.pad1
,
636 sym_file
->range
.offset
,
637 sym_file
->range
.size
,
638 sym_file
->range
.characteristics
);
639 dump_section_characteristics(sym_file
->range
.characteristics
, " ");
641 "\t\t\tindex: %04x\n"
645 "\t\tsymb size: %08x\n"
646 "\t\tline size: %08x\n"
647 "\t\tline2 size: %08x\n"
648 "\t\tnSrcFiles: %08x\n"
649 "\t\tattribute: %08x\n",
650 sym_file
->range
.index
,
651 sym_file
->range
.pad2
,
654 sym_file
->symbol_size
,
655 sym_file
->lineno_size
,
656 sym_file
->lineno2_size
,
658 sym_file
->attribute
);
662 const PDB_SYMBOL_FILE_EX
* sym_file
= (const PDB_SYMBOL_FILE_EX
*) file
;
664 stream_nr
= sym_file
->stream
;
665 file_name
= sym_file
->filename
;
666 lib_name
= file_name
+ strlen(file_name
) + 1;
667 symbol_size
= sym_file
->symbol_size
;
668 lineno_size
= sym_file
->lineno_size
;
669 lineno2_size
= sym_file
->lineno2_size
;
670 printf("\t--------symbol file-----------\n");
671 printf("\tName: %s\n", file_name
);
672 if (strcmp(file_name
, lib_name
)) printf("\tLibrary: %s\n", lib_name
);
673 printf("\t\tunknown1: %08x\n"
675 "\t\t\tsegment: %04x\n"
677 "\t\t\toffset: %08x\n"
679 "\t\t\tcharacteristics: %08x",
681 sym_file
->range
.segment
,
682 sym_file
->range
.pad1
,
683 sym_file
->range
.offset
,
684 sym_file
->range
.size
,
685 sym_file
->range
.characteristics
);
686 dump_section_characteristics(sym_file
->range
.characteristics
, " ");
688 "\t\t\tindex: %04x\n"
690 "\t\t\ttimestamp: %08x\n"
691 "\t\t\tunknown: %08x\n"
694 "\t\tsymb size: %08x\n"
695 "\t\tline size: %08x\n"
696 "\t\tline2 size: %08x\n"
697 "\t\tnSrcFiles: %08x\n"
698 "\t\tattribute: %08x\n"
699 "\t\treserved/0: %08x\n"
700 "\t\treserved/1: %08x\n",
701 sym_file
->range
.index
,
702 sym_file
->range
.pad2
,
703 sym_file
->range
.timestamp
,
704 sym_file
->range
.unknown
,
707 sym_file
->symbol_size
,
708 sym_file
->lineno_size
,
709 sym_file
->lineno2_size
,
712 sym_file
->reserved
[0],
713 sym_file
->reserved
[1]);
715 modimage
= reader
->read_stream(reader
, stream_nr
);
718 int total_size
= pdb_get_stream_size(reader
, stream_nr
);
721 codeview_dump_symbols((const char*)modimage
, sizeof(DWORD
), symbol_size
);
723 /* line number info */
725 codeview_dump_linetab((const char*)modimage
+ symbol_size
, TRUE
, " ");
726 else if (lineno2_size
) /* actually, only one of the 2 lineno should be present */
727 codeview_dump_linetab2((const char*)modimage
+ symbol_size
, lineno2_size
,
729 /* what's that part ??? */
731 dump_data(modimage
+ symbol_size
+ lineno_size
+ lineno2_size
,
732 total_size
- (symbol_size
+ lineno_size
+ lineno2_size
), " ");
736 file
= (char*)((DWORD_PTR
)(lib_name
+ strlen(lib_name
) + 1 + 3) & ~3);
738 dump_global_symbol(reader
, symbols
->global_hash_stream
);
739 dump_public_symbol(reader
, symbols
->public_stream
);
745 static BOOL
is_bit_set(const unsigned* dw
, unsigned len
, unsigned i
)
747 if (i
>= len
* sizeof(unsigned) * 8) return FALSE
;
748 return (dw
[i
>> 5] & (1u << (i
& 31u))) != 0;
751 static void pdb_dump_hash_value(const BYTE
* ptr
, unsigned len
)
756 for (i
= len
- 1; i
>= 0; i
--)
757 printf("%02x", ptr
[i
]);
767 static int collision_compar(const void *p1
, const void *p2
)
769 unsigned idx1
= *(unsigned*)p1
;
770 unsigned idx2
= *(unsigned*)p2
;
771 return memcmp(collision_arg
.hash
+ idx1
* collision_arg
.hash_size
,
772 collision_arg
.hash
+ idx2
* collision_arg
.hash_size
,
773 collision_arg
.hash_size
);
776 static void pdb_dump_types_hash(struct pdb_reader
* reader
, const PDB_TYPES
* types
, const char* strmname
)
779 unsigned i
, strmsize
;
780 const unsigned* table
;
781 PDB_STRING_TABLE
* strbase
;
783 hash
= reader
->read_stream(reader
, types
->hash_stream
);
786 printf("Types (%s) hash:\n", strmname
);
787 strmsize
= pdb_get_stream_size(reader
, types
->hash_stream
);
788 if (types
->hash_offset
+ types
->hash_size
> strmsize
||
789 (types
->last_index
- types
->first_index
) * types
->hash_value_size
!= types
->hash_size
||
790 types
->search_offset
+ types
->search_size
> strmsize
||
791 types
->type_remap_offset
+ types
->type_remap_size
> strmsize
)
793 printf("\nIncoherent sizes... skipping\n");
796 printf("\n\tIndexes => hash value:\n");
797 for (i
= types
->first_index
; i
< types
->last_index
; i
++)
799 printf("\t\t%08x => ", i
);
800 pdb_dump_hash_value((const BYTE
*)hash
+ types
->hash_offset
+ (i
- types
->first_index
) * types
->hash_value_size
, types
->hash_value_size
);
803 /* print collisions in hash table (if any) */
804 collision
= malloc((types
->last_index
- types
->first_index
) * sizeof(unsigned));
807 unsigned head_printed
= 0;
809 collision_arg
.hash
= (const BYTE
*)hash
+ types
->hash_offset
;
810 collision_arg
.hash_size
= types
->hash_value_size
;
812 for (i
= 0; i
< types
->last_index
- types
->first_index
; i
++) collision
[i
] = i
;
813 qsort(collision
, types
->last_index
- types
->first_index
, sizeof(unsigned), collision_compar
);
814 for (i
= 0; i
< types
->last_index
- types
->first_index
; i
++)
817 for (j
= i
+ 1; j
< types
->last_index
- types
->first_index
; j
++)
818 if (memcmp((const BYTE
*)hash
+ types
->hash_offset
+ collision
[i
] * types
->hash_value_size
,
819 (const BYTE
*)hash
+ types
->hash_offset
+ collision
[j
] * types
->hash_value_size
,
820 types
->hash_value_size
))
827 printf("\n\t\tCollisions:\n");
830 printf("\t\t\tHash ");
831 pdb_dump_hash_value((const BYTE
*)hash
+ types
->hash_offset
+ collision
[i
] * types
->hash_value_size
, types
->hash_value_size
);
833 for (k
= i
; k
< j
; k
++)
834 printf(" %x", types
->first_index
+ collision
[k
]);
841 printf("\n\tIndexes => offsets:\n");
842 table
= (const unsigned*)((const BYTE
*)hash
+ types
->search_offset
);
843 for (i
= 0; i
< types
->search_size
/ (2 * sizeof(unsigned)); i
+= 2)
845 printf("\t\t%08x => %08x\n", table
[2 * i
+ 0], table
[2 * i
+ 1]);
847 if (types
->type_remap_size
&& (strbase
= read_string_table(reader
)))
849 unsigned num
, capa
, count_present
, count_deleted
;
850 const unsigned* present_bitset
;
851 const unsigned* deleted_bitset
;
853 printf("\n\tType remap:\n");
854 table
= (const unsigned*)((const BYTE
*)hash
+ types
->type_remap_offset
);
857 count_present
= *table
++;
858 present_bitset
= table
;
859 table
+= count_present
;
860 count_deleted
= *table
++;
861 deleted_bitset
= table
;
862 table
+= count_deleted
;
863 printf("\t\tNumber of present entries: %u\n", num
);
864 printf("\t\tCapacity: %u\n", capa
);
865 printf("\t\tBitset present:\n");
866 printf("\t\t\tCount: %u\n", count_present
);
867 printf("\t\t\tBitset: ");
868 pdb_dump_hash_value((const BYTE
*)present_bitset
, count_present
* sizeof(unsigned));
870 printf("\t\tBitset deleted:\n");
871 printf("\t\t\tCount: %u\n", count_deleted
);
872 printf("\t\t\tBitset: ");
873 pdb_dump_hash_value((const BYTE
*)deleted_bitset
, count_deleted
* sizeof(unsigned));
875 for (i
= 0; i
< capa
; ++i
)
877 printf("\t\t%2u) %c",
879 is_bit_set(present_bitset
, count_present
, i
) ? 'P' :
880 is_bit_set(deleted_bitset
, count_deleted
, i
) ? 'D' : '_');
881 if (is_bit_set(present_bitset
, count_present
, i
))
883 printf(" %s => ", pdb_get_string_table_entry(strbase
, *table
++));
884 pdb_dump_hash_value((const BYTE
*)table
, types
->hash_value_size
);
885 table
= (const unsigned*)((const BYTE
*)table
+ types
->hash_value_size
);
895 /* there are two 'type' related streams, but with different indexes... */
896 static void pdb_dump_types(struct pdb_reader
* reader
, unsigned strmidx
, const char* strmname
)
898 PDB_TYPES
* types
= NULL
;
899 BOOL used
= has_stream_been_read(reader
, strmidx
);
901 if (pdb_get_stream_size(reader
, strmidx
) < sizeof(*types
))
904 printf("-Too small type header\n");
907 types
= reader
->read_stream(reader
, strmidx
);
910 switch (types
->version
)
912 case 19950410: /* VC 4.0 */
914 case 19961031: /* VC 5.0 / 6.0 */
915 case 19990903: /* VC 7.0 */
916 case 20040203: /* VC 8.0 */
919 /* IPI stream is not always present in older PDB files */
921 printf("-Unknown type info version %d\n", types
->version
);
923 if (used
) clear_stream_been_read(reader
, strmidx
);
927 /* Read type table */
928 printf("Types (%s):\n"
930 "\ttype_offset: %08x\n"
931 "\tfirst_index: %x\n"
934 "\thash_stream: %x\n"
936 "\thash_value_size: %x\n"
937 "\thash_buckets %x\n"
938 "\thash_offset: %x\n"
940 "\tsearch_offset: %x\n"
941 "\tsearch_size: %x\n"
942 "\ttype_remap_offset: %x\n"
943 "\ttype_remap_size: %x\n",
952 types
->hash_value_size
,
953 types
->hash_num_buckets
,
956 types
->search_offset
,
958 types
->type_remap_offset
,
959 types
->type_remap_size
);
960 codeview_dump_types_from_block((const char*)types
+ types
->type_offset
, types
->type_size
);
961 pdb_dump_types_hash(reader
, types
, strmname
);
965 static void pdb_dump_fpo(struct pdb_reader
* reader
, unsigned stream_idx
)
969 const char* frame_type
[4] = {"Fpo", "Trap", "Tss", "NonFpo"};
971 if (stream_idx
== (WORD
)-1) return;
972 fpo
= reader
->read_stream(reader
, stream_idx
);
973 size
= pdb_get_stream_size(reader
, stream_idx
);
974 if (fpo
&& (size
% sizeof(*fpo
)) == 0)
976 size
/= sizeof(*fpo
);
977 printf("FPO data:\n\t Start Length #loc #pmt #prolog #reg frame SEH /BP\n");
978 for (i
= 0; i
< size
; i
++)
980 printf("\t%08x %08x %4d %4d %7d %4d %6s %c %c\n",
981 (UINT
)fpo
[i
].ulOffStart
, (UINT
)fpo
[i
].cbProcSize
, (UINT
)fpo
[i
].cdwLocals
, fpo
[i
].cdwParams
,
982 fpo
[i
].cbProlog
, fpo
[i
].cbRegs
, frame_type
[fpo
[i
].cbFrame
],
983 fpo
[i
].fHasSEH
? 'Y' : 'N', fpo
[i
].fUseBP
? 'Y' : 'N');
989 static void pdb_dump_fpo_ext(struct pdb_reader
* reader
, unsigned stream_idx
)
991 PDB_FPO_DATA
* fpoext
;
993 PDB_STRING_TABLE
* strbase
;
995 if (stream_idx
== (WORD
)-1) return;
996 strbase
= read_string_table(reader
);
997 if (!strbase
) return;
999 fpoext
= reader
->read_stream(reader
, stream_idx
);
1000 size
= pdb_get_stream_size(reader
, stream_idx
);
1001 if (fpoext
&& (size
% sizeof(*fpoext
)) == 0)
1003 size
/= sizeof(*fpoext
);
1004 printf("FPO data (extended):\n"
1005 "\t Start Length Locals Params MaxStack Prolog #SavedRegs Flags Command\n");
1006 for (i
= 0; i
< size
; i
++)
1008 printf("\t%08x %08x %8x %8x %8x %6x %8x %08x %s\n",
1009 fpoext
[i
].start
, fpoext
[i
].func_size
, fpoext
[i
].locals_size
, fpoext
[i
].params_size
,
1010 fpoext
[i
].maxstack_size
, fpoext
[i
].prolog_size
, fpoext
[i
].savedregs_size
, fpoext
[i
].flags
,
1011 pdb_get_string_table_entry(strbase
, fpoext
[i
].str_offset
));
1018 static void pdb_dump_sections(struct pdb_reader
* reader
, unsigned stream_idx
)
1022 const IMAGE_SECTION_HEADER
* sect_hdr
;
1024 if (stream_idx
== (WORD
)-1) return;
1025 segs
= reader
->read_stream(reader
, stream_idx
);
1029 printf("Sections:\n");
1030 size
= pdb_get_stream_size(reader
, stream_idx
);
1031 for (sect_hdr
= (const IMAGE_SECTION_HEADER
*)segs
; (const char*)sect_hdr
< segs
+ size
; sect_hdr
++)
1033 printf("\tSection: %-8.8s\n", sect_hdr
->Name
);
1034 printf("\t\tVirtual size: %08x\n", (unsigned)sect_hdr
->Misc
.VirtualSize
);
1035 printf("\t\tVirtualAddress: %08x\n", (unsigned)sect_hdr
->VirtualAddress
);
1036 printf("\t\tSizeOfRawData: %08x\n", (unsigned)sect_hdr
->SizeOfRawData
);
1037 printf("\t\tPointerToRawData: %08x\n", (unsigned)sect_hdr
->PointerToRawData
);
1038 printf("\t\tPointerToRelocations: %08x\n", (unsigned)sect_hdr
->PointerToRelocations
);
1039 printf("\t\tPointerToLinenumbers: %08x\n", (unsigned)sect_hdr
->PointerToLinenumbers
);
1040 printf("\t\tNumberOfRelocations: %u\n", (unsigned)sect_hdr
->NumberOfRelocations
);
1041 printf("\t\tNumberOfLinenumbers: %u\n", (unsigned)sect_hdr
->NumberOfLinenumbers
);
1042 printf("\t\tCharacteristics: %08x", (unsigned)sect_hdr
->Characteristics
);
1043 dump_section_characteristics(sect_hdr
->Characteristics
, " ");
1050 static const char pdb2
[] = "Microsoft C/C++ program database 2.00";
1052 static void pdb_jg_dump(void)
1054 struct pdb_reader reader
;
1057 * Read in TOC and well-known streams
1059 pdb_jg_init(&reader
);
1060 printf("Header (JG):\n"
1062 "\tsignature: %08x\n"
1063 "\tblock_size: %08x\n"
1064 "\tfree_list_block: %04x\n"
1065 "\ttotal_alloc: %04x\n",
1066 (int)sizeof(pdb2
) - 1, reader
.u
.jg
.header
->ident
,
1067 reader
.u
.jg
.header
->signature
,
1068 reader
.u
.jg
.header
->block_size
,
1069 reader
.u
.jg
.header
->free_list_block
,
1070 reader
.u
.jg
.header
->total_alloc
);
1072 reader
.u
.jg
.root
= reader
.read_stream(&reader
, 1);
1073 if (reader
.u
.jg
.root
)
1075 UINT
*pdw
, *ok_bits
;
1076 UINT i
, numok
, count
;
1077 PDB_STREAM_INDEXES sidx
;
1081 "\tTimeDateStamp: %08x\n"
1084 reader
.u
.jg
.root
->Version
,
1085 reader
.u
.jg
.root
->TimeDateStamp
,
1086 reader
.u
.jg
.root
->Age
,
1087 (unsigned)reader
.u
.jg
.root
->cbNames
);
1089 pdw
= (UINT
*)(reader
.u
.jg
.root
->names
+ reader
.u
.jg
.root
->cbNames
);
1092 printf("\tStreams directory:\n"
1098 /* bitfield: first dword is len (in dword), then data */
1100 pdw
+= *ok_bits
++ + 1;
1103 printf("unexpected value\n");
1107 for (i
= 0; i
< count
; i
++)
1109 if (ok_bits
[i
/ 32] & (1 << (i
% 32)))
1111 UINT string_idx
, stream_idx
;
1112 string_idx
= *pdw
++;
1113 stream_idx
= *pdw
++;
1114 printf("\t\t\t%2d) %-20s => %x\n", i
, &reader
.u
.jg
.root
->names
[string_idx
], stream_idx
);
1118 if (numok
) printf(">>> unmatched present field with found\n");
1120 /* Check for unknown versions */
1121 switch (reader
.u
.jg
.root
->Version
)
1123 case 19950623: /* VC 4.0 */
1125 case 19960307: /* VC 5.0 */
1126 case 19970604: /* VC 6.0 */
1129 printf("-Unknown root block version %d\n", reader
.u
.jg
.root
->Version
);
1131 pdb_dump_types(&reader
, 2, "TPI");
1132 pdb_dump_types(&reader
, 4, "IPI");
1133 pdb_dump_symbols(&reader
, &sidx
);
1134 pdb_dump_fpo(&reader
, sidx
.FPO
);
1135 pdb_dump_sections(&reader
, sidx
.sections_stream
);
1137 else printf("-Unable to get root\n");
1142 static void* pdb_ds_read(const struct PDB_DS_HEADER
* header
, const UINT
*block_list
, int size
)
1147 if (!size
) return NULL
;
1149 nBlocks
= (size
+ header
->block_size
- 1) / header
->block_size
;
1150 buffer
= xmalloc(nBlocks
* header
->block_size
);
1152 for (i
= 0; i
< nBlocks
; i
++)
1153 memcpy(buffer
+ i
* header
->block_size
,
1154 (const char*)header
+ block_list
[i
] * header
->block_size
, header
->block_size
);
1159 static void* pdb_ds_read_stream(struct pdb_reader
* reader
, DWORD stream_number
)
1161 const UINT
*block_list
;
1164 if (!reader
->u
.ds
.toc
|| stream_number
>= reader
->u
.ds
.toc
->num_streams
) return NULL
;
1166 mark_stream_been_read(reader
, stream_number
);
1167 if (reader
->u
.ds
.toc
->stream_size
[stream_number
] == 0 ||
1168 reader
->u
.ds
.toc
->stream_size
[stream_number
] == 0xFFFFFFFF)
1170 block_list
= reader
->u
.ds
.toc
->stream_size
+ reader
->u
.ds
.toc
->num_streams
;
1171 for (i
= 0; i
< stream_number
; i
++)
1172 block_list
+= (reader
->u
.ds
.toc
->stream_size
[i
] + reader
->u
.ds
.header
->block_size
- 1) /
1173 reader
->u
.ds
.header
->block_size
;
1175 return pdb_ds_read(reader
->u
.ds
.header
, block_list
, reader
->u
.ds
.toc
->stream_size
[stream_number
]);
1178 static BOOL
pdb_ds_init(struct pdb_reader
* reader
)
1180 reader
->u
.ds
.header
= PRD(0, sizeof(*reader
->u
.ds
.header
));
1181 if (!reader
->u
.ds
.header
) return FALSE
;
1182 reader
->read_stream
= pdb_ds_read_stream
;
1183 reader
->u
.ds
.toc
= pdb_ds_read(reader
->u
.ds
.header
,
1184 (const UINT
*)((const char*)reader
->u
.ds
.header
+ reader
->u
.ds
.header
->toc_block
* reader
->u
.ds
.header
->block_size
),
1185 reader
->u
.ds
.header
->toc_size
);
1186 memset(reader
->stream_used
, 0, sizeof(reader
->stream_used
));
1190 static const char pdb7
[] = "Microsoft C/C++ MSF 7.00";
1192 static void pdb_ds_dump(void)
1194 struct pdb_reader reader
;
1196 pdb_ds_init(&reader
);
1197 printf("Header (DS)\n"
1198 "\tsignature: %.*s\n"
1199 "\tblock_size: %08x\n"
1200 "\tfree_list_block: %08x\n"
1201 "\tnum_blocks: %08x\n"
1202 "\ttoc_size: %08x\n"
1203 "\tunknown2: %08x\n"
1204 "\ttoc_block: %08x\n",
1205 (int)sizeof(pdb7
) - 1, reader
.u
.ds
.header
->signature
,
1206 reader
.u
.ds
.header
->block_size
,
1207 reader
.u
.ds
.header
->free_list_block
,
1208 reader
.u
.ds
.header
->num_blocks
,
1209 reader
.u
.ds
.header
->toc_size
,
1210 reader
.u
.ds
.header
->unknown2
,
1211 reader
.u
.ds
.header
->toc_block
);
1213 /* streams with static indexes:
1214 * 0: JG says old toc blocks
1218 * 4: types (second stream)
1219 * other known streams:
1220 * - string table: its index is in the stream table from ROOT object under "/names"
1221 * - type hash table: its index is in the types header (2 and 4)
1222 * - global and public streams: from symbol stream header
1223 * those streams get their indexes out of the PDB_STREAM_INDEXES object
1226 * - extended FPO data
1228 mark_stream_been_read(&reader
, 0); /* mark stream #0 as read */
1229 reader
.u
.ds
.root
= reader
.read_stream(&reader
, 1);
1230 if (reader
.u
.ds
.root
)
1232 UINT
*pdw
, *ok_bits
;
1233 UINT i
, numok
, count
;
1234 PDB_STREAM_INDEXES sidx
;
1238 "\tTimeDateStamp: %08x\n"
1241 "\tcbNames: %08x\n",
1242 reader
.u
.ds
.root
->Version
,
1243 reader
.u
.ds
.root
->TimeDateStamp
,
1244 reader
.u
.ds
.root
->Age
,
1245 get_guid_str(&reader
.u
.ds
.root
->guid
),
1246 reader
.u
.ds
.root
->cbNames
);
1247 pdw
= (UINT
*)(reader
.u
.ds
.root
->names
+ reader
.u
.ds
.root
->cbNames
);
1250 printf("\tStreams directory:\n"
1256 /* bitfield: first dword is len (in dword), then data */
1258 pdw
+= *ok_bits
++ + 1;
1261 printf("unexpected value\n");
1265 for (i
= 0; i
< count
; i
++)
1267 if (ok_bits
[i
/ 32] & (1 << (i
% 32)))
1269 UINT string_idx
, stream_idx
;
1270 string_idx
= *pdw
++;
1271 stream_idx
= *pdw
++;
1272 printf("\t\t\t%2d) %-20s => %x\n", i
, &reader
.u
.ds
.root
->names
[string_idx
], stream_idx
);
1276 if (numok
) printf(">>> unmatched present field with found\n");
1278 pdb_dump_types(&reader
, 2, "TPI");
1279 pdb_dump_types(&reader
, 4, "IPI");
1280 pdb_dump_symbols(&reader
, &sidx
);
1281 pdb_dump_fpo(&reader
, sidx
.FPO
);
1282 pdb_dump_fpo_ext(&reader
, sidx
.FPO_EXT
);
1283 pdb_dump_sections(&reader
, sidx
.sections_stream
);
1285 else printf("-Unable to get root\n");
1290 enum FileSig
get_kind_pdb(void)
1294 head
= PRD(0, sizeof(pdb2
) - 1);
1295 if (head
&& !memcmp(head
, pdb2
, sizeof(pdb2
) - 1))
1297 head
= PRD(0, sizeof(pdb7
) - 1);
1298 if (head
&& !memcmp(head
, pdb7
, sizeof(pdb7
) - 1))
1308 head
= PRD(0, sizeof(pdb2
) - 1);
1309 if (head
&& !memcmp(head
, pdb2
, sizeof(pdb2
) - 1))
1314 head
= PRD(0, sizeof(pdb7
) - 1);
1315 if (head
&& !memcmp(head
, pdb7
, sizeof(pdb7
) - 1))
1320 printf("Unrecognized header %s\n", head
);