LLDB  mainline
SymbolFileSymtab.cpp
Go to the documentation of this file.
1 //===-- SymbolFileSymtab.cpp ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SymbolFileSymtab.h"
10 
11 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Function.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Symbol/Symtab.h"
19 #include "lldb/Symbol/TypeList.h"
21 #include "lldb/Utility/Timer.h"
22 
23 #include <memory>
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
29  PluginManager::RegisterPlugin(GetPluginNameStatic(),
30  GetPluginDescriptionStatic(), CreateInstance);
31 }
32 
34  PluginManager::UnregisterPlugin(CreateInstance);
35 }
36 
38  static ConstString g_name("symtab");
39  return g_name;
40 }
41 
43  return "Reads debug symbols from an object file's symbol table.";
44 }
45 
47  return new SymbolFileSymtab(obj_file);
48 }
49 
51  TypeClass type_mask,
52  lldb_private::TypeList &type_list) {
53  return 0;
54 }
55 
57  : SymbolFile(obj_file), m_source_indexes(), m_func_indexes(),
58  m_code_indexes(), m_objc_class_name_to_index() {}
59 
61 
63  uint32_t abilities = 0;
64  if (m_obj_file) {
65  const Symtab *symtab = m_obj_file->GetSymtab();
66  if (symtab) {
67  // The snippet of code below will get the indexes the module symbol table
68  // entries that are code, data, or function related (debug info), sort
69  // them by value (address) and dump the sorted symbols.
72  abilities |= CompileUnits;
73  }
74 
75  if (symtab->AppendSymbolIndexesWithType(
76  eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
77  m_func_indexes)) {
79  abilities |= Functions;
80  }
81 
82  if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
83  Symtab::eVisibilityAny,
84  m_code_indexes)) {
86  abilities |= Functions;
87  }
88 
90  m_data_indexes)) {
92  abilities |= GlobalVariables;
93  }
94 
95  lldb_private::Symtab::IndexCollection objc_class_indexes;
97  objc_class_indexes)) {
98  symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
101  }
102  }
103  }
104  return abilities;
105 }
106 
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 
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 =
126  if (cu_symbol)
127  cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
128  cu_symbol->GetName().AsCString(), 0,
130  }
131  return cu_sp;
132 }
133 
135  return eLanguageTypeUnknown;
136 }
137 
139  size_t num_added = 0;
140  // We must at least have a valid compile unit
141  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);
149 
150  // If we don't have any source file symbols we will just have one compile
151  // unit for the entire object file
152  if (m_source_indexes.empty()) {
153  // 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
156  if (!m_func_indexes.empty()) {
157  }
158 
159  if (!m_code_indexes.empty()) {
160  // StreamFile s(stdout);
161  // symtab->Dump(&s, m_code_indexes);
162 
163  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  }
183  }
184 
185  FunctionSP func_sp(
186  new Function(&comp_unit,
187  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) {
195  comp_unit.AddFunction(func_sp);
196  ++num_added;
197  }
198  }
199  }
200  }
201  }
202  } else {
203  // We assume we
204  }
205  return num_added;
206 }
207 
208 size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
209 
210 bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
211 
213  return false;
214 }
215 
217  FileSpecList &support_files) {
218  return false;
219 }
220 
222  const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
223  return false;
224 }
225 
227 
229  return 0;
230 }
231 
233  return NULL;
234 }
235 
236 llvm::Optional<SymbolFile::ArrayInfo>
238  lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
239  return llvm::None;
240 }
241 
243  return false;
244 }
245 
247  SymbolContextItem 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) {
255  so_addr.GetFileAddress());
256  if (sc.symbol)
257  resolved_flags |= eSymbolContextSymbol;
258  }
259  return resolved_flags;
260 }
261 
262 // PluginInterface protocol
264  return GetPluginNameStatic();
265 }
266 
Address & GetAddressRef()
Definition: Symbol.h:56
lldb_private::Symtab::IndexCollection m_data_indexes
static lldb_private::SymbolFile * CreateInstance(lldb_private::ObjectFile *obj_file)
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
void SetByteSize(lldb::addr_t size)
Definition: Symbol.h:173
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
bool ParseImportedModules(const lldb_private::SymbolContext &sc, std::vector< lldb_private::SourceModule > &imported_modules) override
lldb_private::Symtab::IndexCollection m_func_indexes
lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override
llvm::Optional< ArrayInfo > GetDynamicArrayInfoForUID(lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) override
If type_uid points to an array type, return its characteristics.
A class that describes a function.
Definition: Function.h:323
size_t ParseBlocksRecursive(lldb_private::Function &func) override
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:272
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:58
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition: Symtab.cpp:441
Address GetAddress() const
Definition: Symbol.h:72
#define LLDB_INVALID_UID
Definition: lldb-defines.h:91
uint32_t GetNumCompileUnits() override
Symbol * symbol
The Symbol for a given query.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:27
uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr, lldb::SymbolContextItem resolve_scope, lldb_private::SymbolContext &sc) override
uint32_t GetPluginVersion() override
A class that describes a compilation unit.
Definition: CompileUnit.h:35
lldb_private::Symtab::NameToIndexMap m_objc_class_name_to_index
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition: Symtab.cpp:599
~SymbolFileSymtab() override
lldb_private::Type * ResolveTypeUID(lldb::user_id_t type_uid) override
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:408
uint64_t user_id_t
Definition: lldb-types.h:84
LanguageType
Programming language type.
size_t GetTypes(lldb_private::SymbolContextScope *sc_scope, lldb::TypeClass type_mask, lldb_private::TypeList &type_list) override
bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override
size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override
uint32_t CalculateAbilities() override
bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override
std::vector< uint32_t > IndexCollection
Definition: Symtab.h:23
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition: Symtab.cpp:994
static void Terminate()
uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type, std::vector< uint32_t > &indexes, uint32_t start_idx=0, uint32_t end_index=UINT32_MAX) const
Definition: Symtab.cpp:473
A section + offset based address class.
Definition: Address.h:80
static lldb_private::ConstString GetPluginNameStatic()
static const char * GetPluginDescriptionStatic()
Symbol * SymbolAtIndex(size_t idx)
Definition: Symtab.cpp:203
size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override
ObjectFile * m_obj_file
Definition: SymbolFile.h:231
bool CompleteType(lldb_private::CompilerType &compiler_type) override
A uniqued constant string class.
Definition: ConstString.h:38
Unknown or invalid language value.
bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) override
Definition: SBAddress.h:15
ConstString GetName() const
Definition: Symbol.cpp:494
lldb_private::Symtab::IndexCollection m_source_indexes
bool GetSizeIsSibling() const
Definition: Symbol.h:178
lldb::LanguageType ParseLanguage(lldb_private::CompileUnit &comp_unit) override
size_t ParseVariablesForContext(const lldb_private::SymbolContext &sc) override
static void Initialize()
lldb_private::ConstString GetPluginName() override
lldb_private::Symtab::IndexCollection m_code_indexes
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
virtual Symtab * GetSymtab()=0
Gets the symbol table for the currently selected architecture (and object for archives).
A section + offset based address range class.
Definition: AddressRange.h:32
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
SymbolFileSymtab(lldb_private::ObjectFile *obj_file)
Mangled & GetMangled()
Definition: Symbol.h:120