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