blob: 63a7ba109ce1c697e5b5a1bafe3c32cec981595a [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 Turnerd218c262016-07-22 15:46:37 +0000145 if (auto EC =
146 Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize))
Zachary Turner1de49c92016-05-27 18:47:20 +0000147 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000148 if (auto EC = initializeModInfoArray())
149 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000150
Zachary Turner8dbe3622016-05-27 01:54:44 +0000151 if (auto EC = Reader.readStreamRef(SecContrSubstream,
152 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000153 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000154 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000155 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000156 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000157 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000158 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000159 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000160 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000161 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000162 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000163 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
164 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000165 return EC;
166
Zachary Turner93839cb2016-06-02 05:07:49 +0000167 if (auto EC = initializeSectionContributionData())
168 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000169 if (auto EC = initializeSectionHeadersData())
170 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000171 if (auto EC = initializeSectionMapData())
172 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000173 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000174 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000175 if (auto EC = initializeFpoRecords())
176 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000177
Zachary Turner6ba65de2016-04-29 17:22:58 +0000178 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000179 return make_error<RawError>(raw_error_code::corrupt_file,
180 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000181
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000182 if (ECSubstream.getLength() > 0) {
183 StreamReader ECReader(ECSubstream);
184 if (auto EC = ECNames.load(ECReader))
185 return EC;
186 }
Zachary Turner0eace0b2016-05-02 18:09:14 +0000187
Zachary Turner819e77d2016-05-06 20:51:57 +0000188 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000189}
190
Zachary Turner2f09b502016-04-29 17:28:47 +0000191PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000192 uint32_t Value = Header->VersionHeader;
193 return static_cast<PdbRaw_DbiVer>(Value);
194}
195
Zachary Turner2f09b502016-04-29 17:28:47 +0000196uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000197
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000198uint16_t DbiStream::getPublicSymbolStreamIndex() const {
199 return Header->PublicSymbolStreamIndex;
200}
201
Zachary Turner96e60f72016-05-24 20:31:48 +0000202uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
203 return Header->GlobalSymbolStreamIndex;
204}
205
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000206uint16_t DbiStream::getFlags() const { return Header->Flags; }
207
Zachary Turner2f09b502016-04-29 17:28:47 +0000208bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000209 return (Header->Flags & FlagIncrementalMask) != 0;
210}
211
Zachary Turner2f09b502016-04-29 17:28:47 +0000212bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000213 return (Header->Flags & FlagHasCTypesMask) != 0;
214}
215
Zachary Turner2f09b502016-04-29 17:28:47 +0000216bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000217 return (Header->Flags & FlagStrippedMask) != 0;
218}
219
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000220uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
221
Zachary Turner2f09b502016-04-29 17:28:47 +0000222uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000223 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
224}
225
Zachary Turner2f09b502016-04-29 17:28:47 +0000226uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000227 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
228}
229
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000230uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
231
Zachary Turner2f09b502016-04-29 17:28:47 +0000232uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000233
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000234uint32_t DbiStream::getSymRecordStreamIndex() const {
235 return Header->SymRecordStreamIndex;
236}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000237
Zachary Turner2f09b502016-04-29 17:28:47 +0000238PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000239 uint16_t Machine = Header->MachineType;
240 return static_cast<PDB_Machine>(Machine);
241}
Zachary Turner1822af542016-04-27 23:41:42 +0000242
Rui Ueyama90db7882016-06-02 18:20:20 +0000243codeview::FixedStreamArray<object::coff_section>
244DbiStream::getSectionHeaders() {
245 return SectionHeaders;
246}
247
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000248codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
249 return FpoRecords;
250}
251
Zachary Turner2f09b502016-04-29 17:28:47 +0000252ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000253codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
254 return SectionMap;
255}
256
257void llvm::pdb::DbiStream::visitSectionContributions(
258 ISectionContribVisitor &Visitor) const {
259 if (SectionContribVersion == DbiSecContribVer60) {
260 for (auto &SC : SectionContribs)
261 Visitor.visit(SC);
262 } else if (SectionContribVersion == DbiSecContribV2) {
263 for (auto &SC : SectionContribs2)
264 Visitor.visit(SC);
265 }
266}
267
268Error DbiStream::initializeSectionContributionData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000269 if (SecContrSubstream.getLength() == 0)
270 return Error::success();
271
Zachary Turner93839cb2016-06-02 05:07:49 +0000272 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
Zachary Turnerd218c262016-07-22 15:46:37 +0000285Error DbiStream::initializeModInfoArray() {
286 if (ModInfoSubstream.getLength() == 0)
287 return Error::success();
288
289 // Since each ModInfo in the stream is a variable length, we have to iterate
290 // them to know how many there actually are.
291 StreamReader Reader(ModInfoSubstream);
292
293 VarStreamArray<ModInfo> ModInfoArray;
294 if (auto EC = Reader.readArray(ModInfoArray, ModInfoSubstream.getLength()))
295 return EC;
296 for (auto &Info : ModInfoArray) {
297 ModuleInfos.emplace_back(Info);
298 }
299
300 return Error::success();
301}
302
Rui Ueyama90db7882016-06-02 18:20:20 +0000303// Initializes this->SectionHeaders.
304Error DbiStream::initializeSectionHeadersData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000305 if (DbgStreams.size() == 0)
306 return Error::success();
307
Rui Ueyama90db7882016-06-02 18:20:20 +0000308 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000309 if (StreamNum >= Pdb.getNumStreams())
310 return make_error<RawError>(raw_error_code::no_stream);
311
Zachary Turnera1657a92016-06-08 17:26:39 +0000312 auto SHS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
313 if (!SHS)
314 return SHS.takeError();
Rui Ueyama90db7882016-06-02 18:20:20 +0000315
Zachary Turnera1657a92016-06-08 17:26:39 +0000316 size_t StreamLen = (*SHS)->getLength();
Rui Ueyama90db7882016-06-02 18:20:20 +0000317 if (StreamLen % sizeof(object::coff_section))
318 return make_error<RawError>(raw_error_code::corrupt_file,
319 "Corrupted section header stream.");
320
321 size_t NumSections = StreamLen / sizeof(object::coff_section);
Zachary Turnera1657a92016-06-08 17:26:39 +0000322 codeview::StreamReader Reader(**SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000323 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
324 return make_error<RawError>(raw_error_code::corrupt_file,
325 "Could not read a bitmap.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000326
327 SectionHeaderStream = std::move(*SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000328 return Error::success();
329}
330
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000331// Initializes this->Fpos.
332Error DbiStream::initializeFpoRecords() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000333 if (DbgStreams.size() == 0)
334 return Error::success();
335
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000336 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Reid Kleckner11582c52016-06-17 20:38:01 +0000337
338 // This means there is no FPO data.
339 if (StreamNum == InvalidStreamIndex)
340 return Error::success();
341
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000342 if (StreamNum >= Pdb.getNumStreams())
343 return make_error<RawError>(raw_error_code::no_stream);
344
Zachary Turnera1657a92016-06-08 17:26:39 +0000345 auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
346 if (!FS)
347 return FS.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000348
Zachary Turnera1657a92016-06-08 17:26:39 +0000349 size_t StreamLen = (*FS)->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000350 if (StreamLen % sizeof(object::FpoData))
351 return make_error<RawError>(raw_error_code::corrupt_file,
352 "Corrupted New FPO stream.");
353
354 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turnera1657a92016-06-08 17:26:39 +0000355 codeview::StreamReader Reader(**FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000356 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
357 return make_error<RawError>(raw_error_code::corrupt_file,
358 "Corrupted New FPO stream.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000359 FpoStream = std::move(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000360 return Error::success();
361}
362
Zachary Turner93839cb2016-06-02 05:07:49 +0000363Error DbiStream::initializeSectionMapData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000364 if (SecMapSubstream.getLength() == 0)
365 return Error::success();
366
Zachary Turner93839cb2016-06-02 05:07:49 +0000367 StreamReader SMReader(SecMapSubstream);
368 const SecMapHeader *Header;
369 if (auto EC = SMReader.readObject(Header))
370 return EC;
371 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
372 return EC;
373 return Error::success();
374}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000375
Zachary Turner819e77d2016-05-06 20:51:57 +0000376Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000377 // The layout of the FileInfoSubstream is like this:
378 // struct {
379 // ulittle16_t NumModules;
380 // ulittle16_t NumSourceFiles;
381 // ulittle16_t ModIndices[NumModules];
382 // ulittle16_t ModFileCounts[NumModules];
383 // ulittle32_t FileNameOffsets[NumSourceFiles];
384 // char Names[][NumSourceFiles];
385 // };
386 // with the caveat that `NumSourceFiles` cannot be trusted, so
387 // it is computed by summing `ModFileCounts`.
388 //
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000389 if (FileInfoSubstream.getLength() == 0)
390 return Error::success();
391
Zachary Turner8dbe3622016-05-27 01:54:44 +0000392 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000393 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000394 if (auto EC = FISR.readObject(FH))
395 return EC;
396
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000397 // The number of modules in the stream should be the same as reported by
398 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000399 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000400 return make_error<RawError>(raw_error_code::corrupt_file,
401 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000402
Zachary Turner93839cb2016-06-02 05:07:49 +0000403 FixedStreamArray<ulittle16_t> ModIndexArray;
404 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000405
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000406 // First is an array of `NumModules` module indices. This is not used for the
407 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
408 // but it's possible there are more than 64k source files, which would imply
409 // more than 64k modules (e.g. object files) as well. So we ignore this
410 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000411 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
412 return EC;
413 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
414 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000415
416 // Compute the real number of source files.
417 uint32_t NumSourceFiles = 0;
418 for (auto Count : ModFileCountArray)
419 NumSourceFiles += Count;
420
421 // This is the array that in the reference implementation corresponds to
422 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
423 // pointer. Due to the mentioned problems of pointers causing difficulty
424 // when reading from the file on 64-bit systems, we continue to ignore that
425 // field in `ModInfo`, and instead build a vector of StringRefs and stores
426 // them in `ModuleInfoEx`. The value written to and read from the file is
427 // not used anyway, it is only there as a way to store the offsets for the
428 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000429 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
430 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000431
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000432 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000433 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000434
435 // We go through each ModuleInfo, determine the number N of source files for
436 // that module, and then get the next N offsets from the Offsets array, using
437 // them to get the corresponding N names from the Names buffer and associating
438 // each one with the corresponding module.
439 uint32_t NextFileIndex = 0;
440 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
441 uint32_t NumFiles = ModFileCountArray[I];
442 ModuleInfos[I].SourceFiles.resize(NumFiles);
443 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000444 auto ThisName = getFileNameForIndex(NextFileIndex);
445 if (!ThisName)
446 return ThisName.takeError();
447 ModuleInfos[I].SourceFiles[J] = *ThisName;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000448 }
449 }
450
Zachary Turner819e77d2016-05-06 20:51:57 +0000451 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000452}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000453
454uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turnerd218c262016-07-22 15:46:37 +0000455 uint16_t T = static_cast<uint16_t>(Type);
456 if (T >= DbgStreams.size())
457 return DbiStream::InvalidStreamIndex;
458 return DbgStreams[T];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000459}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000460
461Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
462 StreamReader Names(NamesBuffer);
463 if (Index >= FileNameOffsets.size())
464 return make_error<RawError>(raw_error_code::index_out_of_bounds);
465
466 uint32_t FileOffset = FileNameOffsets[Index];
467 Names.setOffset(FileOffset);
468 StringRef Name;
469 if (auto EC = Names.readZeroString(Name))
470 return std::move(EC);
471 return Name;
472}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000473
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000474Error DbiStream::commit() {
475 StreamWriter Writer(*Stream);
476 if (auto EC = Writer.writeObject(*Header))
477 return EC;
478
Zachary Turnerd218c262016-07-22 15:46:37 +0000479 if (auto EC = Writer.writeStreamRef(ModInfoSubstream))
480 return EC;
481
482 if (auto EC = Writer.writeStreamRef(SecContrSubstream,
483 SecContrSubstream.getLength()))
484 return EC;
485 if (auto EC =
486 Writer.writeStreamRef(SecMapSubstream, SecMapSubstream.getLength()))
487 return EC;
488 if (auto EC = Writer.writeStreamRef(FileInfoSubstream,
489 FileInfoSubstream.getLength()))
490 return EC;
491 if (auto EC = Writer.writeStreamRef(TypeServerMapSubstream,
492 TypeServerMapSubstream.getLength()))
493 return EC;
494 if (auto EC = Writer.writeStreamRef(ECSubstream, ECSubstream.getLength()))
495 return EC;
496
497 if (Writer.bytesRemaining() > 0)
498 return make_error<RawError>(raw_error_code::invalid_format,
499 "Unexpected bytes found in DBI Stream");
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000500 return Error::success();
501}