15ffd83dbSDimitry Andric //===-- SymbolFileDWARFDebugMap.cpp ---------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "SymbolFileDWARFDebugMap.h"
10bdd1243dSDimitry Andric #include "DWARFCompileUnit.h"
110b57cec5SDimitry Andric #include "DWARFDebugAranges.h"
12bdd1243dSDimitry Andric #include "DWARFDebugInfo.h"
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "lldb/Core/Module.h"
150b57cec5SDimitry Andric #include "lldb/Core/ModuleList.h"
160b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
170b57cec5SDimitry Andric #include "lldb/Core/Section.h"
180b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
190b57cec5SDimitry Andric #include "lldb/Utility/RangeMap.h"
200b57cec5SDimitry Andric #include "lldb/Utility/RegularExpression.h"
21bdd1243dSDimitry Andric #include "lldb/Utility/StreamString.h"
22c9157d92SDimitry Andric #include "lldb/Utility/StructuredData.h"
23c9157d92SDimitry Andric #include "lldb/Utility/Timer.h"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric //#define DEBUG_OSO_DMAP // DO NOT CHECKIN WITH THIS NOT COMMENTED OUT
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
280b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h"
290b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
300b57cec5SDimitry Andric #include "lldb/Symbol/SymbolVendor.h"
310b57cec5SDimitry Andric #include "lldb/Symbol/TypeMap.h"
320b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
33fe013be4SDimitry Andric #include "llvm/ADT/STLExtras.h"
340b57cec5SDimitry Andric #include "llvm/Support/ScopedPrinter.h"
350b57cec5SDimitry Andric 
36bdd1243dSDimitry Andric #include "lldb/Target/StackFrame.h"
37bdd1243dSDimitry Andric 
380b57cec5SDimitry Andric #include "LogChannelDWARF.h"
390b57cec5SDimitry Andric #include "SymbolFileDWARF.h"
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #include <memory>
42bdd1243dSDimitry Andric #include <optional>
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric using namespace lldb;
450b57cec5SDimitry Andric using namespace lldb_private;
46c9157d92SDimitry Andric using namespace lldb_private::plugin::dwarf;
470b57cec5SDimitry Andric 
48480093f4SDimitry Andric char SymbolFileDWARFDebugMap::ID;
49480093f4SDimitry Andric 
500b57cec5SDimitry Andric // Subclass lldb_private::Module so we can intercept the
510b57cec5SDimitry Andric // "Module::GetObjectFile()" (so we can fixup the object file sections) and
529dba64beSDimitry Andric // also for "Module::GetSymbolFile()" (so we can fixup the symbol file id.
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric const SymbolFileDWARFDebugMap::FileRangeMap &
GetFileRangeMap(SymbolFileDWARFDebugMap * exe_symfile)550b57cec5SDimitry Andric SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap(
560b57cec5SDimitry Andric     SymbolFileDWARFDebugMap *exe_symfile) {
570b57cec5SDimitry Andric   if (file_range_map_valid)
580b57cec5SDimitry Andric     return file_range_map;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   file_range_map_valid = true;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   Module *oso_module = exe_symfile->GetModuleByCompUnitInfo(this);
630b57cec5SDimitry Andric   if (!oso_module)
640b57cec5SDimitry Andric     return file_range_map;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   ObjectFile *oso_objfile = oso_module->GetObjectFile();
670b57cec5SDimitry Andric   if (!oso_objfile)
680b57cec5SDimitry Andric     return file_range_map;
690b57cec5SDimitry Andric 
701fd87a68SDimitry Andric   Log *log = GetLog(DWARFLog::DebugMap);
719dba64beSDimitry Andric   LLDB_LOGF(
729dba64beSDimitry Andric       log,
730b57cec5SDimitry Andric       "%p: SymbolFileDWARFDebugMap::CompileUnitInfo::GetFileRangeMap ('%s')",
740b57cec5SDimitry Andric       static_cast<void *>(this),
750b57cec5SDimitry Andric       oso_module->GetSpecificationDescription().c_str());
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   std::vector<SymbolFileDWARFDebugMap::CompileUnitInfo *> cu_infos;
780b57cec5SDimitry Andric   if (exe_symfile->GetCompUnitInfosForModule(oso_module, cu_infos)) {
790b57cec5SDimitry Andric     for (auto comp_unit_info : cu_infos) {
800b57cec5SDimitry Andric       Symtab *exe_symtab = exe_symfile->GetObjectFile()->GetSymtab();
810b57cec5SDimitry Andric       ModuleSP oso_module_sp(oso_objfile->GetModule());
820b57cec5SDimitry Andric       Symtab *oso_symtab = oso_objfile->GetSymtab();
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric       /// const uint32_t fun_resolve_flags = SymbolContext::Module |
850b57cec5SDimitry Andric       /// eSymbolContextCompUnit | eSymbolContextFunction;
860b57cec5SDimitry Andric       // SectionList *oso_sections = oso_objfile->Sections();
870b57cec5SDimitry Andric       // Now we need to make sections that map from zero based object file
880b57cec5SDimitry Andric       // addresses to where things ended up in the main executable.
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric       assert(comp_unit_info->first_symbol_index != UINT32_MAX);
910b57cec5SDimitry Andric       // End index is one past the last valid symbol index
920b57cec5SDimitry Andric       const uint32_t oso_end_idx = comp_unit_info->last_symbol_index + 1;
930b57cec5SDimitry Andric       for (uint32_t idx = comp_unit_info->first_symbol_index +
940b57cec5SDimitry Andric                           2; // Skip the N_SO and N_OSO
950b57cec5SDimitry Andric            idx < oso_end_idx; ++idx) {
960b57cec5SDimitry Andric         Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx);
970b57cec5SDimitry Andric         if (exe_symbol) {
980b57cec5SDimitry Andric           if (!exe_symbol->IsDebug())
990b57cec5SDimitry Andric             continue;
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric           switch (exe_symbol->GetType()) {
1020b57cec5SDimitry Andric           default:
1030b57cec5SDimitry Andric             break;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric           case eSymbolTypeCode: {
1060b57cec5SDimitry Andric             // For each N_FUN, or function that we run into in the debug map we
1070b57cec5SDimitry Andric             // make a new section that we add to the sections found in the .o
1080b57cec5SDimitry Andric             // file. This new section has the file address set to what the
1090b57cec5SDimitry Andric             // addresses are in the .o file, and the load address is adjusted
1100b57cec5SDimitry Andric             // to match where it ended up in the final executable! We do this
1110b57cec5SDimitry Andric             // before we parse any dwarf info so that when it goes get parsed
1120b57cec5SDimitry Andric             // all section/offset addresses that get registered will resolve
1130b57cec5SDimitry Andric             // correctly to the new addresses in the main executable.
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric             // First we find the original symbol in the .o file's symbol table
1160b57cec5SDimitry Andric             Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(
1175ffd83dbSDimitry Andric                 exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
1180b57cec5SDimitry Andric                 eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny);
1190b57cec5SDimitry Andric             if (oso_fun_symbol) {
1200b57cec5SDimitry Andric               // Add the inverse OSO file address to debug map entry mapping
1210b57cec5SDimitry Andric               exe_symfile->AddOSOFileRange(
1220b57cec5SDimitry Andric                   this, exe_symbol->GetAddressRef().GetFileAddress(),
1230b57cec5SDimitry Andric                   exe_symbol->GetByteSize(),
1240b57cec5SDimitry Andric                   oso_fun_symbol->GetAddressRef().GetFileAddress(),
1250b57cec5SDimitry Andric                   oso_fun_symbol->GetByteSize());
1260b57cec5SDimitry Andric             }
1270b57cec5SDimitry Andric           } break;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric           case eSymbolTypeData: {
1300b57cec5SDimitry Andric             // For each N_GSYM we remap the address for the global by making a
1310b57cec5SDimitry Andric             // new section that we add to the sections found in the .o file.
1320b57cec5SDimitry Andric             // This new section has the file address set to what the addresses
1330b57cec5SDimitry Andric             // are in the .o file, and the load address is adjusted to match
1340b57cec5SDimitry Andric             // where it ended up in the final executable! We do this before we
1350b57cec5SDimitry Andric             // parse any dwarf info so that when it goes get parsed all
1360b57cec5SDimitry Andric             // section/offset addresses that get registered will resolve
1370b57cec5SDimitry Andric             // correctly to the new addresses in the main executable. We
1380b57cec5SDimitry Andric             // initially set the section size to be 1 byte, but will need to
1390b57cec5SDimitry Andric             // fix up these addresses further after all globals have been
1400b57cec5SDimitry Andric             // parsed to span the gaps, or we can find the global variable
1410b57cec5SDimitry Andric             // sizes from the DWARF info as we are parsing.
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric             // Next we find the non-stab entry that corresponds to the N_GSYM
1440b57cec5SDimitry Andric             // in the .o file
1450b57cec5SDimitry Andric             Symbol *oso_gsym_symbol =
1460b57cec5SDimitry Andric                 oso_symtab->FindFirstSymbolWithNameAndType(
1475ffd83dbSDimitry Andric                     exe_symbol->GetMangled().GetName(Mangled::ePreferMangled),
1480b57cec5SDimitry Andric                     eSymbolTypeData, Symtab::eDebugNo, Symtab::eVisibilityAny);
1490b57cec5SDimitry Andric             if (exe_symbol && oso_gsym_symbol && exe_symbol->ValueIsAddress() &&
1500b57cec5SDimitry Andric                 oso_gsym_symbol->ValueIsAddress()) {
1510b57cec5SDimitry Andric               // Add the inverse OSO file address to debug map entry mapping
1520b57cec5SDimitry Andric               exe_symfile->AddOSOFileRange(
1530b57cec5SDimitry Andric                   this, exe_symbol->GetAddressRef().GetFileAddress(),
1540b57cec5SDimitry Andric                   exe_symbol->GetByteSize(),
1550b57cec5SDimitry Andric                   oso_gsym_symbol->GetAddressRef().GetFileAddress(),
1560b57cec5SDimitry Andric                   oso_gsym_symbol->GetByteSize());
1570b57cec5SDimitry Andric             }
1580b57cec5SDimitry Andric           } break;
1590b57cec5SDimitry Andric           }
1600b57cec5SDimitry Andric         }
1610b57cec5SDimitry Andric       }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric       exe_symfile->FinalizeOSOFileRanges(this);
1640b57cec5SDimitry Andric       // We don't need the symbols anymore for the .o files
1650b57cec5SDimitry Andric       oso_objfile->ClearSymtab();
1660b57cec5SDimitry Andric     }
1670b57cec5SDimitry Andric   }
1680b57cec5SDimitry Andric   return file_range_map;
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
171c9157d92SDimitry Andric namespace lldb_private::plugin {
172c9157d92SDimitry Andric namespace dwarf {
1730b57cec5SDimitry Andric class DebugMapModule : public Module {
1740b57cec5SDimitry Andric public:
DebugMapModule(const ModuleSP & exe_module_sp,uint32_t cu_idx,const FileSpec & file_spec,const ArchSpec & arch,ConstString object_name,off_t object_offset,const llvm::sys::TimePoint<> object_mod_time)1750b57cec5SDimitry Andric   DebugMapModule(const ModuleSP &exe_module_sp, uint32_t cu_idx,
1760b57cec5SDimitry Andric                  const FileSpec &file_spec, const ArchSpec &arch,
177c9157d92SDimitry Andric                  ConstString object_name, off_t object_offset,
1780b57cec5SDimitry Andric                  const llvm::sys::TimePoint<> object_mod_time)
1790b57cec5SDimitry Andric       : Module(file_spec, arch, object_name, object_offset, object_mod_time),
1800b57cec5SDimitry Andric         m_exe_module_wp(exe_module_sp), m_cu_idx(cu_idx) {}
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   ~DebugMapModule() override = default;
1830b57cec5SDimitry Andric 
1849dba64beSDimitry Andric   SymbolFile *
GetSymbolFile(bool can_create=true,lldb_private::Stream * feedback_strm=nullptr)1859dba64beSDimitry Andric   GetSymbolFile(bool can_create = true,
1860b57cec5SDimitry Andric                 lldb_private::Stream *feedback_strm = nullptr) override {
1870b57cec5SDimitry Andric     // Scope for locker
1880b57cec5SDimitry Andric     if (m_symfile_up.get() || !can_create)
1899dba64beSDimitry Andric       return m_symfile_up ? m_symfile_up->GetSymbolFile() : nullptr;
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric     ModuleSP exe_module_sp(m_exe_module_wp.lock());
1920b57cec5SDimitry Andric     if (exe_module_sp) {
1930b57cec5SDimitry Andric       // Now get the object file outside of a locking scope
1940b57cec5SDimitry Andric       ObjectFile *oso_objfile = GetObjectFile();
1950b57cec5SDimitry Andric       if (oso_objfile) {
1960b57cec5SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(m_mutex);
1979dba64beSDimitry Andric         if (SymbolFile *symfile =
1989dba64beSDimitry Andric                 Module::GetSymbolFile(can_create, feedback_strm)) {
1990b57cec5SDimitry Andric           // Set a pointer to this class to set our OSO DWARF file know that
2000b57cec5SDimitry Andric           // the DWARF is being used along with a debug map and that it will
2010b57cec5SDimitry Andric           // have the remapped sections that we do below.
2020b57cec5SDimitry Andric           SymbolFileDWARF *oso_symfile =
2039dba64beSDimitry Andric               SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(symfile);
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric           if (!oso_symfile)
2060b57cec5SDimitry Andric             return nullptr;
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric           ObjectFile *exe_objfile = exe_module_sp->GetObjectFile();
2099dba64beSDimitry Andric           SymbolFile *exe_symfile = exe_module_sp->GetSymbolFile();
2100b57cec5SDimitry Andric 
2119dba64beSDimitry Andric           if (exe_objfile && exe_symfile) {
2120b57cec5SDimitry Andric             oso_symfile->SetDebugMapModule(exe_module_sp);
2130b57cec5SDimitry Andric             // Set the ID of the symbol file DWARF to the index of the OSO
2140b57cec5SDimitry Andric             // shifted left by 32 bits to provide a unique prefix for any
2150b57cec5SDimitry Andric             // UserID's that get created in the symbol file.
216fe013be4SDimitry Andric             oso_symfile->SetFileIndex((uint64_t)m_cu_idx);
2170b57cec5SDimitry Andric           }
2189dba64beSDimitry Andric           return symfile;
2190b57cec5SDimitry Andric         }
2200b57cec5SDimitry Andric       }
2210b57cec5SDimitry Andric     }
2220b57cec5SDimitry Andric     return nullptr;
2230b57cec5SDimitry Andric   }
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric protected:
2260b57cec5SDimitry Andric   ModuleWP m_exe_module_wp;
2270b57cec5SDimitry Andric   const uint32_t m_cu_idx;
2280b57cec5SDimitry Andric };
229c9157d92SDimitry Andric } // namespace dwarf
230c9157d92SDimitry Andric } // namespace lldb_private::plugin
2310b57cec5SDimitry Andric 
Initialize()2320b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::Initialize() {
2330b57cec5SDimitry Andric   PluginManager::RegisterPlugin(GetPluginNameStatic(),
2340b57cec5SDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance);
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
Terminate()2370b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::Terminate() {
2380b57cec5SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
GetPluginDescriptionStatic()241349cc55cSDimitry Andric llvm::StringRef SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() {
2420b57cec5SDimitry Andric   return "DWARF and DWARF3 debug symbol file reader (debug map).";
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
CreateInstance(ObjectFileSP objfile_sp)2459dba64beSDimitry Andric SymbolFile *SymbolFileDWARFDebugMap::CreateInstance(ObjectFileSP objfile_sp) {
2469dba64beSDimitry Andric   return new SymbolFileDWARFDebugMap(std::move(objfile_sp));
2470b57cec5SDimitry Andric }
2480b57cec5SDimitry Andric 
SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp)2499dba64beSDimitry Andric SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap(ObjectFileSP objfile_sp)
25081ad6265SDimitry Andric     : SymbolFileCommon(std::move(objfile_sp)), m_flags(), m_compile_unit_infos(),
2519dba64beSDimitry Andric       m_func_indexes(), m_glob_indexes(),
2520b57cec5SDimitry Andric       m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
2530b57cec5SDimitry Andric 
254fe6060f1SDimitry Andric SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() = default;
2550b57cec5SDimitry Andric 
InitializeObject()2560b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::InitializeObject() {}
2570b57cec5SDimitry Andric 
InitOSO()2580b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::InitOSO() {
2590b57cec5SDimitry Andric   if (m_flags.test(kHaveInitializedOSOs))
2600b57cec5SDimitry Andric     return;
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   m_flags.set(kHaveInitializedOSOs);
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   // If the object file has been stripped, there is no sense in looking further
2650b57cec5SDimitry Andric   // as all of the debug symbols for the debug map will not be available
2669dba64beSDimitry Andric   if (m_objfile_sp->IsStripped())
2670b57cec5SDimitry Andric     return;
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   // Also make sure the file type is some sort of executable. Core files, debug
2700b57cec5SDimitry Andric   // info files (dSYM), object files (.o files), and stub libraries all can
2719dba64beSDimitry Andric   switch (m_objfile_sp->GetType()) {
2720b57cec5SDimitry Andric   case ObjectFile::eTypeInvalid:
2730b57cec5SDimitry Andric   case ObjectFile::eTypeCoreFile:
2740b57cec5SDimitry Andric   case ObjectFile::eTypeDebugInfo:
2750b57cec5SDimitry Andric   case ObjectFile::eTypeObjectFile:
2760b57cec5SDimitry Andric   case ObjectFile::eTypeStubLibrary:
2770b57cec5SDimitry Andric   case ObjectFile::eTypeUnknown:
2780b57cec5SDimitry Andric   case ObjectFile::eTypeJIT:
2790b57cec5SDimitry Andric     return;
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric   case ObjectFile::eTypeExecutable:
2820b57cec5SDimitry Andric   case ObjectFile::eTypeDynamicLinker:
2830b57cec5SDimitry Andric   case ObjectFile::eTypeSharedLibrary:
2840b57cec5SDimitry Andric     break;
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   // In order to get the abilities of this plug-in, we look at the list of
2880b57cec5SDimitry Andric   // N_OSO entries (object files) from the symbol table and make sure that
2890b57cec5SDimitry Andric   // these files exist and also contain valid DWARF. If we get any of that then
2900b57cec5SDimitry Andric   // we return the abilities of the first N_OSO's DWARF.
2910b57cec5SDimitry Andric 
2929dba64beSDimitry Andric   Symtab *symtab = m_objfile_sp->GetSymtab();
293fe013be4SDimitry Andric   if (!symtab)
294fe013be4SDimitry Andric     return;
295fe013be4SDimitry Andric 
2961fd87a68SDimitry Andric   Log *log = GetLog(DWARFLog::DebugMap);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   std::vector<uint32_t> oso_indexes;
2990b57cec5SDimitry Andric   // When a mach-o symbol is encoded, the n_type field is encoded in bits
3000b57cec5SDimitry Andric   // 23:16, and the n_desc field is encoded in bits 15:0.
3010b57cec5SDimitry Andric   //
3020b57cec5SDimitry Andric   // To find all N_OSO entries that are part of the DWARF + debug map we find
3030b57cec5SDimitry Andric   // only object file symbols with the flags value as follows: bits 23:16 ==
3040b57cec5SDimitry Andric   // 0x66 (N_OSO) bits 15: 0 == 0x0001 (specifies this is a debug map object
3050b57cec5SDimitry Andric   // file)
3060b57cec5SDimitry Andric   const uint32_t k_oso_symbol_flags_value = 0x660001u;
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   const uint32_t oso_index_count =
3090b57cec5SDimitry Andric       symtab->AppendSymbolIndexesWithTypeAndFlagsValue(
3100b57cec5SDimitry Andric           eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes);
3110b57cec5SDimitry Andric 
312fe013be4SDimitry Andric   if (oso_index_count == 0)
313fe013be4SDimitry Andric     return;
314fe013be4SDimitry Andric 
3150b57cec5SDimitry Andric   symtab->AppendSymbolIndexesWithType(eSymbolTypeCode, Symtab::eDebugYes,
316fe013be4SDimitry Andric                                       Symtab::eVisibilityAny, m_func_indexes);
3170b57cec5SDimitry Andric   symtab->AppendSymbolIndexesWithType(eSymbolTypeData, Symtab::eDebugYes,
318fe013be4SDimitry Andric                                       Symtab::eVisibilityAny, m_glob_indexes);
3190b57cec5SDimitry Andric 
3200b57cec5SDimitry Andric   symtab->SortSymbolIndexesByValue(m_func_indexes, true);
3210b57cec5SDimitry Andric   symtab->SortSymbolIndexesByValue(m_glob_indexes, true);
3220b57cec5SDimitry Andric 
323fe013be4SDimitry Andric   for (uint32_t sym_idx :
324fe013be4SDimitry Andric        llvm::concat<uint32_t>(m_func_indexes, m_glob_indexes)) {
3250b57cec5SDimitry Andric     const Symbol *symbol = symtab->SymbolAtIndex(sym_idx);
3260b57cec5SDimitry Andric     lldb::addr_t file_addr = symbol->GetAddressRef().GetFileAddress();
3270b57cec5SDimitry Andric     lldb::addr_t byte_size = symbol->GetByteSize();
328fe013be4SDimitry Andric     DebugMap::Entry debug_map_entry(file_addr, byte_size,
329fe013be4SDimitry Andric                                     OSOEntry(sym_idx, LLDB_INVALID_ADDRESS));
3300b57cec5SDimitry Andric     m_debug_map.Append(debug_map_entry);
3310b57cec5SDimitry Andric   }
3320b57cec5SDimitry Andric   m_debug_map.Sort();
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   m_compile_unit_infos.resize(oso_index_count);
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   for (uint32_t i = 0; i < oso_index_count; ++i) {
3370b57cec5SDimitry Andric     const uint32_t so_idx = oso_indexes[i] - 1;
3380b57cec5SDimitry Andric     const uint32_t oso_idx = oso_indexes[i];
3390b57cec5SDimitry Andric     const Symbol *so_symbol = symtab->SymbolAtIndex(so_idx);
3400b57cec5SDimitry Andric     const Symbol *oso_symbol = symtab->SymbolAtIndex(oso_idx);
3410b57cec5SDimitry Andric     if (so_symbol && oso_symbol &&
3420b57cec5SDimitry Andric         so_symbol->GetType() == eSymbolTypeSourceFile &&
3430b57cec5SDimitry Andric         oso_symbol->GetType() == eSymbolTypeObjectFile) {
344fe013be4SDimitry Andric       m_compile_unit_infos[i].so_file.SetFile(so_symbol->GetName().AsCString(),
345fe013be4SDimitry Andric                                               FileSpec::Style::native);
3460b57cec5SDimitry Andric       m_compile_unit_infos[i].oso_path = oso_symbol->GetName();
3470b57cec5SDimitry Andric       m_compile_unit_infos[i].oso_mod_time =
3480b57cec5SDimitry Andric           llvm::sys::toTimePoint(oso_symbol->GetIntegerValue(0));
3490b57cec5SDimitry Andric       uint32_t sibling_idx = so_symbol->GetSiblingIndex();
3500b57cec5SDimitry Andric       // The sibling index can't be less that or equal to the current index
3510b57cec5SDimitry Andric       // "i"
352fe013be4SDimitry Andric       if (sibling_idx <= i || sibling_idx == UINT32_MAX) {
3539dba64beSDimitry Andric         m_objfile_sp->GetModule()->ReportError(
354bdd1243dSDimitry Andric             "N_SO in symbol with UID {0} has invalid sibling in debug "
355bdd1243dSDimitry Andric             "map, "
3560b57cec5SDimitry Andric             "please file a bug and attach the binary listed in this error",
3570b57cec5SDimitry Andric             so_symbol->GetID());
3580b57cec5SDimitry Andric       } else {
3590b57cec5SDimitry Andric         const Symbol *last_symbol = symtab->SymbolAtIndex(sibling_idx - 1);
3600b57cec5SDimitry Andric         m_compile_unit_infos[i].first_symbol_index = so_idx;
3610b57cec5SDimitry Andric         m_compile_unit_infos[i].last_symbol_index = sibling_idx - 1;
3620b57cec5SDimitry Andric         m_compile_unit_infos[i].first_symbol_id = so_symbol->GetID();
3630b57cec5SDimitry Andric         m_compile_unit_infos[i].last_symbol_id = last_symbol->GetID();
3640b57cec5SDimitry Andric 
3659dba64beSDimitry Andric         LLDB_LOGF(log, "Initialized OSO 0x%8.8x: file=%s", i,
3660b57cec5SDimitry Andric                   oso_symbol->GetName().GetCString());
3670b57cec5SDimitry Andric       }
3680b57cec5SDimitry Andric     } else {
3690b57cec5SDimitry Andric       if (oso_symbol == nullptr)
3709dba64beSDimitry Andric         m_objfile_sp->GetModule()->ReportError(
371bdd1243dSDimitry Andric             "N_OSO symbol[{0}] can't be found, please file a bug and "
372bdd1243dSDimitry Andric             "attach "
3730b57cec5SDimitry Andric             "the binary listed in this error",
3740b57cec5SDimitry Andric             oso_idx);
3750b57cec5SDimitry Andric       else if (so_symbol == nullptr)
3769dba64beSDimitry Andric         m_objfile_sp->GetModule()->ReportError(
377bdd1243dSDimitry Andric             "N_SO not found for N_OSO symbol[{0}], please file a bug and "
3780b57cec5SDimitry Andric             "attach the binary listed in this error",
3790b57cec5SDimitry Andric             oso_idx);
3800b57cec5SDimitry Andric       else if (so_symbol->GetType() != eSymbolTypeSourceFile)
3819dba64beSDimitry Andric         m_objfile_sp->GetModule()->ReportError(
382bdd1243dSDimitry Andric             "N_SO has incorrect symbol type ({0}) for N_OSO "
383bdd1243dSDimitry Andric             "symbol[{1}], "
3840b57cec5SDimitry Andric             "please file a bug and attach the binary listed in this error",
3850b57cec5SDimitry Andric             so_symbol->GetType(), oso_idx);
3860b57cec5SDimitry Andric       else if (oso_symbol->GetType() != eSymbolTypeSourceFile)
3879dba64beSDimitry Andric         m_objfile_sp->GetModule()->ReportError(
388bdd1243dSDimitry Andric             "N_OSO has incorrect symbol type ({0}) for N_OSO "
389bdd1243dSDimitry Andric             "symbol[{1}], "
3900b57cec5SDimitry Andric             "please file a bug and attach the binary listed in this error",
3910b57cec5SDimitry Andric             oso_symbol->GetType(), oso_idx);
3920b57cec5SDimitry Andric     }
3930b57cec5SDimitry Andric   }
3940b57cec5SDimitry Andric }
3950b57cec5SDimitry Andric 
GetModuleByOSOIndex(uint32_t oso_idx)3960b57cec5SDimitry Andric Module *SymbolFileDWARFDebugMap::GetModuleByOSOIndex(uint32_t oso_idx) {
3970b57cec5SDimitry Andric   const uint32_t cu_count = GetNumCompileUnits();
3980b57cec5SDimitry Andric   if (oso_idx < cu_count)
3990b57cec5SDimitry Andric     return GetModuleByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
4000b57cec5SDimitry Andric   return nullptr;
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
GetModuleByCompUnitInfo(CompileUnitInfo * comp_unit_info)4030b57cec5SDimitry Andric Module *SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo(
4040b57cec5SDimitry Andric     CompileUnitInfo *comp_unit_info) {
4050b57cec5SDimitry Andric   if (!comp_unit_info->oso_sp) {
4060b57cec5SDimitry Andric     auto pos = m_oso_map.find(
4070b57cec5SDimitry Andric         {comp_unit_info->oso_path, comp_unit_info->oso_mod_time});
4080b57cec5SDimitry Andric     if (pos != m_oso_map.end()) {
4090b57cec5SDimitry Andric       comp_unit_info->oso_sp = pos->second;
4100b57cec5SDimitry Andric     } else {
4110b57cec5SDimitry Andric       ObjectFile *obj_file = GetObjectFile();
4120b57cec5SDimitry Andric       comp_unit_info->oso_sp = std::make_shared<OSOInfo>();
4130b57cec5SDimitry Andric       m_oso_map[{comp_unit_info->oso_path, comp_unit_info->oso_mod_time}] =
4140b57cec5SDimitry Andric           comp_unit_info->oso_sp;
4150b57cec5SDimitry Andric       const char *oso_path = comp_unit_info->oso_path.GetCString();
4160b57cec5SDimitry Andric       FileSpec oso_file(oso_path);
4170b57cec5SDimitry Andric       ConstString oso_object;
4180b57cec5SDimitry Andric       if (FileSystem::Instance().Exists(oso_file)) {
4190b57cec5SDimitry Andric         // The modification time returned by the FS can have a higher precision
4200b57cec5SDimitry Andric         // than the one from the CU.
4210b57cec5SDimitry Andric         auto oso_mod_time = std::chrono::time_point_cast<std::chrono::seconds>(
4220b57cec5SDimitry Andric             FileSystem::Instance().GetModificationTime(oso_file));
4239dba64beSDimitry Andric         // A timestamp of 0 means that the linker was in deterministic mode. In
4249dba64beSDimitry Andric         // that case, we should skip the check against the filesystem last
4259dba64beSDimitry Andric         // modification timestamp, since it will never match.
4269dba64beSDimitry Andric         if (comp_unit_info->oso_mod_time != llvm::sys::TimePoint<>() &&
4279dba64beSDimitry Andric             oso_mod_time != comp_unit_info->oso_mod_time) {
428bdd1243dSDimitry Andric           comp_unit_info->oso_load_error.SetErrorStringWithFormat(
429bdd1243dSDimitry Andric               "debug map object file \"%s\" changed (actual: 0x%8.8x, debug "
430bdd1243dSDimitry Andric               "map: 0x%8.8x) since this executable was linked, debug info "
431bdd1243dSDimitry Andric               "will not be loaded", oso_file.GetPath().c_str(),
432bdd1243dSDimitry Andric               (uint32_t)llvm::sys::toTimeT(oso_mod_time),
433bdd1243dSDimitry Andric               (uint32_t)llvm::sys::toTimeT(comp_unit_info->oso_mod_time));
4340b57cec5SDimitry Andric           obj_file->GetModule()->ReportError(
435bdd1243dSDimitry Andric               "{0}", comp_unit_info->oso_load_error.AsCString());
4360b57cec5SDimitry Andric           return nullptr;
4370b57cec5SDimitry Andric         }
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric       } else {
4400b57cec5SDimitry Andric         const bool must_exist = true;
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric         if (!ObjectFile::SplitArchivePathWithObject(oso_path, oso_file,
4430b57cec5SDimitry Andric                                                     oso_object, must_exist)) {
444bdd1243dSDimitry Andric           comp_unit_info->oso_load_error.SetErrorStringWithFormat(
445bdd1243dSDimitry Andric               "debug map object file \"%s\" containing debug info does not "
446bdd1243dSDimitry Andric               "exist, debug info will not be loaded",
447bdd1243dSDimitry Andric               comp_unit_info->oso_path.GetCString());
4480b57cec5SDimitry Andric           return nullptr;
4490b57cec5SDimitry Andric         }
4500b57cec5SDimitry Andric       }
4510b57cec5SDimitry Andric       // Always create a new module for .o files. Why? Because we use the debug
4520b57cec5SDimitry Andric       // map, to add new sections to each .o file and even though a .o file
4530b57cec5SDimitry Andric       // might not have changed, the sections that get added to the .o file can
4540b57cec5SDimitry Andric       // change.
4550b57cec5SDimitry Andric       ArchSpec oso_arch;
4560b57cec5SDimitry Andric       // Only adopt the architecture from the module (not the vendor or OS)
4570b57cec5SDimitry Andric       // since .o files for "i386-apple-ios" will historically show up as "i386
4580b57cec5SDimitry Andric       // -apple-macosx" due to the lack of a LC_VERSION_MIN_MACOSX or
4590b57cec5SDimitry Andric       // LC_VERSION_MIN_IPHONEOS load command...
4609dba64beSDimitry Andric       oso_arch.SetTriple(m_objfile_sp->GetModule()
4610b57cec5SDimitry Andric                              ->GetArchitecture()
4620b57cec5SDimitry Andric                              .GetTriple()
4630b57cec5SDimitry Andric                              .getArchName()
4640b57cec5SDimitry Andric                              .str()
4650b57cec5SDimitry Andric                              .c_str());
4660b57cec5SDimitry Andric       comp_unit_info->oso_sp->module_sp = std::make_shared<DebugMapModule>(
4670b57cec5SDimitry Andric           obj_file->GetModule(), GetCompUnitInfoIndex(comp_unit_info), oso_file,
468c9157d92SDimitry Andric           oso_arch, oso_object, 0,
4690b57cec5SDimitry Andric           oso_object ? comp_unit_info->oso_mod_time : llvm::sys::TimePoint<>());
470bdd1243dSDimitry Andric 
471fe013be4SDimitry Andric       if (oso_object && !comp_unit_info->oso_sp->module_sp->GetObjectFile() &&
472fe013be4SDimitry Andric           FileSystem::Instance().Exists(oso_file)) {
473bdd1243dSDimitry Andric         // If we are loading a .o file from a .a file the "oso_object" will
474bdd1243dSDimitry Andric         // have a valid value name and if the .a file exists, either the .o
475bdd1243dSDimitry Andric         // file didn't exist in the .a file or the mod time didn't match.
476bdd1243dSDimitry Andric         comp_unit_info->oso_load_error.SetErrorStringWithFormat(
477bdd1243dSDimitry Andric             "\"%s\" object from the \"%s\" archive: "
478bdd1243dSDimitry Andric             "either the .o file doesn't exist in the archive or the "
479bdd1243dSDimitry Andric             "modification time (0x%8.8x) of the .o file doesn't match",
480bdd1243dSDimitry Andric             oso_object.AsCString(), oso_file.GetPath().c_str(),
481bdd1243dSDimitry Andric             (uint32_t)llvm::sys::toTimeT(comp_unit_info->oso_mod_time));
482bdd1243dSDimitry Andric       }
483bdd1243dSDimitry Andric     }
4840b57cec5SDimitry Andric   }
4850b57cec5SDimitry Andric   if (comp_unit_info->oso_sp)
4860b57cec5SDimitry Andric     return comp_unit_info->oso_sp->module_sp.get();
4870b57cec5SDimitry Andric   return nullptr;
4880b57cec5SDimitry Andric }
4890b57cec5SDimitry Andric 
GetFileSpecForSO(uint32_t oso_idx,FileSpec & file_spec)4900b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::GetFileSpecForSO(uint32_t oso_idx,
4910b57cec5SDimitry Andric                                                FileSpec &file_spec) {
4920b57cec5SDimitry Andric   if (oso_idx < m_compile_unit_infos.size()) {
4930b57cec5SDimitry Andric     if (m_compile_unit_infos[oso_idx].so_file) {
4940b57cec5SDimitry Andric       file_spec = m_compile_unit_infos[oso_idx].so_file;
4950b57cec5SDimitry Andric       return true;
4960b57cec5SDimitry Andric     }
4970b57cec5SDimitry Andric   }
4980b57cec5SDimitry Andric   return false;
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric 
GetObjectFileByOSOIndex(uint32_t oso_idx)5010b57cec5SDimitry Andric ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex(uint32_t oso_idx) {
5020b57cec5SDimitry Andric   Module *oso_module = GetModuleByOSOIndex(oso_idx);
5030b57cec5SDimitry Andric   if (oso_module)
5040b57cec5SDimitry Andric     return oso_module->GetObjectFile();
5050b57cec5SDimitry Andric   return nullptr;
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric 
5080b57cec5SDimitry Andric SymbolFileDWARF *
GetSymbolFile(const SymbolContext & sc)5090b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetSymbolFile(const SymbolContext &sc) {
5100b57cec5SDimitry Andric   return GetSymbolFile(*sc.comp_unit);
5110b57cec5SDimitry Andric }
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric SymbolFileDWARF *
GetSymbolFile(const CompileUnit & comp_unit)5140b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetSymbolFile(const CompileUnit &comp_unit) {
5150b57cec5SDimitry Andric   CompileUnitInfo *comp_unit_info = GetCompUnitInfo(comp_unit);
5160b57cec5SDimitry Andric   if (comp_unit_info)
5170b57cec5SDimitry Andric     return GetSymbolFileByCompUnitInfo(comp_unit_info);
5180b57cec5SDimitry Andric   return nullptr;
5190b57cec5SDimitry Andric }
5200b57cec5SDimitry Andric 
GetObjectFileByCompUnitInfo(CompileUnitInfo * comp_unit_info)5210b57cec5SDimitry Andric ObjectFile *SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo(
5220b57cec5SDimitry Andric     CompileUnitInfo *comp_unit_info) {
5230b57cec5SDimitry Andric   Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
5240b57cec5SDimitry Andric   if (oso_module)
5250b57cec5SDimitry Andric     return oso_module->GetObjectFile();
5260b57cec5SDimitry Andric   return nullptr;
5270b57cec5SDimitry Andric }
5280b57cec5SDimitry Andric 
GetCompUnitInfoIndex(const CompileUnitInfo * comp_unit_info)5290b57cec5SDimitry Andric uint32_t SymbolFileDWARFDebugMap::GetCompUnitInfoIndex(
5300b57cec5SDimitry Andric     const CompileUnitInfo *comp_unit_info) {
5310b57cec5SDimitry Andric   if (!m_compile_unit_infos.empty()) {
5320b57cec5SDimitry Andric     const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front();
5330b57cec5SDimitry Andric     const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back();
5340b57cec5SDimitry Andric     if (first_comp_unit_info <= comp_unit_info &&
5350b57cec5SDimitry Andric         comp_unit_info <= last_comp_unit_info)
5360b57cec5SDimitry Andric       return comp_unit_info - first_comp_unit_info;
5370b57cec5SDimitry Andric   }
5380b57cec5SDimitry Andric   return UINT32_MAX;
5390b57cec5SDimitry Andric }
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric SymbolFileDWARF *
GetSymbolFileByOSOIndex(uint32_t oso_idx)5420b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex(uint32_t oso_idx) {
5430b57cec5SDimitry Andric   unsigned size = m_compile_unit_infos.size();
5440b57cec5SDimitry Andric   if (oso_idx < size)
5450b57cec5SDimitry Andric     return GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[oso_idx]);
5460b57cec5SDimitry Andric   return nullptr;
5470b57cec5SDimitry Andric }
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric SymbolFileDWARF *
GetSymbolFileAsSymbolFileDWARF(SymbolFile * sym_file)5500b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetSymbolFileAsSymbolFileDWARF(SymbolFile *sym_file) {
5510b57cec5SDimitry Andric   if (sym_file &&
5520b57cec5SDimitry Andric       sym_file->GetPluginName() == SymbolFileDWARF::GetPluginNameStatic())
5535ffd83dbSDimitry Andric     return static_cast<SymbolFileDWARF *>(sym_file);
5540b57cec5SDimitry Andric   return nullptr;
5550b57cec5SDimitry Andric }
5560b57cec5SDimitry Andric 
GetSymbolFileByCompUnitInfo(CompileUnitInfo * comp_unit_info)5570b57cec5SDimitry Andric SymbolFileDWARF *SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo(
5580b57cec5SDimitry Andric     CompileUnitInfo *comp_unit_info) {
5599dba64beSDimitry Andric   if (Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info))
5609dba64beSDimitry Andric     return GetSymbolFileAsSymbolFileDWARF(oso_module->GetSymbolFile());
5610b57cec5SDimitry Andric   return nullptr;
5620b57cec5SDimitry Andric }
5630b57cec5SDimitry Andric 
CalculateAbilities()5640b57cec5SDimitry Andric uint32_t SymbolFileDWARFDebugMap::CalculateAbilities() {
5650b57cec5SDimitry Andric   // In order to get the abilities of this plug-in, we look at the list of
5660b57cec5SDimitry Andric   // N_OSO entries (object files) from the symbol table and make sure that
5670b57cec5SDimitry Andric   // these files exist and also contain valid DWARF. If we get any of that then
5680b57cec5SDimitry Andric   // we return the abilities of the first N_OSO's DWARF.
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   const uint32_t oso_index_count = GetNumCompileUnits();
5710b57cec5SDimitry Andric   if (oso_index_count > 0) {
5720b57cec5SDimitry Andric     InitOSO();
5730b57cec5SDimitry Andric     if (!m_compile_unit_infos.empty()) {
5740b57cec5SDimitry Andric       return SymbolFile::CompileUnits | SymbolFile::Functions |
5750b57cec5SDimitry Andric              SymbolFile::Blocks | SymbolFile::GlobalVariables |
5760b57cec5SDimitry Andric              SymbolFile::LocalVariables | SymbolFile::VariableTypes |
5770b57cec5SDimitry Andric              SymbolFile::LineTables;
5780b57cec5SDimitry Andric     }
5790b57cec5SDimitry Andric   }
5800b57cec5SDimitry Andric   return 0;
5810b57cec5SDimitry Andric }
5820b57cec5SDimitry Andric 
CalculateNumCompileUnits()5839dba64beSDimitry Andric uint32_t SymbolFileDWARFDebugMap::CalculateNumCompileUnits() {
5840b57cec5SDimitry Andric   InitOSO();
5850b57cec5SDimitry Andric   return m_compile_unit_infos.size();
5860b57cec5SDimitry Andric }
5870b57cec5SDimitry Andric 
ParseCompileUnitAtIndex(uint32_t cu_idx)5880b57cec5SDimitry Andric CompUnitSP SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) {
5890b57cec5SDimitry Andric   CompUnitSP comp_unit_sp;
5900b57cec5SDimitry Andric   const uint32_t cu_count = GetNumCompileUnits();
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   if (cu_idx < cu_count) {
593bdd1243dSDimitry Andric     auto &cu_info = m_compile_unit_infos[cu_idx];
594bdd1243dSDimitry Andric     Module *oso_module = GetModuleByCompUnitInfo(&cu_info);
5950b57cec5SDimitry Andric     if (oso_module) {
5960b57cec5SDimitry Andric       FileSpec so_file_spec;
5970b57cec5SDimitry Andric       if (GetFileSpecForSO(cu_idx, so_file_spec)) {
5980b57cec5SDimitry Andric         // User zero as the ID to match the compile unit at offset zero in each
599bdd1243dSDimitry Andric         // .o file.
6000b57cec5SDimitry Andric         lldb::user_id_t cu_id = 0;
601*a58f00eaSDimitry Andric         cu_info.compile_units_sps.push_back(std::make_shared<CompileUnit>(
602*a58f00eaSDimitry Andric             m_objfile_sp->GetModule(), nullptr,
603*a58f00eaSDimitry Andric             std::make_shared<SupportFile>(so_file_spec), cu_id,
604bdd1243dSDimitry Andric             eLanguageTypeUnknown, eLazyBoolCalculate));
605bdd1243dSDimitry Andric         cu_info.id_to_index_map.insert({0, 0});
606bdd1243dSDimitry Andric         SetCompileUnitAtIndex(cu_idx, cu_info.compile_units_sps[0]);
607bdd1243dSDimitry Andric         // If there's a symbol file also register all the extra compile units.
608bdd1243dSDimitry Andric         if (SymbolFileDWARF *oso_symfile =
609bdd1243dSDimitry Andric                 GetSymbolFileByCompUnitInfo(&cu_info)) {
610bdd1243dSDimitry Andric           auto num_dwarf_units = oso_symfile->DebugInfo().GetNumUnits();
611bdd1243dSDimitry Andric           for (size_t i = 0; i < num_dwarf_units; ++i) {
612bdd1243dSDimitry Andric             auto *dwarf_unit = oso_symfile->DebugInfo().GetUnitAtIndex(i);
613bdd1243dSDimitry Andric             if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(dwarf_unit)) {
614bdd1243dSDimitry Andric               // The "main" one was already registered.
615bdd1243dSDimitry Andric               if (dwarf_cu->GetID() == 0)
616bdd1243dSDimitry Andric                 continue;
617bdd1243dSDimitry Andric               cu_info.compile_units_sps.push_back(std::make_shared<CompileUnit>(
618*a58f00eaSDimitry Andric                   m_objfile_sp->GetModule(), nullptr,
619*a58f00eaSDimitry Andric                   std::make_shared<SupportFile>(so_file_spec),
620bdd1243dSDimitry Andric                   dwarf_cu->GetID(), eLanguageTypeUnknown, eLazyBoolCalculate));
621bdd1243dSDimitry Andric               cu_info.id_to_index_map.insert(
622bdd1243dSDimitry Andric                   {dwarf_cu->GetID(), cu_info.compile_units_sps.size() - 1});
6230b57cec5SDimitry Andric             }
6240b57cec5SDimitry Andric           }
6250b57cec5SDimitry Andric         }
626bdd1243dSDimitry Andric       }
627bdd1243dSDimitry Andric     }
628bdd1243dSDimitry Andric     if (!cu_info.compile_units_sps.empty())
629bdd1243dSDimitry Andric       comp_unit_sp = cu_info.compile_units_sps[0];
6300b57cec5SDimitry Andric   }
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric   return comp_unit_sp;
6330b57cec5SDimitry Andric }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompUnitInfo(const SymbolContext & sc)6360b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetCompUnitInfo(const SymbolContext &sc) {
6370b57cec5SDimitry Andric   return GetCompUnitInfo(*sc.comp_unit);
6380b57cec5SDimitry Andric }
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompUnitInfo(const CompileUnit & comp_unit)6410b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetCompUnitInfo(const CompileUnit &comp_unit) {
6420b57cec5SDimitry Andric   const uint32_t cu_count = GetNumCompileUnits();
6430b57cec5SDimitry Andric   for (uint32_t i = 0; i < cu_count; ++i) {
644bdd1243dSDimitry Andric     auto &id_to_index_map = m_compile_unit_infos[i].id_to_index_map;
645bdd1243dSDimitry Andric 
646bdd1243dSDimitry Andric     auto it = id_to_index_map.find(comp_unit.GetID());
647bdd1243dSDimitry Andric     if (it != id_to_index_map.end() &&
648bdd1243dSDimitry Andric         &comp_unit ==
649bdd1243dSDimitry Andric             m_compile_unit_infos[i].compile_units_sps[it->getSecond()].get())
6500b57cec5SDimitry Andric       return &m_compile_unit_infos[i];
6510b57cec5SDimitry Andric   }
6520b57cec5SDimitry Andric   return nullptr;
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric 
GetCompUnitInfosForModule(const lldb_private::Module * module,std::vector<CompileUnitInfo * > & cu_infos)6550b57cec5SDimitry Andric size_t SymbolFileDWARFDebugMap::GetCompUnitInfosForModule(
6560b57cec5SDimitry Andric     const lldb_private::Module *module,
6570b57cec5SDimitry Andric     std::vector<CompileUnitInfo *> &cu_infos) {
6580b57cec5SDimitry Andric   const uint32_t cu_count = GetNumCompileUnits();
6590b57cec5SDimitry Andric   for (uint32_t i = 0; i < cu_count; ++i) {
6600b57cec5SDimitry Andric     if (module == GetModuleByCompUnitInfo(&m_compile_unit_infos[i]))
6610b57cec5SDimitry Andric       cu_infos.push_back(&m_compile_unit_infos[i]);
6620b57cec5SDimitry Andric   }
6630b57cec5SDimitry Andric   return cu_infos.size();
6640b57cec5SDimitry Andric }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric lldb::LanguageType
ParseLanguage(CompileUnit & comp_unit)6670b57cec5SDimitry Andric SymbolFileDWARFDebugMap::ParseLanguage(CompileUnit &comp_unit) {
6689dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
6690b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
6700b57cec5SDimitry Andric   if (oso_dwarf)
6710b57cec5SDimitry Andric     return oso_dwarf->ParseLanguage(comp_unit);
6720b57cec5SDimitry Andric   return eLanguageTypeUnknown;
6730b57cec5SDimitry Andric }
6740b57cec5SDimitry Andric 
ParseXcodeSDK(CompileUnit & comp_unit)6755ffd83dbSDimitry Andric XcodeSDK SymbolFileDWARFDebugMap::ParseXcodeSDK(CompileUnit &comp_unit) {
6765ffd83dbSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
6775ffd83dbSDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
6785ffd83dbSDimitry Andric   if (oso_dwarf)
6795ffd83dbSDimitry Andric     return oso_dwarf->ParseXcodeSDK(comp_unit);
6805ffd83dbSDimitry Andric   return {};
6815ffd83dbSDimitry Andric }
6825ffd83dbSDimitry Andric 
683fe013be4SDimitry Andric llvm::SmallSet<lldb::LanguageType, 4>
ParseAllLanguages(lldb_private::CompileUnit & comp_unit)684fe013be4SDimitry Andric SymbolFileDWARFDebugMap::ParseAllLanguages(
685fe013be4SDimitry Andric     lldb_private::CompileUnit &comp_unit) {
686fe013be4SDimitry Andric   llvm::SmallSet<lldb::LanguageType, 4> langs;
687fe013be4SDimitry Andric   auto *info = GetCompUnitInfo(comp_unit);
688fe013be4SDimitry Andric   for (auto &comp_unit : info->compile_units_sps) {
689fe013be4SDimitry Andric     langs.insert(comp_unit->GetLanguage());
690fe013be4SDimitry Andric   }
691fe013be4SDimitry Andric   return langs;
692fe013be4SDimitry Andric }
693fe013be4SDimitry Andric 
ParseFunctions(CompileUnit & comp_unit)6940b57cec5SDimitry Andric size_t SymbolFileDWARFDebugMap::ParseFunctions(CompileUnit &comp_unit) {
6959dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
6960b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
6970b57cec5SDimitry Andric   if (oso_dwarf)
6980b57cec5SDimitry Andric     return oso_dwarf->ParseFunctions(comp_unit);
6990b57cec5SDimitry Andric   return 0;
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric 
ParseLineTable(CompileUnit & comp_unit)7020b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::ParseLineTable(CompileUnit &comp_unit) {
7039dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7040b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
7050b57cec5SDimitry Andric   if (oso_dwarf)
7060b57cec5SDimitry Andric     return oso_dwarf->ParseLineTable(comp_unit);
7070b57cec5SDimitry Andric   return false;
7080b57cec5SDimitry Andric }
7090b57cec5SDimitry Andric 
ParseDebugMacros(CompileUnit & comp_unit)7100b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::ParseDebugMacros(CompileUnit &comp_unit) {
7119dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7120b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
7130b57cec5SDimitry Andric   if (oso_dwarf)
7140b57cec5SDimitry Andric     return oso_dwarf->ParseDebugMacros(comp_unit);
7150b57cec5SDimitry Andric   return false;
7160b57cec5SDimitry Andric }
7170b57cec5SDimitry Andric 
ForEachExternalModule(CompileUnit & comp_unit,llvm::DenseSet<lldb_private::SymbolFile * > & visited_symbol_files,llvm::function_ref<bool (Module &)> f)718480093f4SDimitry Andric bool SymbolFileDWARFDebugMap::ForEachExternalModule(
719480093f4SDimitry Andric     CompileUnit &comp_unit,
720480093f4SDimitry Andric     llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,
721480093f4SDimitry Andric     llvm::function_ref<bool(Module &)> f) {
7229dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7239dba64beSDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
7249dba64beSDimitry Andric   if (oso_dwarf)
725480093f4SDimitry Andric     return oso_dwarf->ForEachExternalModule(comp_unit, visited_symbol_files, f);
726480093f4SDimitry Andric   return false;
7279dba64beSDimitry Andric }
7289dba64beSDimitry Andric 
ParseSupportFiles(CompileUnit & comp_unit,SupportFileList & support_files)729cdc20ff6SDimitry Andric bool SymbolFileDWARFDebugMap::ParseSupportFiles(
730cdc20ff6SDimitry Andric     CompileUnit &comp_unit, SupportFileList &support_files) {
7319dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7320b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
7330b57cec5SDimitry Andric   if (oso_dwarf)
7340b57cec5SDimitry Andric     return oso_dwarf->ParseSupportFiles(comp_unit, support_files);
7350b57cec5SDimitry Andric   return false;
7360b57cec5SDimitry Andric }
7370b57cec5SDimitry Andric 
ParseIsOptimized(CompileUnit & comp_unit)7380b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::ParseIsOptimized(CompileUnit &comp_unit) {
7399dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7400b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
7410b57cec5SDimitry Andric   if (oso_dwarf)
7420b57cec5SDimitry Andric     return oso_dwarf->ParseIsOptimized(comp_unit);
7430b57cec5SDimitry Andric   return false;
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric 
ParseImportedModules(const SymbolContext & sc,std::vector<SourceModule> & imported_modules)7460b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::ParseImportedModules(
7470b57cec5SDimitry Andric     const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
7489dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7490b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
7500b57cec5SDimitry Andric   if (oso_dwarf)
7510b57cec5SDimitry Andric     return oso_dwarf->ParseImportedModules(sc, imported_modules);
7520b57cec5SDimitry Andric   return false;
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric 
ParseBlocksRecursive(Function & func)7550b57cec5SDimitry Andric size_t SymbolFileDWARFDebugMap::ParseBlocksRecursive(Function &func) {
7569dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7570b57cec5SDimitry Andric   CompileUnit *comp_unit = func.GetCompileUnit();
7580b57cec5SDimitry Andric   if (!comp_unit)
7590b57cec5SDimitry Andric     return 0;
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(*comp_unit);
7620b57cec5SDimitry Andric   if (oso_dwarf)
7630b57cec5SDimitry Andric     return oso_dwarf->ParseBlocksRecursive(func);
7640b57cec5SDimitry Andric   return 0;
7650b57cec5SDimitry Andric }
7660b57cec5SDimitry Andric 
ParseTypes(CompileUnit & comp_unit)7670b57cec5SDimitry Andric size_t SymbolFileDWARFDebugMap::ParseTypes(CompileUnit &comp_unit) {
7689dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7690b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(comp_unit);
7700b57cec5SDimitry Andric   if (oso_dwarf)
7710b57cec5SDimitry Andric     return oso_dwarf->ParseTypes(comp_unit);
7720b57cec5SDimitry Andric   return 0;
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric size_t
ParseVariablesForContext(const SymbolContext & sc)7760b57cec5SDimitry Andric SymbolFileDWARFDebugMap::ParseVariablesForContext(const SymbolContext &sc) {
7779dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7780b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFile(sc);
7790b57cec5SDimitry Andric   if (oso_dwarf)
7800b57cec5SDimitry Andric     return oso_dwarf->ParseVariablesForContext(sc);
7810b57cec5SDimitry Andric   return 0;
7820b57cec5SDimitry Andric }
7830b57cec5SDimitry Andric 
ResolveTypeUID(lldb::user_id_t type_uid)7840b57cec5SDimitry Andric Type *SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) {
7859dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
7860b57cec5SDimitry Andric   const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
7870b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
7880b57cec5SDimitry Andric   if (oso_dwarf)
7890b57cec5SDimitry Andric     return oso_dwarf->ResolveTypeUID(type_uid);
7900b57cec5SDimitry Andric   return nullptr;
7910b57cec5SDimitry Andric }
7920b57cec5SDimitry Andric 
793bdd1243dSDimitry Andric std::optional<SymbolFile::ArrayInfo>
GetDynamicArrayInfoForUID(lldb::user_id_t type_uid,const lldb_private::ExecutionContext * exe_ctx)7940b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetDynamicArrayInfoForUID(
7950b57cec5SDimitry Andric     lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
7960b57cec5SDimitry Andric   const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
7970b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
7980b57cec5SDimitry Andric   if (oso_dwarf)
7990b57cec5SDimitry Andric     return oso_dwarf->GetDynamicArrayInfoForUID(type_uid, exe_ctx);
800bdd1243dSDimitry Andric   return std::nullopt;
8010b57cec5SDimitry Andric }
8020b57cec5SDimitry Andric 
CompleteType(CompilerType & compiler_type)8030b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::CompleteType(CompilerType &compiler_type) {
8040b57cec5SDimitry Andric   bool success = false;
8050b57cec5SDimitry Andric   if (compiler_type) {
8060b57cec5SDimitry Andric     ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
807c9157d92SDimitry Andric       if (oso_dwarf->HasForwardDeclForCompilerType(compiler_type)) {
8080b57cec5SDimitry Andric         oso_dwarf->CompleteType(compiler_type);
8090b57cec5SDimitry Andric         success = true;
8100b57cec5SDimitry Andric         return true;
8110b57cec5SDimitry Andric       }
8120b57cec5SDimitry Andric       return false;
8130b57cec5SDimitry Andric     });
8140b57cec5SDimitry Andric   }
8150b57cec5SDimitry Andric   return success;
8160b57cec5SDimitry Andric }
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric uint32_t
ResolveSymbolContext(const Address & exe_so_addr,SymbolContextItem resolve_scope,SymbolContext & sc)8190b57cec5SDimitry Andric SymbolFileDWARFDebugMap::ResolveSymbolContext(const Address &exe_so_addr,
8200b57cec5SDimitry Andric                                               SymbolContextItem resolve_scope,
8210b57cec5SDimitry Andric                                               SymbolContext &sc) {
8229dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
8230b57cec5SDimitry Andric   uint32_t resolved_flags = 0;
8249dba64beSDimitry Andric   Symtab *symtab = m_objfile_sp->GetSymtab();
8250b57cec5SDimitry Andric   if (symtab) {
8260b57cec5SDimitry Andric     const addr_t exe_file_addr = exe_so_addr.GetFileAddress();
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric     const DebugMap::Entry *debug_map_entry =
8290b57cec5SDimitry Andric         m_debug_map.FindEntryThatContains(exe_file_addr);
8300b57cec5SDimitry Andric     if (debug_map_entry) {
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric       sc.symbol =
8330b57cec5SDimitry Andric           symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric       if (sc.symbol != nullptr) {
8360b57cec5SDimitry Andric         resolved_flags |= eSymbolContextSymbol;
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric         uint32_t oso_idx = 0;
8390b57cec5SDimitry Andric         CompileUnitInfo *comp_unit_info =
8400b57cec5SDimitry Andric             GetCompileUnitInfoForSymbolWithID(sc.symbol->GetID(), &oso_idx);
8410b57cec5SDimitry Andric         if (comp_unit_info) {
8420b57cec5SDimitry Andric           comp_unit_info->GetFileRangeMap(this);
8430b57cec5SDimitry Andric           Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
8440b57cec5SDimitry Andric           if (oso_module) {
8450b57cec5SDimitry Andric             lldb::addr_t oso_file_addr =
8460b57cec5SDimitry Andric                 exe_file_addr - debug_map_entry->GetRangeBase() +
8470b57cec5SDimitry Andric                 debug_map_entry->data.GetOSOFileAddress();
8480b57cec5SDimitry Andric             Address oso_so_addr;
8490b57cec5SDimitry Andric             if (oso_module->ResolveFileAddress(oso_file_addr, oso_so_addr)) {
8500b57cec5SDimitry Andric               resolved_flags |=
8519dba64beSDimitry Andric                   oso_module->GetSymbolFile()->ResolveSymbolContext(
8520b57cec5SDimitry Andric                       oso_so_addr, resolve_scope, sc);
8530b57cec5SDimitry Andric             }
8540b57cec5SDimitry Andric           }
8550b57cec5SDimitry Andric         }
8560b57cec5SDimitry Andric       }
8570b57cec5SDimitry Andric     }
8580b57cec5SDimitry Andric   }
8590b57cec5SDimitry Andric   return resolved_flags;
8600b57cec5SDimitry Andric }
8610b57cec5SDimitry Andric 
ResolveSymbolContext(const SourceLocationSpec & src_location_spec,SymbolContextItem resolve_scope,SymbolContextList & sc_list)8620b57cec5SDimitry Andric uint32_t SymbolFileDWARFDebugMap::ResolveSymbolContext(
863fe6060f1SDimitry Andric     const SourceLocationSpec &src_location_spec,
8640b57cec5SDimitry Andric     SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
8659dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
8660b57cec5SDimitry Andric   const uint32_t initial = sc_list.GetSize();
8670b57cec5SDimitry Andric   const uint32_t cu_count = GetNumCompileUnits();
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   for (uint32_t i = 0; i < cu_count; ++i) {
8700b57cec5SDimitry Andric     // If we are checking for inlines, then we need to look through all compile
8710b57cec5SDimitry Andric     // units no matter if "file_spec" matches.
872fe6060f1SDimitry Andric     bool resolve = src_location_spec.GetCheckInlines();
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric     if (!resolve) {
8750b57cec5SDimitry Andric       FileSpec so_file_spec;
876480093f4SDimitry Andric       if (GetFileSpecForSO(i, so_file_spec))
877fe6060f1SDimitry Andric         resolve =
878fe6060f1SDimitry Andric             FileSpec::Match(src_location_spec.GetFileSpec(), so_file_spec);
8790b57cec5SDimitry Andric     }
8800b57cec5SDimitry Andric     if (resolve) {
8810b57cec5SDimitry Andric       SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i);
8820b57cec5SDimitry Andric       if (oso_dwarf)
883fe6060f1SDimitry Andric         oso_dwarf->ResolveSymbolContext(src_location_spec, resolve_scope,
884fe6060f1SDimitry Andric                                         sc_list);
8850b57cec5SDimitry Andric     }
8860b57cec5SDimitry Andric   }
8870b57cec5SDimitry Andric   return sc_list.GetSize() - initial;
8880b57cec5SDimitry Andric }
8890b57cec5SDimitry Andric 
PrivateFindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,const std::vector<uint32_t> & indexes,uint32_t max_matches,VariableList & variables)8909dba64beSDimitry Andric void SymbolFileDWARFDebugMap::PrivateFindGlobalVariables(
8915ffd83dbSDimitry Andric     ConstString name, const CompilerDeclContext &parent_decl_ctx,
8920b57cec5SDimitry Andric     const std::vector<uint32_t>
8930b57cec5SDimitry Andric         &indexes, // Indexes into the symbol table that match "name"
8940b57cec5SDimitry Andric     uint32_t max_matches, VariableList &variables) {
8950b57cec5SDimitry Andric   const size_t match_count = indexes.size();
8960b57cec5SDimitry Andric   for (size_t i = 0; i < match_count; ++i) {
8970b57cec5SDimitry Andric     uint32_t oso_idx;
8980b57cec5SDimitry Andric     CompileUnitInfo *comp_unit_info =
8990b57cec5SDimitry Andric         GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx);
9000b57cec5SDimitry Andric     if (comp_unit_info) {
9010b57cec5SDimitry Andric       SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
9020b57cec5SDimitry Andric       if (oso_dwarf) {
9039dba64beSDimitry Andric         oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
9049dba64beSDimitry Andric                                        variables);
9050b57cec5SDimitry Andric         if (variables.GetSize() > max_matches)
9060b57cec5SDimitry Andric           break;
9070b57cec5SDimitry Andric       }
9080b57cec5SDimitry Andric     }
9090b57cec5SDimitry Andric   }
9100b57cec5SDimitry Andric }
9110b57cec5SDimitry Andric 
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,VariableList & variables)9129dba64beSDimitry Andric void SymbolFileDWARFDebugMap::FindGlobalVariables(
9135ffd83dbSDimitry Andric     ConstString name, const CompilerDeclContext &parent_decl_ctx,
9140b57cec5SDimitry Andric     uint32_t max_matches, VariableList &variables) {
9159dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
9160b57cec5SDimitry Andric   uint32_t total_matches = 0;
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
9199dba64beSDimitry Andric     const uint32_t old_size = variables.GetSize();
9209dba64beSDimitry Andric     oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches,
9219dba64beSDimitry Andric                                    variables);
9229dba64beSDimitry Andric     const uint32_t oso_matches = variables.GetSize() - old_size;
9230b57cec5SDimitry Andric     if (oso_matches > 0) {
9240b57cec5SDimitry Andric       total_matches += oso_matches;
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric       // Are we getting all matches?
9270b57cec5SDimitry Andric       if (max_matches == UINT32_MAX)
9280b57cec5SDimitry Andric         return false; // Yep, continue getting everything
9290b57cec5SDimitry Andric 
9300b57cec5SDimitry Andric       // If we have found enough matches, lets get out
9310b57cec5SDimitry Andric       if (max_matches >= total_matches)
9320b57cec5SDimitry Andric         return true;
9330b57cec5SDimitry Andric 
9340b57cec5SDimitry Andric       // Update the max matches for any subsequent calls to find globals in any
9350b57cec5SDimitry Andric       // other object files with DWARF
9360b57cec5SDimitry Andric       max_matches -= oso_matches;
9370b57cec5SDimitry Andric     }
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric     return false;
9400b57cec5SDimitry Andric   });
9410b57cec5SDimitry Andric }
9420b57cec5SDimitry Andric 
FindGlobalVariables(const RegularExpression & regex,uint32_t max_matches,VariableList & variables)9439dba64beSDimitry Andric void SymbolFileDWARFDebugMap::FindGlobalVariables(
9449dba64beSDimitry Andric     const RegularExpression &regex, uint32_t max_matches,
9450b57cec5SDimitry Andric     VariableList &variables) {
9469dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
9470b57cec5SDimitry Andric   uint32_t total_matches = 0;
9480b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
9499dba64beSDimitry Andric     const uint32_t old_size = variables.GetSize();
9500b57cec5SDimitry Andric     oso_dwarf->FindGlobalVariables(regex, max_matches, variables);
9519dba64beSDimitry Andric 
9529dba64beSDimitry Andric     const uint32_t oso_matches = variables.GetSize() - old_size;
9530b57cec5SDimitry Andric     if (oso_matches > 0) {
9540b57cec5SDimitry Andric       total_matches += oso_matches;
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric       // Are we getting all matches?
9570b57cec5SDimitry Andric       if (max_matches == UINT32_MAX)
9580b57cec5SDimitry Andric         return false; // Yep, continue getting everything
9590b57cec5SDimitry Andric 
9600b57cec5SDimitry Andric       // If we have found enough matches, lets get out
9610b57cec5SDimitry Andric       if (max_matches >= total_matches)
9620b57cec5SDimitry Andric         return true;
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric       // Update the max matches for any subsequent calls to find globals in any
9650b57cec5SDimitry Andric       // other object files with DWARF
9660b57cec5SDimitry Andric       max_matches -= oso_matches;
9670b57cec5SDimitry Andric     }
9680b57cec5SDimitry Andric 
9690b57cec5SDimitry Andric     return false;
9700b57cec5SDimitry Andric   });
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric 
SymbolContainsSymbolWithIndex(uint32_t * symbol_idx_ptr,const CompileUnitInfo * comp_unit_info)9730b57cec5SDimitry Andric int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex(
9740b57cec5SDimitry Andric     uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
9750b57cec5SDimitry Andric   const uint32_t symbol_idx = *symbol_idx_ptr;
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   if (symbol_idx < comp_unit_info->first_symbol_index)
9780b57cec5SDimitry Andric     return -1;
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric   if (symbol_idx <= comp_unit_info->last_symbol_index)
9810b57cec5SDimitry Andric     return 0;
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   return 1;
9840b57cec5SDimitry Andric }
9850b57cec5SDimitry Andric 
SymbolContainsSymbolWithID(user_id_t * symbol_idx_ptr,const CompileUnitInfo * comp_unit_info)9860b57cec5SDimitry Andric int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID(
9870b57cec5SDimitry Andric     user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) {
9880b57cec5SDimitry Andric   const user_id_t symbol_id = *symbol_idx_ptr;
9890b57cec5SDimitry Andric 
9900b57cec5SDimitry Andric   if (symbol_id < comp_unit_info->first_symbol_id)
9910b57cec5SDimitry Andric     return -1;
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric   if (symbol_id <= comp_unit_info->last_symbol_id)
9940b57cec5SDimitry Andric     return 0;
9950b57cec5SDimitry Andric 
9960b57cec5SDimitry Andric   return 1;
9970b57cec5SDimitry Andric }
9980b57cec5SDimitry Andric 
9990b57cec5SDimitry Andric SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfoForSymbolWithIndex(uint32_t symbol_idx,uint32_t * oso_idx_ptr)10000b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex(
10010b57cec5SDimitry Andric     uint32_t symbol_idx, uint32_t *oso_idx_ptr) {
10020b57cec5SDimitry Andric   const uint32_t oso_index_count = m_compile_unit_infos.size();
10030b57cec5SDimitry Andric   CompileUnitInfo *comp_unit_info = nullptr;
10040b57cec5SDimitry Andric   if (oso_index_count) {
10050b57cec5SDimitry Andric     comp_unit_info = (CompileUnitInfo *)bsearch(
10060b57cec5SDimitry Andric         &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
10070b57cec5SDimitry Andric         sizeof(CompileUnitInfo),
10080b57cec5SDimitry Andric         (ComparisonFunction)SymbolContainsSymbolWithIndex);
10090b57cec5SDimitry Andric   }
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric   if (oso_idx_ptr) {
10120b57cec5SDimitry Andric     if (comp_unit_info != nullptr)
10130b57cec5SDimitry Andric       *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
10140b57cec5SDimitry Andric     else
10150b57cec5SDimitry Andric       *oso_idx_ptr = UINT32_MAX;
10160b57cec5SDimitry Andric   }
10170b57cec5SDimitry Andric   return comp_unit_info;
10180b57cec5SDimitry Andric }
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfoForSymbolWithID(user_id_t symbol_id,uint32_t * oso_idx_ptr)10210b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID(
10220b57cec5SDimitry Andric     user_id_t symbol_id, uint32_t *oso_idx_ptr) {
10230b57cec5SDimitry Andric   const uint32_t oso_index_count = m_compile_unit_infos.size();
10240b57cec5SDimitry Andric   CompileUnitInfo *comp_unit_info = nullptr;
10250b57cec5SDimitry Andric   if (oso_index_count) {
10260b57cec5SDimitry Andric     comp_unit_info = (CompileUnitInfo *)::bsearch(
10270b57cec5SDimitry Andric         &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(),
10280b57cec5SDimitry Andric         sizeof(CompileUnitInfo),
10290b57cec5SDimitry Andric         (ComparisonFunction)SymbolContainsSymbolWithID);
10300b57cec5SDimitry Andric   }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric   if (oso_idx_ptr) {
10330b57cec5SDimitry Andric     if (comp_unit_info != nullptr)
10340b57cec5SDimitry Andric       *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0];
10350b57cec5SDimitry Andric     else
10360b57cec5SDimitry Andric       *oso_idx_ptr = UINT32_MAX;
10370b57cec5SDimitry Andric   }
10380b57cec5SDimitry Andric   return comp_unit_info;
10390b57cec5SDimitry Andric }
10400b57cec5SDimitry Andric 
RemoveFunctionsWithModuleNotEqualTo(const ModuleSP & module_sp,SymbolContextList & sc_list,uint32_t start_idx)10410b57cec5SDimitry Andric static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp,
10420b57cec5SDimitry Andric                                                 SymbolContextList &sc_list,
10430b57cec5SDimitry Andric                                                 uint32_t start_idx) {
10440b57cec5SDimitry Andric   // We found functions in .o files. Not all functions in the .o files will
10450b57cec5SDimitry Andric   // have made it into the final output file. The ones that did make it into
10460b57cec5SDimitry Andric   // the final output file will have a section whose module matches the module
10470b57cec5SDimitry Andric   // from the ObjectFile for this SymbolFile. When the modules don't match,
10480b57cec5SDimitry Andric   // then we have something that was in a .o file, but doesn't map to anything
10490b57cec5SDimitry Andric   // in the final executable.
10500b57cec5SDimitry Andric   uint32_t i = start_idx;
10510b57cec5SDimitry Andric   while (i < sc_list.GetSize()) {
10520b57cec5SDimitry Andric     SymbolContext sc;
10530b57cec5SDimitry Andric     sc_list.GetContextAtIndex(i, sc);
10540b57cec5SDimitry Andric     if (sc.function) {
10550b57cec5SDimitry Andric       const SectionSP section_sp(
10560b57cec5SDimitry Andric           sc.function->GetAddressRange().GetBaseAddress().GetSection());
10570b57cec5SDimitry Andric       if (section_sp->GetModule() != module_sp) {
10580b57cec5SDimitry Andric         sc_list.RemoveContextAtIndex(i);
10590b57cec5SDimitry Andric         continue;
10600b57cec5SDimitry Andric       }
10610b57cec5SDimitry Andric     }
10620b57cec5SDimitry Andric     ++i;
10630b57cec5SDimitry Andric   }
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric 
FindFunctions(const Module::LookupInfo & lookup_info,const CompilerDeclContext & parent_decl_ctx,bool include_inlines,SymbolContextList & sc_list)10669dba64beSDimitry Andric void SymbolFileDWARFDebugMap::FindFunctions(
1067bdd1243dSDimitry Andric     const Module::LookupInfo &lookup_info,
1068bdd1243dSDimitry Andric     const CompilerDeclContext &parent_decl_ctx, bool include_inlines,
10690b57cec5SDimitry Andric     SymbolContextList &sc_list) {
10709dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1071e8d8bef9SDimitry Andric   LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (name = %s)",
1072bdd1243dSDimitry Andric                      lookup_info.GetLookupName().GetCString());
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
10750b57cec5SDimitry Andric     uint32_t sc_idx = sc_list.GetSize();
1076bdd1243dSDimitry Andric     oso_dwarf->FindFunctions(lookup_info, parent_decl_ctx, include_inlines,
1077bdd1243dSDimitry Andric                              sc_list);
10789dba64beSDimitry Andric     if (!sc_list.IsEmpty()) {
10799dba64beSDimitry Andric       RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
10800b57cec5SDimitry Andric                                           sc_idx);
10810b57cec5SDimitry Andric     }
10820b57cec5SDimitry Andric     return false;
10830b57cec5SDimitry Andric   });
10840b57cec5SDimitry Andric }
10850b57cec5SDimitry Andric 
FindFunctions(const RegularExpression & regex,bool include_inlines,SymbolContextList & sc_list)10869dba64beSDimitry Andric void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression &regex,
10870b57cec5SDimitry Andric                                             bool include_inlines,
10880b57cec5SDimitry Andric                                             SymbolContextList &sc_list) {
10899dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1090e8d8bef9SDimitry Andric   LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')",
10910b57cec5SDimitry Andric                      regex.GetText().str().c_str());
10920b57cec5SDimitry Andric 
10930b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
10940b57cec5SDimitry Andric     uint32_t sc_idx = sc_list.GetSize();
10950b57cec5SDimitry Andric 
10969dba64beSDimitry Andric     oso_dwarf->FindFunctions(regex, include_inlines, sc_list);
10979dba64beSDimitry Andric     if (!sc_list.IsEmpty()) {
10989dba64beSDimitry Andric       RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list,
10990b57cec5SDimitry Andric                                           sc_idx);
11000b57cec5SDimitry Andric     }
11010b57cec5SDimitry Andric     return false;
11020b57cec5SDimitry Andric   });
11030b57cec5SDimitry Andric }
11040b57cec5SDimitry Andric 
GetTypes(SymbolContextScope * sc_scope,lldb::TypeClass type_mask,TypeList & type_list)11059dba64beSDimitry Andric void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope,
11060b57cec5SDimitry Andric                                        lldb::TypeClass type_mask,
11070b57cec5SDimitry Andric                                        TypeList &type_list) {
11089dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1109e8d8bef9SDimitry Andric   LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)",
11100b57cec5SDimitry Andric                      type_mask);
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = nullptr;
11130b57cec5SDimitry Andric   if (sc_scope) {
11140b57cec5SDimitry Andric     SymbolContext sc;
11150b57cec5SDimitry Andric     sc_scope->CalculateSymbolContext(&sc);
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric     CompileUnitInfo *cu_info = GetCompUnitInfo(sc);
11180b57cec5SDimitry Andric     if (cu_info) {
11190b57cec5SDimitry Andric       oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info);
11200b57cec5SDimitry Andric       if (oso_dwarf)
11210b57cec5SDimitry Andric         oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
11220b57cec5SDimitry Andric     }
11230b57cec5SDimitry Andric   } else {
11240b57cec5SDimitry Andric     ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
11250b57cec5SDimitry Andric       oso_dwarf->GetTypes(sc_scope, type_mask, type_list);
11260b57cec5SDimitry Andric       return false;
11270b57cec5SDimitry Andric     });
11280b57cec5SDimitry Andric   }
11290b57cec5SDimitry Andric }
11300b57cec5SDimitry Andric 
1131480093f4SDimitry Andric std::vector<std::unique_ptr<lldb_private::CallEdge>>
ParseCallEdgesInFunction(lldb_private::UserID func_id)1132fe013be4SDimitry Andric SymbolFileDWARFDebugMap::ParseCallEdgesInFunction(
1133fe013be4SDimitry Andric     lldb_private::UserID func_id) {
11340b57cec5SDimitry Andric   uint32_t oso_idx = GetOSOIndexFromUserID(func_id.GetID());
11350b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx);
11360b57cec5SDimitry Andric   if (oso_dwarf)
11370b57cec5SDimitry Andric     return oso_dwarf->ParseCallEdgesInFunction(func_id);
11380b57cec5SDimitry Andric   return {};
11390b57cec5SDimitry Andric }
11400b57cec5SDimitry Andric 
FindDefinitionTypeForDWARFDeclContext(const DWARFDIE & die)11410b57cec5SDimitry Andric TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext(
1142bdd1243dSDimitry Andric     const DWARFDIE &die) {
11430b57cec5SDimitry Andric   TypeSP type_sp;
11440b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1145bdd1243dSDimitry Andric     type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die);
11460b57cec5SDimitry Andric     return ((bool)type_sp);
11470b57cec5SDimitry Andric   });
11480b57cec5SDimitry Andric   return type_sp;
11490b57cec5SDimitry Andric }
11500b57cec5SDimitry Andric 
Supports_DW_AT_APPLE_objc_complete_type(SymbolFileDWARF * skip_dwarf_oso)11510b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type(
11520b57cec5SDimitry Andric     SymbolFileDWARF *skip_dwarf_oso) {
11530b57cec5SDimitry Andric   if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) {
11540b57cec5SDimitry Andric     m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
11550b57cec5SDimitry Andric     ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
11560b57cec5SDimitry Andric       if (skip_dwarf_oso != oso_dwarf &&
11570b57cec5SDimitry Andric           oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(nullptr)) {
11580b57cec5SDimitry Andric         m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
11590b57cec5SDimitry Andric         return true;
11600b57cec5SDimitry Andric       }
11610b57cec5SDimitry Andric       return false;
11620b57cec5SDimitry Andric     });
11630b57cec5SDimitry Andric   }
11640b57cec5SDimitry Andric   return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
11650b57cec5SDimitry Andric }
11660b57cec5SDimitry Andric 
FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE & die,ConstString type_name,bool must_be_implementation)11670b57cec5SDimitry Andric TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE(
11680b57cec5SDimitry Andric     const DWARFDIE &die, ConstString type_name,
11690b57cec5SDimitry Andric     bool must_be_implementation) {
11700b57cec5SDimitry Andric   // If we have a debug map, we will have an Objective-C symbol whose name is
11710b57cec5SDimitry Andric   // the type name and whose type is eSymbolTypeObjCClass. If we can find that
11720b57cec5SDimitry Andric   // symbol and find its containing parent, we can locate the .o file that will
11730b57cec5SDimitry Andric   // contain the implementation definition since it will be scoped inside the
11740b57cec5SDimitry Andric   // N_SO and we can then locate the SymbolFileDWARF that corresponds to that
11750b57cec5SDimitry Andric   // N_SO.
11760b57cec5SDimitry Andric   SymbolFileDWARF *oso_dwarf = nullptr;
11770b57cec5SDimitry Andric   TypeSP type_sp;
11789dba64beSDimitry Andric   ObjectFile *module_objfile = m_objfile_sp->GetModule()->GetObjectFile();
11790b57cec5SDimitry Andric   if (module_objfile) {
11800b57cec5SDimitry Andric     Symtab *symtab = module_objfile->GetSymtab();
11810b57cec5SDimitry Andric     if (symtab) {
11820b57cec5SDimitry Andric       Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType(
11830b57cec5SDimitry Andric           type_name, eSymbolTypeObjCClass, Symtab::eDebugAny,
11840b57cec5SDimitry Andric           Symtab::eVisibilityAny);
11850b57cec5SDimitry Andric       if (objc_class_symbol) {
11860b57cec5SDimitry Andric         // Get the N_SO symbol that contains the objective C class symbol as
11870b57cec5SDimitry Andric         // this should be the .o file that contains the real definition...
11880b57cec5SDimitry Andric         const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol);
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric         if (source_file_symbol &&
11910b57cec5SDimitry Andric             source_file_symbol->GetType() == eSymbolTypeSourceFile) {
11920b57cec5SDimitry Andric           const uint32_t source_file_symbol_idx =
11930b57cec5SDimitry Andric               symtab->GetIndexForSymbol(source_file_symbol);
11940b57cec5SDimitry Andric           if (source_file_symbol_idx != UINT32_MAX) {
11950b57cec5SDimitry Andric             CompileUnitInfo *compile_unit_info =
11960b57cec5SDimitry Andric                 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx,
11970b57cec5SDimitry Andric                                                      nullptr);
11980b57cec5SDimitry Andric             if (compile_unit_info) {
11990b57cec5SDimitry Andric               oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info);
12000b57cec5SDimitry Andric               if (oso_dwarf) {
12010b57cec5SDimitry Andric                 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
12020b57cec5SDimitry Andric                     die, type_name, must_be_implementation));
12030b57cec5SDimitry Andric                 if (type_sp) {
12040b57cec5SDimitry Andric                   return type_sp;
12050b57cec5SDimitry Andric                 }
12060b57cec5SDimitry Andric               }
12070b57cec5SDimitry Andric             }
12080b57cec5SDimitry Andric           }
12090b57cec5SDimitry Andric         }
12100b57cec5SDimitry Andric       }
12110b57cec5SDimitry Andric     }
12120b57cec5SDimitry Andric   }
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric   // Only search all .o files for the definition if we don't need the
12150b57cec5SDimitry Andric   // implementation because otherwise, with a valid debug map we should have
12160b57cec5SDimitry Andric   // the ObjC class symbol and the code above should have found it.
12170b57cec5SDimitry Andric   if (!must_be_implementation) {
12180b57cec5SDimitry Andric     TypeSP type_sp;
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric     ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
12210b57cec5SDimitry Andric       type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE(
12220b57cec5SDimitry Andric           die, type_name, must_be_implementation);
12230b57cec5SDimitry Andric       return (bool)type_sp;
12240b57cec5SDimitry Andric     });
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric     return type_sp;
12270b57cec5SDimitry Andric   }
12280b57cec5SDimitry Andric   return TypeSP();
12290b57cec5SDimitry Andric }
12300b57cec5SDimitry Andric 
FindTypes(const TypeQuery & query,TypeResults & results)1231c9157d92SDimitry Andric void SymbolFileDWARFDebugMap::FindTypes(const TypeQuery &query,
1232c9157d92SDimitry Andric                                         TypeResults &results) {
12339dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
12340b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1235c9157d92SDimitry Andric     oso_dwarf->FindTypes(query, results);
1236c9157d92SDimitry Andric     return !results.Done(query); // Keep iterating if we aren't done.
1237480093f4SDimitry Andric   });
1238480093f4SDimitry Andric }
1239480093f4SDimitry Andric 
FindNamespace(lldb_private::ConstString name,const CompilerDeclContext & parent_decl_ctx,bool only_root_namespaces)12400b57cec5SDimitry Andric CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace(
1241fe013be4SDimitry Andric     lldb_private::ConstString name, const CompilerDeclContext &parent_decl_ctx,
1242fe013be4SDimitry Andric     bool only_root_namespaces) {
12439dba64beSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
12440b57cec5SDimitry Andric   CompilerDeclContext matching_namespace;
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1247fe013be4SDimitry Andric     matching_namespace =
1248fe013be4SDimitry Andric         oso_dwarf->FindNamespace(name, parent_decl_ctx, only_root_namespaces);
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric     return (bool)matching_namespace;
12510b57cec5SDimitry Andric   });
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric   return matching_namespace;
12540b57cec5SDimitry Andric }
12550b57cec5SDimitry Andric 
DumpClangAST(Stream & s)12560b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) {
12570b57cec5SDimitry Andric   ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool {
12580b57cec5SDimitry Andric     oso_dwarf->DumpClangAST(s);
12595ffd83dbSDimitry Andric     // The underlying assumption is that DumpClangAST(...) will obtain the
12605ffd83dbSDimitry Andric     // AST from the underlying TypeSystem and therefore we only need to do
12615ffd83dbSDimitry Andric     // this once and can stop after the first iteration hence we return true.
12620b57cec5SDimitry Andric     return true;
12630b57cec5SDimitry Andric   });
12640b57cec5SDimitry Andric }
12650b57cec5SDimitry Andric 
GetSeparateDebugInfo(lldb_private::StructuredData::Dictionary & d,bool errors_only)1266c9157d92SDimitry Andric bool SymbolFileDWARFDebugMap::GetSeparateDebugInfo(
1267c9157d92SDimitry Andric     lldb_private::StructuredData::Dictionary &d, bool errors_only) {
1268c9157d92SDimitry Andric   StructuredData::Array separate_debug_info_files;
1269c9157d92SDimitry Andric   const uint32_t cu_count = GetNumCompileUnits();
1270c9157d92SDimitry Andric   for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
1271c9157d92SDimitry Andric     const auto &info = m_compile_unit_infos[cu_idx];
1272c9157d92SDimitry Andric     StructuredData::DictionarySP oso_data =
1273c9157d92SDimitry Andric         std::make_shared<StructuredData::Dictionary>();
1274c9157d92SDimitry Andric     oso_data->AddStringItem("so_file", info.so_file.GetPath());
1275c9157d92SDimitry Andric     oso_data->AddStringItem("oso_path", info.oso_path);
1276c9157d92SDimitry Andric     oso_data->AddIntegerItem("oso_mod_time",
1277c9157d92SDimitry Andric                              (uint32_t)llvm::sys::toTimeT(info.oso_mod_time));
1278c9157d92SDimitry Andric 
1279c9157d92SDimitry Andric     bool loaded_successfully = false;
1280c9157d92SDimitry Andric     if (GetModuleByOSOIndex(cu_idx)) {
1281c9157d92SDimitry Andric       // If we have a valid pointer to the module, we successfully
1282c9157d92SDimitry Andric       // loaded the oso if there are no load errors.
1283c9157d92SDimitry Andric       if (!info.oso_load_error.Fail()) {
1284c9157d92SDimitry Andric         loaded_successfully = true;
1285c9157d92SDimitry Andric       }
1286c9157d92SDimitry Andric     }
1287c9157d92SDimitry Andric     if (!loaded_successfully) {
1288c9157d92SDimitry Andric       oso_data->AddStringItem("error", info.oso_load_error.AsCString());
1289c9157d92SDimitry Andric     }
1290c9157d92SDimitry Andric     oso_data->AddBooleanItem("loaded", loaded_successfully);
1291c9157d92SDimitry Andric     if (!errors_only || oso_data->HasKey("error"))
1292c9157d92SDimitry Andric       separate_debug_info_files.AddItem(oso_data);
1293c9157d92SDimitry Andric   }
1294c9157d92SDimitry Andric 
1295c9157d92SDimitry Andric   d.AddStringItem("type", "oso");
1296c9157d92SDimitry Andric   d.AddStringItem("symfile", GetMainObjectFile()->GetFileSpec().GetPath());
1297c9157d92SDimitry Andric   d.AddItem("separate-debug-info-files",
1298c9157d92SDimitry Andric             std::make_shared<StructuredData::Array>(
1299c9157d92SDimitry Andric                 std::move(separate_debug_info_files)));
1300c9157d92SDimitry Andric   return true;
1301c9157d92SDimitry Andric }
1302c9157d92SDimitry Andric 
13030b57cec5SDimitry Andric lldb::CompUnitSP
GetCompileUnit(SymbolFileDWARF * oso_dwarf,DWARFCompileUnit & dwarf_cu)1304bdd1243dSDimitry Andric SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf, DWARFCompileUnit &dwarf_cu) {
13050b57cec5SDimitry Andric   if (oso_dwarf) {
13060b57cec5SDimitry Andric     const uint32_t cu_count = GetNumCompileUnits();
13070b57cec5SDimitry Andric     for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
13080b57cec5SDimitry Andric       SymbolFileDWARF *oso_symfile =
13090b57cec5SDimitry Andric           GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
13100b57cec5SDimitry Andric       if (oso_symfile == oso_dwarf) {
1311bdd1243dSDimitry Andric         if (m_compile_unit_infos[cu_idx].compile_units_sps.empty())
13120b57cec5SDimitry Andric           ParseCompileUnitAtIndex(cu_idx);
13130b57cec5SDimitry Andric 
1314bdd1243dSDimitry Andric         auto &id_to_index_map = m_compile_unit_infos[cu_idx].id_to_index_map;
1315bdd1243dSDimitry Andric         auto it = id_to_index_map.find(dwarf_cu.GetID());
1316bdd1243dSDimitry Andric         if (it != id_to_index_map.end())
1317bdd1243dSDimitry Andric           return m_compile_unit_infos[cu_idx]
1318bdd1243dSDimitry Andric               .compile_units_sps[it->getSecond()];
13190b57cec5SDimitry Andric       }
13200b57cec5SDimitry Andric     }
13210b57cec5SDimitry Andric   }
13220b57cec5SDimitry Andric   llvm_unreachable("this shouldn't happen");
13230b57cec5SDimitry Andric }
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric SymbolFileDWARFDebugMap::CompileUnitInfo *
GetCompileUnitInfo(SymbolFileDWARF * oso_dwarf)13260b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) {
13270b57cec5SDimitry Andric   if (oso_dwarf) {
13280b57cec5SDimitry Andric     const uint32_t cu_count = GetNumCompileUnits();
13290b57cec5SDimitry Andric     for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
13300b57cec5SDimitry Andric       SymbolFileDWARF *oso_symfile =
13310b57cec5SDimitry Andric           GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
13320b57cec5SDimitry Andric       if (oso_symfile == oso_dwarf) {
13330b57cec5SDimitry Andric         return &m_compile_unit_infos[cu_idx];
13340b57cec5SDimitry Andric       }
13350b57cec5SDimitry Andric     }
13360b57cec5SDimitry Andric   }
13370b57cec5SDimitry Andric   return nullptr;
13380b57cec5SDimitry Andric }
13390b57cec5SDimitry Andric 
SetCompileUnit(SymbolFileDWARF * oso_dwarf,const CompUnitSP & cu_sp)13400b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf,
13410b57cec5SDimitry Andric                                              const CompUnitSP &cu_sp) {
13420b57cec5SDimitry Andric   if (oso_dwarf) {
13430b57cec5SDimitry Andric     const uint32_t cu_count = GetNumCompileUnits();
13440b57cec5SDimitry Andric     for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) {
13450b57cec5SDimitry Andric       SymbolFileDWARF *oso_symfile =
13460b57cec5SDimitry Andric           GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]);
13470b57cec5SDimitry Andric       if (oso_symfile == oso_dwarf) {
1348bdd1243dSDimitry Andric         if (!m_compile_unit_infos[cu_idx].compile_units_sps.empty()) {
1349bdd1243dSDimitry Andric           assert(m_compile_unit_infos[cu_idx].compile_units_sps[0].get() ==
13500b57cec5SDimitry Andric                  cu_sp.get());
13510b57cec5SDimitry Andric         } else {
1352bdd1243dSDimitry Andric           assert(cu_sp->GetID() == 0 &&
1353bdd1243dSDimitry Andric                  "Setting first compile unit but with id different than 0!");
1354bdd1243dSDimitry Andric           auto &compile_units_sps = m_compile_unit_infos[cu_idx].compile_units_sps;
1355bdd1243dSDimitry Andric           compile_units_sps.push_back(cu_sp);
1356bdd1243dSDimitry Andric           m_compile_unit_infos[cu_idx].id_to_index_map.insert(
1357bdd1243dSDimitry Andric               {cu_sp->GetID(), compile_units_sps.size() - 1});
1358bdd1243dSDimitry Andric 
13599dba64beSDimitry Andric           SetCompileUnitAtIndex(cu_idx, cu_sp);
13600b57cec5SDimitry Andric         }
13610b57cec5SDimitry Andric       }
13620b57cec5SDimitry Andric     }
13630b57cec5SDimitry Andric   }
13640b57cec5SDimitry Andric }
13650b57cec5SDimitry Andric 
13660b57cec5SDimitry Andric CompilerDeclContext
GetDeclContextForUID(lldb::user_id_t type_uid)13670b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) {
13680b57cec5SDimitry Andric   const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1369c9157d92SDimitry Andric   if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
13700b57cec5SDimitry Andric     return oso_dwarf->GetDeclContextForUID(type_uid);
1371c9157d92SDimitry Andric   return {};
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric CompilerDeclContext
GetDeclContextContainingUID(lldb::user_id_t type_uid)13750b57cec5SDimitry Andric SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) {
13760b57cec5SDimitry Andric   const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1377c9157d92SDimitry Andric   if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
13780b57cec5SDimitry Andric     return oso_dwarf->GetDeclContextContainingUID(type_uid);
1379c9157d92SDimitry Andric   return {};
1380c9157d92SDimitry Andric }
1381c9157d92SDimitry Andric 
1382c9157d92SDimitry Andric std::vector<CompilerContext>
GetCompilerContextForUID(lldb::user_id_t type_uid)1383c9157d92SDimitry Andric SymbolFileDWARFDebugMap::GetCompilerContextForUID(lldb::user_id_t type_uid) {
1384c9157d92SDimitry Andric   const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid);
1385c9157d92SDimitry Andric   if (SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx))
1386c9157d92SDimitry Andric     return oso_dwarf->GetCompilerContextForUID(type_uid);
1387c9157d92SDimitry Andric   return {};
13880b57cec5SDimitry Andric }
13890b57cec5SDimitry Andric 
ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx)13900b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::ParseDeclsForContext(
13910b57cec5SDimitry Andric     lldb_private::CompilerDeclContext decl_ctx) {
13920b57cec5SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
13930b57cec5SDimitry Andric     oso_dwarf->ParseDeclsForContext(decl_ctx);
13940b57cec5SDimitry Andric     return true; // Keep iterating
13950b57cec5SDimitry Andric   });
13960b57cec5SDimitry Andric }
13970b57cec5SDimitry Andric 
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)13980b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info,
13990b57cec5SDimitry Andric                                               lldb::addr_t exe_file_addr,
14000b57cec5SDimitry Andric                                               lldb::addr_t exe_byte_size,
14010b57cec5SDimitry Andric                                               lldb::addr_t oso_file_addr,
14020b57cec5SDimitry Andric                                               lldb::addr_t oso_byte_size) {
14030b57cec5SDimitry Andric   const uint32_t debug_map_idx =
14040b57cec5SDimitry Andric       m_debug_map.FindEntryIndexThatContains(exe_file_addr);
14050b57cec5SDimitry Andric   if (debug_map_idx != UINT32_MAX) {
14060b57cec5SDimitry Andric     DebugMap::Entry *debug_map_entry =
14070b57cec5SDimitry Andric         m_debug_map.FindEntryThatContains(exe_file_addr);
14080b57cec5SDimitry Andric     debug_map_entry->data.SetOSOFileAddress(oso_file_addr);
14090b57cec5SDimitry Andric     addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size);
14100b57cec5SDimitry Andric     if (range_size == 0) {
14110b57cec5SDimitry Andric       range_size = std::max<addr_t>(exe_byte_size, oso_byte_size);
14120b57cec5SDimitry Andric       if (range_size == 0)
14130b57cec5SDimitry Andric         range_size = 1;
14140b57cec5SDimitry Andric     }
14150b57cec5SDimitry Andric     cu_info->file_range_map.Append(
14160b57cec5SDimitry Andric         FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr));
14170b57cec5SDimitry Andric     return true;
14180b57cec5SDimitry Andric   }
14190b57cec5SDimitry Andric   return false;
14200b57cec5SDimitry Andric }
14210b57cec5SDimitry Andric 
FinalizeOSOFileRanges(CompileUnitInfo * cu_info)14220b57cec5SDimitry Andric void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) {
14230b57cec5SDimitry Andric   cu_info->file_range_map.Sort();
14240b57cec5SDimitry Andric #if defined(DEBUG_OSO_DMAP)
14250b57cec5SDimitry Andric   const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this);
14260b57cec5SDimitry Andric   const size_t n = oso_file_range_map.GetSize();
14270b57cec5SDimitry Andric   printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n",
14280b57cec5SDimitry Andric          cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str());
14290b57cec5SDimitry Andric   for (size_t i = 0; i < n; ++i) {
14300b57cec5SDimitry Andric     const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i);
14310b57cec5SDimitry Andric     printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64
14320b57cec5SDimitry Andric            ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n",
14330b57cec5SDimitry Andric            entry.GetRangeBase(), entry.GetRangeEnd(), entry.data,
14340b57cec5SDimitry Andric            entry.data + entry.GetByteSize());
14350b57cec5SDimitry Andric   }
14360b57cec5SDimitry Andric #endif
14370b57cec5SDimitry Andric }
14380b57cec5SDimitry Andric 
14390b57cec5SDimitry Andric lldb::addr_t
LinkOSOFileAddress(SymbolFileDWARF * oso_symfile,lldb::addr_t oso_file_addr)14400b57cec5SDimitry Andric SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile,
14410b57cec5SDimitry Andric                                             lldb::addr_t oso_file_addr) {
14420b57cec5SDimitry Andric   CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile);
14430b57cec5SDimitry Andric   if (cu_info) {
14440b57cec5SDimitry Andric     const FileRangeMap::Entry *oso_range_entry =
14450b57cec5SDimitry Andric         cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
14460b57cec5SDimitry Andric     if (oso_range_entry) {
14470b57cec5SDimitry Andric       const DebugMap::Entry *debug_map_entry =
14480b57cec5SDimitry Andric           m_debug_map.FindEntryThatContains(oso_range_entry->data);
14490b57cec5SDimitry Andric       if (debug_map_entry) {
14500b57cec5SDimitry Andric         const lldb::addr_t offset =
14510b57cec5SDimitry Andric             oso_file_addr - oso_range_entry->GetRangeBase();
14520b57cec5SDimitry Andric         const lldb::addr_t exe_file_addr =
14530b57cec5SDimitry Andric             debug_map_entry->GetRangeBase() + offset;
14540b57cec5SDimitry Andric         return exe_file_addr;
14550b57cec5SDimitry Andric       }
14560b57cec5SDimitry Andric     }
14570b57cec5SDimitry Andric   }
14580b57cec5SDimitry Andric   return LLDB_INVALID_ADDRESS;
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric 
LinkOSOAddress(Address & addr)14610b57cec5SDimitry Andric bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) {
14620b57cec5SDimitry Andric   // Make sure this address hasn't been fixed already
14630b57cec5SDimitry Andric   Module *exe_module = GetObjectFile()->GetModule().get();
14640b57cec5SDimitry Andric   Module *addr_module = addr.GetModule().get();
14650b57cec5SDimitry Andric   if (addr_module == exe_module)
14660b57cec5SDimitry Andric     return true; // Address is already in terms of the main executable module
14670b57cec5SDimitry Andric 
14689dba64beSDimitry Andric   CompileUnitInfo *cu_info = GetCompileUnitInfo(
14699dba64beSDimitry Andric       GetSymbolFileAsSymbolFileDWARF(addr_module->GetSymbolFile()));
14700b57cec5SDimitry Andric   if (cu_info) {
14710b57cec5SDimitry Andric     const lldb::addr_t oso_file_addr = addr.GetFileAddress();
14720b57cec5SDimitry Andric     const FileRangeMap::Entry *oso_range_entry =
14730b57cec5SDimitry Andric         cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr);
14740b57cec5SDimitry Andric     if (oso_range_entry) {
14750b57cec5SDimitry Andric       const DebugMap::Entry *debug_map_entry =
14760b57cec5SDimitry Andric           m_debug_map.FindEntryThatContains(oso_range_entry->data);
14770b57cec5SDimitry Andric       if (debug_map_entry) {
14780b57cec5SDimitry Andric         const lldb::addr_t offset =
14790b57cec5SDimitry Andric             oso_file_addr - oso_range_entry->GetRangeBase();
14800b57cec5SDimitry Andric         const lldb::addr_t exe_file_addr =
14810b57cec5SDimitry Andric             debug_map_entry->GetRangeBase() + offset;
14820b57cec5SDimitry Andric         return exe_module->ResolveFileAddress(exe_file_addr, addr);
14830b57cec5SDimitry Andric       }
14840b57cec5SDimitry Andric     }
14850b57cec5SDimitry Andric   }
14860b57cec5SDimitry Andric   return true;
14870b57cec5SDimitry Andric }
14880b57cec5SDimitry Andric 
LinkOSOLineTable(SymbolFileDWARF * oso_dwarf,LineTable * line_table)14890b57cec5SDimitry Andric LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf,
14900b57cec5SDimitry Andric                                                      LineTable *line_table) {
14910b57cec5SDimitry Andric   CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf);
14920b57cec5SDimitry Andric   if (cu_info)
14930b57cec5SDimitry Andric     return line_table->LinkLineTable(cu_info->GetFileRangeMap(this));
14940b57cec5SDimitry Andric   return nullptr;
14950b57cec5SDimitry Andric }
14960b57cec5SDimitry Andric 
14970b57cec5SDimitry Andric size_t
AddOSOARanges(SymbolFileDWARF * dwarf2Data,DWARFDebugAranges * debug_aranges)14980b57cec5SDimitry Andric SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data,
14990b57cec5SDimitry Andric                                        DWARFDebugAranges *debug_aranges) {
15000b57cec5SDimitry Andric   size_t num_line_entries_added = 0;
15010b57cec5SDimitry Andric   if (debug_aranges && dwarf2Data) {
15020b57cec5SDimitry Andric     CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data);
15030b57cec5SDimitry Andric     if (compile_unit_info) {
15040b57cec5SDimitry Andric       const FileRangeMap &file_range_map =
15050b57cec5SDimitry Andric           compile_unit_info->GetFileRangeMap(this);
15060b57cec5SDimitry Andric       for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) {
15070b57cec5SDimitry Andric         const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx);
15080b57cec5SDimitry Andric         if (entry) {
1509fe013be4SDimitry Andric           debug_aranges->AppendRange(*dwarf2Data->GetFileIndex(),
1510fe013be4SDimitry Andric                                      entry->GetRangeBase(),
15110b57cec5SDimitry Andric                                      entry->GetRangeEnd());
15120b57cec5SDimitry Andric           num_line_entries_added++;
15130b57cec5SDimitry Andric         }
15140b57cec5SDimitry Andric       }
15150b57cec5SDimitry Andric     }
15160b57cec5SDimitry Andric   }
15170b57cec5SDimitry Andric   return num_line_entries_added;
15180b57cec5SDimitry Andric }
1519349cc55cSDimitry Andric 
GetDebugInfoModules()152081ad6265SDimitry Andric ModuleList SymbolFileDWARFDebugMap::GetDebugInfoModules() {
152181ad6265SDimitry Andric   ModuleList oso_modules;
1522349cc55cSDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1523349cc55cSDimitry Andric     ObjectFile *oso_objfile = oso_dwarf->GetObjectFile();
1524349cc55cSDimitry Andric     if (oso_objfile) {
1525349cc55cSDimitry Andric       ModuleSP module_sp = oso_objfile->GetModule();
152681ad6265SDimitry Andric       if (module_sp)
152781ad6265SDimitry Andric         oso_modules.Append(module_sp);
1528349cc55cSDimitry Andric     }
1529349cc55cSDimitry Andric     return false; // Keep iterating
1530349cc55cSDimitry Andric   });
153181ad6265SDimitry Andric   return oso_modules;
1532349cc55cSDimitry Andric }
1533bdd1243dSDimitry Andric 
CalculateFrameVariableError(StackFrame & frame)1534bdd1243dSDimitry Andric Status SymbolFileDWARFDebugMap::CalculateFrameVariableError(StackFrame &frame) {
1535bdd1243dSDimitry Andric   std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1536bdd1243dSDimitry Andric 
1537bdd1243dSDimitry Andric   // We need to make sure that our PC value from the frame matches the module
1538bdd1243dSDimitry Andric   // for this object file since we will lookup the PC file address in the debug
1539bdd1243dSDimitry Andric   // map below.
1540bdd1243dSDimitry Andric   Address pc_addr = frame.GetFrameCodeAddress();
1541bdd1243dSDimitry Andric   if (pc_addr.GetModule() == m_objfile_sp->GetModule()) {
1542bdd1243dSDimitry Andric     Symtab *symtab = m_objfile_sp->GetSymtab();
1543bdd1243dSDimitry Andric     if (symtab) {
1544bdd1243dSDimitry Andric       const DebugMap::Entry *debug_map_entry =
1545bdd1243dSDimitry Andric           m_debug_map.FindEntryThatContains(pc_addr.GetFileAddress());
1546bdd1243dSDimitry Andric       if (debug_map_entry) {
1547bdd1243dSDimitry Andric         Symbol *symbol =
1548bdd1243dSDimitry Andric             symtab->SymbolAtIndex(debug_map_entry->data.GetExeSymbolIndex());
1549bdd1243dSDimitry Andric         if (symbol) {
1550bdd1243dSDimitry Andric           uint32_t oso_idx = 0;
1551bdd1243dSDimitry Andric           CompileUnitInfo *comp_unit_info =
1552bdd1243dSDimitry Andric               GetCompileUnitInfoForSymbolWithID(symbol->GetID(), &oso_idx);
1553bdd1243dSDimitry Andric           if (comp_unit_info) {
1554bdd1243dSDimitry Andric             Module *oso_module = GetModuleByCompUnitInfo(comp_unit_info);
1555bdd1243dSDimitry Andric             if (oso_module) {
1556bdd1243dSDimitry Andric               // Check the .o file's DWARF in case it has an error to display.
1557bdd1243dSDimitry Andric               SymbolFile *oso_sym_file = oso_module->GetSymbolFile();
1558bdd1243dSDimitry Andric               if (oso_sym_file)
1559bdd1243dSDimitry Andric                 return oso_sym_file->GetFrameVariableError(frame);
1560bdd1243dSDimitry Andric             }
1561bdd1243dSDimitry Andric             // If we don't have a valid OSO module here, then something went
1562bdd1243dSDimitry Andric             // wrong as we have a symbol for the address in the debug map, but
1563bdd1243dSDimitry Andric             // we weren't able to open the .o file. Display an appropriate
1564bdd1243dSDimitry Andric             // error
1565bdd1243dSDimitry Andric             if (comp_unit_info->oso_load_error.Fail())
1566bdd1243dSDimitry Andric               return comp_unit_info->oso_load_error;
1567bdd1243dSDimitry Andric             else
1568bdd1243dSDimitry Andric               return Status("unable to load debug map object file \"%s\" "
1569bdd1243dSDimitry Andric                             "exist, debug info will not be loaded",
1570bdd1243dSDimitry Andric                             comp_unit_info->oso_path.GetCString());
1571bdd1243dSDimitry Andric           }
1572bdd1243dSDimitry Andric         }
1573bdd1243dSDimitry Andric       }
1574bdd1243dSDimitry Andric     }
1575bdd1243dSDimitry Andric   }
1576bdd1243dSDimitry Andric   return Status();
1577bdd1243dSDimitry Andric }
1578fe013be4SDimitry Andric 
GetCompileOptions(std::unordered_map<lldb::CompUnitSP,lldb_private::Args> & args)1579fe013be4SDimitry Andric void SymbolFileDWARFDebugMap::GetCompileOptions(
1580fe013be4SDimitry Andric     std::unordered_map<lldb::CompUnitSP, lldb_private::Args> &args) {
1581fe013be4SDimitry Andric 
1582fe013be4SDimitry Andric   ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool {
1583fe013be4SDimitry Andric     oso_dwarf->GetCompileOptions(args);
1584fe013be4SDimitry Andric     return false;
1585fe013be4SDimitry Andric   });
1586fe013be4SDimitry Andric }
1587