LLDB: SymbolFileSymtab.cpp Source File
LLDB mainline llvm-project lldb source Plugins SymbolFile Symtab SymbolFileSymtab.cpp Go to the documentation of this file. 1 //===-- SymbolFileSymtab.cpp ----------------------------------------------===// 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 #include <optional> 25 26 using namespace lldb ; 27 using namespace lldb_private ; 28 29 LLDB_PLUGIN_DEFINE ( SymbolFileSymtab ) 30 31 char SymbolFileSymtab::ID ; 32 33 void SymbolFileSymtab::Initialize () { 34 PluginManager::RegisterPlugin ( GetPluginNameStatic (), 35 GetPluginDescriptionStatic (), CreateInstance ); 36 } 37 38 void SymbolFileSymtab::Terminate () { 39 PluginManager::UnregisterPlugin ( CreateInstance ); 40 } 41 42 llvm::StringRef 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 : SymbolFileCommon (std::move(objfile_sp)), m_source_indexes (), 56 m_code_indexes () {} 57 58 uint32_t SymbolFileSymtab::CalculateAbilities () { 59 uint32_t abilities = 0; 60 if ( m_objfile_sp ) { 61 const Symtab *symtab = m_objfile_sp ->GetSymtab(); 62 if (symtab) { 63 if (symtab-> GetNumSymbols () > 0) 64 abilities |= Symbols ; 65 66 // Only source-file entries provide a genuine debug-info ability.
Code 67 // and data entries remain available as symbols but are not equivalent 68 // to debug-info functions or global variables. The symbol indexes are 69 // built later by InitializeSymbolIndexes() so we don't need to iterate 70 // over all of the symbols to calculate our abilities. 71 if (symtab-> HasSourceFiles ()) 72 abilities |= CompileUnits ; 73 } 74 } 75 return abilities; 76 } 77 78 void SymbolFileSymtab::InitializeSymbolIndexes () { 79 // GetModuleMutex() dereferences the object file, so check it first.
80 if (! m_objfile_sp ) 81 return ; 82 std::lock_guard<std::recursive_mutex> guard( GetModuleMutex ()); 83 if ( m_symbol_indexes_initialized ) 84 return ; 85 const Symtab *symtab = m_objfile_sp ->GetSymtab(); 86 if (!symtab) 87 return ; 88 m_symbol_indexes_initialized = true ; 89 90 // Get the indexes of the source file and non-debug code entries in the 91 // module symbol table. 92 symtab-> AppendSymbolIndexesWithType ( eSymbolTypeSourceFile , m_source_indexes ); 93 94 if (symtab-> AppendSymbolIndexesWithType ( eSymbolTypeCode , Symtab::eDebugNo , 95 Symtab::eVisibilityAny , 96 m_code_indexes )) { 97 symtab-> SortSymbolIndexesByValue ( m_code_indexes , true ); 98 } 99 } 100 101 uint32_t SymbolFileSymtab::CalculateNumCompileUnits () { 102 // Compile units are only created from source file symbols, so don't build 103 // the symbol indexes, which iterates over all of the symbols, if there 104 // aren't any.
Nothing else uses the indexes without a compile unit. 105 if (! m_objfile_sp ) 106 return 0; 107 const Symtab *symtab = m_objfile_sp ->GetSymtab(); 108 if (!symtab || !symtab-> HasSourceFiles ()) 109 return 0; 110 111 InitializeSymbolIndexes (); 112 113 // Each source file symbol is a compile unit, and the object file symbols 114 // are logically organized using these. 115 return m_source_indexes .size(); 116 } 117 118 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex (uint32_t idx) { 119 InitializeSymbolIndexes (); 120 121 CompUnitSP cu_sp; 122 123 // Each source file symbol is a compile unit.
124 if (idx < m_source_indexes .size()) { 125 const Symbol *cu_symbol = 126 m_objfile_sp ->GetSymtab()->SymbolAtIndex( m_source_indexes [idx]); 127 if (cu_symbol) 128 cu_sp = 129 std::make_shared<CompileUnit>( m_objfile_sp ->GetModule(), nullptr , 130 cu_symbol-> GetName (). AsCString ( nullptr ), 131 0, 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 std::lock_guard<std::recursive_mutex> guard( GetModuleMutex ()); 142 InitializeSymbolIndexes (); 143 size_t num_added = 0; 144 // We must at least have a valid compile unit 145 const Symtab *symtab = m_objfile_sp ->GetSymtab(); 146 const Symbol *curr_symbol = nullptr ; 147 const Symbol *next_symbol = nullptr ; 148 // const char *prefix = m_objfile_sp->SymbolPrefix(); 149 // if (prefix == NULL) 150 // prefix == ""; 151 // 152 // const uint32_t prefix_len = strlen(prefix); 153 154 // If we don't have any source file symbols we will just have one compile 155 // unit for the entire object file 156 if ( m_source_indexes .empty()) { 157 // The only time we will have a user ID of zero is when we don't have and 158 // source file symbols and we declare one compile unit for the entire 159 // object file 160 if (! m_code_indexes .empty()) { 161 // StreamFile s(stdout); 162 // symtab->Dump(&s, m_code_indexes); 163 164 uint32_t idx = 0; // Index into the indexes 165 const uint32_t num_indexes = m_code_indexes .size(); 166 for (idx = 0; idx < num_indexes; ++idx) { 167 uint32_t symbol_idx = m_code_indexes [idx]; 168 curr_symbol = symtab-> SymbolAtIndex (symbol_idx); 169 if (curr_symbol) { 170 // Union of all ranges in the function DIE (if the function is 171 // discontiguous) 172 AddressRange func_range(curr_symbol-> GetAddress (), 0); 173 if (func_range.
GetBaseAddress (). IsSectionOffset ()) { 174 uint32_t symbol_size = curr_symbol-> GetByteSize (); 175 if (symbol_size != 0 && !curr_symbol-> GetSizeIsSibling ()) 176 func_range. SetByteSize (symbol_size); 177 else if (idx + 1 < num_indexes) { 178 next_symbol = symtab-> SymbolAtIndex ( m_code_indexes [idx + 1]); 179 if (next_symbol) { 180 func_range. SetByteSize ( 181 next_symbol-> GetAddressRef (). GetOffset () - 182 curr_symbol-> GetAddressRef ().
GetOffset ()); 183 } 184 } 185 186 FunctionSP func_sp( new Function ( 187 &comp_unit, 188 symbol_idx, // UserID is the DIE offset 189 LLDB_INVALID_UID , // We don't have any type info 190 // for this function 191 curr_symbol-> GetMangled (), // Linker/mangled name 192 nullptr , // no return type for a code symbol... 193 curr_symbol-> GetAddress (), AddressRanges {func_range})); 194 195 if (func_sp.get() != nullptr ) { 196 comp_unit.
AddFunction (func_sp); 197 ++num_added; 198 } 199 } 200 } 201 } 202 } 203 } else { 204 // We assume we 205 } 206 return num_added; 207 } 208 209 size_t SymbolFileSymtab::ParseTypes ( CompileUnit &comp_unit) { return 0; } 210 211 bool SymbolFileSymtab::ParseLineTable ( CompileUnit &comp_unit) { return false ; } 212 213 bool SymbolFileSymtab::ParseDebugMacros ( CompileUnit &comp_unit) { 214 return false ; 215 } 216 217 bool SymbolFileSymtab::ParseSupportFiles ( CompileUnit &comp_unit, 218 SupportFileList &support_files) { 219 return false ; 220 } 221 222 bool SymbolFileSymtab::ParseImportedModules ( 223 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) { 224 return false ; 225 } 226 227 size_t SymbolFileSymtab::ParseBlocksRecursive ( Function &func) {
The rest of this page is on the live site.