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() {} 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 FileSpec &file_spec, uint32_t line, bool check_inlines, 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 = check_inlines; 819 820 if (!resolve) { 821 FileSpec so_file_spec; 822 if (GetFileSpecForSO(i, so_file_spec)) 823 resolve = FileSpec::Match(file_spec, so_file_spec); 824 } 825 if (resolve) { 826 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(i); 827 if (oso_dwarf) 828 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, 829 resolve_scope, sc_list); 830 } 831 } 832 return sc_list.GetSize() - initial; 833 } 834 835 void SymbolFileDWARFDebugMap::PrivateFindGlobalVariables( 836 ConstString name, const CompilerDeclContext &parent_decl_ctx, 837 const std::vector<uint32_t> 838 &indexes, // Indexes into the symbol table that match "name" 839 uint32_t max_matches, VariableList &variables) { 840 const size_t match_count = indexes.size(); 841 for (size_t i = 0; i < match_count; ++i) { 842 uint32_t oso_idx; 843 CompileUnitInfo *comp_unit_info = 844 GetCompileUnitInfoForSymbolWithIndex(indexes[i], &oso_idx); 845 if (comp_unit_info) { 846 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); 847 if (oso_dwarf) { 848 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches, 849 variables); 850 if (variables.GetSize() > max_matches) 851 break; 852 } 853 } 854 } 855 } 856 857 void SymbolFileDWARFDebugMap::FindGlobalVariables( 858 ConstString name, const CompilerDeclContext &parent_decl_ctx, 859 uint32_t max_matches, VariableList &variables) { 860 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 861 uint32_t total_matches = 0; 862 863 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 864 const uint32_t old_size = variables.GetSize(); 865 oso_dwarf->FindGlobalVariables(name, parent_decl_ctx, max_matches, 866 variables); 867 const uint32_t oso_matches = variables.GetSize() - old_size; 868 if (oso_matches > 0) { 869 total_matches += oso_matches; 870 871 // Are we getting all matches? 872 if (max_matches == UINT32_MAX) 873 return false; // Yep, continue getting everything 874 875 // If we have found enough matches, lets get out 876 if (max_matches >= total_matches) 877 return true; 878 879 // Update the max matches for any subsequent calls to find globals in any 880 // other object files with DWARF 881 max_matches -= oso_matches; 882 } 883 884 return false; 885 }); 886 } 887 888 void SymbolFileDWARFDebugMap::FindGlobalVariables( 889 const RegularExpression ®ex, uint32_t max_matches, 890 VariableList &variables) { 891 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 892 uint32_t total_matches = 0; 893 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 894 const uint32_t old_size = variables.GetSize(); 895 oso_dwarf->FindGlobalVariables(regex, max_matches, variables); 896 897 const uint32_t oso_matches = variables.GetSize() - old_size; 898 if (oso_matches > 0) { 899 total_matches += oso_matches; 900 901 // Are we getting all matches? 902 if (max_matches == UINT32_MAX) 903 return false; // Yep, continue getting everything 904 905 // If we have found enough matches, lets get out 906 if (max_matches >= total_matches) 907 return true; 908 909 // Update the max matches for any subsequent calls to find globals in any 910 // other object files with DWARF 911 max_matches -= oso_matches; 912 } 913 914 return false; 915 }); 916 } 917 918 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex( 919 uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) { 920 const uint32_t symbol_idx = *symbol_idx_ptr; 921 922 if (symbol_idx < comp_unit_info->first_symbol_index) 923 return -1; 924 925 if (symbol_idx <= comp_unit_info->last_symbol_index) 926 return 0; 927 928 return 1; 929 } 930 931 int SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID( 932 user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) { 933 const user_id_t symbol_id = *symbol_idx_ptr; 934 935 if (symbol_id < comp_unit_info->first_symbol_id) 936 return -1; 937 938 if (symbol_id <= comp_unit_info->last_symbol_id) 939 return 0; 940 941 return 1; 942 } 943 944 SymbolFileDWARFDebugMap::CompileUnitInfo * 945 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex( 946 uint32_t symbol_idx, uint32_t *oso_idx_ptr) { 947 const uint32_t oso_index_count = m_compile_unit_infos.size(); 948 CompileUnitInfo *comp_unit_info = nullptr; 949 if (oso_index_count) { 950 comp_unit_info = (CompileUnitInfo *)bsearch( 951 &symbol_idx, &m_compile_unit_infos[0], m_compile_unit_infos.size(), 952 sizeof(CompileUnitInfo), 953 (ComparisonFunction)SymbolContainsSymbolWithIndex); 954 } 955 956 if (oso_idx_ptr) { 957 if (comp_unit_info != nullptr) 958 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; 959 else 960 *oso_idx_ptr = UINT32_MAX; 961 } 962 return comp_unit_info; 963 } 964 965 SymbolFileDWARFDebugMap::CompileUnitInfo * 966 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID( 967 user_id_t symbol_id, uint32_t *oso_idx_ptr) { 968 const uint32_t oso_index_count = m_compile_unit_infos.size(); 969 CompileUnitInfo *comp_unit_info = nullptr; 970 if (oso_index_count) { 971 comp_unit_info = (CompileUnitInfo *)::bsearch( 972 &symbol_id, &m_compile_unit_infos[0], m_compile_unit_infos.size(), 973 sizeof(CompileUnitInfo), 974 (ComparisonFunction)SymbolContainsSymbolWithID); 975 } 976 977 if (oso_idx_ptr) { 978 if (comp_unit_info != nullptr) 979 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; 980 else 981 *oso_idx_ptr = UINT32_MAX; 982 } 983 return comp_unit_info; 984 } 985 986 static void RemoveFunctionsWithModuleNotEqualTo(const ModuleSP &module_sp, 987 SymbolContextList &sc_list, 988 uint32_t start_idx) { 989 // We found functions in .o files. Not all functions in the .o files will 990 // have made it into the final output file. The ones that did make it into 991 // the final output file will have a section whose module matches the module 992 // from the ObjectFile for this SymbolFile. When the modules don't match, 993 // then we have something that was in a .o file, but doesn't map to anything 994 // in the final executable. 995 uint32_t i = start_idx; 996 while (i < sc_list.GetSize()) { 997 SymbolContext sc; 998 sc_list.GetContextAtIndex(i, sc); 999 if (sc.function) { 1000 const SectionSP section_sp( 1001 sc.function->GetAddressRange().GetBaseAddress().GetSection()); 1002 if (section_sp->GetModule() != module_sp) { 1003 sc_list.RemoveContextAtIndex(i); 1004 continue; 1005 } 1006 } 1007 ++i; 1008 } 1009 } 1010 1011 void SymbolFileDWARFDebugMap::FindFunctions( 1012 ConstString name, const CompilerDeclContext &parent_decl_ctx, 1013 FunctionNameType name_type_mask, bool include_inlines, 1014 SymbolContextList &sc_list) { 1015 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1016 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (name = %s)", 1017 name.GetCString()); 1018 1019 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1020 uint32_t sc_idx = sc_list.GetSize(); 1021 oso_dwarf->FindFunctions(name, parent_decl_ctx, name_type_mask, 1022 include_inlines, sc_list); 1023 if (!sc_list.IsEmpty()) { 1024 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list, 1025 sc_idx); 1026 } 1027 return false; 1028 }); 1029 } 1030 1031 void SymbolFileDWARFDebugMap::FindFunctions(const RegularExpression ®ex, 1032 bool include_inlines, 1033 SymbolContextList &sc_list) { 1034 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1035 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')", 1036 regex.GetText().str().c_str()); 1037 1038 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1039 uint32_t sc_idx = sc_list.GetSize(); 1040 1041 oso_dwarf->FindFunctions(regex, include_inlines, sc_list); 1042 if (!sc_list.IsEmpty()) { 1043 RemoveFunctionsWithModuleNotEqualTo(m_objfile_sp->GetModule(), sc_list, 1044 sc_idx); 1045 } 1046 return false; 1047 }); 1048 } 1049 1050 void SymbolFileDWARFDebugMap::GetTypes(SymbolContextScope *sc_scope, 1051 lldb::TypeClass type_mask, 1052 TypeList &type_list) { 1053 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1054 LLDB_SCOPED_TIMERF("SymbolFileDWARFDebugMap::GetTypes (type_mask = 0x%8.8x)", 1055 type_mask); 1056 1057 SymbolFileDWARF *oso_dwarf = nullptr; 1058 if (sc_scope) { 1059 SymbolContext sc; 1060 sc_scope->CalculateSymbolContext(&sc); 1061 1062 CompileUnitInfo *cu_info = GetCompUnitInfo(sc); 1063 if (cu_info) { 1064 oso_dwarf = GetSymbolFileByCompUnitInfo(cu_info); 1065 if (oso_dwarf) 1066 oso_dwarf->GetTypes(sc_scope, type_mask, type_list); 1067 } 1068 } else { 1069 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1070 oso_dwarf->GetTypes(sc_scope, type_mask, type_list); 1071 return false; 1072 }); 1073 } 1074 } 1075 1076 std::vector<std::unique_ptr<lldb_private::CallEdge>> 1077 SymbolFileDWARFDebugMap::ParseCallEdgesInFunction(UserID func_id) { 1078 uint32_t oso_idx = GetOSOIndexFromUserID(func_id.GetID()); 1079 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); 1080 if (oso_dwarf) 1081 return oso_dwarf->ParseCallEdgesInFunction(func_id); 1082 return {}; 1083 } 1084 1085 TypeSP SymbolFileDWARFDebugMap::FindDefinitionTypeForDWARFDeclContext( 1086 const DWARFDeclContext &die_decl_ctx) { 1087 TypeSP type_sp; 1088 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1089 type_sp = oso_dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx); 1090 return ((bool)type_sp); 1091 }); 1092 return type_sp; 1093 } 1094 1095 bool SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type( 1096 SymbolFileDWARF *skip_dwarf_oso) { 1097 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) { 1098 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo; 1099 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1100 if (skip_dwarf_oso != oso_dwarf && 1101 oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(nullptr)) { 1102 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 1103 return true; 1104 } 1105 return false; 1106 }); 1107 } 1108 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes; 1109 } 1110 1111 TypeSP SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE( 1112 const DWARFDIE &die, ConstString type_name, 1113 bool must_be_implementation) { 1114 // If we have a debug map, we will have an Objective-C symbol whose name is 1115 // the type name and whose type is eSymbolTypeObjCClass. If we can find that 1116 // symbol and find its containing parent, we can locate the .o file that will 1117 // contain the implementation definition since it will be scoped inside the 1118 // N_SO and we can then locate the SymbolFileDWARF that corresponds to that 1119 // N_SO. 1120 SymbolFileDWARF *oso_dwarf = nullptr; 1121 TypeSP type_sp; 1122 ObjectFile *module_objfile = m_objfile_sp->GetModule()->GetObjectFile(); 1123 if (module_objfile) { 1124 Symtab *symtab = module_objfile->GetSymtab(); 1125 if (symtab) { 1126 Symbol *objc_class_symbol = symtab->FindFirstSymbolWithNameAndType( 1127 type_name, eSymbolTypeObjCClass, Symtab::eDebugAny, 1128 Symtab::eVisibilityAny); 1129 if (objc_class_symbol) { 1130 // Get the N_SO symbol that contains the objective C class symbol as 1131 // this should be the .o file that contains the real definition... 1132 const Symbol *source_file_symbol = symtab->GetParent(objc_class_symbol); 1133 1134 if (source_file_symbol && 1135 source_file_symbol->GetType() == eSymbolTypeSourceFile) { 1136 const uint32_t source_file_symbol_idx = 1137 symtab->GetIndexForSymbol(source_file_symbol); 1138 if (source_file_symbol_idx != UINT32_MAX) { 1139 CompileUnitInfo *compile_unit_info = 1140 GetCompileUnitInfoForSymbolWithIndex(source_file_symbol_idx, 1141 nullptr); 1142 if (compile_unit_info) { 1143 oso_dwarf = GetSymbolFileByCompUnitInfo(compile_unit_info); 1144 if (oso_dwarf) { 1145 TypeSP type_sp(oso_dwarf->FindCompleteObjCDefinitionTypeForDIE( 1146 die, type_name, must_be_implementation)); 1147 if (type_sp) { 1148 return type_sp; 1149 } 1150 } 1151 } 1152 } 1153 } 1154 } 1155 } 1156 } 1157 1158 // Only search all .o files for the definition if we don't need the 1159 // implementation because otherwise, with a valid debug map we should have 1160 // the ObjC class symbol and the code above should have found it. 1161 if (!must_be_implementation) { 1162 TypeSP type_sp; 1163 1164 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1165 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE( 1166 die, type_name, must_be_implementation); 1167 return (bool)type_sp; 1168 }); 1169 1170 return type_sp; 1171 } 1172 return TypeSP(); 1173 } 1174 1175 void SymbolFileDWARFDebugMap::FindTypes( 1176 ConstString name, const CompilerDeclContext &parent_decl_ctx, 1177 uint32_t max_matches, 1178 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1179 TypeMap &types) { 1180 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1181 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1182 oso_dwarf->FindTypes(name, parent_decl_ctx, max_matches, 1183 searched_symbol_files, types); 1184 return types.GetSize() >= max_matches; 1185 }); 1186 } 1187 1188 void SymbolFileDWARFDebugMap::FindTypes( 1189 llvm::ArrayRef<CompilerContext> context, LanguageSet languages, 1190 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1191 TypeMap &types) { 1192 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1193 oso_dwarf->FindTypes(context, languages, searched_symbol_files, types); 1194 return false; 1195 }); 1196 } 1197 1198 // 1199 // uint32_t 1200 // SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const 1201 // RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding 1202 // encoding, lldb::user_id_t udt_uid, TypeList& types) 1203 //{ 1204 // SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 1205 // if (oso_dwarf) 1206 // return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, 1207 // udt_uid, types); 1208 // return 0; 1209 //} 1210 1211 CompilerDeclContext SymbolFileDWARFDebugMap::FindNamespace( 1212 lldb_private::ConstString name, 1213 const CompilerDeclContext &parent_decl_ctx) { 1214 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1215 CompilerDeclContext matching_namespace; 1216 1217 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1218 matching_namespace = oso_dwarf->FindNamespace(name, parent_decl_ctx); 1219 1220 return (bool)matching_namespace; 1221 }); 1222 1223 return matching_namespace; 1224 } 1225 1226 void SymbolFileDWARFDebugMap::DumpClangAST(Stream &s) { 1227 ForEachSymbolFile([&s](SymbolFileDWARF *oso_dwarf) -> bool { 1228 oso_dwarf->DumpClangAST(s); 1229 // The underlying assumption is that DumpClangAST(...) will obtain the 1230 // AST from the underlying TypeSystem and therefore we only need to do 1231 // this once and can stop after the first iteration hence we return true. 1232 return true; 1233 }); 1234 } 1235 1236 // PluginInterface protocol 1237 lldb_private::ConstString SymbolFileDWARFDebugMap::GetPluginName() { 1238 return GetPluginNameStatic(); 1239 } 1240 1241 uint32_t SymbolFileDWARFDebugMap::GetPluginVersion() { return 1; } 1242 1243 lldb::CompUnitSP 1244 SymbolFileDWARFDebugMap::GetCompileUnit(SymbolFileDWARF *oso_dwarf) { 1245 if (oso_dwarf) { 1246 const uint32_t cu_count = GetNumCompileUnits(); 1247 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { 1248 SymbolFileDWARF *oso_symfile = 1249 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]); 1250 if (oso_symfile == oso_dwarf) { 1251 if (!m_compile_unit_infos[cu_idx].compile_unit_sp) 1252 m_compile_unit_infos[cu_idx].compile_unit_sp = 1253 ParseCompileUnitAtIndex(cu_idx); 1254 1255 return m_compile_unit_infos[cu_idx].compile_unit_sp; 1256 } 1257 } 1258 } 1259 llvm_unreachable("this shouldn't happen"); 1260 } 1261 1262 SymbolFileDWARFDebugMap::CompileUnitInfo * 1263 SymbolFileDWARFDebugMap::GetCompileUnitInfo(SymbolFileDWARF *oso_dwarf) { 1264 if (oso_dwarf) { 1265 const uint32_t cu_count = GetNumCompileUnits(); 1266 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { 1267 SymbolFileDWARF *oso_symfile = 1268 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]); 1269 if (oso_symfile == oso_dwarf) { 1270 return &m_compile_unit_infos[cu_idx]; 1271 } 1272 } 1273 } 1274 return nullptr; 1275 } 1276 1277 void SymbolFileDWARFDebugMap::SetCompileUnit(SymbolFileDWARF *oso_dwarf, 1278 const CompUnitSP &cu_sp) { 1279 if (oso_dwarf) { 1280 const uint32_t cu_count = GetNumCompileUnits(); 1281 for (uint32_t cu_idx = 0; cu_idx < cu_count; ++cu_idx) { 1282 SymbolFileDWARF *oso_symfile = 1283 GetSymbolFileByCompUnitInfo(&m_compile_unit_infos[cu_idx]); 1284 if (oso_symfile == oso_dwarf) { 1285 if (m_compile_unit_infos[cu_idx].compile_unit_sp) { 1286 assert(m_compile_unit_infos[cu_idx].compile_unit_sp.get() == 1287 cu_sp.get()); 1288 } else { 1289 m_compile_unit_infos[cu_idx].compile_unit_sp = cu_sp; 1290 SetCompileUnitAtIndex(cu_idx, cu_sp); 1291 } 1292 } 1293 } 1294 } 1295 } 1296 1297 CompilerDeclContext 1298 SymbolFileDWARFDebugMap::GetDeclContextForUID(lldb::user_id_t type_uid) { 1299 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid); 1300 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); 1301 if (oso_dwarf) 1302 return oso_dwarf->GetDeclContextForUID(type_uid); 1303 return CompilerDeclContext(); 1304 } 1305 1306 CompilerDeclContext 1307 SymbolFileDWARFDebugMap::GetDeclContextContainingUID(lldb::user_id_t type_uid) { 1308 const uint64_t oso_idx = GetOSOIndexFromUserID(type_uid); 1309 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex(oso_idx); 1310 if (oso_dwarf) 1311 return oso_dwarf->GetDeclContextContainingUID(type_uid); 1312 return CompilerDeclContext(); 1313 } 1314 1315 void SymbolFileDWARFDebugMap::ParseDeclsForContext( 1316 lldb_private::CompilerDeclContext decl_ctx) { 1317 ForEachSymbolFile([&](SymbolFileDWARF *oso_dwarf) -> bool { 1318 oso_dwarf->ParseDeclsForContext(decl_ctx); 1319 return true; // Keep iterating 1320 }); 1321 } 1322 1323 bool SymbolFileDWARFDebugMap::AddOSOFileRange(CompileUnitInfo *cu_info, 1324 lldb::addr_t exe_file_addr, 1325 lldb::addr_t exe_byte_size, 1326 lldb::addr_t oso_file_addr, 1327 lldb::addr_t oso_byte_size) { 1328 const uint32_t debug_map_idx = 1329 m_debug_map.FindEntryIndexThatContains(exe_file_addr); 1330 if (debug_map_idx != UINT32_MAX) { 1331 DebugMap::Entry *debug_map_entry = 1332 m_debug_map.FindEntryThatContains(exe_file_addr); 1333 debug_map_entry->data.SetOSOFileAddress(oso_file_addr); 1334 addr_t range_size = std::min<addr_t>(exe_byte_size, oso_byte_size); 1335 if (range_size == 0) { 1336 range_size = std::max<addr_t>(exe_byte_size, oso_byte_size); 1337 if (range_size == 0) 1338 range_size = 1; 1339 } 1340 cu_info->file_range_map.Append( 1341 FileRangeMap::Entry(oso_file_addr, range_size, exe_file_addr)); 1342 return true; 1343 } 1344 return false; 1345 } 1346 1347 void SymbolFileDWARFDebugMap::FinalizeOSOFileRanges(CompileUnitInfo *cu_info) { 1348 cu_info->file_range_map.Sort(); 1349 #if defined(DEBUG_OSO_DMAP) 1350 const FileRangeMap &oso_file_range_map = cu_info->GetFileRangeMap(this); 1351 const size_t n = oso_file_range_map.GetSize(); 1352 printf("SymbolFileDWARFDebugMap::FinalizeOSOFileRanges (cu_info = %p) %s\n", 1353 cu_info, cu_info->oso_sp->module_sp->GetFileSpec().GetPath().c_str()); 1354 for (size_t i = 0; i < n; ++i) { 1355 const FileRangeMap::Entry &entry = oso_file_range_map.GetEntryRef(i); 1356 printf("oso [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 1357 ") ==> exe [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", 1358 entry.GetRangeBase(), entry.GetRangeEnd(), entry.data, 1359 entry.data + entry.GetByteSize()); 1360 } 1361 #endif 1362 } 1363 1364 lldb::addr_t 1365 SymbolFileDWARFDebugMap::LinkOSOFileAddress(SymbolFileDWARF *oso_symfile, 1366 lldb::addr_t oso_file_addr) { 1367 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_symfile); 1368 if (cu_info) { 1369 const FileRangeMap::Entry *oso_range_entry = 1370 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr); 1371 if (oso_range_entry) { 1372 const DebugMap::Entry *debug_map_entry = 1373 m_debug_map.FindEntryThatContains(oso_range_entry->data); 1374 if (debug_map_entry) { 1375 const lldb::addr_t offset = 1376 oso_file_addr - oso_range_entry->GetRangeBase(); 1377 const lldb::addr_t exe_file_addr = 1378 debug_map_entry->GetRangeBase() + offset; 1379 return exe_file_addr; 1380 } 1381 } 1382 } 1383 return LLDB_INVALID_ADDRESS; 1384 } 1385 1386 bool SymbolFileDWARFDebugMap::LinkOSOAddress(Address &addr) { 1387 // Make sure this address hasn't been fixed already 1388 Module *exe_module = GetObjectFile()->GetModule().get(); 1389 Module *addr_module = addr.GetModule().get(); 1390 if (addr_module == exe_module) 1391 return true; // Address is already in terms of the main executable module 1392 1393 CompileUnitInfo *cu_info = GetCompileUnitInfo( 1394 GetSymbolFileAsSymbolFileDWARF(addr_module->GetSymbolFile())); 1395 if (cu_info) { 1396 const lldb::addr_t oso_file_addr = addr.GetFileAddress(); 1397 const FileRangeMap::Entry *oso_range_entry = 1398 cu_info->GetFileRangeMap(this).FindEntryThatContains(oso_file_addr); 1399 if (oso_range_entry) { 1400 const DebugMap::Entry *debug_map_entry = 1401 m_debug_map.FindEntryThatContains(oso_range_entry->data); 1402 if (debug_map_entry) { 1403 const lldb::addr_t offset = 1404 oso_file_addr - oso_range_entry->GetRangeBase(); 1405 const lldb::addr_t exe_file_addr = 1406 debug_map_entry->GetRangeBase() + offset; 1407 return exe_module->ResolveFileAddress(exe_file_addr, addr); 1408 } 1409 } 1410 } 1411 return true; 1412 } 1413 1414 LineTable *SymbolFileDWARFDebugMap::LinkOSOLineTable(SymbolFileDWARF *oso_dwarf, 1415 LineTable *line_table) { 1416 CompileUnitInfo *cu_info = GetCompileUnitInfo(oso_dwarf); 1417 if (cu_info) 1418 return line_table->LinkLineTable(cu_info->GetFileRangeMap(this)); 1419 return nullptr; 1420 } 1421 1422 size_t 1423 SymbolFileDWARFDebugMap::AddOSOARanges(SymbolFileDWARF *dwarf2Data, 1424 DWARFDebugAranges *debug_aranges) { 1425 size_t num_line_entries_added = 0; 1426 if (debug_aranges && dwarf2Data) { 1427 CompileUnitInfo *compile_unit_info = GetCompileUnitInfo(dwarf2Data); 1428 if (compile_unit_info) { 1429 const FileRangeMap &file_range_map = 1430 compile_unit_info->GetFileRangeMap(this); 1431 for (size_t idx = 0; idx < file_range_map.GetSize(); idx++) { 1432 const FileRangeMap::Entry *entry = file_range_map.GetEntryAtIndex(idx); 1433 if (entry) { 1434 debug_aranges->AppendRange(dwarf2Data->GetID(), entry->GetRangeBase(), 1435 entry->GetRangeEnd()); 1436 num_line_entries_added++; 1437 } 1438 } 1439 } 1440 } 1441 return num_line_entries_added; 1442 } 1443