LLVM  10.0.0svn
ModuleDebugStream.cpp
Go to the documentation of this file.
1 //===- ModuleDebugStream.cpp - PDB Module Info Stream Access --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
21 #include "llvm/Support/Error.h"
22 #include <algorithm>
23 #include <cstdint>
24 
25 using namespace llvm;
26 using namespace llvm::codeview;
27 using namespace llvm::msf;
28 using namespace llvm::pdb;
29 
30 ModuleDebugStreamRef::ModuleDebugStreamRef(
32  std::unique_ptr<MappedBlockStream> Stream)
33  : Mod(Module), Stream(std::move(Stream)) {}
34 
36 
38  BinaryStreamReader Reader(*Stream);
39 
40  if (Mod.getModuleStreamIndex() != llvm::pdb::kInvalidStreamIndex) {
41  if (Error E = reloadSerialize(Reader))
42  return E;
43  }
44  if (Reader.bytesRemaining() > 0)
45  return make_error<RawError>(raw_error_code::corrupt_file,
46  "Unexpected bytes in module stream.");
47  return Error::success();
48 }
49 
50 Error ModuleDebugStreamRef::reloadSerialize(BinaryStreamReader &Reader) {
51  uint32_t SymbolSize = Mod.getSymbolDebugInfoByteSize();
52  uint32_t C11Size = Mod.getC11LineInfoByteSize();
53  uint32_t C13Size = Mod.getC13LineInfoByteSize();
54 
55  if (C11Size > 0 && C13Size > 0)
56  return make_error<RawError>(raw_error_code::corrupt_file,
57  "Module has both C11 and C13 line info");
58 
60 
61  if (auto EC = Reader.readInteger(Signature))
62  return EC;
63  Reader.setOffset(0);
64  if (auto EC = Reader.readSubstream(SymbolsSubstream, SymbolSize))
65  return EC;
66  if (auto EC = Reader.readSubstream(C11LinesSubstream, C11Size))
67  return EC;
68  if (auto EC = Reader.readSubstream(C13LinesSubstream, C13Size))
69  return EC;
70 
71  BinaryStreamReader SymbolReader(SymbolsSubstream.StreamData);
72  if (auto EC = SymbolReader.readArray(
73  SymbolArray, SymbolReader.bytesRemaining(), sizeof(uint32_t)))
74  return EC;
75 
76  BinaryStreamReader SubsectionsReader(C13LinesSubstream.StreamData);
77  if (auto EC = SubsectionsReader.readArray(Subsections,
78  SubsectionsReader.bytesRemaining()))
79  return EC;
80 
81  uint32_t GlobalRefsSize;
82  if (auto EC = Reader.readInteger(GlobalRefsSize))
83  return EC;
84  if (auto EC = Reader.readSubstream(GlobalRefsSubstream, GlobalRefsSize))
85  return EC;
86  return Error::success();
87 }
88 
91  return limitSymbolArrayToScope(SymbolArray, ScopeBegin);
92 }
93 
95  return SymbolsSubstream;
96 }
97 
99  return C11LinesSubstream;
100 }
101 
103  return C13LinesSubstream;
104 }
105 
107  return GlobalRefsSubstream;
108 }
109 
111 ModuleDebugStreamRef::symbols(bool *HadError) const {
112  return make_range(SymbolArray.begin(HadError), SymbolArray.end());
113 }
114 
116  auto Iter = SymbolArray.at(Offset);
117  assert(Iter != SymbolArray.end());
118  return *Iter;
119 }
120 
123  return make_range(Subsections.begin(), Subsections.end());
124 }
125 
127  return !C13LinesSubstream.empty();
128 }
129 
131 
135  for (const auto &SS : subsections()) {
136  if (SS.kind() != DebugSubsectionKind::FileChecksums)
137  continue;
138 
139  if (auto EC = Result.initialize(SS.getRecordData()))
140  return std::move(EC);
141  return Result;
142  }
143  return Result;
144 }
const codeview::CVSymbolArray getSymbolArrayForScope(uint32_t ScopeBegin) const
Error readSubstream(BinarySubstreamRef &Stream, uint32_t Size)
Read Length bytes from the underlying stream into Stream.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream&#39;s offset.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
Expected< codeview::DebugChecksumsSubsectionRef > findChecksumsSubsection() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
BinarySubstreamRef getC13LinesSubstream() const
iterator_range< DebugSubsectionIterator > subsections() const
codeview::CVSymbol readSymbolAtOffset(uint32_t Offset) const
Definition: BitVector.h:937
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:19
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
iterator_range< codeview::CVSymbolArray::Iterator > symbols(bool *HadError) const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
BinarySubstreamRef getC11LinesSubstream() const
BinarySubstreamRef getSymbolsSubstream() const
The access may modify the value stored in memory.
A range adaptor for a pair of iterators.
CVSymbolArray limitSymbolArrayToScope(const CVSymbolArray &Symbols, uint32_t ScopeBegin)
BinarySubstreamRef getGlobalRefsSubstream() const
uint32_t bytesRemaining() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Provides read only access to a subclass of BinaryStream.
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition: CVRecord.h:30
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...