xref: /freebsd/contrib/llvm-project/lldb/source/Symbol/Symtab.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 //===-- Symtab.cpp --------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <map>
10 #include <set>
11 
12 #include "lldb/Core/DataFileCache.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/RichManglingContext.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 #include "lldb/Symbol/Symtab.h"
20 #include "lldb/Target/Language.h"
21 #include "lldb/Utility/DataEncoder.h"
22 #include "lldb/Utility/Endian.h"
23 #include "lldb/Utility/RegularExpression.h"
24 #include "lldb/Utility/Stream.h"
25 #include "lldb/Utility/Timer.h"
26 
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Support/DJB.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 Symtab::Symtab(ObjectFile *objfile)
35     : m_objfile(objfile), m_symbols(), m_file_addr_to_index(*this),
36       m_name_to_symbol_indices(), m_mutex(),
37       m_file_addr_to_index_computed(false), m_name_indexes_computed(false),
38       m_loaded_from_cache(false), m_saved_to_cache(false) {
39   m_name_to_symbol_indices.emplace(std::make_pair(
40       lldb::eFunctionNameTypeNone, UniqueCStringMap<uint32_t>()));
41   m_name_to_symbol_indices.emplace(std::make_pair(
42       lldb::eFunctionNameTypeBase, UniqueCStringMap<uint32_t>()));
43   m_name_to_symbol_indices.emplace(std::make_pair(
44       lldb::eFunctionNameTypeMethod, UniqueCStringMap<uint32_t>()));
45   m_name_to_symbol_indices.emplace(std::make_pair(
46       lldb::eFunctionNameTypeSelector, UniqueCStringMap<uint32_t>()));
47 }
48 
49 Symtab::~Symtab() = default;
50 
51 void Symtab::Reserve(size_t count) {
52   // Clients should grab the mutex from this symbol table and lock it manually
53   // when calling this function to avoid performance issues.
54   m_symbols.reserve(count);
55 }
56 
57 Symbol *Symtab::Resize(size_t count) {
58   // Clients should grab the mutex from this symbol table and lock it manually
59   // when calling this function to avoid performance issues.
60   m_symbols.resize(count);
61   return m_symbols.empty() ? nullptr : &m_symbols[0];
62 }
63 
64 uint32_t Symtab::AddSymbol(const Symbol &symbol) {
65   // Clients should grab the mutex from this symbol table and lock it manually
66   // when calling this function to avoid performance issues.
67   uint32_t symbol_idx = m_symbols.size();
68   auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
69   name_to_index.Clear();
70   m_file_addr_to_index.Clear();
71   m_symbols.push_back(symbol);
72   m_file_addr_to_index_computed = false;
73   m_name_indexes_computed = false;
74   return symbol_idx;
75 }
76 
77 size_t Symtab::GetNumSymbols() const {
78   std::lock_guard<std::recursive_mutex> guard(m_mutex);
79   return m_symbols.size();
80 }
81 
82 void Symtab::SectionFileAddressesChanged() {
83   m_file_addr_to_index.Clear();
84   m_file_addr_to_index_computed = false;
85 }
86 
87 void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
88                   Mangled::NamePreference name_preference) {
89   std::lock_guard<std::recursive_mutex> guard(m_mutex);
90 
91   //    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
92   s->Indent();
93   const FileSpec &file_spec = m_objfile->GetFileSpec();
94   const char *object_name = nullptr;
95   if (m_objfile->GetModule())
96     object_name = m_objfile->GetModule()->GetObjectName().GetCString();
97 
98   if (file_spec)
99     s->Printf("Symtab, file = %s%s%s%s, num_symbols = %" PRIu64,
100               file_spec.GetPath().c_str(), object_name ? "(" : "",
101               object_name ? object_name : "", object_name ? ")" : "",
102               (uint64_t)m_symbols.size());
103   else
104     s->Printf("Symtab, num_symbols = %" PRIu64 "", (uint64_t)m_symbols.size());
105 
106   if (!m_symbols.empty()) {
107     switch (sort_order) {
108     case eSortOrderNone: {
109       s->PutCString(":\n");
110       DumpSymbolHeader(s);
111       const_iterator begin = m_symbols.begin();
112       const_iterator end = m_symbols.end();
113       for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
114         s->Indent();
115         pos->Dump(s, target, std::distance(begin, pos), name_preference);
116       }
117     }
118     break;
119 
120     case eSortOrderByName: {
121       // Although we maintain a lookup by exact name map, the table isn't
122       // sorted by name. So we must make the ordered symbol list up ourselves.
123       s->PutCString(" (sorted by name):\n");
124       DumpSymbolHeader(s);
125 
126       std::multimap<llvm::StringRef, const Symbol *> name_map;
127       for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
128            pos != end; ++pos) {
129         const char *name = pos->GetName().AsCString();
130         if (name && name[0])
131           name_map.insert(std::make_pair(name, &(*pos)));
132       }
133 
134       for (const auto &name_to_symbol : name_map) {
135         const Symbol *symbol = name_to_symbol.second;
136         s->Indent();
137         symbol->Dump(s, target, symbol - &m_symbols[0], name_preference);
138       }
139     } break;
140 
141     case eSortOrderByAddress:
142       s->PutCString(" (sorted by address):\n");
143       DumpSymbolHeader(s);
144       if (!m_file_addr_to_index_computed)
145         InitAddressIndexes();
146       const size_t num_entries = m_file_addr_to_index.GetSize();
147       for (size_t i = 0; i < num_entries; ++i) {
148         s->Indent();
149         const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data;
150         m_symbols[symbol_idx].Dump(s, target, symbol_idx, name_preference);
151       }
152       break;
153     }
154   } else {
155     s->PutCString("\n");
156   }
157 }
158 
159 void Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t> &indexes,
160                   Mangled::NamePreference name_preference) const {
161   std::lock_guard<std::recursive_mutex> guard(m_mutex);
162 
163   const size_t num_symbols = GetNumSymbols();
164   // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
165   s->Indent();
166   s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n",
167             (uint64_t)indexes.size(), (uint64_t)m_symbols.size());
168   s->IndentMore();
169 
170   if (!indexes.empty()) {
171     std::vector<uint32_t>::const_iterator pos;
172     std::vector<uint32_t>::const_iterator end = indexes.end();
173     DumpSymbolHeader(s);
174     for (pos = indexes.begin(); pos != end; ++pos) {
175       size_t idx = *pos;
176       if (idx < num_symbols) {
177         s->Indent();
178         m_symbols[idx].Dump(s, target, idx, name_preference);
179       }
180     }
181   }
182   s->IndentLess();
183 }
184 
185 void Symtab::DumpSymbolHeader(Stream *s) {
186   s->Indent("               Debug symbol\n");
187   s->Indent("               |Synthetic symbol\n");
188   s->Indent("               ||Externally Visible\n");
189   s->Indent("               |||\n");
190   s->Indent("Index   UserID DSX Type            File Address/Value Load "
191             "Address       Size               Flags      Name\n");
192   s->Indent("------- ------ --- --------------- ------------------ "
193             "------------------ ------------------ ---------- "
194             "----------------------------------\n");
195 }
196 
197 static int CompareSymbolID(const void *key, const void *p) {
198   const user_id_t match_uid = *(const user_id_t *)key;
199   const user_id_t symbol_uid = ((const Symbol *)p)->GetID();
200   if (match_uid < symbol_uid)
201     return -1;
202   if (match_uid > symbol_uid)
203     return 1;
204   return 0;
205 }
206 
207 Symbol *Symtab::FindSymbolByID(lldb::user_id_t symbol_uid) const {
208   std::lock_guard<std::recursive_mutex> guard(m_mutex);
209 
210   Symbol *symbol =
211       (Symbol *)::bsearch(&symbol_uid, &m_symbols[0], m_symbols.size(),
212                           sizeof(m_symbols[0]), CompareSymbolID);
213   return symbol;
214 }
215 
216 Symbol *Symtab::SymbolAtIndex(size_t idx) {
217   // Clients should grab the mutex from this symbol table and lock it manually
218   // when calling this function to avoid performance issues.
219   if (idx < m_symbols.size())
220     return &m_symbols[idx];
221   return nullptr;
222 }
223 
224 const Symbol *Symtab::SymbolAtIndex(size_t idx) const {
225   // Clients should grab the mutex from this symbol table and lock it manually
226   // when calling this function to avoid performance issues.
227   if (idx < m_symbols.size())
228     return &m_symbols[idx];
229   return nullptr;
230 }
231 
232 static bool lldb_skip_name(llvm::StringRef mangled,
233                            Mangled::ManglingScheme scheme) {
234   switch (scheme) {
235   case Mangled::eManglingSchemeItanium: {
236     if (mangled.size() < 3 || !mangled.startswith("_Z"))
237       return true;
238 
239     // Avoid the following types of symbols in the index.
240     switch (mangled[2]) {
241     case 'G': // guard variables
242     case 'T': // virtual tables, VTT structures, typeinfo structures + names
243     case 'Z': // named local entities (if we eventually handle
244               // eSymbolTypeData, we will want this back)
245       return true;
246 
247     default:
248       break;
249     }
250 
251     // Include this name in the index.
252     return false;
253   }
254 
255   // No filters for this scheme yet. Include all names in indexing.
256   case Mangled::eManglingSchemeMSVC:
257   case Mangled::eManglingSchemeRustV0:
258   case Mangled::eManglingSchemeD:
259     return false;
260 
261   // Don't try and demangle things we can't categorize.
262   case Mangled::eManglingSchemeNone:
263     return true;
264   }
265   llvm_unreachable("unknown scheme!");
266 }
267 
268 void Symtab::InitNameIndexes() {
269   // Protected function, no need to lock mutex...
270   if (!m_name_indexes_computed) {
271     m_name_indexes_computed = true;
272     ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabIndexTime());
273     LLDB_SCOPED_TIMER();
274 
275     // Collect all loaded language plugins.
276     std::vector<Language *> languages;
277     Language::ForEach([&languages](Language *l) {
278       languages.push_back(l);
279       return true;
280     });
281 
282     auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
283     auto &basename_to_index =
284         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
285     auto &method_to_index =
286         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
287     auto &selector_to_index =
288         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeSelector);
289     // Create the name index vector to be able to quickly search by name
290     const size_t num_symbols = m_symbols.size();
291     name_to_index.Reserve(num_symbols);
292 
293     // The "const char *" in "class_contexts" and backlog::value_type::second
294     // must come from a ConstString::GetCString()
295     std::set<const char *> class_contexts;
296     std::vector<std::pair<NameToIndexMap::Entry, const char *>> backlog;
297     backlog.reserve(num_symbols / 2);
298 
299     // Instantiation of the demangler is expensive, so better use a single one
300     // for all entries during batch processing.
301     RichManglingContext rmc;
302     for (uint32_t value = 0; value < num_symbols; ++value) {
303       Symbol *symbol = &m_symbols[value];
304 
305       // Don't let trampolines get into the lookup by name map If we ever need
306       // the trampoline symbols to be searchable by name we can remove this and
307       // then possibly add a new bool to any of the Symtab functions that
308       // lookup symbols by name to indicate if they want trampolines. We also
309       // don't want any synthetic symbols with auto generated names in the
310       // name lookups.
311       if (symbol->IsTrampoline() || symbol->IsSyntheticWithAutoGeneratedName())
312         continue;
313 
314       // If the symbol's name string matched a Mangled::ManglingScheme, it is
315       // stored in the mangled field.
316       Mangled &mangled = symbol->GetMangled();
317       if (ConstString name = mangled.GetMangledName()) {
318         name_to_index.Append(name, value);
319 
320         if (symbol->ContainsLinkerAnnotations()) {
321           // If the symbol has linker annotations, also add the version without
322           // the annotations.
323           ConstString stripped = ConstString(
324               m_objfile->StripLinkerSymbolAnnotations(name.GetStringRef()));
325           name_to_index.Append(stripped, value);
326         }
327 
328         const SymbolType type = symbol->GetType();
329         if (type == eSymbolTypeCode || type == eSymbolTypeResolver) {
330           if (mangled.GetRichManglingInfo(rmc, lldb_skip_name)) {
331             RegisterMangledNameEntry(value, class_contexts, backlog, rmc);
332             continue;
333           }
334         }
335       }
336 
337       // Symbol name strings that didn't match a Mangled::ManglingScheme, are
338       // stored in the demangled field.
339       if (ConstString name = mangled.GetDemangledName()) {
340         name_to_index.Append(name, value);
341 
342         if (symbol->ContainsLinkerAnnotations()) {
343           // If the symbol has linker annotations, also add the version without
344           // the annotations.
345           name = ConstString(
346               m_objfile->StripLinkerSymbolAnnotations(name.GetStringRef()));
347           name_to_index.Append(name, value);
348         }
349 
350         // If the demangled name turns out to be an ObjC name, and is a category
351         // name, add the version without categories to the index too.
352         for (Language *lang : languages) {
353           for (auto variant : lang->GetMethodNameVariants(name)) {
354             if (variant.GetType() & lldb::eFunctionNameTypeSelector)
355               selector_to_index.Append(variant.GetName(), value);
356             else if (variant.GetType() & lldb::eFunctionNameTypeFull)
357               name_to_index.Append(variant.GetName(), value);
358             else if (variant.GetType() & lldb::eFunctionNameTypeMethod)
359               method_to_index.Append(variant.GetName(), value);
360             else if (variant.GetType() & lldb::eFunctionNameTypeBase)
361               basename_to_index.Append(variant.GetName(), value);
362           }
363         }
364       }
365     }
366 
367     for (const auto &record : backlog) {
368       RegisterBacklogEntry(record.first, record.second, class_contexts);
369     }
370 
371     name_to_index.Sort();
372     name_to_index.SizeToFit();
373     selector_to_index.Sort();
374     selector_to_index.SizeToFit();
375     basename_to_index.Sort();
376     basename_to_index.SizeToFit();
377     method_to_index.Sort();
378     method_to_index.SizeToFit();
379   }
380 }
381 
382 void Symtab::RegisterMangledNameEntry(
383     uint32_t value, std::set<const char *> &class_contexts,
384     std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
385     RichManglingContext &rmc) {
386   // Only register functions that have a base name.
387   llvm::StringRef base_name = rmc.ParseFunctionBaseName();
388   if (base_name.empty())
389     return;
390 
391   // The base name will be our entry's name.
392   NameToIndexMap::Entry entry(ConstString(base_name), value);
393   llvm::StringRef decl_context = rmc.ParseFunctionDeclContextName();
394 
395   // Register functions with no context.
396   if (decl_context.empty()) {
397     // This has to be a basename
398     auto &basename_to_index =
399         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
400     basename_to_index.Append(entry);
401     // If there is no context (no namespaces or class scopes that come before
402     // the function name) then this also could be a fullname.
403     auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
404     name_to_index.Append(entry);
405     return;
406   }
407 
408   // Make sure we have a pool-string pointer and see if we already know the
409   // context name.
410   const char *decl_context_ccstr = ConstString(decl_context).GetCString();
411   auto it = class_contexts.find(decl_context_ccstr);
412 
413   auto &method_to_index =
414       GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
415   // Register constructors and destructors. They are methods and create
416   // declaration contexts.
417   if (rmc.IsCtorOrDtor()) {
418     method_to_index.Append(entry);
419     if (it == class_contexts.end())
420       class_contexts.insert(it, decl_context_ccstr);
421     return;
422   }
423 
424   // Register regular methods with a known declaration context.
425   if (it != class_contexts.end()) {
426     method_to_index.Append(entry);
427     return;
428   }
429 
430   // Regular methods in unknown declaration contexts are put to the backlog. We
431   // will revisit them once we processed all remaining symbols.
432   backlog.push_back(std::make_pair(entry, decl_context_ccstr));
433 }
434 
435 void Symtab::RegisterBacklogEntry(
436     const NameToIndexMap::Entry &entry, const char *decl_context,
437     const std::set<const char *> &class_contexts) {
438   auto &method_to_index =
439       GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
440   auto it = class_contexts.find(decl_context);
441   if (it != class_contexts.end()) {
442     method_to_index.Append(entry);
443   } else {
444     // If we got here, we have something that had a context (was inside
445     // a namespace or class) yet we don't know the entry
446     method_to_index.Append(entry);
447     auto &basename_to_index =
448         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
449     basename_to_index.Append(entry);
450   }
451 }
452 
453 void Symtab::PreloadSymbols() {
454   std::lock_guard<std::recursive_mutex> guard(m_mutex);
455   InitNameIndexes();
456 }
457 
458 void Symtab::AppendSymbolNamesToMap(const IndexCollection &indexes,
459                                     bool add_demangled, bool add_mangled,
460                                     NameToIndexMap &name_to_index_map) const {
461   LLDB_SCOPED_TIMER();
462   if (add_demangled || add_mangled) {
463     std::lock_guard<std::recursive_mutex> guard(m_mutex);
464 
465     // Create the name index vector to be able to quickly search by name
466     const size_t num_indexes = indexes.size();
467     for (size_t i = 0; i < num_indexes; ++i) {
468       uint32_t value = indexes[i];
469       assert(i < m_symbols.size());
470       const Symbol *symbol = &m_symbols[value];
471 
472       const Mangled &mangled = symbol->GetMangled();
473       if (add_demangled) {
474         if (ConstString name = mangled.GetDemangledName())
475           name_to_index_map.Append(name, value);
476       }
477 
478       if (add_mangled) {
479         if (ConstString name = mangled.GetMangledName())
480           name_to_index_map.Append(name, value);
481       }
482     }
483   }
484 }
485 
486 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
487                                              std::vector<uint32_t> &indexes,
488                                              uint32_t start_idx,
489                                              uint32_t end_index) const {
490   std::lock_guard<std::recursive_mutex> guard(m_mutex);
491 
492   uint32_t prev_size = indexes.size();
493 
494   const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
495 
496   for (uint32_t i = start_idx; i < count; ++i) {
497     if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
498       indexes.push_back(i);
499   }
500 
501   return indexes.size() - prev_size;
502 }
503 
504 uint32_t Symtab::AppendSymbolIndexesWithTypeAndFlagsValue(
505     SymbolType symbol_type, uint32_t flags_value,
506     std::vector<uint32_t> &indexes, uint32_t start_idx,
507     uint32_t end_index) const {
508   std::lock_guard<std::recursive_mutex> guard(m_mutex);
509 
510   uint32_t prev_size = indexes.size();
511 
512   const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
513 
514   for (uint32_t i = start_idx; i < count; ++i) {
515     if ((symbol_type == eSymbolTypeAny ||
516          m_symbols[i].GetType() == symbol_type) &&
517         m_symbols[i].GetFlags() == flags_value)
518       indexes.push_back(i);
519   }
520 
521   return indexes.size() - prev_size;
522 }
523 
524 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
525                                              Debug symbol_debug_type,
526                                              Visibility symbol_visibility,
527                                              std::vector<uint32_t> &indexes,
528                                              uint32_t start_idx,
529                                              uint32_t end_index) const {
530   std::lock_guard<std::recursive_mutex> guard(m_mutex);
531 
532   uint32_t prev_size = indexes.size();
533 
534   const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
535 
536   for (uint32_t i = start_idx; i < count; ++i) {
537     if (symbol_type == eSymbolTypeAny ||
538         m_symbols[i].GetType() == symbol_type) {
539       if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
540         indexes.push_back(i);
541     }
542   }
543 
544   return indexes.size() - prev_size;
545 }
546 
547 uint32_t Symtab::GetIndexForSymbol(const Symbol *symbol) const {
548   if (!m_symbols.empty()) {
549     const Symbol *first_symbol = &m_symbols[0];
550     if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
551       return symbol - first_symbol;
552   }
553   return UINT32_MAX;
554 }
555 
556 struct SymbolSortInfo {
557   const bool sort_by_load_addr;
558   const Symbol *symbols;
559 };
560 
561 namespace {
562 struct SymbolIndexComparator {
563   const std::vector<Symbol> &symbols;
564   std::vector<lldb::addr_t> &addr_cache;
565 
566   // Getting from the symbol to the Address to the File Address involves some
567   // work. Since there are potentially many symbols here, and we're using this
568   // for sorting so we're going to be computing the address many times, cache
569   // that in addr_cache. The array passed in has to be the same size as the
570   // symbols array passed into the member variable symbols, and should be
571   // initialized with LLDB_INVALID_ADDRESS.
572   // NOTE: You have to make addr_cache externally and pass it in because
573   // std::stable_sort
574   // makes copies of the comparator it is initially passed in, and you end up
575   // spending huge amounts of time copying this array...
576 
577   SymbolIndexComparator(const std::vector<Symbol> &s,
578                         std::vector<lldb::addr_t> &a)
579       : symbols(s), addr_cache(a) {
580     assert(symbols.size() == addr_cache.size());
581   }
582   bool operator()(uint32_t index_a, uint32_t index_b) {
583     addr_t value_a = addr_cache[index_a];
584     if (value_a == LLDB_INVALID_ADDRESS) {
585       value_a = symbols[index_a].GetAddressRef().GetFileAddress();
586       addr_cache[index_a] = value_a;
587     }
588 
589     addr_t value_b = addr_cache[index_b];
590     if (value_b == LLDB_INVALID_ADDRESS) {
591       value_b = symbols[index_b].GetAddressRef().GetFileAddress();
592       addr_cache[index_b] = value_b;
593     }
594 
595     if (value_a == value_b) {
596       // The if the values are equal, use the original symbol user ID
597       lldb::user_id_t uid_a = symbols[index_a].GetID();
598       lldb::user_id_t uid_b = symbols[index_b].GetID();
599       if (uid_a < uid_b)
600         return true;
601       if (uid_a > uid_b)
602         return false;
603       return false;
604     } else if (value_a < value_b)
605       return true;
606 
607     return false;
608   }
609 };
610 }
611 
612 void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
613                                       bool remove_duplicates) const {
614   std::lock_guard<std::recursive_mutex> guard(m_mutex);
615   LLDB_SCOPED_TIMER();
616   // No need to sort if we have zero or one items...
617   if (indexes.size() <= 1)
618     return;
619 
620   // Sort the indexes in place using std::stable_sort.
621   // NOTE: The use of std::stable_sort instead of llvm::sort here is strictly
622   // for performance, not correctness.  The indexes vector tends to be "close"
623   // to sorted, which the stable sort handles better.
624 
625   std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
626 
627   SymbolIndexComparator comparator(m_symbols, addr_cache);
628   std::stable_sort(indexes.begin(), indexes.end(), comparator);
629 
630   // Remove any duplicates if requested
631   if (remove_duplicates) {
632     auto last = std::unique(indexes.begin(), indexes.end());
633     indexes.erase(last, indexes.end());
634   }
635 }
636 
637 uint32_t Symtab::GetNameIndexes(ConstString symbol_name,
638                                 std::vector<uint32_t> &indexes) {
639   auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
640   const uint32_t count = name_to_index.GetValues(symbol_name, indexes);
641   if (count)
642     return count;
643   // Synthetic symbol names are not added to the name indexes, but they start
644   // with a prefix and end with a the symbol UserID. This allows users to find
645   // these symbols without having to add them to the name indexes. These
646   // queries will not happen very often since the names don't mean anything, so
647   // performance is not paramount in this case.
648   llvm::StringRef name = symbol_name.GetStringRef();
649   // String the synthetic prefix if the name starts with it.
650   if (!name.consume_front(Symbol::GetSyntheticSymbolPrefix()))
651     return 0; // Not a synthetic symbol name
652 
653   // Extract the user ID from the symbol name
654   unsigned long long uid = 0;
655   if (getAsUnsignedInteger(name, /*Radix=*/10, uid))
656     return 0; // Failed to extract the user ID as an integer
657   Symbol *symbol = FindSymbolByID(uid);
658   if (symbol == nullptr)
659     return 0;
660   const uint32_t symbol_idx = GetIndexForSymbol(symbol);
661   if (symbol_idx == UINT32_MAX)
662     return 0;
663   indexes.push_back(symbol_idx);
664   return 1;
665 }
666 
667 uint32_t Symtab::AppendSymbolIndexesWithName(ConstString symbol_name,
668                                              std::vector<uint32_t> &indexes) {
669   std::lock_guard<std::recursive_mutex> guard(m_mutex);
670 
671   if (symbol_name) {
672     if (!m_name_indexes_computed)
673       InitNameIndexes();
674 
675     return GetNameIndexes(symbol_name, indexes);
676   }
677   return 0;
678 }
679 
680 uint32_t Symtab::AppendSymbolIndexesWithName(ConstString symbol_name,
681                                              Debug symbol_debug_type,
682                                              Visibility symbol_visibility,
683                                              std::vector<uint32_t> &indexes) {
684   std::lock_guard<std::recursive_mutex> guard(m_mutex);
685 
686   LLDB_SCOPED_TIMER();
687   if (symbol_name) {
688     const size_t old_size = indexes.size();
689     if (!m_name_indexes_computed)
690       InitNameIndexes();
691 
692     std::vector<uint32_t> all_name_indexes;
693     const size_t name_match_count =
694         GetNameIndexes(symbol_name, all_name_indexes);
695     for (size_t i = 0; i < name_match_count; ++i) {
696       if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type,
697                              symbol_visibility))
698         indexes.push_back(all_name_indexes[i]);
699     }
700     return indexes.size() - old_size;
701   }
702   return 0;
703 }
704 
705 uint32_t
706 Symtab::AppendSymbolIndexesWithNameAndType(ConstString symbol_name,
707                                            SymbolType symbol_type,
708                                            std::vector<uint32_t> &indexes) {
709   std::lock_guard<std::recursive_mutex> guard(m_mutex);
710 
711   if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) {
712     std::vector<uint32_t>::iterator pos = indexes.begin();
713     while (pos != indexes.end()) {
714       if (symbol_type == eSymbolTypeAny ||
715           m_symbols[*pos].GetType() == symbol_type)
716         ++pos;
717       else
718         pos = indexes.erase(pos);
719     }
720   }
721   return indexes.size();
722 }
723 
724 uint32_t Symtab::AppendSymbolIndexesWithNameAndType(
725     ConstString symbol_name, 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);
729 
730   if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type,
731                                   symbol_visibility, indexes) > 0) {
732     std::vector<uint32_t>::iterator pos = indexes.begin();
733     while (pos != indexes.end()) {
734       if (symbol_type == eSymbolTypeAny ||
735           m_symbols[*pos].GetType() == symbol_type)
736         ++pos;
737       else
738         pos = indexes.erase(pos);
739     }
740   }
741   return indexes.size();
742 }
743 
744 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
745     const RegularExpression &regexp, SymbolType symbol_type,
746     std::vector<uint32_t> &indexes, Mangled::NamePreference name_preference) {
747   std::lock_guard<std::recursive_mutex> guard(m_mutex);
748 
749   uint32_t prev_size = indexes.size();
750   uint32_t sym_end = m_symbols.size();
751 
752   for (uint32_t i = 0; i < sym_end; i++) {
753     if (symbol_type == eSymbolTypeAny ||
754         m_symbols[i].GetType() == symbol_type) {
755       const char *name =
756           m_symbols[i].GetMangled().GetName(name_preference).AsCString();
757       if (name) {
758         if (regexp.Execute(name))
759           indexes.push_back(i);
760       }
761     }
762   }
763   return indexes.size() - prev_size;
764 }
765 
766 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
767     const RegularExpression &regexp, SymbolType symbol_type,
768     Debug symbol_debug_type, Visibility symbol_visibility,
769     std::vector<uint32_t> &indexes, Mangled::NamePreference name_preference) {
770   std::lock_guard<std::recursive_mutex> guard(m_mutex);
771 
772   uint32_t prev_size = indexes.size();
773   uint32_t sym_end = m_symbols.size();
774 
775   for (uint32_t i = 0; i < sym_end; i++) {
776     if (symbol_type == eSymbolTypeAny ||
777         m_symbols[i].GetType() == symbol_type) {
778       if (!CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
779         continue;
780 
781       const char *name =
782           m_symbols[i].GetMangled().GetName(name_preference).AsCString();
783       if (name) {
784         if (regexp.Execute(name))
785           indexes.push_back(i);
786       }
787     }
788   }
789   return indexes.size() - prev_size;
790 }
791 
792 Symbol *Symtab::FindSymbolWithType(SymbolType symbol_type,
793                                    Debug symbol_debug_type,
794                                    Visibility symbol_visibility,
795                                    uint32_t &start_idx) {
796   std::lock_guard<std::recursive_mutex> guard(m_mutex);
797 
798   const size_t count = m_symbols.size();
799   for (size_t idx = start_idx; idx < count; ++idx) {
800     if (symbol_type == eSymbolTypeAny ||
801         m_symbols[idx].GetType() == symbol_type) {
802       if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) {
803         start_idx = idx;
804         return &m_symbols[idx];
805       }
806     }
807   }
808   return nullptr;
809 }
810 
811 void
812 Symtab::FindAllSymbolsWithNameAndType(ConstString name,
813                                       SymbolType symbol_type,
814                                       std::vector<uint32_t> &symbol_indexes) {
815   std::lock_guard<std::recursive_mutex> guard(m_mutex);
816 
817   // Initialize all of the lookup by name indexes before converting NAME to a
818   // uniqued string NAME_STR below.
819   if (!m_name_indexes_computed)
820     InitNameIndexes();
821 
822   if (name) {
823     // The string table did have a string that matched, but we need to check
824     // the symbols and match the symbol_type if any was given.
825     AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes);
826   }
827 }
828 
829 void Symtab::FindAllSymbolsWithNameAndType(
830     ConstString name, SymbolType symbol_type, Debug symbol_debug_type,
831     Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) {
832   std::lock_guard<std::recursive_mutex> guard(m_mutex);
833 
834   LLDB_SCOPED_TIMER();
835   // Initialize all of the lookup by name indexes before converting NAME to a
836   // uniqued string NAME_STR below.
837   if (!m_name_indexes_computed)
838     InitNameIndexes();
839 
840   if (name) {
841     // The string table did have a string that matched, but we need to check
842     // the symbols and match the symbol_type if any was given.
843     AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
844                                        symbol_visibility, symbol_indexes);
845   }
846 }
847 
848 void Symtab::FindAllSymbolsMatchingRexExAndType(
849     const RegularExpression &regex, SymbolType symbol_type,
850     Debug symbol_debug_type, Visibility symbol_visibility,
851     std::vector<uint32_t> &symbol_indexes,
852     Mangled::NamePreference name_preference) {
853   std::lock_guard<std::recursive_mutex> guard(m_mutex);
854 
855   AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type,
856                                           symbol_visibility, symbol_indexes,
857                                           name_preference);
858 }
859 
860 Symbol *Symtab::FindFirstSymbolWithNameAndType(ConstString name,
861                                                SymbolType symbol_type,
862                                                Debug symbol_debug_type,
863                                                Visibility symbol_visibility) {
864   std::lock_guard<std::recursive_mutex> guard(m_mutex);
865   LLDB_SCOPED_TIMER();
866   if (!m_name_indexes_computed)
867     InitNameIndexes();
868 
869   if (name) {
870     std::vector<uint32_t> matching_indexes;
871     // The string table did have a string that matched, but we need to check
872     // the symbols and match the symbol_type if any was given.
873     if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
874                                            symbol_visibility,
875                                            matching_indexes)) {
876       std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
877       for (pos = matching_indexes.begin(); pos != end; ++pos) {
878         Symbol *symbol = SymbolAtIndex(*pos);
879 
880         if (symbol->Compare(name, symbol_type))
881           return symbol;
882       }
883     }
884   }
885   return nullptr;
886 }
887 
888 typedef struct {
889   const Symtab *symtab;
890   const addr_t file_addr;
891   Symbol *match_symbol;
892   const uint32_t *match_index_ptr;
893   addr_t match_offset;
894 } SymbolSearchInfo;
895 
896 // Add all the section file start address & size to the RangeVector, recusively
897 // adding any children sections.
898 static void AddSectionsToRangeMap(SectionList *sectlist,
899                                   RangeVector<addr_t, addr_t> &section_ranges) {
900   const int num_sections = sectlist->GetNumSections(0);
901   for (int i = 0; i < num_sections; i++) {
902     SectionSP sect_sp = sectlist->GetSectionAtIndex(i);
903     if (sect_sp) {
904       SectionList &child_sectlist = sect_sp->GetChildren();
905 
906       // If this section has children, add the children to the RangeVector.
907       // Else add this section to the RangeVector.
908       if (child_sectlist.GetNumSections(0) > 0) {
909         AddSectionsToRangeMap(&child_sectlist, section_ranges);
910       } else {
911         size_t size = sect_sp->GetByteSize();
912         if (size > 0) {
913           addr_t base_addr = sect_sp->GetFileAddress();
914           RangeVector<addr_t, addr_t>::Entry entry;
915           entry.SetRangeBase(base_addr);
916           entry.SetByteSize(size);
917           section_ranges.Append(entry);
918         }
919       }
920     }
921   }
922 }
923 
924 void Symtab::InitAddressIndexes() {
925   // Protected function, no need to lock mutex...
926   if (!m_file_addr_to_index_computed && !m_symbols.empty()) {
927     m_file_addr_to_index_computed = true;
928 
929     FileRangeToIndexMap::Entry entry;
930     const_iterator begin = m_symbols.begin();
931     const_iterator end = m_symbols.end();
932     for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
933       if (pos->ValueIsAddress()) {
934         entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
935         entry.SetByteSize(pos->GetByteSize());
936         entry.data = std::distance(begin, pos);
937         m_file_addr_to_index.Append(entry);
938       }
939     }
940     const size_t num_entries = m_file_addr_to_index.GetSize();
941     if (num_entries > 0) {
942       m_file_addr_to_index.Sort();
943 
944       // Create a RangeVector with the start & size of all the sections for
945       // this objfile.  We'll need to check this for any FileRangeToIndexMap
946       // entries with an uninitialized size, which could potentially be a large
947       // number so reconstituting the weak pointer is busywork when it is
948       // invariant information.
949       SectionList *sectlist = m_objfile->GetSectionList();
950       RangeVector<addr_t, addr_t> section_ranges;
951       if (sectlist) {
952         AddSectionsToRangeMap(sectlist, section_ranges);
953         section_ranges.Sort();
954       }
955 
956       // Iterate through the FileRangeToIndexMap and fill in the size for any
957       // entries that didn't already have a size from the Symbol (e.g. if we
958       // have a plain linker symbol with an address only, instead of debug info
959       // where we get an address and a size and a type, etc.)
960       for (size_t i = 0; i < num_entries; i++) {
961         FileRangeToIndexMap::Entry *entry =
962             m_file_addr_to_index.GetMutableEntryAtIndex(i);
963         if (entry->GetByteSize() == 0) {
964           addr_t curr_base_addr = entry->GetRangeBase();
965           const RangeVector<addr_t, addr_t>::Entry *containing_section =
966               section_ranges.FindEntryThatContains(curr_base_addr);
967 
968           // Use the end of the section as the default max size of the symbol
969           addr_t sym_size = 0;
970           if (containing_section) {
971             sym_size =
972                 containing_section->GetByteSize() -
973                 (entry->GetRangeBase() - containing_section->GetRangeBase());
974           }
975 
976           for (size_t j = i; j < num_entries; j++) {
977             FileRangeToIndexMap::Entry *next_entry =
978                 m_file_addr_to_index.GetMutableEntryAtIndex(j);
979             addr_t next_base_addr = next_entry->GetRangeBase();
980             if (next_base_addr > curr_base_addr) {
981               addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
982 
983               // Take the difference between this symbol and the next one as
984               // its size, if it is less than the size of the section.
985               if (sym_size == 0 || size_to_next_symbol < sym_size) {
986                 sym_size = size_to_next_symbol;
987               }
988               break;
989             }
990           }
991 
992           if (sym_size > 0) {
993             entry->SetByteSize(sym_size);
994             Symbol &symbol = m_symbols[entry->data];
995             symbol.SetByteSize(sym_size);
996             symbol.SetSizeIsSynthesized(true);
997           }
998         }
999       }
1000 
1001       // Sort again in case the range size changes the ordering
1002       m_file_addr_to_index.Sort();
1003     }
1004   }
1005 }
1006 
1007 void Symtab::Finalize() {
1008   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1009   // Calculate the size of symbols inside InitAddressIndexes.
1010   InitAddressIndexes();
1011   // Shrink to fit the symbols so we don't waste memory
1012   if (m_symbols.capacity() > m_symbols.size()) {
1013     collection new_symbols(m_symbols.begin(), m_symbols.end());
1014     m_symbols.swap(new_symbols);
1015   }
1016   SaveToCache();
1017 }
1018 
1019 Symbol *Symtab::FindSymbolAtFileAddress(addr_t file_addr) {
1020   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1021   if (!m_file_addr_to_index_computed)
1022     InitAddressIndexes();
1023 
1024   const FileRangeToIndexMap::Entry *entry =
1025       m_file_addr_to_index.FindEntryStartsAt(file_addr);
1026   if (entry) {
1027     Symbol *symbol = SymbolAtIndex(entry->data);
1028     if (symbol->GetFileAddress() == file_addr)
1029       return symbol;
1030   }
1031   return nullptr;
1032 }
1033 
1034 Symbol *Symtab::FindSymbolContainingFileAddress(addr_t file_addr) {
1035   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1036 
1037   if (!m_file_addr_to_index_computed)
1038     InitAddressIndexes();
1039 
1040   const FileRangeToIndexMap::Entry *entry =
1041       m_file_addr_to_index.FindEntryThatContains(file_addr);
1042   if (entry) {
1043     Symbol *symbol = SymbolAtIndex(entry->data);
1044     if (symbol->ContainsFileAddress(file_addr))
1045       return symbol;
1046   }
1047   return nullptr;
1048 }
1049 
1050 void Symtab::ForEachSymbolContainingFileAddress(
1051     addr_t file_addr, std::function<bool(Symbol *)> const &callback) {
1052   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1053 
1054   if (!m_file_addr_to_index_computed)
1055     InitAddressIndexes();
1056 
1057   std::vector<uint32_t> all_addr_indexes;
1058 
1059   // Get all symbols with file_addr
1060   const size_t addr_match_count =
1061       m_file_addr_to_index.FindEntryIndexesThatContain(file_addr,
1062                                                        all_addr_indexes);
1063 
1064   for (size_t i = 0; i < addr_match_count; ++i) {
1065     Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]);
1066     if (symbol->ContainsFileAddress(file_addr)) {
1067       if (!callback(symbol))
1068         break;
1069     }
1070   }
1071 }
1072 
1073 void Symtab::SymbolIndicesToSymbolContextList(
1074     std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) {
1075   // No need to protect this call using m_mutex all other method calls are
1076   // already thread safe.
1077 
1078   const bool merge_symbol_into_function = true;
1079   size_t num_indices = symbol_indexes.size();
1080   if (num_indices > 0) {
1081     SymbolContext sc;
1082     sc.module_sp = m_objfile->GetModule();
1083     for (size_t i = 0; i < num_indices; i++) {
1084       sc.symbol = SymbolAtIndex(symbol_indexes[i]);
1085       if (sc.symbol)
1086         sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1087     }
1088   }
1089 }
1090 
1091 void Symtab::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
1092                                  SymbolContextList &sc_list) {
1093   std::vector<uint32_t> symbol_indexes;
1094 
1095   // eFunctionNameTypeAuto should be pre-resolved by a call to
1096   // Module::LookupInfo::LookupInfo()
1097   assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1098 
1099   if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1100     std::vector<uint32_t> temp_symbol_indexes;
1101     FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes);
1102 
1103     unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1104     if (temp_symbol_indexes_size > 0) {
1105       std::lock_guard<std::recursive_mutex> guard(m_mutex);
1106       for (unsigned i = 0; i < temp_symbol_indexes_size; i++) {
1107         SymbolContext sym_ctx;
1108         sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]);
1109         if (sym_ctx.symbol) {
1110           switch (sym_ctx.symbol->GetType()) {
1111           case eSymbolTypeCode:
1112           case eSymbolTypeResolver:
1113           case eSymbolTypeReExported:
1114           case eSymbolTypeAbsolute:
1115             symbol_indexes.push_back(temp_symbol_indexes[i]);
1116             break;
1117           default:
1118             break;
1119           }
1120         }
1121       }
1122     }
1123   }
1124 
1125   if (!m_name_indexes_computed)
1126     InitNameIndexes();
1127 
1128   for (lldb::FunctionNameType type :
1129        {lldb::eFunctionNameTypeBase, lldb::eFunctionNameTypeMethod,
1130         lldb::eFunctionNameTypeSelector}) {
1131     if (name_type_mask & type) {
1132       auto map = GetNameToSymbolIndexMap(type);
1133 
1134       const UniqueCStringMap<uint32_t>::Entry *match;
1135       for (match = map.FindFirstValueForName(name); match != nullptr;
1136            match = map.FindNextValueForName(match)) {
1137         symbol_indexes.push_back(match->value);
1138       }
1139     }
1140   }
1141 
1142   if (!symbol_indexes.empty()) {
1143     llvm::sort(symbol_indexes);
1144     symbol_indexes.erase(
1145         std::unique(symbol_indexes.begin(), symbol_indexes.end()),
1146         symbol_indexes.end());
1147     SymbolIndicesToSymbolContextList(symbol_indexes, sc_list);
1148   }
1149 }
1150 
1151 const Symbol *Symtab::GetParent(Symbol *child_symbol) const {
1152   uint32_t child_idx = GetIndexForSymbol(child_symbol);
1153   if (child_idx != UINT32_MAX && child_idx > 0) {
1154     for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) {
1155       const Symbol *symbol = SymbolAtIndex(idx);
1156       const uint32_t sibling_idx = symbol->GetSiblingIndex();
1157       if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)
1158         return symbol;
1159     }
1160   }
1161   return nullptr;
1162 }
1163 
1164 std::string Symtab::GetCacheKey() {
1165   std::string key;
1166   llvm::raw_string_ostream strm(key);
1167   // Symbol table can come from different object files for the same module. A
1168   // module can have one object file as the main executable and might have
1169   // another object file in a separate symbol file.
1170   strm << m_objfile->GetModule()->GetCacheKey() << "-symtab-"
1171       << llvm::format_hex(m_objfile->GetCacheHash(), 10);
1172   return strm.str();
1173 }
1174 
1175 void Symtab::SaveToCache() {
1176   DataFileCache *cache = Module::GetIndexCache();
1177   if (!cache)
1178     return; // Caching is not enabled.
1179   InitNameIndexes(); // Init the name indexes so we can cache them as well.
1180   const auto byte_order = endian::InlHostByteOrder();
1181   DataEncoder file(byte_order, /*addr_size=*/8);
1182   // Encode will return false if the symbol table's object file doesn't have
1183   // anything to make a signature from.
1184   if (Encode(file))
1185     if (cache->SetCachedData(GetCacheKey(), file.GetData()))
1186       SetWasSavedToCache();
1187 }
1188 
1189 constexpr llvm::StringLiteral kIdentifierCStrMap("CMAP");
1190 
1191 static void EncodeCStrMap(DataEncoder &encoder, ConstStringTable &strtab,
1192                           const UniqueCStringMap<uint32_t> &cstr_map) {
1193   encoder.AppendData(kIdentifierCStrMap);
1194   encoder.AppendU32(cstr_map.GetSize());
1195   for (const auto &entry: cstr_map) {
1196     // Make sure there are no empty strings.
1197     assert((bool)entry.cstring);
1198     encoder.AppendU32(strtab.Add(entry.cstring));
1199     encoder.AppendU32(entry.value);
1200   }
1201 }
1202 
1203 bool DecodeCStrMap(const DataExtractor &data, lldb::offset_t *offset_ptr,
1204                    const StringTableReader &strtab,
1205                    UniqueCStringMap<uint32_t> &cstr_map) {
1206   llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1207   if (identifier != kIdentifierCStrMap)
1208     return false;
1209   const uint32_t count = data.GetU32(offset_ptr);
1210   cstr_map.Reserve(count);
1211   for (uint32_t i=0; i<count; ++i)
1212   {
1213     llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));
1214     uint32_t value = data.GetU32(offset_ptr);
1215     // No empty strings in the name indexes in Symtab
1216     if (str.empty())
1217       return false;
1218     cstr_map.Append(ConstString(str), value);
1219   }
1220   // We must sort the UniqueCStringMap after decoding it since it is a vector
1221   // of UniqueCStringMap::Entry objects which contain a ConstString and type T.
1222   // ConstString objects are sorted by "const char *" and then type T and
1223   // the "const char *" are point values that will depend on the order in which
1224   // ConstString objects are created and in which of the 256 string pools they
1225   // are created in. So after we decode all of the entries, we must sort the
1226   // name map to ensure name lookups succeed. If we encode and decode within
1227   // the same process we wouldn't need to sort, so unit testing didn't catch
1228   // this issue when first checked in.
1229   cstr_map.Sort();
1230   return true;
1231 }
1232 
1233 constexpr llvm::StringLiteral kIdentifierSymbolTable("SYMB");
1234 constexpr uint32_t CURRENT_CACHE_VERSION = 1;
1235 
1236 /// The encoding format for the symbol table is as follows:
1237 ///
1238 /// Signature signature;
1239 /// ConstStringTable strtab;
1240 /// Identifier four character code: 'SYMB'
1241 /// uint32_t version;
1242 /// uint32_t num_symbols;
1243 /// Symbol symbols[num_symbols];
1244 /// uint8_t num_cstr_maps;
1245 /// UniqueCStringMap<uint32_t> cstr_maps[num_cstr_maps]
1246 bool Symtab::Encode(DataEncoder &encoder) const {
1247   // Name indexes must be computed before calling this function.
1248   assert(m_name_indexes_computed);
1249 
1250   // Encode the object file's signature
1251   CacheSignature signature(m_objfile);
1252   if (!signature.Encode(encoder))
1253     return false;
1254   ConstStringTable strtab;
1255 
1256   // Encoder the symbol table into a separate encoder first. This allows us
1257   // gather all of the strings we willl need in "strtab" as we will need to
1258   // write the string table out before the symbol table.
1259   DataEncoder symtab_encoder(encoder.GetByteOrder(),
1260                               encoder.GetAddressByteSize());
1261   symtab_encoder.AppendData(kIdentifierSymbolTable);
1262   // Encode the symtab data version.
1263   symtab_encoder.AppendU32(CURRENT_CACHE_VERSION);
1264   // Encode the number of symbols.
1265   symtab_encoder.AppendU32(m_symbols.size());
1266   // Encode the symbol data for all symbols.
1267   for (const auto &symbol: m_symbols)
1268     symbol.Encode(symtab_encoder, strtab);
1269 
1270   // Emit a byte for how many C string maps we emit. We will fix this up after
1271   // we emit the C string maps since we skip emitting C string maps if they are
1272   // empty.
1273   size_t num_cmaps_offset = symtab_encoder.GetByteSize();
1274   uint8_t num_cmaps = 0;
1275   symtab_encoder.AppendU8(0);
1276   for (const auto &pair: m_name_to_symbol_indices) {
1277     if (pair.second.IsEmpty())
1278       continue;
1279     ++num_cmaps;
1280     symtab_encoder.AppendU8(pair.first);
1281     EncodeCStrMap(symtab_encoder, strtab, pair.second);
1282   }
1283   if (num_cmaps > 0)
1284     symtab_encoder.PutU8(num_cmaps_offset, num_cmaps);
1285 
1286   // Now that all strings have been gathered, we will emit the string table.
1287   strtab.Encode(encoder);
1288   // Followed the the symbol table data.
1289   encoder.AppendData(symtab_encoder.GetData());
1290   return true;
1291 }
1292 
1293 bool Symtab::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
1294                     bool &signature_mismatch) {
1295   signature_mismatch = false;
1296   CacheSignature signature;
1297   StringTableReader strtab;
1298   { // Scope for "elapsed" object below so it can measure the time parse.
1299     ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabParseTime());
1300     if (!signature.Decode(data, offset_ptr))
1301       return false;
1302     if (CacheSignature(m_objfile) != signature) {
1303       signature_mismatch = true;
1304       return false;
1305     }
1306     // We now decode the string table for all strings in the data cache file.
1307     if (!strtab.Decode(data, offset_ptr))
1308       return false;
1309 
1310     // And now we can decode the symbol table with string table we just decoded.
1311     llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1312     if (identifier != kIdentifierSymbolTable)
1313       return false;
1314     const uint32_t version = data.GetU32(offset_ptr);
1315     if (version != CURRENT_CACHE_VERSION)
1316       return false;
1317     const uint32_t num_symbols = data.GetU32(offset_ptr);
1318     if (num_symbols == 0)
1319       return true;
1320     m_symbols.resize(num_symbols);
1321     SectionList *sections = m_objfile->GetModule()->GetSectionList();
1322     for (uint32_t i=0; i<num_symbols; ++i) {
1323       if (!m_symbols[i].Decode(data, offset_ptr, sections, strtab))
1324         return false;
1325     }
1326   }
1327 
1328   { // Scope for "elapsed" object below so it can measure the time to index.
1329     ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabIndexTime());
1330     const uint8_t num_cstr_maps = data.GetU8(offset_ptr);
1331     for (uint8_t i=0; i<num_cstr_maps; ++i) {
1332       uint8_t type = data.GetU8(offset_ptr);
1333       UniqueCStringMap<uint32_t> &cstr_map =
1334           GetNameToSymbolIndexMap((lldb::FunctionNameType)type);
1335       if (!DecodeCStrMap(data, offset_ptr, strtab, cstr_map))
1336         return false;
1337     }
1338     m_name_indexes_computed = true;
1339   }
1340   return true;
1341 }
1342 
1343 bool Symtab::LoadFromCache() {
1344   DataFileCache *cache = Module::GetIndexCache();
1345   if (!cache)
1346     return false;
1347 
1348   std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =
1349       cache->GetCachedData(GetCacheKey());
1350   if (!mem_buffer_up)
1351     return false;
1352   DataExtractor data(mem_buffer_up->getBufferStart(),
1353                      mem_buffer_up->getBufferSize(),
1354                      m_objfile->GetByteOrder(),
1355                      m_objfile->GetAddressByteSize());
1356   bool signature_mismatch = false;
1357   lldb::offset_t offset = 0;
1358   const bool result = Decode(data, &offset, signature_mismatch);
1359   if (signature_mismatch)
1360     cache->RemoveCacheFile(GetCacheKey());
1361   if (result)
1362     SetWasLoadedFromCache();
1363   return result;
1364 }
1365