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/Core/StreamString.h"
19 #include "lldb/Core/Timer.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/Symbols.h"
22 #include "lldb/Symbol/ObjectFile.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 //----------------------------------------------------------------------
28 // SymbolVendorELF constructor
29 //----------------------------------------------------------------------
30 SymbolVendorELF::SymbolVendorELF(const lldb::ModuleSP &module_sp)
31     : SymbolVendor(module_sp) {}
32 
33 //----------------------------------------------------------------------
34 // Destructor
35 //----------------------------------------------------------------------
36 SymbolVendorELF::~SymbolVendorELF() {}
37 
38 void SymbolVendorELF::Initialize() {
39   PluginManager::RegisterPlugin(GetPluginNameStatic(),
40                                 GetPluginDescriptionStatic(), CreateInstance);
41 }
42 
43 void SymbolVendorELF::Terminate() {
44   PluginManager::UnregisterPlugin(CreateInstance);
45 }
46 
47 lldb_private::ConstString SymbolVendorELF::GetPluginNameStatic() {
48   static ConstString g_name("ELF");
49   return g_name;
50 }
51 
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
61 // vendors to allow for complex debug information file setups, and to
62 // also allow for finding separate debug information files.
63 //----------------------------------------------------------------------
64 SymbolVendor *
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   Timer scoped_timer(LLVM_PRETTY_FUNCTION,
96                      "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     module_spec.GetFileSpec().ResolvePath();
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 = ObjectFile::FindPlugin(
112           module_sp, &dsym_fspec, 0, dsym_fspec.GetByteSize(),
113           dsym_file_data_sp, dsym_file_data_offset);
114       if (dsym_objfile_sp) {
115         // This objfile is for debugging purposes. Sadly, ObjectFileELF won't be
116         // able
117         // to figure this out consistently as the symbol file may not have
118         // stripped the
119         // code sections, etc.
120         dsym_objfile_sp->SetType(ObjectFile::eTypeDebugInfo);
121 
122         SymbolVendorELF *symbol_vendor = new SymbolVendorELF(module_sp);
123         if (symbol_vendor) {
124           // Get the module unified section list and add our debug sections to
125           // that.
126           SectionList *module_section_list = module_sp->GetSectionList();
127           SectionList *objfile_section_list = dsym_objfile_sp->GetSectionList();
128 
129           static const SectionType g_sections[] = {
130               eSectionTypeDWARFDebugAbbrev,     eSectionTypeDWARFDebugAddr,
131               eSectionTypeDWARFDebugAranges,    eSectionTypeDWARFDebugFrame,
132               eSectionTypeDWARFDebugInfo,       eSectionTypeDWARFDebugLine,
133               eSectionTypeDWARFDebugLoc,        eSectionTypeDWARFDebugMacInfo,
134               eSectionTypeDWARFDebugPubNames,   eSectionTypeDWARFDebugPubTypes,
135               eSectionTypeDWARFDebugRanges,     eSectionTypeDWARFDebugStr,
136               eSectionTypeDWARFDebugStrOffsets, eSectionTypeELFSymbolTable,
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 //------------------------------------------------------------------
166 ConstString SymbolVendorELF::GetPluginName() { return GetPluginNameStatic(); }
167 
168 uint32_t SymbolVendorELF::GetPluginVersion() { return 1; }
169