blob: bfe0251f4ce47f28c39ca969e66bd0be16ca5e2e [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 Turnerdbeaea72016-07-11 21:45:26 +000014#include "llvm/DebugInfo/CodeView/StreamWriter.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000015#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000016#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000017#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000018#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000019#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000020#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000021#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000022#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000023#include "llvm/DebugInfo/PDB/Raw/RawTypes.h"
Rui Ueyama90db7882016-06-02 18:20:20 +000024#include "llvm/Object/COFF.h"
Zachary Turner53a65ba2016-04-26 18:42:34 +000025
26using namespace llvm;
Zachary Turner93839cb2016-06-02 05:07:49 +000027using namespace llvm::codeview;
Zachary Turner2f09b502016-04-29 17:28:47 +000028using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000029using namespace llvm::support;
30
31namespace {
32// Some of the values are stored in bitfields. Since this needs to be portable
33// across compilers and architectures (big / little endian in particular) we
34// can't use the actual structures below, but must instead do the shifting
35// and masking ourselves. The struct definitions are provided for reference.
36
37// struct DbiFlags {
38// uint16_t IncrementalLinking : 1; // True if linked incrementally
39// uint16_t IsStripped : 1; // True if private symbols were stripped.
40// uint16_t HasCTypes : 1; // True if linked with /debug:ctypes.
41// uint16_t Reserved : 13;
42//};
43const uint16_t FlagIncrementalMask = 0x0001;
44const uint16_t FlagStrippedMask = 0x0002;
45const uint16_t FlagHasCTypesMask = 0x0004;
46
47// struct DbiBuildNo {
48// uint16_t MinorVersion : 8;
49// uint16_t MajorVersion : 7;
50// uint16_t NewVersionFormat : 1;
51//};
52const uint16_t BuildMinorMask = 0x00FF;
53const uint16_t BuildMinorShift = 0;
54
55const uint16_t BuildMajorMask = 0x7F00;
56const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000057
Zachary Turnerdbeaea72016-07-11 21:45:26 +000058struct FileInfoSubstreamHeader {
59 ulittle16_t NumModules; // Total # of modules, should match number of
60 // records in the ModuleInfo substream.
61 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
62 // accurate because PDB actually supports more
63 // than 64k source files, so we ignore it and
64 // compute the value from other stream fields.
Zachary Turner53a65ba2016-04-26 18:42:34 +000065};
Zachary Turnerdbeaea72016-07-11 21:45:26 +000066}
Zachary Turner53a65ba2016-04-26 18:42:34 +000067
Zachary Turner93839cb2016-06-02 05:07:49 +000068template <typename ContribType>
Benjamin Kramer4d098922016-07-10 11:28:51 +000069static Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
70 StreamReader &Reader) {
Zachary Turner93839cb2016-06-02 05:07:49 +000071 if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
72 return make_error<RawError>(
73 raw_error_code::corrupt_file,
74 "Invalid number of bytes of section contributions");
75
76 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
77 if (auto EC = Reader.readArray(Output, Count))
78 return EC;
79 return Error::success();
80}
81
Zachary Turnera1657a92016-06-08 17:26:39 +000082DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
83 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000084 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
85}
86
Zachary Turner2f09b502016-04-29 17:28:47 +000087DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000088
Zachary Turner819e77d2016-05-06 20:51:57 +000089Error DbiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +000090 StreamReader Reader(*Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +000091
Zachary Turnera1657a92016-06-08 17:26:39 +000092 if (Stream->getLength() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +000093 return make_error<RawError>(raw_error_code::corrupt_file,
94 "DBI Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000095 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000096 return make_error<RawError>(raw_error_code::corrupt_file,
97 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000098
99 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +0000100 return make_error<RawError>(raw_error_code::corrupt_file,
101 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000102
Zachary Turner1822af542016-04-27 23:41:42 +0000103 // Require at least version 7, which should be present in all PDBs
104 // produced in the last decade and allows us to avoid having to
105 // special case all kinds of complicated arcane formats.
106 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner93839cb2016-06-02 05:07:49 +0000107 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +0000108 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000109
Zachary Turnera1657a92016-06-08 17:26:39 +0000110 auto IS = Pdb.getPDBInfoStream();
111 if (!IS)
112 return IS.takeError();
Zachary Turner819e77d2016-05-06 20:51:57 +0000113
Zachary Turnera1657a92016-06-08 17:26:39 +0000114 if (Header->Age != IS->getAge())
Zachary Turner819e77d2016-05-06 20:51:57 +0000115 return make_error<RawError>(raw_error_code::corrupt_file,
116 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000117
Zachary Turnera1657a92016-06-08 17:26:39 +0000118 if (Stream->getLength() !=
Zachary Turner53a65ba2016-04-26 18:42:34 +0000119 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
120 Header->SecContrSubstreamSize + Header->SectionMapSize +
121 Header->FileInfoSize + Header->TypeServerSize +
122 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +0000123 return make_error<RawError>(raw_error_code::corrupt_file,
124 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000125
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000126 // Only certain substreams are guaranteed to be aligned. Validate
127 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000128 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000129 return make_error<RawError>(raw_error_code::corrupt_file,
130 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000131 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000132 return make_error<RawError>(
133 raw_error_code::corrupt_file,
134 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000135 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000136 return make_error<RawError>(raw_error_code::corrupt_file,
137 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000138 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000139 return make_error<RawError>(raw_error_code::corrupt_file,
140 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000141 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000142 return make_error<RawError>(raw_error_code::corrupt_file,
143 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000144
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000145 // Since each ModInfo in the stream is a variable length, we have to iterate
146 // them to know how many there actually are.
Zachary Turner93839cb2016-06-02 05:07:49 +0000147 VarStreamArray<ModInfo> ModInfoArray;
Zachary Turner1de49c92016-05-27 18:47:20 +0000148 if (auto EC = Reader.readArray(ModInfoArray, Header->ModiSubstreamSize))
149 return EC;
150 for (auto &Info : ModInfoArray) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000151 ModuleInfos.emplace_back(Info);
152 }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000153
Zachary Turner8dbe3622016-05-27 01:54:44 +0000154 if (auto EC = Reader.readStreamRef(SecContrSubstream,
155 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000156 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000157 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000158 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000159 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000160 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000161 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000162 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000163 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000164 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000165 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000166 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
167 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000168 return EC;
169
Zachary Turner93839cb2016-06-02 05:07:49 +0000170 if (auto EC = initializeSectionContributionData())
171 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000172 if (auto EC = initializeSectionHeadersData())
173 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000174 if (auto EC = initializeSectionMapData())
175 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000176 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000177 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000178 if (auto EC = initializeFpoRecords())
179 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000180
Zachary Turner6ba65de2016-04-29 17:22:58 +0000181 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000182 return make_error<RawError>(raw_error_code::corrupt_file,
183 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000184
Zachary Turner93839cb2016-06-02 05:07:49 +0000185 StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000186 if (auto EC = ECNames.load(ECReader))
187 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000188
Zachary Turner819e77d2016-05-06 20:51:57 +0000189 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000190}
191
Zachary Turner2f09b502016-04-29 17:28:47 +0000192PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000193 uint32_t Value = Header->VersionHeader;
194 return static_cast<PdbRaw_DbiVer>(Value);
195}
196
Zachary Turner2f09b502016-04-29 17:28:47 +0000197uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000198
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000199uint16_t DbiStream::getPublicSymbolStreamIndex() const {
200 return Header->PublicSymbolStreamIndex;
201}
202
Zachary Turner96e60f72016-05-24 20:31:48 +0000203uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
204 return Header->GlobalSymbolStreamIndex;
205}
206
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000207uint16_t DbiStream::getFlags() const { return Header->Flags; }
208
Zachary Turner2f09b502016-04-29 17:28:47 +0000209bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000210 return (Header->Flags & FlagIncrementalMask) != 0;
211}
212
Zachary Turner2f09b502016-04-29 17:28:47 +0000213bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000214 return (Header->Flags & FlagHasCTypesMask) != 0;
215}
216
Zachary Turner2f09b502016-04-29 17:28:47 +0000217bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000218 return (Header->Flags & FlagStrippedMask) != 0;
219}
220
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000221uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
222
Zachary Turner2f09b502016-04-29 17:28:47 +0000223uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000224 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
225}
226
Zachary Turner2f09b502016-04-29 17:28:47 +0000227uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000228 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
229}
230
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000231uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
232
Zachary Turner2f09b502016-04-29 17:28:47 +0000233uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000234
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000235uint32_t DbiStream::getSymRecordStreamIndex() const {
236 return Header->SymRecordStreamIndex;
237}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000238
Zachary Turner2f09b502016-04-29 17:28:47 +0000239PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000240 uint16_t Machine = Header->MachineType;
241 return static_cast<PDB_Machine>(Machine);
242}
Zachary Turner1822af542016-04-27 23:41:42 +0000243
Rui Ueyama90db7882016-06-02 18:20:20 +0000244codeview::FixedStreamArray<object::coff_section>
245DbiStream::getSectionHeaders() {
246 return SectionHeaders;
247}
248
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000249codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
250 return FpoRecords;
251}
252
Zachary Turner2f09b502016-04-29 17:28:47 +0000253ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000254codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
255 return SectionMap;
256}
257
258void llvm::pdb::DbiStream::visitSectionContributions(
259 ISectionContribVisitor &Visitor) const {
260 if (SectionContribVersion == DbiSecContribVer60) {
261 for (auto &SC : SectionContribs)
262 Visitor.visit(SC);
263 } else if (SectionContribVersion == DbiSecContribV2) {
264 for (auto &SC : SectionContribs2)
265 Visitor.visit(SC);
266 }
267}
268
269Error DbiStream::initializeSectionContributionData() {
270 StreamReader SCReader(SecContrSubstream);
271 if (auto EC = SCReader.readEnum(SectionContribVersion))
272 return EC;
273
274 if (SectionContribVersion == DbiSecContribVer60)
275 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
276 if (SectionContribVersion == DbiSecContribV2)
277 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
278
279 return make_error<RawError>(raw_error_code::feature_unsupported,
280 "Unsupported DBI Section Contribution version");
281}
282
Rui Ueyama90db7882016-06-02 18:20:20 +0000283// Initializes this->SectionHeaders.
284Error DbiStream::initializeSectionHeadersData() {
285 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000286 if (StreamNum >= Pdb.getNumStreams())
287 return make_error<RawError>(raw_error_code::no_stream);
288
Zachary Turnera1657a92016-06-08 17:26:39 +0000289 auto SHS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
290 if (!SHS)
291 return SHS.takeError();
Rui Ueyama90db7882016-06-02 18:20:20 +0000292
Zachary Turnera1657a92016-06-08 17:26:39 +0000293 size_t StreamLen = (*SHS)->getLength();
Rui Ueyama90db7882016-06-02 18:20:20 +0000294 if (StreamLen % sizeof(object::coff_section))
295 return make_error<RawError>(raw_error_code::corrupt_file,
296 "Corrupted section header stream.");
297
298 size_t NumSections = StreamLen / sizeof(object::coff_section);
Zachary Turnera1657a92016-06-08 17:26:39 +0000299 codeview::StreamReader Reader(**SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000300 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
301 return make_error<RawError>(raw_error_code::corrupt_file,
302 "Could not read a bitmap.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000303
304 SectionHeaderStream = std::move(*SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000305 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);
Reid Kleckner11582c52016-06-17 20:38:01 +0000311
312 // This means there is no FPO data.
313 if (StreamNum == InvalidStreamIndex)
314 return Error::success();
315
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000316 if (StreamNum >= Pdb.getNumStreams())
317 return make_error<RawError>(raw_error_code::no_stream);
318
Zachary Turnera1657a92016-06-08 17:26:39 +0000319 auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
320 if (!FS)
321 return FS.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000322
Zachary Turnera1657a92016-06-08 17:26:39 +0000323 size_t StreamLen = (*FS)->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000324 if (StreamLen % sizeof(object::FpoData))
325 return make_error<RawError>(raw_error_code::corrupt_file,
326 "Corrupted New FPO stream.");
327
328 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turnera1657a92016-06-08 17:26:39 +0000329 codeview::StreamReader Reader(**FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000330 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
331 return make_error<RawError>(raw_error_code::corrupt_file,
332 "Corrupted New FPO stream.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000333 FpoStream = std::move(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000334 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 // The layout of the FileInfoSubstream is like this:
349 // struct {
350 // ulittle16_t NumModules;
351 // ulittle16_t NumSourceFiles;
352 // ulittle16_t ModIndices[NumModules];
353 // ulittle16_t ModFileCounts[NumModules];
354 // ulittle32_t FileNameOffsets[NumSourceFiles];
355 // char Names[][NumSourceFiles];
356 // };
357 // with the caveat that `NumSourceFiles` cannot be trusted, so
358 // it is computed by summing `ModFileCounts`.
359 //
Zachary Turner8dbe3622016-05-27 01:54:44 +0000360 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000361 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000362 if (auto EC = FISR.readObject(FH))
363 return EC;
364
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000365 // The number of modules in the stream should be the same as reported by
366 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000367 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000368 return make_error<RawError>(raw_error_code::corrupt_file,
369 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000370
Zachary Turner93839cb2016-06-02 05:07:49 +0000371 FixedStreamArray<ulittle16_t> ModIndexArray;
372 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000373
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000374 // First is an array of `NumModules` module indices. This is not used for the
375 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
376 // but it's possible there are more than 64k source files, which would imply
377 // more than 64k modules (e.g. object files) as well. So we ignore this
378 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000379 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
380 return EC;
381 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
382 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000383
384 // Compute the real number of source files.
385 uint32_t NumSourceFiles = 0;
386 for (auto Count : ModFileCountArray)
387 NumSourceFiles += Count;
388
389 // This is the array that in the reference implementation corresponds to
390 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
391 // pointer. Due to the mentioned problems of pointers causing difficulty
392 // when reading from the file on 64-bit systems, we continue to ignore that
393 // field in `ModInfo`, and instead build a vector of StringRefs and stores
394 // them in `ModuleInfoEx`. The value written to and read from the file is
395 // not used anyway, it is only there as a way to store the offsets for the
396 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000397 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
398 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000399
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000400 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000401 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000402
403 // We go through each ModuleInfo, determine the number N of source files for
404 // that module, and then get the next N offsets from the Offsets array, using
405 // them to get the corresponding N names from the Names buffer and associating
406 // each one with the corresponding module.
407 uint32_t NextFileIndex = 0;
408 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
409 uint32_t NumFiles = ModFileCountArray[I];
410 ModuleInfos[I].SourceFiles.resize(NumFiles);
411 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000412 auto ThisName = getFileNameForIndex(NextFileIndex);
413 if (!ThisName)
414 return ThisName.takeError();
415 ModuleInfos[I].SourceFiles[J] = *ThisName;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000416 }
417 }
418
Zachary Turner819e77d2016-05-06 20:51:57 +0000419 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000420}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000421
422uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000423 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000424}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000425
426Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
427 StreamReader Names(NamesBuffer);
428 if (Index >= FileNameOffsets.size())
429 return make_error<RawError>(raw_error_code::index_out_of_bounds);
430
431 uint32_t FileOffset = FileNameOffsets[Index];
432 Names.setOffset(FileOffset);
433 StringRef Name;
434 if (auto EC = Names.readZeroString(Name))
435 return std::move(EC);
436 return Name;
437}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000438
439Error DbiStream::commit() { return Error::success(); }