27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Support/DJB.h"
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) {
69 name_to_index.Clear();
78 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
84 name_to_index.Clear();
89 Mangled::NamePreference name_preference) {
90 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
95 const char *object_name =
nullptr;
100 s->
Printf(
"Symtab, file = %s%s%s%s, num_symbols = %" PRIu64,
101 file_spec.
GetPath().c_str(), object_name ?
"(" :
"",
102 object_name ? object_name :
"", object_name ?
")" :
"",
105 s->
Printf(
"Symtab, num_symbols = %" PRIu64
"", (uint64_t)
m_symbols.size());
108 switch (sort_order) {
116 pos->Dump(s, target, std::distance(begin, pos), name_preference);
127 std::multimap<llvm::StringRef, const Symbol *> name_map;
130 const char *name = pos->GetName().AsCString();
132 name_map.insert(std::make_pair(name, &(*pos)));
135 for (
const auto &name_to_symbol : name_map) {
136 const Symbol *symbol = name_to_symbol.second;
138 symbol->
Dump(s, target, symbol - &
m_symbols[0], name_preference);
148 for (
size_t i = 0; i < num_entries; ++i) {
151 m_symbols[symbol_idx].Dump(s, target, symbol_idx, name_preference);
161 Mangled::NamePreference name_preference)
const {
162 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
167 s->
Printf(
"Symtab %" PRIu64
" symbol indexes (%" PRIu64
" symbols total):\n",
168 (uint64_t)indexes.size(), (uint64_t)
m_symbols.size());
171 if (!indexes.empty()) {
172 std::vector<uint32_t>::const_iterator pos;
173 std::vector<uint32_t>::const_iterator end = indexes.end();
175 for (pos = indexes.begin(); pos != end; ++pos) {
177 if (idx < num_symbols) {
179 m_symbols[idx].Dump(s, target, idx, name_preference);
187 s->
Indent(
" Debug symbol\n");
188 s->
Indent(
" |Synthetic symbol\n");
189 s->
Indent(
" ||Externally Visible\n");
191 s->
Indent(
"Index UserID DSX Type File Address/Value Load "
192 "Address Size Flags Name\n");
193 s->
Indent(
"------- ------ --- --------------- ------------------ "
194 "------------------ ------------------ ---------- "
195 "----------------------------------\n");
201 if (match_uid < symbol_uid)
203 if (match_uid > symbol_uid)
209 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
234 Mangled::ManglingScheme scheme) {
236 case Mangled::eManglingSchemeItanium: {
237 if (mangled.size() < 3 || !mangled.startswith(
"_Z"))
241 switch (mangled[2]) {
257 case Mangled::eManglingSchemeMSVC:
258 case Mangled::eManglingSchemeRustV0:
259 case Mangled::eManglingSchemeD:
263 case Mangled::eManglingSchemeNone:
266 llvm_unreachable(
"unknown scheme!");
277 std::vector<Language *> languages;
279 languages.push_back(l);
284 auto &basename_to_index =
286 auto &method_to_index =
288 auto &selector_to_index =
291 const size_t num_symbols =
m_symbols.size();
292 name_to_index.Reserve(num_symbols);
296 std::set<const char *> class_contexts;
297 std::vector<std::pair<NameToIndexMap::Entry, const char *>> backlog;
298 backlog.reserve(num_symbols / 2);
303 for (
uint32_t value = 0; value < num_symbols; ++value) {
319 name_to_index.Append(name, value);
326 name_to_index.Append(stripped, value);
340 if (
ConstString name = mangled.GetDemangledName()) {
341 name_to_index.Append(name, value);
348 name_to_index.Append(name, value);
354 for (
auto variant : lang->GetMethodNameVariants(name)) {
355 if (variant.GetType() & lldb::eFunctionNameTypeSelector)
356 selector_to_index.Append(variant.GetName(), value);
357 else if (variant.GetType() & lldb::eFunctionNameTypeFull)
358 name_to_index.Append(variant.GetName(), value);
359 else if (variant.GetType() & lldb::eFunctionNameTypeMethod)
360 method_to_index.Append(variant.GetName(), value);
361 else if (variant.GetType() & lldb::eFunctionNameTypeBase)
362 basename_to_index.Append(variant.GetName(), value);
368 for (
const auto &record : backlog) {
372 name_to_index.Sort();
373 name_to_index.SizeToFit();
374 selector_to_index.Sort();
375 selector_to_index.SizeToFit();
376 basename_to_index.Sort();
377 basename_to_index.SizeToFit();
378 method_to_index.Sort();
379 method_to_index.SizeToFit();
384 uint32_t value, std::set<const char *> &class_contexts,
385 std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
389 if (base_name.empty())
397 if (decl_context.empty()) {
399 auto &basename_to_index =
401 basename_to_index.Append(entry);
405 name_to_index.Append(entry);
412 auto it = class_contexts.find(decl_context_ccstr);
414 auto &method_to_index =
419 method_to_index.Append(entry);
420 if (it == class_contexts.end())
421 class_contexts.insert(it, decl_context_ccstr);
426 if (it != class_contexts.end()) {
427 method_to_index.Append(entry);
433 backlog.push_back(std::make_pair(entry, decl_context_ccstr));
438 const std::set<const char *> &class_contexts) {
439 auto &method_to_index =
441 auto it = class_contexts.find(decl_context);
442 if (it != class_contexts.end()) {
443 method_to_index.Append(entry);
447 method_to_index.Append(entry);
448 auto &basename_to_index =
450 basename_to_index.Append(entry);
455 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
460 bool add_demangled,
bool add_mangled,
463 if (add_demangled || add_mangled) {
464 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
467 const size_t num_indexes = indexes.size();
468 for (
size_t i = 0; i < num_indexes; ++i) {
473 const Mangled &mangled = symbol->
GetMangled();
476 name_to_index_map.
Append(name, value);
481 name_to_index_map.
Append(name, value);
488 std::vector<uint32_t> &indexes,
491 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
493 uint32_t prev_size = indexes.size();
497 for (
uint32_t i = start_idx; i < count; ++i) {
499 indexes.push_back(i);
502 return indexes.size() - prev_size;
507 std::vector<uint32_t> &indexes,
uint32_t start_idx,
509 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
511 uint32_t prev_size = indexes.size();
515 for (
uint32_t i = start_idx; i < count; ++i) {
517 m_symbols[i].GetType() == symbol_type) &&
519 indexes.push_back(i);
522 return indexes.size() - prev_size;
526 Debug symbol_debug_type,
528 std::vector<uint32_t> &indexes,
531 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
533 uint32_t prev_size = indexes.size();
537 for (
uint32_t i = start_idx; i < count; ++i) {
541 indexes.push_back(i);
545 return indexes.size() - prev_size;
551 if (symbol >= first_symbol && symbol < first_symbol +
m_symbols.size())
552 return symbol - first_symbol;
563 struct SymbolIndexComparator {
564 const std::vector<Symbol> &symbols;
565 std::vector<lldb::addr_t> &addr_cache;
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());
584 addr_t value_a = addr_cache[index_a];
586 value_a = symbols[index_a].GetAddressRef().GetFileAddress();
587 addr_cache[index_a] = value_a;
590 addr_t value_b = addr_cache[index_b];
592 value_b = symbols[index_b].GetAddressRef().GetFileAddress();
593 addr_cache[index_b] = value_b;
596 if (value_a == value_b) {
605 }
else if (value_a < value_b)
614 bool remove_duplicates)
const {
615 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
618 if (indexes.size() <= 1)
628 SymbolIndexComparator comparator(
m_symbols, addr_cache);
629 std::stable_sort(indexes.begin(), indexes.end(), comparator);
632 if (remove_duplicates) {
633 auto last = std::unique(indexes.begin(), indexes.end());
634 indexes.erase(last, indexes.end());
639 std::vector<uint32_t> &indexes) {
641 const uint32_t count = name_to_index.GetValues(symbol_name, indexes);
655 unsigned long long uid = 0;
656 if (getAsUnsignedInteger(name, 10, uid))
659 if (symbol ==
nullptr)
664 indexes.push_back(symbol_idx);
669 std::vector<uint32_t> &indexes) {
670 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
682 Debug symbol_debug_type,
684 std::vector<uint32_t> &indexes) {
685 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
689 const size_t old_size = indexes.size();
693 std::vector<uint32_t> all_name_indexes;
694 const size_t name_match_count =
696 for (
size_t i = 0; i < name_match_count; ++i) {
699 indexes.push_back(all_name_indexes[i]);
701 return indexes.size() - old_size;
709 std::vector<uint32_t> &indexes) {
710 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
713 std::vector<uint32_t>::iterator pos = indexes.begin();
714 while (pos != indexes.end()) {
716 m_symbols[*pos].GetType() == symbol_type)
719 pos = indexes.erase(pos);
722 return indexes.size();
728 std::vector<uint32_t> &indexes) {
729 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
732 symbol_visibility, indexes) > 0) {
733 std::vector<uint32_t>::iterator pos = indexes.begin();
734 while (pos != indexes.end()) {
736 m_symbols[*pos].GetType() == symbol_type)
739 pos = indexes.erase(pos);
742 return indexes.size();
747 std::vector<uint32_t> &indexes) {
748 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
750 uint32_t prev_size = indexes.size();
753 for (
uint32_t i = 0; i < sym_end; i++) {
756 const char *name =
m_symbols[i].GetName().AsCString();
759 indexes.push_back(i);
763 return indexes.size() - prev_size;
769 std::vector<uint32_t> &indexes) {
770 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
772 uint32_t prev_size = indexes.size();
775 for (
uint32_t i = 0; i < sym_end; i++) {
781 const char *name =
m_symbols[i].GetName().AsCString();
784 indexes.push_back(i);
788 return indexes.size() - prev_size;
792 Debug symbol_debug_type,
795 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
798 for (
size_t idx = start_idx; idx < count; ++idx) {
800 m_symbols[idx].GetType() == symbol_type) {
813 std::vector<uint32_t> &symbol_indexes) {
814 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
830 Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) {
831 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
843 symbol_visibility, symbol_indexes);
850 std::vector<uint32_t> &symbol_indexes) {
851 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
854 symbol_visibility, symbol_indexes);
859 Debug symbol_debug_type,
861 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
867 std::vector<uint32_t> matching_indexes;
873 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
874 for (pos = matching_indexes.begin(); pos != end; ++pos) {
877 if (symbol->
Compare(name, symbol_type))
898 for (
int i = 0; i < num_sections; i++) {
901 SectionList &child_sectlist = sect_sp->GetChildren();
908 size_t size = sect_sp->GetByteSize();
910 addr_t base_addr = sect_sp->GetFileAddress();
914 section_ranges.
Append(entry);
930 if (pos->ValueIsAddress()) {
931 entry.
SetRangeBase(pos->GetAddressRef().GetFileAddress());
933 entry.
data = std::distance(begin, pos);
938 if (num_entries > 0) {
950 section_ranges.
Sort();
957 for (
size_t i = 0; i < num_entries; i++) {
967 if (containing_section) {
973 for (
size_t j = i; j < num_entries; j++) {
977 if (next_base_addr > curr_base_addr) {
978 addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
982 if (sym_size == 0 || size_to_next_symbol < sym_size) {
983 sym_size = size_to_next_symbol;
1005 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
1017 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
1032 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
1048 addr_t file_addr, std::function<
bool(
Symbol *)>
const &callback) {
1049 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
1054 std::vector<uint32_t> all_addr_indexes;
1057 const size_t addr_match_count =
1061 for (
size_t i = 0; i < addr_match_count; ++i) {
1064 if (!callback(symbol))
1075 const bool merge_symbol_into_function =
true;
1076 size_t num_indices = symbol_indexes.size();
1077 if (num_indices > 0) {
1080 for (
size_t i = 0; i < num_indices; i++) {
1090 std::vector<uint32_t> symbol_indexes;
1094 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1096 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1097 std::vector<uint32_t> temp_symbol_indexes;
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++) {
1112 symbol_indexes.push_back(temp_symbol_indexes[i]);
1125 for (lldb::FunctionNameType type :
1126 {lldb::eFunctionNameTypeBase, lldb::eFunctionNameTypeMethod,
1127 lldb::eFunctionNameTypeSelector}) {
1128 if (name_type_mask & type) {
1134 symbol_indexes.push_back(match->value);
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());
1150 if (child_idx !=
UINT32_MAX && child_idx > 0) {
1154 if (sibling_idx !=
UINT32_MAX && sibling_idx > child_idx)
1163 llvm::raw_string_ostream strm(key);
1178 DataEncoder file(byte_order, 8);
1191 encoder.AppendU32(cstr_map.
GetSize());
1192 for (
const auto &entry: cstr_map) {
1194 assert((
bool)entry.cstring);
1195 encoder.AppendU32(strtab.
Add(entry.cstring));
1196 encoder.AppendU32(entry.value);
1203 llvm::StringRef identifier((
const char *)data.
GetData(offset_ptr, 4), 4);
1210 llvm::StringRef str(strtab.
Get(data.
GetU32(offset_ptr)));
1249 if (!signature.
Encode(encoder))
1256 DataEncoder symtab_encoder(encoder.GetByteOrder(),
1257 encoder.GetAddressByteSize());
1262 symtab_encoder.AppendU32(
m_symbols.size());
1265 symbol.Encode(symtab_encoder, strtab);
1270 size_t num_cmaps_offset = symtab_encoder.GetByteSize();
1271 uint8_t num_cmaps = 0;
1272 symtab_encoder.AppendU8(0);
1274 if (pair.second.IsEmpty())
1277 symtab_encoder.AppendU8(pair.first);
1281 symtab_encoder.PutU8(num_cmaps_offset, num_cmaps);
1286 encoder.AppendData(symtab_encoder.GetData());
1291 bool &signature_mismatch) {
1292 signature_mismatch =
false;
1297 if (!signature.
Decode(data, offset_ptr))
1300 signature_mismatch =
true;
1304 if (!strtab.
Decode(data, offset_ptr))
1308 llvm::StringRef identifier((
const char *)data.
GetData(offset_ptr, 4), 4);
1315 if (num_symbols == 0)
1319 for (
uint32_t i=0; i<num_symbols; ++i) {
1327 const uint8_t num_cstr_maps = data.
GetU8(offset_ptr);
1328 for (uint8_t i=0; i<num_cstr_maps; ++i) {
1329 uint8_t type = data.
GetU8(offset_ptr);
1345 std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =
1350 mem_buffer_up->getBufferSize(),
1353 bool signature_mismatch =
false;
1355 const bool result =
Decode(data, &offset, signature_mismatch);
1356 if (signature_mismatch)