blob: 22094773624fa1912c8102c08a38d9a141d05548 [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
Zachary Turnere6fee882016-06-07 20:38:37 +0000141ArrayRef<uint8_t> PDBFile::getBlockData(uint32_t BlockIndex,
142 uint32_t NumBytes) const {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000143 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
144
Zachary Turnere6fee882016-06-07 20:38:37 +0000145 return ArrayRef<uint8_t>(
146 reinterpret_cast<const uint8_t *>(Context->Buffer->getBufferStart()) +
147 StreamBlockOffset,
148 NumBytes);
Zachary Turner0a43efe2016-04-25 17:38:08 +0000149}
150
Zachary Turner819e77d2016-05-06 20:51:57 +0000151Error PDBFile::parseFileHeaders() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000152 std::error_code EC;
153 MemoryBufferRef BufferRef = *Context->Buffer;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000154
Zachary Turnerf5c59652016-05-03 00:28:21 +0000155 // Make sure the file is sufficiently large to hold a super block.
156 // Do this before attempting to read the super block.
Zachary Turnerd6192f42016-05-02 22:16:57 +0000157 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
Zachary Turner819e77d2016-05-06 20:51:57 +0000158 return make_error<RawError>(raw_error_code::corrupt_file,
159 "Does not contain superblock");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000160
161 Context->SB =
162 reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
163 const SuperBlock *SB = Context->SB;
Zachary Turner819e77d2016-05-06 20:51:57 +0000164 // Check the magic bytes.
165 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
166 return make_error<RawError>(raw_error_code::corrupt_file,
167 "MSF magic header doesn't match");
168
David Majnemer878cadb2016-05-27 15:57:38 +0000169 // We don't support blocksizes which aren't a multiple of four bytes.
170 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000171 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000172 "Block size is not multiple of 4.");
Zachary Turner819e77d2016-05-06 20:51:57 +0000173
Zachary Turner9213ba52016-04-29 18:09:19 +0000174 switch (SB->BlockSize) {
175 case 512: case 1024: case 2048: case 4096:
176 break;
177 default:
178 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000179 return make_error<RawError>(raw_error_code::corrupt_file,
180 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000181 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000182
David Majnemer878cadb2016-05-27 15:57:38 +0000183 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000184 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000185 "File size is not a multiple of block size");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000186
187 // We don't support directories whose sizes aren't a multiple of four bytes.
188 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000189 return make_error<RawError>(raw_error_code::corrupt_file,
190 "Directory size is not multiple of 4.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000191
192 // The number of blocks which comprise the directory is a simple function of
193 // the number of bytes it contains.
194 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
195
196 // The block map, as we understand it, is a block which consists of a list of
197 // block numbers.
198 // It is unclear what would happen if the number of blocks couldn't fit on a
199 // single block.
200 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000201 return make_error<RawError>(raw_error_code::corrupt_file,
202 "Too many directory blocks.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000203
David Majnemer5d842ea2016-05-27 16:16:45 +0000204 // Make sure the directory block array fits within the file.
205 if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray()))
206 return EC;
207
Zachary Turner819e77d2016-05-06 20:51:57 +0000208 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000209}
210
Zachary Turner819e77d2016-05-06 20:51:57 +0000211Error PDBFile::parseStreamData() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000212 assert(Context && Context->SB);
Zachary Turnerd8447992016-06-07 05:28:55 +0000213 if (DirectoryStream)
214 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000215
Zachary Turner0a43efe2016-04-25 17:38:08 +0000216 uint32_t NumStreams = 0;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000217
Zachary Turner0a43efe2016-04-25 17:38:08 +0000218 const SuperBlock *SB = Context->SB;
219
Zachary Turnerd8447992016-06-07 05:28:55 +0000220 // Normally you can't use a MappedBlockStream without having fully parsed the
221 // PDB file, because it accesses the directory and various other things, which
222 // is exactly what we are attempting to parse. By specifying a custom
223 // subclass of IPDBStreamData which only accesses the fields that have already
224 // been parsed, we can avoid this and reuse MappedBlockStream.
225 auto SD = llvm::make_unique<DirectoryStreamData>(*this);
226 DirectoryStream = llvm::make_unique<MappedBlockStream>(std::move(SD), *this);
227 codeview::StreamReader Reader(*DirectoryStream);
228 if (auto EC = Reader.readInteger(NumStreams))
229 return EC;
Zachary Turner0a43efe2016-04-25 17:38:08 +0000230
Zachary Turnerd8447992016-06-07 05:28:55 +0000231 if (auto EC = Reader.readArray(Context->StreamSizes, NumStreams))
232 return EC;
233 for (uint32_t I = 0; I < NumStreams; ++I) {
David Majnemer9efba742016-05-27 16:16:48 +0000234 uint64_t NumExpectedStreamBlocks =
Zachary Turnerd8447992016-06-07 05:28:55 +0000235 bytesToBlocks(getStreamByteSize(I), SB->BlockSize);
236 ulittle_array Blocks;
237 if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks))
238 return EC;
239 Context->StreamMap.push_back(Blocks);
David Majnemer9efba742016-05-27 16:16:48 +0000240 }
241
Zachary Turner0a43efe2016-04-25 17:38:08 +0000242 // We should have read exactly SB->NumDirectoryBytes bytes.
Zachary Turnerd8447992016-06-07 05:28:55 +0000243 assert(Reader.bytesRemaining() == 0);
Zachary Turner819e77d2016-05-06 20:51:57 +0000244 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000245}
246
Zachary Turnerd8447992016-06-07 05:28:55 +0000247llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000248 return makeArrayRef(
249 reinterpret_cast<const support::ulittle32_t *>(
250 Context->Buffer->getBufferStart() + getBlockMapOffset()),
251 getNumDirectoryBlocks());
252}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000253
Zachary Turner819e77d2016-05-06 20:51:57 +0000254Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000255 if (!Info) {
256 Info.reset(new InfoStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000257 if (auto EC = Info->reload())
258 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000259 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000260 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000261}
262
Zachary Turner819e77d2016-05-06 20:51:57 +0000263Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000264 if (!Dbi) {
265 Dbi.reset(new DbiStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000266 if (auto EC = Dbi->reload())
267 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000268 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000269 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000270}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000271
Zachary Turner819e77d2016-05-06 20:51:57 +0000272Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000273 if (!Tpi) {
Zachary Turnerc9972c62016-05-25 04:35:22 +0000274 Tpi.reset(new TpiStream(*this, StreamTPI));
Zachary Turner819e77d2016-05-06 20:51:57 +0000275 if (auto EC = Tpi->reload())
276 return std::move(EC);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000277 }
278 return *Tpi;
279}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000280
Zachary Turnerc9972c62016-05-25 04:35:22 +0000281Expected<TpiStream &> PDBFile::getPDBIpiStream() {
282 if (!Ipi) {
283 Ipi.reset(new TpiStream(*this, StreamIPI));
284 if (auto EC = Ipi->reload())
285 return std::move(EC);
286 }
287 return *Ipi;
288}
289
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000290Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
291 if (!Publics) {
292 auto DbiS = getPDBDbiStream();
293 if (auto EC = DbiS.takeError())
294 return std::move(EC);
295 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
296
297 Publics.reset(new PublicsStream(*this, PublicsStreamNum));
298 if (auto EC = Publics->reload())
299 return std::move(EC);
300 }
301 return *Publics;
302}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000303
304Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
305 if (!Symbols) {
306 auto DbiS = getPDBDbiStream();
307 if (auto EC = DbiS.takeError())
308 return std::move(EC);
309 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
310
311 Symbols.reset(new SymbolStream(*this, SymbolStreamNum));
312 if (auto EC = Symbols->reload())
313 return std::move(EC);
314 }
315 return *Symbols;
316}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000317
318Expected<NameHashTable &> PDBFile::getStringTable() {
319 if (!StringTable || !StringTableStream) {
320 auto InfoS = getPDBInfoStream();
321 if (auto EC = InfoS.takeError())
322 return std::move(EC);
323 auto &IS = InfoS.get();
324 uint32_t NameStreamIndex = IS.getNamedStreamIndex("/names");
325
326 if (NameStreamIndex == 0)
327 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd8447992016-06-07 05:28:55 +0000328 auto SD = llvm::make_unique<IndexedStreamData>(NameStreamIndex, *this);
329 auto S = llvm::make_unique<MappedBlockStream>(std::move(SD), *this);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000330 codeview::StreamReader Reader(*S);
331 auto N = llvm::make_unique<NameHashTable>();
332 if (auto EC = N->load(Reader))
333 return std::move(EC);
334 StringTable = std::move(N);
335 StringTableStream = std::move(S);
336 }
337 return *StringTable;
338}