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