1 //===-- SymbolFileSymtab.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolFileSymtab.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Core/Timer.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 #include "lldb/Symbol/Symtab.h"
20 #include "lldb/Symbol/TypeList.h"
21 #include "lldb/Utility/RegularExpression.h"
24 using namespace lldb_private;
26 void SymbolFileSymtab::Initialize() {
27 PluginManager::RegisterPlugin(GetPluginNameStatic(),
28 GetPluginDescriptionStatic(), CreateInstance);
31 void SymbolFileSymtab::Terminate() {
32 PluginManager::UnregisterPlugin(CreateInstance);
35 lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() {
36 static ConstString g_name("symtab");
40 const char *SymbolFileSymtab::GetPluginDescriptionStatic() {
41 return "Reads debug symbols from an object file's symbol table.";
44 SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFile *obj_file) {
45 return new SymbolFileSymtab(obj_file);
48 size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
50 lldb_private::TypeList &type_list) {
54 SymbolFileSymtab::SymbolFileSymtab(ObjectFile *obj_file)
55 : SymbolFile(obj_file), m_source_indexes(), m_func_indexes(),
56 m_code_indexes(), m_objc_class_name_to_index() {}
58 SymbolFileSymtab::~SymbolFileSymtab() {}
60 uint32_t SymbolFileSymtab::CalculateAbilities() {
61 uint32_t abilities = 0;
63 const Symtab *symtab = m_obj_file->GetSymtab();
65 //----------------------------------------------------------------------
66 // The snippet of code below will get the indexes the module symbol
67 // table entries that are code, data, or function related (debug info),
68 // sort them by value (address) and dump the sorted symbols.
69 //----------------------------------------------------------------------
70 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,
72 abilities |= CompileUnits;
75 if (symtab->AppendSymbolIndexesWithType(
76 eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
78 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
79 abilities |= Functions;
82 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
83 Symtab::eVisibilityAny,
85 symtab->SortSymbolIndexesByValue(m_code_indexes, true);
86 abilities |= Functions;
89 if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,
91 symtab->SortSymbolIndexesByValue(m_data_indexes, true);
92 abilities |= GlobalVariables;
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();
107 uint32_t SymbolFileSymtab::GetNumCompileUnits() {
108 // If we don't have any source file symbols we will just have one compile unit
110 // the entire object file
111 if (m_source_indexes.empty())
114 // If we have any source file symbols we will logically organize the object
117 return m_source_indexes.size();
120 CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
123 // If we don't have any source file symbols we will just have one compile unit
125 // the entire object file
126 if (idx < m_source_indexes.size()) {
127 const Symbol *cu_symbol =
128 m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
130 cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL,
131 cu_symbol->GetName().AsCString(), 0,
132 eLanguageTypeUnknown, eLazyBoolNo));
138 SymbolFileSymtab::ParseCompileUnitLanguage(const SymbolContext &sc) {
139 return eLanguageTypeUnknown;
142 size_t SymbolFileSymtab::ParseCompileUnitFunctions(const SymbolContext &sc) {
143 size_t num_added = 0;
144 // We must at least have a valid compile unit
145 assert(sc.comp_unit != NULL);
146 const Symtab *symtab = m_obj_file->GetSymtab();
147 const Symbol *curr_symbol = NULL;
148 const Symbol *next_symbol = NULL;
149 // const char *prefix = m_obj_file->SymbolPrefix();
150 // if (prefix == NULL)
153 // const uint32_t prefix_len = strlen(prefix);
155 // If we don't have any source file symbols we will just have one compile unit
157 // the entire object file
158 if (m_source_indexes.empty()) {
159 // The only time we will have a user ID of zero is when we don't have
160 // and source file symbols and we declare one compile unit for the
161 // entire object file
162 if (!m_func_indexes.empty()) {
165 if (!m_code_indexes.empty()) {
166 // StreamFile s(stdout);
167 // symtab->Dump(&s, m_code_indexes);
169 uint32_t idx = 0; // Index into the indexes
170 const uint32_t num_indexes = m_code_indexes.size();
171 for (idx = 0; idx < num_indexes; ++idx) {
172 uint32_t symbol_idx = m_code_indexes[idx];
173 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
175 // Union of all ranges in the function DIE (if the function is
177 AddressRange func_range(curr_symbol->GetAddress(), 0);
178 if (func_range.GetBaseAddress().IsSectionOffset()) {
179 uint32_t symbol_size = curr_symbol->GetByteSize();
180 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
181 func_range.SetByteSize(symbol_size);
182 else if (idx + 1 < num_indexes) {
183 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
185 func_range.SetByteSize(
186 next_symbol->GetAddressRef().GetOffset() -
187 curr_symbol->GetAddressRef().GetOffset());
192 new Function(sc.comp_unit,
193 symbol_idx, // UserID is the DIE offset
194 LLDB_INVALID_UID, // We don't have any type info
196 curr_symbol->GetMangled(), // Linker/mangled name
197 NULL, // no return type for a code symbol...
198 func_range)); // first address range
200 if (func_sp.get() != NULL) {
201 sc.comp_unit->AddFunction(func_sp);
214 bool SymbolFileSymtab::ParseCompileUnitLineTable(const SymbolContext &sc) {
218 bool SymbolFileSymtab::ParseCompileUnitDebugMacros(const SymbolContext &sc) {
222 bool SymbolFileSymtab::ParseCompileUnitSupportFiles(
223 const SymbolContext &sc, FileSpecList &support_files) {
227 bool SymbolFileSymtab::ParseImportedModules(
228 const SymbolContext &sc, std::vector<ConstString> &imported_modules) {
232 size_t SymbolFileSymtab::ParseFunctionBlocks(const SymbolContext &sc) {
236 size_t SymbolFileSymtab::ParseTypes(const SymbolContext &sc) { return 0; }
238 size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
242 Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
246 bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
250 uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
251 uint32_t resolve_scope,
253 if (m_obj_file->GetSymtab() == NULL)
256 uint32_t resolved_flags = 0;
257 if (resolve_scope & eSymbolContextSymbol) {
258 sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(
259 so_addr.GetFileAddress());
261 resolved_flags |= eSymbolContextSymbol;
263 return resolved_flags;
266 //------------------------------------------------------------------
267 // PluginInterface protocol
268 //------------------------------------------------------------------
269 lldb_private::ConstString SymbolFileSymtab::GetPluginName() {
270 return GetPluginNameStatic();
273 uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; }