1ac7ddfbfSEd Maste //===-- SymbolFileSymtab.cpp ------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "SymbolFileSymtab.h"
11ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
12ac7ddfbfSEd Maste #include "lldb/Core/PluginManager.h"
13ac7ddfbfSEd Maste #include "lldb/Symbol/CompileUnit.h"
14ac7ddfbfSEd Maste #include "lldb/Symbol/Function.h"
15ac7ddfbfSEd Maste #include "lldb/Symbol/ObjectFile.h"
16ac7ddfbfSEd Maste #include "lldb/Symbol/Symbol.h"
17ac7ddfbfSEd Maste #include "lldb/Symbol/SymbolContext.h"
18ac7ddfbfSEd Maste #include "lldb/Symbol/Symtab.h"
19ac7ddfbfSEd Maste #include "lldb/Symbol/TypeList.h"
20f678e45dSDimitry Andric #include "lldb/Utility/RegularExpression.h"
21a580b014SDimitry Andric #include "lldb/Utility/Timer.h"
22ac7ddfbfSEd Maste 
23ac7ddfbfSEd Maste using namespace lldb;
24ac7ddfbfSEd Maste using namespace lldb_private;
25ac7ddfbfSEd Maste 
Initialize()26435933ddSDimitry Andric void SymbolFileSymtab::Initialize() {
27ac7ddfbfSEd Maste   PluginManager::RegisterPlugin(GetPluginNameStatic(),
28435933ddSDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance);
29ac7ddfbfSEd Maste }
30ac7ddfbfSEd Maste 
Terminate()31435933ddSDimitry Andric void SymbolFileSymtab::Terminate() {
32ac7ddfbfSEd Maste   PluginManager::UnregisterPlugin(CreateInstance);
33ac7ddfbfSEd Maste }
34ac7ddfbfSEd Maste 
GetPluginNameStatic()35435933ddSDimitry Andric lldb_private::ConstString SymbolFileSymtab::GetPluginNameStatic() {
36ac7ddfbfSEd Maste   static ConstString g_name("symtab");
37ac7ddfbfSEd Maste   return g_name;
38ac7ddfbfSEd Maste }
39ac7ddfbfSEd Maste 
GetPluginDescriptionStatic()40435933ddSDimitry Andric const char *SymbolFileSymtab::GetPluginDescriptionStatic() {
41ac7ddfbfSEd Maste   return "Reads debug symbols from an object file's symbol table.";
42ac7ddfbfSEd Maste }
43ac7ddfbfSEd Maste 
CreateInstance(ObjectFile * obj_file)44435933ddSDimitry Andric SymbolFile *SymbolFileSymtab::CreateInstance(ObjectFile *obj_file) {
45ac7ddfbfSEd Maste   return new SymbolFileSymtab(obj_file);
46ac7ddfbfSEd Maste }
47ac7ddfbfSEd Maste 
GetTypes(SymbolContextScope * sc_scope,TypeClass type_mask,lldb_private::TypeList & type_list)48435933ddSDimitry Andric size_t SymbolFileSymtab::GetTypes(SymbolContextScope *sc_scope,
49*b5893f02SDimitry Andric                                   TypeClass type_mask,
50435933ddSDimitry Andric                                   lldb_private::TypeList &type_list) {
51ac7ddfbfSEd Maste   return 0;
52ac7ddfbfSEd Maste }
53ac7ddfbfSEd Maste 
SymbolFileSymtab(ObjectFile * obj_file)54435933ddSDimitry Andric SymbolFileSymtab::SymbolFileSymtab(ObjectFile *obj_file)
55435933ddSDimitry Andric     : SymbolFile(obj_file), m_source_indexes(), m_func_indexes(),
56435933ddSDimitry Andric       m_code_indexes(), m_objc_class_name_to_index() {}
57ac7ddfbfSEd Maste 
~SymbolFileSymtab()58435933ddSDimitry Andric SymbolFileSymtab::~SymbolFileSymtab() {}
59ac7ddfbfSEd Maste 
CalculateAbilities()60435933ddSDimitry Andric uint32_t SymbolFileSymtab::CalculateAbilities() {
61ac7ddfbfSEd Maste   uint32_t abilities = 0;
62435933ddSDimitry Andric   if (m_obj_file) {
63ac7ddfbfSEd Maste     const Symtab *symtab = m_obj_file->GetSymtab();
64435933ddSDimitry Andric     if (symtab) {
65ac7ddfbfSEd Maste       //----------------------------------------------------------------------
664ba319b5SDimitry Andric       // The snippet of code below will get the indexes the module symbol table
674ba319b5SDimitry Andric       // entries that are code, data, or function related (debug info), sort
684ba319b5SDimitry Andric       // them by value (address) and dump the sorted symbols.
69ac7ddfbfSEd Maste       //----------------------------------------------------------------------
70435933ddSDimitry Andric       if (symtab->AppendSymbolIndexesWithType(eSymbolTypeSourceFile,
71435933ddSDimitry Andric                                               m_source_indexes)) {
72ac7ddfbfSEd Maste         abilities |= CompileUnits;
73ac7ddfbfSEd Maste       }
74ac7ddfbfSEd Maste 
75435933ddSDimitry Andric       if (symtab->AppendSymbolIndexesWithType(
76435933ddSDimitry Andric               eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny,
77435933ddSDimitry Andric               m_func_indexes)) {
78ac7ddfbfSEd Maste         symtab->SortSymbolIndexesByValue(m_func_indexes, true);
79ac7ddfbfSEd Maste         abilities |= Functions;
80ac7ddfbfSEd Maste       }
81ac7ddfbfSEd Maste 
82435933ddSDimitry Andric       if (symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugNo,
83435933ddSDimitry Andric                                               Symtab::eVisibilityAny,
84435933ddSDimitry Andric                                               m_code_indexes)) {
85ac7ddfbfSEd Maste         symtab->SortSymbolIndexesByValue(m_code_indexes, true);
869f2f44ceSEd Maste         abilities |= Functions;
87ac7ddfbfSEd Maste       }
88ac7ddfbfSEd Maste 
89435933ddSDimitry Andric       if (symtab->AppendSymbolIndexesWithType(eSymbolTypeData,
90435933ddSDimitry Andric                                               m_data_indexes)) {
91ac7ddfbfSEd Maste         symtab->SortSymbolIndexesByValue(m_data_indexes, true);
92ac7ddfbfSEd Maste         abilities |= GlobalVariables;
93ac7ddfbfSEd Maste       }
94ac7ddfbfSEd Maste 
95ac7ddfbfSEd Maste       lldb_private::Symtab::IndexCollection objc_class_indexes;
96435933ddSDimitry Andric       if (symtab->AppendSymbolIndexesWithType(eSymbolTypeObjCClass,
97435933ddSDimitry Andric                                               objc_class_indexes)) {
98435933ddSDimitry Andric         symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
99ac7ddfbfSEd Maste                                        m_objc_class_name_to_index);
100ac7ddfbfSEd Maste         m_objc_class_name_to_index.Sort();
101ac7ddfbfSEd Maste       }
102ac7ddfbfSEd Maste     }
103ac7ddfbfSEd Maste   }
104ac7ddfbfSEd Maste   return abilities;
105ac7ddfbfSEd Maste }
106ac7ddfbfSEd Maste 
GetNumCompileUnits()107435933ddSDimitry Andric uint32_t SymbolFileSymtab::GetNumCompileUnits() {
1084ba319b5SDimitry Andric   // If we don't have any source file symbols we will just have one compile
1094ba319b5SDimitry Andric   // unit for the entire object file
110ac7ddfbfSEd Maste   if (m_source_indexes.empty())
111ac7ddfbfSEd Maste     return 0;
112ac7ddfbfSEd Maste 
113435933ddSDimitry Andric   // If we have any source file symbols we will logically organize the object
1144ba319b5SDimitry Andric   // symbols using these.
115ac7ddfbfSEd Maste   return m_source_indexes.size();
116ac7ddfbfSEd Maste }
117ac7ddfbfSEd Maste 
ParseCompileUnitAtIndex(uint32_t idx)118435933ddSDimitry Andric CompUnitSP SymbolFileSymtab::ParseCompileUnitAtIndex(uint32_t idx) {
119ac7ddfbfSEd Maste   CompUnitSP cu_sp;
120ac7ddfbfSEd Maste 
1214ba319b5SDimitry Andric   // If we don't have any source file symbols we will just have one compile
1224ba319b5SDimitry Andric   // unit for the entire object file
123435933ddSDimitry Andric   if (idx < m_source_indexes.size()) {
124435933ddSDimitry Andric     const Symbol *cu_symbol =
125435933ddSDimitry Andric         m_obj_file->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
126ac7ddfbfSEd Maste     if (cu_symbol)
127435933ddSDimitry Andric       cu_sp.reset(new CompileUnit(m_obj_file->GetModule(), NULL,
128435933ddSDimitry Andric                                   cu_symbol->GetName().AsCString(), 0,
1294bb0738eSEd Maste                                   eLanguageTypeUnknown, eLazyBoolNo));
130ac7ddfbfSEd Maste   }
131ac7ddfbfSEd Maste   return cu_sp;
132ac7ddfbfSEd Maste }
133ac7ddfbfSEd Maste 
ParseLanguage(CompileUnit & comp_unit)134*b5893f02SDimitry Andric lldb::LanguageType SymbolFileSymtab::ParseLanguage(CompileUnit &comp_unit) {
135ac7ddfbfSEd Maste   return eLanguageTypeUnknown;
136ac7ddfbfSEd Maste }
137ac7ddfbfSEd Maste 
ParseFunctions(CompileUnit & comp_unit)138*b5893f02SDimitry Andric size_t SymbolFileSymtab::ParseFunctions(CompileUnit &comp_unit) {
139ac7ddfbfSEd Maste   size_t num_added = 0;
140ac7ddfbfSEd Maste   // We must at least have a valid compile unit
141ac7ddfbfSEd Maste   const Symtab *symtab = m_obj_file->GetSymtab();
142ac7ddfbfSEd Maste   const Symbol *curr_symbol = NULL;
143ac7ddfbfSEd Maste   const Symbol *next_symbol = NULL;
144ac7ddfbfSEd Maste   //  const char *prefix = m_obj_file->SymbolPrefix();
145ac7ddfbfSEd Maste   //  if (prefix == NULL)
146ac7ddfbfSEd Maste   //      prefix == "";
147ac7ddfbfSEd Maste   //
148ac7ddfbfSEd Maste   //  const uint32_t prefix_len = strlen(prefix);
149ac7ddfbfSEd Maste 
1504ba319b5SDimitry Andric   // If we don't have any source file symbols we will just have one compile
1514ba319b5SDimitry Andric   // unit for the entire object file
152435933ddSDimitry Andric   if (m_source_indexes.empty()) {
1534ba319b5SDimitry Andric     // The only time we will have a user ID of zero is when we don't have and
1544ba319b5SDimitry Andric     // source file symbols and we declare one compile unit for the entire
1554ba319b5SDimitry Andric     // object file
156435933ddSDimitry Andric     if (!m_func_indexes.empty()) {
157ac7ddfbfSEd Maste     }
158ac7ddfbfSEd Maste 
159435933ddSDimitry Andric     if (!m_code_indexes.empty()) {
160ac7ddfbfSEd Maste       //          StreamFile s(stdout);
161ac7ddfbfSEd Maste       //          symtab->Dump(&s, m_code_indexes);
162ac7ddfbfSEd Maste 
163ac7ddfbfSEd Maste       uint32_t idx = 0; // Index into the indexes
164ac7ddfbfSEd Maste       const uint32_t num_indexes = m_code_indexes.size();
165435933ddSDimitry Andric       for (idx = 0; idx < num_indexes; ++idx) {
166ac7ddfbfSEd Maste         uint32_t symbol_idx = m_code_indexes[idx];
167ac7ddfbfSEd Maste         curr_symbol = symtab->SymbolAtIndex(symbol_idx);
168435933ddSDimitry Andric         if (curr_symbol) {
169435933ddSDimitry Andric           // Union of all ranges in the function DIE (if the function is
170435933ddSDimitry Andric           // discontiguous)
171ac7ddfbfSEd Maste           AddressRange func_range(curr_symbol->GetAddress(), 0);
172435933ddSDimitry Andric           if (func_range.GetBaseAddress().IsSectionOffset()) {
173ac7ddfbfSEd Maste             uint32_t symbol_size = curr_symbol->GetByteSize();
174ac7ddfbfSEd Maste             if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
175ac7ddfbfSEd Maste               func_range.SetByteSize(symbol_size);
176435933ddSDimitry Andric             else if (idx + 1 < num_indexes) {
177ac7ddfbfSEd Maste               next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
178435933ddSDimitry Andric               if (next_symbol) {
179435933ddSDimitry Andric                 func_range.SetByteSize(
180435933ddSDimitry Andric                     next_symbol->GetAddressRef().GetOffset() -
181435933ddSDimitry Andric                     curr_symbol->GetAddressRef().GetOffset());
182ac7ddfbfSEd Maste               }
183ac7ddfbfSEd Maste             }
184ac7ddfbfSEd Maste 
185435933ddSDimitry Andric             FunctionSP func_sp(
186*b5893f02SDimitry Andric                 new Function(&comp_unit,
187ac7ddfbfSEd Maste                              symbol_idx,       // UserID is the DIE offset
188435933ddSDimitry Andric                              LLDB_INVALID_UID, // We don't have any type info
189435933ddSDimitry Andric                                                // for this function
190ac7ddfbfSEd Maste                              curr_symbol->GetMangled(), // Linker/mangled name
191ac7ddfbfSEd Maste                              NULL, // no return type for a code symbol...
192ac7ddfbfSEd Maste                              func_range)); // first address range
193ac7ddfbfSEd Maste 
194435933ddSDimitry Andric             if (func_sp.get() != NULL) {
195*b5893f02SDimitry Andric               comp_unit.AddFunction(func_sp);
196ac7ddfbfSEd Maste               ++num_added;
197ac7ddfbfSEd Maste             }
198ac7ddfbfSEd Maste           }
199ac7ddfbfSEd Maste         }
200ac7ddfbfSEd Maste       }
201ac7ddfbfSEd Maste     }
202435933ddSDimitry Andric   } else {
203ac7ddfbfSEd Maste     // We assume we
204ac7ddfbfSEd Maste   }
205ac7ddfbfSEd Maste   return num_added;
206ac7ddfbfSEd Maste }
207ac7ddfbfSEd Maste 
ParseTypes(CompileUnit & comp_unit)208*b5893f02SDimitry Andric size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
209*b5893f02SDimitry Andric 
ParseLineTable(CompileUnit & comp_unit)210*b5893f02SDimitry Andric bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
211*b5893f02SDimitry Andric 
ParseDebugMacros(CompileUnit & comp_unit)212*b5893f02SDimitry Andric bool SymbolFileSymtab::ParseDebugMacros(CompileUnit &comp_unit) {
213ac7ddfbfSEd Maste   return false;
214ac7ddfbfSEd Maste }
215ac7ddfbfSEd Maste 
ParseSupportFiles(CompileUnit & comp_unit,FileSpecList & support_files)216*b5893f02SDimitry Andric bool SymbolFileSymtab::ParseSupportFiles(CompileUnit &comp_unit,
217*b5893f02SDimitry Andric                                          FileSpecList &support_files) {
218ac7ddfbfSEd Maste   return false;
219ac7ddfbfSEd Maste }
220ac7ddfbfSEd Maste 
ParseImportedModules(const SymbolContext & sc,std::vector<ConstString> & imported_modules)221435933ddSDimitry Andric bool SymbolFileSymtab::ParseImportedModules(
222435933ddSDimitry Andric     const SymbolContext &sc, std::vector<ConstString> &imported_modules) {
2231c3bbb01SEd Maste   return false;
2241c3bbb01SEd Maste }
2251c3bbb01SEd Maste 
ParseBlocksRecursive(Function & func)226*b5893f02SDimitry Andric size_t SymbolFileSymtab::ParseBlocksRecursive(Function &func) { return 0; }
227ac7ddfbfSEd Maste 
ParseVariablesForContext(const SymbolContext & sc)228435933ddSDimitry Andric size_t SymbolFileSymtab::ParseVariablesForContext(const SymbolContext &sc) {
229ac7ddfbfSEd Maste   return 0;
230ac7ddfbfSEd Maste }
231ac7ddfbfSEd Maste 
ResolveTypeUID(lldb::user_id_t type_uid)232435933ddSDimitry Andric Type *SymbolFileSymtab::ResolveTypeUID(lldb::user_id_t type_uid) {
233ac7ddfbfSEd Maste   return NULL;
234ac7ddfbfSEd Maste }
235ac7ddfbfSEd Maste 
236*b5893f02SDimitry Andric llvm::Optional<SymbolFile::ArrayInfo>
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)237*b5893f02SDimitry Andric SymbolFileSymtab::GetDynamicArrayInfoForUID(
238*b5893f02SDimitry Andric     lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
239*b5893f02SDimitry Andric   return llvm::None;
240*b5893f02SDimitry Andric }
241*b5893f02SDimitry Andric 
CompleteType(lldb_private::CompilerType & compiler_type)242435933ddSDimitry Andric bool SymbolFileSymtab::CompleteType(lldb_private::CompilerType &compiler_type) {
243ac7ddfbfSEd Maste   return false;
244ac7ddfbfSEd Maste }
245ac7ddfbfSEd Maste 
ResolveSymbolContext(const Address & so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)246435933ddSDimitry Andric uint32_t SymbolFileSymtab::ResolveSymbolContext(const Address &so_addr,
247*b5893f02SDimitry Andric                                                 SymbolContextItem resolve_scope,
248435933ddSDimitry Andric                                                 SymbolContext &sc) {
249ac7ddfbfSEd Maste   if (m_obj_file->GetSymtab() == NULL)
250ac7ddfbfSEd Maste     return 0;
251ac7ddfbfSEd Maste 
252ac7ddfbfSEd Maste   uint32_t resolved_flags = 0;
253435933ddSDimitry Andric   if (resolve_scope & eSymbolContextSymbol) {
254435933ddSDimitry Andric     sc.symbol = m_obj_file->GetSymtab()->FindSymbolContainingFileAddress(
255435933ddSDimitry Andric         so_addr.GetFileAddress());
256ac7ddfbfSEd Maste     if (sc.symbol)
257ac7ddfbfSEd Maste       resolved_flags |= eSymbolContextSymbol;
258ac7ddfbfSEd Maste   }
259ac7ddfbfSEd Maste   return resolved_flags;
260ac7ddfbfSEd Maste }
261ac7ddfbfSEd Maste 
262ac7ddfbfSEd Maste //------------------------------------------------------------------
263ac7ddfbfSEd Maste // PluginInterface protocol
264ac7ddfbfSEd Maste //------------------------------------------------------------------
GetPluginName()265435933ddSDimitry Andric lldb_private::ConstString SymbolFileSymtab::GetPluginName() {
266ac7ddfbfSEd Maste   return GetPluginNameStatic();
267ac7ddfbfSEd Maste }
268ac7ddfbfSEd Maste 
GetPluginVersion()269435933ddSDimitry Andric uint32_t SymbolFileSymtab::GetPluginVersion() { return 1; }
270