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