LLVM 22.0.0git
SymbolCache.h
Go to the documentation of this file.
1//==- SymbolCache.h - Cache of native symbols and ids ------------*- 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#ifndef LLVM_DEBUGINFO_PDB_NATIVE_SYMBOLCACHE_H
10#define LLVM_DEBUGINFO_PDB_NATIVE_SYMBOLCACHE_H
11
12#include "llvm/ADT/DenseMap.h"
22
23#include <memory>
24#include <vector>
25
26namespace llvm {
27namespace codeview {
28class InlineSiteSym;
29struct FileChecksumEntry;
30} // namespace codeview
31namespace pdb {
32class IPDBSourceFile;
33class NativeSession;
34class PDBSymbol;
35class PDBSymbolCompiland;
36class DbiStream;
37
39 NativeSession &Session;
40 DbiStream *Dbi = nullptr;
41
42 /// Cache of all stable symbols, indexed by SymIndexId. Just because a
43 /// symbol has been parsed does not imply that it will be stable and have
44 /// an Id. Id allocation is an implementation, with the only guarantee
45 /// being that once an Id is allocated, the symbol can be assumed to be
46 /// cached.
47 mutable std::vector<std::unique_ptr<NativeRawSymbol>> Cache;
48
49 /// For type records from the TPI stream which have been paresd and cached,
50 /// stores a mapping to SymIndexId of the cached symbol.
51 mutable DenseMap<codeview::TypeIndex, SymIndexId> TypeIndexToSymbolId;
52
53 /// For field list members which have been parsed and cached, stores a mapping
54 /// from (IndexOfClass, MemberIndex) to the corresponding SymIndexId of the
55 /// cached symbol.
57 FieldListMembersToSymbolId;
58
59 /// List of SymIndexIds for each compiland, indexed by compiland index as they
60 /// appear in the PDB file.
61 mutable std::vector<SymIndexId> Compilands;
62
63 /// List of source files, indexed by unique source file index.
64 mutable std::vector<std::unique_ptr<NativeSourceFile>> SourceFiles;
65
66 /// Map from string table offset to source file Id.
67 mutable DenseMap<uint32_t, SymIndexId> FileNameOffsetToId;
68
69 /// Map from global symbol offset to SymIndexId.
70 mutable DenseMap<uint32_t, SymIndexId> GlobalOffsetToSymbolId;
71
72 /// Map from segment and code offset to function symbols.
73 mutable DenseMap<std::pair<uint32_t, uint32_t>, SymIndexId> AddressToSymbolId;
74 /// Map from segment and code offset to public symbols.
76 AddressToPublicSymId;
77
78 /// Map from module index and symbol table offset to SymIndexId.
80 SymTabOffsetToSymbolId;
81
82 struct LineTableEntry {
85 uint32_t ColumnNumber;
86 uint32_t FileNameIndex;
87 bool IsTerminalEntry;
88 };
89
90 std::vector<LineTableEntry> findLineTable(uint16_t Modi) const;
92
93 SymIndexId createSymbolPlaceholder() const {
94 SymIndexId Id = Cache.size();
95 Cache.push_back(nullptr);
96 return Id;
97 }
98
99 template <typename ConcreteSymbolT, typename CVRecordT, typename... Args>
100 SymIndexId createSymbolForType(codeview::TypeIndex TI, codeview::CVType CVT,
101 Args &&...ConstructorArgs) const {
102 CVRecordT Record;
103 if (auto EC =
104 codeview::TypeDeserializer::deserializeAs<CVRecordT>(CVT, Record)) {
105 consumeError(std::move(EC));
106 return 0;
107 }
108
109 return createSymbol<ConcreteSymbolT>(
110 TI, std::move(Record), std::forward<Args>(ConstructorArgs)...);
111 }
112
113 SymIndexId createSymbolForModifiedType(codeview::TypeIndex ModifierTI,
114 codeview::CVType CVT) const;
115
116 SymIndexId createSimpleType(codeview::TypeIndex TI,
117 codeview::ModifierOptions Mods) const;
118
119 std::unique_ptr<PDBSymbol> findFunctionSymbolBySectOffset(uint32_t Sect,
121 std::unique_ptr<PDBSymbol> findPublicSymbolBySectOffset(uint32_t Sect,
123
124public:
126
127 template <typename ConcreteSymbolT, typename... Args>
128 SymIndexId createSymbol(Args &&...ConstructorArgs) const {
129 SymIndexId Id = Cache.size();
130
131 // Initial construction must not access the cache, since it must be done
132 // atomically.
133 auto Result = std::make_unique<ConcreteSymbolT>(
134 Session, Id, std::forward<Args>(ConstructorArgs)...);
135 Result->SymbolId = Id;
136
137 NativeRawSymbol *NRS = static_cast<NativeRawSymbol *>(Result.get());
138 Cache.push_back(std::move(Result));
139
140 // After the item is in the cache, we can do further initialization which
141 // is then allowed to access the cache.
142 NRS->initialize();
143 return Id;
144 }
145
146 LLVM_ABI std::unique_ptr<IPDBEnumSymbols>
148
149 LLVM_ABI std::unique_ptr<IPDBEnumSymbols>
150 createTypeEnumerator(std::vector<codeview::TypeLeafKind> Kinds);
151
152 LLVM_ABI std::unique_ptr<IPDBEnumSymbols>
154
156
157 template <typename ConcreteSymbolT, typename... Args>
160 Args &&... ConstructorArgs) {
161 SymIndexId SymId = Cache.size();
162 std::pair<codeview::TypeIndex, uint32_t> Key{FieldListTI, Index};
163 auto Result = FieldListMembersToSymbolId.try_emplace(Key, SymId);
164 if (Result.second)
165 SymId =
166 createSymbol<ConcreteSymbolT>(std::forward<Args>(ConstructorArgs)...);
167 else
168 SymId = Result.first->second;
169 return SymId;
170 }
171
174 uint64_t ParentAddr,
175 uint16_t Modi,
176 uint32_t RecordOffset) const;
177
178 LLVM_ABI std::unique_ptr<PDBSymbol>
180
181 LLVM_ABI std::unique_ptr<IPDBEnumLineNumbers>
183
184 LLVM_ABI std::unique_ptr<PDBSymbolCompiland>
187
188 LLVM_ABI std::unique_ptr<PDBSymbol> getSymbolById(SymIndexId SymbolId) const;
189
191
192 template <typename ConcreteT>
193 ConcreteT &getNativeSymbolById(SymIndexId SymbolId) const {
194 return static_cast<ConcreteT &>(getNativeSymbolById(SymbolId));
195 }
196
197 LLVM_ABI std::unique_ptr<IPDBSourceFile>
198 getSourceFileById(SymIndexId FileId) const;
201};
202
203} // namespace pdb
204} // namespace llvm
205
206#endif
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
uint64_t Addr
uint32_t Index
Symbol * Sym
Definition: ELF_riscv.cpp:479
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:245
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A 32-bit type reference.
Definition: TypeIndex.h:97
LLVM_ABI std::unique_ptr< IPDBEnumSymbols > createGlobalsEnumerator(codeview::SymbolKind Kind)
LLVM_ABI SymIndexId getOrCreateInlineSymbol(codeview::InlineSiteSym Sym, uint64_t ParentAddr, uint16_t Modi, uint32_t RecordOffset) const
SymIndexId getOrCreateFieldListMember(codeview::TypeIndex FieldListTI, uint32_t Index, Args &&... ConstructorArgs)
Definition: SymbolCache.h:158
LLVM_ABI std::unique_ptr< IPDBEnumSymbols > createTypeEnumerator(codeview::TypeLeafKind Kind)
Definition: SymbolCache.cpp:89
LLVM_ABI std::unique_ptr< PDBSymbol > getSymbolById(SymIndexId SymbolId) const
LLVM_ABI SymIndexId getOrCreateSourceFile(const codeview::FileChecksumEntry &Checksum) const
LLVM_ABI SymIndexId findSymbolByTypeIndex(codeview::TypeIndex TI) const
LLVM_ABI NativeRawSymbol & getNativeSymbolById(SymIndexId SymbolId) const
ConcreteT & getNativeSymbolById(SymIndexId SymbolId) const
Definition: SymbolCache.h:193
LLVM_ABI std::unique_ptr< PDBSymbolCompiland > getOrCreateCompiland(uint32_t Index)
LLVM_ABI uint32_t getNumCompilands() const
LLVM_ABI std::unique_ptr< IPDBSourceFile > getSourceFileById(SymIndexId FileId) const
LLVM_ABI std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByVA(uint64_t VA, uint32_t Length) const
SymIndexId createSymbol(Args &&...ConstructorArgs) const
Definition: SymbolCache.h:128
LLVM_ABI std::unique_ptr< PDBSymbol > findSymbolBySectOffset(uint32_t Sect, uint32_t Offset, PDB_SymType Type)
LLVM_ABI SymIndexId getOrCreateGlobalSymbolByOffset(uint32_t Offset)
TypeLeafKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:35
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:49
ModifierOptions
Equivalent to CV_modifier_t.
Definition: CodeView.h:284
uint32_t SymIndexId
Definition: PDBTypes.h:26
PDB_SymType
These values correspond to the SymTagEnum enumeration, and are documented here: https://msdn....
Definition: PDBTypes.h:243
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:477
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083