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/SymbolRecord.h"
13 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
14 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
15 #include "llvm/DebugInfo/PDB/Native/RawError.h"
16 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
17 #include "llvm/Support/BinaryStreamReader.h"
18 #include "llvm/Support/BinaryStreamRef.h"
19 #include "llvm/Support/Error.h"
20 #include <algorithm>
21 #include <cstdint>
22 
23 using namespace llvm;
24 using namespace llvm::codeview;
25 using namespace llvm::msf;
26 using namespace llvm::pdb;
27 
28 ModuleDebugStreamRef::ModuleDebugStreamRef(
29     const DbiModuleDescriptor &Module,
30     std::unique_ptr<MappedBlockStream> Stream)
31     : Mod(Module), Stream(std::move(Stream)) {}
32 
33 ModuleDebugStreamRef::ModuleDebugStreamRef(ModuleDebugStreamRef &&Other)
34     : Mod(Other.Mod), Signature(Other.Signature),
35       Stream(std::move(Other.Stream)),
36       SymbolsSubstream(std::move(Other.SymbolsSubstream)),
37       C11LinesSubstream(std::move(Other.C11LinesSubstream)),
38       C13LinesSubstream(std::move(Other.C13LinesSubstream)),
39       GlobalRefsSubstream(std::move(Other.GlobalRefsSubstream)),
40       Subsections(std::move(Other.Subsections)) {}
41 
42 ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
43 
44 Error ModuleDebugStreamRef::reload() {
45   BinaryStreamReader Reader(*Stream);
46 
47   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
48   uint32_t C11Size = Mod.getC11LineInfoByteSize();
49   uint32_t C13Size = Mod.getC13LineInfoByteSize();
50 
51   if (C11Size > 0 && C13Size > 0)
52     return make_error<RawError>(raw_error_code::corrupt_file,
53                                 "Module has both C11 and C13 line info");
54 
55   BinaryStreamRef S;
56 
57   if (auto EC = Reader.readInteger(Signature))
58     return EC;
59   if (auto EC = Reader.readArray(SymbolsSubstream, SymbolSize - 4))
60     return EC;
61 
62   if (auto EC = Reader.readStreamRef(C11LinesSubstream, C11Size))
63     return EC;
64   if (auto EC = Reader.readStreamRef(C13LinesSubstream, C13Size))
65     return EC;
66 
67   BinaryStreamReader SubsectionsReader(C13LinesSubstream);
68   if (auto EC = SubsectionsReader.readArray(Subsections,
69                                             SubsectionsReader.bytesRemaining()))
70     return EC;
71 
72   uint32_t GlobalRefsSize;
73   if (auto EC = Reader.readInteger(GlobalRefsSize))
74     return EC;
75   if (auto EC = Reader.readStreamRef(GlobalRefsSubstream, GlobalRefsSize))
76     return EC;
77   if (Reader.bytesRemaining() > 0)
78     return make_error<RawError>(raw_error_code::corrupt_file,
79                                 "Unexpected bytes in module stream.");
80 
81   return Error::success();
82 }
83 
84 iterator_range<codeview::CVSymbolArray::Iterator>
85 ModuleDebugStreamRef::symbols(bool *HadError) const {
86   return make_range(SymbolsSubstream.begin(HadError), SymbolsSubstream.end());
87 }
88 
89 llvm::iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
90 ModuleDebugStreamRef::subsections() const {
91   return make_range(Subsections.begin(), Subsections.end());
92 }
93 
94 bool ModuleDebugStreamRef::hasDebugSubsections() const {
95   return C13LinesSubstream.getLength() > 0;
96 }
97 
98 Error ModuleDebugStreamRef::commit() { return Error::success(); }
99 
100 Expected<codeview::DebugChecksumsSubsectionRef>
101 ModuleDebugStreamRef::findChecksumsSubsection() const {
102   codeview::DebugChecksumsSubsectionRef Result;
103   for (const auto &SS : subsections()) {
104     if (SS.kind() != DebugSubsectionKind::FileChecksums)
105       continue;
106 
107     if (auto EC = Result.initialize(SS.getRecordData()))
108       return std::move(EC);
109     return Result;
110   }
111   return Result;
112 }
113