1 //===-- SymbolFileDWARFDebugMap.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 //===----------------------------------------------------------------------===//
8
9 #include "SymbolFileDWARFDebugMap.h"
10 #include "DWARFDebugAranges.h"
11
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleList.h"
14 #include "lldb/Core/PluginManager.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Utility/RangeMap.h"
18 #include "lldb/Utility/RegularExpression.h"
19
20 //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
21 #if defined(DEBUG_OSO_DMAP)
22 #include "lldb/Core/StreamFile.h"
23 #endif
24
25 #include "lldb/Symbol/CompileUnit.h"
26 #include "lldb/Symbol/LineTable.h"
27 #include "lldb/Symbol/ObjectFile.h"
28 #include "lldb/Symbol/SymbolVendor.h"
29 #include "lldb/Symbol/TypeMap.h"
30 #include "lldb/Symbol/VariableList.h"
31 #include "llvm/Support/ScopedPrinter.h"
32
33 #include "LogChannelDWARF.h"
34 #include "SymbolFileDWARF.h"
35
36 // Work around the fact that Timer.h pulls in the system Mach-O headers.
37 #include "lldb/Utility/Timer.h"
38
39 #include <memory>
40
41 using namespace lldb;
42 using namespace lldb_private;
43
44 char SymbolFileDWARFDebugMap::ID;
45
46 // Subclass lldb_private::Module so we can intercept the
47 // "Module::GetObjectFile()" (so we can fixup the object file sections) and
48 // also for "Module::GetSymbolFile()" (so we can fixup the symbol file id.
49
50 const SymbolFileDWARFDebugMap::FileRangeMap &
GetFileRangeMap(SymbolFileDWARFDebugMap * exe_symfile)51 SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(
52 SymbolFileDWARFDebugMap *exe_symfile) {
53 if (file_range_map_valid)
54 return file_range_map;
55
56 file_range_map_valid = true;
57
58 Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this);
59 if (!oso_module)
60 return file_range_map;
61
62 ObjectFile *oso_objfile = oso_module->GetObjectFile();
63 if (!oso_objfile)
64 return file_range_map;
65
66 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
67 LLDB_LOGF(
68 log,
69 "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
70 static_cast<void *>(this),
71 oso_module->GetSpecificationDescription().c_str());
72
73 std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos;
74 if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) {
75 for (auto comp_unit_info : cu_infos) {
76 Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab();
77 ModuleSP oso_module_sp(oso_objfile->GetModule());
78 Symtab *oso_symtab = oso_objfile->GetSymtab();
79
80 /// const uint32_t fun_resolve_flags = SymbolContext::Module |
81 /// eSymbolContextCompUnit | eSymbolContextFunction;
82 // SectionList *oso_sections = oso_objfile->Sections();
83 // Now we need to make sections that map from zero based object file
84 // addresses to where things ended up in the main executable.
85
86 assert(comp_unit_info->first_symbol_index != UINT32_MAX);
87 // End index is one past the last valid symbol index
88 const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1;
89 for (uint32_t idx = comp_unit_info->first_symbol_index +
90 2; // Skip the N_SO and N_OSO
91 idx < oso_end_idx; ++idx) {
92 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
93 if (exe_symbol) {
94 if (!exe_symbol->IsDebug())
95 continue;
96
97 switch (exe_symbol->GetType()) {
98 default:
99 break;
100
101 case eSymbolTypeCode: {
102 // For each N_FUN, or function that we run into in the debug map we
103 // make a new section that we add to the sections found in the .o
104 // file. This new section has the file address set to what the
105 // addresses are in the .o file, and the load address is adjusted
106 // to match where it ended up in the final executable! We do this
107 // before we parse any dwarf info so that when it goes get parsed
108 // all section/offset addresses that get registered will resolve
109 // correctly to the new addresses in the main executable.
110
111 // First we find the original symbol in the .o file's symbol table
112 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(
113 exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
114 eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
115 if (oso_fun_symbol) {
116 // Add the inverse OSO file address to debug map entry mapping
117 exe_symfile->AddOSOFileRange(
118 this, exe_symbol->GetAddressRef().GetFileAddress(),
119 exe_symbol->GetByteSize(),
120 oso_fun_symbol->GetAddressRef().GetFileAddress(),
121 oso_fun_symbol->GetByteSize());
122 }
123 } break;
124
125 case eSymbolTypeData: {
126 // For each N_GSYM we remap the address for the global by making a
127 // new section that we add to the sections found in the .o file.
128 // This new section has the file address set to what the addresses
129 // are in the .o file, and the load address is adjusted to match
130 // where it ended up in the final executable! We do this before we
131 // parse any dwarf info so that when it goes get parsed all
132 // section/offset addresses that get registered will resolve
133 // correctly to the new addresses in the main executable. We
134 // initially set the section size to be 1 byte, but will need to
135 // fix up these addresses further after all globals have been
136 // parsed to span the gaps, or we can find the global variable
137 // sizes from the DWARF info as we are parsing.
138
139 // Next we find the non-stab entry that corresponds to the N_GSYM
140 // in the .o file
141 Symbol *oso_gsym_symbol =
142 oso_symtab->FindFirstSymbolWithNameAndType(
143 exe_symbol->GetMangled().GetName(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;
165 }
166
167 class DebugMapModule : public Module {
168 public:
DebugMapModule(const ModuleSP & exe_module_sp,uint32_t cu_idx,const FileSpec & file_spec,const ArchSpec & arch,const ConstString * object_name,off_t object_offset,const llvm::sys::TimePoint<> object_mod_time)169 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,
172 const llvm::sys::TimePoint<> object_mod_time)
173 : Module(file_spec, arch, object_name, object_offset, object_mod_time),
174 m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {}
175
176 ~DebugMapModule() override = default;
177
178 SymbolFile *
GetSymbolFile(bool can_create=true,lldb_private::Stream * feedback_strm=nullptr)179 GetSymbolFile(bool can_create = true,
180 lldb_private::Stream *feedback_strm = nullptr) override {
181 // Scope for locker
182 if (m_symfile_up.get() || !can_create)
183 return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
184
185 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 if (SymbolFile *symfile =
192 Module::GetSymbolFile(can_create, feedback_strm)) {
193 // Set a pointer to this class to set our OSO DWARF file know that
194 // the DWARF is being used along with a debug map and that it will
195 // have the remapped sections that we do below.
196 SymbolFileDWARF *oso_symfile =
197 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(symfile);
198
199 if (!oso_symfile)
200 return nullptr;
201
202 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
203 SymbolFile *exe_symfile = exe_module_sp->GetSymbolFile();
204
205 if (exe_objfile && exe_symfile) {
206 oso_symfile->SetDebugMapModule(exe_module_sp);
207 // Set the ID of the symbol file DWARF to the index of the OSO
208 // shifted left by 32 bits to provide a unique prefix for any
209 // UserID's that get created in the symbol file.
210 oso_symfile->SetID(((uint64_t)m_cu_idx + 1ull) << 32ull);
211 }
212 return symfile;
213 }
214 }
215 }
216 return nullptr;
217 }
218
219 protected:
220 ModuleWP m_exe_module_wp;
221 const uint32_t m_cu_idx;
222 };
223
Initialize()224 void SymbolFileDWARFDebugMap::Initialize() {
225 PluginManager::RegisterPlugin(GetPluginNameStatic(),
226 GetPluginDescriptionStatic(), CreateInstance);
227 }
228
Terminate()229 void SymbolFileDWARFDebugMap::Terminate() {
230 PluginManager::UnregisterPlugin(CreateInstance);
231 }
232
GetPluginNameStatic()233 lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginNameStatic() {
234 static ConstString g_name("dwarf-debugmap");
235 return g_name;
236 }
237
GetPluginDescriptionStatic()238 const char *SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
239 return "DWARF and DWARF3 debug symbol file reader (debug map).";
240 }
241
CreateInstance(ObjectFileSP objfile_sp)242 SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFileSP objfile_sp) {
243 return new SymbolFileDWARFDebugMap(std::move(objfile_sp));
244 }
245
SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp)246 SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp)
247 : SymbolFile(std::move(objfile_sp)), m_flags(), m_compile_unit_infos(),
248 m_func_indexes(), m_glob_indexes(),
249 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
250
251 SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() = default;
252
InitializeObject()253 void SymbolFileDWARFDebugMap::InitializeObject() {}
254
InitOSO()255 void SymbolFileDWARFDebugMap::InitOSO() {
256 if (m_flags.test(kHaveInitializedOSOs))
257 return;
258
259 m_flags.set(kHaveInitializedOSOs);
260
261 // If the object file has been stripped, there is no sense in looking further
262 // as all of the debug symbols for the debug map will not be available
263 if (m_objfile_sp->IsStripped())
264 return;
265
266 // Also make sure the file type is some sort of executable. Core files, debug
267 // info files (dSYM), object files (.o files), and stub libraries all can
268 switch (m_objfile_sp->GetType()) {
269 case ObjectFile::eTypeInvalid:
270 case ObjectFile::eTypeCoreFile:
271 case ObjectFile::eTypeDebugInfo:
272 case ObjectFile::eTypeObjectFile:
273 case ObjectFile::eTypeStubLibrary:
274 case ObjectFile::eTypeUnknown:
275 case ObjectFile::eTypeJIT:
276 return;
277
278 case ObjectFile::eTypeExecutable:
279 case ObjectFile::eTypeDynamicLinker:
280 case ObjectFile::eTypeSharedLibrary:
281 break;
282 }
283
284 // In order to get the abilities of this plug-in, we look at the list of
285 // N_OSO entries (object files) from the symbol table and make sure that
286 // these files exist and also contain valid DWARF. If we get any of that then
287 // we return the abilities of the first N_OSO's DWARF.
288
289 Symtab *symtab = m_objfile_sp->GetSymtab();
290 if (symtab) {
291 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
292
293 std::vector<uint32_t> oso_indexes;
294 // When a mach-o symbol is encoded, the n_type field is encoded in bits
295 // 23:16, and the n_desc field is encoded in bits 15:0.
296 //
297 // To find all N_OSO entries that are part of the DWARF + debug map we find
298 // only object file symbols with the flags value as follows: bits 23:16 ==
299 // 0x66 (N_OSO) bits 15: 0 == 0x0001 (specifies this is a debug map object
300 // file)
301 const uint32_t k_oso_symbol_flags_value = 0x660001u;
302
303 const uint32_t oso_index_count =
304 symtab->AppendSymbolIndexesWithTypeAndFlagsValue(
305 eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
306
307 if (oso_index_count > 0) {
308 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes,
309 Symtab::eVisibilityAny,
310 m_func_indexes);
311 symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes,
312 Symtab::eVisibilityAny,
313 m_glob_indexes);
314
315 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
316 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
317
318 for (uint32_t sym_idx : m_func_indexes) {
319 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
320 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
321 lldb::addr_t byte_size = symbol->GetByteSize();
322 DebugMap::Entry debug_map_entry(
323 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
324 m_debug_map.Append(debug_map_entry);
325 }
326 for (uint32_t sym_idx : m_glob_indexes) {
327 const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
328 lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
329 lldb::addr_t byte_size = symbol->GetByteSize();
330 DebugMap::Entry debug_map_entry(
331 file_addr, byte_size, OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
332 m_debug_map.Append(debug_map_entry);
333 }
334 m_debug_map.Sort();
335
336 m_compile_unit_infos.resize(oso_index_count);
337
338 for (uint32_t i = 0; i < oso_index_count; ++i) {
339 const uint32_t so_idx = oso_indexes[i] - 1;
340 const uint32_t oso_idx = oso_indexes[i];
341 const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx);
342 const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx);
343 if (so_symbol && oso_symbol &&
344 so_symbol->GetType() == eSymbolTypeSourceFile &&
345 oso_symbol->GetType() == eSymbolTypeObjectFile) {
346 m_compile_unit_infos[i].so_file.SetFile(
347 so_symbol->GetName().AsCString(), FileSpec::Style::native);
348 m_compile_unit_infos[i].oso_path = oso_symbol->GetName();
349 m_compile_unit_infos[i].oso_mod_time =
350 llvm::sys::toTimePoint(oso_symbol->GetIntegerValue(0));
351 uint32_t sibling_idx = so_symbol->GetSiblingIndex();
352 // The sibling index can't be less that or equal to the current index
353 // "i"
354 if (sibling_idx == UINT32_MAX) {
355 m_objfile_sp->GetModule()->ReportError(
356 "N_SO in symbol with UID %u has invalid sibling in debug map, "
357 "please file a bug and attach the binary listed in this error",
358 so_symbol->GetID());
359 } else {
360 const Symbol *last_symbol = symtab->SymbolAtIndex(sibling_idx - 1);
361 m_compile_unit_infos[i].first_symbol_index = so_idx;
362 m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1;
363 m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID();
364 m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID();
365
366 LLDB_LOGF(log, "Initialized OSO 0x%8.8x: file=%s", i,
367 oso_symbol->GetName().GetCString());
368 }
369 } else {
370 if (oso_symbol == nullptr)
371 m_objfile_sp->GetModule()->ReportError(
372 "N_OSO symbol[%u] can't be found, please file a bug and attach "
373 "the binary listed in this error",
374 oso_idx);
375 else if (so_symbol == nullptr)
376 m_objfile_sp->GetModule()->ReportError(
377 "N_SO not found for N_OSO symbol[%u], please file a bug and "
378 "attach the binary listed in this error",
379 oso_idx);
380 else if (so_symbol->GetType() != eSymbolTypeSourceFile)
381 m_objfile_sp->GetModule()->ReportError(
382 "N_SO has incorrect symbol type (%u) for N_OSO symbol[%u], "
383 "please file a bug and attach the binary listed in this error",
384 so_symbol->GetType(), oso_idx);
385 else if (oso_symbol->GetType() != eSymbolTypeSourceFile)
386 m_objfile_sp->GetModule()->ReportError(
387 "N_OSO has incorrect symbol type (%u) for N_OSO symbol[%u], "
388 "please file a bug and attach the binary listed in this error",
389 oso_symbol->GetType(), oso_idx);
390 }
391 }
392 }
393 }
394 }
395
GetModuleByOSOIndex(uint32_t oso_idx)396 Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) {
397 const uint32_t cu_count = GetNumCompileUnits();
398 if (oso_idx < cu_count)
399 return GetModuleByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
400 return nullptr;
401 }
402
GetModuleByCompUnitInfo(CompileUnitInfo * comp_unit_info)403 Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo(
404 CompileUnitInfo *comp_unit_info) {
405 if (!comp_unit_info->oso_sp) {
406 auto pos = m_oso_map.find(
407 {comp_unit_info->oso_path, comp_unit_info->oso_mod_time});
408 if (pos != m_oso_map.end()) {
409 comp_unit_info->oso_sp = pos->second;
410 } else {
411 ObjectFile *obj_file = GetObjectFile();
412 comp_unit_info->oso_sp = std::make_shared<OSOInfo>();
413 m_oso_map[{comp_unit_info->oso_path, comp_unit_info->oso_mod_time}] =
414 comp_unit_info->oso_sp;
415 const char *oso_path = comp_unit_info->oso_path.GetCString();
416 FileSpec oso_file(oso_path);
417 ConstString oso_object;
418 if (FileSystem::Instance().Exists(oso_file)) {
419 FileSystem::Instance().Collect(oso_file);
420 // The modification time returned by the FS can have a higher precision
421 // than the one from the CU.
422 auto oso_mod_time = std::chrono::time_point_cast<std::chrono::seconds>(
423 FileSystem::Instance().GetModificationTime(oso_file));
424 // A timestamp of 0 means that the linker was in deterministic mode. In
425 // that case, we should skip the check against the filesystem last
426 // modification timestamp, since it will never match.
427 if (comp_unit_info->oso_mod_time != llvm::sys::TimePoint<>() &&
428 oso_mod_time != comp_unit_info->oso_mod_time) {
429 obj_file->GetModule()->ReportError(
430 "debug map object file '%s' has changed (actual time is "
431 "%s, debug map time is %s"
432 ") since this executable was linked, file will be ignored",
433 oso_file.GetPath().c_str(), llvm::to_string(oso_mod_time).c_str(),
434 llvm::to_string(comp_unit_info->oso_mod_time).c_str());
435 return nullptr;
436 }
437
438 } else {
439 const bool must_exist = true;
440
441 if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file,
442 oso_object, must_exist)) {
443 return nullptr;
444 }
445 }
446 // Always create a new module for .o files. Why? Because we use the debug
447 // map, to add new sections to each .o file and even though a .o file
448 // might not have changed, the sections that get added to the .o file can
449 // change.
450 ArchSpec oso_arch;
451 // Only adopt the architecture from the module (not the vendor or OS)
452 // since .o files for "i386-apple-ios" will historically show up as "i386
453 // -apple-macosx" due to the lack of a LC_VERSION_MIN_MACOSX or
454 // LC_VERSION_MIN_IPHONEOS load command...
455 oso_arch.SetTriple(m_objfile_sp->GetModule()
456 ->GetArchitecture()
457 .GetTriple()
458 .getArchName()
459 .str()
460 .c_str());
461 comp_unit_info->oso_sp->module_sp = std::make_shared<DebugMapModule>(
462 obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file,
463 oso_arch, oso_object ? &oso_object : nullptr, 0,
464 oso_object ? comp_unit_info->oso_mod_time : llvm::sys::TimePoint<>());
465 }
466 }
467 if (comp_unit_info->oso_sp)
468 return comp_unit_info->oso_sp->module_sp.get();
469 return nullptr;
470 }
471
GetFileSpecForSO(uint32_t oso_idx,FileSpec & file_spec)472 bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx,
473 FileSpec &file_spec) {
474 if (oso_idx < m_compile_unit_infos.size()) {
475 if (m_compile_unit_infos[oso_idx].so_file) {
476 file_spec = m_compile_unit_infos[oso_idx].so_file;
477 return true;
478 }
479 }
480 return false;
481 }
482
GetObjectFileByOSOIndex(uint32_t oso_idx)483 ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) {
484 Module *oso_module = GetModuleByOSOIndex(oso_idx);
485 if (oso_module)
486 return oso_module->GetObjectFile();
487 return nullptr;
488 }
489
490 SymbolFileDWARF *
GetSymbolFile(const SymbolContext & sc)491 SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) {
492 return GetSymbolFile(*sc.comp_unit);
493 }
494
495 SymbolFileDWARF *
GetSymbolFile(const CompileUnit & comp_unit)496 SymbolFileDWARFDebugMap::GetSymbolFile(const CompileUnit &comp_unit) {
497 CompileUnitInfo *comp_unit_info = GetCompUnitInfo(comp_unit);
498 if (comp_unit_info)
499 return GetSymbolFileByCompUnitInfo(comp_unit_info);
500 return nullptr;
501 }
502
GetObjectFileByCompUnitInfo(CompileUnitInfo * comp_unit_info)503 ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo(
504 CompileUnitInfo *comp_unit_info) {
505 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
506 if (oso_module)
507 return oso_module->GetObjectFile();
508 return nullptr;
509 }
510
GetCompUnitInfoIndex(const CompileUnitInfo * comp_unit_info)511 uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex(
512 const CompileUnitInfo *comp_unit_info) {
513 if (!m_compile_unit_infos.empty()) {
514 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
515 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
516 if (first_comp_unit_info <= comp_unit_info &&
517 comp_unit_info <= last_comp_unit_info)
518 return comp_unit_info - first_comp_unit_info;
519 }
520 return UINT32_MAX;
521 }
522
523 SymbolFileDWARF *
GetSymbolFileByOSOIndex(uint32_t oso_idx)524 SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) {
525 unsigned size = m_compile_unit_infos.size();
526 if (oso_idx < size)
527 return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
528 return nullptr;
529 }
530
531 SymbolFileDWARF *
GetSymbolFileAsSymbolFileDWARF(SymbolFile * sym_file)532 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) {
533 if (sym_file &&
534 sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
535 return static_cast<SymbolFileDWARF *>(sym_file);
536 return nullptr;
537 }
538
GetSymbolFileByCompUnitInfo(CompileUnitInfo * comp_unit_info)539 SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo(
540 CompileUnitInfo *comp_unit_info) {
541 if (Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info))
542 return GetSymbolFileAsSymbolFileDWARF(oso_module->GetSymbolFile());
543 return nullptr;
544 }
545
CalculateAbilities()546 uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() {
547 // In order to get the abilities of this plug-in, we look at the list of
548 // N_OSO entries (object files) from the symbol table and make sure that
549 // these files exist and also contain valid DWARF. If we get any of that then
550 // we return the abilities of the first N_OSO's DWARF.
551
552 const uint32_t oso_index_count = GetNumCompileUnits();
553 if (oso_index_count > 0) {
554 InitOSO();
555 if (!m_compile_unit_infos.empty()) {
556 return SymbolFile::CompileUnits | SymbolFile::Functions |
557 SymbolFile::Blocks | SymbolFile::GlobalVariables |
558 SymbolFile::LocalVariables | SymbolFile::VariableTypes |
559 SymbolFile::LineTables;
560 }
561 }
562 return 0;
563 }
564
CalculateNumCompileUnits()565 uint32_t SymbolFileDWARFDebugMap::CalculateNumCompileUnits() {
566 InitOSO();
567 return m_compile_unit_infos.size();
568 }
569
ParseCompileUnitAtIndex(uint32_t cu_idx)570 CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) {
571 CompUnitSP comp_unit_sp;
572 const uint32_t cu_count = GetNumCompileUnits();
573
574 if (cu_idx < cu_count) {
575 Module *oso_module = GetModuleByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
576 if (oso_module) {
577 FileSpec so_file_spec;
578 if (GetFileSpecForSO(cu_idx, so_file_spec)) {
579 // User zero as the ID to match the compile unit at offset zero in each
580 // .o file since each .o file can only have one compile unit for now.
581 lldb::user_id_t cu_id = 0;
582 m_compile_unit_infos[cu_idx].compile_unit_sp =
583 std::make_shared<CompileUnit>(
584 m_objfile_sp->GetModule(), nullptr, so_file_spec, cu_id,
585 eLanguageTypeUnknown, eLazyBoolCalculate);
586
587 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
588 SetCompileUnitAtIndex(cu_idx,
589 m_compile_unit_infos[cu_idx].compile_unit_sp);
590 }
591 }
592 }
593 comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp;
594 }
595
596 return comp_unit_sp;
597 }
598
599 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompUnitInfo(const SymbolContext & sc)600 SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) {
601 return GetCompUnitInfo(*sc.comp_unit);
602 }
603
604 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompUnitInfo(const CompileUnit & comp_unit)605 SymbolFileDWARFDebugMap::GetCompUnitInfo(const CompileUnit &comp_unit) {
606 const uint32_t cu_count = GetNumCompileUnits();
607 for (uint32_t i = 0; i < cu_count; ++i) {
608 if (&comp_unit == m_compile_unit_infos[i].compile_unit_sp.get())
609 return &m_compile_unit_infos[i];
610 }
611 return nullptr;
612 }
613
GetCompUnitInfosForModule(const lldb_private::Module * module,std::vector<CompileUnitInfo * > & cu_infos)614 size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule(
615 const lldb_private::Module *module,
616 std::vector<CompileUnitInfo *> &cu_infos) {
617 const uint32_t cu_count = GetNumCompileUnits();
618 for (uint32_t i = 0; i < cu_count; ++i) {
619 if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i]))
620 cu_infos.push_back(&m_compile_unit_infos[i]);
621 }
622 return cu_infos.size();
623 }
624
625 lldb::LanguageType
ParseLanguage(CompileUnit & comp_unit)626 SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) {
627 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
628 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
629 if (oso_dwarf)
630 return oso_dwarf->ParseLanguage(comp_unit);
631 return eLanguageTypeUnknown;
632 }
633
ParseXcodeSDK(CompileUnit & comp_unit)634 XcodeSDK SymbolFileDWARFDebugMap::ParseXcodeSDK(CompileUnit &comp_unit) {
635 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
636 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
637 if (oso_dwarf)
638 return oso_dwarf->ParseXcodeSDK(comp_unit);
639 return {};
640 }
641
ParseFunctions(CompileUnit & comp_unit)642 size_t SymbolFileDWARFDebugMap::ParseFunctions(CompileUnit &comp_unit) {
643 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
644 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
645 if (oso_dwarf)
646 return oso_dwarf->ParseFunctions(comp_unit);
647 return 0;
648 }
649
ParseLineTable(CompileUnit & comp_unit)650 bool SymbolFileDWARFDebugMap::ParseLineTable(CompileUnit &comp_unit) {
651 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
652 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
653 if (oso_dwarf)
654 return oso_dwarf->ParseLineTable(comp_unit);
655 return false;
656 }
657
ParseDebugMacros(CompileUnit & comp_unit)658 bool SymbolFileDWARFDebugMap::ParseDebugMacros(CompileUnit &comp_unit) {
659 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
660 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
661 if (oso_dwarf)
662 return oso_dwarf->ParseDebugMacros(comp_unit);
663 return false;
664 }
665
ForEachExternalModule(CompileUnit & comp_unit,llvm::DenseSet<lldb_private::SymbolFile * > & visited_symbol_files,llvm::function_ref<bool (Module &)> f)666 bool SymbolFileDWARFDebugMap::ForEachExternalModule(
667 CompileUnit &comp_unit,
668 llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
669 llvm::function_ref<bool(Module &)> f) {
670 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
671 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
672 if (oso_dwarf)
673 return oso_dwarf->ForEachExternalModule(comp_unit, visited_symbol_files, f);
674 return false;
675 }
676
ParseSupportFiles(CompileUnit & comp_unit,FileSpecList & support_files)677 bool SymbolFileDWARFDebugMap::ParseSupportFiles(CompileUnit &comp_unit,
678 FileSpecList &support_files) {
679 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
680 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
681 if (oso_dwarf)
682 return oso_dwarf->ParseSupportFiles(comp_unit, support_files);
683 return false;
684 }
685
ParseIsOptimized(CompileUnit & comp_unit)686 bool SymbolFileDWARFDebugMap::ParseIsOptimized(CompileUnit &comp_unit) {
687 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
688 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
689 if (oso_dwarf)
690 return oso_dwarf->ParseIsOptimized(comp_unit);
691 return false;
692 }
693
ParseImportedModules(const SymbolContext & sc,std::vector<SourceModule> & imported_modules)694 bool SymbolFileDWARFDebugMap::ParseImportedModules(
695 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
696 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
697 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
698 if (oso_dwarf)
699 return oso_dwarf->ParseImportedModules(sc, imported_modules);
700 return false;
701 }
702
ParseBlocksRecursive(Function & func)703 size_t SymbolFileDWARFDebugMap::ParseBlocksRecursive(Function &func) {
704 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
705 CompileUnit *comp_unit = func.GetCompileUnit();
706 if (!comp_unit)
707 return 0;
708
709 SymbolFileDWARF *oso_dwarf = GetSymbolFile(*comp_unit);
710 if (oso_dwarf)
711 return oso_dwarf->ParseBlocksRecursive(func);
712 return 0;
713 }
714
ParseTypes(CompileUnit & comp_unit)715 size_t SymbolFileDWARFDebugMap::ParseTypes(CompileUnit &comp_unit) {
716 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
717 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
718 if (oso_dwarf)
719 return oso_dwarf->ParseTypes(comp_unit);
720 return 0;
721 }
722
723 size_t
ParseVariablesForContext(const SymbolContext & sc)724 SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) {
725 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
726 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
727 if (oso_dwarf)
728 return oso_dwarf->ParseVariablesForContext(sc);
729 return 0;
730 }
731
ResolveTypeUID(lldb::user_id_t type_uid)732 Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) {
733 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
734 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
735 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
736 if (oso_dwarf)
737 return oso_dwarf->ResolveTypeUID(type_uid);
738 return nullptr;
739 }
740
741 llvm::Optional<SymbolFile::ArrayInfo>
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)742 SymbolFileDWARFDebugMap::GetDynamicArrayInfoForUID(
743 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
744 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
745 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
746 if (oso_dwarf)
747 return oso_dwarf->GetDynamicArrayInfoForUID(type_uid, exe_ctx);
748 return llvm::None;
749 }
750
CompleteType(CompilerType & compiler_type)751 bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) {
752 bool success = false;
753 if (compiler_type) {
754 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
755 if (oso_dwarf->HasForwardDeclForClangType(compiler_type)) {
756 oso_dwarf->CompleteType(compiler_type);
757 success = true;
758 return true;
759 }
760 return false;
761 });
762 }
763 return success;
764 }
765
766 uint32_t
ResolveSymbolContext(const Address & exe_so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)767 SymbolFileDWARFDebugMap::ResolveSymbolContext(const Address &exe_so_addr,
768 SymbolContextItem resolve_scope,
769 SymbolContext &sc) {
770 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
771 uint32_t resolved_flags = 0;
772 Symtab *symtab = m_objfile_sp->GetSymtab();
773 if (symtab) {
774 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
775
776 const DebugMap::Entry *debug_map_entry =
777 m_debug_map.FindEntryThatContains(exe_file_addr);
778 if (debug_map_entry) {
779
780 sc.symbol =
781 symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
782
783 if (sc.symbol != nullptr) {
784 resolved_flags |= eSymbolContextSymbol;
785
786 uint32_t oso_idx = 0;
787 CompileUnitInfo *comp_unit_info =
788 GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx);
789 if (comp_unit_info) {
790 comp_unit_info->GetFileRangeMap(this);
791 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
792 if (oso_module) {
793 lldb::addr_t oso_file_addr =
794 exe_file_addr - debug_map_entry->GetRangeBase() +
795 debug_map_entry->data.GetOSOFileAddress();
796 Address oso_so_addr;
797 if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) {
798 resolved_flags |=
799 oso_module->GetSymbolFile()->ResolveSymbolContext(
800 oso_so_addr, resolve_scope, sc);
801 }
802 }
803 }
804 }
805 }
806 }
807 return resolved_flags;
808 }
809
ResolveSymbolContext(const SourceLocationSpec & src_location_spec,SymbolContextItem resolve_scope,SymbolContextList & sc_list)810 uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
811 const SourceLocationSpec &src_location_spec,
812 SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
813 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
814 const uint32_t initial = sc_list.GetSize();
815 const uint32_t cu_count = GetNumCompileUnits();
816
817 for (uint32_t i = 0; i < cu_count; ++i) {
818 // If we are checking for inlines, then we need to look through all compile
819 // units no matter if "file_spec" matches.
820 bool resolve = src_location_spec.GetCheckInlines();
821
822 if (!resolve) {
823 FileSpec so_file_spec;
824 if (GetFileSpecForSO(i, so_file_spec))
825 resolve =
826 FileSpec::Match(src_location_spec.GetFileSpec(), so_file_spec);
827 }
828 if (resolve) {
829 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i);
830 if (oso_dwarf)
831 oso_dwarf->ResolveSymbolContext(src_location_spec, resolve_scope,
832 sc_list);
833 }
834 }
835 return sc_list.GetSize() - initial;
836 }
837
PrivateFindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,const std::vector<uint32_t> & indexes,uint32_t max_matches,VariableList & variables)838 void SymbolFileDWARFDebugMap::PrivateFindGlobalVariables(
839 ConstString name, const CompilerDeclContext &parent_decl_ctx,
840 const std::vector<uint32_t>
841 &indexes, // Indexes into the symbol table that match "name"
842 uint32_t max_matches, VariableList &variables) {
843 const size_t match_count = indexes.size();
844 for (size_t i = 0; i < match_count; ++i) {
845 uint32_t oso_idx;
846 CompileUnitInfo *comp_unit_info =
847 GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx);
848 if (comp_unit_info) {
849 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
850 if (oso_dwarf) {
851 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
852 variables);
853 if (variables.GetSize() > max_matches)
854 break;
855 }
856 }
857 }
858 }
859
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,VariableList & variables)860 void SymbolFileDWARFDebugMap::FindGlobalVariables(
861 ConstString name, const CompilerDeclContext &parent_decl_ctx,
862 uint32_t max_matches, VariableList &variables) {
863 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
864 uint32_t total_matches = 0;
865
866 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
867 const uint32_t old_size = variables.GetSize();
868 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
869 variables);
870 const uint32_t oso_matches = variables.GetSize() - old_size;
871 if (oso_matches > 0) {
872 total_matches += oso_matches;
873
874 // Are we getting all matches?
875 if (max_matches == UINT32_MAX)
876 return false; // Yep, continue getting everything
877
878 // If we have found enough matches, lets get out
879 if (max_matches >= total_matches)
880 return true;
881
882 // Update the max matches for any subsequent calls to find globals in any
883 // other object files with DWARF
884 max_matches -= oso_matches;
885 }
886
887 return false;
888 });
889 }
890
FindGlobalVariables(const RegularExpression & regex,uint32_t max_matches,VariableList & variables)891 void SymbolFileDWARFDebugMap::FindGlobalVariables(
892 const RegularExpression ®ex, uint32_t max_matches,
893 VariableList &variables) {
894 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
895 uint32_t total_matches = 0;
896 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
897 const uint32_t old_size = variables.GetSize();
898 oso_dwarf->FindGlobalVariables(regex, max_matches, variables);
899
900 const uint32_t oso_matches = variables.GetSize() - old_size;
901 if (oso_matches > 0) {
902 total_matches += oso_matches;
903
904 // Are we getting all matches?
905 if (max_matches == UINT32_MAX)
906 return false; // Yep, continue getting everything
907
908 // If we have found enough matches, lets get out
909 if (max_matches >= total_matches)
910 return true;
911
912 // Update the max matches for any subsequent calls to find globals in any
913 // other object files with DWARF
914 max_matches -= oso_matches;
915 }
916
917 return false;
918 });
919 }
920
SymbolContainsSymbolWithIndex(uint32_t * symbol_idx_ptr,const CompileUnitInfo * comp_unit_info)921 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex(
922 uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
923 const uint32_t symbol_idx = *symbol_idx_ptr;
924
925 if (symbol_idx < comp_unit_info->first_symbol_index)
926 return -1;
927
928 if (symbol_idx <= comp_unit_info->last_symbol_index)
929 return 0;
930
931 return 1;
932 }
933
SymbolContainsSymbolWithID(user_id_t * symbol_idx_ptr,const CompileUnitInfo * comp_unit_info)934 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID(
935 user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
936 const user_id_t symbol_id = *symbol_idx_ptr;
937
938 if (symbol_id < comp_unit_info->first_symbol_id)
939 return -1;
940
941 if (symbol_id <= comp_unit_info->last_symbol_id)
942 return 0;
943
944 return 1;
945 }
946
947 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx,uint32_t * oso_idx_ptr)948 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex(
949 uint32_t symbol_idx, uint32_t *oso_idx_ptr) {
950 const uint32_t oso_index_count = m_compile_unit_infos.size();
951 CompileUnitInfo *comp_unit_info = nullptr;
952 if (oso_index_count) {
953 comp_unit_info = (CompileUnitInfo *)bsearch(
954 &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
955 sizeof(CompileUnitInfo),
956 (ComparisonFunction)SymbolContainsSymbolWithIndex);
957 }
958
959 if (oso_idx_ptr) {
960 if (comp_unit_info != nullptr)
961 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
962 else
963 *oso_idx_ptr = UINT32_MAX;
964 }
965 return comp_unit_info;
966 }
967
968 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfoForSymbolWithID(user_id_t symbol_id,uint32_t * oso_idx_ptr)969 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID(
970 user_id_t symbol_id, uint32_t *oso_idx_ptr) {
971 const uint32_t oso_index_count = m_compile_unit_infos.size();
972 CompileUnitInfo *comp_unit_info = nullptr;
973 if (oso_index_count) {
974 comp_unit_info = (CompileUnitInfo *)::bsearch(
975 &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
976 sizeof(CompileUnitInfo),
977 (ComparisonFunction)SymbolContainsSymbolWithID);
978 }
979
980 if (oso_idx_ptr) {
981 if (comp_unit_info != nullptr)
982 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
983 else
984 *oso_idx_ptr = UINT32_MAX;
985 }
986 return comp_unit_info;
987 }
988
RemoveFunctionsWithModuleNotEqualTo(const ModuleSP & module_sp,SymbolContextList & sc_list,uint32_t start_idx)989 static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp,
990 SymbolContextList &sc_list,
991 uint32_t start_idx) {
992 // We found functions in .o files. Not all functions in the .o files will
993 // have made it into the final output file. The ones that did make it into
994 // the final output file will have a section whose module matches the module
995 // from the ObjectFile for this SymbolFile. When the modules don't match,
996 // then we have something that was in a .o file, but doesn't map to anything
997 // in the final executable.
998 uint32_t i = start_idx;
999 while (i < sc_list.GetSize()) {
1000 SymbolContext sc;
1001 sc_list.GetContextAtIndex(i, sc);
1002 if (sc.function) {
1003 const SectionSP section_sp(
1004 sc.function->GetAddressRange().GetBaseAddress().GetSection());
1005 if (section_sp->GetModule() != module_sp) {
1006 sc_list.RemoveContextAtIndex(i);
1007 continue;
1008 }
1009 }
1010 ++i;
1011 }
1012 }
1013
FindFunctions(ConstString name,const CompilerDeclContext & parent_decl_ctx,FunctionNameType name_type_mask,bool include_inlines,SymbolContextList & sc_list)1014 void SymbolFileDWARFDebugMap::FindFunctions(
1015 ConstString name, const CompilerDeclContext &parent_decl_ctx,
1016 FunctionNameType name_type_mask, bool include_inlines,
1017 SymbolContextList &sc_list) {
1018 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1019 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
1020 name.GetCString());
1021
1022 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1023 uint32_t sc_idx = sc_list.GetSize();
1024 oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask,
1025 include_inlines, sc_list);
1026 if (!sc_list.IsEmpty()) {
1027 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
1028 sc_idx);
1029 }
1030 return false;
1031 });
1032 }
1033
FindFunctions(const RegularExpression & regex,bool include_inlines,SymbolContextList & sc_list)1034 void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression ®ex,
1035 bool include_inlines,
1036 SymbolContextList &sc_list) {
1037 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1038 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
1039 regex.GetText().str().c_str());
1040
1041 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1042 uint32_t sc_idx = sc_list.GetSize();
1043
1044 oso_dwarf->FindFunctions(regex, include_inlines, sc_list);
1045 if (!sc_list.IsEmpty()) {
1046 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
1047 sc_idx);
1048 }
1049 return false;
1050 });
1051 }
1052
GetTypes(SymbolContextScope * sc_scope,lldb::TypeClass type_mask,TypeList & type_list)1053 void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
1054 lldb::TypeClass type_mask,
1055 TypeList &type_list) {
1056 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1057 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
1058 type_mask);
1059
1060 SymbolFileDWARF *oso_dwarf = nullptr;
1061 if (sc_scope) {
1062 SymbolContext sc;
1063 sc_scope->CalculateSymbolContext(&sc);
1064
1065 CompileUnitInfo *cu_info = GetCompUnitInfo(sc);
1066 if (cu_info) {
1067 oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info);
1068 if (oso_dwarf)
1069 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1070 }
1071 } else {
1072 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1073 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1074 return false;
1075 });
1076 }
1077 }
1078
1079 std::vector<std::unique_ptr<lldb_private::CallEdge>>
ParseCallEdgesInFunction(UserID func_id)1080 SymbolFileDWARFDebugMap::ParseCallEdgesInFunction(UserID func_id) {
1081 uint32_t oso_idx = GetOSOIndexFromUserID(func_id.GetID());
1082 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1083 if (oso_dwarf)
1084 return oso_dwarf->ParseCallEdgesInFunction(func_id);
1085 return {};
1086 }
1087
FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext & die_decl_ctx)1088 TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext(
1089 const DWARFDeclContext &die_decl_ctx) {
1090 TypeSP type_sp;
1091 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1092 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1093 return ((bool)type_sp);
1094 });
1095 return type_sp;
1096 }
1097
Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF * skip_dwarf_oso)1098 bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type(
1099 SymbolFileDWARF *skip_dwarf_oso) {
1100 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
1101 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
1102 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1103 if (skip_dwarf_oso != oso_dwarf &&
1104 oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(nullptr)) {
1105 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
1106 return true;
1107 }
1108 return false;
1109 });
1110 }
1111 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
1112 }
1113
FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE & die,ConstString type_name,bool must_be_implementation)1114 TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE(
1115 const DWARFDIE &die, ConstString type_name,
1116 bool must_be_implementation) {
1117 // If we have a debug map, we will have an Objective-C symbol whose name is
1118 // the type name and whose type is eSymbolTypeObjCClass. If we can find that
1119 // symbol and find its containing parent, we can locate the .o file that will
1120 // contain the implementation definition since it will be scoped inside the
1121 // N_SO and we can then locate the SymbolFileDWARF that corresponds to that
1122 // N_SO.
1123 SymbolFileDWARF *oso_dwarf = nullptr;
1124 TypeSP type_sp;
1125 ObjectFile *module_objfile = m_objfile_sp->GetModule()->GetObjectFile();
1126 if (module_objfile) {
1127 Symtab *symtab = module_objfile->GetSymtab();
1128 if (symtab) {
1129 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
1130 type_name, eSymbolTypeObjCClass, Symtab::eDebugAny,
1131 Symtab::eVisibilityAny);
1132 if (objc_class_symbol) {
1133 // Get the N_SO symbol that contains the objective C class symbol as
1134 // this should be the .o file that contains the real definition...
1135 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
1136
1137 if (source_file_symbol &&
1138 source_file_symbol->GetType() == eSymbolTypeSourceFile) {
1139 const uint32_t source_file_symbol_idx =
1140 symtab->GetIndexForSymbol(source_file_symbol);
1141 if (source_file_symbol_idx != UINT32_MAX) {
1142 CompileUnitInfo *compile_unit_info =
1143 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx,
1144 nullptr);
1145 if (compile_unit_info) {
1146 oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info);
1147 if (oso_dwarf) {
1148 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1149 die, type_name, must_be_implementation));
1150 if (type_sp) {
1151 return type_sp;
1152 }
1153 }
1154 }
1155 }
1156 }
1157 }
1158 }
1159 }
1160
1161 // Only search all .o files for the definition if we don't need the
1162 // implementation because otherwise, with a valid debug map we should have
1163 // the ObjC class symbol and the code above should have found it.
1164 if (!must_be_implementation) {
1165 TypeSP type_sp;
1166
1167 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1168 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1169 die, type_name, must_be_implementation);
1170 return (bool)type_sp;
1171 });
1172
1173 return type_sp;
1174 }
1175 return TypeSP();
1176 }
1177
FindTypes(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,llvm::DenseSet<lldb_private::SymbolFile * > & searched_symbol_files,TypeMap & types)1178 void SymbolFileDWARFDebugMap::FindTypes(
1179 ConstString name, const CompilerDeclContext &parent_decl_ctx,
1180 uint32_t max_matches,
1181 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1182 TypeMap &types) {
1183 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1184 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1185 oso_dwarf->FindTypes(name, parent_decl_ctx, max_matches,
1186 searched_symbol_files, types);
1187 return types.GetSize() >= max_matches;
1188 });
1189 }
1190
FindTypes(llvm::ArrayRef<CompilerContext> context,LanguageSet languages,llvm::DenseSet<lldb_private::SymbolFile * > & searched_symbol_files,TypeMap & types)1191 void SymbolFileDWARFDebugMap::FindTypes(
1192 llvm::ArrayRef<CompilerContext> context, LanguageSet languages,
1193 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1194 TypeMap &types) {
1195 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1196 oso_dwarf->FindTypes(context, languages, searched_symbol_files, types);
1197 return false;
1198 });
1199 }
1200
1201 //
1202 // 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)
1206 //{
1207 // SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
1208 // if (oso_dwarf)
1209 // return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding,
1210 // udt_uid, types);
1211 // return 0;
1212 //}
1213
FindNamespace(lldb_private::ConstString name,const CompilerDeclContext & parent_decl_ctx)1214 CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
1215 lldb_private::ConstString name,
1216 const CompilerDeclContext &parent_decl_ctx) {
1217 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1218 CompilerDeclContext matching_namespace;
1219
1220 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1221 matching_namespace = oso_dwarf->FindNamespace(name, parent_decl_ctx);
1222
1223 return (bool)matching_namespace;
1224 });
1225
1226 return matching_namespace;
1227 }
1228
DumpClangAST(Stream & s)1229 void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
1230 ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool {
1231 oso_dwarf->DumpClangAST(s);
1232 // The underlying assumption is that DumpClangAST(...) will obtain the
1233 // AST from the underlying TypeSystem and therefore we only need to do
1234 // this once and can stop after the first iteration hence we return true.
1235 return true;
1236 });
1237 }
1238
1239 // PluginInterface protocol
GetPluginName()1240 lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginName() {
1241 return GetPluginNameStatic();
1242 }
1243
GetPluginVersion()1244 uint32_t SymbolFileDWARFDebugMap::GetPluginVersion() { return 1; }
1245
1246 lldb::CompUnitSP
GetCompileUnit(SymbolFileDWARF * oso_dwarf)1247 SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) {
1248 if (oso_dwarf) {
1249 const uint32_t cu_count = GetNumCompileUnits();
1250 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1251 SymbolFileDWARF *oso_symfile =
1252 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1253 if (oso_symfile == oso_dwarf) {
1254 if (!m_compile_unit_infos[cu_idx].compile_unit_sp)
1255 m_compile_unit_infos[cu_idx].compile_unit_sp =
1256 ParseCompileUnitAtIndex(cu_idx);
1257
1258 return m_compile_unit_infos[cu_idx].compile_unit_sp;
1259 }
1260 }
1261 }
1262 llvm_unreachable("this shouldn't happen");
1263 }
1264
1265 SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfo(SymbolFileDWARF * oso_dwarf)1266 SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) {
1267 if (oso_dwarf) {
1268 const uint32_t cu_count = GetNumCompileUnits();
1269 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1270 SymbolFileDWARF *oso_symfile =
1271 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1272 if (oso_symfile == oso_dwarf) {
1273 return &m_compile_unit_infos[cu_idx];
1274 }
1275 }
1276 }
1277 return nullptr;
1278 }
1279
SetCompileUnit(SymbolFileDWARF * oso_dwarf,const CompUnitSP & cu_sp)1280 void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
1281 const CompUnitSP &cu_sp) {
1282 if (oso_dwarf) {
1283 const uint32_t cu_count = GetNumCompileUnits();
1284 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1285 SymbolFileDWARF *oso_symfile =
1286 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1287 if (oso_symfile == oso_dwarf) {
1288 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
1289 assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() ==
1290 cu_sp.get());
1291 } else {
1292 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
1293 SetCompileUnitAtIndex(cu_idx, cu_sp);
1294 }
1295 }
1296 }
1297 }
1298 }
1299
1300 CompilerDeclContext
GetDeclContextForUID(lldb::user_id_t type_uid)1301 SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
1302 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1303 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1304 if (oso_dwarf)
1305 return oso_dwarf->GetDeclContextForUID(type_uid);
1306 return CompilerDeclContext();
1307 }
1308
1309 CompilerDeclContext
GetDeclContextContainingUID(lldb::user_id_t type_uid)1310 SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1311 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1312 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1313 if (oso_dwarf)
1314 return oso_dwarf->GetDeclContextContainingUID(type_uid);
1315 return CompilerDeclContext();
1316 }
1317
ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx)1318 void SymbolFileDWARFDebugMap::ParseDeclsForContext(
1319 lldb_private::CompilerDeclContext decl_ctx) {
1320 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1321 oso_dwarf->ParseDeclsForContext(decl_ctx);
1322 return true; // Keep iterating
1323 });
1324 }
1325
AddOSOFileRange(CompileUnitInfo * cu_info,lldb::addr_t exe_file_addr,lldb::addr_t exe_byte_size,lldb::addr_t oso_file_addr,lldb::addr_t oso_byte_size)1326 bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info,
1327 lldb::addr_t exe_file_addr,
1328 lldb::addr_t exe_byte_size,
1329 lldb::addr_t oso_file_addr,
1330 lldb::addr_t oso_byte_size) {
1331 const uint32_t debug_map_idx =
1332 m_debug_map.FindEntryIndexThatContains(exe_file_addr);
1333 if (debug_map_idx != UINT32_MAX) {
1334 DebugMap::Entry *debug_map_entry =
1335 m_debug_map.FindEntryThatContains(exe_file_addr);
1336 debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
1337 addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size);
1338 if (range_size == 0) {
1339 range_size = std::max<addr_t>(exe_byte_size, oso_byte_size);
1340 if (range_size == 0)
1341 range_size = 1;
1342 }
1343 cu_info->file_range_map.Append(
1344 FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr));
1345 return true;
1346 }
1347 return false;
1348 }
1349
FinalizeOSOFileRanges(CompileUnitInfo * cu_info)1350 void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) {
1351 cu_info->file_range_map.Sort();
1352 #if defined(DEBUG_OSO_DMAP)
1353 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
1354 const size_t n = oso_file_range_map.GetSize();
1355 printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
1356 cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
1357 for (size_t i = 0; i < n; ++i) {
1358 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
1359 printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
1360 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
1361 entry.GetRangeBase(), entry.GetRangeEnd(), entry.data,
1362 entry.data + entry.GetByteSize());
1363 }
1364 #endif
1365 }
1366
1367 lldb::addr_t
LinkOSOFileAddress(SymbolFileDWARF * oso_symfile,lldb::addr_t oso_file_addr)1368 SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
1369 lldb::addr_t oso_file_addr) {
1370 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile);
1371 if (cu_info) {
1372 const FileRangeMap::Entry *oso_range_entry =
1373 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1374 if (oso_range_entry) {
1375 const DebugMap::Entry *debug_map_entry =
1376 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1377 if (debug_map_entry) {
1378 const lldb::addr_t offset =
1379 oso_file_addr - oso_range_entry->GetRangeBase();
1380 const lldb::addr_t exe_file_addr =
1381 debug_map_entry->GetRangeBase() + offset;
1382 return exe_file_addr;
1383 }
1384 }
1385 }
1386 return LLDB_INVALID_ADDRESS;
1387 }
1388
LinkOSOAddress(Address & addr)1389 bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) {
1390 // Make sure this address hasn't been fixed already
1391 Module *exe_module = GetObjectFile()->GetModule().get();
1392 Module *addr_module = addr.GetModule().get();
1393 if (addr_module == exe_module)
1394 return true; // Address is already in terms of the main executable module
1395
1396 CompileUnitInfo *cu_info = GetCompileUnitInfo(
1397 GetSymbolFileAsSymbolFileDWARF(addr_module->GetSymbolFile()));
1398 if (cu_info) {
1399 const lldb::addr_t oso_file_addr = addr.GetFileAddress();
1400 const FileRangeMap::Entry *oso_range_entry =
1401 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1402 if (oso_range_entry) {
1403 const DebugMap::Entry *debug_map_entry =
1404 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1405 if (debug_map_entry) {
1406 const lldb::addr_t offset =
1407 oso_file_addr - oso_range_entry->GetRangeBase();
1408 const lldb::addr_t exe_file_addr =
1409 debug_map_entry->GetRangeBase() + offset;
1410 return exe_module->ResolveFileAddress(exe_file_addr, addr);
1411 }
1412 }
1413 }
1414 return true;
1415 }
1416
LinkOSOLineTable(SymbolFileDWARF * oso_dwarf,LineTable * line_table)1417 LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf,
1418 LineTable *line_table) {
1419 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf);
1420 if (cu_info)
1421 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
1422 return nullptr;
1423 }
1424
1425 size_t
AddOSOARanges(SymbolFileDWARF * dwarf2Data,DWARFDebugAranges * debug_aranges)1426 SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data,
1427 DWARFDebugAranges *debug_aranges) {
1428 size_t num_line_entries_added = 0;
1429 if (debug_aranges && dwarf2Data) {
1430 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
1431 if (compile_unit_info) {
1432 const FileRangeMap &file_range_map =
1433 compile_unit_info->GetFileRangeMap(this);
1434 for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) {
1435 const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx);
1436 if (entry) {
1437 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(),
1438 entry->GetRangeEnd());
1439 num_line_entries_added++;
1440 }
1441 }
1442 }
1443 }
1444 return num_line_entries_added;
1445 }
1446