blob: de460d03e6b6c300d745781311a6659eed025051 [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;
Zachary Turner9fb9d712017-08-02 22:31:39 +0000153 if (auto EC = FpmReader.readBytes(FpmBytes, FpmReader.bytesRemaining()))
Zachary Turner8cf51c32016-08-03 16:53:21 +0000154 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 Turner5b74ff32017-06-03 00:33:35 +0000187 auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer,
188 Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000189 BinaryStreamReader Reader(*DS);
Zachary Turner695ed562017-02-28 00:04:07 +0000190 if (auto EC = Reader.readInteger(NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000191 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000192
Zachary Turnerd66889c2016-07-28 19:12:28 +0000193 if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000194 return EC;
195 for (uint32_t I = 0; I < NumStreams; ++I) {
Reid Kleckner5aba52f2016-06-22 22:42:24 +0000196 uint32_t StreamSize = getStreamByteSize(I);
197 // FIXME: What does StreamSize ~0U mean?
David Majnemer9efba742016-05-27 16:16:48 +0000198 uint64_t NumExpectedStreamBlocks =
Zachary Turnere4a4f332016-07-22 19:56:33 +0000199 StreamSize == UINT32_MAX
200 ? 0
Zachary Turnerd66889c2016-07-28 19:12:28 +0000201 : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000202
203 // For convenience, we store the block array contiguously. This is because
204 // if someone calls setStreamMap(), it is more convenient to be able to call
205 // it with an ArrayRef instead of setting up a StreamRef. Since the
206 // DirectoryStream is cached in the class and thus lives for the life of the
207 // class, we can be guaranteed that readArray() will return a stable
208 // reference, even if it has to allocate from its internal pool.
209 ArrayRef<support::ulittle32_t> Blocks;
Zachary Turnerd8447992016-06-07 05:28:55 +0000210 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
211 return EC;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000212 for (uint32_t Block : Blocks) {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000213 uint64_t BlockEndOffset =
214 (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize;
David Majnemer1b79e9a2016-07-10 05:32:05 +0000215 if (BlockEndOffset > getFileSize())
216 return make_error<RawError>(raw_error_code::corrupt_file,
217 "Stream block map is corrupt.");
218 }
Zachary Turnerd66889c2016-07-28 19:12:28 +0000219 ContainerLayout.StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000220 }
221
Zachary Turner0a43efe2016-04-25 17:38:08 +0000222 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000223 assert(Reader.bytesRemaining() == 0);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000224 DirectoryStream = std::move(DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000225 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000226}
227
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000228ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerd66889c2016-07-28 19:12:28 +0000229 return ContainerLayout.DirectoryBlocks;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000230}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000231
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000232MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const {
233 MSFStreamLayout Result;
234 auto Blocks = getStreamBlockList(StreamIdx);
235 Result.Blocks.assign(Blocks.begin(), Blocks.end());
236 Result.Length = getStreamByteSize(StreamIdx);
237 return Result;
238}
239
Zachary Turnerc3d8eec2017-08-02 22:25:52 +0000240msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const {
241 return msf::getFpmStreamLayout(ContainerLayout);
242}
243
Bob Haarman653baa22016-10-21 19:43:19 +0000244Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() {
245 if (!Globals) {
246 auto DbiS = getPDBDbiStream();
247 if (!DbiS)
248 return DbiS.takeError();
249
Bob Haarmana5b43582016-12-05 22:44:00 +0000250 auto GlobalS = safelyCreateIndexedStream(
Bob Haarman653baa22016-10-21 19:43:19 +0000251 ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000252 if (!GlobalS)
253 return GlobalS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000254 auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS));
Bob Haarman653baa22016-10-21 19:43:19 +0000255 if (auto EC = TempGlobals->reload())
256 return std::move(EC);
257 Globals = std::move(TempGlobals);
258 }
259 return *Globals;
260}
261
Zachary Turner819e77d2016-05-06 20:51:57 +0000262Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000263 if (!Info) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000264 auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000265 if (!InfoS)
266 return InfoS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000267 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000268 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000269 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000270 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000271 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000272 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000273}
274
Zachary Turner819e77d2016-05-06 20:51:57 +0000275Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000276 if (!Dbi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000277 auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000278 if (!DbiS)
279 return DbiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000280 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000281 if (auto EC = TempDbi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000282 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000283 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000284 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000285 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000286}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000287
Zachary Turner819e77d2016-05-06 20:51:57 +0000288Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000289 if (!Tpi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000290 auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000291 if (!TpiS)
292 return TpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000293 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000294 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000295 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000296 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000297 }
298 return *Tpi;
299}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000300
Zachary Turnerc9972c62016-05-25 04:35:22 +0000301Expected<TpiStream &> PDBFile::getPDBIpiStream() {
302 if (!Ipi) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000303 auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000304 if (!IpiS)
305 return IpiS.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000306 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000307 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000308 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000309 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000310 }
311 return *Ipi;
312}
313
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000314Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
315 if (!Publics) {
316 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000317 if (!DbiS)
318 return DbiS.takeError();
319
Bob Haarmana5b43582016-12-05 22:44:00 +0000320 auto PublicS = safelyCreateIndexedStream(
321 ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000322 if (!PublicS)
323 return PublicS.takeError();
Reid Kleckner14d90fd2017-07-26 00:40:36 +0000324 auto TempPublics = llvm::make_unique<PublicsStream>(std::move(*PublicS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000325 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000326 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000327 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000328 }
329 return *Publics;
330}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000331
332Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
333 if (!Symbols) {
334 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000335 if (!DbiS)
336 return DbiS.takeError();
337
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000338 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
Bob Haarmana5b43582016-12-05 22:44:00 +0000339 auto SymbolS =
340 safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000341 if (!SymbolS)
342 return SymbolS.takeError();
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000343
Bob Haarmana5b43582016-12-05 22:44:00 +0000344 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
Zachary Turnera1657a92016-06-08 17:26:39 +0000345 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000346 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000347 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000348 }
349 return *Symbols;
350}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000351
Zachary Turnere204a6c2017-05-02 18:00:13 +0000352Expected<PDBStringTable &> PDBFile::getStringTable() {
Zachary Turnerc504ae32017-05-03 15:58:37 +0000353 if (!Strings) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000354 auto IS = getPDBInfoStream();
355 if (!IS)
356 return IS.takeError();
357
358 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000359
Bob Haarmana5b43582016-12-05 22:44:00 +0000360 auto NS =
361 safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000362 if (!NS)
363 return NS.takeError();
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000364
Daniel Jasperdff096f2017-05-03 07:29:25 +0000365 auto N = llvm::make_unique<PDBStringTable>();
Zachary Turnerc504ae32017-05-03 15:58:37 +0000366 BinaryStreamReader Reader(**NS);
367 if (auto EC = N->reload(Reader))
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000368 return std::move(EC);
Zachary Turnerc504ae32017-05-03 15:58:37 +0000369 assert(Reader.bytesRemaining() == 0);
370 StringTableStream = std::move(*NS);
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000371 Strings = std::move(N);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000372 }
Zachary Turnerf04d6e82017-01-20 22:41:15 +0000373 return *Strings;
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000374}
Bob Haarmana5b43582016-12-05 22:44:00 +0000375
Zachary Turner4e950642017-06-15 23:56:19 +0000376uint32_t PDBFile::getPointerSize() {
377 auto DbiS = getPDBDbiStream();
378 if (!DbiS)
379 return 0;
380 PDB_Machine Machine = DbiS->getMachineType();
381 if (Machine == PDB_Machine::Amd64)
382 return 8;
383 return 4;
384}
385
Bob Haarmana5b43582016-12-05 22:44:00 +0000386bool PDBFile::hasPDBDbiStream() const { return StreamDBI < getNumStreams(); }
387
388bool PDBFile::hasPDBGlobalsStream() {
389 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000390 if (!DbiS) {
391 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000392 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000393 }
394
Bob Haarmana5b43582016-12-05 22:44:00 +0000395 return DbiS->getGlobalSymbolStreamIndex() < getNumStreams();
396}
397
398bool PDBFile::hasPDBInfoStream() { return StreamPDB < getNumStreams(); }
399
400bool PDBFile::hasPDBIpiStream() const { return StreamIPI < getNumStreams(); }
401
402bool PDBFile::hasPDBPublicsStream() {
403 auto DbiS = getPDBDbiStream();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000404 if (!DbiS) {
405 consumeError(DbiS.takeError());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000406 return false;
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000407 }
Bob Haarmana5b43582016-12-05 22:44:00 +0000408 return DbiS->getPublicSymbolStreamIndex() < getNumStreams();
409}
410
411bool PDBFile::hasPDBSymbolStream() {
412 auto DbiS = getPDBDbiStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000413 if (!DbiS)
414 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000415 return DbiS->getSymRecordStreamIndex() < getNumStreams();
416}
417
418bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); }
419
Zachary Turnere204a6c2017-05-02 18:00:13 +0000420bool PDBFile::hasPDBStringTable() {
Bob Haarmana5b43582016-12-05 22:44:00 +0000421 auto IS = getPDBInfoStream();
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000422 if (!IS)
423 return false;
Bob Haarmana5b43582016-12-05 22:44:00 +0000424 return IS->getNamedStreamIndex("/names") < getNumStreams();
425}
426
Zachary Turnerd0b44fa2017-02-28 17:49:34 +0000427/// Wrapper around MappedBlockStream::createIndexedStream() that checks if a
428/// stream with that index actually exists. If it does not, the return value
429/// will have an MSFError with code msf_error_code::no_stream. Else, the return
430/// value will contain the stream returned by createIndexedStream().
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000431Expected<std::unique_ptr<MappedBlockStream>>
432PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000433 BinaryStreamRef MsfData,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000434 uint32_t StreamIndex) const {
Bob Haarmana5b43582016-12-05 22:44:00 +0000435 if (StreamIndex >= getNumStreams())
436 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turner5b74ff32017-06-03 00:33:35 +0000437 return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex,
438 Allocator);
Bob Haarmana5b43582016-12-05 22:44:00 +0000439}