1 //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
26 using namespace llvm::codeview;
27 using namespace llvm::msf;
28 using namespace llvm::pdb;
30 ModuleDebugStreamRef::ModuleDebugStreamRef(
31 const DbiModuleDescriptor &Module,
32 std::unique_ptr<MappedBlockStream> Stream)
33 : Mod(Module), Stream(std::move(Stream)) {}
35 ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
37 Error ModuleDebugStreamRef::reload() {
38 BinaryStreamReader Reader(*Stream);
40 uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
41 uint32_t C11Size = Mod.getC11LineInfoByteSize();
42 uint32_t C13Size = Mod.getC13LineInfoByteSize();
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");
50 if (auto EC = Reader.readInteger(Signature))
53 if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
55 if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
57 if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
60 BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
61 if (auto EC = SymbolReader.readArray(
62 SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
65 BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
66 if (auto EC = SubsectionsReader.readArray(Subsections,
67 SubsectionsReader.bytesRemaining()))
70 uint32_t GlobalRefsSize;
71 if (auto EC = Reader.readInteger(GlobalRefsSize))
73 if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
75 if (Reader.bytesRemaining() > 0)
76 return make_error<RawError>(raw_error_code::corrupt_file,
77 "Unexpected bytes in module stream.");
79 return Error::success();
82 const codeview::CVSymbolArray
83 ModuleDebugStreamRef::getSymbolArrayForScope(uint32_t ScopeBegin) const {
84 return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
87 BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
88 return SymbolsSubstream;
91 BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
92 return C11LinesSubstream;
95 BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
96 return C13LinesSubstream;
99 BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
100 return GlobalRefsSubstream;
103 iterator_range<codeview::CVSymbolArray::Iterator>
104 ModuleDebugStreamRef::symbols(bool *HadError) const {
105 return make_range(SymbolArray.begin(HadError), SymbolArray.end());
108 CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
109 auto Iter = SymbolArray.at(Offset);
110 assert(Iter != SymbolArray.end());
114 iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
115 ModuleDebugStreamRef::subsections() const {
116 return make_range(Subsections.begin(), Subsections.end());
119 bool ModuleDebugStreamRef::hasDebugSubsections() const {
120 return !C13LinesSubstream.empty();
123 Error ModuleDebugStreamRef::commit() { return Error::success(); }
125 Expected<codeview::DebugChecksumsSubsectionRef>
126 ModuleDebugStreamRef::findChecksumsSubsection() const {
127 codeview::DebugChecksumsSubsectionRef Result;
128 for (const auto &SS : subsections()) {
129 if (SS.kind() != DebugSubsectionKind::FileChecksums)
132 if (auto EC = Result.initialize(SS.getRecordData()))
133 return std::move(EC);