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