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/Section.h"
17 #include "lldb/Core/STLUtils.h"
18 #include "lldb/Core/Timer.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Symbol/Symtab.h"
23 #include "lldb/Utility/RegularExpression.h"
24 #include "lldb/Utility/Stream.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
104 // isn't sorted by name. So we must make the ordered symbol list
106 s->PutCString(" (sorted by name):\n");
108 typedef std::multimap<const char *, const Symbol *,
109 CStringCompareFunctionObject>
111 CStringToSymbol name_map;
112 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
114 const char *name = pos->GetName().AsCString();
116 name_map.insert(std::make_pair(name, &(*pos)));
119 for (CStringToSymbol::const_iterator pos = name_map.begin(),
120 end = name_map.end();
123 pos->second->Dump(s, target, pos->second - &m_symbols[0]);
127 case eSortOrderByAddress:
128 s->PutCString(" (sorted by address):\n");
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) {
135 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data;
136 m_symbols[symbol_idx].Dump(s, target, symbol_idx);
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);
147 const size_t num_symbols = GetNumSymbols();
148 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
150 s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n",
151 (uint64_t)indexes.size(), (uint64_t)m_symbols.size());
154 if (!indexes.empty()) {
155 std::vector<uint32_t>::const_iterator pos;
156 std::vector<uint32_t>::const_iterator end = indexes.end();
158 for (pos = indexes.begin(); pos != end; ++pos) {
160 if (idx < num_symbols) {
162 m_symbols[idx].Dump(s, target, idx);
169 void Symtab::DumpSymbolHeader(Stream *s) {
170 s->Indent(" Debug symbol\n");
171 s->Indent(" |Synthetic symbol\n");
172 s->Indent(" ||Externally Visible\n");
174 s->Indent("Index UserID DSX Type File Address/Value Load "
175 "Address Size Flags Name\n");
176 s->Indent("------- ------ --- --------------- ------------------ "
177 "------------------ ------------------ ---------- "
178 "----------------------------------\n");
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)
186 if (match_uid > symbol_uid)
191 Symbol *Symtab::FindSymbolByID(lldb::user_id_t symbol_uid) const {
192 std::lock_guard<std::recursive_mutex> guard(m_mutex);
195 (Symbol *)::bsearch(&symbol_uid, &m_symbols[0], m_symbols.size(),
196 sizeof(m_symbols[0]), CompareSymbolID);
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];
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];
216 //----------------------------------------------------------------------
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();
228 m_name_to_index.Reserve(num_symbols);
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();
236 const Mangled &mangled = pos->GetMangled();
237 if (mangled.GetMangledName())
240 if (mangled.GetDemangledName())
244 m_name_to_index.Reserve(actual_count);
247 NameToIndexMap::Entry entry;
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);
255 for (entry.value = 0; entry.value < num_symbols; ++entry.value) {
256 const Symbol *symbol = &m_symbols[entry.value];
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
263 if (symbol->IsTrampoline())
266 const Mangled &mangled = symbol->GetMangled();
267 entry.cstring = mangled.GetMangledName();
269 m_name_to_index.Append(entry);
271 if (symbol->ContainsLinkerAnnotations()) {
272 // If the symbol has linker annotations, also add the version without
274 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations(
275 entry.cstring.GetStringRef()));
276 m_name_to_index.Append(entry);
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
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)
292 CPlusPlusLanguage::MethodName cxx_method(
293 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus));
294 entry.cstring = ConstString(cxx_method.GetBasename());
296 // ConstString objects permanently store the string in the pool so
298 // GetCString() on the value gets us a const char * that will
300 const char *const_context =
301 ConstString(cxx_method.GetContext()).GetCString();
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
309 m_name_to_index.Append(entry);
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);
321 if (class_contexts.find(const_context) !=
322 class_contexts.end()) {
323 // The current decl context is in our "class_contexts" which
325 // this is a method on a class
326 m_method_to_index.Append(entry);
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
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;
344 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage());
346 m_name_to_index.Append(entry);
348 if (symbol->ContainsLinkerAnnotations()) {
349 // If the symbol has linker annotations, also add the version without
351 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations(
352 entry.cstring.GetStringRef()));
353 m_name_to_index.Append(entry);
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
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);
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);
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);
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);
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();
403 // static StreamFile a ("/tmp/a.txt");
405 // count = m_basename_to_index.GetSize();
408 // for (size_t i=0; i<count; ++i)
410 // if (m_basename_to_index.GetValueAtIndex(i, entry.value))
411 // a.Printf ("%s BASENAME\n",
412 // m_symbols[entry.value].GetMangled().GetName().GetCString());
415 // count = m_method_to_index.GetSize();
418 // for (size_t i=0; i<count; ++i)
420 // if (m_method_to_index.GetValueAtIndex(i, entry.value))
421 // a.Printf ("%s METHOD\n",
422 // m_symbols[entry.value].GetMangled().GetName().GetCString());
428 void Symtab::PreloadSymbols() {
429 std::lock_guard<std::recursive_mutex> guard(m_mutex);
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);
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];
449 const Mangled &mangled = symbol->GetMangled();
451 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage());
453 name_to_index_map.Append(entry);
457 entry.cstring = mangled.GetMangledName();
459 name_to_index_map.Append(entry);
465 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
466 std::vector<uint32_t> &indexes,
468 uint32_t end_index) const {
469 std::lock_guard<std::recursive_mutex> guard(m_mutex);
471 uint32_t prev_size = indexes.size();
473 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
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);
480 return indexes.size() - prev_size;
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);
489 uint32_t prev_size = indexes.size();
491 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
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);
500 return indexes.size() - prev_size;
503 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
504 Debug symbol_debug_type,
505 Visibility symbol_visibility,
506 std::vector<uint32_t> &indexes,
508 uint32_t end_index) const {
509 std::lock_guard<std::recursive_mutex> guard(m_mutex);
511 uint32_t prev_size = indexes.size();
513 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
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);
523 return indexes.size() - prev_size;
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;
535 struct SymbolSortInfo {
536 const bool sort_by_load_addr;
537 const Symbol *symbols;
541 struct SymbolIndexComparator {
542 const std::vector<Symbol> &symbols;
543 std::vector<lldb::addr_t> &addr_cache;
545 // Getting from the symbol to the Address to the File Address involves some
547 // Since there are potentially many symbols here, and we're using this for
549 // we're going to be computing the address many times, cache that in
551 // The array passed in has to be the same size as the symbols array passed
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
557 // makes copies of the comparator it is initially passed in, and you end up
559 // huge amounts of time copying this array...
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());
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;
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;
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();
588 } else if (value_a < value_b)
596 void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
597 bool remove_duplicates) const {
598 std::lock_guard<std::recursive_mutex> guard(m_mutex);
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)
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
609 // not correctness. The indexes vector tends to be "close" to sorted, which
611 // stable sort handles better.
613 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
615 SymbolIndexComparator comparator(m_symbols, addr_cache);
616 std::stable_sort(indexes.begin(), indexes.end(), comparator);
618 // Remove any duplicates if requested
619 if (remove_duplicates)
620 std::unique(indexes.begin(), indexes.end());
623 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name,
624 std::vector<uint32_t> &indexes) {
625 std::lock_guard<std::recursive_mutex> guard(m_mutex);
627 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
628 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
630 if (!m_name_indexes_computed)
633 return m_name_to_index.GetValues(symbol_name, indexes);
638 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name,
639 Debug symbol_debug_type,
640 Visibility symbol_visibility,
641 std::vector<uint32_t> &indexes) {
642 std::lock_guard<std::recursive_mutex> guard(m_mutex);
644 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
645 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
647 const size_t old_size = indexes.size();
648 if (!m_name_indexes_computed)
651 std::vector<uint32_t> all_name_indexes;
652 const size_t name_match_count =
653 m_name_to_index.GetValues(symbol_name, all_name_indexes);
654 for (size_t i = 0; i < name_match_count; ++i) {
655 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type,
657 indexes.push_back(all_name_indexes[i]);
659 return indexes.size() - old_size;
665 Symtab::AppendSymbolIndexesWithNameAndType(const ConstString &symbol_name,
666 SymbolType symbol_type,
667 std::vector<uint32_t> &indexes) {
668 std::lock_guard<std::recursive_mutex> guard(m_mutex);
670 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) {
671 std::vector<uint32_t>::iterator pos = indexes.begin();
672 while (pos != indexes.end()) {
673 if (symbol_type == eSymbolTypeAny ||
674 m_symbols[*pos].GetType() == symbol_type)
677 pos = indexes.erase(pos);
680 return indexes.size();
683 uint32_t Symtab::AppendSymbolIndexesWithNameAndType(
684 const ConstString &symbol_name, SymbolType symbol_type,
685 Debug symbol_debug_type, Visibility symbol_visibility,
686 std::vector<uint32_t> &indexes) {
687 std::lock_guard<std::recursive_mutex> guard(m_mutex);
689 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type,
690 symbol_visibility, indexes) > 0) {
691 std::vector<uint32_t>::iterator pos = indexes.begin();
692 while (pos != indexes.end()) {
693 if (symbol_type == eSymbolTypeAny ||
694 m_symbols[*pos].GetType() == symbol_type)
697 pos = indexes.erase(pos);
700 return indexes.size();
703 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
704 const RegularExpression ®exp, SymbolType symbol_type,
705 std::vector<uint32_t> &indexes) {
706 std::lock_guard<std::recursive_mutex> guard(m_mutex);
708 uint32_t prev_size = indexes.size();
709 uint32_t sym_end = m_symbols.size();
711 for (uint32_t i = 0; i < sym_end; i++) {
712 if (symbol_type == eSymbolTypeAny ||
713 m_symbols[i].GetType() == symbol_type) {
714 const char *name = m_symbols[i].GetName().AsCString();
716 if (regexp.Execute(name))
717 indexes.push_back(i);
721 return indexes.size() - prev_size;
724 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
725 const RegularExpression ®exp, SymbolType symbol_type,
726 Debug symbol_debug_type, Visibility symbol_visibility,
727 std::vector<uint32_t> &indexes) {
728 std::lock_guard<std::recursive_mutex> guard(m_mutex);
730 uint32_t prev_size = indexes.size();
731 uint32_t sym_end = m_symbols.size();
733 for (uint32_t i = 0; i < sym_end; i++) {
734 if (symbol_type == eSymbolTypeAny ||
735 m_symbols[i].GetType() == symbol_type) {
736 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false)
739 const char *name = m_symbols[i].GetName().AsCString();
741 if (regexp.Execute(name))
742 indexes.push_back(i);
746 return indexes.size() - prev_size;
749 Symbol *Symtab::FindSymbolWithType(SymbolType symbol_type,
750 Debug symbol_debug_type,
751 Visibility symbol_visibility,
752 uint32_t &start_idx) {
753 std::lock_guard<std::recursive_mutex> guard(m_mutex);
755 const size_t count = m_symbols.size();
756 for (size_t idx = start_idx; idx < count; ++idx) {
757 if (symbol_type == eSymbolTypeAny ||
758 m_symbols[idx].GetType() == symbol_type) {
759 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) {
761 return &m_symbols[idx];
769 Symtab::FindAllSymbolsWithNameAndType(const ConstString &name,
770 SymbolType symbol_type,
771 std::vector<uint32_t> &symbol_indexes) {
772 std::lock_guard<std::recursive_mutex> guard(m_mutex);
774 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
775 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
776 // Initialize all of the lookup by name indexes before converting NAME
777 // to a uniqued string NAME_STR below.
778 if (!m_name_indexes_computed)
782 // The string table did have a string that matched, but we need
783 // to check the symbols and match the symbol_type if any was given.
784 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes);
786 return symbol_indexes.size();
789 size_t Symtab::FindAllSymbolsWithNameAndType(
790 const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type,
791 Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) {
792 std::lock_guard<std::recursive_mutex> guard(m_mutex);
794 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
795 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
796 // Initialize all of the lookup by name indexes before converting NAME
797 // to a uniqued string NAME_STR below.
798 if (!m_name_indexes_computed)
802 // The string table did have a string that matched, but we need
803 // to check the symbols and match the symbol_type if any was given.
804 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
805 symbol_visibility, symbol_indexes);
807 return symbol_indexes.size();
810 size_t Symtab::FindAllSymbolsMatchingRexExAndType(
811 const RegularExpression ®ex, SymbolType symbol_type,
812 Debug symbol_debug_type, Visibility symbol_visibility,
813 std::vector<uint32_t> &symbol_indexes) {
814 std::lock_guard<std::recursive_mutex> guard(m_mutex);
816 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type,
817 symbol_visibility, symbol_indexes);
818 return symbol_indexes.size();
821 Symbol *Symtab::FindFirstSymbolWithNameAndType(const ConstString &name,
822 SymbolType symbol_type,
823 Debug symbol_debug_type,
824 Visibility symbol_visibility) {
825 std::lock_guard<std::recursive_mutex> guard(m_mutex);
827 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
828 Timer scoped_timer(func_cat, "%s", LLVM_PRETTY_FUNCTION);
829 if (!m_name_indexes_computed)
833 std::vector<uint32_t> matching_indexes;
834 // The string table did have a string that matched, but we need
835 // to check the symbols and match the symbol_type if any was given.
836 if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
839 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
840 for (pos = matching_indexes.begin(); pos != end; ++pos) {
841 Symbol *symbol = SymbolAtIndex(*pos);
843 if (symbol->Compare(name, symbol_type))
852 const Symtab *symtab;
853 const addr_t file_addr;
854 Symbol *match_symbol;
855 const uint32_t *match_index_ptr;
859 // Add all the section file start address & size to the RangeVector,
860 // recusively adding any children sections.
861 static void AddSectionsToRangeMap(SectionList *sectlist,
862 RangeVector<addr_t, addr_t> §ion_ranges) {
863 const int num_sections = sectlist->GetNumSections(0);
864 for (int i = 0; i < num_sections; i++) {
865 SectionSP sect_sp = sectlist->GetSectionAtIndex(i);
867 SectionList &child_sectlist = sect_sp->GetChildren();
869 // If this section has children, add the children to the RangeVector.
870 // Else add this section to the RangeVector.
871 if (child_sectlist.GetNumSections(0) > 0) {
872 AddSectionsToRangeMap(&child_sectlist, section_ranges);
874 size_t size = sect_sp->GetByteSize();
876 addr_t base_addr = sect_sp->GetFileAddress();
877 RangeVector<addr_t, addr_t>::Entry entry;
878 entry.SetRangeBase(base_addr);
879 entry.SetByteSize(size);
880 section_ranges.Append(entry);
887 void Symtab::InitAddressIndexes() {
888 // Protected function, no need to lock mutex...
889 if (!m_file_addr_to_index_computed && !m_symbols.empty()) {
890 m_file_addr_to_index_computed = true;
892 FileRangeToIndexMap::Entry entry;
893 const_iterator begin = m_symbols.begin();
894 const_iterator end = m_symbols.end();
895 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
896 if (pos->ValueIsAddress()) {
897 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
898 entry.SetByteSize(pos->GetByteSize());
899 entry.data = std::distance(begin, pos);
900 m_file_addr_to_index.Append(entry);
903 const size_t num_entries = m_file_addr_to_index.GetSize();
904 if (num_entries > 0) {
905 m_file_addr_to_index.Sort();
907 // Create a RangeVector with the start & size of all the sections for
908 // this objfile. We'll need to check this for any FileRangeToIndexMap
909 // entries with an uninitialized size, which could potentially be a
910 // large number so reconstituting the weak pointer is busywork when it
911 // is invariant information.
912 SectionList *sectlist = m_objfile->GetSectionList();
913 RangeVector<addr_t, addr_t> section_ranges;
915 AddSectionsToRangeMap(sectlist, section_ranges);
916 section_ranges.Sort();
919 // Iterate through the FileRangeToIndexMap and fill in the size for any
920 // entries that didn't already have a size from the Symbol (e.g. if we
921 // have a plain linker symbol with an address only, instead of debug info
922 // where we get an address and a size and a type, etc.)
923 for (size_t i = 0; i < num_entries; i++) {
924 FileRangeToIndexMap::Entry *entry =
925 m_file_addr_to_index.GetMutableEntryAtIndex(i);
926 if (entry->GetByteSize() == 0) {
927 addr_t curr_base_addr = entry->GetRangeBase();
928 const RangeVector<addr_t, addr_t>::Entry *containing_section =
929 section_ranges.FindEntryThatContains(curr_base_addr);
931 // Use the end of the section as the default max size of the symbol
933 if (containing_section) {
935 containing_section->GetByteSize() -
936 (entry->GetRangeBase() - containing_section->GetRangeBase());
939 for (size_t j = i; j < num_entries; j++) {
940 FileRangeToIndexMap::Entry *next_entry =
941 m_file_addr_to_index.GetMutableEntryAtIndex(j);
942 addr_t next_base_addr = next_entry->GetRangeBase();
943 if (next_base_addr > curr_base_addr) {
944 addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
946 // Take the difference between this symbol and the next one as its
948 // if it is less than the size of the section.
949 if (sym_size == 0 || size_to_next_symbol < sym_size) {
950 sym_size = size_to_next_symbol;
957 entry->SetByteSize(sym_size);
958 Symbol &symbol = m_symbols[entry->data];
959 symbol.SetByteSize(sym_size);
960 symbol.SetSizeIsSynthesized(true);
965 // Sort again in case the range size changes the ordering
966 m_file_addr_to_index.Sort();
971 void Symtab::CalculateSymbolSizes() {
972 std::lock_guard<std::recursive_mutex> guard(m_mutex);
974 if (!m_symbols.empty()) {
975 if (!m_file_addr_to_index_computed)
976 InitAddressIndexes();
978 const size_t num_entries = m_file_addr_to_index.GetSize();
980 for (size_t i = 0; i < num_entries; ++i) {
981 // The entries in the m_file_addr_to_index have calculated the sizes
983 // so we will use this size if we need to.
984 const FileRangeToIndexMap::Entry &entry =
985 m_file_addr_to_index.GetEntryRef(i);
987 Symbol &symbol = m_symbols[entry.data];
989 // If the symbol size is already valid, no need to do anything
990 if (symbol.GetByteSizeIsValid())
993 const addr_t range_size = entry.GetByteSize();
994 if (range_size > 0) {
995 symbol.SetByteSize(range_size);
996 symbol.SetSizeIsSynthesized(true);
1002 Symbol *Symtab::FindSymbolAtFileAddress(addr_t file_addr) {
1003 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1004 if (!m_file_addr_to_index_computed)
1005 InitAddressIndexes();
1007 const FileRangeToIndexMap::Entry *entry =
1008 m_file_addr_to_index.FindEntryStartsAt(file_addr);
1010 Symbol *symbol = SymbolAtIndex(entry->data);
1011 if (symbol->GetFileAddress() == file_addr)
1017 Symbol *Symtab::FindSymbolContainingFileAddress(addr_t file_addr) {
1018 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1020 if (!m_file_addr_to_index_computed)
1021 InitAddressIndexes();
1023 const FileRangeToIndexMap::Entry *entry =
1024 m_file_addr_to_index.FindEntryThatContains(file_addr);
1026 Symbol *symbol = SymbolAtIndex(entry->data);
1027 if (symbol->ContainsFileAddress(file_addr))
1033 void Symtab::ForEachSymbolContainingFileAddress(
1034 addr_t file_addr, std::function<bool(Symbol *)> const &callback) {
1035 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1037 if (!m_file_addr_to_index_computed)
1038 InitAddressIndexes();
1040 std::vector<uint32_t> all_addr_indexes;
1042 // Get all symbols with file_addr
1043 const size_t addr_match_count =
1044 m_file_addr_to_index.FindEntryIndexesThatContain(file_addr,
1047 for (size_t i = 0; i < addr_match_count; ++i) {
1048 Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]);
1049 if (symbol->ContainsFileAddress(file_addr)) {
1050 if (!callback(symbol))
1056 void Symtab::SymbolIndicesToSymbolContextList(
1057 std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) {
1058 // No need to protect this call using m_mutex all other method calls are
1059 // already thread safe.
1061 const bool merge_symbol_into_function = true;
1062 size_t num_indices = symbol_indexes.size();
1063 if (num_indices > 0) {
1065 sc.module_sp = m_objfile->GetModule();
1066 for (size_t i = 0; i < num_indices; i++) {
1067 sc.symbol = SymbolAtIndex(symbol_indexes[i]);
1069 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1074 size_t Symtab::FindFunctionSymbols(const ConstString &name,
1075 uint32_t name_type_mask,
1076 SymbolContextList &sc_list) {
1078 std::vector<uint32_t> symbol_indexes;
1080 // eFunctionNameTypeAuto should be pre-resolved by a call to
1081 // Module::LookupInfo::LookupInfo()
1082 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1084 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1085 std::vector<uint32_t> temp_symbol_indexes;
1086 FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes);
1088 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1089 if (temp_symbol_indexes_size > 0) {
1090 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1091 for (unsigned i = 0; i < temp_symbol_indexes_size; i++) {
1092 SymbolContext sym_ctx;
1093 sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]);
1094 if (sym_ctx.symbol) {
1095 switch (sym_ctx.symbol->GetType()) {
1096 case eSymbolTypeCode:
1097 case eSymbolTypeResolver:
1098 case eSymbolTypeReExported:
1099 symbol_indexes.push_back(temp_symbol_indexes[i]);
1109 if (name_type_mask & eFunctionNameTypeBase) {
1110 // From mangled names we can't tell what is a basename and what
1111 // is a method name, so we just treat them the same
1112 if (!m_name_indexes_computed)
1115 if (!m_basename_to_index.IsEmpty()) {
1116 const UniqueCStringMap<uint32_t>::Entry *match;
1117 for (match = m_basename_to_index.FindFirstValueForName(name);
1119 match = m_basename_to_index.FindNextValueForName(match)) {
1120 symbol_indexes.push_back(match->value);
1125 if (name_type_mask & eFunctionNameTypeMethod) {
1126 if (!m_name_indexes_computed)
1129 if (!m_method_to_index.IsEmpty()) {
1130 const UniqueCStringMap<uint32_t>::Entry *match;
1131 for (match = m_method_to_index.FindFirstValueForName(name);
1133 match = m_method_to_index.FindNextValueForName(match)) {
1134 symbol_indexes.push_back(match->value);
1139 if (name_type_mask & eFunctionNameTypeSelector) {
1140 if (!m_name_indexes_computed)
1143 if (!m_selector_to_index.IsEmpty()) {
1144 const UniqueCStringMap<uint32_t>::Entry *match;
1145 for (match = m_selector_to_index.FindFirstValueForName(name);
1147 match = m_selector_to_index.FindNextValueForName(match)) {
1148 symbol_indexes.push_back(match->value);
1153 if (!symbol_indexes.empty()) {
1154 std::sort(symbol_indexes.begin(), symbol_indexes.end());
1155 symbol_indexes.erase(
1156 std::unique(symbol_indexes.begin(), symbol_indexes.end()),
1157 symbol_indexes.end());
1158 count = symbol_indexes.size();
1159 SymbolIndicesToSymbolContextList(symbol_indexes, sc_list);
1165 const Symbol *Symtab::GetParent(Symbol *child_symbol) const {
1166 uint32_t child_idx = GetIndexForSymbol(child_symbol);
1167 if (child_idx != UINT32_MAX && child_idx > 0) {
1168 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) {
1169 const Symbol *symbol = SymbolAtIndex(idx);
1170 const uint32_t sibling_idx = symbol->GetSiblingIndex();
1171 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)