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