1 //===-- SymbolFileSymtab.cpp ------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SymbolFileSymtab.h"
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"
26 using namespace lldb_private;
28 void SymbolFileSymtab::Initialize() {
29 PluginManager::RegisterPlugin(GetPluginNameStatic(),
30 GetPluginDescriptionStatic(), CreateInstance);
33 void SymbolFileSymtab::Terminate() {
34 PluginManager::UnregisterPlugin(CreateInstance);
37 lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() {
38 static ConstString g_name("symtab");
42 const char *SymbolFileSymtab::GetPluginDescriptionStatic() {
43 return "Reads debug symbols from an object file's symbol table.";
46 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFileSP objfile_sp) {
47 return new SymbolFileSymtab(std::move(objfile_sp));
50 void SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
52 lldb_private::TypeList &type_list) {}
54 SymbolFileSymtab::SymbolFileSymtab(ObjectFileSP objfile_sp)
55 : SymbolFile(std::move(objfile_sp)), m_source_indexes(), m_func_indexes(),
56 m_code_indexes(), m_objc_class_name_to_index() {}
58 SymbolFileSymtab::~SymbolFileSymtab() {}
60 uint32_t SymbolFileSymtab::CalculateAbilities() {
61 uint32_t abilities = 0;
63 const Symtab *symtab = m_objfile_sp->GetSymtab();
65 // The snippet of code below will get the indexes the module symbol table
66 // entries that are code, data, or function related (debug info), sort
67 // them by value (address) and dump the sorted symbols.
68 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,
70 abilities |= CompileUnits;
73 if (symtab->AppendSymbolIndexesWithType(
74 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
76 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
77 abilities |= Functions;
80 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
81 Symtab::eVisibilityAny,
83 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
84 abilities |= Functions;
87 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,
89 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
90 abilities |= GlobalVariables;
93 lldb_private::Symtab::IndexCollection objc_class_indexes;
94 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass,
95 objc_class_indexes)) {
96 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
97 m_objc_class_name_to_index);
98 m_objc_class_name_to_index.Sort();
105 uint32_t SymbolFileSymtab::CalculateNumCompileUnits() {
106 // If we don't have any source file symbols we will just have one compile
107 // unit for the entire object file
108 if (m_source_indexes.empty())
111 // If we have any source file symbols we will logically organize the object
112 // symbols using these.
113 return m_source_indexes.size();
116 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
119 // If we don't have any source file symbols we will just have one compile
120 // unit for the entire object file
121 if (idx < m_source_indexes.size()) {
122 const Symbol *cu_symbol =
123 m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
125 cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
126 cu_symbol->GetName().AsCString(), 0,
127 eLanguageTypeUnknown, eLazyBoolNo);
132 lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) {
133 return eLanguageTypeUnknown;
136 size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) {
137 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
138 size_t num_added = 0;
139 // We must at least have a valid compile unit
140 const Symtab *symtab = m_objfile_sp->GetSymtab();
141 const Symbol *curr_symbol = nullptr;
142 const Symbol *next_symbol = nullptr;
143 // const char *prefix = m_objfile_sp->SymbolPrefix();
144 // if (prefix == NULL)
147 // const uint32_t prefix_len = strlen(prefix);
149 // If we don't have any source file symbols we will just have one compile
150 // unit for the entire object file
151 if (m_source_indexes.empty()) {
152 // The only time we will have a user ID of zero is when we don't have and
153 // source file symbols and we declare one compile unit for the entire
155 if (!m_func_indexes.empty()) {
158 if (!m_code_indexes.empty()) {
159 // StreamFile s(stdout);
160 // symtab->Dump(&s, m_code_indexes);
162 uint32_t idx = 0; // Index into the indexes
163 const uint32_t num_indexes = m_code_indexes.size();
164 for (idx = 0; idx < num_indexes; ++idx) {
165 uint32_t symbol_idx = m_code_indexes[idx];
166 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
168 // Union of all ranges in the function DIE (if the function is
170 AddressRange func_range(curr_symbol->GetAddress(), 0);
171 if (func_range.GetBaseAddress().IsSectionOffset()) {
172 uint32_t symbol_size = curr_symbol->GetByteSize();
173 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
174 func_range.SetByteSize(symbol_size);
175 else if (idx + 1 < num_indexes) {
176 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
178 func_range.SetByteSize(
179 next_symbol->GetAddressRef().GetOffset() -
180 curr_symbol->GetAddressRef().GetOffset());
185 new Function(&comp_unit,
186 symbol_idx, // UserID is the DIE offset
187 LLDB_INVALID_UID, // We don't have any type info
189 curr_symbol->GetMangled(), // Linker/mangled name
190 nullptr, // no return type for a code symbol...
191 func_range)); // first address range
193 if (func_sp.get() != nullptr) {
194 comp_unit.AddFunction(func_sp);
207 size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
209 bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
211 bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) {
215 bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit,
216 FileSpecList &support_files) {
220 bool SymbolFileSymtab::ParseImportedModules(
221 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
225 size_t SymbolFileSymtab::ParseBlocksRecursive(Function &func) { return 0; }
227 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
231 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
235 llvm::Optional<SymbolFile::ArrayInfo>
236 SymbolFileSymtab::GetDynamicArrayInfoForUID(
237 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
241 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
245 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
246 SymbolContextItem resolve_scope,
248 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
249 if (m_objfile_sp->GetSymtab() == nullptr)
252 uint32_t resolved_flags = 0;
253 if (resolve_scope & eSymbolContextSymbol) {
254 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
255 so_addr.GetFileAddress());
257 resolved_flags |= eSymbolContextSymbol;
259 return resolved_flags;
262 // PluginInterface protocol
263 lldb_private::ConstString SymbolFileSymtab::GetPluginName() {
264 return GetPluginNameStatic();
267 uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; }