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