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