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/SymbolRecordHelpers.h"
14*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MSFCommon.h"
15*0b57cec5SDimitry Andric #include "llvm/DebugInfo/MSF/MappedBlockStream.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/DebugInfo/PDB/Native/RawTypes.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamArray.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
22*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamRef.h"
23*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
24*0b57cec5SDimitry Andric #include <cstdint>
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric using namespace llvm;
27*0b57cec5SDimitry Andric using namespace llvm::codeview;
28*0b57cec5SDimitry Andric using namespace llvm::msf;
29*0b57cec5SDimitry Andric using namespace llvm::pdb;
30*0b57cec5SDimitry Andric 
ModuleDebugStreamRef(const DbiModuleDescriptor & Module,std::unique_ptr<MappedBlockStream> Stream)31*0b57cec5SDimitry Andric ModuleDebugStreamRef::ModuleDebugStreamRef(
32*0b57cec5SDimitry Andric     const DbiModuleDescriptor &Module,
33*0b57cec5SDimitry Andric     std::unique_ptr<MappedBlockStream> Stream)
34*0b57cec5SDimitry Andric     : Mod(Module), Stream(std::move(Stream)) {}
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
37*0b57cec5SDimitry Andric 
reload()38*0b57cec5SDimitry Andric Error ModuleDebugStreamRef::reload() {
39*0b57cec5SDimitry Andric   BinaryStreamReader Reader(*Stream);
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric   if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {
42*0b57cec5SDimitry Andric     if (Error E = reloadSerialize(Reader))
43*0b57cec5SDimitry Andric       return E;
44*0b57cec5SDimitry Andric   }
45*0b57cec5SDimitry Andric   if (Reader.bytesRemaining() > 0)
46*0b57cec5SDimitry Andric     return make_error<RawError>(raw_error_code::corrupt_file,
47*0b57cec5SDimitry Andric                                 "Unexpected bytes in module stream.");
48*0b57cec5SDimitry Andric   return Error::success();
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
reloadSerialize(BinaryStreamReader & Reader)51*0b57cec5SDimitry Andric Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {
52*0b57cec5SDimitry Andric   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
53*0b57cec5SDimitry Andric   uint32_t C11Size = Mod.getC11LineInfoByteSize();
54*0b57cec5SDimitry Andric   uint32_t C13Size = Mod.getC13LineInfoByteSize();
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric   if (C11Size > 0 && C13Size > 0)
57*0b57cec5SDimitry Andric     return make_error<RawError>(raw_error_code::corrupt_file,
58*0b57cec5SDimitry Andric                                 "Module has both C11 and C13 line info");
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric   BinaryStreamRef S;
61*0b57cec5SDimitry Andric 
62*0b57cec5SDimitry Andric   if (auto EC = Reader.readInteger(Signature))
63*0b57cec5SDimitry Andric     return EC;
64*0b57cec5SDimitry Andric   Reader.setOffset(0);
65*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
66*0b57cec5SDimitry Andric     return EC;
67*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
68*0b57cec5SDimitry Andric     return EC;
69*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
70*0b57cec5SDimitry Andric     return EC;
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric   BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
73*0b57cec5SDimitry Andric   if (auto EC = SymbolReader.readArray(
74*0b57cec5SDimitry Andric           SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
75*0b57cec5SDimitry Andric     return EC;
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric   BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
78*0b57cec5SDimitry Andric   if (auto EC = SubsectionsReader.readArray(Subsections,
79*0b57cec5SDimitry Andric                                             SubsectionsReader.bytesRemaining()))
80*0b57cec5SDimitry Andric     return EC;
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric   uint32_t GlobalRefsSize;
83*0b57cec5SDimitry Andric   if (auto EC = Reader.readInteger(GlobalRefsSize))
84*0b57cec5SDimitry Andric     return EC;
85*0b57cec5SDimitry Andric   if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
86*0b57cec5SDimitry Andric     return EC;
87*0b57cec5SDimitry Andric   return Error::success();
88*0b57cec5SDimitry Andric }
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric const codeview::CVSymbolArray
getSymbolArrayForScope(uint32_t ScopeBegin) const91*0b57cec5SDimitry Andric ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
92*0b57cec5SDimitry Andric   return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
93*0b57cec5SDimitry Andric }
94*0b57cec5SDimitry Andric 
getSymbolsSubstream() const95*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
96*0b57cec5SDimitry Andric   return SymbolsSubstream;
97*0b57cec5SDimitry Andric }
98*0b57cec5SDimitry Andric 
getC11LinesSubstream() const99*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
100*0b57cec5SDimitry Andric   return C11LinesSubstream;
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric 
getC13LinesSubstream() const103*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
104*0b57cec5SDimitry Andric   return C13LinesSubstream;
105*0b57cec5SDimitry Andric }
106*0b57cec5SDimitry Andric 
getGlobalRefsSubstream() const107*0b57cec5SDimitry Andric BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
108*0b57cec5SDimitry Andric   return GlobalRefsSubstream;
109*0b57cec5SDimitry Andric }
110*0b57cec5SDimitry Andric 
111*0b57cec5SDimitry Andric iterator_range<codeview::CVSymbolArray::Iterator>
symbols(bool * HadError) const112*0b57cec5SDimitry Andric ModuleDebugStreamRef::symbols(bool *HadError) const {
113*0b57cec5SDimitry Andric   return make_range(SymbolArray.begin(HadError), SymbolArray.end());
114*0b57cec5SDimitry Andric }
115*0b57cec5SDimitry Andric 
readSymbolAtOffset(uint32_t Offset) const116*0b57cec5SDimitry Andric CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
117*0b57cec5SDimitry Andric   auto Iter = SymbolArray.at(Offset);
118*0b57cec5SDimitry Andric   assert(Iter != SymbolArray.end());
119*0b57cec5SDimitry Andric   return *Iter;
120*0b57cec5SDimitry Andric }
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
subsections() const123*0b57cec5SDimitry Andric ModuleDebugStreamRef::subsections() const {
124*0b57cec5SDimitry Andric   return make_range(Subsections.begin(), Subsections.end());
125*0b57cec5SDimitry Andric }
126*0b57cec5SDimitry Andric 
hasDebugSubsections() const127*0b57cec5SDimitry Andric bool ModuleDebugStreamRef::hasDebugSubsections() const {
128*0b57cec5SDimitry Andric   return !C13LinesSubstream.empty();
129*0b57cec5SDimitry Andric }
130*0b57cec5SDimitry Andric 
commit()131*0b57cec5SDimitry Andric Error ModuleDebugStreamRef::commit() { return Error::success(); }
132*0b57cec5SDimitry Andric 
133*0b57cec5SDimitry Andric Expected<codeview::DebugChecksumsSubsectionRef>
findChecksumsSubsection() const134*0b57cec5SDimitry Andric ModuleDebugStreamRef::findChecksumsSubsection() const {
135*0b57cec5SDimitry Andric   codeview::DebugChecksumsSubsectionRef Result;
136*0b57cec5SDimitry Andric   for (const auto &SS : subsections()) {
137*0b57cec5SDimitry Andric     if (SS.kind() != DebugSubsectionKind::FileChecksums)
138*0b57cec5SDimitry Andric       continue;
139*0b57cec5SDimitry Andric 
140*0b57cec5SDimitry Andric     if (auto EC = Result.initialize(SS.getRecordData()))
141*0b57cec5SDimitry Andric       return std::move(EC);
142*0b57cec5SDimitry Andric     return Result;
143*0b57cec5SDimitry Andric   }
144*0b57cec5SDimitry Andric   return Result;
145 }
146