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