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/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"
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() = default;
34 
35 Error ModuleDebugStreamRef::reload() {
36   BinaryStreamReader Reader(*Stream);
37 
38   uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
39   uint32_t C11Size = Mod.getC11LineInfoByteSize();
40   uint32_t C13Size = Mod.getC13LineInfoByteSize();
41 
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");
45 
46   BinaryStreamRef S;
47 
48   if (auto EC = Reader.readInteger(Signature))
49     return EC;
50   Reader.setOffset(0);
51   if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
52     return EC;
53   if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
54     return EC;
55   if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
56     return EC;
57 
58   BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
59   if (auto EC = SymbolReader.readArray(
60           SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
61     return EC;
62 
63   BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
64   if (auto EC = SubsectionsReader.readArray(Subsections,
65                                             SubsectionsReader.bytesRemaining()))
66     return EC;
67 
68   uint32_t GlobalRefsSize;
69   if (auto EC = Reader.readInteger(GlobalRefsSize))
70     return EC;
71   if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
72     return EC;
73   if (Reader.bytesRemaining() > 0)
74     return make_error<RawError>(raw_error_code::corrupt_file,
75                                 "Unexpected bytes in module stream.");
76 
77   return Error::success();
78 }
79 
80 BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
81   return SymbolsSubstream;
82 }
83 
84 BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
85   return C11LinesSubstream;
86 }
87 
88 BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
89   return C13LinesSubstream;
90 }
91 
92 BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
93   return GlobalRefsSubstream;
94 }
95 
96 iterator_range<codeview::CVSymbolArray::Iterator>
97 ModuleDebugStreamRef::symbols(bool *HadError) const {
98   return make_range(SymbolArray.begin(HadError), SymbolArray.end());
99 }
100 
101 CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
102   auto Iter = SymbolArray.at(Offset);
103   assert(Iter != SymbolArray.end());
104   return *Iter;
105 }
106 
107 iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
108 ModuleDebugStreamRef::subsections() const {
109   return make_range(Subsections.begin(), Subsections.end());
110 }
111 
112 bool ModuleDebugStreamRef::hasDebugSubsections() const {
113   return !C13LinesSubstream.empty();
114 }
115 
116 Error ModuleDebugStreamRef::commit() { return Error::success(); }
117 
118 Expected<codeview::DebugChecksumsSubsectionRef>
119 ModuleDebugStreamRef::findChecksumsSubsection() const {
120   codeview::DebugChecksumsSubsectionRef Result;
121   for (const auto &SS : subsections()) {
122     if (SS.kind() != DebugSubsectionKind::FileChecksums)
123       continue;
124 
125     if (auto EC = Result.initialize(SS.getRecordData()))
126       return std::move(EC);
127     return Result;
128   }
129   return Result;
130 }
131