blob: 8bb41ad09af965a5c7fe5898bbaecb469de64624 [file] [log] [blame]
Eugene Zelenko0af149a2015-10-19 18:52:10 +00001//===-- SymbolFileDWARFDebugMap.cpp -----------------------------*- C++ -*-===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolFileDWARFDebugMap.h"
Enrico Granatac76e60b2013-06-27 01:43:09 +000010#include "DWARFDebugAranges.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/ModuleList.h"
14#include "lldb/Core/PluginManager.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include "lldb/Core/RangeMap.h"
Greg Clayton1f746072012-08-29 21:13:06 +000016#include "lldb/Core/Section.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000017#include "lldb/Host/FileSystem.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/RegularExpression.h"
Pavel Labath38d06322017-06-29 14:32:17 +000019#include "lldb/Utility/Timer.h"
Greg Clayton9422dd62013-03-04 21:46:16 +000020
21//#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
Greg Clayton1f746072012-08-29 21:13:06 +000022#if defined(DEBUG_OSO_DMAP)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/StreamFile.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#endif
Greg Clayton6beaaa62011-01-17 03:46:26 +000025
Greg Clayton1f746072012-08-29 21:13:06 +000026#include "lldb/Symbol/CompileUnit.h"
Greg Clayton9422dd62013-03-04 21:46:16 +000027#include "lldb/Symbol/LineTable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Symbol/SymbolVendor.h"
Ravitheja Addepally40697302015-10-08 09:45:41 +000030#include "lldb/Symbol/TypeMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Symbol/VariableList.h"
Pavel Labath7e2cfbf2016-11-09 09:59:18 +000032#include "llvm/Support/ScopedPrinter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Sean Callanan60217122012-04-13 00:10:03 +000034#include "LogChannelDWARF.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "SymbolFileDWARF.h"
36
Jonas Devlieghere796ac802019-02-11 23:13:08 +000037#include <memory>
38
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
41
Kate Stoneb9c1b512016-09-06 20:57:50 +000042// Subclass lldb_private::Module so we can intercept the
Adrian Prantl05097242018-04-30 16:49:04 +000043// "Module::GetObjectFile()" (so we can fixup the object file sections) and
44// also for "Module::GetSymbolVendor()" (so we can fixup the symbol file id.
Greg Clayton1f746072012-08-29 21:13:06 +000045
Greg Clayton9422dd62013-03-04 21:46:16 +000046const SymbolFileDWARFDebugMap::FileRangeMap &
Kate Stoneb9c1b512016-09-06 20:57:50 +000047SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(
48 SymbolFileDWARFDebugMap *exe_symfile) {
49 if (file_range_map_valid)
Greg Clayton9422dd62013-03-04 21:46:16 +000050 return file_range_map;
Kate Stoneb9c1b512016-09-06 20:57:50 +000051
52 file_range_map_valid = true;
53
54 Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this);
55 if (!oso_module)
56 return file_range_map;
57
58 ObjectFile *oso_objfile = oso_module->GetObjectFile();
59 if (!oso_objfile)
60 return file_range_map;
61
62 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
63 if (log) {
64 ConstString object_name(oso_module->GetObjectName());
65 log->Printf(
66 "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
67 static_cast<void *>(this),
68 oso_module->GetSpecificationDescription().c_str());
69 }
70
71 std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos;
72 if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) {
73 for (auto comp_unit_info : cu_infos) {
74 Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab();
75 ModuleSP oso_module_sp(oso_objfile->GetModule());
76 Symtab *oso_symtab = oso_objfile->GetSymtab();
77
78 /// const uint32_t fun_resolve_flags = SymbolContext::Module |
79 /// eSymbolContextCompUnit | eSymbolContextFunction;
80 // SectionList *oso_sections = oso_objfile->Sections();
Adrian Prantl05097242018-04-30 16:49:04 +000081 // Now we need to make sections that map from zero based object file
82 // addresses to where things ended up in the main executable.
Kate Stoneb9c1b512016-09-06 20:57:50 +000083
84 assert(comp_unit_info->first_symbol_index != UINT32_MAX);
85 // End index is one past the last valid symbol index
86 const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1;
87 for (uint32_t idx = comp_unit_info->first_symbol_index +
88 2; // Skip the N_SO and N_OSO
Jonas Devlieghere010b56b2018-11-27 15:25:58 +000089 idx < oso_end_idx; ++idx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
91 if (exe_symbol) {
Jonas Devliegherea6682a42018-12-15 00:15:33 +000092 if (!exe_symbol->IsDebug())
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 continue;
94
95 switch (exe_symbol->GetType()) {
96 default:
97 break;
98
99 case eSymbolTypeCode: {
Adrian Prantl05097242018-04-30 16:49:04 +0000100 // For each N_FUN, or function that we run into in the debug map we
101 // make a new section that we add to the sections found in the .o
102 // file. This new section has the file address set to what the
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 // addresses are in the .o file, and the load address is adjusted
104 // to match where it ended up in the final executable! We do this
105 // before we parse any dwarf info so that when it goes get parsed
106 // all section/offset addresses that get registered will resolve
107 // correctly to the new addresses in the main executable.
108
109 // First we find the original symbol in the .o file's symbol table
110 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(
111 exe_symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown,
112 Mangled::ePreferMangled),
113 eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
114 if (oso_fun_symbol) {
115 // Add the inverse OSO file address to debug map entry mapping
116 exe_symfile->AddOSOFileRange(
117 this, exe_symbol->GetAddressRef().GetFileAddress(),
118 exe_symbol->GetByteSize(),
119 oso_fun_symbol->GetAddressRef().GetFileAddress(),
120 oso_fun_symbol->GetByteSize());
121 }
122 } break;
123
124 case eSymbolTypeData: {
Adrian Prantl05097242018-04-30 16:49:04 +0000125 // For each N_GSYM we remap the address for the global by making a
126 // new section that we add to the sections found in the .o file.
127 // This new section has the file address set to what the addresses
128 // are in the .o file, and the load address is adjusted to match
129 // where it ended up in the final executable! We do this before we
130 // parse any dwarf info so that when it goes get parsed all
131 // section/offset addresses that get registered will resolve
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 // correctly to the new addresses in the main executable. We
133 // initially set the section size to be 1 byte, but will need to
134 // fix up these addresses further after all globals have been
135 // parsed to span the gaps, or we can find the global variable
136 // sizes from the DWARF info as we are parsing.
137
Adrian Prantl05097242018-04-30 16:49:04 +0000138 // Next we find the non-stab entry that corresponds to the N_GSYM
139 // in the .o file
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 Symbol *oso_gsym_symbol =
141 oso_symtab->FindFirstSymbolWithNameAndType(
142 exe_symbol->GetMangled().GetName(lldb::eLanguageTypeUnknown,
143 Mangled::ePreferMangled),
144 eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
145 if (exe_symbol && oso_gsym_symbol && exe_symbol->ValueIsAddress() &&
146 oso_gsym_symbol->ValueIsAddress()) {
147 // Add the inverse OSO file address to debug map entry mapping
148 exe_symfile->AddOSOFileRange(
149 this, exe_symbol->GetAddressRef().GetFileAddress(),
150 exe_symbol->GetByteSize(),
151 oso_gsym_symbol->GetAddressRef().GetFileAddress(),
152 oso_gsym_symbol->GetByteSize());
153 }
154 } break;
155 }
156 }
157 }
158
159 exe_symfile->FinalizeOSOFileRanges(this);
160 // We don't need the symbols anymore for the .o files
161 oso_objfile->ClearSymtab();
162 }
163 }
164 return file_range_map;
Greg Clayton9422dd62013-03-04 21:46:16 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167class DebugMapModule : public Module {
Greg Clayton1f746072012-08-29 21:13:06 +0000168public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 DebugMapModule(const ModuleSP &exe_module_sp, uint32_t cu_idx,
170 const FileSpec &file_spec, const ArchSpec &arch,
171 const ConstString *object_name, off_t object_offset,
Pavel Labath7e2cfbf2016-11-09 09:59:18 +0000172 const llvm::sys::TimePoint<> object_mod_time)
173 : Module(file_spec, arch, object_name, object_offset, object_mod_time),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {}
Greg Clayton1f746072012-08-29 21:13:06 +0000175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 ~DebugMapModule() override = default;
Greg Clayton9ba42d12012-10-08 22:48:57 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 SymbolVendor *
179 GetSymbolVendor(bool can_create = true,
180 lldb_private::Stream *feedback_strm = NULL) override {
181 // Scope for locker
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000182 if (m_symfile_up.get() || !can_create)
183 return m_symfile_up.get();
Greg Clayton1f746072012-08-29 21:13:06 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 ModuleSP exe_module_sp(m_exe_module_wp.lock());
186 if (exe_module_sp) {
187 // Now get the object file outside of a locking scope
188 ObjectFile *oso_objfile = GetObjectFile();
189 if (oso_objfile) {
190 std::lock_guard<std::recursive_mutex> guard(m_mutex);
191 SymbolVendor *symbol_vendor =
192 Module::GetSymbolVendor(can_create, feedback_strm);
193 if (symbol_vendor) {
Adrian Prantl05097242018-04-30 16:49:04 +0000194 // Set a pointer to this class to set our OSO DWARF file know that
195 // the DWARF is being used along with a debug map and that it will
196 // have the remapped sections that we do below.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 SymbolFileDWARF *oso_symfile =
198 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(
199 symbol_vendor->GetSymbolFile());
Greg Clayton1f746072012-08-29 21:13:06 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (!oso_symfile)
202 return NULL;
203
204 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
205 SymbolVendor *exe_sym_vendor = exe_module_sp->GetSymbolVendor();
206
207 if (exe_objfile && exe_sym_vendor) {
208 oso_symfile->SetDebugMapModule(exe_module_sp);
209 // Set the ID of the symbol file DWARF to the index of the OSO
210 // shifted left by 32 bits to provide a unique prefix for any
211 // UserID's that get created in the symbol file.
212 oso_symfile->SetID(((uint64_t)m_cu_idx + 1ull) << 32ull);
213 }
214 return symbol_vendor;
Greg Clayton1f746072012-08-29 21:13:06 +0000215 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 }
Greg Clayton1f746072012-08-29 21:13:06 +0000217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 return NULL;
219 }
Greg Clayton1f746072012-08-29 21:13:06 +0000220
221protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 ModuleWP m_exe_module_wp;
223 const uint32_t m_cu_idx;
Greg Clayton1f746072012-08-29 21:13:06 +0000224};
225
Kate Stoneb9c1b512016-09-06 20:57:50 +0000226void SymbolFileDWARFDebugMap::Initialize() {
227 PluginManager::RegisterPlugin(GetPluginNameStatic(),
228 GetPluginDescriptionStatic(), CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229}
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231void SymbolFileDWARFDebugMap::Terminate() {
232 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginNameStatic() {
236 static ConstString g_name("dwarf-debugmap");
237 return g_name;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240const char *SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
241 return "DWARF and DWARF3 debug symbol file reader (debug map).";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFile *obj_file) {
245 return new SymbolFileDWARFDebugMap(obj_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFile *ofile)
249 : SymbolFile(ofile), m_flags(), m_compile_unit_infos(), m_func_indexes(),
250 m_glob_indexes(),
251 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255void SymbolFileDWARFDebugMap::InitializeObject() {}
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000256
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257void SymbolFileDWARFDebugMap::InitOSO() {
258 if (m_flags.test(kHaveInitializedOSOs))
259 return;
Greg Clayton3046e662013-07-10 01:23:25 +0000260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 m_flags.set(kHaveInitializedOSOs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 // If the object file has been stripped, there is no sense in looking further
264 // as all of the debug symbols for the debug map will not be available
265 if (m_obj_file->IsStripped())
266 return;
Sean Callanan60217122012-04-13 00:10:03 +0000267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 // Also make sure the file type is some sort of executable. Core files, debug
269 // info files (dSYM), object files (.o files), and stub libraries all can
270 switch (m_obj_file->GetType()) {
271 case ObjectFile::eTypeInvalid:
272 case ObjectFile::eTypeCoreFile:
273 case ObjectFile::eTypeDebugInfo:
274 case ObjectFile::eTypeObjectFile:
275 case ObjectFile::eTypeStubLibrary:
276 case ObjectFile::eTypeUnknown:
277 case ObjectFile::eTypeJIT:
278 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 case ObjectFile::eTypeExecutable:
281 case ObjectFile::eTypeDynamicLinker:
282 case ObjectFile::eTypeSharedLibrary:
283 break;
284 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286 // In order to get the abilities of this plug-in, we look at the list of
287 // N_OSO entries (object files) from the symbol table and make sure that
Adrian Prantl05097242018-04-30 16:49:04 +0000288 // these files exist and also contain valid DWARF. If we get any of that then
289 // we return the abilities of the first N_OSO's DWARF.
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 Symtab *symtab = m_obj_file->GetSymtab();
292 if (symtab) {
293 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 std::vector<uint32_t> oso_indexes;
296 // When a mach-o symbol is encoded, the n_type field is encoded in bits
297 // 23:16, and the n_desc field is encoded in bits 15:0.
298 //
Adrian Prantl05097242018-04-30 16:49:04 +0000299 // To find all N_OSO entries that are part of the DWARF + debug map we find
300 // only object file symbols with the flags value as follows: bits 23:16 ==
301 // 0x66 (N_OSO) bits 15: 0 == 0x0001 (specifies this is a debug map object
302 // file)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 const uint32_t k_oso_symbol_flags_value = 0x660001u;
Greg Clayton9422dd62013-03-04 21:46:16 +0000304
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 const uint32_t oso_index_count =
306 symtab->AppendSymbolIndexesWithTypeAndFlagsValue(
307 eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 if (oso_index_count > 0) {
310 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes,
311 Symtab::eVisibilityAny,
312 m_func_indexes);
313 symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes,
314 Symtab::eVisibilityAny,
315 m_glob_indexes);
316
317 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
318 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
319
320 for (uint32_t sym_idx : m_func_indexes) {
321 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
322 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
323 lldb::addr_t byte_size = symbol->GetByteSize();
324 DebugMap::Entry debug_map_entry(
325 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
326 m_debug_map.Append(debug_map_entry);
327 }
328 for (uint32_t sym_idx : m_glob_indexes) {
329 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
330 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
331 lldb::addr_t byte_size = symbol->GetByteSize();
332 DebugMap::Entry debug_map_entry(
333 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
334 m_debug_map.Append(debug_map_entry);
335 }
336 m_debug_map.Sort();
337
338 m_compile_unit_infos.resize(oso_index_count);
339
340 for (uint32_t i = 0; i < oso_index_count; ++i) {
341 const uint32_t so_idx = oso_indexes[i] - 1;
342 const uint32_t oso_idx = oso_indexes[i];
343 const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx);
344 const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx);
345 if (so_symbol && oso_symbol &&
346 so_symbol->GetType() == eSymbolTypeSourceFile &&
347 oso_symbol->GetType() == eSymbolTypeObjectFile) {
348 m_compile_unit_infos[i].so_file.SetFile(
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000349 so_symbol->GetName().AsCString(), FileSpec::Style::native);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000350 m_compile_unit_infos[i].oso_path = oso_symbol->GetName();
Pavel Labath7e2cfbf2016-11-09 09:59:18 +0000351 m_compile_unit_infos[i].oso_mod_time =
352 llvm::sys::toTimePoint(oso_symbol->GetIntegerValue(0));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 uint32_t sibling_idx = so_symbol->GetSiblingIndex();
354 // The sibling index can't be less that or equal to the current index
355 // "i"
356 if (sibling_idx == UINT32_MAX) {
357 m_obj_file->GetModule()->ReportError(
358 "N_SO in symbol with UID %u has invalid sibling in debug map, "
359 "please file a bug and attach the binary listed in this error",
360 so_symbol->GetID());
361 } else {
362 const Symbol *last_symbol = symtab->SymbolAtIndex(sibling_idx - 1);
363 m_compile_unit_infos[i].first_symbol_index = so_idx;
364 m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1;
365 m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID();
366 m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID();
367
368 if (log)
369 log->Printf("Initialized OSO 0x%8.8x: file=%s", i,
370 oso_symbol->GetName().GetCString());
371 }
372 } else {
373 if (oso_symbol == NULL)
374 m_obj_file->GetModule()->ReportError(
375 "N_OSO symbol[%u] can't be found, please file a bug and attach "
376 "the binary listed in this error",
377 oso_idx);
378 else if (so_symbol == NULL)
379 m_obj_file->GetModule()->ReportError(
380 "N_SO not found for N_OSO symbol[%u], please file a bug and "
381 "attach the binary listed in this error",
382 oso_idx);
383 else if (so_symbol->GetType() != eSymbolTypeSourceFile)
384 m_obj_file->GetModule()->ReportError(
385 "N_SO has incorrect symbol type (%u) for N_OSO symbol[%u], "
386 "please file a bug and attach the binary listed in this error",
387 so_symbol->GetType(), oso_idx);
388 else if (oso_symbol->GetType() != eSymbolTypeSourceFile)
389 m_obj_file->GetModule()->ReportError(
390 "N_OSO has incorrect symbol type (%u) for N_OSO symbol[%u], "
391 "please file a bug and attach the binary listed in this error",
392 oso_symbol->GetType(), oso_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397}
398
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) {
400 const uint32_t cu_count = GetNumCompileUnits();
401 if (oso_idx < cu_count)
402 return GetModuleByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
403 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404}
405
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo(
407 CompileUnitInfo *comp_unit_info) {
408 if (!comp_unit_info->oso_sp) {
James Y Knight9c2d5202018-06-02 02:44:10 +0000409 auto pos = m_oso_map.find(
410 {comp_unit_info->oso_path, comp_unit_info->oso_mod_time});
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411 if (pos != m_oso_map.end()) {
412 comp_unit_info->oso_sp = pos->second;
413 } else {
414 ObjectFile *obj_file = GetObjectFile();
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000415 comp_unit_info->oso_sp = std::make_shared<OSOInfo>();
James Y Knight9c2d5202018-06-02 02:44:10 +0000416 m_oso_map[{comp_unit_info->oso_path, comp_unit_info->oso_mod_time}] =
417 comp_unit_info->oso_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418 const char *oso_path = comp_unit_info->oso_path.GetCString();
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000419 FileSpec oso_file(oso_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 ConstString oso_object;
Jonas Devliegheredbd7fab2018-11-01 17:09:25 +0000421 if (FileSystem::Instance().Exists(oso_file)) {
Jonas Devlieghere010b56b2018-11-27 15:25:58 +0000422 // The modification time returned by the FS can have a higher precision
423 // than the one from the CU.
424 auto oso_mod_time = std::chrono::time_point_cast<std::chrono::seconds>(
425 FileSystem::Instance().GetModificationTime(oso_file));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 if (oso_mod_time != comp_unit_info->oso_mod_time) {
427 obj_file->GetModule()->ReportError(
428 "debug map object file '%s' has changed (actual time is "
Pavel Labath7e2cfbf2016-11-09 09:59:18 +0000429 "%s, debug map time is %s"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 ") since this executable was linked, file will be ignored",
Pavel Labath7e2cfbf2016-11-09 09:59:18 +0000431 oso_file.GetPath().c_str(), llvm::to_string(oso_mod_time).c_str(),
432 llvm::to_string(comp_unit_info->oso_mod_time).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 }
Greg Clayton57abc5d2013-05-10 21:47:16 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 } else {
437 const bool must_exist = true;
Greg Clayton906ba472013-02-06 00:38:25 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file,
440 oso_object, must_exist)) {
441 return NULL;
Greg Clayton906ba472013-02-06 00:38:25 +0000442 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443 }
Adrian Prantl05097242018-04-30 16:49:04 +0000444 // Always create a new module for .o files. Why? Because we use the debug
445 // map, to add new sections to each .o file and even though a .o file
446 // might not have changed, the sections that get added to the .o file can
447 // change.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 ArchSpec oso_arch;
449 // Only adopt the architecture from the module (not the vendor or OS)
Adrian Prantl05097242018-04-30 16:49:04 +0000450 // since .o files for "i386-apple-ios" will historically show up as "i386
451 // -apple-macosx" due to the lack of a LC_VERSION_MIN_MACOSX or
452 // LC_VERSION_MIN_IPHONEOS load command...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 oso_arch.SetTriple(m_obj_file->GetModule()
454 ->GetArchitecture()
455 .GetTriple()
456 .getArchName()
457 .str()
458 .c_str());
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000459 comp_unit_info->oso_sp->module_sp = std::make_shared<DebugMapModule>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file,
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000461 oso_arch, oso_object ? &oso_object : nullptr, 0,
462 oso_object ? comp_unit_info->oso_mod_time : llvm::sys::TimePoint<>());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 }
465 if (comp_unit_info->oso_sp)
466 return comp_unit_info->oso_sp->module_sp.get();
467 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx,
471 FileSpec &file_spec) {
472 if (oso_idx < m_compile_unit_infos.size()) {
473 if (m_compile_unit_infos[oso_idx].so_file) {
474 file_spec = m_compile_unit_infos[oso_idx].so_file;
475 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477 }
478 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) {
482 Module *oso_module = GetModuleByOSOIndex(oso_idx);
483 if (oso_module)
484 return oso_module->GetObjectFile();
485 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486}
487
488SymbolFileDWARF *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000490 return GetSymbolFile(*sc.comp_unit);
491}
492
493SymbolFileDWARF *
494SymbolFileDWARFDebugMap::GetSymbolFile(const CompileUnit &comp_unit) {
495 CompileUnitInfo *comp_unit_info = GetCompUnitInfo(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496 if (comp_unit_info)
497 return GetSymbolFileByCompUnitInfo(comp_unit_info);
498 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499}
500
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo(
502 CompileUnitInfo *comp_unit_info) {
503 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
504 if (oso_module)
505 return oso_module->GetObjectFile();
506 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507}
508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex(
510 const CompileUnitInfo *comp_unit_info) {
511 if (!m_compile_unit_infos.empty()) {
512 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
513 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
514 if (first_comp_unit_info <= comp_unit_info &&
515 comp_unit_info <= last_comp_unit_info)
516 return comp_unit_info - first_comp_unit_info;
517 }
518 return UINT32_MAX;
519}
520
521SymbolFileDWARF *
522SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) {
Adrian Prantleca07c52018-11-05 20:49:07 +0000523 unsigned size = m_compile_unit_infos.size();
524 if (oso_idx < size)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
526 return NULL;
527}
528
529SymbolFileDWARF *
530SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) {
531 if (sym_file &&
532 sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
533 return (SymbolFileDWARF *)sym_file;
534 return NULL;
535}
536
537SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo(
538 CompileUnitInfo *comp_unit_info) {
539 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
540 if (oso_module) {
541 SymbolVendor *sym_vendor = oso_module->GetSymbolVendor();
542 if (sym_vendor)
543 return GetSymbolFileAsSymbolFileDWARF(sym_vendor->GetSymbolFile());
544 }
545 return NULL;
546}
547
548uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() {
549 // In order to get the abilities of this plug-in, we look at the list of
550 // N_OSO entries (object files) from the symbol table and make sure that
Adrian Prantl05097242018-04-30 16:49:04 +0000551 // these files exist and also contain valid DWARF. If we get any of that then
552 // we return the abilities of the first N_OSO's DWARF.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000553
554 const uint32_t oso_index_count = GetNumCompileUnits();
555 if (oso_index_count > 0) {
556 InitOSO();
557 if (!m_compile_unit_infos.empty()) {
558 return SymbolFile::CompileUnits | SymbolFile::Functions |
559 SymbolFile::Blocks | SymbolFile::GlobalVariables |
560 SymbolFile::LocalVariables | SymbolFile::VariableTypes |
561 SymbolFile::LineTables;
Greg Clayton81c22f62011-10-19 18:09:39 +0000562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 }
564 return 0;
Greg Clayton81c22f62011-10-19 18:09:39 +0000565}
566
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567uint32_t SymbolFileDWARFDebugMap::GetNumCompileUnits() {
568 InitOSO();
569 return m_compile_unit_infos.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) {
573 CompUnitSP comp_unit_sp;
574 const uint32_t cu_count = GetNumCompileUnits();
Greg Clayton57abc5d2013-05-10 21:47:16 +0000575
Kate Stoneb9c1b512016-09-06 20:57:50 +0000576 if (cu_idx < cu_count) {
577 Module *oso_module = GetModuleByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
578 if (oso_module) {
579 FileSpec so_file_spec;
580 if (GetFileSpecForSO(cu_idx, so_file_spec)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000581 // User zero as the ID to match the compile unit at offset zero in each
582 // .o file since each .o file can only have one compile unit for now.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000583 lldb::user_id_t cu_id = 0;
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000584 m_compile_unit_infos[cu_idx].compile_unit_sp =
585 std::make_shared<CompileUnit>(
586 m_obj_file->GetModule(), nullptr, so_file_spec, cu_id,
587 eLanguageTypeUnknown, eLazyBoolCalculate);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
590 // Let our symbol vendor know about this compile unit
591 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
592 cu_idx, m_compile_unit_infos[cu_idx].compile_unit_sp);
Greg Clayton3046e662013-07-10 01:23:25 +0000593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp;
597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598
Kate Stoneb9c1b512016-09-06 20:57:50 +0000599 return comp_unit_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
602SymbolFileDWARFDebugMap::CompileUnitInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000603SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000604 return GetCompUnitInfo(*sc.comp_unit);
605}
606
607SymbolFileDWARFDebugMap::CompileUnitInfo *
608SymbolFileDWARFDebugMap::GetCompUnitInfo(const CompileUnit &comp_unit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000609 const uint32_t cu_count = GetNumCompileUnits();
610 for (uint32_t i = 0; i < cu_count; ++i) {
Zachary Turner863f8c12019-01-11 18:03:20 +0000611 if (comp_unit == m_compile_unit_infos[i].compile_unit_sp.get())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 return &m_compile_unit_infos[i];
613 }
614 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615}
616
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule(
618 const lldb_private::Module *module,
619 std::vector<CompileUnitInfo *> &cu_infos) {
620 const uint32_t cu_count = GetNumCompileUnits();
621 for (uint32_t i = 0; i < cu_count; ++i) {
622 if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i]))
623 cu_infos.push_back(&m_compile_unit_infos[i]);
624 }
625 return cu_infos.size();
Greg Clayton1f746072012-08-29 21:13:06 +0000626}
627
628lldb::LanguageType
Zachary Turner863f8c12019-01-11 18:03:20 +0000629SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) {
630 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000632 return oso_dwarf->ParseLanguage(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 return eLanguageTypeUnknown;
Greg Clayton1f746072012-08-29 21:13:06 +0000634}
635
Zachary Turner863f8c12019-01-11 18:03:20 +0000636size_t SymbolFileDWARFDebugMap::ParseFunctions(CompileUnit &comp_unit) {
637 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000638 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000639 return oso_dwarf->ParseFunctions(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641}
642
Zachary Turner863f8c12019-01-11 18:03:20 +0000643bool SymbolFileDWARFDebugMap::ParseLineTable(CompileUnit &comp_unit) {
644 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000646 return oso_dwarf->ParseLineTable(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648}
649
Zachary Turner863f8c12019-01-11 18:03:20 +0000650bool SymbolFileDWARFDebugMap::ParseDebugMacros(CompileUnit &comp_unit) {
651 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000652 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000653 return oso_dwarf->ParseDebugMacros(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000654 return false;
Siva Chandrad8335e92015-12-16 00:22:08 +0000655}
656
Zachary Turner863f8c12019-01-11 18:03:20 +0000657bool SymbolFileDWARFDebugMap::ParseSupportFiles(CompileUnit &comp_unit,
658 FileSpecList &support_files) {
659 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000660 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000661 return oso_dwarf->ParseSupportFiles(comp_unit, support_files);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000662 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663}
664
Zachary Turner863f8c12019-01-11 18:03:20 +0000665bool SymbolFileDWARFDebugMap::ParseIsOptimized(CompileUnit &comp_unit) {
666 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000668 return oso_dwarf->ParseIsOptimized(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669 return false;
Greg Claytonad2b63c2016-07-05 23:01:20 +0000670}
671
Kate Stoneb9c1b512016-09-06 20:57:50 +0000672bool SymbolFileDWARFDebugMap::ParseImportedModules(
Adrian Prantl0f30a3b2019-02-13 18:10:41 +0000673 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
675 if (oso_dwarf)
676 return oso_dwarf->ParseImportedModules(sc, imported_modules);
677 return false;
678}
679
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000680size_t SymbolFileDWARFDebugMap::ParseBlocksRecursive(Function &func) {
681 CompileUnit *comp_unit = func.GetCompileUnit();
682 if (!comp_unit)
683 return 0;
684
685 SymbolFileDWARF *oso_dwarf = GetSymbolFile(*comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 if (oso_dwarf)
Zachary Turnerffc1b8f2019-01-14 22:40:41 +0000687 return oso_dwarf->ParseBlocksRecursive(func);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 return 0;
689}
690
Zachary Turner863f8c12019-01-11 18:03:20 +0000691size_t SymbolFileDWARFDebugMap::ParseTypes(CompileUnit &comp_unit) {
692 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000693 if (oso_dwarf)
Zachary Turner863f8c12019-01-11 18:03:20 +0000694 return oso_dwarf->ParseTypes(comp_unit);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 return 0;
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000696}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697
698size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) {
700 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
701 if (oso_dwarf)
702 return oso_dwarf->ParseVariablesForContext(sc);
703 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704}
705
Kate Stoneb9c1b512016-09-06 20:57:50 +0000706Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) {
707 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
708 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
709 if (oso_dwarf)
710 return oso_dwarf->ResolveTypeUID(type_uid);
711 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712}
713
Adrian Prantleca07c52018-11-05 20:49:07 +0000714llvm::Optional<SymbolFile::ArrayInfo>
715SymbolFileDWARFDebugMap::GetDynamicArrayInfoForUID(
716 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
717 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
718 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
719 if (oso_dwarf)
720 return oso_dwarf->GetDynamicArrayInfoForUID(type_uid, exe_ctx);
721 return llvm::None;
722}
723
Kate Stoneb9c1b512016-09-06 20:57:50 +0000724bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) {
725 bool success = false;
726 if (compiler_type) {
727 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
728 if (oso_dwarf->HasForwardDeclForClangType(compiler_type)) {
729 oso_dwarf->CompleteType(compiler_type);
730 success = true;
731 return true;
732 }
733 return false;
734 });
735 }
736 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737}
738
Zachary Turner991e4452018-10-25 20:45:19 +0000739uint32_t
740SymbolFileDWARFDebugMap::ResolveSymbolContext(const Address &exe_so_addr,
741 SymbolContextItem resolve_scope,
742 SymbolContext &sc) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743 uint32_t resolved_flags = 0;
744 Symtab *symtab = m_obj_file->GetSymtab();
745 if (symtab) {
746 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747
Kate Stoneb9c1b512016-09-06 20:57:50 +0000748 const DebugMap::Entry *debug_map_entry =
749 m_debug_map.FindEntryThatContains(exe_file_addr);
750 if (debug_map_entry) {
751
752 sc.symbol =
753 symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
754
755 if (sc.symbol != NULL) {
756 resolved_flags |= eSymbolContextSymbol;
757
758 uint32_t oso_idx = 0;
759 CompileUnitInfo *comp_unit_info =
760 GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx);
761 if (comp_unit_info) {
762 comp_unit_info->GetFileRangeMap(this);
763 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
764 if (oso_module) {
765 lldb::addr_t oso_file_addr =
766 exe_file_addr - debug_map_entry->GetRangeBase() +
767 debug_map_entry->data.GetOSOFileAddress();
768 Address oso_so_addr;
769 if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) {
770 resolved_flags |=
771 oso_module->GetSymbolVendor()->ResolveSymbolContext(
772 oso_so_addr, resolve_scope, sc);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 }
775 }
776 }
Greg Clayton6dc8d582015-08-18 22:32:36 +0000777 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 }
779 return resolved_flags;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000780}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
783 const FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000784 SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 const uint32_t initial = sc_list.GetSize();
786 const uint32_t cu_count = GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787
Kate Stoneb9c1b512016-09-06 20:57:50 +0000788 for (uint32_t i = 0; i < cu_count; ++i) {
Adrian Prantl05097242018-04-30 16:49:04 +0000789 // If we are checking for inlines, then we need to look through all compile
790 // units no matter if "file_spec" matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000791 bool resolve = check_inlines;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 if (!resolve) {
794 FileSpec so_file_spec;
795 if (GetFileSpecForSO(i, so_file_spec)) {
796 // Match the full path if the incoming file_spec has a directory (not
797 // just a basename)
798 const bool full_match = (bool)file_spec.GetDirectory();
799 resolve = FileSpec::Equal(file_spec, so_file_spec, full_match);
800 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000802 if (resolve) {
803 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i);
804 if (oso_dwarf)
805 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines,
806 resolve_scope, sc_list);
807 }
808 }
809 return sc_list.GetSize() - initial;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810}
811
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812uint32_t SymbolFileDWARFDebugMap::PrivateFindGlobalVariables(
813 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
814 const std::vector<uint32_t>
815 &indexes, // Indexes into the symbol table that match "name"
Jonas Devlieghere010b56b2018-11-27 15:25:58 +0000816 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 const uint32_t original_size = variables.GetSize();
818 const size_t match_count = indexes.size();
819 for (size_t i = 0; i < match_count; ++i) {
820 uint32_t oso_idx;
821 CompileUnitInfo *comp_unit_info =
822 GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx);
823 if (comp_unit_info) {
824 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
825 if (oso_dwarf) {
Pavel Labath34cda142018-05-31 09:46:26 +0000826 if (oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
827 variables))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 if (variables.GetSize() > max_matches)
829 break;
830 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000831 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000832 }
833 return variables.GetSize() - original_size;
834}
835
836uint32_t SymbolFileDWARFDebugMap::FindGlobalVariables(
837 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Pavel Labath34cda142018-05-31 09:46:26 +0000838 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839
Pavel Labath34cda142018-05-31 09:46:26 +0000840 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000841 const uint32_t original_size = variables.GetSize();
842
843 uint32_t total_matches = 0;
844
845 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
846 const uint32_t oso_matches = oso_dwarf->FindGlobalVariables(
Pavel Labath34cda142018-05-31 09:46:26 +0000847 name, parent_decl_ctx, max_matches, variables);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 if (oso_matches > 0) {
849 total_matches += oso_matches;
850
851 // Are we getting all matches?
852 if (max_matches == UINT32_MAX)
853 return false; // Yep, continue getting everything
854
855 // If we have found enough matches, lets get out
856 if (max_matches >= total_matches)
857 return true;
858
Adrian Prantl05097242018-04-30 16:49:04 +0000859 // Update the max matches for any subsequent calls to find globals in any
860 // other object files with DWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 max_matches -= oso_matches;
862 }
863
864 return false;
865 });
866
867 // Return the number of variable that were appended to the list
868 return variables.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869}
870
871uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872SymbolFileDWARFDebugMap::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +0000873 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874 VariableList &variables) {
Pavel Labath34cda142018-05-31 09:46:26 +0000875 // Remember how many variables are in the list before we search.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 const uint32_t original_size = variables.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877
Kate Stoneb9c1b512016-09-06 20:57:50 +0000878 uint32_t total_matches = 0;
879 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
880 const uint32_t oso_matches =
Pavel Labath34cda142018-05-31 09:46:26 +0000881 oso_dwarf->FindGlobalVariables(regex, max_matches, variables);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000882 if (oso_matches > 0) {
883 total_matches += oso_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884
Kate Stoneb9c1b512016-09-06 20:57:50 +0000885 // Are we getting all matches?
886 if (max_matches == UINT32_MAX)
887 return false; // Yep, continue getting everything
Sean Callanan0dc848c2015-04-01 20:43:23 +0000888
Kate Stoneb9c1b512016-09-06 20:57:50 +0000889 // If we have found enough matches, lets get out
890 if (max_matches >= total_matches)
891 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892
Adrian Prantl05097242018-04-30 16:49:04 +0000893 // Update the max matches for any subsequent calls to find globals in any
894 // other object files with DWARF
Kate Stoneb9c1b512016-09-06 20:57:50 +0000895 max_matches -= oso_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896 }
897
Kate Stoneb9c1b512016-09-06 20:57:50 +0000898 return false;
899 });
900
901 // Return the number of variable that were appended to the list
902 return variables.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903}
904
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex(
906 uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
907 const uint32_t symbol_idx = *symbol_idx_ptr;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000908
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 if (symbol_idx < comp_unit_info->first_symbol_index)
910 return -1;
911
912 if (symbol_idx <= comp_unit_info->last_symbol_index)
913 return 0;
914
915 return 1;
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000916}
917
Kate Stoneb9c1b512016-09-06 20:57:50 +0000918int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID(
919 user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
920 const user_id_t symbol_id = *symbol_idx_ptr;
921
922 if (symbol_id < comp_unit_info->first_symbol_id)
923 return -1;
924
925 if (symbol_id <= comp_unit_info->last_symbol_id)
926 return 0;
927
928 return 1;
Greg Clayton3ef3fc62010-08-17 23:16:15 +0000929}
930
Kate Stoneb9c1b512016-09-06 20:57:50 +0000931SymbolFileDWARFDebugMap::CompileUnitInfo *
932SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex(
933 uint32_t symbol_idx, uint32_t *oso_idx_ptr) {
934 const uint32_t oso_index_count = m_compile_unit_infos.size();
935 CompileUnitInfo *comp_unit_info = NULL;
936 if (oso_index_count) {
937 comp_unit_info = (CompileUnitInfo *)bsearch(
938 &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
939 sizeof(CompileUnitInfo),
940 (ComparisonFunction)SymbolContainsSymbolWithIndex);
941 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 if (oso_idx_ptr) {
944 if (comp_unit_info != NULL)
945 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000947 *oso_idx_ptr = UINT32_MAX;
948 }
949 return comp_unit_info;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950}
951
Kate Stoneb9c1b512016-09-06 20:57:50 +0000952SymbolFileDWARFDebugMap::CompileUnitInfo *
953SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID(
954 user_id_t symbol_id, uint32_t *oso_idx_ptr) {
955 const uint32_t oso_index_count = m_compile_unit_infos.size();
956 CompileUnitInfo *comp_unit_info = NULL;
957 if (oso_index_count) {
958 comp_unit_info = (CompileUnitInfo *)::bsearch(
959 &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
960 sizeof(CompileUnitInfo),
961 (ComparisonFunction)SymbolContainsSymbolWithID);
962 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 if (oso_idx_ptr) {
965 if (comp_unit_info != NULL)
966 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
Greg Clayton57a6b992010-08-17 00:35:34 +0000967 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 *oso_idx_ptr = UINT32_MAX;
969 }
970 return comp_unit_info;
971}
Greg Clayton57a6b992010-08-17 00:35:34 +0000972
Kate Stoneb9c1b512016-09-06 20:57:50 +0000973static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp,
974 SymbolContextList &sc_list,
975 uint32_t start_idx) {
Adrian Prantl05097242018-04-30 16:49:04 +0000976 // We found functions in .o files. Not all functions in the .o files will
977 // have made it into the final output file. The ones that did make it into
978 // the final output file will have a section whose module matches the module
979 // from the ObjectFile for this SymbolFile. When the modules don't match,
980 // then we have something that was in a .o file, but doesn't map to anything
981 // in the final executable.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982 uint32_t i = start_idx;
983 while (i < sc_list.GetSize()) {
984 SymbolContext sc;
985 sc_list.GetContextAtIndex(i, sc);
986 if (sc.function) {
987 const SectionSP section_sp(
988 sc.function->GetAddressRange().GetBaseAddress().GetSection());
989 if (section_sp->GetModule() != module_sp) {
990 sc_list.RemoveContextAtIndex(i);
991 continue;
992 }
993 }
994 ++i;
995 }
996}
997
998uint32_t SymbolFileDWARFDebugMap::FindFunctions(
999 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001000 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001001 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001002 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1003 Timer scoped_timer(func_cat,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
1005 name.GetCString());
1006
1007 uint32_t initial_size = 0;
1008 if (append)
1009 initial_size = sc_list.GetSize();
1010 else
1011 sc_list.Clear();
1012
1013 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1014 uint32_t sc_idx = sc_list.GetSize();
1015 if (oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask,
1016 include_inlines, true, sc_list)) {
1017 RemoveFunctionsWithModuleNotEqualTo(m_obj_file->GetModule(), sc_list,
1018 sc_idx);
1019 }
1020 return false;
1021 });
1022
1023 return sc_list.GetSize() - initial_size;
1024}
1025
1026uint32_t SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression &regex,
1027 bool include_inlines,
1028 bool append,
1029 SymbolContextList &sc_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001030 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1031 Timer scoped_timer(func_cat,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
Zachary Turner95eae422016-09-21 16:01:28 +00001033 regex.GetText().str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034
1035 uint32_t initial_size = 0;
1036 if (append)
1037 initial_size = sc_list.GetSize();
1038 else
1039 sc_list.Clear();
1040
1041 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1042 uint32_t sc_idx = sc_list.GetSize();
1043
1044 if (oso_dwarf->FindFunctions(regex, include_inlines, true, sc_list)) {
1045 RemoveFunctionsWithModuleNotEqualTo(m_obj_file->GetModule(), sc_list,
1046 sc_idx);
1047 }
1048 return false;
1049 });
1050
1051 return sc_list.GetSize() - initial_size;
1052}
1053
1054size_t SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001055 lldb::TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 TypeList &type_list) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001057 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1058 Timer scoped_timer(func_cat,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 "SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
1060 type_mask);
1061
1062 uint32_t initial_size = type_list.GetSize();
1063 SymbolFileDWARF *oso_dwarf = NULL;
1064 if (sc_scope) {
1065 SymbolContext sc;
1066 sc_scope->CalculateSymbolContext(&sc);
1067
1068 CompileUnitInfo *cu_info = GetCompUnitInfo(sc);
1069 if (cu_info) {
1070 oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info);
1071 if (oso_dwarf)
1072 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1073 }
1074 } else {
Sean Callanan0dc848c2015-04-01 20:43:23 +00001075 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1077 return false;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001078 });
Kate Stoneb9c1b512016-09-06 20:57:50 +00001079 }
1080 return type_list.GetSize() - initial_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081}
1082
Vedant Kumar4b36f792018-10-05 23:23:15 +00001083std::vector<lldb_private::CallEdge>
1084SymbolFileDWARFDebugMap::ParseCallEdgesInFunction(UserID func_id) {
1085 uint32_t oso_idx = GetOSOIndexFromUserID(func_id.GetID());
1086 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1087 if (oso_dwarf)
1088 return oso_dwarf->ParseCallEdgesInFunction(func_id);
1089 return {};
1090}
1091
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext(
1093 const DWARFDeclContext &die_decl_ctx) {
1094 TypeSP type_sp;
1095 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1096 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1097 return ((bool)type_sp);
1098 });
1099 return type_sp;
Greg Claytonf02500c2013-06-18 22:51:05 +00001100}
1101
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type(
1103 SymbolFileDWARF *skip_dwarf_oso) {
1104 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
1105 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001106 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 if (skip_dwarf_oso != oso_dwarf &&
1108 oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(NULL)) {
1109 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
1110 return true;
1111 }
1112 return false;
Sean Callanan0dc848c2015-04-01 20:43:23 +00001113 });
Kate Stoneb9c1b512016-09-06 20:57:50 +00001114 }
1115 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001116}
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001117
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE(
1119 const DWARFDIE &die, const ConstString &type_name,
1120 bool must_be_implementation) {
Adrian Prantl4e8be2c2018-06-13 16:21:24 +00001121 // If we have a debug map, we will have an Objective-C symbol whose name is
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122 // the type name and whose type is eSymbolTypeObjCClass. If we can find that
1123 // symbol and find its containing parent, we can locate the .o file that will
1124 // contain the implementation definition since it will be scoped inside the
Adrian Prantl05097242018-04-30 16:49:04 +00001125 // N_SO and we can then locate the SymbolFileDWARF that corresponds to that
1126 // N_SO.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127 SymbolFileDWARF *oso_dwarf = NULL;
1128 TypeSP type_sp;
1129 ObjectFile *module_objfile = m_obj_file->GetModule()->GetObjectFile();
1130 if (module_objfile) {
1131 Symtab *symtab = module_objfile->GetSymtab();
1132 if (symtab) {
1133 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
1134 type_name, eSymbolTypeObjCClass, Symtab::eDebugAny,
1135 Symtab::eVisibilityAny);
1136 if (objc_class_symbol) {
1137 // Get the N_SO symbol that contains the objective C class symbol as
Adrian Prantl05097242018-04-30 16:49:04 +00001138 // this should be the .o file that contains the real definition...
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
Greg Claytonc7f03b62012-01-12 04:33:28 +00001140
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141 if (source_file_symbol &&
1142 source_file_symbol->GetType() == eSymbolTypeSourceFile) {
1143 const uint32_t source_file_symbol_idx =
1144 symtab->GetIndexForSymbol(source_file_symbol);
1145 if (source_file_symbol_idx != UINT32_MAX) {
1146 CompileUnitInfo *compile_unit_info =
1147 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx,
1148 NULL);
1149 if (compile_unit_info) {
1150 oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info);
1151 if (oso_dwarf) {
1152 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1153 die, type_name, must_be_implementation));
1154 if (type_sp) {
1155 return type_sp;
Greg Claytonbc63aac2015-02-25 22:41:34 +00001156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001158 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001160 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001161 }
Greg Clayton901c5ca2011-12-03 04:40:03 +00001162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 }
Greg Claytonbc63aac2015-02-25 22:41:34 +00001164
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165 // Only search all .o files for the definition if we don't need the
Adrian Prantl05097242018-04-30 16:49:04 +00001166 // implementation because otherwise, with a valid debug map we should have
1167 // the ObjC class symbol and the code above should have found it.
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001168 if (!must_be_implementation) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 TypeSP type_sp;
1170
1171 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1172 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1173 die, type_name, must_be_implementation);
1174 return (bool)type_sp;
1175 });
1176
1177 return type_sp;
1178 }
1179 return TypeSP();
Greg Clayton901c5ca2011-12-03 04:40:03 +00001180}
1181
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182uint32_t SymbolFileDWARFDebugMap::FindTypes(
Zachary Turner576495e2019-01-14 22:41:21 +00001183 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
1184 bool append, uint32_t max_matches,
Greg Claytonae088e52016-02-10 21:28:13 +00001185 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186 TypeMap &types) {
1187 if (!append)
1188 types.Clear();
Greg Clayton6dbd3982010-09-15 05:51:24 +00001189
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 const uint32_t initial_types_size = types.GetSize();
Greg Clayton6dbd3982010-09-15 05:51:24 +00001191
Zachary Turner576495e2019-01-14 22:41:21 +00001192 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1193 oso_dwarf->FindTypes(name, parent_decl_ctx, append, max_matches,
1194 searched_symbol_files, types);
1195 return types.GetSize() >= max_matches;
1196 });
Greg Clayton6dbd3982010-09-15 05:51:24 +00001197
Kate Stoneb9c1b512016-09-06 20:57:50 +00001198 return types.GetSize() - initial_types_size;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00001199}
1200
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201//
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202// uint32_t
1203// SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const
1204// RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding
1205// encoding, lldb::user_id_t udt_uid, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206//{
1207// SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
1208// if (oso_dwarf)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209// return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding,
1210// udt_uid, types);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211// return 0;
1212//}
1213
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215 const lldb_private::ConstString &name,
1216 const CompilerDeclContext *parent_decl_ctx) {
1217 CompilerDeclContext matching_namespace;
Greg Clayton96d7d742010-11-10 23:42:09 +00001218
Zachary Turnerc0a246a2019-01-14 22:41:00 +00001219 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1220 matching_namespace = oso_dwarf->FindNamespace(name, parent_decl_ctx);
Greg Clayton96d7d742010-11-10 23:42:09 +00001221
Zachary Turnerc0a246a2019-01-14 22:41:00 +00001222 return (bool)matching_namespace;
1223 });
Greg Clayton96d7d742010-11-10 23:42:09 +00001224
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225 return matching_namespace;
Greg Clayton96d7d742010-11-10 23:42:09 +00001226}
1227
Zachary Turner49110232018-11-05 17:40:28 +00001228void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
1229 ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool {
1230 oso_dwarf->DumpClangAST(s);
1231 return true;
1232 });
1233}
1234
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235//------------------------------------------------------------------
1236// PluginInterface protocol
1237//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001238lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginName() {
1239 return GetPluginNameStatic();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240}
1241
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242uint32_t SymbolFileDWARFDebugMap::GetPluginVersion() { return 1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243
Greg Clayton1f746072012-08-29 21:13:06 +00001244lldb::CompUnitSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001245SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) {
1246 if (oso_dwarf) {
1247 const uint32_t cu_count = GetNumCompileUnits();
1248 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1249 SymbolFileDWARF *oso_symfile =
1250 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1251 if (oso_symfile == oso_dwarf) {
1252 if (!m_compile_unit_infos[cu_idx].compile_unit_sp)
1253 m_compile_unit_infos[cu_idx].compile_unit_sp =
1254 ParseCompileUnitAtIndex(cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00001255
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 return m_compile_unit_infos[cu_idx].compile_unit_sp;
1257 }
Greg Clayton1f746072012-08-29 21:13:06 +00001258 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 }
David Blaikiea322f362017-01-06 00:38:06 +00001260 llvm_unreachable("this shouldn't happen");
Greg Clayton1f746072012-08-29 21:13:06 +00001261}
1262
Greg Clayton9422dd62013-03-04 21:46:16 +00001263SymbolFileDWARFDebugMap::CompileUnitInfo *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001264SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) {
1265 if (oso_dwarf) {
1266 const uint32_t cu_count = GetNumCompileUnits();
1267 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1268 SymbolFileDWARF *oso_symfile =
1269 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1270 if (oso_symfile == oso_dwarf) {
1271 return &m_compile_unit_infos[cu_idx];
1272 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001273 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 }
1275 return NULL;
Greg Clayton9422dd62013-03-04 21:46:16 +00001276}
1277
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
1279 const CompUnitSP &cu_sp) {
1280 if (oso_dwarf) {
1281 const uint32_t cu_count = GetNumCompileUnits();
1282 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1283 SymbolFileDWARF *oso_symfile =
1284 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1285 if (oso_symfile == oso_dwarf) {
1286 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
1287 assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() ==
1288 cu_sp.get());
1289 } else {
1290 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
1291 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
1292 cu_idx, cu_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00001293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001295 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001297}
1298
Greg Clayton99558cc42015-08-24 23:46:31 +00001299CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
1301 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1302 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1303 if (oso_dwarf)
1304 return oso_dwarf->GetDeclContextForUID(type_uid);
1305 return CompilerDeclContext();
Greg Clayton81c22f62011-10-19 18:09:39 +00001306}
1307
Greg Clayton99558cc42015-08-24 23:46:31 +00001308CompilerDeclContext
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1310 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1311 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1312 if (oso_dwarf)
1313 return oso_dwarf->GetDeclContextContainingUID(type_uid);
1314 return CompilerDeclContext();
Greg Clayton81c22f62011-10-19 18:09:39 +00001315}
1316
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317void SymbolFileDWARFDebugMap::ParseDeclsForContext(
1318 lldb_private::CompilerDeclContext decl_ctx) {
1319 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1320 oso_dwarf->ParseDeclsForContext(decl_ctx);
1321 return true; // Keep iterating
1322 });
Greg Clayton2bd8e7b2015-10-07 22:07:33 +00001323}
1324
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info,
1326 lldb::addr_t exe_file_addr,
1327 lldb::addr_t exe_byte_size,
1328 lldb::addr_t oso_file_addr,
1329 lldb::addr_t oso_byte_size) {
1330 const uint32_t debug_map_idx =
1331 m_debug_map.FindEntryIndexThatContains(exe_file_addr);
1332 if (debug_map_idx != UINT32_MAX) {
1333 DebugMap::Entry *debug_map_entry =
1334 m_debug_map.FindEntryThatContains(exe_file_addr);
1335 debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
1336 addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size);
1337 if (range_size == 0) {
1338 range_size = std::max<addr_t>(exe_byte_size, oso_byte_size);
1339 if (range_size == 0)
1340 range_size = 1;
Greg Clayton9422dd62013-03-04 21:46:16 +00001341 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 cu_info->file_range_map.Append(
1343 FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr));
1344 return true;
1345 }
1346 return false;
Greg Clayton9422dd62013-03-04 21:46:16 +00001347}
1348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) {
1350 cu_info->file_range_map.Sort();
Greg Clayton9422dd62013-03-04 21:46:16 +00001351#if defined(DEBUG_OSO_DMAP)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001352 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
1353 const size_t n = oso_file_range_map.GetSize();
1354 printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
1355 cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
1356 for (size_t i = 0; i < n; ++i) {
1357 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
1358 printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
1359 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
1360 entry.GetRangeBase(), entry.GetRangeEnd(), entry.data,
1361 entry.data + entry.GetByteSize());
1362 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001363#endif
1364}
1365
1366lldb::addr_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
1368 lldb::addr_t oso_file_addr) {
1369 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile);
1370 if (cu_info) {
1371 const FileRangeMap::Entry *oso_range_entry =
1372 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1373 if (oso_range_entry) {
1374 const DebugMap::Entry *debug_map_entry =
1375 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1376 if (debug_map_entry) {
1377 const lldb::addr_t offset =
1378 oso_file_addr - oso_range_entry->GetRangeBase();
1379 const lldb::addr_t exe_file_addr =
1380 debug_map_entry->GetRangeBase() + offset;
1381 return exe_file_addr;
1382 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001383 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 }
1385 return LLDB_INVALID_ADDRESS;
Greg Clayton9422dd62013-03-04 21:46:16 +00001386}
1387
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) {
1389 // Make sure this address hasn't been fixed already
1390 Module *exe_module = GetObjectFile()->GetModule().get();
1391 Module *addr_module = addr.GetModule().get();
1392 if (addr_module == exe_module)
1393 return true; // Address is already in terms of the main executable module
Greg Clayton9422dd62013-03-04 21:46:16 +00001394
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 CompileUnitInfo *cu_info = GetCompileUnitInfo(GetSymbolFileAsSymbolFileDWARF(
1396 addr_module->GetSymbolVendor()->GetSymbolFile()));
1397 if (cu_info) {
1398 const lldb::addr_t oso_file_addr = addr.GetFileAddress();
1399 const FileRangeMap::Entry *oso_range_entry =
1400 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1401 if (oso_range_entry) {
1402 const DebugMap::Entry *debug_map_entry =
1403 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1404 if (debug_map_entry) {
1405 const lldb::addr_t offset =
1406 oso_file_addr - oso_range_entry->GetRangeBase();
1407 const lldb::addr_t exe_file_addr =
1408 debug_map_entry->GetRangeBase() + offset;
1409 return exe_module->ResolveFileAddress(exe_file_addr, addr);
1410 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 }
1413 return true;
Greg Clayton9422dd62013-03-04 21:46:16 +00001414}
1415
Kate Stoneb9c1b512016-09-06 20:57:50 +00001416LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf,
1417 LineTable *line_table) {
1418 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf);
1419 if (cu_info)
1420 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
1421 return NULL;
Greg Clayton9422dd62013-03-04 21:46:16 +00001422}
1423
Enrico Granatac76e60b2013-06-27 01:43:09 +00001424size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data,
1426 DWARFDebugAranges *debug_aranges) {
1427 size_t num_line_entries_added = 0;
1428 if (debug_aranges && dwarf2Data) {
1429 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
1430 if (compile_unit_info) {
1431 const FileRangeMap &file_range_map =
1432 compile_unit_info->GetFileRangeMap(this);
1433 for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) {
1434 const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx);
1435 if (entry) {
1436 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(),
1437 entry->GetRangeEnd());
1438 num_line_entries_added++;
Enrico Granatac76e60b2013-06-27 01:43:09 +00001439 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440 }
Enrico Granatac76e60b2013-06-27 01:43:09 +00001441 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 }
1443 return num_line_entries_added;
Enrico Granatac76e60b2013-06-27 01:43:09 +00001444}