blob: 6186a47700106de457270a6f92105cb4f257875a [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
10#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000011
Zachary Turner0a43efe2016-04-25 17:38:08 +000012#include "llvm/ADT/ArrayRef.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000013#include "llvm/DebugInfo/CodeView/StreamArray.h"
Zachary Turnerb84faa82016-06-10 05:10:19 +000014#include "llvm/DebugInfo/CodeView/StreamInterface.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000015#include "llvm/DebugInfo/CodeView/StreamReader.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000016#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
Zachary Turnera1657a92016-06-08 17:26:39 +000017#include "llvm/DebugInfo/PDB/Raw/DirectoryStreamData.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000018#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000019#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner3df1bfa2016-06-03 05:52:57 +000020#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000021#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000022#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000023#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000024#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/MemoryBuffer.h"
27
28using namespace llvm;
Zachary Turnerb84faa82016-06-10 05:10:19 +000029using namespace llvm::codeview;
Zachary Turner2f09b502016-04-29 17:28:47 +000030using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000031
32namespace {
Zachary Turnerb84faa82016-06-10 05:10:19 +000033typedef FixedStreamArray<support::ulittle32_t> ulittle_array;
Zachary Turner0a43efe2016-04-25 17:38:08 +000034}
35
Zachary Turnerb84faa82016-06-10 05:10:19 +000036PDBFile::PDBFile(std::unique_ptr<StreamInterface> PdbFileBuffer)
37 : Buffer(std::move(PdbFileBuffer)), SB(nullptr) {}
Zachary Turner0a43efe2016-04-25 17:38:08 +000038
39PDBFile::~PDBFile() {}
40
Zachary Turnerb84faa82016-06-10 05:10:19 +000041uint32_t PDBFile::getBlockSize() const { return SB->BlockSize; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000042
Zachary Turnerb84faa82016-06-10 05:10:19 +000043uint32_t PDBFile::getUnknown0() const { return SB->Unknown0; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000044
Zachary Turnerb84faa82016-06-10 05:10:19 +000045uint32_t PDBFile::getBlockCount() const { return SB->NumBlocks; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000046
Zachary Turnerb84faa82016-06-10 05:10:19 +000047uint32_t PDBFile::getNumDirectoryBytes() const { return SB->NumDirectoryBytes; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000048
Zachary Turnerb84faa82016-06-10 05:10:19 +000049uint32_t PDBFile::getBlockMapIndex() const { return SB->BlockMapAddr; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000050
Zachary Turnerb84faa82016-06-10 05:10:19 +000051uint32_t PDBFile::getUnknown1() const { return SB->Unknown1; }
Zachary Turner0a43efe2016-04-25 17:38:08 +000052
53uint32_t PDBFile::getNumDirectoryBlocks() const {
Zachary Turnerb84faa82016-06-10 05:10:19 +000054 return bytesToBlocks(SB->NumDirectoryBytes, SB->BlockSize);
Zachary Turner0a43efe2016-04-25 17:38:08 +000055}
56
57uint64_t PDBFile::getBlockMapOffset() const {
Zachary Turnerb84faa82016-06-10 05:10:19 +000058 return (uint64_t)SB->BlockMapAddr * SB->BlockSize;
Zachary Turner0a43efe2016-04-25 17:38:08 +000059}
60
Zachary Turnerb84faa82016-06-10 05:10:19 +000061uint32_t PDBFile::getNumStreams() const { return StreamSizes.size(); }
Zachary Turner0a43efe2016-04-25 17:38:08 +000062
63uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
Zachary Turnerb84faa82016-06-10 05:10:19 +000064 return StreamSizes[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000065}
66
Zachary Turnerd8447992016-06-07 05:28:55 +000067ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +000068PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerb84faa82016-06-10 05:10:19 +000069 return StreamMap[StreamIndex];
Zachary Turner0a43efe2016-04-25 17:38:08 +000070}
71
Zachary Turnere6fee882016-06-07 20:38:37 +000072ArrayRef<uint8_t> PDBFile::getBlockData(uint32_t BlockIndex,
73 uint32_t NumBytes) const {
Zachary Turner0a43efe2016-04-25 17:38:08 +000074 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
75
Zachary Turnerb84faa82016-06-10 05:10:19 +000076 ArrayRef<uint8_t> Result;
77 if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result))
78 consumeError(std::move(EC));
79 return Result;
Zachary Turner0a43efe2016-04-25 17:38:08 +000080}
81
Zachary Turner5acb4ac2016-06-10 05:09:12 +000082Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset,
83 ArrayRef<uint8_t> Data) const {
Zachary Turnerb84faa82016-06-10 05:10:19 +000084 if (Offset >= getBlockSize())
85 return make_error<RawError>(
86 raw_error_code::invalid_block_address,
87 "setBlockData attempted to write out of block bounds.");
88 if (Data.size() >= getBlockSize() - Offset)
89 return make_error<RawError>(
90 raw_error_code::invalid_block_address,
91 "setBlockData attempted to write out of block bounds.");
Zachary Turner5acb4ac2016-06-10 05:09:12 +000092
Zachary Turnerb84faa82016-06-10 05:10:19 +000093 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
94 StreamBlockOffset += Offset;
95 return Buffer->writeBytes(StreamBlockOffset, Data);
Zachary Turner5acb4ac2016-06-10 05:09:12 +000096}
97
Zachary Turner819e77d2016-05-06 20:51:57 +000098Error PDBFile::parseFileHeaders() {
Zachary Turnerb84faa82016-06-10 05:10:19 +000099 StreamReader Reader(*Buffer);
Zachary Turnerc59261c2016-05-25 03:53:16 +0000100
Zachary Turnerb84faa82016-06-10 05:10:19 +0000101 if (auto EC = Reader.readObject(SB)) {
102 consumeError(std::move(EC));
Zachary Turner819e77d2016-05-06 20:51:57 +0000103 return make_error<RawError>(raw_error_code::corrupt_file,
104 "Does not contain superblock");
Zachary Turnerb84faa82016-06-10 05:10:19 +0000105 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000106
Zachary Turner819e77d2016-05-06 20:51:57 +0000107 // Check the magic bytes.
Zachary Turnerb84faa82016-06-10 05:10:19 +0000108 if (memcmp(SB->MagicBytes, MsfMagic, sizeof(MsfMagic)) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000109 return make_error<RawError>(raw_error_code::corrupt_file,
110 "MSF magic header doesn't match");
111
David Majnemer878cadb2016-05-27 15:57:38 +0000112 // We don't support blocksizes which aren't a multiple of four bytes.
113 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000114 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000115 "Block size is not multiple of 4.");
Zachary Turner819e77d2016-05-06 20:51:57 +0000116
Zachary Turner9213ba52016-04-29 18:09:19 +0000117 switch (SB->BlockSize) {
118 case 512: case 1024: case 2048: case 4096:
119 break;
120 default:
121 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000122 return make_error<RawError>(raw_error_code::corrupt_file,
123 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000124 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000125
Zachary Turnerb84faa82016-06-10 05:10:19 +0000126 if (Buffer->getLength() % SB->BlockSize != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000127 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000128 "File size is not a multiple of block size");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000129
130 // We don't support directories whose sizes aren't a multiple of four bytes.
131 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000132 return make_error<RawError>(raw_error_code::corrupt_file,
133 "Directory size is not multiple of 4.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000134
135 // The number of blocks which comprise the directory is a simple function of
136 // the number of bytes it contains.
137 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
138
Zachary Turnerb84faa82016-06-10 05:10:19 +0000139 // The directory, as we understand it, is a block which consists of a list of
140 // block numbers. It is unclear what would happen if the number of blocks
141 // couldn't fit on a single block.
Zachary Turner0a43efe2016-04-25 17:38:08 +0000142 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000143 return make_error<RawError>(raw_error_code::corrupt_file,
144 "Too many directory blocks.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000145
Zachary Turner819e77d2016-05-06 20:51:57 +0000146 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000147}
148
Zachary Turner819e77d2016-05-06 20:51:57 +0000149Error PDBFile::parseStreamData() {
Zachary Turnerb84faa82016-06-10 05:10:19 +0000150 assert(SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000151 if (DirectoryStream)
152 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000153
Zachary Turner0a43efe2016-04-25 17:38:08 +0000154 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000155
Zachary Turnerd8447992016-06-07 05:28:55 +0000156 // Normally you can't use a MappedBlockStream without having fully parsed the
157 // PDB file, because it accesses the directory and various other things, which
158 // is exactly what we are attempting to parse. By specifying a custom
159 // subclass of IPDBStreamData which only accesses the fields that have already
160 // been parsed, we can avoid this and reuse MappedBlockStream.
Zachary Turnera1657a92016-06-08 17:26:39 +0000161 auto DS = MappedBlockStream::createDirectoryStream(*this);
162 if (!DS)
163 return DS.takeError();
Zachary Turnerb84faa82016-06-10 05:10:19 +0000164 StreamReader Reader(**DS);
Zachary Turnerd8447992016-06-07 05:28:55 +0000165 if (auto EC = Reader.readInteger(NumStreams))
166 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000167
Zachary Turnerb84faa82016-06-10 05:10:19 +0000168 if (auto EC = Reader.readArray(StreamSizes, NumStreams))
Zachary Turnerd8447992016-06-07 05:28:55 +0000169 return EC;
170 for (uint32_t I = 0; I < NumStreams; ++I) {
David Majnemer9efba742016-05-27 16:16:48 +0000171 uint64_t NumExpectedStreamBlocks =
Zachary Turnerd8447992016-06-07 05:28:55 +0000172 bytesToBlocks(getStreamByteSize(I), SB->BlockSize);
Zachary Turnerb84faa82016-06-10 05:10:19 +0000173
174 // For convenience, we store the block array contiguously. This is because
175 // if someone calls setStreamMap(), it is more convenient to be able to call
176 // it with an ArrayRef instead of setting up a StreamRef. Since the
177 // DirectoryStream is cached in the class and thus lives for the life of the
178 // class, we can be guaranteed that readArray() will return a stable
179 // reference, even if it has to allocate from its internal pool.
180 ArrayRef<support::ulittle32_t> Blocks;
Zachary Turnerd8447992016-06-07 05:28:55 +0000181 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
182 return EC;
Zachary Turnerb84faa82016-06-10 05:10:19 +0000183 StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000184 }
185
Zachary Turner0a43efe2016-04-25 17:38:08 +0000186 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000187 assert(Reader.bytesRemaining() == 0);
Zachary Turnera1657a92016-06-08 17:26:39 +0000188 DirectoryStream = std::move(*DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000189 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000190}
191
Zachary Turnerd8447992016-06-07 05:28:55 +0000192llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turnerb84faa82016-06-10 05:10:19 +0000193 StreamReader Reader(*Buffer);
194 Reader.setOffset(getBlockMapOffset());
195 llvm::ArrayRef<support::ulittle32_t> Result;
196 if (auto EC = Reader.readArray(Result, getNumDirectoryBlocks()))
197 consumeError(std::move(EC));
198 return Result;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000199}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000200
Zachary Turner819e77d2016-05-06 20:51:57 +0000201Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000202 if (!Info) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000203 auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this);
204 if (!InfoS)
205 return InfoS.takeError();
206 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
207 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000208 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000209 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000210 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000211 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000212}
213
Zachary Turner819e77d2016-05-06 20:51:57 +0000214Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000215 if (!Dbi) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000216 auto DbiS = MappedBlockStream::createIndexedStream(StreamDBI, *this);
217 if (!DbiS)
218 return DbiS.takeError();
219 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
220 if (auto EC = TempDbi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000221 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000222 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000223 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000224 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000225}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000226
Zachary Turner819e77d2016-05-06 20:51:57 +0000227Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000228 if (!Tpi) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000229 auto TpiS = MappedBlockStream::createIndexedStream(StreamTPI, *this);
230 if (!TpiS)
231 return TpiS.takeError();
232 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
233 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000234 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000235 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000236 }
237 return *Tpi;
238}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000239
Zachary Turnerc9972c62016-05-25 04:35:22 +0000240Expected<TpiStream &> PDBFile::getPDBIpiStream() {
241 if (!Ipi) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000242 auto IpiS = MappedBlockStream::createIndexedStream(StreamIPI, *this);
243 if (!IpiS)
244 return IpiS.takeError();
245 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
246 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000247 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000248 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000249 }
250 return *Ipi;
251}
252
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000253Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
254 if (!Publics) {
255 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000256 if (!DbiS)
257 return DbiS.takeError();
258
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000259 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
260
Zachary Turnera1657a92016-06-08 17:26:39 +0000261 auto PublicS =
262 MappedBlockStream::createIndexedStream(PublicsStreamNum, *this);
263 if (!PublicS)
264 return PublicS.takeError();
265 auto TempPublics =
266 llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
267 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000268 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000269 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000270 }
271 return *Publics;
272}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000273
274Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
275 if (!Symbols) {
276 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000277 if (!DbiS)
278 return DbiS.takeError();
279
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000280 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
281
Zachary Turnera1657a92016-06-08 17:26:39 +0000282 auto SymbolS =
283 MappedBlockStream::createIndexedStream(SymbolStreamNum, *this);
284 if (!SymbolS)
285 return SymbolS.takeError();
286 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
287 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000288 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000289 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000290 }
291 return *Symbols;
292}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000293
294Expected<NameHashTable &> PDBFile::getStringTable() {
295 if (!StringTable || !StringTableStream) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000296 auto IS = getPDBInfoStream();
297 if (!IS)
298 return IS.takeError();
299
300 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000301
302 if (NameStreamIndex == 0)
303 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000304 if (NameStreamIndex >= getNumStreams())
305 return make_error<RawError>(raw_error_code::no_stream);
306
Zachary Turnera1657a92016-06-08 17:26:39 +0000307 auto NS = MappedBlockStream::createIndexedStream(NameStreamIndex, *this);
308 if (!NS)
309 return NS.takeError();
310
Zachary Turnerb84faa82016-06-10 05:10:19 +0000311 StreamReader Reader(**NS);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000312 auto N = llvm::make_unique<NameHashTable>();
313 if (auto EC = N->load(Reader))
314 return std::move(EC);
315 StringTable = std::move(N);
Zachary Turnera1657a92016-06-08 17:26:39 +0000316 StringTableStream = std::move(*NS);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000317 }
318 return *StringTable;
319}