blob: f158c5c538695d2875aac391915f1df75d6a2e44 [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 Turner8cf51c32016-08-03 16:53:21 +0000149 auto FpmStream = MappedBlockStream::createFpmStream(ContainerLayout, *Buffer);
Zachary Turner120faca2017-02-27 22:11:43 +0000150 BinaryStreamReader FpmReader(*FpmStream);
Zachary Turner8cf51c32016-08-03 16:53:21 +0000151 ArrayRef<uint8_t> FpmBytes;
152 if (auto EC = FpmReader.readBytes(FpmBytes,
153 msf::getFullFpmByteSize(ContainerLayout)))
154 return EC;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000155 uint32_t BlocksRemaining = getBlockCount();
Zachary Turner8cf51c32016-08-03 16:53:21 +0000156 uint32_t BI = 0;
157 for (auto Byte : FpmBytes) {
158 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
159 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
160 if (Byte & (1 << I))
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000161 ContainerLayout.FreePageMap[BI] = true;
Zachary Turner8cf51c32016-08-03 16:53:21 +0000162 --BlocksRemaining;
163 ++BI;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000164 }
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000165 }
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000166
Zachary Turnerab58ae82016-06-30 17:43:00 +0000167 Reader.setOffset(getBlockMapOffset());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000168 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
169 getNumDirectoryBlocks()))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000170 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000171
Zachary Turner819e77d2016-05-06 20:51:57 +0000172 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000173}
174
Zachary Turner819e77d2016-05-06 20:51:57 +0000175Error PDBFile::parseStreamData() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000176 assert(ContainerLayout.SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000177 if (DirectoryStream)
178 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000179
Zachary Turner0a43efe2016-04-25 17:38:08 +0000180 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000181
Zachary Turnerd8447992016-06-07 05:28:55 +0000182 // 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.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000187 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer);
Zachary Turner120faca2017-02-27 22:11:43 +0000188 BinaryStreamReader Reader(*DS);
Zachary Turner695ed562017-02-28 00:04:07 +0000189 if (auto EC = Reader.readInteger(NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000190 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000191
Zachary Turnerd66889c2016-07-28 19:12:28 +0000192 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000193 return EC;
194 for (uint32_t I = 0; I < NumStreams; ++I) {
Reid Kleckner5aba52f2016-06-22 22:42:24 +0000195 uint32_t StreamSize = getStreamByteSize(I);
196 // FIXME: What does StreamSize ~0U mean?
David Majnemer9efba742016-05-27 16:16:48 +0000197 uint64_t NumExpectedStreamBlocks =
Zachary Turnere4a4f332016-07-22 19:56:33 +0000198 StreamSize == UINT32_MAX
199 ? 0
Zachary Turnerd66889c2016-07-28 19:12:28 +0000200 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000201
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;
Zachary Turnerd8447992016-06-07 05:28:55 +0000209 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
210 return EC;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000211 for (uint32_t Block : Blocks) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000212 uint64_t BlockEndOffset =
213 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000214 if (BlockEndOffset > getFileSize())
215 return make_error<RawError>(raw_error_code::corrupt_file,
216 "Stream block map is corrupt.");
217 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000218 ContainerLayout.StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000219 }
220
Zachary Turner0a43efe2016-04-25 17:38:08 +0000221 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000222 assert(Reader.bytesRemaining() == 0);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000223 DirectoryStream = std::move(DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000224 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000225}
226
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000227ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000228 return ContainerLayout.DirectoryBlocks;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000229}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000230
Bob Haarman653baa22016-10-21 19:43:19 +0000231Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
232 if (!Globals) {
233 auto DbiS = getPDBDbiStream();
234 if (!DbiS)
235 return DbiS.takeError();
236
Bob Haarmana5b43582016-12-05 22:44:00 +0000237 auto GlobalS = safelyCreateIndexedStream(
Bob Haarman653baa22016-10-21 19:43:19 +0000238 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000239 if (!GlobalS)
240 return GlobalS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000241 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000242 if (auto EC = TempGlobals->reload())
243 return std::move(EC);
244 Globals = std::move(TempGlobals);
245 }
246 return *Globals;
247}
248
Zachary Turner819e77d2016-05-06 20:51:57 +0000249Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000250 if (!Info) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000251 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000252 if (!InfoS)
253 return InfoS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000254 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000255 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000256 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000257 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000258 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000259 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000260}
261
Zachary Turner819e77d2016-05-06 20:51:57 +0000262Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000263 if (!Dbi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000264 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000265 if (!DbiS)
266 return DbiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000267 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000268 if (auto EC = TempDbi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000269 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000270 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000271 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000272 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000273}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000274
Zachary Turner819e77d2016-05-06 20:51:57 +0000275Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000276 if (!Tpi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000277 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000278 if (!TpiS)
279 return TpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000280 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000281 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000282 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000283 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000284 }
285 return *Tpi;
286}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000287
Zachary Turnerc9972c62016-05-25 04:35:22 +0000288Expected<TpiStream &> PDBFile::getPDBIpiStream() {
289 if (!Ipi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000290 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000291 if (!IpiS)
292 return IpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000293 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000294 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000295 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000296 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000297 }
298 return *Ipi;
299}
300
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000301Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
302 if (!Publics) {
303 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000304 if (!DbiS)
305 return DbiS.takeError();
306
Bob Haarmana5b43582016-12-05 22:44:00 +0000307 auto PublicS = safelyCreateIndexedStream(
308 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000309 if (!PublicS)
310 return PublicS.takeError();
Zachary Turnera1657a92016-06-08 17:26:39 +0000311 auto TempPublics =
Bob Haarmana5b43582016-12-05 22:44:00 +0000312 llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000313 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000314 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000315 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000316 }
317 return *Publics;
318}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000319
320Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
321 if (!Symbols) {
322 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000323 if (!DbiS)
324 return DbiS.takeError();
325
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000326 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Bob Haarmana5b43582016-12-05 22:44:00 +0000327 auto SymbolS =
328 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000329 if (!SymbolS)
330 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000331
Bob Haarmana5b43582016-12-05 22:44:00 +0000332 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000333 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000334 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000335 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000336 }
337 return *Symbols;
338}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000339
Zachary Turnere204a6c2017-05-02 18:00:13 +0000340Expected<PDBStringTable &> PDBFile::getStringTable() {
341 if (!Strings || !PDBStringTableStream) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000342 auto IS = getPDBInfoStream();
343 if (!IS)
344 return IS.takeError();
345
346 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000347
Bob Haarmana5b43582016-12-05 22:44:00 +0000348 auto NS =
349 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000350 if (!NS)
351 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000352
Zachary Turner120faca2017-02-27 22:11:43 +0000353 BinaryStreamReader Reader(**NS);
Zachary Turnere204a6c2017-05-02 18:00:13 +0000354 auto N = llvm::make_unique<PDBStringTable>();
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000355 if (auto EC = N->load(Reader))
356 return std::move(EC);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000357 Strings = std::move(N);
Zachary Turnere204a6c2017-05-02 18:00:13 +0000358 PDBStringTableStream = std::move(*NS);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000359 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000360 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000361}
Bob Haarmana5b43582016-12-05 22:44:00 +0000362
363bool PDBFile::hasPDBDbiStream() const { return StreamDBI < getNumStreams(); }
364
365bool PDBFile::hasPDBGlobalsStream() {
366 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000367 if (!DbiS)
368 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000369 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
370}
371
372bool PDBFile::hasPDBInfoStream() { return StreamPDB < getNumStreams(); }
373
374bool PDBFile::hasPDBIpiStream() const { return StreamIPI < getNumStreams(); }
375
376bool PDBFile::hasPDBPublicsStream() {
377 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000378 if (!DbiS)
379 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000380 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
381}
382
383bool PDBFile::hasPDBSymbolStream() {
384 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000385 if (!DbiS)
386 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000387 return DbiS->getSymRecordStreamIndex() < getNumStreams();
388}
389
390bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
391
Zachary Turnere204a6c2017-05-02 18:00:13 +0000392bool PDBFile::hasPDBStringTable() {
Bob Haarmana5b43582016-12-05 22:44:00 +0000393 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000394 if (!IS)
395 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000396 return IS->getNamedStreamIndex("/names") < getNumStreams();
397}
398
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000399/// 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().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000403Expected<std::unique_ptr<MappedBlockStream>>
404PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000405 BinaryStreamRef MsfData,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000406 uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000407 if (StreamIndex >= getNumStreams())
408 return make_error<RawError>(raw_error_code::no_stream);
409 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex);
410}