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