blob: 1bad0009c3711804de9df2d4809d9d31f864d564 [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 Turnerbac69d32016-07-22 19:56:05 +000012#include "llvm/DebugInfo/Msf/IndexedStreamData.h"
13#include "llvm/DebugInfo/Msf/StreamArray.h"
14#include "llvm/DebugInfo/Msf/StreamReader.h"
15#include "llvm/DebugInfo/Msf/StreamWriter.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000016#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.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 Turnerbac69d32016-07-22 19:56:05 +000028using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000029using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000030using namespace llvm::support;
31
Zachary Turner93839cb2016-06-02 05:07:49 +000032template <typename ContribType>
Benjamin Kramer4d098922016-07-10 11:28:51 +000033static Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
34 StreamReader &Reader) {
Zachary Turner93839cb2016-06-02 05:07:49 +000035 if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
36 return make_error<RawError>(
37 raw_error_code::corrupt_file,
38 "Invalid number of bytes of section contributions");
39
40 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
41 if (auto EC = Reader.readArray(Output, Count))
42 return EC;
43 return Error::success();
44}
45
Zachary Turnera1657a92016-06-08 17:26:39 +000046DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
47 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000048}
49
Zachary Turner2f09b502016-04-29 17:28:47 +000050DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +000051
Zachary Turner819e77d2016-05-06 20:51:57 +000052Error DbiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +000053 StreamReader Reader(*Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +000054
Zachary Turnerb383d622016-07-22 15:46:46 +000055 if (Stream->getLength() < sizeof(DbiStreamHeader))
Zachary Turner819e77d2016-05-06 20:51:57 +000056 return make_error<RawError>(raw_error_code::corrupt_file,
57 "DBI Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +000058 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +000059 return make_error<RawError>(raw_error_code::corrupt_file,
60 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000061
62 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +000063 return make_error<RawError>(raw_error_code::corrupt_file,
64 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000065
Zachary Turner1822af542016-04-27 23:41:42 +000066 // Require at least version 7, which should be present in all PDBs
67 // produced in the last decade and allows us to avoid having to
68 // special case all kinds of complicated arcane formats.
69 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner93839cb2016-06-02 05:07:49 +000070 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +000071 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000072
Zachary Turnera1657a92016-06-08 17:26:39 +000073 auto IS = Pdb.getPDBInfoStream();
74 if (!IS)
75 return IS.takeError();
Zachary Turner819e77d2016-05-06 20:51:57 +000076
Zachary Turnera1657a92016-06-08 17:26:39 +000077 if (Header->Age != IS->getAge())
Zachary Turner819e77d2016-05-06 20:51:57 +000078 return make_error<RawError>(raw_error_code::corrupt_file,
79 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000080
Zachary Turnera1657a92016-06-08 17:26:39 +000081 if (Stream->getLength() !=
Zachary Turnerb383d622016-07-22 15:46:46 +000082 sizeof(DbiStreamHeader) + Header->ModiSubstreamSize +
Zachary Turner53a65ba2016-04-26 18:42:34 +000083 Header->SecContrSubstreamSize + Header->SectionMapSize +
84 Header->FileInfoSize + Header->TypeServerSize +
85 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +000086 return make_error<RawError>(raw_error_code::corrupt_file,
87 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +000088
Zachary Turner84c3a8b2016-04-28 20:05:18 +000089 // Only certain substreams are guaranteed to be aligned. Validate
90 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +000091 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000092 return make_error<RawError>(raw_error_code::corrupt_file,
93 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +000094 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000095 return make_error<RawError>(
96 raw_error_code::corrupt_file,
97 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +000098 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +000099 return make_error<RawError>(raw_error_code::corrupt_file,
100 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000101 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000102 return make_error<RawError>(raw_error_code::corrupt_file,
103 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000104 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000105 return make_error<RawError>(raw_error_code::corrupt_file,
106 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000107
Zachary Turnerd218c262016-07-22 15:46:37 +0000108 if (auto EC =
109 Reader.readStreamRef(ModInfoSubstream, Header->ModiSubstreamSize))
Zachary Turner1de49c92016-05-27 18:47:20 +0000110 return EC;
Zachary Turnerd218c262016-07-22 15:46:37 +0000111 if (auto EC = initializeModInfoArray())
112 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000113
Zachary Turner8dbe3622016-05-27 01:54:44 +0000114 if (auto EC = Reader.readStreamRef(SecContrSubstream,
115 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000116 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000117 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000118 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000119 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000120 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000121 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000122 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000123 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000124 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000125 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000126 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
127 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000128 return EC;
129
Zachary Turner93839cb2016-06-02 05:07:49 +0000130 if (auto EC = initializeSectionContributionData())
131 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000132 if (auto EC = initializeSectionHeadersData())
133 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000134 if (auto EC = initializeSectionMapData())
135 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000136 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000137 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000138 if (auto EC = initializeFpoRecords())
139 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000140
Zachary Turner6ba65de2016-04-29 17:22:58 +0000141 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000142 return make_error<RawError>(raw_error_code::corrupt_file,
143 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000144
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000145 if (ECSubstream.getLength() > 0) {
146 StreamReader ECReader(ECSubstream);
147 if (auto EC = ECNames.load(ECReader))
148 return EC;
149 }
Zachary Turner0eace0b2016-05-02 18:09:14 +0000150
Zachary Turner819e77d2016-05-06 20:51:57 +0000151 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000152}
153
Zachary Turner2f09b502016-04-29 17:28:47 +0000154PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000155 uint32_t Value = Header->VersionHeader;
156 return static_cast<PdbRaw_DbiVer>(Value);
157}
158
Zachary Turner2f09b502016-04-29 17:28:47 +0000159uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000160
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000161uint16_t DbiStream::getPublicSymbolStreamIndex() const {
162 return Header->PublicSymbolStreamIndex;
163}
164
Zachary Turner96e60f72016-05-24 20:31:48 +0000165uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
166 return Header->GlobalSymbolStreamIndex;
167}
168
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000169uint16_t DbiStream::getFlags() const { return Header->Flags; }
170
Zachary Turner2f09b502016-04-29 17:28:47 +0000171bool DbiStream::isIncrementallyLinked() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000172 return (Header->Flags & DbiFlags::FlagIncrementalMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000173}
174
Zachary Turner2f09b502016-04-29 17:28:47 +0000175bool DbiStream::hasCTypes() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000176 return (Header->Flags & DbiFlags::FlagHasCTypesMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000177}
178
Zachary Turner2f09b502016-04-29 17:28:47 +0000179bool DbiStream::isStripped() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000180 return (Header->Flags & DbiFlags::FlagStrippedMask) != 0;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000181}
182
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000183uint16_t DbiStream::getBuildNumber() const { return Header->BuildNumber; }
184
Zachary Turner2f09b502016-04-29 17:28:47 +0000185uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000186 return (Header->BuildNumber & DbiBuildNo::BuildMajorMask) >>
187 DbiBuildNo::BuildMajorShift;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000188}
189
Zachary Turner2f09b502016-04-29 17:28:47 +0000190uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turnerb383d622016-07-22 15:46:46 +0000191 return (Header->BuildNumber & DbiBuildNo::BuildMinorMask) >>
192 DbiBuildNo::BuildMinorShift;
Zachary Turner53a65ba2016-04-26 18:42:34 +0000193}
194
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000195uint16_t DbiStream::getPdbDllRbld() const { return Header->PdbDllRbld; }
196
Zachary Turner2f09b502016-04-29 17:28:47 +0000197uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000198
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000199uint32_t DbiStream::getSymRecordStreamIndex() const {
200 return Header->SymRecordStreamIndex;
201}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000202
Zachary Turner2f09b502016-04-29 17:28:47 +0000203PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000204 uint16_t Machine = Header->MachineType;
205 return static_cast<PDB_Machine>(Machine);
206}
Zachary Turner1822af542016-04-27 23:41:42 +0000207
Zachary Turnerbac69d32016-07-22 19:56:05 +0000208msf::FixedStreamArray<object::coff_section> DbiStream::getSectionHeaders() {
Rui Ueyama90db7882016-06-02 18:20:20 +0000209 return SectionHeaders;
210}
211
Zachary Turnerbac69d32016-07-22 19:56:05 +0000212msf::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000213 return FpoRecords;
214}
215
Zachary Turner2f09b502016-04-29 17:28:47 +0000216ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turnerbac69d32016-07-22 19:56:05 +0000217msf::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
Zachary Turner93839cb2016-06-02 05:07:49 +0000218 return SectionMap;
219}
220
221void llvm::pdb::DbiStream::visitSectionContributions(
222 ISectionContribVisitor &Visitor) const {
223 if (SectionContribVersion == DbiSecContribVer60) {
224 for (auto &SC : SectionContribs)
225 Visitor.visit(SC);
226 } else if (SectionContribVersion == DbiSecContribV2) {
227 for (auto &SC : SectionContribs2)
228 Visitor.visit(SC);
229 }
230}
231
232Error DbiStream::initializeSectionContributionData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000233 if (SecContrSubstream.getLength() == 0)
234 return Error::success();
235
Zachary Turner93839cb2016-06-02 05:07:49 +0000236 StreamReader SCReader(SecContrSubstream);
237 if (auto EC = SCReader.readEnum(SectionContribVersion))
238 return EC;
239
240 if (SectionContribVersion == DbiSecContribVer60)
241 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
242 if (SectionContribVersion == DbiSecContribV2)
243 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
244
245 return make_error<RawError>(raw_error_code::feature_unsupported,
246 "Unsupported DBI Section Contribution version");
247}
248
Zachary Turnerd218c262016-07-22 15:46:37 +0000249Error DbiStream::initializeModInfoArray() {
250 if (ModInfoSubstream.getLength() == 0)
251 return Error::success();
252
253 // Since each ModInfo in the stream is a variable length, we have to iterate
254 // them to know how many there actually are.
255 StreamReader Reader(ModInfoSubstream);
256
257 VarStreamArray<ModInfo> ModInfoArray;
258 if (auto EC = Reader.readArray(ModInfoArray, ModInfoSubstream.getLength()))
259 return EC;
260 for (auto &Info : ModInfoArray) {
261 ModuleInfos.emplace_back(Info);
262 }
263
264 return Error::success();
265}
266
Rui Ueyama90db7882016-06-02 18:20:20 +0000267// Initializes this->SectionHeaders.
268Error DbiStream::initializeSectionHeadersData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000269 if (DbgStreams.size() == 0)
270 return Error::success();
271
Rui Ueyama90db7882016-06-02 18:20:20 +0000272 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::SectionHdr);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000273 if (StreamNum >= Pdb.getNumStreams())
274 return make_error<RawError>(raw_error_code::no_stream);
275
Zachary Turnera1657a92016-06-08 17:26:39 +0000276 auto SHS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
277 if (!SHS)
278 return SHS.takeError();
Rui Ueyama90db7882016-06-02 18:20:20 +0000279
Zachary Turnera1657a92016-06-08 17:26:39 +0000280 size_t StreamLen = (*SHS)->getLength();
Rui Ueyama90db7882016-06-02 18:20:20 +0000281 if (StreamLen % sizeof(object::coff_section))
282 return make_error<RawError>(raw_error_code::corrupt_file,
283 "Corrupted section header stream.");
284
285 size_t NumSections = StreamLen / sizeof(object::coff_section);
Zachary Turnerbac69d32016-07-22 19:56:05 +0000286 msf::StreamReader Reader(**SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000287 if (auto EC = Reader.readArray(SectionHeaders, NumSections))
288 return make_error<RawError>(raw_error_code::corrupt_file,
289 "Could not read a bitmap.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000290
291 SectionHeaderStream = std::move(*SHS);
Rui Ueyama90db7882016-06-02 18:20:20 +0000292 return Error::success();
293}
294
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000295// Initializes this->Fpos.
296Error DbiStream::initializeFpoRecords() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000297 if (DbgStreams.size() == 0)
298 return Error::success();
299
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000300 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Reid Kleckner11582c52016-06-17 20:38:01 +0000301
302 // This means there is no FPO data.
Zachary Turnerb383d622016-07-22 15:46:46 +0000303 if (StreamNum == kInvalidStreamIndex)
Reid Kleckner11582c52016-06-17 20:38:01 +0000304 return Error::success();
305
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000306 if (StreamNum >= Pdb.getNumStreams())
307 return make_error<RawError>(raw_error_code::no_stream);
308
Zachary Turnera1657a92016-06-08 17:26:39 +0000309 auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
310 if (!FS)
311 return FS.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000312
Zachary Turnera1657a92016-06-08 17:26:39 +0000313 size_t StreamLen = (*FS)->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000314 if (StreamLen % sizeof(object::FpoData))
315 return make_error<RawError>(raw_error_code::corrupt_file,
316 "Corrupted New FPO stream.");
317
318 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turnerbac69d32016-07-22 19:56:05 +0000319 msf::StreamReader Reader(**FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000320 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
321 return make_error<RawError>(raw_error_code::corrupt_file,
322 "Corrupted New FPO stream.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000323 FpoStream = std::move(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000324 return Error::success();
325}
326
Zachary Turner93839cb2016-06-02 05:07:49 +0000327Error DbiStream::initializeSectionMapData() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000328 if (SecMapSubstream.getLength() == 0)
329 return Error::success();
330
Zachary Turner93839cb2016-06-02 05:07:49 +0000331 StreamReader SMReader(SecMapSubstream);
332 const SecMapHeader *Header;
333 if (auto EC = SMReader.readObject(Header))
334 return EC;
335 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
336 return EC;
337 return Error::success();
338}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000339
Zachary Turner819e77d2016-05-06 20:51:57 +0000340Error DbiStream::initializeFileInfo() {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000341 if (FileInfoSubstream.getLength() == 0)
342 return Error::success();
343
Zachary Turner8dbe3622016-05-27 01:54:44 +0000344 const FileInfoSubstreamHeader *FH;
Zachary Turner93839cb2016-06-02 05:07:49 +0000345 StreamReader FISR(FileInfoSubstream);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000346 if (auto EC = FISR.readObject(FH))
347 return EC;
348
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000349 // The number of modules in the stream should be the same as reported by
350 // the FileInfoSubstreamHeader.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000351 if (FH->NumModules != ModuleInfos.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000352 return make_error<RawError>(raw_error_code::corrupt_file,
353 "FileInfo substream count doesn't match DBI.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000354
Zachary Turner93839cb2016-06-02 05:07:49 +0000355 FixedStreamArray<ulittle16_t> ModIndexArray;
356 FixedStreamArray<ulittle16_t> ModFileCountArray;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000357
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000358 // First is an array of `NumModules` module indices. This is not used for the
359 // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
360 // but it's possible there are more than 64k source files, which would imply
361 // more than 64k modules (e.g. object files) as well. So we ignore this
362 // field.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000363 if (auto EC = FISR.readArray(ModIndexArray, ModuleInfos.size()))
364 return EC;
365 if (auto EC = FISR.readArray(ModFileCountArray, ModuleInfos.size()))
366 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000367
368 // Compute the real number of source files.
369 uint32_t NumSourceFiles = 0;
370 for (auto Count : ModFileCountArray)
371 NumSourceFiles += Count;
372
373 // This is the array that in the reference implementation corresponds to
374 // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
375 // pointer. Due to the mentioned problems of pointers causing difficulty
376 // when reading from the file on 64-bit systems, we continue to ignore that
377 // field in `ModInfo`, and instead build a vector of StringRefs and stores
378 // them in `ModuleInfoEx`. The value written to and read from the file is
379 // not used anyway, it is only there as a way to store the offsets for the
380 // purposes of later accessing the names at runtime.
Zachary Turner8dbe3622016-05-27 01:54:44 +0000381 if (auto EC = FISR.readArray(FileNameOffsets, NumSourceFiles))
382 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000383
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000384 if (auto EC = FISR.readStreamRef(NamesBuffer))
Zachary Turner8dbe3622016-05-27 01:54:44 +0000385 return EC;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000386
387 // We go through each ModuleInfo, determine the number N of source files for
388 // that module, and then get the next N offsets from the Offsets array, using
389 // them to get the corresponding N names from the Names buffer and associating
390 // each one with the corresponding module.
391 uint32_t NextFileIndex = 0;
392 for (size_t I = 0; I < ModuleInfos.size(); ++I) {
393 uint32_t NumFiles = ModFileCountArray[I];
394 ModuleInfos[I].SourceFiles.resize(NumFiles);
395 for (size_t J = 0; J < NumFiles; ++J, ++NextFileIndex) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000396 auto ThisName = getFileNameForIndex(NextFileIndex);
397 if (!ThisName)
398 return ThisName.takeError();
399 ModuleInfos[I].SourceFiles[J] = *ThisName;
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000400 }
401 }
402
Zachary Turner819e77d2016-05-06 20:51:57 +0000403 return Error::success();
Zachary Turner1822af542016-04-27 23:41:42 +0000404}
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000405
406uint32_t DbiStream::getDebugStreamIndex(DbgHeaderType Type) const {
Zachary Turnerd218c262016-07-22 15:46:37 +0000407 uint16_t T = static_cast<uint16_t>(Type);
408 if (T >= DbgStreams.size())
Zachary Turnerb383d622016-07-22 15:46:46 +0000409 return kInvalidStreamIndex;
Zachary Turnerd218c262016-07-22 15:46:37 +0000410 return DbgStreams[T];
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000411}
Zachary Turner3df1bfa2016-06-03 05:52:57 +0000412
413Expected<StringRef> DbiStream::getFileNameForIndex(uint32_t Index) const {
414 StreamReader Names(NamesBuffer);
415 if (Index >= FileNameOffsets.size())
416 return make_error<RawError>(raw_error_code::index_out_of_bounds);
417
418 uint32_t FileOffset = FileNameOffsets[Index];
419 Names.setOffset(FileOffset);
420 StringRef Name;
421 if (auto EC = Names.readZeroString(Name))
422 return std::move(EC);
423 return Name;
424}
Zachary Turner8848a7a2016-07-06 18:05:57 +0000425
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000426Error DbiStream::commit() {
427 StreamWriter Writer(*Stream);
428 if (auto EC = Writer.writeObject(*Header))
429 return EC;
430
Zachary Turnerd218c262016-07-22 15:46:37 +0000431 if (auto EC = Writer.writeStreamRef(ModInfoSubstream))
432 return EC;
433
434 if (auto EC = Writer.writeStreamRef(SecContrSubstream,
435 SecContrSubstream.getLength()))
436 return EC;
437 if (auto EC =
438 Writer.writeStreamRef(SecMapSubstream, SecMapSubstream.getLength()))
439 return EC;
440 if (auto EC = Writer.writeStreamRef(FileInfoSubstream,
441 FileInfoSubstream.getLength()))
442 return EC;
443 if (auto EC = Writer.writeStreamRef(TypeServerMapSubstream,
444 TypeServerMapSubstream.getLength()))
445 return EC;
446 if (auto EC = Writer.writeStreamRef(ECSubstream, ECSubstream.getLength()))
447 return EC;
448
449 if (Writer.bytesRemaining() > 0)
450 return make_error<RawError>(raw_error_code::invalid_format,
451 "Unexpected bytes found in DBI Stream");
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000452 return Error::success();
453}