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 
LLDB_PLUGIN_DEFINE(SymbolVendorELF)28 LLDB_PLUGIN_DEFINE(SymbolVendorELF)
29 
30 // SymbolVendorELF constructor
31 SymbolVendorELF::SymbolVendorELF(const lldb::ModuleSP &module_sp)
32     : SymbolVendor(module_sp) {}
33 
Initialize()34 void SymbolVendorELF::Initialize() {
35   PluginManager::RegisterPlugin(GetPluginNameStatic(),
36                                 GetPluginDescriptionStatic(), CreateInstance);
37 }
38 
Terminate()39 void SymbolVendorELF::Terminate() {
40   PluginManager::UnregisterPlugin(CreateInstance);
41 }
42 
GetPluginNameStatic()43 lldb_private::ConstString SymbolVendorELF::GetPluginNameStatic() {
44   static ConstString g_name("ELF");
45   return g_name;
46 }
47 
GetPluginDescriptionStatic()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 *
CreateInstance(const lldb::ModuleSP & module_sp,lldb_private::Stream * feedback_strm)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   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
85   Timer scoped_timer(func_cat, "SymbolVendorELF::CreateInstance (module = %s)",
86                      module_sp->GetFileSpec().GetPath().c_str());
87 
88   ModuleSpec module_spec;
89 
90   module_spec.GetFileSpec() = obj_file->GetFileSpec();
91   FileSystem::Instance().Resolve(module_spec.GetFileSpec());
92   module_spec.GetSymbolFileSpec() = fspec;
93   module_spec.GetUUID() = uuid;
94   FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths();
95   FileSpec dsym_fspec =
96       Symbols::LocateExecutableSymbolFile(module_spec, search_paths);
97   if (!dsym_fspec)
98     return nullptr;
99 
100   DataBufferSP dsym_file_data_sp;
101   lldb::offset_t dsym_file_data_offset = 0;
102   ObjectFileSP dsym_objfile_sp = ObjectFile::FindPlugin(
103       module_sp, &dsym_fspec, 0, FileSystem::Instance().GetByteSize(dsym_fspec),
104       dsym_file_data_sp, dsym_file_data_offset);
105   if (!dsym_objfile_sp)
106     return nullptr;
107 
108   // This objfile is for debugging purposes. Sadly, ObjectFileELF won't
109   // be able to figure this out consistently as the symbol file may not
110   // have stripped the code sections, etc.
111   dsym_objfile_sp->SetType(ObjectFile::eTypeDebugInfo);
112 
113   SymbolVendorELF *symbol_vendor = new SymbolVendorELF(module_sp);
114 
115   // Get the module unified section list and add our debug sections to
116   // that.
117   SectionList *module_section_list = module_sp->GetSectionList();
118   SectionList *objfile_section_list = dsym_objfile_sp->GetSectionList();
119 
120   static const SectionType g_sections[] = {
121       eSectionTypeDWARFDebugAbbrev,     eSectionTypeDWARFDebugAddr,
122       eSectionTypeDWARFDebugAranges,    eSectionTypeDWARFDebugCuIndex,
123       eSectionTypeDWARFDebugFrame,      eSectionTypeDWARFDebugInfo,
124       eSectionTypeDWARFDebugLine,       eSectionTypeDWARFDebugLineStr,
125       eSectionTypeDWARFDebugLoc,        eSectionTypeDWARFDebugLocLists,
126       eSectionTypeDWARFDebugMacInfo,    eSectionTypeDWARFDebugMacro,
127       eSectionTypeDWARFDebugNames,      eSectionTypeDWARFDebugPubNames,
128       eSectionTypeDWARFDebugPubTypes,   eSectionTypeDWARFDebugRanges,
129       eSectionTypeDWARFDebugRngLists,   eSectionTypeDWARFDebugStr,
130       eSectionTypeDWARFDebugStrOffsets, eSectionTypeDWARFDebugTypes,
131       eSectionTypeELFSymbolTable,       eSectionTypeDWARFGNUDebugAltLink,
132   };
133   for (SectionType section_type : g_sections) {
134     if (SectionSP section_sp =
135             objfile_section_list->FindSectionByType(section_type, true)) {
136       if (SectionSP module_section_sp =
137               module_section_list->FindSectionByType(section_type, true))
138         module_section_list->ReplaceSection(module_section_sp->GetID(),
139                                             section_sp);
140       else
141         module_section_list->AddSection(section_sp);
142     }
143   }
144 
145   symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
146   return symbol_vendor;
147 }
148 
149 // PluginInterface protocol
GetPluginName()150 ConstString SymbolVendorELF::GetPluginName() { return GetPluginNameStatic(); }
151 
GetPluginVersion()152 uint32_t SymbolVendorELF::GetPluginVersion() { return 1; }
153