blob: 9ef0a492e878e6978de6a3037129968a2ce5a14c [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 Turnerd8447992016-06-07 05:28:55 +000016#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000017#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner3df1bfa2016-06-03 05:52:57 +000018#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000019#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000020#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000021#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000022#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000023#include "llvm/Support/Endian.h"
24#include "llvm/Support/MemoryBuffer.h"
25
26using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000027using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000028
29namespace {
30static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f',
31 't', ' ', 'C', '/', 'C', '+', '+', ' ',
32 'M', 'S', 'F', ' ', '7', '.', '0', '0',
33 '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
34
35// The superblock is overlaid at the beginning of the file (offset 0).
36// It starts with a magic header and is followed by information which describes
37// the layout of the file system.
38struct SuperBlock {
39 char MagicBytes[sizeof(Magic)];
40 // The file system is split into a variable number of fixed size elements.
41 // These elements are referred to as blocks. The size of a block may vary
42 // from system to system.
43 support::ulittle32_t BlockSize;
44 // This field's purpose is not yet known.
45 support::ulittle32_t Unknown0;
46 // This contains the number of blocks resident in the file system. In
47 // practice, NumBlocks * BlockSize is equivalent to the size of the PDB file.
48 support::ulittle32_t NumBlocks;
49 // This contains the number of bytes which make up the directory.
50 support::ulittle32_t NumDirectoryBytes;
51 // This field's purpose is not yet known.
52 support::ulittle32_t Unknown1;
53 // This contains the block # of the block map.
54 support::ulittle32_t BlockMapAddr;
55};
Zachary Turnerd8447992016-06-07 05:28:55 +000056
57class DirectoryStreamData : public IPDBStreamData {
58public:
59 DirectoryStreamData(const PDBFile &File) : File(File) {}
60
61 virtual uint32_t getLength() { return File.getNumDirectoryBytes(); }
62 virtual llvm::ArrayRef<llvm::support::ulittle32_t> getStreamBlocks() {
63 return File.getDirectoryBlockArray();
64 }
65
66private:
67 const PDBFile &File;
68};
69
70typedef codeview::FixedStreamArray<support::ulittle32_t> ulittle_array;
Zachary Turner0a43efe2016-04-25 17:38:08 +000071}
72
Zachary Turner2f09b502016-04-29 17:28:47 +000073struct llvm::pdb::PDBFileContext {
Zachary Turner0a43efe2016-04-25 17:38:08 +000074 std::unique_ptr<MemoryBuffer> Buffer;
75 const SuperBlock *SB;
Zachary Turnerd8447992016-06-07 05:28:55 +000076 ArrayRef<support::ulittle32_t> StreamSizes;
77 std::vector<ulittle_array> StreamMap;
Zachary Turner0a43efe2016-04-25 17:38:08 +000078};
79
Zachary Turner819e77d2016-05-06 20:51:57 +000080static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
81 const uint64_t Size) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000082 if (Addr + Size < Addr || Addr + Size < Size ||
83 Addr + Size > uintptr_t(M.getBufferEnd()) ||
84 Addr < uintptr_t(M.getBufferStart())) {
Zachary Turner819e77d2016-05-06 20:51:57 +000085 return make_error<RawError>(raw_error_code::corrupt_file,
86 "Invalid buffer address");
Zachary Turner0a43efe2016-04-25 17:38:08 +000087 }
Zachary Turner819e77d2016-05-06 20:51:57 +000088 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +000089}
90
91template <typename T>
Zachary Turner819e77d2016-05-06 20:51:57 +000092static Error checkOffset(MemoryBufferRef M, ArrayRef<T> AR) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000093 return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T));
94}
95
96PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) {
97 Context.reset(new PDBFileContext());
98 Context->Buffer = std::move(MemBuffer);
99}
100
101PDBFile::~PDBFile() {}
102
103uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; }
104
105uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; }
106
107uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; }
108
109uint32_t PDBFile::getNumDirectoryBytes() const {
110 return Context->SB->NumDirectoryBytes;
111}
112
113uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; }
114
115uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; }
116
117uint32_t PDBFile::getNumDirectoryBlocks() const {
118 return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize);
119}
120
121uint64_t PDBFile::getBlockMapOffset() const {
122 return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize;
123}
124
125uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); }
126
127uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
128 return Context->StreamSizes[StreamIndex];
129}
130
Zachary Turnerd8447992016-06-07 05:28:55 +0000131ArrayRef<support::ulittle32_t>
Zachary Turner0a43efe2016-04-25 17:38:08 +0000132PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
Zachary Turnerd8447992016-06-07 05:28:55 +0000133 auto Result = Context->StreamMap[StreamIndex];
134 codeview::StreamReader Reader(Result.getUnderlyingStream());
135 ArrayRef<support::ulittle32_t> Array;
136 if (auto EC = Reader.readArray(Array, Result.size()))
137 return ArrayRef<support::ulittle32_t>();
138 return Array;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000139}
140
141StringRef PDBFile::getBlockData(uint32_t BlockIndex, uint32_t NumBytes) const {
142 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
143
144 return StringRef(Context->Buffer->getBufferStart() + StreamBlockOffset,
145 NumBytes);
146}
147
Zachary Turner819e77d2016-05-06 20:51:57 +0000148Error PDBFile::parseFileHeaders() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000149 std::error_code EC;
150 MemoryBufferRef BufferRef = *Context->Buffer;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000151
Zachary Turnerf5c59652016-05-03 00:28:21 +0000152 // Make sure the file is sufficiently large to hold a super block.
153 // Do this before attempting to read the super block.
Zachary Turnerd6192f42016-05-02 22:16:57 +0000154 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
Zachary Turner819e77d2016-05-06 20:51:57 +0000155 return make_error<RawError>(raw_error_code::corrupt_file,
156 "Does not contain superblock");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000157
158 Context->SB =
159 reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
160 const SuperBlock *SB = Context->SB;
Zachary Turner819e77d2016-05-06 20:51:57 +0000161 // Check the magic bytes.
162 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
163 return make_error<RawError>(raw_error_code::corrupt_file,
164 "MSF magic header doesn't match");
165
David Majnemer878cadb2016-05-27 15:57:38 +0000166 // We don't support blocksizes which aren't a multiple of four bytes.
167 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000168 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000169 "Block size is not multiple of 4.");
Zachary Turner819e77d2016-05-06 20:51:57 +0000170
Zachary Turner9213ba52016-04-29 18:09:19 +0000171 switch (SB->BlockSize) {
172 case 512: case 1024: case 2048: case 4096:
173 break;
174 default:
175 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000176 return make_error<RawError>(raw_error_code::corrupt_file,
177 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000178 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000179
David Majnemer878cadb2016-05-27 15:57:38 +0000180 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000181 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000182 "File size is not a multiple of block size");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000183
184 // We don't support directories whose sizes aren't a multiple of four bytes.
185 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000186 return make_error<RawError>(raw_error_code::corrupt_file,
187 "Directory size is not multiple of 4.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000188
189 // The number of blocks which comprise the directory is a simple function of
190 // the number of bytes it contains.
191 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
192
193 // The block map, as we understand it, is a block which consists of a list of
194 // block numbers.
195 // It is unclear what would happen if the number of blocks couldn't fit on a
196 // single block.
197 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000198 return make_error<RawError>(raw_error_code::corrupt_file,
199 "Too many directory blocks.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000200
David Majnemer5d842ea2016-05-27 16:16:45 +0000201 // Make sure the directory block array fits within the file.
202 if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray()))
203 return EC;
204
Zachary Turner819e77d2016-05-06 20:51:57 +0000205 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000206}
207
Zachary Turner819e77d2016-05-06 20:51:57 +0000208Error PDBFile::parseStreamData() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000209 assert(Context && Context->SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000210 if (DirectoryStream)
211 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000212
Zachary Turner0a43efe2016-04-25 17:38:08 +0000213 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000214
Zachary Turner0a43efe2016-04-25 17:38:08 +0000215 const SuperBlock *SB = Context->SB;
216
Zachary Turnerd8447992016-06-07 05:28:55 +0000217 // Normally you can't use a MappedBlockStream without having fully parsed the
218 // PDB file, because it accesses the directory and various other things, which
219 // is exactly what we are attempting to parse. By specifying a custom
220 // subclass of IPDBStreamData which only accesses the fields that have already
221 // been parsed, we can avoid this and reuse MappedBlockStream.
222 auto SD = llvm::make_unique<DirectoryStreamData>(*this);
223 DirectoryStream = llvm::make_unique<MappedBlockStream>(std::move(SD), *this);
224 codeview::StreamReader Reader(*DirectoryStream);
225 if (auto EC = Reader.readInteger(NumStreams))
226 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000227
Zachary Turnerd8447992016-06-07 05:28:55 +0000228 if (auto EC = Reader.readArray(Context->StreamSizes, NumStreams))
229 return EC;
230 for (uint32_t I = 0; I < NumStreams; ++I) {
David Majnemer9efba742016-05-27 16:16:48 +0000231 uint64_t NumExpectedStreamBlocks =
Zachary Turnerd8447992016-06-07 05:28:55 +0000232 bytesToBlocks(getStreamByteSize(I), SB->BlockSize);
233 ulittle_array Blocks;
234 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
235 return EC;
236 Context->StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000237 }
238
Zachary Turner0a43efe2016-04-25 17:38:08 +0000239 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000240 assert(Reader.bytesRemaining() == 0);
Zachary Turner819e77d2016-05-06 20:51:57 +0000241 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000242}
243
Zachary Turnerd8447992016-06-07 05:28:55 +0000244llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000245 return makeArrayRef(
246 reinterpret_cast<const support::ulittle32_t *>(
247 Context->Buffer->getBufferStart() + getBlockMapOffset()),
248 getNumDirectoryBlocks());
249}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000250
Zachary Turner819e77d2016-05-06 20:51:57 +0000251Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000252 if (!Info) {
253 Info.reset(new InfoStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000254 if (auto EC = Info->reload())
255 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000256 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000257 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000258}
259
Zachary Turner819e77d2016-05-06 20:51:57 +0000260Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000261 if (!Dbi) {
262 Dbi.reset(new DbiStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000263 if (auto EC = Dbi->reload())
264 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000265 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000266 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000267}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000268
Zachary Turner819e77d2016-05-06 20:51:57 +0000269Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000270 if (!Tpi) {
Zachary Turnerc9972c62016-05-25 04:35:22 +0000271 Tpi.reset(new TpiStream(*this, StreamTPI));
Zachary Turner819e77d2016-05-06 20:51:57 +0000272 if (auto EC = Tpi->reload())
273 return std::move(EC);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000274 }
275 return *Tpi;
276}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000277
Zachary Turnerc9972c62016-05-25 04:35:22 +0000278Expected<TpiStream &> PDBFile::getPDBIpiStream() {
279 if (!Ipi) {
280 Ipi.reset(new TpiStream(*this, StreamIPI));
281 if (auto EC = Ipi->reload())
282 return std::move(EC);
283 }
284 return *Ipi;
285}
286
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000287Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
288 if (!Publics) {
289 auto DbiS = getPDBDbiStream();
290 if (auto EC = DbiS.takeError())
291 return std::move(EC);
292 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
293
294 Publics.reset(new PublicsStream(*this, PublicsStreamNum));
295 if (auto EC = Publics->reload())
296 return std::move(EC);
297 }
298 return *Publics;
299}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000300
301Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
302 if (!Symbols) {
303 auto DbiS = getPDBDbiStream();
304 if (auto EC = DbiS.takeError())
305 return std::move(EC);
306 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
307
308 Symbols.reset(new SymbolStream(*this, SymbolStreamNum));
309 if (auto EC = Symbols->reload())
310 return std::move(EC);
311 }
312 return *Symbols;
313}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000314
315Expected<NameHashTable &> PDBFile::getStringTable() {
316 if (!StringTable || !StringTableStream) {
317 auto InfoS = getPDBInfoStream();
318 if (auto EC = InfoS.takeError())
319 return std::move(EC);
320 auto &IS = InfoS.get();
321 uint32_t NameStreamIndex = IS.getNamedStreamIndex("/names");
322
323 if (NameStreamIndex == 0)
324 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd8447992016-06-07 05:28:55 +0000325 auto SD = llvm::make_unique<IndexedStreamData>(NameStreamIndex, *this);
326 auto S = llvm::make_unique<MappedBlockStream>(std::move(SD), *this);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000327 codeview::StreamReader Reader(*S);
328 auto N = llvm::make_unique<NameHashTable>();
329 if (auto EC = N->load(Reader))
330 return std::move(EC);
331 StringTable = std::move(N);
332 StringTableStream = std::move(S);
333 }
334 return *StringTable;
335}