1 //===-- SymbolVendorELF.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 "SymbolVendorELF.h"
11 
12 #include <string.h>
13 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Host/Host.h"
19 #include "lldb/Host/Symbols.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Utility/StreamString.h"
22 #include "lldb/Utility/Timer.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 //----------------------------------------------------------------------
28 // SymbolVendorELF constructor
29 //----------------------------------------------------------------------
SymbolVendorELF(const lldb::ModuleSP & module_sp)30 SymbolVendorELF::SymbolVendorELF(const lldb::ModuleSP &module_sp)
31     : SymbolVendor(module_sp) {}
32 
33 //----------------------------------------------------------------------
34 // Destructor
35 //----------------------------------------------------------------------
~SymbolVendorELF()36 SymbolVendorELF::~SymbolVendorELF() {}
37 
Initialize()38 void SymbolVendorELF::Initialize() {
39   PluginManager::RegisterPlugin(GetPluginNameStatic(),
40                                 GetPluginDescriptionStatic(), CreateInstance);
41 }
42 
Terminate()43 void SymbolVendorELF::Terminate() {
44   PluginManager::UnregisterPlugin(CreateInstance);
45 }
46 
GetPluginNameStatic()47 lldb_private::ConstString SymbolVendorELF::GetPluginNameStatic() {
48   static ConstString g_name("ELF");
49   return g_name;
50 }
51 
GetPluginDescriptionStatic()52 const char *SymbolVendorELF::GetPluginDescriptionStatic() {
53   return "Symbol vendor for ELF that looks for dSYM files that match "
54          "executables.";
55 }
56 
57 //----------------------------------------------------------------------
58 // CreateInstance
59 //
60 // Platforms can register a callback to use when creating symbol vendors to
61 // allow for complex debug information file setups, and to also allow for
62 // finding separate debug information files.
63 //----------------------------------------------------------------------
64 SymbolVendor *
CreateInstance(const lldb::ModuleSP & module_sp,lldb_private::Stream * feedback_strm)65 SymbolVendorELF::CreateInstance(const lldb::ModuleSP &module_sp,
66                                 lldb_private::Stream *feedback_strm) {
67   if (!module_sp)
68     return NULL;
69 
70   ObjectFile *obj_file = module_sp->GetObjectFile();
71   if (!obj_file)
72     return NULL;
73 
74   static ConstString obj_file_elf("elf");
75   ConstString obj_name = obj_file->GetPluginName();
76   if (obj_name != obj_file_elf)
77     return NULL;
78 
79   lldb_private::UUID uuid;
80   if (!obj_file->GetUUID(&uuid))
81     return NULL;
82 
83   // Get the .gnu_debuglink file (if specified).
84   FileSpecList file_spec_list = obj_file->GetDebugSymbolFilePaths();
85 
86   // If the module specified a filespec, use it first.
87   FileSpec debug_symbol_fspec(module_sp->GetSymbolFileFileSpec());
88   if (debug_symbol_fspec)
89     file_spec_list.Insert(0, debug_symbol_fspec);
90 
91   // If we have no debug symbol files, then nothing to do.
92   if (file_spec_list.IsEmpty())
93     return NULL;
94 
95   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
96   Timer scoped_timer(func_cat, "SymbolVendorELF::CreateInstance (module = %s)",
97                      module_sp->GetFileSpec().GetPath().c_str());
98 
99   for (size_t idx = 0; idx < file_spec_list.GetSize(); ++idx) {
100     ModuleSpec module_spec;
101     const FileSpec fspec = file_spec_list.GetFileSpecAtIndex(idx);
102 
103     module_spec.GetFileSpec() = obj_file->GetFileSpec();
104     FileSystem::Instance().Resolve(module_spec.GetFileSpec());
105     module_spec.GetSymbolFileSpec() = fspec;
106     module_spec.GetUUID() = uuid;
107     FileSpec dsym_fspec = Symbols::LocateExecutableSymbolFile(module_spec);
108     if (dsym_fspec) {
109       DataBufferSP dsym_file_data_sp;
110       lldb::offset_t dsym_file_data_offset = 0;
111       ObjectFileSP dsym_objfile_sp =
112           ObjectFile::FindPlugin(module_sp, &dsym_fspec, 0,
113                                  FileSystem::Instance().GetByteSize(dsym_fspec),
114                                  dsym_file_data_sp, dsym_file_data_offset);
115       if (dsym_objfile_sp) {
116         // This objfile is for debugging purposes. Sadly, ObjectFileELF won't
117         // be able to figure this out consistently as the symbol file may not
118         // have stripped the code sections, etc.
119         dsym_objfile_sp->SetType(ObjectFile::eTypeDebugInfo);
120 
121         SymbolVendorELF *symbol_vendor = new SymbolVendorELF(module_sp);
122         if (symbol_vendor) {
123           // Get the module unified section list and add our debug sections to
124           // that.
125           SectionList *module_section_list = module_sp->GetSectionList();
126           SectionList *objfile_section_list = dsym_objfile_sp->GetSectionList();
127 
128           static const SectionType g_sections[] = {
129               eSectionTypeDWARFDebugAbbrev,   eSectionTypeDWARFDebugAddr,
130               eSectionTypeDWARFDebugAranges,  eSectionTypeDWARFDebugCuIndex,
131               eSectionTypeDWARFDebugFrame,    eSectionTypeDWARFDebugInfo,
132               eSectionTypeDWARFDebugLine,     eSectionTypeDWARFDebugLoc,
133               eSectionTypeDWARFDebugMacInfo,  eSectionTypeDWARFDebugPubNames,
134               eSectionTypeDWARFDebugPubTypes, eSectionTypeDWARFDebugRanges,
135               eSectionTypeDWARFDebugStr,      eSectionTypeDWARFDebugStrOffsets,
136               eSectionTypeELFSymbolTable,     eSectionTypeDWARFGNUDebugAltLink,
137           };
138           for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]);
139                ++idx) {
140             SectionType section_type = g_sections[idx];
141             SectionSP section_sp(
142                 objfile_section_list->FindSectionByType(section_type, true));
143             if (section_sp) {
144               SectionSP module_section_sp(
145                   module_section_list->FindSectionByType(section_type, true));
146               if (module_section_sp)
147                 module_section_list->ReplaceSection(module_section_sp->GetID(),
148                                                     section_sp);
149               else
150                 module_section_list->AddSection(section_sp);
151             }
152           }
153 
154           symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
155           return symbol_vendor;
156         }
157       }
158     }
159   }
160   return NULL;
161 }
162 
163 //------------------------------------------------------------------
164 // PluginInterface protocol
165 //------------------------------------------------------------------
GetPluginName()166 ConstString SymbolVendorELF::GetPluginName() { return GetPluginNameStatic(); }
167 
GetPluginVersion()168 uint32_t SymbolVendorELF::GetPluginVersion() { return 1; }
169