Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 1 | //===- 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 Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
| 13 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Endian.h" |
| 15 | #include "llvm/Support/MemoryBuffer.h" |
| 16 | |
| 17 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 18 | using namespace llvm::pdb; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | static 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. |
| 29 | struct 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 Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 49 | struct llvm::pdb::PDBFileContext { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 50 | 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 | |
| 56 | static 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 | |
| 66 | template <typename T> |
| 67 | static 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 | |
| 71 | PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) { |
| 72 | Context.reset(new PDBFileContext()); |
| 73 | Context->Buffer = std::move(MemBuffer); |
| 74 | } |
| 75 | |
| 76 | PDBFile::~PDBFile() {} |
| 77 | |
| 78 | uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; } |
| 79 | |
| 80 | uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; } |
| 81 | |
| 82 | uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; } |
| 83 | |
| 84 | uint32_t PDBFile::getNumDirectoryBytes() const { |
| 85 | return Context->SB->NumDirectoryBytes; |
| 86 | } |
| 87 | |
| 88 | uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; } |
| 89 | |
| 90 | uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; } |
| 91 | |
| 92 | uint32_t PDBFile::getNumDirectoryBlocks() const { |
| 93 | return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize); |
| 94 | } |
| 95 | |
| 96 | uint64_t PDBFile::getBlockMapOffset() const { |
| 97 | return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize; |
| 98 | } |
| 99 | |
| 100 | uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); } |
| 101 | |
| 102 | uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const { |
| 103 | return Context->StreamSizes[StreamIndex]; |
| 104 | } |
| 105 | |
| 106 | llvm::ArrayRef<uint32_t> |
| 107 | PDBFile::getStreamBlockList(uint32_t StreamIndex) const { |
| 108 | auto &Data = Context->StreamMap[StreamIndex]; |
| 109 | return llvm::ArrayRef<uint32_t>(Data); |
| 110 | } |
| 111 | |
| 112 | StringRef 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 | |
| 119 | std::error_code PDBFile::parseFileHeaders() { |
| 120 | std::error_code EC; |
| 121 | MemoryBufferRef BufferRef = *Context->Buffer; |
Zachary Turner | d6192f4 | 2016-05-02 22:16:57 +0000 | [diff] [blame] | 122 | if (BufferRef.getBufferSize() < sizeof(SuperBlock)) |
| 123 | return std::make_error_code(std::errc::illegal_byte_sequence); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 124 | |
| 125 | Context->SB = |
| 126 | reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart()); |
| 127 | const SuperBlock *SB = Context->SB; |
Zachary Turner | 9213ba5 | 2016-04-29 18:09:19 +0000 | [diff] [blame] | 128 | switch (SB->BlockSize) { |
| 129 | case 512: case 1024: case 2048: case 4096: |
| 130 | break; |
| 131 | default: |
| 132 | // An invalid block size suggests a corrupt PDB file. |
| 133 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 134 | } |
Zachary Turner | d6192f4 | 2016-05-02 22:16:57 +0000 | [diff] [blame] | 135 | if (BufferRef.getBufferSize() % SB->BlockSize != 0) |
| 136 | return std::make_error_code(std::errc::illegal_byte_sequence); |
David Majnemer | ca9ac47 | 2016-04-29 01:00:17 +0000 | [diff] [blame] | 137 | |
| 138 | // Make sure the file is sufficiently large to hold a super block. |
| 139 | if (BufferRef.getBufferSize() < sizeof(SuperBlock)) |
| 140 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 141 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 142 | // Check the magic bytes. |
| 143 | if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0) |
| 144 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 145 | |
| 146 | // We don't support blocksizes which aren't a multiple of four bytes. |
David Majnemer | 1573b24 | 2016-04-28 23:47:27 +0000 | [diff] [blame] | 147 | if (SB->BlockSize == 0 || SB->BlockSize % sizeof(support::ulittle32_t) != 0) |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 148 | return std::make_error_code(std::errc::not_supported); |
| 149 | |
| 150 | // We don't support directories whose sizes aren't a multiple of four bytes. |
| 151 | if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0) |
| 152 | return std::make_error_code(std::errc::not_supported); |
| 153 | |
| 154 | // The number of blocks which comprise the directory is a simple function of |
| 155 | // the number of bytes it contains. |
| 156 | uint64_t NumDirectoryBlocks = getNumDirectoryBlocks(); |
| 157 | |
| 158 | // The block map, as we understand it, is a block which consists of a list of |
| 159 | // block numbers. |
| 160 | // It is unclear what would happen if the number of blocks couldn't fit on a |
| 161 | // single block. |
| 162 | if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t)) |
| 163 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 164 | |
| 165 | return std::error_code(); |
| 166 | } |
| 167 | |
| 168 | std::error_code PDBFile::parseStreamData() { |
| 169 | assert(Context && Context->SB); |
| 170 | |
| 171 | bool SeenNumStreams = false; |
| 172 | uint32_t NumStreams = 0; |
| 173 | uint32_t StreamIdx = 0; |
| 174 | uint64_t DirectoryBytesRead = 0; |
| 175 | |
| 176 | MemoryBufferRef M = *Context->Buffer; |
| 177 | const SuperBlock *SB = Context->SB; |
| 178 | |
| 179 | auto DirectoryBlocks = getDirectoryBlockArray(); |
| 180 | |
| 181 | // The structure of the directory is as follows: |
| 182 | // struct PDBDirectory { |
| 183 | // uint32_t NumStreams; |
| 184 | // uint32_t StreamSizes[NumStreams]; |
| 185 | // uint32_t StreamMap[NumStreams][]; |
| 186 | // }; |
| 187 | // |
| 188 | // Empty streams don't consume entries in the StreamMap. |
| 189 | for (uint32_t DirectoryBlockAddr : DirectoryBlocks) { |
| 190 | uint64_t DirectoryBlockOffset = |
| 191 | blockToOffset(DirectoryBlockAddr, SB->BlockSize); |
| 192 | auto DirectoryBlock = |
| 193 | makeArrayRef(reinterpret_cast<const support::ulittle32_t *>( |
| 194 | M.getBufferStart() + DirectoryBlockOffset), |
| 195 | SB->BlockSize / sizeof(support::ulittle32_t)); |
| 196 | if (auto EC = checkOffset(M, DirectoryBlock)) |
| 197 | return EC; |
| 198 | |
| 199 | // We read data out of the directory four bytes at a time. Depending on |
| 200 | // where we are in the directory, the contents may be: the number of streams |
| 201 | // in the directory, a stream's size, or a block in the stream map. |
| 202 | for (uint32_t Data : DirectoryBlock) { |
| 203 | // Don't read beyond the end of the directory. |
| 204 | if (DirectoryBytesRead == SB->NumDirectoryBytes) |
| 205 | break; |
| 206 | |
| 207 | DirectoryBytesRead += sizeof(Data); |
| 208 | |
| 209 | // This data must be the number of streams if we haven't seen it yet. |
| 210 | if (!SeenNumStreams) { |
| 211 | NumStreams = Data; |
| 212 | SeenNumStreams = true; |
| 213 | continue; |
| 214 | } |
| 215 | // This data must be a stream size if we have not seen them all yet. |
| 216 | if (Context->StreamSizes.size() < NumStreams) { |
| 217 | // It seems like some streams have their set to -1 when their contents |
| 218 | // are not present. Treat them like empty streams for now. |
| 219 | if (Data == UINT32_MAX) |
| 220 | Context->StreamSizes.push_back(0); |
| 221 | else |
| 222 | Context->StreamSizes.push_back(Data); |
| 223 | continue; |
| 224 | } |
| 225 | |
| 226 | // This data must be a stream block number if we have seen all of the |
| 227 | // stream sizes. |
| 228 | std::vector<uint32_t> *StreamBlocks = nullptr; |
| 229 | // Figure out which stream this block number belongs to. |
| 230 | while (StreamIdx < NumStreams) { |
| 231 | uint64_t NumExpectedStreamBlocks = |
| 232 | bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize); |
| 233 | StreamBlocks = &Context->StreamMap[StreamIdx]; |
| 234 | if (NumExpectedStreamBlocks > StreamBlocks->size()) |
| 235 | break; |
| 236 | ++StreamIdx; |
| 237 | } |
| 238 | // It seems this block doesn't belong to any stream? The stream is either |
| 239 | // corrupt or something more mysterious is going on. |
| 240 | if (StreamIdx == NumStreams) |
| 241 | return std::make_error_code(std::errc::illegal_byte_sequence); |
| 242 | |
| 243 | StreamBlocks->push_back(Data); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // We should have read exactly SB->NumDirectoryBytes bytes. |
| 248 | assert(DirectoryBytesRead == SB->NumDirectoryBytes); |
| 249 | return std::error_code(); |
| 250 | } |
| 251 | |
| 252 | llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() { |
| 253 | return makeArrayRef( |
| 254 | reinterpret_cast<const support::ulittle32_t *>( |
| 255 | Context->Buffer->getBufferStart() + getBlockMapOffset()), |
| 256 | getNumDirectoryBlocks()); |
| 257 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 258 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 259 | InfoStream &PDBFile::getPDBInfoStream() { |
| 260 | if (!Info) { |
| 261 | Info.reset(new InfoStream(*this)); |
| 262 | Info->reload(); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 263 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 264 | return *Info; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 267 | DbiStream &PDBFile::getPDBDbiStream() { |
| 268 | if (!Dbi) { |
| 269 | Dbi.reset(new DbiStream(*this)); |
| 270 | Dbi->reload(); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 271 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 272 | return *Dbi; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 273 | } |