blob: e0bb460d21cd6d331b445c72292a5ba28d943740 [file] [log] [blame]
Zachary Turner0a43efe2016-04-25 17:38:08 +00001//===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000011#include "llvm/ADT/ArrayRef.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000012#include "llvm/ADT/STLExtras.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000013#include "llvm/DebugInfo/MSF/MSFCommon.h"
Zachary Turnerf04d6e82017-01-20 22:41:15 +000014#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000015#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
16#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
17#include "llvm/DebugInfo/PDB/Native/InfoStream.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
88uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000089 return ContainerLayout.StreamSizes[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000090}
91
Zachary Turnerd8447992016-06-07 05:28:55 +000092ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +000093PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000094 return ContainerLayout.StreamMap[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000095}
96
David Majnemer1b79e9a2016-07-10 05:32:05 +000097uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
Zachary Turner1dc9fd32016-06-14 20:48:36 +000098
David Majnemer6211b1f2016-07-10 03:34:47 +000099Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
100 uint32_t NumBytes) const {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000101 uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
Zachary Turner0a43efe2016-04-25 17:38:08 +0000102
Zachary Turnerb84faa82016-06-10 05:10:19 +0000103 ArrayRef<uint8_t> Result;
104 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
David Majnemer6211b1f2016-07-10 03:34:47 +0000105 return std::move(EC);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000106 return Result;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000107}
108
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000109Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
110 ArrayRef<uint8_t> Data) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000111 return make_error<RawError>(raw_error_code::not_writable,
112 "PDBFile is immutable");
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000113}
114
Zachary Turner819e77d2016-05-06 20:51:57 +0000115Error PDBFile::parseFileHeaders() {
Zachary Turner120faca2017-02-27 22:11:43 +0000116 BinaryStreamReader Reader(*Buffer);
Zachary Turnerc59261c2016-05-25 03:53:16 +0000117
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000118 // Initialize SB.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000119 const msf::SuperBlock *SB = nullptr;
Zachary Turnerb84faa82016-06-10 05:10:19 +0000120 if (auto EC = Reader.readObject(SB)) {
121 consumeError(std::move(EC));
Zachary Turner819e77d2016-05-06 20:51:57 +0000122 return make_error<RawError>(raw_error_code::corrupt_file,
123 "Does not contain superblock");
Zachary Turnerb84faa82016-06-10 05:10:19 +0000124 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000125
Zachary Turnere4a4f332016-07-22 19:56:33 +0000126 if (auto EC = msf::validateSuperBlock(*SB))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000127 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000128
Zachary Turnere4a4f332016-07-22 19:56:33 +0000129 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");
Zachary Turnerd66889c2016-07-28 19:12:28 +0000132 ContainerLayout.SB = SB;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000133
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000134 // Initialize Free Page Map.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000135 ContainerLayout.FreePageMap.resize(SB->NumBlocks);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000136 // 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
Zachary Turner5b74ff32017-06-03 00:33:35 +0000149 auto FpmStream =
150 MappedBlockStream::createFpmStream(ContainerLayout, *Buffer, Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000151 BinaryStreamReader FpmReader(*FpmStream);
Zachary Turner8cf51c32016-08-03 16:53:21 +0000152 ArrayRef<uint8_t> FpmBytes;
153 if (auto EC = FpmReader.readBytes(FpmBytes,
154 msf::getFullFpmByteSize(ContainerLayout)))
155 return EC;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000156 uint32_t BlocksRemaining = getBlockCount();
Zachary Turner8cf51c32016-08-03 16:53:21 +0000157 uint32_t BI = 0;
158 for (auto Byte : FpmBytes) {
159 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
160 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
161 if (Byte & (1 << I))
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000162 ContainerLayout.FreePageMap[BI] = true;
Zachary Turner8cf51c32016-08-03 16:53:21 +0000163 --BlocksRemaining;
164 ++BI;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000165 }
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000166 }
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000167
Zachary Turnerab58ae82016-06-30 17:43:00 +0000168 Reader.setOffset(getBlockMapOffset());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000169 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
170 getNumDirectoryBlocks()))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000171 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000172
Zachary Turner819e77d2016-05-06 20:51:57 +0000173 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000174}
175
Zachary Turner819e77d2016-05-06 20:51:57 +0000176Error PDBFile::parseStreamData() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000177 assert(ContainerLayout.SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000178 if (DirectoryStream)
179 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000180
Zachary Turner0a43efe2016-04-25 17:38:08 +0000181 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000182
Zachary Turnerd8447992016-06-07 05:28:55 +0000183 // Normally you can't use a MappedBlockStream without having fully parsed the
184 // PDB file, because it accesses the directory and various other things, which
185 // is exactly what we are attempting to parse. By specifying a custom
186 // subclass of IPDBStreamData which only accesses the fields that have already
187 // been parsed, we can avoid this and reuse MappedBlockStream.
Zachary Turner5b74ff32017-06-03 00:33:35 +0000188 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer,
189 Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000190 BinaryStreamReader Reader(*DS);
Zachary Turner695ed562017-02-28 00:04:07 +0000191 if (auto EC = Reader.readInteger(NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000192 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000193
Zachary Turnerd66889c2016-07-28 19:12:28 +0000194 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000195 return EC;
196 for (uint32_t I = 0; I < NumStreams; ++I) {
Reid Kleckner5aba52f2016-06-22 22:42:24 +0000197 uint32_t StreamSize = getStreamByteSize(I);
198 // FIXME: What does StreamSize ~0U mean?
David Majnemer9efba742016-05-27 16:16:48 +0000199 uint64_t NumExpectedStreamBlocks =
Zachary Turnere4a4f332016-07-22 19:56:33 +0000200 StreamSize == UINT32_MAX
201 ? 0
Zachary Turnerd66889c2016-07-28 19:12:28 +0000202 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000203
204 // For convenience, we store the block array contiguously. This is because
205 // if someone calls setStreamMap(), it is more convenient to be able to call
206 // it with an ArrayRef instead of setting up a StreamRef. Since the
207 // DirectoryStream is cached in the class and thus lives for the life of the
208 // class, we can be guaranteed that readArray() will return a stable
209 // reference, even if it has to allocate from its internal pool.
210 ArrayRef<support::ulittle32_t> Blocks;
Zachary Turnerd8447992016-06-07 05:28:55 +0000211 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
212 return EC;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000213 for (uint32_t Block : Blocks) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000214 uint64_t BlockEndOffset =
215 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000216 if (BlockEndOffset > getFileSize())
217 return make_error<RawError>(raw_error_code::corrupt_file,
218 "Stream block map is corrupt.");
219 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000220 ContainerLayout.StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000221 }
222
Zachary Turner0a43efe2016-04-25 17:38:08 +0000223 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000224 assert(Reader.bytesRemaining() == 0);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000225 DirectoryStream = std::move(DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000226 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000227}
228
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000229ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000230 return ContainerLayout.DirectoryBlocks;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000231}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000232
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000233MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
234 MSFStreamLayout Result;
235 auto Blocks = getStreamBlockList(StreamIdx);
236 Result.Blocks.assign(Blocks.begin(), Blocks.end());
237 Result.Length = getStreamByteSize(StreamIdx);
238 return Result;
239}
240
Zachary Turnerc3d8eec2017-08-02 22:25:52 +0000241msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
242 return msf::getFpmStreamLayout(ContainerLayout);
243}
244
Bob Haarman653baa22016-10-21 19:43:19 +0000245Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
246 if (!Globals) {
247 auto DbiS = getPDBDbiStream();
248 if (!DbiS)
249 return DbiS.takeError();
250
Bob Haarmana5b43582016-12-05 22:44:00 +0000251 auto GlobalS = safelyCreateIndexedStream(
Bob Haarman653baa22016-10-21 19:43:19 +0000252 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000253 if (!GlobalS)
254 return GlobalS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000255 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000256 if (auto EC = TempGlobals->reload())
257 return std::move(EC);
258 Globals = std::move(TempGlobals);
259 }
260 return *Globals;
261}
262
Zachary Turner819e77d2016-05-06 20:51:57 +0000263Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000264 if (!Info) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000265 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000266 if (!InfoS)
267 return InfoS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000268 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000269 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000270 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000271 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000272 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000273 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000274}
275
Zachary Turner819e77d2016-05-06 20:51:57 +0000276Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000277 if (!Dbi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000278 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000279 if (!DbiS)
280 return DbiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000281 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000282 if (auto EC = TempDbi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000283 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000284 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000285 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000286 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000287}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000288
Zachary Turner819e77d2016-05-06 20:51:57 +0000289Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000290 if (!Tpi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000291 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000292 if (!TpiS)
293 return TpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000294 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000295 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000296 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000297 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000298 }
299 return *Tpi;
300}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000301
Zachary Turnerc9972c62016-05-25 04:35:22 +0000302Expected<TpiStream &> PDBFile::getPDBIpiStream() {
303 if (!Ipi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000304 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000305 if (!IpiS)
306 return IpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000307 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000308 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000309 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000310 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000311 }
312 return *Ipi;
313}
314
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000315Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
316 if (!Publics) {
317 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000318 if (!DbiS)
319 return DbiS.takeError();
320
Bob Haarmana5b43582016-12-05 22:44:00 +0000321 auto PublicS = safelyCreateIndexedStream(
322 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000323 if (!PublicS)
324 return PublicS.takeError();
Reid Kleckner14d90fd2017-07-26 00:40:36 +0000325 auto TempPublics = llvm::make_unique<PublicsStream>(std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000326 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000327 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000328 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000329 }
330 return *Publics;
331}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000332
333Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
334 if (!Symbols) {
335 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000336 if (!DbiS)
337 return DbiS.takeError();
338
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000339 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Bob Haarmana5b43582016-12-05 22:44:00 +0000340 auto SymbolS =
341 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000342 if (!SymbolS)
343 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000344
Bob Haarmana5b43582016-12-05 22:44:00 +0000345 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000346 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000347 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000348 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000349 }
350 return *Symbols;
351}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000352
Zachary Turnere204a6c2017-05-02 18:00:13 +0000353Expected<PDBStringTable &> PDBFile::getStringTable() {
Zachary Turnerc504ae32017-05-03 15:58:37 +0000354 if (!Strings) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000355 auto IS = getPDBInfoStream();
356 if (!IS)
357 return IS.takeError();
358
359 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000360
Bob Haarmana5b43582016-12-05 22:44:00 +0000361 auto NS =
362 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000363 if (!NS)
364 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000365
Daniel Jasperdff096f2017-05-03 07:29:25 +0000366 auto N = llvm::make_unique<PDBStringTable>();
Zachary Turnerc504ae32017-05-03 15:58:37 +0000367 BinaryStreamReader Reader(**NS);
368 if (auto EC = N->reload(Reader))
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000369 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +0000370 assert(Reader.bytesRemaining() == 0);
371 StringTableStream = std::move(*NS);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000372 Strings = std::move(N);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000373 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000374 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000375}
Bob Haarmana5b43582016-12-05 22:44:00 +0000376
Zachary Turner4e950642017-06-15 23:56:19 +0000377uint32_t PDBFile::getPointerSize() {
378 auto DbiS = getPDBDbiStream();
379 if (!DbiS)
380 return 0;
381 PDB_Machine Machine = DbiS->getMachineType();
382 if (Machine == PDB_Machine::Amd64)
383 return 8;
384 return 4;
385}
386
Bob Haarmana5b43582016-12-05 22:44:00 +0000387bool PDBFile::hasPDBDbiStream() const { return StreamDBI < getNumStreams(); }
388
389bool PDBFile::hasPDBGlobalsStream() {
390 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000391 if (!DbiS) {
392 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000393 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000394 }
395
Bob Haarmana5b43582016-12-05 22:44:00 +0000396 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
397}
398
399bool PDBFile::hasPDBInfoStream() { return StreamPDB < getNumStreams(); }
400
401bool PDBFile::hasPDBIpiStream() const { return StreamIPI < getNumStreams(); }
402
403bool PDBFile::hasPDBPublicsStream() {
404 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000405 if (!DbiS) {
406 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000407 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000408 }
Bob Haarmana5b43582016-12-05 22:44:00 +0000409 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
410}
411
412bool PDBFile::hasPDBSymbolStream() {
413 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000414 if (!DbiS)
415 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000416 return DbiS->getSymRecordStreamIndex() < getNumStreams();
417}
418
419bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
420
Zachary Turnere204a6c2017-05-02 18:00:13 +0000421bool PDBFile::hasPDBStringTable() {
Bob Haarmana5b43582016-12-05 22:44:00 +0000422 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000423 if (!IS)
424 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000425 return IS->getNamedStreamIndex("/names") < getNumStreams();
426}
427
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000428/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
429/// stream with that index actually exists. If it does not, the return value
430/// will have an MSFError with code msf_error_code::no_stream. Else, the return
431/// value will contain the stream returned by createIndexedStream().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000432Expected<std::unique_ptr<MappedBlockStream>>
433PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000434 BinaryStreamRef MsfData,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000435 uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000436 if (StreamIndex >= getNumStreams())
437 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turner5b74ff32017-06-03 00:33:35 +0000438 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex,
439 Allocator);
Bob Haarmana5b43582016-12-05 22:44:00 +0000440}