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