blob: d4732fd69eebfe1072bdbab56f5bd86d62db49c4 [file] [log] [blame]
Zachary Turner8ab7dd602018-09-07 00:12:34 +00001#include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
2
Zachary Turner4d689512018-09-14 21:03:57 +00003#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner355ffb02018-09-21 22:36:04 +00004#include "llvm/DebugInfo/CodeView/TypeRecordHelpers.h"
Zachary Turner8ab7dd602018-09-07 00:12:34 +00005#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
6#include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
Zachary Turner8ab7dd602018-09-07 00:12:34 +00007#include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
8#include "llvm/DebugInfo/PDB/Native/NativeRawSymbol.h"
9#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
Zachary Turner5d629962018-09-07 00:12:56 +000010#include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
11#include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
Zachary Turner6345e842018-09-21 22:36:28 +000012#include "llvm/DebugInfo/PDB/Native/NativeTypeFunctionSig.h"
Zachary Turnerda4b63a2018-09-07 23:21:33 +000013#include "llvm/DebugInfo/PDB/Native/NativeTypePointer.h"
Zachary Turner355ffb02018-09-21 22:36:04 +000014#include "llvm/DebugInfo/PDB/Native/NativeTypeUDT.h"
Zachary Turner8ab7dd602018-09-07 00:12:34 +000015#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
16#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
17#include "llvm/DebugInfo/PDB/PDBSymbol.h"
18#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
19#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
20
21using namespace llvm;
Zachary Turner4d689512018-09-14 21:03:57 +000022using namespace llvm::codeview;
Zachary Turner8ab7dd602018-09-07 00:12:34 +000023using namespace llvm::pdb;
24
25// Maps codeview::SimpleTypeKind of a built-in type to the parameters necessary
26// to instantiate a NativeBuiltinSymbol for that type.
27static const struct BuiltinTypeEntry {
28 codeview::SimpleTypeKind Kind;
29 PDB_BuiltinType Type;
30 uint32_t Size;
31} BuiltinTypes[] = {
Zachary Turner5907a782018-09-20 16:12:05 +000032 {codeview::SimpleTypeKind::None, PDB_BuiltinType::None, 0},
Zachary Turner6345e842018-09-21 22:36:28 +000033 {codeview::SimpleTypeKind::Void, PDB_BuiltinType::Void, 0},
Zachary Turnerbdf03812018-09-17 21:08:11 +000034 {codeview::SimpleTypeKind::Int16Short, PDB_BuiltinType::Int, 2},
35 {codeview::SimpleTypeKind::UInt16Short, PDB_BuiltinType::UInt, 2},
Zachary Turner8ab7dd602018-09-07 00:12:34 +000036 {codeview::SimpleTypeKind::Int32, PDB_BuiltinType::Int, 4},
37 {codeview::SimpleTypeKind::UInt32, PDB_BuiltinType::UInt, 4},
Zachary Turnerbdf03812018-09-17 21:08:11 +000038 {codeview::SimpleTypeKind::Int32Long, PDB_BuiltinType::Int, 4},
Zachary Turner8ab7dd602018-09-07 00:12:34 +000039 {codeview::SimpleTypeKind::UInt32Long, PDB_BuiltinType::UInt, 4},
Zachary Turnerbdf03812018-09-17 21:08:11 +000040 {codeview::SimpleTypeKind::Int64Quad, PDB_BuiltinType::Int, 8},
Zachary Turner8ab7dd602018-09-07 00:12:34 +000041 {codeview::SimpleTypeKind::UInt64Quad, PDB_BuiltinType::UInt, 8},
42 {codeview::SimpleTypeKind::NarrowCharacter, PDB_BuiltinType::Char, 1},
43 {codeview::SimpleTypeKind::SignedCharacter, PDB_BuiltinType::Char, 1},
44 {codeview::SimpleTypeKind::UnsignedCharacter, PDB_BuiltinType::UInt, 1},
Zachary Turner8ab7dd602018-09-07 00:12:34 +000045 {codeview::SimpleTypeKind::Boolean8, PDB_BuiltinType::Bool, 1}
46 // This table can be grown as necessary, but these are the only types we've
47 // needed so far.
48};
49
50SymbolCache::SymbolCache(NativeSession &Session, DbiStream *Dbi)
51 : Session(Session), Dbi(Dbi) {
52 // Id 0 is reserved for the invalid symbol.
53 Cache.push_back(nullptr);
54
55 if (Dbi)
56 Compilands.resize(Dbi->modules().getModuleCount());
Zachary Turner355ffb02018-09-21 22:36:04 +000057
58 auto &Tpi = cantFail(Session.getPDBFile().getPDBTpiStream());
59 Tpi.buildHashMap();
Zachary Turner8ab7dd602018-09-07 00:12:34 +000060}
61
Zachary Turner8ab7dd602018-09-07 00:12:34 +000062std::unique_ptr<IPDBEnumSymbols>
Zachary Turner355ffb02018-09-21 22:36:04 +000063SymbolCache::createTypeEnumerator(TypeLeafKind Kind) {
64 return createTypeEnumerator(std::vector<TypeLeafKind>{Kind});
65}
66
67std::unique_ptr<IPDBEnumSymbols>
68SymbolCache::createTypeEnumerator(std::vector<TypeLeafKind> Kinds) {
Zachary Turner8ab7dd602018-09-07 00:12:34 +000069 auto Tpi = Session.getPDBFile().getPDBTpiStream();
70 if (!Tpi) {
71 consumeError(Tpi.takeError());
72 return nullptr;
73 }
74 auto &Types = Tpi->typeCollection();
75 return std::unique_ptr<IPDBEnumSymbols>(
Zachary Turner355ffb02018-09-21 22:36:04 +000076 new NativeEnumTypes(Session, Types, std::move(Kinds)));
Zachary Turner8ab7dd602018-09-07 00:12:34 +000077}
78
Zachary Turner4727ac22018-09-17 21:07:48 +000079SymIndexId SymbolCache::createSimpleType(TypeIndex Index,
80 ModifierOptions Mods) {
Zachary Turner6345e842018-09-21 22:36:28 +000081 if (Index.getSimpleMode() != codeview::SimpleTypeMode::Direct)
82 return createSymbol<NativeTypePointer>(Index);
Zachary Turner4727ac22018-09-17 21:07:48 +000083
84 const auto Kind = Index.getSimpleKind();
85 const auto It = std::find_if(
86 std::begin(BuiltinTypes), std::end(BuiltinTypes),
87 [Kind](const BuiltinTypeEntry &Builtin) { return Builtin.Kind == Kind; });
88 if (It == std::end(BuiltinTypes))
89 return 0;
Zachary Turner6345e842018-09-21 22:36:28 +000090 return createSymbol<NativeTypeBuiltin>(Mods, It->Type, It->Size);
Zachary Turner4727ac22018-09-17 21:07:48 +000091}
92
93SymIndexId
94SymbolCache::createSymbolForModifiedType(codeview::TypeIndex ModifierTI,
95 codeview::CVType CVT) {
96 ModifierRecord Record;
97 if (auto EC = TypeDeserializer::deserializeAs<ModifierRecord>(CVT, Record)) {
98 consumeError(std::move(EC));
99 return 0;
100 }
101
102 if (Record.ModifiedType.isSimple())
103 return createSimpleType(Record.ModifiedType, Record.Modifiers);
104
Zachary Turner355ffb02018-09-21 22:36:04 +0000105 // Make sure we create and cache a record for the unmodified type.
106 SymIndexId UnmodifiedId = findSymbolByTypeIndex(Record.ModifiedType);
107 NativeRawSymbol &UnmodifiedNRS = *Cache[UnmodifiedId];
Zachary Turner4727ac22018-09-17 21:07:48 +0000108
Zachary Turner355ffb02018-09-21 22:36:04 +0000109 switch (UnmodifiedNRS.getSymTag()) {
110 case PDB_SymType::Enum:
111 return createSymbol<NativeTypeEnum>(
112 static_cast<NativeTypeEnum &>(UnmodifiedNRS), std::move(Record));
113 case PDB_SymType::UDT:
114 return createSymbol<NativeTypeUDT>(
115 static_cast<NativeTypeUDT &>(UnmodifiedNRS), std::move(Record));
Zachary Turner4727ac22018-09-17 21:07:48 +0000116 default:
117 // No other types can be modified. (LF_POINTER, for example, records
118 // its modifiers a different way.
119 assert(false && "Invalid LF_MODIFIER record");
120 break;
121 }
Zachary Turner355ffb02018-09-21 22:36:04 +0000122 return 0;
Zachary Turner4727ac22018-09-17 21:07:48 +0000123}
124
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000125SymIndexId SymbolCache::findSymbolByTypeIndex(codeview::TypeIndex Index) {
126 // First see if it's already in our cache.
127 const auto Entry = TypeIndexToSymbolId.find(Index);
128 if (Entry != TypeIndexToSymbolId.end())
129 return Entry->second;
130
131 // Symbols for built-in types are created on the fly.
Zachary Turner6345e842018-09-21 22:36:28 +0000132 if (Index.isSimple()) {
133 SymIndexId Result = createSimpleType(Index, ModifierOptions::None);
134 assert(TypeIndexToSymbolId.count(Index) == 0);
135 TypeIndexToSymbolId[Index] = Result;
136 return Result;
137 }
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000138
139 // We need to instantiate and cache the desired type symbol.
140 auto Tpi = Session.getPDBFile().getPDBTpiStream();
141 if (!Tpi) {
142 consumeError(Tpi.takeError());
143 return 0;
144 }
145 codeview::LazyRandomTypeCollection &Types = Tpi->typeCollection();
Zachary Turner4d689512018-09-14 21:03:57 +0000146 codeview::CVType CVT = Types.getType(Index);
Zachary Turner4727ac22018-09-17 21:07:48 +0000147
Zachary Turner355ffb02018-09-21 22:36:04 +0000148 if (isUdtForwardRef(CVT)) {
149 Expected<TypeIndex> EFD = Tpi->findFullDeclForForwardRef(Index);
150
151 if (!EFD)
152 consumeError(EFD.takeError());
153 else if (*EFD != Index) {
154 assert(!isUdtForwardRef(Types.getType(*EFD)));
155 SymIndexId Result = findSymbolByTypeIndex(*EFD);
156 // Record a mapping from ForwardRef -> SymIndex of complete type so that
157 // we'll take the fast path next time.
Zachary Turner6345e842018-09-21 22:36:28 +0000158 assert(TypeIndexToSymbolId.count(Index) == 0);
Zachary Turner355ffb02018-09-21 22:36:04 +0000159 TypeIndexToSymbolId[Index] = Result;
160 return Result;
161 }
162 }
163
164 // At this point if we still have a forward ref udt it means the full decl was
165 // not in the PDB. We just have to deal with it and use the forward ref.
166 SymIndexId Id = 0;
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000167 switch (CVT.kind()) {
168 case codeview::LF_ENUM:
Zachary Turner4d689512018-09-14 21:03:57 +0000169 Id = createSymbolForType<NativeTypeEnum, EnumRecord>(Index, std::move(CVT));
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000170 break;
Zachary Turner355ffb02018-09-21 22:36:04 +0000171 case codeview::LF_CLASS:
172 case codeview::LF_STRUCTURE:
173 case codeview::LF_INTERFACE:
174 Id = createSymbolForType<NativeTypeUDT, ClassRecord>(Index, std::move(CVT));
175 break;
176 case codeview::LF_UNION:
177 Id = createSymbolForType<NativeTypeUDT, UnionRecord>(Index, std::move(CVT));
178 break;
Zachary Turnerda4b63a2018-09-07 23:21:33 +0000179 case codeview::LF_POINTER:
Zachary Turner4d689512018-09-14 21:03:57 +0000180 Id = createSymbolForType<NativeTypePointer, PointerRecord>(Index,
181 std::move(CVT));
Zachary Turnerda4b63a2018-09-07 23:21:33 +0000182 break;
Zachary Turner4727ac22018-09-17 21:07:48 +0000183 case codeview::LF_MODIFIER:
184 Id = createSymbolForModifiedType(Index, std::move(CVT));
185 break;
Zachary Turner6345e842018-09-21 22:36:28 +0000186 case codeview::LF_PROCEDURE:
187 Id = createSymbolForType<NativeTypeFunctionSig, ProcedureRecord>(
188 Index, std::move(CVT));
189 break;
190 case codeview::LF_MFUNCTION:
191 Id = createSymbolForType<NativeTypeFunctionSig, MemberFunctionRecord>(
192 Index, std::move(CVT));
193 break;
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000194 default:
Zachary Turnerda4b63a2018-09-07 23:21:33 +0000195 Id = createSymbolPlaceholder();
196 break;
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000197 }
Zachary Turner6345e842018-09-21 22:36:28 +0000198 if (Id != 0) {
199 assert(TypeIndexToSymbolId.count(Index) == 0);
Zachary Turner4d689512018-09-14 21:03:57 +0000200 TypeIndexToSymbolId[Index] = Id;
Zachary Turner6345e842018-09-21 22:36:28 +0000201 }
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000202 return Id;
203}
204
205std::unique_ptr<PDBSymbol>
206SymbolCache::getSymbolById(SymIndexId SymbolId) const {
Zachary Turnerbdf03812018-09-17 21:08:11 +0000207 assert(SymbolId < Cache.size());
208
Zachary Turnerda4b63a2018-09-07 23:21:33 +0000209 // Id 0 is reserved.
Zachary Turnerc41ce832018-09-18 16:35:05 +0000210 if (SymbolId == 0 || SymbolId >= Cache.size())
Zachary Turnerda4b63a2018-09-07 23:21:33 +0000211 return nullptr;
212
Zachary Turnerc41ce832018-09-18 16:35:05 +0000213 // Make sure to handle the case where we've inserted a placeholder symbol
214 // for types we don't yet suppport.
215 NativeRawSymbol *NRS = Cache[SymbolId].get();
216 if (!NRS)
217 return nullptr;
218
219 return PDBSymbol::create(Session, *NRS);
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000220}
221
222NativeRawSymbol &SymbolCache::getNativeSymbolById(SymIndexId SymbolId) const {
223 return *Cache[SymbolId];
224}
225
226uint32_t SymbolCache::getNumCompilands() const {
227 if (!Dbi)
228 return 0;
229
230 return Dbi->modules().getModuleCount();
231}
232
233std::unique_ptr<PDBSymbolCompiland>
234SymbolCache::getOrCreateCompiland(uint32_t Index) {
235 if (!Dbi)
236 return nullptr;
237
238 if (Index >= Compilands.size())
239 return nullptr;
240
241 if (Compilands[Index] == 0) {
242 const DbiModuleList &Modules = Dbi->modules();
243 Compilands[Index] =
244 createSymbol<NativeCompilandSymbol>(Modules.getModuleDescriptor(Index));
245 }
246
247 return Session.getConcreteSymbolById<PDBSymbolCompiland>(Compilands[Index]);
248}