1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===-- LVCodeViewReader.h --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the LVCodeViewReader class, which is used to describe a
// debug information (COFF) reader.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWREADER_H
#define LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWREADER_H
#include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
#include "llvm/DebugInfo/LogicalView/Readers/LVBinaryReader.h"
#include "llvm/DebugInfo/LogicalView/Readers/LVCodeViewVisitor.h"
#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
#include "llvm/DebugInfo/PDB/PDB.h"
#include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/BinaryItemStream.h"
#include "llvm/Support/BinaryStreamArray.h"
namespace llvm {
template <> struct BinaryItemTraits<codeview::CVType> {
static size_t length(const codeview::CVType &Item) { return Item.length(); }
static ArrayRef<uint8_t> bytes(const codeview::CVType &Item) {
return Item.data();
}
};
namespace codeview {
class LazyRandomTypeCollection;
}
namespace object {
struct coff_section;
}
namespace pdb {
class SymbolGroup;
}
namespace logicalview {
class LVElement;
class LVLine;
class LVScope;
class LVScopeCompileUnit;
class LVSymbol;
class LVType;
class LVTypeVisitor;
class LVSymbolVisitor;
class LVSymbolVisitorDelegate;
using LVNames = SmallVector<StringRef, 16>;
// The ELF reader uses the DWARF constants to create the logical elements.
// The DW_TAG_* and DW_AT_* are used to select the logical object and to
// set specific attributes, such as name, type, etc.
// As the CodeView constants are different to the DWARF constants, the
// CodeView reader will map them to the DWARF ones.
class LVCodeViewReader final : public LVBinaryReader {
friend class LVTypeVisitor;
friend class LVSymbolVisitor;
friend class LVSymbolVisitorDelegate;
using LVModules = std::vector<LVScope *>;
LVModules Modules;
// Encapsulates access to the input file and any dependent type server,
// including any precompiled header object.
llvm::pdb::InputFile Input;
std::shared_ptr<llvm::pdb::InputFile> TypeServer;
std::shared_ptr<LazyRandomTypeCollection> PrecompHeader;
// Persistance data when loading a type server.
ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr = nullptr;
std::unique_ptr<MemoryBuffer> MemBuffer;
std::unique_ptr<llvm::pdb::IPDBSession> Session;
std::unique_ptr<llvm::pdb::NativeSession> PdbSession;
// Persistance data when loading a precompiled header.
BumpPtrAllocator BuilderAllocator;
std::unique_ptr<AppendingTypeTableBuilder> Builder;
std::unique_ptr<BinaryItemStream<CVType>> ItemStream;
std::unique_ptr<BinaryStreamReader> ReaderPrecomp;
std::vector<CVType> TypeArray;
CVTypeArray TypeStream;
CVTypeArray CVTypesPrecomp;
// Persistance data when loading an executable file.
std::unique_ptr<MemoryBuffer> BinaryBuffer;
std::unique_ptr<llvm::object::Binary> BinaryExecutable;
Error loadTargetInfo(const object::ObjectFile &Obj);
Error loadTargetInfo(const llvm::pdb::PDBFile &Pdb);
void mapRangeAddress(const object::ObjectFile &Obj,
const object::SectionRef &Section,
bool IsComdat) override;
llvm::object::COFFObjectFile &getObj() { return Input.obj(); }
llvm::pdb::PDBFile &getPdb() { return Input.pdb(); }
bool isObj() const { return Input.isObj(); }
bool isPdb() const { return Input.isPdb(); }
StringRef getFileName() { return Input.getFilePath(); }
// Pathname to executable image.
std::string ExePath;
LVOffset CurrentOffset = 0;
int32_t CurrentModule = -1;
using RelocMapTy = DenseMap<const llvm::object::coff_section *,
std::vector<llvm::object::RelocationRef>>;
RelocMapTy RelocMap;
// Object files have only one type stream that contains both types and ids.
// Precompiled header objects don't contain an IPI stream. Use the TPI.
LazyRandomTypeCollection &types() {
return TypeServer ? TypeServer->types()
: (PrecompHeader ? *PrecompHeader : Input.types());
}
LazyRandomTypeCollection &ids() {
return TypeServer ? TypeServer->ids()
: (PrecompHeader ? *PrecompHeader : Input.ids());
}
LVLogicalVisitor LogicalVisitor;
Expected<StringRef>
getFileNameForFileOffset(uint32_t FileOffset,
const llvm::pdb::SymbolGroup *SG = nullptr);
void printRelocatedField(StringRef Label,
const llvm::object::coff_section *CoffSection,
uint32_t RelocOffset, uint32_t Offset,
StringRef *RelocSym);
Error printFileNameForOffset(StringRef Label, uint32_t FileOffset,
const llvm::pdb::SymbolGroup *SG = nullptr);
Error loadPrecompiledObject(PrecompRecord &Precomp, CVTypeArray &CVTypesObj);
Error loadTypeServer(TypeServer2Record &TS);
Error traverseTypes(llvm::pdb::PDBFile &Pdb, LazyRandomTypeCollection &Types,
LazyRandomTypeCollection &Ids);
Error collectInlineeInfo(DebugInlineeLinesSubsectionRef &Lines,
const llvm::pdb::SymbolGroup *SG = nullptr);
void cacheRelocations();
Error resolveSymbol(const llvm::object::coff_section *CoffSection,
uint64_t Offset, llvm::object::SymbolRef &Sym);
Error resolveSymbolName(const llvm::object::coff_section *CoffSection,
uint64_t Offset, StringRef &Name);
Error traverseTypeSection(StringRef SectionName,
const llvm::object::SectionRef &Section);
Error traverseSymbolSection(StringRef SectionName,
const llvm::object::SectionRef &Section);
Error traverseInlineeLines(StringRef Subsection);
DebugChecksumsSubsectionRef CVFileChecksumTable;
DebugStringTableSubsectionRef CVStringTable;
Error traverseSymbolsSubsection(StringRef Subsection,
const llvm::object::SectionRef &Section,
StringRef SectionContents);
/// Given a .debug$S section, find the string table and file checksum table.
/// This function taken from (COFFDumper.cpp).
/// TODO: It can be moved to the COFF library.
Error initializeFileAndStringTables(BinaryStreamReader &Reader);
Error createLines(const FixedStreamArray<LineNumberEntry> &LineNumbers,
LVAddress Addendum, uint32_t Segment, uint32_t Begin,
uint32_t Size, uint32_t NameIndex,
const llvm::pdb::SymbolGroup *SG = nullptr);
Error createScopes(llvm::object::COFFObjectFile &Obj);
Error createScopes(llvm::pdb::PDBFile &Pdb);
Error processModule();
protected:
Error createScopes() override;
void sortScopes() override;
public:
LVCodeViewReader() = delete;
LVCodeViewReader(StringRef Filename, StringRef FileFormatName,
llvm::object::COFFObjectFile &Obj, ScopedPrinter &W,
StringRef ExePath)
: LVBinaryReader(Filename, FileFormatName, W, LVBinaryType::COFF),
Input(&Obj), ExePath(ExePath), LogicalVisitor(this, W, Input) {}
LVCodeViewReader(StringRef Filename, StringRef FileFormatName,
llvm::pdb::PDBFile &Pdb, ScopedPrinter &W, StringRef ExePath)
: LVBinaryReader(Filename, FileFormatName, W, LVBinaryType::COFF),
Input(&Pdb), ExePath(ExePath), LogicalVisitor(this, W, Input) {}
LVCodeViewReader(const LVCodeViewReader &) = delete;
LVCodeViewReader &operator=(const LVCodeViewReader &) = delete;
~LVCodeViewReader() = default;
void getLinkageName(const llvm::object::coff_section *CoffSection,
uint32_t RelocOffset, uint32_t Offset,
StringRef *RelocSym);
void addModule(LVScope *Scope) { Modules.push_back(Scope); }
LVScope *getScopeForModule(uint32_t Modi) {
return Modi >= Modules.size() ? nullptr : Modules[Modi];
}
// Get the string representation for the CodeView symbols.
static StringRef getSymbolKindName(SymbolKind Kind);
static std::string formatRegisterId(RegisterId Register, CPUType CPU);
std::string getRegisterName(LVSmall Opcode,
ArrayRef<uint64_t> Operands) override;
bool isSystemEntry(LVElement *Element, StringRef Name) const override;
void print(raw_ostream &OS) const;
void printRecords(raw_ostream &OS) const override {
LogicalVisitor.printRecords(OS);
};
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
void dump() const { print(dbgs()); }
#endif
};
} // end namespace logicalview
} // end namespace llvm
#endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWREADER_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|