Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winedump/pdb.c
8729 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 void pdb_dump_dbi_module(struct pdb_reader* reader, const PDB_SYMBOL_FILE_EX* sym_file,
431
const char* file_name, const char* lib_name, unsigned index)
432
{
433
BOOL new_format = file_name == sym_file->filename;
434
unsigned char* modimage;
435
436
printf("\t--------compilation unit #%u-----------\n", index);
437
printf("\tName: %s\n", file_name);
438
if (strcmp(file_name, lib_name)) printf("\tLibrary: %s\n", lib_name);
439
printf("\t\tunknown1: %08x\n"
440
"\t\trange\n"
441
"\t\t\tsegment: %04x\n"
442
"\t\t\tpad1: %04x\n"
443
"\t\t\toffset: %08x\n"
444
"\t\t\tsize: %08x\n"
445
"\t\t\tcharacteristics: %08x",
446
sym_file->unknown1,
447
sym_file->range.segment,
448
sym_file->range.pad1,
449
sym_file->range.offset,
450
sym_file->range.size,
451
sym_file->range.characteristics);
452
dump_section_characteristics(sym_file->range.characteristics, " ");
453
printf("\n"
454
"\t\t\tindex: %04x\n"
455
"\t\t\tpad2: %04x\n",
456
sym_file->range.index,
457
sym_file->range.pad2);
458
if (new_format)
459
printf("\t\t\ttimestamp: %08x\n"
460
"\t\t\tunknown: %08x\n",
461
sym_file->range.timestamp,
462
sym_file->range.unknown);
463
printf("\t\tflag: %04x (%x%s%s)\n"
464
"\t\tstream: %04x\n"
465
"\t\tsymb size: %08x\n"
466
"\t\tline size: %08x\n"
467
"\t\tline2 size: %08x\n"
468
"\t\tnSrcFiles: %08x\n"
469
"\t\tattribute: %08x\n",
470
sym_file->flag,
471
HIBYTE(sym_file->flag),
472
sym_file->flag & 0x01 ? ";Dirty" : "",
473
sym_file->flag & 0x02 ? ";EC" : "",
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
/* first (skipped) DWORD is always 4... */
492
if (sym_file->symbol_size)
493
codeview_dump_symbols((const char*)modimage, sizeof(DWORD), sym_file->symbol_size);
494
495
/* line number info */
496
if (sym_file->lineno_size)
497
codeview_dump_linetab((const char*)modimage + sym_file->symbol_size, TRUE, " ");
498
else if (sym_file->lineno2_size) /* actually, only one of the 2 lineno should be present */
499
codeview_dump_linetab2((const char*)modimage + sym_file->symbol_size, sym_file->lineno2_size,
500
reader->global_string_table, " ");
501
/* what's that part ??? */
502
if (0)
503
dump_data(modimage + sym_file->symbol_size + sym_file->lineno_size + sym_file->lineno2_size,
504
total_size - (sym_file->symbol_size + sym_file->lineno_size + sym_file->lineno2_size), " ");
505
free(modimage);
506
}
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
const char* section_filter;
518
519
symbols = reader->read_stream(reader, 3);
520
if (!symbols) return;
521
522
if (globals_dump_sect("DBI"))
523
{
524
switch (symbols->version)
525
{
526
case 0: /* VC 4.0 */
527
case 19960307: /* VC 5.0 */
528
case 19970606: /* VC 6.0 */
529
case 19990903: /* VC 7.0 */
530
break;
531
default:
532
printf("-Unknown symbol info version %d\n", symbols->version);
533
}
534
if (symbols->flags & 0x8000) /* new */
535
sprintf(tcver, "%u.%u", (symbols->flags >> 8) & 0x7f, symbols->flags & 0xff);
536
else
537
sprintf(tcver, "old-%x", symbols->flags);
538
printf("Symbols:\n"
539
"\tsignature: %08x\n"
540
"\tversion: %u\n"
541
"\tage: %08x\n"
542
"\tglobal_hash_stream: %u\n"
543
"\tbuilder: %s\n"
544
"\tpublic_stream: %u\n"
545
"\tbldVer: %u\n"
546
"\tgsym_stream: %u\n"
547
"\trbldVer: %u\n"
548
"\tmodule_size: %08x\n"
549
"\tsectcontrib_size: %08x\n"
550
"\tsegmap_size: %08x\n"
551
"\tsrc_module_size: %08x\n"
552
"\tpdbimport_size: %08x\n"
553
"\tresvd0: %08x\n"
554
"\tstream_idx_size: %08x\n"
555
"\tunknown2_size: %08x\n"
556
"\tresvd3: %04x\n"
557
"\tmachine: %s\n"
558
"\tresvd4 %08x\n",
559
symbols->signature,
560
symbols->version,
561
symbols->age,
562
symbols->global_hash_stream,
563
tcver, /* from symbols->flags */
564
symbols->public_stream,
565
symbols->bldVer,
566
symbols->gsym_stream,
567
symbols->rbldVer,
568
symbols->module_size,
569
symbols->sectcontrib_size,
570
symbols->segmap_size,
571
symbols->srcmodule_size,
572
symbols->pdbimport_size,
573
symbols->resvd0,
574
symbols->stream_index_size,
575
symbols->unknown2_size,
576
symbols->resvd3,
577
get_machine_str( symbols->machine ),
578
symbols->resvd4);
579
}
580
581
if (symbols->sectcontrib_size && globals_dump_sect("image"))
582
{
583
const BYTE* src = (const BYTE*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size;
584
const BYTE* last = src + symbols->sectcontrib_size;
585
unsigned version, size;
586
587
printf("\t----------section contrib------------\n");
588
version = *(unsigned*)src;
589
printf("\tVersion: %#x (%d)\n", version, version - 0xeffe0000);
590
switch (version)
591
{
592
case 0xeffe0000 + 19970605: size = sizeof(PDB_SYMBOL_RANGE_EX); break;
593
case 0xeffe0000 + 20140516: size = sizeof(PDB_SYMBOL_RANGE_EX) + sizeof(unsigned); break;
594
default: printf("\t\tUnsupported version number\n"); size = 0;
595
}
596
if (size)
597
{
598
const PDB_SYMBOL_RANGE_EX* range;
599
600
if ((symbols->sectcontrib_size - sizeof(unsigned)) % size)
601
printf("Incoherent size: %zu = %zu * %u + %zu\n",
602
symbols->sectcontrib_size - sizeof(unsigned),
603
(symbols->sectcontrib_size - sizeof(unsigned)) / size,
604
size,
605
(symbols->sectcontrib_size - sizeof(unsigned)) % size);
606
for (src += sizeof(unsigned); src + size <= last; src += size)
607
{
608
range = (const PDB_SYMBOL_RANGE_EX*)src;
609
printf("\tRange #%tu\n",
610
((const BYTE*)range - ((const BYTE*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size)) / size);
611
printf("\t\tsegment: %04x\n"
612
"\t\tpad1: %04x\n"
613
"\t\toffset: %08x\n"
614
"\t\tsize: %08x\n"
615
"\t\tcharacteristics: %08x",
616
range->segment,
617
range->pad1,
618
range->offset,
619
range->size,
620
range->characteristics);
621
dump_section_characteristics(range->characteristics, " ");
622
printf("\n"
623
"\t\tindex: %04x\n"
624
"\t\tpad2: %04x\n"
625
"\t\ttimestamp: %08x\n"
626
"\t\tunknown: %08x\n",
627
range->index,
628
range->pad2,
629
range->timestamp,
630
range->unknown);
631
if (version == 0xeffe0000 + 20140516)
632
printf("\t\tcoff_section: %08x\n", *(unsigned*)(range + 1));
633
}
634
}
635
}
636
637
if (symbols->srcmodule_size && globals_dump_sect("DBI"))
638
{
639
const PDB_SYMBOL_SOURCE*src;
640
unsigned int i, j, cfile;
641
unsigned int num_source_files;
642
const WORD* indx;
643
const DWORD* offset;
644
const char* start_cstr;
645
const char* cstr;
646
647
printf("\t----------src module------------\n");
648
src = (const PDB_SYMBOL_SOURCE*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
649
symbols->module_size + symbols->sectcontrib_size + symbols->segmap_size);
650
printf("\tSource Modules\n"
651
"\t\tnModules: %u\n"
652
"\t\tnSrcFiles: %u\n",
653
src->nModules, src->nSrcFiles);
654
655
/* usage of table seems to be as follows:
656
* two arrays of WORD (src->nModules as size)
657
* - first array contains index into files for "module" compilation
658
* (module = compilation unit ??)
659
* - second array contains the number of source files in module
660
* an array of DWORD (src->nSrcFiles as size)
661
* - contains offset (in following string table) of the source file name
662
* a string table
663
* - each string is a pascal string (ie. with its length as first BYTE) or
664
* 0-terminated string (depending on version)
665
*/
666
indx = &src->table[src->nModules];
667
/* the file format limits the number of source files to 64K... so always
668
* recompute the number of source files and use that result instead of nSrcFiles
669
*/
670
num_source_files = 0;
671
for (i = 0; i < src->nModules; i++)
672
num_source_files += indx[i];
673
if (num_source_files != src->nSrcFiles)
674
printf("\t\tnSrcFiles: %u (overriden by computed value)\n", num_source_files);
675
offset = (const DWORD*)&src->table[2 * src->nModules];
676
cstr = (const char*)&src->table[2 * (src->nModules + num_source_files)];
677
start_cstr = cstr;
678
679
for (i = cfile = 0; i < src->nModules; i++)
680
{
681
printf("\t\tModule[%2d]:\n", i);
682
cfile = src->table[i];
683
for (j = cfile; j < num_source_files && j < cfile + indx[i]; j++)
684
{
685
/* FIXME: in some cases, it's a p_string but WHEN ? */
686
if (cstr + offset[j] >= start_cstr /* wrap around */ &&
687
cstr + offset[j] < (const char*)src + symbols->srcmodule_size)
688
printf("\t\t\tSource file: %s\n", cstr + offset[j]);
689
else
690
printf("\t\t\tSource file: <<out of bounds>>\n");
691
}
692
}
693
}
694
if (symbols->pdbimport_size && globals_dump_sect("PDB"))
695
{
696
const PDB_SYMBOL_IMPORT* imp;
697
const char* first;
698
const char* last;
699
const char* ptr;
700
701
printf("\t------------import--------------\n");
702
imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols + sizeof(PDB_SYMBOLS) +
703
symbols->module_size + symbols->sectcontrib_size +
704
symbols->segmap_size + symbols->srcmodule_size);
705
first = (const char*)imp;
706
last = (const char*)imp + symbols->pdbimport_size;
707
while (imp < (const PDB_SYMBOL_IMPORT*)last)
708
{
709
ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
710
printf("\tImport: %lx\n"
711
"\t\tUnknown1: %08x\n"
712
"\t\tUnknown2: %08x\n"
713
"\t\tTimeDateStamp: %08x\n"
714
"\t\tAge: %08u\n"
715
"\t\tfile1: %s\n"
716
"\t\tfile2: %s\n",
717
(ULONG_PTR)((const char*)imp - first),
718
imp->unknown1,
719
imp->unknown2,
720
imp->TimeDateStamp,
721
imp->Age,
722
imp->filename,
723
ptr);
724
imp = (const PDB_SYMBOL_IMPORT*)(first + ((ptr - first + strlen(ptr) + 1 + 3) & ~3));
725
}
726
}
727
if (symbols->segmap_size && globals_dump_sect("image"))
728
{
729
const struct OMFSegMap* segmap = (const struct OMFSegMap*)((const BYTE*)symbols + sizeof(PDB_SYMBOLS) +
730
symbols->module_size + symbols->sectcontrib_size);
731
const struct OMFSegMapDesc* desc = (const struct OMFSegMapDesc*)(segmap + 1);
732
733
printf("\t--------------segment map----------------\n");
734
printf("\tNumber of segments: %x\n", segmap->cSeg);
735
printf("\tNumber of logical segments: %x\n", segmap->cSegLog);
736
/* FIXME check mapping old symbols */
737
for (; (const BYTE*)(desc + 1) <= ((const BYTE*)(segmap + 1) + symbols->segmap_size); desc++)
738
{
739
printf("\t\tSegment descriptor #%tu\n", desc - (const struct OMFSegMapDesc*)(segmap + 1));
740
printf("\t\t\tFlags: %04x (%c%c%c%s%s%s%s)\n",
741
desc->flags,
742
(desc->flags & 0x01) ? 'R' : '-',
743
(desc->flags & 0x02) ? 'W' : '-',
744
(desc->flags & 0x04) ? 'X' : '-',
745
(desc->flags & 0x08) ? " 32bit-linear" : "",
746
(desc->flags & 0x100) ? " selector" : "",
747
(desc->flags & 0x200) ? " absolute" : "",
748
(desc->flags & 0x400) ? " group" : "");
749
printf("\t\t\tOverlay: %04x\n", desc->ovl);
750
printf("\t\t\tGroup: %04x\n", desc->group);
751
printf("\t\t\tFrame: %04x\n", desc->frame);
752
printf("\t\t\tSegment name: %s\n", desc->iSegName == 0xffff ? "none" : pdb_get_string_table_entry(reader->global_string_table, desc->iSegName));
753
printf("\t\t\tClass name: %s\n", desc->iClassName == 0xffff ? "none" : pdb_get_string_table_entry(reader->global_string_table, desc->iClassName));
754
printf("\t\t\tOffset: %08x\n", desc->offset);
755
printf("\t\t\tSize: %04x\n", desc->cbSeg);
756
}
757
}
758
if (symbols->unknown2_size && globals_dump_sect("PDB"))
759
{
760
const char* ptr = (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
761
symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
762
symbols->pdbimport_size;
763
printf("\t------------Unknown2--------------\n");
764
dump_string_table((const PDB_STRING_TABLE*)ptr, "Unknown from DBI", "\t");
765
}
766
if (symbols->stream_index_size && globals_dump_sect("image"))
767
{
768
const char* sub_stream_names[] = {"FPO", NULL, NULL, NULL, NULL, "Sections stream", NULL, NULL, NULL, "FPO-ext"};
769
int i;
770
771
printf("\t------------stream indexes--------------\n");
772
num_sub_streams = symbols->stream_index_size / sizeof(sub_streams[0]);
773
sub_streams = (const unsigned short*)((const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size +
774
symbols->sectcontrib_size + symbols->segmap_size + symbols->srcmodule_size +
775
symbols->pdbimport_size + symbols->unknown2_size);
776
for (i = 0; i < num_sub_streams; i++)
777
{
778
const char* name = "?";
779
if (i < ARRAY_SIZE(sub_stream_names) && sub_stream_names[i])
780
name = sub_stream_names[i];
781
printf("\t%s:%.*s%04x\n", name, (int)(21 - strlen(name)), "", sub_streams[i]);
782
}
783
}
784
785
/* Read global symbol table */
786
modimage = reader->read_stream(reader, symbols->gsym_stream);
787
if (modimage)
788
{
789
unsigned from = 0, to = pdb_get_stream_size(reader, symbols->gsym_stream);
790
791
if (globals_dump_sect_with_range("DBI", &from, &to))
792
{
793
printf("\t------------globals-------------\n");
794
codeview_dump_symbols(modimage, from, to);
795
free(modimage);
796
}
797
}
798
799
/* Read per-module symbol / linenumber tables */
800
section_filter = NULL;
801
if (symbols->module_size && globals_dump_sect_with_option("DBI", "compiland", &section_filter))
802
{
803
SIZE_T module_header_size = symbols->version < 19970000 ? sizeof(PDB_SYMBOL_FILE) : sizeof(PDB_SYMBOL_FILE_EX);
804
unsigned compiland_index = 0;
805
806
file = (const char*)symbols + sizeof(PDB_SYMBOLS);
807
while (file + module_header_size <= (const char*)symbols + sizeof(PDB_SYMBOLS) + symbols->module_size)
808
{
809
PDB_SYMBOL_FILE_EX copy;
810
const PDB_SYMBOL_FILE_EX *sym_file_ex;
811
const char *file_name;
812
const char *lib_name;
813
814
if (symbols->version < 19970000)
815
{
816
const PDB_SYMBOL_FILE* sym_file = (const PDB_SYMBOL_FILE*)file;
817
818
copy.unknown1 = sym_file->unknown1;
819
copy.range.segment = sym_file->range.segment;
820
copy.range.pad1 = sym_file->range.pad1;
821
copy.range.offset = sym_file->range.offset;
822
copy.range.size = sym_file->range.size;
823
copy.range.characteristics = sym_file->range.characteristics;
824
copy.range.index = sym_file->range.index;
825
copy.range.pad2 = sym_file->range.pad2;
826
copy.range.timestamp = 0;
827
copy.range.unknown = 0;
828
copy.flag = sym_file->flag;
829
copy.stream = sym_file->stream;
830
copy.symbol_size = sym_file->symbol_size;
831
copy.lineno_size = sym_file->lineno_size;
832
copy.lineno2_size = sym_file->lineno2_size;
833
copy.nSrcFiles = sym_file->nSrcFiles;
834
copy.attribute = sym_file->attribute;
835
copy.reserved[0] = 0;
836
copy.reserved[1] = 0;
837
file_name = sym_file->filename;
838
sym_file_ex = &copy;
839
}
840
else
841
{
842
sym_file_ex = (const void*)file;
843
file_name = sym_file_ex->filename;
844
}
845
lib_name = file_name + strlen(file_name) + 1;
846
if (!section_filter || strstr(file_name, section_filter) != NULL)
847
pdb_dump_dbi_module(reader, sym_file_ex, file_name, lib_name, compiland_index);
848
compiland_index++;
849
file = (const void*)((DWORD_PTR)(lib_name + strlen(lib_name) + 1 + 3) & ~3);
850
}
851
}
852
dump_global_symbol(reader, symbols->global_hash_stream);
853
dump_public_symbol(reader, symbols->public_stream);
854
855
if (sub_streams && globals_dump_sect("image"))
856
{
857
if (PDB_SIDX_FPO < num_sub_streams)
858
pdb_dump_fpo(reader, sub_streams[PDB_SIDX_FPO]);
859
if (PDB_SIDX_FPOEXT < num_sub_streams)
860
pdb_dump_fpo_ext(reader, sub_streams[PDB_SIDX_FPOEXT]);
861
if (PDB_SIDX_SECTIONS < num_sub_streams)
862
pdb_dump_sections(reader, sub_streams[PDB_SIDX_SECTIONS]);
863
}
864
865
free(symbols);
866
}
867
868
static BOOL is_bit_set(const unsigned* dw, unsigned len, unsigned i)
869
{
870
if (i >= len * sizeof(unsigned) * 8) return FALSE;
871
return (dw[i >> 5] & (1u << (i & 31u))) != 0;
872
}
873
874
static void pdb_dump_hash_value(const BYTE* ptr, unsigned len)
875
{
876
int i;
877
878
printf("[");
879
for (i = len - 1; i >= 0; i--)
880
printf("%02x", ptr[i]);
881
printf("]");
882
}
883
884
static struct
885
{
886
const BYTE* hash;
887
unsigned hash_size;
888
} collision_arg;
889
890
static int collision_compar(const void *p1, const void *p2)
891
{
892
unsigned idx1 = *(unsigned*)p1;
893
unsigned idx2 = *(unsigned*)p2;
894
return memcmp(collision_arg.hash + idx1 * collision_arg.hash_size,
895
collision_arg.hash + idx2 * collision_arg.hash_size,
896
collision_arg.hash_size);
897
}
898
899
static void pdb_dump_types_hash(struct pdb_reader* reader, const PDB_TYPES* types, const char* strmname)
900
{
901
void* hash = NULL;
902
unsigned i, strmsize;
903
const unsigned* table;
904
unsigned *collision;
905
906
if (!globals_dump_sect("hash")) return;
907
hash = reader->read_stream(reader, types->hash_stream);
908
if (!hash) return;
909
910
printf("Types (%s) hash:\n", strmname);
911
strmsize = pdb_get_stream_size(reader, types->hash_stream);
912
if (types->hash_offset + types->hash_size > strmsize ||
913
(types->last_index - types->first_index) * types->hash_value_size != types->hash_size ||
914
types->search_offset + types->search_size > strmsize ||
915
types->type_remap_offset + types->type_remap_size > strmsize)
916
{
917
printf("\nIncoherent sizes... skipping\n");
918
return;
919
}
920
printf("\n\tIndexes => hash value:\n");
921
for (i = types->first_index; i < types->last_index; i++)
922
{
923
printf("\t\t%08x => ", i);
924
pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + (i - types->first_index) * types->hash_value_size, types->hash_value_size);
925
printf("\n");
926
}
927
/* print collisions in hash table (if any) */
928
collision = malloc((types->last_index - types->first_index) * sizeof(unsigned));
929
if (collision)
930
{
931
unsigned head_printed = 0;
932
933
collision_arg.hash = (const BYTE*)hash + types->hash_offset;
934
collision_arg.hash_size = types->hash_value_size;
935
936
for (i = 0; i < types->last_index - types->first_index; i++) collision[i] = i;
937
qsort(collision, types->last_index - types->first_index, sizeof(unsigned), collision_compar);
938
for (i = 0; i < types->last_index - types->first_index; i++)
939
{
940
unsigned j;
941
for (j = i + 1; j < types->last_index - types->first_index; j++)
942
if (memcmp((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size,
943
(const BYTE*)hash + types->hash_offset + collision[j] * types->hash_value_size,
944
types->hash_value_size))
945
break;
946
if (j > i + 1)
947
{
948
unsigned k;
949
if (!head_printed)
950
{
951
printf("\n\t\tCollisions:\n");
952
head_printed = 1;
953
}
954
printf("\t\t\tHash ");
955
pdb_dump_hash_value((const BYTE*)hash + types->hash_offset + collision[i] * types->hash_value_size, types->hash_value_size);
956
printf(":");
957
for (k = i; k < j; k++)
958
printf(" %x", types->first_index + collision[k]);
959
printf("\n");
960
i = j - 1;
961
}
962
}
963
free(collision);
964
}
965
printf("\n\tIndexes => offsets:\n");
966
table = (const unsigned*)((const BYTE*)hash + types->search_offset);
967
for (i = 0; i < types->search_size / (2 * sizeof(unsigned)); i++)
968
{
969
printf("\t\t%08x => %08x\n", table[2 * i + 0], table[2 * i + 1]);
970
}
971
972
if (types->type_remap_size)
973
{
974
unsigned num, capa, count_present, count_deleted;
975
const unsigned* present_bitset;
976
const unsigned* deleted_bitset;
977
978
printf("\n\tType remap:\n");
979
table = (const unsigned*)((const BYTE*)hash + types->type_remap_offset);
980
num = *table++;
981
capa = *table++;
982
count_present = *table++;
983
present_bitset = table;
984
table += count_present;
985
count_deleted = *table++;
986
deleted_bitset = table;
987
table += count_deleted;
988
printf("\t\tNumber of present entries: %u\n", num);
989
printf("\t\tCapacity: %u\n", capa);
990
printf("\t\tBitset present:\n");
991
printf("\t\t\tCount: %u\n", count_present);
992
printf("\t\t\tBitset: ");
993
pdb_dump_hash_value((const BYTE*)present_bitset, count_present * sizeof(unsigned));
994
printf("\n");
995
printf("\t\tBitset deleted:\n");
996
printf("\t\t\tCount: %u\n", count_deleted);
997
printf("\t\t\tBitset: ");
998
pdb_dump_hash_value((const BYTE*)deleted_bitset, count_deleted * sizeof(unsigned));
999
printf("\n");
1000
for (i = 0; i < capa; ++i)
1001
{
1002
printf("\t\t%2u) %c",
1003
i,
1004
is_bit_set(present_bitset, count_present, i) ? 'P' :
1005
is_bit_set(deleted_bitset, count_deleted, i) ? 'D' : '_');
1006
if (is_bit_set(present_bitset, count_present, i))
1007
{
1008
printf(" %s => ", pdb_get_string_table_entry(reader->global_string_table, *table++));
1009
pdb_dump_hash_value((const BYTE*)table, types->hash_value_size);
1010
table = (const unsigned*)((const BYTE*)table + types->hash_value_size);
1011
}
1012
printf("\n");
1013
}
1014
printf("\n");
1015
}
1016
free(hash);
1017
}
1018
1019
/* there are two 'type' related streams, but with different indexes... */
1020
static void pdb_dump_types(struct pdb_reader* reader, unsigned strmidx, const char* strmname)
1021
{
1022
PDB_TYPES* types = NULL;
1023
BOOL used = has_stream_been_read(reader, strmidx);
1024
unsigned from = 0x1000, to = ~0u;
1025
1026
if (!globals_dump_sect_with_range(strmidx == 2 ? "TPI" : "IPI", &from, &to)) return;
1027
1028
if (pdb_get_stream_size(reader, strmidx) < sizeof(*types))
1029
{
1030
if (strmidx == 2)
1031
printf("-Too small type header\n");
1032
return;
1033
}
1034
types = reader->read_stream(reader, strmidx);
1035
if (!types) return;
1036
1037
switch (types->version)
1038
{
1039
case 19950410: /* VC 4.0 */
1040
case 19951122:
1041
case 19961031: /* VC 5.0 / 6.0 */
1042
case 19990903: /* VC 7.0 */
1043
case 20040203: /* VC 8.0 */
1044
break;
1045
default:
1046
/* IPI stream is not always present in older PDB files */
1047
if (strmidx == 2)
1048
printf("-Unknown type info version %d\n", types->version);
1049
free(types);
1050
if (used) clear_stream_been_read(reader, strmidx);
1051
return;
1052
}
1053
1054
/* Read type table */
1055
printf("Types (%s):\n"
1056
"\tversion: %u\n"
1057
"\ttype_offset: %08x\n"
1058
"\tfirst_index: %x\n"
1059
"\tlast_index: %x\n"
1060
"\ttype_size: %x\n"
1061
"\thash_stream: %x\n"
1062
"\tpad: %x\n"
1063
"\thash_value_size: %x\n"
1064
"\thash_buckets %x\n"
1065
"\thash_offset: %x\n"
1066
"\thash_size: %x\n"
1067
"\tsearch_offset: %x\n"
1068
"\tsearch_size: %x\n"
1069
"\ttype_remap_offset: %x\n"
1070
"\ttype_remap_size: %x\n",
1071
strmname,
1072
types->version,
1073
types->type_offset,
1074
types->first_index,
1075
types->last_index,
1076
types->type_size,
1077
types->hash_stream,
1078
types->pad,
1079
types->hash_value_size,
1080
types->hash_num_buckets,
1081
types->hash_offset,
1082
types->hash_size,
1083
types->search_offset,
1084
types->search_size,
1085
types->type_remap_offset,
1086
types->type_remap_size);
1087
codeview_dump_types_from_block((const char*)types + types->type_offset, types->type_size, from, to);
1088
pdb_dump_types_hash(reader, types, strmname);
1089
free(types);
1090
}
1091
1092
static void pdb_dump_fpo(struct pdb_reader* reader, unsigned stream_idx)
1093
{
1094
FPO_DATA* fpo;
1095
unsigned i, size;
1096
const char* frame_type[4] = {"Fpo", "Trap", "Tss", "NonFpo"};
1097
1098
if (stream_idx == (WORD)-1) return;
1099
fpo = reader->read_stream(reader, stream_idx);
1100
size = pdb_get_stream_size(reader, stream_idx);
1101
if (fpo && (size % sizeof(*fpo)) == 0)
1102
{
1103
size /= sizeof(*fpo);
1104
printf("FPO data:\n\t Start Length #loc #pmt #prolog #reg frame SEH /BP\n");
1105
for (i = 0; i < size; i++)
1106
{
1107
printf("\t%08x %08x %4d %4d %7d %4d %6s %c %c\n",
1108
(UINT)fpo[i].ulOffStart, (UINT)fpo[i].cbProcSize, (UINT)fpo[i].cdwLocals, fpo[i].cdwParams,
1109
fpo[i].cbProlog, fpo[i].cbRegs, frame_type[fpo[i].cbFrame],
1110
fpo[i].fHasSEH ? 'Y' : 'N', fpo[i].fUseBP ? 'Y' : 'N');
1111
}
1112
}
1113
free(fpo);
1114
}
1115
1116
static void pdb_dump_fpo_ext(struct pdb_reader* reader, unsigned stream_idx)
1117
{
1118
PDB_FPO_DATA* fpoext;
1119
unsigned i, size;
1120
1121
if (stream_idx == (WORD)-1) return;
1122
1123
fpoext = reader->read_stream(reader, stream_idx);
1124
size = pdb_get_stream_size(reader, stream_idx);
1125
if (fpoext && (size % sizeof(*fpoext)) == 0)
1126
{
1127
size /= sizeof(*fpoext);
1128
printf("FPO data (extended):\n"
1129
"\t Start Length Locals Params MaxStack Prolog #SavedRegs Flags Command\n");
1130
for (i = 0; i < size; i++)
1131
{
1132
printf("\t%08x %08x %8x %8x %8x %6x %8x %08x %s\n",
1133
fpoext[i].start, fpoext[i].func_size, fpoext[i].locals_size, fpoext[i].params_size,
1134
fpoext[i].maxstack_size, fpoext[i].prolog_size, fpoext[i].savedregs_size, fpoext[i].flags,
1135
pdb_get_string_table_entry(reader->global_string_table, fpoext[i].str_offset));
1136
}
1137
}
1138
free(fpoext);
1139
}
1140
1141
static void pdb_dump_sections(struct pdb_reader* reader, unsigned stream_idx)
1142
{
1143
const char* segs;
1144
DWORD size;
1145
const IMAGE_SECTION_HEADER* sect_hdr;
1146
1147
if (stream_idx == (WORD)-1) return;
1148
segs = reader->read_stream(reader, stream_idx);
1149
1150
if (segs)
1151
{
1152
printf("Sections:\n");
1153
size = pdb_get_stream_size(reader, stream_idx);
1154
for (sect_hdr = (const IMAGE_SECTION_HEADER*)segs; (const char*)sect_hdr < segs + size; sect_hdr++)
1155
{
1156
printf("\tSection: %-8.8s\n", sect_hdr->Name);
1157
printf("\t\tVirtual size: %08x\n", (unsigned)sect_hdr->Misc.VirtualSize);
1158
printf("\t\tVirtualAddress: %08x\n", (unsigned)sect_hdr->VirtualAddress);
1159
printf("\t\tSizeOfRawData: %08x\n", (unsigned)sect_hdr->SizeOfRawData);
1160
printf("\t\tPointerToRawData: %08x\n", (unsigned)sect_hdr->PointerToRawData);
1161
printf("\t\tPointerToRelocations: %08x\n", (unsigned)sect_hdr->PointerToRelocations);
1162
printf("\t\tPointerToLinenumbers: %08x\n", (unsigned)sect_hdr->PointerToLinenumbers);
1163
printf("\t\tNumberOfRelocations: %u\n", (unsigned)sect_hdr->NumberOfRelocations);
1164
printf("\t\tNumberOfLinenumbers: %u\n", (unsigned)sect_hdr->NumberOfLinenumbers);
1165
printf("\t\tCharacteristics: %08x", (unsigned)sect_hdr->Characteristics);
1166
dump_section_characteristics(sect_hdr->Characteristics, " ");
1167
printf("\n");
1168
}
1169
free((char*)segs);
1170
}
1171
}
1172
1173
static const char pdb2[] = "Microsoft C/C++ program database 2.00";
1174
1175
static void pdb_jg_dump_header_root(struct pdb_reader* reader)
1176
{
1177
UINT *pdw, *ok_bits;
1178
UINT i, numok, count;
1179
1180
if (!globals_dump_sect("PDB")) return;
1181
1182
printf("Header (JG):\n"
1183
"\tident: %.*s\n"
1184
"\tsignature: %08x\n"
1185
"\tblock_size: %08x\n"
1186
"\tfree_list_block: %04x\n"
1187
"\ttotal_alloc: %04x\n",
1188
(int)sizeof(pdb2) - 1, reader->u.jg.header.ident,
1189
reader->u.jg.header.signature,
1190
reader->u.jg.header.block_size,
1191
reader->u.jg.header.free_list_block,
1192
reader->u.jg.header.total_alloc);
1193
1194
printf("Root:\n"
1195
"\tVersion: %u\n"
1196
"\tTimeDateStamp: %08x\n"
1197
"\tAge: %08x\n"
1198
"\tnames: %d\n",
1199
reader->u.jg.root->Version,
1200
reader->u.jg.root->TimeDateStamp,
1201
reader->u.jg.root->Age,
1202
(unsigned)reader->u.jg.root->cbNames);
1203
1204
pdw = (UINT *)(reader->u.jg.root->names + reader->u.jg.root->cbNames);
1205
numok = *pdw++;
1206
count = *pdw++;
1207
printf("\tStreams directory:\n"
1208
"\t\tok: %08x\n"
1209
"\t\tcount: %08x\n"
1210
"\t\ttable:\n",
1211
numok, count);
1212
1213
/* bitfield: first dword is len (in dword), then data */
1214
ok_bits = pdw;
1215
pdw += *ok_bits++ + 1;
1216
pdw += *pdw + 1; /* skip deleted vector */
1217
1218
for (i = 0; i < count; i++)
1219
{
1220
if (ok_bits[i / 32] & (1 << (i % 32)))
1221
{
1222
UINT string_idx, stream_idx;
1223
string_idx = *pdw++;
1224
stream_idx = *pdw++;
1225
printf("\t\t\t%2d) %-20s => %x\n", i, &reader->u.jg.root->names[string_idx], stream_idx);
1226
numok--;
1227
}
1228
}
1229
if (numok) printf(">>> unmatched present field with found\n");
1230
1231
/* Check for unknown versions */
1232
switch (reader->u.jg.root->Version)
1233
{
1234
case 19950623: /* VC 4.0 */
1235
case 19950814:
1236
case 19960307: /* VC 5.0 */
1237
case 19970604: /* VC 6.0 */
1238
break;
1239
default:
1240
printf("-Unknown root block version %d\n", reader->u.jg.root->Version);
1241
}
1242
}
1243
1244
static void* pdb_ds_read(int fd, const struct PDB_DS_HEADER* header, const UINT *block_list, unsigned int size)
1245
{
1246
unsigned int i, nBlocks;
1247
BYTE* buffer;
1248
1249
if (!size) return NULL;
1250
1251
nBlocks = NUMBER_OF(size, header->block_size);
1252
buffer = xmalloc(nBlocks * header->block_size);
1253
1254
for (i = 0; i < nBlocks; i++)
1255
if (pdb_read_at(fd, buffer + i * header->block_size, header->block_size,
1256
(size_t)block_list[i] * header->block_size) != header->block_size)
1257
{
1258
free(buffer);
1259
return NULL;
1260
}
1261
1262
return buffer;
1263
}
1264
1265
static void* pdb_ds_read_stream(struct pdb_reader* reader, DWORD stream_number)
1266
{
1267
const UINT *block_list;
1268
UINT i;
1269
1270
if (!reader->u.ds.toc || stream_number >= reader->u.ds.toc->num_streams) return NULL;
1271
1272
mark_stream_been_read(reader, stream_number);
1273
if (reader->u.ds.toc->stream_size[stream_number] == 0 ||
1274
reader->u.ds.toc->stream_size[stream_number] == 0xFFFFFFFF)
1275
return NULL;
1276
block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1277
for (i = 0; i < stream_number; i++)
1278
block_list += NUMBER_OF(reader->u.ds.toc->stream_size[i], reader->u.ds.header.block_size);
1279
1280
return pdb_ds_read(reader->fd, &reader->u.ds.header, block_list, reader->u.ds.toc->stream_size[stream_number]);
1281
}
1282
1283
static BOOL pdb_ds_init(int fd, struct pdb_reader* reader)
1284
{
1285
unsigned size_blocks;
1286
unsigned *blocks;
1287
BOOL ret;
1288
1289
if (pdb_read_at(fd, &reader->u.ds.header, sizeof(reader->u.ds.header), 0) != sizeof(reader->u.ds.header)) return FALSE;
1290
reader->fd = fd;
1291
reader->read_stream = pdb_ds_read_stream;
1292
size_blocks = NUMBER_OF(reader->u.ds.header.toc_size, reader->u.ds.header.block_size) * sizeof(blocks[0]);
1293
blocks = xmalloc(size_blocks);
1294
ret = pdb_read_at(fd, blocks, size_blocks,
1295
(size_t)reader->u.ds.header.toc_block * reader->u.ds.header.block_size) == size_blocks;
1296
if (ret)
1297
ret = (reader->u.ds.toc = pdb_ds_read(reader->fd, &reader->u.ds.header,
1298
blocks, reader->u.ds.header.toc_size)) != NULL;
1299
free(blocks);
1300
if (ret)
1301
ret = (reader->stream_used = calloc(sizeof(DWORD),
1302
NUMBER_OF(reader->u.ds.toc->num_streams, sizeof(DWORD) * 8))) != NULL;
1303
if (ret)
1304
ret = (reader->u.ds.root = reader->read_stream(reader, 1)) != NULL;
1305
1306
return ret;
1307
}
1308
1309
static const char pdb7[] = "Microsoft C/C++ MSF 7.00";
1310
1311
static void pdb_ds_dump_header_root(struct pdb_reader* reader)
1312
{
1313
unsigned int i, j, ofs;
1314
const UINT *block_list;
1315
UINT *pdw, *ok_bits;
1316
UINT numok, count;
1317
unsigned strmsize;
1318
1319
if (!globals_dump_sect("PDB")) return;
1320
strmsize = pdb_get_stream_size(reader, 1);
1321
printf("Header (DS)\n"
1322
"\tsignature: %.*s\n"
1323
"\tblock_size: %08x\n"
1324
"\tfree_list_block: %08x\n"
1325
"\tnum_blocks: %08x\n"
1326
"\ttoc_size: %08x\n"
1327
"\tunknown2: %08x\n"
1328
"\ttoc_block: %08x\n",
1329
(int)sizeof(pdb7) - 1, reader->u.ds.header.signature,
1330
reader->u.ds.header.block_size,
1331
reader->u.ds.header.free_list_block,
1332
reader->u.ds.header.num_blocks,
1333
reader->u.ds.header.toc_size,
1334
reader->u.ds.header.unknown2,
1335
reader->u.ds.header.toc_block);
1336
1337
block_list = reader->u.ds.toc->stream_size + reader->u.ds.toc->num_streams;
1338
printf("\t\tnum_streams: %u\n", reader->u.ds.toc->num_streams);
1339
for (ofs = i = 0; i < reader->u.ds.toc->num_streams; i++)
1340
{
1341
unsigned int nblk = NUMBER_OF(reader->u.ds.toc->stream_size[i], reader->u.ds.header.block_size);
1342
printf("\t\tstream[%#x]:\tsize: %u\n", i, reader->u.ds.toc->stream_size[i]);
1343
if (nblk)
1344
{
1345
for (j = 0; j < nblk; j++)
1346
{
1347
if (j % 16 == 0) printf("\t\t\t");
1348
printf("%4x ", block_list[ofs + j]);
1349
if (j % 16 == 15 || (j + 1 == nblk)) printf("\n");
1350
}
1351
ofs += nblk;
1352
}
1353
}
1354
1355
printf("Root:\n"
1356
"\tVersion: %u\n"
1357
"\tTimeDateStamp: %08x\n"
1358
"\tAge: %08x\n"
1359
"\tguid %s\n"
1360
"\tcbNames: %08x\n",
1361
reader->u.ds.root->Version,
1362
reader->u.ds.root->TimeDateStamp,
1363
reader->u.ds.root->Age,
1364
get_guid_str(&reader->u.ds.root->guid),
1365
reader->u.ds.root->cbNames);
1366
pdw = (UINT *)(reader->u.ds.root->names + reader->u.ds.root->cbNames);
1367
numok = *pdw++;
1368
count = *pdw++;
1369
printf("\tStreams directory:\n"
1370
"\t\tok: %08x\n"
1371
"\t\tcount: %08x\n"
1372
"\t\ttable:\n",
1373
numok, count);
1374
1375
/* bitfield: first dword is len (in dword), then data */
1376
ok_bits = pdw;
1377
pdw += *ok_bits++ + 1;
1378
pdw += *pdw + 1; /* skip deleted vector */
1379
1380
for (i = 0; i < count; i++)
1381
{
1382
if (ok_bits[i / 32] & (1 << (i % 32)))
1383
{
1384
UINT string_idx, stream_idx;
1385
string_idx = *pdw++;
1386
stream_idx = *pdw++;
1387
printf("\t\t\t%2d) %-20s => %x\n", i, &reader->u.ds.root->names[string_idx], stream_idx);
1388
numok--;
1389
}
1390
}
1391
if (numok) printf(">>> unmatched present field with found\n");
1392
if (*pdw++ != 0)
1393
{
1394
printf("unexpected value\n");
1395
return;
1396
}
1397
1398
if (pdw + 1 <= (UINT*)((char*)reader->u.ds.root + strmsize))
1399
{
1400
/* extra information (version reference and features) */
1401
printf("\tVersion and features\n");
1402
while (pdw + 1 <= (UINT*)((char*)reader->u.ds.root + strmsize))
1403
{
1404
switch (*pdw)
1405
{
1406
/* version reference */
1407
case 20091201: printf("\t\tVC110\n"); break;
1408
case 20140508: printf("\t\tVC140\n"); break;
1409
/* features */
1410
case 0x4D544F4E /* NOTM */: printf("\t\tNo type merge\n"); break;
1411
case 0x494E494D /* MINI */: printf("\t\tMinimal debug info\n"); break;
1412
default: printf("\t\tUnknown value %x\n", *pdw);
1413
}
1414
pdw++;
1415
}
1416
}
1417
}
1418
1419
enum FileSig get_kind_pdb(int fd)
1420
{
1421
char tmp[max(sizeof(pdb7), sizeof(pdb2)) - 1];
1422
1423
if (read(fd, tmp, sizeof(tmp)) == sizeof(tmp) &&
1424
(!memcmp(tmp, pdb2, sizeof(pdb2) - 1) ||
1425
!memcmp(tmp, pdb7, sizeof(pdb7) - 1)))
1426
return SIG_PDB;
1427
return SIG_UNKNOWN;
1428
}
1429
1430
void pdb_dump(int fd)
1431
{
1432
char tmp[max(sizeof(pdb2), sizeof(pdb7)) - 1];
1433
const char** saved_dumpsect = globals.dumpsect;
1434
static const char* default_dumpsect[] = {"DBI", "TPI", "IPI", NULL};
1435
struct pdb_reader reader;
1436
1437
if (!globals.dumpsect) globals.dumpsect = default_dumpsect;
1438
1439
if (read(fd, tmp, sizeof(tmp)) == sizeof(tmp))
1440
{
1441
if (!memcmp(tmp, pdb2, sizeof(pdb2) - 1))
1442
{
1443
if (!pdb_jg_init(fd, &reader))
1444
{
1445
printf("Unable to get header information\n");
1446
return;
1447
}
1448
pdb_jg_dump_header_root(&reader);
1449
}
1450
else if (!memcmp(tmp, pdb7, sizeof(pdb7) - 1))
1451
{
1452
if (!pdb_ds_init(fd, &reader))
1453
{
1454
printf("Unable to get header information\n");
1455
return;
1456
}
1457
pdb_ds_dump_header_root(&reader);
1458
}
1459
}
1460
mark_stream_been_read(&reader, 0); /* mark stream #0 (old TOC) as read */
1461
1462
reader.global_string_table = read_string_table(&reader);
1463
1464
pdb_dump_types(&reader, 2, "TPI");
1465
pdb_dump_types(&reader, 4, "IPI");
1466
pdb_dump_symbols(&reader);
1467
1468
pdb_exit(&reader);
1469
1470
globals.dumpsect = saved_dumpsect;
1471
}
1472
1473