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