blob: 8eb3e3a09c8bc24a7e1b7429443a0d6fbf324e39 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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/Core/RegularExpression.h"
14#include "lldb/Core/Timer.h"
Jim Ingham379397632012-10-27 02:54:13 +000015#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Greg Clayton1f746072012-08-29 21:13:06 +000016#include "lldb/Symbol/CompileUnit.h"
17#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Symbol/ObjectFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/SymbolContext.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Symbol/Symtab.h"
23#include "lldb/Symbol/TypeList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25using namespace lldb;
26using namespace lldb_private;
27
28void
29SymbolFileSymtab::Initialize()
30{
31 PluginManager::RegisterPlugin (GetPluginNameStatic(),
32 GetPluginDescriptionStatic(),
33 CreateInstance);
34}
35
36void
37SymbolFileSymtab::Terminate()
38{
39 PluginManager::UnregisterPlugin (CreateInstance);
40}
41
42
Greg Clayton57abc5d2013-05-10 21:47:16 +000043lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044SymbolFileSymtab::GetPluginNameStatic()
45{
Greg Clayton57abc5d2013-05-10 21:47:16 +000046 static ConstString g_name("symtab");
47 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048}
49
50const char *
51SymbolFileSymtab::GetPluginDescriptionStatic()
52{
53 return "Reads debug symbols from an object file's symbol table.";
54}
55
56
57SymbolFile*
58SymbolFileSymtab::CreateInstance (ObjectFile* obj_file)
59{
60 return new SymbolFileSymtab(obj_file);
61}
62
Greg Claytonf02500c2013-06-18 22:51:05 +000063size_t
64SymbolFileSymtab::GetTypes (SymbolContextScope *sc_scope, uint32_t type_mask, lldb_private::TypeList &type_list)
65{
66 return 0;
67}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069SymbolFileSymtab::SymbolFileSymtab(ObjectFile* obj_file) :
70 SymbolFile(obj_file),
71 m_source_indexes(),
72 m_func_indexes(),
73 m_code_indexes(),
Greg Clayton1075aca2011-12-03 20:02:42 +000074 m_objc_class_name_to_index ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075{
76}
77
78SymbolFileSymtab::~SymbolFileSymtab()
79{
80}
81
Sean Callanan09ab4b72011-11-30 22:11:59 +000082ClangASTContext &
83SymbolFileSymtab::GetClangASTContext ()
84{
85 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
86
87 return ast;
88}
89
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +000091SymbolFileSymtab::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092{
93 uint32_t abilities = 0;
Greg Clayton5861d3e2011-06-19 04:02:02 +000094 if (m_obj_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 {
Greg Clayton3046e662013-07-10 01:23:25 +000096 const Symtab *symtab = m_obj_file->GetSymtab();
Greg Clayton5861d3e2011-06-19 04:02:02 +000097 if (symtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 {
Greg Clayton5861d3e2011-06-19 04:02:02 +000099 //----------------------------------------------------------------------
100 // The snippet of code below will get the indexes the module symbol
101 // table entries that are code, data, or function related (debug info),
102 // sort them by value (address) and dump the sorted symbols.
103 //----------------------------------------------------------------------
Greg Clayton1075aca2011-12-03 20:02:42 +0000104 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, m_source_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000105 {
106 abilities |= CompileUnits;
107 }
Greg Clayton1075aca2011-12-03 20:02:42 +0000108
109 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000110 {
111 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
112 abilities |= Functions;
113 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
Greg Clayton1075aca2011-12-03 20:02:42 +0000115 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny, m_code_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000116 {
117 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
Greg Clayton5861d3e2011-06-19 04:02:02 +0000118 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Greg Clayton1075aca2011-12-03 20:02:42 +0000120 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, m_data_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000121 {
122 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
123 abilities |= GlobalVariables;
124 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000125
Greg Clayton1075aca2011-12-03 20:02:42 +0000126 lldb_private::Symtab::IndexCollection objc_class_indexes;
127 if (symtab->AppendSymbolIndexesWithType (eSymbolTypeObjCClass, objc_class_indexes))
Sean Callanan09ab4b72011-11-30 22:11:59 +0000128 {
Greg Clayton1075aca2011-12-03 20:02:42 +0000129 symtab->AppendSymbolNamesToMap (objc_class_indexes,
130 true,
131 true,
132 m_objc_class_name_to_index);
133 m_objc_class_name_to_index.Sort();
Sean Callanan09ab4b72011-11-30 22:11:59 +0000134 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 }
136 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 return abilities;
138}
139
140uint32_t
141SymbolFileSymtab::GetNumCompileUnits()
142{
143 // If we don't have any source file symbols we will just have one compile unit for
144 // the entire object file
145 if (m_source_indexes.empty())
Jim Ingham969795f2011-09-21 01:17:13 +0000146 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000148 // If we have any source file symbols we will logically organize the object symbols
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 // using these.
150 return m_source_indexes.size();
151}
152
153CompUnitSP
154SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx)
155{
156 CompUnitSP cu_sp;
157
158 // If we don't have any source file symbols we will just have one compile unit for
159 // the entire object file
Greg Clayton9efa0762012-04-26 16:53:42 +0000160 if (idx < m_source_indexes.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 {
Greg Clayton3046e662013-07-10 01:23:25 +0000162 const Symbol *cu_symbol = m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163 if (cu_symbol)
Greg Claytone72dfb32012-02-24 01:59:29 +0000164 cu_sp.reset(new CompileUnit (m_obj_file->GetModule(), NULL, cu_symbol->GetMangled().GetName().AsCString(), 0, eLanguageTypeUnknown));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 }
166 return cu_sp;
167}
168
Greg Clayton1f746072012-08-29 21:13:06 +0000169lldb::LanguageType
170SymbolFileSymtab::ParseCompileUnitLanguage (const SymbolContext& sc)
171{
172 return eLanguageTypeUnknown;
173}
174
175
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176size_t
177SymbolFileSymtab::ParseCompileUnitFunctions (const SymbolContext &sc)
178{
179 size_t num_added = 0;
180 // We must at least have a valid compile unit
181 assert (sc.comp_unit != NULL);
Greg Clayton3046e662013-07-10 01:23:25 +0000182 const Symtab *symtab = m_obj_file->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 const Symbol *curr_symbol = NULL;
184 const Symbol *next_symbol = NULL;
185// const char *prefix = m_obj_file->SymbolPrefix();
186// if (prefix == NULL)
187// prefix == "";
188//
189// const uint32_t prefix_len = strlen(prefix);
190
191 // If we don't have any source file symbols we will just have one compile unit for
192 // the entire object file
193 if (m_source_indexes.empty())
194 {
195 // The only time we will have a user ID of zero is when we don't have
196 // and source file symbols and we declare one compile unit for the
197 // entire object file
198 if (!m_func_indexes.empty())
199 {
200
201 }
202
203 if (!m_code_indexes.empty())
204 {
205// StreamFile s(stdout);
206// symtab->Dump(&s, m_code_indexes);
207
208 uint32_t idx = 0; // Index into the indexes
209 const uint32_t num_indexes = m_code_indexes.size();
210 for (idx = 0; idx < num_indexes; ++idx)
211 {
212 uint32_t symbol_idx = m_code_indexes[idx];
213 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
214 if (curr_symbol)
215 {
216 // Union of all ranges in the function DIE (if the function is discontiguous)
Greg Claytone7612132012-03-07 21:03:09 +0000217 AddressRange func_range(curr_symbol->GetAddress(), 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218 if (func_range.GetBaseAddress().IsSectionOffset())
219 {
220 uint32_t symbol_size = curr_symbol->GetByteSize();
221 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
222 func_range.SetByteSize(symbol_size);
223 else if (idx + 1 < num_indexes)
224 {
225 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
226 if (next_symbol)
227 {
Greg Claytone7612132012-03-07 21:03:09 +0000228 func_range.SetByteSize(next_symbol->GetAddress().GetOffset() - curr_symbol->GetAddress().GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229 }
230 }
231
232 FunctionSP func_sp(new Function(sc.comp_unit,
233 symbol_idx, // UserID is the DIE offset
234 LLDB_INVALID_UID, // We don't have any type info for this function
235 curr_symbol->GetMangled(), // Linker/mangled name
236 NULL, // no return type for a code symbol...
237 func_range)); // first address range
238
239 if (func_sp.get() != NULL)
240 {
241 sc.comp_unit->AddFunction(func_sp);
242 ++num_added;
243 }
244 }
245 }
246 }
247
248 }
249 }
250 else
251 {
252 // We assume we
253 }
254 return num_added;
255}
256
257bool
258SymbolFileSymtab::ParseCompileUnitLineTable (const SymbolContext &sc)
259{
260 return false;
261}
262
263bool
264SymbolFileSymtab::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
265{
266 return false;
267}
268
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000269bool
270SymbolFileSymtab::ParseImportedModules (const SymbolContext &sc, std::vector<ConstString> &imported_modules)
271{
272 return false;
273}
274
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275size_t
276SymbolFileSymtab::ParseFunctionBlocks (const SymbolContext &sc)
277{
278 return 0;
279}
280
281
282size_t
283SymbolFileSymtab::ParseTypes (const SymbolContext &sc)
284{
285 return 0;
286}
287
288
289size_t
290SymbolFileSymtab::ParseVariablesForContext (const SymbolContext& sc)
291{
292 return 0;
293}
294
295Type*
296SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid)
297{
298 return NULL;
299}
300
Greg Clayton57ee3062013-07-11 22:46:58 +0000301bool
302SymbolFileSymtab::ResolveClangOpaqueTypeDefinition (lldb_private::ClangASTType& clang_opaque_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +0000303{
Greg Clayton57ee3062013-07-11 22:46:58 +0000304 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000305}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306
Greg Clayton526e5af2010-11-13 03:52:47 +0000307ClangNamespaceDecl
Sean Callanan213fdb82011-10-13 01:49:10 +0000308SymbolFileSymtab::FindNamespace (const SymbolContext& sc, const ConstString &name, const ClangNamespaceDecl *namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +0000309{
Greg Clayton526e5af2010-11-13 03:52:47 +0000310 return ClangNamespaceDecl();
Greg Clayton96d7d742010-11-10 23:42:09 +0000311}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312
313uint32_t
314SymbolFileSymtab::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
315{
Greg Clayton3046e662013-07-10 01:23:25 +0000316 if (m_obj_file->GetSymtab() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 return 0;
318
319 uint32_t resolved_flags = 0;
320 if (resolve_scope & eSymbolContextSymbol)
321 {
Greg Clayton3046e662013-07-10 01:23:25 +0000322 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 if (sc.symbol)
324 resolved_flags |= eSymbolContextSymbol;
325 }
326 return resolved_flags;
327}
328
329uint32_t
330SymbolFileSymtab::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
331{
332 return 0;
333}
334
335uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +0000336SymbolFileSymtab::FindGlobalVariables(const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337{
338 return 0;
339}
340
341uint32_t
342SymbolFileSymtab::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
343{
344 return 0;
345}
346
347uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +0000348SymbolFileSymtab::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349{
350 Timer scoped_timer (__PRETTY_FUNCTION__,
351 "SymbolFileSymtab::FindFunctions (name = '%s')",
352 name.GetCString());
Greg Clayton931180e2011-01-27 06:44:37 +0000353 // If we ever support finding STABS or COFF debug info symbols,
354 // we will need to add support here. We are not trying to find symbols
355 // here, just "lldb_private::Function" objects that come from complete
356 // debug information. Any symbol queries should go through the symbol
357 // table itself in the module's object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 return 0;
359}
360
361uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +0000362SymbolFileSymtab::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363{
364 Timer scoped_timer (__PRETTY_FUNCTION__,
365 "SymbolFileSymtab::FindFunctions (regex = '%s')",
366 regex.GetText());
Greg Clayton931180e2011-01-27 06:44:37 +0000367 // If we ever support finding STABS or COFF debug info symbols,
368 // we will need to add support here. We are not trying to find symbols
369 // here, just "lldb_private::Function" objects that come from complete
370 // debug information. Any symbol queries should go through the symbol
371 // table itself in the module's object file.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 return 0;
373}
374
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000375uint32_t
Greg Clayton1075aca2011-12-03 20:02:42 +0000376SymbolFileSymtab::FindTypes (const lldb_private::SymbolContext& sc,
377 const lldb_private::ConstString &name,
378 const ClangNamespaceDecl *namespace_decl,
379 bool append,
380 uint32_t max_matches,
381 lldb_private::TypeList& types)
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000382{
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000383 return 0;
384}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385
386//------------------------------------------------------------------
387// PluginInterface protocol
388//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000389lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390SymbolFileSymtab::GetPluginName()
391{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 return GetPluginNameStatic();
393}
394
395uint32_t
396SymbolFileSymtab::GetPluginVersion()
397{
398 return 1;
399}