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