Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 1 | //===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===// |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 9 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Zachary Turner | 67c5601 | 2017-04-27 16:11:19 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h" |
| 15 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/RawConstants.h" |
| 18 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
| 19 | #include "llvm/DebugInfo/PDB/Native/RawTypes.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/PDBTypes.h" |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 21 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 22 | #include "llvm/Support/BinaryStreamArray.h" |
| 23 | #include "llvm/Support/BinaryStreamReader.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Error.h" |
| 25 | #include <algorithm> |
| 26 | #include <cstddef> |
| 27 | #include <cstdint> |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 30 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 31 | using namespace llvm::msf; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 32 | using namespace llvm::pdb; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 33 | using namespace llvm::support; |
| 34 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 35 | template <typename ContribType> |
Benjamin Kramer | 4d09892 | 2016-07-10 11:28:51 +0000 | [diff] [blame] | 36 | static Error loadSectionContribs(FixedStreamArray<ContribType> &Output, |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 37 | BinaryStreamReader &Reader) { |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 38 | if (Reader.bytesRemaining() % sizeof(ContribType) != 0) |
| 39 | return make_error<RawError>( |
| 40 | raw_error_code::corrupt_file, |
| 41 | "Invalid number of bytes of section contributions"); |
| 42 | |
| 43 | uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType); |
| 44 | if (auto EC = Reader.readArray(Output, Count)) |
| 45 | return EC; |
| 46 | return Error::success(); |
| 47 | } |
| 48 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 49 | DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream) |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 50 | : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {} |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 51 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 52 | DbiStream::~DbiStream() = default; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 53 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 54 | Error DbiStream::reload() { |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 55 | BinaryStreamReader Reader(*Stream); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 56 | |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 57 | if (Stream->getLength() < sizeof(DbiStreamHeader)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 58 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 59 | "DBI Stream does not contain a header."); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 60 | if (auto EC = Reader.readObject(Header)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 61 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 62 | "DBI Stream does not contain a header."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 63 | |
| 64 | if (Header->VersionSignature != -1) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 65 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 66 | "Invalid DBI version signature."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 67 | |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 68 | // Require at least version 7, which should be present in all PDBs |
| 69 | // produced in the last decade and allows us to avoid having to |
| 70 | // special case all kinds of complicated arcane formats. |
| 71 | if (Header->VersionHeader < PdbDbiV70) |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 72 | return make_error<RawError>(raw_error_code::feature_unsupported, |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 73 | "Unsupported DBI version."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 74 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 75 | if (Stream->getLength() != |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 76 | sizeof(DbiStreamHeader) + Header->ModiSubstreamSize + |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 77 | Header->SecContrSubstreamSize + Header->SectionMapSize + |
| 78 | Header->FileInfoSize + Header->TypeServerSize + |
| 79 | Header->OptionalDbgHdrSize + Header->ECSubstreamSize) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 80 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 81 | "DBI Length does not equal sum of substreams."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 82 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 83 | // Only certain substreams are guaranteed to be aligned. Validate |
| 84 | // them here. |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 85 | if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 86 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 87 | "DBI MODI substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 88 | if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 89 | return make_error<RawError>( |
| 90 | raw_error_code::corrupt_file, |
| 91 | "DBI section contribution substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 92 | if (Header->SectionMapSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 93 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 94 | "DBI section map substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 95 | if (Header->FileInfoSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 96 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 97 | "DBI file info substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 98 | if (Header->TypeServerSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 99 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 100 | "DBI type server substream not aligned."); |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 101 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 102 | if (auto EC = Reader.readSubstream(ModiSubstream, Header->ModiSubstreamSize)) |
Zachary Turner | 1de49c9 | 2016-05-27 18:47:20 +0000 | [diff] [blame] | 103 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 104 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 105 | if (auto EC = Reader.readSubstream(SecContrSubstream, |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 106 | Header->SecContrSubstreamSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 107 | return EC; |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 108 | if (auto EC = Reader.readSubstream(SecMapSubstream, Header->SectionMapSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 109 | return EC; |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 110 | if (auto EC = Reader.readSubstream(FileInfoSubstream, Header->FileInfoSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 111 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 112 | if (auto EC = |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 113 | Reader.readSubstream(TypeServerMapSubstream, Header->TypeServerSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 114 | return EC; |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 115 | if (auto EC = Reader.readSubstream(ECSubstream, Header->ECSubstreamSize)) |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 116 | return EC; |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 117 | if (auto EC = Reader.readArray( |
| 118 | DbgStreams, Header->OptionalDbgHdrSize / sizeof(ulittle16_t))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 119 | return EC; |
| 120 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 121 | if (auto EC = Modules.initialize(ModiSubstream.StreamData, |
| 122 | FileInfoSubstream.StreamData)) |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 123 | return EC; |
| 124 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 125 | if (auto EC = initializeSectionContributionData()) |
| 126 | return EC; |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 127 | if (auto EC = initializeSectionHeadersData()) |
| 128 | return EC; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 129 | if (auto EC = initializeSectionMapData()) |
| 130 | return EC; |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 131 | if (auto EC = initializeFpoRecords()) |
| 132 | return EC; |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 133 | |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 134 | if (Reader.bytesRemaining() > 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 135 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 136 | "Found unexpected bytes in DBI Stream."); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 137 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 138 | if (!ECSubstream.empty()) { |
| 139 | BinaryStreamReader ECReader(ECSubstream.StreamData); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 140 | if (auto EC = ECNames.reload(ECReader)) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 141 | return EC; |
| 142 | } |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 143 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 144 | return Error::success(); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 147 | PdbRaw_DbiVer DbiStream::getDbiVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 148 | uint32_t Value = Header->VersionHeader; |
| 149 | return static_cast<PdbRaw_DbiVer>(Value); |
| 150 | } |
| 151 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 152 | uint32_t DbiStream::getAge() const { return Header->Age; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 153 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 154 | uint16_t DbiStream::getPublicSymbolStreamIndex() const { |
| 155 | return Header->PublicSymbolStreamIndex; |
| 156 | } |
| 157 | |
Zachary Turner | 96e60f7 | 2016-05-24 20:31:48 +0000 | [diff] [blame] | 158 | uint16_t DbiStream::getGlobalSymbolStreamIndex() const { |
| 159 | return Header->GlobalSymbolStreamIndex; |
| 160 | } |
| 161 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 162 | uint16_t DbiStream::getFlags() const { return Header->Flags; } |
| 163 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 164 | bool DbiStream::isIncrementallyLinked() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 165 | return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 168 | bool DbiStream::hasCTypes() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 169 | return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 172 | bool DbiStream::isStripped() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 173 | return (Header->Flags & DbiFlags::FlagStrippedMask) != 0; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 176 | uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; } |
| 177 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 178 | uint16_t DbiStream::getBuildMajorVersion() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 179 | return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >> |
| 180 | DbiBuildNo::BuildMajorShift; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 183 | uint16_t DbiStream::getBuildMinorVersion() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 184 | return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >> |
| 185 | DbiBuildNo::BuildMinorShift; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 188 | uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; } |
| 189 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 190 | uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 191 | |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 192 | uint32_t DbiStream::getSymRecordStreamIndex() const { |
| 193 | return Header->SymRecordStreamIndex; |
| 194 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 195 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 196 | PDB_Machine DbiStream::getMachineType() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 197 | uint16_t Machine = Header->MachineType; |
| 198 | return static_cast<PDB_Machine>(Machine); |
| 199 | } |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 200 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 201 | FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() { |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 202 | return SectionHeaders; |
| 203 | } |
| 204 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 205 | FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() { |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 206 | return FpoRecords; |
| 207 | } |
| 208 | |
Zachary Turner | 1eb9a02 | 2017-05-04 23:53:29 +0000 | [diff] [blame] | 209 | const DbiModuleList &DbiStream::modules() const { return Modules; } |
| 210 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 211 | FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const { |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 212 | return SectionMap; |
| 213 | } |
| 214 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 215 | void DbiStream::visitSectionContributions( |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 216 | ISectionContribVisitor &Visitor) const { |
Zachary Turner | 1bfb9f4 | 2017-06-06 23:54:23 +0000 | [diff] [blame] | 217 | if (!SectionContribs.empty()) { |
| 218 | assert(SectionContribVersion == DbiSecContribVer60); |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 219 | for (auto &SC : SectionContribs) |
| 220 | Visitor.visit(SC); |
Zachary Turner | 1bfb9f4 | 2017-06-06 23:54:23 +0000 | [diff] [blame] | 221 | } else if (!SectionContribs2.empty()) { |
| 222 | assert(SectionContribVersion == DbiSecContribV2); |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 223 | for (auto &SC : SectionContribs2) |
| 224 | Visitor.visit(SC); |
| 225 | } |
| 226 | } |
| 227 | |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 228 | Expected<StringRef> DbiStream::getECName(uint32_t NI) const { |
| 229 | return ECNames.getStringForID(NI); |
| 230 | } |
| 231 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 232 | Error DbiStream::initializeSectionContributionData() { |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 233 | if (SecContrSubstream.empty()) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 234 | return Error::success(); |
| 235 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 236 | BinaryStreamReader SCReader(SecContrSubstream.StreamData); |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 237 | if (auto EC = SCReader.readEnum(SectionContribVersion)) |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 238 | return EC; |
| 239 | |
| 240 | if (SectionContribVersion == DbiSecContribVer60) |
| 241 | return loadSectionContribs<SectionContrib>(SectionContribs, SCReader); |
| 242 | if (SectionContribVersion == DbiSecContribV2) |
| 243 | return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader); |
| 244 | |
| 245 | return make_error<RawError>(raw_error_code::feature_unsupported, |
| 246 | "Unsupported DBI Section Contribution version"); |
| 247 | } |
| 248 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 249 | // Initializes this->SectionHeaders. |
| 250 | Error DbiStream::initializeSectionHeadersData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 251 | if (DbgStreams.size() == 0) |
| 252 | return Error::success(); |
| 253 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 254 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 255 | if (StreamNum >= Pdb.getNumStreams()) |
| 256 | return make_error<RawError>(raw_error_code::no_stream); |
| 257 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 258 | auto SHS = MappedBlockStream::createIndexedStream( |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 259 | Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum, Pdb.getAllocator()); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 260 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 261 | size_t StreamLen = SHS->getLength(); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 262 | if (StreamLen % sizeof(object::coff_section)) |
| 263 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 264 | "Corrupted section header stream."); |
| 265 | |
| 266 | size_t NumSections = StreamLen / sizeof(object::coff_section); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 267 | BinaryStreamReader Reader(*SHS); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 268 | if (auto EC = Reader.readArray(SectionHeaders, NumSections)) |
| 269 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 270 | "Could not read a bitmap."); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 271 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 272 | SectionHeaderStream = std::move(SHS); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 273 | return Error::success(); |
| 274 | } |
| 275 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 276 | // Initializes this->Fpos. |
| 277 | Error DbiStream::initializeFpoRecords() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 278 | if (DbgStreams.size() == 0) |
| 279 | return Error::success(); |
| 280 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 281 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO); |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 282 | |
| 283 | // This means there is no FPO data. |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 284 | if (StreamNum == kInvalidStreamIndex) |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 285 | return Error::success(); |
| 286 | |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 287 | if (StreamNum >= Pdb.getNumStreams()) |
| 288 | return make_error<RawError>(raw_error_code::no_stream); |
| 289 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 290 | auto FS = MappedBlockStream::createIndexedStream( |
Zachary Turner | 5b74ff3 | 2017-06-03 00:33:35 +0000 | [diff] [blame] | 291 | Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum, Pdb.getAllocator()); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 292 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 293 | size_t StreamLen = FS->getLength(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 294 | if (StreamLen % sizeof(object::FpoData)) |
| 295 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 296 | "Corrupted New FPO stream."); |
| 297 | |
| 298 | size_t NumRecords = StreamLen / sizeof(object::FpoData); |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 299 | BinaryStreamReader Reader(*FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 300 | if (auto EC = Reader.readArray(FpoRecords, NumRecords)) |
| 301 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 302 | "Corrupted New FPO stream."); |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 303 | FpoStream = std::move(FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 304 | return Error::success(); |
| 305 | } |
| 306 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 307 | BinarySubstreamRef DbiStream::getSectionContributionData() const { |
| 308 | return SecContrSubstream; |
| 309 | } |
| 310 | |
| 311 | BinarySubstreamRef DbiStream::getSecMapSubstreamData() const { |
| 312 | return SecMapSubstream; |
| 313 | } |
| 314 | |
| 315 | BinarySubstreamRef DbiStream::getModiSubstreamData() const { |
| 316 | return ModiSubstream; |
| 317 | } |
| 318 | |
| 319 | BinarySubstreamRef DbiStream::getFileInfoSubstreamData() const { |
| 320 | return FileInfoSubstream; |
| 321 | } |
| 322 | |
| 323 | BinarySubstreamRef DbiStream::getTypeServerMapSubstreamData() const { |
| 324 | return TypeServerMapSubstream; |
| 325 | } |
| 326 | |
| 327 | BinarySubstreamRef DbiStream::getECSubstreamData() const { return ECSubstream; } |
| 328 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 329 | Error DbiStream::initializeSectionMapData() { |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 330 | if (SecMapSubstream.empty()) |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 331 | return Error::success(); |
| 332 | |
Zachary Turner | dd73968 | 2017-06-23 21:11:54 +0000 | [diff] [blame] | 333 | BinaryStreamReader SMReader(SecMapSubstream.StreamData); |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 334 | const SecMapHeader *Header; |
| 335 | if (auto EC = SMReader.readObject(Header)) |
| 336 | return EC; |
| 337 | if (auto EC = SMReader.readArray(SectionMap, Header->SecCount)) |
| 338 | return EC; |
| 339 | return Error::success(); |
| 340 | } |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 341 | |
Zachary Turner | d3076ab | 2016-05-25 05:49:48 +0000 | [diff] [blame] | 342 | uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const { |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 343 | uint16_t T = static_cast<uint16_t>(Type); |
| 344 | if (T >= DbgStreams.size()) |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 345 | return kInvalidStreamIndex; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 346 | return DbgStreams[T]; |
Zachary Turner | d3076ab | 2016-05-25 05:49:48 +0000 | [diff] [blame] | 347 | } |