1 //===-- SymbolFileSymtab.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolFileSymtab.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Core/RegularExpression.h"
14 #include "lldb/Core/Timer.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/Function.h"
17 #include "lldb/Symbol/ObjectFile.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/Symtab.h"
22 #include "lldb/Symbol/TypeList.h"
25 using namespace lldb_private;
27 void SymbolFileSymtab::Initialize() {
28 PluginManager::RegisterPlugin(GetPluginNameStatic(),
29 GetPluginDescriptionStatic(), CreateInstance);
32 void SymbolFileSymtab::Terminate() {
33 PluginManager::UnregisterPlugin(CreateInstance);
36 lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() {
37 static ConstString g_name("symtab");
41 const char *SymbolFileSymtab::GetPluginDescriptionStatic() {
42 return "Reads debug symbols from an object file's symbol table.";
45 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFile *obj_file) {
46 return new SymbolFileSymtab(obj_file);
49 size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
51 lldb_private::TypeList &type_list) {
55 SymbolFileSymtab::SymbolFileSymtab(ObjectFile *obj_file)
56 : SymbolFile(obj_file), m_source_indexes(), m_func_indexes(),
57 m_code_indexes(), m_objc_class_name_to_index() {}
59 SymbolFileSymtab::~SymbolFileSymtab() {}
61 uint32_t SymbolFileSymtab::CalculateAbilities() {
62 uint32_t abilities = 0;
64 const Symtab *symtab = m_obj_file->GetSymtab();
66 //----------------------------------------------------------------------
67 // The snippet of code below will get the indexes the module symbol
68 // table entries that are code, data, or function related (debug info),
69 // sort them by value (address) and dump the sorted symbols.
70 //----------------------------------------------------------------------
71 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,
73 abilities |= CompileUnits;
76 if (symtab->AppendSymbolIndexesWithType(
77 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
79 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
80 abilities |= Functions;
83 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
84 Symtab::eVisibilityAny,
86 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
87 abilities |= Functions;
90 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,
92 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
93 abilities |= GlobalVariables;
96 lldb_private::Symtab::IndexCollection objc_class_indexes;
97 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass,
98 objc_class_indexes)) {
99 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
100 m_objc_class_name_to_index);
101 m_objc_class_name_to_index.Sort();
108 uint32_t SymbolFileSymtab::GetNumCompileUnits() {
109 // If we don't have any source file symbols we will just have one compile unit
111 // the entire object file
112 if (m_source_indexes.empty())
115 // If we have any source file symbols we will logically organize the object
118 return m_source_indexes.size();
121 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
124 // If we don't have any source file symbols we will just have one compile unit
126 // the entire object file
127 if (idx < m_source_indexes.size()) {
128 const Symbol *cu_symbol =
129 m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
131 cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL,
132 cu_symbol->GetName().AsCString(), 0,
133 eLanguageTypeUnknown, eLazyBoolNo));
139 SymbolFileSymtab::ParseCompileUnitLanguage(const SymbolContext &sc) {
140 return eLanguageTypeUnknown;
143 size_t SymbolFileSymtab::ParseCompileUnitFunctions(const SymbolContext &sc) {
144 size_t num_added = 0;
145 // We must at least have a valid compile unit
146 assert(sc.comp_unit != NULL);
147 const Symtab *symtab = m_obj_file->GetSymtab();
148 const Symbol *curr_symbol = NULL;
149 const Symbol *next_symbol = NULL;
150 // const char *prefix = m_obj_file->SymbolPrefix();
151 // if (prefix == NULL)
154 // const uint32_t prefix_len = strlen(prefix);
156 // If we don't have any source file symbols we will just have one compile unit
158 // the entire object file
159 if (m_source_indexes.empty()) {
160 // The only time we will have a user ID of zero is when we don't have
161 // and source file symbols and we declare one compile unit for the
162 // entire object file
163 if (!m_func_indexes.empty()) {
166 if (!m_code_indexes.empty()) {
167 // StreamFile s(stdout);
168 // symtab->Dump(&s, m_code_indexes);
170 uint32_t idx = 0; // Index into the indexes
171 const uint32_t num_indexes = m_code_indexes.size();
172 for (idx = 0; idx < num_indexes; ++idx) {
173 uint32_t symbol_idx = m_code_indexes[idx];
174 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
176 // Union of all ranges in the function DIE (if the function is
178 AddressRange func_range(curr_symbol->GetAddress(), 0);
179 if (func_range.GetBaseAddress().IsSectionOffset()) {
180 uint32_t symbol_size = curr_symbol->GetByteSize();
181 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
182 func_range.SetByteSize(symbol_size);
183 else if (idx + 1 < num_indexes) {
184 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
186 func_range.SetByteSize(
187 next_symbol->GetAddressRef().GetOffset() -
188 curr_symbol->GetAddressRef().GetOffset());
193 new Function(sc.comp_unit,
194 symbol_idx, // UserID is the DIE offset
195 LLDB_INVALID_UID, // We don't have any type info
197 curr_symbol->GetMangled(), // Linker/mangled name
198 NULL, // no return type for a code symbol...
199 func_range)); // first address range
201 if (func_sp.get() != NULL) {
202 sc.comp_unit->AddFunction(func_sp);
215 bool SymbolFileSymtab::ParseCompileUnitLineTable(const SymbolContext &sc) {
219 bool SymbolFileSymtab::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
223 bool SymbolFileSymtab::ParseCompileUnitSupportFiles(
224 const SymbolContext &sc, FileSpecList &support_files) {
228 bool SymbolFileSymtab::ParseImportedModules(
229 const SymbolContext &sc, std::vector<ConstString> &imported_modules) {
233 size_t SymbolFileSymtab::ParseFunctionBlocks(const SymbolContext &sc) {
237 size_t SymbolFileSymtab::ParseTypes(const SymbolContext &sc) { return 0; }
239 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
243 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
247 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
251 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
252 uint32_t resolve_scope,
254 if (m_obj_file->GetSymtab() == NULL)
257 uint32_t resolved_flags = 0;
258 if (resolve_scope & eSymbolContextSymbol) {
259 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(
260 so_addr.GetFileAddress());
262 resolved_flags |= eSymbolContextSymbol;
264 return resolved_flags;
267 //------------------------------------------------------------------
268 // PluginInterface protocol
269 //------------------------------------------------------------------
270 lldb_private::ConstString SymbolFileSymtab::GetPluginName() {
271 return GetPluginNameStatic();
274 uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; }