blob: 7e4f16521147f9e6fa10b1d744763fdc9bbd21ab [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"
Zachary Turnerd2684b72017-02-25 00:33:34 +000013#include "llvm/DebugInfo/MSF/BinaryStream.h"
Zachary Turneraf299ea2017-02-25 00:44:30 +000014#include "llvm/DebugInfo/MSF/BinaryStream.h"
Zachary Turnerd2684b72017-02-25 00:33:34 +000015#include "llvm/DebugInfo/MSF/BinaryStreamArray.h"
Zachary Turneraf299ea2017-02-25 00:44:30 +000016#include "llvm/DebugInfo/MSF/BinaryStreamArray.h"
17#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
Zachary Turnerd2684b72017-02-25 00:33:34 +000018#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000019#include "llvm/DebugInfo/MSF/MSFCommon.h"
Zachary Turnerf04d6e82017-01-20 22:41:15 +000020#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000021#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
22#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
23#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
24#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
25#include "llvm/DebugInfo/PDB/Native/RawError.h"
26#include "llvm/DebugInfo/PDB/Native/StringTable.h"
27#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
28#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000029#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000030#include "llvm/Support/Error.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000031#include "llvm/Support/Path.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000032#include <algorithm>
33#include <cassert>
34#include <cstdint>
Zachary Turner0a43efe2016-04-25 17:38:08 +000035
36using namespace llvm;
Zachary Turnerb84faa82016-06-10 05:10:19 +000037using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000038using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000039using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000040
41namespace {
Zachary Turnerb84faa82016-06-10 05:10:19 +000042typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
Eugene Zelenko570e39a2016-11-23 23:16:32 +000043} // end anonymous namespace
Zachary Turner0a43efe2016-04-25 17:38:08 +000044
Zachary Turneraf299ea2017-02-25 00:44:30 +000045PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer,
Zachary Turnere109dc62016-07-22 19:56:26 +000046 BumpPtrAllocator &Allocator)
Zachary Turner7b327d02017-02-16 23:35:45 +000047 : FilePath(Path), Allocator(Allocator), Buffer(std::move(PdbFileBuffer)) {}
Zachary Turner0a43efe2016-04-25 17:38:08 +000048
Eugene Zelenko570e39a2016-11-23 23:16:32 +000049PDBFile::~PDBFile() = default;
Zachary Turner0a43efe2016-04-25 17:38:08 +000050
Zachary Turner7b327d02017-02-16 23:35:45 +000051StringRef PDBFile::getFilePath() const { return FilePath; }
52
53StringRef PDBFile::getFileDirectory() const {
54 return sys::path::parent_path(FilePath);
55}
56
Zachary Turnerd66889c2016-07-28 19:12:28 +000057uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000058
Zachary Turnere4a4f332016-07-22 19:56:33 +000059uint32_t PDBFile::getFreeBlockMapBlock() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000060 return ContainerLayout.SB->FreeBlockMapBlock;
Zachary Turnere4a4f332016-07-22 19:56:33 +000061}
Zachary Turner0a43efe2016-04-25 17:38:08 +000062
Zachary Turnerd66889c2016-07-28 19:12:28 +000063uint32_t PDBFile::getBlockCount() const {
64 return ContainerLayout.SB->NumBlocks;
65}
Zachary Turner0a43efe2016-04-25 17:38:08 +000066
Zachary Turnere4a4f332016-07-22 19:56:33 +000067uint32_t PDBFile::getNumDirectoryBytes() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000068 return ContainerLayout.SB->NumDirectoryBytes;
Zachary Turnere4a4f332016-07-22 19:56:33 +000069}
Zachary Turner0a43efe2016-04-25 17:38:08 +000070
Zachary Turnere4a4f332016-07-22 19:56:33 +000071uint32_t PDBFile::getBlockMapIndex() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000072 return ContainerLayout.SB->BlockMapAddr;
Zachary Turnere4a4f332016-07-22 19:56:33 +000073}
Zachary Turner0a43efe2016-04-25 17:38:08 +000074
Zachary Turnerd66889c2016-07-28 19:12:28 +000075uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000076
77uint32_t PDBFile::getNumDirectoryBlocks() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000078 return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes,
79 ContainerLayout.SB->BlockSize);
Zachary Turner0a43efe2016-04-25 17:38:08 +000080}
81
82uint64_t PDBFile::getBlockMapOffset() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000083 return (uint64_t)ContainerLayout.SB->BlockMapAddr *
84 ContainerLayout.SB->BlockSize;
Zachary Turner0a43efe2016-04-25 17:38:08 +000085}
86
Zachary Turnerd66889c2016-07-28 19:12:28 +000087uint32_t PDBFile::getNumStreams() const {
88 return ContainerLayout.StreamSizes.size();
89}
Zachary Turner0a43efe2016-04-25 17:38:08 +000090
91uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000092 return ContainerLayout.StreamSizes[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000093}
94
Zachary Turnerd8447992016-06-07 05:28:55 +000095ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +000096PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +000097 return ContainerLayout.StreamMap[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000098}
99
David Majnemer1b79e9a2016-07-10 05:32:05 +0000100uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000101
David Majnemer6211b1f2016-07-10 03:34:47 +0000102Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex,
103 uint32_t NumBytes) const {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000104 uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize());
Zachary Turner0a43efe2016-04-25 17:38:08 +0000105
Zachary Turnerb84faa82016-06-10 05:10:19 +0000106 ArrayRef<uint8_t> Result;
107 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
David Majnemer6211b1f2016-07-10 03:34:47 +0000108 return std::move(EC);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000109 return Result;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000110}
111
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000112Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
113 ArrayRef<uint8_t> Data) const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000114 return make_error<RawError>(raw_error_code::not_writable,
115 "PDBFile is immutable");
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000116}
117
Zachary Turner819e77d2016-05-06 20:51:57 +0000118Error PDBFile::parseFileHeaders() {
Zachary Turneraf299ea2017-02-25 00:44:30 +0000119 BinaryStreamReader Reader(*Buffer);
Zachary Turnerc59261c2016-05-25 03:53:16 +0000120
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000121 // Initialize SB.
Zachary Turnere4a4f332016-07-22 19:56:33 +0000122 const msf::SuperBlock *SB = nullptr;
Zachary Turnerb84faa82016-06-10 05:10:19 +0000123 if (auto EC = Reader.readObject(SB)) {
124 consumeError(std::move(EC));
Zachary Turner819e77d2016-05-06 20:51:57 +0000125 return make_error<RawError>(raw_error_code::corrupt_file,
126 "Does not contain superblock");
Zachary Turnerb84faa82016-06-10 05:10:19 +0000127 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000128
Zachary Turnere4a4f332016-07-22 19:56:33 +0000129 if (auto EC = msf::validateSuperBlock(*SB))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000130 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000131
Zachary Turnere4a4f332016-07-22 19:56:33 +0000132 if (Buffer->getLength() % SB->BlockSize != 0)
133 return make_error<RawError>(raw_error_code::corrupt_file,
134 "File size is not a multiple of block size");
Zachary Turnerd66889c2016-07-28 19:12:28 +0000135 ContainerLayout.SB = SB;
Zachary Turnere4a4f332016-07-22 19:56:33 +0000136
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000137 // Initialize Free Page Map.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000138 ContainerLayout.FreePageMap.resize(SB->NumBlocks);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000139 // The Fpm exists either at block 1 or block 2 of the MSF. However, this
140 // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and
141 // thusly an equal number of total blocks in the file. For a block size
142 // of 4KiB (very common), this would yield 32KiB total blocks in file, for a
143 // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so
144 // the Fpm is split across the file at `getBlockSize()` intervals. As a
145 // result, every block whose index is of the form |{1,2} + getBlockSize() * k|
146 // for any non-negative integer k is an Fpm block. In theory, we only really
147 // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but
148 // current versions of the MSF format already expect the Fpm to be arranged
149 // at getBlockSize() intervals, so we have to be compatible.
150 // See the function fpmPn() for more information:
151 // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489
Zachary Turner8cf51c32016-08-03 16:53:21 +0000152 auto FpmStream = MappedBlockStream::createFpmStream(ContainerLayout, *Buffer);
Zachary Turneraf299ea2017-02-25 00:44:30 +0000153 BinaryStreamReader FpmReader(*FpmStream);
Zachary Turner8cf51c32016-08-03 16:53:21 +0000154 ArrayRef<uint8_t> FpmBytes;
155 if (auto EC = FpmReader.readBytes(FpmBytes,
156 msf::getFullFpmByteSize(ContainerLayout)))
157 return EC;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000158 uint32_t BlocksRemaining = getBlockCount();
Zachary Turner8cf51c32016-08-03 16:53:21 +0000159 uint32_t BI = 0;
160 for (auto Byte : FpmBytes) {
161 uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U);
162 for (uint32_t I = 0; I < BlocksThisByte; ++I) {
163 if (Byte & (1 << I))
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000164 ContainerLayout.FreePageMap[BI] = true;
Zachary Turner8cf51c32016-08-03 16:53:21 +0000165 --BlocksRemaining;
166 ++BI;
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000167 }
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000168 }
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000169
Zachary Turnerab58ae82016-06-30 17:43:00 +0000170 Reader.setOffset(getBlockMapOffset());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000171 if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks,
172 getNumDirectoryBlocks()))
Zachary Turnerab58ae82016-06-30 17:43:00 +0000173 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000174
Zachary Turner819e77d2016-05-06 20:51:57 +0000175 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000176}
177
Zachary Turner819e77d2016-05-06 20:51:57 +0000178Error PDBFile::parseStreamData() {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000179 assert(ContainerLayout.SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000180 if (DirectoryStream)
181 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000182
Zachary Turner0a43efe2016-04-25 17:38:08 +0000183 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000184
Zachary Turnerd8447992016-06-07 05:28:55 +0000185 // Normally you can't use a MappedBlockStream without having fully parsed the
186 // PDB file, because it accesses the directory and various other things, which
187 // is exactly what we are attempting to parse. By specifying a custom
188 // subclass of IPDBStreamData which only accesses the fields that have already
189 // been parsed, we can avoid this and reuse MappedBlockStream.
Zachary Turnerd66889c2016-07-28 19:12:28 +0000190 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer);
Zachary Turneraf299ea2017-02-25 00:44:30 +0000191 BinaryStreamReader Reader(*DS);
192 if (auto EC = Reader.readInteger(NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000193 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000194
Zachary Turnerd66889c2016-07-28 19:12:28 +0000195 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000196 return EC;
197 for (uint32_t I = 0; I < NumStreams; ++I) {
Reid Kleckner5aba52f2016-06-22 22:42:24 +0000198 uint32_t StreamSize = getStreamByteSize(I);
199 // FIXME: What does StreamSize ~0U mean?
David Majnemer9efba742016-05-27 16:16:48 +0000200 uint64_t NumExpectedStreamBlocks =
Zachary Turnere4a4f332016-07-22 19:56:33 +0000201 StreamSize == UINT32_MAX
202 ? 0
Zachary Turnerd66889c2016-07-28 19:12:28 +0000203 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000204
205 // For convenience, we store the block array contiguously. This is because
206 // if someone calls setStreamMap(), it is more convenient to be able to call
207 // it with an ArrayRef instead of setting up a StreamRef. Since the
208 // DirectoryStream is cached in the class and thus lives for the life of the
209 // class, we can be guaranteed that readArray() will return a stable
210 // reference, even if it has to allocate from its internal pool.
211 ArrayRef<support::ulittle32_t> Blocks;
Zachary Turnerd8447992016-06-07 05:28:55 +0000212 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
213 return EC;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000214 for (uint32_t Block : Blocks) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000215 uint64_t BlockEndOffset =
216 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000217 if (BlockEndOffset > getFileSize())
218 return make_error<RawError>(raw_error_code::corrupt_file,
219 "Stream block map is corrupt.");
220 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000221 ContainerLayout.StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000222 }
223
Zachary Turner0a43efe2016-04-25 17:38:08 +0000224 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000225 assert(Reader.bytesRemaining() == 0);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000226 DirectoryStream = std::move(DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000227 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000228}
229
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000230ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000231 return ContainerLayout.DirectoryBlocks;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000232}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000233
Bob Haarman653baa22016-10-21 19:43:19 +0000234Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
235 if (!Globals) {
236 auto DbiS = getPDBDbiStream();
237 if (!DbiS)
238 return DbiS.takeError();
239
Bob Haarmana5b43582016-12-05 22:44:00 +0000240 auto GlobalS = safelyCreateIndexedStream(
Bob Haarman653baa22016-10-21 19:43:19 +0000241 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000242 if (!GlobalS)
243 return GlobalS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000244 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000245 if (auto EC = TempGlobals->reload())
246 return std::move(EC);
247 Globals = std::move(TempGlobals);
248 }
249 return *Globals;
250}
251
Zachary Turner819e77d2016-05-06 20:51:57 +0000252Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000253 if (!Info) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000254 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000255 if (!InfoS)
256 return InfoS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000257 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000258 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000259 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000260 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000261 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000262 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000263}
264
Zachary Turner819e77d2016-05-06 20:51:57 +0000265Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000266 if (!Dbi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000267 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000268 if (!DbiS)
269 return DbiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000270 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000271 if (auto EC = TempDbi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000272 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000273 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000274 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000275 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000276}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000277
Zachary Turner819e77d2016-05-06 20:51:57 +0000278Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000279 if (!Tpi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000280 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000281 if (!TpiS)
282 return TpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000283 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000284 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000285 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000286 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000287 }
288 return *Tpi;
289}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000290
Zachary Turnerc9972c62016-05-25 04:35:22 +0000291Expected<TpiStream &> PDBFile::getPDBIpiStream() {
292 if (!Ipi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000293 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000294 if (!IpiS)
295 return IpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000296 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000297 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000298 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000299 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000300 }
301 return *Ipi;
302}
303
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000304Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
305 if (!Publics) {
306 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000307 if (!DbiS)
308 return DbiS.takeError();
309
Bob Haarmana5b43582016-12-05 22:44:00 +0000310 auto PublicS = safelyCreateIndexedStream(
311 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000312 if (!PublicS)
313 return PublicS.takeError();
Zachary Turnera1657a92016-06-08 17:26:39 +0000314 auto TempPublics =
Bob Haarmana5b43582016-12-05 22:44:00 +0000315 llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000316 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000317 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000318 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000319 }
320 return *Publics;
321}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000322
323Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
324 if (!Symbols) {
325 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000326 if (!DbiS)
327 return DbiS.takeError();
328
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000329 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Bob Haarmana5b43582016-12-05 22:44:00 +0000330 auto SymbolS =
331 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000332 if (!SymbolS)
333 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000334
Bob Haarmana5b43582016-12-05 22:44:00 +0000335 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000336 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000337 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000338 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000339 }
340 return *Symbols;
341}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000342
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000343Expected<StringTable &> PDBFile::getStringTable() {
344 if (!Strings || !StringTableStream) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000345 auto IS = getPDBInfoStream();
346 if (!IS)
347 return IS.takeError();
348
349 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000350
Bob Haarmana5b43582016-12-05 22:44:00 +0000351 auto NS =
352 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000353 if (!NS)
354 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000355
Zachary Turneraf299ea2017-02-25 00:44:30 +0000356 BinaryStreamReader Reader(**NS);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000357 auto N = llvm::make_unique<StringTable>();
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000358 if (auto EC = N->load(Reader))
359 return std::move(EC);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000360 Strings = std::move(N);
Bob Haarmana5b43582016-12-05 22:44:00 +0000361 StringTableStream = std::move(*NS);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000362 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000363 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000364}
Bob Haarmana5b43582016-12-05 22:44:00 +0000365
366bool PDBFile::hasPDBDbiStream() const { return StreamDBI < getNumStreams(); }
367
368bool PDBFile::hasPDBGlobalsStream() {
369 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000370 if (!DbiS)
371 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000372 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
373}
374
375bool PDBFile::hasPDBInfoStream() { return StreamPDB < getNumStreams(); }
376
377bool PDBFile::hasPDBIpiStream() const { return StreamIPI < getNumStreams(); }
378
379bool PDBFile::hasPDBPublicsStream() {
380 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000381 if (!DbiS)
382 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000383 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
384}
385
386bool PDBFile::hasPDBSymbolStream() {
387 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000388 if (!DbiS)
389 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000390 return DbiS->getSymRecordStreamIndex() < getNumStreams();
391}
392
393bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
394
395bool PDBFile::hasStringTable() {
396 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000397 if (!IS)
398 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000399 return IS->getNamedStreamIndex("/names") < getNumStreams();
400}
401
402/// Wrapper around MappedBlockStream::createIndexedStream()
403/// that checks if a stream with that index actually exists.
404/// If it does not, the return value will have an MSFError with
405/// code msf_error_code::no_stream. Else, the return value will
406/// contain the stream returned by createIndexedStream().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000407Expected<std::unique_ptr<MappedBlockStream>>
408PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
Zachary Turneraf299ea2017-02-25 00:44:30 +0000409 BinaryStreamRef MsfData,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000410 uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000411 if (StreamIndex >= getNumStreams())
412 return make_error<RawError>(raw_error_code::no_stream);
413 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex);
414}