blob: 73d8eef570f8c0987a5bef4444e37459b4a57987 [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"
11#include "llvm/ADT/ArrayRef.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000012#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
13#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner3df1bfa2016-06-03 05:52:57 +000014#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000015#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000016#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000017#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000018#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000019#include "llvm/Support/Endian.h"
20#include "llvm/Support/MemoryBuffer.h"
21
22using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000023using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000024
25namespace {
26static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f',
27 't', ' ', 'C', '/', 'C', '+', '+', ' ',
28 'M', 'S', 'F', ' ', '7', '.', '0', '0',
29 '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
30
31// The superblock is overlaid at the beginning of the file (offset 0).
32// It starts with a magic header and is followed by information which describes
33// the layout of the file system.
34struct SuperBlock {
35 char MagicBytes[sizeof(Magic)];
36 // The file system is split into a variable number of fixed size elements.
37 // These elements are referred to as blocks. The size of a block may vary
38 // from system to system.
39 support::ulittle32_t BlockSize;
40 // This field's purpose is not yet known.
41 support::ulittle32_t Unknown0;
42 // This contains the number of blocks resident in the file system. In
43 // practice, NumBlocks * BlockSize is equivalent to the size of the PDB file.
44 support::ulittle32_t NumBlocks;
45 // This contains the number of bytes which make up the directory.
46 support::ulittle32_t NumDirectoryBytes;
47 // This field's purpose is not yet known.
48 support::ulittle32_t Unknown1;
49 // This contains the block # of the block map.
50 support::ulittle32_t BlockMapAddr;
51};
52}
53
Zachary Turner2f09b502016-04-29 17:28:47 +000054struct llvm::pdb::PDBFileContext {
Zachary Turner0a43efe2016-04-25 17:38:08 +000055 std::unique_ptr<MemoryBuffer> Buffer;
56 const SuperBlock *SB;
57 std::vector<uint32_t> StreamSizes;
58 DenseMap<uint32_t, std::vector<uint32_t>> StreamMap;
59};
60
Zachary Turner819e77d2016-05-06 20:51:57 +000061static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
62 const uint64_t Size) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000063 if (Addr + Size < Addr || Addr + Size < Size ||
64 Addr + Size > uintptr_t(M.getBufferEnd()) ||
65 Addr < uintptr_t(M.getBufferStart())) {
Zachary Turner819e77d2016-05-06 20:51:57 +000066 return make_error<RawError>(raw_error_code::corrupt_file,
67 "Invalid buffer address");
Zachary Turner0a43efe2016-04-25 17:38:08 +000068 }
Zachary Turner819e77d2016-05-06 20:51:57 +000069 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +000070}
71
72template <typename T>
Zachary Turner819e77d2016-05-06 20:51:57 +000073static Error checkOffset(MemoryBufferRef M, ArrayRef<T> AR) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000074 return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T));
75}
76
77PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) {
78 Context.reset(new PDBFileContext());
79 Context->Buffer = std::move(MemBuffer);
80}
81
82PDBFile::~PDBFile() {}
83
84uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; }
85
86uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; }
87
88uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; }
89
90uint32_t PDBFile::getNumDirectoryBytes() const {
91 return Context->SB->NumDirectoryBytes;
92}
93
94uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; }
95
96uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; }
97
98uint32_t PDBFile::getNumDirectoryBlocks() const {
99 return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize);
100}
101
102uint64_t PDBFile::getBlockMapOffset() const {
103 return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize;
104}
105
106uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); }
107
108uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
109 return Context->StreamSizes[StreamIndex];
110}
111
112llvm::ArrayRef<uint32_t>
113PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
114 auto &Data = Context->StreamMap[StreamIndex];
115 return llvm::ArrayRef<uint32_t>(Data);
116}
117
118StringRef PDBFile::getBlockData(uint32_t BlockIndex, uint32_t NumBytes) const {
119 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
120
121 return StringRef(Context->Buffer->getBufferStart() + StreamBlockOffset,
122 NumBytes);
123}
124
Zachary Turner819e77d2016-05-06 20:51:57 +0000125Error PDBFile::parseFileHeaders() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000126 std::error_code EC;
127 MemoryBufferRef BufferRef = *Context->Buffer;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000128
Zachary Turnerf5c59652016-05-03 00:28:21 +0000129 // Make sure the file is sufficiently large to hold a super block.
130 // Do this before attempting to read the super block.
Zachary Turnerd6192f42016-05-02 22:16:57 +0000131 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
Zachary Turner819e77d2016-05-06 20:51:57 +0000132 return make_error<RawError>(raw_error_code::corrupt_file,
133 "Does not contain superblock");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000134
135 Context->SB =
136 reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
137 const SuperBlock *SB = Context->SB;
Zachary Turner819e77d2016-05-06 20:51:57 +0000138 // Check the magic bytes.
139 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
140 return make_error<RawError>(raw_error_code::corrupt_file,
141 "MSF magic header doesn't match");
142
David Majnemer878cadb2016-05-27 15:57:38 +0000143 // We don't support blocksizes which aren't a multiple of four bytes.
144 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000145 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000146 "Block size is not multiple of 4.");
Zachary Turner819e77d2016-05-06 20:51:57 +0000147
Zachary Turner9213ba52016-04-29 18:09:19 +0000148 switch (SB->BlockSize) {
149 case 512: case 1024: case 2048: case 4096:
150 break;
151 default:
152 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000153 return make_error<RawError>(raw_error_code::corrupt_file,
154 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000155 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000156
David Majnemer878cadb2016-05-27 15:57:38 +0000157 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000158 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000159 "File size is not a multiple of block size");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000160
161 // We don't support directories whose sizes aren't a multiple of four bytes.
162 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000163 return make_error<RawError>(raw_error_code::corrupt_file,
164 "Directory size is not multiple of 4.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000165
166 // The number of blocks which comprise the directory is a simple function of
167 // the number of bytes it contains.
168 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
169
170 // The block map, as we understand it, is a block which consists of a list of
171 // block numbers.
172 // It is unclear what would happen if the number of blocks couldn't fit on a
173 // single block.
174 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000175 return make_error<RawError>(raw_error_code::corrupt_file,
176 "Too many directory blocks.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000177
David Majnemer5d842ea2016-05-27 16:16:45 +0000178 // Make sure the directory block array fits within the file.
179 if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray()))
180 return EC;
181
Zachary Turner819e77d2016-05-06 20:51:57 +0000182 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000183}
184
Zachary Turner819e77d2016-05-06 20:51:57 +0000185Error PDBFile::parseStreamData() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000186 assert(Context && Context->SB);
187
188 bool SeenNumStreams = false;
189 uint32_t NumStreams = 0;
190 uint32_t StreamIdx = 0;
191 uint64_t DirectoryBytesRead = 0;
192
193 MemoryBufferRef M = *Context->Buffer;
194 const SuperBlock *SB = Context->SB;
195
196 auto DirectoryBlocks = getDirectoryBlockArray();
197
198 // The structure of the directory is as follows:
199 // struct PDBDirectory {
200 // uint32_t NumStreams;
201 // uint32_t StreamSizes[NumStreams];
202 // uint32_t StreamMap[NumStreams][];
203 // };
204 //
205 // Empty streams don't consume entries in the StreamMap.
206 for (uint32_t DirectoryBlockAddr : DirectoryBlocks) {
207 uint64_t DirectoryBlockOffset =
208 blockToOffset(DirectoryBlockAddr, SB->BlockSize);
209 auto DirectoryBlock =
210 makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
211 M.getBufferStart() + DirectoryBlockOffset),
212 SB->BlockSize / sizeof(support::ulittle32_t));
213 if (auto EC = checkOffset(M, DirectoryBlock))
214 return EC;
215
216 // We read data out of the directory four bytes at a time. Depending on
217 // where we are in the directory, the contents may be: the number of streams
218 // in the directory, a stream's size, or a block in the stream map.
219 for (uint32_t Data : DirectoryBlock) {
220 // Don't read beyond the end of the directory.
221 if (DirectoryBytesRead == SB->NumDirectoryBytes)
222 break;
223
224 DirectoryBytesRead += sizeof(Data);
225
226 // This data must be the number of streams if we haven't seen it yet.
227 if (!SeenNumStreams) {
228 NumStreams = Data;
229 SeenNumStreams = true;
230 continue;
231 }
232 // This data must be a stream size if we have not seen them all yet.
233 if (Context->StreamSizes.size() < NumStreams) {
234 // It seems like some streams have their set to -1 when their contents
235 // are not present. Treat them like empty streams for now.
236 if (Data == UINT32_MAX)
237 Context->StreamSizes.push_back(0);
238 else
239 Context->StreamSizes.push_back(Data);
240 continue;
241 }
242
243 // This data must be a stream block number if we have seen all of the
244 // stream sizes.
245 std::vector<uint32_t> *StreamBlocks = nullptr;
246 // Figure out which stream this block number belongs to.
247 while (StreamIdx < NumStreams) {
248 uint64_t NumExpectedStreamBlocks =
249 bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize);
250 StreamBlocks = &Context->StreamMap[StreamIdx];
251 if (NumExpectedStreamBlocks > StreamBlocks->size())
252 break;
253 ++StreamIdx;
254 }
255 // It seems this block doesn't belong to any stream? The stream is either
256 // corrupt or something more mysterious is going on.
257 if (StreamIdx == NumStreams)
Zachary Turner819e77d2016-05-06 20:51:57 +0000258 return make_error<RawError>(raw_error_code::corrupt_file,
259 "Orphaned block found?");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000260
David Majnemer9efba742016-05-27 16:16:48 +0000261 uint64_t BlockOffset = blockToOffset(Data, getBlockSize());
262 if (BlockOffset + getBlockSize() < BlockOffset)
263 return make_error<RawError>(raw_error_code::corrupt_file,
264 "Bogus stream block number");
265 if (BlockOffset + getBlockSize() > M.getBufferSize())
266 return make_error<RawError>(raw_error_code::corrupt_file,
267 "Stream block number is out of bounds");
268
Zachary Turner0a43efe2016-04-25 17:38:08 +0000269 StreamBlocks->push_back(Data);
270 }
271 }
272
David Majnemer7e950b22016-05-28 05:59:19 +0000273 if (Context->StreamSizes.size() != NumStreams)
274 return make_error<RawError>(
275 raw_error_code::corrupt_file,
276 "The directory has fewer streams then expected");
277
278 for (uint32_t I = 0; I != NumStreams; ++I) {
David Majnemer9efba742016-05-27 16:16:48 +0000279 uint64_t NumExpectedStreamBlocks =
David Majnemer7e950b22016-05-28 05:59:19 +0000280 bytesToBlocks(getStreamByteSize(I), getBlockSize());
281 size_t NumStreamBlocks = getStreamBlockList(I).size();
David Majnemer9efba742016-05-27 16:16:48 +0000282 if (NumExpectedStreamBlocks != NumStreamBlocks)
283 return make_error<RawError>(raw_error_code::corrupt_file,
284 "The number of stream blocks is not "
285 "sufficient for the size of this stream");
286 }
287
Zachary Turner0a43efe2016-04-25 17:38:08 +0000288 // We should have read exactly SB->NumDirectoryBytes bytes.
289 assert(DirectoryBytesRead == SB->NumDirectoryBytes);
Zachary Turner819e77d2016-05-06 20:51:57 +0000290 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000291}
292
293llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() {
294 return makeArrayRef(
295 reinterpret_cast<const support::ulittle32_t *>(
296 Context->Buffer->getBufferStart() + getBlockMapOffset()),
297 getNumDirectoryBlocks());
298}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000299
Zachary Turner819e77d2016-05-06 20:51:57 +0000300Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000301 if (!Info) {
302 Info.reset(new InfoStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000303 if (auto EC = Info->reload())
304 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000305 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000306 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000307}
308
Zachary Turner819e77d2016-05-06 20:51:57 +0000309Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000310 if (!Dbi) {
311 Dbi.reset(new DbiStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000312 if (auto EC = Dbi->reload())
313 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000314 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000315 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000316}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000317
Zachary Turner819e77d2016-05-06 20:51:57 +0000318Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000319 if (!Tpi) {
Zachary Turnerc9972c62016-05-25 04:35:22 +0000320 Tpi.reset(new TpiStream(*this, StreamTPI));
Zachary Turner819e77d2016-05-06 20:51:57 +0000321 if (auto EC = Tpi->reload())
322 return std::move(EC);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000323 }
324 return *Tpi;
325}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000326
Zachary Turnerc9972c62016-05-25 04:35:22 +0000327Expected<TpiStream &> PDBFile::getPDBIpiStream() {
328 if (!Ipi) {
329 Ipi.reset(new TpiStream(*this, StreamIPI));
330 if (auto EC = Ipi->reload())
331 return std::move(EC);
332 }
333 return *Ipi;
334}
335
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000336Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
337 if (!Publics) {
338 auto DbiS = getPDBDbiStream();
339 if (auto EC = DbiS.takeError())
340 return std::move(EC);
341 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
342
343 Publics.reset(new PublicsStream(*this, PublicsStreamNum));
344 if (auto EC = Publics->reload())
345 return std::move(EC);
346 }
347 return *Publics;
348}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000349
350Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
351 if (!Symbols) {
352 auto DbiS = getPDBDbiStream();
353 if (auto EC = DbiS.takeError())
354 return std::move(EC);
355 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
356
357 Symbols.reset(new SymbolStream(*this, SymbolStreamNum));
358 if (auto EC = Symbols->reload())
359 return std::move(EC);
360 }
361 return *Symbols;
362}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000363
364Expected<NameHashTable &> PDBFile::getStringTable() {
365 if (!StringTable || !StringTableStream) {
366 auto InfoS = getPDBInfoStream();
367 if (auto EC = InfoS.takeError())
368 return std::move(EC);
369 auto &IS = InfoS.get();
370 uint32_t NameStreamIndex = IS.getNamedStreamIndex("/names");
371
372 if (NameStreamIndex == 0)
373 return make_error<RawError>(raw_error_code::no_stream);
374 auto S = llvm::make_unique<MappedBlockStream>(NameStreamIndex, *this);
375 codeview::StreamReader Reader(*S);
376 auto N = llvm::make_unique<NameHashTable>();
377 if (auto EC = N->load(Reader))
378 return std::move(EC);
379 StringTable = std::move(N);
380 StringTableStream = std::move(S);
381 }
382 return *StringTable;
383}