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