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