LLDB  mainline
SymbolFileSymtab.cpp
Go to the documentation of this file.
1 //===-- SymbolFileSymtab.cpp ----------------------------------------------===//
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 
31 
33  PluginManager::RegisterPlugin(GetPluginNameStatic(),
34  GetPluginDescriptionStatic(), CreateInstance);
35 }
36 
38  PluginManager::UnregisterPlugin(CreateInstance);
39 }
40 
42  static ConstString g_name("symtab");
43  return g_name;
44 }
45 
47  return "Reads debug symbols from an object file's symbol table.";
48 }
49 
50 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFileSP objfile_sp) {
51  return new SymbolFileSymtab(std::move(objfile_sp));
52 }
53 
55  TypeClass type_mask,
56  lldb_private::TypeList &type_list) {}
57 
58 SymbolFileSymtab::SymbolFileSymtab(ObjectFileSP objfile_sp)
59  : SymbolFile(std::move(objfile_sp)), m_source_indexes(), m_func_indexes(),
60  m_code_indexes(), m_objc_class_name_to_index() {}
61 
63  uint32_t abilities = 0;
64  if (m_objfile_sp) {
65  const Symtab *symtab = m_objfile_sp->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 =
125  m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
126  if (cu_symbol)
127  cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
128  cu_symbol->GetName().AsCString(), 0,
130  }
131  return cu_sp;
132 }
133 
135  return eLanguageTypeUnknown;
136 }
137 
139  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
140  size_t num_added = 0;
141  // We must at least have a valid compile unit
142  const Symtab *symtab = m_objfile_sp->GetSymtab();
143  const Symbol *curr_symbol = nullptr;
144  const Symbol *next_symbol = nullptr;
145  // const char *prefix = m_objfile_sp->SymbolPrefix();
146  // if (prefix == NULL)
147  // prefix == "";
148  //
149  // const uint32_t prefix_len = strlen(prefix);
150 
151  // If we don't have any source file symbols we will just have one compile
152  // unit for the entire object file
153  if (m_source_indexes.empty()) {
154  // The only time we will have a user ID of zero is when we don't have and
155  // source file symbols and we declare one compile unit for the entire
156  // object file
157  if (!m_func_indexes.empty()) {
158  }
159 
160  if (!m_code_indexes.empty()) {
161  // StreamFile s(stdout);
162  // symtab->Dump(&s, m_code_indexes);
163 
164  uint32_t idx = 0; // Index into the indexes
165  const uint32_t num_indexes = m_code_indexes.size();
166  for (idx = 0; idx < num_indexes; ++idx) {
167  uint32_t symbol_idx = m_code_indexes[idx];
168  curr_symbol = symtab->SymbolAtIndex(symbol_idx);
169  if (curr_symbol) {
170  // Union of all ranges in the function DIE (if the function is
171  // discontiguous)
172  AddressRange func_range(curr_symbol->GetAddress(), 0);
173  if (func_range.GetBaseAddress().IsSectionOffset()) {
174  uint32_t symbol_size = curr_symbol->GetByteSize();
175  if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
176  func_range.SetByteSize(symbol_size);
177  else if (idx + 1 < num_indexes) {
178  next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
179  if (next_symbol) {
180  func_range.SetByteSize(
181  next_symbol->GetAddressRef().GetOffset() -
182  curr_symbol->GetAddressRef().GetOffset());
183  }
184  }
185 
186  FunctionSP func_sp(
187  new Function(&comp_unit,
188  symbol_idx, // UserID is the DIE offset
189  LLDB_INVALID_UID, // We don't have any type info
190  // for this function
191  curr_symbol->GetMangled(), // Linker/mangled name
192  nullptr, // no return type for a code symbol...
193  func_range)); // first address range
194 
195  if (func_sp.get() != nullptr) {
196  comp_unit.AddFunction(func_sp);
197  ++num_added;
198  }
199  }
200  }
201  }
202  }
203  } else {
204  // We assume we
205  }
206  return num_added;
207 }
208 
209 size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
210 
211 bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
212 
214  return false;
215 }
216 
218  FileSpecList &support_files) {
219  return false;
220 }
221 
223  const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
224  return false;
225 }
226 
228 
230  return 0;
231 }
232 
234  return nullptr;
235 }
236 
237 llvm::Optional<SymbolFile::ArrayInfo>
239  lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
240  return llvm::None;
241 }
242 
244  return false;
245 }
246 
248  SymbolContextItem resolve_scope,
249  SymbolContext &sc) {
250  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
251  if (m_objfile_sp->GetSymtab() == nullptr)
252  return 0;
253 
254  uint32_t resolved_flags = 0;
255  if (resolve_scope & eSymbolContextSymbol) {
256  sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
257  so_addr.GetFileAddress());
258  if (sc.symbol)
259  resolved_flags |= eSymbolContextSymbol;
260  }
261  return resolved_flags;
262 }
RegularExpression.h
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
SymbolFileSymtab::ParseDebugMacros
bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override
Definition: SymbolFileSymtab.cpp:213
lldb_private::SymbolFile
Definition: SymbolFile.h:37
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
SymbolFileSymtab::Terminate
static void Terminate()
Definition: SymbolFileSymtab.cpp:37
lldb_private::UniqueCStringMap::Sort
void Sort()
Definition: UniqueCStringMap.h:168
SymbolFileSymtab::ParseLineTable
bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override
Definition: SymbolFileSymtab.cpp:211
lldb::eSymbolTypeCode
@ eSymbolTypeCode
Definition: lldb-enumerations.h:616
lldb_private::Symtab::AppendSymbolNamesToMap
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition: Symtab.cpp:454
CompileUnit.h
lldb_private::Symbol
Definition: Symbol.h:20
SymbolFileSymtab.h
LLDB_INVALID_UID
#define LLDB_INVALID_UID
Definition: lldb-defines.h:92
lldb_private::SymbolFile::GetModuleMutex
virtual std::recursive_mutex & GetModuleMutex() const
Symbols file subclasses should override this to return the Module that owns the TypeSystem that this ...
Definition: SymbolFile.cpp:33
SymbolFileSymtab::CompleteType
bool CompleteType(lldb_private::CompilerType &compiler_type) override
Definition: SymbolFileSymtab.cpp:243
Module.h
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb::eSymbolTypeData
@ eSymbolTypeData
Definition: lldb-enumerations.h:618
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
lldb_private::SymbolFile::CompileUnits
@ CompileUnits
Definition: SymbolFile.h:55
SymbolFileSymtab::GetTypes
void GetTypes(lldb_private::SymbolContextScope *sc_scope, lldb::TypeClass type_mask, lldb_private::TypeList &type_list) override
Definition: SymbolFileSymtab.cpp:54
lldb_private::Symtab::SymbolAtIndex
Symbol * SymbolAtIndex(size_t idx)
Definition: Symtab.cpp:210
SymbolFileSymtab::ResolveSymbolContext
uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr, lldb::SymbolContextItem resolve_scope, lldb_private::SymbolContext &sc) override
Definition: SymbolFileSymtab.cpp:247
SymbolFileSymtab::CalculateAbilities
uint32_t CalculateAbilities() override
Definition: SymbolFileSymtab.cpp:62
SymbolFileSymtab::ParseLanguage
lldb::LanguageType ParseLanguage(lldb_private::CompileUnit &comp_unit) override
Definition: SymbolFileSymtab.cpp:134
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::SymbolFile::Functions
@ Functions
Definition: SymbolFile.h:57
SymbolFileSymtab::ParseFunctions
size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override
Definition: SymbolFileSymtab.cpp:138
SymbolFileSymtab::Initialize
static void Initialize()
Definition: SymbolFileSymtab.cpp:32
lldb_private::CompileUnit
Definition: CompileUnit.h:38
SymbolFileSymtab::ID
static char ID
LLVM RTTI support.
Definition: SymbolFileSymtab.h:20
lldb_private::SymbolContext::symbol
Symbol * symbol
The Symbol for a given query.
Definition: SymbolContext.h:323
SymbolFileSymtab::ResolveTypeUID
lldb_private::Type * ResolveTypeUID(lldb::user_id_t type_uid) override
Definition: SymbolFileSymtab.cpp:233
lldb_private::Symbol::GetSizeIsSibling
bool GetSizeIsSibling() const
Definition: Symbol.h:191
lldb_private::Function
Definition: Function.h:413
SymbolFileSymtab::ParseBlocksRecursive
size_t ParseBlocksRecursive(lldb_private::Function &func) override
Definition: SymbolFileSymtab.cpp:227
SymbolFileSymtab::ParseCompileUnitAtIndex
lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override
Definition: SymbolFileSymtab.cpp:118
lldb::eSymbolTypeSourceFile
@ eSymbolTypeSourceFile
Definition: lldb-enumerations.h:622
SymbolFileSymtab::CreateInstance
static lldb_private::SymbolFile * CreateInstance(lldb::ObjectFileSP objfile_sp)
Definition: SymbolFileSymtab.cpp:50
SymbolFileSymtab::m_func_indexes
lldb_private::Symtab::IndexCollection m_func_indexes
Definition: SymbolFileSymtab.h:100
lldb_private::Address::GetFileAddress
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
lldb_private::Symbol::GetAddressRef
Address & GetAddressRef()
Definition: Symbol.h:57
lldb_private::CompileUnit::AddFunction
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
Definition: CompileUnit.cpp:139
SymbolFileSymtab::CalculateNumCompileUnits
uint32_t CalculateNumCompileUnits() override
Definition: SymbolFileSymtab.cpp:107
SymbolFileSymtab::ParseVariablesForContext
size_t ParseVariablesForContext(const lldb_private::SymbolContext &sc) override
Definition: SymbolFileSymtab.cpp:229
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::AddressRange
Definition: AddressRange.h:25
Timer.h
SymbolFileSymtab::SymbolFileSymtab
SymbolFileSymtab(lldb::ObjectFileSP objfile_sp)
Definition: SymbolFileSymtab.cpp:58
lldb::eSymbolTypeObjCClass
@ eSymbolTypeObjCClass
Definition: lldb-enumerations.h:640
lldb_private::SymbolFile::m_objfile_sp
lldb::ObjectFileSP m_objfile_sp
Definition: SymbolFile.h:310
lldb_private::Symbol::GetMangled
Mangled & GetMangled()
Definition: Symbol.h:121
lldb_private::SymbolContextScope
Definition: SymbolContextScope.h:64
ObjectFile.h
TypeList.h
Symbol.h
lldb_private::Symbol::GetName
ConstString GetName() const
Definition: Symbol.cpp:500
SymbolFileSymtab::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: SymbolFileSymtab.cpp:41
lldb_private::Symtab::AppendSymbolIndexesWithType
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:482
lldb::eLanguageTypeUnknown
@ eLanguageTypeUnknown
Unknown or invalid language value.
Definition: lldb-enumerations.h:437
lldb_private::Symtab
Definition: Symtab.h:22
uint32_t
lldb_private::eLazyBoolNo
@ eLazyBoolNo
Definition: lldb-private-enumerations.h:115
SymbolFileSymtab::ParseImportedModules
bool ParseImportedModules(const lldb_private::SymbolContext &sc, std::vector< lldb_private::SourceModule > &imported_modules) override
Definition: SymbolFileSymtab.cpp:222
lldb_private::Address
Definition: Address.h:59
SymbolFileSymtab
Definition: SymbolFileSymtab.h:18
lldb_private::Type
Definition: Type.h:66
SymbolFileSymtab::ParseTypes
size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override
Definition: SymbolFileSymtab.cpp:209
Symtab.h
lldb_private::Symbol::GetByteSize
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:414
PluginManager.h
lldb_private::AddressRange::SetByteSize
void SetByteSize(lldb::addr_t byte_size)
Set accessor for the byte size of this range.
Definition: AddressRange.h:237
SymbolFileSymtab::m_code_indexes
lldb_private::Symtab::IndexCollection m_code_indexes
Definition: SymbolFileSymtab.h:101
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
Function.h
SymbolFileSymtab::ParseSupportFiles
bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) override
Definition: SymbolFileSymtab.cpp:217
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
SymbolContext.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
SymbolFileSymtab::GetDynamicArrayInfoForUID
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.
Definition: SymbolFileSymtab.cpp:238
lldb_private::TypeList
Definition: TypeList.h:20
lldb_private::Symtab::SortSymbolIndexesByValue
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition: Symtab.cpp:608
lldb_private::Address::IsSectionOffset
bool IsSectionOffset() const
Check if an address is section offset.
Definition: Address.h:323
SymbolFileSymtab::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: SymbolFileSymtab.cpp:46
lldb_private::Address::GetOffset
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:310
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb_private::SymbolFile::GlobalVariables
@ GlobalVariables
Definition: SymbolFile.h:59
lldb_private::Symtab::IndexCollection
std::vector< uint32_t > IndexCollection
Definition: Symtab.h:24
SymbolFileSymtab::m_objc_class_name_to_index
lldb_private::Symtab::NameToIndexMap m_objc_class_name_to_index
Definition: SymbolFileSymtab.h:103
lldb
Definition: SBAddress.h:15
SymbolFileSymtab::m_data_indexes
lldb_private::Symtab::IndexCollection m_data_indexes
Definition: SymbolFileSymtab.h:102
SymbolFileSymtab::m_source_indexes
lldb_private::Symtab::IndexCollection m_source_indexes
Definition: SymbolFileSymtab.h:99
lldb_private::Symbol::GetAddress
Address GetAddress() const
Definition: Symbol.h:73