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