blob: 9ed34701c8fc249d3fd9e24bb3a05a92e4c553be [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 Turnera3225b02016-07-29 20:56:36 +000012#include "llvm/DebugInfo/MSF/StreamArray.h"
13#include "llvm/DebugInfo/MSF/StreamReader.h"
14#include "llvm/DebugInfo/MSF/StreamWriter.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000015#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.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 Turnerbac69d32016-07-22 19:56:05 +000027using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000028using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000029using namespace llvm::support;
30
Zachary Turner93839cb2016-06-02 05:07:49 +000031template <typename ContribType>
Benjamin Kramer4d098922016-07-10 11:28:51 +000032static Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
33 StreamReader &Reader) {
Zachary Turner93839cb2016-06-02 05:07:49 +000034 if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
35 return make_error<RawError>(
36 raw_error_code::corrupt_file,
37 "Invalid number of bytes of section contributions");
38
39 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
40 if (auto EC = Reader.readArray(Output, Count))
41 return EC;
42 return Error::success();
43}
44
Zachary Turnera1657a92016-06-08 17:26:39 +000045DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
46 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000047}
48
Zachary Turner2f09b502016-04-29 17:28:47 +000049DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000050
Zachary Turner819e77d2016-05-06 20:51:57 +000051Error DbiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +000052 StreamReader Reader(*Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +000053
Zachary Turnerb383d622016-07-22 15:46:46 +000054 if (Stream->getLength() < sizeof(DbiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000055 return make_error<RawError>(raw_error_code::corrupt_file,
56 "DBI Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000057 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000058 return make_error<RawError>(raw_error_code::corrupt_file,
59 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000060
61 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +000062 return make_error<RawError>(raw_error_code::corrupt_file,
63 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000064
Zachary Turner1822af542016-04-27 23:41:42 +000065 // Require at least version 7, which should be present in all PDBs
66 // produced in the last decade and allows us to avoid having to
67 // special case all kinds of complicated arcane formats.
68 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner93839cb2016-06-02 05:07:49 +000069 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +000070 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000071
Zachary Turnera1657a92016-06-08 17:26:39 +000072 auto IS = Pdb.getPDBInfoStream();
73 if (!IS)
74 return IS.takeError();
Zachary Turner819e77d2016-05-06 20:51:57 +000075
Zachary Turnera1657a92016-06-08 17:26:39 +000076 if (Header->Age != IS->getAge())
Zachary Turner819e77d2016-05-06 20:51:57 +000077 return make_error<RawError>(raw_error_code::corrupt_file,
78 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000079
Zachary Turnera1657a92016-06-08 17:26:39 +000080 if (Stream->getLength() !=
Zachary Turnerb383d622016-07-22 15:46:46 +000081 sizeof(DbiStreamHeader) + Header->ModiSubstreamSize +
Zachary Turner53a65ba2016-04-26 18:42:34 +000082 Header->SecContrSubstreamSize + Header->SectionMapSize +
83 Header->FileInfoSize + Header->TypeServerSize +
84 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +000085 return make_error<RawError>(raw_error_code::corrupt_file,
86 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000087
Zachary Turner84c3a8b2016-04-28 20:05:18 +000088 // Only certain substreams are guaranteed to be aligned. Validate
89 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +000090 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000091 return make_error<RawError>(raw_error_code::corrupt_file,
92 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +000093 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000094 return make_error<RawError>(
95 raw_error_code::corrupt_file,
96 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +000097 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000098 return make_error<RawError>(raw_error_code::corrupt_file,
99 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000100 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000101 return make_error<RawError>(raw_error_code::corrupt_file,
102 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000103 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000104 return make_error<RawError>(raw_error_code::corrupt_file,
105 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000106
Zachary Turnerd218c262016-07-22 15:46:37 +0000107 if (auto EC =
108 Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize))
Zachary Turner1de49c92016-05-27 18:47:20 +0000109 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000110 if (auto EC = initializeModInfoArray())
111 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000112
Zachary Turner8dbe3622016-05-27 01:54:44 +0000113 if (auto EC = Reader.readStreamRef(SecContrSubstream,
114 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000115 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000116 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000117 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000118 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000119 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000120 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000121 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000122 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000123 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000124 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000125 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
126 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000127 return EC;
128
Zachary Turner93839cb2016-06-02 05:07:49 +0000129 if (auto EC = initializeSectionContributionData())
130 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000131 if (auto EC = initializeSectionHeadersData())
132 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000133 if (auto EC = initializeSectionMapData())
134 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000135 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000136 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000137 if (auto EC = initializeFpoRecords())
138 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000139
Zachary Turner6ba65de2016-04-29 17:22:58 +0000140 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000141 return make_error<RawError>(raw_error_code::corrupt_file,
142 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000143
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000144 if (ECSubstream.getLength() > 0) {
145 StreamReader ECReader(ECSubstream);
146 if (auto EC = ECNames.load(ECReader))
147 return EC;
148 }
Zachary Turner0eace0b2016-05-02 18:09:14 +0000149
Zachary Turner819e77d2016-05-06 20:51:57 +0000150 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000151}
152
Zachary Turner2f09b502016-04-29 17:28:47 +0000153PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000154 uint32_t Value = Header->VersionHeader;
155 return static_cast<PdbRaw_DbiVer>(Value);
156}
157
Zachary Turner2f09b502016-04-29 17:28:47 +0000158uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000159
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000160uint16_t DbiStream::getPublicSymbolStreamIndex() const {
161 return Header->PublicSymbolStreamIndex;
162}
163
Zachary Turner96e60f72016-05-24 20:31:48 +0000164uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
165 return Header->GlobalSymbolStreamIndex;
166}
167
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000168uint16_t DbiStream::getFlags() const { return Header->Flags; }
169
Zachary Turner2f09b502016-04-29 17:28:47 +0000170bool DbiStream::isIncrementallyLinked() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000171 return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000172}
173
Zachary Turner2f09b502016-04-29 17:28:47 +0000174bool DbiStream::hasCTypes() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000175 return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000176}
177
Zachary Turner2f09b502016-04-29 17:28:47 +0000178bool DbiStream::isStripped() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000179 return (Header->Flags & DbiFlags::FlagStrippedMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000180}
181
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000182uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
183
Zachary Turner2f09b502016-04-29 17:28:47 +0000184uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000185 return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >>
186 DbiBuildNo::BuildMajorShift;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000187}
188
Zachary Turner2f09b502016-04-29 17:28:47 +0000189uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000190 return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >>
191 DbiBuildNo::BuildMinorShift;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000192}
193
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000194uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
195
Zachary Turner2f09b502016-04-29 17:28:47 +0000196uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000197
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000198uint32_t DbiStream::getSymRecordStreamIndex() const {
199 return Header->SymRecordStreamIndex;
200}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000201
Zachary Turner2f09b502016-04-29 17:28:47 +0000202PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000203 uint16_t Machine = Header->MachineType;
204 return static_cast<PDB_Machine>(Machine);
205}
Zachary Turner1822af542016-04-27 23:41:42 +0000206
Zachary Turnerbac69d32016-07-22 19:56:05 +0000207msf::FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() {
Rui Ueyama90db7882016-06-02 18:20:20 +0000208 return SectionHeaders;
209}
210
Zachary Turnerbac69d32016-07-22 19:56:05 +0000211msf::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000212 return FpoRecords;
213}
214
Zachary Turner2f09b502016-04-29 17:28:47 +0000215ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turnerbac69d32016-07-22 19:56:05 +0000216msf::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
Zachary Turner93839cb2016-06-02 05:07:49 +0000217 return SectionMap;
218}
219
220void llvm::pdb::DbiStream::visitSectionContributions(
221 ISectionContribVisitor &Visitor) const {
222 if (SectionContribVersion == DbiSecContribVer60) {
223 for (auto &SC : SectionContribs)
224 Visitor.visit(SC);
225 } else if (SectionContribVersion == DbiSecContribV2) {
226 for (auto &SC : SectionContribs2)
227 Visitor.visit(SC);
228 }
229}
230
231Error DbiStream::initializeSectionContributionData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000232 if (SecContrSubstream.getLength() == 0)
233 return Error::success();
234
Zachary Turner93839cb2016-06-02 05:07:49 +0000235 StreamReader SCReader(SecContrSubstream);
236 if (auto EC = SCReader.readEnum(SectionContribVersion))
237 return EC;
238
239 if (SectionContribVersion == DbiSecContribVer60)
240 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
241 if (SectionContribVersion == DbiSecContribV2)
242 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
243
244 return make_error<RawError>(raw_error_code::feature_unsupported,
245 "Unsupported DBI Section Contribution version");
246}
247
Zachary Turnerd218c262016-07-22 15:46:37 +0000248Error DbiStream::initializeModInfoArray() {
249 if (ModInfoSubstream.getLength() == 0)
250 return Error::success();
251
252 // Since each ModInfo in the stream is a variable length, we have to iterate
253 // them to know how many there actually are.
254 StreamReader Reader(ModInfoSubstream);
255
256 VarStreamArray<ModInfo> ModInfoArray;
257 if (auto EC = Reader.readArray(ModInfoArray, ModInfoSubstream.getLength()))
258 return EC;
259 for (auto &Info : ModInfoArray) {
260 ModuleInfos.emplace_back(Info);
261 }
262
263 return Error::success();
264}
265
Rui Ueyama90db7882016-06-02 18:20:20 +0000266// Initializes this->SectionHeaders.
267Error DbiStream::initializeSectionHeadersData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000268 if (DbgStreams.size() == 0)
269 return Error::success();
270
Rui Ueyama90db7882016-06-02 18:20:20 +0000271 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000272 if (StreamNum >= Pdb.getNumStreams())
273 return make_error<RawError>(raw_error_code::no_stream);
274
Zachary Turnerd66889c2016-07-28 19:12:28 +0000275 auto SHS = MappedBlockStream::createIndexedStream(
276 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum);
Rui Ueyama90db7882016-06-02 18:20:20 +0000277
Zachary Turnerd66889c2016-07-28 19:12:28 +0000278 size_t StreamLen = SHS->getLength();
Rui Ueyama90db7882016-06-02 18:20:20 +0000279 if (StreamLen % sizeof(object::coff_section))
280 return make_error<RawError>(raw_error_code::corrupt_file,
281 "Corrupted section header stream.");
282
283 size_t NumSections = StreamLen / sizeof(object::coff_section);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000284 msf::StreamReader Reader(*SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000285 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
286 return make_error<RawError>(raw_error_code::corrupt_file,
287 "Could not read a bitmap.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000288
Zachary Turnerd66889c2016-07-28 19:12:28 +0000289 SectionHeaderStream = std::move(SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000290 return Error::success();
291}
292
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000293// Initializes this->Fpos.
294Error DbiStream::initializeFpoRecords() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000295 if (DbgStreams.size() == 0)
296 return Error::success();
297
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000298 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Reid Kleckner11582c52016-06-17 20:38:01 +0000299
300 // This means there is no FPO data.
Zachary Turnerb383d622016-07-22 15:46:46 +0000301 if (StreamNum == kInvalidStreamIndex)
Reid Kleckner11582c52016-06-17 20:38:01 +0000302 return Error::success();
303
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000304 if (StreamNum >= Pdb.getNumStreams())
305 return make_error<RawError>(raw_error_code::no_stream);
306
Zachary Turnerd66889c2016-07-28 19:12:28 +0000307 auto FS = MappedBlockStream::createIndexedStream(
308 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000309
Zachary Turnerd66889c2016-07-28 19:12:28 +0000310 size_t StreamLen = FS->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000311 if (StreamLen % sizeof(object::FpoData))
312 return make_error<RawError>(raw_error_code::corrupt_file,
313 "Corrupted New FPO stream.");
314
315 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turnerd66889c2016-07-28 19:12:28 +0000316 msf::StreamReader Reader(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000317 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
318 return make_error<RawError>(raw_error_code::corrupt_file,
319 "Corrupted New FPO stream.");
Zachary Turnerd66889c2016-07-28 19:12:28 +0000320 FpoStream = std::move(FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000321 return Error::success();
322}
323
Zachary Turner93839cb2016-06-02 05:07:49 +0000324Error DbiStream::initializeSectionMapData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000325 if (SecMapSubstream.getLength() == 0)
326 return Error::success();
327
Zachary Turner93839cb2016-06-02 05:07:49 +0000328 StreamReader SMReader(SecMapSubstream);
329 const SecMapHeader *Header;
330 if (auto EC = SMReader.readObject(Header))
331 return EC;
332 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
333 return EC;
334 return Error::success();
335}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000336
Zachary Turner819e77d2016-05-06 20:51:57 +0000337Error DbiStream::initializeFileInfo() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000338 if (FileInfoSubstream.getLength() == 0)
339 return Error::success();
340
Zachary Turner8dbe3622016-05-27 01:54:44 +0000341 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000342 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000343 if (auto EC = FISR.readObject(FH))
344 return EC;
345
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000346 // The number of modules in the stream should be the same as reported by
347 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000348 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000349 return make_error<RawError>(raw_error_code::corrupt_file,
350 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000351
Zachary Turner93839cb2016-06-02 05:07:49 +0000352 FixedStreamArray<ulittle16_t> ModIndexArray;
353 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000354
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000355 // First is an array of `NumModules` module indices. This is not used for the
356 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
357 // but it's possible there are more than 64k source files, which would imply
358 // more than 64k modules (e.g. object files) as well. So we ignore this
359 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000360 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
361 return EC;
362 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
363 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000364
365 // Compute the real number of source files.
366 uint32_t NumSourceFiles = 0;
367 for (auto Count : ModFileCountArray)
368 NumSourceFiles += Count;
369
370 // This is the array that in the reference implementation corresponds to
371 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
372 // pointer. Due to the mentioned problems of pointers causing difficulty
373 // when reading from the file on 64-bit systems, we continue to ignore that
374 // field in `ModInfo`, and instead build a vector of StringRefs and stores
375 // them in `ModuleInfoEx`. The value written to and read from the file is
376 // not used anyway, it is only there as a way to store the offsets for the
377 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000378 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
379 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000380
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000381 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000382 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000383
384 // We go through each ModuleInfo, determine the number N of source files for
385 // that module, and then get the next N offsets from the Offsets array, using
386 // them to get the corresponding N names from the Names buffer and associating
387 // each one with the corresponding module.
388 uint32_t NextFileIndex = 0;
389 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
390 uint32_t NumFiles = ModFileCountArray[I];
391 ModuleInfos[I].SourceFiles.resize(NumFiles);
392 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000393 auto ThisName = getFileNameForIndex(NextFileIndex);
394 if (!ThisName)
395 return ThisName.takeError();
396 ModuleInfos[I].SourceFiles[J] = *ThisName;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000397 }
398 }
399
Zachary Turner819e77d2016-05-06 20:51:57 +0000400 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000401}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000402
403uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turnerd218c262016-07-22 15:46:37 +0000404 uint16_t T = static_cast<uint16_t>(Type);
405 if (T >= DbgStreams.size())
Zachary Turnerb383d622016-07-22 15:46:46 +0000406 return kInvalidStreamIndex;
Zachary Turnerd218c262016-07-22 15:46:37 +0000407 return DbgStreams[T];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000408}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000409
410Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
411 StreamReader Names(NamesBuffer);
412 if (Index >= FileNameOffsets.size())
413 return make_error<RawError>(raw_error_code::index_out_of_bounds);
414
415 uint32_t FileOffset = FileNameOffsets[Index];
416 Names.setOffset(FileOffset);
417 StringRef Name;
418 if (auto EC = Names.readZeroString(Name))
419 return std::move(EC);
420 return Name;
421}