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 | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Raw/PublicsStream.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Raw/SymbolStream.h" |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Endian.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | |
| 22 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 23 | using namespace llvm::pdb; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | static const char Magic[] = {'M', 'i', 'c', 'r', 'o', 's', 'o', 'f', |
| 27 | 't', ' ', 'C', '/', 'C', '+', '+', ' ', |
| 28 | 'M', 'S', 'F', ' ', '7', '.', '0', '0', |
| 29 | '\r', '\n', '\x1a', 'D', 'S', '\0', '\0', '\0'}; |
| 30 | |
| 31 | // The superblock is overlaid at the beginning of the file (offset 0). |
| 32 | // It starts with a magic header and is followed by information which describes |
| 33 | // the layout of the file system. |
| 34 | struct SuperBlock { |
| 35 | char MagicBytes[sizeof(Magic)]; |
| 36 | // The file system is split into a variable number of fixed size elements. |
| 37 | // These elements are referred to as blocks. The size of a block may vary |
| 38 | // from system to system. |
| 39 | support::ulittle32_t BlockSize; |
| 40 | // This field's purpose is not yet known. |
| 41 | support::ulittle32_t Unknown0; |
| 42 | // This contains the number of blocks resident in the file system. In |
| 43 | // practice, NumBlocks * BlockSize is equivalent to the size of the PDB file. |
| 44 | support::ulittle32_t NumBlocks; |
| 45 | // This contains the number of bytes which make up the directory. |
| 46 | support::ulittle32_t NumDirectoryBytes; |
| 47 | // This field's purpose is not yet known. |
| 48 | support::ulittle32_t Unknown1; |
| 49 | // This contains the block # of the block map. |
| 50 | support::ulittle32_t BlockMapAddr; |
| 51 | }; |
| 52 | } |
| 53 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 54 | struct llvm::pdb::PDBFileContext { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 55 | std::unique_ptr<MemoryBuffer> Buffer; |
| 56 | const SuperBlock *SB; |
| 57 | std::vector<uint32_t> StreamSizes; |
| 58 | DenseMap<uint32_t, std::vector<uint32_t>> StreamMap; |
| 59 | }; |
| 60 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 61 | static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, |
| 62 | const uint64_t Size) { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 63 | if (Addr + Size < Addr || Addr + Size < Size || |
| 64 | Addr + Size > uintptr_t(M.getBufferEnd()) || |
| 65 | Addr < uintptr_t(M.getBufferStart())) { |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 66 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 67 | "Invalid buffer address"); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 68 | } |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 69 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | template <typename T> |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 73 | static Error checkOffset(MemoryBufferRef M, ArrayRef<T> AR) { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 74 | return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T)); |
| 75 | } |
| 76 | |
| 77 | PDBFile::PDBFile(std::unique_ptr<MemoryBuffer> MemBuffer) { |
| 78 | Context.reset(new PDBFileContext()); |
| 79 | Context->Buffer = std::move(MemBuffer); |
| 80 | } |
| 81 | |
| 82 | PDBFile::~PDBFile() {} |
| 83 | |
| 84 | uint32_t PDBFile::getBlockSize() const { return Context->SB->BlockSize; } |
| 85 | |
| 86 | uint32_t PDBFile::getUnknown0() const { return Context->SB->Unknown0; } |
| 87 | |
| 88 | uint32_t PDBFile::getBlockCount() const { return Context->SB->NumBlocks; } |
| 89 | |
| 90 | uint32_t PDBFile::getNumDirectoryBytes() const { |
| 91 | return Context->SB->NumDirectoryBytes; |
| 92 | } |
| 93 | |
| 94 | uint32_t PDBFile::getBlockMapIndex() const { return Context->SB->BlockMapAddr; } |
| 95 | |
| 96 | uint32_t PDBFile::getUnknown1() const { return Context->SB->Unknown1; } |
| 97 | |
| 98 | uint32_t PDBFile::getNumDirectoryBlocks() const { |
| 99 | return bytesToBlocks(Context->SB->NumDirectoryBytes, Context->SB->BlockSize); |
| 100 | } |
| 101 | |
| 102 | uint64_t PDBFile::getBlockMapOffset() const { |
| 103 | return (uint64_t)Context->SB->BlockMapAddr * Context->SB->BlockSize; |
| 104 | } |
| 105 | |
| 106 | uint32_t PDBFile::getNumStreams() const { return Context->StreamSizes.size(); } |
| 107 | |
| 108 | uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const { |
| 109 | return Context->StreamSizes[StreamIndex]; |
| 110 | } |
| 111 | |
| 112 | llvm::ArrayRef<uint32_t> |
| 113 | PDBFile::getStreamBlockList(uint32_t StreamIndex) const { |
| 114 | auto &Data = Context->StreamMap[StreamIndex]; |
| 115 | return llvm::ArrayRef<uint32_t>(Data); |
| 116 | } |
| 117 | |
| 118 | StringRef PDBFile::getBlockData(uint32_t BlockIndex, uint32_t NumBytes) const { |
| 119 | uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize()); |
| 120 | |
| 121 | return StringRef(Context->Buffer->getBufferStart() + StreamBlockOffset, |
| 122 | NumBytes); |
| 123 | } |
| 124 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 125 | Error PDBFile::parseFileHeaders() { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 126 | std::error_code EC; |
| 127 | MemoryBufferRef BufferRef = *Context->Buffer; |
Zachary Turner | c59261c | 2016-05-25 03:53:16 +0000 | [diff] [blame] | 128 | |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 129 | // Make sure the file is sufficiently large to hold a super block. |
| 130 | // Do this before attempting to read the super block. |
Zachary Turner | d6192f4 | 2016-05-02 22:16:57 +0000 | [diff] [blame] | 131 | if (BufferRef.getBufferSize() < sizeof(SuperBlock)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 132 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 133 | "Does not contain superblock"); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 134 | |
| 135 | Context->SB = |
| 136 | reinterpret_cast<const SuperBlock *>(BufferRef.getBufferStart()); |
| 137 | const SuperBlock *SB = Context->SB; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 138 | // Check the magic bytes. |
| 139 | if (memcmp(SB->MagicBytes, Magic, sizeof(Magic)) != 0) |
| 140 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 141 | "MSF magic header doesn't match"); |
| 142 | |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 143 | // We don't support blocksizes which aren't a multiple of four bytes. |
| 144 | if (SB->BlockSize % sizeof(support::ulittle32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 145 | return make_error<RawError>(raw_error_code::corrupt_file, |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 146 | "Block size is not multiple of 4."); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 147 | |
Zachary Turner | 9213ba5 | 2016-04-29 18:09:19 +0000 | [diff] [blame] | 148 | switch (SB->BlockSize) { |
| 149 | case 512: case 1024: case 2048: case 4096: |
| 150 | break; |
| 151 | default: |
| 152 | // An invalid block size suggests a corrupt PDB file. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 153 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 154 | "Unsupported block size."); |
Zachary Turner | 9213ba5 | 2016-04-29 18:09:19 +0000 | [diff] [blame] | 155 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 156 | |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 157 | if (BufferRef.getBufferSize() % SB->BlockSize != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 158 | return make_error<RawError>(raw_error_code::corrupt_file, |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 159 | "File size is not a multiple of block size"); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 160 | |
| 161 | // We don't support directories whose sizes aren't a multiple of four bytes. |
| 162 | if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 163 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 164 | "Directory size is not multiple of 4."); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 165 | |
| 166 | // The number of blocks which comprise the directory is a simple function of |
| 167 | // the number of bytes it contains. |
| 168 | uint64_t NumDirectoryBlocks = getNumDirectoryBlocks(); |
| 169 | |
| 170 | // The block map, as we understand it, is a block which consists of a list of |
| 171 | // block numbers. |
| 172 | // It is unclear what would happen if the number of blocks couldn't fit on a |
| 173 | // single block. |
| 174 | if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 175 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 176 | "Too many directory blocks."); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 177 | |
David Majnemer | 5d842ea | 2016-05-27 16:16:45 +0000 | [diff] [blame] | 178 | // Make sure the directory block array fits within the file. |
| 179 | if (auto EC = checkOffset(BufferRef, getDirectoryBlockArray())) |
| 180 | return EC; |
| 181 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 182 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 185 | Error PDBFile::parseStreamData() { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 186 | assert(Context && Context->SB); |
| 187 | |
| 188 | bool SeenNumStreams = false; |
| 189 | uint32_t NumStreams = 0; |
| 190 | uint32_t StreamIdx = 0; |
| 191 | uint64_t DirectoryBytesRead = 0; |
| 192 | |
| 193 | MemoryBufferRef M = *Context->Buffer; |
| 194 | const SuperBlock *SB = Context->SB; |
| 195 | |
| 196 | auto DirectoryBlocks = getDirectoryBlockArray(); |
| 197 | |
| 198 | // The structure of the directory is as follows: |
| 199 | // struct PDBDirectory { |
| 200 | // uint32_t NumStreams; |
| 201 | // uint32_t StreamSizes[NumStreams]; |
| 202 | // uint32_t StreamMap[NumStreams][]; |
| 203 | // }; |
| 204 | // |
| 205 | // Empty streams don't consume entries in the StreamMap. |
| 206 | for (uint32_t DirectoryBlockAddr : DirectoryBlocks) { |
| 207 | uint64_t DirectoryBlockOffset = |
| 208 | blockToOffset(DirectoryBlockAddr, SB->BlockSize); |
| 209 | auto DirectoryBlock = |
| 210 | makeArrayRef(reinterpret_cast<const support::ulittle32_t *>( |
| 211 | M.getBufferStart() + DirectoryBlockOffset), |
| 212 | SB->BlockSize / sizeof(support::ulittle32_t)); |
| 213 | if (auto EC = checkOffset(M, DirectoryBlock)) |
| 214 | return EC; |
| 215 | |
| 216 | // We read data out of the directory four bytes at a time. Depending on |
| 217 | // where we are in the directory, the contents may be: the number of streams |
| 218 | // in the directory, a stream's size, or a block in the stream map. |
| 219 | for (uint32_t Data : DirectoryBlock) { |
| 220 | // Don't read beyond the end of the directory. |
| 221 | if (DirectoryBytesRead == SB->NumDirectoryBytes) |
| 222 | break; |
| 223 | |
| 224 | DirectoryBytesRead += sizeof(Data); |
| 225 | |
| 226 | // This data must be the number of streams if we haven't seen it yet. |
| 227 | if (!SeenNumStreams) { |
| 228 | NumStreams = Data; |
| 229 | SeenNumStreams = true; |
| 230 | continue; |
| 231 | } |
| 232 | // This data must be a stream size if we have not seen them all yet. |
| 233 | if (Context->StreamSizes.size() < NumStreams) { |
| 234 | // It seems like some streams have their set to -1 when their contents |
| 235 | // are not present. Treat them like empty streams for now. |
| 236 | if (Data == UINT32_MAX) |
| 237 | Context->StreamSizes.push_back(0); |
| 238 | else |
| 239 | Context->StreamSizes.push_back(Data); |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | // This data must be a stream block number if we have seen all of the |
| 244 | // stream sizes. |
| 245 | std::vector<uint32_t> *StreamBlocks = nullptr; |
| 246 | // Figure out which stream this block number belongs to. |
| 247 | while (StreamIdx < NumStreams) { |
| 248 | uint64_t NumExpectedStreamBlocks = |
| 249 | bytesToBlocks(Context->StreamSizes[StreamIdx], SB->BlockSize); |
| 250 | StreamBlocks = &Context->StreamMap[StreamIdx]; |
| 251 | if (NumExpectedStreamBlocks > StreamBlocks->size()) |
| 252 | break; |
| 253 | ++StreamIdx; |
| 254 | } |
| 255 | // It seems this block doesn't belong to any stream? The stream is either |
| 256 | // corrupt or something more mysterious is going on. |
| 257 | if (StreamIdx == NumStreams) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 258 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 259 | "Orphaned block found?"); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 260 | |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 261 | uint64_t BlockOffset = blockToOffset(Data, getBlockSize()); |
| 262 | if (BlockOffset + getBlockSize() < BlockOffset) |
| 263 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 264 | "Bogus stream block number"); |
| 265 | if (BlockOffset + getBlockSize() > M.getBufferSize()) |
| 266 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 267 | "Stream block number is out of bounds"); |
| 268 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 269 | StreamBlocks->push_back(Data); |
| 270 | } |
| 271 | } |
| 272 | |
David Majnemer | 7e950b2 | 2016-05-28 05:59:19 +0000 | [diff] [blame] | 273 | if (Context->StreamSizes.size() != NumStreams) |
| 274 | return make_error<RawError>( |
| 275 | raw_error_code::corrupt_file, |
| 276 | "The directory has fewer streams then expected"); |
| 277 | |
| 278 | for (uint32_t I = 0; I != NumStreams; ++I) { |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 279 | uint64_t NumExpectedStreamBlocks = |
David Majnemer | 7e950b2 | 2016-05-28 05:59:19 +0000 | [diff] [blame] | 280 | bytesToBlocks(getStreamByteSize(I), getBlockSize()); |
| 281 | size_t NumStreamBlocks = getStreamBlockList(I).size(); |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 282 | if (NumExpectedStreamBlocks != NumStreamBlocks) |
| 283 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 284 | "The number of stream blocks is not " |
| 285 | "sufficient for the size of this stream"); |
| 286 | } |
| 287 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 288 | // We should have read exactly SB->NumDirectoryBytes bytes. |
| 289 | assert(DirectoryBytesRead == SB->NumDirectoryBytes); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 290 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() { |
| 294 | return makeArrayRef( |
| 295 | reinterpret_cast<const support::ulittle32_t *>( |
| 296 | Context->Buffer->getBufferStart() + getBlockMapOffset()), |
| 297 | getNumDirectoryBlocks()); |
| 298 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 299 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 300 | Expected<InfoStream &> PDBFile::getPDBInfoStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 301 | if (!Info) { |
| 302 | Info.reset(new InfoStream(*this)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 303 | if (auto EC = Info->reload()) |
| 304 | return std::move(EC); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 305 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 306 | return *Info; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 309 | Expected<DbiStream &> PDBFile::getPDBDbiStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 310 | if (!Dbi) { |
| 311 | Dbi.reset(new DbiStream(*this)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 312 | if (auto EC = Dbi->reload()) |
| 313 | return std::move(EC); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 314 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 315 | return *Dbi; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 316 | } |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 317 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 318 | Expected<TpiStream &> PDBFile::getPDBTpiStream() { |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 319 | if (!Tpi) { |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 320 | Tpi.reset(new TpiStream(*this, StreamTPI)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 321 | if (auto EC = Tpi->reload()) |
| 322 | return std::move(EC); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 323 | } |
| 324 | return *Tpi; |
| 325 | } |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 326 | |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 327 | Expected<TpiStream &> PDBFile::getPDBIpiStream() { |
| 328 | if (!Ipi) { |
| 329 | Ipi.reset(new TpiStream(*this, StreamIPI)); |
| 330 | if (auto EC = Ipi->reload()) |
| 331 | return std::move(EC); |
| 332 | } |
| 333 | return *Ipi; |
| 334 | } |
| 335 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 336 | Expected<PublicsStream &> PDBFile::getPDBPublicsStream() { |
| 337 | if (!Publics) { |
| 338 | auto DbiS = getPDBDbiStream(); |
| 339 | if (auto EC = DbiS.takeError()) |
| 340 | return std::move(EC); |
| 341 | uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex(); |
| 342 | |
| 343 | Publics.reset(new PublicsStream(*this, PublicsStreamNum)); |
| 344 | if (auto EC = Publics->reload()) |
| 345 | return std::move(EC); |
| 346 | } |
| 347 | return *Publics; |
| 348 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 349 | |
| 350 | Expected<SymbolStream &> PDBFile::getPDBSymbolStream() { |
| 351 | if (!Symbols) { |
| 352 | auto DbiS = getPDBDbiStream(); |
| 353 | if (auto EC = DbiS.takeError()) |
| 354 | return std::move(EC); |
| 355 | uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex(); |
| 356 | |
| 357 | Symbols.reset(new SymbolStream(*this, SymbolStreamNum)); |
| 358 | if (auto EC = Symbols->reload()) |
| 359 | return std::move(EC); |
| 360 | } |
| 361 | return *Symbols; |
| 362 | } |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 363 | |
| 364 | Expected<NameHashTable &> PDBFile::getStringTable() { |
| 365 | if (!StringTable || !StringTableStream) { |
| 366 | auto InfoS = getPDBInfoStream(); |
| 367 | if (auto EC = InfoS.takeError()) |
| 368 | return std::move(EC); |
| 369 | auto &IS = InfoS.get(); |
| 370 | uint32_t NameStreamIndex = IS.getNamedStreamIndex("/names"); |
| 371 | |
| 372 | if (NameStreamIndex == 0) |
| 373 | return make_error<RawError>(raw_error_code::no_stream); |
| 374 | auto S = llvm::make_unique<MappedBlockStream>(NameStreamIndex, *this); |
| 375 | codeview::StreamReader Reader(*S); |
| 376 | auto N = llvm::make_unique<NameHashTable>(); |
| 377 | if (auto EC = N->load(Reader)) |
| 378 | return std::move(EC); |
| 379 | StringTable = std::move(N); |
| 380 | StringTableStream = std::move(S); |
| 381 | } |
| 382 | return *StringTable; |
| 383 | } |