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 | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h" |
Zachary Turner | d844799 | 2016-06-07 05:28:55 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Raw/ModInfo.h" |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Raw/NameHashTable.h" |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Raw/RawTypes.h" |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 23 | #include "llvm/Object/COFF.h" |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 26 | using namespace llvm::codeview; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 27 | using namespace llvm::pdb; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 28 | using namespace llvm::support; |
| 29 | |
| 30 | namespace { |
| 31 | // Some of the values are stored in bitfields. Since this needs to be portable |
| 32 | // across compilers and architectures (big / little endian in particular) we |
| 33 | // can't use the actual structures below, but must instead do the shifting |
| 34 | // and masking ourselves. The struct definitions are provided for reference. |
| 35 | |
| 36 | // struct DbiFlags { |
| 37 | // uint16_t IncrementalLinking : 1; // True if linked incrementally |
| 38 | // uint16_t IsStripped : 1; // True if private symbols were stripped. |
| 39 | // uint16_t HasCTypes : 1; // True if linked with /debug:ctypes. |
| 40 | // uint16_t Reserved : 13; |
| 41 | //}; |
| 42 | const uint16_t FlagIncrementalMask = 0x0001; |
| 43 | const uint16_t FlagStrippedMask = 0x0002; |
| 44 | const uint16_t FlagHasCTypesMask = 0x0004; |
| 45 | |
| 46 | // struct DbiBuildNo { |
| 47 | // uint16_t MinorVersion : 8; |
| 48 | // uint16_t MajorVersion : 7; |
| 49 | // uint16_t NewVersionFormat : 1; |
| 50 | //}; |
| 51 | const uint16_t BuildMinorMask = 0x00FF; |
| 52 | const uint16_t BuildMinorShift = 0; |
| 53 | |
| 54 | const uint16_t BuildMajorMask = 0x7F00; |
| 55 | const uint16_t BuildMajorShift = 8; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 58 | struct DbiStream::HeaderInfo { |
Zachary Turner | ff788aa | 2016-04-26 19:24:10 +0000 | [diff] [blame] | 59 | little32_t VersionSignature; |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 60 | ulittle32_t VersionHeader; |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 61 | ulittle32_t Age; // Should match InfoStream. |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 62 | ulittle16_t GlobalSymbolStreamIndex; // Global symbol stream # |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 63 | ulittle16_t BuildNumber; // See DbiBuildNo structure. |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 64 | ulittle16_t PublicSymbolStreamIndex; // Public symbols stream # |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 65 | ulittle16_t PdbDllVersion; // version of mspdbNNN.dll |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 66 | ulittle16_t SymRecordStreamIndex; // Symbol records stream # |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 67 | ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll |
| 68 | little32_t ModiSubstreamSize; // Size of module info stream |
| 69 | little32_t SecContrSubstreamSize; // Size of sec. contribution stream |
| 70 | little32_t SectionMapSize; // Size of sec. map substream |
| 71 | little32_t FileInfoSize; // Size of file info substream |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 72 | little32_t TypeServerSize; // Size of type server map |
| 73 | ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server |
| 74 | little32_t OptionalDbgHdrSize; // Size of DbgHeader info |
| 75 | little32_t ECSubstreamSize; // Size of EC stream (what is EC?) |
| 76 | ulittle16_t Flags; // See DbiFlags enum. |
| 77 | ulittle16_t MachineType; // See PDB_MachineType enum. |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 78 | |
| 79 | ulittle32_t Reserved; // Pad to 64 bytes |
| 80 | }; |
| 81 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 82 | template <typename ContribType> |
| 83 | Error loadSectionContribs(FixedStreamArray<ContribType> &Output, |
| 84 | StreamReader &Reader) { |
| 85 | if (Reader.bytesRemaining() % sizeof(ContribType) != 0) |
| 86 | return make_error<RawError>( |
| 87 | raw_error_code::corrupt_file, |
| 88 | "Invalid number of bytes of section contributions"); |
| 89 | |
| 90 | uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType); |
| 91 | if (auto EC = Reader.readArray(Output, Count)) |
| 92 | return EC; |
| 93 | return Error::success(); |
| 94 | } |
| 95 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 96 | DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream) |
| 97 | : Pdb(File), Stream(std::move(Stream)), Header(nullptr) { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 98 | static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!"); |
| 99 | } |
| 100 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 101 | DbiStream::~DbiStream() {} |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 102 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 103 | Error DbiStream::reload() { |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 104 | StreamReader Reader(*Stream); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 105 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 106 | if (Stream->getLength() < sizeof(HeaderInfo)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 107 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 108 | "DBI Stream does not contain a header."); |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 109 | if (auto EC = Reader.readObject(Header)) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 110 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 111 | "DBI Stream does not contain a header."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 112 | |
| 113 | if (Header->VersionSignature != -1) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 114 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 115 | "Invalid DBI version signature."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 116 | |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 117 | // Require at least version 7, which should be present in all PDBs |
| 118 | // produced in the last decade and allows us to avoid having to |
| 119 | // special case all kinds of complicated arcane formats. |
| 120 | if (Header->VersionHeader < PdbDbiV70) |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 121 | return make_error<RawError>(raw_error_code::feature_unsupported, |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 122 | "Unsupported DBI version."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 123 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 124 | auto IS = Pdb.getPDBInfoStream(); |
| 125 | if (!IS) |
| 126 | return IS.takeError(); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 127 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 128 | if (Header->Age != IS->getAge()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 129 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 130 | "DBI Age does not match PDB Age."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 131 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 132 | if (Stream->getLength() != |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 133 | sizeof(HeaderInfo) + Header->ModiSubstreamSize + |
| 134 | Header->SecContrSubstreamSize + Header->SectionMapSize + |
| 135 | Header->FileInfoSize + Header->TypeServerSize + |
| 136 | Header->OptionalDbgHdrSize + Header->ECSubstreamSize) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 137 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 138 | "DBI Length does not equal sum of substreams."); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 139 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 140 | // Only certain substreams are guaranteed to be aligned. Validate |
| 141 | // them here. |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 142 | if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 143 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 144 | "DBI MODI substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 145 | if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 146 | return make_error<RawError>( |
| 147 | raw_error_code::corrupt_file, |
| 148 | "DBI section contribution substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 149 | if (Header->SectionMapSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 150 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 151 | "DBI section map substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 152 | if (Header->FileInfoSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 153 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 154 | "DBI file info substream not aligned."); |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 155 | if (Header->TypeServerSize % sizeof(uint32_t) != 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 156 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 157 | "DBI type server substream not aligned."); |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 158 | |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 159 | // Since each ModInfo in the stream is a variable length, we have to iterate |
| 160 | // them to know how many there actually are. |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 161 | VarStreamArray<ModInfo> ModInfoArray; |
Zachary Turner | 1de49c9 | 2016-05-27 18:47:20 +0000 | [diff] [blame] | 162 | if (auto EC = Reader.readArray(ModInfoArray, Header->ModiSubstreamSize)) |
| 163 | return EC; |
| 164 | for (auto &Info : ModInfoArray) { |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 165 | ModuleInfos.emplace_back(Info); |
| 166 | } |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 167 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 168 | if (auto EC = Reader.readStreamRef(SecContrSubstream, |
| 169 | Header->SecContrSubstreamSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 170 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 171 | if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 172 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 173 | if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 174 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 175 | if (auto EC = |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 176 | Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize)) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 177 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 178 | if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize)) |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 179 | return EC; |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 180 | if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize / |
| 181 | sizeof(ulittle16_t))) |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 182 | return EC; |
| 183 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 184 | if (auto EC = initializeSectionContributionData()) |
| 185 | return EC; |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 186 | if (auto EC = initializeSectionHeadersData()) |
| 187 | return EC; |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 188 | if (auto EC = initializeSectionMapData()) |
| 189 | return EC; |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 190 | if (auto EC = initializeFileInfo()) |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 191 | return EC; |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 192 | if (auto EC = initializeFpoRecords()) |
| 193 | return EC; |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 194 | |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 195 | if (Reader.bytesRemaining() > 0) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 196 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 197 | "Found unexpected bytes in DBI Stream."); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 198 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 199 | StreamReader ECReader(ECSubstream); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 200 | if (auto EC = ECNames.load(ECReader)) |
| 201 | return EC; |
Zachary Turner | 0eace0b | 2016-05-02 18:09:14 +0000 | [diff] [blame] | 202 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 203 | return Error::success(); |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 206 | PdbRaw_DbiVer DbiStream::getDbiVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 207 | uint32_t Value = Header->VersionHeader; |
| 208 | return static_cast<PdbRaw_DbiVer>(Value); |
| 209 | } |
| 210 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 211 | uint32_t DbiStream::getAge() const { return Header->Age; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 212 | |
Rui Ueyama | 1f6b6e2 | 2016-05-13 21:21:53 +0000 | [diff] [blame] | 213 | uint16_t DbiStream::getPublicSymbolStreamIndex() const { |
| 214 | return Header->PublicSymbolStreamIndex; |
| 215 | } |
| 216 | |
Zachary Turner | 96e60f7 | 2016-05-24 20:31:48 +0000 | [diff] [blame] | 217 | uint16_t DbiStream::getGlobalSymbolStreamIndex() const { |
| 218 | return Header->GlobalSymbolStreamIndex; |
| 219 | } |
| 220 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 221 | bool DbiStream::isIncrementallyLinked() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 222 | return (Header->Flags & FlagIncrementalMask) != 0; |
| 223 | } |
| 224 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 225 | bool DbiStream::hasCTypes() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 226 | return (Header->Flags & FlagHasCTypesMask) != 0; |
| 227 | } |
| 228 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 229 | bool DbiStream::isStripped() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 230 | return (Header->Flags & FlagStrippedMask) != 0; |
| 231 | } |
| 232 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 233 | uint16_t DbiStream::getBuildMajorVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 234 | return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift; |
| 235 | } |
| 236 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 237 | uint16_t DbiStream::getBuildMinorVersion() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 238 | return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift; |
| 239 | } |
| 240 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 241 | uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 242 | |
Rui Ueyama | 0376b1a | 2016-05-19 18:05:58 +0000 | [diff] [blame] | 243 | uint32_t DbiStream::getSymRecordStreamIndex() const { |
| 244 | return Header->SymRecordStreamIndex; |
| 245 | } |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 246 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 247 | PDB_Machine DbiStream::getMachineType() const { |
Zachary Turner | 53a65ba | 2016-04-26 18:42:34 +0000 | [diff] [blame] | 248 | uint16_t Machine = Header->MachineType; |
| 249 | return static_cast<PDB_Machine>(Machine); |
| 250 | } |
Zachary Turner | 1822af54 | 2016-04-27 23:41:42 +0000 | [diff] [blame] | 251 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 252 | codeview::FixedStreamArray<object::coff_section> |
| 253 | DbiStream::getSectionHeaders() { |
| 254 | return SectionHeaders; |
| 255 | } |
| 256 | |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 257 | codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() { |
| 258 | return FpoRecords; |
| 259 | } |
| 260 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 261 | ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; } |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 262 | codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const { |
| 263 | return SectionMap; |
| 264 | } |
| 265 | |
| 266 | void llvm::pdb::DbiStream::visitSectionContributions( |
| 267 | ISectionContribVisitor &Visitor) const { |
| 268 | if (SectionContribVersion == DbiSecContribVer60) { |
| 269 | for (auto &SC : SectionContribs) |
| 270 | Visitor.visit(SC); |
| 271 | } else if (SectionContribVersion == DbiSecContribV2) { |
| 272 | for (auto &SC : SectionContribs2) |
| 273 | Visitor.visit(SC); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | Error DbiStream::initializeSectionContributionData() { |
| 278 | StreamReader SCReader(SecContrSubstream); |
| 279 | if (auto EC = SCReader.readEnum(SectionContribVersion)) |
| 280 | return EC; |
| 281 | |
| 282 | if (SectionContribVersion == DbiSecContribVer60) |
| 283 | return loadSectionContribs<SectionContrib>(SectionContribs, SCReader); |
| 284 | if (SectionContribVersion == DbiSecContribV2) |
| 285 | return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader); |
| 286 | |
| 287 | return make_error<RawError>(raw_error_code::feature_unsupported, |
| 288 | "Unsupported DBI Section Contribution version"); |
| 289 | } |
| 290 | |
Rui Ueyama | 90db788 | 2016-06-02 18:20:20 +0000 | [diff] [blame] | 291 | // Initializes this->SectionHeaders. |
| 292 | Error DbiStream::initializeSectionHeadersData() { |
| 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() { |
| 318 | uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO); |
Reid Kleckner | 11582c5 | 2016-06-17 20:38:01 +0000 | [diff] [blame^] | 319 | |
| 320 | // This means there is no FPO data. |
| 321 | if (StreamNum == InvalidStreamIndex) |
| 322 | return Error::success(); |
| 323 | |
Zachary Turner | d2b2bfe | 2016-06-08 00:25:08 +0000 | [diff] [blame] | 324 | if (StreamNum >= Pdb.getNumStreams()) |
| 325 | return make_error<RawError>(raw_error_code::no_stream); |
| 326 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 327 | auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb); |
| 328 | if (!FS) |
| 329 | return FS.takeError(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 330 | |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 331 | size_t StreamLen = (*FS)->getLength(); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 332 | if (StreamLen % sizeof(object::FpoData)) |
| 333 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 334 | "Corrupted New FPO stream."); |
| 335 | |
| 336 | size_t NumRecords = StreamLen / sizeof(object::FpoData); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 337 | codeview::StreamReader Reader(**FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 338 | if (auto EC = Reader.readArray(FpoRecords, NumRecords)) |
| 339 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 340 | "Corrupted New FPO stream."); |
Zachary Turner | a1657a9 | 2016-06-08 17:26:39 +0000 | [diff] [blame] | 341 | FpoStream = std::move(*FS); |
Rui Ueyama | ef2b488 | 2016-06-06 18:39:21 +0000 | [diff] [blame] | 342 | return Error::success(); |
| 343 | } |
| 344 | |
Zachary Turner | 93839cb | 2016-06-02 05:07:49 +0000 | [diff] [blame] | 345 | Error DbiStream::initializeSectionMapData() { |
| 346 | StreamReader SMReader(SecMapSubstream); |
| 347 | const SecMapHeader *Header; |
| 348 | if (auto EC = SMReader.readObject(Header)) |
| 349 | return EC; |
| 350 | if (auto EC = SMReader.readArray(SectionMap, Header->SecCount)) |
| 351 | return EC; |
| 352 | return Error::success(); |
| 353 | } |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 354 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 355 | Error DbiStream::initializeFileInfo() { |
Zachary Turner | 84c3a8b | 2016-04-28 20:05:18 +0000 | [diff] [blame] | 356 | struct FileInfoSubstreamHeader { |
| 357 | ulittle16_t NumModules; // Total # of modules, should match number of |
| 358 | // records in the ModuleInfo substream. |
| 359 | ulittle16_t NumSourceFiles; // Total # of source files. This value is not |
| 360 | // accurate because PDB actually supports more |
| 361 | // than 64k source files, so we ignore it and |
| 362 | // compute the value from other stream fields. |
| 363 | }; |
| 364 | |
| 365 | // The layout of the FileInfoSubstream is like this: |
| 366 | // struct { |
| 367 | // ulittle16_t NumModules; |
| 368 | // ulittle16_t NumSourceFiles; |
| 369 | // ulittle16_t ModIndices[NumModules]; |
| 370 | // ulittle16_t ModFileCounts[NumModules]; |
| 371 | // ulittle32_t FileNameOffsets[NumSourceFiles]; |
| 372 | // char Names[][NumSourceFiles]; |
| 373 | // }; |
| 374 | // with the caveat that `NumSourceFiles` cannot be trusted, so |
| 375 | // it is computed by summing `ModFileCounts`. |
| 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 | } |