blob: 594739f4b8851951ba5a217a14954fb30144043a [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
142 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
143 return make_error<RawError>(raw_error_code::corrupt_file,
144 "File size is not a multiple of block size");
145
Zachary Turner9213ba52016-04-29 18:09:19 +0000146 switch (SB->BlockSize) {
147 case 512: case 1024: case 2048: case 4096:
148 break;
149 default:
150 // An invalid block size suggests a corrupt PDB file.
Zachary Turner819e77d2016-05-06 20:51:57 +0000151 return make_error<RawError>(raw_error_code::corrupt_file,
152 "Unsupported block size.");
Zachary Turner9213ba52016-04-29 18:09:19 +0000153 }
Zachary Turner0a43efe2016-04-25 17:38:08 +0000154
155 // We don't support blocksizes which aren't a multiple of four bytes.
Zachary Turner819e77d2016-05-06 20:51:57 +0000156 if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
157 return make_error<RawError>(raw_error_code::corrupt_file,
158 "Block size is not multiple of 4.");
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
Zachary Turner819e77d2016-05-06 20:51:57 +0000177 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000178}
179
Zachary Turner819e77d2016-05-06 20:51:57 +0000180Error PDBFile::parseStreamData() {
Zachary Turner0a43efe2016-04-25 17:38:08 +0000181 assert(Context && Context->SB);
182
183 bool SeenNumStreams = false;
184 uint32_t NumStreams = 0;
185 uint32_t StreamIdx = 0;
186 uint64_t DirectoryBytesRead = 0;
187
188 MemoryBufferRef M = *Context->Buffer;
189 const SuperBlock *SB = Context->SB;
190
191 auto DirectoryBlocks = getDirectoryBlockArray();
192
193 // The structure of the directory is as follows:
194 // struct PDBDirectory {
195 // uint32_t NumStreams;
196 // uint32_t StreamSizes[NumStreams];
197 // uint32_t StreamMap[NumStreams][];
198 // };
199 //
200 // Empty streams don't consume entries in the StreamMap.
201 for (uint32_t DirectoryBlockAddr : DirectoryBlocks) {
202 uint64_t DirectoryBlockOffset =
203 blockToOffset(DirectoryBlockAddr, SB->BlockSize);
204 auto DirectoryBlock =
205 makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
206 M.getBufferStart() + DirectoryBlockOffset),
207 SB->BlockSize / sizeof(support::ulittle32_t));
208 if (auto EC = checkOffset(M, DirectoryBlock))
209 return EC;
210
211 // We read data out of the directory four bytes at a time. Depending on
212 // where we are in the directory, the contents may be: the number of streams
213 // in the directory, a stream's size, or a block in the stream map.
214 for (uint32_t Data : DirectoryBlock) {
215 // Don't read beyond the end of the directory.
216 if (DirectoryBytesRead == SB->NumDirectoryBytes)
217 break;
218
219 DirectoryBytesRead += sizeof(Data);
220
221 // This data must be the number of streams if we haven't seen it yet.
222 if (!SeenNumStreams) {
223 NumStreams = Data;
224 SeenNumStreams = true;
225 continue;
226 }
227 // This data must be a stream size if we have not seen them all yet.
228 if (Context->StreamSizes.size() < NumStreams) {
229 // It seems like some streams have their set to -1 when their contents
230 // are not present. Treat them like empty streams for now.
231 if (Data == UINT32_MAX)
232 Context->StreamSizes.push_back(0);
233 else
234 Context->StreamSizes.push_back(Data);
235 continue;
236 }
237
238 // This data must be a stream block number if we have seen all of the
239 // stream sizes.
240 std::vector<uint32_t> *StreamBlocks = nullptr;
241 // Figure out which stream this block number belongs to.
242 while (StreamIdx < NumStreams) {
243 uint64_t NumExpectedStreamBlocks =
244 bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize);
245 StreamBlocks = &Context->StreamMap[StreamIdx];
246 if (NumExpectedStreamBlocks > StreamBlocks->size())
247 break;
248 ++StreamIdx;
249 }
250 // It seems this block doesn't belong to any stream? The stream is either
251 // corrupt or something more mysterious is going on.
252 if (StreamIdx == NumStreams)
Zachary Turner819e77d2016-05-06 20:51:57 +0000253 return make_error<RawError>(raw_error_code::corrupt_file,
254 "Orphaned block found?");
Zachary Turner0a43efe2016-04-25 17:38:08 +0000255
256 StreamBlocks->push_back(Data);
257 }
258 }
259
260 // We should have read exactly SB->NumDirectoryBytes bytes.
261 assert(DirectoryBytesRead == SB->NumDirectoryBytes);
Zachary Turner819e77d2016-05-06 20:51:57 +0000262 return Error::success();
Zachary Turner0a43efe2016-04-25 17:38:08 +0000263}
264
265llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() {
266 return makeArrayRef(
267 reinterpret_cast<const support::ulittle32_t *>(
268 Context->Buffer->getBufferStart() + getBlockMapOffset()),
269 getNumDirectoryBlocks());
270}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000271
Zachary Turner819e77d2016-05-06 20:51:57 +0000272Expected<InfoStream &> PDBFile::getPDBInfoStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000273 if (!Info) {
274 Info.reset(new InfoStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000275 if (auto EC = Info->reload())
276 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000277 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000278 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000279}
280
Zachary Turner819e77d2016-05-06 20:51:57 +0000281Expected<DbiStream &> PDBFile::getPDBDbiStream() {
Zachary Turner2f09b502016-04-29 17:28:47 +0000282 if (!Dbi) {
283 Dbi.reset(new DbiStream(*this));
Zachary Turner819e77d2016-05-06 20:51:57 +0000284 if (auto EC = Dbi->reload())
285 return std::move(EC);
Zachary Turner53a65ba2016-04-26 18:42:34 +0000286 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000287 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000288}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000289
Zachary Turner819e77d2016-05-06 20:51:57 +0000290Expected<TpiStream &> PDBFile::getPDBTpiStream() {
Zachary Turnerf5c59652016-05-03 00:28:21 +0000291 if (!Tpi) {
Zachary Turnerc9972c62016-05-25 04:35:22 +0000292 Tpi.reset(new TpiStream(*this, StreamTPI));
Zachary Turner819e77d2016-05-06 20:51:57 +0000293 if (auto EC = Tpi->reload())
294 return std::move(EC);
Zachary Turnerf5c59652016-05-03 00:28:21 +0000295 }
296 return *Tpi;
297}
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000298
Zachary Turnerc9972c62016-05-25 04:35:22 +0000299Expected<TpiStream &> PDBFile::getPDBIpiStream() {
300 if (!Ipi) {
301 Ipi.reset(new TpiStream(*this, StreamIPI));
302 if (auto EC = Ipi->reload())
303 return std::move(EC);
304 }
305 return *Ipi;
306}
307
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000308Expected<PublicsStream &> PDBFile::getPDBPublicsStream() {
309 if (!Publics) {
310 auto DbiS = getPDBDbiStream();
311 if (auto EC = DbiS.takeError())
312 return std::move(EC);
313 uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex();
314
315 Publics.reset(new PublicsStream(*this, PublicsStreamNum));
316 if (auto EC = Publics->reload())
317 return std::move(EC);
318 }
319 return *Publics;
320}
Rui Ueyama0fcd8262016-05-20 19:55:17 +0000321
322Expected<SymbolStream &> PDBFile::getPDBSymbolStream() {
323 if (!Symbols) {
324 auto DbiS = getPDBDbiStream();
325 if (auto EC = DbiS.takeError())
326 return std::move(EC);
327 uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex();
328
329 Symbols.reset(new SymbolStream(*this, SymbolStreamNum));
330 if (auto EC = Symbols->reload())
331 return std::move(EC);
332 }
333 return *Symbols;
334}