blob: db703809f7c9c076ab94f8f5ea99a62733133d94 [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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/StringRef.h"
12#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000013#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000014#include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
15#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000016#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
18#include "llvm/DebugInfo/PDB/Native/RawError.h"
19#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000020#include "llvm/DebugInfo/PDB/PDBTypes.h"
Rui Ueyama90db7882016-06-02 18:20:20 +000021#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000022#include "llvm/Support/BinaryStreamArray.h"
23#include "llvm/Support/BinaryStreamReader.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include "llvm/Support/Error.h"
25#include <algorithm>
26#include <cstddef>
27#include <cstdint>
Zachary Turner53a65ba2016-04-26 18:42:34 +000028
29using namespace llvm;
Zachary Turner93839cb2016-06-02 05:07:49 +000030using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000031using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000032using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000033using namespace llvm::support;
34
Zachary Turner93839cb2016-06-02 05:07:49 +000035template <typename ContribType>
Benjamin Kramer4d098922016-07-10 11:28:51 +000036static Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
Zachary Turner120faca2017-02-27 22:11:43 +000037 BinaryStreamReader &Reader) {
Zachary Turner93839cb2016-06-02 05:07:49 +000038 if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
39 return make_error<RawError>(
40 raw_error_code::corrupt_file,
41 "Invalid number of bytes of section contributions");
42
43 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
44 if (auto EC = Reader.readArray(Output, Count))
45 return EC;
46 return Error::success();
47}
48
Zachary Turnera1657a92016-06-08 17:26:39 +000049DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000050 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000051
Eugene Zelenko570e39a2016-11-23 23:16:32 +000052DbiStream::~DbiStream() = default;
Zachary Turner53a65ba2016-04-26 18:42:34 +000053
Zachary Turner819e77d2016-05-06 20:51:57 +000054Error DbiStream::reload() {
Zachary Turner120faca2017-02-27 22:11:43 +000055 BinaryStreamReader Reader(*Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +000056
Zachary Turnerb383d622016-07-22 15:46:46 +000057 if (Stream->getLength() < sizeof(DbiStreamHeader))
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 Turner8dbe3622016-05-27 01:54:44 +000060 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000061 return make_error<RawError>(raw_error_code::corrupt_file,
62 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000063
64 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +000065 return make_error<RawError>(raw_error_code::corrupt_file,
66 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000067
Zachary Turner1822af542016-04-27 23:41:42 +000068 // Require at least version 7, which should be present in all PDBs
69 // produced in the last decade and allows us to avoid having to
70 // special case all kinds of complicated arcane formats.
71 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner93839cb2016-06-02 05:07:49 +000072 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +000073 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000074
Zachary Turnera1657a92016-06-08 17:26:39 +000075 auto IS = Pdb.getPDBInfoStream();
76 if (!IS)
77 return IS.takeError();
Zachary Turner819e77d2016-05-06 20:51:57 +000078
Zachary Turnera1657a92016-06-08 17:26:39 +000079 if (Header->Age != IS->getAge())
Zachary Turner819e77d2016-05-06 20:51:57 +000080 return make_error<RawError>(raw_error_code::corrupt_file,
81 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000082
Zachary Turnera1657a92016-06-08 17:26:39 +000083 if (Stream->getLength() !=
Zachary Turnerb383d622016-07-22 15:46:46 +000084 sizeof(DbiStreamHeader) + Header->ModiSubstreamSize +
Zachary Turner53a65ba2016-04-26 18:42:34 +000085 Header->SecContrSubstreamSize + Header->SectionMapSize +
86 Header->FileInfoSize + Header->TypeServerSize +
87 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +000088 return make_error<RawError>(raw_error_code::corrupt_file,
89 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000090
Zachary Turner84c3a8b2016-04-28 20:05:18 +000091 // Only certain substreams are guaranteed to be aligned. Validate
92 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +000093 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000094 return make_error<RawError>(raw_error_code::corrupt_file,
95 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +000096 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000097 return make_error<RawError>(
98 raw_error_code::corrupt_file,
99 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000100 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000101 return make_error<RawError>(raw_error_code::corrupt_file,
102 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000103 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000104 return make_error<RawError>(raw_error_code::corrupt_file,
105 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000106 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000107 return make_error<RawError>(raw_error_code::corrupt_file,
108 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000109
Zachary Turnerd218c262016-07-22 15:46:37 +0000110 if (auto EC =
111 Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize))
Zachary Turner1de49c92016-05-27 18:47:20 +0000112 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000113 if (auto EC = initializeModInfoArray())
114 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000115
Zachary Turner8dbe3622016-05-27 01:54:44 +0000116 if (auto EC = Reader.readStreamRef(SecContrSubstream,
117 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000118 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000119 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000120 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000121 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000122 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000123 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000124 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000125 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000126 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000127 return EC;
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000128 if (auto EC = Reader.readArray(
129 DbgStreams, Header->OptionalDbgHdrSize / sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000130 return EC;
131
Zachary Turner93839cb2016-06-02 05:07:49 +0000132 if (auto EC = initializeSectionContributionData())
133 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000134 if (auto EC = initializeSectionHeadersData())
135 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000136 if (auto EC = initializeSectionMapData())
137 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000138 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000139 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000140 if (auto EC = initializeFpoRecords())
141 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000142
Zachary Turner6ba65de2016-04-29 17:22:58 +0000143 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000144 return make_error<RawError>(raw_error_code::corrupt_file,
145 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000146
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000147 if (ECSubstream.getLength() > 0) {
Zachary Turner120faca2017-02-27 22:11:43 +0000148 BinaryStreamReader ECReader(ECSubstream);
Zachary Turnerc504ae32017-05-03 15:58:37 +0000149 if (auto EC = ECNames.reload(ECReader))
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000150 return EC;
151 }
Zachary Turner0eace0b2016-05-02 18:09:14 +0000152
Zachary Turner819e77d2016-05-06 20:51:57 +0000153 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000154}
155
Zachary Turner2f09b502016-04-29 17:28:47 +0000156PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000157 uint32_t Value = Header->VersionHeader;
158 return static_cast<PdbRaw_DbiVer>(Value);
159}
160
Zachary Turner2f09b502016-04-29 17:28:47 +0000161uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000162
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000163uint16_t DbiStream::getPublicSymbolStreamIndex() const {
164 return Header->PublicSymbolStreamIndex;
165}
166
Zachary Turner96e60f72016-05-24 20:31:48 +0000167uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
168 return Header->GlobalSymbolStreamIndex;
169}
170
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000171uint16_t DbiStream::getFlags() const { return Header->Flags; }
172
Zachary Turner2f09b502016-04-29 17:28:47 +0000173bool DbiStream::isIncrementallyLinked() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000174 return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000175}
176
Zachary Turner2f09b502016-04-29 17:28:47 +0000177bool DbiStream::hasCTypes() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000178 return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000179}
180
Zachary Turner2f09b502016-04-29 17:28:47 +0000181bool DbiStream::isStripped() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000182 return (Header->Flags & DbiFlags::FlagStrippedMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000183}
184
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000185uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
186
Zachary Turner2f09b502016-04-29 17:28:47 +0000187uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000188 return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >>
189 DbiBuildNo::BuildMajorShift;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000190}
191
Zachary Turner2f09b502016-04-29 17:28:47 +0000192uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000193 return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >>
194 DbiBuildNo::BuildMinorShift;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000195}
196
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000197uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
198
Zachary Turner2f09b502016-04-29 17:28:47 +0000199uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000200
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000201uint32_t DbiStream::getSymRecordStreamIndex() const {
202 return Header->SymRecordStreamIndex;
203}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000204
Zachary Turner2f09b502016-04-29 17:28:47 +0000205PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000206 uint16_t Machine = Header->MachineType;
207 return static_cast<PDB_Machine>(Machine);
208}
Zachary Turner1822af542016-04-27 23:41:42 +0000209
Zachary Turner120faca2017-02-27 22:11:43 +0000210FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() {
Rui Ueyama90db7882016-06-02 18:20:20 +0000211 return SectionHeaders;
212}
213
Zachary Turner120faca2017-02-27 22:11:43 +0000214FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000215 return FpoRecords;
216}
217
Zachary Turner2f09b502016-04-29 17:28:47 +0000218ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner120faca2017-02-27 22:11:43 +0000219FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
Zachary Turner93839cb2016-06-02 05:07:49 +0000220 return SectionMap;
221}
222
Eugene Zelenko570e39a2016-11-23 23:16:32 +0000223void DbiStream::visitSectionContributions(
Zachary Turner93839cb2016-06-02 05:07:49 +0000224 ISectionContribVisitor &Visitor) const {
225 if (SectionContribVersion == DbiSecContribVer60) {
226 for (auto &SC : SectionContribs)
227 Visitor.visit(SC);
228 } else if (SectionContribVersion == DbiSecContribV2) {
229 for (auto &SC : SectionContribs2)
230 Visitor.visit(SC);
231 }
232}
233
234Error DbiStream::initializeSectionContributionData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000235 if (SecContrSubstream.getLength() == 0)
236 return Error::success();
237
Zachary Turner120faca2017-02-27 22:11:43 +0000238 BinaryStreamReader SCReader(SecContrSubstream);
Zachary Turner695ed562017-02-28 00:04:07 +0000239 if (auto EC = SCReader.readEnum(SectionContribVersion))
Zachary Turner93839cb2016-06-02 05:07:49 +0000240 return EC;
241
242 if (SectionContribVersion == DbiSecContribVer60)
243 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
244 if (SectionContribVersion == DbiSecContribV2)
245 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
246
247 return make_error<RawError>(raw_error_code::feature_unsupported,
248 "Unsupported DBI Section Contribution version");
249}
250
Zachary Turnerd218c262016-07-22 15:46:37 +0000251Error DbiStream::initializeModInfoArray() {
252 if (ModInfoSubstream.getLength() == 0)
253 return Error::success();
254
Zachary Turner67c56012017-04-27 16:11:19 +0000255 // Since each DbiModuleDescriptor in the stream is a variable length, we have
256 // to iterate
Zachary Turnerd218c262016-07-22 15:46:37 +0000257 // them to know how many there actually are.
Zachary Turner120faca2017-02-27 22:11:43 +0000258 BinaryStreamReader Reader(ModInfoSubstream);
Zachary Turnerd218c262016-07-22 15:46:37 +0000259
Zachary Turner67c56012017-04-27 16:11:19 +0000260 VarStreamArray<DbiModuleDescriptor> ModInfoArray;
Zachary Turnerd218c262016-07-22 15:46:37 +0000261 if (auto EC = Reader.readArray(ModInfoArray, ModInfoSubstream.getLength()))
262 return EC;
263 for (auto &Info : ModInfoArray) {
264 ModuleInfos.emplace_back(Info);
265 }
266
267 return Error::success();
268}
269
Rui Ueyama90db7882016-06-02 18:20:20 +0000270// Initializes this->SectionHeaders.
271Error DbiStream::initializeSectionHeadersData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000272 if (DbgStreams.size() == 0)
273 return Error::success();
274
Rui Ueyama90db7882016-06-02 18:20:20 +0000275 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000276 if (StreamNum >= Pdb.getNumStreams())
277 return make_error<RawError>(raw_error_code::no_stream);
278
Zachary Turnerd66889c2016-07-28 19:12:28 +0000279 auto SHS = MappedBlockStream::createIndexedStream(
280 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum);
Rui Ueyama90db7882016-06-02 18:20:20 +0000281
Zachary Turnerd66889c2016-07-28 19:12:28 +0000282 size_t StreamLen = SHS->getLength();
Rui Ueyama90db7882016-06-02 18:20:20 +0000283 if (StreamLen % sizeof(object::coff_section))
284 return make_error<RawError>(raw_error_code::corrupt_file,
285 "Corrupted section header stream.");
286
287 size_t NumSections = StreamLen / sizeof(object::coff_section);
Zachary Turner120faca2017-02-27 22:11:43 +0000288 BinaryStreamReader Reader(*SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000289 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
290 return make_error<RawError>(raw_error_code::corrupt_file,
291 "Could not read a bitmap.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000292
Zachary Turnerd66889c2016-07-28 19:12:28 +0000293 SectionHeaderStream = std::move(SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000294 return Error::success();
295}
296
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000297// Initializes this->Fpos.
298Error DbiStream::initializeFpoRecords() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000299 if (DbgStreams.size() == 0)
300 return Error::success();
301
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000302 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Reid Kleckner11582c52016-06-17 20:38:01 +0000303
304 // This means there is no FPO data.
Zachary Turnerb383d622016-07-22 15:46:46 +0000305 if (StreamNum == kInvalidStreamIndex)
Reid Kleckner11582c52016-06-17 20:38:01 +0000306 return Error::success();
307
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000308 if (StreamNum >= Pdb.getNumStreams())
309 return make_error<RawError>(raw_error_code::no_stream);
310
Zachary Turnerd66889c2016-07-28 19:12:28 +0000311 auto FS = MappedBlockStream::createIndexedStream(
312 Pdb.getMsfLayout(), Pdb.getMsfBuffer(), StreamNum);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000313
Zachary Turnerd66889c2016-07-28 19:12:28 +0000314 size_t StreamLen = FS->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000315 if (StreamLen % sizeof(object::FpoData))
316 return make_error<RawError>(raw_error_code::corrupt_file,
317 "Corrupted New FPO stream.");
318
319 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turner120faca2017-02-27 22:11:43 +0000320 BinaryStreamReader Reader(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000321 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
322 return make_error<RawError>(raw_error_code::corrupt_file,
323 "Corrupted New FPO stream.");
Zachary Turnerd66889c2016-07-28 19:12:28 +0000324 FpoStream = std::move(FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000325 return Error::success();
326}
327
Zachary Turner93839cb2016-06-02 05:07:49 +0000328Error DbiStream::initializeSectionMapData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000329 if (SecMapSubstream.getLength() == 0)
330 return Error::success();
331
Zachary Turner120faca2017-02-27 22:11:43 +0000332 BinaryStreamReader SMReader(SecMapSubstream);
Zachary Turner93839cb2016-06-02 05:07:49 +0000333 const SecMapHeader *Header;
334 if (auto EC = SMReader.readObject(Header))
335 return EC;
336 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
337 return EC;
338 return Error::success();
339}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000340
Zachary Turner819e77d2016-05-06 20:51:57 +0000341Error DbiStream::initializeFileInfo() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000342 if (FileInfoSubstream.getLength() == 0)
343 return Error::success();
344
Zachary Turner8dbe3622016-05-27 01:54:44 +0000345 const FileInfoSubstreamHeader *FH;
Zachary Turner120faca2017-02-27 22:11:43 +0000346 BinaryStreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000347 if (auto EC = FISR.readObject(FH))
348 return EC;
349
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000350 // The number of modules in the stream should be the same as reported by
351 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000352 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000353 return make_error<RawError>(raw_error_code::corrupt_file,
354 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000355
Zachary Turner93839cb2016-06-02 05:07:49 +0000356 FixedStreamArray<ulittle16_t> ModIndexArray;
357 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000358
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000359 // First is an array of `NumModules` module indices. This is not used for the
360 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
361 // but it's possible there are more than 64k source files, which would imply
362 // more than 64k modules (e.g. object files) as well. So we ignore this
363 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000364 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
365 return EC;
366 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
367 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000368
369 // Compute the real number of source files.
370 uint32_t NumSourceFiles = 0;
371 for (auto Count : ModFileCountArray)
372 NumSourceFiles += Count;
373
374 // This is the array that in the reference implementation corresponds to
Zachary Turner67c56012017-04-27 16:11:19 +0000375 // `DbiModuleDescriptor::FileLayout::FileNameOffs`, which is commented there
376 // as being a
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000377 // pointer. Due to the mentioned problems of pointers causing difficulty
378 // when reading from the file on 64-bit systems, we continue to ignore that
Zachary Turner67c56012017-04-27 16:11:19 +0000379 // field in `DbiModuleDescriptor`, and instead build a vector of StringRefs
380 // and stores
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000381 // them in `ModuleInfoEx`. The value written to and read from the file is
382 // not used anyway, it is only there as a way to store the offsets for the
383 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000384 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
385 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000386
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000387 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000388 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000389
390 // We go through each ModuleInfo, determine the number N of source files for
391 // that module, and then get the next N offsets from the Offsets array, using
392 // them to get the corresponding N names from the Names buffer and associating
393 // each one with the corresponding module.
394 uint32_t NextFileIndex = 0;
395 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
396 uint32_t NumFiles = ModFileCountArray[I];
397 ModuleInfos[I].SourceFiles.resize(NumFiles);
398 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000399 auto ThisName = getFileNameForIndex(NextFileIndex);
400 if (!ThisName)
401 return ThisName.takeError();
402 ModuleInfos[I].SourceFiles[J] = *ThisName;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000403 }
404 }
405
Zachary Turner819e77d2016-05-06 20:51:57 +0000406 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000407}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000408
409uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turnerd218c262016-07-22 15:46:37 +0000410 uint16_t T = static_cast<uint16_t>(Type);
411 if (T >= DbgStreams.size())
Zachary Turnerb383d622016-07-22 15:46:46 +0000412 return kInvalidStreamIndex;
Zachary Turnerd218c262016-07-22 15:46:37 +0000413 return DbgStreams[T];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000414}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000415
416Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
Zachary Turner120faca2017-02-27 22:11:43 +0000417 BinaryStreamReader Names(NamesBuffer);
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000418 if (Index >= FileNameOffsets.size())
419 return make_error<RawError>(raw_error_code::index_out_of_bounds);
420
421 uint32_t FileOffset = FileNameOffsets[Index];
422 Names.setOffset(FileOffset);
423 StringRef Name;
Zachary Turner120faca2017-02-27 22:11:43 +0000424 if (auto EC = Names.readCString(Name))
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000425 return std::move(EC);
426 return Name;
427}