]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/SymbolVendor/ELF/SymbolVendorELF.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / SymbolVendor / ELF / SymbolVendorELF.cpp
1 //===-- SymbolVendorELF.cpp ----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "SymbolVendorELF.h"
11
12 #include <string.h>
13
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"
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 //----------------------------------------------------------------------
28 // SymbolVendorELF constructor
29 //----------------------------------------------------------------------
30 SymbolVendorELF::SymbolVendorELF(const lldb::ModuleSP &module_sp)
31     : SymbolVendor(module_sp) {}
32
33 //----------------------------------------------------------------------
34 // Destructor
35 //----------------------------------------------------------------------
36 SymbolVendorELF::~SymbolVendorELF() {}
37
38 void SymbolVendorELF::Initialize() {
39   PluginManager::RegisterPlugin(GetPluginNameStatic(),
40                                 GetPluginDescriptionStatic(), CreateInstance);
41 }
42
43 void SymbolVendorELF::Terminate() {
44   PluginManager::UnregisterPlugin(CreateInstance);
45 }
46
47 lldb_private::ConstString SymbolVendorELF::GetPluginNameStatic() {
48   static ConstString g_name("ELF");
49   return g_name;
50 }
51
52 const char *SymbolVendorELF::GetPluginDescriptionStatic() {
53   return "Symbol vendor for ELF that looks for dSYM files that match "
54          "executables.";
55 }
56
57 //----------------------------------------------------------------------
58 // CreateInstance
59 //
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 //----------------------------------------------------------------------
64 SymbolVendor *
65 SymbolVendorELF::CreateInstance(const lldb::ModuleSP &module_sp,
66                                 lldb_private::Stream *feedback_strm) {
67   if (!module_sp)
68     return NULL;
69
70   ObjectFile *obj_file = module_sp->GetObjectFile();
71   if (!obj_file)
72     return NULL;
73
74   static ConstString obj_file_elf("elf");
75   ConstString obj_name = obj_file->GetPluginName();
76   if (obj_name != obj_file_elf)
77     return NULL;
78
79   lldb_private::UUID uuid;
80   if (!obj_file->GetUUID(&uuid))
81     return NULL;
82
83   // Get the .gnu_debuglink file (if specified).
84   FileSpecList file_spec_list = obj_file->GetDebugSymbolFilePaths();
85
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);
90
91   // If we have no debug symbol files, then nothing to do.
92   if (file_spec_list.IsEmpty())
93     return NULL;
94
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());
98
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);
102
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);
108     if (dsym_fspec) {
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);
119
120         SymbolVendorELF *symbol_vendor = new SymbolVendorELF(module_sp);
121         if (symbol_vendor) {
122           // Get the module unified section list and add our debug sections to
123           // that.
124           SectionList *module_section_list = module_sp->GetSectionList();
125           SectionList *objfile_section_list = dsym_objfile_sp->GetSectionList();
126
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,
136           };
137           for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]);
138                ++idx) {
139             SectionType section_type = g_sections[idx];
140             SectionSP section_sp(
141                 objfile_section_list->FindSectionByType(section_type, true));
142             if (section_sp) {
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(),
147                                                     section_sp);
148               else
149                 module_section_list->AddSection(section_sp);
150             }
151           }
152
153           symbol_vendor->AddSymbolFileRepresentation(dsym_objfile_sp);
154           return symbol_vendor;
155         }
156       }
157     }
158   }
159   return NULL;
160 }
161
162 //------------------------------------------------------------------
163 // PluginInterface protocol
164 //------------------------------------------------------------------
165 ConstString SymbolVendorELF::GetPluginName() { return GetPluginNameStatic(); }
166
167 uint32_t SymbolVendorELF::GetPluginVersion() { return 1; }