1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
//===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/DebugInfo/CodeView/StreamArray.h"
#include "llvm/DebugInfo/CodeView/StreamInterface.h"
#include "llvm/DebugInfo/CodeView/StreamReader.h"
#include "llvm/DebugInfo/CodeView/StreamWriter.h"
#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
#include "llvm/DebugInfo/PDB/Raw/DirectoryStreamData.h"
#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
#include "llvm/DebugInfo/PDB/Raw/RawError.h"
#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace llvm;
using namespace llvm::codeview;
using namespace llvm::pdb;
namespace {
typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
}
PDBFile::PDBFile(std::unique_ptr<StreamInterface> PdbFileBuffer)
: Buffer(std::move(PdbFileBuffer)), SB(nullptr) {}
PDBFile::~PDBFile() {}
uint32_t PDBFile::getBlockSize() const { return SB->BlockSize; }
uint32_t PDBFile::getUnknown0() const { return SB->Unknown0; }
uint32_t PDBFile::getBlockCount() const { return SB->NumBlocks; }
uint32_t PDBFile::getNumDirectoryBytes() const { return SB->NumDirectoryBytes; }
uint32_t PDBFile::getBlockMapIndex() const { return SB->BlockMapAddr; }
uint32_t PDBFile::getUnknown1() const { return SB->Unknown1; }
uint32_t PDBFile::getNumDirectoryBlocks() const {
return bytesToBlocks(SB->NumDirectoryBytes, SB->BlockSize);
}
uint64_t PDBFile::getBlockMapOffset() const {
return (uint64_t)SB->BlockMapAddr * SB->BlockSize;
}
uint32_t PDBFile::getNumStreams() const { return StreamSizes.size(); }
uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
return StreamSizes[StreamIndex];
}
ArrayRef<support::ulittle32_t>
PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
return StreamMap[StreamIndex];
}
uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
uint32_t NumBytes) const {
uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
ArrayRef<uint8_t> Result;
if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
return std::move(EC);
return Result;
}
Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
ArrayRef<uint8_t> Data) const {
if (Offset >= getBlockSize())
return make_error<RawError>(
raw_error_code::invalid_block_address,
"setBlockData attempted to write out of block bounds.");
if (Data.size() > getBlockSize() - Offset)
return make_error<RawError>(
raw_error_code::invalid_block_address,
"setBlockData attempted to write out of block bounds.");
uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
StreamBlockOffset += Offset;
return Buffer->writeBytes(StreamBlockOffset, Data);
}
Error PDBFile::parseFileHeaders() {
StreamReader Reader(*Buffer);
if (auto EC = Reader.readObject(SB)) {
consumeError(std::move(EC));
return make_error<RawError>(raw_error_code::corrupt_file,
"Does not contain superblock");
}
if (auto EC = setSuperBlock(SB))
return EC;
Reader.setOffset(getBlockMapOffset());
if (auto EC = Reader.readArray(DirectoryBlocks, getNumDirectoryBlocks()))
return EC;
return Error::success();
}
Error PDBFile::parseStreamData() {
assert(SB);
if (DirectoryStream)
return Error::success();
uint32_t NumStreams = 0;
// Normally you can't use a MappedBlockStream without having fully parsed the
// PDB file, because it accesses the directory and various other things, which
// is exactly what we are attempting to parse. By specifying a custom
// subclass of IPDBStreamData which only accesses the fields that have already
// been parsed, we can avoid this and reuse MappedBlockStream.
auto DS = MappedBlockStream::createDirectoryStream(*this);
if (!DS)
return DS.takeError();
StreamReader Reader(**DS);
if (auto EC = Reader.readInteger(NumStreams))
return EC;
if (auto EC = Reader.readArray(StreamSizes, NumStreams))
return EC;
for (uint32_t I = 0; I < NumStreams; ++I) {
uint32_t StreamSize = getStreamByteSize(I);
// FIXME: What does StreamSize ~0U mean?
uint64_t NumExpectedStreamBlocks =
StreamSize == UINT32_MAX ? 0 : bytesToBlocks(StreamSize, SB->BlockSize);
// For convenience, we store the block array contiguously. This is because
// if someone calls setStreamMap(), it is more convenient to be able to call
// it with an ArrayRef instead of setting up a StreamRef. Since the
// DirectoryStream is cached in the class and thus lives for the life of the
// class, we can be guaranteed that readArray() will return a stable
// reference, even if it has to allocate from its internal pool.
ArrayRef<support::ulittle32_t> Blocks;
if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
return EC;
for (uint32_t Block : Blocks) {
uint64_t BlockEndOffset = (uint64_t)(Block + 1) * SB->BlockSize;
if (BlockEndOffset > getFileSize())
return make_error<RawError>(raw_error_code::corrupt_file,
"Stream block map is corrupt.");
}
StreamMap.push_back(Blocks);
}
// We should have read exactly SB->NumDirectoryBytes bytes.
assert(Reader.bytesRemaining() == 0);
DirectoryStream = std::move(*DS);
return Error::success();
}
llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
return DirectoryBlocks;
}
Expected<InfoStream &> PDBFile::emplacePDBInfoStream() {
if (Info)
Info.reset();
auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this);
if (!InfoS)
return InfoS.takeError();
Info = llvm::make_unique<InfoStream>(std::move(*InfoS));
return *Info;
}
Expected<InfoStream &> PDBFile::getPDBInfoStream() {
if (!Info) {
auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this);
if (!InfoS)
return InfoS.takeError();
auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
if (auto EC = TempInfo->reload())
return std::move(EC);
Info = std::move(TempInfo);
}
return *Info;
}
Expected<DbiStream &> PDBFile::getPDBDbiStream() {
if (!Dbi) {
auto DbiS = MappedBlockStream::createIndexedStream(StreamDBI, *this);
if (!DbiS)
return DbiS.takeError();
auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
if (auto EC = TempDbi->reload())
return std::move(EC);
Dbi = std::move(TempDbi);
}
return *Dbi;
}
Expected<TpiStream &> PDBFile::getPDBTpiStream() {
if (!Tpi) {
auto TpiS = MappedBlockStream::createIndexedStream(StreamTPI, *this);
if (!TpiS)
return TpiS.takeError();
auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
if (auto EC = TempTpi->reload())
return std::move(EC);
Tpi = std::move(TempTpi);
}
return *Tpi;
}
Expected<TpiStream &> PDBFile::getPDBIpiStream() {
if (!Ipi) {
auto IpiS = MappedBlockStream::createIndexedStream(StreamIPI, *this);
if (!IpiS)
return IpiS.takeError();
auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
if (auto EC = TempIpi->reload())
return std::move(EC);
Ipi = std::move(TempIpi);
}
return *Ipi;
}
Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
if (!Publics) {
auto DbiS = getPDBDbiStream();
if (!DbiS)
return DbiS.takeError();
uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
auto PublicS =
MappedBlockStream::createIndexedStream(PublicsStreamNum, *this);
if (!PublicS)
return PublicS.takeError();
auto TempPublics =
llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
if (auto EC = TempPublics->reload())
return std::move(EC);
Publics = std::move(TempPublics);
}
return *Publics;
}
Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
if (!Symbols) {
auto DbiS = getPDBDbiStream();
if (!DbiS)
return DbiS.takeError();
uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
auto SymbolS =
MappedBlockStream::createIndexedStream(SymbolStreamNum, *this);
if (!SymbolS)
return SymbolS.takeError();
auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
if (auto EC = TempSymbols->reload())
return std::move(EC);
Symbols = std::move(TempSymbols);
}
return *Symbols;
}
Expected<NameHashTable &> PDBFile::getStringTable() {
if (!StringTable || !StringTableStream) {
auto IS = getPDBInfoStream();
if (!IS)
return IS.takeError();
uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
if (NameStreamIndex == 0)
return make_error<RawError>(raw_error_code::no_stream);
if (NameStreamIndex >= getNumStreams())
return make_error<RawError>(raw_error_code::no_stream);
auto NS = MappedBlockStream::createIndexedStream(NameStreamIndex, *this);
if (!NS)
return NS.takeError();
StreamReader Reader(**NS);
auto N = llvm::make_unique<NameHashTable>();
if (auto EC = N->load(Reader))
return std::move(EC);
StringTable = std::move(N);
StringTableStream = std::move(*NS);
}
return *StringTable;
}
Error PDBFile::setSuperBlock(const SuperBlock *Block) {
SB = Block;
// Check the magic bytes.
if (memcmp(SB->MagicBytes, MsfMagic, sizeof(MsfMagic)) != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"MSF magic header doesn't match");
// We don't support blocksizes which aren't a multiple of four bytes.
if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"Block size is not multiple of 4.");
switch (SB->BlockSize) {
case 512:
case 1024:
case 2048:
case 4096:
break;
default:
// An invalid block size suggests a corrupt PDB file.
return make_error<RawError>(raw_error_code::corrupt_file,
"Unsupported block size.");
}
if (Buffer->getLength() % SB->BlockSize != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"File size is not a multiple of block size");
// We don't support directories whose sizes aren't a multiple of four bytes.
if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
return make_error<RawError>(raw_error_code::corrupt_file,
"Directory size is not multiple of 4.");
// The number of blocks which comprise the directory is a simple function of
// the number of bytes it contains.
uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
// The directory, as we understand it, is a block which consists of a list of
// block numbers. It is unclear what would happen if the number of blocks
// couldn't fit on a single block.
if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
return make_error<RawError>(raw_error_code::corrupt_file,
"Too many directory blocks.");
return Error::success();
}
void PDBFile::setStreamSizes(ArrayRef<support::ulittle32_t> Sizes) {
StreamSizes = Sizes;
}
void PDBFile::setStreamMap(
std::vector<ArrayRef<support::ulittle32_t>> &Streams) {
StreamMap = Streams;
}
void PDBFile::setDirectoryBlocks(ArrayRef<support::ulittle32_t> Directory) {
DirectoryBlocks = Directory;
}
Error PDBFile::generateSimpleStreamMap() {
if (StreamSizes.empty())
return Error::success();
static std::vector<std::vector<support::ulittle32_t>> StaticMap;
StreamMap.clear();
StaticMap.clear();
// Figure out how many blocks are needed for all streams, and set the first
// used block to the highest block so that we can write the rest of the
// blocks contiguously.
uint32_t TotalFileBlocks = getBlockCount();
std::vector<support::ulittle32_t> ReservedBlocks;
ReservedBlocks.push_back(support::ulittle32_t(0));
ReservedBlocks.push_back(SB->BlockMapAddr);
ReservedBlocks.insert(ReservedBlocks.end(), DirectoryBlocks.begin(),
DirectoryBlocks.end());
uint32_t BlocksNeeded = 0;
for (auto Size : StreamSizes)
BlocksNeeded += bytesToBlocks(Size, getBlockSize());
support::ulittle32_t NextBlock(TotalFileBlocks - BlocksNeeded -
ReservedBlocks.size());
StaticMap.resize(StreamSizes.size());
for (uint32_t S = 0; S < StreamSizes.size(); ++S) {
uint32_t Size = StreamSizes[S];
uint32_t NumBlocks = bytesToBlocks(Size, getBlockSize());
auto &ThisStream = StaticMap[S];
for (uint32_t I = 0; I < NumBlocks;) {
NextBlock += 1;
if (std::find(ReservedBlocks.begin(), ReservedBlocks.end(), NextBlock) !=
ReservedBlocks.end())
continue;
++I;
assert(NextBlock < getBlockCount());
ThisStream.push_back(NextBlock);
}
StreamMap.push_back(ThisStream);
}
return Error::success();
}
Error PDBFile::commit() {
StreamWriter Writer(*Buffer);
if (auto EC = Writer.writeObject(*SB))
return EC;
Writer.setOffset(getBlockMapOffset());
if (auto EC = Writer.writeArray(DirectoryBlocks))
return EC;
auto DS = MappedBlockStream::createDirectoryStream(*this);
if (!DS)
return DS.takeError();
auto DirStream = std::move(*DS);
StreamWriter DW(*DirStream);
if (auto EC = DW.writeInteger(this->getNumStreams()))
return EC;
if (auto EC = DW.writeArray(StreamSizes))
return EC;
for (const auto &Blocks : StreamMap) {
if (auto EC = DW.writeArray(Blocks))
return EC;
}
if (Info) {
if (auto EC = Info->commit())
return EC;
}
if (Dbi) {
if (auto EC = Dbi->commit())
return EC;
}
if (Symbols) {
if (auto EC = Symbols->commit())
return EC;
}
if (Publics) {
if (auto EC = Publics->commit())
return EC;
}
if (Tpi) {
if (auto EC = Tpi->commit())
return EC;
}
if (Ipi) {
if (auto EC = Ipi->commit())
return EC;
}
return Buffer->commit();
}
|