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"
17#include "lldb/Core/Section.h"
18#include "lldb/Host/Host.h"
20#include "lldb/Target/Target.h"
22#include "lldb/Utility/Timer.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
28
29// SymbolVendorELF constructor
31 : SymbolVendor(module_sp) {}
32
36}
37
40}
41
43 return "Symbol vendor for ELF that looks for dSYM files that match "
44 "executables.";
45}
46
47// CreateInstance
48//
49// Platforms can register a callback to use when creating symbol vendors to
50// allow for complex debug information file setups, and to also allow for
51// finding separate debug information files.
54 lldb_private::Stream *feedback_strm) {
55 if (!module_sp)
56 return nullptr;
57
58 ObjectFileELF *obj_file =
59 llvm::dyn_cast_or_null<ObjectFileELF>(module_sp->GetObjectFile());
60 if (!obj_file)
61 return nullptr;
62
63 lldb_private::UUID uuid = obj_file->GetUUID();
64 if (!uuid)
65 return nullptr;
66
67 // If the main object file already contains debug info, then we are done.
68 if (obj_file->GetSectionList()->FindSectionByType(
70 return nullptr;
71
72 // If the module specified a filespec, use that.
73 FileSpec fspec = module_sp->GetSymbolFileFileSpec();
74 // Otherwise, try gnu_debuglink, if one exists.
75 if (!fspec)
76 fspec = obj_file->GetDebugLink().value_or(FileSpec());
77
78 LLDB_SCOPED_TIMERF("SymbolVendorELF::CreateInstance (module = %s)",
79 module_sp->GetFileSpec().GetPath().c_str());
80
81 ModuleSpec module_spec;
82
83 module_spec.GetFileSpec() = obj_file->GetFileSpec();
85 module_spec.GetSymbolFileSpec() = fspec;
86 module_spec.GetUUID() = uuid;
88 FileSpec dsym_fspec =
89 PluginManager::LocateExecutableSymbolFile(module_spec, search_paths);
90 if (!dsym_fspec)
91 return nullptr;
92
93 DataBufferSP dsym_file_data_sp;
94 lldb::offset_t dsym_file_data_offset = 0;
95 ObjectFileSP dsym_objfile_sp = ObjectFile::FindPlugin(
96 module_sp, &dsym_fspec, 0, FileSystem::Instance().GetByteSize(dsym_fspec),
97 dsym_file_data_sp, dsym_file_data_offset);
98 if (!dsym_objfile_sp)
99 return nullptr;
100
101 // This objfile is for debugging purposes. Sadly, ObjectFileELF won't
102 // be able to figure this out consistently as the symbol file may not
103 // have stripped the code sections, etc.
104 dsym_objfile_sp->SetType(ObjectFile::eTypeDebugInfo);
105
106 SymbolVendorELF *symbol_vendor = new SymbolVendorELF(module_sp);
107
108 // Get the module unified section list and add our debug sections to
109 // that.
110 SectionList *module_section_list = module_sp->GetSectionList();
111 SectionList *objfile_section_list = dsym_objfile_sp->GetSectionList();
112
113 if (!module_section_list || !objfile_section_list)
114 return nullptr;
115
116 static const SectionType g_sections[] = {
128 };
129 for (SectionType section_type : g_sections) {
130 if (SectionSP section_sp =
131 objfile_section_list->FindSectionByType(section_type, true)) {
132 if (SectionSP module_section_sp =
133 module_section_list->FindSectionByType(section_type, true))
134 module_section_list->ReplaceSection(module_section_sp->GetID(),
135 section_sp);
136 else
137 module_section_list->AddSection(section_sp);
138 }
139 }
140
141 symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
142 return symbol_vendor;
143}
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
#define LLDB_SCOPED_TIMERF(...)
Definition: Timer.h:86
Generic COFF object file reader.
Definition: ObjectFileELF.h:58
std::optional< lldb_private::FileSpec > GetDebugLink()
Return the contents of the .gnu_debuglink section, if the object file contains it.
lldb_private::UUID GetUUID() override
Gets the UUID for this object file.
static llvm::StringRef GetPluginDescriptionStatic()
static void Initialize()
static void Terminate()
static lldb_private::SymbolVendor * CreateInstance(const lldb::ModuleSP &module_sp, lldb_private::Stream *feedback_strm)
static llvm::StringRef GetPluginNameStatic()
A file collection class.
Definition: FileSpecList.h:85
A file utility class.
Definition: FileSpec.h:56
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
static FileSystem & Instance()
FileSpec & GetFileSpec()
Definition: ModuleSpec.h:53
FileSpec & GetSymbolFileSpec()
Definition: ModuleSpec.h:77
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataBufferSP &data_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
Definition: ObjectFile.cpp:53
@ eTypeDebugInfo
An object file that contains only debug information.
Definition: ObjectFile.h:55
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition: ObjectFile.h:274
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:590
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static FileSpec LocateExecutableSymbolFile(const ModuleSpec &module_spec, const FileSpecList &default_search_paths)
static bool UnregisterPlugin(ABICreateInstance create_callback)
size_t AddSection(const lldb::SectionSP &section_sp)
Definition: Section.cpp:475
bool ReplaceSection(lldb::user_id_t sect_id, const lldb::SectionSP &section_sp, uint32_t depth=UINT32_MAX)
Definition: Section.cpp:515
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition: Section.cpp:592
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void AddSymbolFileRepresentation(const lldb::ObjectFileSP &objfile_sp)
static FileSpecList GetDefaultDebugFileSearchPaths()
Definition: Target.cpp:2605
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:83
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
Definition: lldb-forward.h:367
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:328
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:406
@ eSectionTypeDWARFDebugStrOffsets
@ eSectionTypeDWARFDebugPubNames
@ eSectionTypeDWARFDebugFrame
@ eSectionTypeDWARFDebugLocLists
DWARF v5 .debug_loclists.
@ eSectionTypeDWARFDebugTypes
DWARF .debug_types section.
@ eSectionTypeDWARFDebugMacInfo
@ eSectionTypeDWARFDebugNames
DWARF v5 .debug_names.
@ eSectionTypeDWARFDebugRngLists
DWARF v5 .debug_rnglists.
@ eSectionTypeDWARFDebugMacro
@ eSectionTypeDWARFDebugInfo
@ eSectionTypeDWARFDebugRanges
@ eSectionTypeDWARFDebugLine
@ eSectionTypeDWARFDebugPubTypes
@ eSectionTypeDWARFDebugStr
@ eSectionTypeDWARFDebugLineStr
DWARF v5 .debug_line_str.
@ eSectionTypeDWARFDebugLoc
@ eSectionTypeDWARFDebugCuIndex
@ eSectionTypeDWARFDebugAranges
@ eSectionTypeDWARFGNUDebugAltLink
@ eSectionTypeDWARFDebugAbbrev
@ eSectionTypeDWARFDebugAddr
@ eSectionTypeELFSymbolTable
Elf SHT_SYMTAB section.
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:365