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 LVScope;
51class LVTypeVisitor;
52class LVSymbolVisitor;
53class LVSymbolVisitorDelegate;
54
56
57// The DWARF reader uses the DWARF constants to create the logical elements.
58// The DW_TAG_* and DW_AT_* are used to select the logical object and to
59// set specific attributes, such as name, type, etc.
60// As the CodeView constants are different to the DWARF constants, the
61// CodeView reader will map them to the DWARF ones.
62
64 friend class LVTypeVisitor;
65 friend class LVSymbolVisitor;
67
68 using LVModules = std::vector<LVScope *>;
69 LVModules Modules;
70
71 // Encapsulates access to the input file and any dependent type server,
72 // including any precompiled header object.
74 std::shared_ptr<llvm::pdb::InputFile> TypeServer;
75 std::shared_ptr<LazyRandomTypeCollection> PrecompHeader;
76
77 // Persistance data when loading a type server.
78 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = nullptr;
79 std::unique_ptr<MemoryBuffer> MemBuffer;
80 std::unique_ptr<llvm::pdb::IPDBSession> Session;
81 std::unique_ptr<llvm::pdb::NativeSession> PdbSession;
82
83 // Persistance data when loading a precompiled header.
84 BumpPtrAllocator BuilderAllocator;
85 std::unique_ptr<AppendingTypeTableBuilder> Builder;
86 std::unique_ptr<BinaryItemStream<CVType>> ItemStream;
87 std::unique_ptr<BinaryStreamReader> ReaderPrecomp;
88 std::vector<CVType> TypeArray;
89 CVTypeArray TypeStream;
90 CVTypeArray CVTypesPrecomp;
91
92 // Persistance data when loading an executable file.
93 std::unique_ptr<MemoryBuffer> BinaryBuffer;
94 std::unique_ptr<llvm::object::Binary> BinaryExecutable;
95
96 Error loadTargetInfo(const object::ObjectFile &Obj);
97 Error loadTargetInfo(const llvm::pdb::PDBFile &Pdb);
98
99 void mapRangeAddress(const object::ObjectFile &Obj,
100 const object::SectionRef &Section,
101 bool IsComdat) override;
102
103 llvm::object::COFFObjectFile &getObj() { return Input.obj(); }
104 llvm::pdb::PDBFile &getPdb() { return Input.pdb(); }
105 bool isObj() const { return Input.isObj(); }
106 bool isPdb() const { return Input.isPdb(); }
107 StringRef getFileName() { return Input.getFilePath(); }
108
109 // Pathname to executable image.
110 std::string ExePath;
111
112 int32_t CurrentModule = -1;
113
114 using RelocMapTy = DenseMap<const llvm::object::coff_section *,
115 std::vector<llvm::object::RelocationRef>>;
116 RelocMapTy RelocMap;
117
118 // Object files have only one type stream that contains both types and ids.
119 // Precompiled header objects don't contain an IPI stream. Use the TPI.
120 LazyRandomTypeCollection &types() {
121 return TypeServer ? TypeServer->types()
122 : (PrecompHeader ? *PrecompHeader : Input.types());
123 }
124 LazyRandomTypeCollection &ids() {
125 return TypeServer ? TypeServer->ids()
126 : (PrecompHeader ? *PrecompHeader : Input.ids());
127 }
128
129 LVLogicalVisitor LogicalVisitor;
130
131 Expected<StringRef>
132 getFileNameForFileOffset(uint32_t FileOffset,
133 const llvm::pdb::SymbolGroup *SG = nullptr);
134 void printRelocatedField(StringRef Label,
135 const llvm::object::coff_section *CoffSection,
136 uint32_t RelocOffset, uint32_t Offset,
137 StringRef *RelocSym);
138
139 Error printFileNameForOffset(StringRef Label, uint32_t FileOffset,
140 const llvm::pdb::SymbolGroup *SG = nullptr);
141
142 Error loadPrecompiledObject(PrecompRecord &Precomp, CVTypeArray &CVTypesObj);
143 Error loadTypeServer(TypeServer2Record &TS);
144 Error traverseTypes(llvm::pdb::PDBFile &Pdb, LazyRandomTypeCollection &Types,
145 LazyRandomTypeCollection &Ids);
146
147 Error collectInlineeInfo(DebugInlineeLinesSubsectionRef &Lines,
148 const llvm::pdb::SymbolGroup *SG = nullptr);
149
150 void cacheRelocations();
151 Error resolveSymbol(const llvm::object::coff_section *CoffSection,
152 uint64_t Offset, llvm::object::SymbolRef &Sym);
153 Error resolveSymbolName(const llvm::object::coff_section *CoffSection,
154 uint64_t Offset, StringRef &Name);
155 Error traverseTypeSection(StringRef SectionName,
156 const llvm::object::SectionRef &Section);
157 Error traverseSymbolSection(StringRef SectionName,
158 const llvm::object::SectionRef &Section);
159 Error traverseInlineeLines(StringRef Subsection);
160
161 DebugChecksumsSubsectionRef CVFileChecksumTable;
162 DebugStringTableSubsectionRef CVStringTable;
163
164 Error traverseSymbolsSubsection(StringRef Subsection,
165 const llvm::object::SectionRef &Section,
166 StringRef SectionContents);
167
168 /// Given a .debug$S section, find the string table and file checksum table.
169 /// This function taken from (COFFDumper.cpp).
170 /// TODO: It can be moved to the COFF library.
171 Error initializeFileAndStringTables(BinaryStreamReader &Reader);
172
173 Error createLines(const FixedStreamArray<LineNumberEntry> &LineNumbers,
174 LVAddress Addendum, uint32_t Segment, uint32_t Begin,
175 uint32_t Size, uint32_t NameIndex,
176 const llvm::pdb::SymbolGroup *SG = nullptr);
177 Error createScopes(llvm::object::COFFObjectFile &Obj);
178 Error createScopes(llvm::pdb::PDBFile &Pdb);
180
181protected:
182 Error createScopes() override;
183 void sortScopes() override;
184
185public:
189 StringRef ExePath)
191 Input(&Obj), ExePath(ExePath), LogicalVisitor(this, W, Input) {}
195 Input(&Pdb), ExePath(ExePath), LogicalVisitor(this, W, Input) {}
198 ~LVCodeViewReader() override = default;
199
200 void getLinkageName(const llvm::object::coff_section *CoffSection,
201 uint32_t RelocOffset, uint32_t Offset,
202 StringRef *RelocSym);
203
204 void addModule(LVScope *Scope) { Modules.push_back(Scope); }
206 return Modi >= Modules.size() ? nullptr : Modules[Modi];
207 }
208
209 // Get the string representation for the CodeView symbols.
211 static std::string formatRegisterId(RegisterId Register, CPUType CPU);
212
213 std::string getRegisterName(LVSmall Opcode,
215
216 bool isSystemEntry(LVElement *Element, StringRef Name) const override;
217
218 void print(raw_ostream &OS) const;
219 void printRecords(raw_ostream &OS) const override {
220 LogicalVisitor.printRecords(OS);
221 };
222
223#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
224 void dump() const { print(dbgs()); }
225#endif
226};
227
228} // end namespace logicalview
229} // end namespace llvm
230
231#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)