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/SymbolRecord.h"
15 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
16 #include "llvm/DebugInfo/PDB/Native/RawError.h"
17 #include "llvm/Support/BinaryStreamReader.h"
18 #include "llvm/Support/BinaryStreamRef.h"
19 #include "llvm/Support/Error.h"
24 using namespace llvm::codeview;
25 using namespace llvm::msf;
26 using namespace llvm::pdb;
28 ModuleDebugStreamRef::ModuleDebugStreamRef(
29 const DbiModuleDescriptor &Module,
30 std::unique_ptr<MappedBlockStream> Stream)
31 : Mod(Module), Stream(std::move(Stream)) {}
33 ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
35 Error ModuleDebugStreamRef::reload() {
36 BinaryStreamReader Reader(*Stream);
38 uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
39 uint32_t C11Size = Mod.getC11LineInfoByteSize();
40 uint32_t C13Size = Mod.getC13LineInfoByteSize();
42 if (C11Size > 0 && C13Size > 0)
43 return make_error<RawError>(raw_error_code::corrupt_file,
44 "Module has both C11 and C13 line info");
48 if (auto EC = Reader.readInteger(Signature))
50 if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize - 4))
52 if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
54 if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
57 BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
59 SymbolReader.readArray(SymbolArray, SymbolReader.bytesRemaining()))
62 BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
63 if (auto EC = SubsectionsReader.readArray(Subsections,
64 SubsectionsReader.bytesRemaining()))
67 uint32_t GlobalRefsSize;
68 if (auto EC = Reader.readInteger(GlobalRefsSize))
70 if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
72 if (Reader.bytesRemaining() > 0)
73 return make_error<RawError>(raw_error_code::corrupt_file,
74 "Unexpected bytes in module stream.");
76 return Error::success();
79 BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
80 return SymbolsSubstream;
83 BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
84 return C11LinesSubstream;
87 BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
88 return C13LinesSubstream;
91 BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
92 return GlobalRefsSubstream;
95 iterator_range<codeview::CVSymbolArray::Iterator>
96 ModuleDebugStreamRef::symbols(bool *HadError) const {
97 return make_range(SymbolArray.begin(HadError), SymbolArray.end());
100 iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
101 ModuleDebugStreamRef::subsections() const {
102 return make_range(Subsections.begin(), Subsections.end());
105 bool ModuleDebugStreamRef::hasDebugSubsections() const {
106 return !C13LinesSubstream.empty();
109 Error ModuleDebugStreamRef::commit() { return Error::success(); }
111 Expected<codeview::DebugChecksumsSubsectionRef>
112 ModuleDebugStreamRef::findChecksumsSubsection() const {
113 codeview::DebugChecksumsSubsectionRef Result;
114 for (const auto &SS : subsections()) {
115 if (SS.kind() != DebugSubsectionKind::FileChecksums)
118 if (auto EC = Result.initialize(SS.getRecordData()))
119 return std::move(EC);