winedump: Explain a bit more errors on hash header.
[wine.git] / tools / winedump / pdb.c
blob5ab1b515691b6da70c4a9df8e1c5a9ec6200116b
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];
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)
71 int i, nBlocks;
72 BYTE* buffer;
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);
83 return buffer;
86 static void* pdb_jg_read_stream(struct pdb_reader* reader, DWORD stream_nr)
88 const WORD* block_list;
89 DWORD i;
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)
96 return NULL;
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;
120 else
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;
128 else
129 return reader->u.ds.toc->stream_size[idx];
132 static void pdb_exit(struct pdb_reader* reader)
134 unsigned i, size;
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, " ");
148 free(stream);
151 if (reader->read_stream == pdb_jg_read_stream)
153 free((char*)reader->u.jg.root);
154 free((char*)reader->u.jg.toc);
156 else
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)
165 DWORD* pdw;
166 DWORD* ok_bits;
167 DWORD cbstr, count;
168 DWORD string_idx, stream_idx;
169 unsigned i;
170 const char* str;
172 if (reader->read_stream == pdb_jg_read_stream)
174 str = reader->u.jg.root->names;
175 cbstr = reader->u.jg.root->cbNames;
177 else
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 */
185 count = *pdw++;
187 /* bitfield: first dword is len (in dword), then data */
188 ok_bits = pdw;
189 pdw += *ok_bits++ + 1;
190 if (*pdw++ != 0)
192 printf("unexpected value\n");
193 return -1;
196 for (i = 0; i < count; i++)
198 if (ok_bits[i / 32] & (1 << (i % 32)))
200 string_idx = *pdw++;
201 stream_idx = *pdw++;
202 if (!strcmp(name, &str[string_idx])) return stream_idx;
205 return -1;
208 static PDB_STRING_TABLE* read_string_table(struct pdb_reader* reader)
210 unsigned stream_idx;
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, " ");
222 free( ret );
223 return NULL;
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);
254 else
255 printf("%s\t\tIncorrect hash structure\n", pfx);
257 else
259 unsigned i;
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)))
275 unsigned j;
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);
279 index++;
281 else
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)));
292 else
293 printf("%sNo header in symbols hash\n", pfx);
296 static void dump_global_symbol(struct pdb_reader* reader, unsigned stream)
298 void* global = NULL;
299 DWORD size;
301 global = reader->read_stream(reader, stream);
302 if (!global) return;
304 size = pdb_get_stream_size(reader, stream);
306 dump_dbi_hash_table(global, size, "Global", "");
307 free(global);
310 static void dump_public_symbol(struct pdb_reader* reader, unsigned stream)
312 unsigned size;
313 DBI_PUBLIC_HEADER* hdr;
315 hdr = reader->read_stream(reader, stream);
316 if (!hdr) return;
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");
331 free(hdr);
334 static void pdb_dump_symbols(struct pdb_reader* reader, PDB_STREAM_INDEXES* sidx)
336 PDB_SYMBOLS* symbols;
337 unsigned char* modimage;
338 const char* file;
339 PDB_STRING_TABLE* filesimage;
340 char tcver[32];
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)
350 case 0: /* VC 4.0 */
351 case 19960307: /* VC 5.0 */
352 case 19970606: /* VC 6.0 */
353 case 19990903: /* VC 7.0 */
354 break;
355 default:
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);
360 else
361 sprintf(tcver, "old-%x", symbols->flags);
362 printf("Symbols:\n"
363 "\tsignature: %08x\n"
364 "\tversion: %u\n"
365 "\tage: %08x\n"
366 "\tglobal_hash_stream: %u\n"
367 "\tbuilder: %s\n"
368 "\tpublic_stream: %u\n"
369 "\tbldVer: %u\n"
370 "\tgsym_stream: %u\n"
371 "\trbldVer: %u\n"
372 "\tmodule_size: %08x\n"
373 "\toffset_size: %08x\n"
374 "\thash_size: %08x\n"
375 "\tsrc_module_size: %08x\n"
376 "\tpdbimport_size: %08x\n"
377 "\tresvd0: %08x\n"
378 "\tstream_idx_size: %08x\n"
379 "\tunknown2_size: %08x\n"
380 "\tresvd3: %04x\n"
381 "\tmachine: %s\n"
382 "\tresvd4 %08x\n",
383 symbols->signature,
384 symbols->version,
385 symbols->age,
386 symbols->global_hash_stream,
387 tcver, /* from symbols->flags */
388 symbols->public_stream,
389 symbols->bldVer,
390 symbols->gsym_stream,
391 symbols->rbldVer,
392 symbols->module_size,
393 symbols->offset_size,
394 symbols->hash_size,
395 symbols->srcmodule_size,
396 symbols->pdbimport_size,
397 symbols->resvd0,
398 symbols->stream_index_size,
399 symbols->unknown2_size,
400 symbols->resvd3,
401 get_machine_str( symbols->machine ),
402 symbols->resvd4);
404 if (symbols->offset_size)
406 const BYTE* src;
408 printf("\t----------offsets------------\n");
409 src = (const BYTE*)((const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size);
410 dump_data(src, symbols->offset_size, " ");
413 if (!(filesimage = read_string_table(reader))) printf("string table not found\n");
415 if (symbols->srcmodule_size)
417 const PDB_SYMBOL_SOURCE*src;
418 int i, j, cfile;
419 const WORD* indx;
420 const DWORD* offset;
421 const char* start_cstr;
422 const char* cstr;
424 printf("\t----------src module------------\n");
425 src = (const PDB_SYMBOL_SOURCE*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
426 symbols->module_size + symbols->offset_size + symbols->hash_size);
427 printf("\tSource Modules\n"
428 "\t\tnModules: %u\n"
429 "\t\tnSrcFiles: %u\n",
430 src->nModules, src->nSrcFiles);
432 /* usage of table seems to be as follows:
433 * two arrays of WORD (src->nModules as size)
434 * - first array contains index into files for "module" compilation
435 * (module = compilation unit ??)
436 * - second array contains the number of source files in module
437 * an array of DWORD (src->nSrcFiles as size)
438 * - contains offset (in following string table) of the source file name
439 * a string table
440 * - each string is a pascal string (ie. with its length as first BYTE) or
441 * 0-terminated string (depending on version)
443 indx = &src->table[src->nModules];
444 offset = (const DWORD*)&src->table[2 * src->nModules];
445 cstr = (const char*)&src->table[2 * (src->nModules + src->nSrcFiles)];
446 start_cstr = cstr;
448 for (i = cfile = 0; i < src->nModules; i++)
450 printf("\t\tModule[%2d]:\n", i);
451 cfile = src->table[i];
452 for (j = cfile; j < src->nSrcFiles && j < cfile + indx[i]; j++)
454 /* FIXME: in some cases, it's a p_string but WHEN ? */
455 if (cstr + offset[j] >= start_cstr /* wrap around */ &&
456 cstr + offset[j] < (const char*)src + symbols->srcmodule_size)
457 printf("\t\t\tSource file: %s\n", cstr + offset[j]);
458 else
459 printf("\t\t\tSource file: <<out of bounds>>\n");
463 if (symbols->pdbimport_size)
465 const PDB_SYMBOL_IMPORT* imp;
466 const char* first;
467 const char* last;
468 const char* ptr;
470 printf("\t------------import--------------\n");
471 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
472 symbols->module_size + symbols->offset_size +
473 symbols->hash_size + symbols->srcmodule_size);
474 first = (const char*)imp;
475 last = (const char*)imp + symbols->pdbimport_size;
476 while (imp < (const PDB_SYMBOL_IMPORT*)last)
478 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
479 printf("\tImport: %lx\n"
480 "\t\tUnknown1: %08x\n"
481 "\t\tUnknown2: %08x\n"
482 "\t\tTimeDateStamp: %08x\n"
483 "\t\tAge: %08u\n"
484 "\t\tfile1: %s\n"
485 "\t\tfile2: %s\n",
486 (ULONG_PTR)((const char*)imp - first),
487 imp->unknown1,
488 imp->unknown2,
489 imp->TimeDateStamp,
490 imp->Age,
491 imp->filename,
492 ptr);
493 imp = (const PDB_SYMBOL_IMPORT*)(first + ((ptr - first + strlen(ptr) + 1 + 3) & ~3));
496 if (symbols->stream_index_size)
498 printf("\t------------stream indexes--------------\n");
499 switch (symbols->stream_index_size)
501 case sizeof(PDB_STREAM_INDEXES_OLD):
502 /* PDB_STREAM_INDEXES is a superset of PDB_STREAM_INDEX_OLD
503 * FIXME: to be confirmed when all fields are fully understood
505 memcpy(sidx,
506 (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
507 symbols->offset_size + symbols->hash_size + symbols->srcmodule_size +
508 symbols->pdbimport_size + symbols->unknown2_size,
509 sizeof(PDB_STREAM_INDEXES_OLD));
510 printf("\tFPO: %04x\n"
511 "\t?: %04x\n"
512 "\t?: %04x\n"
513 "\t?: %04x\n"
514 "\t?: %04x\n"
515 "\tSections stream: %04x\n",
516 sidx->FPO, sidx->unk0, sidx->unk1, sidx->unk2, sidx->unk3,
517 sidx->sections_stream);
518 break;
519 case sizeof(PDB_STREAM_INDEXES):
520 memcpy(sidx,
521 (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
522 symbols->offset_size + symbols->hash_size + symbols->srcmodule_size +
523 symbols->pdbimport_size + symbols->unknown2_size,
524 sizeof(*sidx));
525 printf("\tFPO: %04x\n"
526 "\t?: %04x\n"
527 "\t?: %04x\n"
528 "\t?: %04x\n"
529 "\t?: %04x\n"
530 "\tSection stream: %04x\n"
531 "\t?: %04x\n"
532 "\t?: %04x\n"
533 "\t?: %04x\n"
534 "\tFPO-ext: %04x\n"
535 "\t?: %04x\n",
536 sidx->FPO, sidx->unk0, sidx->unk1, sidx->unk2, sidx->unk3,
537 sidx->sections_stream, sidx->unk4, sidx->unk5, sidx->unk6, sidx->FPO_EXT,
538 sidx->unk7);
539 break;
540 default:
541 printf("unexpected size for stream index %d\n", symbols->stream_index_size);
542 break;
546 /* Read global symbol table */
547 modimage = reader->read_stream(reader, symbols->gsym_stream);
548 if (modimage)
550 printf("\t------------globals-------------\n");
551 codeview_dump_symbols(modimage, 0, pdb_get_stream_size(reader, symbols->gsym_stream));
552 free(modimage);
555 /* Read per-module symbol / linenumber tables */
556 file = (const char*)symbols + sizeof(PDB_SYMBOLS);
557 while (file - (const char*)symbols < sizeof(PDB_SYMBOLS) + symbols->module_size)
559 int stream_nr, symbol_size, lineno_size, lineno2_size;
560 const char* file_name;
561 const char* lib_name;
563 if (symbols->version < 19970000)
565 const PDB_SYMBOL_FILE* sym_file = (const PDB_SYMBOL_FILE*) file;
566 stream_nr = sym_file->stream;
567 file_name = sym_file->filename;
568 lib_name = file_name + strlen(file_name) + 1;
569 symbol_size = sym_file->symbol_size;
570 lineno_size = sym_file->lineno_size;
571 lineno2_size = sym_file->lineno2_size;
572 printf("\t--------symbol file-----------\n");
573 printf("\tName: %s\n", file_name);
574 if (strcmp(file_name, lib_name)) printf("\tLibrary: %s\n", lib_name);
575 printf("\t\tunknown1: %08x\n"
576 "\t\trange\n"
577 "\t\t\tsegment: %04x\n"
578 "\t\t\tpad1: %04x\n"
579 "\t\t\toffset: %08x\n"
580 "\t\t\tsize: %08x\n"
581 "\t\t\tcharacteristics: %08x\n"
582 "\t\t\tindex: %04x\n"
583 "\t\t\tpad2: %04x\n"
584 "\t\tflag: %04x\n"
585 "\t\tstream: %04x\n"
586 "\t\tsymb size: %08x\n"
587 "\t\tline size: %08x\n"
588 "\t\tline2 size: %08x\n"
589 "\t\tnSrcFiles: %08x\n"
590 "\t\tattribute: %08x\n",
591 sym_file->unknown1,
592 sym_file->range.segment,
593 sym_file->range.pad1,
594 sym_file->range.offset,
595 sym_file->range.size,
596 sym_file->range.characteristics,
597 sym_file->range.index,
598 sym_file->range.pad2,
599 sym_file->flag,
600 sym_file->stream,
601 sym_file->symbol_size,
602 sym_file->lineno_size,
603 sym_file->lineno2_size,
604 sym_file->nSrcFiles,
605 sym_file->attribute);
607 else
609 const PDB_SYMBOL_FILE_EX* sym_file = (const PDB_SYMBOL_FILE_EX*) file;
611 stream_nr = sym_file->stream;
612 file_name = sym_file->filename;
613 lib_name = file_name + strlen(file_name) + 1;
614 symbol_size = sym_file->symbol_size;
615 lineno_size = sym_file->lineno_size;
616 lineno2_size = sym_file->lineno2_size;
617 printf("\t--------symbol file-----------\n");
618 printf("\tName: %s\n", file_name);
619 if (strcmp(file_name, lib_name)) printf("\tLibrary: %s\n", lib_name);
620 printf("\t\tunknown1: %08x\n"
621 "\t\trange\n"
622 "\t\t\tsegment: %04x\n"
623 "\t\t\tpad1: %04x\n"
624 "\t\t\toffset: %08x\n"
625 "\t\t\tsize: %08x\n"
626 "\t\t\tcharacteristics: %08x\n"
627 "\t\t\tindex: %04x\n"
628 "\t\t\tpad2: %04x\n"
629 "\t\t\ttimestamp: %08x\n"
630 "\t\t\tunknown: %08x\n"
631 "\t\tflag: %04x\n"
632 "\t\tstream: %04x\n"
633 "\t\tsymb size: %08x\n"
634 "\t\tline size: %08x\n"
635 "\t\tline2 size: %08x\n"
636 "\t\tnSrcFiles: %08x\n"
637 "\t\tattribute: %08x\n"
638 "\t\treserved/0: %08x\n"
639 "\t\treserved/1: %08x\n",
640 sym_file->unknown1,
641 sym_file->range.segment,
642 sym_file->range.pad1,
643 sym_file->range.offset,
644 sym_file->range.size,
645 sym_file->range.characteristics,
646 sym_file->range.index,
647 sym_file->range.pad2,
648 sym_file->range.timestamp,
649 sym_file->range.unknown,
650 sym_file->flag,
651 sym_file->stream,
652 sym_file->symbol_size,
653 sym_file->lineno_size,
654 sym_file->lineno2_size,
655 sym_file->nSrcFiles,
656 sym_file->attribute,
657 sym_file->reserved[0],
658 sym_file->reserved[1]);
660 modimage = reader->read_stream(reader, stream_nr);
661 if (modimage)
663 int total_size = pdb_get_stream_size(reader, stream_nr);
665 if (symbol_size)
666 codeview_dump_symbols((const char*)modimage, sizeof(DWORD), symbol_size);
668 /* line number info */
669 if (lineno_size)
670 codeview_dump_linetab((const char*)modimage + symbol_size, TRUE, " ");
671 else if (lineno2_size) /* actually, only one of the 2 lineno should be present */
672 codeview_dump_linetab2((const char*)modimage + symbol_size, lineno2_size,
673 filesimage, " ");
674 /* what's that part ??? */
675 if (0)
676 dump_data(modimage + symbol_size + lineno_size + lineno2_size,
677 total_size - (symbol_size + lineno_size + lineno2_size), " ");
678 free(modimage);
681 file = (char*)((DWORD_PTR)(lib_name + strlen(lib_name) + 1 + 3) & ~3);
683 dump_global_symbol(reader, symbols->global_hash_stream);
684 dump_public_symbol(reader, symbols->public_stream);
686 free(symbols);
687 free(filesimage);
690 static BOOL is_bit_set(const unsigned* dw, unsigned len, unsigned i)
692 if (i >= len * sizeof(unsigned) * 8) return FALSE;
693 return (dw[i >> 5] & (1u << (i & 31u))) != 0;
696 static void pdb_dump_hash_value(const BYTE* ptr, unsigned len)
698 int i;
700 printf("[");
701 for (i = len - 1; i >= 0; i--)
702 printf("%02x", ptr[i]);
703 printf("]");
706 static struct
708 const BYTE* hash;
709 unsigned hash_size;
710 } collision_arg;
712 static int collision_compar(const void *p1, const void *p2)
714 unsigned idx1 = *(unsigned*)p1;
715 unsigned idx2 = *(unsigned*)p2;
716 return memcmp(collision_arg.hash + idx1 * collision_arg.hash_size,
717 collision_arg.hash + idx2 * collision_arg.hash_size,
718 collision_arg.hash_size);
721 static void pdb_dump_types_hash(struct pdb_reader* reader, const PDB_TYPES* types, const char* strmname)
723 void* hash = NULL;
724 unsigned i, strmsize;
725 const unsigned* table;
726 PDB_STRING_TABLE* strbase;
727 unsigned *collision;
728 hash = reader->read_stream(reader, types->hash_stream);
729 if (!hash) return;
731 printf("Types (%s) hash:\n", strmname);
732 strmsize = pdb_get_stream_size(reader, types->hash_stream);
733 if (types->hash_offset + types->hash_size > strmsize ||
734 (types->last_index - types->first_index) * types->hash_value_size != types->hash_size ||
735 types->search_offset + types->search_size > strmsize ||
736 types->type_remap_offset + types->type_remap_size > strmsize)
738 printf("\nIncoherent sizes... skipping\n");
739 return;
741 printf("\n\tIndexes => hash value:\n");
742 for (i = types->first_index; i < types->last_index; i++)
744 printf("\t\t%08x => ", i);
745 pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + (i - types->first_index) * types->hash_value_size, types->hash_value_size);
746 printf("\n");
748 /* print collisions in hash table (if any) */
749 collision = malloc((types->last_index - types->first_index) * sizeof(unsigned));
750 if (collision)
752 unsigned head_printed = 0;
754 collision_arg.hash = (const BYTE*)hash + types->hash_offset;
755 collision_arg.hash_size = types->hash_value_size;
757 for (i = 0; i < types->last_index - types->first_index; i++) collision[i] = i;
758 qsort(collision, types->last_index - types->first_index, sizeof(unsigned), collision_compar);
759 for (i = 0; i < types->last_index - types->first_index; i++)
761 unsigned j;
762 for (j = i + 1; j < types->last_index - types->first_index; j++)
763 if (memcmp((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size,
764 (const BYTE*)hash + types->hash_offset + collision[j] * types->hash_value_size,
765 types->hash_value_size))
766 break;
767 if (j > i + 1)
769 unsigned k;
770 if (!head_printed)
772 printf("\n\t\tCollisions:\n");
773 head_printed = 1;
775 printf("\t\t\tHash ");
776 pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size, types->hash_value_size);
777 printf(":");
778 for (k = i; k < j; k++)
779 printf(" %x", types->first_index + collision[k]);
780 printf("\n");
781 i = j - 1;
784 free(collision);
786 printf("\n\tIndexes => offsets:\n");
787 table = (const unsigned*)((const BYTE*)hash + types->search_offset);
788 for (i = 0; i < types->search_size / (2 * sizeof(unsigned)); i += 2)
790 printf("\t\t%08x => %08x\n", table[2 * i + 0], table[2 * i + 1]);
792 if (types->type_remap_size && (strbase = read_string_table(reader)))
794 unsigned num, capa, count_present, count_deleted;
795 const unsigned* present_bitset;
796 const unsigned* deleted_bitset;
798 printf("\n\tType remap:\n");
799 table = (const unsigned*)((const BYTE*)hash + types->type_remap_offset);
800 num = *table++;
801 capa = *table++;
802 count_present = *table++;
803 present_bitset = table;
804 table += count_present;
805 count_deleted = *table++;
806 deleted_bitset = table;
807 table += count_deleted;
808 printf("\t\tNumber of present entries: %u\n", num);
809 printf("\t\tCapacity: %u\n", capa);
810 printf("\t\tBitset present:\n");
811 printf("\t\t\tCount: %u\n", count_present);
812 printf("\t\t\tBitset: ");
813 pdb_dump_hash_value((const BYTE*)present_bitset, count_present * sizeof(unsigned));
814 printf("\n");
815 printf("\t\tBitset deleted:\n");
816 printf("\t\t\tCount: %u\n", count_deleted);
817 printf("\t\t\tBitset: ");
818 pdb_dump_hash_value((const BYTE*)deleted_bitset, count_deleted * sizeof(unsigned));
819 printf("\n");
820 for (i = 0; i < capa; ++i)
822 printf("\t\t%2u) %c",
824 is_bit_set(present_bitset, count_present, i) ? 'P' :
825 is_bit_set(deleted_bitset, count_deleted, i) ? 'D' : '_');
826 if (is_bit_set(present_bitset, count_present, i))
828 printf(" %s => ", pdb_get_string_table_entry(strbase, *table++));
829 pdb_dump_hash_value((const BYTE*)table, types->hash_value_size);
830 table = (const unsigned*)((const BYTE*)table + types->hash_value_size);
832 printf("\n");
834 free(strbase);
835 printf("\n");
837 free(hash);
840 /* there are two 'type' related streams, but with different indexes... */
841 static void pdb_dump_types(struct pdb_reader* reader, unsigned strmidx, const char* strmname)
843 PDB_TYPES* types = NULL;
844 BOOL used = has_stream_been_read(reader, strmidx);
846 if (pdb_get_stream_size(reader, strmidx) < sizeof(*types))
848 if (strmidx == 2)
849 printf("-Too small type header\n");
850 return;
852 types = reader->read_stream(reader, strmidx);
853 if (!types) return;
855 switch (types->version)
857 case 19950410: /* VC 4.0 */
858 case 19951122:
859 case 19961031: /* VC 5.0 / 6.0 */
860 case 19990903: /* VC 7.0 */
861 case 20040203: /* VC 8.0 */
862 break;
863 default:
864 /* IPI stream is not always present in older PDB files */
865 if (strmidx == 2)
866 printf("-Unknown type info version %d\n", types->version);
867 free(types);
868 if (used) clear_stream_been_read(reader, strmidx);
869 return;
872 /* Read type table */
873 printf("Types (%s):\n"
874 "\tversion: %u\n"
875 "\ttype_offset: %08x\n"
876 "\tfirst_index: %x\n"
877 "\tlast_index: %x\n"
878 "\ttype_size: %x\n"
879 "\thash_stream: %x\n"
880 "\tpad: %x\n"
881 "\thash_value_size: %x\n"
882 "\thash_buckets %x\n"
883 "\thash_offset: %x\n"
884 "\thash_size: %x\n"
885 "\tsearch_offset: %x\n"
886 "\tsearch_size: %x\n"
887 "\ttype_remap_offset: %x\n"
888 "\ttype_remap_size: %x\n",
889 strmname,
890 types->version,
891 types->type_offset,
892 types->first_index,
893 types->last_index,
894 types->type_size,
895 types->hash_stream,
896 types->pad,
897 types->hash_value_size,
898 types->hash_num_buckets,
899 types->hash_offset,
900 types->hash_size,
901 types->search_offset,
902 types->search_size,
903 types->type_remap_offset,
904 types->type_remap_size);
905 codeview_dump_types_from_block((const char*)types + types->type_offset, types->type_size);
906 pdb_dump_types_hash(reader, types, strmname);
907 free(types);
910 static void pdb_dump_fpo(struct pdb_reader* reader, unsigned stream_idx)
912 FPO_DATA* fpo;
913 unsigned i, size;
914 const char* frame_type[4] = {"Fpo", "Trap", "Tss", "NonFpo"};
916 if (stream_idx == (WORD)-1) return;
917 fpo = reader->read_stream(reader, stream_idx);
918 size = pdb_get_stream_size(reader, stream_idx);
919 if (fpo && (size % sizeof(*fpo)) == 0)
921 size /= sizeof(*fpo);
922 printf("FPO data:\n\t Start Length #loc #pmt #prolog #reg frame SEH /BP\n");
923 for (i = 0; i < size; i++)
925 printf("\t%08x %08x %4d %4d %7d %4d %6s %c %c\n",
926 (UINT)fpo[i].ulOffStart, (UINT)fpo[i].cbProcSize, (UINT)fpo[i].cdwLocals, fpo[i].cdwParams,
927 fpo[i].cbProlog, fpo[i].cbRegs, frame_type[fpo[i].cbFrame],
928 fpo[i].fHasSEH ? 'Y' : 'N', fpo[i].fUseBP ? 'Y' : 'N');
931 free(fpo);
934 static void pdb_dump_fpo_ext(struct pdb_reader* reader, unsigned stream_idx)
936 PDB_FPO_DATA* fpoext;
937 unsigned i, size;
938 PDB_STRING_TABLE* strbase;
940 if (stream_idx == (WORD)-1) return;
941 strbase = read_string_table(reader);
942 if (!strbase) return;
944 fpoext = reader->read_stream(reader, stream_idx);
945 size = pdb_get_stream_size(reader, stream_idx);
946 if (fpoext && (size % sizeof(*fpoext)) == 0)
948 size /= sizeof(*fpoext);
949 printf("FPO data (extended):\n"
950 "\t Start Length Locals Params MaxStack Prolog #SavedRegs Flags Command\n");
951 for (i = 0; i < size; i++)
953 printf("\t%08x %08x %8x %8x %8x %6x %8x %08x %s\n",
954 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size, fpoext[i].params_size,
955 fpoext[i].maxstack_size, fpoext[i].prolog_size, fpoext[i].savedregs_size, fpoext[i].flags,
956 pdb_get_string_table_entry(strbase, fpoext[i].str_offset));
959 free(fpoext);
960 free(strbase);
963 static void pdb_dump_sections(struct pdb_reader* reader, unsigned stream_idx)
965 const char* segs;
966 DWORD size;
967 const IMAGE_SECTION_HEADER* sect_hdr;
969 if (stream_idx == (WORD)-1) return;
970 segs = reader->read_stream(reader, stream_idx);
972 if (segs)
974 printf("Sections:\n");
975 size = pdb_get_stream_size(reader, stream_idx);
976 for (sect_hdr = (const IMAGE_SECTION_HEADER*)segs; (const char*)sect_hdr < segs + size; sect_hdr++)
978 printf("\tSection: %-8.8s\n", sect_hdr->Name);
979 printf("\t\tVirtual size: %08x\n", (unsigned)sect_hdr->Misc.VirtualSize);
980 printf("\t\tVirtualAddress: %08x\n", (unsigned)sect_hdr->VirtualAddress);
981 printf("\t\tSizeOfRawData: %08x\n", (unsigned)sect_hdr->SizeOfRawData);
982 printf("\t\tPointerToRawData: %08x\n", (unsigned)sect_hdr->PointerToRawData);
983 printf("\t\tPointerToRelocations: %08x\n", (unsigned)sect_hdr->PointerToRelocations);
984 printf("\t\tPointerToLinenumbers: %08x\n", (unsigned)sect_hdr->PointerToLinenumbers);
985 printf("\t\tNumberOfRelocations: %u\n", (unsigned)sect_hdr->NumberOfRelocations);
986 printf("\t\tNumberOfLinenumbers: %u\n", (unsigned)sect_hdr->NumberOfLinenumbers);
987 printf("\t\tCharacteristics: %08x", (unsigned)sect_hdr->Characteristics);
988 dump_section_characteristics(sect_hdr->Characteristics, " ");
989 printf("\n");
991 free((char*)segs);
995 static const char pdb2[] = "Microsoft C/C++ program database 2.00";
997 static void pdb_jg_dump(void)
999 struct pdb_reader reader;
1002 * Read in TOC and well-known streams
1004 pdb_jg_init(&reader);
1005 printf("Header (JG):\n"
1006 "\tident: %.*s\n"
1007 "\tsignature: %08x\n"
1008 "\tblock_size: %08x\n"
1009 "\tfree_list_block: %04x\n"
1010 "\ttotal_alloc: %04x\n",
1011 (int)sizeof(pdb2) - 1, reader.u.jg.header->ident,
1012 reader.u.jg.header->signature,
1013 reader.u.jg.header->block_size,
1014 reader.u.jg.header->free_list_block,
1015 reader.u.jg.header->total_alloc);
1017 reader.u.jg.root = reader.read_stream(&reader, 1);
1018 if (reader.u.jg.root)
1020 UINT *pdw, *ok_bits;
1021 UINT i, numok, count;
1022 PDB_STREAM_INDEXES sidx;
1024 printf("Root:\n"
1025 "\tVersion: %u\n"
1026 "\tTimeDateStamp: %08x\n"
1027 "\tAge: %08x\n"
1028 "\tnames: %d\n",
1029 reader.u.jg.root->Version,
1030 reader.u.jg.root->TimeDateStamp,
1031 reader.u.jg.root->Age,
1032 (unsigned)reader.u.jg.root->cbNames);
1034 pdw = (UINT *)(reader.u.jg.root->names + reader.u.jg.root->cbNames);
1035 numok = *pdw++;
1036 count = *pdw++;
1037 printf("\tStreams directory:\n"
1038 "\t\tok: %08x\n"
1039 "\t\tcount: %08x\n"
1040 "\t\ttable:\n",
1041 numok, count);
1043 /* bitfield: first dword is len (in dword), then data */
1044 ok_bits = pdw;
1045 pdw += *ok_bits++ + 1;
1046 if (*pdw++ != 0)
1048 printf("unexpected value\n");
1049 return;
1052 for (i = 0; i < count; i++)
1054 if (ok_bits[i / 32] & (1 << (i % 32)))
1056 UINT string_idx, stream_idx;
1057 string_idx = *pdw++;
1058 stream_idx = *pdw++;
1059 printf("\t\t\t%2d) %-20s => %x\n", i, &reader.u.jg.root->names[string_idx], stream_idx);
1060 numok--;
1063 if (numok) printf(">>> unmatched present field with found\n");
1065 /* Check for unknown versions */
1066 switch (reader.u.jg.root->Version)
1068 case 19950623: /* VC 4.0 */
1069 case 19950814:
1070 case 19960307: /* VC 5.0 */
1071 case 19970604: /* VC 6.0 */
1072 break;
1073 default:
1074 printf("-Unknown root block version %d\n", reader.u.jg.root->Version);
1076 pdb_dump_types(&reader, 2, "TPI");
1077 pdb_dump_types(&reader, 4, "IPI");
1078 pdb_dump_symbols(&reader, &sidx);
1079 pdb_dump_fpo(&reader, sidx.FPO);
1080 pdb_dump_sections(&reader, sidx.sections_stream);
1082 else printf("-Unable to get root\n");
1084 pdb_exit(&reader);
1087 static void* pdb_ds_read(const struct PDB_DS_HEADER* header, const UINT *block_list, int size)
1089 int i, nBlocks;
1090 BYTE* buffer;
1092 if (!size) return NULL;
1094 nBlocks = (size + header->block_size - 1) / header->block_size;
1095 buffer = xmalloc(nBlocks * header->block_size);
1097 for (i = 0; i < nBlocks; i++)
1098 memcpy(buffer + i * header->block_size,
1099 (const char*)header + block_list[i] * header->block_size, header->block_size);
1101 return buffer;
1104 static void* pdb_ds_read_stream(struct pdb_reader* reader, DWORD stream_number)
1106 const UINT *block_list;
1107 UINT i;
1109 if (!reader->u.ds.toc || stream_number >= reader->u.ds.toc->num_streams) return NULL;
1111 mark_stream_been_read(reader, stream_number);
1112 if (reader->u.ds.toc->stream_size[stream_number] == 0 ||
1113 reader->u.ds.toc->stream_size[stream_number] == 0xFFFFFFFF)
1114 return NULL;
1115 block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1116 for (i = 0; i < stream_number; i++)
1117 block_list += (reader->u.ds.toc->stream_size[i] + reader->u.ds.header->block_size - 1) /
1118 reader->u.ds.header->block_size;
1120 return pdb_ds_read(reader->u.ds.header, block_list, reader->u.ds.toc->stream_size[stream_number]);
1123 static BOOL pdb_ds_init(struct pdb_reader* reader)
1125 reader->u.ds.header = PRD(0, sizeof(*reader->u.ds.header));
1126 if (!reader->u.ds.header) return FALSE;
1127 reader->read_stream = pdb_ds_read_stream;
1128 reader->u.ds.toc = pdb_ds_read(reader->u.ds.header,
1129 (const UINT *)((const char*)reader->u.ds.header + reader->u.ds.header->toc_block * reader->u.ds.header->block_size),
1130 reader->u.ds.header->toc_size);
1131 memset(reader->stream_used, 0, sizeof(reader->stream_used));
1132 return TRUE;
1135 static const char pdb7[] = "Microsoft C/C++ MSF 7.00";
1137 static void pdb_ds_dump(void)
1139 struct pdb_reader reader;
1141 pdb_ds_init(&reader);
1142 printf("Header (DS)\n"
1143 "\tsignature: %.*s\n"
1144 "\tblock_size: %08x\n"
1145 "\tfree_list_block: %08x\n"
1146 "\tnum_blocks: %08x\n"
1147 "\ttoc_size: %08x\n"
1148 "\tunknown2: %08x\n"
1149 "\ttoc_block: %08x\n",
1150 (int)sizeof(pdb7) - 1, reader.u.ds.header->signature,
1151 reader.u.ds.header->block_size,
1152 reader.u.ds.header->free_list_block,
1153 reader.u.ds.header->num_blocks,
1154 reader.u.ds.header->toc_size,
1155 reader.u.ds.header->unknown2,
1156 reader.u.ds.header->toc_block);
1158 /* streams with static indexes:
1159 * 0: JG says old toc blocks
1160 * 1: root structure
1161 * 2: types
1162 * 3: modules
1163 * 4: types (second stream)
1164 * other known streams:
1165 * - string table: its index is in the stream table from ROOT object under "/names"
1166 * - type hash table: its index is in the types header (2 and 4)
1167 * - global and public streams: from symbol stream header
1168 * those streams get their indexes out of the PDB_STREAM_INDEXES object
1169 * - FPO data
1170 * - sections
1171 * - extended FPO data
1173 mark_stream_been_read(&reader, 0); /* mark stream #0 as read */
1174 reader.u.ds.root = reader.read_stream(&reader, 1);
1175 if (reader.u.ds.root)
1177 UINT *pdw, *ok_bits;
1178 UINT i, numok, count;
1179 PDB_STREAM_INDEXES sidx;
1181 printf("Root:\n"
1182 "\tVersion: %u\n"
1183 "\tTimeDateStamp: %08x\n"
1184 "\tAge: %08x\n"
1185 "\tguid %s\n"
1186 "\tcbNames: %08x\n",
1187 reader.u.ds.root->Version,
1188 reader.u.ds.root->TimeDateStamp,
1189 reader.u.ds.root->Age,
1190 get_guid_str(&reader.u.ds.root->guid),
1191 reader.u.ds.root->cbNames);
1192 pdw = (UINT *)(reader.u.ds.root->names + reader.u.ds.root->cbNames);
1193 numok = *pdw++;
1194 count = *pdw++;
1195 printf("\tStreams directory:\n"
1196 "\t\tok: %08x\n"
1197 "\t\tcount: %08x\n"
1198 "\t\ttable:\n",
1199 numok, count);
1201 /* bitfield: first dword is len (in dword), then data */
1202 ok_bits = pdw;
1203 pdw += *ok_bits++ + 1;
1204 if (*pdw++ != 0)
1206 printf("unexpected value\n");
1207 return;
1210 for (i = 0; i < count; i++)
1212 if (ok_bits[i / 32] & (1 << (i % 32)))
1214 UINT string_idx, stream_idx;
1215 string_idx = *pdw++;
1216 stream_idx = *pdw++;
1217 printf("\t\t\t%2d) %-20s => %x\n", i, &reader.u.ds.root->names[string_idx], stream_idx);
1218 numok--;
1221 if (numok) printf(">>> unmatched present field with found\n");
1223 pdb_dump_types(&reader, 2, "TPI");
1224 pdb_dump_types(&reader, 4, "IPI");
1225 pdb_dump_symbols(&reader, &sidx);
1226 pdb_dump_fpo(&reader, sidx.FPO);
1227 pdb_dump_fpo_ext(&reader, sidx.FPO_EXT);
1228 pdb_dump_sections(&reader, sidx.sections_stream);
1230 else printf("-Unable to get root\n");
1232 pdb_exit(&reader);
1235 enum FileSig get_kind_pdb(void)
1237 const char* head;
1239 head = PRD(0, sizeof(pdb2) - 1);
1240 if (head && !memcmp(head, pdb2, sizeof(pdb2) - 1))
1241 return SIG_PDB;
1242 head = PRD(0, sizeof(pdb7) - 1);
1243 if (head && !memcmp(head, pdb7, sizeof(pdb7) - 1))
1244 return SIG_PDB;
1245 return SIG_UNKNOWN;
1248 void pdb_dump(void)
1250 const char* head;
1252 /* init_types(); */
1253 head = PRD(0, sizeof(pdb2) - 1);
1254 if (head && !memcmp(head, pdb2, sizeof(pdb2) - 1))
1256 pdb_jg_dump();
1257 return;
1259 head = PRD(0, sizeof(pdb7) - 1);
1260 if (head && !memcmp(head, pdb7, sizeof(pdb7) - 1))
1262 pdb_ds_dump();
1263 return;
1265 printf("Unrecognized header %s\n", head);