1 //===-- Symtab.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 <map> 11 #include <set> 12 13 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 14 #include "Plugins/Language/ObjC/ObjCLanguage.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/STLUtils.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Symbol/Symbol.h" 20 #include "lldb/Symbol/SymbolContext.h" 21 #include "lldb/Symbol/Symtab.h" 22 #include "lldb/Utility/RegularExpression.h" 23 #include "lldb/Utility/Stream.h" 24 #include "lldb/Utility/Timer.h" 25 26 using namespace lldb; 27 using namespace lldb_private; 28 29 Symtab::Symtab(ObjectFile *objfile) 30 : m_objfile(objfile), m_symbols(), m_file_addr_to_index(), 31 m_name_to_index(), m_mutex(), m_file_addr_to_index_computed(false), 32 m_name_indexes_computed(false) {} 33 34 Symtab::~Symtab() {} 35 36 void Symtab::Reserve(size_t count) { 37 // Clients should grab the mutex from this symbol table and lock it manually 38 // when calling this function to avoid performance issues. 39 m_symbols.reserve(count); 40 } 41 42 Symbol *Symtab::Resize(size_t count) { 43 // Clients should grab the mutex from this symbol table and lock it manually 44 // when calling this function to avoid performance issues. 45 m_symbols.resize(count); 46 return m_symbols.empty() ? nullptr : &m_symbols[0]; 47 } 48 49 uint32_t Symtab::AddSymbol(const Symbol &symbol) { 50 // Clients should grab the mutex from this symbol table and lock it manually 51 // when calling this function to avoid performance issues. 52 uint32_t symbol_idx = m_symbols.size(); 53 m_name_to_index.Clear(); 54 m_file_addr_to_index.Clear(); 55 m_symbols.push_back(symbol); 56 m_file_addr_to_index_computed = false; 57 m_name_indexes_computed = false; 58 return symbol_idx; 59 } 60 61 size_t Symtab::GetNumSymbols() const { 62 std::lock_guard<std::recursive_mutex> guard(m_mutex); 63 return m_symbols.size(); 64 } 65 66 void Symtab::SectionFileAddressesChanged() { 67 m_name_to_index.Clear(); 68 m_file_addr_to_index_computed = false; 69 } 70 71 void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order) { 72 std::lock_guard<std::recursive_mutex> guard(m_mutex); 73 74 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 75 s->Indent(); 76 const FileSpec &file_spec = m_objfile->GetFileSpec(); 77 const char *object_name = nullptr; 78 if (m_objfile->GetModule()) 79 object_name = m_objfile->GetModule()->GetObjectName().GetCString(); 80 81 if (file_spec) 82 s->Printf("Symtab, file = %s%s%s%s, num_symbols = %" PRIu64, 83 file_spec.GetPath().c_str(), object_name ? "(" : "", 84 object_name ? object_name : "", object_name ? ")" : "", 85 (uint64_t)m_symbols.size()); 86 else 87 s->Printf("Symtab, num_symbols = %" PRIu64 "", (uint64_t)m_symbols.size()); 88 89 if (!m_symbols.empty()) { 90 switch (sort_order) { 91 case eSortOrderNone: { 92 s->PutCString(":\n"); 93 DumpSymbolHeader(s); 94 const_iterator begin = m_symbols.begin(); 95 const_iterator end = m_symbols.end(); 96 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) { 97 s->Indent(); 98 pos->Dump(s, target, std::distance(begin, pos)); 99 } 100 } break; 101 102 case eSortOrderByName: { 103 // Although we maintain a lookup by exact name map, the table 104 // isn't sorted by name. So we must make the ordered symbol list 105 // up ourselves. 106 s->PutCString(" (sorted by name):\n"); 107 DumpSymbolHeader(s); 108 typedef std::multimap<const char *, const Symbol *, 109 CStringCompareFunctionObject> 110 CStringToSymbol; 111 CStringToSymbol name_map; 112 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); 113 pos != end; ++pos) { 114 const char *name = pos->GetName().AsCString(); 115 if (name && name[0]) 116 name_map.insert(std::make_pair(name, &(*pos))); 117 } 118 119 for (CStringToSymbol::const_iterator pos = name_map.begin(), 120 end = name_map.end(); 121 pos != end; ++pos) { 122 s->Indent(); 123 pos->second->Dump(s, target, pos->second - &m_symbols[0]); 124 } 125 } break; 126 127 case eSortOrderByAddress: 128 s->PutCString(" (sorted by address):\n"); 129 DumpSymbolHeader(s); 130 if (!m_file_addr_to_index_computed) 131 InitAddressIndexes(); 132 const size_t num_entries = m_file_addr_to_index.GetSize(); 133 for (size_t i = 0; i < num_entries; ++i) { 134 s->Indent(); 135 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data; 136 m_symbols[symbol_idx].Dump(s, target, symbol_idx); 137 } 138 break; 139 } 140 } 141 } 142 143 void Symtab::Dump(Stream *s, Target *target, 144 std::vector<uint32_t> &indexes) const { 145 std::lock_guard<std::recursive_mutex> guard(m_mutex); 146 147 const size_t num_symbols = GetNumSymbols(); 148 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 149 s->Indent(); 150 s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n", 151 (uint64_t)indexes.size(), (uint64_t)m_symbols.size()); 152 s->IndentMore(); 153 154 if (!indexes.empty()) { 155 std::vector<uint32_t>::const_iterator pos; 156 std::vector<uint32_t>::const_iterator end = indexes.end(); 157 DumpSymbolHeader(s); 158 for (pos = indexes.begin(); pos != end; ++pos) { 159 size_t idx = *pos; 160 if (idx < num_symbols) { 161 s->Indent(); 162 m_symbols[idx].Dump(s, target, idx); 163 } 164 } 165 } 166 s->IndentLess(); 167 } 168 169 void Symtab::DumpSymbolHeader(Stream *s) { 170 s->Indent(" Debug symbol\n"); 171 s->Indent(" |Synthetic symbol\n"); 172 s->Indent(" ||Externally Visible\n"); 173 s->Indent(" |||\n"); 174 s->Indent("Index UserID DSX Type File Address/Value Load " 175 "Address Size Flags Name\n"); 176 s->Indent("------- ------ --- --------------- ------------------ " 177 "------------------ ------------------ ---------- " 178 "----------------------------------\n"); 179 } 180 181 static int CompareSymbolID(const void *key, const void *p) { 182 const user_id_t match_uid = *(const user_id_t *)key; 183 const user_id_t symbol_uid = ((const Symbol *)p)->GetID(); 184 if (match_uid < symbol_uid) 185 return -1; 186 if (match_uid > symbol_uid) 187 return 1; 188 return 0; 189 } 190 191 Symbol *Symtab::FindSymbolByID(lldb::user_id_t symbol_uid) const { 192 std::lock_guard<std::recursive_mutex> guard(m_mutex); 193 194 Symbol *symbol = 195 (Symbol *)::bsearch(&symbol_uid, &m_symbols[0], m_symbols.size(), 196 sizeof(m_symbols[0]), CompareSymbolID); 197 return symbol; 198 } 199 200 Symbol *Symtab::SymbolAtIndex(size_t idx) { 201 // Clients should grab the mutex from this symbol table and lock it manually 202 // when calling this function to avoid performance issues. 203 if (idx < m_symbols.size()) 204 return &m_symbols[idx]; 205 return nullptr; 206 } 207 208 const Symbol *Symtab::SymbolAtIndex(size_t idx) const { 209 // Clients should grab the mutex from this symbol table and lock it manually 210 // when calling this function to avoid performance issues. 211 if (idx < m_symbols.size()) 212 return &m_symbols[idx]; 213 return nullptr; 214 } 215 216 //---------------------------------------------------------------------- 217 // InitNameIndexes 218 //---------------------------------------------------------------------- 219 void Symtab::InitNameIndexes() { 220 // Protected function, no need to lock mutex... 221 if (!m_name_indexes_computed) { 222 m_name_indexes_computed = true; 223 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 224 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 225 // Create the name index vector to be able to quickly search by name 226 const size_t num_symbols = m_symbols.size(); 227 #if 1 228 m_name_to_index.Reserve(num_symbols); 229 #else 230 // TODO: benchmark this to see if we save any memory. Otherwise we 231 // will always keep the memory reserved in the vector unless we pull 232 // some STL swap magic and then recopy... 233 uint32_t actual_count = 0; 234 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); 235 pos != end; ++pos) { 236 const Mangled &mangled = pos->GetMangled(); 237 if (mangled.GetMangledName()) 238 ++actual_count; 239 240 if (mangled.GetDemangledName()) 241 ++actual_count; 242 } 243 244 m_name_to_index.Reserve(actual_count); 245 #endif 246 247 NameToIndexMap::Entry entry; 248 249 // The "const char *" in "class_contexts" must come from a 250 // ConstString::GetCString() 251 std::set<const char *> class_contexts; 252 UniqueCStringMap<uint32_t> mangled_name_to_index; 253 std::vector<const char *> symbol_contexts(num_symbols, nullptr); 254 255 for (entry.value = 0; entry.value < num_symbols; ++entry.value) { 256 const Symbol *symbol = &m_symbols[entry.value]; 257 258 // Don't let trampolines get into the lookup by name map 259 // If we ever need the trampoline symbols to be searchable by name 260 // we can remove this and then possibly add a new bool to any of the 261 // Symtab functions that lookup symbols by name to indicate if they 262 // want trampolines. 263 if (symbol->IsTrampoline()) 264 continue; 265 266 const Mangled &mangled = symbol->GetMangled(); 267 entry.cstring = mangled.GetMangledName(); 268 if (entry.cstring) { 269 m_name_to_index.Append(entry); 270 271 if (symbol->ContainsLinkerAnnotations()) { 272 // If the symbol has linker annotations, also add the version without 273 // the annotations. 274 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations( 275 entry.cstring.GetStringRef())); 276 m_name_to_index.Append(entry); 277 } 278 279 const SymbolType symbol_type = symbol->GetType(); 280 if (symbol_type == eSymbolTypeCode || 281 symbol_type == eSymbolTypeResolver) { 282 llvm::StringRef entry_ref(entry.cstring.GetStringRef()); 283 if (entry_ref[0] == '_' && entry_ref[1] == 'Z' && 284 (entry_ref[2] != 'T' && // avoid virtual table, VTT structure, 285 // typeinfo structure, and typeinfo 286 // name 287 entry_ref[2] != 'G' && // avoid guard variables 288 entry_ref[2] != 'Z')) // named local entities (if we 289 // eventually handle eSymbolTypeData, 290 // we will want this back) 291 { 292 CPlusPlusLanguage::MethodName cxx_method( 293 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus)); 294 entry.cstring = ConstString(cxx_method.GetBasename()); 295 if (entry.cstring) { 296 // ConstString objects permanently store the string in the pool so 297 // calling 298 // GetCString() on the value gets us a const char * that will 299 // never go away 300 const char *const_context = 301 ConstString(cxx_method.GetContext()).GetCString(); 302 303 if (!const_context || const_context[0] == 0) { 304 // No context for this function so this has to be a basename 305 m_basename_to_index.Append(entry); 306 // If there is no context (no namespaces or class scopes that 307 // come before the function name) then this also could be a 308 // fullname. 309 m_name_to_index.Append(entry); 310 } else { 311 entry_ref = entry.cstring.GetStringRef(); 312 if (entry_ref[0] == '~' || 313 !cxx_method.GetQualifiers().empty()) { 314 // The first character of the demangled basename is '~' which 315 // means we have a class destructor. We can use this information 316 // to help us know what is a class and what isn't. 317 if (class_contexts.find(const_context) == class_contexts.end()) 318 class_contexts.insert(const_context); 319 m_method_to_index.Append(entry); 320 } else { 321 if (class_contexts.find(const_context) != 322 class_contexts.end()) { 323 // The current decl context is in our "class_contexts" which 324 // means 325 // this is a method on a class 326 m_method_to_index.Append(entry); 327 } else { 328 // We don't know if this is a function basename or a method, 329 // so put it into a temporary collection so once we are done 330 // we can look in class_contexts to see if each entry is a 331 // class 332 // or just a function and will put any remaining items into 333 // m_method_to_index or m_basename_to_index as needed 334 mangled_name_to_index.Append(entry); 335 symbol_contexts[entry.value] = const_context; 336 } 337 } 338 } 339 } 340 } 341 } 342 } 343 344 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage()); 345 if (entry.cstring) { 346 m_name_to_index.Append(entry); 347 348 if (symbol->ContainsLinkerAnnotations()) { 349 // If the symbol has linker annotations, also add the version without 350 // the annotations. 351 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations( 352 entry.cstring.GetStringRef())); 353 m_name_to_index.Append(entry); 354 } 355 } 356 357 // If the demangled name turns out to be an ObjC name, and 358 // is a category name, add the version without categories to the index 359 // too. 360 ObjCLanguage::MethodName objc_method(entry.cstring.GetStringRef(), true); 361 if (objc_method.IsValid(true)) { 362 entry.cstring = objc_method.GetSelector(); 363 m_selector_to_index.Append(entry); 364 365 ConstString objc_method_no_category( 366 objc_method.GetFullNameWithoutCategory(true)); 367 if (objc_method_no_category) { 368 entry.cstring = objc_method_no_category; 369 m_name_to_index.Append(entry); 370 } 371 } 372 } 373 374 size_t count; 375 if (!mangled_name_to_index.IsEmpty()) { 376 count = mangled_name_to_index.GetSize(); 377 for (size_t i = 0; i < count; ++i) { 378 if (mangled_name_to_index.GetValueAtIndex(i, entry.value)) { 379 entry.cstring = mangled_name_to_index.GetCStringAtIndex(i); 380 if (symbol_contexts[entry.value] && 381 class_contexts.find(symbol_contexts[entry.value]) != 382 class_contexts.end()) { 383 m_method_to_index.Append(entry); 384 } else { 385 // If we got here, we have something that had a context (was inside 386 // a namespace or class) 387 // yet we don't know if the entry 388 m_method_to_index.Append(entry); 389 m_basename_to_index.Append(entry); 390 } 391 } 392 } 393 } 394 m_name_to_index.Sort(); 395 m_name_to_index.SizeToFit(); 396 m_selector_to_index.Sort(); 397 m_selector_to_index.SizeToFit(); 398 m_basename_to_index.Sort(); 399 m_basename_to_index.SizeToFit(); 400 m_method_to_index.Sort(); 401 m_method_to_index.SizeToFit(); 402 403 // static StreamFile a ("/tmp/a.txt"); 404 // 405 // count = m_basename_to_index.GetSize(); 406 // if (count) 407 // { 408 // for (size_t i=0; i<count; ++i) 409 // { 410 // if (m_basename_to_index.GetValueAtIndex(i, entry.value)) 411 // a.Printf ("%s BASENAME\n", 412 // m_symbols[entry.value].GetMangled().GetName().GetCString()); 413 // } 414 // } 415 // count = m_method_to_index.GetSize(); 416 // if (count) 417 // { 418 // for (size_t i=0; i<count; ++i) 419 // { 420 // if (m_method_to_index.GetValueAtIndex(i, entry.value)) 421 // a.Printf ("%s METHOD\n", 422 // m_symbols[entry.value].GetMangled().GetName().GetCString()); 423 // } 424 // } 425 } 426 } 427 428 void Symtab::PreloadSymbols() { 429 std::lock_guard<std::recursive_mutex> guard(m_mutex); 430 InitNameIndexes(); 431 } 432 433 void Symtab::AppendSymbolNamesToMap(const IndexCollection &indexes, 434 bool add_demangled, bool add_mangled, 435 NameToIndexMap &name_to_index_map) const { 436 if (add_demangled || add_mangled) { 437 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 438 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 439 std::lock_guard<std::recursive_mutex> guard(m_mutex); 440 441 // Create the name index vector to be able to quickly search by name 442 NameToIndexMap::Entry entry; 443 const size_t num_indexes = indexes.size(); 444 for (size_t i = 0; i < num_indexes; ++i) { 445 entry.value = indexes[i]; 446 assert(i < m_symbols.size()); 447 const Symbol *symbol = &m_symbols[entry.value]; 448 449 const Mangled &mangled = symbol->GetMangled(); 450 if (add_demangled) { 451 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage()); 452 if (entry.cstring) 453 name_to_index_map.Append(entry); 454 } 455 456 if (add_mangled) { 457 entry.cstring = mangled.GetMangledName(); 458 if (entry.cstring) 459 name_to_index_map.Append(entry); 460 } 461 } 462 } 463 } 464 465 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type, 466 std::vector<uint32_t> &indexes, 467 uint32_t start_idx, 468 uint32_t end_index) const { 469 std::lock_guard<std::recursive_mutex> guard(m_mutex); 470 471 uint32_t prev_size = indexes.size(); 472 473 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index); 474 475 for (uint32_t i = start_idx; i < count; ++i) { 476 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 477 indexes.push_back(i); 478 } 479 480 return indexes.size() - prev_size; 481 } 482 483 uint32_t Symtab::AppendSymbolIndexesWithTypeAndFlagsValue( 484 SymbolType symbol_type, uint32_t flags_value, 485 std::vector<uint32_t> &indexes, uint32_t start_idx, 486 uint32_t end_index) const { 487 std::lock_guard<std::recursive_mutex> guard(m_mutex); 488 489 uint32_t prev_size = indexes.size(); 490 491 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index); 492 493 for (uint32_t i = start_idx; i < count; ++i) { 494 if ((symbol_type == eSymbolTypeAny || 495 m_symbols[i].GetType() == symbol_type) && 496 m_symbols[i].GetFlags() == flags_value) 497 indexes.push_back(i); 498 } 499 500 return indexes.size() - prev_size; 501 } 502 503 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type, 504 Debug symbol_debug_type, 505 Visibility symbol_visibility, 506 std::vector<uint32_t> &indexes, 507 uint32_t start_idx, 508 uint32_t end_index) const { 509 std::lock_guard<std::recursive_mutex> guard(m_mutex); 510 511 uint32_t prev_size = indexes.size(); 512 513 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index); 514 515 for (uint32_t i = start_idx; i < count; ++i) { 516 if (symbol_type == eSymbolTypeAny || 517 m_symbols[i].GetType() == symbol_type) { 518 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility)) 519 indexes.push_back(i); 520 } 521 } 522 523 return indexes.size() - prev_size; 524 } 525 526 uint32_t Symtab::GetIndexForSymbol(const Symbol *symbol) const { 527 if (!m_symbols.empty()) { 528 const Symbol *first_symbol = &m_symbols[0]; 529 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size()) 530 return symbol - first_symbol; 531 } 532 return UINT32_MAX; 533 } 534 535 struct SymbolSortInfo { 536 const bool sort_by_load_addr; 537 const Symbol *symbols; 538 }; 539 540 namespace { 541 struct SymbolIndexComparator { 542 const std::vector<Symbol> &symbols; 543 std::vector<lldb::addr_t> &addr_cache; 544 545 // Getting from the symbol to the Address to the File Address involves some 546 // work. 547 // Since there are potentially many symbols here, and we're using this for 548 // sorting so 549 // we're going to be computing the address many times, cache that in 550 // addr_cache. 551 // The array passed in has to be the same size as the symbols array passed 552 // into the 553 // member variable symbols, and should be initialized with 554 // LLDB_INVALID_ADDRESS. 555 // NOTE: You have to make addr_cache externally and pass it in because 556 // std::stable_sort 557 // makes copies of the comparator it is initially passed in, and you end up 558 // spending 559 // huge amounts of time copying this array... 560 561 SymbolIndexComparator(const std::vector<Symbol> &s, 562 std::vector<lldb::addr_t> &a) 563 : symbols(s), addr_cache(a) { 564 assert(symbols.size() == addr_cache.size()); 565 } 566 bool operator()(uint32_t index_a, uint32_t index_b) { 567 addr_t value_a = addr_cache[index_a]; 568 if (value_a == LLDB_INVALID_ADDRESS) { 569 value_a = symbols[index_a].GetAddressRef().GetFileAddress(); 570 addr_cache[index_a] = value_a; 571 } 572 573 addr_t value_b = addr_cache[index_b]; 574 if (value_b == LLDB_INVALID_ADDRESS) { 575 value_b = symbols[index_b].GetAddressRef().GetFileAddress(); 576 addr_cache[index_b] = value_b; 577 } 578 579 if (value_a == value_b) { 580 // The if the values are equal, use the original symbol user ID 581 lldb::user_id_t uid_a = symbols[index_a].GetID(); 582 lldb::user_id_t uid_b = symbols[index_b].GetID(); 583 if (uid_a < uid_b) 584 return true; 585 if (uid_a > uid_b) 586 return false; 587 return false; 588 } else if (value_a < value_b) 589 return true; 590 591 return false; 592 } 593 }; 594 } 595 596 void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes, 597 bool remove_duplicates) const { 598 std::lock_guard<std::recursive_mutex> guard(m_mutex); 599 600 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 601 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); 602 // No need to sort if we have zero or one items... 603 if (indexes.size() <= 1) 604 return; 605 606 // Sort the indexes in place using std::stable_sort. 607 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for 608 // performance, 609 // not correctness. The indexes vector tends to be "close" to sorted, which 610 // the 611 // stable sort handles better. 612 613 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS); 614 615 SymbolIndexComparator comparator(m_symbols, addr_cache); 616 std::stable_sort(indexes.begin(), indexes.end(), comparator); 617 618 // Remove any duplicates if requested 619 if (remove_duplicates) { 620 auto last = std::unique(indexes.begin(), indexes.end()); 621 indexes.erase(last, indexes.end()); 622 } 623 } 624 625 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name, 626 std::vector<uint32_t> &indexes) { 627 std::lock_guard<std::recursive_mutex> guard(m_mutex); 628 629 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 630 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 631 if (symbol_name) { 632 if (!m_name_indexes_computed) 633 InitNameIndexes(); 634 635 return m_name_to_index.GetValues(symbol_name, indexes); 636 } 637 return 0; 638 } 639 640 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name, 641 Debug symbol_debug_type, 642 Visibility symbol_visibility, 643 std::vector<uint32_t> &indexes) { 644 std::lock_guard<std::recursive_mutex> guard(m_mutex); 645 646 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 647 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 648 if (symbol_name) { 649 const size_t old_size = indexes.size(); 650 if (!m_name_indexes_computed) 651 InitNameIndexes(); 652 653 std::vector<uint32_t> all_name_indexes; 654 const size_t name_match_count = 655 m_name_to_index.GetValues(symbol_name, all_name_indexes); 656 for (size_t i = 0; i < name_match_count; ++i) { 657 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, 658 symbol_visibility)) 659 indexes.push_back(all_name_indexes[i]); 660 } 661 return indexes.size() - old_size; 662 } 663 return 0; 664 } 665 666 uint32_t 667 Symtab::AppendSymbolIndexesWithNameAndType(const ConstString &symbol_name, 668 SymbolType symbol_type, 669 std::vector<uint32_t> &indexes) { 670 std::lock_guard<std::recursive_mutex> guard(m_mutex); 671 672 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) { 673 std::vector<uint32_t>::iterator pos = indexes.begin(); 674 while (pos != indexes.end()) { 675 if (symbol_type == eSymbolTypeAny || 676 m_symbols[*pos].GetType() == symbol_type) 677 ++pos; 678 else 679 pos = indexes.erase(pos); 680 } 681 } 682 return indexes.size(); 683 } 684 685 uint32_t Symtab::AppendSymbolIndexesWithNameAndType( 686 const ConstString &symbol_name, SymbolType symbol_type, 687 Debug symbol_debug_type, Visibility symbol_visibility, 688 std::vector<uint32_t> &indexes) { 689 std::lock_guard<std::recursive_mutex> guard(m_mutex); 690 691 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, 692 symbol_visibility, indexes) > 0) { 693 std::vector<uint32_t>::iterator pos = indexes.begin(); 694 while (pos != indexes.end()) { 695 if (symbol_type == eSymbolTypeAny || 696 m_symbols[*pos].GetType() == symbol_type) 697 ++pos; 698 else 699 pos = indexes.erase(pos); 700 } 701 } 702 return indexes.size(); 703 } 704 705 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType( 706 const RegularExpression ®exp, SymbolType symbol_type, 707 std::vector<uint32_t> &indexes) { 708 std::lock_guard<std::recursive_mutex> guard(m_mutex); 709 710 uint32_t prev_size = indexes.size(); 711 uint32_t sym_end = m_symbols.size(); 712 713 for (uint32_t i = 0; i < sym_end; i++) { 714 if (symbol_type == eSymbolTypeAny || 715 m_symbols[i].GetType() == symbol_type) { 716 const char *name = m_symbols[i].GetName().AsCString(); 717 if (name) { 718 if (regexp.Execute(name)) 719 indexes.push_back(i); 720 } 721 } 722 } 723 return indexes.size() - prev_size; 724 } 725 726 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType( 727 const RegularExpression ®exp, SymbolType symbol_type, 728 Debug symbol_debug_type, Visibility symbol_visibility, 729 std::vector<uint32_t> &indexes) { 730 std::lock_guard<std::recursive_mutex> guard(m_mutex); 731 732 uint32_t prev_size = indexes.size(); 733 uint32_t sym_end = m_symbols.size(); 734 735 for (uint32_t i = 0; i < sym_end; i++) { 736 if (symbol_type == eSymbolTypeAny || 737 m_symbols[i].GetType() == symbol_type) { 738 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false) 739 continue; 740 741 const char *name = m_symbols[i].GetName().AsCString(); 742 if (name) { 743 if (regexp.Execute(name)) 744 indexes.push_back(i); 745 } 746 } 747 } 748 return indexes.size() - prev_size; 749 } 750 751 Symbol *Symtab::FindSymbolWithType(SymbolType symbol_type, 752 Debug symbol_debug_type, 753 Visibility symbol_visibility, 754 uint32_t &start_idx) { 755 std::lock_guard<std::recursive_mutex> guard(m_mutex); 756 757 const size_t count = m_symbols.size(); 758 for (size_t idx = start_idx; idx < count; ++idx) { 759 if (symbol_type == eSymbolTypeAny || 760 m_symbols[idx].GetType() == symbol_type) { 761 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) { 762 start_idx = idx; 763 return &m_symbols[idx]; 764 } 765 } 766 } 767 return nullptr; 768 } 769 770 size_t 771 Symtab::FindAllSymbolsWithNameAndType(const ConstString &name, 772 SymbolType symbol_type, 773 std::vector<uint32_t> &symbol_indexes) { 774 std::lock_guard<std::recursive_mutex> guard(m_mutex); 775 776 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 777 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 778 // Initialize all of the lookup by name indexes before converting NAME 779 // to a uniqued string NAME_STR below. 780 if (!m_name_indexes_computed) 781 InitNameIndexes(); 782 783 if (name) { 784 // The string table did have a string that matched, but we need 785 // to check the symbols and match the symbol_type if any was given. 786 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes); 787 } 788 return symbol_indexes.size(); 789 } 790 791 size_t Symtab::FindAllSymbolsWithNameAndType( 792 const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, 793 Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) { 794 std::lock_guard<std::recursive_mutex> guard(m_mutex); 795 796 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 797 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 798 // Initialize all of the lookup by name indexes before converting NAME 799 // to a uniqued string NAME_STR below. 800 if (!m_name_indexes_computed) 801 InitNameIndexes(); 802 803 if (name) { 804 // The string table did have a string that matched, but we need 805 // to check the symbols and match the symbol_type if any was given. 806 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type, 807 symbol_visibility, symbol_indexes); 808 } 809 return symbol_indexes.size(); 810 } 811 812 size_t Symtab::FindAllSymbolsMatchingRexExAndType( 813 const RegularExpression ®ex, SymbolType symbol_type, 814 Debug symbol_debug_type, Visibility symbol_visibility, 815 std::vector<uint32_t> &symbol_indexes) { 816 std::lock_guard<std::recursive_mutex> guard(m_mutex); 817 818 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, 819 symbol_visibility, symbol_indexes); 820 return symbol_indexes.size(); 821 } 822 823 Symbol *Symtab::FindFirstSymbolWithNameAndType(const ConstString &name, 824 SymbolType symbol_type, 825 Debug symbol_debug_type, 826 Visibility symbol_visibility) { 827 std::lock_guard<std::recursive_mutex> guard(m_mutex); 828 829 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 830 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION); 831 if (!m_name_indexes_computed) 832 InitNameIndexes(); 833 834 if (name) { 835 std::vector<uint32_t> matching_indexes; 836 // The string table did have a string that matched, but we need 837 // to check the symbols and match the symbol_type if any was given. 838 if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type, 839 symbol_visibility, 840 matching_indexes)) { 841 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end(); 842 for (pos = matching_indexes.begin(); pos != end; ++pos) { 843 Symbol *symbol = SymbolAtIndex(*pos); 844 845 if (symbol->Compare(name, symbol_type)) 846 return symbol; 847 } 848 } 849 } 850 return nullptr; 851 } 852 853 typedef struct { 854 const Symtab *symtab; 855 const addr_t file_addr; 856 Symbol *match_symbol; 857 const uint32_t *match_index_ptr; 858 addr_t match_offset; 859 } SymbolSearchInfo; 860 861 // Add all the section file start address & size to the RangeVector, 862 // recusively adding any children sections. 863 static void AddSectionsToRangeMap(SectionList *sectlist, 864 RangeVector<addr_t, addr_t> §ion_ranges) { 865 const int num_sections = sectlist->GetNumSections(0); 866 for (int i = 0; i < num_sections; i++) { 867 SectionSP sect_sp = sectlist->GetSectionAtIndex(i); 868 if (sect_sp) { 869 SectionList &child_sectlist = sect_sp->GetChildren(); 870 871 // If this section has children, add the children to the RangeVector. 872 // Else add this section to the RangeVector. 873 if (child_sectlist.GetNumSections(0) > 0) { 874 AddSectionsToRangeMap(&child_sectlist, section_ranges); 875 } else { 876 size_t size = sect_sp->GetByteSize(); 877 if (size > 0) { 878 addr_t base_addr = sect_sp->GetFileAddress(); 879 RangeVector<addr_t, addr_t>::Entry entry; 880 entry.SetRangeBase(base_addr); 881 entry.SetByteSize(size); 882 section_ranges.Append(entry); 883 } 884 } 885 } 886 } 887 } 888 889 void Symtab::InitAddressIndexes() { 890 // Protected function, no need to lock mutex... 891 if (!m_file_addr_to_index_computed && !m_symbols.empty()) { 892 m_file_addr_to_index_computed = true; 893 894 FileRangeToIndexMap::Entry entry; 895 const_iterator begin = m_symbols.begin(); 896 const_iterator end = m_symbols.end(); 897 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) { 898 if (pos->ValueIsAddress()) { 899 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress()); 900 entry.SetByteSize(pos->GetByteSize()); 901 entry.data = std::distance(begin, pos); 902 m_file_addr_to_index.Append(entry); 903 } 904 } 905 const size_t num_entries = m_file_addr_to_index.GetSize(); 906 if (num_entries > 0) { 907 m_file_addr_to_index.Sort(); 908 909 // Create a RangeVector with the start & size of all the sections for 910 // this objfile. We'll need to check this for any FileRangeToIndexMap 911 // entries with an uninitialized size, which could potentially be a 912 // large number so reconstituting the weak pointer is busywork when it 913 // is invariant information. 914 SectionList *sectlist = m_objfile->GetSectionList(); 915 RangeVector<addr_t, addr_t> section_ranges; 916 if (sectlist) { 917 AddSectionsToRangeMap(sectlist, section_ranges); 918 section_ranges.Sort(); 919 } 920 921 // Iterate through the FileRangeToIndexMap and fill in the size for any 922 // entries that didn't already have a size from the Symbol (e.g. if we 923 // have a plain linker symbol with an address only, instead of debug info 924 // where we get an address and a size and a type, etc.) 925 for (size_t i = 0; i < num_entries; i++) { 926 FileRangeToIndexMap::Entry *entry = 927 m_file_addr_to_index.GetMutableEntryAtIndex(i); 928 if (entry->GetByteSize() == 0) { 929 addr_t curr_base_addr = entry->GetRangeBase(); 930 const RangeVector<addr_t, addr_t>::Entry *containing_section = 931 section_ranges.FindEntryThatContains(curr_base_addr); 932 933 // Use the end of the section as the default max size of the symbol 934 addr_t sym_size = 0; 935 if (containing_section) { 936 sym_size = 937 containing_section->GetByteSize() - 938 (entry->GetRangeBase() - containing_section->GetRangeBase()); 939 } 940 941 for (size_t j = i; j < num_entries; j++) { 942 FileRangeToIndexMap::Entry *next_entry = 943 m_file_addr_to_index.GetMutableEntryAtIndex(j); 944 addr_t next_base_addr = next_entry->GetRangeBase(); 945 if (next_base_addr > curr_base_addr) { 946 addr_t size_to_next_symbol = next_base_addr - curr_base_addr; 947 948 // Take the difference between this symbol and the next one as its 949 // size, 950 // if it is less than the size of the section. 951 if (sym_size == 0 || size_to_next_symbol < sym_size) { 952 sym_size = size_to_next_symbol; 953 } 954 break; 955 } 956 } 957 958 if (sym_size > 0) { 959 entry->SetByteSize(sym_size); 960 Symbol &symbol = m_symbols[entry->data]; 961 symbol.SetByteSize(sym_size); 962 symbol.SetSizeIsSynthesized(true); 963 } 964 } 965 } 966 967 // Sort again in case the range size changes the ordering 968 m_file_addr_to_index.Sort(); 969 } 970 } 971 } 972 973 void Symtab::CalculateSymbolSizes() { 974 std::lock_guard<std::recursive_mutex> guard(m_mutex); 975 976 if (!m_symbols.empty()) { 977 if (!m_file_addr_to_index_computed) 978 InitAddressIndexes(); 979 980 const size_t num_entries = m_file_addr_to_index.GetSize(); 981 982 for (size_t i = 0; i < num_entries; ++i) { 983 // The entries in the m_file_addr_to_index have calculated the sizes 984 // already 985 // so we will use this size if we need to. 986 const FileRangeToIndexMap::Entry &entry = 987 m_file_addr_to_index.GetEntryRef(i); 988 989 Symbol &symbol = m_symbols[entry.data]; 990 991 // If the symbol size is already valid, no need to do anything 992 if (symbol.GetByteSizeIsValid()) 993 continue; 994 995 const addr_t range_size = entry.GetByteSize(); 996 if (range_size > 0) { 997 symbol.SetByteSize(range_size); 998 symbol.SetSizeIsSynthesized(true); 999 } 1000 } 1001 } 1002 } 1003 1004 Symbol *Symtab::FindSymbolAtFileAddress(addr_t file_addr) { 1005 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1006 if (!m_file_addr_to_index_computed) 1007 InitAddressIndexes(); 1008 1009 const FileRangeToIndexMap::Entry *entry = 1010 m_file_addr_to_index.FindEntryStartsAt(file_addr); 1011 if (entry) { 1012 Symbol *symbol = SymbolAtIndex(entry->data); 1013 if (symbol->GetFileAddress() == file_addr) 1014 return symbol; 1015 } 1016 return nullptr; 1017 } 1018 1019 Symbol *Symtab::FindSymbolContainingFileAddress(addr_t file_addr) { 1020 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1021 1022 if (!m_file_addr_to_index_computed) 1023 InitAddressIndexes(); 1024 1025 const FileRangeToIndexMap::Entry *entry = 1026 m_file_addr_to_index.FindEntryThatContains(file_addr); 1027 if (entry) { 1028 Symbol *symbol = SymbolAtIndex(entry->data); 1029 if (symbol->ContainsFileAddress(file_addr)) 1030 return symbol; 1031 } 1032 return nullptr; 1033 } 1034 1035 void Symtab::ForEachSymbolContainingFileAddress( 1036 addr_t file_addr, std::function<bool(Symbol *)> const &callback) { 1037 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1038 1039 if (!m_file_addr_to_index_computed) 1040 InitAddressIndexes(); 1041 1042 std::vector<uint32_t> all_addr_indexes; 1043 1044 // Get all symbols with file_addr 1045 const size_t addr_match_count = 1046 m_file_addr_to_index.FindEntryIndexesThatContain(file_addr, 1047 all_addr_indexes); 1048 1049 for (size_t i = 0; i < addr_match_count; ++i) { 1050 Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]); 1051 if (symbol->ContainsFileAddress(file_addr)) { 1052 if (!callback(symbol)) 1053 break; 1054 } 1055 } 1056 } 1057 1058 void Symtab::SymbolIndicesToSymbolContextList( 1059 std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) { 1060 // No need to protect this call using m_mutex all other method calls are 1061 // already thread safe. 1062 1063 const bool merge_symbol_into_function = true; 1064 size_t num_indices = symbol_indexes.size(); 1065 if (num_indices > 0) { 1066 SymbolContext sc; 1067 sc.module_sp = m_objfile->GetModule(); 1068 for (size_t i = 0; i < num_indices; i++) { 1069 sc.symbol = SymbolAtIndex(symbol_indexes[i]); 1070 if (sc.symbol) 1071 sc_list.AppendIfUnique(sc, merge_symbol_into_function); 1072 } 1073 } 1074 } 1075 1076 size_t Symtab::FindFunctionSymbols(const ConstString &name, 1077 uint32_t name_type_mask, 1078 SymbolContextList &sc_list) { 1079 size_t count = 0; 1080 std::vector<uint32_t> symbol_indexes; 1081 1082 // eFunctionNameTypeAuto should be pre-resolved by a call to 1083 // Module::LookupInfo::LookupInfo() 1084 assert((name_type_mask & eFunctionNameTypeAuto) == 0); 1085 1086 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) { 1087 std::vector<uint32_t> temp_symbol_indexes; 1088 FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes); 1089 1090 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size(); 1091 if (temp_symbol_indexes_size > 0) { 1092 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1093 for (unsigned i = 0; i < temp_symbol_indexes_size; i++) { 1094 SymbolContext sym_ctx; 1095 sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]); 1096 if (sym_ctx.symbol) { 1097 switch (sym_ctx.symbol->GetType()) { 1098 case eSymbolTypeCode: 1099 case eSymbolTypeResolver: 1100 case eSymbolTypeReExported: 1101 symbol_indexes.push_back(temp_symbol_indexes[i]); 1102 break; 1103 default: 1104 break; 1105 } 1106 } 1107 } 1108 } 1109 } 1110 1111 if (name_type_mask & eFunctionNameTypeBase) { 1112 // From mangled names we can't tell what is a basename and what 1113 // is a method name, so we just treat them the same 1114 if (!m_name_indexes_computed) 1115 InitNameIndexes(); 1116 1117 if (!m_basename_to_index.IsEmpty()) { 1118 const UniqueCStringMap<uint32_t>::Entry *match; 1119 for (match = m_basename_to_index.FindFirstValueForName(name); 1120 match != nullptr; 1121 match = m_basename_to_index.FindNextValueForName(match)) { 1122 symbol_indexes.push_back(match->value); 1123 } 1124 } 1125 } 1126 1127 if (name_type_mask & eFunctionNameTypeMethod) { 1128 if (!m_name_indexes_computed) 1129 InitNameIndexes(); 1130 1131 if (!m_method_to_index.IsEmpty()) { 1132 const UniqueCStringMap<uint32_t>::Entry *match; 1133 for (match = m_method_to_index.FindFirstValueForName(name); 1134 match != nullptr; 1135 match = m_method_to_index.FindNextValueForName(match)) { 1136 symbol_indexes.push_back(match->value); 1137 } 1138 } 1139 } 1140 1141 if (name_type_mask & eFunctionNameTypeSelector) { 1142 if (!m_name_indexes_computed) 1143 InitNameIndexes(); 1144 1145 if (!m_selector_to_index.IsEmpty()) { 1146 const UniqueCStringMap<uint32_t>::Entry *match; 1147 for (match = m_selector_to_index.FindFirstValueForName(name); 1148 match != nullptr; 1149 match = m_selector_to_index.FindNextValueForName(match)) { 1150 symbol_indexes.push_back(match->value); 1151 } 1152 } 1153 } 1154 1155 if (!symbol_indexes.empty()) { 1156 std::sort(symbol_indexes.begin(), symbol_indexes.end()); 1157 symbol_indexes.erase( 1158 std::unique(symbol_indexes.begin(), symbol_indexes.end()), 1159 symbol_indexes.end()); 1160 count = symbol_indexes.size(); 1161 SymbolIndicesToSymbolContextList(symbol_indexes, sc_list); 1162 } 1163 1164 return count; 1165 } 1166 1167 const Symbol *Symtab::GetParent(Symbol *child_symbol) const { 1168 uint32_t child_idx = GetIndexForSymbol(child_symbol); 1169 if (child_idx != UINT32_MAX && child_idx > 0) { 1170 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) { 1171 const Symbol *symbol = SymbolAtIndex(idx); 1172 const uint32_t sibling_idx = symbol->GetSiblingIndex(); 1173 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx) 1174 return symbol; 1175 } 1176 } 1177 return NULL; 1178 } 1179