blob: e79d6572be1c19631467bcf0d233e8fb017e9829 [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;
189
Zachary Turner819e77d2016-05-06 20:51:57 +0000190 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000191 return EC;
192
Zachary Turner6ba65de2016-04-29 17:22:58 +0000193 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000194 return make_error<RawError>(raw_error_code::corrupt_file,
195 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000196
Zachary Turner93839cb2016-06-02 05:07:49 +0000197 StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000198 if (auto EC = ECNames.load(ECReader))
199 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000200
Zachary Turner819e77d2016-05-06 20:51:57 +0000201 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000202}
203
Zachary Turner2f09b502016-04-29 17:28:47 +0000204PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000205 uint32_t Value = Header->VersionHeader;
206 return static_cast<PdbRaw_DbiVer>(Value);
207}
208
Zachary Turner2f09b502016-04-29 17:28:47 +0000209uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000210
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000211uint16_t DbiStream::getPublicSymbolStreamIndex() const {
212 return Header->PublicSymbolStreamIndex;
213}
214
Zachary Turner96e60f72016-05-24 20:31:48 +0000215uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
216 return Header->GlobalSymbolStreamIndex;
217}
218
Zachary Turner2f09b502016-04-29 17:28:47 +0000219bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000220 return (Header->Flags & FlagIncrementalMask) != 0;
221}
222
Zachary Turner2f09b502016-04-29 17:28:47 +0000223bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000224 return (Header->Flags & FlagHasCTypesMask) != 0;
225}
226
Zachary Turner2f09b502016-04-29 17:28:47 +0000227bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000228 return (Header->Flags & FlagStrippedMask) != 0;
229}
230
Zachary Turner2f09b502016-04-29 17:28:47 +0000231uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000232 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
233}
234
Zachary Turner2f09b502016-04-29 17:28:47 +0000235uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000236 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
237}
238
Zachary Turner2f09b502016-04-29 17:28:47 +0000239uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000240
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000241uint32_t DbiStream::getSymRecordStreamIndex() const {
242 return Header->SymRecordStreamIndex;
243}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000244
Zachary Turner2f09b502016-04-29 17:28:47 +0000245PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000246 uint16_t Machine = Header->MachineType;
247 return static_cast<PDB_Machine>(Machine);
248}
Zachary Turner1822af542016-04-27 23:41:42 +0000249
Rui Ueyama90db7882016-06-02 18:20:20 +0000250codeview::FixedStreamArray<object::coff_section>
251DbiStream::getSectionHeaders() {
252 return SectionHeaders;
253}
254
Zachary Turner2f09b502016-04-29 17:28:47 +0000255ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000256codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
257 return SectionMap;
258}
259
260void llvm::pdb::DbiStream::visitSectionContributions(
261 ISectionContribVisitor &Visitor) const {
262 if (SectionContribVersion == DbiSecContribVer60) {
263 for (auto &SC : SectionContribs)
264 Visitor.visit(SC);
265 } else if (SectionContribVersion == DbiSecContribV2) {
266 for (auto &SC : SectionContribs2)
267 Visitor.visit(SC);
268 }
269}
270
271Error DbiStream::initializeSectionContributionData() {
272 StreamReader SCReader(SecContrSubstream);
273 if (auto EC = SCReader.readEnum(SectionContribVersion))
274 return EC;
275
276 if (SectionContribVersion == DbiSecContribVer60)
277 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
278 if (SectionContribVersion == DbiSecContribV2)
279 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
280
281 return make_error<RawError>(raw_error_code::feature_unsupported,
282 "Unsupported DBI Section Contribution version");
283}
284
Rui Ueyama90db7882016-06-02 18:20:20 +0000285// Initializes this->SectionHeaders.
286Error DbiStream::initializeSectionHeadersData() {
287 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
288 SectionHeaderStream.reset(new MappedBlockStream(StreamNum, Pdb));
289
290 size_t StreamLen = SectionHeaderStream->getLength();
291 if (StreamLen % sizeof(object::coff_section))
292 return make_error<RawError>(raw_error_code::corrupt_file,
293 "Corrupted section header stream.");
294
295 size_t NumSections = StreamLen / sizeof(object::coff_section);
296 codeview::StreamReader Reader(*SectionHeaderStream);
297 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
298 return make_error<RawError>(raw_error_code::corrupt_file,
299 "Could not read a bitmap.");
300 return Error::success();
301}
302
Zachary Turner93839cb2016-06-02 05:07:49 +0000303Error DbiStream::initializeSectionMapData() {
304 StreamReader SMReader(SecMapSubstream);
305 const SecMapHeader *Header;
306 if (auto EC = SMReader.readObject(Header))
307 return EC;
308 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
309 return EC;
310 return Error::success();
311}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000312
Zachary Turner819e77d2016-05-06 20:51:57 +0000313Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000314 struct FileInfoSubstreamHeader {
315 ulittle16_t NumModules; // Total # of modules, should match number of
316 // records in the ModuleInfo substream.
317 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
318 // accurate because PDB actually supports more
319 // than 64k source files, so we ignore it and
320 // compute the value from other stream fields.
321 };
322
323 // The layout of the FileInfoSubstream is like this:
324 // struct {
325 // ulittle16_t NumModules;
326 // ulittle16_t NumSourceFiles;
327 // ulittle16_t ModIndices[NumModules];
328 // ulittle16_t ModFileCounts[NumModules];
329 // ulittle32_t FileNameOffsets[NumSourceFiles];
330 // char Names[][NumSourceFiles];
331 // };
332 // with the caveat that `NumSourceFiles` cannot be trusted, so
333 // it is computed by summing `ModFileCounts`.
334 //
Zachary Turner8dbe3622016-05-27 01:54:44 +0000335 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000336 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000337 if (auto EC = FISR.readObject(FH))
338 return EC;
339
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000340 // The number of modules in the stream should be the same as reported by
341 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000342 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000343 return make_error<RawError>(raw_error_code::corrupt_file,
344 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000345
Zachary Turner93839cb2016-06-02 05:07:49 +0000346 FixedStreamArray<ulittle16_t> ModIndexArray;
347 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000348
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000349 // First is an array of `NumModules` module indices. This is not used for the
350 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
351 // but it's possible there are more than 64k source files, which would imply
352 // more than 64k modules (e.g. object files) as well. So we ignore this
353 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000354 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
355 return EC;
356 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
357 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000358
359 // Compute the real number of source files.
360 uint32_t NumSourceFiles = 0;
361 for (auto Count : ModFileCountArray)
362 NumSourceFiles += Count;
363
364 // This is the array that in the reference implementation corresponds to
365 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
366 // pointer. Due to the mentioned problems of pointers causing difficulty
367 // when reading from the file on 64-bit systems, we continue to ignore that
368 // field in `ModInfo`, and instead build a vector of StringRefs and stores
369 // them in `ModuleInfoEx`. The value written to and read from the file is
370 // not used anyway, it is only there as a way to store the offsets for the
371 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000372 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
373 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000374
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000375 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000376 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000377
378 // We go through each ModuleInfo, determine the number N of source files for
379 // that module, and then get the next N offsets from the Offsets array, using
380 // them to get the corresponding N names from the Names buffer and associating
381 // each one with the corresponding module.
382 uint32_t NextFileIndex = 0;
383 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
384 uint32_t NumFiles = ModFileCountArray[I];
385 ModuleInfos[I].SourceFiles.resize(NumFiles);
386 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000387 if (auto Name = getFileNameForIndex(NextFileIndex))
388 ModuleInfos[I].SourceFiles[J] = Name.get();
389 else
390 return Name.takeError();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000391 }
392 }
393
Zachary Turner819e77d2016-05-06 20:51:57 +0000394 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000395}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000396
397uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000398 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000399}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000400
401Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
402 StreamReader Names(NamesBuffer);
403 if (Index >= FileNameOffsets.size())
404 return make_error<RawError>(raw_error_code::index_out_of_bounds);
405
406 uint32_t FileOffset = FileNameOffsets[Index];
407 Names.setOffset(FileOffset);
408 StringRef Name;
409 if (auto EC = Names.readZeroString(Name))
410 return std::move(EC);
411 return Name;
412}