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