1 //===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/DebugInfo/MSF/MSFCommon.h"
13 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
14 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
15 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
16 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
17 #include "llvm/DebugInfo/PDB/Native/InjectedSourceStream.h"
18 #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
19 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
20 #include "llvm/DebugInfo/PDB/Native/RawError.h"
21 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
22 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
23 #include "llvm/Support/BinaryStream.h"
24 #include "llvm/Support/BinaryStreamArray.h"
25 #include "llvm/Support/BinaryStreamReader.h"
26 #include "llvm/Support/Endian.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/Path.h"
34 using namespace llvm::codeview;
35 using namespace llvm::msf;
36 using namespace llvm::pdb;
39 typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
40 } // end anonymous namespace
42 PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer,
43 BumpPtrAllocator &Allocator)
44 : FilePath(std::string(Path)), Allocator(Allocator),
45 Buffer(std::move(PdbFileBuffer)) {}
47 PDBFile::~PDBFile() = default;
49 StringRef PDBFile::getFilePath() const { return FilePath; }
51 StringRef PDBFile::getFileDirectory() const {
52 return sys::path::parent_path(FilePath);
55 uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
57 uint32_t PDBFile::getFreeBlockMapBlock() const {
58 return ContainerLayout.SB->FreeBlockMapBlock;
61 uint32_t PDBFile::getBlockCount() const {
62 return ContainerLayout.SB->NumBlocks;
65 uint32_t PDBFile::getNumDirectoryBytes() const {
66 return ContainerLayout.SB->NumDirectoryBytes;
69 uint32_t PDBFile::getBlockMapIndex() const {
70 return ContainerLayout.SB->BlockMapAddr;
73 uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
75 uint32_t PDBFile::getNumDirectoryBlocks() const {
76 return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes,
77 ContainerLayout.SB->BlockSize);
80 uint64_t PDBFile::getBlockMapOffset() const {
81 return (uint64_t)ContainerLayout.SB->BlockMapAddr *
82 ContainerLayout.SB->BlockSize;
85 uint32_t PDBFile::getNumStreams() const {
86 return ContainerLayout.StreamSizes.size();
89 uint32_t PDBFile::getMaxStreamSize() const {
90 return *std::max_element(ContainerLayout.StreamSizes.begin(),
91 ContainerLayout.StreamSizes.end());
94 uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
95 return ContainerLayout.StreamSizes[StreamIndex];
98 ArrayRef<support::ulittle32_t>
99 PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
100 return ContainerLayout.StreamMap[StreamIndex];
103 uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
105 Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
106 uint32_t NumBytes) const {
107 uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
109 ArrayRef<uint8_t> Result;
110 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
111 return std::move(EC);
115 Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
116 ArrayRef<uint8_t> Data) const {
117 return make_error<RawError>(raw_error_code::not_writable,
118 "PDBFile is immutable");
121 Error PDBFile::parseFileHeaders() {
122 BinaryStreamReader Reader(*Buffer);
125 const msf::SuperBlock *SB = nullptr;
126 if (auto EC = Reader.readObject(SB)) {
127 consumeError(std::move(EC));
128 return make_error<RawError>(raw_error_code::corrupt_file,
129 "MSF superblock is missing");
132 if (auto EC = msf::validateSuperBlock(*SB))
135 if (Buffer->getLength() % SB->BlockSize != 0)
136 return make_error<RawError>(raw_error_code::corrupt_file,
137 "File size is not a multiple of block size");
138 ContainerLayout.SB = SB;
140 // Initialize Free Page Map.
141 ContainerLayout.FreePageMap.resize(SB->NumBlocks);
142 // The Fpm exists either at block 1 or block 2 of the MSF. However, this
143 // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
144 // thusly an equal number of total blocks in the file. For a block size
145 // of 4KiB (very common), this would yield 32KiB total blocks in file, for a
146 // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
147 // the Fpm is split across the file at `getBlockSize()` intervals. As a
148 // result, every block whose index is of the form |{1,2} + getBlockSize() * k|
149 // for any non-negative integer k is an Fpm block. In theory, we only really
150 // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
151 // current versions of the MSF format already expect the Fpm to be arranged
152 // at getBlockSize() intervals, so we have to be compatible.
153 // See the function fpmPn() for more information:
154 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
156 MappedBlockStream::createFpmStream(ContainerLayout, *Buffer, Allocator);
157 BinaryStreamReader FpmReader(*FpmStream);
158 ArrayRef<uint8_t> FpmBytes;
159 if (auto EC = FpmReader.readBytes(FpmBytes, FpmReader.bytesRemaining()))
161 uint32_t BlocksRemaining = getBlockCount();
163 for (auto Byte : FpmBytes) {
164 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
165 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
167 ContainerLayout.FreePageMap[BI] = true;
173 Reader.setOffset(getBlockMapOffset());
174 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
175 getNumDirectoryBlocks()))
178 return Error::success();
181 Error PDBFile::parseStreamData() {
182 assert(ContainerLayout.SB);
184 return Error::success();
186 uint32_t NumStreams = 0;
188 // Normally you can't use a MappedBlockStream without having fully parsed the
189 // PDB file, because it accesses the directory and various other things, which
190 // is exactly what we are attempting to parse. By specifying a custom
191 // subclass of IPDBStreamData which only accesses the fields that have already
192 // been parsed, we can avoid this and reuse MappedBlockStream.
193 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer,
195 BinaryStreamReader Reader(*DS);
196 if (auto EC = Reader.readInteger(NumStreams))
199 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
201 for (uint32_t I = 0; I < NumStreams; ++I) {
202 uint32_t StreamSize = getStreamByteSize(I);
203 // FIXME: What does StreamSize ~0U mean?
204 uint64_t NumExpectedStreamBlocks =
205 StreamSize == UINT32_MAX
207 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
209 // For convenience, we store the block array contiguously. This is because
210 // if someone calls setStreamMap(), it is more convenient to be able to call
211 // it with an ArrayRef instead of setting up a StreamRef. Since the
212 // DirectoryStream is cached in the class and thus lives for the life of the
213 // class, we can be guaranteed that readArray() will return a stable
214 // reference, even if it has to allocate from its internal pool.
215 ArrayRef<support::ulittle32_t> Blocks;
216 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
218 for (uint32_t Block : Blocks) {
219 uint64_t BlockEndOffset =
220 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
221 if (BlockEndOffset > getFileSize())
222 return make_error<RawError>(raw_error_code::corrupt_file,
223 "Stream block map is corrupt.");
225 ContainerLayout.StreamMap.push_back(Blocks);
228 // We should have read exactly SB->NumDirectoryBytes bytes.
229 assert(Reader.bytesRemaining() == 0);
230 DirectoryStream = std::move(DS);
231 return Error::success();
234 ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
235 return ContainerLayout.DirectoryBlocks;
238 std::unique_ptr<MappedBlockStream>
239 PDBFile::createIndexedStream(uint16_t SN) const {
240 if (SN == kInvalidStreamIndex)
242 return MappedBlockStream::createIndexedStream(ContainerLayout, *Buffer, SN,
246 MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
247 MSFStreamLayout Result;
248 auto Blocks = getStreamBlockList(StreamIdx);
249 Result.Blocks.assign(Blocks.begin(), Blocks.end());
250 Result.Length = getStreamByteSize(StreamIdx);
254 msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
255 return msf::getFpmStreamLayout(ContainerLayout);
258 Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
260 auto DbiS = getPDBDbiStream();
262 return DbiS.takeError();
265 safelyCreateIndexedStream(DbiS->getGlobalSymbolStreamIndex());
267 return GlobalS.takeError();
268 auto TempGlobals = std::make_unique<GlobalsStream>(std::move(*GlobalS));
269 if (auto EC = TempGlobals->reload())
270 return std::move(EC);
271 Globals = std::move(TempGlobals);
276 Expected<InfoStream &> PDBFile::getPDBInfoStream() {
278 auto InfoS = safelyCreateIndexedStream(StreamPDB);
280 return InfoS.takeError();
281 auto TempInfo = std::make_unique<InfoStream>(std::move(*InfoS));
282 if (auto EC = TempInfo->reload())
283 return std::move(EC);
284 Info = std::move(TempInfo);
289 Expected<DbiStream &> PDBFile::getPDBDbiStream() {
291 auto DbiS = safelyCreateIndexedStream(StreamDBI);
293 return DbiS.takeError();
294 auto TempDbi = std::make_unique<DbiStream>(std::move(*DbiS));
295 if (auto EC = TempDbi->reload(this))
296 return std::move(EC);
297 Dbi = std::move(TempDbi);
302 Expected<TpiStream &> PDBFile::getPDBTpiStream() {
304 auto TpiS = safelyCreateIndexedStream(StreamTPI);
306 return TpiS.takeError();
307 auto TempTpi = std::make_unique<TpiStream>(*this, std::move(*TpiS));
308 if (auto EC = TempTpi->reload())
309 return std::move(EC);
310 Tpi = std::move(TempTpi);
315 Expected<TpiStream &> PDBFile::getPDBIpiStream() {
317 if (!hasPDBIpiStream())
318 return make_error<RawError>(raw_error_code::no_stream);
320 auto IpiS = safelyCreateIndexedStream(StreamIPI);
322 return IpiS.takeError();
323 auto TempIpi = std::make_unique<TpiStream>(*this, std::move(*IpiS));
324 if (auto EC = TempIpi->reload())
325 return std::move(EC);
326 Ipi = std::move(TempIpi);
331 Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
333 auto DbiS = getPDBDbiStream();
335 return DbiS.takeError();
338 safelyCreateIndexedStream(DbiS->getPublicSymbolStreamIndex());
340 return PublicS.takeError();
341 auto TempPublics = std::make_unique<PublicsStream>(std::move(*PublicS));
342 if (auto EC = TempPublics->reload())
343 return std::move(EC);
344 Publics = std::move(TempPublics);
349 Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
351 auto DbiS = getPDBDbiStream();
353 return DbiS.takeError();
355 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
356 auto SymbolS = safelyCreateIndexedStream(SymbolStreamNum);
358 return SymbolS.takeError();
360 auto TempSymbols = std::make_unique<SymbolStream>(std::move(*SymbolS));
361 if (auto EC = TempSymbols->reload())
362 return std::move(EC);
363 Symbols = std::move(TempSymbols);
368 Expected<PDBStringTable &> PDBFile::getStringTable() {
370 auto NS = safelyCreateNamedStream("/names");
372 return NS.takeError();
374 auto N = std::make_unique<PDBStringTable>();
375 BinaryStreamReader Reader(**NS);
376 if (auto EC = N->reload(Reader))
377 return std::move(EC);
378 assert(Reader.bytesRemaining() == 0);
379 StringTableStream = std::move(*NS);
380 Strings = std::move(N);
385 Expected<InjectedSourceStream &> PDBFile::getInjectedSourceStream() {
386 if (!InjectedSources) {
387 auto IJS = safelyCreateNamedStream("/src/headerblock");
389 return IJS.takeError();
391 auto Strings = getStringTable();
393 return Strings.takeError();
395 auto IJ = std::make_unique<InjectedSourceStream>(std::move(*IJS));
396 if (auto EC = IJ->reload(*Strings))
397 return std::move(EC);
398 InjectedSources = std::move(IJ);
400 return *InjectedSources;
403 uint32_t PDBFile::getPointerSize() {
404 auto DbiS = getPDBDbiStream();
407 PDB_Machine Machine = DbiS->getMachineType();
408 if (Machine == PDB_Machine::Amd64)
413 bool PDBFile::hasPDBDbiStream() const {
414 return StreamDBI < getNumStreams() && getStreamByteSize(StreamDBI) > 0;
417 bool PDBFile::hasPDBGlobalsStream() {
418 auto DbiS = getPDBDbiStream();
420 consumeError(DbiS.takeError());
424 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
427 bool PDBFile::hasPDBInfoStream() const { return StreamPDB < getNumStreams(); }
429 bool PDBFile::hasPDBIpiStream() const {
430 if (!hasPDBInfoStream())
433 if (StreamIPI >= getNumStreams())
436 auto &InfoStream = cantFail(const_cast<PDBFile *>(this)->getPDBInfoStream());
437 return InfoStream.containsIdStream();
440 bool PDBFile::hasPDBPublicsStream() {
441 auto DbiS = getPDBDbiStream();
443 consumeError(DbiS.takeError());
446 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
449 bool PDBFile::hasPDBSymbolStream() {
450 auto DbiS = getPDBDbiStream();
453 return DbiS->getSymRecordStreamIndex() < getNumStreams();
456 bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
458 bool PDBFile::hasPDBStringTable() {
459 auto IS = getPDBInfoStream();
462 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names");
464 consumeError(ExpectedNSI.takeError());
467 assert(*ExpectedNSI < getNumStreams());
471 bool PDBFile::hasPDBInjectedSourceStream() {
472 auto IS = getPDBInfoStream();
475 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/src/headerblock");
477 consumeError(ExpectedNSI.takeError());
480 assert(*ExpectedNSI < getNumStreams());
484 /// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
485 /// stream with that index actually exists. If it does not, the return value
486 /// will have an MSFError with code msf_error_code::no_stream. Else, the return
487 /// value will contain the stream returned by createIndexedStream().
488 Expected<std::unique_ptr<MappedBlockStream>>
489 PDBFile::safelyCreateIndexedStream(uint32_t StreamIndex) const {
490 if (StreamIndex >= getNumStreams())
491 // This rejects kInvalidStreamIndex with an error as well.
492 return make_error<RawError>(raw_error_code::no_stream);
493 return createIndexedStream(StreamIndex);
496 Expected<std::unique_ptr<MappedBlockStream>>
497 PDBFile::safelyCreateNamedStream(StringRef Name) {
498 auto IS = getPDBInfoStream();
500 return IS.takeError();
502 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex(Name);
504 return ExpectedNSI.takeError();
505 uint32_t NameStreamIndex = *ExpectedNSI;
507 return safelyCreateIndexedStream(NameStreamIndex);