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(ObjectFile *obj_file) {
47 return new SymbolFileSymtab(obj_file);
50 size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
52 lldb_private::TypeList &type_list) {
56 SymbolFileSymtab::SymbolFileSymtab(ObjectFile *obj_file)
57 : SymbolFile(obj_file), m_source_indexes(), m_func_indexes(),
58 m_code_indexes(), m_objc_class_name_to_index() {}
60 SymbolFileSymtab::~SymbolFileSymtab() {}
62 uint32_t SymbolFileSymtab::CalculateAbilities() {
63 uint32_t abilities = 0;
65 const Symtab *symtab = m_obj_file->GetSymtab();
67 // The snippet of code below will get the indexes the module symbol table
68 // entries that are code, data, or function related (debug info), sort
69 // them by value (address) and dump the sorted symbols.
70 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,
72 abilities |= CompileUnits;
75 if (symtab->AppendSymbolIndexesWithType(
76 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
78 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
79 abilities |= Functions;
82 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
83 Symtab::eVisibilityAny,
85 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
86 abilities |= Functions;
89 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,
91 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
92 abilities |= GlobalVariables;
95 lldb_private::Symtab::IndexCollection objc_class_indexes;
96 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass,
97 objc_class_indexes)) {
98 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
99 m_objc_class_name_to_index);
100 m_objc_class_name_to_index.Sort();
107 uint32_t SymbolFileSymtab::GetNumCompileUnits() {
108 // If we don't have any source file symbols we will just have one compile
109 // unit for the entire object file
110 if (m_source_indexes.empty())
113 // If we have any source file symbols we will logically organize the object
114 // symbols using these.
115 return m_source_indexes.size();
118 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
121 // If we don't have any source file symbols we will just have one compile
122 // unit for the entire object file
123 if (idx < m_source_indexes.size()) {
124 const Symbol *cu_symbol =
125 m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
127 cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
128 cu_symbol->GetName().AsCString(), 0,
129 eLanguageTypeUnknown, eLazyBoolNo);
134 lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) {
135 return eLanguageTypeUnknown;
138 size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) {
139 size_t num_added = 0;
140 // We must at least have a valid compile unit
141 const Symtab *symtab = m_obj_file->GetSymtab();
142 const Symbol *curr_symbol = nullptr;
143 const Symbol *next_symbol = nullptr;
144 // const char *prefix = m_obj_file->SymbolPrefix();
145 // if (prefix == NULL)
148 // const uint32_t prefix_len = strlen(prefix);
150 // If we don't have any source file symbols we will just have one compile
151 // unit for the entire object file
152 if (m_source_indexes.empty()) {
153 // The only time we will have a user ID of zero is when we don't have and
154 // source file symbols and we declare one compile unit for the entire
156 if (!m_func_indexes.empty()) {
159 if (!m_code_indexes.empty()) {
160 // StreamFile s(stdout);
161 // symtab->Dump(&s, m_code_indexes);
163 uint32_t idx = 0; // Index into the indexes
164 const uint32_t num_indexes = m_code_indexes.size();
165 for (idx = 0; idx < num_indexes; ++idx) {
166 uint32_t symbol_idx = m_code_indexes[idx];
167 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
169 // Union of all ranges in the function DIE (if the function is
171 AddressRange func_range(curr_symbol->GetAddress(), 0);
172 if (func_range.GetBaseAddress().IsSectionOffset()) {
173 uint32_t symbol_size = curr_symbol->GetByteSize();
174 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
175 func_range.SetByteSize(symbol_size);
176 else if (idx + 1 < num_indexes) {
177 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
179 func_range.SetByteSize(
180 next_symbol->GetAddressRef().GetOffset() -
181 curr_symbol->GetAddressRef().GetOffset());
186 new Function(&comp_unit,
187 symbol_idx, // UserID is the DIE offset
188 LLDB_INVALID_UID, // We don't have any type info
190 curr_symbol->GetMangled(), // Linker/mangled name
191 nullptr, // no return type for a code symbol...
192 func_range)); // first address range
194 if (func_sp.get() != nullptr) {
195 comp_unit.AddFunction(func_sp);
208 size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
210 bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
212 bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) {
216 bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit,
217 FileSpecList &support_files) {
221 bool SymbolFileSymtab::ParseImportedModules(
222 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
226 size_t SymbolFileSymtab::ParseBlocksRecursive(Function &func) { return 0; }
228 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
232 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
236 llvm::Optional<SymbolFile::ArrayInfo>
237 SymbolFileSymtab::GetDynamicArrayInfoForUID(
238 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
242 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
246 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
247 SymbolContextItem resolve_scope,
249 if (m_obj_file->GetSymtab() == nullptr)
252 uint32_t resolved_flags = 0;
253 if (resolve_scope & eSymbolContextSymbol) {
254 sc.symbol = m_obj_file->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; }