blob: 0adabfe625c8d981a6cecaf3b2bfa200cebaae30 [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;
David Majnemerca9ac472016-04-29 01:00:17 +0000125
126 // Make sure the file is sufficiently large to hold a super block.
127 if (BufferRef.getBufferSize() < sizeof(SuperBlock))
128 return std::make_error_code(std::errc::illegal_byte_sequence);
129
Zachary Turner0a43efe2016-04-25 17:38:08 +0000130 // Check the magic bytes.
131 if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0)
132 return std::make_error_code(std::errc::illegal_byte_sequence);
133
134 // We don't support blocksizes which aren't a multiple of four bytes.
David Majnemer1573b242016-04-28 23:47:27 +0000135 if (SB->BlockSize == 0 || SB->BlockSize % sizeof(support::ulittle32_t) != 0)
Zachary Turner0a43efe2016-04-25 17:38:08 +0000136 return std::make_error_code(std::errc::not_supported);
137
138 // We don't support directories whose sizes aren't a multiple of four bytes.
139 if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
140 return std::make_error_code(std::errc::not_supported);
141
142 // The number of blocks which comprise the directory is a simple function of
143 // the number of bytes it contains.
144 uint64_t NumDirectoryBlocks = getNumDirectoryBlocks();
145
146 // The block map, as we understand it, is a block which consists of a list of
147 // block numbers.
148 // It is unclear what would happen if the number of blocks couldn't fit on a
149 // single block.
150 if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
151 return std::make_error_code(std::errc::illegal_byte_sequence);
152
153 return std::error_code();
154}
155
156std::error_code PDBFile::parseStreamData() {
157 assert(Context && Context->SB);
158
159 bool SeenNumStreams = false;
160 uint32_t NumStreams = 0;
161 uint32_t StreamIdx = 0;
162 uint64_t DirectoryBytesRead = 0;
163
164 MemoryBufferRef M = *Context->Buffer;
165 const SuperBlock *SB = Context->SB;
166
167 auto DirectoryBlocks = getDirectoryBlockArray();
168
169 // The structure of the directory is as follows:
170 // struct PDBDirectory {
171 // uint32_t NumStreams;
172 // uint32_t StreamSizes[NumStreams];
173 // uint32_t StreamMap[NumStreams][];
174 // };
175 //
176 // Empty streams don't consume entries in the StreamMap.
177 for (uint32_t DirectoryBlockAddr : DirectoryBlocks) {
178 uint64_t DirectoryBlockOffset =
179 blockToOffset(DirectoryBlockAddr, SB->BlockSize);
180 auto DirectoryBlock =
181 makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
182 M.getBufferStart() + DirectoryBlockOffset),
183 SB->BlockSize / sizeof(support::ulittle32_t));
184 if (auto EC = checkOffset(M, DirectoryBlock))
185 return EC;
186
187 // We read data out of the directory four bytes at a time. Depending on
188 // where we are in the directory, the contents may be: the number of streams
189 // in the directory, a stream's size, or a block in the stream map.
190 for (uint32_t Data : DirectoryBlock) {
191 // Don't read beyond the end of the directory.
192 if (DirectoryBytesRead == SB->NumDirectoryBytes)
193 break;
194
195 DirectoryBytesRead += sizeof(Data);
196
197 // This data must be the number of streams if we haven't seen it yet.
198 if (!SeenNumStreams) {
199 NumStreams = Data;
200 SeenNumStreams = true;
201 continue;
202 }
203 // This data must be a stream size if we have not seen them all yet.
204 if (Context->StreamSizes.size() < NumStreams) {
205 // It seems like some streams have their set to -1 when their contents
206 // are not present. Treat them like empty streams for now.
207 if (Data == UINT32_MAX)
208 Context->StreamSizes.push_back(0);
209 else
210 Context->StreamSizes.push_back(Data);
211 continue;
212 }
213
214 // This data must be a stream block number if we have seen all of the
215 // stream sizes.
216 std::vector<uint32_t> *StreamBlocks = nullptr;
217 // Figure out which stream this block number belongs to.
218 while (StreamIdx < NumStreams) {
219 uint64_t NumExpectedStreamBlocks =
220 bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize);
221 StreamBlocks = &Context->StreamMap[StreamIdx];
222 if (NumExpectedStreamBlocks > StreamBlocks->size())
223 break;
224 ++StreamIdx;
225 }
226 // It seems this block doesn't belong to any stream? The stream is either
227 // corrupt or something more mysterious is going on.
228 if (StreamIdx == NumStreams)
229 return std::make_error_code(std::errc::illegal_byte_sequence);
230
231 StreamBlocks->push_back(Data);
232 }
233 }
234
235 // We should have read exactly SB->NumDirectoryBytes bytes.
236 assert(DirectoryBytesRead == SB->NumDirectoryBytes);
237 return std::error_code();
238}
239
240llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() {
241 return makeArrayRef(
242 reinterpret_cast<const support::ulittle32_t *>(
243 Context->Buffer->getBufferStart() + getBlockMapOffset()),
244 getNumDirectoryBlocks());
245}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000246
247PDBInfoStream &PDBFile::getPDBInfoStream() {
248 if (!InfoStream) {
249 InfoStream.reset(new PDBInfoStream(*this));
250 InfoStream->reload();
251 }
252 return *InfoStream;
253}
254
255PDBDbiStream &PDBFile::getPDBDbiStream() {
256 if (!DbiStream) {
257 DbiStream.reset(new PDBDbiStream(*this));
258 DbiStream->reload();
259 }
260 return *DbiStream;
261}