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 #include "SymbolFileDWARFDebugMap.h" 11 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/ModuleList.h" 14 #include "lldb/Core/PluginManager.h" 15 #include "lldb/Core/RegularExpression.h" 16 #include "lldb/Core/StreamFile.h" 17 #include "lldb/Core/Timer.h" 18 19 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" 20 #include "lldb/Symbol/ObjectFile.h" 21 #include "lldb/Symbol/SymbolVendor.h" 22 #include "lldb/Symbol/VariableList.h" 23 24 #include "SymbolFileDWARF.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 void 30 SymbolFileDWARFDebugMap::Initialize() 31 { 32 PluginManager::RegisterPlugin (GetPluginNameStatic(), 33 GetPluginDescriptionStatic(), 34 CreateInstance); 35 } 36 37 void 38 SymbolFileDWARFDebugMap::Terminate() 39 { 40 PluginManager::UnregisterPlugin (CreateInstance); 41 } 42 43 44 const char * 45 SymbolFileDWARFDebugMap::GetPluginNameStatic() 46 { 47 return "dwarf-debugmap"; 48 } 49 50 const char * 51 SymbolFileDWARFDebugMap::GetPluginDescriptionStatic() 52 { 53 return "DWARF and DWARF3 debug symbol file reader (debug map)."; 54 } 55 56 SymbolFile* 57 SymbolFileDWARFDebugMap::CreateInstance (ObjectFile* obj_file) 58 { 59 return new SymbolFileDWARFDebugMap (obj_file); 60 } 61 62 63 SymbolFileDWARFDebugMap::SymbolFileDWARFDebugMap (ObjectFile* ofile) : 64 SymbolFile(ofile), 65 m_flags(), 66 m_compile_unit_infos(), 67 m_func_indexes(), 68 m_glob_indexes(), 69 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate) 70 { 71 } 72 73 74 SymbolFileDWARFDebugMap::~SymbolFileDWARFDebugMap() 75 { 76 } 77 78 void 79 SymbolFileDWARFDebugMap::InitializeObject() 80 { 81 // Install our external AST source callbacks so we can complete Clang types. 82 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap ( 83 new ClangExternalASTSourceCallbacks (SymbolFileDWARFDebugMap::CompleteTagDecl, 84 SymbolFileDWARFDebugMap::CompleteObjCInterfaceDecl, 85 NULL, 86 this)); 87 88 GetClangASTContext().SetExternalSource (ast_source_ap); 89 } 90 91 92 93 void 94 SymbolFileDWARFDebugMap::InitOSO () 95 { 96 if (m_flags.test(kHaveInitializedOSOs)) 97 return; 98 99 m_flags.set(kHaveInitializedOSOs); 100 // In order to get the abilities of this plug-in, we look at the list of 101 // N_OSO entries (object files) from the symbol table and make sure that 102 // these files exist and also contain valid DWARF. If we get any of that 103 // then we return the abilities of the first N_OSO's DWARF. 104 105 Symtab* symtab = m_obj_file->GetSymtab(); 106 if (symtab) 107 { 108 std::vector<uint32_t> oso_indexes; 109 // StreamFile s(stdout); 110 // symtab->Dump(&s, NULL, eSortOrderNone); 111 112 // When a mach-o symbol is encoded, the n_type field is encoded in bits 113 // 23:16, and the n_desc field is encoded in bits 15:0. 114 // 115 // To find all N_OSO entries that are part of the DWARF + debug map 116 // we find only object file symbols with the flags value as follows: 117 // bits 23:16 == 0x66 (N_OSO) 118 // bits 15: 0 == 0x0001 (specifies this is a debug map object file) 119 const uint32_t k_oso_symbol_flags_value = 0x660001u; 120 121 const uint32_t oso_index_count = symtab->AppendSymbolIndexesWithTypeAndFlagsValue(eSymbolTypeObjectFile, k_oso_symbol_flags_value, oso_indexes); 122 123 if (oso_index_count > 0) 124 { 125 symtab->AppendSymbolIndexesWithType (eSymbolTypeCode, Symtab::eDebugYes, Symtab::eVisibilityAny, m_func_indexes); 126 symtab->AppendSymbolIndexesWithType (eSymbolTypeData, Symtab::eDebugYes, Symtab::eVisibilityAny, m_glob_indexes); 127 128 symtab->SortSymbolIndexesByValue(m_func_indexes, true); 129 symtab->SortSymbolIndexesByValue(m_glob_indexes, true); 130 131 m_compile_unit_infos.resize(oso_index_count); 132 // s.Printf("%s N_OSO symbols:\n", __PRETTY_FUNCTION__); 133 // symtab->Dump(&s, oso_indexes); 134 135 for (uint32_t i=0; i<oso_index_count; ++i) 136 { 137 m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 1); 138 if (m_compile_unit_infos[i].so_symbol->GetSiblingIndex() == 0) 139 m_compile_unit_infos[i].so_symbol = symtab->SymbolAtIndex(oso_indexes[i] - 2); 140 m_compile_unit_infos[i].oso_symbol = symtab->SymbolAtIndex(oso_indexes[i]); 141 uint32_t sibling_idx = m_compile_unit_infos[i].so_symbol->GetSiblingIndex(); 142 assert (sibling_idx != 0); 143 assert (sibling_idx > i + 1); 144 m_compile_unit_infos[i].last_symbol = symtab->SymbolAtIndex (sibling_idx - 1); 145 m_compile_unit_infos[i].first_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].so_symbol); 146 m_compile_unit_infos[i].last_symbol_index = symtab->GetIndexForSymbol(m_compile_unit_infos[i].last_symbol); 147 } 148 } 149 } 150 } 151 152 Module * 153 SymbolFileDWARFDebugMap::GetModuleByOSOIndex (uint32_t oso_idx) 154 { 155 const uint32_t cu_count = GetNumCompileUnits(); 156 if (oso_idx < cu_count) 157 return GetModuleByCompUnitInfo (&m_compile_unit_infos[oso_idx]); 158 return NULL; 159 } 160 161 Module * 162 SymbolFileDWARFDebugMap::GetModuleByCompUnitInfo (CompileUnitInfo *comp_unit_info) 163 { 164 if (comp_unit_info->oso_module_sp.get() == NULL) 165 { 166 Symbol *oso_symbol = comp_unit_info->oso_symbol; 167 if (oso_symbol) 168 { 169 FileSpec oso_file_spec(oso_symbol->GetMangled().GetName().AsCString(), true); 170 // Always create a new module for .o files. Why? Because we 171 // use the debug map, to add new sections to each .o file and 172 // even though a .o file might not have changed, the sections 173 // that get added to the .o file can change. 174 comp_unit_info->oso_module_sp = new Module (oso_file_spec, 175 m_obj_file->GetModule()->GetArchitecture(), 176 NULL, 177 0); 178 } 179 } 180 return comp_unit_info->oso_module_sp.get(); 181 } 182 183 184 bool 185 SymbolFileDWARFDebugMap::GetFileSpecForSO (uint32_t oso_idx, FileSpec &file_spec) 186 { 187 if (oso_idx < m_compile_unit_infos.size()) 188 { 189 if (!m_compile_unit_infos[oso_idx].so_file) 190 { 191 192 if (m_compile_unit_infos[oso_idx].so_symbol == NULL) 193 return false; 194 195 std::string so_path (m_compile_unit_infos[oso_idx].so_symbol->GetMangled().GetName().AsCString()); 196 if (m_compile_unit_infos[oso_idx].so_symbol[1].GetType() == eSymbolTypeSourceFile) 197 so_path += m_compile_unit_infos[oso_idx].so_symbol[1].GetMangled().GetName().AsCString(); 198 m_compile_unit_infos[oso_idx].so_file.SetFile(so_path.c_str(), true); 199 } 200 file_spec = m_compile_unit_infos[oso_idx].so_file; 201 return true; 202 } 203 return false; 204 } 205 206 207 208 ObjectFile * 209 SymbolFileDWARFDebugMap::GetObjectFileByOSOIndex (uint32_t oso_idx) 210 { 211 Module *oso_module = GetModuleByOSOIndex (oso_idx); 212 if (oso_module) 213 return oso_module->GetObjectFile(); 214 return NULL; 215 } 216 217 SymbolFileDWARF * 218 SymbolFileDWARFDebugMap::GetSymbolFile (const SymbolContext& sc) 219 { 220 CompileUnitInfo *comp_unit_info = GetCompUnitInfo (sc); 221 if (comp_unit_info) 222 return GetSymbolFileByCompUnitInfo (comp_unit_info); 223 return NULL; 224 } 225 226 ObjectFile * 227 SymbolFileDWARFDebugMap::GetObjectFileByCompUnitInfo (CompileUnitInfo *comp_unit_info) 228 { 229 Module *oso_module = GetModuleByCompUnitInfo (comp_unit_info); 230 if (oso_module) 231 return oso_module->GetObjectFile(); 232 return NULL; 233 } 234 235 236 uint32_t 237 SymbolFileDWARFDebugMap::GetCompUnitInfoIndex (const CompileUnitInfo *comp_unit_info) 238 { 239 if (!m_compile_unit_infos.empty()) 240 { 241 const CompileUnitInfo *first_comp_unit_info = &m_compile_unit_infos.front(); 242 const CompileUnitInfo *last_comp_unit_info = &m_compile_unit_infos.back(); 243 if (first_comp_unit_info <= comp_unit_info && comp_unit_info <= last_comp_unit_info) 244 return comp_unit_info - first_comp_unit_info; 245 } 246 return UINT32_MAX; 247 } 248 249 SymbolFileDWARF * 250 SymbolFileDWARFDebugMap::GetSymbolFileByOSOIndex (uint32_t oso_idx) 251 { 252 if (oso_idx < m_compile_unit_infos.size()) 253 return GetSymbolFileByCompUnitInfo (&m_compile_unit_infos[oso_idx]); 254 return NULL; 255 } 256 257 SymbolFileDWARF * 258 SymbolFileDWARFDebugMap::GetSymbolFileByCompUnitInfo (CompileUnitInfo *comp_unit_info) 259 { 260 if (comp_unit_info->oso_symbol_vendor == NULL) 261 { 262 ObjectFile *oso_objfile = GetObjectFileByCompUnitInfo (comp_unit_info); 263 264 if (oso_objfile) 265 { 266 comp_unit_info->oso_symbol_vendor = oso_objfile->GetModule()->GetSymbolVendor(); 267 // SymbolFileDWARF *oso_dwarf = new SymbolFileDWARF(oso_objfile); 268 // comp_unit_info->oso_dwarf_sp.reset (oso_dwarf); 269 if (comp_unit_info->oso_symbol_vendor) 270 { 271 // Set a a pointer to this class to set our OSO DWARF file know 272 // that the DWARF is being used along with a debug map and that 273 // it will have the remapped sections that we do below. 274 SymbolFileDWARF *oso_symfile = (SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile(); 275 oso_symfile->SetDebugMapSymfile(this); 276 // Set the ID of the symbol file DWARF to the index of the OSO 277 // shifted left by 32 bits to provide a unique prefix for any 278 // UserID's that get created in the symbol file. 279 oso_symfile->SetID (((uint64_t)GetCompUnitInfoIndex(comp_unit_info) + 1ull) << 32ull); 280 comp_unit_info->debug_map_sections_sp.reset(new SectionList); 281 282 Symtab *exe_symtab = m_obj_file->GetSymtab(); 283 Module *oso_module = oso_objfile->GetModule(); 284 Symtab *oso_symtab = oso_objfile->GetSymtab(); 285 //#define DEBUG_OSO_DMAP // Do not check in with this defined... 286 #if defined(DEBUG_OSO_DMAP) 287 StreamFile s(stdout); 288 s << "OSO symtab:\n"; 289 oso_symtab->Dump(&s, NULL); 290 s << "OSO sections before:\n"; 291 oso_objfile->GetSectionList()->Dump(&s, NULL, true); 292 #endif 293 294 ///const uint32_t fun_resolve_flags = SymbolContext::Module | eSymbolContextCompUnit | eSymbolContextFunction; 295 //SectionList *oso_sections = oso_objfile->Sections(); 296 // Now we need to make sections that map from zero based object 297 // file addresses to where things eneded up in the main executable. 298 uint32_t oso_start_idx = exe_symtab->GetIndexForSymbol (comp_unit_info->oso_symbol); 299 assert (oso_start_idx != UINT32_MAX); 300 oso_start_idx += 1; 301 const uint32_t oso_end_idx = comp_unit_info->so_symbol->GetSiblingIndex(); 302 uint32_t sect_id = 0x10000; 303 for (uint32_t idx = oso_start_idx; idx < oso_end_idx; ++idx) 304 { 305 Symbol *exe_symbol = exe_symtab->SymbolAtIndex(idx); 306 if (exe_symbol) 307 { 308 if (exe_symbol->IsDebug() == false) 309 continue; 310 311 switch (exe_symbol->GetType()) 312 { 313 default: 314 break; 315 316 case eSymbolTypeCode: 317 { 318 // For each N_FUN, or function that we run into in the debug map 319 // we make a new section that we add to the sections found in the 320 // .o file. This new section has the file address set to what the 321 // addresses are in the .o file, and the load address is adjusted 322 // to match where it ended up in the final executable! We do this 323 // before we parse any dwarf info so that when it goes get parsed 324 // all section/offset addresses that get registered will resolve 325 // correctly to the new addresses in the main executable. 326 327 // First we find the original symbol in the .o file's symbol table 328 Symbol *oso_fun_symbol = oso_symtab->FindFirstSymbolWithNameAndType(exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), eSymbolTypeCode, Symtab::eDebugNo, Symtab::eVisibilityAny); 329 if (oso_fun_symbol) 330 { 331 // If we found the symbol, then we 332 Section* exe_fun_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); 333 Section* oso_fun_section = const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); 334 if (oso_fun_section) 335 { 336 // Now we create a section that we will add as a child of the 337 // section in which the .o symbol (the N_FUN) exists. 338 339 // We use the exe_symbol size because the one in the .o file 340 // will just be a symbol with no size, and the exe_symbol 341 // size will reflect any size changes (ppc has been known to 342 // shrink function sizes when it gets rid of jump islands that 343 // aren't needed anymore). 344 SectionSP oso_fun_section_sp (new Section (const_cast<Section *>(oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()), 345 oso_module, // Module (the .o file) 346 sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs 347 exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated! 348 eSectionTypeDebug, 349 oso_fun_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section 350 exe_symbol->GetByteSize(), // File size (we need the size from the executable) 351 0, 0, 0)); 352 353 oso_fun_section_sp->SetLinkedLocation (exe_fun_section, 354 exe_symbol->GetValue().GetFileAddress() - exe_fun_section->GetFileAddress()); 355 oso_fun_section->GetChildren().AddSection(oso_fun_section_sp); 356 comp_unit_info->debug_map_sections_sp->AddSection(oso_fun_section_sp); 357 } 358 } 359 } 360 break; 361 362 case eSymbolTypeData: 363 { 364 // For each N_GSYM we remap the address for the global by making 365 // a new section that we add to the sections found in the .o file. 366 // This new section has the file address set to what the 367 // addresses are in the .o file, and the load address is adjusted 368 // to match where it ended up in the final executable! We do this 369 // before we parse any dwarf info so that when it goes get parsed 370 // all section/offset addresses that get registered will resolve 371 // correctly to the new addresses in the main executable. We 372 // initially set the section size to be 1 byte, but will need to 373 // fix up these addresses further after all globals have been 374 // parsed to span the gaps, or we can find the global variable 375 // sizes from the DWARF info as we are parsing. 376 377 // Next we find the non-stab entry that corresponds to the N_GSYM in the .o file 378 Symbol *oso_gsym_symbol = oso_symtab->FindFirstSymbolWithNameAndType (exe_symbol->GetMangled().GetName(), 379 eSymbolTypeData, 380 Symtab::eDebugNo, 381 Symtab::eVisibilityAny); 382 383 if (exe_symbol && oso_gsym_symbol && exe_symbol->GetAddressRangePtr() && oso_gsym_symbol->GetAddressRangePtr()) 384 { 385 // If we found the symbol, then we 386 Section* exe_gsym_section = const_cast<Section *>(exe_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); 387 Section* oso_gsym_section = const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()); 388 if (oso_gsym_section) 389 { 390 SectionSP oso_gsym_section_sp (new Section (const_cast<Section *>(oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetSection()), 391 oso_module, // Module (the .o file) 392 sect_id++, // Section ID starts at 0x10000 and increments so the section IDs don't overlap with the standard mach IDs 393 exe_symbol->GetMangled().GetName(Mangled::ePreferMangled), // Name the section the same as the symbol for which is was generated! 394 eSectionTypeDebug, 395 oso_gsym_symbol->GetAddressRangePtr()->GetBaseAddress().GetOffset(), // File VM address offset in the current section 396 1, // We don't know the size of the global, just do the main address for now. 397 0, 0, 0)); 398 399 oso_gsym_section_sp->SetLinkedLocation (exe_gsym_section, 400 exe_symbol->GetValue().GetFileAddress() - exe_gsym_section->GetFileAddress()); 401 oso_gsym_section->GetChildren().AddSection(oso_gsym_section_sp); 402 comp_unit_info->debug_map_sections_sp->AddSection(oso_gsym_section_sp); 403 } 404 } 405 } 406 break; 407 } 408 } 409 } 410 #if defined(DEBUG_OSO_DMAP) 411 s << "OSO sections after:\n"; 412 oso_objfile->GetSectionList()->Dump(&s, NULL, true); 413 #endif 414 } 415 } 416 } 417 if (comp_unit_info->oso_symbol_vendor) 418 return (SymbolFileDWARF *)comp_unit_info->oso_symbol_vendor->GetSymbolFile(); 419 return NULL; 420 } 421 422 uint32_t 423 SymbolFileDWARFDebugMap::CalculateAbilities () 424 { 425 // In order to get the abilities of this plug-in, we look at the list of 426 // N_OSO entries (object files) from the symbol table and make sure that 427 // these files exist and also contain valid DWARF. If we get any of that 428 // then we return the abilities of the first N_OSO's DWARF. 429 430 const uint32_t oso_index_count = GetNumCompileUnits(); 431 if (oso_index_count > 0) 432 { 433 const uint32_t dwarf_abilities = SymbolFile::CompileUnits | 434 SymbolFile::Functions | 435 SymbolFile::Blocks | 436 SymbolFile::GlobalVariables | 437 SymbolFile::LocalVariables | 438 SymbolFile::VariableTypes | 439 SymbolFile::LineTables; 440 441 for (uint32_t oso_idx=0; oso_idx<oso_index_count; ++oso_idx) 442 { 443 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); 444 if (oso_dwarf) 445 { 446 uint32_t oso_abilities = oso_dwarf->GetAbilities(); 447 if ((oso_abilities & dwarf_abilities) == dwarf_abilities) 448 return oso_abilities; 449 } 450 } 451 } 452 return 0; 453 } 454 455 uint32_t 456 SymbolFileDWARFDebugMap::GetNumCompileUnits() 457 { 458 InitOSO (); 459 return m_compile_unit_infos.size(); 460 } 461 462 463 CompUnitSP 464 SymbolFileDWARFDebugMap::ParseCompileUnitAtIndex(uint32_t cu_idx) 465 { 466 CompUnitSP comp_unit_sp; 467 const uint32_t cu_count = GetNumCompileUnits(); 468 469 if (cu_idx < cu_count) 470 { 471 if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL) 472 { 473 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (cu_idx); 474 if (oso_dwarf) 475 { 476 // There is only one compile unit for N_OSO entry right now, so 477 // it will always exist at index zero. 478 m_compile_unit_infos[cu_idx].oso_compile_unit_sp = m_compile_unit_infos[cu_idx].oso_symbol_vendor->GetCompileUnitAtIndex (0); 479 } 480 481 if (m_compile_unit_infos[cu_idx].oso_compile_unit_sp.get() == NULL) 482 { 483 // We weren't able to get the DWARF for this N_OSO entry (the 484 // .o file may be missing or not at the specified path), make 485 // one up as best we can from the debug map. We set the uid 486 // of the compile unit to the symbol index with the MSBit set 487 // so that it doesn't collide with any uid values from the DWARF 488 Symbol *so_symbol = m_compile_unit_infos[cu_idx].so_symbol; 489 if (so_symbol) 490 { 491 m_compile_unit_infos[cu_idx].oso_compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(), 492 NULL, 493 so_symbol->GetMangled().GetName().AsCString(), 494 cu_idx, 495 eLanguageTypeUnknown)); 496 497 // Let our symbol vendor know about this compile unit 498 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex (m_compile_unit_infos[cu_idx].oso_compile_unit_sp, 499 cu_idx); 500 } 501 } 502 } 503 comp_unit_sp = m_compile_unit_infos[cu_idx].oso_compile_unit_sp; 504 } 505 506 return comp_unit_sp; 507 } 508 509 SymbolFileDWARFDebugMap::CompileUnitInfo * 510 SymbolFileDWARFDebugMap::GetCompUnitInfo (const SymbolContext& sc) 511 { 512 const uint32_t cu_count = GetNumCompileUnits(); 513 for (uint32_t i=0; i<cu_count; ++i) 514 { 515 if (sc.comp_unit == m_compile_unit_infos[i].oso_compile_unit_sp.get()) 516 return &m_compile_unit_infos[i]; 517 } 518 return NULL; 519 } 520 521 size_t 522 SymbolFileDWARFDebugMap::ParseCompileUnitFunctions (const SymbolContext& sc) 523 { 524 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 525 if (oso_dwarf) 526 return oso_dwarf->ParseCompileUnitFunctions (sc); 527 return 0; 528 } 529 530 bool 531 SymbolFileDWARFDebugMap::ParseCompileUnitLineTable (const SymbolContext& sc) 532 { 533 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 534 if (oso_dwarf) 535 return oso_dwarf->ParseCompileUnitLineTable (sc); 536 return false; 537 } 538 539 bool 540 SymbolFileDWARFDebugMap::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList &support_files) 541 { 542 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 543 if (oso_dwarf) 544 return oso_dwarf->ParseCompileUnitSupportFiles (sc, support_files); 545 return false; 546 } 547 548 549 size_t 550 SymbolFileDWARFDebugMap::ParseFunctionBlocks (const SymbolContext& sc) 551 { 552 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 553 if (oso_dwarf) 554 return oso_dwarf->ParseFunctionBlocks (sc); 555 return 0; 556 } 557 558 559 size_t 560 SymbolFileDWARFDebugMap::ParseTypes (const SymbolContext& sc) 561 { 562 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 563 if (oso_dwarf) 564 return oso_dwarf->ParseTypes (sc); 565 return 0; 566 } 567 568 569 size_t 570 SymbolFileDWARFDebugMap::ParseVariablesForContext (const SymbolContext& sc) 571 { 572 SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 573 if (oso_dwarf) 574 return oso_dwarf->ParseTypes (sc); 575 return 0; 576 } 577 578 579 580 Type* 581 SymbolFileDWARFDebugMap::ResolveTypeUID(lldb::user_id_t type_uid) 582 { 583 const uint64_t oso_idx = GetOSOIndexFromUserID (type_uid); 584 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); 585 if (oso_dwarf) 586 oso_dwarf->ResolveTypeUID (type_uid); 587 return NULL; 588 } 589 590 lldb::clang_type_t 591 SymbolFileDWARFDebugMap::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type) 592 { 593 // We have a struct/union/class/enum that needs to be fully resolved. 594 return NULL; 595 } 596 597 uint32_t 598 SymbolFileDWARFDebugMap::ResolveSymbolContext (const Address& exe_so_addr, uint32_t resolve_scope, SymbolContext& sc) 599 { 600 uint32_t resolved_flags = 0; 601 Symtab* symtab = m_obj_file->GetSymtab(); 602 if (symtab) 603 { 604 const addr_t exe_file_addr = exe_so_addr.GetFileAddress(); 605 sc.symbol = symtab->FindSymbolContainingFileAddress (exe_file_addr, &m_func_indexes[0], m_func_indexes.size()); 606 607 if (sc.symbol != NULL) 608 { 609 resolved_flags |= eSymbolContextSymbol; 610 611 uint32_t oso_idx = 0; 612 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithID (sc.symbol->GetID(), &oso_idx); 613 if (comp_unit_info) 614 { 615 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); 616 ObjectFile *oso_objfile = GetObjectFileByOSOIndex (oso_idx); 617 if (oso_dwarf && oso_objfile) 618 { 619 SectionList *oso_section_list = oso_objfile->GetSectionList(); 620 621 SectionSP oso_symbol_section_sp (oso_section_list->FindSectionContainingLinkedFileAddress (exe_file_addr, UINT32_MAX)); 622 623 if (oso_symbol_section_sp) 624 { 625 const addr_t linked_file_addr = oso_symbol_section_sp->GetLinkedFileAddress(); 626 Address oso_so_addr (oso_symbol_section_sp.get(), exe_file_addr - linked_file_addr); 627 if (oso_so_addr.IsSectionOffset()) 628 resolved_flags |= oso_dwarf->ResolveSymbolContext (oso_so_addr, resolve_scope, sc); 629 } 630 } 631 } 632 } 633 } 634 return resolved_flags; 635 } 636 637 638 uint32_t 639 SymbolFileDWARFDebugMap::ResolveSymbolContext (const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 640 { 641 uint32_t initial = sc_list.GetSize(); 642 const uint32_t cu_count = GetNumCompileUnits(); 643 644 FileSpec so_file_spec; 645 for (uint32_t i=0; i<cu_count; ++i) 646 { 647 if (GetFileSpecForSO (i, so_file_spec)) 648 { 649 // By passing false to the comparison we will be able to match 650 // and files given a filename only. If both file_spec and 651 // so_file_spec have directories, we will still do a full match. 652 if (FileSpec::Compare (file_spec, so_file_spec, false) == 0) 653 { 654 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (i); 655 if (oso_dwarf) 656 oso_dwarf->ResolveSymbolContext(file_spec, line, check_inlines, resolve_scope, sc_list); 657 } 658 } 659 } 660 return sc_list.GetSize() - initial; 661 } 662 663 uint32_t 664 SymbolFileDWARFDebugMap::PrivateFindGlobalVariables 665 ( 666 const ConstString &name, 667 const ClangNamespaceDecl *namespace_decl, 668 const std::vector<uint32_t> &indexes, // Indexes into the symbol table that match "name" 669 uint32_t max_matches, 670 VariableList& variables 671 ) 672 { 673 const uint32_t original_size = variables.GetSize(); 674 const size_t match_count = indexes.size(); 675 for (size_t i=0; i<match_count; ++i) 676 { 677 uint32_t oso_idx; 678 CompileUnitInfo* comp_unit_info = GetCompileUnitInfoForSymbolWithIndex (indexes[i], &oso_idx); 679 if (comp_unit_info) 680 { 681 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); 682 if (oso_dwarf) 683 { 684 if (oso_dwarf->FindGlobalVariables(name, namespace_decl, true, max_matches, variables)) 685 if (variables.GetSize() > max_matches) 686 break; 687 } 688 } 689 } 690 return variables.GetSize() - original_size; 691 } 692 693 uint32_t 694 SymbolFileDWARFDebugMap::FindGlobalVariables (const ConstString &name, const ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) 695 { 696 697 // If we aren't appending the results to this list, then clear the list 698 if (!append) 699 variables.Clear(); 700 701 // Remember how many variables are in the list before we search in case 702 // we are appending the results to a variable list. 703 const uint32_t original_size = variables.GetSize(); 704 705 uint32_t total_matches = 0; 706 SymbolFileDWARF *oso_dwarf; 707 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 708 { 709 const uint32_t oso_matches = oso_dwarf->FindGlobalVariables (name, 710 namespace_decl, 711 true, 712 max_matches, 713 variables); 714 if (oso_matches > 0) 715 { 716 total_matches += oso_matches; 717 718 // Are we getting all matches? 719 if (max_matches == UINT32_MAX) 720 continue; // Yep, continue getting everything 721 722 // If we have found enough matches, lets get out 723 if (max_matches >= total_matches) 724 break; 725 726 // Update the max matches for any subsequent calls to find globals 727 // in any other object files with DWARF 728 max_matches -= oso_matches; 729 } 730 } 731 // Return the number of variable that were appended to the list 732 return variables.GetSize() - original_size; 733 } 734 735 736 uint32_t 737 SymbolFileDWARFDebugMap::FindGlobalVariables (const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 738 { 739 // If we aren't appending the results to this list, then clear the list 740 if (!append) 741 variables.Clear(); 742 743 // Remember how many variables are in the list before we search in case 744 // we are appending the results to a variable list. 745 const uint32_t original_size = variables.GetSize(); 746 747 uint32_t total_matches = 0; 748 SymbolFileDWARF *oso_dwarf; 749 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 750 { 751 const uint32_t oso_matches = oso_dwarf->FindGlobalVariables (regex, 752 true, 753 max_matches, 754 variables); 755 if (oso_matches > 0) 756 { 757 total_matches += oso_matches; 758 759 // Are we getting all matches? 760 if (max_matches == UINT32_MAX) 761 continue; // Yep, continue getting everything 762 763 // If we have found enough matches, lets get out 764 if (max_matches >= total_matches) 765 break; 766 767 // Update the max matches for any subsequent calls to find globals 768 // in any other object files with DWARF 769 max_matches -= oso_matches; 770 } 771 } 772 // Return the number of variable that were appended to the list 773 return variables.GetSize() - original_size; 774 } 775 776 777 int 778 SymbolFileDWARFDebugMap::SymbolContainsSymbolWithIndex (uint32_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) 779 { 780 const uint32_t symbol_idx = *symbol_idx_ptr; 781 782 if (symbol_idx < comp_unit_info->first_symbol_index) 783 return -1; 784 785 if (symbol_idx <= comp_unit_info->last_symbol_index) 786 return 0; 787 788 return 1; 789 } 790 791 792 int 793 SymbolFileDWARFDebugMap::SymbolContainsSymbolWithID (user_id_t *symbol_idx_ptr, const CompileUnitInfo *comp_unit_info) 794 { 795 const user_id_t symbol_id = *symbol_idx_ptr; 796 797 if (symbol_id < comp_unit_info->so_symbol->GetID()) 798 return -1; 799 800 if (symbol_id <= comp_unit_info->last_symbol->GetID()) 801 return 0; 802 803 return 1; 804 } 805 806 807 SymbolFileDWARFDebugMap::CompileUnitInfo* 808 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithIndex (uint32_t symbol_idx, uint32_t *oso_idx_ptr) 809 { 810 const uint32_t oso_index_count = m_compile_unit_infos.size(); 811 CompileUnitInfo *comp_unit_info = NULL; 812 if (oso_index_count) 813 { 814 comp_unit_info = (CompileUnitInfo*)bsearch(&symbol_idx, 815 &m_compile_unit_infos[0], 816 m_compile_unit_infos.size(), 817 sizeof(CompileUnitInfo), 818 (ComparisonFunction)SymbolContainsSymbolWithIndex); 819 } 820 821 if (oso_idx_ptr) 822 { 823 if (comp_unit_info != NULL) 824 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; 825 else 826 *oso_idx_ptr = UINT32_MAX; 827 } 828 return comp_unit_info; 829 } 830 831 SymbolFileDWARFDebugMap::CompileUnitInfo* 832 SymbolFileDWARFDebugMap::GetCompileUnitInfoForSymbolWithID (user_id_t symbol_id, uint32_t *oso_idx_ptr) 833 { 834 const uint32_t oso_index_count = m_compile_unit_infos.size(); 835 CompileUnitInfo *comp_unit_info = NULL; 836 if (oso_index_count) 837 { 838 comp_unit_info = (CompileUnitInfo*)::bsearch (&symbol_id, 839 &m_compile_unit_infos[0], 840 m_compile_unit_infos.size(), 841 sizeof(CompileUnitInfo), 842 (ComparisonFunction)SymbolContainsSymbolWithID); 843 } 844 845 if (oso_idx_ptr) 846 { 847 if (comp_unit_info != NULL) 848 *oso_idx_ptr = comp_unit_info - &m_compile_unit_infos[0]; 849 else 850 *oso_idx_ptr = UINT32_MAX; 851 } 852 return comp_unit_info; 853 } 854 855 856 static void 857 RemoveFunctionsWithModuleNotEqualTo (Module *module, SymbolContextList &sc_list, uint32_t start_idx) 858 { 859 // We found functions in .o files. Not all functions in the .o files 860 // will have made it into the final output file. The ones that did 861 // make it into the final output file will have a section whose module 862 // matches the module from the ObjectFile for this SymbolFile. When 863 // the modules don't match, then we have something that was in a 864 // .o file, but doesn't map to anything in the final executable. 865 uint32_t i=start_idx; 866 while (i < sc_list.GetSize()) 867 { 868 SymbolContext sc; 869 sc_list.GetContextAtIndex(i, sc); 870 if (sc.function) 871 { 872 const Section *section = sc.function->GetAddressRange().GetBaseAddress().GetSection(); 873 if (section->GetModule() != module) 874 { 875 sc_list.RemoveContextAtIndex(i); 876 continue; 877 } 878 } 879 ++i; 880 } 881 } 882 883 uint32_t 884 SymbolFileDWARFDebugMap::FindFunctions(const ConstString &name, const ClangNamespaceDecl *namespace_decl, uint32_t name_type_mask, bool append, SymbolContextList& sc_list) 885 { 886 Timer scoped_timer (__PRETTY_FUNCTION__, 887 "SymbolFileDWARFDebugMap::FindFunctions (name = %s)", 888 name.GetCString()); 889 890 uint32_t initial_size = 0; 891 if (append) 892 initial_size = sc_list.GetSize(); 893 else 894 sc_list.Clear(); 895 896 uint32_t oso_idx = 0; 897 SymbolFileDWARF *oso_dwarf; 898 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL) 899 { 900 uint32_t sc_idx = sc_list.GetSize(); 901 if (oso_dwarf->FindFunctions(name, namespace_decl, name_type_mask, true, sc_list)) 902 { 903 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx); 904 } 905 } 906 907 return sc_list.GetSize() - initial_size; 908 } 909 910 911 uint32_t 912 SymbolFileDWARFDebugMap::FindFunctions (const RegularExpression& regex, bool append, SymbolContextList& sc_list) 913 { 914 Timer scoped_timer (__PRETTY_FUNCTION__, 915 "SymbolFileDWARFDebugMap::FindFunctions (regex = '%s')", 916 regex.GetText()); 917 918 uint32_t initial_size = 0; 919 if (append) 920 initial_size = sc_list.GetSize(); 921 else 922 sc_list.Clear(); 923 924 uint32_t oso_idx = 0; 925 SymbolFileDWARF *oso_dwarf; 926 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL) 927 { 928 uint32_t sc_idx = sc_list.GetSize(); 929 930 if (oso_dwarf->FindFunctions(regex, true, sc_list)) 931 { 932 RemoveFunctionsWithModuleNotEqualTo (m_obj_file->GetModule(), sc_list, sc_idx); 933 } 934 } 935 936 return sc_list.GetSize() - initial_size; 937 } 938 939 TypeSP 940 SymbolFileDWARFDebugMap::FindDefinitionTypeForDIE (DWARFCompileUnit* cu, 941 const DWARFDebugInfoEntry *die, 942 const ConstString &type_name) 943 { 944 TypeSP type_sp; 945 SymbolFileDWARF *oso_dwarf; 946 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 947 { 948 type_sp = oso_dwarf->FindDefinitionTypeForDIE (cu, die, type_name); 949 if (type_sp) 950 break; 951 } 952 return type_sp; 953 } 954 955 956 957 bool 958 SymbolFileDWARFDebugMap::Supports_DW_AT_APPLE_objc_complete_type (SymbolFileDWARF *skip_dwarf_oso) 959 { 960 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) 961 { 962 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo; 963 SymbolFileDWARF *oso_dwarf; 964 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 965 { 966 if (skip_dwarf_oso != oso_dwarf && oso_dwarf->Supports_DW_AT_APPLE_objc_complete_type(NULL)) 967 { 968 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 969 break; 970 } 971 } 972 } 973 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes; 974 } 975 976 TypeSP 977 SymbolFileDWARFDebugMap::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die, 978 const ConstString &type_name, 979 bool must_be_implementation) 980 { 981 TypeSP type_sp; 982 SymbolFileDWARF *oso_dwarf; 983 for (uint32_t oso_idx = 0; ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 984 { 985 type_sp = oso_dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name, must_be_implementation); 986 if (type_sp) 987 break; 988 } 989 return type_sp; 990 } 991 992 uint32_t 993 SymbolFileDWARFDebugMap::FindTypes 994 ( 995 const SymbolContext& sc, 996 const ConstString &name, 997 const ClangNamespaceDecl *namespace_decl, 998 bool append, 999 uint32_t max_matches, 1000 TypeList& types 1001 ) 1002 { 1003 if (!append) 1004 types.Clear(); 1005 1006 const uint32_t initial_types_size = types.GetSize(); 1007 SymbolFileDWARF *oso_dwarf; 1008 1009 if (sc.comp_unit) 1010 { 1011 oso_dwarf = GetSymbolFile (sc); 1012 if (oso_dwarf) 1013 return oso_dwarf->FindTypes (sc, name, namespace_decl, append, max_matches, types); 1014 } 1015 else 1016 { 1017 uint32_t oso_idx = 0; 1018 while ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx++)) != NULL) 1019 oso_dwarf->FindTypes (sc, name, namespace_decl, append, max_matches, types); 1020 } 1021 1022 return types.GetSize() - initial_types_size; 1023 } 1024 1025 // 1026 //uint32_t 1027 //SymbolFileDWARFDebugMap::FindTypes (const SymbolContext& sc, const RegularExpression& regex, bool append, uint32_t max_matches, Type::Encoding encoding, lldb::user_id_t udt_uid, TypeList& types) 1028 //{ 1029 // SymbolFileDWARF *oso_dwarf = GetSymbolFile (sc); 1030 // if (oso_dwarf) 1031 // return oso_dwarf->FindTypes (sc, regex, append, max_matches, encoding, udt_uid, types); 1032 // return 0; 1033 //} 1034 1035 1036 ClangNamespaceDecl 1037 SymbolFileDWARFDebugMap::FindNamespace (const lldb_private::SymbolContext& sc, 1038 const lldb_private::ConstString &name, 1039 const ClangNamespaceDecl *parent_namespace_decl) 1040 { 1041 ClangNamespaceDecl matching_namespace; 1042 SymbolFileDWARF *oso_dwarf; 1043 1044 if (sc.comp_unit) 1045 { 1046 oso_dwarf = GetSymbolFile (sc); 1047 if (oso_dwarf) 1048 matching_namespace = oso_dwarf->FindNamespace (sc, name, parent_namespace_decl); 1049 } 1050 else 1051 { 1052 for (uint32_t oso_idx = 0; 1053 ((oso_dwarf = GetSymbolFileByOSOIndex (oso_idx)) != NULL); 1054 ++oso_idx) 1055 { 1056 matching_namespace = oso_dwarf->FindNamespace (sc, name, parent_namespace_decl); 1057 1058 if (matching_namespace) 1059 break; 1060 } 1061 } 1062 1063 return matching_namespace; 1064 } 1065 1066 //------------------------------------------------------------------ 1067 // PluginInterface protocol 1068 //------------------------------------------------------------------ 1069 const char * 1070 SymbolFileDWARFDebugMap::GetPluginName() 1071 { 1072 return "SymbolFileDWARFDebugMap"; 1073 } 1074 1075 const char * 1076 SymbolFileDWARFDebugMap::GetShortPluginName() 1077 { 1078 return GetPluginNameStatic(); 1079 } 1080 1081 uint32_t 1082 SymbolFileDWARFDebugMap::GetPluginVersion() 1083 { 1084 return 1; 1085 } 1086 1087 void 1088 SymbolFileDWARFDebugMap::SetCompileUnit (SymbolFileDWARF *oso_dwarf, const CompUnitSP &cu_sp) 1089 { 1090 const uint32_t cu_count = GetNumCompileUnits(); 1091 for (uint32_t i=0; i<cu_count; ++i) 1092 { 1093 if (m_compile_unit_infos[i].oso_symbol_vendor && 1094 m_compile_unit_infos[i].oso_symbol_vendor->GetSymbolFile() == oso_dwarf) 1095 { 1096 if (m_compile_unit_infos[i].oso_compile_unit_sp) 1097 { 1098 assert (m_compile_unit_infos[i].oso_compile_unit_sp.get() == cu_sp.get()); 1099 } 1100 else 1101 { 1102 m_compile_unit_infos[i].oso_compile_unit_sp = cu_sp; 1103 } 1104 } 1105 } 1106 } 1107 1108 1109 void 1110 SymbolFileDWARFDebugMap::CompleteTagDecl (void *baton, clang::TagDecl *decl) 1111 { 1112 SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton; 1113 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 1114 if (clang_type) 1115 { 1116 SymbolFileDWARF *oso_dwarf; 1117 1118 for (uint32_t oso_idx = 0; ((oso_dwarf = symbol_file_dwarf->GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 1119 { 1120 if (oso_dwarf->HasForwardDeclForClangType (clang_type)) 1121 { 1122 oso_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 1123 return; 1124 } 1125 } 1126 } 1127 } 1128 1129 void 1130 SymbolFileDWARFDebugMap::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl) 1131 { 1132 SymbolFileDWARFDebugMap *symbol_file_dwarf = (SymbolFileDWARFDebugMap *)baton; 1133 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 1134 if (clang_type) 1135 { 1136 SymbolFileDWARF *oso_dwarf; 1137 1138 for (uint32_t oso_idx = 0; ((oso_dwarf = symbol_file_dwarf->GetSymbolFileByOSOIndex (oso_idx)) != NULL); ++oso_idx) 1139 { 1140 if (oso_dwarf->HasForwardDeclForClangType (clang_type)) 1141 { 1142 oso_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 1143 return; 1144 } 1145 } 1146 } 1147 } 1148 1149 clang::DeclContext* 1150 SymbolFileDWARFDebugMap::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid) 1151 { 1152 const uint64_t oso_idx = GetOSOIndexFromUserID (type_uid); 1153 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); 1154 if (oso_dwarf) 1155 return oso_dwarf->GetClangDeclContextContainingTypeUID (type_uid); 1156 return NULL; 1157 } 1158 1159 clang::DeclContext* 1160 SymbolFileDWARFDebugMap::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid) 1161 { 1162 const uint64_t oso_idx = GetOSOIndexFromUserID (type_uid); 1163 SymbolFileDWARF *oso_dwarf = GetSymbolFileByOSOIndex (oso_idx); 1164 if (oso_dwarf) 1165 return oso_dwarf->GetClangDeclContextForTypeUID (sc, type_uid); 1166 return NULL; 1167 } 1168 1169 1170