blob: 3c0586c728f9934933fc6a68c64506211698d42a [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 Turnerfaa554b2016-07-15 22:16:56 +0000185 if (ECSubstream.getLength() > 0) {
186 StreamReader ECReader(ECSubstream);
187 if (auto EC = ECNames.load(ECReader))
188 return EC;
189 }
Zachary Turner0eace0b2016-05-02 18:09:14 +0000190
Zachary Turner819e77d2016-05-06 20:51:57 +0000191 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000192}
193
Zachary Turner2f09b502016-04-29 17:28:47 +0000194PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000195 uint32_t Value = Header->VersionHeader;
196 return static_cast<PdbRaw_DbiVer>(Value);
197}
198
Zachary Turner2f09b502016-04-29 17:28:47 +0000199uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000200
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000201uint16_t DbiStream::getPublicSymbolStreamIndex() const {
202 return Header->PublicSymbolStreamIndex;
203}
204
Zachary Turner96e60f72016-05-24 20:31:48 +0000205uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
206 return Header->GlobalSymbolStreamIndex;
207}
208
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000209uint16_t DbiStream::getFlags() const { return Header->Flags; }
210
Zachary Turner2f09b502016-04-29 17:28:47 +0000211bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000212 return (Header->Flags & FlagIncrementalMask) != 0;
213}
214
Zachary Turner2f09b502016-04-29 17:28:47 +0000215bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000216 return (Header->Flags & FlagHasCTypesMask) != 0;
217}
218
Zachary Turner2f09b502016-04-29 17:28:47 +0000219bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000220 return (Header->Flags & FlagStrippedMask) != 0;
221}
222
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000223uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
224
Zachary Turner2f09b502016-04-29 17:28:47 +0000225uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000226 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
227}
228
Zachary Turner2f09b502016-04-29 17:28:47 +0000229uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000230 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
231}
232
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000233uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
234
Zachary Turner2f09b502016-04-29 17:28:47 +0000235uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000236
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000237uint32_t DbiStream::getSymRecordStreamIndex() const {
238 return Header->SymRecordStreamIndex;
239}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000240
Zachary Turner2f09b502016-04-29 17:28:47 +0000241PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000242 uint16_t Machine = Header->MachineType;
243 return static_cast<PDB_Machine>(Machine);
244}
Zachary Turner1822af542016-04-27 23:41:42 +0000245
Rui Ueyama90db7882016-06-02 18:20:20 +0000246codeview::FixedStreamArray<object::coff_section>
247DbiStream::getSectionHeaders() {
248 return SectionHeaders;
249}
250
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000251codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
252 return FpoRecords;
253}
254
Zachary Turner2f09b502016-04-29 17:28:47 +0000255ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000256codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
257 return SectionMap;
258}
259
260void llvm::pdb::DbiStream::visitSectionContributions(
261 ISectionContribVisitor &Visitor) const {
262 if (SectionContribVersion == DbiSecContribVer60) {
263 for (auto &SC : SectionContribs)
264 Visitor.visit(SC);
265 } else if (SectionContribVersion == DbiSecContribV2) {
266 for (auto &SC : SectionContribs2)
267 Visitor.visit(SC);
268 }
269}
270
271Error DbiStream::initializeSectionContributionData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000272 if (SecContrSubstream.getLength() == 0)
273 return Error::success();
274
Zachary Turner93839cb2016-06-02 05:07:49 +0000275 StreamReader SCReader(SecContrSubstream);
276 if (auto EC = SCReader.readEnum(SectionContribVersion))
277 return EC;
278
279 if (SectionContribVersion == DbiSecContribVer60)
280 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
281 if (SectionContribVersion == DbiSecContribV2)
282 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
283
284 return make_error<RawError>(raw_error_code::feature_unsupported,
285 "Unsupported DBI Section Contribution version");
286}
287
Rui Ueyama90db7882016-06-02 18:20:20 +0000288// Initializes this->SectionHeaders.
289Error DbiStream::initializeSectionHeadersData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000290 if (DbgStreams.size() == 0)
291 return Error::success();
292
Rui Ueyama90db7882016-06-02 18:20:20 +0000293 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000294 if (StreamNum >= Pdb.getNumStreams())
295 return make_error<RawError>(raw_error_code::no_stream);
296
Zachary Turnera1657a92016-06-08 17:26:39 +0000297 auto SHS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
298 if (!SHS)
299 return SHS.takeError();
Rui Ueyama90db7882016-06-02 18:20:20 +0000300
Zachary Turnera1657a92016-06-08 17:26:39 +0000301 size_t StreamLen = (*SHS)->getLength();
Rui Ueyama90db7882016-06-02 18:20:20 +0000302 if (StreamLen % sizeof(object::coff_section))
303 return make_error<RawError>(raw_error_code::corrupt_file,
304 "Corrupted section header stream.");
305
306 size_t NumSections = StreamLen / sizeof(object::coff_section);
Zachary Turnera1657a92016-06-08 17:26:39 +0000307 codeview::StreamReader Reader(**SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000308 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
309 return make_error<RawError>(raw_error_code::corrupt_file,
310 "Could not read a bitmap.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000311
312 SectionHeaderStream = std::move(*SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000313 return Error::success();
314}
315
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000316// Initializes this->Fpos.
317Error DbiStream::initializeFpoRecords() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000318 if (DbgStreams.size() == 0)
319 return Error::success();
320
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000321 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Reid Kleckner11582c52016-06-17 20:38:01 +0000322
323 // This means there is no FPO data.
324 if (StreamNum == InvalidStreamIndex)
325 return Error::success();
326
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000327 if (StreamNum >= Pdb.getNumStreams())
328 return make_error<RawError>(raw_error_code::no_stream);
329
Zachary Turnera1657a92016-06-08 17:26:39 +0000330 auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
331 if (!FS)
332 return FS.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000333
Zachary Turnera1657a92016-06-08 17:26:39 +0000334 size_t StreamLen = (*FS)->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000335 if (StreamLen % sizeof(object::FpoData))
336 return make_error<RawError>(raw_error_code::corrupt_file,
337 "Corrupted New FPO stream.");
338
339 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turnera1657a92016-06-08 17:26:39 +0000340 codeview::StreamReader Reader(**FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000341 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
342 return make_error<RawError>(raw_error_code::corrupt_file,
343 "Corrupted New FPO stream.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000344 FpoStream = std::move(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000345 return Error::success();
346}
347
Zachary Turner93839cb2016-06-02 05:07:49 +0000348Error DbiStream::initializeSectionMapData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000349 if (SecMapSubstream.getLength() == 0)
350 return Error::success();
351
Zachary Turner93839cb2016-06-02 05:07:49 +0000352 StreamReader SMReader(SecMapSubstream);
353 const SecMapHeader *Header;
354 if (auto EC = SMReader.readObject(Header))
355 return EC;
356 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
357 return EC;
358 return Error::success();
359}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000360
Zachary Turner819e77d2016-05-06 20:51:57 +0000361Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000362 // The layout of the FileInfoSubstream is like this:
363 // struct {
364 // ulittle16_t NumModules;
365 // ulittle16_t NumSourceFiles;
366 // ulittle16_t ModIndices[NumModules];
367 // ulittle16_t ModFileCounts[NumModules];
368 // ulittle32_t FileNameOffsets[NumSourceFiles];
369 // char Names[][NumSourceFiles];
370 // };
371 // with the caveat that `NumSourceFiles` cannot be trusted, so
372 // it is computed by summing `ModFileCounts`.
373 //
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000374 if (FileInfoSubstream.getLength() == 0)
375 return Error::success();
376
Zachary Turner8dbe3622016-05-27 01:54:44 +0000377 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000378 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000379 if (auto EC = FISR.readObject(FH))
380 return EC;
381
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000382 // The number of modules in the stream should be the same as reported by
383 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000384 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000385 return make_error<RawError>(raw_error_code::corrupt_file,
386 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000387
Zachary Turner93839cb2016-06-02 05:07:49 +0000388 FixedStreamArray<ulittle16_t> ModIndexArray;
389 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000390
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000391 // First is an array of `NumModules` module indices. This is not used for the
392 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
393 // but it's possible there are more than 64k source files, which would imply
394 // more than 64k modules (e.g. object files) as well. So we ignore this
395 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000396 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
397 return EC;
398 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
399 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000400
401 // Compute the real number of source files.
402 uint32_t NumSourceFiles = 0;
403 for (auto Count : ModFileCountArray)
404 NumSourceFiles += Count;
405
406 // This is the array that in the reference implementation corresponds to
407 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
408 // pointer. Due to the mentioned problems of pointers causing difficulty
409 // when reading from the file on 64-bit systems, we continue to ignore that
410 // field in `ModInfo`, and instead build a vector of StringRefs and stores
411 // them in `ModuleInfoEx`. The value written to and read from the file is
412 // not used anyway, it is only there as a way to store the offsets for the
413 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000414 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
415 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000416
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000417 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000418 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000419
420 // We go through each ModuleInfo, determine the number N of source files for
421 // that module, and then get the next N offsets from the Offsets array, using
422 // them to get the corresponding N names from the Names buffer and associating
423 // each one with the corresponding module.
424 uint32_t NextFileIndex = 0;
425 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
426 uint32_t NumFiles = ModFileCountArray[I];
427 ModuleInfos[I].SourceFiles.resize(NumFiles);
428 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000429 auto ThisName = getFileNameForIndex(NextFileIndex);
430 if (!ThisName)
431 return ThisName.takeError();
432 ModuleInfos[I].SourceFiles[J] = *ThisName;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000433 }
434 }
435
Zachary Turner819e77d2016-05-06 20:51:57 +0000436 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000437}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000438
439uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000440 return DbgStreams[static_cast<uint16_t>(Type)];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000441}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000442
443Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
444 StreamReader Names(NamesBuffer);
445 if (Index >= FileNameOffsets.size())
446 return make_error<RawError>(raw_error_code::index_out_of_bounds);
447
448 uint32_t FileOffset = FileNameOffsets[Index];
449 Names.setOffset(FileOffset);
450 StringRef Name;
451 if (auto EC = Names.readZeroString(Name))
452 return std::move(EC);
453 return Name;
454}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000455
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000456Error DbiStream::commit() {
457 StreamWriter Writer(*Stream);
458 if (auto EC = Writer.writeObject(*Header))
459 return EC;
460
461 return Error::success();
462}