1 //===-- SymbolVendorELF.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SymbolVendorELF.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/Host/Symbols.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Utility/StreamString.h"
22 #include "lldb/Utility/Timer.h"
25 using namespace lldb_private;
27 //----------------------------------------------------------------------
28 // SymbolVendorELF constructor
29 //----------------------------------------------------------------------
30 SymbolVendorELF::SymbolVendorELF(const lldb::ModuleSP &module_sp)
31 : SymbolVendor(module_sp) {}
33 //----------------------------------------------------------------------
35 //----------------------------------------------------------------------
36 SymbolVendorELF::~SymbolVendorELF() {}
38 void SymbolVendorELF::Initialize() {
39 PluginManager::RegisterPlugin(GetPluginNameStatic(),
40 GetPluginDescriptionStatic(), CreateInstance);
43 void SymbolVendorELF::Terminate() {
44 PluginManager::UnregisterPlugin(CreateInstance);
47 lldb_private::ConstString SymbolVendorELF::GetPluginNameStatic() {
48 static ConstString g_name("ELF");
52 const char *SymbolVendorELF::GetPluginDescriptionStatic() {
53 return "Symbol vendor for ELF that looks for dSYM files that match "
57 //----------------------------------------------------------------------
60 // Platforms can register a callback to use when creating symbol vendors to
61 // allow for complex debug information file setups, and to also allow for
62 // finding separate debug information files.
63 //----------------------------------------------------------------------
65 SymbolVendorELF::CreateInstance(const lldb::ModuleSP &module_sp,
66 lldb_private::Stream *feedback_strm) {
70 ObjectFile *obj_file = module_sp->GetObjectFile();
74 static ConstString obj_file_elf("elf");
75 ConstString obj_name = obj_file->GetPluginName();
76 if (obj_name != obj_file_elf)
79 lldb_private::UUID uuid;
80 if (!obj_file->GetUUID(&uuid))
83 // Get the .gnu_debuglink file (if specified).
84 FileSpecList file_spec_list = obj_file->GetDebugSymbolFilePaths();
86 // If the module specified a filespec, use it first.
87 FileSpec debug_symbol_fspec(module_sp->GetSymbolFileFileSpec());
88 if (debug_symbol_fspec)
89 file_spec_list.Insert(0, debug_symbol_fspec);
91 // If we have no debug symbol files, then nothing to do.
92 if (file_spec_list.IsEmpty())
95 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
96 Timer scoped_timer(func_cat, "SymbolVendorELF::CreateInstance (module = %s)",
97 module_sp->GetFileSpec().GetPath().c_str());
99 for (size_t idx = 0; idx < file_spec_list.GetSize(); ++idx) {
100 ModuleSpec module_spec;
101 const FileSpec fspec = file_spec_list.GetFileSpecAtIndex(idx);
103 module_spec.GetFileSpec() = obj_file->GetFileSpec();
104 module_spec.GetFileSpec().ResolvePath();
105 module_spec.GetSymbolFileSpec() = fspec;
106 module_spec.GetUUID() = uuid;
107 FileSpec dsym_fspec = Symbols::LocateExecutableSymbolFile(module_spec);
109 DataBufferSP dsym_file_data_sp;
110 lldb::offset_t dsym_file_data_offset = 0;
111 ObjectFileSP dsym_objfile_sp = ObjectFile::FindPlugin(
112 module_sp, &dsym_fspec, 0, dsym_fspec.GetByteSize(),
113 dsym_file_data_sp, dsym_file_data_offset);
114 if (dsym_objfile_sp) {
115 // This objfile is for debugging purposes. Sadly, ObjectFileELF won't
116 // be able to figure this out consistently as the symbol file may not
117 // have stripped the code sections, etc.
118 dsym_objfile_sp->SetType(ObjectFile::eTypeDebugInfo);
120 SymbolVendorELF *symbol_vendor = new SymbolVendorELF(module_sp);
122 // Get the module unified section list and add our debug sections to
124 SectionList *module_section_list = module_sp->GetSectionList();
125 SectionList *objfile_section_list = dsym_objfile_sp->GetSectionList();
127 static const SectionType g_sections[] = {
128 eSectionTypeDWARFDebugAbbrev, eSectionTypeDWARFDebugAddr,
129 eSectionTypeDWARFDebugAranges, eSectionTypeDWARFDebugCuIndex,
130 eSectionTypeDWARFDebugFrame, eSectionTypeDWARFDebugInfo,
131 eSectionTypeDWARFDebugLine, eSectionTypeDWARFDebugLoc,
132 eSectionTypeDWARFDebugMacInfo, eSectionTypeDWARFDebugPubNames,
133 eSectionTypeDWARFDebugPubTypes, eSectionTypeDWARFDebugRanges,
134 eSectionTypeDWARFDebugStr, eSectionTypeDWARFDebugStrOffsets,
135 eSectionTypeELFSymbolTable, eSectionTypeDWARFGNUDebugAltLink,
137 for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]);
139 SectionType section_type = g_sections[idx];
140 SectionSP section_sp(
141 objfile_section_list->FindSectionByType(section_type, true));
143 SectionSP module_section_sp(
144 module_section_list->FindSectionByType(section_type, true));
145 if (module_section_sp)
146 module_section_list->ReplaceSection(module_section_sp->GetID(),
149 module_section_list->AddSection(section_sp);
153 symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
154 return symbol_vendor;
162 //------------------------------------------------------------------
163 // PluginInterface protocol
164 //------------------------------------------------------------------
165 ConstString SymbolVendorELF::GetPluginName() { return GetPluginNameStatic(); }
167 uint32_t SymbolVendorELF::GetPluginVersion() { return 1; }