1 //===-- SymbolFileSymtab.cpp ------------------------------------*- 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 #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 void SymbolFileSymtab::Initialize() { 29 PluginManager::RegisterPlugin(GetPluginNameStatic(), 30 GetPluginDescriptionStatic(), CreateInstance); 31 } 32 33 void SymbolFileSymtab::Terminate() { 34 PluginManager::UnregisterPlugin(CreateInstance); 35 } 36 37 lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() { 38 static ConstString g_name("symtab"); 39 return g_name; 40 } 41 42 const char *SymbolFileSymtab::GetPluginDescriptionStatic() { 43 return "Reads debug symbols from an object file's symbol table."; 44 } 45 46 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFileSP objfile_sp) { 47 return new SymbolFileSymtab(std::move(objfile_sp)); 48 } 49 50 void SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope, 51 TypeClass type_mask, 52 lldb_private::TypeList &type_list) {} 53 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() {} 57 58 SymbolFileSymtab::~SymbolFileSymtab() {} 59 60 uint32_t SymbolFileSymtab::CalculateAbilities() { 61 uint32_t abilities = 0; 62 if (m_objfile_sp) { 63 const Symtab *symtab = m_objfile_sp->GetSymtab(); 64 if (symtab) { 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, 69 m_source_indexes)) { 70 abilities |= CompileUnits; 71 } 72 73 if (symtab->AppendSymbolIndexesWithType( 74 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, 75 m_func_indexes)) { 76 symtab->SortSymbolIndexesByValue(m_func_indexes, true); 77 abilities |= Functions; 78 } 79 80 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, 81 Symtab::eVisibilityAny, 82 m_code_indexes)) { 83 symtab->SortSymbolIndexesByValue(m_code_indexes, true); 84 abilities |= Functions; 85 } 86 87 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, 88 m_data_indexes)) { 89 symtab->SortSymbolIndexesByValue(m_data_indexes, true); 90 abilities |= GlobalVariables; 91 } 92 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(); 99 } 100 } 101 } 102 return abilities; 103 } 104 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()) 109 return 0; 110 111 // If we have any source file symbols we will logically organize the object 112 // symbols using these. 113 return m_source_indexes.size(); 114 } 115 116 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) { 117 CompUnitSP cu_sp; 118 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]); 124 if (cu_symbol) 125 cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr, 126 cu_symbol->GetName().AsCString(), 0, 127 eLanguageTypeUnknown, eLazyBoolNo); 128 } 129 return cu_sp; 130 } 131 132 lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) { 133 return eLanguageTypeUnknown; 134 } 135 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) 145 // prefix == ""; 146 // 147 // const uint32_t prefix_len = strlen(prefix); 148 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 154 // object file 155 if (!m_func_indexes.empty()) { 156 } 157 158 if (!m_code_indexes.empty()) { 159 // StreamFile s(stdout); 160 // symtab->Dump(&s, m_code_indexes); 161 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); 167 if (curr_symbol) { 168 // Union of all ranges in the function DIE (if the function is 169 // discontiguous) 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]); 177 if (next_symbol) { 178 func_range.SetByteSize( 179 next_symbol->GetAddressRef().GetOffset() - 180 curr_symbol->GetAddressRef().GetOffset()); 181 } 182 } 183 184 FunctionSP func_sp( 185 new Function(&comp_unit, 186 symbol_idx, // UserID is the DIE offset 187 LLDB_INVALID_UID, // We don't have any type info 188 // for this function 189 curr_symbol->GetMangled(), // Linker/mangled name 190 nullptr, // no return type for a code symbol... 191 func_range)); // first address range 192 193 if (func_sp.get() != nullptr) { 194 comp_unit.AddFunction(func_sp); 195 ++num_added; 196 } 197 } 198 } 199 } 200 } 201 } else { 202 // We assume we 203 } 204 return num_added; 205 } 206 207 size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; } 208 209 bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; } 210 211 bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) { 212 return false; 213 } 214 215 bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit, 216 FileSpecList &support_files) { 217 return false; 218 } 219 220 bool SymbolFileSymtab::ParseImportedModules( 221 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) { 222 return false; 223 } 224 225 size_t SymbolFileSymtab::ParseBlocksRecursive(Function &func) { return 0; } 226 227 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) { 228 return 0; 229 } 230 231 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) { 232 return nullptr; 233 } 234 235 llvm::Optional<SymbolFile::ArrayInfo> 236 SymbolFileSymtab::GetDynamicArrayInfoForUID( 237 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { 238 return llvm::None; 239 } 240 241 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) { 242 return false; 243 } 244 245 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr, 246 SymbolContextItem resolve_scope, 247 SymbolContext &sc) { 248 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 249 if (m_objfile_sp->GetSymtab() == nullptr) 250 return 0; 251 252 uint32_t resolved_flags = 0; 253 if (resolve_scope & eSymbolContextSymbol) { 254 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress( 255 so_addr.GetFileAddress()); 256 if (sc.symbol) 257 resolved_flags |= eSymbolContextSymbol; 258 } 259 return resolved_flags; 260 } 261 262 // PluginInterface protocol 263 lldb_private::ConstString SymbolFileSymtab::GetPluginName() { 264 return GetPluginNameStatic(); 265 } 266 267 uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; } 268