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 | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame^] | 145 | if (auto EC = |
| 146 | Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize)) |
Zachary Turner | 1de49c9 | 2016-05-27 18:47:20 +0000 | [diff] [blame] | 147 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame^] | 148 | if (auto EC = initializeModInfoArray()) |
| 149 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 150 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 151 | if (auto EC = Reader.readStreamRef(SecContrSubstream, |
| 152 | Header->SecContrSubstreamSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 153 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 154 | if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 155 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 156 | if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 157 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 158 | if (auto EC = |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 159 | Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 160 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 161 | if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize)) |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 162 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 163 | if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize / |
| 164 | sizeof(ulittle16_t))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 165 | return EC; |
| 166 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 167 | if (auto EC = initializeSectionContributionData()) |
| 168 | return EC; |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 169 | if (auto EC = initializeSectionHeadersData()) |
| 170 | return EC; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 171 | if (auto EC = initializeSectionMapData()) |
| 172 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 173 | if (auto EC = initializeFileInfo()) |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 174 | return EC; |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 175 | if (auto EC = initializeFpoRecords()) |
| 176 | return EC; |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 177 | |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 178 | if (Reader.bytesRemaining() > 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 179 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 180 | "Found unexpected bytes in DBI Stream."); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 181 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 182 | if (ECSubstream.getLength() > 0) { |
| 183 | StreamReader ECReader(ECSubstream); |
| 184 | if (auto EC = ECNames.load(ECReader)) |
| 185 | return EC; |
| 186 | } |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 187 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 188 | return Error::success(); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 191 | PdbRaw_DbiVer DbiStream::getDbiVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 192 | uint32_t Value = Header->VersionHeader; |
| 193 | return static_cast<PdbRaw_DbiVer>(Value); |
| 194 | } |
| 195 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 196 | uint32_t DbiStream::getAge() const { return Header->Age; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 197 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 198 | uint16_t DbiStream::getPublicSymbolStreamIndex() const { |
| 199 | return Header->PublicSymbolStreamIndex; |
| 200 | } |
| 201 | |
Zachary Turner | 96e60f7 | 2016-05-24 20:31:48 +0000 | [diff] [blame] | 202 | uint16_t DbiStream::getGlobalSymbolStreamIndex() const { |
| 203 | return Header->GlobalSymbolStreamIndex; |
| 204 | } |
| 205 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 206 | uint16_t DbiStream::getFlags() const { return Header->Flags; } |
| 207 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 208 | bool DbiStream::isIncrementallyLinked() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 209 | return (Header->Flags & FlagIncrementalMask) != 0; |
| 210 | } |
| 211 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 212 | bool DbiStream::hasCTypes() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 213 | return (Header->Flags & FlagHasCTypesMask) != 0; |
| 214 | } |
| 215 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 216 | bool DbiStream::isStripped() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 217 | return (Header->Flags & FlagStrippedMask) != 0; |
| 218 | } |
| 219 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 220 | uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; } |
| 221 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 222 | uint16_t DbiStream::getBuildMajorVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 223 | return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift; |
| 224 | } |
| 225 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 226 | uint16_t DbiStream::getBuildMinorVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 227 | return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift; |
| 228 | } |
| 229 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 230 | uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; } |
| 231 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 232 | uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 233 | |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 234 | uint32_t DbiStream::getSymRecordStreamIndex() const { |
| 235 | return Header->SymRecordStreamIndex; |
| 236 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 237 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 238 | PDB_Machine DbiStream::getMachineType() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 239 | uint16_t Machine = Header->MachineType; |
| 240 | return static_cast<PDB_Machine>(Machine); |
| 241 | } |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 242 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 243 | codeview::FixedStreamArray<object::coff_section> |
| 244 | DbiStream::getSectionHeaders() { |
| 245 | return SectionHeaders; |
| 246 | } |
| 247 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 248 | codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() { |
| 249 | return FpoRecords; |
| 250 | } |
| 251 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 252 | ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; } |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 253 | codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const { |
| 254 | return SectionMap; |
| 255 | } |
| 256 | |
| 257 | void llvm::pdb::DbiStream::visitSectionContributions( |
| 258 | ISectionContribVisitor &Visitor) const { |
| 259 | if (SectionContribVersion == DbiSecContribVer60) { |
| 260 | for (auto &SC : SectionContribs) |
| 261 | Visitor.visit(SC); |
| 262 | } else if (SectionContribVersion == DbiSecContribV2) { |
| 263 | for (auto &SC : SectionContribs2) |
| 264 | Visitor.visit(SC); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | Error DbiStream::initializeSectionContributionData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 269 | if (SecContrSubstream.getLength() == 0) |
| 270 | return Error::success(); |
| 271 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 272 | StreamReader SCReader(SecContrSubstream); |
| 273 | if (auto EC = SCReader.readEnum(SectionContribVersion)) |
| 274 | return EC; |
| 275 | |
| 276 | if (SectionContribVersion == DbiSecContribVer60) |
| 277 | return loadSectionContribs<SectionContrib>(SectionContribs, SCReader); |
| 278 | if (SectionContribVersion == DbiSecContribV2) |
| 279 | return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader); |
| 280 | |
| 281 | return make_error<RawError>(raw_error_code::feature_unsupported, |
| 282 | "Unsupported DBI Section Contribution version"); |
| 283 | } |
| 284 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame^] | 285 | Error DbiStream::initializeModInfoArray() { |
| 286 | if (ModInfoSubstream.getLength() == 0) |
| 287 | return Error::success(); |
| 288 | |
| 289 | // Since each ModInfo in the stream is a variable length, we have to iterate |
| 290 | // them to know how many there actually are. |
| 291 | StreamReader Reader(ModInfoSubstream); |
| 292 | |
| 293 | VarStreamArray<ModInfo> ModInfoArray; |
| 294 | if (auto EC = Reader.readArray(ModInfoArray, ModInfoSubstream.getLength())) |
| 295 | return EC; |
| 296 | for (auto &Info : ModInfoArray) { |
| 297 | ModuleInfos.emplace_back(Info); |
| 298 | } |
| 299 | |
| 300 | return Error::success(); |
| 301 | } |
| 302 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 303 | // Initializes this->SectionHeaders. |
| 304 | Error DbiStream::initializeSectionHeadersData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 305 | if (DbgStreams.size() == 0) |
| 306 | return Error::success(); |
| 307 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 308 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr); |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 309 | if (StreamNum >= Pdb.getNumStreams()) |
| 310 | return make_error<RawError>(raw_error_code::no_stream); |
| 311 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 312 | auto SHS = MappedBlockStream::createIndexedStream(StreamNum, Pdb); |
| 313 | if (!SHS) |
| 314 | return SHS.takeError(); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 315 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 316 | size_t StreamLen = (*SHS)->getLength(); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 317 | if (StreamLen % sizeof(object::coff_section)) |
| 318 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 319 | "Corrupted section header stream."); |
| 320 | |
| 321 | size_t NumSections = StreamLen / sizeof(object::coff_section); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 322 | codeview::StreamReader Reader(**SHS); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 323 | if (auto EC = Reader.readArray(SectionHeaders, NumSections)) |
| 324 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 325 | "Could not read a bitmap."); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 326 | |
| 327 | SectionHeaderStream = std::move(*SHS); |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 328 | return Error::success(); |
| 329 | } |
| 330 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 331 | // Initializes this->Fpos. |
| 332 | Error DbiStream::initializeFpoRecords() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 333 | if (DbgStreams.size() == 0) |
| 334 | return Error::success(); |
| 335 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 336 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO); |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame] | 337 | |
| 338 | // This means there is no FPO data. |
| 339 | if (StreamNum == InvalidStreamIndex) |
| 340 | return Error::success(); |
| 341 | |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 342 | if (StreamNum >= Pdb.getNumStreams()) |
| 343 | return make_error<RawError>(raw_error_code::no_stream); |
| 344 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 345 | auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb); |
| 346 | if (!FS) |
| 347 | return FS.takeError(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 348 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 349 | size_t StreamLen = (*FS)->getLength(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 350 | if (StreamLen % sizeof(object::FpoData)) |
| 351 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 352 | "Corrupted New FPO stream."); |
| 353 | |
| 354 | size_t NumRecords = StreamLen / sizeof(object::FpoData); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 355 | codeview::StreamReader Reader(**FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 356 | if (auto EC = Reader.readArray(FpoRecords, NumRecords)) |
| 357 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 358 | "Corrupted New FPO stream."); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 359 | FpoStream = std::move(*FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 360 | return Error::success(); |
| 361 | } |
| 362 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 363 | Error DbiStream::initializeSectionMapData() { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 364 | if (SecMapSubstream.getLength() == 0) |
| 365 | return Error::success(); |
| 366 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 367 | StreamReader SMReader(SecMapSubstream); |
| 368 | const SecMapHeader *Header; |
| 369 | if (auto EC = SMReader.readObject(Header)) |
| 370 | return EC; |
| 371 | if (auto EC = SMReader.readArray(SectionMap, Header->SecCount)) |
| 372 | return EC; |
| 373 | return Error::success(); |
| 374 | } |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 375 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 376 | Error DbiStream::initializeFileInfo() { |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 377 | // The layout of the FileInfoSubstream is like this: |
| 378 | // struct { |
| 379 | // ulittle16_t NumModules; |
| 380 | // ulittle16_t NumSourceFiles; |
| 381 | // ulittle16_t ModIndices[NumModules]; |
| 382 | // ulittle16_t ModFileCounts[NumModules]; |
| 383 | // ulittle32_t FileNameOffsets[NumSourceFiles]; |
| 384 | // char Names[][NumSourceFiles]; |
| 385 | // }; |
| 386 | // with the caveat that `NumSourceFiles` cannot be trusted, so |
| 387 | // it is computed by summing `ModFileCounts`. |
| 388 | // |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 389 | if (FileInfoSubstream.getLength() == 0) |
| 390 | return Error::success(); |
| 391 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 392 | const FileInfoSubstreamHeader *FH; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 393 | StreamReader FISR(FileInfoSubstream); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 394 | if (auto EC = FISR.readObject(FH)) |
| 395 | return EC; |
| 396 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 397 | // The number of modules in the stream should be the same as reported by |
| 398 | // the FileInfoSubstreamHeader. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 399 | if (FH->NumModules != ModuleInfos.size()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 400 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 401 | "FileInfo substream count doesn't match DBI."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 402 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 403 | FixedStreamArray<ulittle16_t> ModIndexArray; |
| 404 | FixedStreamArray<ulittle16_t> ModFileCountArray; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 405 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 406 | // First is an array of `NumModules` module indices. This is not used for the |
| 407 | // same reason that `NumSourceFiles` is not used. It's an array of uint16's, |
| 408 | // but it's possible there are more than 64k source files, which would imply |
| 409 | // more than 64k modules (e.g. object files) as well. So we ignore this |
| 410 | // field. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 411 | if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size())) |
| 412 | return EC; |
| 413 | if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size())) |
| 414 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 415 | |
| 416 | // Compute the real number of source files. |
| 417 | uint32_t NumSourceFiles = 0; |
| 418 | for (auto Count : ModFileCountArray) |
| 419 | NumSourceFiles += Count; |
| 420 | |
| 421 | // This is the array that in the reference implementation corresponds to |
| 422 | // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a |
| 423 | // pointer. Due to the mentioned problems of pointers causing difficulty |
| 424 | // when reading from the file on 64-bit systems, we continue to ignore that |
| 425 | // field in `ModInfo`, and instead build a vector of StringRefs and stores |
| 426 | // them in `ModuleInfoEx`. The value written to and read from the file is |
| 427 | // not used anyway, it is only there as a way to store the offsets for the |
| 428 | // purposes of later accessing the names at runtime. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 429 | if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles)) |
| 430 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 431 | |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 432 | if (auto EC = FISR.readStreamRef(NamesBuffer)) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 433 | return EC; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 434 | |
| 435 | // We go through each ModuleInfo, determine the number N of source files for |
| 436 | // that module, and then get the next N offsets from the Offsets array, using |
| 437 | // them to get the corresponding N names from the Names buffer and associating |
| 438 | // each one with the corresponding module. |
| 439 | uint32_t NextFileIndex = 0; |
| 440 | for (size_t I = 0; I < ModuleInfos.size(); ++I) { |
| 441 | uint32_t NumFiles = ModFileCountArray[I]; |
| 442 | ModuleInfos[I].SourceFiles.resize(NumFiles); |
| 443 | for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 444 | auto ThisName = getFileNameForIndex(NextFileIndex); |
| 445 | if (!ThisName) |
| 446 | return ThisName.takeError(); |
| 447 | ModuleInfos[I].SourceFiles[J] = *ThisName; |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 451 | return Error::success(); |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 452 | } |
Zachary Turner | d3076ab | 2016-05-25 05:49:48 +0000 | [diff] [blame] | 453 | |
| 454 | uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const { |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame^] | 455 | uint16_t T = static_cast<uint16_t>(Type); |
| 456 | if (T >= DbgStreams.size()) |
| 457 | return DbiStream::InvalidStreamIndex; |
| 458 | return DbgStreams[T]; |
Zachary Turner | d3076ab | 2016-05-25 05:49:48 +0000 | [diff] [blame] | 459 | } |
Zachary Turner | 3df1bfa | 2016-06-03 05:52:57 +0000 | [diff] [blame] | 460 | |
| 461 | Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const { |
| 462 | StreamReader Names(NamesBuffer); |
| 463 | if (Index >= FileNameOffsets.size()) |
| 464 | return make_error<RawError>(raw_error_code::index_out_of_bounds); |
| 465 | |
| 466 | uint32_t FileOffset = FileNameOffsets[Index]; |
| 467 | Names.setOffset(FileOffset); |
| 468 | StringRef Name; |
| 469 | if (auto EC = Names.readZeroString(Name)) |
| 470 | return std::move(EC); |
| 471 | return Name; |
| 472 | } |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 473 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 474 | Error DbiStream::commit() { |
| 475 | StreamWriter Writer(*Stream); |
| 476 | if (auto EC = Writer.writeObject(*Header)) |
| 477 | return EC; |
| 478 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame^] | 479 | if (auto EC = Writer.writeStreamRef(ModInfoSubstream)) |
| 480 | return EC; |
| 481 | |
| 482 | if (auto EC = Writer.writeStreamRef(SecContrSubstream, |
| 483 | SecContrSubstream.getLength())) |
| 484 | return EC; |
| 485 | if (auto EC = |
| 486 | Writer.writeStreamRef(SecMapSubstream, SecMapSubstream.getLength())) |
| 487 | return EC; |
| 488 | if (auto EC = Writer.writeStreamRef(FileInfoSubstream, |
| 489 | FileInfoSubstream.getLength())) |
| 490 | return EC; |
| 491 | if (auto EC = Writer.writeStreamRef(TypeServerMapSubstream, |
| 492 | TypeServerMapSubstream.getLength())) |
| 493 | return EC; |
| 494 | if (auto EC = Writer.writeStreamRef(ECSubstream, ECSubstream.getLength())) |
| 495 | return EC; |
| 496 | |
| 497 | if (Writer.bytesRemaining() > 0) |
| 498 | return make_error<RawError>(raw_error_code::invalid_format, |
| 499 | "Unexpected bytes found in DBI Stream"); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 500 | return Error::success(); |
| 501 | } |