blob: 9ac226b89139b29b1259e277b274627e7892a11b [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"
Nico Weberd100b5dd2019-07-16 18:04:26 +000017#include "llvm/DebugInfo/PDB/Native/InjectedSourceStream.h"
Zachary Turnere204a6c2017-05-02 18:00:13 +000018#include "llvm/DebugInfo/PDB/Native/PDBStringTable.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000019#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
20#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000021#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
22#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000023#include "llvm/Support/BinaryStream.h"
24#include "llvm/Support/BinaryStreamArray.h"
25#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000026#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000027#include "llvm/Support/Error.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000028#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000029#include <algorithm>
30#include <cassert>
31#include <cstdint>
Zachary Turner0a43efe2016-04-25 17:38:08 +000032
33using namespace llvm;
Zachary Turnerb84faa82016-06-10 05:10:19 +000034using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000035using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000036using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000037
38namespace {
Zachary Turnerb84faa82016-06-10 05:10:19 +000039typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
Eugene Zelenko570e39a2016-11-23 23:16:32 +000040} // end anonymous namespace
Zachary Turner0a43efe2016-04-25 17:38:08 +000041
Zachary Turner120faca2017-02-27 22:11:43 +000042PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer,
Zachary Turnere109dc62016-07-22 19:56:26 +000043 BumpPtrAllocator &Allocator)
Zachary Turner7b327d02017-02-16 23:35:45 +000044 : FilePath(Path), Allocator(Allocator), Buffer(std::move(PdbFileBuffer)) {}
Zachary Turner0a43efe2016-04-25 17:38:08 +000045
Eugene Zelenko570e39a2016-11-23 23:16:32 +000046PDBFile::~PDBFile() = default;
Zachary Turner0a43efe2016-04-25 17:38:08 +000047
Zachary Turner7b327d02017-02-16 23:35:45 +000048StringRef PDBFile::getFilePath() const { return FilePath; }
49
50StringRef PDBFile::getFileDirectory() const {
51 return sys::path::parent_path(FilePath);
52}
53
Zachary Turnerd66889c2016-07-28 19:12:28 +000054uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000055
Zachary Turnere4a4f332016-07-22 19:56:33 +000056uint32_t PDBFile::getFreeBlockMapBlock() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000057 return ContainerLayout.SB->FreeBlockMapBlock;
Zachary Turnere4a4f332016-07-22 19:56:33 +000058}
Zachary Turner0a43efe2016-04-25 17:38:08 +000059
Zachary Turnerd66889c2016-07-28 19:12:28 +000060uint32_t PDBFile::getBlockCount() const {
61 return ContainerLayout.SB->NumBlocks;
62}
Zachary Turner0a43efe2016-04-25 17:38:08 +000063
Zachary Turnere4a4f332016-07-22 19:56:33 +000064uint32_t PDBFile::getNumDirectoryBytes() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000065 return ContainerLayout.SB->NumDirectoryBytes;
Zachary Turnere4a4f332016-07-22 19:56:33 +000066}
Zachary Turner0a43efe2016-04-25 17:38:08 +000067
Zachary Turnere4a4f332016-07-22 19:56:33 +000068uint32_t PDBFile::getBlockMapIndex() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000069 return ContainerLayout.SB->BlockMapAddr;
Zachary Turnere4a4f332016-07-22 19:56:33 +000070}
Zachary Turner0a43efe2016-04-25 17:38:08 +000071
Zachary Turnerd66889c2016-07-28 19:12:28 +000072uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000073
74uint32_t PDBFile::getNumDirectoryBlocks() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000075 return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes,
76 ContainerLayout.SB->BlockSize);
Zachary Turner0a43efe2016-04-25 17:38:08 +000077}
78
79uint64_t PDBFile::getBlockMapOffset() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000080 return (uint64_t)ContainerLayout.SB->BlockMapAddr *
81 ContainerLayout.SB->BlockSize;
Zachary Turner0a43efe2016-04-25 17:38:08 +000082}
83
Zachary Turnerd66889c2016-07-28 19:12:28 +000084uint32_t PDBFile::getNumStreams() const {
85 return ContainerLayout.StreamSizes.size();
86}
Zachary Turner0a43efe2016-04-25 17:38:08 +000087
Zachary Turnerd1de2f42017-08-21 14:53:25 +000088uint32_t PDBFile::getMaxStreamSize() const {
89 return *std::max_element(ContainerLayout.StreamSizes.begin(),
90 ContainerLayout.StreamSizes.end());
91}
92
Zachary Turner0a43efe2016-04-25 17:38:08 +000093uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000094 return ContainerLayout.StreamSizes[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000095}
96
Zachary Turnerd8447992016-06-07 05:28:55 +000097ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +000098PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000099 return ContainerLayout.StreamMap[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +0000100}
101
David Majnemer1b79e9a2016-07-10 05:32:05 +0000102uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000103
David Majnemer6211b1f2016-07-10 03:34:47 +0000104Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
105 uint32_t NumBytes) const {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000106 uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
Zachary Turner0a43efe2016-04-25 17:38:08 +0000107
Zachary Turnerb84faa82016-06-10 05:10:19 +0000108 ArrayRef<uint8_t> Result;
109 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
David Majnemer6211b1f2016-07-10 03:34:47 +0000110 return std::move(EC);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000111 return Result;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000112}
113
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000114Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
115 ArrayRef<uint8_t> Data) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000116 return make_error<RawError>(raw_error_code::not_writable,
117 "PDBFile is immutable");
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000118}
119
Zachary Turner819e77d2016-05-06 20:51:57 +0000120Error PDBFile::parseFileHeaders() {
Zachary Turner120faca2017-02-27 22:11:43 +0000121 BinaryStreamReader Reader(*Buffer);
Zachary Turnerc59261c2016-05-25 03:53:16 +0000122
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000123 // Initialize SB.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000124 const msf::SuperBlock *SB = nullptr;
Zachary Turnerb84faa82016-06-10 05:10:19 +0000125 if (auto EC = Reader.readObject(SB)) {
126 consumeError(std::move(EC));
Zachary Turner819e77d2016-05-06 20:51:57 +0000127 return make_error<RawError>(raw_error_code::corrupt_file,
Alexandre Ganea6a7efef2018-08-31 17:41:58 +0000128 "MSF superblock is missing");
Zachary Turnerb84faa82016-06-10 05:10:19 +0000129 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000130
Zachary Turnere4a4f332016-07-22 19:56:33 +0000131 if (auto EC = msf::validateSuperBlock(*SB))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000132 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000133
Zachary Turnere4a4f332016-07-22 19:56:33 +0000134 if (Buffer->getLength() % SB->BlockSize != 0)
135 return make_error<RawError>(raw_error_code::corrupt_file,
136 "File size is not a multiple of block size");
Zachary Turnerd66889c2016-07-28 19:12:28 +0000137 ContainerLayout.SB = SB;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000138
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000139 // Initialize Free Page Map.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000140 ContainerLayout.FreePageMap.resize(SB->NumBlocks);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000141 // The Fpm exists either at block 1 or block 2 of the MSF. However, this
142 // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
143 // thusly an equal number of total blocks in the file. For a block size
144 // of 4KiB (very common), this would yield 32KiB total blocks in file, for a
145 // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
146 // the Fpm is split across the file at `getBlockSize()` intervals. As a
147 // result, every block whose index is of the form |{1,2} + getBlockSize() * k|
148 // for any non-negative integer k is an Fpm block. In theory, we only really
149 // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
150 // current versions of the MSF format already expect the Fpm to be arranged
151 // at getBlockSize() intervals, so we have to be compatible.
152 // See the function fpmPn() for more information:
153 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
Zachary Turner5b74ff32017-06-03 00:33:35 +0000154 auto FpmStream =
155 MappedBlockStream::createFpmStream(ContainerLayout, *Buffer, Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000156 BinaryStreamReader FpmReader(*FpmStream);
Zachary Turner8cf51c32016-08-03 16:53:21 +0000157 ArrayRef<uint8_t> FpmBytes;
Zachary Turner9fb9d712017-08-02 22:31:39 +0000158 if (auto EC = FpmReader.readBytes(FpmBytes, FpmReader.bytesRemaining()))
Zachary Turner8cf51c32016-08-03 16:53:21 +0000159 return EC;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000160 uint32_t BlocksRemaining = getBlockCount();
Zachary Turner8cf51c32016-08-03 16:53:21 +0000161 uint32_t BI = 0;
162 for (auto Byte : FpmBytes) {
163 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
164 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
165 if (Byte & (1 << I))
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000166 ContainerLayout.FreePageMap[BI] = true;
Zachary Turner8cf51c32016-08-03 16:53:21 +0000167 --BlocksRemaining;
168 ++BI;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000169 }
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000170 }
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000171
Zachary Turnerab58ae82016-06-30 17:43:00 +0000172 Reader.setOffset(getBlockMapOffset());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000173 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
174 getNumDirectoryBlocks()))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000175 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000176
Zachary Turner819e77d2016-05-06 20:51:57 +0000177 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000178}
179
Zachary Turner819e77d2016-05-06 20:51:57 +0000180Error PDBFile::parseStreamData() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000181 assert(ContainerLayout.SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000182 if (DirectoryStream)
183 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000184
Zachary Turner0a43efe2016-04-25 17:38:08 +0000185 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000186
Zachary Turnerd8447992016-06-07 05:28:55 +0000187 // Normally you can't use a MappedBlockStream without having fully parsed the
188 // PDB file, because it accesses the directory and various other things, which
189 // is exactly what we are attempting to parse. By specifying a custom
190 // subclass of IPDBStreamData which only accesses the fields that have already
191 // been parsed, we can avoid this and reuse MappedBlockStream.
Zachary Turner5b74ff32017-06-03 00:33:35 +0000192 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer,
193 Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000194 BinaryStreamReader Reader(*DS);
Zachary Turner695ed562017-02-28 00:04:07 +0000195 if (auto EC = Reader.readInteger(NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000196 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000197
Zachary Turnerd66889c2016-07-28 19:12:28 +0000198 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000199 return EC;
200 for (uint32_t I = 0; I < NumStreams; ++I) {
Reid Kleckner5aba52f2016-06-22 22:42:24 +0000201 uint32_t StreamSize = getStreamByteSize(I);
202 // FIXME: What does StreamSize ~0U mean?
David Majnemer9efba742016-05-27 16:16:48 +0000203 uint64_t NumExpectedStreamBlocks =
Zachary Turnere4a4f332016-07-22 19:56:33 +0000204 StreamSize == UINT32_MAX
205 ? 0
Zachary Turnerd66889c2016-07-28 19:12:28 +0000206 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000207
208 // For convenience, we store the block array contiguously. This is because
209 // if someone calls setStreamMap(), it is more convenient to be able to call
210 // it with an ArrayRef instead of setting up a StreamRef. Since the
211 // DirectoryStream is cached in the class and thus lives for the life of the
212 // class, we can be guaranteed that readArray() will return a stable
213 // reference, even if it has to allocate from its internal pool.
214 ArrayRef<support::ulittle32_t> Blocks;
Zachary Turnerd8447992016-06-07 05:28:55 +0000215 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
216 return EC;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000217 for (uint32_t Block : Blocks) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000218 uint64_t BlockEndOffset =
219 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000220 if (BlockEndOffset > getFileSize())
221 return make_error<RawError>(raw_error_code::corrupt_file,
222 "Stream block map is corrupt.");
223 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000224 ContainerLayout.StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000225 }
226
Zachary Turner0a43efe2016-04-25 17:38:08 +0000227 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000228 assert(Reader.bytesRemaining() == 0);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000229 DirectoryStream = std::move(DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000230 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000231}
232
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000233ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000234 return ContainerLayout.DirectoryBlocks;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000235}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000236
Nico Weber13f7ddf2019-07-12 18:24:38 +0000237std::unique_ptr<MappedBlockStream>
238PDBFile::createIndexedStream(uint16_t SN) const {
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000239 if (SN == kInvalidStreamIndex)
240 return nullptr;
241 return MappedBlockStream::createIndexedStream(ContainerLayout, *Buffer, SN,
242 Allocator);
243}
244
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000245MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
246 MSFStreamLayout Result;
247 auto Blocks = getStreamBlockList(StreamIdx);
248 Result.Blocks.assign(Blocks.begin(), Blocks.end());
249 Result.Length = getStreamByteSize(StreamIdx);
250 return Result;
251}
252
Zachary Turnerc3d8eec2017-08-02 22:25:52 +0000253msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
254 return msf::getFpmStreamLayout(ContainerLayout);
255}
256
Bob Haarman653baa22016-10-21 19:43:19 +0000257Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
258 if (!Globals) {
259 auto DbiS = getPDBDbiStream();
260 if (!DbiS)
261 return DbiS.takeError();
262
Nico Weber13f7ddf2019-07-12 18:24:38 +0000263 auto GlobalS =
264 safelyCreateIndexedStream(DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000265 if (!GlobalS)
266 return GlobalS.takeError();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000267 auto TempGlobals = std::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000268 if (auto EC = TempGlobals->reload())
269 return std::move(EC);
270 Globals = std::move(TempGlobals);
271 }
272 return *Globals;
273}
274
Zachary Turner819e77d2016-05-06 20:51:57 +0000275Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000276 if (!Info) {
Nico Weber13f7ddf2019-07-12 18:24:38 +0000277 auto InfoS = safelyCreateIndexedStream(StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000278 if (!InfoS)
279 return InfoS.takeError();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000280 auto TempInfo = std::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000281 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000282 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000283 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000284 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000285 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000286}
287
Zachary Turner819e77d2016-05-06 20:51:57 +0000288Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000289 if (!Dbi) {
Nico Weber13f7ddf2019-07-12 18:24:38 +0000290 auto DbiS = safelyCreateIndexedStream(StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000291 if (!DbiS)
292 return DbiS.takeError();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000293 auto TempDbi = std::make_unique<DbiStream>(std::move(*DbiS));
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000294 if (auto EC = TempDbi->reload(this))
Zachary Turner819e77d2016-05-06 20:51:57 +0000295 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000296 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000297 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000298 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000299}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000300
Zachary Turner819e77d2016-05-06 20:51:57 +0000301Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000302 if (!Tpi) {
Nico Weber13f7ddf2019-07-12 18:24:38 +0000303 auto TpiS = safelyCreateIndexedStream(StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000304 if (!TpiS)
305 return TpiS.takeError();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000306 auto TempTpi = std::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000307 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000308 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000309 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000310 }
311 return *Tpi;
312}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000313
Zachary Turnerc9972c62016-05-25 04:35:22 +0000314Expected<TpiStream &> PDBFile::getPDBIpiStream() {
315 if (!Ipi) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000316 if (!hasPDBIpiStream())
317 return make_error<RawError>(raw_error_code::no_stream);
318
Nico Weber13f7ddf2019-07-12 18:24:38 +0000319 auto IpiS = safelyCreateIndexedStream(StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000320 if (!IpiS)
321 return IpiS.takeError();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000322 auto TempIpi = std::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000323 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000324 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000325 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000326 }
327 return *Ipi;
328}
329
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000330Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
331 if (!Publics) {
332 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000333 if (!DbiS)
334 return DbiS.takeError();
335
Nico Weber13f7ddf2019-07-12 18:24:38 +0000336 auto PublicS =
337 safelyCreateIndexedStream(DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000338 if (!PublicS)
339 return PublicS.takeError();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000340 auto TempPublics = std::make_unique<PublicsStream>(std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000341 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000342 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000343 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000344 }
345 return *Publics;
346}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000347
348Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
349 if (!Symbols) {
350 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000351 if (!DbiS)
352 return DbiS.takeError();
353
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000354 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Nico Weber13f7ddf2019-07-12 18:24:38 +0000355 auto SymbolS = safelyCreateIndexedStream(SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000356 if (!SymbolS)
357 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000358
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000359 auto TempSymbols = std::make_unique<SymbolStream>(std::move(*SymbolS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000360 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000361 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000362 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000363 }
364 return *Symbols;
365}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000366
Zachary Turnere204a6c2017-05-02 18:00:13 +0000367Expected<PDBStringTable &> PDBFile::getStringTable() {
Zachary Turnerc504ae32017-05-03 15:58:37 +0000368 if (!Strings) {
Nico Weberd100b5dd2019-07-16 18:04:26 +0000369 auto NS = safelyCreateNamedStream("/names");
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000370 if (!NS)
371 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000372
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000373 auto N = std::make_unique<PDBStringTable>();
Zachary Turnerc504ae32017-05-03 15:58:37 +0000374 BinaryStreamReader Reader(**NS);
375 if (auto EC = N->reload(Reader))
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000376 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +0000377 assert(Reader.bytesRemaining() == 0);
378 StringTableStream = std::move(*NS);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000379 Strings = std::move(N);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000380 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000381 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000382}
Bob Haarmana5b43582016-12-05 22:44:00 +0000383
Nico Weberd100b5dd2019-07-16 18:04:26 +0000384Expected<InjectedSourceStream &> PDBFile::getInjectedSourceStream() {
385 if (!InjectedSources) {
386 auto IJS = safelyCreateNamedStream("/src/headerblock");
387 if (!IJS)
388 return IJS.takeError();
389
390 auto Strings = getStringTable();
391 if (!Strings)
392 return Strings.takeError();
393
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000394 auto IJ = std::make_unique<InjectedSourceStream>(std::move(*IJS));
Nico Weberd100b5dd2019-07-16 18:04:26 +0000395 if (auto EC = IJ->reload(*Strings))
396 return std::move(EC);
397 InjectedSources = std::move(IJ);
398 }
399 return *InjectedSources;
400}
401
Zachary Turner4e950642017-06-15 23:56:19 +0000402uint32_t PDBFile::getPointerSize() {
403 auto DbiS = getPDBDbiStream();
404 if (!DbiS)
405 return 0;
406 PDB_Machine Machine = DbiS->getMachineType();
407 if (Machine == PDB_Machine::Amd64)
408 return 8;
409 return 4;
410}
411
Alexandre Ganea741cc352018-08-06 19:35:00 +0000412bool PDBFile::hasPDBDbiStream() const {
413 return StreamDBI < getNumStreams() && getStreamByteSize(StreamDBI) > 0;
414}
Bob Haarmana5b43582016-12-05 22:44:00 +0000415
416bool PDBFile::hasPDBGlobalsStream() {
417 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000418 if (!DbiS) {
419 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000420 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000421 }
422
Bob Haarmana5b43582016-12-05 22:44:00 +0000423 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
424}
425
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000426bool PDBFile::hasPDBInfoStream() const { return StreamPDB < getNumStreams(); }
Bob Haarmana5b43582016-12-05 22:44:00 +0000427
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000428bool PDBFile::hasPDBIpiStream() const {
429 if (!hasPDBInfoStream())
430 return false;
431
432 if (StreamIPI >= getNumStreams())
433 return false;
434
435 auto &InfoStream = cantFail(const_cast<PDBFile *>(this)->getPDBInfoStream());
436 return InfoStream.containsIdStream();
437}
Bob Haarmana5b43582016-12-05 22:44:00 +0000438
439bool PDBFile::hasPDBPublicsStream() {
440 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000441 if (!DbiS) {
442 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000443 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000444 }
Bob Haarmana5b43582016-12-05 22:44:00 +0000445 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
446}
447
448bool PDBFile::hasPDBSymbolStream() {
449 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000450 if (!DbiS)
451 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000452 return DbiS->getSymRecordStreamIndex() < getNumStreams();
453}
454
455bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
456
Zachary Turnere204a6c2017-05-02 18:00:13 +0000457bool PDBFile::hasPDBStringTable() {
Bob Haarmana5b43582016-12-05 22:44:00 +0000458 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000459 if (!IS)
460 return false;
Zachary Turnerd11328a2018-04-02 18:35:21 +0000461 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names");
462 if (!ExpectedNSI) {
463 consumeError(ExpectedNSI.takeError());
464 return false;
465 }
466 assert(*ExpectedNSI < getNumStreams());
467 return true;
Bob Haarmana5b43582016-12-05 22:44:00 +0000468}
469
Nico Weberd100b5dd2019-07-16 18:04:26 +0000470bool PDBFile::hasPDBInjectedSourceStream() {
471 auto IS = getPDBInfoStream();
472 if (!IS)
473 return false;
474 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/src/headerblock");
475 if (!ExpectedNSI) {
476 consumeError(ExpectedNSI.takeError());
477 return false;
478 }
479 assert(*ExpectedNSI < getNumStreams());
480 return true;
481}
482
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000483/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
484/// stream with that index actually exists. If it does not, the return value
485/// will have an MSFError with code msf_error_code::no_stream. Else, the return
486/// value will contain the stream returned by createIndexedStream().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000487Expected<std::unique_ptr<MappedBlockStream>>
Nico Weber13f7ddf2019-07-12 18:24:38 +0000488PDBFile::safelyCreateIndexedStream(uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000489 if (StreamIndex >= getNumStreams())
Nico Weber13f7ddf2019-07-12 18:24:38 +0000490 // This rejects kInvalidStreamIndex with an error as well.
Bob Haarmana5b43582016-12-05 22:44:00 +0000491 return make_error<RawError>(raw_error_code::no_stream);
Nico Weber13f7ddf2019-07-12 18:24:38 +0000492 return createIndexedStream(StreamIndex);
Bob Haarmana5b43582016-12-05 22:44:00 +0000493}
Nico Weberd100b5dd2019-07-16 18:04:26 +0000494
495Expected<std::unique_ptr<MappedBlockStream>>
496PDBFile::safelyCreateNamedStream(StringRef Name) {
497 auto IS = getPDBInfoStream();
498 if (!IS)
499 return IS.takeError();
500
501 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex(Name);
502 if (!ExpectedNSI)
503 return ExpectedNSI.takeError();
504 uint32_t NameStreamIndex = *ExpectedNSI;
505
506 return safelyCreateIndexedStream(NameStreamIndex);
507}