1*0b57cec5SDimitry Andric //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
11*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
12*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
13*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
14*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
15*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
16*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
17*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
18*0b57cec5SDimitry Andric #include "llvm/DebugInfo/PDB/Native/RawError.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamRef.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
22*0b57cec5SDimitry Andric #include <algorithm>
23*0b57cec5SDimitry Andric #include <cstdint>
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric using namespace llvm;
26*0b57cec5SDimitry Andric using namespace llvm::codeview;
27*0b57cec5SDimitry Andric using namespace llvm::msf;
28*0b57cec5SDimitry Andric using namespace llvm::pdb;
29*0b57cec5SDimitry Andric 
ModuleDebugStreamRef(const DbiModuleDescriptor & Module,std::unique_ptr<MappedBlockStream> Stream)30*0b57cec5SDimitry Andric ModuleDebugStreamRef::ModuleDebugStreamRef(
31*0b57cec5SDimitry Andric     const DbiModuleDescriptor &Module,
32*0b57cec5SDimitry Andric     std::unique_ptr<MappedBlockStream> Stream)
33*0b57cec5SDimitry Andric     : Mod(Module), Stream(std::move(Stream)) {}
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
36*0b57cec5SDimitry Andric 
reload()37*0b57cec5SDimitry Andric Error ModuleDebugStreamRef::reload() {
38*0b57cec5SDimitry Andric   BinaryStreamReader Reader(*Stream);
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric   if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {
41*0b57cec5SDimitry Andric     if (Error E = reloadSerialize(Reader))
42*0b57cec5SDimitry Andric       return E;
43*0b57cec5SDimitry Andric   }
44*0b57cec5SDimitry Andric   if (Reader.bytesRemaining() > 0)
45*0b57cec5SDimitry Andric     return make_error<RawError>(raw_error_code::corrupt_file,
46*0b57cec5SDimitry Andric                                 "Unexpected bytes in module stream.");
47*0b57cec5SDimitry Andric   return Error::success();
48*0b57cec5SDimitry Andric }
49*0b57cec5SDimitry Andric 
reloadSerialize(BinaryStreamReader & Reader)50*0b57cec5SDimitry Andric Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {
51*0b57cec5SDimitry Andric   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
52*0b57cec5SDimitry Andric   uint32_t C11Size = Mod.getC11LineInfoByteSize();
53*0b57cec5SDimitry Andric   uint32_t C13Size = Mod.getC13LineInfoByteSize();
54*0b57cec5SDimitry Andric 
55*0b57cec5SDimitry Andric   if (C11Size > 0 && C13Size > 0)
56*0b57cec5SDimitry Andric     return make_error<RawError>(raw_error_code::corrupt_file,
57*0b57cec5SDimitry Andric                                 "Module has both C11 and C13 line info");
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric   BinaryStreamRef S;
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric   if (auto EC = Reader.readInteger(Signature))
62*0b57cec5SDimitry Andric     return EC;
63*0b57cec5SDimitry Andric   Reader.setOffset(0);
64*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
65*0b57cec5SDimitry Andric     return EC;
66*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
67*0b57cec5SDimitry Andric     return EC;
68*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
69*0b57cec5SDimitry Andric     return EC;
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
72*0b57cec5SDimitry Andric   if (auto EC = SymbolReader.readArray(
73*0b57cec5SDimitry Andric           SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
74*0b57cec5SDimitry Andric     return EC;
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric   BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
77*0b57cec5SDimitry Andric   if (auto EC = SubsectionsReader.readArray(Subsections,
78*0b57cec5SDimitry Andric                                             SubsectionsReader.bytesRemaining()))
79*0b57cec5SDimitry Andric     return EC;
80*0b57cec5SDimitry Andric 
81*0b57cec5SDimitry Andric   uint32_t GlobalRefsSize;
82*0b57cec5SDimitry Andric   if (auto EC = Reader.readInteger(GlobalRefsSize))
83*0b57cec5SDimitry Andric     return EC;
84*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
85*0b57cec5SDimitry Andric     return EC;
86*0b57cec5SDimitry Andric   return Error::success();
87*0b57cec5SDimitry Andric }
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric const codeview::CVSymbolArray
getSymbolArrayForScope(uint32_t ScopeBegin) const90*0b57cec5SDimitry Andric ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
91*0b57cec5SDimitry Andric   return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
92*0b57cec5SDimitry Andric }
93*0b57cec5SDimitry Andric 
getSymbolsSubstream() const94*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
95*0b57cec5SDimitry Andric   return SymbolsSubstream;
96*0b57cec5SDimitry Andric }
97*0b57cec5SDimitry Andric 
getC11LinesSubstream() const98*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
99*0b57cec5SDimitry Andric   return C11LinesSubstream;
100*0b57cec5SDimitry Andric }
101*0b57cec5SDimitry Andric 
getC13LinesSubstream() const102*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
103*0b57cec5SDimitry Andric   return C13LinesSubstream;
104*0b57cec5SDimitry Andric }
105*0b57cec5SDimitry Andric 
getGlobalRefsSubstream() const106*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
107*0b57cec5SDimitry Andric   return GlobalRefsSubstream;
108*0b57cec5SDimitry Andric }
109*0b57cec5SDimitry Andric 
110*0b57cec5SDimitry Andric iterator_range<codeview::CVSymbolArray::Iterator>
symbols(bool * HadError) const111*0b57cec5SDimitry Andric ModuleDebugStreamRef::symbols(bool *HadError) const {
112*0b57cec5SDimitry Andric   return make_range(SymbolArray.begin(HadError), SymbolArray.end());
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric 
readSymbolAtOffset(uint32_t Offset) const115*0b57cec5SDimitry Andric CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
116*0b57cec5SDimitry Andric   auto Iter = SymbolArray.at(Offset);
117*0b57cec5SDimitry Andric   assert(Iter != SymbolArray.end());
118*0b57cec5SDimitry Andric   return *Iter;
119*0b57cec5SDimitry Andric }
120*0b57cec5SDimitry Andric 
121*0b57cec5SDimitry Andric iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
subsections() const122*0b57cec5SDimitry Andric ModuleDebugStreamRef::subsections() const {
123*0b57cec5SDimitry Andric   return make_range(Subsections.begin(), Subsections.end());
124*0b57cec5SDimitry Andric }
125*0b57cec5SDimitry Andric 
hasDebugSubsections() const126*0b57cec5SDimitry Andric bool ModuleDebugStreamRef::hasDebugSubsections() const {
127*0b57cec5SDimitry Andric   return !C13LinesSubstream.empty();
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric 
commit()130*0b57cec5SDimitry Andric Error ModuleDebugStreamRef::commit() { return Error::success(); }
131*0b57cec5SDimitry Andric 
132*0b57cec5SDimitry Andric Expected<codeview::DebugChecksumsSubsectionRef>
findChecksumsSubsection() const133*0b57cec5SDimitry Andric ModuleDebugStreamRef::findChecksumsSubsection() const {
134*0b57cec5SDimitry Andric   codeview::DebugChecksumsSubsectionRef Result;
135*0b57cec5SDimitry Andric   for (const auto &SS : subsections()) {
136*0b57cec5SDimitry Andric     if (SS.kind() != DebugSubsectionKind::FileChecksums)
137*0b57cec5SDimitry Andric       continue;
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric     if (auto EC = Result.initialize(SS.getRecordData()))
140*0b57cec5SDimitry Andric       return std::move(EC);
141*0b57cec5SDimitry Andric     return Result;
142*0b57cec5SDimitry Andric   }
143*0b57cec5SDimitry Andric   return Result;
144*0b57cec5SDimitry Andric }
145