1 //===-- Symtab.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
27 using namespace lldb_private;
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) {}
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);
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];
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;
61 size_t Symtab::GetNumSymbols() const {
62 std::lock_guard<std::recursive_mutex> guard(m_mutex);
63 return m_symbols.size();
66 void Symtab::SectionFileAddressesChanged() {
67 m_name_to_index.Clear();
68 m_file_addr_to_index_computed = false;
71 void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order) {
72 std::lock_guard<std::recursive_mutex> guard(m_mutex);
74 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
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();
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());
87 s->Printf("Symtab, num_symbols = %" PRIu64 "", (uint64_t)m_symbols.size());
89 if (!m_symbols.empty()) {
91 case eSortOrderNone: {
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) {
98 pos->Dump(s, target, std::distance(begin, pos));
102 case eSortOrderByName: {
103 // Although we maintain a lookup by exact name map, the table isn't
104 // sorted by name. So we must make the ordered symbol list up ourselves.
105 s->PutCString(" (sorted by name):\n");
107 typedef std::multimap<const char *, const Symbol *,
108 CStringCompareFunctionObject>
110 CStringToSymbol name_map;
111 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
113 const char *name = pos->GetName().AsCString();
115 name_map.insert(std::make_pair(name, &(*pos)));
118 for (CStringToSymbol::const_iterator pos = name_map.begin(),
119 end = name_map.end();
122 pos->second->Dump(s, target, pos->second - &m_symbols[0]);
126 case eSortOrderByAddress:
127 s->PutCString(" (sorted by address):\n");
129 if (!m_file_addr_to_index_computed)
130 InitAddressIndexes();
131 const size_t num_entries = m_file_addr_to_index.GetSize();
132 for (size_t i = 0; i < num_entries; ++i) {
134 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data;
135 m_symbols[symbol_idx].Dump(s, target, symbol_idx);
142 void Symtab::Dump(Stream *s, Target *target,
143 std::vector<uint32_t> &indexes) const {
144 std::lock_guard<std::recursive_mutex> guard(m_mutex);
146 const size_t num_symbols = GetNumSymbols();
147 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
149 s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n",
150 (uint64_t)indexes.size(), (uint64_t)m_symbols.size());
153 if (!indexes.empty()) {
154 std::vector<uint32_t>::const_iterator pos;
155 std::vector<uint32_t>::const_iterator end = indexes.end();
157 for (pos = indexes.begin(); pos != end; ++pos) {
159 if (idx < num_symbols) {
161 m_symbols[idx].Dump(s, target, idx);
168 void Symtab::DumpSymbolHeader(Stream *s) {
169 s->Indent(" Debug symbol\n");
170 s->Indent(" |Synthetic symbol\n");
171 s->Indent(" ||Externally Visible\n");
173 s->Indent("Index UserID DSX Type File Address/Value Load "
174 "Address Size Flags Name\n");
175 s->Indent("------- ------ --- --------------- ------------------ "
176 "------------------ ------------------ ---------- "
177 "----------------------------------\n");
180 static int CompareSymbolID(const void *key, const void *p) {
181 const user_id_t match_uid = *(const user_id_t *)key;
182 const user_id_t symbol_uid = ((const Symbol *)p)->GetID();
183 if (match_uid < symbol_uid)
185 if (match_uid > symbol_uid)
190 Symbol *Symtab::FindSymbolByID(lldb::user_id_t symbol_uid) const {
191 std::lock_guard<std::recursive_mutex> guard(m_mutex);
194 (Symbol *)::bsearch(&symbol_uid, &m_symbols[0], m_symbols.size(),
195 sizeof(m_symbols[0]), CompareSymbolID);
199 Symbol *Symtab::SymbolAtIndex(size_t idx) {
200 // Clients should grab the mutex from this symbol table and lock it manually
201 // when calling this function to avoid performance issues.
202 if (idx < m_symbols.size())
203 return &m_symbols[idx];
207 const Symbol *Symtab::SymbolAtIndex(size_t idx) const {
208 // Clients should grab the mutex from this symbol table and lock it manually
209 // when calling this function to avoid performance issues.
210 if (idx < m_symbols.size())
211 return &m_symbols[idx];
215 //----------------------------------------------------------------------
217 //----------------------------------------------------------------------
218 void Symtab::InitNameIndexes() {
219 // Protected function, no need to lock mutex...
220 if (!m_name_indexes_computed) {
221 m_name_indexes_computed = true;
222 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
223 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
224 // Create the name index vector to be able to quickly search by name
225 const size_t num_symbols = m_symbols.size();
227 m_name_to_index.Reserve(num_symbols);
229 // TODO: benchmark this to see if we save any memory. Otherwise we
230 // will always keep the memory reserved in the vector unless we pull some
231 // STL swap magic and then recopy...
232 uint32_t actual_count = 0;
233 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
235 const Mangled &mangled = pos->GetMangled();
236 if (mangled.GetMangledName())
239 if (mangled.GetDemangledName())
243 m_name_to_index.Reserve(actual_count);
246 NameToIndexMap::Entry entry;
248 // The "const char *" in "class_contexts" must come from a
249 // ConstString::GetCString()
250 std::set<const char *> class_contexts;
251 UniqueCStringMap<uint32_t> mangled_name_to_index;
252 std::vector<const char *> symbol_contexts(num_symbols, nullptr);
254 for (entry.value = 0; entry.value < num_symbols; ++entry.value) {
255 const Symbol *symbol = &m_symbols[entry.value];
257 // Don't let trampolines get into the lookup by name map If we ever need
258 // the trampoline symbols to be searchable by name we can remove this and
259 // then possibly add a new bool to any of the Symtab functions that
260 // lookup symbols by name to indicate if they want trampolines.
261 if (symbol->IsTrampoline())
264 const Mangled &mangled = symbol->GetMangled();
265 entry.cstring = mangled.GetMangledName();
267 m_name_to_index.Append(entry);
269 if (symbol->ContainsLinkerAnnotations()) {
270 // If the symbol has linker annotations, also add the version without
272 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations(
273 entry.cstring.GetStringRef()));
274 m_name_to_index.Append(entry);
277 const SymbolType symbol_type = symbol->GetType();
278 if (symbol_type == eSymbolTypeCode ||
279 symbol_type == eSymbolTypeResolver) {
280 llvm::StringRef entry_ref(entry.cstring.GetStringRef());
281 if (entry_ref[0] == '_' && entry_ref[1] == 'Z' &&
282 (entry_ref[2] != 'T' && // avoid virtual table, VTT structure,
283 // typeinfo structure, and typeinfo
285 entry_ref[2] != 'G' && // avoid guard variables
286 entry_ref[2] != 'Z')) // named local entities (if we
287 // eventually handle eSymbolTypeData,
288 // we will want this back)
290 CPlusPlusLanguage::MethodName cxx_method(
291 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus));
292 entry.cstring = ConstString(cxx_method.GetBasename());
294 // ConstString objects permanently store the string in the pool
295 // so calling GetCString() on the value gets us a const char *
296 // that will never go away
297 const char *const_context =
298 ConstString(cxx_method.GetContext()).GetCString();
300 if (!const_context || const_context[0] == 0) {
301 // No context for this function so this has to be a basename
302 m_basename_to_index.Append(entry);
303 // If there is no context (no namespaces or class scopes that
304 // come before the function name) then this also could be a
306 m_name_to_index.Append(entry);
308 entry_ref = entry.cstring.GetStringRef();
309 if (entry_ref[0] == '~' ||
310 !cxx_method.GetQualifiers().empty()) {
311 // The first character of the demangled basename is '~' which
312 // means we have a class destructor. We can use this
313 // information to help us know what is a class and what
315 if (class_contexts.find(const_context) == class_contexts.end())
316 class_contexts.insert(const_context);
317 m_method_to_index.Append(entry);
319 if (class_contexts.find(const_context) !=
320 class_contexts.end()) {
321 // The current decl context is in our "class_contexts"
322 // which means this is a method on a class
323 m_method_to_index.Append(entry);
325 // We don't know if this is a function basename or a
326 // method, so put it into a temporary collection so once we
327 // are done we can look in class_contexts to see if each
328 // entry is a class or just a function and will put any
329 // remaining items into m_method_to_index or
330 // m_basename_to_index as needed
331 mangled_name_to_index.Append(entry);
332 symbol_contexts[entry.value] = const_context;
341 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage());
343 m_name_to_index.Append(entry);
345 if (symbol->ContainsLinkerAnnotations()) {
346 // If the symbol has linker annotations, also add the version without
348 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations(
349 entry.cstring.GetStringRef()));
350 m_name_to_index.Append(entry);
354 // If the demangled name turns out to be an ObjC name, and is a category
355 // name, add the version without categories to the index too.
356 ObjCLanguage::MethodName objc_method(entry.cstring.GetStringRef(), true);
357 if (objc_method.IsValid(true)) {
358 entry.cstring = objc_method.GetSelector();
359 m_selector_to_index.Append(entry);
361 ConstString objc_method_no_category(
362 objc_method.GetFullNameWithoutCategory(true));
363 if (objc_method_no_category) {
364 entry.cstring = objc_method_no_category;
365 m_name_to_index.Append(entry);
371 if (!mangled_name_to_index.IsEmpty()) {
372 count = mangled_name_to_index.GetSize();
373 for (size_t i = 0; i < count; ++i) {
374 if (mangled_name_to_index.GetValueAtIndex(i, entry.value)) {
375 entry.cstring = mangled_name_to_index.GetCStringAtIndex(i);
376 if (symbol_contexts[entry.value] &&
377 class_contexts.find(symbol_contexts[entry.value]) !=
378 class_contexts.end()) {
379 m_method_to_index.Append(entry);
381 // If we got here, we have something that had a context (was inside
382 // a namespace or class) yet we don't know if the entry
383 m_method_to_index.Append(entry);
384 m_basename_to_index.Append(entry);
389 m_name_to_index.Sort();
390 m_name_to_index.SizeToFit();
391 m_selector_to_index.Sort();
392 m_selector_to_index.SizeToFit();
393 m_basename_to_index.Sort();
394 m_basename_to_index.SizeToFit();
395 m_method_to_index.Sort();
396 m_method_to_index.SizeToFit();
400 void Symtab::PreloadSymbols() {
401 std::lock_guard<std::recursive_mutex> guard(m_mutex);
405 void Symtab::AppendSymbolNamesToMap(const IndexCollection &indexes,
406 bool add_demangled, bool add_mangled,
407 NameToIndexMap &name_to_index_map) const {
408 if (add_demangled || add_mangled) {
409 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
410 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
411 std::lock_guard<std::recursive_mutex> guard(m_mutex);
413 // Create the name index vector to be able to quickly search by name
414 NameToIndexMap::Entry entry;
415 const size_t num_indexes = indexes.size();
416 for (size_t i = 0; i < num_indexes; ++i) {
417 entry.value = indexes[i];
418 assert(i < m_symbols.size());
419 const Symbol *symbol = &m_symbols[entry.value];
421 const Mangled &mangled = symbol->GetMangled();
423 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage());
425 name_to_index_map.Append(entry);
429 entry.cstring = mangled.GetMangledName();
431 name_to_index_map.Append(entry);
437 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
438 std::vector<uint32_t> &indexes,
440 uint32_t end_index) const {
441 std::lock_guard<std::recursive_mutex> guard(m_mutex);
443 uint32_t prev_size = indexes.size();
445 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
447 for (uint32_t i = start_idx; i < count; ++i) {
448 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
449 indexes.push_back(i);
452 return indexes.size() - prev_size;
455 uint32_t Symtab::AppendSymbolIndexesWithTypeAndFlagsValue(
456 SymbolType symbol_type, uint32_t flags_value,
457 std::vector<uint32_t> &indexes, uint32_t start_idx,
458 uint32_t end_index) const {
459 std::lock_guard<std::recursive_mutex> guard(m_mutex);
461 uint32_t prev_size = indexes.size();
463 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
465 for (uint32_t i = start_idx; i < count; ++i) {
466 if ((symbol_type == eSymbolTypeAny ||
467 m_symbols[i].GetType() == symbol_type) &&
468 m_symbols[i].GetFlags() == flags_value)
469 indexes.push_back(i);
472 return indexes.size() - prev_size;
475 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
476 Debug symbol_debug_type,
477 Visibility symbol_visibility,
478 std::vector<uint32_t> &indexes,
480 uint32_t end_index) const {
481 std::lock_guard<std::recursive_mutex> guard(m_mutex);
483 uint32_t prev_size = indexes.size();
485 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
487 for (uint32_t i = start_idx; i < count; ++i) {
488 if (symbol_type == eSymbolTypeAny ||
489 m_symbols[i].GetType() == symbol_type) {
490 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
491 indexes.push_back(i);
495 return indexes.size() - prev_size;
498 uint32_t Symtab::GetIndexForSymbol(const Symbol *symbol) const {
499 if (!m_symbols.empty()) {
500 const Symbol *first_symbol = &m_symbols[0];
501 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
502 return symbol - first_symbol;
507 struct SymbolSortInfo {
508 const bool sort_by_load_addr;
509 const Symbol *symbols;
513 struct SymbolIndexComparator {
514 const std::vector<Symbol> &symbols;
515 std::vector<lldb::addr_t> &addr_cache;
517 // Getting from the symbol to the Address to the File Address involves some
518 // work. Since there are potentially many symbols here, and we're using this
519 // for sorting so we're going to be computing the address many times, cache
520 // that in addr_cache. The array passed in has to be the same size as the
521 // symbols array passed into the member variable symbols, and should be
522 // initialized with LLDB_INVALID_ADDRESS.
523 // NOTE: You have to make addr_cache externally and pass it in because
525 // makes copies of the comparator it is initially passed in, and you end up
526 // spending huge amounts of time copying this array...
528 SymbolIndexComparator(const std::vector<Symbol> &s,
529 std::vector<lldb::addr_t> &a)
530 : symbols(s), addr_cache(a) {
531 assert(symbols.size() == addr_cache.size());
533 bool operator()(uint32_t index_a, uint32_t index_b) {
534 addr_t value_a = addr_cache[index_a];
535 if (value_a == LLDB_INVALID_ADDRESS) {
536 value_a = symbols[index_a].GetAddressRef().GetFileAddress();
537 addr_cache[index_a] = value_a;
540 addr_t value_b = addr_cache[index_b];
541 if (value_b == LLDB_INVALID_ADDRESS) {
542 value_b = symbols[index_b].GetAddressRef().GetFileAddress();
543 addr_cache[index_b] = value_b;
546 if (value_a == value_b) {
547 // The if the values are equal, use the original symbol user ID
548 lldb::user_id_t uid_a = symbols[index_a].GetID();
549 lldb::user_id_t uid_b = symbols[index_b].GetID();
555 } else if (value_a < value_b)
563 void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
564 bool remove_duplicates) const {
565 std::lock_guard<std::recursive_mutex> guard(m_mutex);
567 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
568 Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
569 // No need to sort if we have zero or one items...
570 if (indexes.size() <= 1)
573 // Sort the indexes in place using std::stable_sort.
574 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for
576 // not correctness. The indexes vector tends to be "close" to sorted, which
577 // the stable sort handles better.
579 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
581 SymbolIndexComparator comparator(m_symbols, addr_cache);
582 std::stable_sort(indexes.begin(), indexes.end(), comparator);
584 // Remove any duplicates if requested
585 if (remove_duplicates) {
586 auto last = std::unique(indexes.begin(), indexes.end());
587 indexes.erase(last, indexes.end());
591 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name,
592 std::vector<uint32_t> &indexes) {
593 std::lock_guard<std::recursive_mutex> guard(m_mutex);
595 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
596 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
598 if (!m_name_indexes_computed)
601 return m_name_to_index.GetValues(symbol_name, indexes);
606 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name,
607 Debug symbol_debug_type,
608 Visibility symbol_visibility,
609 std::vector<uint32_t> &indexes) {
610 std::lock_guard<std::recursive_mutex> guard(m_mutex);
612 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
613 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
615 const size_t old_size = indexes.size();
616 if (!m_name_indexes_computed)
619 std::vector<uint32_t> all_name_indexes;
620 const size_t name_match_count =
621 m_name_to_index.GetValues(symbol_name, all_name_indexes);
622 for (size_t i = 0; i < name_match_count; ++i) {
623 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type,
625 indexes.push_back(all_name_indexes[i]);
627 return indexes.size() - old_size;
633 Symtab::AppendSymbolIndexesWithNameAndType(const ConstString &symbol_name,
634 SymbolType symbol_type,
635 std::vector<uint32_t> &indexes) {
636 std::lock_guard<std::recursive_mutex> guard(m_mutex);
638 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) {
639 std::vector<uint32_t>::iterator pos = indexes.begin();
640 while (pos != indexes.end()) {
641 if (symbol_type == eSymbolTypeAny ||
642 m_symbols[*pos].GetType() == symbol_type)
645 pos = indexes.erase(pos);
648 return indexes.size();
651 uint32_t Symtab::AppendSymbolIndexesWithNameAndType(
652 const ConstString &symbol_name, SymbolType symbol_type,
653 Debug symbol_debug_type, Visibility symbol_visibility,
654 std::vector<uint32_t> &indexes) {
655 std::lock_guard<std::recursive_mutex> guard(m_mutex);
657 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type,
658 symbol_visibility, indexes) > 0) {
659 std::vector<uint32_t>::iterator pos = indexes.begin();
660 while (pos != indexes.end()) {
661 if (symbol_type == eSymbolTypeAny ||
662 m_symbols[*pos].GetType() == symbol_type)
665 pos = indexes.erase(pos);
668 return indexes.size();
671 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
672 const RegularExpression ®exp, SymbolType symbol_type,
673 std::vector<uint32_t> &indexes) {
674 std::lock_guard<std::recursive_mutex> guard(m_mutex);
676 uint32_t prev_size = indexes.size();
677 uint32_t sym_end = m_symbols.size();
679 for (uint32_t i = 0; i < sym_end; i++) {
680 if (symbol_type == eSymbolTypeAny ||
681 m_symbols[i].GetType() == symbol_type) {
682 const char *name = m_symbols[i].GetName().AsCString();
684 if (regexp.Execute(name))
685 indexes.push_back(i);
689 return indexes.size() - prev_size;
692 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
693 const RegularExpression ®exp, SymbolType symbol_type,
694 Debug symbol_debug_type, Visibility symbol_visibility,
695 std::vector<uint32_t> &indexes) {
696 std::lock_guard<std::recursive_mutex> guard(m_mutex);
698 uint32_t prev_size = indexes.size();
699 uint32_t sym_end = m_symbols.size();
701 for (uint32_t i = 0; i < sym_end; i++) {
702 if (symbol_type == eSymbolTypeAny ||
703 m_symbols[i].GetType() == symbol_type) {
704 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false)
707 const char *name = m_symbols[i].GetName().AsCString();
709 if (regexp.Execute(name))
710 indexes.push_back(i);
714 return indexes.size() - prev_size;
717 Symbol *Symtab::FindSymbolWithType(SymbolType symbol_type,
718 Debug symbol_debug_type,
719 Visibility symbol_visibility,
720 uint32_t &start_idx) {
721 std::lock_guard<std::recursive_mutex> guard(m_mutex);
723 const size_t count = m_symbols.size();
724 for (size_t idx = start_idx; idx < count; ++idx) {
725 if (symbol_type == eSymbolTypeAny ||
726 m_symbols[idx].GetType() == symbol_type) {
727 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) {
729 return &m_symbols[idx];
737 Symtab::FindAllSymbolsWithNameAndType(const ConstString &name,
738 SymbolType symbol_type,
739 std::vector<uint32_t> &symbol_indexes) {
740 std::lock_guard<std::recursive_mutex> guard(m_mutex);
742 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
743 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
744 // Initialize all of the lookup by name indexes before converting NAME to a
745 // uniqued string NAME_STR below.
746 if (!m_name_indexes_computed)
750 // The string table did have a string that matched, but we need to check
751 // the symbols and match the symbol_type if any was given.
752 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes);
754 return symbol_indexes.size();
757 size_t Symtab::FindAllSymbolsWithNameAndType(
758 const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type,
759 Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) {
760 std::lock_guard<std::recursive_mutex> guard(m_mutex);
762 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
763 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
764 // Initialize all of the lookup by name indexes before converting NAME to a
765 // uniqued string NAME_STR below.
766 if (!m_name_indexes_computed)
770 // The string table did have a string that matched, but we need to check
771 // the symbols and match the symbol_type if any was given.
772 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
773 symbol_visibility, symbol_indexes);
775 return symbol_indexes.size();
778 size_t Symtab::FindAllSymbolsMatchingRexExAndType(
779 const RegularExpression ®ex, SymbolType symbol_type,
780 Debug symbol_debug_type, Visibility symbol_visibility,
781 std::vector<uint32_t> &symbol_indexes) {
782 std::lock_guard<std::recursive_mutex> guard(m_mutex);
784 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type,
785 symbol_visibility, symbol_indexes);
786 return symbol_indexes.size();
789 Symbol *Symtab::FindFirstSymbolWithNameAndType(const ConstString &name,
790 SymbolType symbol_type,
791 Debug symbol_debug_type,
792 Visibility symbol_visibility) {
793 std::lock_guard<std::recursive_mutex> guard(m_mutex);
795 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
796 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
797 if (!m_name_indexes_computed)
801 std::vector<uint32_t> matching_indexes;
802 // The string table did have a string that matched, but we need to check
803 // the symbols and match the symbol_type if any was given.
804 if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
807 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
808 for (pos = matching_indexes.begin(); pos != end; ++pos) {
809 Symbol *symbol = SymbolAtIndex(*pos);
811 if (symbol->Compare(name, symbol_type))
820 const Symtab *symtab;
821 const addr_t file_addr;
822 Symbol *match_symbol;
823 const uint32_t *match_index_ptr;
827 // Add all the section file start address & size to the RangeVector, recusively
828 // adding any children sections.
829 static void AddSectionsToRangeMap(SectionList *sectlist,
830 RangeVector<addr_t, addr_t> §ion_ranges) {
831 const int num_sections = sectlist->GetNumSections(0);
832 for (int i = 0; i < num_sections; i++) {
833 SectionSP sect_sp = sectlist->GetSectionAtIndex(i);
835 SectionList &child_sectlist = sect_sp->GetChildren();
837 // If this section has children, add the children to the RangeVector.
838 // Else add this section to the RangeVector.
839 if (child_sectlist.GetNumSections(0) > 0) {
840 AddSectionsToRangeMap(&child_sectlist, section_ranges);
842 size_t size = sect_sp->GetByteSize();
844 addr_t base_addr = sect_sp->GetFileAddress();
845 RangeVector<addr_t, addr_t>::Entry entry;
846 entry.SetRangeBase(base_addr);
847 entry.SetByteSize(size);
848 section_ranges.Append(entry);
855 void Symtab::InitAddressIndexes() {
856 // Protected function, no need to lock mutex...
857 if (!m_file_addr_to_index_computed && !m_symbols.empty()) {
858 m_file_addr_to_index_computed = true;
860 FileRangeToIndexMap::Entry entry;
861 const_iterator begin = m_symbols.begin();
862 const_iterator end = m_symbols.end();
863 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
864 if (pos->ValueIsAddress()) {
865 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
866 entry.SetByteSize(pos->GetByteSize());
867 entry.data = std::distance(begin, pos);
868 m_file_addr_to_index.Append(entry);
871 const size_t num_entries = m_file_addr_to_index.GetSize();
872 if (num_entries > 0) {
873 m_file_addr_to_index.Sort();
875 // Create a RangeVector with the start & size of all the sections for
876 // this objfile. We'll need to check this for any FileRangeToIndexMap
877 // entries with an uninitialized size, which could potentially be a large
878 // number so reconstituting the weak pointer is busywork when it is
879 // invariant information.
880 SectionList *sectlist = m_objfile->GetSectionList();
881 RangeVector<addr_t, addr_t> section_ranges;
883 AddSectionsToRangeMap(sectlist, section_ranges);
884 section_ranges.Sort();
887 // Iterate through the FileRangeToIndexMap and fill in the size for any
888 // entries that didn't already have a size from the Symbol (e.g. if we
889 // have a plain linker symbol with an address only, instead of debug info
890 // where we get an address and a size and a type, etc.)
891 for (size_t i = 0; i < num_entries; i++) {
892 FileRangeToIndexMap::Entry *entry =
893 m_file_addr_to_index.GetMutableEntryAtIndex(i);
894 if (entry->GetByteSize() == 0) {
895 addr_t curr_base_addr = entry->GetRangeBase();
896 const RangeVector<addr_t, addr_t>::Entry *containing_section =
897 section_ranges.FindEntryThatContains(curr_base_addr);
899 // Use the end of the section as the default max size of the symbol
901 if (containing_section) {
903 containing_section->GetByteSize() -
904 (entry->GetRangeBase() - containing_section->GetRangeBase());
907 for (size_t j = i; j < num_entries; j++) {
908 FileRangeToIndexMap::Entry *next_entry =
909 m_file_addr_to_index.GetMutableEntryAtIndex(j);
910 addr_t next_base_addr = next_entry->GetRangeBase();
911 if (next_base_addr > curr_base_addr) {
912 addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
914 // Take the difference between this symbol and the next one as
915 // its size, if it is less than the size of the section.
916 if (sym_size == 0 || size_to_next_symbol < sym_size) {
917 sym_size = size_to_next_symbol;
924 entry->SetByteSize(sym_size);
925 Symbol &symbol = m_symbols[entry->data];
926 symbol.SetByteSize(sym_size);
927 symbol.SetSizeIsSynthesized(true);
932 // Sort again in case the range size changes the ordering
933 m_file_addr_to_index.Sort();
938 void Symtab::CalculateSymbolSizes() {
939 std::lock_guard<std::recursive_mutex> guard(m_mutex);
941 if (!m_symbols.empty()) {
942 if (!m_file_addr_to_index_computed)
943 InitAddressIndexes();
945 const size_t num_entries = m_file_addr_to_index.GetSize();
947 for (size_t i = 0; i < num_entries; ++i) {
948 // The entries in the m_file_addr_to_index have calculated the sizes
949 // already so we will use this size if we need to.
950 const FileRangeToIndexMap::Entry &entry =
951 m_file_addr_to_index.GetEntryRef(i);
953 Symbol &symbol = m_symbols[entry.data];
955 // If the symbol size is already valid, no need to do anything
956 if (symbol.GetByteSizeIsValid())
959 const addr_t range_size = entry.GetByteSize();
960 if (range_size > 0) {
961 symbol.SetByteSize(range_size);
962 symbol.SetSizeIsSynthesized(true);
968 Symbol *Symtab::FindSymbolAtFileAddress(addr_t file_addr) {
969 std::lock_guard<std::recursive_mutex> guard(m_mutex);
970 if (!m_file_addr_to_index_computed)
971 InitAddressIndexes();
973 const FileRangeToIndexMap::Entry *entry =
974 m_file_addr_to_index.FindEntryStartsAt(file_addr);
976 Symbol *symbol = SymbolAtIndex(entry->data);
977 if (symbol->GetFileAddress() == file_addr)
983 Symbol *Symtab::FindSymbolContainingFileAddress(addr_t file_addr) {
984 std::lock_guard<std::recursive_mutex> guard(m_mutex);
986 if (!m_file_addr_to_index_computed)
987 InitAddressIndexes();
989 const FileRangeToIndexMap::Entry *entry =
990 m_file_addr_to_index.FindEntryThatContains(file_addr);
992 Symbol *symbol = SymbolAtIndex(entry->data);
993 if (symbol->ContainsFileAddress(file_addr))
999 void Symtab::ForEachSymbolContainingFileAddress(
1000 addr_t file_addr, std::function<bool(Symbol *)> const &callback) {
1001 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1003 if (!m_file_addr_to_index_computed)
1004 InitAddressIndexes();
1006 std::vector<uint32_t> all_addr_indexes;
1008 // Get all symbols with file_addr
1009 const size_t addr_match_count =
1010 m_file_addr_to_index.FindEntryIndexesThatContain(file_addr,
1013 for (size_t i = 0; i < addr_match_count; ++i) {
1014 Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]);
1015 if (symbol->ContainsFileAddress(file_addr)) {
1016 if (!callback(symbol))
1022 void Symtab::SymbolIndicesToSymbolContextList(
1023 std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) {
1024 // No need to protect this call using m_mutex all other method calls are
1025 // already thread safe.
1027 const bool merge_symbol_into_function = true;
1028 size_t num_indices = symbol_indexes.size();
1029 if (num_indices > 0) {
1031 sc.module_sp = m_objfile->GetModule();
1032 for (size_t i = 0; i < num_indices; i++) {
1033 sc.symbol = SymbolAtIndex(symbol_indexes[i]);
1035 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1040 size_t Symtab::FindFunctionSymbols(const ConstString &name,
1041 uint32_t name_type_mask,
1042 SymbolContextList &sc_list) {
1044 std::vector<uint32_t> symbol_indexes;
1046 // eFunctionNameTypeAuto should be pre-resolved by a call to
1047 // Module::LookupInfo::LookupInfo()
1048 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1050 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1051 std::vector<uint32_t> temp_symbol_indexes;
1052 FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes);
1054 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1055 if (temp_symbol_indexes_size > 0) {
1056 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1057 for (unsigned i = 0; i < temp_symbol_indexes_size; i++) {
1058 SymbolContext sym_ctx;
1059 sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]);
1060 if (sym_ctx.symbol) {
1061 switch (sym_ctx.symbol->GetType()) {
1062 case eSymbolTypeCode:
1063 case eSymbolTypeResolver:
1064 case eSymbolTypeReExported:
1065 symbol_indexes.push_back(temp_symbol_indexes[i]);
1075 if (name_type_mask & eFunctionNameTypeBase) {
1076 // From mangled names we can't tell what is a basename and what is a method
1077 // name, so we just treat them the same
1078 if (!m_name_indexes_computed)
1081 if (!m_basename_to_index.IsEmpty()) {
1082 const UniqueCStringMap<uint32_t>::Entry *match;
1083 for (match = m_basename_to_index.FindFirstValueForName(name);
1085 match = m_basename_to_index.FindNextValueForName(match)) {
1086 symbol_indexes.push_back(match->value);
1091 if (name_type_mask & eFunctionNameTypeMethod) {
1092 if (!m_name_indexes_computed)
1095 if (!m_method_to_index.IsEmpty()) {
1096 const UniqueCStringMap<uint32_t>::Entry *match;
1097 for (match = m_method_to_index.FindFirstValueForName(name);
1099 match = m_method_to_index.FindNextValueForName(match)) {
1100 symbol_indexes.push_back(match->value);
1105 if (name_type_mask & eFunctionNameTypeSelector) {
1106 if (!m_name_indexes_computed)
1109 if (!m_selector_to_index.IsEmpty()) {
1110 const UniqueCStringMap<uint32_t>::Entry *match;
1111 for (match = m_selector_to_index.FindFirstValueForName(name);
1113 match = m_selector_to_index.FindNextValueForName(match)) {
1114 symbol_indexes.push_back(match->value);
1119 if (!symbol_indexes.empty()) {
1120 std::sort(symbol_indexes.begin(), symbol_indexes.end());
1121 symbol_indexes.erase(
1122 std::unique(symbol_indexes.begin(), symbol_indexes.end()),
1123 symbol_indexes.end());
1124 count = symbol_indexes.size();
1125 SymbolIndicesToSymbolContextList(symbol_indexes, sc_list);
1131 const Symbol *Symtab::GetParent(Symbol *child_symbol) const {
1132 uint32_t child_idx = GetIndexForSymbol(child_symbol);
1133 if (child_idx != UINT32_MAX && child_idx > 0) {
1134 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) {
1135 const Symbol *symbol = SymbolAtIndex(idx);
1136 const uint32_t sibling_idx = symbol->GetSiblingIndex();
1137 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)