blob: a1f8786ff12fbf2f1be94e7a1cdf28f66ed0ccfd [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
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
Zachary Turnerd1de2f42017-08-21 14:53:25 +0000237std::unique_ptr<MappedBlockStream> PDBFile::createIndexedStream(uint16_t SN) {
238 if (SN == kInvalidStreamIndex)
239 return nullptr;
240 return MappedBlockStream::createIndexedStream(ContainerLayout, *Buffer, SN,
241 Allocator);
242}
243
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000244MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
245 MSFStreamLayout Result;
246 auto Blocks = getStreamBlockList(StreamIdx);
247 Result.Blocks.assign(Blocks.begin(), Blocks.end());
248 Result.Length = getStreamByteSize(StreamIdx);
249 return Result;
250}
251
Zachary Turnerc3d8eec2017-08-02 22:25:52 +0000252msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
253 return msf::getFpmStreamLayout(ContainerLayout);
254}
255
Bob Haarman653baa22016-10-21 19:43:19 +0000256Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
257 if (!Globals) {
258 auto DbiS = getPDBDbiStream();
259 if (!DbiS)
260 return DbiS.takeError();
261
Bob Haarmana5b43582016-12-05 22:44:00 +0000262 auto GlobalS = safelyCreateIndexedStream(
Bob Haarman653baa22016-10-21 19:43:19 +0000263 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000264 if (!GlobalS)
265 return GlobalS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000266 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000267 if (auto EC = TempGlobals->reload())
268 return std::move(EC);
269 Globals = std::move(TempGlobals);
270 }
271 return *Globals;
272}
273
Zachary Turner819e77d2016-05-06 20:51:57 +0000274Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000275 if (!Info) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000276 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000277 if (!InfoS)
278 return InfoS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000279 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000280 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000281 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000282 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000283 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000284 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000285}
286
Zachary Turner819e77d2016-05-06 20:51:57 +0000287Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000288 if (!Dbi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000289 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000290 if (!DbiS)
291 return DbiS.takeError();
Zachary Turner15b2bdf2018-04-04 17:29:09 +0000292 auto TempDbi = llvm::make_unique<DbiStream>(std::move(*DbiS));
293 if (auto EC = TempDbi->reload(this))
Zachary Turner819e77d2016-05-06 20:51:57 +0000294 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000295 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000296 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000297 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000298}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000299
Zachary Turner819e77d2016-05-06 20:51:57 +0000300Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000301 if (!Tpi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000302 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000303 if (!TpiS)
304 return TpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000305 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000306 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000307 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000308 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000309 }
310 return *Tpi;
311}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000312
Zachary Turnerc9972c62016-05-25 04:35:22 +0000313Expected<TpiStream &> PDBFile::getPDBIpiStream() {
314 if (!Ipi) {
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000315 if (!hasPDBIpiStream())
316 return make_error<RawError>(raw_error_code::no_stream);
317
Bob Haarmana5b43582016-12-05 22:44:00 +0000318 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000319 if (!IpiS)
320 return IpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000321 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000322 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000323 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000324 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000325 }
326 return *Ipi;
327}
328
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000329Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
330 if (!Publics) {
331 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000332 if (!DbiS)
333 return DbiS.takeError();
334
Bob Haarmana5b43582016-12-05 22:44:00 +0000335 auto PublicS = safelyCreateIndexedStream(
336 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000337 if (!PublicS)
338 return PublicS.takeError();
Reid Kleckner14d90fd2017-07-26 00:40:36 +0000339 auto TempPublics = llvm::make_unique<PublicsStream>(std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000340 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000341 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000342 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000343 }
344 return *Publics;
345}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000346
347Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
348 if (!Symbols) {
349 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000350 if (!DbiS)
351 return DbiS.takeError();
352
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000353 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Bob Haarmana5b43582016-12-05 22:44:00 +0000354 auto SymbolS =
355 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000356 if (!SymbolS)
357 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000358
Bob Haarmana5b43582016-12-05 22:44:00 +0000359 auto TempSymbols = llvm::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) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000369 auto IS = getPDBInfoStream();
370 if (!IS)
371 return IS.takeError();
372
Zachary Turnerd11328a2018-04-02 18:35:21 +0000373 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names");
374 if (!ExpectedNSI)
375 return ExpectedNSI.takeError();
376 uint32_t NameStreamIndex = *ExpectedNSI;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000377
Bob Haarmana5b43582016-12-05 22:44:00 +0000378 auto NS =
379 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000380 if (!NS)
381 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000382
Daniel Jasperdff096f2017-05-03 07:29:25 +0000383 auto N = llvm::make_unique<PDBStringTable>();
Zachary Turnerc504ae32017-05-03 15:58:37 +0000384 BinaryStreamReader Reader(**NS);
385 if (auto EC = N->reload(Reader))
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000386 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +0000387 assert(Reader.bytesRemaining() == 0);
388 StringTableStream = std::move(*NS);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000389 Strings = std::move(N);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000390 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000391 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000392}
Bob Haarmana5b43582016-12-05 22:44:00 +0000393
Zachary Turner4e950642017-06-15 23:56:19 +0000394uint32_t PDBFile::getPointerSize() {
395 auto DbiS = getPDBDbiStream();
396 if (!DbiS)
397 return 0;
398 PDB_Machine Machine = DbiS->getMachineType();
399 if (Machine == PDB_Machine::Amd64)
400 return 8;
401 return 4;
402}
403
Alexandre Ganea741cc352018-08-06 19:35:00 +0000404bool PDBFile::hasPDBDbiStream() const {
405 return StreamDBI < getNumStreams() && getStreamByteSize(StreamDBI) > 0;
406}
Bob Haarmana5b43582016-12-05 22:44:00 +0000407
408bool PDBFile::hasPDBGlobalsStream() {
409 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000410 if (!DbiS) {
411 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000412 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000413 }
414
Bob Haarmana5b43582016-12-05 22:44:00 +0000415 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
416}
417
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000418bool PDBFile::hasPDBInfoStream() const { return StreamPDB < getNumStreams(); }
Bob Haarmana5b43582016-12-05 22:44:00 +0000419
Zachary Turnerabb17cc2017-09-01 20:06:56 +0000420bool PDBFile::hasPDBIpiStream() const {
421 if (!hasPDBInfoStream())
422 return false;
423
424 if (StreamIPI >= getNumStreams())
425 return false;
426
427 auto &InfoStream = cantFail(const_cast<PDBFile *>(this)->getPDBInfoStream());
428 return InfoStream.containsIdStream();
429}
Bob Haarmana5b43582016-12-05 22:44:00 +0000430
431bool PDBFile::hasPDBPublicsStream() {
432 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000433 if (!DbiS) {
434 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000435 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000436 }
Bob Haarmana5b43582016-12-05 22:44:00 +0000437 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
438}
439
440bool PDBFile::hasPDBSymbolStream() {
441 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000442 if (!DbiS)
443 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000444 return DbiS->getSymRecordStreamIndex() < getNumStreams();
445}
446
447bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
448
Zachary Turnere204a6c2017-05-02 18:00:13 +0000449bool PDBFile::hasPDBStringTable() {
Bob Haarmana5b43582016-12-05 22:44:00 +0000450 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000451 if (!IS)
452 return false;
Zachary Turnerd11328a2018-04-02 18:35:21 +0000453 Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names");
454 if (!ExpectedNSI) {
455 consumeError(ExpectedNSI.takeError());
456 return false;
457 }
458 assert(*ExpectedNSI < getNumStreams());
459 return true;
Bob Haarmana5b43582016-12-05 22:44:00 +0000460}
461
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000462/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
463/// stream with that index actually exists. If it does not, the return value
464/// will have an MSFError with code msf_error_code::no_stream. Else, the return
465/// value will contain the stream returned by createIndexedStream().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000466Expected<std::unique_ptr<MappedBlockStream>>
467PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000468 BinaryStreamRef MsfData,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000469 uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000470 if (StreamIndex >= getNumStreams())
471 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turner5b74ff32017-06-03 00:33:35 +0000472 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex,
473 Allocator);
Bob Haarmana5b43582016-12-05 22:44:00 +0000474}