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