dswave: Remove DECLSPEC_HIDDEN usage.
[wine.git] / tools / winedump / pdb.c
blob552fa4d8da832db5fdd8b9870c8eebca0a7328eb
1 /*
2 * PDB dumping utility
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <time.h>
27 #include <fcntl.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winedump.h"
33 struct pdb_reader
35 union
37 struct
39 const struct PDB_JG_HEADER* header;
40 const struct PDB_JG_TOC* toc;
41 const struct PDB_JG_ROOT* root;
42 } jg;
43 struct
45 const struct PDB_DS_HEADER* header;
46 const struct PDB_DS_TOC* toc;
47 const struct PDB_DS_ROOT* root;
48 } ds;
49 } u;
50 void* (*read_stream)(struct pdb_reader*, DWORD);
51 DWORD stream_used[1024];
52 PDB_STRING_TABLE* global_string_table;
55 static inline BOOL has_stream_been_read(struct pdb_reader* reader, unsigned stream_nr)
57 return reader->stream_used[stream_nr / 32] & (1 << (stream_nr % 32));
60 static inline void mark_stream_been_read(struct pdb_reader* reader, unsigned stream_nr)
62 reader->stream_used[stream_nr / 32] |= 1 << (stream_nr % 32);
65 static inline void clear_stream_been_read(struct pdb_reader* reader, unsigned stream_nr)
67 reader->stream_used[stream_nr / 32] &= ~(1 << (stream_nr % 32));
70 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list, int size)
72 int i, nBlocks;
73 BYTE* buffer;
75 if (!size) return NULL;
77 nBlocks = (size + pdb->block_size - 1) / pdb->block_size;
78 buffer = xmalloc(nBlocks * pdb->block_size);
80 for (i = 0; i < nBlocks; i++)
81 memcpy(buffer + i * pdb->block_size,
82 (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
84 return buffer;
87 static void* pdb_jg_read_stream(struct pdb_reader* reader, DWORD stream_nr)
89 const WORD* block_list;
90 DWORD i;
92 if (!reader->u.jg.toc || stream_nr >= reader->u.jg.toc->num_streams) return NULL;
94 mark_stream_been_read(reader, stream_nr);
95 if (reader->u.jg.toc->streams[stream_nr].size == 0 ||
96 reader->u.jg.toc->streams[stream_nr].size == 0xFFFFFFFF)
97 return NULL;
98 block_list = (const WORD*) &reader->u.jg.toc->streams[reader->u.jg.toc->num_streams];
99 for (i = 0; i < stream_nr; i++)
100 block_list += (reader->u.jg.toc->streams[i].size +
101 reader->u.jg.header->block_size - 1) / reader->u.jg.header->block_size;
103 return pdb_jg_read(reader->u.jg.header, block_list,
104 reader->u.jg.toc->streams[stream_nr].size);
107 static BOOL pdb_jg_init(struct pdb_reader* reader)
109 reader->u.jg.header = PRD(0, sizeof(struct PDB_JG_HEADER));
110 if (!reader->u.jg.header) return FALSE;
111 reader->read_stream = pdb_jg_read_stream;
112 reader->u.jg.toc = pdb_jg_read(reader->u.jg.header,
113 reader->u.jg.header->toc_block,
114 reader->u.jg.header->toc.size);
115 memset(reader->stream_used, 0, sizeof(reader->stream_used));
116 reader->u.jg.root = reader->read_stream(reader, 1);
117 if (!reader->u.jg.root) return FALSE;
118 return TRUE;
121 static DWORD pdb_get_num_streams(const struct pdb_reader* reader)
123 if (reader->read_stream == pdb_jg_read_stream)
124 return reader->u.jg.toc->num_streams;
125 else
126 return reader->u.ds.toc->num_streams;
129 static DWORD pdb_get_stream_size(const struct pdb_reader* reader, unsigned idx)
131 if (reader->read_stream == pdb_jg_read_stream)
132 return reader->u.jg.toc->streams[idx].size;
133 else
134 return reader->u.ds.toc->stream_size[idx];
137 static void pdb_exit(struct pdb_reader* reader)
139 unsigned i, size;
140 unsigned char* stream;
142 if (globals_dump_sect("ALL")) /* otherwise we won't have loaded all streams */
144 for (i = 0; i < pdb_get_num_streams(reader); i++)
146 if (has_stream_been_read(reader, i)) continue;
148 stream = reader->read_stream(reader, i);
149 if (!stream) continue;
151 size = pdb_get_stream_size(reader, i);
153 printf("Stream --unused-- #%d (%x)\n", i, size);
154 dump_data(stream, size, " ");
155 free(stream);
158 free(reader->global_string_table);
159 if (reader->read_stream == pdb_jg_read_stream)
161 free((char*)reader->u.jg.root);
162 free((char*)reader->u.jg.toc);
164 else
166 free((char*)reader->u.ds.root);
167 free((char*)reader->u.ds.toc);
171 /* forward declarations */
172 static void pdb_dump_fpo(struct pdb_reader* reader, unsigned stream_idx);
173 static void pdb_dump_fpo_ext(struct pdb_reader* reader, unsigned stream_idx);
174 static void pdb_dump_sections(struct pdb_reader* reader, unsigned stream_idx);
176 static unsigned get_stream_by_name(struct pdb_reader* reader, const char* name)
178 DWORD* pdw;
179 DWORD* ok_bits;
180 DWORD cbstr, count;
181 DWORD string_idx, stream_idx;
182 unsigned i;
183 const char* str;
185 if (reader->read_stream == pdb_jg_read_stream)
187 str = reader->u.jg.root->names;
188 cbstr = reader->u.jg.root->cbNames;
190 else
192 str = reader->u.ds.root->names;
193 cbstr = reader->u.ds.root->cbNames;
196 pdw = (DWORD*)(str + cbstr);
197 pdw++; /* number of ok entries */
198 count = *pdw++;
200 /* bitfield: first dword is len (in dword), then data */
201 ok_bits = pdw;
202 pdw += *ok_bits++ + 1;
203 if (*pdw++ != 0)
205 printf("unexpected value\n");
206 return -1;
209 for (i = 0; i < count; i++)
211 if (ok_bits[i / 32] & (1 << (i % 32)))
213 string_idx = *pdw++;
214 stream_idx = *pdw++;
215 if (!strcmp(name, &str[string_idx])) return stream_idx;
218 return -1;
221 static void dump_string_table(const PDB_STRING_TABLE* strtable, const char* name, const char* pfx)
223 const char* end;
224 const char* ptr;
225 unsigned* table;
226 unsigned num_buckets;
227 unsigned i;
229 if (!strtable)
231 printf("%sString table (%s) isn't present\n", pfx, name);
232 return;
234 printf("%sString table (%s)\n"
235 "%s\tHeader: %08x\n"
236 "%s\tLength: %08x\n"
237 "%s\tHash version: %u\n",
238 pfx, name, pfx, strtable->magic, pfx, strtable->length, pfx, strtable->hash_version);
239 ptr = (const char*)(strtable + 1);
240 end = ptr + strtable->length;
241 while (ptr < end)
243 printf("%s\t%tu] %s\n", pfx, ptr - (const char*)(strtable + 1), ptr);
244 ptr += strlen(ptr) + 1;
246 table = (unsigned *)((char*)(strtable + 1) + strtable->length);
247 num_buckets = *table++;
249 if (globals_dump_sect("hash"))
251 printf("%s\tHash:\n"
252 "%s\t\tnum_strings: %x\n"
253 "%s\t\tnum_buckets: %x\n",
254 pfx, pfx, table[num_buckets], pfx, num_buckets);
256 for (i = 0; i < num_buckets; i++)
257 printf("%s\t\t%x] %x\n", pfx, i, table[i]);
261 static PDB_STRING_TABLE* read_string_table(struct pdb_reader* reader)
263 unsigned stream_idx;
264 PDB_STRING_TABLE* ret;
265 unsigned stream_size;
267 stream_idx = get_stream_by_name(reader, "/names");
268 if (stream_idx == -1) return NULL;
269 ret = reader->read_stream(reader, stream_idx);
270 if (!ret) return NULL;
271 stream_size = pdb_get_stream_size(reader, stream_idx);
272 if (globals_dump_sect("PDB")) dump_string_table(ret, "Global", " ");
273 if (ret->magic == 0xeffeeffe && sizeof(*ret) + ret->length < stream_size) return ret;
274 printf("Improper string table header (magic=%x)\n", ret->magic);
275 dump_data((const unsigned char*)ret, stream_size, " ");
276 free( ret );
277 return NULL;
280 const char* pdb_get_string_table_entry(const PDB_STRING_TABLE* table, unsigned ofs)
282 if (!table) return "<<no string table>>";
283 if (ofs >= table->length) return "<<invalid string table offset>>";
284 /* strings start after header */
285 return (char*)(table + 1) + ofs;
288 static void dump_dbi_hash_table(const BYTE* root, unsigned size, const char* name, const char* pfx)
290 if (!globals_dump_sect("hash")) return;
291 if (size >= sizeof(DBI_HASH_HEADER))
293 const DBI_HASH_HEADER* hdr = (const DBI_HASH_HEADER*)root;
295 printf("%s%s symbols hash:\n", pfx, name);
296 printf("%s\tSignature: 0x%x\n", pfx, hdr->signature);
297 printf("%s\tVersion: 0x%x (%u)\n", pfx, hdr->version, hdr->version - 0xeffe0000);
298 printf("%s\tSize of hash records: %u\n", pfx, hdr->hash_records_size);
299 printf("%s\tUnknown: %u\n", pfx, hdr->unknown);
301 if (hdr->signature != 0xFFFFFFFF ||
302 hdr->version != 0xeffe0000 + 19990810 ||
303 (hdr->hash_records_size % sizeof(DBI_HASH_RECORD)) != 0 ||
304 sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE > size ||
305 (size - (sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE)) % sizeof(unsigned))
307 if (size >= sizeof(DBI_HASH_HEADER) && !hdr->hash_records_size)
308 printf("%s\t\tEmpty hash structure\n", pfx);
309 else
310 printf("%s\t\tIncorrect hash structure\n", pfx);
312 else
314 unsigned i;
315 unsigned num_hash_records = hdr->hash_records_size / sizeof(DBI_HASH_RECORD);
316 const DBI_HASH_RECORD* hr = (const DBI_HASH_RECORD*)(hdr + 1);
317 unsigned* bitmap = (unsigned*)((char*)(hdr + 1) + hdr->hash_records_size);
318 unsigned* buckets = (unsigned*)((char*)(hdr + 1) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE);
319 unsigned index, last_index = (size - (sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE)) / sizeof(unsigned);
321 /* Yes, offsets for accessiong hr[] are stored as multiple of 12; and not
322 * as multiple of sizeof(*hr) = 8 as one might expect.
323 * Perhaps, native implementation likes to keep the same offsets between
324 * in memory representation vs on file representations.
326 for (index = 0, i = 0; i <= DBI_MAX_HASH; i++)
328 if (bitmap[i / 32] & (1u << (i % 32)))
330 unsigned j;
331 printf("%s\t[%u]\n", pfx, i);
332 for (j = buckets[index] / 12; j < (index + 1 < last_index ? buckets[index + 1] / 12 : num_hash_records); j++)
333 printf("%s\t\t[%u] offset=%08x unk=%x\n", pfx, j, hr[j].offset - 1, hr[j].unknown);
334 index++;
336 else
337 printf("%s\t[%u] <<empty>>\n", pfx, i);
339 /* shouldn't happen */
340 if (sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE + index * sizeof(unsigned) > size)
342 printf("%s-- left over %u bytes\n", pfx,
343 size - (unsigned)(sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE + index * sizeof(unsigned)));
347 else
348 printf("%sNo header in symbols hash\n", pfx);
351 static void dump_global_symbol(struct pdb_reader* reader, unsigned stream)
353 void* global = NULL;
354 DWORD size;
356 global = reader->read_stream(reader, stream);
357 if (!global) return;
359 size = pdb_get_stream_size(reader, stream);
361 dump_dbi_hash_table(global, size, "Global", "");
362 free(global);
365 static void dump_public_symbol(struct pdb_reader* reader, unsigned stream)
367 unsigned size;
368 DBI_PUBLIC_HEADER* hdr;
369 const BYTE* ptr;
370 unsigned i;
372 if (!globals_dump_sect("public")) return;
373 hdr = reader->read_stream(reader, stream);
374 if (!hdr) return;
376 size = pdb_get_stream_size(reader, stream);
378 printf("Public symbols table: (%u)\n", size);
380 printf("\tHash size: %u\n", hdr->hash_size);
381 printf("\tAddress map size: %u\n", hdr->address_map_size);
382 printf("\tNumber of thunks: %u\n", hdr->num_thunks);
383 printf("\tSize of thunk map: %u\n", hdr->thunk_size);
384 printf("\tSection of thunk table: %u\n", hdr->section_thunk_table);
385 printf("\tOffset of thunk table: %u\n", hdr->offset_thunk_table);
386 printf("\tNumber of sections: %u\n", hdr->num_sections);
388 ptr = (const BYTE*)(hdr + 1);
389 dump_dbi_hash_table(ptr, hdr->hash_size, "Public", "\t");
391 ptr += hdr->hash_size;
392 printf("\tAddress map:\n");
393 for (i = 0; i < hdr->address_map_size / sizeof(unsigned); i++)
394 printf("\t\t%u] %08x\n", i, ((const unsigned*)ptr)[i]);
396 ptr += hdr->address_map_size;
397 printf("\tThunk map:\n");
398 for (i = 0; i < hdr->num_thunks; i++)
399 printf("\t\t%u] %08x\n", i, ((const unsigned*)ptr)[i]);
401 ptr += hdr->num_thunks * sizeof(unsigned);
402 printf("\tSection map:\n");
403 for (i = 0; i < hdr->num_sections; i++)
404 printf("\t\t%u] %04x:%08x\n", i, (unsigned short)((const unsigned*)ptr)[2 * i + 1], ((const unsigned*)ptr)[2 * i + 0]);
406 if (ptr + hdr->num_sections * 8 != ((const BYTE*)hdr) + size)
407 printf("Incorrect stream\n");
408 free(hdr);
411 static const void* pdb_dump_dbi_module(struct pdb_reader* reader, const PDB_SYMBOL_FILE_EX* sym_file,
412 const char* file_name)
414 const char* lib_name;
415 unsigned char* modimage;
416 BOOL new_format = !file_name;
418 if (new_format) file_name = sym_file->filename;
419 printf("\t--------symbol file-----------\n");
420 printf("\tName: %s\n", file_name);
421 lib_name = file_name + strlen(file_name) + 1;
422 if (strcmp(file_name, lib_name)) printf("\tLibrary: %s\n", lib_name);
423 printf("\t\tunknown1: %08x\n"
424 "\t\trange\n"
425 "\t\t\tsegment: %04x\n"
426 "\t\t\tpad1: %04x\n"
427 "\t\t\toffset: %08x\n"
428 "\t\t\tsize: %08x\n"
429 "\t\t\tcharacteristics: %08x",
430 sym_file->unknown1,
431 sym_file->range.segment,
432 sym_file->range.pad1,
433 sym_file->range.offset,
434 sym_file->range.size,
435 sym_file->range.characteristics);
436 dump_section_characteristics(sym_file->range.characteristics, " ");
437 printf("\n"
438 "\t\t\tindex: %04x\n"
439 "\t\t\tpad2: %04x\n",
440 sym_file->range.index,
441 sym_file->range.pad2);
442 if (new_format)
443 printf("\t\t\ttimestamp: %08x\n"
444 "\t\t\tunknown: %08x\n",
445 sym_file->range.timestamp,
446 sym_file->range.unknown);
447 printf("\t\tflag: %04x\n"
448 "\t\tstream: %04x\n"
449 "\t\tsymb size: %08x\n"
450 "\t\tline size: %08x\n"
451 "\t\tline2 size: %08x\n"
452 "\t\tnSrcFiles: %08x\n"
453 "\t\tattribute: %08x\n",
454 sym_file->flag,
455 sym_file->stream,
456 sym_file->symbol_size,
457 sym_file->lineno_size,
458 sym_file->lineno2_size,
459 sym_file->nSrcFiles,
460 sym_file->attribute);
461 if (new_format)
462 printf("\t\treserved/0: %08x\n"
463 "\t\treserved/1: %08x\n",
464 sym_file->reserved[0],
465 sym_file->reserved[1]);
467 modimage = reader->read_stream(reader, sym_file->stream);
468 if (modimage)
470 int total_size = pdb_get_stream_size(reader, sym_file->stream);
472 if (sym_file->symbol_size)
473 codeview_dump_symbols((const char*)modimage, sizeof(DWORD), sym_file->symbol_size);
475 /* line number info */
476 if (sym_file->lineno_size)
477 codeview_dump_linetab((const char*)modimage + sym_file->symbol_size, TRUE, " ");
478 else if (sym_file->lineno2_size) /* actually, only one of the 2 lineno should be present */
479 codeview_dump_linetab2((const char*)modimage + sym_file->symbol_size, sym_file->lineno2_size,
480 reader->global_string_table, " ");
481 /* what's that part ??? */
482 if (0)
483 dump_data(modimage + sym_file->symbol_size + sym_file->lineno_size + sym_file->lineno2_size,
484 total_size - (sym_file->symbol_size + sym_file->lineno_size + sym_file->lineno2_size), " ");
485 free(modimage);
487 return (const void*)((DWORD_PTR)(lib_name + strlen(lib_name) + 1 + 3) & ~3);
490 static void pdb_dump_symbols(struct pdb_reader* reader)
492 PDB_SYMBOLS* symbols;
493 unsigned char* modimage;
494 const char* file;
495 char tcver[32];
496 PDB_STREAM_INDEXES sidx;
498 sidx.FPO = sidx.unk0 = sidx.unk1 = sidx.unk2 = sidx.unk3 = sidx.sections_stream =
499 sidx.unk4 = sidx.unk5 = sidx.unk6 = sidx.FPO_EXT = sidx.unk7 = -1;
501 symbols = reader->read_stream(reader, 3);
502 if (!symbols) return;
504 if (globals_dump_sect("DBI"))
506 switch (symbols->version)
508 case 0: /* VC 4.0 */
509 case 19960307: /* VC 5.0 */
510 case 19970606: /* VC 6.0 */
511 case 19990903: /* VC 7.0 */
512 break;
513 default:
514 printf("-Unknown symbol info version %d\n", symbols->version);
516 if (symbols->flags & 0x8000) /* new */
517 sprintf(tcver, "%u.%u", (symbols->flags >> 8) & 0x7f, symbols->flags & 0xff);
518 else
519 sprintf(tcver, "old-%x", symbols->flags);
520 printf("Symbols:\n"
521 "\tsignature: %08x\n"
522 "\tversion: %u\n"
523 "\tage: %08x\n"
524 "\tglobal_hash_stream: %u\n"
525 "\tbuilder: %s\n"
526 "\tpublic_stream: %u\n"
527 "\tbldVer: %u\n"
528 "\tgsym_stream: %u\n"
529 "\trbldVer: %u\n"
530 "\tmodule_size: %08x\n"
531 "\tsectcontrib_size: %08x\n"
532 "\tsegmap_size: %08x\n"
533 "\tsrc_module_size: %08x\n"
534 "\tpdbimport_size: %08x\n"
535 "\tresvd0: %08x\n"
536 "\tstream_idx_size: %08x\n"
537 "\tunknown2_size: %08x\n"
538 "\tresvd3: %04x\n"
539 "\tmachine: %s\n"
540 "\tresvd4 %08x\n",
541 symbols->signature,
542 symbols->version,
543 symbols->age,
544 symbols->global_hash_stream,
545 tcver, /* from symbols->flags */
546 symbols->public_stream,
547 symbols->bldVer,
548 symbols->gsym_stream,
549 symbols->rbldVer,
550 symbols->module_size,
551 symbols->sectcontrib_size,
552 symbols->segmap_size,
553 symbols->srcmodule_size,
554 symbols->pdbimport_size,
555 symbols->resvd0,
556 symbols->stream_index_size,
557 symbols->unknown2_size,
558 symbols->resvd3,
559 get_machine_str( symbols->machine ),
560 symbols->resvd4);
563 if (symbols->sectcontrib_size && globals_dump_sect("image"))
565 const BYTE* src = (const BYTE*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size;
566 const BYTE* last = src + symbols->sectcontrib_size;
567 unsigned version, size;
569 printf("\t----------section contrib------------\n");
570 version = *(unsigned*)src;
571 printf("\tVersion: %#x (%d)\n", version, version - 0xeffe0000);
572 switch (version)
574 case 0xeffe0000 + 19970605: size = sizeof(PDB_SYMBOL_RANGE_EX); break;
575 case 0xeffe0000 + 20140516: size = sizeof(PDB_SYMBOL_RANGE_EX) + sizeof(unsigned); break;
576 default: printf("\t\tUnsupported version number\n"); size = 0;
578 if (size)
580 const PDB_SYMBOL_RANGE_EX* range;
582 if ((symbols->sectcontrib_size - sizeof(unsigned)) % size)
583 printf("Incoherent size: %zu = %zu * %u + %zu\n",
584 symbols->sectcontrib_size - sizeof(unsigned),
585 (symbols->sectcontrib_size - sizeof(unsigned)) / size,
586 size,
587 (symbols->sectcontrib_size - sizeof(unsigned)) % size);
588 src += sizeof(unsigned);
589 while (src + size <= last)
591 range = (const PDB_SYMBOL_RANGE_EX*)(src + sizeof(unsigned));
592 printf("\tRange #%tu\n",
593 ((const BYTE*)range - ((const BYTE*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size)) / size);
594 printf("\t\tsegment: %04x\n"
595 "\t\tpad1: %04x\n"
596 "\t\toffset: %08x\n"
597 "\t\tsize: %08x\n"
598 "\t\tcharacteristics: %08x",
599 range->segment,
600 range->pad1,
601 range->offset,
602 range->size,
603 range->characteristics);
604 dump_section_characteristics(range->characteristics, " ");
605 printf("\n"
606 "\t\tindex: %04x\n"
607 "\t\tpad2: %04x\n"
608 "\t\ttimestamp: %08x\n"
609 "\t\tunknown: %08x\n",
610 range->index,
611 range->pad2,
612 range->timestamp,
613 range->unknown);
614 if (version == 0xeffe0000 + 20140516)
615 printf("\t\tcoff_section: %08x\n", *(unsigned*)(range + 1));
616 src += size;
621 if (symbols->srcmodule_size && globals_dump_sect("DBI"))
623 const PDB_SYMBOL_SOURCE*src;
624 int i, j, cfile;
625 const WORD* indx;
626 const DWORD* offset;
627 const char* start_cstr;
628 const char* cstr;
630 printf("\t----------src module------------\n");
631 src = (const PDB_SYMBOL_SOURCE*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
632 symbols->module_size + symbols->sectcontrib_size + symbols->segmap_size);
633 printf("\tSource Modules\n"
634 "\t\tnModules: %u\n"
635 "\t\tnSrcFiles: %u\n",
636 src->nModules, src->nSrcFiles);
638 /* usage of table seems to be as follows:
639 * two arrays of WORD (src->nModules as size)
640 * - first array contains index into files for "module" compilation
641 * (module = compilation unit ??)
642 * - second array contains the number of source files in module
643 * an array of DWORD (src->nSrcFiles as size)
644 * - contains offset (in following string table) of the source file name
645 * a string table
646 * - each string is a pascal string (ie. with its length as first BYTE) or
647 * 0-terminated string (depending on version)
649 indx = &src->table[src->nModules];
650 offset = (const DWORD*)&src->table[2 * src->nModules];
651 cstr = (const char*)&src->table[2 * (src->nModules + src->nSrcFiles)];
652 start_cstr = cstr;
654 for (i = cfile = 0; i < src->nModules; i++)
656 printf("\t\tModule[%2d]:\n", i);
657 cfile = src->table[i];
658 for (j = cfile; j < src->nSrcFiles && j < cfile + indx[i]; j++)
660 /* FIXME: in some cases, it's a p_string but WHEN ? */
661 if (cstr + offset[j] >= start_cstr /* wrap around */ &&
662 cstr + offset[j] < (const char*)src + symbols->srcmodule_size)
663 printf("\t\t\tSource file: %s\n", cstr + offset[j]);
664 else
665 printf("\t\t\tSource file: <<out of bounds>>\n");
669 if (symbols->pdbimport_size && globals_dump_sect("PDB"))
671 const PDB_SYMBOL_IMPORT* imp;
672 const char* first;
673 const char* last;
674 const char* ptr;
676 printf("\t------------import--------------\n");
677 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
678 symbols->module_size + symbols->sectcontrib_size +
679 symbols->segmap_size + symbols->srcmodule_size);
680 first = (const char*)imp;
681 last = (const char*)imp + symbols->pdbimport_size;
682 while (imp < (const PDB_SYMBOL_IMPORT*)last)
684 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
685 printf("\tImport: %lx\n"
686 "\t\tUnknown1: %08x\n"
687 "\t\tUnknown2: %08x\n"
688 "\t\tTimeDateStamp: %08x\n"
689 "\t\tAge: %08u\n"
690 "\t\tfile1: %s\n"
691 "\t\tfile2: %s\n",
692 (ULONG_PTR)((const char*)imp - first),
693 imp->unknown1,
694 imp->unknown2,
695 imp->TimeDateStamp,
696 imp->Age,
697 imp->filename,
698 ptr);
699 imp = (const PDB_SYMBOL_IMPORT*)(first + ((ptr - first + strlen(ptr) + 1 + 3) & ~3));
702 if (symbols->segmap_size && globals_dump_sect("image"))
704 const struct OMFSegMap* segmap = (const struct OMFSegMap*)((const BYTE*)symbols + sizeof(PDB_SYMBOLS) +
705 symbols->module_size + symbols->sectcontrib_size);
706 const struct OMFSegMapDesc* desc = (const struct OMFSegMapDesc*)(segmap + 1);
708 printf("\t--------------segment map----------------\n");
709 printf("\tNumber of segments: %x\n", segmap->cSeg);
710 printf("\tNumber of logical segments: %x\n", segmap->cSegLog);
711 /* FIXME check mapping old symbols */
712 for (; (const BYTE*)(desc + 1) <= ((const BYTE*)(segmap + 1) + symbols->segmap_size); desc++)
714 printf("\t\tSegment descriptor #%tu\n", desc - (const struct OMFSegMapDesc*)(segmap + 1));
715 printf("\t\t\tFlags: %04x (%c%c%c%s%s%s%s)\n",
716 desc->flags,
717 (desc->flags & 0x01) ? 'R' : '-',
718 (desc->flags & 0x02) ? 'W' : '-',
719 (desc->flags & 0x04) ? 'X' : '-',
720 (desc->flags & 0x08) ? " 32bit-linear" : "",
721 (desc->flags & 0x100) ? " selector" : "",
722 (desc->flags & 0x200) ? " absolute" : "",
723 (desc->flags & 0x400) ? " group" : "");
724 printf("\t\t\tOverlay: %04x\n", desc->ovl);
725 printf("\t\t\tGroup: %04x\n", desc->group);
726 printf("\t\t\tFrame: %04x\n", desc->frame);
727 printf("\t\t\tSegment name: %s\n", desc->iSegName == 0xffff ? "none" : pdb_get_string_table_entry(reader->global_string_table, desc->iSegName));
728 printf("\t\t\tClass name: %s\n", desc->iClassName == 0xffff ? "none" : pdb_get_string_table_entry(reader->global_string_table, desc->iClassName));
729 printf("\t\t\tOffset: %08x\n", desc->offset);
730 printf("\t\t\tSize: %04x\n", desc->cbSeg);
733 if (symbols->unknown2_size && globals_dump_sect("PDB"))
735 const char* ptr = (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
736 symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
737 symbols->pdbimport_size;
738 printf("\t------------Unknown2--------------\n");
739 dump_string_table((const PDB_STRING_TABLE*)ptr, "Unknown from DBI", "\t");
741 if (symbols->stream_index_size && globals_dump_sect("image"))
743 printf("\t------------stream indexes--------------\n");
744 switch (symbols->stream_index_size)
746 case sizeof(PDB_STREAM_INDEXES_OLD):
747 /* PDB_STREAM_INDEXES is a superset of PDB_STREAM_INDEX_OLD
748 * FIXME: to be confirmed when all fields are fully understood
750 memcpy(&sidx,
751 (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
752 symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
753 symbols->pdbimport_size + symbols->unknown2_size,
754 sizeof(PDB_STREAM_INDEXES_OLD));
755 printf("\tFPO: %04x\n"
756 "\t?: %04x\n"
757 "\t?: %04x\n"
758 "\t?: %04x\n"
759 "\t?: %04x\n"
760 "\tSections stream: %04x\n",
761 sidx.FPO, sidx.unk0, sidx.unk1, sidx.unk2, sidx.unk3,
762 sidx.sections_stream);
763 break;
764 case sizeof(PDB_STREAM_INDEXES):
765 memcpy(&sidx,
766 (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
767 symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
768 symbols->pdbimport_size + symbols->unknown2_size,
769 sizeof(sidx));
770 printf("\tFPO: %04x\n"
771 "\t?: %04x\n"
772 "\t?: %04x\n"
773 "\t?: %04x\n"
774 "\t?: %04x\n"
775 "\tSection stream: %04x\n"
776 "\t?: %04x\n"
777 "\t?: %04x\n"
778 "\t?: %04x\n"
779 "\tFPO-ext: %04x\n"
780 "\t?: %04x\n",
781 sidx.FPO, sidx.unk0, sidx.unk1, sidx.unk2, sidx.unk3,
782 sidx.sections_stream, sidx.unk4, sidx.unk5, sidx.unk6, sidx.FPO_EXT,
783 sidx.unk7);
784 break;
785 default:
786 printf("unexpected size for stream index %d\n", symbols->stream_index_size);
787 break;
791 /* Read global symbol table */
792 modimage = reader->read_stream(reader, symbols->gsym_stream);
793 if (modimage && globals_dump_sect("DBI"))
795 printf("\t------------globals-------------\n");
796 codeview_dump_symbols(modimage, 0, pdb_get_stream_size(reader, symbols->gsym_stream));
797 free(modimage);
800 /* Read per-module symbol / linenumber tables */
801 if (symbols->module_size && globals_dump_sect("DBI"))
803 SIZE_T module_header_size = symbols->version < 19970000 ? sizeof(PDB_SYMBOL_FILE) : sizeof(PDB_SYMBOL_FILE_EX);
805 file = (const char*)symbols + sizeof(PDB_SYMBOLS);
806 while (file + module_header_size <= (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size)
808 if (symbols->version < 19970000)
810 PDB_SYMBOL_FILE_EX copy;
811 const PDB_SYMBOL_FILE* sym_file = (const PDB_SYMBOL_FILE*)file;
813 copy.unknown1 = sym_file->unknown1;
814 copy.range.segment = sym_file->range.segment;
815 copy.range.pad1 = sym_file->range.pad1;
816 copy.range.offset = sym_file->range.offset;
817 copy.range.size = sym_file->range.size;
818 copy.range.characteristics = sym_file->range.characteristics;
819 copy.range.index = sym_file->range.index;
820 copy.range.pad2 = sym_file->range.pad2;
821 copy.range.timestamp = 0;
822 copy.range.unknown = 0;
823 copy.flag = sym_file->flag;
824 copy.stream = sym_file->stream;
825 copy.symbol_size = sym_file->symbol_size;
826 copy.lineno_size = sym_file->lineno_size;
827 copy.lineno2_size = sym_file->lineno2_size;
828 copy.nSrcFiles = sym_file->nSrcFiles;
829 copy.attribute = sym_file->attribute;
830 copy.reserved[0] = 0;
831 copy.reserved[1] = 0;
832 file = pdb_dump_dbi_module(reader, &copy, sym_file->filename);
834 else
835 file = pdb_dump_dbi_module(reader, (const PDB_SYMBOL_FILE_EX*)file, NULL);
838 dump_global_symbol(reader, symbols->global_hash_stream);
839 dump_public_symbol(reader, symbols->public_stream);
841 if (globals_dump_sect("image"))
843 pdb_dump_fpo(reader, sidx.FPO);
844 pdb_dump_fpo_ext(reader, sidx.FPO_EXT);
845 pdb_dump_sections(reader, sidx.sections_stream);
848 free(symbols);
851 static BOOL is_bit_set(const unsigned* dw, unsigned len, unsigned i)
853 if (i >= len * sizeof(unsigned) * 8) return FALSE;
854 return (dw[i >> 5] & (1u << (i & 31u))) != 0;
857 static void pdb_dump_hash_value(const BYTE* ptr, unsigned len)
859 int i;
861 printf("[");
862 for (i = len - 1; i >= 0; i--)
863 printf("%02x", ptr[i]);
864 printf("]");
867 static struct
869 const BYTE* hash;
870 unsigned hash_size;
871 } collision_arg;
873 static int collision_compar(const void *p1, const void *p2)
875 unsigned idx1 = *(unsigned*)p1;
876 unsigned idx2 = *(unsigned*)p2;
877 return memcmp(collision_arg.hash + idx1 * collision_arg.hash_size,
878 collision_arg.hash + idx2 * collision_arg.hash_size,
879 collision_arg.hash_size);
882 static void pdb_dump_types_hash(struct pdb_reader* reader, const PDB_TYPES* types, const char* strmname)
884 void* hash = NULL;
885 unsigned i, strmsize;
886 const unsigned* table;
887 unsigned *collision;
889 if (!globals_dump_sect("hash")) return;
890 hash = reader->read_stream(reader, types->hash_stream);
891 if (!hash) return;
893 printf("Types (%s) hash:\n", strmname);
894 strmsize = pdb_get_stream_size(reader, types->hash_stream);
895 if (types->hash_offset + types->hash_size > strmsize ||
896 (types->last_index - types->first_index) * types->hash_value_size != types->hash_size ||
897 types->search_offset + types->search_size > strmsize ||
898 types->type_remap_offset + types->type_remap_size > strmsize)
900 printf("\nIncoherent sizes... skipping\n");
901 return;
903 printf("\n\tIndexes => hash value:\n");
904 for (i = types->first_index; i < types->last_index; i++)
906 printf("\t\t%08x => ", i);
907 pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + (i - types->first_index) * types->hash_value_size, types->hash_value_size);
908 printf("\n");
910 /* print collisions in hash table (if any) */
911 collision = malloc((types->last_index - types->first_index) * sizeof(unsigned));
912 if (collision)
914 unsigned head_printed = 0;
916 collision_arg.hash = (const BYTE*)hash + types->hash_offset;
917 collision_arg.hash_size = types->hash_value_size;
919 for (i = 0; i < types->last_index - types->first_index; i++) collision[i] = i;
920 qsort(collision, types->last_index - types->first_index, sizeof(unsigned), collision_compar);
921 for (i = 0; i < types->last_index - types->first_index; i++)
923 unsigned j;
924 for (j = i + 1; j < types->last_index - types->first_index; j++)
925 if (memcmp((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size,
926 (const BYTE*)hash + types->hash_offset + collision[j] * types->hash_value_size,
927 types->hash_value_size))
928 break;
929 if (j > i + 1)
931 unsigned k;
932 if (!head_printed)
934 printf("\n\t\tCollisions:\n");
935 head_printed = 1;
937 printf("\t\t\tHash ");
938 pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size, types->hash_value_size);
939 printf(":");
940 for (k = i; k < j; k++)
941 printf(" %x", types->first_index + collision[k]);
942 printf("\n");
943 i = j - 1;
946 free(collision);
948 printf("\n\tIndexes => offsets:\n");
949 table = (const unsigned*)((const BYTE*)hash + types->search_offset);
950 for (i = 0; i < types->search_size / (2 * sizeof(unsigned)); i += 2)
952 printf("\t\t%08x => %08x\n", table[2 * i + 0], table[2 * i + 1]);
955 if (types->type_remap_size)
957 unsigned num, capa, count_present, count_deleted;
958 const unsigned* present_bitset;
959 const unsigned* deleted_bitset;
961 printf("\n\tType remap:\n");
962 table = (const unsigned*)((const BYTE*)hash + types->type_remap_offset);
963 num = *table++;
964 capa = *table++;
965 count_present = *table++;
966 present_bitset = table;
967 table += count_present;
968 count_deleted = *table++;
969 deleted_bitset = table;
970 table += count_deleted;
971 printf("\t\tNumber of present entries: %u\n", num);
972 printf("\t\tCapacity: %u\n", capa);
973 printf("\t\tBitset present:\n");
974 printf("\t\t\tCount: %u\n", count_present);
975 printf("\t\t\tBitset: ");
976 pdb_dump_hash_value((const BYTE*)present_bitset, count_present * sizeof(unsigned));
977 printf("\n");
978 printf("\t\tBitset deleted:\n");
979 printf("\t\t\tCount: %u\n", count_deleted);
980 printf("\t\t\tBitset: ");
981 pdb_dump_hash_value((const BYTE*)deleted_bitset, count_deleted * sizeof(unsigned));
982 printf("\n");
983 for (i = 0; i < capa; ++i)
985 printf("\t\t%2u) %c",
987 is_bit_set(present_bitset, count_present, i) ? 'P' :
988 is_bit_set(deleted_bitset, count_deleted, i) ? 'D' : '_');
989 if (is_bit_set(present_bitset, count_present, i))
991 printf(" %s => ", pdb_get_string_table_entry(reader->global_string_table, *table++));
992 pdb_dump_hash_value((const BYTE*)table, types->hash_value_size);
993 table = (const unsigned*)((const BYTE*)table + types->hash_value_size);
995 printf("\n");
997 printf("\n");
999 free(hash);
1002 /* there are two 'type' related streams, but with different indexes... */
1003 static void pdb_dump_types(struct pdb_reader* reader, unsigned strmidx, const char* strmname)
1005 PDB_TYPES* types = NULL;
1006 BOOL used = has_stream_been_read(reader, strmidx);
1008 if (!globals_dump_sect(strmidx == 2 ? "TPI" : "IPI")) return;
1009 if (pdb_get_stream_size(reader, strmidx) < sizeof(*types))
1011 if (strmidx == 2)
1012 printf("-Too small type header\n");
1013 return;
1015 types = reader->read_stream(reader, strmidx);
1016 if (!types) return;
1018 switch (types->version)
1020 case 19950410: /* VC 4.0 */
1021 case 19951122:
1022 case 19961031: /* VC 5.0 / 6.0 */
1023 case 19990903: /* VC 7.0 */
1024 case 20040203: /* VC 8.0 */
1025 break;
1026 default:
1027 /* IPI stream is not always present in older PDB files */
1028 if (strmidx == 2)
1029 printf("-Unknown type info version %d\n", types->version);
1030 free(types);
1031 if (used) clear_stream_been_read(reader, strmidx);
1032 return;
1035 /* Read type table */
1036 printf("Types (%s):\n"
1037 "\tversion: %u\n"
1038 "\ttype_offset: %08x\n"
1039 "\tfirst_index: %x\n"
1040 "\tlast_index: %x\n"
1041 "\ttype_size: %x\n"
1042 "\thash_stream: %x\n"
1043 "\tpad: %x\n"
1044 "\thash_value_size: %x\n"
1045 "\thash_buckets %x\n"
1046 "\thash_offset: %x\n"
1047 "\thash_size: %x\n"
1048 "\tsearch_offset: %x\n"
1049 "\tsearch_size: %x\n"
1050 "\ttype_remap_offset: %x\n"
1051 "\ttype_remap_size: %x\n",
1052 strmname,
1053 types->version,
1054 types->type_offset,
1055 types->first_index,
1056 types->last_index,
1057 types->type_size,
1058 types->hash_stream,
1059 types->pad,
1060 types->hash_value_size,
1061 types->hash_num_buckets,
1062 types->hash_offset,
1063 types->hash_size,
1064 types->search_offset,
1065 types->search_size,
1066 types->type_remap_offset,
1067 types->type_remap_size);
1068 codeview_dump_types_from_block((const char*)types + types->type_offset, types->type_size);
1069 pdb_dump_types_hash(reader, types, strmname);
1070 free(types);
1073 static void pdb_dump_fpo(struct pdb_reader* reader, unsigned stream_idx)
1075 FPO_DATA* fpo;
1076 unsigned i, size;
1077 const char* frame_type[4] = {"Fpo", "Trap", "Tss", "NonFpo"};
1079 if (stream_idx == (WORD)-1) return;
1080 fpo = reader->read_stream(reader, stream_idx);
1081 size = pdb_get_stream_size(reader, stream_idx);
1082 if (fpo && (size % sizeof(*fpo)) == 0)
1084 size /= sizeof(*fpo);
1085 printf("FPO data:\n\t Start Length #loc #pmt #prolog #reg frame SEH /BP\n");
1086 for (i = 0; i < size; i++)
1088 printf("\t%08x %08x %4d %4d %7d %4d %6s %c %c\n",
1089 (UINT)fpo[i].ulOffStart, (UINT)fpo[i].cbProcSize, (UINT)fpo[i].cdwLocals, fpo[i].cdwParams,
1090 fpo[i].cbProlog, fpo[i].cbRegs, frame_type[fpo[i].cbFrame],
1091 fpo[i].fHasSEH ? 'Y' : 'N', fpo[i].fUseBP ? 'Y' : 'N');
1094 free(fpo);
1097 static void pdb_dump_fpo_ext(struct pdb_reader* reader, unsigned stream_idx)
1099 PDB_FPO_DATA* fpoext;
1100 unsigned i, size;
1102 if (stream_idx == (WORD)-1) return;
1104 fpoext = reader->read_stream(reader, stream_idx);
1105 size = pdb_get_stream_size(reader, stream_idx);
1106 if (fpoext && (size % sizeof(*fpoext)) == 0)
1108 size /= sizeof(*fpoext);
1109 printf("FPO data (extended):\n"
1110 "\t Start Length Locals Params MaxStack Prolog #SavedRegs Flags Command\n");
1111 for (i = 0; i < size; i++)
1113 printf("\t%08x %08x %8x %8x %8x %6x %8x %08x %s\n",
1114 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size, fpoext[i].params_size,
1115 fpoext[i].maxstack_size, fpoext[i].prolog_size, fpoext[i].savedregs_size, fpoext[i].flags,
1116 pdb_get_string_table_entry(reader->global_string_table, fpoext[i].str_offset));
1119 free(fpoext);
1122 static void pdb_dump_sections(struct pdb_reader* reader, unsigned stream_idx)
1124 const char* segs;
1125 DWORD size;
1126 const IMAGE_SECTION_HEADER* sect_hdr;
1128 if (stream_idx == (WORD)-1) return;
1129 segs = reader->read_stream(reader, stream_idx);
1131 if (segs)
1133 printf("Sections:\n");
1134 size = pdb_get_stream_size(reader, stream_idx);
1135 for (sect_hdr = (const IMAGE_SECTION_HEADER*)segs; (const char*)sect_hdr < segs + size; sect_hdr++)
1137 printf("\tSection: %-8.8s\n", sect_hdr->Name);
1138 printf("\t\tVirtual size: %08x\n", (unsigned)sect_hdr->Misc.VirtualSize);
1139 printf("\t\tVirtualAddress: %08x\n", (unsigned)sect_hdr->VirtualAddress);
1140 printf("\t\tSizeOfRawData: %08x\n", (unsigned)sect_hdr->SizeOfRawData);
1141 printf("\t\tPointerToRawData: %08x\n", (unsigned)sect_hdr->PointerToRawData);
1142 printf("\t\tPointerToRelocations: %08x\n", (unsigned)sect_hdr->PointerToRelocations);
1143 printf("\t\tPointerToLinenumbers: %08x\n", (unsigned)sect_hdr->PointerToLinenumbers);
1144 printf("\t\tNumberOfRelocations: %u\n", (unsigned)sect_hdr->NumberOfRelocations);
1145 printf("\t\tNumberOfLinenumbers: %u\n", (unsigned)sect_hdr->NumberOfLinenumbers);
1146 printf("\t\tCharacteristics: %08x", (unsigned)sect_hdr->Characteristics);
1147 dump_section_characteristics(sect_hdr->Characteristics, " ");
1148 printf("\n");
1150 free((char*)segs);
1154 static const char pdb2[] = "Microsoft C/C++ program database 2.00";
1156 static void pdb_jg_dump_header_root(struct pdb_reader* reader)
1158 UINT *pdw, *ok_bits;
1159 UINT i, numok, count;
1161 if (!globals_dump_sect("PDB")) return;
1163 printf("Header (JG):\n"
1164 "\tident: %.*s\n"
1165 "\tsignature: %08x\n"
1166 "\tblock_size: %08x\n"
1167 "\tfree_list_block: %04x\n"
1168 "\ttotal_alloc: %04x\n",
1169 (int)sizeof(pdb2) - 1, reader->u.jg.header->ident,
1170 reader->u.jg.header->signature,
1171 reader->u.jg.header->block_size,
1172 reader->u.jg.header->free_list_block,
1173 reader->u.jg.header->total_alloc);
1175 printf("Root:\n"
1176 "\tVersion: %u\n"
1177 "\tTimeDateStamp: %08x\n"
1178 "\tAge: %08x\n"
1179 "\tnames: %d\n",
1180 reader->u.jg.root->Version,
1181 reader->u.jg.root->TimeDateStamp,
1182 reader->u.jg.root->Age,
1183 (unsigned)reader->u.jg.root->cbNames);
1185 pdw = (UINT *)(reader->u.jg.root->names + reader->u.jg.root->cbNames);
1186 numok = *pdw++;
1187 count = *pdw++;
1188 printf("\tStreams directory:\n"
1189 "\t\tok: %08x\n"
1190 "\t\tcount: %08x\n"
1191 "\t\ttable:\n",
1192 numok, count);
1194 /* bitfield: first dword is len (in dword), then data */
1195 ok_bits = pdw;
1196 pdw += *ok_bits++ + 1;
1197 if (*pdw++ != 0)
1199 printf("unexpected value\n");
1200 return;
1203 for (i = 0; i < count; i++)
1205 if (ok_bits[i / 32] & (1 << (i % 32)))
1207 UINT string_idx, stream_idx;
1208 string_idx = *pdw++;
1209 stream_idx = *pdw++;
1210 printf("\t\t\t%2d) %-20s => %x\n", i, &reader->u.jg.root->names[string_idx], stream_idx);
1211 numok--;
1214 if (numok) printf(">>> unmatched present field with found\n");
1216 /* Check for unknown versions */
1217 switch (reader->u.jg.root->Version)
1219 case 19950623: /* VC 4.0 */
1220 case 19950814:
1221 case 19960307: /* VC 5.0 */
1222 case 19970604: /* VC 6.0 */
1223 break;
1224 default:
1225 printf("-Unknown root block version %d\n", reader->u.jg.root->Version);
1229 static void* pdb_ds_read(const struct PDB_DS_HEADER* header, const UINT *block_list, int size)
1231 int i, nBlocks;
1232 BYTE* buffer;
1234 if (!size) return NULL;
1236 nBlocks = (size + header->block_size - 1) / header->block_size;
1237 buffer = xmalloc(nBlocks * header->block_size);
1239 for (i = 0; i < nBlocks; i++)
1240 memcpy(buffer + i * header->block_size,
1241 (const char*)header + block_list[i] * header->block_size, header->block_size);
1243 return buffer;
1246 static void* pdb_ds_read_stream(struct pdb_reader* reader, DWORD stream_number)
1248 const UINT *block_list;
1249 UINT i;
1251 if (!reader->u.ds.toc || stream_number >= reader->u.ds.toc->num_streams) return NULL;
1253 mark_stream_been_read(reader, stream_number);
1254 if (reader->u.ds.toc->stream_size[stream_number] == 0 ||
1255 reader->u.ds.toc->stream_size[stream_number] == 0xFFFFFFFF)
1256 return NULL;
1257 block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1258 for (i = 0; i < stream_number; i++)
1259 block_list += (reader->u.ds.toc->stream_size[i] + reader->u.ds.header->block_size - 1) /
1260 reader->u.ds.header->block_size;
1262 return pdb_ds_read(reader->u.ds.header, block_list, reader->u.ds.toc->stream_size[stream_number]);
1265 static BOOL pdb_ds_init(struct pdb_reader* reader)
1267 reader->u.ds.header = PRD(0, sizeof(*reader->u.ds.header));
1268 if (!reader->u.ds.header) return FALSE;
1269 reader->read_stream = pdb_ds_read_stream;
1270 reader->u.ds.toc = pdb_ds_read(reader->u.ds.header,
1271 (const UINT *)((const char*)reader->u.ds.header + reader->u.ds.header->toc_block * reader->u.ds.header->block_size),
1272 reader->u.ds.header->toc_size);
1273 memset(reader->stream_used, 0, sizeof(reader->stream_used));
1274 reader->u.ds.root = reader->read_stream(reader, 1);
1275 if (!reader->u.ds.root) return FALSE;
1276 return TRUE;
1279 static const char pdb7[] = "Microsoft C/C++ MSF 7.00";
1281 static void pdb_ds_dump_header_root(struct pdb_reader* reader)
1283 unsigned int i, j, ofs;
1284 const UINT *block_list;
1285 UINT *pdw, *ok_bits;
1286 UINT numok, count;
1287 unsigned strmsize;
1289 if (!globals_dump_sect("PDB")) return;
1290 strmsize = pdb_get_stream_size(reader, 1);
1291 printf("Header (DS)\n"
1292 "\tsignature: %.*s\n"
1293 "\tblock_size: %08x\n"
1294 "\tfree_list_block: %08x\n"
1295 "\tnum_blocks: %08x\n"
1296 "\ttoc_size: %08x\n"
1297 "\tunknown2: %08x\n"
1298 "\ttoc_block: %08x\n",
1299 (int)sizeof(pdb7) - 1, reader->u.ds.header->signature,
1300 reader->u.ds.header->block_size,
1301 reader->u.ds.header->free_list_block,
1302 reader->u.ds.header->num_blocks,
1303 reader->u.ds.header->toc_size,
1304 reader->u.ds.header->unknown2,
1305 reader->u.ds.header->toc_block);
1307 block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1308 printf("\t\tnum_streams: %u\n", reader->u.ds.toc->num_streams);
1309 for (ofs = i = 0; i < reader->u.ds.toc->num_streams; i++)
1311 unsigned int nblk = (reader->u.ds.toc->stream_size[i] + reader->u.ds.header->block_size - 1) / reader->u.ds.header->block_size;
1312 printf("\t\tstream[%#x]:\tsize: %u\n", i, reader->u.ds.toc->stream_size[i]);
1313 if (nblk)
1315 for (j = 0; j < nblk; j++)
1317 if (j % 16 == 0) printf("\t\t\t");
1318 printf("%4x ", block_list[ofs + j]);
1319 if (j % 16 == 15 || (j + 1 == nblk)) printf("\n");
1321 ofs += nblk;
1325 printf("Root:\n"
1326 "\tVersion: %u\n"
1327 "\tTimeDateStamp: %08x\n"
1328 "\tAge: %08x\n"
1329 "\tguid %s\n"
1330 "\tcbNames: %08x\n",
1331 reader->u.ds.root->Version,
1332 reader->u.ds.root->TimeDateStamp,
1333 reader->u.ds.root->Age,
1334 get_guid_str(&reader->u.ds.root->guid),
1335 reader->u.ds.root->cbNames);
1336 pdw = (UINT *)(reader->u.ds.root->names + reader->u.ds.root->cbNames);
1337 numok = *pdw++;
1338 count = *pdw++;
1339 printf("\tStreams directory:\n"
1340 "\t\tok: %08x\n"
1341 "\t\tcount: %08x\n"
1342 "\t\ttable:\n",
1343 numok, count);
1345 /* bitfield: first dword is len (in dword), then data */
1346 ok_bits = pdw;
1347 pdw += *ok_bits++ + 1;
1348 if (*pdw++ != 0)
1350 printf("unexpected value\n");
1351 return;
1354 for (i = 0; i < count; i++)
1356 if (ok_bits[i / 32] & (1 << (i % 32)))
1358 UINT string_idx, stream_idx;
1359 string_idx = *pdw++;
1360 stream_idx = *pdw++;
1361 printf("\t\t\t%2d) %-20s => %x\n", i, &reader->u.ds.root->names[string_idx], stream_idx);
1362 numok--;
1365 if (numok) printf(">>> unmatched present field with found\n");
1366 if (*pdw++ != 0)
1368 printf("unexpected value\n");
1369 return;
1372 if (pdw + 1 <= (UINT*)((char*)reader->u.ds.root + strmsize))
1374 /* extra information (version reference and features) */
1375 printf("\tVersion and features\n");
1376 while (pdw + 1 <= (UINT*)((char*)reader->u.ds.root + strmsize))
1378 switch (*pdw)
1380 /* version reference */
1381 case 20091201: printf("\t\tVC110\n"); break;
1382 case 20140508: printf("\t\tVC140\n"); break;
1383 /* features */
1384 case 0x4D544F4E /* NOTM */: printf("\t\tNo type merge\n"); break;
1385 case 0x494E494D /* MINI */: printf("\t\tMinimal debug info\n"); break;
1386 default: printf("\t\tUnknown value %x\n", *pdw);
1388 pdw++;
1393 enum FileSig get_kind_pdb(void)
1395 const char* head;
1397 head = PRD(0, sizeof(pdb2) - 1);
1398 if (head && !memcmp(head, pdb2, sizeof(pdb2) - 1))
1399 return SIG_PDB;
1400 head = PRD(0, sizeof(pdb7) - 1);
1401 if (head && !memcmp(head, pdb7, sizeof(pdb7) - 1))
1402 return SIG_PDB;
1403 return SIG_UNKNOWN;
1406 void pdb_dump(void)
1408 const BYTE* head;
1409 const char** saved_dumpsect = globals.dumpsect;
1410 static const char* default_dumpsect[] = {"DBI", "TPI", "IPI", NULL};
1411 struct pdb_reader reader;
1413 if (!globals.dumpsect) globals.dumpsect = default_dumpsect;
1415 if ((head = PRD(0, sizeof(pdb2) - 1)) && !memcmp(head, pdb2, sizeof(pdb2) - 1))
1417 if (!pdb_jg_init(&reader))
1419 printf("Unable to get header information\n");
1420 return;
1423 pdb_jg_dump_header_root(&reader);
1425 else if ((head = PRD(0, sizeof(pdb7) - 1)) && !memcmp(head, pdb7, sizeof(pdb7) - 1))
1427 if (!pdb_ds_init(&reader))
1429 printf("Unable to get header information\n");
1430 return;
1432 pdb_ds_dump_header_root(&reader);
1434 mark_stream_been_read(&reader, 0); /* mark stream #0 (old TOC) as read */
1436 reader.global_string_table = read_string_table(&reader);
1438 pdb_dump_types(&reader, 2, "TPI");
1439 pdb_dump_types(&reader, 4, "IPI");
1440 pdb_dump_symbols(&reader);
1442 pdb_exit(&reader);
1444 globals.dumpsect = saved_dumpsect;