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