1 //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
2 //
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 //===----------------------------------------------------------------------===//
9
10 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/DebugInfo/CodeView/CodeView.h"
13 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
14 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
15 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
16 #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
17 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
18 #include "llvm/DebugInfo/PDB/Native/RawError.h"
19 #include "llvm/Support/BinaryStreamReader.h"
20 #include "llvm/Support/BinaryStreamRef.h"
21 #include "llvm/Support/Error.h"
22 #include <algorithm>
23 #include <cstdint>
24
25 using namespace llvm;
26 using namespace llvm::codeview;
27 using namespace llvm::msf;
28 using namespace llvm::pdb;
29
ModuleDebugStreamRef(const DbiModuleDescriptor & Module,std::unique_ptr<MappedBlockStream> Stream)30 ModuleDebugStreamRef::ModuleDebugStreamRef(
31 const DbiModuleDescriptor &Module,
32 std::unique_ptr<MappedBlockStream> Stream)
33 : Mod(Module), Stream(std::move(Stream)) {}
34
35 ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
36
reload()37 Error ModuleDebugStreamRef::reload() {
38 BinaryStreamReader Reader(*Stream);
39
40 uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
41 uint32_t C11Size = Mod.getC11LineInfoByteSize();
42 uint32_t C13Size = Mod.getC13LineInfoByteSize();
43
44 if (C11Size > 0 && C13Size > 0)
45 return make_error<RawError>(raw_error_code::corrupt_file,
46 "Module has both C11 and C13 line info");
47
48 BinaryStreamRef S;
49
50 if (auto EC = Reader.readInteger(Signature))
51 return EC;
52 Reader.setOffset(0);
53 if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
54 return EC;
55 if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
56 return EC;
57 if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
58 return EC;
59
60 BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
61 if (auto EC = SymbolReader.readArray(
62 SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
63 return EC;
64
65 BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
66 if (auto EC = SubsectionsReader.readArray(Subsections,
67 SubsectionsReader.bytesRemaining()))
68 return EC;
69
70 uint32_t GlobalRefsSize;
71 if (auto EC = Reader.readInteger(GlobalRefsSize))
72 return EC;
73 if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
74 return EC;
75 if (Reader.bytesRemaining() > 0)
76 return make_error<RawError>(raw_error_code::corrupt_file,
77 "Unexpected bytes in module stream.");
78
79 return Error::success();
80 }
81
82 const codeview::CVSymbolArray
getSymbolArrayForScope(uint32_t ScopeBegin) const83 ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
84 return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
85 }
86
getSymbolsSubstream() const87 BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
88 return SymbolsSubstream;
89 }
90
getC11LinesSubstream() const91 BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
92 return C11LinesSubstream;
93 }
94
getC13LinesSubstream() const95 BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
96 return C13LinesSubstream;
97 }
98
getGlobalRefsSubstream() const99 BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
100 return GlobalRefsSubstream;
101 }
102
103 iterator_range<codeview::CVSymbolArray::Iterator>
symbols(bool * HadError) const104 ModuleDebugStreamRef::symbols(bool *HadError) const {
105 return make_range(SymbolArray.begin(HadError), SymbolArray.end());
106 }
107
readSymbolAtOffset(uint32_t Offset) const108 CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
109 auto Iter = SymbolArray.at(Offset);
110 assert(Iter != SymbolArray.end());
111 return *Iter;
112 }
113
114 iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
subsections() const115 ModuleDebugStreamRef::subsections() const {
116 return make_range(Subsections.begin(), Subsections.end());
117 }
118
hasDebugSubsections() const119 bool ModuleDebugStreamRef::hasDebugSubsections() const {
120 return !C13LinesSubstream.empty();
121 }
122
commit()123 Error ModuleDebugStreamRef::commit() { return Error::success(); }
124
125 Expected<codeview::DebugChecksumsSubsectionRef>
findChecksumsSubsection() const126 ModuleDebugStreamRef::findChecksumsSubsection() const {
127 codeview::DebugChecksumsSubsectionRef Result;
128 for (const auto &SS : subsections()) {
129 if (SS.kind() != DebugSubsectionKind::FileChecksums)
130 continue;
131
132 if (auto EC = Result.initialize(SS.getRecordData()))
133 return std::move(EC);
134 return Result;
135 }
136 return Result;
137 }
138