xref: /freebsd-src/contrib/llvm-project/lldb/source/Symbol/Symtab.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
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_name_to_symbol_indices.emplace(std::make_pair(
39       lldb::eFunctionNameTypeNone, UniqueCStringMap<uint32_t>()));
40   m_name_to_symbol_indices.emplace(std::make_pair(
41       lldb::eFunctionNameTypeBase, UniqueCStringMap<uint32_t>()));
42   m_name_to_symbol_indices.emplace(std::make_pair(
43       lldb::eFunctionNameTypeMethod, UniqueCStringMap<uint32_t>()));
44   m_name_to_symbol_indices.emplace(std::make_pair(
45       lldb::eFunctionNameTypeSelector, UniqueCStringMap<uint32_t>()));
46 }
47 
48 Symtab::~Symtab() = default;
49 
50 void Symtab::Reserve(size_t count) {
51   // Clients should grab the mutex from this symbol table and lock it manually
52   // when calling this function to avoid performance issues.
53   m_symbols.reserve(count);
54 }
55 
56 Symbol *Symtab::Resize(size_t count) {
57   // Clients should grab the mutex from this symbol table and lock it manually
58   // when calling this function to avoid performance issues.
59   m_symbols.resize(count);
60   return m_symbols.empty() ? nullptr : &m_symbols[0];
61 }
62 
63 uint32_t Symtab::AddSymbol(const Symbol &symbol) {
64   // Clients should grab the mutex from this symbol table and lock it manually
65   // when calling this function to avoid performance issues.
66   uint32_t symbol_idx = m_symbols.size();
67   auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
68   name_to_index.Clear();
69   m_file_addr_to_index.Clear();
70   m_symbols.push_back(symbol);
71   m_file_addr_to_index_computed = false;
72   m_name_indexes_computed = false;
73   return symbol_idx;
74 }
75 
76 size_t Symtab::GetNumSymbols() const {
77   std::lock_guard<std::recursive_mutex> guard(m_mutex);
78   return m_symbols.size();
79 }
80 
81 void Symtab::SectionFileAddressesChanged() {
82   auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
83   name_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.DemangleWithRichManglingInfo(rmc, lldb_skip_name))
331             RegisterMangledNameEntry(value, class_contexts, backlog, rmc);
332         }
333       }
334 
335       // Symbol name strings that didn't match a Mangled::ManglingScheme, are
336       // stored in the demangled field.
337       if (ConstString name = mangled.GetDemangledName()) {
338         name_to_index.Append(name, value);
339 
340         if (symbol->ContainsLinkerAnnotations()) {
341           // If the symbol has linker annotations, also add the version without
342           // the annotations.
343           name = ConstString(
344               m_objfile->StripLinkerSymbolAnnotations(name.GetStringRef()));
345           name_to_index.Append(name, value);
346         }
347 
348         // If the demangled name turns out to be an ObjC name, and is a category
349         // name, add the version without categories to the index too.
350         for (Language *lang : languages) {
351           for (auto variant : lang->GetMethodNameVariants(name)) {
352             if (variant.GetType() & lldb::eFunctionNameTypeSelector)
353               selector_to_index.Append(variant.GetName(), value);
354             else if (variant.GetType() & lldb::eFunctionNameTypeFull)
355               name_to_index.Append(variant.GetName(), value);
356             else if (variant.GetType() & lldb::eFunctionNameTypeMethod)
357               method_to_index.Append(variant.GetName(), value);
358             else if (variant.GetType() & lldb::eFunctionNameTypeBase)
359               basename_to_index.Append(variant.GetName(), value);
360           }
361         }
362       }
363     }
364 
365     for (const auto &record : backlog) {
366       RegisterBacklogEntry(record.first, record.second, class_contexts);
367     }
368 
369     name_to_index.Sort();
370     name_to_index.SizeToFit();
371     selector_to_index.Sort();
372     selector_to_index.SizeToFit();
373     basename_to_index.Sort();
374     basename_to_index.SizeToFit();
375     method_to_index.Sort();
376     method_to_index.SizeToFit();
377   }
378 }
379 
380 void Symtab::RegisterMangledNameEntry(
381     uint32_t value, std::set<const char *> &class_contexts,
382     std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
383     RichManglingContext &rmc) {
384   // Only register functions that have a base name.
385   rmc.ParseFunctionBaseName();
386   llvm::StringRef base_name = rmc.GetBufferRef();
387   if (base_name.empty())
388     return;
389 
390   // The base name will be our entry's name.
391   NameToIndexMap::Entry entry(ConstString(base_name), value);
392 
393   rmc.ParseFunctionDeclContextName();
394   llvm::StringRef decl_context = rmc.GetBufferRef();
395 
396   // Register functions with no context.
397   if (decl_context.empty()) {
398     // This has to be a basename
399     auto &basename_to_index =
400         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
401     basename_to_index.Append(entry);
402     // If there is no context (no namespaces or class scopes that come before
403     // the function name) then this also could be a fullname.
404     auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
405     name_to_index.Append(entry);
406     return;
407   }
408 
409   // Make sure we have a pool-string pointer and see if we already know the
410   // context name.
411   const char *decl_context_ccstr = ConstString(decl_context).GetCString();
412   auto it = class_contexts.find(decl_context_ccstr);
413 
414   auto &method_to_index =
415       GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
416   // Register constructors and destructors. They are methods and create
417   // declaration contexts.
418   if (rmc.IsCtorOrDtor()) {
419     method_to_index.Append(entry);
420     if (it == class_contexts.end())
421       class_contexts.insert(it, decl_context_ccstr);
422     return;
423   }
424 
425   // Register regular methods with a known declaration context.
426   if (it != class_contexts.end()) {
427     method_to_index.Append(entry);
428     return;
429   }
430 
431   // Regular methods in unknown declaration contexts are put to the backlog. We
432   // will revisit them once we processed all remaining symbols.
433   backlog.push_back(std::make_pair(entry, decl_context_ccstr));
434 }
435 
436 void Symtab::RegisterBacklogEntry(
437     const NameToIndexMap::Entry &entry, const char *decl_context,
438     const std::set<const char *> &class_contexts) {
439   auto &method_to_index =
440       GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
441   auto it = class_contexts.find(decl_context);
442   if (it != class_contexts.end()) {
443     method_to_index.Append(entry);
444   } else {
445     // If we got here, we have something that had a context (was inside
446     // a namespace or class) yet we don't know the entry
447     method_to_index.Append(entry);
448     auto &basename_to_index =
449         GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
450     basename_to_index.Append(entry);
451   }
452 }
453 
454 void Symtab::PreloadSymbols() {
455   std::lock_guard<std::recursive_mutex> guard(m_mutex);
456   InitNameIndexes();
457 }
458 
459 void Symtab::AppendSymbolNamesToMap(const IndexCollection &indexes,
460                                     bool add_demangled, bool add_mangled,
461                                     NameToIndexMap &name_to_index_map) const {
462   LLDB_SCOPED_TIMER();
463   if (add_demangled || add_mangled) {
464     std::lock_guard<std::recursive_mutex> guard(m_mutex);
465 
466     // Create the name index vector to be able to quickly search by name
467     const size_t num_indexes = indexes.size();
468     for (size_t i = 0; i < num_indexes; ++i) {
469       uint32_t value = indexes[i];
470       assert(i < m_symbols.size());
471       const Symbol *symbol = &m_symbols[value];
472 
473       const Mangled &mangled = symbol->GetMangled();
474       if (add_demangled) {
475         if (ConstString name = mangled.GetDemangledName())
476           name_to_index_map.Append(name, value);
477       }
478 
479       if (add_mangled) {
480         if (ConstString name = mangled.GetMangledName())
481           name_to_index_map.Append(name, value);
482       }
483     }
484   }
485 }
486 
487 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
488                                              std::vector<uint32_t> &indexes,
489                                              uint32_t start_idx,
490                                              uint32_t end_index) const {
491   std::lock_guard<std::recursive_mutex> guard(m_mutex);
492 
493   uint32_t prev_size = indexes.size();
494 
495   const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
496 
497   for (uint32_t i = start_idx; i < count; ++i) {
498     if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
499       indexes.push_back(i);
500   }
501 
502   return indexes.size() - prev_size;
503 }
504 
505 uint32_t Symtab::AppendSymbolIndexesWithTypeAndFlagsValue(
506     SymbolType symbol_type, uint32_t flags_value,
507     std::vector<uint32_t> &indexes, uint32_t start_idx,
508     uint32_t end_index) const {
509   std::lock_guard<std::recursive_mutex> guard(m_mutex);
510 
511   uint32_t prev_size = indexes.size();
512 
513   const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
514 
515   for (uint32_t i = start_idx; i < count; ++i) {
516     if ((symbol_type == eSymbolTypeAny ||
517          m_symbols[i].GetType() == symbol_type) &&
518         m_symbols[i].GetFlags() == flags_value)
519       indexes.push_back(i);
520   }
521 
522   return indexes.size() - prev_size;
523 }
524 
525 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type,
526                                              Debug symbol_debug_type,
527                                              Visibility symbol_visibility,
528                                              std::vector<uint32_t> &indexes,
529                                              uint32_t start_idx,
530                                              uint32_t end_index) const {
531   std::lock_guard<std::recursive_mutex> guard(m_mutex);
532 
533   uint32_t prev_size = indexes.size();
534 
535   const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
536 
537   for (uint32_t i = start_idx; i < count; ++i) {
538     if (symbol_type == eSymbolTypeAny ||
539         m_symbols[i].GetType() == symbol_type) {
540       if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
541         indexes.push_back(i);
542     }
543   }
544 
545   return indexes.size() - prev_size;
546 }
547 
548 uint32_t Symtab::GetIndexForSymbol(const Symbol *symbol) const {
549   if (!m_symbols.empty()) {
550     const Symbol *first_symbol = &m_symbols[0];
551     if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
552       return symbol - first_symbol;
553   }
554   return UINT32_MAX;
555 }
556 
557 struct SymbolSortInfo {
558   const bool sort_by_load_addr;
559   const Symbol *symbols;
560 };
561 
562 namespace {
563 struct SymbolIndexComparator {
564   const std::vector<Symbol> &symbols;
565   std::vector<lldb::addr_t> &addr_cache;
566 
567   // Getting from the symbol to the Address to the File Address involves some
568   // work. Since there are potentially many symbols here, and we're using this
569   // for sorting so we're going to be computing the address many times, cache
570   // that in addr_cache. The array passed in has to be the same size as the
571   // symbols array passed into the member variable symbols, and should be
572   // initialized with LLDB_INVALID_ADDRESS.
573   // NOTE: You have to make addr_cache externally and pass it in because
574   // std::stable_sort
575   // makes copies of the comparator it is initially passed in, and you end up
576   // spending huge amounts of time copying this array...
577 
578   SymbolIndexComparator(const std::vector<Symbol> &s,
579                         std::vector<lldb::addr_t> &a)
580       : symbols(s), addr_cache(a) {
581     assert(symbols.size() == addr_cache.size());
582   }
583   bool operator()(uint32_t index_a, uint32_t index_b) {
584     addr_t value_a = addr_cache[index_a];
585     if (value_a == LLDB_INVALID_ADDRESS) {
586       value_a = symbols[index_a].GetAddressRef().GetFileAddress();
587       addr_cache[index_a] = value_a;
588     }
589 
590     addr_t value_b = addr_cache[index_b];
591     if (value_b == LLDB_INVALID_ADDRESS) {
592       value_b = symbols[index_b].GetAddressRef().GetFileAddress();
593       addr_cache[index_b] = value_b;
594     }
595 
596     if (value_a == value_b) {
597       // The if the values are equal, use the original symbol user ID
598       lldb::user_id_t uid_a = symbols[index_a].GetID();
599       lldb::user_id_t uid_b = symbols[index_b].GetID();
600       if (uid_a < uid_b)
601         return true;
602       if (uid_a > uid_b)
603         return false;
604       return false;
605     } else if (value_a < value_b)
606       return true;
607 
608     return false;
609   }
610 };
611 }
612 
613 void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
614                                       bool remove_duplicates) const {
615   std::lock_guard<std::recursive_mutex> guard(m_mutex);
616   LLDB_SCOPED_TIMER();
617   // No need to sort if we have zero or one items...
618   if (indexes.size() <= 1)
619     return;
620 
621   // Sort the indexes in place using std::stable_sort.
622   // NOTE: The use of std::stable_sort instead of llvm::sort here is strictly
623   // for performance, not correctness.  The indexes vector tends to be "close"
624   // to sorted, which the stable sort handles better.
625 
626   std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
627 
628   SymbolIndexComparator comparator(m_symbols, addr_cache);
629   std::stable_sort(indexes.begin(), indexes.end(), comparator);
630 
631   // Remove any duplicates if requested
632   if (remove_duplicates) {
633     auto last = std::unique(indexes.begin(), indexes.end());
634     indexes.erase(last, indexes.end());
635   }
636 }
637 
638 uint32_t Symtab::GetNameIndexes(ConstString symbol_name,
639                                 std::vector<uint32_t> &indexes) {
640   auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
641   const uint32_t count = name_to_index.GetValues(symbol_name, indexes);
642   if (count)
643     return count;
644   // Synthetic symbol names are not added to the name indexes, but they start
645   // with a prefix and end with a the symbol UserID. This allows users to find
646   // these symbols without having to add them to the name indexes. These
647   // queries will not happen very often since the names don't mean anything, so
648   // performance is not paramount in this case.
649   llvm::StringRef name = symbol_name.GetStringRef();
650   // String the synthetic prefix if the name starts with it.
651   if (!name.consume_front(Symbol::GetSyntheticSymbolPrefix()))
652     return 0; // Not a synthetic symbol name
653 
654   // Extract the user ID from the symbol name
655   unsigned long long uid = 0;
656   if (getAsUnsignedInteger(name, /*Radix=*/10, uid))
657     return 0; // Failed to extract the user ID as an integer
658   Symbol *symbol = FindSymbolByID(uid);
659   if (symbol == nullptr)
660     return 0;
661   const uint32_t symbol_idx = GetIndexForSymbol(symbol);
662   if (symbol_idx == UINT32_MAX)
663     return 0;
664   indexes.push_back(symbol_idx);
665   return 1;
666 }
667 
668 uint32_t Symtab::AppendSymbolIndexesWithName(ConstString symbol_name,
669                                              std::vector<uint32_t> &indexes) {
670   std::lock_guard<std::recursive_mutex> guard(m_mutex);
671 
672   if (symbol_name) {
673     if (!m_name_indexes_computed)
674       InitNameIndexes();
675 
676     return GetNameIndexes(symbol_name, indexes);
677   }
678   return 0;
679 }
680 
681 uint32_t Symtab::AppendSymbolIndexesWithName(ConstString symbol_name,
682                                              Debug symbol_debug_type,
683                                              Visibility symbol_visibility,
684                                              std::vector<uint32_t> &indexes) {
685   std::lock_guard<std::recursive_mutex> guard(m_mutex);
686 
687   LLDB_SCOPED_TIMER();
688   if (symbol_name) {
689     const size_t old_size = indexes.size();
690     if (!m_name_indexes_computed)
691       InitNameIndexes();
692 
693     std::vector<uint32_t> all_name_indexes;
694     const size_t name_match_count =
695         GetNameIndexes(symbol_name, all_name_indexes);
696     for (size_t i = 0; i < name_match_count; ++i) {
697       if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type,
698                              symbol_visibility))
699         indexes.push_back(all_name_indexes[i]);
700     }
701     return indexes.size() - old_size;
702   }
703   return 0;
704 }
705 
706 uint32_t
707 Symtab::AppendSymbolIndexesWithNameAndType(ConstString symbol_name,
708                                            SymbolType symbol_type,
709                                            std::vector<uint32_t> &indexes) {
710   std::lock_guard<std::recursive_mutex> guard(m_mutex);
711 
712   if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) {
713     std::vector<uint32_t>::iterator pos = indexes.begin();
714     while (pos != indexes.end()) {
715       if (symbol_type == eSymbolTypeAny ||
716           m_symbols[*pos].GetType() == symbol_type)
717         ++pos;
718       else
719         pos = indexes.erase(pos);
720     }
721   }
722   return indexes.size();
723 }
724 
725 uint32_t Symtab::AppendSymbolIndexesWithNameAndType(
726     ConstString symbol_name, SymbolType symbol_type,
727     Debug symbol_debug_type, Visibility symbol_visibility,
728     std::vector<uint32_t> &indexes) {
729   std::lock_guard<std::recursive_mutex> guard(m_mutex);
730 
731   if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type,
732                                   symbol_visibility, indexes) > 0) {
733     std::vector<uint32_t>::iterator pos = indexes.begin();
734     while (pos != indexes.end()) {
735       if (symbol_type == eSymbolTypeAny ||
736           m_symbols[*pos].GetType() == symbol_type)
737         ++pos;
738       else
739         pos = indexes.erase(pos);
740     }
741   }
742   return indexes.size();
743 }
744 
745 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType(
746     const RegularExpression &regexp, SymbolType symbol_type,
747     std::vector<uint32_t> &indexes) {
748   std::lock_guard<std::recursive_mutex> guard(m_mutex);
749 
750   uint32_t prev_size = indexes.size();
751   uint32_t sym_end = m_symbols.size();
752 
753   for (uint32_t i = 0; i < sym_end; i++) {
754     if (symbol_type == eSymbolTypeAny ||
755         m_symbols[i].GetType() == symbol_type) {
756       const char *name = m_symbols[i].GetName().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) {
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 = m_symbols[i].GetName().AsCString();
782       if (name) {
783         if (regexp.Execute(name))
784           indexes.push_back(i);
785       }
786     }
787   }
788   return indexes.size() - prev_size;
789 }
790 
791 Symbol *Symtab::FindSymbolWithType(SymbolType symbol_type,
792                                    Debug symbol_debug_type,
793                                    Visibility symbol_visibility,
794                                    uint32_t &start_idx) {
795   std::lock_guard<std::recursive_mutex> guard(m_mutex);
796 
797   const size_t count = m_symbols.size();
798   for (size_t idx = start_idx; idx < count; ++idx) {
799     if (symbol_type == eSymbolTypeAny ||
800         m_symbols[idx].GetType() == symbol_type) {
801       if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) {
802         start_idx = idx;
803         return &m_symbols[idx];
804       }
805     }
806   }
807   return nullptr;
808 }
809 
810 void
811 Symtab::FindAllSymbolsWithNameAndType(ConstString name,
812                                       SymbolType symbol_type,
813                                       std::vector<uint32_t> &symbol_indexes) {
814   std::lock_guard<std::recursive_mutex> guard(m_mutex);
815 
816   // Initialize all of the lookup by name indexes before converting NAME to a
817   // uniqued string NAME_STR below.
818   if (!m_name_indexes_computed)
819     InitNameIndexes();
820 
821   if (name) {
822     // The string table did have a string that matched, but we need to check
823     // the symbols and match the symbol_type if any was given.
824     AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes);
825   }
826 }
827 
828 void Symtab::FindAllSymbolsWithNameAndType(
829     ConstString name, SymbolType symbol_type, Debug symbol_debug_type,
830     Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) {
831   std::lock_guard<std::recursive_mutex> guard(m_mutex);
832 
833   LLDB_SCOPED_TIMER();
834   // Initialize all of the lookup by name indexes before converting NAME to a
835   // uniqued string NAME_STR below.
836   if (!m_name_indexes_computed)
837     InitNameIndexes();
838 
839   if (name) {
840     // The string table did have a string that matched, but we need to check
841     // the symbols and match the symbol_type if any was given.
842     AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
843                                        symbol_visibility, symbol_indexes);
844   }
845 }
846 
847 void Symtab::FindAllSymbolsMatchingRexExAndType(
848     const RegularExpression &regex, SymbolType symbol_type,
849     Debug symbol_debug_type, Visibility symbol_visibility,
850     std::vector<uint32_t> &symbol_indexes) {
851   std::lock_guard<std::recursive_mutex> guard(m_mutex);
852 
853   AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type,
854                                           symbol_visibility, symbol_indexes);
855 }
856 
857 Symbol *Symtab::FindFirstSymbolWithNameAndType(ConstString name,
858                                                SymbolType symbol_type,
859                                                Debug symbol_debug_type,
860                                                Visibility symbol_visibility) {
861   std::lock_guard<std::recursive_mutex> guard(m_mutex);
862   LLDB_SCOPED_TIMER();
863   if (!m_name_indexes_computed)
864     InitNameIndexes();
865 
866   if (name) {
867     std::vector<uint32_t> matching_indexes;
868     // The string table did have a string that matched, but we need to check
869     // the symbols and match the symbol_type if any was given.
870     if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
871                                            symbol_visibility,
872                                            matching_indexes)) {
873       std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
874       for (pos = matching_indexes.begin(); pos != end; ++pos) {
875         Symbol *symbol = SymbolAtIndex(*pos);
876 
877         if (symbol->Compare(name, symbol_type))
878           return symbol;
879       }
880     }
881   }
882   return nullptr;
883 }
884 
885 typedef struct {
886   const Symtab *symtab;
887   const addr_t file_addr;
888   Symbol *match_symbol;
889   const uint32_t *match_index_ptr;
890   addr_t match_offset;
891 } SymbolSearchInfo;
892 
893 // Add all the section file start address & size to the RangeVector, recusively
894 // adding any children sections.
895 static void AddSectionsToRangeMap(SectionList *sectlist,
896                                   RangeVector<addr_t, addr_t> &section_ranges) {
897   const int num_sections = sectlist->GetNumSections(0);
898   for (int i = 0; i < num_sections; i++) {
899     SectionSP sect_sp = sectlist->GetSectionAtIndex(i);
900     if (sect_sp) {
901       SectionList &child_sectlist = sect_sp->GetChildren();
902 
903       // If this section has children, add the children to the RangeVector.
904       // Else add this section to the RangeVector.
905       if (child_sectlist.GetNumSections(0) > 0) {
906         AddSectionsToRangeMap(&child_sectlist, section_ranges);
907       } else {
908         size_t size = sect_sp->GetByteSize();
909         if (size > 0) {
910           addr_t base_addr = sect_sp->GetFileAddress();
911           RangeVector<addr_t, addr_t>::Entry entry;
912           entry.SetRangeBase(base_addr);
913           entry.SetByteSize(size);
914           section_ranges.Append(entry);
915         }
916       }
917     }
918   }
919 }
920 
921 void Symtab::InitAddressIndexes() {
922   // Protected function, no need to lock mutex...
923   if (!m_file_addr_to_index_computed && !m_symbols.empty()) {
924     m_file_addr_to_index_computed = true;
925 
926     FileRangeToIndexMap::Entry entry;
927     const_iterator begin = m_symbols.begin();
928     const_iterator end = m_symbols.end();
929     for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
930       if (pos->ValueIsAddress()) {
931         entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
932         entry.SetByteSize(pos->GetByteSize());
933         entry.data = std::distance(begin, pos);
934         m_file_addr_to_index.Append(entry);
935       }
936     }
937     const size_t num_entries = m_file_addr_to_index.GetSize();
938     if (num_entries > 0) {
939       m_file_addr_to_index.Sort();
940 
941       // Create a RangeVector with the start & size of all the sections for
942       // this objfile.  We'll need to check this for any FileRangeToIndexMap
943       // entries with an uninitialized size, which could potentially be a large
944       // number so reconstituting the weak pointer is busywork when it is
945       // invariant information.
946       SectionList *sectlist = m_objfile->GetSectionList();
947       RangeVector<addr_t, addr_t> section_ranges;
948       if (sectlist) {
949         AddSectionsToRangeMap(sectlist, section_ranges);
950         section_ranges.Sort();
951       }
952 
953       // Iterate through the FileRangeToIndexMap and fill in the size for any
954       // entries that didn't already have a size from the Symbol (e.g. if we
955       // have a plain linker symbol with an address only, instead of debug info
956       // where we get an address and a size and a type, etc.)
957       for (size_t i = 0; i < num_entries; i++) {
958         FileRangeToIndexMap::Entry *entry =
959             m_file_addr_to_index.GetMutableEntryAtIndex(i);
960         if (entry->GetByteSize() == 0) {
961           addr_t curr_base_addr = entry->GetRangeBase();
962           const RangeVector<addr_t, addr_t>::Entry *containing_section =
963               section_ranges.FindEntryThatContains(curr_base_addr);
964 
965           // Use the end of the section as the default max size of the symbol
966           addr_t sym_size = 0;
967           if (containing_section) {
968             sym_size =
969                 containing_section->GetByteSize() -
970                 (entry->GetRangeBase() - containing_section->GetRangeBase());
971           }
972 
973           for (size_t j = i; j < num_entries; j++) {
974             FileRangeToIndexMap::Entry *next_entry =
975                 m_file_addr_to_index.GetMutableEntryAtIndex(j);
976             addr_t next_base_addr = next_entry->GetRangeBase();
977             if (next_base_addr > curr_base_addr) {
978               addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
979 
980               // Take the difference between this symbol and the next one as
981               // its size, if it is less than the size of the section.
982               if (sym_size == 0 || size_to_next_symbol < sym_size) {
983                 sym_size = size_to_next_symbol;
984               }
985               break;
986             }
987           }
988 
989           if (sym_size > 0) {
990             entry->SetByteSize(sym_size);
991             Symbol &symbol = m_symbols[entry->data];
992             symbol.SetByteSize(sym_size);
993             symbol.SetSizeIsSynthesized(true);
994           }
995         }
996       }
997 
998       // Sort again in case the range size changes the ordering
999       m_file_addr_to_index.Sort();
1000     }
1001   }
1002 }
1003 
1004 void Symtab::Finalize() {
1005   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1006   // Calculate the size of symbols inside InitAddressIndexes.
1007   InitAddressIndexes();
1008   // Shrink to fit the symbols so we don't waste memory
1009   if (m_symbols.capacity() > m_symbols.size()) {
1010     collection new_symbols(m_symbols.begin(), m_symbols.end());
1011     m_symbols.swap(new_symbols);
1012   }
1013   SaveToCache();
1014 }
1015 
1016 Symbol *Symtab::FindSymbolAtFileAddress(addr_t file_addr) {
1017   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1018   if (!m_file_addr_to_index_computed)
1019     InitAddressIndexes();
1020 
1021   const FileRangeToIndexMap::Entry *entry =
1022       m_file_addr_to_index.FindEntryStartsAt(file_addr);
1023   if (entry) {
1024     Symbol *symbol = SymbolAtIndex(entry->data);
1025     if (symbol->GetFileAddress() == file_addr)
1026       return symbol;
1027   }
1028   return nullptr;
1029 }
1030 
1031 Symbol *Symtab::FindSymbolContainingFileAddress(addr_t file_addr) {
1032   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1033 
1034   if (!m_file_addr_to_index_computed)
1035     InitAddressIndexes();
1036 
1037   const FileRangeToIndexMap::Entry *entry =
1038       m_file_addr_to_index.FindEntryThatContains(file_addr);
1039   if (entry) {
1040     Symbol *symbol = SymbolAtIndex(entry->data);
1041     if (symbol->ContainsFileAddress(file_addr))
1042       return symbol;
1043   }
1044   return nullptr;
1045 }
1046 
1047 void Symtab::ForEachSymbolContainingFileAddress(
1048     addr_t file_addr, std::function<bool(Symbol *)> const &callback) {
1049   std::lock_guard<std::recursive_mutex> guard(m_mutex);
1050 
1051   if (!m_file_addr_to_index_computed)
1052     InitAddressIndexes();
1053 
1054   std::vector<uint32_t> all_addr_indexes;
1055 
1056   // Get all symbols with file_addr
1057   const size_t addr_match_count =
1058       m_file_addr_to_index.FindEntryIndexesThatContain(file_addr,
1059                                                        all_addr_indexes);
1060 
1061   for (size_t i = 0; i < addr_match_count; ++i) {
1062     Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]);
1063     if (symbol->ContainsFileAddress(file_addr)) {
1064       if (!callback(symbol))
1065         break;
1066     }
1067   }
1068 }
1069 
1070 void Symtab::SymbolIndicesToSymbolContextList(
1071     std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) {
1072   // No need to protect this call using m_mutex all other method calls are
1073   // already thread safe.
1074 
1075   const bool merge_symbol_into_function = true;
1076   size_t num_indices = symbol_indexes.size();
1077   if (num_indices > 0) {
1078     SymbolContext sc;
1079     sc.module_sp = m_objfile->GetModule();
1080     for (size_t i = 0; i < num_indices; i++) {
1081       sc.symbol = SymbolAtIndex(symbol_indexes[i]);
1082       if (sc.symbol)
1083         sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1084     }
1085   }
1086 }
1087 
1088 void Symtab::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
1089                                  SymbolContextList &sc_list) {
1090   std::vector<uint32_t> symbol_indexes;
1091 
1092   // eFunctionNameTypeAuto should be pre-resolved by a call to
1093   // Module::LookupInfo::LookupInfo()
1094   assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1095 
1096   if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1097     std::vector<uint32_t> temp_symbol_indexes;
1098     FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes);
1099 
1100     unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1101     if (temp_symbol_indexes_size > 0) {
1102       std::lock_guard<std::recursive_mutex> guard(m_mutex);
1103       for (unsigned i = 0; i < temp_symbol_indexes_size; i++) {
1104         SymbolContext sym_ctx;
1105         sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]);
1106         if (sym_ctx.symbol) {
1107           switch (sym_ctx.symbol->GetType()) {
1108           case eSymbolTypeCode:
1109           case eSymbolTypeResolver:
1110           case eSymbolTypeReExported:
1111           case eSymbolTypeAbsolute:
1112             symbol_indexes.push_back(temp_symbol_indexes[i]);
1113             break;
1114           default:
1115             break;
1116           }
1117         }
1118       }
1119     }
1120   }
1121 
1122   if (!m_name_indexes_computed)
1123     InitNameIndexes();
1124 
1125   for (lldb::FunctionNameType type :
1126        {lldb::eFunctionNameTypeBase, lldb::eFunctionNameTypeMethod,
1127         lldb::eFunctionNameTypeSelector}) {
1128     if (name_type_mask & type) {
1129       auto map = GetNameToSymbolIndexMap(type);
1130 
1131       const UniqueCStringMap<uint32_t>::Entry *match;
1132       for (match = map.FindFirstValueForName(name); match != nullptr;
1133            match = map.FindNextValueForName(match)) {
1134         symbol_indexes.push_back(match->value);
1135       }
1136     }
1137   }
1138 
1139   if (!symbol_indexes.empty()) {
1140     llvm::sort(symbol_indexes.begin(), symbol_indexes.end());
1141     symbol_indexes.erase(
1142         std::unique(symbol_indexes.begin(), symbol_indexes.end()),
1143         symbol_indexes.end());
1144     SymbolIndicesToSymbolContextList(symbol_indexes, sc_list);
1145   }
1146 }
1147 
1148 const Symbol *Symtab::GetParent(Symbol *child_symbol) const {
1149   uint32_t child_idx = GetIndexForSymbol(child_symbol);
1150   if (child_idx != UINT32_MAX && child_idx > 0) {
1151     for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) {
1152       const Symbol *symbol = SymbolAtIndex(idx);
1153       const uint32_t sibling_idx = symbol->GetSiblingIndex();
1154       if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)
1155         return symbol;
1156     }
1157   }
1158   return nullptr;
1159 }
1160 
1161 std::string Symtab::GetCacheKey() {
1162   std::string key;
1163   llvm::raw_string_ostream strm(key);
1164   // Symbol table can come from different object files for the same module. A
1165   // module can have one object file as the main executable and might have
1166   // another object file in a separate symbol file.
1167   strm << m_objfile->GetModule()->GetCacheKey() << "-symtab-"
1168       << llvm::format_hex(m_objfile->GetCacheHash(), 10);
1169   return strm.str();
1170 }
1171 
1172 void Symtab::SaveToCache() {
1173   DataFileCache *cache = Module::GetIndexCache();
1174   if (!cache)
1175     return; // Caching is not enabled.
1176   InitNameIndexes(); // Init the name indexes so we can cache them as well.
1177   const auto byte_order = endian::InlHostByteOrder();
1178   DataEncoder file(byte_order, /*addr_size=*/8);
1179   // Encode will return false if the symbol table's object file doesn't have
1180   // anything to make a signature from.
1181   if (Encode(file))
1182     cache->SetCachedData(GetCacheKey(), file.GetData());
1183 }
1184 
1185 constexpr llvm::StringLiteral kIdentifierCStrMap("CMAP");
1186 
1187 static void EncodeCStrMap(DataEncoder &encoder, ConstStringTable &strtab,
1188                           const UniqueCStringMap<uint32_t> &cstr_map) {
1189   encoder.AppendData(kIdentifierCStrMap);
1190   encoder.AppendU32(cstr_map.GetSize());
1191   for (const auto &entry: cstr_map) {
1192     // Make sure there are no empty strings.
1193     assert((bool)entry.cstring);
1194     encoder.AppendU32(strtab.Add(entry.cstring));
1195     encoder.AppendU32(entry.value);
1196   }
1197 }
1198 
1199 bool DecodeCStrMap(const DataExtractor &data, lldb::offset_t *offset_ptr,
1200                    const StringTableReader &strtab,
1201                    UniqueCStringMap<uint32_t> &cstr_map) {
1202   llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1203   if (identifier != kIdentifierCStrMap)
1204     return false;
1205   const uint32_t count = data.GetU32(offset_ptr);
1206   for (uint32_t i=0; i<count; ++i)
1207   {
1208     llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));
1209     uint32_t value = data.GetU32(offset_ptr);
1210     // No empty strings in the name indexes in Symtab
1211     if (str.empty())
1212       return false;
1213     cstr_map.Append(ConstString(str), value);
1214   }
1215   return true;
1216 }
1217 
1218 constexpr llvm::StringLiteral kIdentifierSymbolTable("SYMB");
1219 constexpr uint32_t CURRENT_CACHE_VERSION = 1;
1220 
1221 /// The encoding format for the symbol table is as follows:
1222 ///
1223 /// Signature signature;
1224 /// ConstStringTable strtab;
1225 /// Identifier four character code: 'SYMB'
1226 /// uint32_t version;
1227 /// uint32_t num_symbols;
1228 /// Symbol symbols[num_symbols];
1229 /// uint8_t num_cstr_maps;
1230 /// UniqueCStringMap<uint32_t> cstr_maps[num_cstr_maps]
1231 bool Symtab::Encode(DataEncoder &encoder) const {
1232   // Name indexes must be computed before calling this function.
1233   assert(m_name_indexes_computed);
1234 
1235   // Encode the object file's signature
1236   CacheSignature signature(m_objfile);
1237   if (!signature.Encode(encoder))
1238     return false;
1239   ConstStringTable strtab;
1240 
1241   // Encoder the symbol table into a separate encoder first. This allows us
1242   // gather all of the strings we willl need in "strtab" as we will need to
1243   // write the string table out before the symbol table.
1244   DataEncoder symtab_encoder(encoder.GetByteOrder(),
1245                               encoder.GetAddressByteSize());
1246   symtab_encoder.AppendData(kIdentifierSymbolTable);
1247   // Encode the symtab data version.
1248   symtab_encoder.AppendU32(CURRENT_CACHE_VERSION);
1249   // Encode the number of symbols.
1250   symtab_encoder.AppendU32(m_symbols.size());
1251   // Encode the symbol data for all symbols.
1252   for (const auto &symbol: m_symbols)
1253     symbol.Encode(symtab_encoder, strtab);
1254 
1255   // Emit a byte for how many C string maps we emit. We will fix this up after
1256   // we emit the C string maps since we skip emitting C string maps if they are
1257   // empty.
1258   size_t num_cmaps_offset = symtab_encoder.GetByteSize();
1259   uint8_t num_cmaps = 0;
1260   symtab_encoder.AppendU8(0);
1261   for (const auto &pair: m_name_to_symbol_indices) {
1262     if (pair.second.IsEmpty())
1263       continue;
1264     ++num_cmaps;
1265     symtab_encoder.AppendU8(pair.first);
1266     EncodeCStrMap(symtab_encoder, strtab, pair.second);
1267   }
1268   if (num_cmaps > 0)
1269     symtab_encoder.PutU8(num_cmaps_offset, num_cmaps);
1270 
1271   // Now that all strings have been gathered, we will emit the string table.
1272   strtab.Encode(encoder);
1273   // Followed the the symbol table data.
1274   encoder.AppendData(symtab_encoder.GetData());
1275   return true;
1276 }
1277 
1278 bool Symtab::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
1279                     bool &signature_mismatch) {
1280   signature_mismatch = false;
1281   CacheSignature signature;
1282   StringTableReader strtab;
1283   { // Scope for "elapsed" object below so it can measure the time parse.
1284     ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabParseTime());
1285     if (!signature.Decode(data, offset_ptr))
1286       return false;
1287     if (CacheSignature(m_objfile) != signature) {
1288       signature_mismatch = true;
1289       return false;
1290     }
1291     // We now decode the string table for all strings in the data cache file.
1292     if (!strtab.Decode(data, offset_ptr))
1293       return false;
1294 
1295     // And now we can decode the symbol table with string table we just decoded.
1296     llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1297     if (identifier != kIdentifierSymbolTable)
1298       return false;
1299     const uint32_t version = data.GetU32(offset_ptr);
1300     if (version != CURRENT_CACHE_VERSION)
1301       return false;
1302     const uint32_t num_symbols = data.GetU32(offset_ptr);
1303     if (num_symbols == 0)
1304       return true;
1305     m_symbols.resize(num_symbols);
1306     SectionList *sections = m_objfile->GetModule()->GetSectionList();
1307     for (uint32_t i=0; i<num_symbols; ++i) {
1308       if (!m_symbols[i].Decode(data, offset_ptr, sections, strtab))
1309         return false;
1310     }
1311   }
1312 
1313   { // Scope for "elapsed" object below so it can measure the time to index.
1314     ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabIndexTime());
1315     const uint8_t num_cstr_maps = data.GetU8(offset_ptr);
1316     for (uint8_t i=0; i<num_cstr_maps; ++i) {
1317       uint8_t type = data.GetU8(offset_ptr);
1318       UniqueCStringMap<uint32_t> &cstr_map =
1319           GetNameToSymbolIndexMap((lldb::FunctionNameType)type);
1320       if (!DecodeCStrMap(data, offset_ptr, strtab, cstr_map))
1321         return false;
1322     }
1323     m_name_indexes_computed = true;
1324   }
1325   return true;
1326 }
1327 
1328 bool Symtab::LoadFromCache() {
1329   DataFileCache *cache = Module::GetIndexCache();
1330   if (!cache)
1331     return false;
1332 
1333   std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =
1334       cache->GetCachedData(GetCacheKey());
1335   if (!mem_buffer_up)
1336     return false;
1337   DataExtractor data(mem_buffer_up->getBufferStart(),
1338                      mem_buffer_up->getBufferSize(),
1339                      m_objfile->GetByteOrder(),
1340                      m_objfile->GetAddressByteSize());
1341   bool signature_mismatch = false;
1342   lldb::offset_t offset = 0;
1343   const bool result = Decode(data, &offset, signature_mismatch);
1344   if (signature_mismatch)
1345     cache->RemoveCacheFile(GetCacheKey());
1346   return result;
1347 }
1348