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