blob: 68ed6c82efdd35428b600ce5b1670d3af41cccb7 [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"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000014#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000015#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Rui Ueyama0fcd8262016-05-20 19:55:17 +000016#include "llvm/DebugInfo/PDB/Raw/SymbolStream.h"
Zachary Turnerf5c59652016-05-03 00:28:21 +000017#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000018#include "llvm/Support/Endian.h"
19#include "llvm/Support/MemoryBuffer.h"
20
21using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000022using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000023
24namespace {
25static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f',
26 't', ' ', 'C', '/', 'C', '+', '+', ' ',
27 'M', 'S', 'F', ' ', '7', '.', '0', '0',
28 '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
29
30// The superblock is overlaid at the beginning of the file (offset 0).
31// It starts with a magic header and is followed by information which describes
32// the layout of the file system.
33struct SuperBlock {
34 char MagicBytes[sizeof(Magic)];
35 // The file system is split into a variable number of fixed size elements.
36 // These elements are referred to as blocks. The size of a block may vary
37 // from system to system.
38 support::ulittle32_t BlockSize;
39 // This field's purpose is not yet known.
40 support::ulittle32_t Unknown0;
41 // This contains the number of blocks resident in the file system. In
42 // practice, NumBlocks * BlockSize is equivalent to the size of the PDB file.
43 support::ulittle32_t NumBlocks;
44 // This contains the number of bytes which make up the directory.
45 support::ulittle32_t NumDirectoryBytes;
46 // This field's purpose is not yet known.
47 support::ulittle32_t Unknown1;
48 // This contains the block # of the block map.
49 support::ulittle32_t BlockMapAddr;
50};
51}
52
Zachary Turner2f09b502016-04-29 17:28:47 +000053struct llvm::pdb::PDBFileContext {
Zachary Turner0a43efe2016-04-25 17:38:08 +000054 std::unique_ptr<MemoryBuffer> Buffer;
55 const SuperBlock *SB;
56 std::vector<uint32_t> StreamSizes;
57 DenseMap<uint32_t, std::vector<uint32_t>> StreamMap;
58};
59
Zachary Turner819e77d2016-05-06 20:51:57 +000060static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
61 const uint64_t Size) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000062 if (Addr + Size < Addr || Addr + Size < Size ||
63 Addr + Size > uintptr_t(M.getBufferEnd()) ||
64 Addr < uintptr_t(M.getBufferStart())) {
Zachary Turner819e77d2016-05-06 20:51:57 +000065 return make_error<RawError>(raw_error_code::corrupt_file,
66 "Invalid buffer address");
Zachary Turner0a43efe2016-04-25 17:38:08 +000067 }
Zachary Turner819e77d2016-05-06 20:51:57 +000068 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +000069}
70
71template <typename T>
Zachary Turner819e77d2016-05-06 20:51:57 +000072static Error checkOffset(MemoryBufferRef M, ArrayRef<T> AR) {
Zachary Turner0a43efe2016-04-25 17:38:08 +000073 return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T));
74}
75
76PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) {
77 Context.reset(new PDBFileContext());
78 Context->Buffer = std::move(MemBuffer);
79}
80
81PDBFile::~PDBFile() {}
82
83uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; }
84
85uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; }
86
87uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; }
88
89uint32_t PDBFile::getNumDirectoryBytes() const {
90 return Context->SB->NumDirectoryBytes;
91}
92
93uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; }
94
95uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; }
96
97uint32_t PDBFile::getNumDirectoryBlocks() const {
98 return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize);
99}
100
101uint64_t PDBFile::getBlockMapOffset() const {
102 return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize;
103}
104
105uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); }
106
107uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
108 return Context->StreamSizes[StreamIndex];
109}
110
111llvm::ArrayRef<uint32_t>
112PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
113 auto &Data = Context->StreamMap[StreamIndex];
114 return llvm::ArrayRef<uint32_t>(Data);
115}
116
117StringRef PDBFile::getBlockData(uint32_t BlockIndex, uint32_t NumBytes) const {
118 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
119
120 return StringRef(Context->Buffer->getBufferStart() + StreamBlockOffset,
121 NumBytes);
122}
123
Zachary Turner819e77d2016-05-06 20:51:57 +0000124Error PDBFile::parseFileHeaders() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000125 std::error_code EC;
126 MemoryBufferRef BufferRef = *Context->Buffer;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000127
Zachary Turnerf5c59652016-05-03 00:28:21 +0000128 // Make sure the file is sufficiently large to hold a super block.
129 // Do this before attempting to read the super block.
Zachary Turnerd6192f42016-05-02 22:16:57 +0000130 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
Zachary Turner819e77d2016-05-06 20:51:57 +0000131 return make_error<RawError>(raw_error_code::corrupt_file,
132 "Does not contain superblock");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000133
134 Context->SB =
135 reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
136 const SuperBlock *SB = Context->SB;
Zachary Turner819e77d2016-05-06 20:51:57 +0000137 // Check the magic bytes.
138 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
139 return make_error<RawError>(raw_error_code::corrupt_file,
140 "MSF magic header doesn't match");
141
David Majnemer878cadb2016-05-27 15:57:38 +0000142 // We don't support blocksizes which aren't a multiple of four bytes.
143 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000144 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000145 "Block size is not multiple of 4.");
Zachary Turner819e77d2016-05-06 20:51:57 +0000146
Zachary Turner9213ba52016-04-29 18:09:19 +0000147 switch (SB->BlockSize) {
148 case 512: case 1024: case 2048: case 4096:
149 break;
150 default:
151 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000152 return make_error<RawError>(raw_error_code::corrupt_file,
153 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000154 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000155
David Majnemer878cadb2016-05-27 15:57:38 +0000156 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000157 return make_error<RawError>(raw_error_code::corrupt_file,
David Majnemer878cadb2016-05-27 15:57:38 +0000158 "File size is not a multiple of block size");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000159
160 // We don't support directories whose sizes aren't a multiple of four bytes.
161 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000162 return make_error<RawError>(raw_error_code::corrupt_file,
163 "Directory size is not multiple of 4.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000164
165 // The number of blocks which comprise the directory is a simple function of
166 // the number of bytes it contains.
167 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
168
169 // The block map, as we understand it, is a block which consists of a list of
170 // block numbers.
171 // It is unclear what would happen if the number of blocks couldn't fit on a
172 // single block.
173 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
Zachary Turner819e77d2016-05-06 20:51:57 +0000174 return make_error<RawError>(raw_error_code::corrupt_file,
175 "Too many directory blocks.");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000176
David Majnemer5d842ea2016-05-27 16:16:45 +0000177 // Make sure the directory block array fits within the file.
178 if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray()))
179 return EC;
180
Zachary Turner819e77d2016-05-06 20:51:57 +0000181 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000182}
183
Zachary Turner819e77d2016-05-06 20:51:57 +0000184Error PDBFile::parseStreamData() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000185 assert(Context && Context->SB);
186
187 bool SeenNumStreams = false;
188 uint32_t NumStreams = 0;
189 uint32_t StreamIdx = 0;
190 uint64_t DirectoryBytesRead = 0;
191
192 MemoryBufferRef M = *Context->Buffer;
193 const SuperBlock *SB = Context->SB;
194
195 auto DirectoryBlocks = getDirectoryBlockArray();
196
197 // The structure of the directory is as follows:
198 // struct PDBDirectory {
199 // uint32_t NumStreams;
200 // uint32_t StreamSizes[NumStreams];
201 // uint32_t StreamMap[NumStreams][];
202 // };
203 //
204 // Empty streams don't consume entries in the StreamMap.
205 for (uint32_t DirectoryBlockAddr : DirectoryBlocks) {
206 uint64_t DirectoryBlockOffset =
207 blockToOffset(DirectoryBlockAddr, SB->BlockSize);
208 auto DirectoryBlock =
209 makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
210 M.getBufferStart() + DirectoryBlockOffset),
211 SB->BlockSize / sizeof(support::ulittle32_t));
212 if (auto EC = checkOffset(M, DirectoryBlock))
213 return EC;
214
215 // We read data out of the directory four bytes at a time. Depending on
216 // where we are in the directory, the contents may be: the number of streams
217 // in the directory, a stream's size, or a block in the stream map.
218 for (uint32_t Data : DirectoryBlock) {
219 // Don't read beyond the end of the directory.
220 if (DirectoryBytesRead == SB->NumDirectoryBytes)
221 break;
222
223 DirectoryBytesRead += sizeof(Data);
224
225 // This data must be the number of streams if we haven't seen it yet.
226 if (!SeenNumStreams) {
227 NumStreams = Data;
228 SeenNumStreams = true;
229 continue;
230 }
231 // This data must be a stream size if we have not seen them all yet.
232 if (Context->StreamSizes.size() < NumStreams) {
233 // It seems like some streams have their set to -1 when their contents
234 // are not present. Treat them like empty streams for now.
235 if (Data == UINT32_MAX)
236 Context->StreamSizes.push_back(0);
237 else
238 Context->StreamSizes.push_back(Data);
239 continue;
240 }
241
242 // This data must be a stream block number if we have seen all of the
243 // stream sizes.
244 std::vector<uint32_t> *StreamBlocks = nullptr;
245 // Figure out which stream this block number belongs to.
246 while (StreamIdx < NumStreams) {
247 uint64_t NumExpectedStreamBlocks =
248 bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize);
249 StreamBlocks = &Context->StreamMap[StreamIdx];
250 if (NumExpectedStreamBlocks > StreamBlocks->size())
251 break;
252 ++StreamIdx;
253 }
254 // It seems this block doesn't belong to any stream? The stream is either
255 // corrupt or something more mysterious is going on.
256 if (StreamIdx == NumStreams)
Zachary Turner819e77d2016-05-06 20:51:57 +0000257 return make_error<RawError>(raw_error_code::corrupt_file,
258 "Orphaned block found?");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000259
David Majnemer9efba742016-05-27 16:16:48 +0000260 uint64_t BlockOffset = blockToOffset(Data, getBlockSize());
261 if (BlockOffset + getBlockSize() < BlockOffset)
262 return make_error<RawError>(raw_error_code::corrupt_file,
263 "Bogus stream block number");
264 if (BlockOffset + getBlockSize() > M.getBufferSize())
265 return make_error<RawError>(raw_error_code::corrupt_file,
266 "Stream block number is out of bounds");
267
Zachary Turner0a43efe2016-04-25 17:38:08 +0000268 StreamBlocks->push_back(Data);
269 }
270 }
271
David Majnemer9efba742016-05-27 16:16:48 +0000272 for (uint32_t SI = 0; SI != NumStreams; ++SI) {
273 uint64_t NumExpectedStreamBlocks =
274 bytesToBlocks(getStreamByteSize(SI), getBlockSize());
275 size_t NumStreamBlocks = getStreamBlockList(SI).size();
276 if (NumExpectedStreamBlocks != NumStreamBlocks)
277 return make_error<RawError>(raw_error_code::corrupt_file,
278 "The number of stream blocks is not "
279 "sufficient for the size of this stream");
280 }
281
Zachary Turner0a43efe2016-04-25 17:38:08 +0000282 // We should have read exactly SB->NumDirectoryBytes bytes.
283 assert(DirectoryBytesRead == SB->NumDirectoryBytes);
Zachary Turner819e77d2016-05-06 20:51:57 +0000284 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000285}
286
287llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() {
288 return makeArrayRef(
289 reinterpret_cast<const support::ulittle32_t *>(
290 Context->Buffer->getBufferStart() + getBlockMapOffset()),
291 getNumDirectoryBlocks());
292}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000293
Zachary Turner819e77d2016-05-06 20:51:57 +0000294Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000295 if (!Info) {
296 Info.reset(new InfoStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000297 if (auto EC = Info->reload())
298 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000299 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000300 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000301}
302
Zachary Turner819e77d2016-05-06 20:51:57 +0000303Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000304 if (!Dbi) {
305 Dbi.reset(new DbiStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000306 if (auto EC = Dbi->reload())
307 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000308 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000309 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000310}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000311
Zachary Turner819e77d2016-05-06 20:51:57 +0000312Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000313 if (!Tpi) {
Zachary Turnerc9972c62016-05-25 04:35:22 +0000314 Tpi.reset(new TpiStream(*this, StreamTPI));
Zachary Turner819e77d2016-05-06 20:51:57 +0000315 if (auto EC = Tpi->reload())
316 return std::move(EC);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000317 }
318 return *Tpi;
319}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000320
Zachary Turnerc9972c62016-05-25 04:35:22 +0000321Expected<TpiStream &> PDBFile::getPDBIpiStream() {
322 if (!Ipi) {
323 Ipi.reset(new TpiStream(*this, StreamIPI));
324 if (auto EC = Ipi->reload())
325 return std::move(EC);
326 }
327 return *Ipi;
328}
329
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000330Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
331 if (!Publics) {
332 auto DbiS = getPDBDbiStream();
333 if (auto EC = DbiS.takeError())
334 return std::move(EC);
335 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
336
337 Publics.reset(new PublicsStream(*this, PublicsStreamNum));
338 if (auto EC = Publics->reload())
339 return std::move(EC);
340 }
341 return *Publics;
342}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000343
344Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
345 if (!Symbols) {
346 auto DbiS = getPDBDbiStream();
347 if (auto EC = DbiS.takeError())
348 return std::move(EC);
349 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
350
351 Symbols.reset(new SymbolStream(*this, SymbolStreamNum));
352 if (auto EC = Symbols->reload())
353 return std::move(EC);
354 }
355 return *Symbols;
356}