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 #include "lldb/Core/Module.h" 11 #include "lldb/Core/PluginManager.h" 12 #include "lldb/Symbol/CompileUnit.h" 13 #include "lldb/Symbol/Function.h" 14 #include "lldb/Symbol/ObjectFile.h" 15 #include "lldb/Symbol/Symbol.h" 16 #include "lldb/Symbol/SymbolContext.h" 17 #include "lldb/Symbol/Symtab.h" 18 #include "lldb/Symbol/TypeList.h" 19 #include "lldb/Utility/RegularExpression.h" 20 #include "lldb/Utility/Timer.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 void SymbolFileSymtab::Initialize() { 26 PluginManager::RegisterPlugin(GetPluginNameStatic(), 27 GetPluginDescriptionStatic(), CreateInstance); 28 } 29 30 void SymbolFileSymtab::Terminate() { 31 PluginManager::UnregisterPlugin(CreateInstance); 32 } 33 34 lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() { 35 static ConstString g_name("symtab"); 36 return g_name; 37 } 38 39 const char *SymbolFileSymtab::GetPluginDescriptionStatic() { 40 return "Reads debug symbols from an object file's symbol table."; 41 } 42 43 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFile *obj_file) { 44 return new SymbolFileSymtab(obj_file); 45 } 46 47 size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope, 48 TypeClass type_mask, 49 lldb_private::TypeList &type_list) { 50 return 0; 51 } 52 53 SymbolFileSymtab::SymbolFileSymtab(ObjectFile *obj_file) 54 : SymbolFile(obj_file), m_source_indexes(), m_func_indexes(), 55 m_code_indexes(), m_objc_class_name_to_index() {} 56 57 SymbolFileSymtab::~SymbolFileSymtab() {} 58 59 uint32_t SymbolFileSymtab::CalculateAbilities() { 60 uint32_t abilities = 0; 61 if (m_obj_file) { 62 const Symtab *symtab = m_obj_file->GetSymtab(); 63 if (symtab) { 64 //---------------------------------------------------------------------- 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 //---------------------------------------------------------------------- 69 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, 70 m_source_indexes)) { 71 abilities |= CompileUnits; 72 } 73 74 if (symtab->AppendSymbolIndexesWithType( 75 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, 76 m_func_indexes)) { 77 symtab->SortSymbolIndexesByValue(m_func_indexes, true); 78 abilities |= Functions; 79 } 80 81 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, 82 Symtab::eVisibilityAny, 83 m_code_indexes)) { 84 symtab->SortSymbolIndexesByValue(m_code_indexes, true); 85 abilities |= Functions; 86 } 87 88 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, 89 m_data_indexes)) { 90 symtab->SortSymbolIndexesByValue(m_data_indexes, true); 91 abilities |= GlobalVariables; 92 } 93 94 lldb_private::Symtab::IndexCollection objc_class_indexes; 95 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass, 96 objc_class_indexes)) { 97 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true, 98 m_objc_class_name_to_index); 99 m_objc_class_name_to_index.Sort(); 100 } 101 } 102 } 103 return abilities; 104 } 105 106 uint32_t SymbolFileSymtab::GetNumCompileUnits() { 107 // If we don't have any source file symbols we will just have one compile 108 // unit for the entire object file 109 if (m_source_indexes.empty()) 110 return 0; 111 112 // If we have any source file symbols we will logically organize the object 113 // symbols using these. 114 return m_source_indexes.size(); 115 } 116 117 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) { 118 CompUnitSP cu_sp; 119 120 // If we don't have any source file symbols we will just have one compile 121 // unit for the entire object file 122 if (idx < m_source_indexes.size()) { 123 const Symbol *cu_symbol = 124 m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]); 125 if (cu_symbol) 126 cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL, 127 cu_symbol->GetName().AsCString(), 0, 128 eLanguageTypeUnknown, eLazyBoolNo)); 129 } 130 return cu_sp; 131 } 132 133 lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) { 134 return eLanguageTypeUnknown; 135 } 136 137 size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) { 138 size_t num_added = 0; 139 // We must at least have a valid compile unit 140 const Symtab *symtab = m_obj_file->GetSymtab(); 141 const Symbol *curr_symbol = NULL; 142 const Symbol *next_symbol = NULL; 143 // const char *prefix = m_obj_file->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 NULL, // no return type for a code symbol... 191 func_range)); // first address range 192 193 if (func_sp.get() != NULL) { 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<ConstString> &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 NULL; 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 if (m_obj_file->GetSymtab() == NULL) 249 return 0; 250 251 uint32_t resolved_flags = 0; 252 if (resolve_scope & eSymbolContextSymbol) { 253 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress( 254 so_addr.GetFileAddress()); 255 if (sc.symbol) 256 resolved_flags |= eSymbolContextSymbol; 257 } 258 return resolved_flags; 259 } 260 261 //------------------------------------------------------------------ 262 // PluginInterface protocol 263 //------------------------------------------------------------------ 264 lldb_private::ConstString SymbolFileSymtab::GetPluginName() { 265 return GetPluginNameStatic(); 266 } 267 268 uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; } 269