1 //===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/DebugInfo/MSF/MSFCommon.h"
14 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
15 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
16 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
17 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18 #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
19 #include "llvm/DebugInfo/PDB/Native/RawError.h"
20 #include "llvm/DebugInfo/PDB/Native/StringTable.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(Path), Allocator(Allocator), Buffer(std::move(PdbFileBuffer)) {}
46 PDBFile::~PDBFile() = default;
48 StringRef PDBFile::getFilePath() const { return FilePath; }
50 StringRef PDBFile::getFileDirectory() const {
51 return sys::path::parent_path(FilePath);
54 uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
56 uint32_t PDBFile::getFreeBlockMapBlock() const {
57 return ContainerLayout.SB->FreeBlockMapBlock;
60 uint32_t PDBFile::getBlockCount() const {
61 return ContainerLayout.SB->NumBlocks;
64 uint32_t PDBFile::getNumDirectoryBytes() const {
65 return ContainerLayout.SB->NumDirectoryBytes;
68 uint32_t PDBFile::getBlockMapIndex() const {
69 return ContainerLayout.SB->BlockMapAddr;
72 uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
74 uint32_t PDBFile::getNumDirectoryBlocks() const {
75 return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes,
76 ContainerLayout.SB->BlockSize);
79 uint64_t PDBFile::getBlockMapOffset() const {
80 return (uint64_t)ContainerLayout.SB->BlockMapAddr *
81 ContainerLayout.SB->BlockSize;
84 uint32_t PDBFile::getNumStreams() const {
85 return ContainerLayout.StreamSizes.size();
88 uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
89 return ContainerLayout.StreamSizes[StreamIndex];
92 ArrayRef<support::ulittle32_t>
93 PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
94 return ContainerLayout.StreamMap[StreamIndex];
97 uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
99 Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
100 uint32_t NumBytes) const {
101 uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
103 ArrayRef<uint8_t> Result;
104 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
105 return std::move(EC);
109 Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
110 ArrayRef<uint8_t> Data) const {
111 return make_error<RawError>(raw_error_code::not_writable,
112 "PDBFile is immutable");
115 Error PDBFile::parseFileHeaders() {
116 BinaryStreamReader Reader(*Buffer);
119 const msf::SuperBlock *SB = nullptr;
120 if (auto EC = Reader.readObject(SB)) {
121 consumeError(std::move(EC));
122 return make_error<RawError>(raw_error_code::corrupt_file,
123 "Does not contain superblock");
126 if (auto EC = msf::validateSuperBlock(*SB))
129 if (Buffer->getLength() % SB->BlockSize != 0)
130 return make_error<RawError>(raw_error_code::corrupt_file,
131 "File size is not a multiple of block size");
132 ContainerLayout.SB = SB;
134 // Initialize Free Page Map.
135 ContainerLayout.FreePageMap.resize(SB->NumBlocks);
136 // The Fpm exists either at block 1 or block 2 of the MSF. However, this
137 // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
138 // thusly an equal number of total blocks in the file. For a block size
139 // of 4KiB (very common), this would yield 32KiB total blocks in file, for a
140 // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
141 // the Fpm is split across the file at `getBlockSize()` intervals. As a
142 // result, every block whose index is of the form |{1,2} + getBlockSize() * k|
143 // for any non-negative integer k is an Fpm block. In theory, we only really
144 // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
145 // current versions of the MSF format already expect the Fpm to be arranged
146 // at getBlockSize() intervals, so we have to be compatible.
147 // See the function fpmPn() for more information:
148 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
149 auto FpmStream = MappedBlockStream::createFpmStream(ContainerLayout, *Buffer);
150 BinaryStreamReader FpmReader(*FpmStream);
151 ArrayRef<uint8_t> FpmBytes;
152 if (auto EC = FpmReader.readBytes(FpmBytes,
153 msf::getFullFpmByteSize(ContainerLayout)))
155 uint32_t BlocksRemaining = getBlockCount();
157 for (auto Byte : FpmBytes) {
158 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
159 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
161 ContainerLayout.FreePageMap[BI] = true;
167 Reader.setOffset(getBlockMapOffset());
168 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
169 getNumDirectoryBlocks()))
172 return Error::success();
175 Error PDBFile::parseStreamData() {
176 assert(ContainerLayout.SB);
178 return Error::success();
180 uint32_t NumStreams = 0;
182 // Normally you can't use a MappedBlockStream without having fully parsed the
183 // PDB file, because it accesses the directory and various other things, which
184 // is exactly what we are attempting to parse. By specifying a custom
185 // subclass of IPDBStreamData which only accesses the fields that have already
186 // been parsed, we can avoid this and reuse MappedBlockStream.
187 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer);
188 BinaryStreamReader Reader(*DS);
189 if (auto EC = Reader.readInteger(NumStreams))
192 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
194 for (uint32_t I = 0; I < NumStreams; ++I) {
195 uint32_t StreamSize = getStreamByteSize(I);
196 // FIXME: What does StreamSize ~0U mean?
197 uint64_t NumExpectedStreamBlocks =
198 StreamSize == UINT32_MAX
200 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
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(Blocks, NumExpectedStreamBlocks))
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>(raw_error_code::corrupt_file,
216 "Stream block map is corrupt.");
218 ContainerLayout.StreamMap.push_back(Blocks);
221 // We should have read exactly SB->NumDirectoryBytes bytes.
222 assert(Reader.bytesRemaining() == 0);
223 DirectoryStream = std::move(DS);
224 return Error::success();
227 ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
228 return ContainerLayout.DirectoryBlocks;
231 Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
233 auto DbiS = getPDBDbiStream();
235 return DbiS.takeError();
237 auto GlobalS = safelyCreateIndexedStream(
238 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
240 return GlobalS.takeError();
241 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
242 if (auto EC = TempGlobals->reload())
243 return std::move(EC);
244 Globals = std::move(TempGlobals);
249 Expected<InfoStream &> PDBFile::getPDBInfoStream() {
251 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
253 return InfoS.takeError();
254 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
255 if (auto EC = TempInfo->reload())
256 return std::move(EC);
257 Info = std::move(TempInfo);
262 Expected<DbiStream &> PDBFile::getPDBDbiStream() {
264 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
266 return DbiS.takeError();
267 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
268 if (auto EC = TempDbi->reload())
269 return std::move(EC);
270 Dbi = std::move(TempDbi);
275 Expected<TpiStream &> PDBFile::getPDBTpiStream() {
277 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
279 return TpiS.takeError();
280 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
281 if (auto EC = TempTpi->reload())
282 return std::move(EC);
283 Tpi = std::move(TempTpi);
288 Expected<TpiStream &> PDBFile::getPDBIpiStream() {
290 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
292 return IpiS.takeError();
293 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
294 if (auto EC = TempIpi->reload())
295 return std::move(EC);
296 Ipi = std::move(TempIpi);
301 Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
303 auto DbiS = getPDBDbiStream();
305 return DbiS.takeError();
307 auto PublicS = safelyCreateIndexedStream(
308 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
310 return PublicS.takeError();
312 llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
313 if (auto EC = TempPublics->reload())
314 return std::move(EC);
315 Publics = std::move(TempPublics);
320 Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
322 auto DbiS = getPDBDbiStream();
324 return DbiS.takeError();
326 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
328 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
330 return SymbolS.takeError();
332 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
333 if (auto EC = TempSymbols->reload())
334 return std::move(EC);
335 Symbols = std::move(TempSymbols);
340 Expected<StringTable &> PDBFile::getStringTable() {
341 if (!Strings || !StringTableStream) {
342 auto IS = getPDBInfoStream();
344 return IS.takeError();
346 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
349 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
351 return NS.takeError();
353 BinaryStreamReader Reader(**NS);
354 auto N = llvm::make_unique<StringTable>();
355 if (auto EC = N->load(Reader))
356 return std::move(EC);
357 Strings = std::move(N);
358 StringTableStream = std::move(*NS);
363 bool PDBFile::hasPDBDbiStream() const { return StreamDBI < getNumStreams(); }
365 bool PDBFile::hasPDBGlobalsStream() {
366 auto DbiS = getPDBDbiStream();
369 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
372 bool PDBFile::hasPDBInfoStream() { return StreamPDB < getNumStreams(); }
374 bool PDBFile::hasPDBIpiStream() const { return StreamIPI < getNumStreams(); }
376 bool PDBFile::hasPDBPublicsStream() {
377 auto DbiS = getPDBDbiStream();
380 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
383 bool PDBFile::hasPDBSymbolStream() {
384 auto DbiS = getPDBDbiStream();
387 return DbiS->getSymRecordStreamIndex() < getNumStreams();
390 bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
392 bool PDBFile::hasStringTable() {
393 auto IS = getPDBInfoStream();
396 return IS->getNamedStreamIndex("/names") < getNumStreams();
399 /// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
400 /// stream with that index actually exists. If it does not, the return value
401 /// will have an MSFError with code msf_error_code::no_stream. Else, the return
402 /// value will contain the stream returned by createIndexedStream().
403 Expected<std::unique_ptr<MappedBlockStream>>
404 PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
405 BinaryStreamRef MsfData,
406 uint32_t StreamIndex) const {
407 if (StreamIndex >= getNumStreams())
408 return make_error<RawError>(raw_error_code::no_stream);
409 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex);