wow64: In wow64_NtSetInformationToken forward TokenIntegrityLevel.
[wine.git] / tools / winedump / pdb.c
blobdc59695a8262672897b0a4799d4dd0384e2b0f46
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 pdw += *pdw + 1; /* skip deleted vector */
205 for (i = 0; i < count; i++)
207 if (ok_bits[i / 32] & (1 << (i % 32)))
209 string_idx = *pdw++;
210 stream_idx = *pdw++;
211 if (!strcmp(name, &str[string_idx])) return stream_idx;
214 return -1;
217 static void dump_string_table(const PDB_STRING_TABLE* strtable, const char* name, const char* pfx)
219 const char* end;
220 const char* ptr;
221 unsigned* table;
222 unsigned num_buckets;
223 unsigned i;
225 if (!strtable)
227 printf("%sString table (%s) isn't present\n", pfx, name);
228 return;
230 printf("%sString table (%s)\n"
231 "%s\tHeader: %08x\n"
232 "%s\tLength: %08x\n"
233 "%s\tHash version: %u\n",
234 pfx, name, pfx, strtable->magic, pfx, strtable->length, pfx, strtable->hash_version);
235 ptr = (const char*)(strtable + 1);
236 end = ptr + strtable->length;
237 while (ptr < end)
239 printf("%s\t%tu] %s\n", pfx, ptr - (const char*)(strtable + 1), ptr);
240 ptr += strlen(ptr) + 1;
242 table = (unsigned *)((char*)(strtable + 1) + strtable->length);
243 num_buckets = *table++;
245 if (globals_dump_sect("hash"))
247 printf("%s\tHash:\n"
248 "%s\t\tnum_strings: %x\n"
249 "%s\t\tnum_buckets: %x\n",
250 pfx, pfx, table[num_buckets], pfx, num_buckets);
252 for (i = 0; i < num_buckets; i++)
253 printf("%s\t\t%x] %x\n", pfx, i, table[i]);
257 static PDB_STRING_TABLE* read_string_table(struct pdb_reader* reader)
259 unsigned stream_idx;
260 PDB_STRING_TABLE* ret;
261 unsigned stream_size;
263 stream_idx = get_stream_by_name(reader, "/names");
264 if (stream_idx == -1) return NULL;
265 ret = reader->read_stream(reader, stream_idx);
266 if (!ret) return NULL;
267 stream_size = pdb_get_stream_size(reader, stream_idx);
268 if (globals_dump_sect("PDB")) dump_string_table(ret, "Global", " ");
269 if (ret->magic == 0xeffeeffe && sizeof(*ret) + ret->length < stream_size) return ret;
270 printf("Improper string table header (magic=%x)\n", ret->magic);
271 dump_data((const unsigned char*)ret, stream_size, " ");
272 free( ret );
273 return NULL;
276 const char* pdb_get_string_table_entry(const PDB_STRING_TABLE* table, unsigned ofs)
278 if (!table) return "<<no string table>>";
279 if (ofs >= table->length) return "<<invalid string table offset>>";
280 /* strings start after header */
281 return (char*)(table + 1) + ofs;
284 static void dump_dbi_hash_table(const BYTE* root, unsigned size, const char* name, const char* pfx)
286 if (!globals_dump_sect("hash")) return;
287 if (size >= sizeof(DBI_HASH_HEADER))
289 const DBI_HASH_HEADER* hdr = (const DBI_HASH_HEADER*)root;
291 printf("%s%s symbols hash:\n", pfx, name);
292 printf("%s\tSignature: 0x%x\n", pfx, hdr->signature);
293 printf("%s\tVersion: 0x%x (%u)\n", pfx, hdr->version, hdr->version - 0xeffe0000);
294 printf("%s\tSize of hash records: %u\n", pfx, hdr->hash_records_size);
295 printf("%s\tUnknown: %u\n", pfx, hdr->unknown);
297 if (hdr->signature != 0xFFFFFFFF ||
298 hdr->version != 0xeffe0000 + 19990810 ||
299 (hdr->hash_records_size % sizeof(DBI_HASH_RECORD)) != 0 ||
300 sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE > size ||
301 (size - (sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE)) % sizeof(unsigned))
303 if (size >= sizeof(DBI_HASH_HEADER) && !hdr->hash_records_size)
304 printf("%s\t\tEmpty hash structure\n", pfx);
305 else
306 printf("%s\t\tIncorrect hash structure\n", pfx);
308 else
310 unsigned i;
311 unsigned num_hash_records = hdr->hash_records_size / sizeof(DBI_HASH_RECORD);
312 const DBI_HASH_RECORD* hr = (const DBI_HASH_RECORD*)(hdr + 1);
313 unsigned* bitmap = (unsigned*)((char*)(hdr + 1) + hdr->hash_records_size);
314 unsigned* buckets = (unsigned*)((char*)(hdr + 1) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE);
315 unsigned index, last_index = (size - (sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE)) / sizeof(unsigned);
317 /* Yes, offsets for accessiong hr[] are stored as multiple of 12; and not
318 * as multiple of sizeof(*hr) = 8 as one might expect.
319 * Perhaps, native implementation likes to keep the same offsets between
320 * in memory representation vs on file representations.
322 for (index = 0, i = 0; i <= DBI_MAX_HASH; i++)
324 if (bitmap[i / 32] & (1u << (i % 32)))
326 unsigned j;
327 printf("%s\t[%u]\n", pfx, i);
328 for (j = buckets[index] / 12; j < (index + 1 < last_index ? buckets[index + 1] / 12 : num_hash_records); j++)
329 printf("%s\t\t[%u] offset=%08x unk=%x\n", pfx, j, hr[j].offset - 1, hr[j].unknown);
330 index++;
332 else
333 printf("%s\t[%u] <<empty>>\n", pfx, i);
335 /* shouldn't happen */
336 if (sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE + index * sizeof(unsigned) > size)
338 printf("%s-- left over %u bytes\n", pfx,
339 size - (unsigned)(sizeof(DBI_HASH_HEADER) + hdr->hash_records_size + DBI_BITMAP_HASH_SIZE + index * sizeof(unsigned)));
343 else
344 printf("%sNo header in symbols hash\n", pfx);
347 static void dump_global_symbol(struct pdb_reader* reader, unsigned stream)
349 void* global = NULL;
350 DWORD size;
352 global = reader->read_stream(reader, stream);
353 if (!global) return;
355 size = pdb_get_stream_size(reader, stream);
357 dump_dbi_hash_table(global, size, "Global", "");
358 free(global);
361 static void dump_public_symbol(struct pdb_reader* reader, unsigned stream)
363 unsigned size;
364 DBI_PUBLIC_HEADER* hdr;
365 const BYTE* ptr;
366 unsigned i;
368 if (!globals_dump_sect("public")) return;
369 hdr = reader->read_stream(reader, stream);
370 if (!hdr) return;
372 size = pdb_get_stream_size(reader, stream);
374 printf("Public symbols table: (%u)\n", size);
376 printf("\tHash size: %u\n", hdr->hash_size);
377 printf("\tAddress map size: %u\n", hdr->address_map_size);
378 printf("\tNumber of thunks: %u\n", hdr->num_thunks);
379 printf("\tSize of thunk map: %u\n", hdr->thunk_size);
380 printf("\tSection of thunk table: %u\n", hdr->section_thunk_table);
381 printf("\tOffset of thunk table: %u\n", hdr->offset_thunk_table);
382 printf("\tNumber of sections: %u\n", hdr->num_sections);
384 ptr = (const BYTE*)(hdr + 1);
385 dump_dbi_hash_table(ptr, hdr->hash_size, "Public", "\t");
387 ptr += hdr->hash_size;
388 printf("\tAddress map:\n");
389 for (i = 0; i < hdr->address_map_size / sizeof(unsigned); i++)
390 printf("\t\t%u] %08x\n", i, ((const unsigned*)ptr)[i]);
392 ptr += hdr->address_map_size;
393 printf("\tThunk map:\n");
394 for (i = 0; i < hdr->num_thunks; i++)
395 printf("\t\t%u] %08x\n", i, ((const unsigned*)ptr)[i]);
397 ptr += hdr->num_thunks * sizeof(unsigned);
398 printf("\tSection map:\n");
399 for (i = 0; i < hdr->num_sections; i++)
400 printf("\t\t%u] %04x:%08x\n", i, (unsigned short)((const unsigned*)ptr)[2 * i + 1], ((const unsigned*)ptr)[2 * i + 0]);
402 if (ptr + hdr->num_sections * 8 != ((const BYTE*)hdr) + size)
403 printf("Incorrect stream\n");
404 free(hdr);
407 static const void* pdb_dump_dbi_module(struct pdb_reader* reader, const PDB_SYMBOL_FILE_EX* sym_file,
408 const char* file_name)
410 const char* lib_name;
411 unsigned char* modimage;
412 BOOL new_format = !file_name;
414 if (new_format) file_name = sym_file->filename;
415 printf("\t--------symbol file-----------\n");
416 printf("\tName: %s\n", file_name);
417 lib_name = file_name + strlen(file_name) + 1;
418 if (strcmp(file_name, lib_name)) printf("\tLibrary: %s\n", lib_name);
419 printf("\t\tunknown1: %08x\n"
420 "\t\trange\n"
421 "\t\t\tsegment: %04x\n"
422 "\t\t\tpad1: %04x\n"
423 "\t\t\toffset: %08x\n"
424 "\t\t\tsize: %08x\n"
425 "\t\t\tcharacteristics: %08x",
426 sym_file->unknown1,
427 sym_file->range.segment,
428 sym_file->range.pad1,
429 sym_file->range.offset,
430 sym_file->range.size,
431 sym_file->range.characteristics);
432 dump_section_characteristics(sym_file->range.characteristics, " ");
433 printf("\n"
434 "\t\t\tindex: %04x\n"
435 "\t\t\tpad2: %04x\n",
436 sym_file->range.index,
437 sym_file->range.pad2);
438 if (new_format)
439 printf("\t\t\ttimestamp: %08x\n"
440 "\t\t\tunknown: %08x\n",
441 sym_file->range.timestamp,
442 sym_file->range.unknown);
443 printf("\t\tflag: %04x\n"
444 "\t\tstream: %04x\n"
445 "\t\tsymb size: %08x\n"
446 "\t\tline size: %08x\n"
447 "\t\tline2 size: %08x\n"
448 "\t\tnSrcFiles: %08x\n"
449 "\t\tattribute: %08x\n",
450 sym_file->flag,
451 sym_file->stream,
452 sym_file->symbol_size,
453 sym_file->lineno_size,
454 sym_file->lineno2_size,
455 sym_file->nSrcFiles,
456 sym_file->attribute);
457 if (new_format)
458 printf("\t\treserved/0: %08x\n"
459 "\t\treserved/1: %08x\n",
460 sym_file->reserved[0],
461 sym_file->reserved[1]);
463 modimage = reader->read_stream(reader, sym_file->stream);
464 if (modimage)
466 int total_size = pdb_get_stream_size(reader, sym_file->stream);
468 if (sym_file->symbol_size)
469 codeview_dump_symbols((const char*)modimage, sizeof(DWORD), sym_file->symbol_size);
471 /* line number info */
472 if (sym_file->lineno_size)
473 codeview_dump_linetab((const char*)modimage + sym_file->symbol_size, TRUE, " ");
474 else if (sym_file->lineno2_size) /* actually, only one of the 2 lineno should be present */
475 codeview_dump_linetab2((const char*)modimage + sym_file->symbol_size, sym_file->lineno2_size,
476 reader->global_string_table, " ");
477 /* what's that part ??? */
478 if (0)
479 dump_data(modimage + sym_file->symbol_size + sym_file->lineno_size + sym_file->lineno2_size,
480 total_size - (sym_file->symbol_size + sym_file->lineno_size + sym_file->lineno2_size), " ");
481 free(modimage);
483 return (const void*)((DWORD_PTR)(lib_name + strlen(lib_name) + 1 + 3) & ~3);
486 static void pdb_dump_symbols(struct pdb_reader* reader)
488 PDB_SYMBOLS* symbols;
489 unsigned char* modimage;
490 const char* file;
491 char tcver[32];
492 const unsigned short* sub_streams = NULL;
493 unsigned num_sub_streams = 0;
495 symbols = reader->read_stream(reader, 3);
496 if (!symbols) return;
498 if (globals_dump_sect("DBI"))
500 switch (symbols->version)
502 case 0: /* VC 4.0 */
503 case 19960307: /* VC 5.0 */
504 case 19970606: /* VC 6.0 */
505 case 19990903: /* VC 7.0 */
506 break;
507 default:
508 printf("-Unknown symbol info version %d\n", symbols->version);
510 if (symbols->flags & 0x8000) /* new */
511 sprintf(tcver, "%u.%u", (symbols->flags >> 8) & 0x7f, symbols->flags & 0xff);
512 else
513 sprintf(tcver, "old-%x", symbols->flags);
514 printf("Symbols:\n"
515 "\tsignature: %08x\n"
516 "\tversion: %u\n"
517 "\tage: %08x\n"
518 "\tglobal_hash_stream: %u\n"
519 "\tbuilder: %s\n"
520 "\tpublic_stream: %u\n"
521 "\tbldVer: %u\n"
522 "\tgsym_stream: %u\n"
523 "\trbldVer: %u\n"
524 "\tmodule_size: %08x\n"
525 "\tsectcontrib_size: %08x\n"
526 "\tsegmap_size: %08x\n"
527 "\tsrc_module_size: %08x\n"
528 "\tpdbimport_size: %08x\n"
529 "\tresvd0: %08x\n"
530 "\tstream_idx_size: %08x\n"
531 "\tunknown2_size: %08x\n"
532 "\tresvd3: %04x\n"
533 "\tmachine: %s\n"
534 "\tresvd4 %08x\n",
535 symbols->signature,
536 symbols->version,
537 symbols->age,
538 symbols->global_hash_stream,
539 tcver, /* from symbols->flags */
540 symbols->public_stream,
541 symbols->bldVer,
542 symbols->gsym_stream,
543 symbols->rbldVer,
544 symbols->module_size,
545 symbols->sectcontrib_size,
546 symbols->segmap_size,
547 symbols->srcmodule_size,
548 symbols->pdbimport_size,
549 symbols->resvd0,
550 symbols->stream_index_size,
551 symbols->unknown2_size,
552 symbols->resvd3,
553 get_machine_str( symbols->machine ),
554 symbols->resvd4);
557 if (symbols->sectcontrib_size && globals_dump_sect("image"))
559 const BYTE* src = (const BYTE*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size;
560 const BYTE* last = src + symbols->sectcontrib_size;
561 unsigned version, size;
563 printf("\t----------section contrib------------\n");
564 version = *(unsigned*)src;
565 printf("\tVersion: %#x (%d)\n", version, version - 0xeffe0000);
566 switch (version)
568 case 0xeffe0000 + 19970605: size = sizeof(PDB_SYMBOL_RANGE_EX); break;
569 case 0xeffe0000 + 20140516: size = sizeof(PDB_SYMBOL_RANGE_EX) + sizeof(unsigned); break;
570 default: printf("\t\tUnsupported version number\n"); size = 0;
572 if (size)
574 const PDB_SYMBOL_RANGE_EX* range;
576 if ((symbols->sectcontrib_size - sizeof(unsigned)) % size)
577 printf("Incoherent size: %zu = %zu * %u + %zu\n",
578 symbols->sectcontrib_size - sizeof(unsigned),
579 (symbols->sectcontrib_size - sizeof(unsigned)) / size,
580 size,
581 (symbols->sectcontrib_size - sizeof(unsigned)) % size);
582 src += sizeof(unsigned);
583 while (src + size <= last)
585 range = (const PDB_SYMBOL_RANGE_EX*)(src + sizeof(unsigned));
586 printf("\tRange #%tu\n",
587 ((const BYTE*)range - ((const BYTE*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size)) / size);
588 printf("\t\tsegment: %04x\n"
589 "\t\tpad1: %04x\n"
590 "\t\toffset: %08x\n"
591 "\t\tsize: %08x\n"
592 "\t\tcharacteristics: %08x",
593 range->segment,
594 range->pad1,
595 range->offset,
596 range->size,
597 range->characteristics);
598 dump_section_characteristics(range->characteristics, " ");
599 printf("\n"
600 "\t\tindex: %04x\n"
601 "\t\tpad2: %04x\n"
602 "\t\ttimestamp: %08x\n"
603 "\t\tunknown: %08x\n",
604 range->index,
605 range->pad2,
606 range->timestamp,
607 range->unknown);
608 if (version == 0xeffe0000 + 20140516)
609 printf("\t\tcoff_section: %08x\n", *(unsigned*)(range + 1));
610 src += size;
615 if (symbols->srcmodule_size && globals_dump_sect("DBI"))
617 const PDB_SYMBOL_SOURCE*src;
618 int i, j, cfile;
619 const WORD* indx;
620 const DWORD* offset;
621 const char* start_cstr;
622 const char* cstr;
624 printf("\t----------src module------------\n");
625 src = (const PDB_SYMBOL_SOURCE*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
626 symbols->module_size + symbols->sectcontrib_size + symbols->segmap_size);
627 printf("\tSource Modules\n"
628 "\t\tnModules: %u\n"
629 "\t\tnSrcFiles: %u\n",
630 src->nModules, src->nSrcFiles);
632 /* usage of table seems to be as follows:
633 * two arrays of WORD (src->nModules as size)
634 * - first array contains index into files for "module" compilation
635 * (module = compilation unit ??)
636 * - second array contains the number of source files in module
637 * an array of DWORD (src->nSrcFiles as size)
638 * - contains offset (in following string table) of the source file name
639 * a string table
640 * - each string is a pascal string (ie. with its length as first BYTE) or
641 * 0-terminated string (depending on version)
643 indx = &src->table[src->nModules];
644 offset = (const DWORD*)&src->table[2 * src->nModules];
645 cstr = (const char*)&src->table[2 * (src->nModules + src->nSrcFiles)];
646 start_cstr = cstr;
648 for (i = cfile = 0; i < src->nModules; i++)
650 printf("\t\tModule[%2d]:\n", i);
651 cfile = src->table[i];
652 for (j = cfile; j < src->nSrcFiles && j < cfile + indx[i]; j++)
654 /* FIXME: in some cases, it's a p_string but WHEN ? */
655 if (cstr + offset[j] >= start_cstr /* wrap around */ &&
656 cstr + offset[j] < (const char*)src + symbols->srcmodule_size)
657 printf("\t\t\tSource file: %s\n", cstr + offset[j]);
658 else
659 printf("\t\t\tSource file: <<out of bounds>>\n");
663 if (symbols->pdbimport_size && globals_dump_sect("PDB"))
665 const PDB_SYMBOL_IMPORT* imp;
666 const char* first;
667 const char* last;
668 const char* ptr;
670 printf("\t------------import--------------\n");
671 imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
672 symbols->module_size + symbols->sectcontrib_size +
673 symbols->segmap_size + symbols->srcmodule_size);
674 first = (const char*)imp;
675 last = (const char*)imp + symbols->pdbimport_size;
676 while (imp < (const PDB_SYMBOL_IMPORT*)last)
678 ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
679 printf("\tImport: %lx\n"
680 "\t\tUnknown1: %08x\n"
681 "\t\tUnknown2: %08x\n"
682 "\t\tTimeDateStamp: %08x\n"
683 "\t\tAge: %08u\n"
684 "\t\tfile1: %s\n"
685 "\t\tfile2: %s\n",
686 (ULONG_PTR)((const char*)imp - first),
687 imp->unknown1,
688 imp->unknown2,
689 imp->TimeDateStamp,
690 imp->Age,
691 imp->filename,
692 ptr);
693 imp = (const PDB_SYMBOL_IMPORT*)(first + ((ptr - first + strlen(ptr) + 1 + 3) & ~3));
696 if (symbols->segmap_size && globals_dump_sect("image"))
698 const struct OMFSegMap* segmap = (const struct OMFSegMap*)((const BYTE*)symbols + sizeof(PDB_SYMBOLS) +
699 symbols->module_size + symbols->sectcontrib_size);
700 const struct OMFSegMapDesc* desc = (const struct OMFSegMapDesc*)(segmap + 1);
702 printf("\t--------------segment map----------------\n");
703 printf("\tNumber of segments: %x\n", segmap->cSeg);
704 printf("\tNumber of logical segments: %x\n", segmap->cSegLog);
705 /* FIXME check mapping old symbols */
706 for (; (const BYTE*)(desc + 1) <= ((const BYTE*)(segmap + 1) + symbols->segmap_size); desc++)
708 printf("\t\tSegment descriptor #%tu\n", desc - (const struct OMFSegMapDesc*)(segmap + 1));
709 printf("\t\t\tFlags: %04x (%c%c%c%s%s%s%s)\n",
710 desc->flags,
711 (desc->flags & 0x01) ? 'R' : '-',
712 (desc->flags & 0x02) ? 'W' : '-',
713 (desc->flags & 0x04) ? 'X' : '-',
714 (desc->flags & 0x08) ? " 32bit-linear" : "",
715 (desc->flags & 0x100) ? " selector" : "",
716 (desc->flags & 0x200) ? " absolute" : "",
717 (desc->flags & 0x400) ? " group" : "");
718 printf("\t\t\tOverlay: %04x\n", desc->ovl);
719 printf("\t\t\tGroup: %04x\n", desc->group);
720 printf("\t\t\tFrame: %04x\n", desc->frame);
721 printf("\t\t\tSegment name: %s\n", desc->iSegName == 0xffff ? "none" : pdb_get_string_table_entry(reader->global_string_table, desc->iSegName));
722 printf("\t\t\tClass name: %s\n", desc->iClassName == 0xffff ? "none" : pdb_get_string_table_entry(reader->global_string_table, desc->iClassName));
723 printf("\t\t\tOffset: %08x\n", desc->offset);
724 printf("\t\t\tSize: %04x\n", desc->cbSeg);
727 if (symbols->unknown2_size && globals_dump_sect("PDB"))
729 const char* ptr = (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
730 symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
731 symbols->pdbimport_size;
732 printf("\t------------Unknown2--------------\n");
733 dump_string_table((const PDB_STRING_TABLE*)ptr, "Unknown from DBI", "\t");
735 if (symbols->stream_index_size && globals_dump_sect("image"))
737 const char* sub_stream_names[] = {"FPO", NULL, NULL, NULL, NULL, "Sections stream", NULL, NULL, NULL, "FPO-ext"};
738 int i;
740 printf("\t------------stream indexes--------------\n");
741 num_sub_streams = symbols->stream_index_size / sizeof(sub_streams[0]);
742 sub_streams = (const unsigned short*)((const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
743 symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
744 symbols->pdbimport_size + symbols->unknown2_size);
745 for (i = 0; i < num_sub_streams; i++)
747 const char* name = "?";
748 if (i < ARRAY_SIZE(sub_stream_names) && sub_stream_names[i])
749 name = sub_stream_names[i];
750 printf("\t%s:%.*s%04x\n", name, (int)(21 - strlen(name)), "", sub_streams[i]);
754 /* Read global symbol table */
755 modimage = reader->read_stream(reader, symbols->gsym_stream);
756 if (modimage && globals_dump_sect("DBI"))
758 printf("\t------------globals-------------\n");
759 codeview_dump_symbols(modimage, 0, pdb_get_stream_size(reader, symbols->gsym_stream));
760 free(modimage);
763 /* Read per-module symbol / linenumber tables */
764 if (symbols->module_size && globals_dump_sect("DBI"))
766 SIZE_T module_header_size = symbols->version < 19970000 ? sizeof(PDB_SYMBOL_FILE) : sizeof(PDB_SYMBOL_FILE_EX);
768 file = (const char*)symbols + sizeof(PDB_SYMBOLS);
769 while (file + module_header_size <= (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size)
771 if (symbols->version < 19970000)
773 PDB_SYMBOL_FILE_EX copy;
774 const PDB_SYMBOL_FILE* sym_file = (const PDB_SYMBOL_FILE*)file;
776 copy.unknown1 = sym_file->unknown1;
777 copy.range.segment = sym_file->range.segment;
778 copy.range.pad1 = sym_file->range.pad1;
779 copy.range.offset = sym_file->range.offset;
780 copy.range.size = sym_file->range.size;
781 copy.range.characteristics = sym_file->range.characteristics;
782 copy.range.index = sym_file->range.index;
783 copy.range.pad2 = sym_file->range.pad2;
784 copy.range.timestamp = 0;
785 copy.range.unknown = 0;
786 copy.flag = sym_file->flag;
787 copy.stream = sym_file->stream;
788 copy.symbol_size = sym_file->symbol_size;
789 copy.lineno_size = sym_file->lineno_size;
790 copy.lineno2_size = sym_file->lineno2_size;
791 copy.nSrcFiles = sym_file->nSrcFiles;
792 copy.attribute = sym_file->attribute;
793 copy.reserved[0] = 0;
794 copy.reserved[1] = 0;
795 file = pdb_dump_dbi_module(reader, &copy, sym_file->filename);
797 else
798 file = pdb_dump_dbi_module(reader, (const PDB_SYMBOL_FILE_EX*)file, NULL);
801 dump_global_symbol(reader, symbols->global_hash_stream);
802 dump_public_symbol(reader, symbols->public_stream);
804 if (sub_streams && globals_dump_sect("image"))
806 if (PDB_SIDX_FPO < num_sub_streams)
807 pdb_dump_fpo(reader, sub_streams[PDB_SIDX_FPO]);
808 if (PDB_SIDX_FPOEXT < num_sub_streams)
809 pdb_dump_fpo_ext(reader, sub_streams[PDB_SIDX_FPOEXT]);
810 if (PDB_SIDX_SECTIONS < num_sub_streams)
811 pdb_dump_sections(reader, sub_streams[PDB_SIDX_SECTIONS]);
814 free(symbols);
817 static BOOL is_bit_set(const unsigned* dw, unsigned len, unsigned i)
819 if (i >= len * sizeof(unsigned) * 8) return FALSE;
820 return (dw[i >> 5] & (1u << (i & 31u))) != 0;
823 static void pdb_dump_hash_value(const BYTE* ptr, unsigned len)
825 int i;
827 printf("[");
828 for (i = len - 1; i >= 0; i--)
829 printf("%02x", ptr[i]);
830 printf("]");
833 static struct
835 const BYTE* hash;
836 unsigned hash_size;
837 } collision_arg;
839 static int collision_compar(const void *p1, const void *p2)
841 unsigned idx1 = *(unsigned*)p1;
842 unsigned idx2 = *(unsigned*)p2;
843 return memcmp(collision_arg.hash + idx1 * collision_arg.hash_size,
844 collision_arg.hash + idx2 * collision_arg.hash_size,
845 collision_arg.hash_size);
848 static void pdb_dump_types_hash(struct pdb_reader* reader, const PDB_TYPES* types, const char* strmname)
850 void* hash = NULL;
851 unsigned i, strmsize;
852 const unsigned* table;
853 unsigned *collision;
855 if (!globals_dump_sect("hash")) return;
856 hash = reader->read_stream(reader, types->hash_stream);
857 if (!hash) return;
859 printf("Types (%s) hash:\n", strmname);
860 strmsize = pdb_get_stream_size(reader, types->hash_stream);
861 if (types->hash_offset + types->hash_size > strmsize ||
862 (types->last_index - types->first_index) * types->hash_value_size != types->hash_size ||
863 types->search_offset + types->search_size > strmsize ||
864 types->type_remap_offset + types->type_remap_size > strmsize)
866 printf("\nIncoherent sizes... skipping\n");
867 return;
869 printf("\n\tIndexes => hash value:\n");
870 for (i = types->first_index; i < types->last_index; i++)
872 printf("\t\t%08x => ", i);
873 pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + (i - types->first_index) * types->hash_value_size, types->hash_value_size);
874 printf("\n");
876 /* print collisions in hash table (if any) */
877 collision = malloc((types->last_index - types->first_index) * sizeof(unsigned));
878 if (collision)
880 unsigned head_printed = 0;
882 collision_arg.hash = (const BYTE*)hash + types->hash_offset;
883 collision_arg.hash_size = types->hash_value_size;
885 for (i = 0; i < types->last_index - types->first_index; i++) collision[i] = i;
886 qsort(collision, types->last_index - types->first_index, sizeof(unsigned), collision_compar);
887 for (i = 0; i < types->last_index - types->first_index; i++)
889 unsigned j;
890 for (j = i + 1; j < types->last_index - types->first_index; j++)
891 if (memcmp((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size,
892 (const BYTE*)hash + types->hash_offset + collision[j] * types->hash_value_size,
893 types->hash_value_size))
894 break;
895 if (j > i + 1)
897 unsigned k;
898 if (!head_printed)
900 printf("\n\t\tCollisions:\n");
901 head_printed = 1;
903 printf("\t\t\tHash ");
904 pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size, types->hash_value_size);
905 printf(":");
906 for (k = i; k < j; k++)
907 printf(" %x", types->first_index + collision[k]);
908 printf("\n");
909 i = j - 1;
912 free(collision);
914 printf("\n\tIndexes => offsets:\n");
915 table = (const unsigned*)((const BYTE*)hash + types->search_offset);
916 for (i = 0; i < types->search_size / (2 * sizeof(unsigned)); i += 2)
918 printf("\t\t%08x => %08x\n", table[2 * i + 0], table[2 * i + 1]);
921 if (types->type_remap_size)
923 unsigned num, capa, count_present, count_deleted;
924 const unsigned* present_bitset;
925 const unsigned* deleted_bitset;
927 printf("\n\tType remap:\n");
928 table = (const unsigned*)((const BYTE*)hash + types->type_remap_offset);
929 num = *table++;
930 capa = *table++;
931 count_present = *table++;
932 present_bitset = table;
933 table += count_present;
934 count_deleted = *table++;
935 deleted_bitset = table;
936 table += count_deleted;
937 printf("\t\tNumber of present entries: %u\n", num);
938 printf("\t\tCapacity: %u\n", capa);
939 printf("\t\tBitset present:\n");
940 printf("\t\t\tCount: %u\n", count_present);
941 printf("\t\t\tBitset: ");
942 pdb_dump_hash_value((const BYTE*)present_bitset, count_present * sizeof(unsigned));
943 printf("\n");
944 printf("\t\tBitset deleted:\n");
945 printf("\t\t\tCount: %u\n", count_deleted);
946 printf("\t\t\tBitset: ");
947 pdb_dump_hash_value((const BYTE*)deleted_bitset, count_deleted * sizeof(unsigned));
948 printf("\n");
949 for (i = 0; i < capa; ++i)
951 printf("\t\t%2u) %c",
953 is_bit_set(present_bitset, count_present, i) ? 'P' :
954 is_bit_set(deleted_bitset, count_deleted, i) ? 'D' : '_');
955 if (is_bit_set(present_bitset, count_present, i))
957 printf(" %s => ", pdb_get_string_table_entry(reader->global_string_table, *table++));
958 pdb_dump_hash_value((const BYTE*)table, types->hash_value_size);
959 table = (const unsigned*)((const BYTE*)table + types->hash_value_size);
961 printf("\n");
963 printf("\n");
965 free(hash);
968 /* there are two 'type' related streams, but with different indexes... */
969 static void pdb_dump_types(struct pdb_reader* reader, unsigned strmidx, const char* strmname)
971 PDB_TYPES* types = NULL;
972 BOOL used = has_stream_been_read(reader, strmidx);
974 if (!globals_dump_sect(strmidx == 2 ? "TPI" : "IPI")) return;
975 if (pdb_get_stream_size(reader, strmidx) < sizeof(*types))
977 if (strmidx == 2)
978 printf("-Too small type header\n");
979 return;
981 types = reader->read_stream(reader, strmidx);
982 if (!types) return;
984 switch (types->version)
986 case 19950410: /* VC 4.0 */
987 case 19951122:
988 case 19961031: /* VC 5.0 / 6.0 */
989 case 19990903: /* VC 7.0 */
990 case 20040203: /* VC 8.0 */
991 break;
992 default:
993 /* IPI stream is not always present in older PDB files */
994 if (strmidx == 2)
995 printf("-Unknown type info version %d\n", types->version);
996 free(types);
997 if (used) clear_stream_been_read(reader, strmidx);
998 return;
1001 /* Read type table */
1002 printf("Types (%s):\n"
1003 "\tversion: %u\n"
1004 "\ttype_offset: %08x\n"
1005 "\tfirst_index: %x\n"
1006 "\tlast_index: %x\n"
1007 "\ttype_size: %x\n"
1008 "\thash_stream: %x\n"
1009 "\tpad: %x\n"
1010 "\thash_value_size: %x\n"
1011 "\thash_buckets %x\n"
1012 "\thash_offset: %x\n"
1013 "\thash_size: %x\n"
1014 "\tsearch_offset: %x\n"
1015 "\tsearch_size: %x\n"
1016 "\ttype_remap_offset: %x\n"
1017 "\ttype_remap_size: %x\n",
1018 strmname,
1019 types->version,
1020 types->type_offset,
1021 types->first_index,
1022 types->last_index,
1023 types->type_size,
1024 types->hash_stream,
1025 types->pad,
1026 types->hash_value_size,
1027 types->hash_num_buckets,
1028 types->hash_offset,
1029 types->hash_size,
1030 types->search_offset,
1031 types->search_size,
1032 types->type_remap_offset,
1033 types->type_remap_size);
1034 codeview_dump_types_from_block((const char*)types + types->type_offset, types->type_size);
1035 pdb_dump_types_hash(reader, types, strmname);
1036 free(types);
1039 static void pdb_dump_fpo(struct pdb_reader* reader, unsigned stream_idx)
1041 FPO_DATA* fpo;
1042 unsigned i, size;
1043 const char* frame_type[4] = {"Fpo", "Trap", "Tss", "NonFpo"};
1045 if (stream_idx == (WORD)-1) return;
1046 fpo = reader->read_stream(reader, stream_idx);
1047 size = pdb_get_stream_size(reader, stream_idx);
1048 if (fpo && (size % sizeof(*fpo)) == 0)
1050 size /= sizeof(*fpo);
1051 printf("FPO data:\n\t Start Length #loc #pmt #prolog #reg frame SEH /BP\n");
1052 for (i = 0; i < size; i++)
1054 printf("\t%08x %08x %4d %4d %7d %4d %6s %c %c\n",
1055 (UINT)fpo[i].ulOffStart, (UINT)fpo[i].cbProcSize, (UINT)fpo[i].cdwLocals, fpo[i].cdwParams,
1056 fpo[i].cbProlog, fpo[i].cbRegs, frame_type[fpo[i].cbFrame],
1057 fpo[i].fHasSEH ? 'Y' : 'N', fpo[i].fUseBP ? 'Y' : 'N');
1060 free(fpo);
1063 static void pdb_dump_fpo_ext(struct pdb_reader* reader, unsigned stream_idx)
1065 PDB_FPO_DATA* fpoext;
1066 unsigned i, size;
1068 if (stream_idx == (WORD)-1) return;
1070 fpoext = reader->read_stream(reader, stream_idx);
1071 size = pdb_get_stream_size(reader, stream_idx);
1072 if (fpoext && (size % sizeof(*fpoext)) == 0)
1074 size /= sizeof(*fpoext);
1075 printf("FPO data (extended):\n"
1076 "\t Start Length Locals Params MaxStack Prolog #SavedRegs Flags Command\n");
1077 for (i = 0; i < size; i++)
1079 printf("\t%08x %08x %8x %8x %8x %6x %8x %08x %s\n",
1080 fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size, fpoext[i].params_size,
1081 fpoext[i].maxstack_size, fpoext[i].prolog_size, fpoext[i].savedregs_size, fpoext[i].flags,
1082 pdb_get_string_table_entry(reader->global_string_table, fpoext[i].str_offset));
1085 free(fpoext);
1088 static void pdb_dump_sections(struct pdb_reader* reader, unsigned stream_idx)
1090 const char* segs;
1091 DWORD size;
1092 const IMAGE_SECTION_HEADER* sect_hdr;
1094 if (stream_idx == (WORD)-1) return;
1095 segs = reader->read_stream(reader, stream_idx);
1097 if (segs)
1099 printf("Sections:\n");
1100 size = pdb_get_stream_size(reader, stream_idx);
1101 for (sect_hdr = (const IMAGE_SECTION_HEADER*)segs; (const char*)sect_hdr < segs + size; sect_hdr++)
1103 printf("\tSection: %-8.8s\n", sect_hdr->Name);
1104 printf("\t\tVirtual size: %08x\n", (unsigned)sect_hdr->Misc.VirtualSize);
1105 printf("\t\tVirtualAddress: %08x\n", (unsigned)sect_hdr->VirtualAddress);
1106 printf("\t\tSizeOfRawData: %08x\n", (unsigned)sect_hdr->SizeOfRawData);
1107 printf("\t\tPointerToRawData: %08x\n", (unsigned)sect_hdr->PointerToRawData);
1108 printf("\t\tPointerToRelocations: %08x\n", (unsigned)sect_hdr->PointerToRelocations);
1109 printf("\t\tPointerToLinenumbers: %08x\n", (unsigned)sect_hdr->PointerToLinenumbers);
1110 printf("\t\tNumberOfRelocations: %u\n", (unsigned)sect_hdr->NumberOfRelocations);
1111 printf("\t\tNumberOfLinenumbers: %u\n", (unsigned)sect_hdr->NumberOfLinenumbers);
1112 printf("\t\tCharacteristics: %08x", (unsigned)sect_hdr->Characteristics);
1113 dump_section_characteristics(sect_hdr->Characteristics, " ");
1114 printf("\n");
1116 free((char*)segs);
1120 static const char pdb2[] = "Microsoft C/C++ program database 2.00";
1122 static void pdb_jg_dump_header_root(struct pdb_reader* reader)
1124 UINT *pdw, *ok_bits;
1125 UINT i, numok, count;
1127 if (!globals_dump_sect("PDB")) return;
1129 printf("Header (JG):\n"
1130 "\tident: %.*s\n"
1131 "\tsignature: %08x\n"
1132 "\tblock_size: %08x\n"
1133 "\tfree_list_block: %04x\n"
1134 "\ttotal_alloc: %04x\n",
1135 (int)sizeof(pdb2) - 1, reader->u.jg.header->ident,
1136 reader->u.jg.header->signature,
1137 reader->u.jg.header->block_size,
1138 reader->u.jg.header->free_list_block,
1139 reader->u.jg.header->total_alloc);
1141 printf("Root:\n"
1142 "\tVersion: %u\n"
1143 "\tTimeDateStamp: %08x\n"
1144 "\tAge: %08x\n"
1145 "\tnames: %d\n",
1146 reader->u.jg.root->Version,
1147 reader->u.jg.root->TimeDateStamp,
1148 reader->u.jg.root->Age,
1149 (unsigned)reader->u.jg.root->cbNames);
1151 pdw = (UINT *)(reader->u.jg.root->names + reader->u.jg.root->cbNames);
1152 numok = *pdw++;
1153 count = *pdw++;
1154 printf("\tStreams directory:\n"
1155 "\t\tok: %08x\n"
1156 "\t\tcount: %08x\n"
1157 "\t\ttable:\n",
1158 numok, count);
1160 /* bitfield: first dword is len (in dword), then data */
1161 ok_bits = pdw;
1162 pdw += *ok_bits++ + 1;
1163 pdw += *pdw + 1; /* skip deleted vector */
1165 for (i = 0; i < count; i++)
1167 if (ok_bits[i / 32] & (1 << (i % 32)))
1169 UINT string_idx, stream_idx;
1170 string_idx = *pdw++;
1171 stream_idx = *pdw++;
1172 printf("\t\t\t%2d) %-20s => %x\n", i, &reader->u.jg.root->names[string_idx], stream_idx);
1173 numok--;
1176 if (numok) printf(">>> unmatched present field with found\n");
1178 /* Check for unknown versions */
1179 switch (reader->u.jg.root->Version)
1181 case 19950623: /* VC 4.0 */
1182 case 19950814:
1183 case 19960307: /* VC 5.0 */
1184 case 19970604: /* VC 6.0 */
1185 break;
1186 default:
1187 printf("-Unknown root block version %d\n", reader->u.jg.root->Version);
1191 static void* pdb_ds_read(const struct PDB_DS_HEADER* header, const UINT *block_list, int size)
1193 int i, nBlocks;
1194 BYTE* buffer;
1196 if (!size) return NULL;
1198 nBlocks = (size + header->block_size - 1) / header->block_size;
1199 buffer = xmalloc(nBlocks * header->block_size);
1201 for (i = 0; i < nBlocks; i++)
1202 memcpy(buffer + i * header->block_size,
1203 (const char*)header + block_list[i] * header->block_size, header->block_size);
1205 return buffer;
1208 static void* pdb_ds_read_stream(struct pdb_reader* reader, DWORD stream_number)
1210 const UINT *block_list;
1211 UINT i;
1213 if (!reader->u.ds.toc || stream_number >= reader->u.ds.toc->num_streams) return NULL;
1215 mark_stream_been_read(reader, stream_number);
1216 if (reader->u.ds.toc->stream_size[stream_number] == 0 ||
1217 reader->u.ds.toc->stream_size[stream_number] == 0xFFFFFFFF)
1218 return NULL;
1219 block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1220 for (i = 0; i < stream_number; i++)
1221 block_list += (reader->u.ds.toc->stream_size[i] + reader->u.ds.header->block_size - 1) /
1222 reader->u.ds.header->block_size;
1224 return pdb_ds_read(reader->u.ds.header, block_list, reader->u.ds.toc->stream_size[stream_number]);
1227 static BOOL pdb_ds_init(struct pdb_reader* reader)
1229 reader->u.ds.header = PRD(0, sizeof(*reader->u.ds.header));
1230 if (!reader->u.ds.header) return FALSE;
1231 reader->read_stream = pdb_ds_read_stream;
1232 reader->u.ds.toc = pdb_ds_read(reader->u.ds.header,
1233 (const UINT *)((const char*)reader->u.ds.header + reader->u.ds.header->toc_block * reader->u.ds.header->block_size),
1234 reader->u.ds.header->toc_size);
1235 memset(reader->stream_used, 0, sizeof(reader->stream_used));
1236 reader->u.ds.root = reader->read_stream(reader, 1);
1237 if (!reader->u.ds.root) return FALSE;
1238 return TRUE;
1241 static const char pdb7[] = "Microsoft C/C++ MSF 7.00";
1243 static void pdb_ds_dump_header_root(struct pdb_reader* reader)
1245 unsigned int i, j, ofs;
1246 const UINT *block_list;
1247 UINT *pdw, *ok_bits;
1248 UINT numok, count;
1249 unsigned strmsize;
1251 if (!globals_dump_sect("PDB")) return;
1252 strmsize = pdb_get_stream_size(reader, 1);
1253 printf("Header (DS)\n"
1254 "\tsignature: %.*s\n"
1255 "\tblock_size: %08x\n"
1256 "\tfree_list_block: %08x\n"
1257 "\tnum_blocks: %08x\n"
1258 "\ttoc_size: %08x\n"
1259 "\tunknown2: %08x\n"
1260 "\ttoc_block: %08x\n",
1261 (int)sizeof(pdb7) - 1, reader->u.ds.header->signature,
1262 reader->u.ds.header->block_size,
1263 reader->u.ds.header->free_list_block,
1264 reader->u.ds.header->num_blocks,
1265 reader->u.ds.header->toc_size,
1266 reader->u.ds.header->unknown2,
1267 reader->u.ds.header->toc_block);
1269 block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1270 printf("\t\tnum_streams: %u\n", reader->u.ds.toc->num_streams);
1271 for (ofs = i = 0; i < reader->u.ds.toc->num_streams; i++)
1273 unsigned int nblk = (reader->u.ds.toc->stream_size[i] + reader->u.ds.header->block_size - 1) / reader->u.ds.header->block_size;
1274 printf("\t\tstream[%#x]:\tsize: %u\n", i, reader->u.ds.toc->stream_size[i]);
1275 if (nblk)
1277 for (j = 0; j < nblk; j++)
1279 if (j % 16 == 0) printf("\t\t\t");
1280 printf("%4x ", block_list[ofs + j]);
1281 if (j % 16 == 15 || (j + 1 == nblk)) printf("\n");
1283 ofs += nblk;
1287 printf("Root:\n"
1288 "\tVersion: %u\n"
1289 "\tTimeDateStamp: %08x\n"
1290 "\tAge: %08x\n"
1291 "\tguid %s\n"
1292 "\tcbNames: %08x\n",
1293 reader->u.ds.root->Version,
1294 reader->u.ds.root->TimeDateStamp,
1295 reader->u.ds.root->Age,
1296 get_guid_str(&reader->u.ds.root->guid),
1297 reader->u.ds.root->cbNames);
1298 pdw = (UINT *)(reader->u.ds.root->names + reader->u.ds.root->cbNames);
1299 numok = *pdw++;
1300 count = *pdw++;
1301 printf("\tStreams directory:\n"
1302 "\t\tok: %08x\n"
1303 "\t\tcount: %08x\n"
1304 "\t\ttable:\n",
1305 numok, count);
1307 /* bitfield: first dword is len (in dword), then data */
1308 ok_bits = pdw;
1309 pdw += *ok_bits++ + 1;
1310 pdw += *pdw + 1; /* skip deleted vector */
1312 for (i = 0; i < count; i++)
1314 if (ok_bits[i / 32] & (1 << (i % 32)))
1316 UINT string_idx, stream_idx;
1317 string_idx = *pdw++;
1318 stream_idx = *pdw++;
1319 printf("\t\t\t%2d) %-20s => %x\n", i, &reader->u.ds.root->names[string_idx], stream_idx);
1320 numok--;
1323 if (numok) printf(">>> unmatched present field with found\n");
1324 if (*pdw++ != 0)
1326 printf("unexpected value\n");
1327 return;
1330 if (pdw + 1 <= (UINT*)((char*)reader->u.ds.root + strmsize))
1332 /* extra information (version reference and features) */
1333 printf("\tVersion and features\n");
1334 while (pdw + 1 <= (UINT*)((char*)reader->u.ds.root + strmsize))
1336 switch (*pdw)
1338 /* version reference */
1339 case 20091201: printf("\t\tVC110\n"); break;
1340 case 20140508: printf("\t\tVC140\n"); break;
1341 /* features */
1342 case 0x4D544F4E /* NOTM */: printf("\t\tNo type merge\n"); break;
1343 case 0x494E494D /* MINI */: printf("\t\tMinimal debug info\n"); break;
1344 default: printf("\t\tUnknown value %x\n", *pdw);
1346 pdw++;
1351 enum FileSig get_kind_pdb(void)
1353 const char* head;
1355 head = PRD(0, sizeof(pdb2) - 1);
1356 if (head && !memcmp(head, pdb2, sizeof(pdb2) - 1))
1357 return SIG_PDB;
1358 head = PRD(0, sizeof(pdb7) - 1);
1359 if (head && !memcmp(head, pdb7, sizeof(pdb7) - 1))
1360 return SIG_PDB;
1361 return SIG_UNKNOWN;
1364 void pdb_dump(void)
1366 const BYTE* head;
1367 const char** saved_dumpsect = globals.dumpsect;
1368 static const char* default_dumpsect[] = {"DBI", "TPI", "IPI", NULL};
1369 struct pdb_reader reader;
1371 if (!globals.dumpsect) globals.dumpsect = default_dumpsect;
1373 if ((head = PRD(0, sizeof(pdb2) - 1)) && !memcmp(head, pdb2, sizeof(pdb2) - 1))
1375 if (!pdb_jg_init(&reader))
1377 printf("Unable to get header information\n");
1378 return;
1381 pdb_jg_dump_header_root(&reader);
1383 else if ((head = PRD(0, sizeof(pdb7) - 1)) && !memcmp(head, pdb7, sizeof(pdb7) - 1))
1385 if (!pdb_ds_init(&reader))
1387 printf("Unable to get header information\n");
1388 return;
1390 pdb_ds_dump_header_root(&reader);
1392 mark_stream_been_read(&reader, 0); /* mark stream #0 (old TOC) as read */
1394 reader.global_string_table = read_string_table(&reader);
1396 pdb_dump_types(&reader, 2, "TPI");
1397 pdb_dump_types(&reader, 4, "IPI");
1398 pdb_dump_symbols(&reader);
1400 pdb_exit(&reader);
1402 globals.dumpsect = saved_dumpsect;