Line data Source code
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 328 : ModuleDebugStreamRef::ModuleDebugStreamRef(
29 : const DbiModuleDescriptor &Module,
30 328 : std::unique_ptr<MappedBlockStream> Stream)
31 328 : : Mod(Module), Stream(std::move(Stream)) {}
32 :
33 : ModuleDebugStreamRef::~ModuleDebugStreamRef() = default;
34 :
35 328 : Error ModuleDebugStreamRef::reload() {
36 328 : BinaryStreamReader Reader(*Stream);
37 :
38 328 : uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
39 328 : uint32_t C11Size = Mod.getC11LineInfoByteSize();
40 328 : uint32_t C13Size = Mod.getC13LineInfoByteSize();
41 :
42 328 : 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 656 : if (auto EC = Reader.readInteger(Signature))
49 : return EC;
50 656 : if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize - 4))
51 : return EC;
52 656 : if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
53 : return EC;
54 656 : if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
55 : return EC;
56 :
57 656 : BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
58 328 : if (auto EC =
59 328 : SymbolReader.readArray(SymbolArray, SymbolReader.bytesRemaining()))
60 : return EC;
61 :
62 656 : BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
63 328 : if (auto EC = SubsectionsReader.readArray(Subsections,
64 328 : SubsectionsReader.bytesRemaining()))
65 : return EC;
66 :
67 : uint32_t GlobalRefsSize;
68 656 : if (auto EC = Reader.readInteger(GlobalRefsSize))
69 : return EC;
70 656 : if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
71 : return EC;
72 328 : if (Reader.bytesRemaining() > 0)
73 : return make_error<RawError>(raw_error_code::corrupt_file,
74 : "Unexpected bytes in module stream.");
75 :
76 : return Error::success();
77 : }
78 :
79 37 : BinarySubstreamRef ModuleDebugStreamRef::getSymbolsSubstream() const {
80 37 : return SymbolsSubstream;
81 : }
82 :
83 0 : BinarySubstreamRef ModuleDebugStreamRef::getC11LinesSubstream() const {
84 0 : return C11LinesSubstream;
85 : }
86 :
87 4 : BinarySubstreamRef ModuleDebugStreamRef::getC13LinesSubstream() const {
88 4 : return C13LinesSubstream;
89 : }
90 :
91 0 : BinarySubstreamRef ModuleDebugStreamRef::getGlobalRefsSubstream() const {
92 0 : return GlobalRefsSubstream;
93 : }
94 :
95 : iterator_range<codeview::CVSymbolArray::Iterator>
96 25 : ModuleDebugStreamRef::symbols(bool *HadError) const {
97 50 : return make_range(SymbolArray.begin(HadError), SymbolArray.end());
98 : }
99 :
100 0 : CVSymbol ModuleDebugStreamRef::readSymbolAtOffset(uint32_t Offset) const {
101 : // Offsets include the size of the 4-byte magic at the beginning, but lookup
102 : // doesn't take that into account, so subtract it here.
103 0 : auto Iter = SymbolArray.at(Offset - 4);
104 : assert(Iter != SymbolArray.end());
105 0 : return *Iter;
106 : }
107 :
108 : iterator_range<ModuleDebugStreamRef::DebugSubsectionIterator>
109 21 : ModuleDebugStreamRef::subsections() const {
110 42 : return make_range(Subsections.begin(), Subsections.end());
111 : }
112 :
113 13 : bool ModuleDebugStreamRef::hasDebugSubsections() const {
114 13 : return !C13LinesSubstream.empty();
115 : }
116 :
117 0 : Error ModuleDebugStreamRef::commit() { return Error::success(); }
118 :
119 : Expected<codeview::DebugChecksumsSubsectionRef>
120 10 : ModuleDebugStreamRef::findChecksumsSubsection() const {
121 10 : codeview::DebugChecksumsSubsectionRef Result;
122 35 : for (const auto &SS : subsections()) {
123 22 : if (SS.kind() != DebugSubsectionKind::FileChecksums)
124 : continue;
125 :
126 14 : if (auto EC = Result.initialize(SS.getRecordData()))
127 : return std::move(EC);
128 : return Result;
129 : }
130 : return Result;
131 : }
|