1 //===-- SymbolVendorPECOFF.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SymbolVendorPECOFF.h"
13 #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.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/ObjectFile.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/StreamString.h"
22 #include "lldb/Utility/Timer.h"
25 using namespace lldb_private
;
27 LLDB_PLUGIN_DEFINE(SymbolVendorPECOFF
)
29 // SymbolVendorPECOFF constructor
30 SymbolVendorPECOFF::SymbolVendorPECOFF(const lldb::ModuleSP
&module_sp
)
31 : SymbolVendor(module_sp
) {}
33 void SymbolVendorPECOFF::Initialize() {
34 PluginManager::RegisterPlugin(GetPluginNameStatic(),
35 GetPluginDescriptionStatic(), CreateInstance
);
38 void SymbolVendorPECOFF::Terminate() {
39 PluginManager::UnregisterPlugin(CreateInstance
);
42 llvm::StringRef
SymbolVendorPECOFF::GetPluginDescriptionStatic() {
43 return "Symbol vendor for PE/COFF that looks for dSYM files that match "
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.
53 SymbolVendorPECOFF::CreateInstance(const lldb::ModuleSP
&module_sp
,
54 lldb_private::Stream
*feedback_strm
) {
58 ObjectFilePECOFF
*obj_file
=
59 llvm::dyn_cast_or_null
<ObjectFilePECOFF
>(module_sp
->GetObjectFile());
63 lldb_private::UUID uuid
= obj_file
->GetUUID();
67 // If the main object file already contains debug info, then we are done.
68 if (obj_file
->GetSectionList()->FindSectionByType(
69 lldb::eSectionTypeDWARFDebugInfo
, true))
72 // If the module specified a filespec, use that.
73 FileSpec fspec
= module_sp
->GetSymbolFileFileSpec();
74 // Otherwise, try gnu_debuglink, if one exists.
76 fspec
= obj_file
->GetDebugLink().value_or(FileSpec());
78 LLDB_SCOPED_TIMERF("SymbolVendorPECOFF::CreateInstance (module = %s)",
79 module_sp
->GetFileSpec().GetPath().c_str());
81 ModuleSpec module_spec
;
83 module_spec
.GetFileSpec() = obj_file
->GetFileSpec();
84 FileSystem::Instance().Resolve(module_spec
.GetFileSpec());
85 module_spec
.GetSymbolFileSpec() = fspec
;
86 module_spec
.GetUUID() = uuid
;
87 FileSpecList search_paths
= Target::GetDefaultDebugFileSearchPaths();
89 PluginManager::LocateExecutableSymbolFile(module_spec
, search_paths
);
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
);
101 // This objfile is for debugging purposes.
102 dsym_objfile_sp
->SetType(ObjectFile::eTypeDebugInfo
);
104 // Get the module unified section list and add our debug sections to
106 SectionList
*module_section_list
= module_sp
->GetSectionList();
107 SectionList
*objfile_section_list
= dsym_objfile_sp
->GetSectionList();
108 if (!objfile_section_list
|| !module_section_list
)
111 static const SectionType g_sections
[] = {
112 eSectionTypeDWARFDebugAbbrev
, eSectionTypeDWARFDebugAranges
,
113 eSectionTypeDWARFDebugFrame
, eSectionTypeDWARFDebugInfo
,
114 eSectionTypeDWARFDebugLine
, eSectionTypeDWARFDebugLoc
,
115 eSectionTypeDWARFDebugLocLists
, eSectionTypeDWARFDebugMacInfo
,
116 eSectionTypeDWARFDebugNames
, eSectionTypeDWARFDebugPubNames
,
117 eSectionTypeDWARFDebugPubTypes
, eSectionTypeDWARFDebugRanges
,
118 eSectionTypeDWARFDebugStr
, eSectionTypeDWARFDebugTypes
,
120 for (SectionType section_type
: g_sections
) {
121 if (SectionSP section_sp
=
122 objfile_section_list
->FindSectionByType(section_type
, true)) {
123 if (SectionSP module_section_sp
=
124 module_section_list
->FindSectionByType(section_type
, true))
125 module_section_list
->ReplaceSection(module_section_sp
->GetID(),
128 module_section_list
->AddSection(section_sp
);
132 SymbolVendorPECOFF
*symbol_vendor
= new SymbolVendorPECOFF(module_sp
);
133 symbol_vendor
->AddSymbolFileRepresentation(dsym_objfile_sp
);
134 return symbol_vendor
;