1 //===-- SymbolFileDWARFDebugMap.cpp -----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "SymbolFileDWARFDebugMap.h"
10 #include "DWARFDebugAranges.h"
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 #include "lldb/Utility/Timer.h"
21 //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
22 #if defined(DEBUG_OSO_DMAP)
23 #include "lldb/Core/StreamFile.h"
26 #include "lldb/Symbol/CompileUnit.h"
27 #include "lldb/Symbol/LineTable.h"
28 #include "lldb/Symbol/ObjectFile.h"
29 #include "lldb/Symbol/SymbolVendor.h"
30 #include "lldb/Symbol/TypeMap.h"
31 #include "lldb/Symbol/VariableList.h"
32 #include "llvm/Support/ScopedPrinter.h"
34 #include "LogChannelDWARF.h"
35 #include "SymbolFileDWARF.h"
40 using namespace lldb_private;
42 char SymbolFileDWARFDebugMap::ID;
44 // Subclass lldb_private::Module so we can intercept the
45 // "Module::GetObjectFile()" (so we can fixup the object file sections) and
46 // also for "Module::GetSymbolFile()" (so we can fixup the symbol file id.
48 const SymbolFileDWARFDebugMap::FileRangeMap &
49 SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(
50 SymbolFileDWARFDebugMap *exe_symfile) {
51 if (file_range_map_valid)
52 return file_range_map;
54 file_range_map_valid = true;
56 Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this);
58 return file_range_map;
60 ObjectFile *oso_objfile = oso_module->GetObjectFile();
62 return file_range_map;
64 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
67 "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
68 static_cast<void *>(this),
69 oso_module->GetSpecificationDescription().c_str());
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();
78 /// const uint32_t fun_resolve_flags = SymbolContext::Module |
79 /// eSymbolContextCompUnit | eSymbolContextFunction;
80 // SectionList *oso_sections = oso_objfile->Sections();
81 // Now we need to make sections that map from zero based object file
82 // addresses to where things ended up in the main executable.
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
89 idx < oso_end_idx; ++idx) {
90 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
92 if (!exe_symbol->IsDebug())
95 switch (exe_symbol->GetType()) {
99 case eSymbolTypeCode: {
100 // 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
103 // 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.
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());
124 case eSymbolTypeData: {
125 // 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
132 // 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.
138 // Next we find the non-stab entry that corresponds to the N_GSYM
140 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());
159 exe_symfile->FinalizeOSOFileRanges(this);
160 // We don't need the symbols anymore for the .o files
161 oso_objfile->ClearSymtab();
164 return file_range_map;
167 class DebugMapModule : public Module {
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) {}
176 ~DebugMapModule() override = default;
179 GetSymbolFile(bool can_create = true,
180 lldb_private::Stream *feedback_strm = nullptr) override {
182 if (m_symfile_up.get() || !can_create)
183 return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
185 ModuleSP exe_module_sp(m_exe_module_wp.lock());
187 // Now get the object file outside of a locking scope
188 ObjectFile *oso_objfile = GetObjectFile();
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);
202 ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
203 SymbolFile *exe_symfile = exe_module_sp->GetSymbolFile();
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);
220 ModuleWP m_exe_module_wp;
221 const uint32_t m_cu_idx;
224 void SymbolFileDWARFDebugMap::Initialize() {
225 PluginManager::RegisterPlugin(GetPluginNameStatic(),
226 GetPluginDescriptionStatic(), CreateInstance);
229 void SymbolFileDWARFDebugMap::Terminate() {
230 PluginManager::UnregisterPlugin(CreateInstance);
233 lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginNameStatic() {
234 static ConstString g_name("dwarf-debugmap");
238 const char *SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
239 return "DWARF and DWARF3 debug symbol file reader (debug map).";
242 SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFileSP objfile_sp) {
243 return new SymbolFileDWARFDebugMap(std::move(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) {}
251 SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() {}
253 void SymbolFileDWARFDebugMap::InitializeObject() {}
255 void SymbolFileDWARFDebugMap::InitOSO() {
256 if (m_flags.test(kHaveInitializedOSOs))
259 m_flags.set(kHaveInitializedOSOs);
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())
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:
278 case ObjectFile::eTypeExecutable:
279 case ObjectFile::eTypeDynamicLinker:
280 case ObjectFile::eTypeSharedLibrary:
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.
289 Symtab *symtab = m_objfile_sp->GetSymtab();
291 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_MAP));
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.
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
301 const uint32_t k_oso_symbol_flags_value = 0x660001u;
303 const uint32_t oso_index_count =
304 symtab->AppendSymbolIndexesWithTypeAndFlagsValue(
305 eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
307 if (oso_index_count > 0) {
308 symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes,
309 Symtab::eVisibilityAny,
311 symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes,
312 Symtab::eVisibilityAny,
315 symtab->SortSymbolIndexesByValue(m_func_indexes, true);
316 symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
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);
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);
336 m_compile_unit_infos.resize(oso_index_count);
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
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",
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();
366 LLDB_LOGF(log, "Initialized OSO 0x%8.8x: file=%s", i,
367 oso_symbol->GetName().GetCString());
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",
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",
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);
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]);
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;
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 // The modification time returned by the FS can have a higher precision
420 // than the one from the CU.
421 auto oso_mod_time = std::chrono::time_point_cast<std::chrono::seconds>(
422 FileSystem::Instance().GetModificationTime(oso_file));
423 // A timestamp of 0 means that the linker was in deterministic mode. In
424 // that case, we should skip the check against the filesystem last
425 // modification timestamp, since it will never match.
426 if (comp_unit_info->oso_mod_time != llvm::sys::TimePoint<>() &&
427 oso_mod_time != comp_unit_info->oso_mod_time) {
428 obj_file->GetModule()->ReportError(
429 "debug map object file '%s' has changed (actual time is "
430 "%s, debug map time is %s"
431 ") since this executable was linked, file will be ignored",
432 oso_file.GetPath().c_str(), llvm::to_string(oso_mod_time).c_str(),
433 llvm::to_string(comp_unit_info->oso_mod_time).c_str());
438 const bool must_exist = true;
440 if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file,
441 oso_object, must_exist)) {
445 // Always create a new module for .o files. Why? Because we use the debug
446 // map, to add new sections to each .o file and even though a .o file
447 // might not have changed, the sections that get added to the .o file can
450 // Only adopt the architecture from the module (not the vendor or OS)
451 // since .o files for "i386-apple-ios" will historically show up as "i386
452 // -apple-macosx" due to the lack of a LC_VERSION_MIN_MACOSX or
453 // LC_VERSION_MIN_IPHONEOS load command...
454 oso_arch.SetTriple(m_objfile_sp->GetModule()
460 comp_unit_info->oso_sp->module_sp = std::make_shared<DebugMapModule>(
461 obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file,
462 oso_arch, oso_object ? &oso_object : nullptr, 0,
463 oso_object ? comp_unit_info->oso_mod_time : llvm::sys::TimePoint<>());
466 if (comp_unit_info->oso_sp)
467 return comp_unit_info->oso_sp->module_sp.get();
471 bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx,
472 FileSpec &file_spec) {
473 if (oso_idx < m_compile_unit_infos.size()) {
474 if (m_compile_unit_infos[oso_idx].so_file) {
475 file_spec = m_compile_unit_infos[oso_idx].so_file;
482 ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) {
483 Module *oso_module = GetModuleByOSOIndex(oso_idx);
485 return oso_module->GetObjectFile();
490 SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) {
491 return GetSymbolFile(*sc.comp_unit);
495 SymbolFileDWARFDebugMap::GetSymbolFile(const CompileUnit &comp_unit) {
496 CompileUnitInfo *comp_unit_info = GetCompUnitInfo(comp_unit);
498 return GetSymbolFileByCompUnitInfo(comp_unit_info);
502 ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo(
503 CompileUnitInfo *comp_unit_info) {
504 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
506 return oso_module->GetObjectFile();
510 uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex(
511 const CompileUnitInfo *comp_unit_info) {
512 if (!m_compile_unit_infos.empty()) {
513 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
514 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
515 if (first_comp_unit_info <= comp_unit_info &&
516 comp_unit_info <= last_comp_unit_info)
517 return comp_unit_info - first_comp_unit_info;
523 SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) {
524 unsigned size = m_compile_unit_infos.size();
526 return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
531 SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) {
533 sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
534 return (SymbolFileDWARF *)sym_file;
538 SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo(
539 CompileUnitInfo *comp_unit_info) {
540 if (Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info))
541 return GetSymbolFileAsSymbolFileDWARF(oso_module->GetSymbolFile());
545 uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() {
546 // In order to get the abilities of this plug-in, we look at the list of
547 // N_OSO entries (object files) from the symbol table and make sure that
548 // these files exist and also contain valid DWARF. If we get any of that then
549 // we return the abilities of the first N_OSO's DWARF.
551 const uint32_t oso_index_count = GetNumCompileUnits();
552 if (oso_index_count > 0) {
554 if (!m_compile_unit_infos.empty()) {
555 return SymbolFile::CompileUnits | SymbolFile::Functions |
556 SymbolFile::Blocks | SymbolFile::GlobalVariables |
557 SymbolFile::LocalVariables | SymbolFile::VariableTypes |
558 SymbolFile::LineTables;
564 uint32_t SymbolFileDWARFDebugMap::CalculateNumCompileUnits() {
566 return m_compile_unit_infos.size();
569 CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) {
570 CompUnitSP comp_unit_sp;
571 const uint32_t cu_count = GetNumCompileUnits();
573 if (cu_idx < cu_count) {
574 Module *oso_module = GetModuleByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
576 FileSpec so_file_spec;
577 if (GetFileSpecForSO(cu_idx, so_file_spec)) {
578 // User zero as the ID to match the compile unit at offset zero in each
579 // .o file since each .o file can only have one compile unit for now.
580 lldb::user_id_t cu_id = 0;
581 m_compile_unit_infos[cu_idx].compile_unit_sp =
582 std::make_shared<CompileUnit>(
583 m_objfile_sp->GetModule(), nullptr, so_file_spec, cu_id,
584 eLanguageTypeUnknown, eLazyBoolCalculate);
586 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
587 SetCompileUnitAtIndex(cu_idx,
588 m_compile_unit_infos[cu_idx].compile_unit_sp);
592 comp_unit_sp = m_compile_unit_infos[cu_idx].compile_unit_sp;
598 SymbolFileDWARFDebugMap::CompileUnitInfo *
599 SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) {
600 return GetCompUnitInfo(*sc.comp_unit);
603 SymbolFileDWARFDebugMap::CompileUnitInfo *
604 SymbolFileDWARFDebugMap::GetCompUnitInfo(const CompileUnit &comp_unit) {
605 const uint32_t cu_count = GetNumCompileUnits();
606 for (uint32_t i = 0; i < cu_count; ++i) {
607 if (&comp_unit == m_compile_unit_infos[i].compile_unit_sp.get())
608 return &m_compile_unit_infos[i];
613 size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule(
614 const lldb_private::Module *module,
615 std::vector<CompileUnitInfo *> &cu_infos) {
616 const uint32_t cu_count = GetNumCompileUnits();
617 for (uint32_t i = 0; i < cu_count; ++i) {
618 if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i]))
619 cu_infos.push_back(&m_compile_unit_infos[i]);
621 return cu_infos.size();
625 SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) {
626 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
627 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
629 return oso_dwarf->ParseLanguage(comp_unit);
630 return eLanguageTypeUnknown;
633 size_t SymbolFileDWARFDebugMap::ParseFunctions(CompileUnit &comp_unit) {
634 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
635 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
637 return oso_dwarf->ParseFunctions(comp_unit);
641 bool SymbolFileDWARFDebugMap::ParseLineTable(CompileUnit &comp_unit) {
642 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
643 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
645 return oso_dwarf->ParseLineTable(comp_unit);
649 bool SymbolFileDWARFDebugMap::ParseDebugMacros(CompileUnit &comp_unit) {
650 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
651 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
653 return oso_dwarf->ParseDebugMacros(comp_unit);
657 bool SymbolFileDWARFDebugMap::ForEachExternalModule(
658 CompileUnit &comp_unit,
659 llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
660 llvm::function_ref<bool(Module &)> f) {
661 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
662 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
664 return oso_dwarf->ForEachExternalModule(comp_unit, visited_symbol_files, f);
668 bool SymbolFileDWARFDebugMap::ParseSupportFiles(CompileUnit &comp_unit,
669 FileSpecList &support_files) {
670 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
671 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
673 return oso_dwarf->ParseSupportFiles(comp_unit, support_files);
677 bool SymbolFileDWARFDebugMap::ParseIsOptimized(CompileUnit &comp_unit) {
678 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
679 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
681 return oso_dwarf->ParseIsOptimized(comp_unit);
685 bool SymbolFileDWARFDebugMap::ParseImportedModules(
686 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
687 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
688 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
690 return oso_dwarf->ParseImportedModules(sc, imported_modules);
694 size_t SymbolFileDWARFDebugMap::ParseBlocksRecursive(Function &func) {
695 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
696 CompileUnit *comp_unit = func.GetCompileUnit();
700 SymbolFileDWARF *oso_dwarf = GetSymbolFile(*comp_unit);
702 return oso_dwarf->ParseBlocksRecursive(func);
706 size_t SymbolFileDWARFDebugMap::ParseTypes(CompileUnit &comp_unit) {
707 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
708 SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
710 return oso_dwarf->ParseTypes(comp_unit);
715 SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) {
716 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
717 SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
719 return oso_dwarf->ParseVariablesForContext(sc);
723 Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) {
724 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
725 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
726 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
728 return oso_dwarf->ResolveTypeUID(type_uid);
732 llvm::Optional<SymbolFile::ArrayInfo>
733 SymbolFileDWARFDebugMap::GetDynamicArrayInfoForUID(
734 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
735 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
736 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
738 return oso_dwarf->GetDynamicArrayInfoForUID(type_uid, exe_ctx);
742 bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) {
743 bool success = false;
745 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
746 if (oso_dwarf->HasForwardDeclForClangType(compiler_type)) {
747 oso_dwarf->CompleteType(compiler_type);
758 SymbolFileDWARFDebugMap::ResolveSymbolContext(const Address &exe_so_addr,
759 SymbolContextItem resolve_scope,
761 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
762 uint32_t resolved_flags = 0;
763 Symtab *symtab = m_objfile_sp->GetSymtab();
765 const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
767 const DebugMap::Entry *debug_map_entry =
768 m_debug_map.FindEntryThatContains(exe_file_addr);
769 if (debug_map_entry) {
772 symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
774 if (sc.symbol != nullptr) {
775 resolved_flags |= eSymbolContextSymbol;
777 uint32_t oso_idx = 0;
778 CompileUnitInfo *comp_unit_info =
779 GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx);
780 if (comp_unit_info) {
781 comp_unit_info->GetFileRangeMap(this);
782 Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
784 lldb::addr_t oso_file_addr =
785 exe_file_addr - debug_map_entry->GetRangeBase() +
786 debug_map_entry->data.GetOSOFileAddress();
788 if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) {
790 oso_module->GetSymbolFile()->ResolveSymbolContext(
791 oso_so_addr, resolve_scope, sc);
798 return resolved_flags;
801 uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
802 const FileSpec &file_spec, uint32_t line, bool check_inlines,
803 SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
804 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
805 const uint32_t initial = sc_list.GetSize();
806 const uint32_t cu_count = GetNumCompileUnits();
808 for (uint32_t i = 0; i < cu_count; ++i) {
809 // If we are checking for inlines, then we need to look through all compile
810 // units no matter if "file_spec" matches.
811 bool resolve = check_inlines;
814 FileSpec so_file_spec;
815 if (GetFileSpecForSO(i, so_file_spec))
816 resolve = FileSpec::Match(file_spec, so_file_spec);
819 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i);
821 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines,
822 resolve_scope, sc_list);
825 return sc_list.GetSize() - initial;
828 void SymbolFileDWARFDebugMap::PrivateFindGlobalVariables(
829 ConstString name, const CompilerDeclContext *parent_decl_ctx,
830 const std::vector<uint32_t>
831 &indexes, // Indexes into the symbol table that match "name"
832 uint32_t max_matches, VariableList &variables) {
833 const size_t match_count = indexes.size();
834 for (size_t i = 0; i < match_count; ++i) {
836 CompileUnitInfo *comp_unit_info =
837 GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx);
838 if (comp_unit_info) {
839 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
841 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
843 if (variables.GetSize() > max_matches)
850 void SymbolFileDWARFDebugMap::FindGlobalVariables(
851 ConstString name, const CompilerDeclContext *parent_decl_ctx,
852 uint32_t max_matches, VariableList &variables) {
853 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
854 uint32_t total_matches = 0;
856 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
857 const uint32_t old_size = variables.GetSize();
858 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
860 const uint32_t oso_matches = variables.GetSize() - old_size;
861 if (oso_matches > 0) {
862 total_matches += oso_matches;
864 // Are we getting all matches?
865 if (max_matches == UINT32_MAX)
866 return false; // Yep, continue getting everything
868 // If we have found enough matches, lets get out
869 if (max_matches >= total_matches)
872 // Update the max matches for any subsequent calls to find globals in any
873 // other object files with DWARF
874 max_matches -= oso_matches;
881 void SymbolFileDWARFDebugMap::FindGlobalVariables(
882 const RegularExpression ®ex, uint32_t max_matches,
883 VariableList &variables) {
884 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
885 uint32_t total_matches = 0;
886 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
887 const uint32_t old_size = variables.GetSize();
888 oso_dwarf->FindGlobalVariables(regex, max_matches, variables);
890 const uint32_t oso_matches = variables.GetSize() - old_size;
891 if (oso_matches > 0) {
892 total_matches += oso_matches;
894 // Are we getting all matches?
895 if (max_matches == UINT32_MAX)
896 return false; // Yep, continue getting everything
898 // If we have found enough matches, lets get out
899 if (max_matches >= total_matches)
902 // Update the max matches for any subsequent calls to find globals in any
903 // other object files with DWARF
904 max_matches -= oso_matches;
911 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex(
912 uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
913 const uint32_t symbol_idx = *symbol_idx_ptr;
915 if (symbol_idx < comp_unit_info->first_symbol_index)
918 if (symbol_idx <= comp_unit_info->last_symbol_index)
924 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID(
925 user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
926 const user_id_t symbol_id = *symbol_idx_ptr;
928 if (symbol_id < comp_unit_info->first_symbol_id)
931 if (symbol_id <= comp_unit_info->last_symbol_id)
937 SymbolFileDWARFDebugMap::CompileUnitInfo *
938 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex(
939 uint32_t symbol_idx, uint32_t *oso_idx_ptr) {
940 const uint32_t oso_index_count = m_compile_unit_infos.size();
941 CompileUnitInfo *comp_unit_info = nullptr;
942 if (oso_index_count) {
943 comp_unit_info = (CompileUnitInfo *)bsearch(
944 &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
945 sizeof(CompileUnitInfo),
946 (ComparisonFunction)SymbolContainsSymbolWithIndex);
950 if (comp_unit_info != nullptr)
951 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
953 *oso_idx_ptr = UINT32_MAX;
955 return comp_unit_info;
958 SymbolFileDWARFDebugMap::CompileUnitInfo *
959 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID(
960 user_id_t symbol_id, uint32_t *oso_idx_ptr) {
961 const uint32_t oso_index_count = m_compile_unit_infos.size();
962 CompileUnitInfo *comp_unit_info = nullptr;
963 if (oso_index_count) {
964 comp_unit_info = (CompileUnitInfo *)::bsearch(
965 &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
966 sizeof(CompileUnitInfo),
967 (ComparisonFunction)SymbolContainsSymbolWithID);
971 if (comp_unit_info != nullptr)
972 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
974 *oso_idx_ptr = UINT32_MAX;
976 return comp_unit_info;
979 static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp,
980 SymbolContextList &sc_list,
981 uint32_t start_idx) {
982 // We found functions in .o files. Not all functions in the .o files will
983 // have made it into the final output file. The ones that did make it into
984 // the final output file will have a section whose module matches the module
985 // from the ObjectFile for this SymbolFile. When the modules don't match,
986 // then we have something that was in a .o file, but doesn't map to anything
987 // in the final executable.
988 uint32_t i = start_idx;
989 while (i < sc_list.GetSize()) {
991 sc_list.GetContextAtIndex(i, sc);
993 const SectionSP section_sp(
994 sc.function->GetAddressRange().GetBaseAddress().GetSection());
995 if (section_sp->GetModule() != module_sp) {
996 sc_list.RemoveContextAtIndex(i);
1004 void SymbolFileDWARFDebugMap::FindFunctions(
1005 ConstString name, const CompilerDeclContext *parent_decl_ctx,
1006 FunctionNameType name_type_mask, bool include_inlines,
1007 SymbolContextList &sc_list) {
1008 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1009 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1010 Timer scoped_timer(func_cat,
1011 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
1014 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1015 uint32_t sc_idx = sc_list.GetSize();
1016 oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask,
1017 include_inlines, sc_list);
1018 if (!sc_list.IsEmpty()) {
1019 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
1026 void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression ®ex,
1027 bool include_inlines,
1028 SymbolContextList &sc_list) {
1029 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1030 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1031 Timer scoped_timer(func_cat,
1032 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
1033 regex.GetText().str().c_str());
1035 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1036 uint32_t sc_idx = sc_list.GetSize();
1038 oso_dwarf->FindFunctions(regex, include_inlines, sc_list);
1039 if (!sc_list.IsEmpty()) {
1040 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
1047 void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
1048 lldb::TypeClass type_mask,
1049 TypeList &type_list) {
1050 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1051 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1052 Timer scoped_timer(func_cat,
1053 "SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
1056 SymbolFileDWARF *oso_dwarf = nullptr;
1059 sc_scope->CalculateSymbolContext(&sc);
1061 CompileUnitInfo *cu_info = GetCompUnitInfo(sc);
1063 oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info);
1065 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1068 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1069 oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
1075 std::vector<std::unique_ptr<lldb_private::CallEdge>>
1076 SymbolFileDWARFDebugMap::ParseCallEdgesInFunction(UserID func_id) {
1077 uint32_t oso_idx = GetOSOIndexFromUserID(func_id.GetID());
1078 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1080 return oso_dwarf->ParseCallEdgesInFunction(func_id);
1084 TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext(
1085 const DWARFDeclContext &die_decl_ctx) {
1087 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1088 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1089 return ((bool)type_sp);
1094 bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type(
1095 SymbolFileDWARF *skip_dwarf_oso) {
1096 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
1097 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
1098 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1099 if (skip_dwarf_oso != oso_dwarf &&
1100 oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(nullptr)) {
1101 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
1107 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
1110 TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE(
1111 const DWARFDIE &die, ConstString type_name,
1112 bool must_be_implementation) {
1113 // If we have a debug map, we will have an Objective-C symbol whose name is
1114 // the type name and whose type is eSymbolTypeObjCClass. If we can find that
1115 // symbol and find its containing parent, we can locate the .o file that will
1116 // contain the implementation definition since it will be scoped inside the
1117 // N_SO and we can then locate the SymbolFileDWARF that corresponds to that
1119 SymbolFileDWARF *oso_dwarf = nullptr;
1121 ObjectFile *module_objfile = m_objfile_sp->GetModule()->GetObjectFile();
1122 if (module_objfile) {
1123 Symtab *symtab = module_objfile->GetSymtab();
1125 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
1126 type_name, eSymbolTypeObjCClass, Symtab::eDebugAny,
1127 Symtab::eVisibilityAny);
1128 if (objc_class_symbol) {
1129 // Get the N_SO symbol that contains the objective C class symbol as
1130 // this should be the .o file that contains the real definition...
1131 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
1133 if (source_file_symbol &&
1134 source_file_symbol->GetType() == eSymbolTypeSourceFile) {
1135 const uint32_t source_file_symbol_idx =
1136 symtab->GetIndexForSymbol(source_file_symbol);
1137 if (source_file_symbol_idx != UINT32_MAX) {
1138 CompileUnitInfo *compile_unit_info =
1139 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx,
1141 if (compile_unit_info) {
1142 oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info);
1144 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1145 die, type_name, must_be_implementation));
1157 // Only search all .o files for the definition if we don't need the
1158 // implementation because otherwise, with a valid debug map we should have
1159 // the ObjC class symbol and the code above should have found it.
1160 if (!must_be_implementation) {
1163 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1164 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
1165 die, type_name, must_be_implementation);
1166 return (bool)type_sp;
1174 void SymbolFileDWARFDebugMap::FindTypes(
1175 ConstString name, const CompilerDeclContext *parent_decl_ctx,
1176 uint32_t max_matches,
1177 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1179 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1180 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1181 oso_dwarf->FindTypes(name, parent_decl_ctx, max_matches,
1182 searched_symbol_files, types);
1183 return types.GetSize() >= max_matches;
1187 void SymbolFileDWARFDebugMap::FindTypes(
1188 llvm::ArrayRef<CompilerContext> context, LanguageSet languages,
1189 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1191 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1192 oso_dwarf->FindTypes(context, languages, searched_symbol_files, types);
1199 // SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const
1200 // RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding
1201 // encoding, lldb::user_id_t udt_uid, TypeList& types)
1203 // SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc);
1205 // return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding,
1210 CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
1211 lldb_private::ConstString name,
1212 const CompilerDeclContext *parent_decl_ctx) {
1213 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1214 CompilerDeclContext matching_namespace;
1216 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1217 matching_namespace = oso_dwarf->FindNamespace(name, parent_decl_ctx);
1219 return (bool)matching_namespace;
1222 return matching_namespace;
1225 void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
1226 ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool {
1227 oso_dwarf->DumpClangAST(s);
1232 // PluginInterface protocol
1233 lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginName() {
1234 return GetPluginNameStatic();
1237 uint32_t SymbolFileDWARFDebugMap::GetPluginVersion() { return 1; }
1240 SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) {
1242 const uint32_t cu_count = GetNumCompileUnits();
1243 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1244 SymbolFileDWARF *oso_symfile =
1245 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1246 if (oso_symfile == oso_dwarf) {
1247 if (!m_compile_unit_infos[cu_idx].compile_unit_sp)
1248 m_compile_unit_infos[cu_idx].compile_unit_sp =
1249 ParseCompileUnitAtIndex(cu_idx);
1251 return m_compile_unit_infos[cu_idx].compile_unit_sp;
1255 llvm_unreachable("this shouldn't happen");
1258 SymbolFileDWARFDebugMap::CompileUnitInfo *
1259 SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) {
1261 const uint32_t cu_count = GetNumCompileUnits();
1262 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1263 SymbolFileDWARF *oso_symfile =
1264 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1265 if (oso_symfile == oso_dwarf) {
1266 return &m_compile_unit_infos[cu_idx];
1273 void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
1274 const CompUnitSP &cu_sp) {
1276 const uint32_t cu_count = GetNumCompileUnits();
1277 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1278 SymbolFileDWARF *oso_symfile =
1279 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
1280 if (oso_symfile == oso_dwarf) {
1281 if (m_compile_unit_infos[cu_idx].compile_unit_sp) {
1282 assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() ==
1285 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp;
1286 SetCompileUnitAtIndex(cu_idx, cu_sp);
1294 SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
1295 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1296 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1298 return oso_dwarf->GetDeclContextForUID(type_uid);
1299 return CompilerDeclContext();
1303 SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
1304 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1305 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
1307 return oso_dwarf->GetDeclContextContainingUID(type_uid);
1308 return CompilerDeclContext();
1311 void SymbolFileDWARFDebugMap::ParseDeclsForContext(
1312 lldb_private::CompilerDeclContext decl_ctx) {
1313 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1314 oso_dwarf->ParseDeclsForContext(decl_ctx);
1315 return true; // Keep iterating
1319 bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info,
1320 lldb::addr_t exe_file_addr,
1321 lldb::addr_t exe_byte_size,
1322 lldb::addr_t oso_file_addr,
1323 lldb::addr_t oso_byte_size) {
1324 const uint32_t debug_map_idx =
1325 m_debug_map.FindEntryIndexThatContains(exe_file_addr);
1326 if (debug_map_idx != UINT32_MAX) {
1327 DebugMap::Entry *debug_map_entry =
1328 m_debug_map.FindEntryThatContains(exe_file_addr);
1329 debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
1330 addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size);
1331 if (range_size == 0) {
1332 range_size = std::max<addr_t>(exe_byte_size, oso_byte_size);
1333 if (range_size == 0)
1336 cu_info->file_range_map.Append(
1337 FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr));
1343 void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) {
1344 cu_info->file_range_map.Sort();
1345 #if defined(DEBUG_OSO_DMAP)
1346 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
1347 const size_t n = oso_file_range_map.GetSize();
1348 printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
1349 cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
1350 for (size_t i = 0; i < n; ++i) {
1351 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
1352 printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
1353 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
1354 entry.GetRangeBase(), entry.GetRangeEnd(), entry.data,
1355 entry.data + entry.GetByteSize());
1361 SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
1362 lldb::addr_t oso_file_addr) {
1363 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile);
1365 const FileRangeMap::Entry *oso_range_entry =
1366 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1367 if (oso_range_entry) {
1368 const DebugMap::Entry *debug_map_entry =
1369 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1370 if (debug_map_entry) {
1371 const lldb::addr_t offset =
1372 oso_file_addr - oso_range_entry->GetRangeBase();
1373 const lldb::addr_t exe_file_addr =
1374 debug_map_entry->GetRangeBase() + offset;
1375 return exe_file_addr;
1379 return LLDB_INVALID_ADDRESS;
1382 bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) {
1383 // Make sure this address hasn't been fixed already
1384 Module *exe_module = GetObjectFile()->GetModule().get();
1385 Module *addr_module = addr.GetModule().get();
1386 if (addr_module == exe_module)
1387 return true; // Address is already in terms of the main executable module
1389 CompileUnitInfo *cu_info = GetCompileUnitInfo(
1390 GetSymbolFileAsSymbolFileDWARF(addr_module->GetSymbolFile()));
1392 const lldb::addr_t oso_file_addr = addr.GetFileAddress();
1393 const FileRangeMap::Entry *oso_range_entry =
1394 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
1395 if (oso_range_entry) {
1396 const DebugMap::Entry *debug_map_entry =
1397 m_debug_map.FindEntryThatContains(oso_range_entry->data);
1398 if (debug_map_entry) {
1399 const lldb::addr_t offset =
1400 oso_file_addr - oso_range_entry->GetRangeBase();
1401 const lldb::addr_t exe_file_addr =
1402 debug_map_entry->GetRangeBase() + offset;
1403 return exe_module->ResolveFileAddress(exe_file_addr, addr);
1410 LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf,
1411 LineTable *line_table) {
1412 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf);
1414 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
1419 SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data,
1420 DWARFDebugAranges *debug_aranges) {
1421 size_t num_line_entries_added = 0;
1422 if (debug_aranges && dwarf2Data) {
1423 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
1424 if (compile_unit_info) {
1425 const FileRangeMap &file_range_map =
1426 compile_unit_info->GetFileRangeMap(this);
1427 for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) {
1428 const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx);
1430 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(),
1431 entry->GetRangeEnd());
1432 num_line_entries_added++;
1437 return num_line_entries_added;