1 //===-- SymbolFileSymtab.cpp ----------------------------------------------===//
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 #include "SymbolFileSymtab.h"
10
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Symbol/CompileUnit.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Symbol/Symbol.h"
17 #include "lldb/Symbol/SymbolContext.h"
18 #include "lldb/Symbol/Symtab.h"
19 #include "lldb/Symbol/TypeList.h"
20 #include "lldb/Utility/RegularExpression.h"
21 #include "lldb/Utility/Timer.h"
22
23 #include <memory>
24
25 using namespace lldb;
26 using namespace lldb_private;
27
28 LLDB_PLUGIN_DEFINE(SymbolFileSymtab)
29
30 char SymbolFileSymtab::ID;
31
Initialize()32 void SymbolFileSymtab::Initialize() {
33 PluginManager::RegisterPlugin(GetPluginNameStatic(),
34 GetPluginDescriptionStatic(), CreateInstance);
35 }
36
Terminate()37 void SymbolFileSymtab::Terminate() {
38 PluginManager::UnregisterPlugin(CreateInstance);
39 }
40
GetPluginDescriptionStatic()41 llvm::StringRef SymbolFileSymtab::GetPluginDescriptionStatic() {
42 return "Reads debug symbols from an object file's symbol table.";
43 }
44
CreateInstance(ObjectFileSP objfile_sp)45 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFileSP objfile_sp) {
46 return new SymbolFileSymtab(std::move(objfile_sp));
47 }
48
GetTypes(SymbolContextScope * sc_scope,TypeClass type_mask,lldb_private::TypeList & type_list)49 void SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
50 TypeClass type_mask,
51 lldb_private::TypeList &type_list) {}
52
SymbolFileSymtab(ObjectFileSP objfile_sp)53 SymbolFileSymtab::SymbolFileSymtab(ObjectFileSP objfile_sp)
54 : SymbolFileCommon(std::move(objfile_sp)), m_source_indexes(),
55 m_func_indexes(), m_code_indexes(), m_objc_class_name_to_index() {}
56
CalculateAbilities()57 uint32_t SymbolFileSymtab::CalculateAbilities() {
58 uint32_t abilities = 0;
59 if (m_objfile_sp) {
60 const Symtab *symtab = m_objfile_sp->GetSymtab();
61 if (symtab) {
62 // The snippet of code below will get the indexes the module symbol table
63 // entries that are code, data, or function related (debug info), sort
64 // them by value (address) and dump the sorted symbols.
65 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,
66 m_source_indexes)) {
67 abilities |= CompileUnits;
68 }
69
70 if (symtab->AppendSymbolIndexesWithType(
71 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
72 m_func_indexes)) {
73 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
74 abilities |= Functions;
75 }
76
77 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
78 Symtab::eVisibilityAny,
79 m_code_indexes)) {
80 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
81 abilities |= Functions;
82 }
83
84 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,
85 m_data_indexes)) {
86 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
87 abilities |= GlobalVariables;
88 }
89
90 lldb_private::Symtab::IndexCollection objc_class_indexes;
91 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass,
92 objc_class_indexes)) {
93 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
94 m_objc_class_name_to_index);
95 m_objc_class_name_to_index.Sort();
96 }
97 }
98 }
99 return abilities;
100 }
101
CalculateNumCompileUnits()102 uint32_t SymbolFileSymtab::CalculateNumCompileUnits() {
103 // If we don't have any source file symbols we will just have one compile
104 // unit for the entire object file
105 if (m_source_indexes.empty())
106 return 0;
107
108 // If we have any source file symbols we will logically organize the object
109 // symbols using these.
110 return m_source_indexes.size();
111 }
112
ParseCompileUnitAtIndex(uint32_t idx)113 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
114 CompUnitSP cu_sp;
115
116 // If we don't have any source file symbols we will just have one compile
117 // unit for the entire object file
118 if (idx < m_source_indexes.size()) {
119 const Symbol *cu_symbol =
120 m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
121 if (cu_symbol)
122 cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
123 cu_symbol->GetName().AsCString(), 0,
124 eLanguageTypeUnknown, eLazyBoolNo);
125 }
126 return cu_sp;
127 }
128
ParseLanguage(CompileUnit & comp_unit)129 lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) {
130 return eLanguageTypeUnknown;
131 }
132
ParseFunctions(CompileUnit & comp_unit)133 size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) {
134 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
135 size_t num_added = 0;
136 // We must at least have a valid compile unit
137 const Symtab *symtab = m_objfile_sp->GetSymtab();
138 const Symbol *curr_symbol = nullptr;
139 const Symbol *next_symbol = nullptr;
140 // const char *prefix = m_objfile_sp->SymbolPrefix();
141 // if (prefix == NULL)
142 // prefix == "";
143 //
144 // const uint32_t prefix_len = strlen(prefix);
145
146 // If we don't have any source file symbols we will just have one compile
147 // unit for the entire object file
148 if (m_source_indexes.empty()) {
149 // The only time we will have a user ID of zero is when we don't have and
150 // source file symbols and we declare one compile unit for the entire
151 // object file
152 if (!m_func_indexes.empty()) {
153 }
154
155 if (!m_code_indexes.empty()) {
156 // StreamFile s(stdout);
157 // symtab->Dump(&s, m_code_indexes);
158
159 uint32_t idx = 0; // Index into the indexes
160 const uint32_t num_indexes = m_code_indexes.size();
161 for (idx = 0; idx < num_indexes; ++idx) {
162 uint32_t symbol_idx = m_code_indexes[idx];
163 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
164 if (curr_symbol) {
165 // Union of all ranges in the function DIE (if the function is
166 // discontiguous)
167 AddressRange func_range(curr_symbol->GetAddress(), 0);
168 if (func_range.GetBaseAddress().IsSectionOffset()) {
169 uint32_t symbol_size = curr_symbol->GetByteSize();
170 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
171 func_range.SetByteSize(symbol_size);
172 else if (idx + 1 < num_indexes) {
173 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
174 if (next_symbol) {
175 func_range.SetByteSize(
176 next_symbol->GetAddressRef().GetOffset() -
177 curr_symbol->GetAddressRef().GetOffset());
178 }
179 }
180
181 FunctionSP func_sp(
182 new Function(&comp_unit,
183 symbol_idx, // UserID is the DIE offset
184 LLDB_INVALID_UID, // We don't have any type info
185 // for this function
186 curr_symbol->GetMangled(), // Linker/mangled name
187 nullptr, // no return type for a code symbol...
188 func_range)); // first address range
189
190 if (func_sp.get() != nullptr) {
191 comp_unit.AddFunction(func_sp);
192 ++num_added;
193 }
194 }
195 }
196 }
197 }
198 } else {
199 // We assume we
200 }
201 return num_added;
202 }
203
ParseTypes(CompileUnit & comp_unit)204 size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
205
ParseLineTable(CompileUnit & comp_unit)206 bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
207
ParseDebugMacros(CompileUnit & comp_unit)208 bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) {
209 return false;
210 }
211
ParseSupportFiles(CompileUnit & comp_unit,FileSpecList & support_files)212 bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit,
213 FileSpecList &support_files) {
214 return false;
215 }
216
ParseImportedModules(const SymbolContext & sc,std::vector<SourceModule> & imported_modules)217 bool SymbolFileSymtab::ParseImportedModules(
218 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
219 return false;
220 }
221
ParseBlocksRecursive(Function & func)222 size_t SymbolFileSymtab::ParseBlocksRecursive(Function &func) { return 0; }
223
ParseVariablesForContext(const SymbolContext & sc)224 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
225 return 0;
226 }
227
ResolveTypeUID(lldb::user_id_t type_uid)228 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
229 return nullptr;
230 }
231
232 llvm::Optional<SymbolFile::ArrayInfo>
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)233 SymbolFileSymtab::GetDynamicArrayInfoForUID(
234 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
235 return llvm::None;
236 }
237
CompleteType(lldb_private::CompilerType & compiler_type)238 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
239 return false;
240 }
241
ResolveSymbolContext(const Address & so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)242 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
243 SymbolContextItem resolve_scope,
244 SymbolContext &sc) {
245 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
246 if (m_objfile_sp->GetSymtab() == nullptr)
247 return 0;
248
249 uint32_t resolved_flags = 0;
250 if (resolve_scope & eSymbolContextSymbol) {
251 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
252 so_addr.GetFileAddress());
253 if (sc.symbol)
254 resolved_flags |= eSymbolContextSymbol;
255 }
256 return resolved_flags;
257 }
258