blob: 086105b4dedecbbb577ae03f9c568bb2aa304633 [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"
14#include "llvm/DebugInfo/CodeView/StreamReader.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000015#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
Zachary Turnera1657a92016-06-08 17:26:39 +000016#include "llvm/DebugInfo/PDB/Raw/DirectoryStreamData.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000017#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000018#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner3df1bfa2016-06-03 05:52:57 +000019#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000020#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000021#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000022#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000023#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/MemoryBuffer.h"
26
27using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000028using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000029
30namespace {
31static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f',
32 't', ' ', 'C', '/', 'C', '+', '+', ' ',
33 'M', 'S', 'F', ' ', '7', '.', '0', '0',
34 '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
35
36// The superblock is overlaid at the beginning of the file (offset 0).
37// It starts with a magic header and is followed by information which describes
38// the layout of the file system.
39struct SuperBlock {
40 char MagicBytes[sizeof(Magic)];
41 // The file system is split into a variable number of fixed size elements.
42 // These elements are referred to as blocks. The size of a block may vary
43 // from system to system.
44 support::ulittle32_t BlockSize;
45 // This field's purpose is not yet known.
46 support::ulittle32_t Unknown0;
47 // This contains the number of blocks resident in the file system. In
48 // practice, NumBlocks * BlockSize is equivalent to the size of the PDB file.
49 support::ulittle32_t NumBlocks;
50 // This contains the number of bytes which make up the directory.
51 support::ulittle32_t NumDirectoryBytes;
52 // This field's purpose is not yet known.
53 support::ulittle32_t Unknown1;
54 // This contains the block # of the block map.
55 support::ulittle32_t BlockMapAddr;
56};
Zachary Turnerd8447992016-06-07 05:28:55 +000057
Zachary Turnerd8447992016-06-07 05:28:55 +000058typedef codeview::FixedStreamArray<support::ulittle32_t> ulittle_array;
Zachary Turner0a43efe2016-04-25 17:38:08 +000059}
60
Zachary Turner2f09b502016-04-29 17:28:47 +000061struct llvm::pdb::PDBFileContext {
Zachary Turner0a43efe2016-04-25 17:38:08 +000062 std::unique_ptr<MemoryBuffer> Buffer;
63 const SuperBlock *SB;
Zachary Turnerd8447992016-06-07 05:28:55 +000064 ArrayRef<support::ulittle32_t> StreamSizes;
65 std::vector<ulittle_array> StreamMap;
Zachary Turner0a43efe2016-04-25 17:38:08 +000066};
67
Zachary Turner819e77d2016-05-06 20:51:57 +000068static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
69 const uint64_t Size) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000070 if (Addr + Size < Addr || Addr + Size < Size ||
71 Addr + Size > uintptr_t(M.getBufferEnd()) ||
72 Addr < uintptr_t(M.getBufferStart())) {
Zachary Turner819e77d2016-05-06 20:51:57 +000073 return make_error<RawError>(raw_error_code::corrupt_file,
74 "Invalid buffer address");
Zachary Turner0a43efe2016-04-25 17:38:08 +000075 }
Zachary Turner819e77d2016-05-06 20:51:57 +000076 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +000077}
78
79template <typename T>
Zachary Turner819e77d2016-05-06 20:51:57 +000080static Error checkOffset(MemoryBufferRef M, ArrayRef<T> AR) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000081 return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T));
82}
83
84PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) {
85 Context.reset(new PDBFileContext());
86 Context->Buffer = std::move(MemBuffer);
87}
88
89PDBFile::~PDBFile() {}
90
91uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; }
92
93uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; }
94
95uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; }
96
97uint32_t PDBFile::getNumDirectoryBytes() const {
98 return Context->SB->NumDirectoryBytes;
99}
100
101uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; }
102
103uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; }
104
105uint32_t PDBFile::getNumDirectoryBlocks() const {
106 return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize);
107}
108
109uint64_t PDBFile::getBlockMapOffset() const {
110 return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize;
111}
112
113uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); }
114
115uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
116 return Context->StreamSizes[StreamIndex];
117}
118
Zachary Turnerd8447992016-06-07 05:28:55 +0000119ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +0000120PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerd8447992016-06-07 05:28:55 +0000121 auto Result = Context->StreamMap[StreamIndex];
122 codeview::StreamReader Reader(Result.getUnderlyingStream());
123 ArrayRef<support::ulittle32_t> Array;
124 if (auto EC = Reader.readArray(Array, Result.size()))
125 return ArrayRef<support::ulittle32_t>();
126 return Array;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000127}
128
Zachary Turnere6fee882016-06-07 20:38:37 +0000129ArrayRef<uint8_t> PDBFile::getBlockData(uint32_t BlockIndex,
130 uint32_t NumBytes) const {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000131 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
132
Zachary Turnere6fee882016-06-07 20:38:37 +0000133 return ArrayRef<uint8_t>(
134 reinterpret_cast<const uint8_t *>(Context->Buffer->getBufferStart()) +
135 StreamBlockOffset,
136 NumBytes);
Zachary Turner0a43efe2016-04-25 17:38:08 +0000137}
138
Zachary Turner819e77d2016-05-06 20:51:57 +0000139Error PDBFile::parseFileHeaders() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000140 std::error_code EC;
141 MemoryBufferRef BufferRef = *Context->Buffer;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000142
Zachary Turnerf5c59652016-05-03 00:28:21 +0000143 // Make sure the file is sufficiently large to hold a super block.
144 // Do this before attempting to read the super block.
Zachary Turnerd6192f42016-05-02 22:16:57 +0000145 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
Zachary Turner819e77d2016-05-06 20:51:57 +0000146 return make_error<RawError>(raw_error_code::corrupt_file,
147 "Does not contain superblock");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000148
149 Context->SB =
150 reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
151 const SuperBlock *SB = Context->SB;
Zachary Turner819e77d2016-05-06 20:51:57 +0000152 // Check the magic bytes.
153 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
154 return make_error<RawError>(raw_error_code::corrupt_file,
155 "MSF magic header doesn't match");
156
David Majnemer878cadb2016-05-27 15:57:38 +0000157 // We don't support blocksizes which aren't a multiple of four bytes.
158 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000159 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000160 "Block size is not multiple of 4.");
Zachary Turner819e77d2016-05-06 20:51:57 +0000161
Zachary Turner9213ba52016-04-29 18:09:19 +0000162 switch (SB->BlockSize) {
163 case 512: case 1024: case 2048: case 4096:
164 break;
165 default:
166 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000167 return make_error<RawError>(raw_error_code::corrupt_file,
168 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000169 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000170
David Majnemer878cadb2016-05-27 15:57:38 +0000171 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000172 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000173 "File size is not a multiple of block size");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000174
175 // We don't support directories whose sizes aren't a multiple of four bytes.
176 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000177 return make_error<RawError>(raw_error_code::corrupt_file,
178 "Directory size is not multiple of 4.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000179
180 // The number of blocks which comprise the directory is a simple function of
181 // the number of bytes it contains.
182 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
183
184 // The block map, as we understand it, is a block which consists of a list of
185 // block numbers.
186 // It is unclear what would happen if the number of blocks couldn't fit on a
187 // single block.
188 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000189 return make_error<RawError>(raw_error_code::corrupt_file,
190 "Too many directory blocks.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000191
David Majnemer5d842ea2016-05-27 16:16:45 +0000192 // Make sure the directory block array fits within the file.
193 if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray()))
194 return EC;
195
Zachary Turner819e77d2016-05-06 20:51:57 +0000196 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000197}
198
Zachary Turner819e77d2016-05-06 20:51:57 +0000199Error PDBFile::parseStreamData() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000200 assert(Context && Context->SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000201 if (DirectoryStream)
202 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000203
Zachary Turner0a43efe2016-04-25 17:38:08 +0000204 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000205
Zachary Turner0a43efe2016-04-25 17:38:08 +0000206 const SuperBlock *SB = Context->SB;
207
Zachary Turnerd8447992016-06-07 05:28:55 +0000208 // Normally you can't use a MappedBlockStream without having fully parsed the
209 // PDB file, because it accesses the directory and various other things, which
210 // is exactly what we are attempting to parse. By specifying a custom
211 // subclass of IPDBStreamData which only accesses the fields that have already
212 // been parsed, we can avoid this and reuse MappedBlockStream.
Zachary Turnera1657a92016-06-08 17:26:39 +0000213 auto DS = MappedBlockStream::createDirectoryStream(*this);
214 if (!DS)
215 return DS.takeError();
216 codeview::StreamReader Reader(**DS);
Zachary Turnerd8447992016-06-07 05:28:55 +0000217 if (auto EC = Reader.readInteger(NumStreams))
218 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000219
Zachary Turnerd8447992016-06-07 05:28:55 +0000220 if (auto EC = Reader.readArray(Context->StreamSizes, NumStreams))
221 return EC;
222 for (uint32_t I = 0; I < NumStreams; ++I) {
David Majnemer9efba742016-05-27 16:16:48 +0000223 uint64_t NumExpectedStreamBlocks =
Zachary Turnerd8447992016-06-07 05:28:55 +0000224 bytesToBlocks(getStreamByteSize(I), SB->BlockSize);
225 ulittle_array Blocks;
226 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
227 return EC;
228 Context->StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000229 }
230
Zachary Turner0a43efe2016-04-25 17:38:08 +0000231 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000232 assert(Reader.bytesRemaining() == 0);
Zachary Turnera1657a92016-06-08 17:26:39 +0000233 DirectoryStream = std::move(*DS);
Zachary Turner819e77d2016-05-06 20:51:57 +0000234 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000235}
236
Zachary Turnerd8447992016-06-07 05:28:55 +0000237llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000238 return makeArrayRef(
239 reinterpret_cast<const support::ulittle32_t *>(
240 Context->Buffer->getBufferStart() + getBlockMapOffset()),
241 getNumDirectoryBlocks());
242}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000243
Zachary Turner819e77d2016-05-06 20:51:57 +0000244Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000245 if (!Info) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000246 auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this);
247 if (!InfoS)
248 return InfoS.takeError();
249 auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS));
250 if (auto EC = TempInfo->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000251 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000252 Info = std::move(TempInfo);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000253 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000254 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000255}
256
Zachary Turner819e77d2016-05-06 20:51:57 +0000257Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000258 if (!Dbi) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000259 auto DbiS = MappedBlockStream::createIndexedStream(StreamDBI, *this);
260 if (!DbiS)
261 return DbiS.takeError();
262 auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS));
263 if (auto EC = TempDbi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000264 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000265 Dbi = std::move(TempDbi);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000266 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000267 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000268}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000269
Zachary Turner819e77d2016-05-06 20:51:57 +0000270Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000271 if (!Tpi) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000272 auto TpiS = MappedBlockStream::createIndexedStream(StreamTPI, *this);
273 if (!TpiS)
274 return TpiS.takeError();
275 auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS));
276 if (auto EC = TempTpi->reload())
Zachary Turner819e77d2016-05-06 20:51:57 +0000277 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000278 Tpi = std::move(TempTpi);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000279 }
280 return *Tpi;
281}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000282
Zachary Turnerc9972c62016-05-25 04:35:22 +0000283Expected<TpiStream &> PDBFile::getPDBIpiStream() {
284 if (!Ipi) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000285 auto IpiS = MappedBlockStream::createIndexedStream(StreamIPI, *this);
286 if (!IpiS)
287 return IpiS.takeError();
288 auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS));
289 if (auto EC = TempIpi->reload())
Zachary Turnerc9972c62016-05-25 04:35:22 +0000290 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000291 Ipi = std::move(TempIpi);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000292 }
293 return *Ipi;
294}
295
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000296Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
297 if (!Publics) {
298 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000299 if (!DbiS)
300 return DbiS.takeError();
301
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000302 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
303
Zachary Turnera1657a92016-06-08 17:26:39 +0000304 auto PublicS =
305 MappedBlockStream::createIndexedStream(PublicsStreamNum, *this);
306 if (!PublicS)
307 return PublicS.takeError();
308 auto TempPublics =
309 llvm::make_unique<PublicsStream>(*this, std::move(*PublicS));
310 if (auto EC = TempPublics->reload())
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000311 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000312 Publics = std::move(TempPublics);
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000313 }
314 return *Publics;
315}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000316
317Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
318 if (!Symbols) {
319 auto DbiS = getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000320 if (!DbiS)
321 return DbiS.takeError();
322
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000323 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
324
Zachary Turnera1657a92016-06-08 17:26:39 +0000325 auto SymbolS =
326 MappedBlockStream::createIndexedStream(SymbolStreamNum, *this);
327 if (!SymbolS)
328 return SymbolS.takeError();
329 auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS));
330 if (auto EC = TempSymbols->reload())
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000331 return std::move(EC);
Zachary Turnera1657a92016-06-08 17:26:39 +0000332 Symbols = std::move(TempSymbols);
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000333 }
334 return *Symbols;
335}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000336
337Expected<NameHashTable &> PDBFile::getStringTable() {
338 if (!StringTable || !StringTableStream) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000339 auto IS = getPDBInfoStream();
340 if (!IS)
341 return IS.takeError();
342
343 uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names");
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000344
345 if (NameStreamIndex == 0)
346 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000347 if (NameStreamIndex >= getNumStreams())
348 return make_error<RawError>(raw_error_code::no_stream);
349
Zachary Turnera1657a92016-06-08 17:26:39 +0000350 auto NS = MappedBlockStream::createIndexedStream(NameStreamIndex, *this);
351 if (!NS)
352 return NS.takeError();
353
354 codeview::StreamReader Reader(**NS);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000355 auto N = llvm::make_unique<NameHashTable>();
356 if (auto EC = N->load(Reader))
357 return std::move(EC);
358 StringTable = std::move(N);
Zachary Turnera1657a92016-06-08 17:26:39 +0000359 StringTableStream = std::move(*NS);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000360 }
361 return *StringTable;
362}