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 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 11 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/CodeView/StreamArray.h" |
Zachary Turner | d5d37dc | 2016-05-25 20:37:03 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/CodeView/StreamReader.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/CodeView/StreamWriter.h" |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Raw/ModInfo.h" |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/PDB/Raw/RawTypes.h" |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 24 | #include "llvm/Object/COFF.h" |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 27 | using namespace llvm::codeview; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 28 | using namespace llvm::pdb; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 29 | using namespace llvm::support; |
| 30 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 31 | template <typename ContribType> |
Benjamin Kramer | 4d09892 | 2016-07-10 11:28:51 +0000 | [diff] [blame] | 32 | static Error loadSectionContribs(FixedStreamArray<ContribType> &Output, |
| 33 | StreamReader &Reader) { |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 34 | if (Reader.bytesRemaining() % sizeof(ContribType) != 0) |
| 35 | return make_error<RawError>( |
| 36 | raw_error_code::corrupt_file, |
| 37 | "Invalid number of bytes of section contributions"); |
| 38 | |
| 39 | uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType); |
| 40 | if (auto EC = Reader.readArray(Output, Count)) |
| 41 | return EC; |
| 42 | return Error::success(); |
| 43 | } |
| 44 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 45 | DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream) |
| 46 | : Pdb(File), Stream(std::move(Stream)), Header(nullptr) { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 49 | DbiStream::~DbiStream() {} |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 50 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 51 | Error DbiStream::reload() { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 52 | StreamReader Reader(*Stream); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 53 | |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 54 | if (Stream->getLength() < sizeof(DbiStreamHeader)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 55 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 56 | "DBI Stream does not contain a header."); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 57 | if (auto EC = Reader.readObject(Header)) |
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 | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 60 | |
| 61 | if (Header->VersionSignature != -1) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 62 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 63 | "Invalid DBI version signature."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 64 | |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 65 | // Require at least version 7, which should be present in all PDBs |
| 66 | // produced in the last decade and allows us to avoid having to |
| 67 | // special case all kinds of complicated arcane formats. |
| 68 | if (Header->VersionHeader < PdbDbiV70) |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 69 | return make_error<RawError>(raw_error_code::feature_unsupported, |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 70 | "Unsupported DBI version."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 71 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 72 | auto IS = Pdb.getPDBInfoStream(); |
| 73 | if (!IS) |
| 74 | return IS.takeError(); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 75 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 76 | if (Header->Age != IS->getAge()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 77 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 78 | "DBI Age does not match PDB Age."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 79 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 80 | if (Stream->getLength() != |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 81 | sizeof(DbiStreamHeader) + Header->ModiSubstreamSize + |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 82 | Header->SecContrSubstreamSize + Header->SectionMapSize + |
| 83 | Header->FileInfoSize + Header->TypeServerSize + |
| 84 | Header->OptionalDbgHdrSize + Header->ECSubstreamSize) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 85 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 86 | "DBI Length does not equal sum of substreams."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 87 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 88 | // Only certain substreams are guaranteed to be aligned. Validate |
| 89 | // them here. |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 90 | if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 91 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 92 | "DBI MODI substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 93 | if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 94 | return make_error<RawError>( |
| 95 | raw_error_code::corrupt_file, |
| 96 | "DBI section contribution substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 97 | if (Header->SectionMapSize % 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 section map substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 100 | if (Header->FileInfoSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 101 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 102 | "DBI file info substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 103 | if (Header->TypeServerSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 104 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 105 | "DBI type server substream not aligned."); |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 106 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 107 | if (auto EC = |
| 108 | Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize)) |
Zachary Turner | 1de49c9 | 2016-05-27 18:47:20 +0000 | [diff] [blame] | 109 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 110 | if (auto EC = initializeModInfoArray()) |
| 111 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 112 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 113 | if (auto EC = Reader.readStreamRef(SecContrSubstream, |
| 114 | Header->SecContrSubstreamSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 115 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 116 | if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 117 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 118 | if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 119 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 120 | if (auto EC = |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 121 | Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 122 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 123 | if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize)) |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 124 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 125 | if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize / |
| 126 | sizeof(ulittle16_t))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 127 | return EC; |
| 128 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 129 | if (auto EC = initializeSectionContributionData()) |
| 130 | return EC; |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 131 | if (auto EC = initializeSectionHeadersData()) |
| 132 | return EC; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 133 | if (auto EC = initializeSectionMapData()) |
| 134 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 135 | if (auto EC = initializeFileInfo()) |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 136 | return EC; |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 137 | if (auto EC = initializeFpoRecords()) |
| 138 | return EC; |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 139 | |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 140 | if (Reader.bytesRemaining() > 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 141 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 142 | "Found unexpected bytes in DBI Stream."); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 143 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 144 | if (ECSubstream.getLength() > 0) { |
| 145 | StreamReader ECReader(ECSubstream); |
| 146 | if (auto EC = ECNames.load(ECReader)) |
| 147 | return EC; |
| 148 | } |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 149 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 150 | return Error::success(); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 153 | PdbRaw_DbiVer DbiStream::getDbiVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 154 | uint32_t Value = Header->VersionHeader; |
| 155 | return static_cast<PdbRaw_DbiVer>(Value); |
| 156 | } |
| 157 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 158 | uint32_t DbiStream::getAge() const { return Header->Age; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 159 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 160 | uint16_t DbiStream::getPublicSymbolStreamIndex() const { |
| 161 | return Header->PublicSymbolStreamIndex; |
| 162 | } |
| 163 | |
Zachary Turner | 96e60f7 | 2016-05-24 20:31:48 +0000 | [diff] [blame] | 164 | uint16_t DbiStream::getGlobalSymbolStreamIndex() const { |
| 165 | return Header->GlobalSymbolStreamIndex; |
| 166 | } |
| 167 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 168 | uint16_t DbiStream::getFlags() const { return Header->Flags; } |
| 169 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 170 | bool DbiStream::isIncrementallyLinked() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 171 | return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 174 | bool DbiStream::hasCTypes() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 175 | return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 178 | bool DbiStream::isStripped() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 179 | return (Header->Flags & DbiFlags::FlagStrippedMask) != 0; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 182 | uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; } |
| 183 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 184 | uint16_t DbiStream::getBuildMajorVersion() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 185 | return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >> |
| 186 | DbiBuildNo::BuildMajorShift; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 189 | uint16_t DbiStream::getBuildMinorVersion() const { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 190 | return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >> |
| 191 | DbiBuildNo::BuildMinorShift; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 194 | uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; } |
| 195 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 196 | uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 197 | |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 198 | uint32_t DbiStream::getSymRecordStreamIndex() const { |
| 199 | return Header->SymRecordStreamIndex; |
| 200 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 201 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 202 | PDB_Machine DbiStream::getMachineType() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 203 | uint16_t Machine = Header->MachineType; |
| 204 | return static_cast<PDB_Machine>(Machine); |
| 205 | } |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 206 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 207 | codeview::FixedStreamArray<object::coff_section> |
| 208 | DbiStream::getSectionHeaders() { |
| 209 | return SectionHeaders; |
| 210 | } |
| 211 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 212 | codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() { |
| 213 | return FpoRecords; |
| 214 | } |
| 215 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 216 | ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; } |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 217 | codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const { |
| 218 | return SectionMap; |
| 219 | } |
| 220 | |
| 221 | void llvm::pdb::DbiStream::visitSectionContributions( |
| 222 | ISectionContribVisitor &Visitor) const { |
| 223 | if (SectionContribVersion == DbiSecContribVer60) { |
| 224 | for (auto &SC : SectionContribs) |
| 225 | Visitor.visit(SC); |
| 226 | } else if (SectionContribVersion == DbiSecContribV2) { |
| 227 | for (auto &SC : SectionContribs2) |
| 228 | Visitor.visit(SC); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | Error DbiStream::initializeSectionContributionData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 233 | if (SecContrSubstream.getLength() == 0) |
| 234 | return Error::success(); |
| 235 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 236 | StreamReader SCReader(SecContrSubstream); |
| 237 | if (auto EC = SCReader.readEnum(SectionContribVersion)) |
| 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 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 249 | Error DbiStream::initializeModInfoArray() { |
| 250 | if (ModInfoSubstream.getLength() == 0) |
| 251 | return Error::success(); |
| 252 | |
| 253 | // Since each ModInfo in the stream is a variable length, we have to iterate |
| 254 | // them to know how many there actually are. |
| 255 | StreamReader Reader(ModInfoSubstream); |
| 256 | |
| 257 | VarStreamArray<ModInfo> ModInfoArray; |
| 258 | if (auto EC = Reader.readArray(ModInfoArray, ModInfoSubstream.getLength())) |
| 259 | return EC; |
| 260 | for (auto &Info : ModInfoArray) { |
| 261 | ModuleInfos.emplace_back(Info); |
| 262 | } |
| 263 | |
| 264 | return Error::success(); |
| 265 | } |
| 266 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 267 | // Initializes this->SectionHeaders. |
| 268 | Error DbiStream::initializeSectionHeadersData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 269 | if (DbgStreams.size() == 0) |
| 270 | return Error::success(); |
| 271 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 272 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 273 | if (StreamNum >= Pdb.getNumStreams()) |
| 274 | return make_error<RawError>(raw_error_code::no_stream); |
| 275 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 276 | auto SHS = MappedBlockStream::createIndexedStream(StreamNum, Pdb); |
| 277 | if (!SHS) |
| 278 | return SHS.takeError(); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 279 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 280 | size_t StreamLen = (*SHS)->getLength(); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 281 | if (StreamLen % sizeof(object::coff_section)) |
| 282 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 283 | "Corrupted section header stream."); |
| 284 | |
| 285 | size_t NumSections = StreamLen / sizeof(object::coff_section); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 286 | codeview::StreamReader Reader(**SHS); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 287 | if (auto EC = Reader.readArray(SectionHeaders, NumSections)) |
| 288 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 289 | "Could not read a bitmap."); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 290 | |
| 291 | SectionHeaderStream = std::move(*SHS); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 292 | return Error::success(); |
| 293 | } |
| 294 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 295 | // Initializes this->Fpos. |
| 296 | Error DbiStream::initializeFpoRecords() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 297 | if (DbgStreams.size() == 0) |
| 298 | return Error::success(); |
| 299 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 300 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO); |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 301 | |
| 302 | // This means there is no FPO data. |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 303 | if (StreamNum == kInvalidStreamIndex) |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 304 | return Error::success(); |
| 305 | |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 306 | if (StreamNum >= Pdb.getNumStreams()) |
| 307 | return make_error<RawError>(raw_error_code::no_stream); |
| 308 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 309 | auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb); |
| 310 | if (!FS) |
| 311 | return FS.takeError(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 312 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 313 | size_t StreamLen = (*FS)->getLength(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 314 | if (StreamLen % sizeof(object::FpoData)) |
| 315 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 316 | "Corrupted New FPO stream."); |
| 317 | |
| 318 | size_t NumRecords = StreamLen / sizeof(object::FpoData); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 319 | codeview::StreamReader Reader(**FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 320 | if (auto EC = Reader.readArray(FpoRecords, NumRecords)) |
| 321 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 322 | "Corrupted New FPO stream."); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 323 | FpoStream = std::move(*FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 324 | return Error::success(); |
| 325 | } |
| 326 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 327 | Error DbiStream::initializeSectionMapData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 328 | if (SecMapSubstream.getLength() == 0) |
| 329 | return Error::success(); |
| 330 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 331 | StreamReader SMReader(SecMapSubstream); |
| 332 | const SecMapHeader *Header; |
| 333 | if (auto EC = SMReader.readObject(Header)) |
| 334 | return EC; |
| 335 | if (auto EC = SMReader.readArray(SectionMap, Header->SecCount)) |
| 336 | return EC; |
| 337 | return Error::success(); |
| 338 | } |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 339 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 340 | Error DbiStream::initializeFileInfo() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 341 | if (FileInfoSubstream.getLength() == 0) |
| 342 | return Error::success(); |
| 343 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 344 | const FileInfoSubstreamHeader *FH; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 345 | StreamReader FISR(FileInfoSubstream); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 346 | if (auto EC = FISR.readObject(FH)) |
| 347 | return EC; |
| 348 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 349 | // The number of modules in the stream should be the same as reported by |
| 350 | // the FileInfoSubstreamHeader. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 351 | if (FH->NumModules != ModuleInfos.size()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 352 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 353 | "FileInfo substream count doesn't match DBI."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 354 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 355 | FixedStreamArray<ulittle16_t> ModIndexArray; |
| 356 | FixedStreamArray<ulittle16_t> ModFileCountArray; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 357 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 358 | // First is an array of `NumModules` module indices. This is not used for the |
| 359 | // same reason that `NumSourceFiles` is not used. It's an array of uint16's, |
| 360 | // but it's possible there are more than 64k source files, which would imply |
| 361 | // more than 64k modules (e.g. object files) as well. So we ignore this |
| 362 | // field. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 363 | if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size())) |
| 364 | return EC; |
| 365 | if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size())) |
| 366 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 367 | |
| 368 | // Compute the real number of source files. |
| 369 | uint32_t NumSourceFiles = 0; |
| 370 | for (auto Count : ModFileCountArray) |
| 371 | NumSourceFiles += Count; |
| 372 | |
| 373 | // This is the array that in the reference implementation corresponds to |
| 374 | // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a |
| 375 | // pointer. Due to the mentioned problems of pointers causing difficulty |
| 376 | // when reading from the file on 64-bit systems, we continue to ignore that |
| 377 | // field in `ModInfo`, and instead build a vector of StringRefs and stores |
| 378 | // them in `ModuleInfoEx`. The value written to and read from the file is |
| 379 | // not used anyway, it is only there as a way to store the offsets for the |
| 380 | // purposes of later accessing the names at runtime. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 381 | if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles)) |
| 382 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 383 | |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 384 | if (auto EC = FISR.readStreamRef(NamesBuffer)) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 385 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 386 | |
| 387 | // We go through each ModuleInfo, determine the number N of source files for |
| 388 | // that module, and then get the next N offsets from the Offsets array, using |
| 389 | // them to get the corresponding N names from the Names buffer and associating |
| 390 | // each one with the corresponding module. |
| 391 | uint32_t NextFileIndex = 0; |
| 392 | for (size_t I = 0; I < ModuleInfos.size(); ++I) { |
| 393 | uint32_t NumFiles = ModFileCountArray[I]; |
| 394 | ModuleInfos[I].SourceFiles.resize(NumFiles); |
| 395 | for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 396 | auto ThisName = getFileNameForIndex(NextFileIndex); |
| 397 | if (!ThisName) |
| 398 | return ThisName.takeError(); |
| 399 | ModuleInfos[I].SourceFiles[J] = *ThisName; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 403 | return Error::success(); |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 404 | } |
Zachary Turner | d3076ab | 2016-05-25 05:49:48 +0000 | [diff] [blame] | 405 | |
| 406 | uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const { |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 407 | uint16_t T = static_cast<uint16_t>(Type); |
| 408 | if (T >= DbgStreams.size()) |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame^] | 409 | return kInvalidStreamIndex; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 410 | return DbgStreams[T]; |
Zachary Turner | d3076ab | 2016-05-25 05:49:48 +0000 | [diff] [blame] | 411 | } |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 412 | |
| 413 | Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const { |
| 414 | StreamReader Names(NamesBuffer); |
| 415 | if (Index >= FileNameOffsets.size()) |
| 416 | return make_error<RawError>(raw_error_code::index_out_of_bounds); |
| 417 | |
| 418 | uint32_t FileOffset = FileNameOffsets[Index]; |
| 419 | Names.setOffset(FileOffset); |
| 420 | StringRef Name; |
| 421 | if (auto EC = Names.readZeroString(Name)) |
| 422 | return std::move(EC); |
| 423 | return Name; |
| 424 | } |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 425 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 426 | Error DbiStream::commit() { |
| 427 | StreamWriter Writer(*Stream); |
| 428 | if (auto EC = Writer.writeObject(*Header)) |
| 429 | return EC; |
| 430 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 431 | if (auto EC = Writer.writeStreamRef(ModInfoSubstream)) |
| 432 | return EC; |
| 433 | |
| 434 | if (auto EC = Writer.writeStreamRef(SecContrSubstream, |
| 435 | SecContrSubstream.getLength())) |
| 436 | return EC; |
| 437 | if (auto EC = |
| 438 | Writer.writeStreamRef(SecMapSubstream, SecMapSubstream.getLength())) |
| 439 | return EC; |
| 440 | if (auto EC = Writer.writeStreamRef(FileInfoSubstream, |
| 441 | FileInfoSubstream.getLength())) |
| 442 | return EC; |
| 443 | if (auto EC = Writer.writeStreamRef(TypeServerMapSubstream, |
| 444 | TypeServerMapSubstream.getLength())) |
| 445 | return EC; |
| 446 | if (auto EC = Writer.writeStreamRef(ECSubstream, ECSubstream.getLength())) |
| 447 | return EC; |
| 448 | |
| 449 | if (Writer.bytesRemaining() > 0) |
| 450 | return make_error<RawError>(raw_error_code::invalid_format, |
| 451 | "Unexpected bytes found in DBI Stream"); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 452 | return Error::success(); |
| 453 | } |