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" |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 14 | #include "llvm/DebugInfo/CodeView/StreamInterface.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/StreamReader.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Raw/DirectoryStreamData.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Raw/PublicsStream.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/PDB/Raw/SymbolStream.h" |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Endian.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | |
| 28 | using namespace llvm; |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 29 | using namespace llvm::codeview; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 30 | using namespace llvm::pdb; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 33 | typedef FixedStreamArray<support::ulittle32_t> ulittle_array; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 36 | PDBFile::PDBFile(std::unique_ptr<StreamInterface> PdbFileBuffer) |
| 37 | : Buffer(std::move(PdbFileBuffer)), SB(nullptr) {} |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 38 | |
| 39 | PDBFile::~PDBFile() {} |
| 40 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 41 | uint32_t PDBFile::getBlockSize() const { return SB->BlockSize; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 42 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 43 | uint32_t PDBFile::getUnknown0() const { return SB->Unknown0; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 44 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 45 | uint32_t PDBFile::getBlockCount() const { return SB->NumBlocks; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 46 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 47 | uint32_t PDBFile::getNumDirectoryBytes() const { return SB->NumDirectoryBytes; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 48 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 49 | uint32_t PDBFile::getBlockMapIndex() const { return SB->BlockMapAddr; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 50 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 51 | uint32_t PDBFile::getUnknown1() const { return SB->Unknown1; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 52 | |
| 53 | uint32_t PDBFile::getNumDirectoryBlocks() const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 54 | return bytesToBlocks(SB->NumDirectoryBytes, SB->BlockSize); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | uint64_t PDBFile::getBlockMapOffset() const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 58 | return (uint64_t)SB->BlockMapAddr * SB->BlockSize; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 61 | uint32_t PDBFile::getNumStreams() const { return StreamSizes.size(); } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 62 | |
| 63 | uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 64 | return StreamSizes[StreamIndex]; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 67 | ArrayRef<support::ulittle32_t> |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 68 | PDBFile::getStreamBlockList(uint32_t StreamIndex) const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 69 | return StreamMap[StreamIndex]; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Zachary Turner | e6fee88 | 2016-06-07 20:38:37 +0000 | [diff] [blame] | 72 | ArrayRef<uint8_t> PDBFile::getBlockData(uint32_t BlockIndex, |
| 73 | uint32_t NumBytes) const { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 74 | uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize()); |
| 75 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 76 | ArrayRef<uint8_t> Result; |
| 77 | if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result)) |
| 78 | consumeError(std::move(EC)); |
| 79 | return Result; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 82 | Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset, |
| 83 | ArrayRef<uint8_t> Data) const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 84 | if (Offset >= getBlockSize()) |
| 85 | return make_error<RawError>( |
| 86 | raw_error_code::invalid_block_address, |
| 87 | "setBlockData attempted to write out of block bounds."); |
| 88 | if (Data.size() >= getBlockSize() - Offset) |
| 89 | return make_error<RawError>( |
| 90 | raw_error_code::invalid_block_address, |
| 91 | "setBlockData attempted to write out of block bounds."); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 92 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 93 | uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize()); |
| 94 | StreamBlockOffset += Offset; |
| 95 | return Buffer->writeBytes(StreamBlockOffset, Data); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 98 | Error PDBFile::parseFileHeaders() { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 99 | StreamReader Reader(*Buffer); |
Zachary Turner | c59261c | 2016-05-25 03:53:16 +0000 | [diff] [blame] | 100 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 101 | if (auto EC = Reader.readObject(SB)) { |
| 102 | consumeError(std::move(EC)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 103 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 104 | "Does not contain superblock"); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 105 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 106 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 107 | // Check the magic bytes. |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 108 | if (memcmp(SB->MagicBytes, MsfMagic, sizeof(MsfMagic)) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 109 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 110 | "MSF magic header doesn't match"); |
| 111 | |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 112 | // We don't support blocksizes which aren't a multiple of four bytes. |
| 113 | if (SB->BlockSize % sizeof(support::ulittle32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 114 | return make_error<RawError>(raw_error_code::corrupt_file, |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 115 | "Block size is not multiple of 4."); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 116 | |
Zachary Turner | 9213ba5 | 2016-04-29 18:09:19 +0000 | [diff] [blame] | 117 | switch (SB->BlockSize) { |
| 118 | case 512: case 1024: case 2048: case 4096: |
| 119 | break; |
| 120 | default: |
| 121 | // An invalid block size suggests a corrupt PDB file. |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 122 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 123 | "Unsupported block size."); |
Zachary Turner | 9213ba5 | 2016-04-29 18:09:19 +0000 | [diff] [blame] | 124 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 125 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 126 | if (Buffer->getLength() % SB->BlockSize != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 127 | return make_error<RawError>(raw_error_code::corrupt_file, |
David Majnemer | 878cadb | 2016-05-27 15:57:38 +0000 | [diff] [blame] | 128 | "File size is not a multiple of block size"); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 129 | |
| 130 | // We don't support directories whose sizes aren't a multiple of four bytes. |
| 131 | if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 132 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 133 | "Directory size is not multiple of 4."); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 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 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 139 | // The directory, as we understand it, is a block which consists of a list of |
| 140 | // block numbers. It is unclear what would happen if the number of blocks |
| 141 | // couldn't fit on a single block. |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 142 | if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 143 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 144 | "Too many directory blocks."); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 145 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 146 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 149 | Error PDBFile::parseStreamData() { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 150 | assert(SB); |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 151 | if (DirectoryStream) |
| 152 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 153 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 154 | uint32_t NumStreams = 0; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 155 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 156 | // Normally you can't use a MappedBlockStream without having fully parsed the |
| 157 | // PDB file, because it accesses the directory and various other things, which |
| 158 | // is exactly what we are attempting to parse. By specifying a custom |
| 159 | // subclass of IPDBStreamData which only accesses the fields that have already |
| 160 | // been parsed, we can avoid this and reuse MappedBlockStream. |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 161 | auto DS = MappedBlockStream::createDirectoryStream(*this); |
| 162 | if (!DS) |
| 163 | return DS.takeError(); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 164 | StreamReader Reader(**DS); |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 165 | if (auto EC = Reader.readInteger(NumStreams)) |
| 166 | return EC; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 167 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 168 | if (auto EC = Reader.readArray(StreamSizes, NumStreams)) |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 169 | return EC; |
| 170 | for (uint32_t I = 0; I < NumStreams; ++I) { |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 171 | uint64_t NumExpectedStreamBlocks = |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 172 | bytesToBlocks(getStreamByteSize(I), SB->BlockSize); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 173 | |
| 174 | // For convenience, we store the block array contiguously. This is because |
| 175 | // if someone calls setStreamMap(), it is more convenient to be able to call |
| 176 | // it with an ArrayRef instead of setting up a StreamRef. Since the |
| 177 | // DirectoryStream is cached in the class and thus lives for the life of the |
| 178 | // class, we can be guaranteed that readArray() will return a stable |
| 179 | // reference, even if it has to allocate from its internal pool. |
| 180 | ArrayRef<support::ulittle32_t> Blocks; |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 181 | if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks)) |
| 182 | return EC; |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 183 | StreamMap.push_back(Blocks); |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 186 | // We should have read exactly SB->NumDirectoryBytes bytes. |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 187 | assert(Reader.bytesRemaining() == 0); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 188 | DirectoryStream = std::move(*DS); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 189 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 192 | llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 193 | StreamReader Reader(*Buffer); |
| 194 | Reader.setOffset(getBlockMapOffset()); |
| 195 | llvm::ArrayRef<support::ulittle32_t> Result; |
| 196 | if (auto EC = Reader.readArray(Result, getNumDirectoryBlocks())) |
| 197 | consumeError(std::move(EC)); |
| 198 | return Result; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 199 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 200 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 201 | Expected<InfoStream &> PDBFile::getPDBInfoStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 202 | if (!Info) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 203 | auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this); |
| 204 | if (!InfoS) |
| 205 | return InfoS.takeError(); |
| 206 | auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS)); |
| 207 | if (auto EC = TempInfo->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 208 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 209 | Info = std::move(TempInfo); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 210 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 211 | return *Info; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 214 | Expected<DbiStream &> PDBFile::getPDBDbiStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 215 | if (!Dbi) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 216 | auto DbiS = MappedBlockStream::createIndexedStream(StreamDBI, *this); |
| 217 | if (!DbiS) |
| 218 | return DbiS.takeError(); |
| 219 | auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS)); |
| 220 | if (auto EC = TempDbi->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 221 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 222 | Dbi = std::move(TempDbi); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 223 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 224 | return *Dbi; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 225 | } |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 226 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 227 | Expected<TpiStream &> PDBFile::getPDBTpiStream() { |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 228 | if (!Tpi) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 229 | auto TpiS = MappedBlockStream::createIndexedStream(StreamTPI, *this); |
| 230 | if (!TpiS) |
| 231 | return TpiS.takeError(); |
| 232 | auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS)); |
| 233 | if (auto EC = TempTpi->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 234 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 235 | Tpi = std::move(TempTpi); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 236 | } |
| 237 | return *Tpi; |
| 238 | } |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 239 | |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 240 | Expected<TpiStream &> PDBFile::getPDBIpiStream() { |
| 241 | if (!Ipi) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 242 | auto IpiS = MappedBlockStream::createIndexedStream(StreamIPI, *this); |
| 243 | if (!IpiS) |
| 244 | return IpiS.takeError(); |
| 245 | auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS)); |
| 246 | if (auto EC = TempIpi->reload()) |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 247 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 248 | Ipi = std::move(TempIpi); |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 249 | } |
| 250 | return *Ipi; |
| 251 | } |
| 252 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 253 | Expected<PublicsStream &> PDBFile::getPDBPublicsStream() { |
| 254 | if (!Publics) { |
| 255 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 256 | if (!DbiS) |
| 257 | return DbiS.takeError(); |
| 258 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 259 | uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex(); |
| 260 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 261 | auto PublicS = |
| 262 | MappedBlockStream::createIndexedStream(PublicsStreamNum, *this); |
| 263 | if (!PublicS) |
| 264 | return PublicS.takeError(); |
| 265 | auto TempPublics = |
| 266 | llvm::make_unique<PublicsStream>(*this, std::move(*PublicS)); |
| 267 | if (auto EC = TempPublics->reload()) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 268 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 269 | Publics = std::move(TempPublics); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 270 | } |
| 271 | return *Publics; |
| 272 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 273 | |
| 274 | Expected<SymbolStream &> PDBFile::getPDBSymbolStream() { |
| 275 | if (!Symbols) { |
| 276 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 277 | if (!DbiS) |
| 278 | return DbiS.takeError(); |
| 279 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 280 | uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex(); |
| 281 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 282 | auto SymbolS = |
| 283 | MappedBlockStream::createIndexedStream(SymbolStreamNum, *this); |
| 284 | if (!SymbolS) |
| 285 | return SymbolS.takeError(); |
| 286 | auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS)); |
| 287 | if (auto EC = TempSymbols->reload()) |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 288 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 289 | Symbols = std::move(TempSymbols); |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 290 | } |
| 291 | return *Symbols; |
| 292 | } |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 293 | |
| 294 | Expected<NameHashTable &> PDBFile::getStringTable() { |
| 295 | if (!StringTable || !StringTableStream) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 296 | auto IS = getPDBInfoStream(); |
| 297 | if (!IS) |
| 298 | return IS.takeError(); |
| 299 | |
| 300 | uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names"); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 301 | |
| 302 | if (NameStreamIndex == 0) |
| 303 | return make_error<RawError>(raw_error_code::no_stream); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 304 | if (NameStreamIndex >= getNumStreams()) |
| 305 | return make_error<RawError>(raw_error_code::no_stream); |
| 306 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 307 | auto NS = MappedBlockStream::createIndexedStream(NameStreamIndex, *this); |
| 308 | if (!NS) |
| 309 | return NS.takeError(); |
| 310 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame^] | 311 | StreamReader Reader(**NS); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 312 | auto N = llvm::make_unique<NameHashTable>(); |
| 313 | if (auto EC = N->load(Reader)) |
| 314 | return std::move(EC); |
| 315 | StringTable = std::move(N); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 316 | StringTableStream = std::move(*NS); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 317 | } |
| 318 | return *StringTable; |
| 319 | } |