Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 1 | //===- PDBFile.cpp - Low level interface to a PDB file ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 11 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/CodeView/StreamArray.h" |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/CodeView/StreamInterface.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/StreamReader.h" |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/StreamWriter.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Raw/DirectoryStreamData.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Raw/PublicsStream.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/PDB/Raw/SymbolStream.h" |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Endian.h" |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileOutputBuffer.h" |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MemoryBuffer.h" |
| 29 | |
| 30 | using namespace llvm; |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 31 | using namespace llvm::codeview; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 32 | using namespace llvm::pdb; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 35 | typedef FixedStreamArray<support::ulittle32_t> ulittle_array; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 38 | PDBFile::PDBFile(std::unique_ptr<StreamInterface> PdbFileBuffer) |
| 39 | : Buffer(std::move(PdbFileBuffer)), SB(nullptr) {} |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 40 | |
| 41 | PDBFile::~PDBFile() {} |
| 42 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 43 | uint32_t PDBFile::getBlockSize() const { return SB->BlockSize; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 44 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 45 | uint32_t PDBFile::getUnknown0() const { return SB->Unknown0; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 46 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 47 | uint32_t PDBFile::getBlockCount() const { return SB->NumBlocks; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 48 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 49 | uint32_t PDBFile::getNumDirectoryBytes() const { return SB->NumDirectoryBytes; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 50 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 51 | uint32_t PDBFile::getBlockMapIndex() const { return SB->BlockMapAddr; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 52 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 53 | uint32_t PDBFile::getUnknown1() const { return SB->Unknown1; } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 54 | |
| 55 | uint32_t PDBFile::getNumDirectoryBlocks() const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 56 | return bytesToBlocks(SB->NumDirectoryBytes, SB->BlockSize); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | uint64_t PDBFile::getBlockMapOffset() const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 60 | return (uint64_t)SB->BlockMapAddr * SB->BlockSize; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 63 | uint32_t PDBFile::getNumStreams() const { return StreamSizes.size(); } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 64 | |
| 65 | uint32_t PDBFile::getStreamByteSize(uint32_t StreamIndex) const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 66 | return StreamSizes[StreamIndex]; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 69 | ArrayRef<support::ulittle32_t> |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 70 | PDBFile::getStreamBlockList(uint32_t StreamIndex) const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 71 | return StreamMap[StreamIndex]; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 72 | } |
| 73 | |
David Majnemer | 1b79e9a | 2016-07-10 05:32:05 +0000 | [diff] [blame^] | 74 | uint32_t PDBFile::getFileSize() const { return Buffer->getLength(); } |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 75 | |
David Majnemer | 6211b1f | 2016-07-10 03:34:47 +0000 | [diff] [blame] | 76 | Expected<ArrayRef<uint8_t>> PDBFile::getBlockData(uint32_t BlockIndex, |
| 77 | uint32_t NumBytes) const { |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 78 | uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize()); |
| 79 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 80 | ArrayRef<uint8_t> Result; |
| 81 | if (auto EC = Buffer->readBytes(StreamBlockOffset, NumBytes, Result)) |
David Majnemer | 6211b1f | 2016-07-10 03:34:47 +0000 | [diff] [blame] | 82 | return std::move(EC); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 83 | return Result; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 86 | Error PDBFile::setBlockData(uint32_t BlockIndex, uint32_t Offset, |
| 87 | ArrayRef<uint8_t> Data) const { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 88 | if (Offset >= getBlockSize()) |
| 89 | return make_error<RawError>( |
| 90 | raw_error_code::invalid_block_address, |
| 91 | "setBlockData attempted to write out of block bounds."); |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 92 | if (Data.size() > getBlockSize() - Offset) |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 93 | return make_error<RawError>( |
| 94 | raw_error_code::invalid_block_address, |
| 95 | "setBlockData attempted to write out of block bounds."); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 96 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 97 | uint64_t StreamBlockOffset = blockToOffset(BlockIndex, getBlockSize()); |
| 98 | StreamBlockOffset += Offset; |
| 99 | return Buffer->writeBytes(StreamBlockOffset, Data); |
Zachary Turner | 5acb4ac | 2016-06-10 05:09:12 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 102 | Error PDBFile::parseFileHeaders() { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 103 | StreamReader Reader(*Buffer); |
Zachary Turner | c59261c | 2016-05-25 03:53:16 +0000 | [diff] [blame] | 104 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 105 | if (auto EC = Reader.readObject(SB)) { |
| 106 | consumeError(std::move(EC)); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 107 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 108 | "Does not contain superblock"); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 109 | } |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 110 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 111 | if (auto EC = setSuperBlock(SB)) |
| 112 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 113 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 114 | Reader.setOffset(getBlockMapOffset()); |
| 115 | if (auto EC = Reader.readArray(DirectoryBlocks, getNumDirectoryBlocks())) |
| 116 | return EC; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 117 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 118 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 121 | Error PDBFile::parseStreamData() { |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 122 | assert(SB); |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 123 | if (DirectoryStream) |
| 124 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 125 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 126 | uint32_t NumStreams = 0; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 127 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 128 | // Normally you can't use a MappedBlockStream without having fully parsed the |
| 129 | // PDB file, because it accesses the directory and various other things, which |
| 130 | // is exactly what we are attempting to parse. By specifying a custom |
| 131 | // subclass of IPDBStreamData which only accesses the fields that have already |
| 132 | // been parsed, we can avoid this and reuse MappedBlockStream. |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 133 | auto DS = MappedBlockStream::createDirectoryStream(*this); |
| 134 | if (!DS) |
| 135 | return DS.takeError(); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 136 | StreamReader Reader(**DS); |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 137 | if (auto EC = Reader.readInteger(NumStreams)) |
| 138 | return EC; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 139 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 140 | if (auto EC = Reader.readArray(StreamSizes, NumStreams)) |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 141 | return EC; |
| 142 | for (uint32_t I = 0; I < NumStreams; ++I) { |
Reid Kleckner | 5aba52f | 2016-06-22 22:42:24 +0000 | [diff] [blame] | 143 | uint32_t StreamSize = getStreamByteSize(I); |
| 144 | // FIXME: What does StreamSize ~0U mean? |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 145 | uint64_t NumExpectedStreamBlocks = |
Reid Kleckner | 5aba52f | 2016-06-22 22:42:24 +0000 | [diff] [blame] | 146 | StreamSize == UINT32_MAX ? 0 : bytesToBlocks(StreamSize, SB->BlockSize); |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 147 | |
| 148 | // For convenience, we store the block array contiguously. This is because |
| 149 | // if someone calls setStreamMap(), it is more convenient to be able to call |
| 150 | // it with an ArrayRef instead of setting up a StreamRef. Since the |
| 151 | // DirectoryStream is cached in the class and thus lives for the life of the |
| 152 | // class, we can be guaranteed that readArray() will return a stable |
| 153 | // reference, even if it has to allocate from its internal pool. |
| 154 | ArrayRef<support::ulittle32_t> Blocks; |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 155 | if (auto EC = Reader.readArray(Blocks, NumExpectedStreamBlocks)) |
| 156 | return EC; |
David Majnemer | 1b79e9a | 2016-07-10 05:32:05 +0000 | [diff] [blame^] | 157 | for (uint32_t Block : Blocks) { |
| 158 | uint64_t BlockEndOffset = (uint64_t)(Block + 1) * SB->BlockSize; |
| 159 | if (BlockEndOffset > getFileSize()) |
| 160 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 161 | "Stream block map is corrupt."); |
| 162 | } |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 163 | StreamMap.push_back(Blocks); |
David Majnemer | 9efba74 | 2016-05-27 16:16:48 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 166 | // We should have read exactly SB->NumDirectoryBytes bytes. |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 167 | assert(Reader.bytesRemaining() == 0); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 168 | DirectoryStream = std::move(*DS); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 169 | return Error::success(); |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 172 | llvm::ArrayRef<support::ulittle32_t> PDBFile::getDirectoryBlockArray() const { |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 173 | return DirectoryBlocks; |
Zachary Turner | 0a43efe | 2016-04-25 17:38:08 +0000 | [diff] [blame] | 174 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 175 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 176 | Expected<InfoStream &> PDBFile::emplacePDBInfoStream() { |
| 177 | if (Info) |
| 178 | Info.reset(); |
| 179 | |
| 180 | auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this); |
| 181 | if (!InfoS) |
| 182 | return InfoS.takeError(); |
| 183 | Info = llvm::make_unique<InfoStream>(std::move(*InfoS)); |
| 184 | return *Info; |
| 185 | } |
| 186 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 187 | Expected<InfoStream &> PDBFile::getPDBInfoStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 188 | if (!Info) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 189 | auto InfoS = MappedBlockStream::createIndexedStream(StreamPDB, *this); |
| 190 | if (!InfoS) |
| 191 | return InfoS.takeError(); |
| 192 | auto TempInfo = llvm::make_unique<InfoStream>(std::move(*InfoS)); |
| 193 | if (auto EC = TempInfo->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 194 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 195 | Info = std::move(TempInfo); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 196 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 197 | return *Info; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 200 | Expected<DbiStream &> PDBFile::getPDBDbiStream() { |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 201 | if (!Dbi) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 202 | auto DbiS = MappedBlockStream::createIndexedStream(StreamDBI, *this); |
| 203 | if (!DbiS) |
| 204 | return DbiS.takeError(); |
| 205 | auto TempDbi = llvm::make_unique<DbiStream>(*this, std::move(*DbiS)); |
| 206 | if (auto EC = TempDbi->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 207 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 208 | Dbi = std::move(TempDbi); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 209 | } |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 210 | return *Dbi; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 211 | } |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 212 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 213 | Expected<TpiStream &> PDBFile::getPDBTpiStream() { |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 214 | if (!Tpi) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 215 | auto TpiS = MappedBlockStream::createIndexedStream(StreamTPI, *this); |
| 216 | if (!TpiS) |
| 217 | return TpiS.takeError(); |
| 218 | auto TempTpi = llvm::make_unique<TpiStream>(*this, std::move(*TpiS)); |
| 219 | if (auto EC = TempTpi->reload()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 220 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 221 | Tpi = std::move(TempTpi); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 222 | } |
| 223 | return *Tpi; |
| 224 | } |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 225 | |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 226 | Expected<TpiStream &> PDBFile::getPDBIpiStream() { |
| 227 | if (!Ipi) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 228 | auto IpiS = MappedBlockStream::createIndexedStream(StreamIPI, *this); |
| 229 | if (!IpiS) |
| 230 | return IpiS.takeError(); |
| 231 | auto TempIpi = llvm::make_unique<TpiStream>(*this, std::move(*IpiS)); |
| 232 | if (auto EC = TempIpi->reload()) |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 233 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 234 | Ipi = std::move(TempIpi); |
Zachary Turner | c9972c6 | 2016-05-25 04:35:22 +0000 | [diff] [blame] | 235 | } |
| 236 | return *Ipi; |
| 237 | } |
| 238 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 239 | Expected<PublicsStream &> PDBFile::getPDBPublicsStream() { |
| 240 | if (!Publics) { |
| 241 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 242 | if (!DbiS) |
| 243 | return DbiS.takeError(); |
| 244 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 245 | uint32_t PublicsStreamNum = DbiS->getPublicSymbolStreamIndex(); |
| 246 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 247 | auto PublicS = |
| 248 | MappedBlockStream::createIndexedStream(PublicsStreamNum, *this); |
| 249 | if (!PublicS) |
| 250 | return PublicS.takeError(); |
| 251 | auto TempPublics = |
| 252 | llvm::make_unique<PublicsStream>(*this, std::move(*PublicS)); |
| 253 | if (auto EC = TempPublics->reload()) |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 254 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 255 | Publics = std::move(TempPublics); |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 256 | } |
| 257 | return *Publics; |
| 258 | } |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 259 | |
| 260 | Expected<SymbolStream &> PDBFile::getPDBSymbolStream() { |
| 261 | if (!Symbols) { |
| 262 | auto DbiS = getPDBDbiStream(); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 263 | if (!DbiS) |
| 264 | return DbiS.takeError(); |
| 265 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 266 | uint32_t SymbolStreamNum = DbiS->getSymRecordStreamIndex(); |
| 267 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 268 | auto SymbolS = |
| 269 | MappedBlockStream::createIndexedStream(SymbolStreamNum, *this); |
| 270 | if (!SymbolS) |
| 271 | return SymbolS.takeError(); |
| 272 | auto TempSymbols = llvm::make_unique<SymbolStream>(std::move(*SymbolS)); |
| 273 | if (auto EC = TempSymbols->reload()) |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 274 | return std::move(EC); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 275 | Symbols = std::move(TempSymbols); |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 276 | } |
| 277 | return *Symbols; |
| 278 | } |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 279 | |
| 280 | Expected<NameHashTable &> PDBFile::getStringTable() { |
| 281 | if (!StringTable || !StringTableStream) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 282 | auto IS = getPDBInfoStream(); |
| 283 | if (!IS) |
| 284 | return IS.takeError(); |
| 285 | |
| 286 | uint32_t NameStreamIndex = IS->getNamedStreamIndex("/names"); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 287 | |
| 288 | if (NameStreamIndex == 0) |
| 289 | return make_error<RawError>(raw_error_code::no_stream); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 290 | if (NameStreamIndex >= getNumStreams()) |
| 291 | return make_error<RawError>(raw_error_code::no_stream); |
| 292 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 293 | auto NS = MappedBlockStream::createIndexedStream(NameStreamIndex, *this); |
| 294 | if (!NS) |
| 295 | return NS.takeError(); |
| 296 | |
Zachary Turner | b84faa8 | 2016-06-10 05:10:19 +0000 | [diff] [blame] | 297 | StreamReader Reader(**NS); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 298 | auto N = llvm::make_unique<NameHashTable>(); |
| 299 | if (auto EC = N->load(Reader)) |
| 300 | return std::move(EC); |
| 301 | StringTable = std::move(N); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 302 | StringTableStream = std::move(*NS); |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 303 | } |
| 304 | return *StringTable; |
| 305 | } |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 306 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 307 | Error PDBFile::setSuperBlock(const SuperBlock *Block) { |
| 308 | SB = Block; |
| 309 | |
| 310 | // Check the magic bytes. |
| 311 | if (memcmp(SB->MagicBytes, MsfMagic, sizeof(MsfMagic)) != 0) |
| 312 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 313 | "MSF magic header doesn't match"); |
| 314 | |
| 315 | // We don't support blocksizes which aren't a multiple of four bytes. |
| 316 | if (SB->BlockSize % sizeof(support::ulittle32_t) != 0) |
| 317 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 318 | "Block size is not multiple of 4."); |
| 319 | |
| 320 | switch (SB->BlockSize) { |
| 321 | case 512: |
| 322 | case 1024: |
| 323 | case 2048: |
| 324 | case 4096: |
| 325 | break; |
| 326 | default: |
| 327 | // An invalid block size suggests a corrupt PDB file. |
| 328 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 329 | "Unsupported block size."); |
| 330 | } |
| 331 | |
| 332 | if (Buffer->getLength() % SB->BlockSize != 0) |
| 333 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 334 | "File size is not a multiple of block size"); |
| 335 | |
| 336 | // We don't support directories whose sizes aren't a multiple of four bytes. |
| 337 | if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0) |
| 338 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 339 | "Directory size is not multiple of 4."); |
| 340 | |
| 341 | // The number of blocks which comprise the directory is a simple function of |
| 342 | // the number of bytes it contains. |
| 343 | uint64_t NumDirectoryBlocks = getNumDirectoryBlocks(); |
| 344 | |
| 345 | // The directory, as we understand it, is a block which consists of a list of |
| 346 | // block numbers. It is unclear what would happen if the number of blocks |
| 347 | // couldn't fit on a single block. |
| 348 | if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t)) |
| 349 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 350 | "Too many directory blocks."); |
| 351 | |
| 352 | return Error::success(); |
| 353 | } |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 354 | |
| 355 | void PDBFile::setStreamSizes(ArrayRef<support::ulittle32_t> Sizes) { |
| 356 | StreamSizes = Sizes; |
| 357 | } |
| 358 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 359 | void PDBFile::setStreamMap( |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 360 | std::vector<ArrayRef<support::ulittle32_t>> &Streams) { |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 361 | StreamMap = Streams; |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 364 | void PDBFile::setDirectoryBlocks(ArrayRef<support::ulittle32_t> Directory) { |
| 365 | DirectoryBlocks = Directory; |
| 366 | } |
| 367 | |
| 368 | Error PDBFile::generateSimpleStreamMap() { |
| 369 | if (StreamSizes.empty()) |
| 370 | return Error::success(); |
| 371 | |
| 372 | static std::vector<std::vector<support::ulittle32_t>> StaticMap; |
| 373 | StreamMap.clear(); |
| 374 | StaticMap.clear(); |
| 375 | |
| 376 | // Figure out how many blocks are needed for all streams, and set the first |
| 377 | // used block to the highest block so that we can write the rest of the |
| 378 | // blocks contiguously. |
| 379 | uint32_t TotalFileBlocks = getBlockCount(); |
| 380 | std::vector<support::ulittle32_t> ReservedBlocks; |
| 381 | ReservedBlocks.push_back(support::ulittle32_t(0)); |
| 382 | ReservedBlocks.push_back(SB->BlockMapAddr); |
| 383 | ReservedBlocks.insert(ReservedBlocks.end(), DirectoryBlocks.begin(), |
| 384 | DirectoryBlocks.end()); |
| 385 | |
| 386 | uint32_t BlocksNeeded = 0; |
| 387 | for (auto Size : StreamSizes) |
| 388 | BlocksNeeded += bytesToBlocks(Size, getBlockSize()); |
| 389 | |
| 390 | support::ulittle32_t NextBlock(TotalFileBlocks - BlocksNeeded - |
| 391 | ReservedBlocks.size()); |
| 392 | |
| 393 | StaticMap.resize(StreamSizes.size()); |
| 394 | for (uint32_t S = 0; S < StreamSizes.size(); ++S) { |
| 395 | uint32_t Size = StreamSizes[S]; |
| 396 | uint32_t NumBlocks = bytesToBlocks(Size, getBlockSize()); |
| 397 | auto &ThisStream = StaticMap[S]; |
| 398 | for (uint32_t I = 0; I < NumBlocks;) { |
| 399 | NextBlock += 1; |
| 400 | if (std::find(ReservedBlocks.begin(), ReservedBlocks.end(), NextBlock) != |
| 401 | ReservedBlocks.end()) |
| 402 | continue; |
| 403 | |
| 404 | ++I; |
| 405 | assert(NextBlock < getBlockCount()); |
| 406 | ThisStream.push_back(NextBlock); |
| 407 | } |
| 408 | StreamMap.push_back(ThisStream); |
| 409 | } |
| 410 | return Error::success(); |
| 411 | } |
| 412 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 413 | Error PDBFile::commit() { |
| 414 | StreamWriter Writer(*Buffer); |
| 415 | |
| 416 | if (auto EC = Writer.writeObject(*SB)) |
| 417 | return EC; |
| 418 | Writer.setOffset(getBlockMapOffset()); |
| 419 | if (auto EC = Writer.writeArray(DirectoryBlocks)) |
| 420 | return EC; |
| 421 | |
| 422 | auto DS = MappedBlockStream::createDirectoryStream(*this); |
| 423 | if (!DS) |
| 424 | return DS.takeError(); |
| 425 | auto DirStream = std::move(*DS); |
| 426 | StreamWriter DW(*DirStream); |
| 427 | if (auto EC = DW.writeInteger(this->getNumStreams())) |
| 428 | return EC; |
| 429 | |
| 430 | if (auto EC = DW.writeArray(StreamSizes)) |
| 431 | return EC; |
| 432 | |
| 433 | for (const auto &Blocks : StreamMap) { |
| 434 | if (auto EC = DW.writeArray(Blocks)) |
| 435 | return EC; |
| 436 | } |
| 437 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 438 | if (Info) { |
| 439 | if (auto EC = Info->commit()) |
| 440 | return EC; |
| 441 | } |
| 442 | |
| 443 | if (Dbi) { |
| 444 | if (auto EC = Dbi->commit()) |
| 445 | return EC; |
| 446 | } |
| 447 | |
| 448 | if (Symbols) { |
| 449 | if (auto EC = Symbols->commit()) |
| 450 | return EC; |
| 451 | } |
| 452 | |
| 453 | if (Publics) { |
| 454 | if (auto EC = Publics->commit()) |
| 455 | return EC; |
| 456 | } |
| 457 | |
| 458 | if (Tpi) { |
| 459 | if (auto EC = Tpi->commit()) |
| 460 | return EC; |
| 461 | } |
| 462 | |
| 463 | if (Ipi) { |
| 464 | if (auto EC = Ipi->commit()) |
| 465 | return EC; |
| 466 | } |
| 467 | |
Zachary Turner | ab58ae8 | 2016-06-30 17:43:00 +0000 | [diff] [blame] | 468 | return Buffer->commit(); |
David Majnemer | 6211b1f | 2016-07-10 03:34:47 +0000 | [diff] [blame] | 469 | } |