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