blob: 05b3dc7fc317f15b04a841edccb59ee7747676cc [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 Turnerf5c59652016-05-03 00:28:21 +000014#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000015#include "llvm/Support/Endian.h"
16#include "llvm/Support/MemoryBuffer.h"
17
18using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000019using namespace llvm::pdb;
Zachary Turner0a43efe2016-04-25 17:38:08 +000020
21namespace {
22static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f',
23 't', ' ', 'C', '/', 'C', '+', '+', ' ',
24 'M', 'S', 'F', ' ', '7', '.', '0', '0',
25 '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'};
26
27// The superblock is overlaid at the beginning of the file (offset 0).
28// It starts with a magic header and is followed by information which describes
29// the layout of the file system.
30struct SuperBlock {
31 char MagicBytes[sizeof(Magic)];
32 // The file system is split into a variable number of fixed size elements.
33 // These elements are referred to as blocks. The size of a block may vary
34 // from system to system.
35 support::ulittle32_t BlockSize;
36 // This field's purpose is not yet known.
37 support::ulittle32_t Unknown0;
38 // This contains the number of blocks resident in the file system. In
39 // practice, NumBlocks * BlockSize is equivalent to the size of the PDB file.
40 support::ulittle32_t NumBlocks;
41 // This contains the number of bytes which make up the directory.
42 support::ulittle32_t NumDirectoryBytes;
43 // This field's purpose is not yet known.
44 support::ulittle32_t Unknown1;
45 // This contains the block # of the block map.
46 support::ulittle32_t BlockMapAddr;
47};
48}
49
Zachary Turner2f09b502016-04-29 17:28:47 +000050struct llvm::pdb::PDBFileContext {
Zachary Turner0a43efe2016-04-25 17:38:08 +000051 std::unique_ptr<MemoryBuffer> Buffer;
52 const SuperBlock *SB;
53 std::vector<uint32_t> StreamSizes;
54 DenseMap<uint32_t, std::vector<uint32_t>> StreamMap;
55};
56
57static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
58 const uint64_t Size) {
59 if (Addr + Size < Addr || Addr + Size < Size ||
60 Addr + Size > uintptr_t(M.getBufferEnd()) ||
61 Addr < uintptr_t(M.getBufferStart())) {
62 return std::make_error_code(std::errc::bad_address);
63 }
64 return std::error_code();
65}
66
67template <typename T>
68static std::error_code checkOffset(MemoryBufferRef M, ArrayRef<T> AR) {
69 return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T));
70}
71
72PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) {
73 Context.reset(new PDBFileContext());
74 Context->Buffer = std::move(MemBuffer);
75}
76
77PDBFile::~PDBFile() {}
78
79uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; }
80
81uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; }
82
83uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; }
84
85uint32_t PDBFile::getNumDirectoryBytes() const {
86 return Context->SB->NumDirectoryBytes;
87}
88
89uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; }
90
91uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; }
92
93uint32_t PDBFile::getNumDirectoryBlocks() const {
94 return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize);
95}
96
97uint64_t PDBFile::getBlockMapOffset() const {
98 return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize;
99}
100
101uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); }
102
103uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const {
104 return Context->StreamSizes[StreamIndex];
105}
106
107llvm::ArrayRef<uint32_t>
108PDBFile::getStreamBlockList(uint32_t StreamIndex) const {
109 auto &Data = Context->StreamMap[StreamIndex];
110 return llvm::ArrayRef<uint32_t>(Data);
111}
112
113StringRef PDBFile::getBlockData(uint32_t BlockIndex, uint32_t NumBytes) const {
114 uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize());
115
116 return StringRef(Context->Buffer->getBufferStart() + StreamBlockOffset,
117 NumBytes);
118}
119
120std::error_code PDBFile::parseFileHeaders() {
121 std::error_code EC;
122 MemoryBufferRef BufferRef = *Context->Buffer;
Zachary Turnerf5c59652016-05-03 00:28:21 +0000123 // Make sure the file is sufficiently large to hold a super block.
124 // Do this before attempting to read the super block.
Zachary Turnerd6192f42016-05-02 22:16:57 +0000125 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
126 return std::make_error_code(std::errc::illegal_byte_sequence);
Zachary Turner0a43efe2016-04-25 17:38:08 +0000127
128 Context->SB =
129 reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart());
130 const SuperBlock *SB = Context->SB;
Zachary Turner9213ba52016-04-29 18:09:19 +0000131 switch (SB->BlockSize) {
132 case 512: case 1024: case 2048: case 4096:
133 break;
134 default:
135 // An invalid block size suggests a corrupt PDB file.
136 return std::make_error_code(std::errc::illegal_byte_sequence);
137 }
Zachary Turnerd6192f42016-05-02 22:16:57 +0000138 if (BufferRef.getBufferSize() % SB->BlockSize != 0)
139 return std::make_error_code(std::errc::illegal_byte_sequence);
David Majnemerca9ac472016-04-29 01:00:17 +0000140
Zachary Turner0a43efe2016-04-25 17:38:08 +0000141 // Check the magic bytes.
142 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
143 return std::make_error_code(std::errc::illegal_byte_sequence);
144
145 // We don't support blocksizes which aren't a multiple of four bytes.
David Majnemer1573b242016-04-28 23:47:27 +0000146 if (SB->BlockSize == 0 || SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner0a43efe2016-04-25 17:38:08 +0000147 return std::make_error_code(std::errc::not_supported);
148
149 // We don't support directories whose sizes aren't a multiple of four bytes.
150 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
151 return std::make_error_code(std::errc::not_supported);
152
153 // The number of blocks which comprise the directory is a simple function of
154 // the number of bytes it contains.
155 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
156
157 // The block map, as we understand it, is a block which consists of a list of
158 // block numbers.
159 // It is unclear what would happen if the number of blocks couldn't fit on a
160 // single block.
161 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
162 return std::make_error_code(std::errc::illegal_byte_sequence);
163
164 return std::error_code();
165}
166
167std::error_code PDBFile::parseStreamData() {
168 assert(Context && Context->SB);
169
170 bool SeenNumStreams = false;
171 uint32_t NumStreams = 0;
172 uint32_t StreamIdx = 0;
173 uint64_t DirectoryBytesRead = 0;
174
175 MemoryBufferRef M = *Context->Buffer;
176 const SuperBlock *SB = Context->SB;
177
178 auto DirectoryBlocks = getDirectoryBlockArray();
179
180 // The structure of the directory is as follows:
181 // struct PDBDirectory {
182 // uint32_t NumStreams;
183 // uint32_t StreamSizes[NumStreams];
184 // uint32_t StreamMap[NumStreams][];
185 // };
186 //
187 // Empty streams don't consume entries in the StreamMap.
188 for (uint32_t DirectoryBlockAddr : DirectoryBlocks) {
189 uint64_t DirectoryBlockOffset =
190 blockToOffset(DirectoryBlockAddr, SB->BlockSize);
191 auto DirectoryBlock =
192 makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
193 M.getBufferStart() + DirectoryBlockOffset),
194 SB->BlockSize / sizeof(support::ulittle32_t));
195 if (auto EC = checkOffset(M, DirectoryBlock))
196 return EC;
197
198 // We read data out of the directory four bytes at a time. Depending on
199 // where we are in the directory, the contents may be: the number of streams
200 // in the directory, a stream's size, or a block in the stream map.
201 for (uint32_t Data : DirectoryBlock) {
202 // Don't read beyond the end of the directory.
203 if (DirectoryBytesRead == SB->NumDirectoryBytes)
204 break;
205
206 DirectoryBytesRead += sizeof(Data);
207
208 // This data must be the number of streams if we haven't seen it yet.
209 if (!SeenNumStreams) {
210 NumStreams = Data;
211 SeenNumStreams = true;
212 continue;
213 }
214 // This data must be a stream size if we have not seen them all yet.
215 if (Context->StreamSizes.size() < NumStreams) {
216 // It seems like some streams have their set to -1 when their contents
217 // are not present. Treat them like empty streams for now.
218 if (Data == UINT32_MAX)
219 Context->StreamSizes.push_back(0);
220 else
221 Context->StreamSizes.push_back(Data);
222 continue;
223 }
224
225 // This data must be a stream block number if we have seen all of the
226 // stream sizes.
227 std::vector<uint32_t> *StreamBlocks = nullptr;
228 // Figure out which stream this block number belongs to.
229 while (StreamIdx < NumStreams) {
230 uint64_t NumExpectedStreamBlocks =
231 bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize);
232 StreamBlocks = &Context->StreamMap[StreamIdx];
233 if (NumExpectedStreamBlocks > StreamBlocks->size())
234 break;
235 ++StreamIdx;
236 }
237 // It seems this block doesn't belong to any stream? The stream is either
238 // corrupt or something more mysterious is going on.
239 if (StreamIdx == NumStreams)
240 return std::make_error_code(std::errc::illegal_byte_sequence);
241
242 StreamBlocks->push_back(Data);
243 }
244 }
245
246 // We should have read exactly SB->NumDirectoryBytes bytes.
247 assert(DirectoryBytesRead == SB->NumDirectoryBytes);
248 return std::error_code();
249}
250
251llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() {
252 return makeArrayRef(
253 reinterpret_cast<const support::ulittle32_t *>(
254 Context->Buffer->getBufferStart() + getBlockMapOffset()),
255 getNumDirectoryBlocks());
256}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000257
Zachary Turner2f09b502016-04-29 17:28:47 +0000258InfoStream &PDBFile::getPDBInfoStream() {
259 if (!Info) {
260 Info.reset(new InfoStream(*this));
261 Info->reload();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000262 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000263 return *Info;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000264}
265
Zachary Turner2f09b502016-04-29 17:28:47 +0000266DbiStream &PDBFile::getPDBDbiStream() {
267 if (!Dbi) {
268 Dbi.reset(new DbiStream(*this));
269 Dbi->reload();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000270 }
Zachary Turner2f09b502016-04-29 17:28:47 +0000271 return *Dbi;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000272}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000273
274TpiStream &PDBFile::getPDBTpiStream() {
275 if (!Tpi) {
276 Tpi.reset(new TpiStream(*this));
277 Tpi->reload();
278 }
279 return *Tpi;
280}