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