blob: 81d4bc1fd4423689f8cc5e6b5545d3cab311819d [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"
Greg Clayton1f746072012-08-29 21:13:06 +000013#include "lldb/Symbol/CompileUnit.h"
14#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Symbol/ObjectFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Symbol/Symbol.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Symbol/SymbolContext.h"
Greg Clayton1f746072012-08-29 21:13:06 +000018#include "lldb/Symbol/Symtab.h"
19#include "lldb/Symbol/TypeList.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000020#include "lldb/Utility/RegularExpression.h"
Pavel Labath38d06322017-06-29 14:32:17 +000021#include "lldb/Utility/Timer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026void SymbolFileSymtab::Initialize() {
27 PluginManager::RegisterPlugin(GetPluginNameStatic(),
28 GetPluginDescriptionStatic(), CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029}
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031void SymbolFileSymtab::Terminate() {
32 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033}
34
Kate Stoneb9c1b512016-09-06 20:57:50 +000035lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() {
36 static ConstString g_name("symtab");
37 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038}
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040const char *SymbolFileSymtab::GetPluginDescriptionStatic() {
41 return "Reads debug symbols from an object file's symbol table.";
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042}
43
Kate Stoneb9c1b512016-09-06 20:57:50 +000044SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFile *obj_file) {
45 return new SymbolFileSymtab(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046}
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +000049 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 lldb_private::TypeList &type_list) {
51 return 0;
52}
53
54SymbolFileSymtab::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
58SymbolFileSymtab::~SymbolFileSymtab() {}
59
60uint32_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 //----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000066 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 //----------------------------------------------------------------------
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
107uint32_t SymbolFileSymtab::GetNumCompileUnits() {
Adrian Prantl05097242018-04-30 16:49:04 +0000108 // If we don't have any source file symbols we will just have one compile
109 // unit for the entire object file
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 if (m_source_indexes.empty())
Greg Claytonf02500c2013-06-18 22:51:05 +0000111 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112
113 // If we have any source file symbols we will logically organize the object
Adrian Prantl05097242018-04-30 16:49:04 +0000114 // symbols using these.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 return m_source_indexes.size();
Greg Claytonf02500c2013-06-18 22:51:05 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
119 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120
Adrian Prantl05097242018-04-30 16:49:04 +0000121 // If we don't have any source file symbols we will just have one compile
122 // unit for the entire object file
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
Zachary Turner863f8c12019-01-11 18:03:20 +0000134lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000136}
137
Zachary Turner863f8c12019-01-11 18:03:20 +0000138size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 size_t num_added = 0;
140 // We must at least have a valid compile unit
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 const Symtab *symtab = m_obj_file->GetSymtab();
142 const Symbol *curr_symbol = NULL;
143 const Symbol *next_symbol = NULL;
144 // const char *prefix = m_obj_file->SymbolPrefix();
145 // if (prefix == NULL)
146 // prefix == "";
147 //
148 // const uint32_t prefix_len = strlen(prefix);
Greg Clayton1f746072012-08-29 21:13:06 +0000149
Adrian Prantl05097242018-04-30 16:49:04 +0000150 // If we don't have any source file symbols we will just have one compile
151 // unit for the entire object file
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 if (m_source_indexes.empty()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000153 // The only time we will have a user ID of zero is when we don't have and
154 // source file symbols and we declare one compile unit for the entire
155 // object file
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 if (!m_func_indexes.empty()) {
157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 if (!m_code_indexes.empty()) {
160 // StreamFile s(stdout);
161 // symtab->Dump(&s, m_code_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 uint32_t idx = 0; // Index into the indexes
164 const uint32_t num_indexes = m_code_indexes.size();
165 for (idx = 0; idx < num_indexes; ++idx) {
166 uint32_t symbol_idx = m_code_indexes[idx];
167 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
168 if (curr_symbol) {
169 // Union of all ranges in the function DIE (if the function is
170 // discontiguous)
171 AddressRange func_range(curr_symbol->GetAddress(), 0);
172 if (func_range.GetBaseAddress().IsSectionOffset()) {
173 uint32_t symbol_size = curr_symbol->GetByteSize();
174 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
175 func_range.SetByteSize(symbol_size);
176 else if (idx + 1 < num_indexes) {
177 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
178 if (next_symbol) {
179 func_range.SetByteSize(
180 next_symbol->GetAddressRef().GetOffset() -
181 curr_symbol->GetAddressRef().GetOffset());
182 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 }
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 FunctionSP func_sp(
Zachary Turner863f8c12019-01-11 18:03:20 +0000186 new Function(&comp_unit,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 symbol_idx, // UserID is the DIE offset
188 LLDB_INVALID_UID, // We don't have any type info
189 // for this function
190 curr_symbol->GetMangled(), // Linker/mangled name
191 NULL, // no return type for a code symbol...
192 func_range)); // first address range
193
194 if (func_sp.get() != NULL) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000195 comp_unit.AddFunction(func_sp);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 ++num_added;
197 }
198 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 } else {
203 // We assume we
204 }
205 return num_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Zachary Turner863f8c12019-01-11 18:03:20 +0000208size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
209
210bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
211
212bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
Zachary Turner863f8c12019-01-11 18:03:20 +0000216bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit,
217 FileSpecList &support_files) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221bool SymbolFileSymtab::ParseImportedModules(
222 const SymbolContext &sc, std::vector<ConstString> &imported_modules) {
223 return false;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000224}
225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226size_t SymbolFileSymtab::ParseFunctionBlocks(const SymbolContext &sc) {
227 return 0;
228}
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
231 return 0;
232}
233
234Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
235 return NULL;
236}
237
Adrian Prantleca07c52018-11-05 20:49:07 +0000238llvm::Optional<SymbolFile::ArrayInfo>
239SymbolFileSymtab::GetDynamicArrayInfoForUID(
240 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
241 return llvm::None;
242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
245 return false;
246}
247
248uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000249 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 SymbolContext &sc) {
251 if (m_obj_file->GetSymtab() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 uint32_t resolved_flags = 0;
255 if (resolve_scope & eSymbolContextSymbol) {
256 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(
257 so_addr.GetFileAddress());
258 if (sc.symbol)
259 resolved_flags |= eSymbolContextSymbol;
260 }
261 return resolved_flags;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264//------------------------------------------------------------------
265// PluginInterface protocol
266//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267lldb_private::ConstString SymbolFileSymtab::GetPluginName() {
268 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269}
270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; }