blob: b1c571bf78c96d83076562aee34edfbf32f5d940 [file] [log] [blame]
Zachary Turner2f09b502016-04-29 17:28:47 +00001//===- DbiStream.cpp - PDB Dbi Stream (Stream 3) Access -------------------===//
Zachary Turner53a65ba2016-04-26 18:42:34 +00002//
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 Ueyama90db7882016-06-02 18:20:20 +00009
Zachary Turner2f09b502016-04-29 17:28:47 +000010#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000011
Zachary Turner8dbe3622016-05-27 01:54:44 +000012#include "llvm/DebugInfo/CodeView/StreamArray.h"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000013#include "llvm/DebugInfo/CodeView/StreamReader.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000014#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000015#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000016#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000017#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000018#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000019#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000020#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000021#include "llvm/DebugInfo/PDB/Raw/RawTypes.h"
Rui Ueyama90db7882016-06-02 18:20:20 +000022#include "llvm/Object/COFF.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000023
24using namespace llvm;
Zachary Turner93839cb2016-06-02 05:07:49 +000025using namespace llvm::codeview;
Zachary Turner2f09b502016-04-29 17:28:47 +000026using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000027using namespace llvm::support;
28
29namespace {
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//};
41const uint16_t FlagIncrementalMask = 0x0001;
42const uint16_t FlagStrippedMask = 0x0002;
43const uint16_t FlagHasCTypesMask = 0x0004;
44
45// struct DbiBuildNo {
46// uint16_t MinorVersion : 8;
47// uint16_t MajorVersion : 7;
48// uint16_t NewVersionFormat : 1;
49//};
50const uint16_t BuildMinorMask = 0x00FF;
51const uint16_t BuildMinorShift = 0;
52
53const uint16_t BuildMajorMask = 0x7F00;
54const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000055}
56
Zachary Turner2f09b502016-04-29 17:28:47 +000057struct DbiStream::HeaderInfo {
Zachary Turnerff788aa2016-04-26 19:24:10 +000058 little32_t VersionSignature;
Zachary Turner53a65ba2016-04-26 18:42:34 +000059 ulittle32_t VersionHeader;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000060 ulittle32_t Age; // Should match InfoStream.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000061 ulittle16_t GlobalSymbolStreamIndex; // Global symbol stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000062 ulittle16_t BuildNumber; // See DbiBuildNo structure.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000063 ulittle16_t PublicSymbolStreamIndex; // Public symbols stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000064 ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
Rui Ueyama0376b1a2016-05-19 18:05:58 +000065 ulittle16_t SymRecordStreamIndex; // Symbol records stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000066 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 Turner2f09b502016-04-29 17:28:47 +000071 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 Turner53a65ba2016-04-26 18:42:34 +000077
78 ulittle32_t Reserved; // Pad to 64 bytes
79};
80
Zachary Turner93839cb2016-06-02 05:07:49 +000081template <typename ContribType>
82Error 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 Turner8dbe3622016-05-27 01:54:44 +000095DbiStream::DbiStream(PDBFile &File)
96 : Pdb(File), Stream(StreamDBI, File), Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000097 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
98}
99
Zachary Turner2f09b502016-04-29 17:28:47 +0000100DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000101
Zachary Turner819e77d2016-05-06 20:51:57 +0000102Error DbiStream::reload() {
Zachary Turner93839cb2016-06-02 05:07:49 +0000103 StreamReader Reader(Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000104
Zachary Turner53a65ba2016-04-26 18:42:34 +0000105 if (Stream.getLength() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000106 return make_error<RawError>(raw_error_code::corrupt_file,
107 "DBI Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +0000108 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +0000109 return make_error<RawError>(raw_error_code::corrupt_file,
110 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000111
112 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +0000113 return make_error<RawError>(raw_error_code::corrupt_file,
114 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000115
Zachary Turner1822af542016-04-27 23:41:42 +0000116 // 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 Turner93839cb2016-06-02 05:07:49 +0000120 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +0000121 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000122
Zachary Turner819e77d2016-05-06 20:51:57 +0000123 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 Turner53a65ba2016-04-26 18:42:34 +0000130
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 Turner819e77d2016-05-06 20:51:57 +0000136 return make_error<RawError>(raw_error_code::corrupt_file,
137 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000138
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000139 // Only certain substreams are guaranteed to be aligned. Validate
140 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000141 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000142 return make_error<RawError>(raw_error_code::corrupt_file,
143 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000144 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000145 return make_error<RawError>(
146 raw_error_code::corrupt_file,
147 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000148 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000149 return make_error<RawError>(raw_error_code::corrupt_file,
150 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000151 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000152 return make_error<RawError>(raw_error_code::corrupt_file,
153 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000154 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000155 return make_error<RawError>(raw_error_code::corrupt_file,
156 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000157
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000158 // Since each ModInfo in the stream is a variable length, we have to iterate
159 // them to know how many there actually are.
Zachary Turner93839cb2016-06-02 05:07:49 +0000160 VarStreamArray<ModInfo> ModInfoArray;
Zachary Turner1de49c92016-05-27 18:47:20 +0000161 if (auto EC = Reader.readArray(ModInfoArray, Header->ModiSubstreamSize))
162 return EC;
163 for (auto &Info : ModInfoArray) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000164 ModuleInfos.emplace_back(Info);
165 }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000166
Zachary Turner8dbe3622016-05-27 01:54:44 +0000167 if (auto EC = Reader.readStreamRef(SecContrSubstream,
168 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000169 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000170 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000171 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000172 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000173 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000174 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000175 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000176 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000177 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000178 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000179 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
180 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000181 return EC;
182
Zachary Turner93839cb2016-06-02 05:07:49 +0000183 if (auto EC = initializeSectionContributionData())
184 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000185 if (auto EC = initializeSectionHeadersData())
186 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000187 if (auto EC = initializeSectionMapData())
188 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000189 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000190 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000191 if (auto EC = initializeFpoRecords())
192 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000193
Zachary Turner6ba65de2016-04-29 17:22:58 +0000194 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000195 return make_error<RawError>(raw_error_code::corrupt_file,
196 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000197
Zachary Turner93839cb2016-06-02 05:07:49 +0000198 StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000199 if (auto EC = ECNames.load(ECReader))
200 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000201
Zachary Turner819e77d2016-05-06 20:51:57 +0000202 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000203}
204
Zachary Turner2f09b502016-04-29 17:28:47 +0000205PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000206 uint32_t Value = Header->VersionHeader;
207 return static_cast<PdbRaw_DbiVer>(Value);
208}
209
Zachary Turner2f09b502016-04-29 17:28:47 +0000210uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000211
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000212uint16_t DbiStream::getPublicSymbolStreamIndex() const {
213 return Header->PublicSymbolStreamIndex;
214}
215
Zachary Turner96e60f72016-05-24 20:31:48 +0000216uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
217 return Header->GlobalSymbolStreamIndex;
218}
219
Zachary Turner2f09b502016-04-29 17:28:47 +0000220bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000221 return (Header->Flags & FlagIncrementalMask) != 0;
222}
223
Zachary Turner2f09b502016-04-29 17:28:47 +0000224bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000225 return (Header->Flags & FlagHasCTypesMask) != 0;
226}
227
Zachary Turner2f09b502016-04-29 17:28:47 +0000228bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000229 return (Header->Flags & FlagStrippedMask) != 0;
230}
231
Zachary Turner2f09b502016-04-29 17:28:47 +0000232uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000233 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
234}
235
Zachary Turner2f09b502016-04-29 17:28:47 +0000236uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000237 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
238}
239
Zachary Turner2f09b502016-04-29 17:28:47 +0000240uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000241
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000242uint32_t DbiStream::getSymRecordStreamIndex() const {
243 return Header->SymRecordStreamIndex;
244}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000245
Zachary Turner2f09b502016-04-29 17:28:47 +0000246PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000247 uint16_t Machine = Header->MachineType;
248 return static_cast<PDB_Machine>(Machine);
249}
Zachary Turner1822af542016-04-27 23:41:42 +0000250
Rui Ueyama90db7882016-06-02 18:20:20 +0000251codeview::FixedStreamArray<object::coff_section>
252DbiStream::getSectionHeaders() {
253 return SectionHeaders;
254}
255
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000256codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
257 return FpoRecords;
258}
259
Zachary Turner2f09b502016-04-29 17:28:47 +0000260ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000261codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
262 return SectionMap;
263}
264
265void 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
276Error 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 Ueyama90db7882016-06-02 18:20:20 +0000290// Initializes this->SectionHeaders.
291Error 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 Ueyamaef2b4882016-06-06 18:39:21 +0000308// Initializes this->Fpos.
309Error 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 Turner93839cb2016-06-02 05:07:49 +0000326Error 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 Turner84c3a8b2016-04-28 20:05:18 +0000335
Zachary Turner819e77d2016-05-06 20:51:57 +0000336Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000337 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 Turner8dbe3622016-05-27 01:54:44 +0000358 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000359 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000360 if (auto EC = FISR.readObject(FH))
361 return EC;
362
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000363 // The number of modules in the stream should be the same as reported by
364 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000365 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000366 return make_error<RawError>(raw_error_code::corrupt_file,
367 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000368
Zachary Turner93839cb2016-06-02 05:07:49 +0000369 FixedStreamArray<ulittle16_t> ModIndexArray;
370 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000371
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000372 // 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 Turner8dbe3622016-05-27 01:54:44 +0000377 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
378 return EC;
379 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
380 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000381
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 Turner8dbe3622016-05-27 01:54:44 +0000395 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
396 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000397
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000398 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000399 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000400
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 Turner3df1bfa2016-06-03 05:52:57 +0000410 if (auto Name = getFileNameForIndex(NextFileIndex))
411 ModuleInfos[I].SourceFiles[J] = Name.get();
412 else
413 return Name.takeError();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000414 }
415 }
416
Zachary Turner819e77d2016-05-06 20:51:57 +0000417 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000418}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000419
420uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000421 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000422}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000423
424Expected<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}