LLDB  mainline
SymbolVendorELF.cpp
Go to the documentation of this file.
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 <cstring>
12 
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/Section.h"
18 #include "lldb/Host/Host.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Target/Target.h"
23 #include "lldb/Utility/Timer.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
29 
30 // SymbolVendorELF constructor
31 SymbolVendorELF::SymbolVendorELF(const lldb::ModuleSP &module_sp)
32  : SymbolVendor(module_sp) {}
33 
35  PluginManager::RegisterPlugin(GetPluginNameStatic(),
36  GetPluginDescriptionStatic(), CreateInstance);
37 }
38 
40  PluginManager::UnregisterPlugin(CreateInstance);
41 }
42 
44  static ConstString g_name("ELF");
45  return g_name;
46 }
47 
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.
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(
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[] = {
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 
lldb_private::SectionList::ReplaceSection
bool ReplaceSection(lldb::user_id_t sect_id, const lldb::SectionSP &section_sp, uint32_t depth=UINT32_MAX)
Definition: Section.cpp:447
lldb_private::UUID
Definition: UUID.h:23
lldb::eSectionTypeDWARFDebugLoc
@ eSectionTypeDWARFDebugLoc
Definition: lldb-enumerations.h:670
lldb_private::ObjectFile::GetFileSpec
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:290
lldb_private::SymbolVendor::AddSymbolFileRepresentation
void AddSymbolFileRepresentation(const lldb::ObjectFileSP &objfile_sp)
Definition: SymbolVendor.cpp:64
ModuleSpec.h
Host.h
SymbolVendorELF::GetPluginVersion
uint32_t GetPluginVersion() override
Definition: SymbolVendorELF.cpp:151
lldb::eSectionTypeDWARFDebugAranges
@ eSectionTypeDWARFDebugAranges
Definition: lldb-enumerations.h:665
lldb::eSectionTypeELFSymbolTable
@ eSectionTypeELFSymbolTable
Elf SHT_SYMTAB section.
Definition: lldb-enumerations.h:682
lldb::eSectionTypeDWARFDebugRanges
@ eSectionTypeDWARFDebugRanges
Definition: lldb-enumerations.h:675
lldb::eSectionTypeDWARFDebugMacro
@ eSectionTypeDWARFDebugMacro
Definition: lldb-enumerations.h:672
lldb::eSectionTypeDWARFDebugFrame
@ eSectionTypeDWARFDebugFrame
Definition: lldb-enumerations.h:667
Module.h
lldb::eSectionTypeDWARFDebugLine
@ eSectionTypeDWARFDebugLine
Definition: lldb-enumerations.h:669
lldb::eSectionTypeDWARFDebugAddr
@ eSectionTypeDWARFDebugAddr
Definition: lldb-enumerations.h:664
LocateSymbolFile.h
lldb_private::SectionList
Definition: Section.h:34
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::Stream
Definition: Stream.h:28
lldb::eSectionTypeDWARFDebugStrOffsets
@ eSectionTypeDWARFDebugStrOffsets
Definition: lldb-enumerations.h:677
Section.h
lldb_private::SymbolVendor
Definition: SymbolVendor.h:31
lldb_private::ModuleSpec::GetSymbolFileSpec
FileSpec & GetSymbolFileSpec()
Definition: ModuleSpec.h:82
SymbolVendorELF::Terminate
static void Terminate()
Definition: SymbolVendorELF.cpp:39
Target.h
lldb_private::ModuleSpec::GetUUID
UUID & GetUUID()
Definition: ModuleSpec.h:104
lldb_private::SectionList::AddSection
size_t AddSection(const lldb::SectionSP &section_sp)
Definition: Section.cpp:407
lldb_private::FileSpec
Definition: FileSpec.h:56
SymbolVendorELF::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: SymbolVendorELF.cpp:43
lldb_private::ObjectFile::GetSectionList
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:583
lldb::eSectionTypeDWARFDebugAbbrev
@ eSectionTypeDWARFDebugAbbrev
Definition: lldb-enumerations.h:663
ObjectFileELF::GetUUID
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
Definition: ObjectFileELF.cpp:763
lldb::eSectionTypeDWARFDebugStr
@ eSectionTypeDWARFDebugStr
Definition: lldb-enumerations.h:676
StreamString.h
ObjectFileELF.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb::eSectionTypeDWARFDebugInfo
@ eSectionTypeDWARFDebugInfo
Definition: lldb-enumerations.h:668
Timer.h
lldb::eSectionTypeDWARFDebugTypes
@ eSectionTypeDWARFDebugTypes
DWARF .debug_types section.
Definition: lldb-enumerations.h:695
SymbolVendorELF::Initialize
static void Initialize()
Definition: SymbolVendorELF.cpp:34
lldb::eSectionTypeDWARFGNUDebugAltLink
@ eSectionTypeDWARFGNUDebugAltLink
Definition: lldb-enumerations.h:694
ObjectFileELF::GetDebugLink
llvm::Optional< lldb_private::FileSpec > GetDebugLink()
Return the contents of the .gnu_debuglink section, if the object file contains it.
Definition: ObjectFileELF.cpp:801
lldb::eSectionTypeDWARFDebugCuIndex
@ eSectionTypeDWARFDebugCuIndex
Definition: lldb-enumerations.h:666
ObjectFile.h
lldb_private::SectionList::FindSectionByType
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition: Section.cpp:524
lldb_private::ModuleSpec::GetFileSpec
FileSpec & GetFileSpec()
Definition: ModuleSpec.h:58
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
uint32_t
lldb::SectionType
SectionType
Definition: lldb-enumerations.h:646
lldb::eSectionTypeDWARFDebugPubTypes
@ eSectionTypeDWARFDebugPubTypes
Definition: lldb-enumerations.h:674
lldb::eSectionTypeDWARFDebugNames
@ eSectionTypeDWARFDebugNames
DWARF v5 .debug_names.
Definition: lldb-enumerations.h:696
SymbolVendorELF.h
SymbolVendorELF::GetPluginName
lldb_private::ConstString GetPluginName() override
Definition: SymbolVendorELF.cpp:149
lldb::eSectionTypeDWARFDebugPubNames
@ eSectionTypeDWARFDebugPubNames
Definition: lldb-enumerations.h:673
ObjectFileELF
Definition: ObjectFileELF.h:57
lldb::eSectionTypeDWARFDebugLineStr
@ eSectionTypeDWARFDebugLineStr
DWARF v5 .debug_line_str.
Definition: lldb-enumerations.h:698
PluginManager.h
LLDB_SCOPED_TIMERF
#define LLDB_SCOPED_TIMERF(FMT,...)
Definition: Timer.h:99
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
SymbolVendorELF
Definition: SymbolVendorELF.h:15
SymbolVendorELF::CreateInstance
static lldb_private::SymbolVendor * CreateInstance(const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
Definition: SymbolVendorELF.cpp:59
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb::eSectionTypeDWARFDebugMacInfo
@ eSectionTypeDWARFDebugMacInfo
Definition: lldb-enumerations.h:671
SymbolVendorELF::GetPluginDescriptionStatic
static const char * GetPluginDescriptionStatic()
Definition: SymbolVendorELF.cpp:48
lldb
Definition: SBAddress.h:15
lldb::eSectionTypeDWARFDebugLocLists
@ eSectionTypeDWARFDebugLocLists
DWARF v5 .debug_loclists.
Definition: lldb-enumerations.h:700
lldb::eSectionTypeDWARFDebugRngLists
@ eSectionTypeDWARFDebugRngLists
DWARF v5 .debug_rnglists.
Definition: lldb-enumerations.h:699