blob: 96fc509d6909cf17e94640023bb57d4748686ba4 [file] [log] [blame]
Zachary Turner0a43efe2016-04-25 17:38:08 +00001//===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner0a43efe2016-04-25 17:38:08 +00006//
7//===----------------------------------------------------------------------===//
8
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00009#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000010#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/STLExtras.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000012#include "llvm/DebugInfo/MSF/MSFCommon.h"
Zachary Turnerf04d6e82017-01-20 22:41:15 +000013#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000014#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
15#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
16#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turnere204a6c2017-05-02 18:00:13 +000017#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000018#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
19#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000020#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
21#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000022#include "llvm/Support/BinaryStream.h"
23#include "llvm/Support/BinaryStreamArray.h"
24#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000025#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000026#include "llvm/Support/Error.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000027#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000028#include <algorithm>
29#include <cassert>
30#include <cstdint>
Zachary Turner0a43efe2016-04-25 17:38:08 +000031
32using namespace llvm;
Zachary Turnerb84faa82016-06-10 05:10:19 +000033using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000034using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000035using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000036
37namespace {
Zachary Turnerb84faa82016-06-10 05:10:19 +000038typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
Eugene Zelenko570e39a2016-11-23 23:16:32 +000039} // end anonymous namespace
Zachary Turner0a43efe2016-04-25 17:38:08 +000040
Zachary Turner120faca2017-02-27 22:11:43 +000041PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer,
Zachary Turnere109dc62016-07-22 19:56:26 +000042 BumpPtrAllocator &Allocator)
Zachary Turner7b327d02017-02-16 23:35:45 +000043 : FilePath(Path), Allocator(Allocator), Buffer(std::move(PdbFileBuffer)) {}
Zachary Turner0a43efe2016-04-25 17:38:08 +000044
Eugene Zelenko570e39a2016-11-23 23:16:32 +000045PDBFile::~PDBFile() = default;
Zachary Turner0a43efe2016-04-25 17:38:08 +000046
Zachary Turner7b327d02017-02-16 23:35:45 +000047StringRef PDBFile::getFilePath() const { return FilePath; }
48
49StringRef PDBFile::getFileDirectory() const {
50 return sys::path::parent_path(FilePath);
51}
52
Zachary Turnerd66889c2016-07-28 19:12:28 +000053uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000054
Zachary Turnere4a4f332016-07-22 19:56:33 +000055uint32_t PDBFile::getFreeBlockMapBlock() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000056 return ContainerLayout.SB->FreeBlockMapBlock;
Zachary Turnere4a4f332016-07-22 19:56:33 +000057}
Zachary Turner0a43efe2016-04-25 17:38:08 +000058
Zachary Turnerd66889c2016-07-28 19:12:28 +000059uint32_t PDBFile::getBlockCount() const {
60 return ContainerLayout.SB->NumBlocks;
61}
Zachary Turner0a43efe2016-04-25 17:38:08 +000062
Zachary Turnere4a4f332016-07-22 19:56:33 +000063uint32_t PDBFile::getNumDirectoryBytes() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000064 return ContainerLayout.SB->NumDirectoryBytes;
Zachary Turnere4a4f332016-07-22 19:56:33 +000065}
Zachary Turner0a43efe2016-04-25 17:38:08 +000066
Zachary Turnere4a4f332016-07-22 19:56:33 +000067uint32_t PDBFile::getBlockMapIndex() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000068 return ContainerLayout.SB->BlockMapAddr;
Zachary Turnere4a4f332016-07-22 19:56:33 +000069}
Zachary Turner0a43efe2016-04-25 17:38:08 +000070
Zachary Turnerd66889c2016-07-28 19:12:28 +000071uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000072
73uint32_t PDBFile::getNumDirectoryBlocks() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000074 return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes,
75 ContainerLayout.SB->BlockSize);
Zachary Turner0a43efe2016-04-25 17:38:08 +000076}
77
78uint64_t PDBFile::getBlockMapOffset() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000079 return (uint64_t)ContainerLayout.SB->BlockMapAddr *
80 ContainerLayout.SB->BlockSize;
Zachary Turner0a43efe2016-04-25 17:38:08 +000081}
82
Zachary Turnerd66889c2016-07-28 19:12:28 +000083uint32_t PDBFile::getNumStreams() const {
84 return ContainerLayout.StreamSizes.size();
85}
Zachary Turner0a43efe2016-04-25 17:38:08 +000086
Zachary Turnerd1de2f42017-08-21 14:53:25 +000087uint32_t PDBFile::getMaxStreamSize() const {
88 return *std::max_element(ContainerLayout.StreamSizes.begin(),
89 ContainerLayout.StreamSizes.end());
90}
91
Zachary Turner0a43efe2016-04-25 17:38:08 +000092uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000093 return ContainerLayout.StreamSizes[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000094}
95
Zachary Turnerd8447992016-06-07 05:28:55 +000096ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +000097PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000098 return ContainerLayout.StreamMap[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000099}
100
David Majnemer1b79e9a2016-07-10 05:32:05 +0000101uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000102
David Majnemer6211b1f2016-07-10 03:34:47 +0000103Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
104 uint32_t NumBytes) const {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000105 uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
Zachary Turner0a43efe2016-04-25 17:38:08 +0000106
Zachary Turnerb84faa82016-06-10 05:10:19 +0000107 ArrayRef<uint8_t> Result;
108 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
David Majnemer6211b1f2016-07-10 03:34:47 +0000109 return std::move(EC);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000110 return Result;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000111}
112
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000113Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
114 ArrayRef<uint8_t> Data) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000115 return make_error<RawError>(raw_error_code::not_writable,
116 "PDBFile is immutable");
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000117}
118
Zachary Turner819e77d2016-05-06 20:51:57 +0000119Error PDBFile::parseFileHeaders() {
Zachary Turner120faca2017-02-27 22:11:43 +0000120 BinaryStreamReader Reader(*Buffer);
Zachary Turnerc59261c2016-05-25 03:53:16 +0000121
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000122 // Initialize SB.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000123 const msf::SuperBlock *SB = nullptr;
Zachary Turnerb84faa82016-06-10 05:10:19 +0000124 if (auto EC = Reader.readObject(SB)) {
125 consumeError(std::move(EC));
Zachary Turner819e77d2016-05-06 20:51:57 +0000126 return make_error<RawError>(raw_error_code::corrupt_file,
Alexandre Ganea6a7efef2018-08-31 17:41:58 +0000127 "MSF superblock is missing");
Zachary Turnerb84faa82016-06-10 05:10:19 +0000128 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000129
Zachary Turnere4a4f332016-07-22 19:56:33 +0000130 if (auto EC = msf::validateSuperBlock(*SB))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000131 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000132
Zachary Turnere4a4f332016-07-22 19:56:33 +0000133 if (Buffer->getLength() % SB->BlockSize != 0)
134 return make_error<RawError>(raw_error_code::corrupt_file,
135 "File size is not a multiple of block size");
Zachary Turnerd66889c2016-07-28 19:12:28 +0000136 ContainerLayout.SB = SB;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000137
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000138 // Initialize Free Page Map.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000139 ContainerLayout.FreePageMap.resize(SB->NumBlocks);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000140 // The Fpm exists either at block 1 or block 2 of the MSF. However, this
141 // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
142 // thusly an equal number of total blocks in the file. For a block size
143 // of 4KiB (very common), this would yield 32KiB total blocks in file, for a
144 // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
145 // the Fpm is split across the file at `getBlockSize()` intervals. As a
146 // result, every block whose index is of the form |{1,2} + getBlockSize() * k|
147 // for any non-negative integer k is an Fpm block. In theory, we only really
148 // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
149 // current versions of the MSF format already expect the Fpm to be arranged
150 // at getBlockSize() intervals, so we have to be compatible.
151 // See the function fpmPn() for more information:
152 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
Zachary Turner5b74ff32017-06-03 00:33:35 +0000153 auto FpmStream =
154 MappedBlockStream::createFpmStream(ContainerLayout, *Buffer, Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000155 BinaryStreamReader FpmReader(*FpmStream);
Zachary Turner8cf51c32016-08-03 16:53:21 +0000156 ArrayRef<uint8_t> FpmBytes;
Zachary Turner9fb9d712017-08-02 22:31:39 +0000157 if (auto EC = FpmReader.readBytes(FpmBytes, FpmReader.bytesRemaining()))
Zachary Turner8cf51c32016-08-03 16:53:21 +0000158 return EC;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000159 uint32_t BlocksRemaining = getBlockCount();
Zachary Turner8cf51c32016-08-03 16:53:21 +0000160 uint32_t BI = 0;
161 for (auto Byte : FpmBytes) {
162 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
163 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
164 if (Byte & (1 << I))
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000165 ContainerLayout.FreePageMap[BI] = true;
Zachary Turner8cf51c32016-08-03 16:53:21 +0000166 --BlocksRemaining;
167 ++BI;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000168 }
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000169 }
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000170
Zachary Turnerab58ae82016-06-30 17:43:00 +0000171 Reader.setOffset(getBlockMapOffset());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000172 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
173 getNumDirectoryBlocks()))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000174 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000175
Zachary Turner819e77d2016-05-06 20:51:57 +0000176 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000177}
178
Zachary Turner819e77d2016-05-06 20:51:57 +0000179Error PDBFile::parseStreamData() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000180 assert(ContainerLayout.SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000181 if (DirectoryStream)
182 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000183
Zachary Turner0a43efe2016-04-25 17:38:08 +0000184 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000185
Zachary Turnerd8447992016-06-07 05:28:55 +0000186 // Normally you can't use a MappedBlockStream without having fully parsed the
187 // PDB file, because it accesses the directory and various other things, which
188 // is exactly what we are attempting to parse. By specifying a custom
189 // subclass of IPDBStreamData which only accesses the fields that have already
190 // been parsed, we can avoid this and reuse MappedBlockStream.
Zachary Turner5b74ff32017-06-03 00:33:35 +0000191 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer,
192 Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000193 BinaryStreamReader Reader(*DS);
Zachary Turner695ed562017-02-28 00:04:07 +0000194 if (auto EC = Reader.readInteger(NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000195 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000196
Zachary Turnerd66889c2016-07-28 19:12:28 +0000197 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000198 return EC;
199 for (uint32_t I = 0; I < NumStreams; ++I) {
Reid Kleckner5aba52f2016-06-22 22:42:24 +0000200 uint32_t StreamSize = getStreamByteSize(I);
201 // FIXME: What does StreamSize ~0U mean?
David Majnemer9efba742016-05-27 16:16:48 +0000202 uint64_t NumExpectedStreamBlocks =
Zachary Turnere4a4f332016-07-22 19:56:33 +0000203 StreamSize == UINT32_MAX
204 ? 0
Zachary Turnerd66889c2016-07-28 19:12:28 +0000205 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000206
207 // For convenience, we store the block array contiguously. This is because
208 // if someone calls setStreamMap(), it is more convenient to be able to call
209 // it with an ArrayRef instead of setting up a StreamRef. Since the
210 // DirectoryStream is cached in the class and thus lives for the life of the
211 // class, we can be guaranteed that readArray() will return a stable
212 // reference, even if it has to allocate from its internal pool.
213 ArrayRef<support::ulittle32_t> Blocks;
Zachary Turnerd8447992016-06-07 05:28:55 +0000214 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
215 return EC;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000216 for (uint32_t Block : Blocks) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000217 uint64_t BlockEndOffset =
218 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000219 if (BlockEndOffset > getFileSize())
220 return make_error<RawError>(raw_error_code::corrupt_file,
221 "Stream block map is corrupt.");
222 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000223 ContainerLayout.StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000224 }
225
Zachary Turner0a43efe2016-04-25 17:38:08 +0000226 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000227 assert(Reader.bytesRemaining() == 0);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000228 DirectoryStream = std::move(DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000229 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000230}
231
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000232ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000233 return ContainerLayout.DirectoryBlocks;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000234}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000235
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000236std::unique_ptr<MappedBlockStream> PDBFile::createIndexedStream(uint16_t SN) {
237 if (SN == kInvalidStreamIndex)
238 return nullptr;
239 return MappedBlockStream::createIndexedStream(ContainerLayout, *Buffer, SN,
240 Allocator);
241}
242
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000243MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
244 MSFStreamLayout Result;
245 auto Blocks = getStreamBlockList(StreamIdx);
246 Result.Blocks.assign(Blocks.begin(), Blocks.end());
247 Result.Length = getStreamByteSize(StreamIdx);
248 return Result;
249}
250
Zachary Turnerc3d8eec2017-08-02 22:25:52 +0000251msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
252 return msf::getFpmStreamLayout(ContainerLayout);
253}
254
Bob Haarman653baa22016-10-21 19:43:19 +0000255Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
256 if (!Globals) {
257 auto DbiS = getPDBDbiStream();
258 if (!DbiS)
259 return DbiS.takeError();
260
Bob Haarmana5b43582016-12-05 22:44:00 +0000261 auto GlobalS = safelyCreateIndexedStream(
Bob Haarman653baa22016-10-21 19:43:19 +0000262 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000263 if (!GlobalS)
264 return GlobalS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000265 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000266 if (auto EC = TempGlobals->reload())
267 return std::move(EC);
268 Globals = std::move(TempGlobals);
269 }
270 return *Globals;
271}
272
Zachary Turner819e77d2016-05-06 20:51:57 +0000273Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000274 if (!Info) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000275 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000276 if (!InfoS)
277 return InfoS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000278 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000279 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000280 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000281 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000282 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000283 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000284}
285
Zachary Turner819e77d2016-05-06 20:51:57 +0000286Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000287 if (!Dbi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000288 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000289 if (!DbiS)
290 return DbiS.takeError();
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000291 auto TempDbi = llvm::make_unique<DbiStream>(std::move(*DbiS));
292 if (auto EC = TempDbi->reload(this))
Zachary Turner819e77d2016-05-06 20:51:57 +0000293 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000294 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000295 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000296 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000297}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000298
Zachary Turner819e77d2016-05-06 20:51:57 +0000299Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000300 if (!Tpi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000301 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000302 if (!TpiS)
303 return TpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000304 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000305 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000306 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000307 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000308 }
309 return *Tpi;
310}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000311
Zachary Turnerc9972c62016-05-25 04:35:22 +0000312Expected<TpiStream &> PDBFile::getPDBIpiStream() {
313 if (!Ipi) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000314 if (!hasPDBIpiStream())
315 return make_error<RawError>(raw_error_code::no_stream);
316
Bob Haarmana5b43582016-12-05 22:44:00 +0000317 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000318 if (!IpiS)
319 return IpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000320 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000321 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000322 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000323 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000324 }
325 return *Ipi;
326}
327
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000328Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
329 if (!Publics) {
330 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000331 if (!DbiS)
332 return DbiS.takeError();
333
Bob Haarmana5b43582016-12-05 22:44:00 +0000334 auto PublicS = safelyCreateIndexedStream(
335 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000336 if (!PublicS)
337 return PublicS.takeError();
Reid Kleckner14d90fd2017-07-26 00:40:36 +0000338 auto TempPublics = llvm::make_unique<PublicsStream>(std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000339 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000340 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000341 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000342 }
343 return *Publics;
344}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000345
346Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
347 if (!Symbols) {
348 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000349 if (!DbiS)
350 return DbiS.takeError();
351
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000352 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Bob Haarmana5b43582016-12-05 22:44:00 +0000353 auto SymbolS =
354 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000355 if (!SymbolS)
356 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000357
Bob Haarmana5b43582016-12-05 22:44:00 +0000358 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000359 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000360 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000361 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000362 }
363 return *Symbols;
364}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000365
Zachary Turnere204a6c2017-05-02 18:00:13 +0000366Expected<PDBStringTable &> PDBFile::getStringTable() {
Zachary Turnerc504ae32017-05-03 15:58:37 +0000367 if (!Strings) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000368 auto IS = getPDBInfoStream();
369 if (!IS)
370 return IS.takeError();
371
Zachary Turnerd11328a2018-04-02 18:35:21 +0000372 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names");
373 if (!ExpectedNSI)
374 return ExpectedNSI.takeError();
375 uint32_t NameStreamIndex = *ExpectedNSI;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000376
Bob Haarmana5b43582016-12-05 22:44:00 +0000377 auto NS =
378 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000379 if (!NS)
380 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000381
Daniel Jasperdff096f2017-05-03 07:29:25 +0000382 auto N = llvm::make_unique<PDBStringTable>();
Zachary Turnerc504ae32017-05-03 15:58:37 +0000383 BinaryStreamReader Reader(**NS);
384 if (auto EC = N->reload(Reader))
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000385 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +0000386 assert(Reader.bytesRemaining() == 0);
387 StringTableStream = std::move(*NS);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000388 Strings = std::move(N);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000389 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000390 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000391}
Bob Haarmana5b43582016-12-05 22:44:00 +0000392
Zachary Turner4e950642017-06-15 23:56:19 +0000393uint32_t PDBFile::getPointerSize() {
394 auto DbiS = getPDBDbiStream();
395 if (!DbiS)
396 return 0;
397 PDB_Machine Machine = DbiS->getMachineType();
398 if (Machine == PDB_Machine::Amd64)
399 return 8;
400 return 4;
401}
402
Alexandre Ganea741cc352018-08-06 19:35:00 +0000403bool PDBFile::hasPDBDbiStream() const {
404 return StreamDBI < getNumStreams() && getStreamByteSize(StreamDBI) > 0;
405}
Bob Haarmana5b43582016-12-05 22:44:00 +0000406
407bool PDBFile::hasPDBGlobalsStream() {
408 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000409 if (!DbiS) {
410 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000411 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000412 }
413
Bob Haarmana5b43582016-12-05 22:44:00 +0000414 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
415}
416
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000417bool PDBFile::hasPDBInfoStream() const { return StreamPDB < getNumStreams(); }
Bob Haarmana5b43582016-12-05 22:44:00 +0000418
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000419bool PDBFile::hasPDBIpiStream() const {
420 if (!hasPDBInfoStream())
421 return false;
422
423 if (StreamIPI >= getNumStreams())
424 return false;
425
426 auto &InfoStream = cantFail(const_cast<PDBFile *>(this)->getPDBInfoStream());
427 return InfoStream.containsIdStream();
428}
Bob Haarmana5b43582016-12-05 22:44:00 +0000429
430bool PDBFile::hasPDBPublicsStream() {
431 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000432 if (!DbiS) {
433 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000434 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000435 }
Bob Haarmana5b43582016-12-05 22:44:00 +0000436 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
437}
438
439bool PDBFile::hasPDBSymbolStream() {
440 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000441 if (!DbiS)
442 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000443 return DbiS->getSymRecordStreamIndex() < getNumStreams();
444}
445
446bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
447
Zachary Turnere204a6c2017-05-02 18:00:13 +0000448bool PDBFile::hasPDBStringTable() {
Bob Haarmana5b43582016-12-05 22:44:00 +0000449 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000450 if (!IS)
451 return false;
Zachary Turnerd11328a2018-04-02 18:35:21 +0000452 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names");
453 if (!ExpectedNSI) {
454 consumeError(ExpectedNSI.takeError());
455 return false;
456 }
457 assert(*ExpectedNSI < getNumStreams());
458 return true;
Bob Haarmana5b43582016-12-05 22:44:00 +0000459}
460
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000461/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
462/// stream with that index actually exists. If it does not, the return value
463/// will have an MSFError with code msf_error_code::no_stream. Else, the return
464/// value will contain the stream returned by createIndexedStream().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000465Expected<std::unique_ptr<MappedBlockStream>>
466PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000467 BinaryStreamRef MsfData,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000468 uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000469 if (StreamIndex >= getNumStreams())
470 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turner5b74ff32017-06-03 00:33:35 +0000471 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex,
472 Allocator);
Bob Haarmana5b43582016-12-05 22:44:00 +0000473}