blob: 80441ccd8255d76ea7c53ba0119188a995cdb7f4 [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 Turnerd8447992016-06-07 05:28:55 +000015#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000016#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000017#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000018#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000019#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000020#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000021#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000022#include "llvm/DebugInfo/PDB/Raw/RawTypes.h"
Rui Ueyama90db7882016-06-02 18:20:20 +000023#include "llvm/Object/COFF.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000024
25using namespace llvm;
Zachary Turner93839cb2016-06-02 05:07:49 +000026using namespace llvm::codeview;
Zachary Turner2f09b502016-04-29 17:28:47 +000027using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000028using namespace llvm::support;
29
30namespace {
31// Some of the values are stored in bitfields. Since this needs to be portable
32// across compilers and architectures (big / little endian in particular) we
33// can't use the actual structures below, but must instead do the shifting
34// and masking ourselves. The struct definitions are provided for reference.
35
36// struct DbiFlags {
37// uint16_t IncrementalLinking : 1; // True if linked incrementally
38// uint16_t IsStripped : 1; // True if private symbols were stripped.
39// uint16_t HasCTypes : 1; // True if linked with /debug:ctypes.
40// uint16_t Reserved : 13;
41//};
42const uint16_t FlagIncrementalMask = 0x0001;
43const uint16_t FlagStrippedMask = 0x0002;
44const uint16_t FlagHasCTypesMask = 0x0004;
45
46// struct DbiBuildNo {
47// uint16_t MinorVersion : 8;
48// uint16_t MajorVersion : 7;
49// uint16_t NewVersionFormat : 1;
50//};
51const uint16_t BuildMinorMask = 0x00FF;
52const uint16_t BuildMinorShift = 0;
53
54const uint16_t BuildMajorMask = 0x7F00;
55const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000056}
57
Zachary Turner2f09b502016-04-29 17:28:47 +000058struct DbiStream::HeaderInfo {
Zachary Turnerff788aa2016-04-26 19:24:10 +000059 little32_t VersionSignature;
Zachary Turner53a65ba2016-04-26 18:42:34 +000060 ulittle32_t VersionHeader;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000061 ulittle32_t Age; // Should match InfoStream.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000062 ulittle16_t GlobalSymbolStreamIndex; // Global symbol stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000063 ulittle16_t BuildNumber; // See DbiBuildNo structure.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000064 ulittle16_t PublicSymbolStreamIndex; // Public symbols stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000065 ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
Rui Ueyama0376b1a2016-05-19 18:05:58 +000066 ulittle16_t SymRecordStreamIndex; // Symbol records stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000067 ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll
68 little32_t ModiSubstreamSize; // Size of module info stream
69 little32_t SecContrSubstreamSize; // Size of sec. contribution stream
70 little32_t SectionMapSize; // Size of sec. map substream
71 little32_t FileInfoSize; // Size of file info substream
Zachary Turner2f09b502016-04-29 17:28:47 +000072 little32_t TypeServerSize; // Size of type server map
73 ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
74 little32_t OptionalDbgHdrSize; // Size of DbgHeader info
75 little32_t ECSubstreamSize; // Size of EC stream (what is EC?)
76 ulittle16_t Flags; // See DbiFlags enum.
77 ulittle16_t MachineType; // See PDB_MachineType enum.
Zachary Turner53a65ba2016-04-26 18:42:34 +000078
79 ulittle32_t Reserved; // Pad to 64 bytes
80};
81
Zachary Turner93839cb2016-06-02 05:07:49 +000082template <typename ContribType>
83Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
84 StreamReader &Reader) {
85 if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
86 return make_error<RawError>(
87 raw_error_code::corrupt_file,
88 "Invalid number of bytes of section contributions");
89
90 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
91 if (auto EC = Reader.readArray(Output, Count))
92 return EC;
93 return Error::success();
94}
95
Zachary Turner8dbe3622016-05-27 01:54:44 +000096DbiStream::DbiStream(PDBFile &File)
Zachary Turnerd8447992016-06-07 05:28:55 +000097 : Pdb(File),
98 Stream(llvm::make_unique<IndexedStreamData>(StreamDBI, File), File),
99 Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000100 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
101}
102
Zachary Turner2f09b502016-04-29 17:28:47 +0000103DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000104
Zachary Turner819e77d2016-05-06 20:51:57 +0000105Error DbiStream::reload() {
Zachary Turner93839cb2016-06-02 05:07:49 +0000106 StreamReader Reader(Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000107
Zachary Turner53a65ba2016-04-26 18:42:34 +0000108 if (Stream.getLength() < sizeof(HeaderInfo))
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 Turner8dbe3622016-05-27 01:54:44 +0000111 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +0000112 return make_error<RawError>(raw_error_code::corrupt_file,
113 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000114
115 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +0000116 return make_error<RawError>(raw_error_code::corrupt_file,
117 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000118
Zachary Turner1822af542016-04-27 23:41:42 +0000119 // Require at least version 7, which should be present in all PDBs
120 // produced in the last decade and allows us to avoid having to
121 // special case all kinds of complicated arcane formats.
122 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner93839cb2016-06-02 05:07:49 +0000123 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +0000124 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000125
Zachary Turner819e77d2016-05-06 20:51:57 +0000126 auto InfoStream = Pdb.getPDBInfoStream();
127 if (auto EC = InfoStream.takeError())
128 return EC;
129
130 if (Header->Age != InfoStream.get().getAge())
131 return make_error<RawError>(raw_error_code::corrupt_file,
132 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000133
134 if (Stream.getLength() !=
135 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
136 Header->SecContrSubstreamSize + Header->SectionMapSize +
137 Header->FileInfoSize + Header->TypeServerSize +
138 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +0000139 return make_error<RawError>(raw_error_code::corrupt_file,
140 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000141
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000142 // Only certain substreams are guaranteed to be aligned. Validate
143 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000144 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000145 return make_error<RawError>(raw_error_code::corrupt_file,
146 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000147 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000148 return make_error<RawError>(
149 raw_error_code::corrupt_file,
150 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000151 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000152 return make_error<RawError>(raw_error_code::corrupt_file,
153 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000154 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000155 return make_error<RawError>(raw_error_code::corrupt_file,
156 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000157 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000158 return make_error<RawError>(raw_error_code::corrupt_file,
159 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000160
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000161 // Since each ModInfo in the stream is a variable length, we have to iterate
162 // them to know how many there actually are.
Zachary Turner93839cb2016-06-02 05:07:49 +0000163 VarStreamArray<ModInfo> ModInfoArray;
Zachary Turner1de49c92016-05-27 18:47:20 +0000164 if (auto EC = Reader.readArray(ModInfoArray, Header->ModiSubstreamSize))
165 return EC;
166 for (auto &Info : ModInfoArray) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000167 ModuleInfos.emplace_back(Info);
168 }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000169
Zachary Turner8dbe3622016-05-27 01:54:44 +0000170 if (auto EC = Reader.readStreamRef(SecContrSubstream,
171 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000172 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000173 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000174 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000175 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000176 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000177 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000178 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000179 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000180 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000181 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000182 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
183 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000184 return EC;
185
Zachary Turner93839cb2016-06-02 05:07:49 +0000186 if (auto EC = initializeSectionContributionData())
187 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000188 if (auto EC = initializeSectionHeadersData())
189 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000190 if (auto EC = initializeSectionMapData())
191 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000192 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000193 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000194 if (auto EC = initializeFpoRecords())
195 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000196
Zachary Turner6ba65de2016-04-29 17:22:58 +0000197 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000198 return make_error<RawError>(raw_error_code::corrupt_file,
199 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000200
Zachary Turner93839cb2016-06-02 05:07:49 +0000201 StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000202 if (auto EC = ECNames.load(ECReader))
203 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000204
Zachary Turner819e77d2016-05-06 20:51:57 +0000205 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000206}
207
Zachary Turner2f09b502016-04-29 17:28:47 +0000208PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000209 uint32_t Value = Header->VersionHeader;
210 return static_cast<PdbRaw_DbiVer>(Value);
211}
212
Zachary Turner2f09b502016-04-29 17:28:47 +0000213uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000214
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000215uint16_t DbiStream::getPublicSymbolStreamIndex() const {
216 return Header->PublicSymbolStreamIndex;
217}
218
Zachary Turner96e60f72016-05-24 20:31:48 +0000219uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
220 return Header->GlobalSymbolStreamIndex;
221}
222
Zachary Turner2f09b502016-04-29 17:28:47 +0000223bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000224 return (Header->Flags & FlagIncrementalMask) != 0;
225}
226
Zachary Turner2f09b502016-04-29 17:28:47 +0000227bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000228 return (Header->Flags & FlagHasCTypesMask) != 0;
229}
230
Zachary Turner2f09b502016-04-29 17:28:47 +0000231bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000232 return (Header->Flags & FlagStrippedMask) != 0;
233}
234
Zachary Turner2f09b502016-04-29 17:28:47 +0000235uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000236 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
237}
238
Zachary Turner2f09b502016-04-29 17:28:47 +0000239uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000240 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
241}
242
Zachary Turner2f09b502016-04-29 17:28:47 +0000243uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000244
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000245uint32_t DbiStream::getSymRecordStreamIndex() const {
246 return Header->SymRecordStreamIndex;
247}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000248
Zachary Turner2f09b502016-04-29 17:28:47 +0000249PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000250 uint16_t Machine = Header->MachineType;
251 return static_cast<PDB_Machine>(Machine);
252}
Zachary Turner1822af542016-04-27 23:41:42 +0000253
Rui Ueyama90db7882016-06-02 18:20:20 +0000254codeview::FixedStreamArray<object::coff_section>
255DbiStream::getSectionHeaders() {
256 return SectionHeaders;
257}
258
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000259codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
260 return FpoRecords;
261}
262
Zachary Turner2f09b502016-04-29 17:28:47 +0000263ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000264codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
265 return SectionMap;
266}
267
268void llvm::pdb::DbiStream::visitSectionContributions(
269 ISectionContribVisitor &Visitor) const {
270 if (SectionContribVersion == DbiSecContribVer60) {
271 for (auto &SC : SectionContribs)
272 Visitor.visit(SC);
273 } else if (SectionContribVersion == DbiSecContribV2) {
274 for (auto &SC : SectionContribs2)
275 Visitor.visit(SC);
276 }
277}
278
279Error DbiStream::initializeSectionContributionData() {
280 StreamReader SCReader(SecContrSubstream);
281 if (auto EC = SCReader.readEnum(SectionContribVersion))
282 return EC;
283
284 if (SectionContribVersion == DbiSecContribVer60)
285 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
286 if (SectionContribVersion == DbiSecContribV2)
287 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
288
289 return make_error<RawError>(raw_error_code::feature_unsupported,
290 "Unsupported DBI Section Contribution version");
291}
292
Rui Ueyama90db7882016-06-02 18:20:20 +0000293// Initializes this->SectionHeaders.
294Error DbiStream::initializeSectionHeadersData() {
295 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd8447992016-06-07 05:28:55 +0000296 SectionHeaderStream.reset(new MappedBlockStream(
297 llvm::make_unique<IndexedStreamData>(StreamNum, Pdb), Pdb));
Rui Ueyama90db7882016-06-02 18:20:20 +0000298
299 size_t StreamLen = SectionHeaderStream->getLength();
300 if (StreamLen % sizeof(object::coff_section))
301 return make_error<RawError>(raw_error_code::corrupt_file,
302 "Corrupted section header stream.");
303
304 size_t NumSections = StreamLen / sizeof(object::coff_section);
305 codeview::StreamReader Reader(*SectionHeaderStream);
306 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
307 return make_error<RawError>(raw_error_code::corrupt_file,
308 "Could not read a bitmap.");
309 return Error::success();
310}
311
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000312// Initializes this->Fpos.
313Error DbiStream::initializeFpoRecords() {
314 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Zachary Turnerd8447992016-06-07 05:28:55 +0000315 FpoStream.reset(new MappedBlockStream(
316 llvm::make_unique<IndexedStreamData>(StreamNum, Pdb), Pdb));
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000317
318 size_t StreamLen = FpoStream->getLength();
319 if (StreamLen % sizeof(object::FpoData))
320 return make_error<RawError>(raw_error_code::corrupt_file,
321 "Corrupted New FPO stream.");
322
323 size_t NumRecords = StreamLen / sizeof(object::FpoData);
324 codeview::StreamReader Reader(*FpoStream);
325 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
326 return make_error<RawError>(raw_error_code::corrupt_file,
327 "Corrupted New FPO stream.");
328 return Error::success();
329}
330
Zachary Turner93839cb2016-06-02 05:07:49 +0000331Error DbiStream::initializeSectionMapData() {
332 StreamReader SMReader(SecMapSubstream);
333 const SecMapHeader *Header;
334 if (auto EC = SMReader.readObject(Header))
335 return EC;
336 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
337 return EC;
338 return Error::success();
339}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000340
Zachary Turner819e77d2016-05-06 20:51:57 +0000341Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000342 struct FileInfoSubstreamHeader {
343 ulittle16_t NumModules; // Total # of modules, should match number of
344 // records in the ModuleInfo substream.
345 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
346 // accurate because PDB actually supports more
347 // than 64k source files, so we ignore it and
348 // compute the value from other stream fields.
349 };
350
351 // The layout of the FileInfoSubstream is like this:
352 // struct {
353 // ulittle16_t NumModules;
354 // ulittle16_t NumSourceFiles;
355 // ulittle16_t ModIndices[NumModules];
356 // ulittle16_t ModFileCounts[NumModules];
357 // ulittle32_t FileNameOffsets[NumSourceFiles];
358 // char Names[][NumSourceFiles];
359 // };
360 // with the caveat that `NumSourceFiles` cannot be trusted, so
361 // it is computed by summing `ModFileCounts`.
362 //
Zachary Turner8dbe3622016-05-27 01:54:44 +0000363 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000364 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000365 if (auto EC = FISR.readObject(FH))
366 return EC;
367
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000368 // The number of modules in the stream should be the same as reported by
369 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000370 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000371 return make_error<RawError>(raw_error_code::corrupt_file,
372 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000373
Zachary Turner93839cb2016-06-02 05:07:49 +0000374 FixedStreamArray<ulittle16_t> ModIndexArray;
375 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000376
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000377 // First is an array of `NumModules` module indices. This is not used for the
378 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
379 // but it's possible there are more than 64k source files, which would imply
380 // more than 64k modules (e.g. object files) as well. So we ignore this
381 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000382 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
383 return EC;
384 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
385 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000386
387 // Compute the real number of source files.
388 uint32_t NumSourceFiles = 0;
389 for (auto Count : ModFileCountArray)
390 NumSourceFiles += Count;
391
392 // This is the array that in the reference implementation corresponds to
393 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
394 // pointer. Due to the mentioned problems of pointers causing difficulty
395 // when reading from the file on 64-bit systems, we continue to ignore that
396 // field in `ModInfo`, and instead build a vector of StringRefs and stores
397 // them in `ModuleInfoEx`. The value written to and read from the file is
398 // not used anyway, it is only there as a way to store the offsets for the
399 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000400 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
401 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000402
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000403 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000404 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000405
406 // We go through each ModuleInfo, determine the number N of source files for
407 // that module, and then get the next N offsets from the Offsets array, using
408 // them to get the corresponding N names from the Names buffer and associating
409 // each one with the corresponding module.
410 uint32_t NextFileIndex = 0;
411 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
412 uint32_t NumFiles = ModFileCountArray[I];
413 ModuleInfos[I].SourceFiles.resize(NumFiles);
414 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000415 if (auto Name = getFileNameForIndex(NextFileIndex))
416 ModuleInfos[I].SourceFiles[J] = Name.get();
417 else
418 return Name.takeError();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000419 }
420 }
421
Zachary Turner819e77d2016-05-06 20:51:57 +0000422 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000423}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000424
425uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000426 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000427}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000428
429Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
430 StreamReader Names(NamesBuffer);
431 if (Index >= FileNameOffsets.size())
432 return make_error<RawError>(raw_error_code::index_out_of_bounds);
433
434 uint32_t FileOffset = FileNameOffsets[Index];
435 Names.setOffset(FileOffset);
436 StringRef Name;
437 if (auto EC = Names.readZeroString(Name))
438 return std::move(EC);
439 return Name;
440}