blob: 90bc2a2e5564a7e13b59efc23e532cf7d577af97 [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 Turnerd2b2bfe2016-06-08 00:25:08 +0000296 if (StreamNum >= Pdb.getNumStreams())
297 return make_error<RawError>(raw_error_code::no_stream);
298
Zachary Turnerd8447992016-06-07 05:28:55 +0000299 SectionHeaderStream.reset(new MappedBlockStream(
300 llvm::make_unique<IndexedStreamData>(StreamNum, Pdb), Pdb));
Rui Ueyama90db7882016-06-02 18:20:20 +0000301
302 size_t StreamLen = SectionHeaderStream->getLength();
303 if (StreamLen % sizeof(object::coff_section))
304 return make_error<RawError>(raw_error_code::corrupt_file,
305 "Corrupted section header stream.");
306
307 size_t NumSections = StreamLen / sizeof(object::coff_section);
308 codeview::StreamReader Reader(*SectionHeaderStream);
309 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
310 return make_error<RawError>(raw_error_code::corrupt_file,
311 "Could not read a bitmap.");
312 return Error::success();
313}
314
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000315// Initializes this->Fpos.
316Error DbiStream::initializeFpoRecords() {
317 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000318 if (StreamNum >= Pdb.getNumStreams())
319 return make_error<RawError>(raw_error_code::no_stream);
320
Zachary Turnerd8447992016-06-07 05:28:55 +0000321 FpoStream.reset(new MappedBlockStream(
322 llvm::make_unique<IndexedStreamData>(StreamNum, Pdb), Pdb));
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000323
324 size_t StreamLen = FpoStream->getLength();
325 if (StreamLen % sizeof(object::FpoData))
326 return make_error<RawError>(raw_error_code::corrupt_file,
327 "Corrupted New FPO stream.");
328
329 size_t NumRecords = StreamLen / sizeof(object::FpoData);
330 codeview::StreamReader Reader(*FpoStream);
331 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
332 return make_error<RawError>(raw_error_code::corrupt_file,
333 "Corrupted New FPO stream.");
334 return Error::success();
335}
336
Zachary Turner93839cb2016-06-02 05:07:49 +0000337Error DbiStream::initializeSectionMapData() {
338 StreamReader SMReader(SecMapSubstream);
339 const SecMapHeader *Header;
340 if (auto EC = SMReader.readObject(Header))
341 return EC;
342 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
343 return EC;
344 return Error::success();
345}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000346
Zachary Turner819e77d2016-05-06 20:51:57 +0000347Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000348 struct FileInfoSubstreamHeader {
349 ulittle16_t NumModules; // Total # of modules, should match number of
350 // records in the ModuleInfo substream.
351 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
352 // accurate because PDB actually supports more
353 // than 64k source files, so we ignore it and
354 // compute the value from other stream fields.
355 };
356
357 // The layout of the FileInfoSubstream is like this:
358 // struct {
359 // ulittle16_t NumModules;
360 // ulittle16_t NumSourceFiles;
361 // ulittle16_t ModIndices[NumModules];
362 // ulittle16_t ModFileCounts[NumModules];
363 // ulittle32_t FileNameOffsets[NumSourceFiles];
364 // char Names[][NumSourceFiles];
365 // };
366 // with the caveat that `NumSourceFiles` cannot be trusted, so
367 // it is computed by summing `ModFileCounts`.
368 //
Zachary Turner8dbe3622016-05-27 01:54:44 +0000369 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000370 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000371 if (auto EC = FISR.readObject(FH))
372 return EC;
373
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000374 // The number of modules in the stream should be the same as reported by
375 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000376 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000377 return make_error<RawError>(raw_error_code::corrupt_file,
378 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000379
Zachary Turner93839cb2016-06-02 05:07:49 +0000380 FixedStreamArray<ulittle16_t> ModIndexArray;
381 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000382
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000383 // First is an array of `NumModules` module indices. This is not used for the
384 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
385 // but it's possible there are more than 64k source files, which would imply
386 // more than 64k modules (e.g. object files) as well. So we ignore this
387 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000388 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
389 return EC;
390 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
391 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000392
393 // Compute the real number of source files.
394 uint32_t NumSourceFiles = 0;
395 for (auto Count : ModFileCountArray)
396 NumSourceFiles += Count;
397
398 // This is the array that in the reference implementation corresponds to
399 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
400 // pointer. Due to the mentioned problems of pointers causing difficulty
401 // when reading from the file on 64-bit systems, we continue to ignore that
402 // field in `ModInfo`, and instead build a vector of StringRefs and stores
403 // them in `ModuleInfoEx`. The value written to and read from the file is
404 // not used anyway, it is only there as a way to store the offsets for the
405 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000406 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
407 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000408
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000409 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000410 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000411
412 // We go through each ModuleInfo, determine the number N of source files for
413 // that module, and then get the next N offsets from the Offsets array, using
414 // them to get the corresponding N names from the Names buffer and associating
415 // each one with the corresponding module.
416 uint32_t NextFileIndex = 0;
417 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
418 uint32_t NumFiles = ModFileCountArray[I];
419 ModuleInfos[I].SourceFiles.resize(NumFiles);
420 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000421 if (auto Name = getFileNameForIndex(NextFileIndex))
422 ModuleInfos[I].SourceFiles[J] = Name.get();
423 else
424 return Name.takeError();
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000425 }
426 }
427
Zachary Turner819e77d2016-05-06 20:51:57 +0000428 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000429}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000430
431uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000432 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000433}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000434
435Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
436 StreamReader Names(NamesBuffer);
437 if (Index >= FileNameOffsets.size())
438 return make_error<RawError>(raw_error_code::index_out_of_bounds);
439
440 uint32_t FileOffset = FileNameOffsets[Index];
441 Names.setOffset(FileOffset);
442 StringRef Name;
443 if (auto EC = Names.readZeroString(Name))
444 return std::move(EC);
445 return Name;
446}