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