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 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/ArrayRef.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 16 | #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Native/PDBStringTable.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Native/PublicsStream.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" |
| 22 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 23 | #include "llvm/Support/BinaryStream.h" |
| 24 | #include "llvm/Support/BinaryStreamArray.h" |
| 25 | #include "llvm/Support/BinaryStreamReader.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Error.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <cassert> |
| 31 | #include <cstdint> |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 34 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 35 | using namespace llvm::msf; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 36 | using namespace llvm::pdb; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 37 | |
| 38 | namespace { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 39 | typedef FixedStreamArray<support::ulittle32_t> ulittle_array; |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 40 | } // end anonymous namespace |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 41 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 42 | PDBFile::PDBFile(StringRef Path, std::unique_ptr<BinaryStream> PdbFileBuffer, |
Zachary Turner | e109dc6 | 2016-07-22 19:56:26 +0000 | [diff] [blame] | 43 | BumpPtrAllocator &Allocator) |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 44 | : FilePath(Path), Allocator(Allocator), Buffer(std::move(PdbFileBuffer)) {} |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 45 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 46 | PDBFile::~PDBFile() = default; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 47 | |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 48 | StringRef PDBFile::getFilePath() const { return FilePath; } |
| 49 | |
| 50 | StringRef PDBFile::getFileDirectory() const { |
| 51 | return sys::path::parent_path(FilePath); |
| 52 | } |
| 53 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 54 | uint32_t PDBFile::getBlockSize() const { return ContainerLayout.SB->BlockSize; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 55 | |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 56 | uint32_t PDBFile::getFreeBlockMapBlock() const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 57 | return ContainerLayout.SB->FreeBlockMapBlock; |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 58 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 59 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 60 | uint32_t PDBFile::getBlockCount() const { |
| 61 | return ContainerLayout.SB->NumBlocks; |
| 62 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 63 | |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 64 | uint32_t PDBFile::getNumDirectoryBytes() const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 65 | return ContainerLayout.SB->NumDirectoryBytes; |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 66 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 67 | |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 68 | uint32_t PDBFile::getBlockMapIndex() const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 69 | return ContainerLayout.SB->BlockMapAddr; |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 70 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 71 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 72 | uint32_t PDBFile::getUnknown1() const { return ContainerLayout.SB->Unknown1; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 73 | |
| 74 | uint32_t PDBFile::getNumDirectoryBlocks() const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 75 | return msf::bytesToBlocks(ContainerLayout.SB->NumDirectoryBytes, |
| 76 | ContainerLayout.SB->BlockSize); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | uint64_t PDBFile::getBlockMapOffset() const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 80 | return (uint64_t)ContainerLayout.SB->BlockMapAddr * |
| 81 | ContainerLayout.SB->BlockSize; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 84 | uint32_t PDBFile::getNumStreams() const { |
| 85 | return ContainerLayout.StreamSizes.size(); |
| 86 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 87 | |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 88 | uint32_t PDBFile::getMaxStreamSize() const { |
| 89 | return *std::max_element(ContainerLayout.StreamSizes.begin(), |
| 90 | ContainerLayout.StreamSizes.end()); |
| 91 | } |
| 92 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 93 | uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 94 | return ContainerLayout.StreamSizes[StreamIndex]; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 97 | ArrayRef<support::ulittle32_t> |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 98 | PDBFile::getStreamBlockList(uint32_t StreamIndex) const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 99 | return ContainerLayout.StreamMap[StreamIndex]; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 100 | } |
| 101 | |
David Majnemer | 1b79e9a | 2016-07-10 05:32:05 +0000 | [diff] [blame] | 102 | uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); } |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 103 | |
David Majnemer | 6211b1f | 2016-07-10 03:34:47 +0000 | [diff] [blame] | 104 | Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex, |
| 105 | uint32_t NumBytes) const { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 106 | uint64_t StreamBlockOffset = msf::blockToOffset(BlockIndex, getBlockSize()); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 107 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 108 | ArrayRef<uint8_t> Result; |
| 109 | if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result)) |
David Majnemer | 6211b1f | 2016-07-10 03:34:47 +0000 | [diff] [blame] | 110 | return std::move(EC); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 111 | return Result; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 114 | Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset, |
| 115 | ArrayRef<uint8_t> Data) const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 116 | return make_error<RawError>(raw_error_code::not_writable, |
| 117 | "PDBFile is immutable"); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 120 | Error PDBFile::parseFileHeaders() { |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 121 | BinaryStreamReader Reader(*Buffer); |
Zachary Turner | c59261c | 2016-05-25 03:53:16 +0000 | [diff] [blame] | 122 | |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 123 | // Initialize SB. |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 124 | const msf::SuperBlock *SB = nullptr; |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 125 | if (auto EC = Reader.readObject(SB)) { |
| 126 | consumeError(std::move(EC)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 127 | return make_error<RawError>(raw_error_code::corrupt_file, |
Alexandre Ganea | 6a7efef | 2018-08-31 17:41:58 +0000 | [diff] [blame] | 128 | "MSF superblock is missing"); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 129 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 130 | |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 131 | if (auto EC = msf::validateSuperBlock(*SB)) |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 132 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 133 | |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 134 | if (Buffer->getLength() % SB->BlockSize != 0) |
| 135 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 136 | "File size is not a multiple of block size"); |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 137 | ContainerLayout.SB = SB; |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 138 | |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 139 | // Initialize Free Page Map. |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 140 | ContainerLayout.FreePageMap.resize(SB->NumBlocks); |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 141 | // The Fpm exists either at block 1 or block 2 of the MSF. However, this |
| 142 | // allows for a maximum of getBlockSize() * 8 blocks bits in the Fpm, and |
| 143 | // thusly an equal number of total blocks in the file. For a block size |
| 144 | // of 4KiB (very common), this would yield 32KiB total blocks in file, for a |
| 145 | // maximum file size of 32KiB * 4KiB = 128MiB. Obviously this won't do, so |
| 146 | // the Fpm is split across the file at `getBlockSize()` intervals. As a |
| 147 | // result, every block whose index is of the form |{1,2} + getBlockSize() * k| |
| 148 | // for any non-negative integer k is an Fpm block. In theory, we only really |
| 149 | // need to reserve blocks of the form |{1,2} + getBlockSize() * 8 * k|, but |
| 150 | // current versions of the MSF format already expect the Fpm to be arranged |
| 151 | // at getBlockSize() intervals, so we have to be compatible. |
| 152 | // See the function fpmPn() for more information: |
| 153 | // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/msf/msf.cpp#L489 |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 154 | auto FpmStream = |
| 155 | MappedBlockStream::createFpmStream(ContainerLayout, *Buffer, Allocator); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 156 | BinaryStreamReader FpmReader(*FpmStream); |
Zachary Turner | 8cf51c3 | 2016-08-03 16:53:21 +0000 | [diff] [blame] | 157 | ArrayRef<uint8_t> FpmBytes; |
Zachary Turner | 9fb9d71 | 2017-08-02 22:31:39 +0000 | [diff] [blame] | 158 | if (auto EC = FpmReader.readBytes(FpmBytes, FpmReader.bytesRemaining())) |
Zachary Turner | 8cf51c3 | 2016-08-03 16:53:21 +0000 | [diff] [blame] | 159 | return EC; |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 160 | uint32_t BlocksRemaining = getBlockCount(); |
Zachary Turner | 8cf51c3 | 2016-08-03 16:53:21 +0000 | [diff] [blame] | 161 | uint32_t BI = 0; |
| 162 | for (auto Byte : FpmBytes) { |
| 163 | uint32_t BlocksThisByte = std::min(BlocksRemaining, 8U); |
| 164 | for (uint32_t I = 0; I < BlocksThisByte; ++I) { |
| 165 | if (Byte & (1 << I)) |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 166 | ContainerLayout.FreePageMap[BI] = true; |
Zachary Turner | 8cf51c3 | 2016-08-03 16:53:21 +0000 | [diff] [blame] | 167 | --BlocksRemaining; |
| 168 | ++BI; |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 169 | } |
Zachary Turner | d3c7b8e | 2016-08-01 21:19:45 +0000 | [diff] [blame] | 170 | } |
Rui Ueyama | 7a5cdc6 | 2016-07-29 21:38:00 +0000 | [diff] [blame] | 171 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 172 | Reader.setOffset(getBlockMapOffset()); |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 173 | if (auto EC = Reader.readArray(ContainerLayout.DirectoryBlocks, |
| 174 | getNumDirectoryBlocks())) |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 175 | return EC; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 176 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 177 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 180 | Error PDBFile::parseStreamData() { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 181 | assert(ContainerLayout.SB); |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 182 | if (DirectoryStream) |
| 183 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 184 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 185 | uint32_t NumStreams = 0; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 186 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 187 | // Normally you can't use a MappedBlockStream without having fully parsed the |
| 188 | // PDB file, because it accesses the directory and various other things, which |
| 189 | // is exactly what we are attempting to parse. By specifying a custom |
| 190 | // subclass of IPDBStreamData which only accesses the fields that have already |
| 191 | // been parsed, we can avoid this and reuse MappedBlockStream. |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 192 | auto DS = MappedBlockStream::createDirectoryStream(ContainerLayout, *Buffer, |
| 193 | Allocator); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 194 | BinaryStreamReader Reader(*DS); |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 195 | if (auto EC = Reader.readInteger(NumStreams)) |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 196 | return EC; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 197 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 198 | if (auto EC = Reader.readArray(ContainerLayout.StreamSizes, NumStreams)) |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 199 | return EC; |
| 200 | for (uint32_t I = 0; I < NumStreams; ++I) { |
Reid Kleckner | 5aba52f | 2016-06-22 22:42:24 +0000 | [diff] [blame] | 201 | uint32_t StreamSize = getStreamByteSize(I); |
| 202 | // FIXME: What does StreamSize ~0U mean? |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 203 | uint64_t NumExpectedStreamBlocks = |
Zachary Turner | e4a4f33 | 2016-07-22 19:56:33 +0000 | [diff] [blame] | 204 | StreamSize == UINT32_MAX |
| 205 | ? 0 |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 206 | : msf::bytesToBlocks(StreamSize, ContainerLayout.SB->BlockSize); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 207 | |
| 208 | // For convenience, we store the block array contiguously. This is because |
| 209 | // if someone calls setStreamMap(), it is more convenient to be able to call |
| 210 | // it with an ArrayRef instead of setting up a StreamRef. Since the |
| 211 | // DirectoryStream is cached in the class and thus lives for the life of the |
| 212 | // class, we can be guaranteed that readArray() will return a stable |
| 213 | // reference, even if it has to allocate from its internal pool. |
| 214 | ArrayRef<support::ulittle32_t> Blocks; |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 215 | if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks)) |
| 216 | return EC; |
David Majnemer | 1b79e9a | 2016-07-10 05:32:05 +0000 | [diff] [blame] | 217 | for (uint32_t Block : Blocks) { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 218 | uint64_t BlockEndOffset = |
| 219 | (uint64_t)(Block + 1) * ContainerLayout.SB->BlockSize; |
David Majnemer | 1b79e9a | 2016-07-10 05:32:05 +0000 | [diff] [blame] | 220 | if (BlockEndOffset > getFileSize()) |
| 221 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 222 | "Stream block map is corrupt."); |
| 223 | } |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 224 | ContainerLayout.StreamMap.push_back(Blocks); |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 227 | // We should have read exactly SB->NumDirectoryBytes bytes. |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 228 | assert(Reader.bytesRemaining() == 0); |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 229 | DirectoryStream = std::move(DS); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 230 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 233 | ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const { |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 234 | return ContainerLayout.DirectoryBlocks; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 235 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 236 | |
Zachary Turner | d1de2f4 | 2017-08-21 14:53:25 +0000 | [diff] [blame] | 237 | std::unique_ptr<MappedBlockStream> PDBFile::createIndexedStream(uint16_t SN) { |
| 238 | if (SN == kInvalidStreamIndex) |
| 239 | return nullptr; |
| 240 | return MappedBlockStream::createIndexedStream(ContainerLayout, *Buffer, SN, |
| 241 | Allocator); |
| 242 | } |
| 243 | |
Zachary Turner | 0b36c3e | 2017-06-23 18:52:13 +0000 | [diff] [blame] | 244 | MSFStreamLayout PDBFile::getStreamLayout(uint32_t StreamIdx) const { |
| 245 | MSFStreamLayout Result; |
| 246 | auto Blocks = getStreamBlockList(StreamIdx); |
| 247 | Result.Blocks.assign(Blocks.begin(), Blocks.end()); |
| 248 | Result.Length = getStreamByteSize(StreamIdx); |
| 249 | return Result; |
| 250 | } |
| 251 | |
Zachary Turner | c3d8eec | 2017-08-02 22:25:52 +0000 | [diff] [blame] | 252 | msf::MSFStreamLayout PDBFile::getFpmStreamLayout() const { |
| 253 | return msf::getFpmStreamLayout(ContainerLayout); |
| 254 | } |
| 255 | |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 256 | Expected<GlobalsStream &> PDBFile::getPDBGlobalsStream() { |
| 257 | if (!Globals) { |
| 258 | auto DbiS = getPDBDbiStream(); |
| 259 | if (!DbiS) |
| 260 | return DbiS.takeError(); |
| 261 | |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 262 | auto GlobalS = safelyCreateIndexedStream( |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 263 | ContainerLayout, *Buffer, DbiS->getGlobalSymbolStreamIndex()); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 264 | if (!GlobalS) |
| 265 | return GlobalS.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 266 | auto TempGlobals = llvm::make_unique<GlobalsStream>(std::move(*GlobalS)); |
Bob Haarman | 653baa2 | 2016-10-21 19:43:19 +0000 | [diff] [blame] | 267 | if (auto EC = TempGlobals->reload()) |
| 268 | return std::move(EC); |
| 269 | Globals = std::move(TempGlobals); |
| 270 | } |
| 271 | return *Globals; |
| 272 | } |
| 273 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 274 | Expected<InfoStream &> PDBFile::getPDBInfoStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 275 | if (!Info) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 276 | auto InfoS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamPDB); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 277 | if (!InfoS) |
| 278 | return InfoS.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 279 | auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS)); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 280 | if (auto EC = TempInfo->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 281 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 282 | Info = std::move(TempInfo); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 283 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 284 | return *Info; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 287 | Expected<DbiStream &> PDBFile::getPDBDbiStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 288 | if (!Dbi) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 289 | auto DbiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamDBI); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 290 | if (!DbiS) |
| 291 | return DbiS.takeError(); |
Zachary Turner | 15b2bdf | 2018-04-04 17:29:09 +0000 | [diff] [blame] | 292 | auto TempDbi = llvm::make_unique<DbiStream>(std::move(*DbiS)); |
| 293 | if (auto EC = TempDbi->reload(this)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 294 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 295 | Dbi = std::move(TempDbi); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 296 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 297 | return *Dbi; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 298 | } |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 299 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 300 | Expected<TpiStream &> PDBFile::getPDBTpiStream() { |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 301 | if (!Tpi) { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 302 | auto TpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamTPI); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 303 | if (!TpiS) |
| 304 | return TpiS.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 305 | auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS)); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 306 | if (auto EC = TempTpi->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 307 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 308 | Tpi = std::move(TempTpi); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 309 | } |
| 310 | return *Tpi; |
| 311 | } |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 312 | |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 313 | Expected<TpiStream &> PDBFile::getPDBIpiStream() { |
| 314 | if (!Ipi) { |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 315 | if (!hasPDBIpiStream()) |
| 316 | return make_error<RawError>(raw_error_code::no_stream); |
| 317 | |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 318 | auto IpiS = safelyCreateIndexedStream(ContainerLayout, *Buffer, StreamIPI); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 319 | if (!IpiS) |
| 320 | return IpiS.takeError(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 321 | auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS)); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 322 | if (auto EC = TempIpi->reload()) |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 323 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 324 | Ipi = std::move(TempIpi); |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 325 | } |
| 326 | return *Ipi; |
| 327 | } |
| 328 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 329 | Expected<PublicsStream &> PDBFile::getPDBPublicsStream() { |
| 330 | if (!Publics) { |
| 331 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 332 | if (!DbiS) |
| 333 | return DbiS.takeError(); |
| 334 | |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 335 | auto PublicS = safelyCreateIndexedStream( |
| 336 | ContainerLayout, *Buffer, DbiS->getPublicSymbolStreamIndex()); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 337 | if (!PublicS) |
| 338 | return PublicS.takeError(); |
Reid Kleckner | 14d90fd | 2017-07-26 00:40:36 +0000 | [diff] [blame] | 339 | auto TempPublics = llvm::make_unique<PublicsStream>(std::move(*PublicS)); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 340 | if (auto EC = TempPublics->reload()) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 341 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 342 | Publics = std::move(TempPublics); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 343 | } |
| 344 | return *Publics; |
| 345 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 346 | |
| 347 | Expected<SymbolStream &> PDBFile::getPDBSymbolStream() { |
| 348 | if (!Symbols) { |
| 349 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 350 | if (!DbiS) |
| 351 | return DbiS.takeError(); |
| 352 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 353 | uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex(); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 354 | auto SymbolS = |
| 355 | safelyCreateIndexedStream(ContainerLayout, *Buffer, SymbolStreamNum); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 356 | if (!SymbolS) |
| 357 | return SymbolS.takeError(); |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 358 | |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 359 | auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS)); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 360 | if (auto EC = TempSymbols->reload()) |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 361 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 362 | Symbols = std::move(TempSymbols); |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 363 | } |
| 364 | return *Symbols; |
| 365 | } |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 366 | |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 367 | Expected<PDBStringTable &> PDBFile::getStringTable() { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 368 | if (!Strings) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 369 | auto IS = getPDBInfoStream(); |
| 370 | if (!IS) |
| 371 | return IS.takeError(); |
| 372 | |
Zachary Turner | d11328a | 2018-04-02 18:35:21 +0000 | [diff] [blame] | 373 | Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names"); |
| 374 | if (!ExpectedNSI) |
| 375 | return ExpectedNSI.takeError(); |
| 376 | uint32_t NameStreamIndex = *ExpectedNSI; |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 377 | |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 378 | auto NS = |
| 379 | safelyCreateIndexedStream(ContainerLayout, *Buffer, NameStreamIndex); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 380 | if (!NS) |
| 381 | return NS.takeError(); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 382 | |
Daniel Jasper | dff096f | 2017-05-03 07:29:25 +0000 | [diff] [blame] | 383 | auto N = llvm::make_unique<PDBStringTable>(); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 384 | BinaryStreamReader Reader(**NS); |
| 385 | if (auto EC = N->reload(Reader)) |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 386 | return std::move(EC); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 387 | assert(Reader.bytesRemaining() == 0); |
| 388 | StringTableStream = std::move(*NS); |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 389 | Strings = std::move(N); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 390 | } |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 391 | return *Strings; |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 392 | } |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 393 | |
Zachary Turner | 4e95064 | 2017-06-15 23:56:19 +0000 | [diff] [blame] | 394 | uint32_t PDBFile::getPointerSize() { |
| 395 | auto DbiS = getPDBDbiStream(); |
| 396 | if (!DbiS) |
| 397 | return 0; |
| 398 | PDB_Machine Machine = DbiS->getMachineType(); |
| 399 | if (Machine == PDB_Machine::Amd64) |
| 400 | return 8; |
| 401 | return 4; |
| 402 | } |
| 403 | |
Alexandre Ganea | 741cc35 | 2018-08-06 19:35:00 +0000 | [diff] [blame] | 404 | bool PDBFile::hasPDBDbiStream() const { |
| 405 | return StreamDBI < getNumStreams() && getStreamByteSize(StreamDBI) > 0; |
| 406 | } |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 407 | |
| 408 | bool PDBFile::hasPDBGlobalsStream() { |
| 409 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 410 | if (!DbiS) { |
| 411 | consumeError(DbiS.takeError()); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 412 | return false; |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 415 | return DbiS->getGlobalSymbolStreamIndex() < getNumStreams(); |
| 416 | } |
| 417 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 418 | bool PDBFile::hasPDBInfoStream() const { return StreamPDB < getNumStreams(); } |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 419 | |
Zachary Turner | abb17cc | 2017-09-01 20:06:56 +0000 | [diff] [blame] | 420 | bool PDBFile::hasPDBIpiStream() const { |
| 421 | if (!hasPDBInfoStream()) |
| 422 | return false; |
| 423 | |
| 424 | if (StreamIPI >= getNumStreams()) |
| 425 | return false; |
| 426 | |
| 427 | auto &InfoStream = cantFail(const_cast<PDBFile *>(this)->getPDBInfoStream()); |
| 428 | return InfoStream.containsIdStream(); |
| 429 | } |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 430 | |
| 431 | bool PDBFile::hasPDBPublicsStream() { |
| 432 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 433 | if (!DbiS) { |
| 434 | consumeError(DbiS.takeError()); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 435 | return false; |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 436 | } |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 437 | return DbiS->getPublicSymbolStreamIndex() < getNumStreams(); |
| 438 | } |
| 439 | |
| 440 | bool PDBFile::hasPDBSymbolStream() { |
| 441 | auto DbiS = getPDBDbiStream(); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 442 | if (!DbiS) |
| 443 | return false; |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 444 | return DbiS->getSymRecordStreamIndex() < getNumStreams(); |
| 445 | } |
| 446 | |
| 447 | bool PDBFile::hasPDBTpiStream() const { return StreamTPI < getNumStreams(); } |
| 448 | |
Zachary Turner | e204a6c | 2017-05-02 18:00:13 +0000 | [diff] [blame] | 449 | bool PDBFile::hasPDBStringTable() { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 450 | auto IS = getPDBInfoStream(); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 451 | if (!IS) |
| 452 | return false; |
Zachary Turner | d11328a | 2018-04-02 18:35:21 +0000 | [diff] [blame] | 453 | Expected<uint32_t> ExpectedNSI = IS->getNamedStreamIndex("/names"); |
| 454 | if (!ExpectedNSI) { |
| 455 | consumeError(ExpectedNSI.takeError()); |
| 456 | return false; |
| 457 | } |
| 458 | assert(*ExpectedNSI < getNumStreams()); |
| 459 | return true; |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Zachary Turner | d0b44fa | 2017-02-28 17:49:34 +0000 | [diff] [blame] | 462 | /// Wrapper around MappedBlockStream::createIndexedStream() that checks if a |
| 463 | /// stream with that index actually exists. If it does not, the return value |
| 464 | /// will have an MSFError with code msf_error_code::no_stream. Else, the return |
| 465 | /// value will contain the stream returned by createIndexedStream(). |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 466 | Expected<std::unique_ptr<MappedBlockStream>> |
| 467 | PDBFile::safelyCreateIndexedStream(const MSFLayout &Layout, |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 468 | BinaryStreamRef MsfData, |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 469 | uint32_t StreamIndex) const { |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 470 | if (StreamIndex >= getNumStreams()) |
| 471 | return make_error<RawError>(raw_error_code::no_stream); |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 472 | return MappedBlockStream::createIndexedStream(Layout, MsfData, StreamIndex, |
| 473 | Allocator); |
Bob Haarman | a5b4358 | 2016-12-05 22:44:00 +0000 | [diff] [blame] | 474 | } |