1//===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/DebugInfo/MSF/MSFCommon.h"
12#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
13#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
14#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
15#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
16#include "llvm/DebugInfo/PDB/Native/InjectedSourceStream.h"
17#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
18#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
19#include "llvm/DebugInfo/PDB/Native/RawError.h"
20#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
21#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
22#include "llvm/Support/BinaryStream.h"
23#include "llvm/Support/BinaryStreamReader.h"
24#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Support/Path.h"
27#include <algorithm>
28#include <cassert>
29#include <cstdint>
30
31using namespace llvm;
32using namespace llvm::codeview;
33using namespace llvm::msf;
34using namespace llvm::pdb;
35
36PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer,
37 BumpPtrAllocator &Allocator)
38 : FilePath(std::string(Path)), Allocator(Allocator),
39 Buffer(std::move(PdbFileBuffer)) {}
40
41PDBFile::~PDBFile() = default;
42
43StringRef PDBFile::getFilePath() const { return FilePath; }
44
45StringRef PDBFile::getFileDirectory() const {
46 return sys::path::parent_path(path: FilePath);
47}
48
49uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
50
51uint32_t PDBFile::getFreeBlockMapBlock() const {
52 return ContainerLayout.SB->FreeBlockMapBlock;
53}
54
55uint32_t PDBFile::getBlockCount() const {
56 return ContainerLayout.SB->NumBlocks;
57}
58
59uint32_t PDBFile::getNumDirectoryBytes() const {
60 return ContainerLayout.SB->NumDirectoryBytes;
61}
62
63uint32_t PDBFile::getBlockMapIndex() const {
64 return ContainerLayout.SB->BlockMapAddr;
65}
66
67uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
68
69uint32_t PDBFile::getNumDirectoryBlocks() const {
70 return msf::bytesToBlocks(NumBytes: ContainerLayout.SB->NumDirectoryBytes,
71 BlockSize: ContainerLayout.SB->BlockSize);
72}
73
74uint64_t PDBFile::getBlockMapOffset() const {
75 return (uint64_t)ContainerLayout.SB->BlockMapAddr *
76 ContainerLayout.SB->BlockSize;
77}
78
79uint32_t PDBFile::getNumStreams() const {
80 return ContainerLayout.StreamSizes.size();
81}
82
83uint32_t PDBFile::getMaxStreamSize() const {
84 return *llvm::max_element(Range: ContainerLayout.StreamSizes);
85}
86
87uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
88 return ContainerLayout.StreamSizes[StreamIndex];
89}
90
91ArrayRef<support::ulittle32_t>
92PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
93 return ContainerLayout.StreamMap[StreamIndex];
94}
95
96uint64_t PDBFile::getFileSize() const { return Buffer->getLength(); }
97
98Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
99 uint32_t NumBytes) const {
100 uint64_t StreamBlockOffset = msf::blockToOffset(BlockNumber: BlockIndex, BlockSize: getBlockSize());
101
102 ArrayRef<uint8_t> Result;
103 if (auto EC = Buffer->readBytes(Offset: StreamBlockOffset, Size: NumBytes, Buffer&: Result))
104 return std::move(EC);
105 return Result;
106}
107
108Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
109 ArrayRef<uint8_t> Data) const {
110 return make_error<RawError>(Args: raw_error_code::not_writable,
111 Args: "PDBFile is immutable");
112}
113
114Error PDBFile::parseFileHeaders() {
115 BinaryStreamReader Reader(*Buffer);
116
117 // Initialize SB.
118 const msf::SuperBlock *SB = nullptr;
119 if (auto EC = Reader.readObject(Dest&: SB)) {
120 consumeError(Err: std::move(EC));
121 return make_error<RawError>(Args: raw_error_code::corrupt_file,
122 Args: "MSF superblock is missing");
123 }
124
125 if (auto EC = msf::validateSuperBlock(SB: *SB))
126 return EC;
127
128 if (Buffer->getLength() % SB->BlockSize != 0)
129 return make_error<RawError>(Args: raw_error_code::corrupt_file,
130 Args: "File size is not a multiple of block size");
131 ContainerLayout.SB = SB;
132
133 // Initialize Free Page Map.
134 ContainerLayout.FreePageMap.resize(N: SB->NumBlocks);
135 // The Fpm exists either at block 1 or block 2 of the MSF. However, this
136 // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
137 // thusly an equal number of total blocks in the file. For a block size
138 // of 4KiB (very common), this would yield 32KiB total blocks in file, for a
139 // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
140 // the Fpm is split across the file at `getBlockSize()` intervals. As a
141 // result, every block whose index is of the form |{1,2} + getBlockSize() * k|
142 // for any non-negative integer k is an Fpm block. In theory, we only really
143 // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
144 // current versions of the MSF format already expect the Fpm to be arranged
145 // at getBlockSize() intervals, so we have to be compatible.
146 // See the function fpmPn() for more information:
147 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
148 auto FpmStream =
149 MappedBlockStream::createFpmStream(Layout: ContainerLayout, MsfData: *Buffer, Allocator);
150 BinaryStreamReader FpmReader(*FpmStream);
151 ArrayRef<uint8_t> FpmBytes;
152 if (auto EC = FpmReader.readBytes(Buffer&: FpmBytes, Size: FpmReader.bytesRemaining()))
153 return EC;
154 uint32_t BlocksRemaining = getBlockCount();
155 uint32_t BI = 0;
156 for (auto Byte : FpmBytes) {
157 uint32_t BlocksThisByte = std::min(a: BlocksRemaining, b: 8U);
158 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
159 if (Byte & (1 << I))
160 ContainerLayout.FreePageMap[BI] = true;
161 --BlocksRemaining;
162 ++BI;
163 }
164 }
165
166 Reader.setOffset(getBlockMapOffset());
167 if (auto EC = Reader.readArray(Array&: ContainerLayout.DirectoryBlocks,
168 NumElements: getNumDirectoryBlocks()))
169 return EC;
170
171 return Error::success();
172}
173
174Error PDBFile::parseStreamData() {
175 assert(ContainerLayout.SB);
176 if (DirectoryStream)
177 return Error::success();
178
179 uint32_t NumStreams = 0;
180
181 // Normally you can't use a MappedBlockStream without having fully parsed the
182 // PDB file, because it accesses the directory and various other things, which
183 // is exactly what we are attempting to parse. By specifying a custom
184 // subclass of IPDBStreamData which only accesses the fields that have already
185 // been parsed, we can avoid this and reuse MappedBlockStream.
186 auto DS = MappedBlockStream::createDirectoryStream(Layout: ContainerLayout, MsfData: *Buffer,
187 Allocator);
188 BinaryStreamReader Reader(*DS);
189 if (auto EC = Reader.readInteger(Dest&: NumStreams))
190 return EC;
191
192 if (auto EC = Reader.readArray(Array&: ContainerLayout.StreamSizes, NumElements: NumStreams))
193 return EC;
194 for (uint32_t I = 0; I < NumStreams; ++I) {
195 uint32_t StreamSize = getStreamByteSize(StreamIndex: I);
196 // FIXME: What does StreamSize ~0U mean?
197 uint64_t NumExpectedStreamBlocks =
198 StreamSize == UINT32_MAX
199 ? 0
200 : msf::bytesToBlocks(NumBytes: StreamSize, BlockSize: ContainerLayout.SB->BlockSize);
201
202 // For convenience, we store the block array contiguously. This is because
203 // if someone calls setStreamMap(), it is more convenient to be able to call
204 // it with an ArrayRef instead of setting up a StreamRef. Since the
205 // DirectoryStream is cached in the class and thus lives for the life of the
206 // class, we can be guaranteed that readArray() will return a stable
207 // reference, even if it has to allocate from its internal pool.
208 ArrayRef<support::ulittle32_t> Blocks;
209 if (auto EC = Reader.readArray(Array&: Blocks, NumElements: NumExpectedStreamBlocks))
210 return EC;
211 for (uint32_t Block : Blocks) {
212 uint64_t BlockEndOffset =
213 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
214 if (BlockEndOffset > getFileSize())
215 return make_error<RawError>(Args: raw_error_code::corrupt_file,
216 Args: "Stream block map is corrupt.");
217 }
218 ContainerLayout.StreamMap.push_back(x: Blocks);
219 }
220
221 // We should have read exactly SB->NumDirectoryBytes bytes.
222 assert(Reader.bytesRemaining() == 0);
223 DirectoryStream = std::move(DS);
224 return Error::success();
225}
226
227ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
228 return ContainerLayout.DirectoryBlocks;
229}
230
231std::unique_ptr<MappedBlockStream>
232PDBFile::createIndexedStream(uint16_t SN) const {
233 if (SN == kInvalidStreamIndex)
234 return nullptr;
235 return MappedBlockStream::createIndexedStream(Layout: ContainerLayout, MsfData: *Buffer, StreamIndex: SN,
236 Allocator);
237}
238
239MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
240 MSFStreamLayout Result;
241 auto Blocks = getStreamBlockList(StreamIndex: StreamIdx);
242 Result.Blocks.assign(first: Blocks.begin(), last: Blocks.end());
243 Result.Length = getStreamByteSize(StreamIndex: StreamIdx);
244 return Result;
245}
246
247msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
248 return msf::getFpmStreamLayout(Msf: ContainerLayout);
249}
250
251Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
252 if (!Globals) {
253 auto DbiS = getPDBDbiStream();
254 if (!DbiS)
255 return DbiS.takeError();
256
257 auto GlobalS =
258 safelyCreateIndexedStream(StreamIndex: DbiS->getGlobalSymbolStreamIndex());
259 if (!GlobalS)
260 return GlobalS.takeError();
261 auto TempGlobals = std::make_unique<GlobalsStream>(args: std::move(*GlobalS));
262 if (auto EC = TempGlobals->reload())
263 return std::move(EC);
264 Globals = std::move(TempGlobals);
265 }
266 return *Globals;
267}
268
269Expected<InfoStream &> PDBFile::getPDBInfoStream() {
270 if (!Info) {
271 auto InfoS = safelyCreateIndexedStream(StreamIndex: StreamPDB);
272 if (!InfoS)
273 return InfoS.takeError();
274 auto TempInfo = std::make_unique<InfoStream>(args: std::move(*InfoS));
275 if (auto EC = TempInfo->reload())
276 return std::move(EC);
277 Info = std::move(TempInfo);
278 }
279 return *Info;
280}
281
282Expected<DbiStream &> PDBFile::getPDBDbiStream() {
283 if (!Dbi) {
284 auto DbiS = safelyCreateIndexedStream(StreamIndex: StreamDBI);
285 if (!DbiS)
286 return DbiS.takeError();
287 auto TempDbi = std::make_unique<DbiStream>(args: std::move(*DbiS));
288 if (auto EC = TempDbi->reload(Pdb: this))
289 return std::move(EC);
290 Dbi = std::move(TempDbi);
291 }
292 return *Dbi;
293}
294
295Expected<TpiStream &> PDBFile::getPDBTpiStream() {
296 if (!Tpi) {
297 auto TpiS = safelyCreateIndexedStream(StreamIndex: StreamTPI);
298 if (!TpiS)
299 return TpiS.takeError();
300 auto TempTpi = std::make_unique<TpiStream>(args&: *this, args: std::move(*TpiS));
301 if (auto EC = TempTpi->reload())
302 return std::move(EC);
303 Tpi = std::move(TempTpi);
304 }
305 return *Tpi;
306}
307
308Expected<TpiStream &> PDBFile::getPDBIpiStream() {
309 if (!Ipi) {
310 if (!hasPDBIpiStream())
311 return make_error<RawError>(Args: raw_error_code::no_stream);
312
313 auto IpiS = safelyCreateIndexedStream(StreamIndex: StreamIPI);
314 if (!IpiS)
315 return IpiS.takeError();
316 auto TempIpi = std::make_unique<TpiStream>(args&: *this, args: std::move(*IpiS));
317 if (auto EC = TempIpi->reload())
318 return std::move(EC);
319 Ipi = std::move(TempIpi);
320 }
321 return *Ipi;
322}
323
324Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
325 if (!Publics) {
326 auto DbiS = getPDBDbiStream();
327 if (!DbiS)
328 return DbiS.takeError();
329
330 auto PublicS =
331 safelyCreateIndexedStream(StreamIndex: DbiS->getPublicSymbolStreamIndex());
332 if (!PublicS)
333 return PublicS.takeError();
334 auto TempPublics = std::make_unique<PublicsStream>(args: std::move(*PublicS));
335 if (auto EC = TempPublics->reload())
336 return std::move(EC);
337 Publics = std::move(TempPublics);
338 }
339 return *Publics;
340}
341
342Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
343 if (!Symbols) {
344 auto DbiS = getPDBDbiStream();
345 if (!DbiS)
346 return DbiS.takeError();
347
348 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
349 auto SymbolS = safelyCreateIndexedStream(StreamIndex: SymbolStreamNum);
350 if (!SymbolS)
351 return SymbolS.takeError();
352
353 auto TempSymbols = std::make_unique<SymbolStream>(args: std::move(*SymbolS));
354 if (auto EC = TempSymbols->reload())
355 return std::move(EC);
356 Symbols = std::move(TempSymbols);
357 }
358 return *Symbols;
359}
360
361Expected<PDBStringTable &> PDBFile::getStringTable() {
362 if (!Strings) {
363 auto NS = safelyCreateNamedStream(Name: "/names");
364 if (!NS)
365 return NS.takeError();
366
367 auto N = std::make_unique<PDBStringTable>();
368 BinaryStreamReader Reader(**NS);
369 if (auto EC = N->reload(Reader))
370 return std::move(EC);
371 assert(Reader.bytesRemaining() == 0);
372 StringTableStream = std::move(*NS);
373 Strings = std::move(N);
374 }
375 return *Strings;
376}
377
378Expected<InjectedSourceStream &> PDBFile::getInjectedSourceStream() {
379 if (!InjectedSources) {
380 auto IJS = safelyCreateNamedStream(Name: "/src/headerblock");
381 if (!IJS)
382 return IJS.takeError();
383
384 auto Strings = getStringTable();
385 if (!Strings)
386 return Strings.takeError();
387
388 auto IJ = std::make_unique<InjectedSourceStream>(args: std::move(*IJS));
389 if (auto EC = IJ->reload(Strings: *Strings))
390 return std::move(EC);
391 InjectedSources = std::move(IJ);
392 }
393 return *InjectedSources;
394}
395
396llvm::Expected<object::DXContainer &> PDBFile::getDXContainerStream() {
397 if (!Dxc) {
398 auto MBS = safelyCreateIndexedStream(StreamIndex: StreamDXContainer);
399 if (!MBS)
400 return MBS.takeError();
401 auto StreamSize = getStreamByteSize(StreamIndex: StreamDXContainer);
402 ArrayRef<uint8_t> StreamData;
403 auto Error = MBS->get()->readBytes(Offset: 0, Size: StreamSize, Buffer&: StreamData);
404 if (Error)
405 return Error;
406
407 StringRef Ref(reinterpret_cast<const char *>(StreamData.data()),
408 StreamSize);
409 MemoryBufferRef MemBuf(Ref, "DXContainerStream");
410 auto DXC = object::DXContainer::create(Object: MemBuf);
411 if (!DXC)
412 return DXC.takeError();
413 Dxc = std::make_unique<object::DXContainer>(args: std::move(*DXC));
414 }
415 return *Dxc;
416}
417
418uint32_t PDBFile::getPointerSize() {
419 auto DbiS = getPDBDbiStream();
420 if (!DbiS)
421 return 0;
422 PDB_Machine Machine = DbiS->getMachineType();
423 if (Machine == PDB_Machine::Amd64)
424 return 8;
425 return 4;
426}
427
428bool PDBFile::hasPDBDbiStream() const {
429 return StreamDBI < getNumStreams() && getStreamByteSize(StreamIndex: StreamDBI) > 0;
430}
431
432bool PDBFile::hasPDBGlobalsStream() {
433 auto DbiS = getPDBDbiStream();
434 if (!DbiS) {
435 consumeError(Err: DbiS.takeError());
436 return false;
437 }
438
439 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
440}
441
442bool PDBFile::hasPDBInfoStream() const { return StreamPDB < getNumStreams(); }
443
444bool PDBFile::hasPDBIpiStream() const {
445 if (!hasPDBInfoStream())
446 return false;
447
448 if (StreamIPI >= getNumStreams())
449 return false;
450
451 auto &InfoStream = cantFail(ValOrErr: const_cast<PDBFile *>(this)->getPDBInfoStream());
452 return InfoStream.containsIdStream();
453}
454
455bool PDBFile::hasPDBPublicsStream() {
456 auto DbiS = getPDBDbiStream();
457 if (!DbiS) {
458 consumeError(Err: DbiS.takeError());
459 return false;
460 }
461 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
462}
463
464bool PDBFile::hasPDBSymbolStream() {
465 auto DbiS = getPDBDbiStream();
466 if (!DbiS)
467 return false;
468 return DbiS->getSymRecordStreamIndex() < getNumStreams();
469}
470
471bool PDBFile::hasPDBTpiStream() const {
472 return StreamTPI < getNumStreams() && getStreamByteSize(StreamIndex: StreamTPI) != 0;
473}
474
475bool PDBFile::hasPDBStringTable() {
476 auto IS = getPDBInfoStream();
477 if (!IS)
478 return false;
479 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex(Name: "/names");
480 if (!ExpectedNSI) {
481 consumeError(Err: ExpectedNSI.takeError());
482 return false;
483 }
484 assert(*ExpectedNSI < getNumStreams());
485 return true;
486}
487
488bool PDBFile::hasPDBInjectedSourceStream() {
489 auto IS = getPDBInfoStream();
490 if (!IS)
491 return false;
492 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex(Name: "/src/headerblock");
493 if (!ExpectedNSI) {
494 consumeError(Err: ExpectedNSI.takeError());
495 return false;
496 }
497 assert(*ExpectedNSI < getNumStreams());
498 return true;
499}
500
501/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
502/// stream with that index actually exists. If it does not, the return value
503/// will have an MSFError with code msf_error_code::no_stream. Else, the return
504/// value will contain the stream returned by createIndexedStream().
505Expected<std::unique_ptr<MappedBlockStream>>
506PDBFile::safelyCreateIndexedStream(uint32_t StreamIndex) const {
507 if (StreamIndex >= getNumStreams())
508 // This rejects kInvalidStreamIndex with an error as well.
509 return make_error<RawError>(Args: raw_error_code::no_stream);
510 return createIndexedStream(SN: StreamIndex);
511}
512
513Expected<std::unique_ptr<MappedBlockStream>>
514PDBFile::safelyCreateNamedStream(StringRef Name) {
515 auto IS = getPDBInfoStream();
516 if (!IS)
517 return IS.takeError();
518
519 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex(Name);
520 if (!ExpectedNSI)
521 return ExpectedNSI.takeError();
522 uint32_t NameStreamIndex = *ExpectedNSI;
523
524 return safelyCreateIndexedStream(StreamIndex: NameStreamIndex);
525}
526