1 //===-- SymbolFileSymtab.cpp ------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #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" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 void SymbolFileSymtab::Initialize() { 27 PluginManager::RegisterPlugin(GetPluginNameStatic(), 28 GetPluginDescriptionStatic(), CreateInstance); 29 } 30 31 void SymbolFileSymtab::Terminate() { 32 PluginManager::UnregisterPlugin(CreateInstance); 33 } 34 35 lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() { 36 static ConstString g_name("symtab"); 37 return g_name; 38 } 39 40 const char *SymbolFileSymtab::GetPluginDescriptionStatic() { 41 return "Reads debug symbols from an object file's symbol table."; 42 } 43 44 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFile *obj_file) { 45 return new SymbolFileSymtab(obj_file); 46 } 47 48 size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope, 49 uint32_t type_mask, 50 lldb_private::TypeList &type_list) { 51 return 0; 52 } 53 54 SymbolFileSymtab::SymbolFileSymtab(ObjectFile *obj_file) 55 : SymbolFile(obj_file), 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_obj_file) { 63 const Symtab *symtab = m_obj_file->GetSymtab(); 64 if (symtab) { 65 //---------------------------------------------------------------------- 66 // The snippet of code below will get the indexes the module symbol table 67 // entries that are code, data, or function related (debug info), sort 68 // them by value (address) and dump the sorted symbols. 69 //---------------------------------------------------------------------- 70 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, 71 m_source_indexes)) { 72 abilities |= CompileUnits; 73 } 74 75 if (symtab->AppendSymbolIndexesWithType( 76 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, 77 m_func_indexes)) { 78 symtab->SortSymbolIndexesByValue(m_func_indexes, true); 79 abilities |= Functions; 80 } 81 82 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, 83 Symtab::eVisibilityAny, 84 m_code_indexes)) { 85 symtab->SortSymbolIndexesByValue(m_code_indexes, true); 86 abilities |= Functions; 87 } 88 89 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, 90 m_data_indexes)) { 91 symtab->SortSymbolIndexesByValue(m_data_indexes, true); 92 abilities |= GlobalVariables; 93 } 94 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(); 101 } 102 } 103 } 104 return abilities; 105 } 106 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()) 111 return 0; 112 113 // If we have any source file symbols we will logically organize the object 114 // symbols using these. 115 return m_source_indexes.size(); 116 } 117 118 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) { 119 CompUnitSP cu_sp; 120 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]); 126 if (cu_symbol) 127 cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL, 128 cu_symbol->GetName().AsCString(), 0, 129 eLanguageTypeUnknown, eLazyBoolNo)); 130 } 131 return cu_sp; 132 } 133 134 lldb::LanguageType 135 SymbolFileSymtab::ParseCompileUnitLanguage(const SymbolContext &sc) { 136 return eLanguageTypeUnknown; 137 } 138 139 size_t SymbolFileSymtab::ParseCompileUnitFunctions(const SymbolContext &sc) { 140 size_t num_added = 0; 141 // We must at least have a valid compile unit 142 assert(sc.comp_unit != NULL); 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(sc.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 sc.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 bool SymbolFileSymtab::ParseCompileUnitLineTable(const SymbolContext &sc) { 211 return false; 212 } 213 214 bool SymbolFileSymtab::ParseCompileUnitDebugMacros(const SymbolContext &sc) { 215 return false; 216 } 217 218 bool SymbolFileSymtab::ParseCompileUnitSupportFiles( 219 const SymbolContext &sc, FileSpecList &support_files) { 220 return false; 221 } 222 223 bool SymbolFileSymtab::ParseImportedModules( 224 const SymbolContext &sc, std::vector<ConstString> &imported_modules) { 225 return false; 226 } 227 228 size_t SymbolFileSymtab::ParseFunctionBlocks(const SymbolContext &sc) { 229 return 0; 230 } 231 232 size_t SymbolFileSymtab::ParseTypes(const SymbolContext &sc) { return 0; } 233 234 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) { 235 return 0; 236 } 237 238 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) { 239 return NULL; 240 } 241 242 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) { 243 return false; 244 } 245 246 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr, 247 uint32_t resolve_scope, 248 SymbolContext &sc) { 249 if (m_obj_file->GetSymtab() == NULL) 250 return 0; 251 252 uint32_t resolved_flags = 0; 253 if (resolve_scope & eSymbolContextSymbol) { 254 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress( 255 so_addr.GetFileAddress()); 256 if (sc.symbol) 257 resolved_flags |= eSymbolContextSymbol; 258 } 259 return resolved_flags; 260 } 261 262 //------------------------------------------------------------------ 263 // PluginInterface protocol 264 //------------------------------------------------------------------ 265 lldb_private::ConstString SymbolFileSymtab::GetPluginName() { 266 return GetPluginNameStatic(); 267 } 268 269 uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; } 270