LLVM 24.0.0git
LVCodeViewReader.h
Go to the documentation of this file.
1//===-- LVCodeViewReader.h --------------------------------------*- C++ -*-===//
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//
9// This file defines the LVCodeViewReader class, which is used to describe a
10// debug information (COFF) reader.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWREADER_H
15#define LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWREADER_H
16
29
30namespace llvm {
31template <> struct BinaryItemTraits<codeview::CVType> {
32 static size_t length(const codeview::CVType &Item) { return Item.length(); }
34 return Item.data();
35 }
36};
37
38namespace codeview {
39class LazyRandomTypeCollection;
40}
41namespace object {
42struct coff_section;
43}
44namespace pdb {
45class SymbolGroup;
46}
47namespace logicalview {
48
49class LVElement;
50class LVLine;
51class LVScope;
52class LVScopeCompileUnit;
53class LVSymbol;
54class LVType;
55class LVTypeVisitor;
56class LVSymbolVisitor;
57class LVSymbolVisitorDelegate;
58
60
61// The DWARF reader uses the DWARF constants to create the logical elements.
62// The DW_TAG_* and DW_AT_* are used to select the logical object and to
63// set specific attributes, such as name, type, etc.
64// As the CodeView constants are different to the DWARF constants, the
65// CodeView reader will map them to the DWARF ones.
66
68 friend class LVTypeVisitor;
69 friend class LVSymbolVisitor;
71
72 using LVModules = std::vector<LVScope *>;
73 LVModules Modules;
74
75 // Encapsulates access to the input file and any dependent type server,
76 // including any precompiled header object.
78 std::shared_ptr<llvm::pdb::InputFile> TypeServer;
79 std::shared_ptr<LazyRandomTypeCollection> PrecompHeader;
80
81 // Persistance data when loading a type server.
82 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = nullptr;
83 std::unique_ptr<MemoryBuffer> MemBuffer;
84 std::unique_ptr<llvm::pdb::IPDBSession> Session;
85 std::unique_ptr<llvm::pdb::NativeSession> PdbSession;
86
87 // Persistance data when loading a precompiled header.
88 BumpPtrAllocator BuilderAllocator;
89 std::unique_ptr<AppendingTypeTableBuilder> Builder;
90 std::unique_ptr<BinaryItemStream<CVType>> ItemStream;
91 std::unique_ptr<BinaryStreamReader> ReaderPrecomp;
92 std::vector<CVType> TypeArray;
93 CVTypeArray TypeStream;
94 CVTypeArray CVTypesPrecomp;
95
96 // Persistance data when loading an executable file.
97 std::unique_ptr<MemoryBuffer> BinaryBuffer;
98 std::unique_ptr<llvm::object::Binary> BinaryExecutable;
99
100 Error loadTargetInfo(const object::ObjectFile &Obj);
101 Error loadTargetInfo(const llvm::pdb::PDBFile &Pdb);
102
103 void mapRangeAddress(const object::ObjectFile &Obj,
104 const object::SectionRef &Section,
105 bool IsComdat) override;
106
107 llvm::object::COFFObjectFile &getObj() { return Input.obj(); }
108 llvm::pdb::PDBFile &getPdb() { return Input.pdb(); }
109 bool isObj() const { return Input.isObj(); }
110 bool isPdb() const { return Input.isPdb(); }
111 StringRef getFileName() { return Input.getFilePath(); }
112
113 // Pathname to executable image.
114 std::string ExePath;
115
116 int32_t CurrentModule = -1;
117
118 using RelocMapTy = DenseMap<const llvm::object::coff_section *,
119 std::vector<llvm::object::RelocationRef>>;
120 RelocMapTy RelocMap;
121
122 // Object files have only one type stream that contains both types and ids.
123 // Precompiled header objects don't contain an IPI stream. Use the TPI.
124 LazyRandomTypeCollection &types() {
125 return TypeServer ? TypeServer->types()
126 : (PrecompHeader ? *PrecompHeader : Input.types());
127 }
128 LazyRandomTypeCollection &ids() {
129 return TypeServer ? TypeServer->ids()
130 : (PrecompHeader ? *PrecompHeader : Input.ids());
131 }
132
133 LVLogicalVisitor LogicalVisitor;
134
135 Expected<StringRef>
136 getFileNameForFileOffset(uint32_t FileOffset,
137 const llvm::pdb::SymbolGroup *SG = nullptr);
138 void printRelocatedField(StringRef Label,
139 const llvm::object::coff_section *CoffSection,
140 uint32_t RelocOffset, uint32_t Offset,
141 StringRef *RelocSym);
142
143 Error printFileNameForOffset(StringRef Label, uint32_t FileOffset,
144 const llvm::pdb::SymbolGroup *SG = nullptr);
145
146 Error loadPrecompiledObject(PrecompRecord &Precomp, CVTypeArray &CVTypesObj);
147 Error loadTypeServer(TypeServer2Record &TS);
148 Error traverseTypes(llvm::pdb::PDBFile &Pdb, LazyRandomTypeCollection &Types,
149 LazyRandomTypeCollection &Ids);
150
151 Error collectInlineeInfo(DebugInlineeLinesSubsectionRef &Lines,
152 const llvm::pdb::SymbolGroup *SG = nullptr);
153
154 void cacheRelocations();
155 Error resolveSymbol(const llvm::object::coff_section *CoffSection,
156 uint64_t Offset, llvm::object::SymbolRef &Sym);
157 Error resolveSymbolName(const llvm::object::coff_section *CoffSection,
158 uint64_t Offset, StringRef &Name);
159 Error traverseTypeSection(StringRef SectionName,
160 const llvm::object::SectionRef &Section);
161 Error traverseSymbolSection(StringRef SectionName,
162 const llvm::object::SectionRef &Section);
163 Error traverseInlineeLines(StringRef Subsection);
164
165 DebugChecksumsSubsectionRef CVFileChecksumTable;
166 DebugStringTableSubsectionRef CVStringTable;
167
168 Error traverseSymbolsSubsection(StringRef Subsection,
169 const llvm::object::SectionRef &Section,
170 StringRef SectionContents);
171
172 /// Given a .debug$S section, find the string table and file checksum table.
173 /// This function taken from (COFFDumper.cpp).
174 /// TODO: It can be moved to the COFF library.
175 Error initializeFileAndStringTables(BinaryStreamReader &Reader);
176
177 Error createLines(const FixedStreamArray<LineNumberEntry> &LineNumbers,
178 LVAddress Addendum, uint32_t Segment, uint32_t Begin,
179 uint32_t Size, uint32_t NameIndex,
180 const llvm::pdb::SymbolGroup *SG = nullptr);
181 Error createScopes(llvm::object::COFFObjectFile &Obj);
182 Error createScopes(llvm::pdb::PDBFile &Pdb);
184
185protected:
186 Error createScopes() override;
187 void sortScopes() override;
188
189public:
193 StringRef ExePath)
195 Input(&Obj), ExePath(ExePath), LogicalVisitor(this, W, Input) {}
199 Input(&Pdb), ExePath(ExePath), LogicalVisitor(this, W, Input) {}
202 ~LVCodeViewReader() override = default;
203
204 void getLinkageName(const llvm::object::coff_section *CoffSection,
205 uint32_t RelocOffset, uint32_t Offset,
206 StringRef *RelocSym);
207
208 void addModule(LVScope *Scope) { Modules.push_back(Scope); }
210 return Modi >= Modules.size() ? nullptr : Modules[Modi];
211 }
212
213 // Get the string representation for the CodeView symbols.
215 static std::string formatRegisterId(RegisterId Register, CPUType CPU);
216
217 std::string getRegisterName(LVSmall Opcode,
219
220 bool isSystemEntry(LVElement *Element, StringRef Name) const override;
221
222 void print(raw_ostream &OS) const;
223 void printRecords(raw_ostream &OS) const override {
224 LogicalVisitor.printRecords(OS);
225 };
226
227#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
228 void dump() const { print(dbgs()); }
229#endif
230};
231
232} // end namespace logicalview
233} // end namespace llvm
234
235#endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWREADER_H
uint64_t uint64_t
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
Lightweight arrays that are backed by an arbitrary BinaryStream.
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
#define LLVM_ABI
Definition Compiler.h:211
DenseMap< uint64_t, uint64_t > RelocMap
static std::string getRegisterName(const TargetRegisterInfo *TRI, Register Reg)
static bool processModule(Module &M, NVPTXTargetMachine &TM)
SI Fold Operands
static StringRef getSymbolKindName(SymbolKind Kind)
The Input class is used to parse a yaml document into in-memory structs and vectors.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents either an error or a value T.
Definition ErrorOr.h:56
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
ArrayRef< uint8_t > data() const
Definition CVRecord.h:49
uint32_t length() const
Definition CVRecord.h:40
void printRecords(raw_ostream &OS) const override
LVCodeViewReader(StringRef Filename, StringRef FileFormatName, llvm::object::COFFObjectFile &Obj, ScopedPrinter &W, StringRef ExePath)
LVCodeViewReader & operator=(const LVCodeViewReader &)=delete
LVScope * getScopeForModule(uint32_t Modi)
LVCodeViewReader(StringRef Filename, StringRef FileFormatName, llvm::pdb::PDBFile &Pdb, ScopedPrinter &W, StringRef ExePath)
LVCodeViewReader(const LVCodeViewReader &)=delete
~LVCodeViewReader() override=default
This class is the base class for all object file types.
Definition ObjectFile.h:231
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CVRecord< TypeLeafKind > CVType
Definition CVRecord.h:64
CPUType
These values correspond to the CV_CPU_TYPE_e enumeration, and are documented here: https://msdn....
Definition CodeView.h:76
VarStreamArray< CVType > CVTypeArray
Definition CVRecord.h:127
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition CodeView.h:48
SmallVector< StringRef, 16 > LVNames
uint8_t LVSmall
Definition LVObject.h:42
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
static size_t length(const codeview::CVType &Item)
static ArrayRef< uint8_t > bytes(const codeview::CVType &Item)