blob: eba09bf06011f8cdfd3101cb31b30c2371a29a89 [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 Turner93839cb2016-06-02 05:07:49 +000014#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000015#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.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 Turner2f09b502016-04-29 17:28:47 +000027using namespace llvm::pdb;
Zachary Turner53a65ba2016-04-26 18:42:34 +000028using namespace llvm::support;
29
30namespace {
31// Some of the values are stored in bitfields. Since this needs to be portable
32// across compilers and architectures (big / little endian in particular) we
33// can't use the actual structures below, but must instead do the shifting
34// and masking ourselves. The struct definitions are provided for reference.
35
36// struct DbiFlags {
37// uint16_t IncrementalLinking : 1; // True if linked incrementally
38// uint16_t IsStripped : 1; // True if private symbols were stripped.
39// uint16_t HasCTypes : 1; // True if linked with /debug:ctypes.
40// uint16_t Reserved : 13;
41//};
42const uint16_t FlagIncrementalMask = 0x0001;
43const uint16_t FlagStrippedMask = 0x0002;
44const uint16_t FlagHasCTypesMask = 0x0004;
45
46// struct DbiBuildNo {
47// uint16_t MinorVersion : 8;
48// uint16_t MajorVersion : 7;
49// uint16_t NewVersionFormat : 1;
50//};
51const uint16_t BuildMinorMask = 0x00FF;
52const uint16_t BuildMinorShift = 0;
53
54const uint16_t BuildMajorMask = 0x7F00;
55const uint16_t BuildMajorShift = 8;
Zachary Turner53a65ba2016-04-26 18:42:34 +000056}
57
Zachary Turner2f09b502016-04-29 17:28:47 +000058struct DbiStream::HeaderInfo {
Zachary Turnerff788aa2016-04-26 19:24:10 +000059 little32_t VersionSignature;
Zachary Turner53a65ba2016-04-26 18:42:34 +000060 ulittle32_t VersionHeader;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000061 ulittle32_t Age; // Should match InfoStream.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000062 ulittle16_t GlobalSymbolStreamIndex; // Global symbol stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000063 ulittle16_t BuildNumber; // See DbiBuildNo structure.
Rui Ueyama0376b1a2016-05-19 18:05:58 +000064 ulittle16_t PublicSymbolStreamIndex; // Public symbols stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000065 ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
Rui Ueyama0376b1a2016-05-19 18:05:58 +000066 ulittle16_t SymRecordStreamIndex; // Symbol records stream #
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000067 ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll
68 little32_t ModiSubstreamSize; // Size of module info stream
69 little32_t SecContrSubstreamSize; // Size of sec. contribution stream
70 little32_t SectionMapSize; // Size of sec. map substream
71 little32_t FileInfoSize; // Size of file info substream
Zachary Turner2f09b502016-04-29 17:28:47 +000072 little32_t TypeServerSize; // Size of type server map
73 ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
74 little32_t OptionalDbgHdrSize; // Size of DbgHeader info
75 little32_t ECSubstreamSize; // Size of EC stream (what is EC?)
76 ulittle16_t Flags; // See DbiFlags enum.
77 ulittle16_t MachineType; // See PDB_MachineType enum.
Zachary Turner53a65ba2016-04-26 18:42:34 +000078
79 ulittle32_t Reserved; // Pad to 64 bytes
80};
81
Zachary Turner93839cb2016-06-02 05:07:49 +000082template <typename ContribType>
83Error loadSectionContribs(FixedStreamArray<ContribType> &Output,
84 StreamReader &Reader) {
85 if (Reader.bytesRemaining() % sizeof(ContribType) != 0)
86 return make_error<RawError>(
87 raw_error_code::corrupt_file,
88 "Invalid number of bytes of section contributions");
89
90 uint32_t Count = Reader.bytesRemaining() / sizeof(ContribType);
91 if (auto EC = Reader.readArray(Output, Count))
92 return EC;
93 return Error::success();
94}
95
Zachary Turnera1657a92016-06-08 17:26:39 +000096DbiStream::DbiStream(PDBFile &File, std::unique_ptr<MappedBlockStream> Stream)
97 : Pdb(File), Stream(std::move(Stream)), Header(nullptr) {
Zachary Turner53a65ba2016-04-26 18:42:34 +000098 static_assert(sizeof(HeaderInfo) == 64, "Invalid HeaderInfo size!");
99}
100
Zachary Turner2f09b502016-04-29 17:28:47 +0000101DbiStream::~DbiStream() {}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000102
Zachary Turner819e77d2016-05-06 20:51:57 +0000103Error DbiStream::reload() {
Zachary Turnera1657a92016-06-08 17:26:39 +0000104 StreamReader Reader(*Stream);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000105
Zachary Turnera1657a92016-06-08 17:26:39 +0000106 if (Stream->getLength() < sizeof(HeaderInfo))
Zachary Turner819e77d2016-05-06 20:51:57 +0000107 return make_error<RawError>(raw_error_code::corrupt_file,
108 "DBI Stream does not contain a header.");
Zachary Turner8dbe3622016-05-27 01:54:44 +0000109 if (auto EC = Reader.readObject(Header))
Zachary Turner819e77d2016-05-06 20:51:57 +0000110 return make_error<RawError>(raw_error_code::corrupt_file,
111 "DBI Stream does not contain a header.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000112
113 if (Header->VersionSignature != -1)
Zachary Turner819e77d2016-05-06 20:51:57 +0000114 return make_error<RawError>(raw_error_code::corrupt_file,
115 "Invalid DBI version signature.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000116
Zachary Turner1822af542016-04-27 23:41:42 +0000117 // Require at least version 7, which should be present in all PDBs
118 // produced in the last decade and allows us to avoid having to
119 // special case all kinds of complicated arcane formats.
120 if (Header->VersionHeader < PdbDbiV70)
Zachary Turner93839cb2016-06-02 05:07:49 +0000121 return make_error<RawError>(raw_error_code::feature_unsupported,
Zachary Turner819e77d2016-05-06 20:51:57 +0000122 "Unsupported DBI version.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000123
Zachary Turnera1657a92016-06-08 17:26:39 +0000124 auto IS = Pdb.getPDBInfoStream();
125 if (!IS)
126 return IS.takeError();
Zachary Turner819e77d2016-05-06 20:51:57 +0000127
Zachary Turnera1657a92016-06-08 17:26:39 +0000128 if (Header->Age != IS->getAge())
Zachary Turner819e77d2016-05-06 20:51:57 +0000129 return make_error<RawError>(raw_error_code::corrupt_file,
130 "DBI Age does not match PDB Age.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000131
Zachary Turnera1657a92016-06-08 17:26:39 +0000132 if (Stream->getLength() !=
Zachary Turner53a65ba2016-04-26 18:42:34 +0000133 sizeof(HeaderInfo) + Header->ModiSubstreamSize +
134 Header->SecContrSubstreamSize + Header->SectionMapSize +
135 Header->FileInfoSize + Header->TypeServerSize +
136 Header->OptionalDbgHdrSize + Header->ECSubstreamSize)
Zachary Turner819e77d2016-05-06 20:51:57 +0000137 return make_error<RawError>(raw_error_code::corrupt_file,
138 "DBI Length does not equal sum of substreams.");
Zachary Turner53a65ba2016-04-26 18:42:34 +0000139
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000140 // Only certain substreams are guaranteed to be aligned. Validate
141 // them here.
Zachary Turner1822af542016-04-27 23:41:42 +0000142 if (Header->ModiSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000143 return make_error<RawError>(raw_error_code::corrupt_file,
144 "DBI MODI substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000145 if (Header->SecContrSubstreamSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000146 return make_error<RawError>(
147 raw_error_code::corrupt_file,
148 "DBI section contribution substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000149 if (Header->SectionMapSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000150 return make_error<RawError>(raw_error_code::corrupt_file,
151 "DBI section map substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000152 if (Header->FileInfoSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000153 return make_error<RawError>(raw_error_code::corrupt_file,
154 "DBI file info substream not aligned.");
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000155 if (Header->TypeServerSize % sizeof(uint32_t) != 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000156 return make_error<RawError>(raw_error_code::corrupt_file,
157 "DBI type server substream not aligned.");
Zachary Turner1822af542016-04-27 23:41:42 +0000158
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000159 // Since each ModInfo in the stream is a variable length, we have to iterate
160 // them to know how many there actually are.
Zachary Turner93839cb2016-06-02 05:07:49 +0000161 VarStreamArray<ModInfo> ModInfoArray;
Zachary Turner1de49c92016-05-27 18:47:20 +0000162 if (auto EC = Reader.readArray(ModInfoArray, Header->ModiSubstreamSize))
163 return EC;
164 for (auto &Info : ModInfoArray) {
Zachary Turner8dbe3622016-05-27 01:54:44 +0000165 ModuleInfos.emplace_back(Info);
166 }
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000167
Zachary Turner8dbe3622016-05-27 01:54:44 +0000168 if (auto EC = Reader.readStreamRef(SecContrSubstream,
169 Header->SecContrSubstreamSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000170 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000171 if (auto EC = Reader.readStreamRef(SecMapSubstream, Header->SectionMapSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000172 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000173 if (auto EC = Reader.readStreamRef(FileInfoSubstream, Header->FileInfoSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000174 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000175 if (auto EC =
Zachary Turner8dbe3622016-05-27 01:54:44 +0000176 Reader.readStreamRef(TypeServerMapSubstream, Header->TypeServerSize))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000177 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000178 if (auto EC = Reader.readStreamRef(ECSubstream, Header->ECSubstreamSize))
Zachary Turner6ba65de2016-04-29 17:22:58 +0000179 return EC;
Zachary Turner8dbe3622016-05-27 01:54:44 +0000180 if (auto EC = Reader.readArray(DbgStreams, Header->OptionalDbgHdrSize /
181 sizeof(ulittle16_t)))
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000182 return EC;
183
Zachary Turner93839cb2016-06-02 05:07:49 +0000184 if (auto EC = initializeSectionContributionData())
185 return EC;
Rui Ueyama90db7882016-06-02 18:20:20 +0000186 if (auto EC = initializeSectionHeadersData())
187 return EC;
Zachary Turner93839cb2016-06-02 05:07:49 +0000188 if (auto EC = initializeSectionMapData())
189 return EC;
Zachary Turner819e77d2016-05-06 20:51:57 +0000190 if (auto EC = initializeFileInfo())
Zachary Turner1822af542016-04-27 23:41:42 +0000191 return EC;
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000192 if (auto EC = initializeFpoRecords())
193 return EC;
Zachary Turner1822af542016-04-27 23:41:42 +0000194
Zachary Turner6ba65de2016-04-29 17:22:58 +0000195 if (Reader.bytesRemaining() > 0)
Zachary Turner819e77d2016-05-06 20:51:57 +0000196 return make_error<RawError>(raw_error_code::corrupt_file,
197 "Found unexpected bytes in DBI Stream.");
Zachary Turner6ba65de2016-04-29 17:22:58 +0000198
Zachary Turner93839cb2016-06-02 05:07:49 +0000199 StreamReader ECReader(ECSubstream);
Zachary Turner819e77d2016-05-06 20:51:57 +0000200 if (auto EC = ECNames.load(ECReader))
201 return EC;
Zachary Turner0eace0b2016-05-02 18:09:14 +0000202
Zachary Turner819e77d2016-05-06 20:51:57 +0000203 return Error::success();
Zachary Turner53a65ba2016-04-26 18:42:34 +0000204}
205
Zachary Turner2f09b502016-04-29 17:28:47 +0000206PdbRaw_DbiVer DbiStream::getDbiVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000207 uint32_t Value = Header->VersionHeader;
208 return static_cast<PdbRaw_DbiVer>(Value);
209}
210
Zachary Turner2f09b502016-04-29 17:28:47 +0000211uint32_t DbiStream::getAge() const { return Header->Age; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000212
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000213uint16_t DbiStream::getPublicSymbolStreamIndex() const {
214 return Header->PublicSymbolStreamIndex;
215}
216
Zachary Turner96e60f72016-05-24 20:31:48 +0000217uint16_t DbiStream::getGlobalSymbolStreamIndex() const {
218 return Header->GlobalSymbolStreamIndex;
219}
220
Zachary Turner2f09b502016-04-29 17:28:47 +0000221bool DbiStream::isIncrementallyLinked() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000222 return (Header->Flags & FlagIncrementalMask) != 0;
223}
224
Zachary Turner2f09b502016-04-29 17:28:47 +0000225bool DbiStream::hasCTypes() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000226 return (Header->Flags & FlagHasCTypesMask) != 0;
227}
228
Zachary Turner2f09b502016-04-29 17:28:47 +0000229bool DbiStream::isStripped() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000230 return (Header->Flags & FlagStrippedMask) != 0;
231}
232
Zachary Turner2f09b502016-04-29 17:28:47 +0000233uint16_t DbiStream::getBuildMajorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000234 return (Header->BuildNumber & BuildMajorMask) >> BuildMajorShift;
235}
236
Zachary Turner2f09b502016-04-29 17:28:47 +0000237uint16_t DbiStream::getBuildMinorVersion() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000238 return (Header->BuildNumber & BuildMinorMask) >> BuildMinorShift;
239}
240
Zachary Turner2f09b502016-04-29 17:28:47 +0000241uint32_t DbiStream::getPdbDllVersion() const { return Header->PdbDllVersion; }
Zachary Turner53a65ba2016-04-26 18:42:34 +0000242
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000243uint32_t DbiStream::getSymRecordStreamIndex() const {
244 return Header->SymRecordStreamIndex;
245}
Zachary Turner53a65ba2016-04-26 18:42:34 +0000246
Zachary Turner2f09b502016-04-29 17:28:47 +0000247PDB_Machine DbiStream::getMachineType() const {
Zachary Turner53a65ba2016-04-26 18:42:34 +0000248 uint16_t Machine = Header->MachineType;
249 return static_cast<PDB_Machine>(Machine);
250}
Zachary Turner1822af542016-04-27 23:41:42 +0000251
Rui Ueyama90db7882016-06-02 18:20:20 +0000252codeview::FixedStreamArray<object::coff_section>
253DbiStream::getSectionHeaders() {
254 return SectionHeaders;
255}
256
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000257codeview::FixedStreamArray<object::FpoData> DbiStream::getFpoRecords() {
258 return FpoRecords;
259}
260
Zachary Turner2f09b502016-04-29 17:28:47 +0000261ArrayRef<ModuleInfoEx> DbiStream::modules() const { return ModuleInfos; }
Zachary Turner93839cb2016-06-02 05:07:49 +0000262codeview::FixedStreamArray<SecMapEntry> DbiStream::getSectionMap() const {
263 return SectionMap;
264}
265
266void llvm::pdb::DbiStream::visitSectionContributions(
267 ISectionContribVisitor &Visitor) const {
268 if (SectionContribVersion == DbiSecContribVer60) {
269 for (auto &SC : SectionContribs)
270 Visitor.visit(SC);
271 } else if (SectionContribVersion == DbiSecContribV2) {
272 for (auto &SC : SectionContribs2)
273 Visitor.visit(SC);
274 }
275}
276
277Error DbiStream::initializeSectionContributionData() {
278 StreamReader SCReader(SecContrSubstream);
279 if (auto EC = SCReader.readEnum(SectionContribVersion))
280 return EC;
281
282 if (SectionContribVersion == DbiSecContribVer60)
283 return loadSectionContribs<SectionContrib>(SectionContribs, SCReader);
284 if (SectionContribVersion == DbiSecContribV2)
285 return loadSectionContribs<SectionContrib2>(SectionContribs2, SCReader);
286
287 return make_error<RawError>(raw_error_code::feature_unsupported,
288 "Unsupported DBI Section Contribution version");
289}
290
Rui Ueyama90db7882016-06-02 18:20:20 +0000291// Initializes this->SectionHeaders.
292Error DbiStream::initializeSectionHeadersData() {
293 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() {
318 uint32_t StreamNum = getDebugStreamIndex(DbgHeaderType::NewFPO);
Reid Kleckner11582c52016-06-17 20:38:01 +0000319
320 // This means there is no FPO data.
321 if (StreamNum == InvalidStreamIndex)
322 return Error::success();
323
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000324 if (StreamNum >= Pdb.getNumStreams())
325 return make_error<RawError>(raw_error_code::no_stream);
326
Zachary Turnera1657a92016-06-08 17:26:39 +0000327 auto FS = MappedBlockStream::createIndexedStream(StreamNum, Pdb);
328 if (!FS)
329 return FS.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000330
Zachary Turnera1657a92016-06-08 17:26:39 +0000331 size_t StreamLen = (*FS)->getLength();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000332 if (StreamLen % sizeof(object::FpoData))
333 return make_error<RawError>(raw_error_code::corrupt_file,
334 "Corrupted New FPO stream.");
335
336 size_t NumRecords = StreamLen / sizeof(object::FpoData);
Zachary Turnera1657a92016-06-08 17:26:39 +0000337 codeview::StreamReader Reader(**FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000338 if (auto EC = Reader.readArray(FpoRecords, NumRecords))
339 return make_error<RawError>(raw_error_code::corrupt_file,
340 "Corrupted New FPO stream.");
Zachary Turnera1657a92016-06-08 17:26:39 +0000341 FpoStream = std::move(*FS);
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000342 return Error::success();
343}
344
Zachary Turner93839cb2016-06-02 05:07:49 +0000345Error DbiStream::initializeSectionMapData() {
346 StreamReader SMReader(SecMapSubstream);
347 const SecMapHeader *Header;
348 if (auto EC = SMReader.readObject(Header))
349 return EC;
350 if (auto EC = SMReader.readArray(SectionMap, Header->SecCount))
351 return EC;
352 return Error::success();
353}
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000354
Zachary Turner819e77d2016-05-06 20:51:57 +0000355Error DbiStream::initializeFileInfo() {
Zachary Turner84c3a8b2016-04-28 20:05:18 +0000356 struct FileInfoSubstreamHeader {
357 ulittle16_t NumModules; // Total # of modules, should match number of
358 // records in the ModuleInfo substream.
359 ulittle16_t NumSourceFiles; // Total # of source files. This value is not
360 // accurate because PDB actually supports more
361 // than 64k source files, so we ignore it and
362 // compute the value from other stream fields.
363 };
364
365 // The layout of the FileInfoSubstream is like this:
366 // struct {
367 // ulittle16_t NumModules;
368 // ulittle16_t NumSourceFiles;
369 // ulittle16_t ModIndices[NumModules];
370 // ulittle16_t ModFileCounts[NumModules];
371 // ulittle32_t FileNameOffsets[NumSourceFiles];
372 // char Names[][NumSourceFiles];
373 // };
374 // with the caveat that `NumSourceFiles` cannot be trusted, so
375 // it is computed by summing `ModFileCounts`.
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}