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