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