blob: d3dd1ae923e02bb7f35306703033ba5f767e4ce6 [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"
Greg Clayton1f746072012-08-29 21:13:06 +000015#include "lldb/Symbol/CompileUnit.h"
16#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/ObjectFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Symbol/ObjectFile.h"
19#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Symbol/SymbolContext.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/Symtab.h"
22#include "lldb/Symbol/TypeList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
27void
28SymbolFileSymtab::Initialize()
29{
30 PluginManager::RegisterPlugin (GetPluginNameStatic(),
31 GetPluginDescriptionStatic(),
32 CreateInstance);
33}
34
35void
36SymbolFileSymtab::Terminate()
37{
38 PluginManager::UnregisterPlugin (CreateInstance);
39}
40
41
Greg Clayton57abc5d2013-05-10 21:47:16 +000042lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043SymbolFileSymtab::GetPluginNameStatic()
44{
Greg Clayton57abc5d2013-05-10 21:47:16 +000045 static ConstString g_name("symtab");
46 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047}
48
49const char *
50SymbolFileSymtab::GetPluginDescriptionStatic()
51{
52 return "Reads debug symbols from an object file's symbol table.";
53}
54
55
56SymbolFile*
57SymbolFileSymtab::CreateInstance (ObjectFile* obj_file)
58{
59 return new SymbolFileSymtab(obj_file);
60}
61
Greg Claytonf02500c2013-06-18 22:51:05 +000062size_t
63SymbolFileSymtab::GetTypes (SymbolContextScope *sc_scope, uint32_t type_mask, lldb_private::TypeList &type_list)
64{
65 return 0;
66}
67
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068SymbolFileSymtab::SymbolFileSymtab(ObjectFile* obj_file) :
69 SymbolFile(obj_file),
70 m_source_indexes(),
71 m_func_indexes(),
72 m_code_indexes(),
Greg Clayton1075aca2011-12-03 20:02:42 +000073 m_objc_class_name_to_index ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074{
75}
76
77SymbolFileSymtab::~SymbolFileSymtab()
78{
79}
80
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +000082SymbolFileSymtab::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083{
84 uint32_t abilities = 0;
Greg Clayton5861d3e2011-06-19 04:02:02 +000085 if (m_obj_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 {
Greg Clayton3046e662013-07-10 01:23:25 +000087 const Symtab *symtab = m_obj_file->GetSymtab();
Greg Clayton5861d3e2011-06-19 04:02:02 +000088 if (symtab)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 {
Greg Clayton5861d3e2011-06-19 04:02:02 +000090 //----------------------------------------------------------------------
91 // The snippet of code below will get the indexes the module symbol
92 // table entries that are code, data, or function related (debug info),
93 // sort them by value (address) and dump the sorted symbols.
94 //----------------------------------------------------------------------
Greg Clayton1075aca2011-12-03 20:02:42 +000095 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile, m_source_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +000096 {
97 abilities |= CompileUnits;
98 }
Greg Clayton1075aca2011-12-03 20:02:42 +000099
100 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000101 {
102 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
103 abilities |= Functions;
104 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Greg Clayton1075aca2011-12-03 20:02:42 +0000106 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny, m_code_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000107 {
108 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000109 abilities |= Functions;
Greg Clayton5861d3e2011-06-19 04:02:02 +0000110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111
Greg Clayton1075aca2011-12-03 20:02:42 +0000112 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData, m_data_indexes))
Greg Clayton5861d3e2011-06-19 04:02:02 +0000113 {
114 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
115 abilities |= GlobalVariables;
116 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000117
Greg Clayton1075aca2011-12-03 20:02:42 +0000118 lldb_private::Symtab::IndexCollection objc_class_indexes;
119 if (symtab->AppendSymbolIndexesWithType (eSymbolTypeObjCClass, objc_class_indexes))
Sean Callanan09ab4b72011-11-30 22:11:59 +0000120 {
Greg Clayton1075aca2011-12-03 20:02:42 +0000121 symtab->AppendSymbolNamesToMap (objc_class_indexes,
122 true,
123 true,
124 m_objc_class_name_to_index);
125 m_objc_class_name_to_index.Sort();
Sean Callanan09ab4b72011-11-30 22:11:59 +0000126 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 }
128 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 return abilities;
130}
131
132uint32_t
133SymbolFileSymtab::GetNumCompileUnits()
134{
135 // If we don't have any source file symbols we will just have one compile unit for
136 // the entire object file
137 if (m_source_indexes.empty())
Jim Ingham969795f2011-09-21 01:17:13 +0000138 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000140 // If we have any source file symbols we will logically organize the object symbols
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 // using these.
142 return m_source_indexes.size();
143}
144
145CompUnitSP
146SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx)
147{
148 CompUnitSP cu_sp;
149
150 // If we don't have any source file symbols we will just have one compile unit for
151 // the entire object file
Greg Clayton9efa0762012-04-26 16:53:42 +0000152 if (idx < m_source_indexes.size())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 {
Greg Clayton3046e662013-07-10 01:23:25 +0000154 const Symbol *cu_symbol = m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 if (cu_symbol)
Jason Molenda6ab659a2015-07-29 00:42:47 +0000156 cu_sp.reset(new CompileUnit (m_obj_file->GetModule(), NULL, cu_symbol->GetName().AsCString(), 0, eLanguageTypeUnknown, false));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 }
158 return cu_sp;
159}
160
Greg Clayton1f746072012-08-29 21:13:06 +0000161lldb::LanguageType
162SymbolFileSymtab::ParseCompileUnitLanguage (const SymbolContext& sc)
163{
164 return eLanguageTypeUnknown;
165}
166
167
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168size_t
169SymbolFileSymtab::ParseCompileUnitFunctions (const SymbolContext &sc)
170{
171 size_t num_added = 0;
172 // We must at least have a valid compile unit
173 assert (sc.comp_unit != NULL);
Greg Clayton3046e662013-07-10 01:23:25 +0000174 const Symtab *symtab = m_obj_file->GetSymtab();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 const Symbol *curr_symbol = NULL;
176 const Symbol *next_symbol = NULL;
177// const char *prefix = m_obj_file->SymbolPrefix();
178// if (prefix == NULL)
179// prefix == "";
180//
181// const uint32_t prefix_len = strlen(prefix);
182
183 // If we don't have any source file symbols we will just have one compile unit for
184 // the entire object file
185 if (m_source_indexes.empty())
186 {
187 // The only time we will have a user ID of zero is when we don't have
188 // and source file symbols and we declare one compile unit for the
189 // entire object file
190 if (!m_func_indexes.empty())
191 {
192
193 }
194
195 if (!m_code_indexes.empty())
196 {
197// StreamFile s(stdout);
198// symtab->Dump(&s, m_code_indexes);
199
200 uint32_t idx = 0; // Index into the indexes
201 const uint32_t num_indexes = m_code_indexes.size();
202 for (idx = 0; idx < num_indexes; ++idx)
203 {
204 uint32_t symbol_idx = m_code_indexes[idx];
205 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
206 if (curr_symbol)
207 {
208 // Union of all ranges in the function DIE (if the function is discontiguous)
Greg Claytone7612132012-03-07 21:03:09 +0000209 AddressRange func_range(curr_symbol->GetAddress(), 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 if (func_range.GetBaseAddress().IsSectionOffset())
211 {
212 uint32_t symbol_size = curr_symbol->GetByteSize();
213 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
214 func_range.SetByteSize(symbol_size);
215 else if (idx + 1 < num_indexes)
216 {
217 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
218 if (next_symbol)
219 {
Greg Clayton358cf1e2015-06-25 21:46:34 +0000220 func_range.SetByteSize(next_symbol->GetAddressRef().GetOffset() - curr_symbol->GetAddressRef().GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221 }
222 }
223
224 FunctionSP func_sp(new Function(sc.comp_unit,
225 symbol_idx, // UserID is the DIE offset
226 LLDB_INVALID_UID, // We don't have any type info for this function
227 curr_symbol->GetMangled(), // Linker/mangled name
228 NULL, // no return type for a code symbol...
229 func_range)); // first address range
230
231 if (func_sp.get() != NULL)
232 {
233 sc.comp_unit->AddFunction(func_sp);
234 ++num_added;
235 }
236 }
237 }
238 }
239
240 }
241 }
242 else
243 {
244 // We assume we
245 }
246 return num_added;
247}
248
249bool
250SymbolFileSymtab::ParseCompileUnitLineTable (const SymbolContext &sc)
251{
252 return false;
253}
254
255bool
Siva Chandrad8335e92015-12-16 00:22:08 +0000256SymbolFileSymtab::ParseCompileUnitDebugMacros (const SymbolContext &sc)
257{
258 return false;
259}
260
261bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262SymbolFileSymtab::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files)
263{
264 return false;
265}
266
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000267bool
268SymbolFileSymtab::ParseImportedModules (const SymbolContext &sc, std::vector<ConstString> &imported_modules)
269{
270 return false;
271}
272
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273size_t
274SymbolFileSymtab::ParseFunctionBlocks (const SymbolContext &sc)
275{
276 return 0;
277}
278
279
280size_t
281SymbolFileSymtab::ParseTypes (const SymbolContext &sc)
282{
283 return 0;
284}
285
286
287size_t
288SymbolFileSymtab::ParseVariablesForContext (const SymbolContext& sc)
289{
290 return 0;
291}
292
293Type*
294SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid)
295{
296 return NULL;
297}
298
Greg Clayton57ee3062013-07-11 22:46:58 +0000299bool
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000300SymbolFileSymtab::CompleteType (lldb_private::CompilerType& compiler_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +0000301{
Greg Clayton57ee3062013-07-11 22:46:58 +0000302 return false;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000303}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305uint32_t
306SymbolFileSymtab::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
307{
Greg Clayton3046e662013-07-10 01:23:25 +0000308 if (m_obj_file->GetSymtab() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309 return 0;
310
311 uint32_t resolved_flags = 0;
312 if (resolve_scope & eSymbolContextSymbol)
313 {
Greg Clayton3046e662013-07-10 01:23:25 +0000314 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(so_addr.GetFileAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 if (sc.symbol)
316 resolved_flags |= eSymbolContextSymbol;
317 }
318 return resolved_flags;
319}
320
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321//------------------------------------------------------------------
322// PluginInterface protocol
323//------------------------------------------------------------------
Greg Clayton57abc5d2013-05-10 21:47:16 +0000324lldb_private::ConstString
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325SymbolFileSymtab::GetPluginName()
326{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 return GetPluginNameStatic();
328}
329
330uint32_t
331SymbolFileSymtab::GetPluginVersion()
332{
333 return 1;
334}