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