2116 llvm::StringRef file_name = file.
GetFilename().nonEmptyOr(
"<Unknown>");
2118 file_name.str().c_str());
2119 LLDB_LOG(log,
"Parsing symbol table for {0}", file_name);
2120 Progress progress(
"Parsing symbol table", file_name.str());
2135 llvm::DenseSet<addr_t> symbols_added;
2139 auto add_symbol_addr = [&symbols_added](
lldb::addr_t file_addr) {
2143 symbols_added.insert(file_addr);
2145 FunctionStarts function_starts;
2151 for (i = 0; i <
m_header.ncmds; ++i) {
2154 llvm::MachO::load_command lc;
2160 llvm::MachO::symtab_command lc_obj;
2161 if (
m_data_nsp->GetU32(&offset, &lc_obj.symoff, 4)) {
2162 lc_obj.cmd = lc.cmd;
2163 lc_obj.cmdsize = lc.cmdsize;
2164 symtab_load_command = lc_obj;
2169 case LC_DYLD_INFO_ONLY: {
2170 llvm::MachO::dyld_info_command lc_obj;
2171 if (
m_data_nsp->GetU32(&offset, &lc_obj.rebase_off, 10)) {
2172 lc_obj.cmd = lc.cmd;
2173 lc_obj.cmdsize = lc.cmdsize;
2179 case LC_LOAD_WEAK_DYLIB:
2180 case LC_REEXPORT_DYLIB:
2182 case LC_LOAD_UPWARD_DYLIB: {
2183 uint32_t name_offset = cmd_offset +
m_data_nsp->GetU32(&offset);
2184 const char *path =
m_data_nsp->PeekCStr(name_offset);
2192 if (lc.cmd == LC_REEXPORT_DYLIB) {
2196 dylib_files.
Append(file_spec);
2200 case LC_DYLD_EXPORTS_TRIE: {
2201 llvm::MachO::linkedit_data_command lc_obj;
2202 lc_obj.cmd = lc.cmd;
2203 lc_obj.cmdsize = lc.cmdsize;
2204 if (
m_data_nsp->GetU32(&offset, &lc_obj.dataoff, 2))
2205 exports_trie_load_command = lc_obj;
2207 case LC_FUNCTION_STARTS: {
2208 llvm::MachO::linkedit_data_command lc_obj;
2209 lc_obj.cmd = lc.cmd;
2210 lc_obj.cmdsize = lc.cmdsize;
2211 if (
m_data_nsp->GetU32(&offset, &lc_obj.dataoff, 2))
2212 function_starts_load_command = lc_obj;
2216 const uint8_t *uuid_bytes =
m_data_nsp->PeekData(offset, 16);
2219 image_uuid =
UUID(uuid_bytes, 16);
2226 offset = cmd_offset + lc.cmdsize;
2229 if (!symtab_load_command.
cmd)
2233 if (section_list ==
nullptr)
2236 const uint32_t addr_byte_size =
m_data_nsp->GetAddressByteSize();
2238 bool bit_width_32 = addr_byte_size == 4;
2239 const size_t nlist_byte_size =
2240 bit_width_32 ?
sizeof(
struct nlist) : sizeof(struct nlist_64);
2242 DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size);
2243 DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size);
2244 DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size);
2245 DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order,
2247 DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size);
2249 const
addr_t nlist_data_byte_size =
2250 symtab_load_command.nsyms * nlist_byte_size;
2251 const
addr_t strtab_data_byte_size = symtab_load_command.strsize;
2254 ProcessSP process_sp(m_process_wp.lock());
2255 Process *process = process_sp.get();
2259 bool is_local_shared_cache_image = is_shared_cache_image && !
IsInMemory();
2262 section_list->FindSectionByName(GetSegmentNameTEXT()));
2264 section_list->FindSectionByName(GetSegmentNameDATA()));
2266 section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2268 section_list->FindSectionByName(GetSegmentNameDATA_DIRTY()));
2270 section_list->FindSectionByName(GetSegmentNameDATA_CONST()));
2272 section_list->FindSectionByName(GetSegmentNameOBJC()));
2277 if (text_section_sp.get()) {
2278 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName(
2279 g_section_name_eh_frame);
2280 lldb_no_nlist_section_sp = text_section_sp->GetChildren().FindSectionByName(
2281 g_section_name_lldb_no_nlist);
2283 eh_frame_section_sp =
2285 lldb_no_nlist_section_sp =
2289 if (process &&
m_header.filetype != llvm::MachO::MH_OBJECT &&
2290 !is_local_shared_cache_image) {
2291 Target &target = process->GetTarget();
2300 if (lldb_no_nlist_section_sp)
2305 if (linkedit_section_sp) {
2306 addr_t linkedit_load_addr =
2307 linkedit_section_sp->GetLoadBaseAddress(&target);
2317 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2318 const addr_t symoff_addr = linkedit_load_addr +
2319 symtab_load_command.
symoff -
2320 linkedit_file_offset;
2321 strtab_addr = linkedit_load_addr + symtab_load_command.
stroff -
2322 linkedit_file_offset;
2330 m_header.filetype == llvm::MachO::MH_DYLINKER) {
2332 ReadMemory(process_sp, symoff_addr, nlist_data_byte_size));
2334 nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2336 const addr_t indirect_syms_addr = linkedit_load_addr +
2338 linkedit_file_offset;
2340 process_sp, indirect_syms_addr, dysymtab.
nindirectsyms * 4));
2341 if (indirect_syms_data_sp)
2342 indirect_symbol_index_data.SetData(
2343 indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2353 if (!is_shared_cache_image) {
2355 ReadMemory(process_sp, strtab_addr, strtab_data_byte_size));
2356 if (strtab_data_sp) {
2357 strtab_data.SetData(strtab_data_sp, 0,
2358 strtab_data_sp->GetByteSize());
2363 if (function_starts_load_command.
cmd) {
2364 const addr_t func_start_addr =
2365 linkedit_load_addr + function_starts_load_command.
dataoff -
2366 linkedit_file_offset;
2369 function_starts_load_command.
datasize));
2370 if (func_start_data_sp)
2371 function_starts_data.SetData(func_start_data_sp, 0,
2372 func_start_data_sp->GetByteSize());
2378 if (is_local_shared_cache_image && linkedit_section_sp) {
2386 lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset();
2389 symtab_load_command.
symoff += linkedit_slide;
2390 symtab_load_command.
stroff += linkedit_slide;
2393 function_starts_load_command.
dataoff += linkedit_slide;
2394 exports_trie_load_command.
dataoff += linkedit_slide;
2398 nlist_data_byte_size);
2399 strtab_data = *
m_data_nsp->GetSubsetExtractorSP(symtab_load_command.
stroff,
2400 strtab_data_byte_size);
2405 && (exports_trie_load_command.
datasize > 0)));
2409 }
else if (exports_trie_load_command.
datasize > 0) {
2412 exports_trie_load_command.
datasize);
2416 indirect_symbol_index_data = *
m_data_nsp->GetSubsetExtractorSP(
2419 if (function_starts_load_command.
cmd) {
2420 function_starts_data = *
m_data_nsp->GetSubsetExtractorSP(
2421 function_starts_load_command.
dataoff,
2422 function_starts_load_command.
datasize);
2426 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2428 const bool is_arm = (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2445 if (text_section_sp && function_starts_data.GetByteSize()) {
2446 FunctionStarts::Entry function_start_entry;
2447 function_start_entry.data =
false;
2449 function_start_entry.addr = text_section_sp->GetFileAddress();
2451 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) >
2454 function_start_entry.addr += delta;
2456 if (function_start_entry.addr & 1) {
2458 function_start_entry.data =
true;
2459 }
else if (always_thumb) {
2460 function_start_entry.data =
true;
2463 function_starts.Append(function_start_entry);
2471 if (text_section_sp.get() && eh_frame_section_sp.get() &&
2477 addr_t text_base_addr = text_section_sp->GetFileAddress();
2478 size_t count = functions.
GetSize();
2479 for (
size_t i = 0; i < count; ++i) {
2483 FunctionStarts::Entry function_start_entry;
2484 function_start_entry.addr = func->
base - text_base_addr;
2486 if (function_start_entry.addr & 1) {
2488 function_start_entry.data =
true;
2489 }
else if (always_thumb) {
2490 function_start_entry.data =
true;
2493 function_starts.Append(function_start_entry);
2499 const size_t function_starts_count = function_starts.GetSize();
2514 if (unwind_or_symbol_log)
2515 module_sp->LogMessage(
2516 unwind_or_symbol_log,
2517 "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2520 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get()
2521 ? eh_frame_section_sp->GetID()
2527 std::vector<uint32_t> N_FUN_indexes;
2528 std::vector<uint32_t> N_NSYM_indexes;
2529 std::vector<uint32_t> N_INCL_indexes;
2530 std::vector<uint32_t> N_BRAC_indexes;
2531 std::vector<uint32_t> N_COMM_indexes;
2532 typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap;
2533 typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2534 typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap;
2535 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2536 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2537 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2540 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2541 uint32_t nlist_idx = 0;
2542 Symbol *symbol_ptr =
nullptr;
2544 uint32_t sym_idx = 0;
2546 size_t num_syms = 0;
2547 std::string memory_symbol_name;
2548 uint32_t unmapped_local_symbols_found = 0;
2550 std::vector<TrieEntryWithOffset> reexport_trie_entries;
2551 std::vector<TrieEntryWithOffset> external_sym_trie_entries;
2552 std::set<lldb::addr_t> resolver_addresses;
2554 const size_t dyld_trie_data_size = dyld_trie_data.
GetByteSize();
2555 if (dyld_trie_data_size > 0) {
2556 LLDB_LOG(log,
"Parsing {0} bytes of dyld trie data", dyld_trie_data_size);
2560 if (text_segment_sp)
2561 text_segment_file_addr = text_segment_sp->GetFileAddress();
2563 resolver_addresses, reexport_trie_entries,
2564 external_sym_trie_entries);
2567 typedef std::set<ConstString> IndirectSymbols;
2568 IndirectSymbols indirect_symbol_names;
2591 UUID process_shared_cache_uuid;
2592 addr_t process_shared_cache_base_addr;
2596 process_shared_cache_uuid);
2599 __block
bool found_image =
false;
2600 __block
void *nlist_buffer =
nullptr;
2601 __block
unsigned nlist_count = 0;
2602 __block
char *string_table =
nullptr;
2603 __block vm_offset_t vm_nlist_memory = 0;
2604 __block mach_msg_type_number_t vm_nlist_bytes_read = 0;
2605 __block vm_offset_t vm_string_memory = 0;
2606 __block mach_msg_type_number_t vm_string_bytes_read = 0;
2608 llvm::scope_exit _(^{
2609 if (vm_nlist_memory)
2610 vm_deallocate(
mach_task_self(), vm_nlist_memory, vm_nlist_bytes_read);
2611 if (vm_string_memory)
2612 vm_deallocate(
mach_task_self(), vm_string_memory, vm_string_bytes_read);
2615 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
2616 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
2617 UndefinedNameToDescMap undefined_name_to_desc;
2618 SymbolIndexToName reexport_shlib_needs_fixup;
2626 if (process_shared_cache_uuid.
IsValid() &&
2627 process_shared_cache_uuid !=
UUID(&cache_uuid, 16))
2636 if (image_uuid !=
UUID(dsc_image_uuid, 16))
2643 dyld_image_local_nlist_content_4Symbolication(
2644 image, ^(
const void *nlistStart, uint64_t nlistCount,
2645 const char *stringTable) {
2646 if (!nlistStart || !nlistCount)
2654 nlist_byte_size * nlistCount, &vm_nlist_memory,
2655 &vm_nlist_bytes_read);
2658 assert(vm_nlist_bytes_read == nlist_byte_size * nlistCount);
2663 vm_address_t string_address = (vm_address_t)stringTable;
2664 vm_size_t region_size;
2665 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
2666 vm_region_basic_info_data_t info;
2667 memory_object_name_t object;
2669 ®ion_size, VM_REGION_BASIC_INFO_64,
2670 (vm_region_info_t)&info, &info_count, &
object);
2676 ((vm_address_t)stringTable - string_address),
2677 &vm_string_memory, &vm_string_bytes_read);
2681 nlist_buffer = (
void *)vm_nlist_memory;
2682 string_table = (
char *)vm_string_memory;
2683 nlist_count = nlistCount;
2689 nlist_count * nlist_byte_size,
2690 byte_order, addr_byte_size);
2691 unmapped_local_symbols_found = nlist_count;
2697 unmapped_local_symbols_found -
m_dysymtab.nlocalsym);
2702 for (uint32_t nlist_index = 0;
2703 nlist_index < nlist_count;
2707 std::optional<struct nlist_64> nlist_maybe =
2708 ParseNList(dsc_local_symbols_data, nlist_data_offset,
2712 struct nlist_64 nlist = *nlist_maybe;
2715 const char *symbol_name = string_table + nlist.n_strx;
2717 if (symbol_name == NULL) {
2722 "DSC unmapped local symbol[{0}] has invalid "
2723 "string table offset {1:x} in {2}, ignoring symbol",
2724 nlist_index, nlist.n_strx,
2725 module_sp->GetFileSpec().GetPath()));
2728 if (symbol_name[0] ==
'\0')
2731 const char *symbol_name_non_abi_mangled = NULL;
2734 bool add_nlist =
true;
2735 bool is_debug = ((nlist.n_type & N_STAB) != 0);
2736 bool demangled_is_synthesized =
false;
2737 bool is_gsym =
false;
2738 bool set_value =
true;
2740 assert(sym_idx < num_syms);
2745 switch (nlist.n_type) {
2765 symbol_name, symbol_name_non_abi_mangled,
2767 demangled_is_synthesized =
true;
2769 if (nlist.n_value != 0)
2771 nlist.n_sect, nlist.n_value);
2787 nlist.n_sect, nlist.n_value);
2789 N_FUN_addr_to_sym_idx.insert(
2790 std::make_pair(nlist.n_value, sym_idx));
2794 N_FUN_indexes.push_back(sym_idx);
2798 if (!N_FUN_indexes.empty()) {
2805 N_FUN_indexes.pop_back();
2817 N_STSYM_addr_to_sym_idx.insert(
2818 std::make_pair(nlist.n_value, sym_idx));
2819 symbol_section = section_info.
GetSection(nlist.n_sect,
2821 if (symbol_name && symbol_name[0]) {
2829 symbol_section = section_info.
GetSection(nlist.n_sect,
2863 symbol_section = section_info.
GetSection(nlist.n_sect,
2876 if (symbol_name == NULL) {
2887 N_NSYM_indexes.clear();
2888 N_INCL_indexes.clear();
2889 N_BRAC_indexes.clear();
2890 N_COMM_indexes.clear();
2891 N_FUN_indexes.clear();
2897 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
2898 if (N_SO_has_full_path) {
2899 if ((N_SO_index == sym_idx - 1) &&
2900 ((sym_idx - 1) < num_syms)) {
2906 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2912 N_SO_index = sym_idx;
2914 }
else if ((N_SO_index == sym_idx - 1) &&
2915 ((sym_idx - 1) < num_syms)) {
2920 const char *so_path = sym[sym_idx - 1]
2924 if (so_path && so_path[0]) {
2925 std::string full_so_path(so_path);
2926 const size_t double_slash_pos =
2927 full_so_path.find(
"//");
2928 if (double_slash_pos != std::string::npos) {
2939 &full_so_path[double_slash_pos + 1],
2940 FileSpec::Style::native);
2943 full_so_path.erase(0, double_slash_pos + 1);
2947 if (*full_so_path.rbegin() !=
'/')
2948 full_so_path +=
'/';
2949 full_so_path += symbol_name;
2953 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
2957 N_SO_index = sym_idx;
2978 N_INCL_indexes.push_back(sym_idx);
2988 if (!N_INCL_indexes.empty()) {
2993 N_INCL_indexes.pop_back();
3028 symbol_section = section_info.
GetSection(nlist.n_sect,
3039 symbol_section = section_info.
GetSection(nlist.n_sect,
3041 N_BRAC_indexes.push_back(sym_idx);
3051 symbol_section = section_info.
GetSection(nlist.n_sect,
3053 if (!N_BRAC_indexes.empty()) {
3058 N_BRAC_indexes.pop_back();
3075 N_COMM_indexes.push_back(sym_idx);
3080 symbol_section = section_info.
GetSection(nlist.n_sect,
3091 if (!N_COMM_indexes.empty()) {
3096 N_COMM_indexes.pop_back();
3111 uint8_t n_type = N_TYPE & nlist.n_type;
3112 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3116 const char *reexport_name_cstr =
3117 strtab_data.PeekCStr(nlist.n_value);
3118 if (reexport_name_cstr && reexport_name_cstr[0]) {
3121 reexport_name_cstr +
3122 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3125 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3127 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
3133 if (symbol_name && symbol_name[0]) {
3135 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0));
3136 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3148 symbol_section = section_info.
GetSection(nlist.n_sect,
3151 if (symbol_section == NULL) {
3157 if (TEXT_eh_frame_sectID == nlist.n_sect) {
3160 uint32_t section_type =
3161 symbol_section->Get() & SECTION_TYPE;
3163 switch (section_type) {
3164 case S_CSTRING_LITERALS:
3167 case S_4BYTE_LITERALS:
3170 case S_8BYTE_LITERALS:
3173 case S_LITERAL_POINTERS:
3176 case S_NON_LAZY_SYMBOL_POINTERS:
3180 case S_LAZY_SYMBOL_POINTERS:
3183 case S_SYMBOL_STUBS:
3187 case S_MOD_INIT_FUNC_POINTERS:
3191 case S_MOD_TERM_FUNC_POINTERS:
3199 case S_16BYTE_LITERALS:
3205 case S_LAZY_DYLIB_SYMBOL_POINTERS:
3209 switch (symbol_section->GetType()) {
3236 llvm::StringRef symbol_sect_name =
3237 symbol_section->GetName();
3238 if (symbol_section->IsDescendant(
3239 text_section_sp.get())) {
3240 if (symbol_section->IsClear(
3241 S_ATTR_PURE_INSTRUCTIONS |
3242 S_ATTR_SELF_MODIFYING_CODE |
3243 S_ATTR_SOME_INSTRUCTIONS))
3247 }
else if (symbol_section->IsDescendant(
3248 data_section_sp.get()) ||
3249 symbol_section->IsDescendant(
3250 data_dirty_section_sp.get()) ||
3251 symbol_section->IsDescendant(
3252 data_const_section_sp.get())) {
3253 if (symbol_sect_name.starts_with(
"__objc")) {
3258 symbol_name_non_abi_mangled, type))
3259 demangled_is_synthesized =
true;
3260 }
else if (symbol_sect_name.starts_with(
"__gcc_except_tab")) {
3265 }
else if (symbol_sect_name.starts_with(
"__IMPORT"))
3267 }
else if (symbol_section->IsDescendant(
3268 objc_section_sp.get())) {
3270 if (symbol_name && symbol_name[0] ==
'.') {
3271 llvm::StringRef symbol_name_ref(symbol_name);
3273 g_objc_v1_prefix_class(
".objc_class_name_");
3274 if (symbol_name_ref.starts_with(
3275 g_objc_v1_prefix_class)) {
3276 symbol_name_non_abi_mangled = symbol_name;
3277 symbol_name = symbol_name +
3278 g_objc_v1_prefix_class.size();
3280 demangled_is_synthesized =
true;
3291 uint64_t symbol_value = nlist.n_value;
3292 if (symbol_name_non_abi_mangled) {
3298 if (symbol_name && symbol_name[0] ==
'_') {
3305 if (is_gsym && is_debug) {
3306 const char *gsym_name =
3312 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3316 if (symbol_section) {
3317 const addr_t section_file_addr =
3318 symbol_section->GetFileAddress();
3319 symbol_value -= section_file_addr;
3322 if (is_debug ==
false) {
3330 N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3331 if (range.first != range.second) {
3332 bool found_it =
false;
3333 for (
auto pos = range.first; pos != range.second;
3335 if (sym[sym_idx].GetMangled().
GetName(
3339 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3345 sym[sym_idx].IsExternal());
3346 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3348 if (resolver_addresses.find(nlist.n_value) !=
3349 resolver_addresses.end())
3351 sym[sym_idx].
Clear();
3359 if (resolver_addresses.find(nlist.n_value) !=
3360 resolver_addresses.end())
3372 auto range = N_STSYM_addr_to_sym_idx.equal_range(
3374 if (range.first != range.second) {
3375 bool found_it =
false;
3376 for (
auto pos = range.first; pos != range.second;
3378 if (sym[sym_idx].GetMangled().
GetName(
3382 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3388 sym[sym_idx].IsExternal());
3389 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3391 sym[sym_idx].
Clear();
3399 const char *gsym_name =
3407 ConstNameToSymbolIndexMap::const_iterator pos =
3408 N_GSYM_name_to_sym_idx.find(gsym_name);
3409 if (pos != N_GSYM_name_to_sym_idx.end()) {
3410 const uint32_t GSYM_sym_idx = pos->second;
3411 m_nlist_idx_to_sym_idx[nlist_idx] =
3417 Address(symbol_section, symbol_value);
3418 add_symbol_addr(sym[GSYM_sym_idx]
3425 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 |
3427 sym[sym_idx].
Clear();
3435 sym[sym_idx].
SetID(nlist_idx);
3439 Address(symbol_section, symbol_value);
3441 sym[sym_idx].GetAddress().GetFileAddress());
3443 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
3445 if (demangled_is_synthesized)
3449 sym[sym_idx].
Clear();
3456 for (
const auto &pos : reexport_shlib_needs_fixup) {
3457 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3458 if (undef_pos != undefined_name_to_desc.end()) {
3459 const uint8_t dylib_ordinal =
3460 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3461 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
3471 if (nlist_data.GetByteSize() > 0) {
3473 const uint64_t max_nsyms = nlist_data.GetByteSize() / nlist_byte_size;
3474 const uint64_t max_nindirectsyms =
3475 indirect_symbol_index_data.GetByteSize() /
sizeof(uint32_t);
3479 if (sym ==
nullptr) {
3480 sym = symtab.Resize(
3481 std::min<uint64_t>(symtab_load_command.nsyms, max_nsyms) +
3482 std::min<uint64_t>(m_dysymtab.nindirectsyms, max_nindirectsyms));
3483 num_syms = symtab.GetNumSymbols();
3486 if (unmapped_local_symbols_found) {
3487 assert(m_dysymtab.ilocalsym == 0);
3488 nlist_data_offset += (m_dysymtab.nlocalsym * nlist_byte_size);
3489 nlist_idx = m_dysymtab.nlocalsym;
3494 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
3495 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
3496 UndefinedNameToDescMap undefined_name_to_desc;
3497 SymbolIndexToName reexport_shlib_needs_fixup;
3505 auto ParseSymbolLambda = [&](
struct nlist_64 &nlist, uint32_t nlist_idx,
3507 const bool is_debug = ((nlist.n_type & N_STAB) != 0);
3508 if (is_debug != debug_only)
3511 const char *symbol_name_non_abi_mangled =
nullptr;
3512 const char *symbol_name =
nullptr;
3514 if (have_strtab_data) {
3515 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3517 if (symbol_name ==
nullptr) {
3521 "symbol[{0}] has invalid string table offset {1:x} in {2}, "
3523 nlist_idx, nlist.n_strx, module_sp->GetFileSpec().GetPath()));
3526 if (symbol_name[0] ==
'\0')
3527 symbol_name =
nullptr;
3529 const addr_t str_addr = strtab_addr + nlist.n_strx;
3531 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name,
3533 symbol_name = memory_symbol_name.c_str();
3538 bool add_nlist =
true;
3539 bool is_gsym =
false;
3540 bool demangled_is_synthesized =
false;
3541 bool set_value =
true;
3543 assert(sym_idx < num_syms);
3544 sym[sym_idx].SetDebug(is_debug);
3547 switch (nlist.n_type) {
3562 sym[sym_idx].SetExternal(
true);
3565 symbol_name_non_abi_mangled, type)) {
3566 demangled_is_synthesized =
true;
3568 if (nlist.n_value != 0)
3570 section_info.GetSection(nlist.n_sect, nlist.n_value);
3586 section_info.GetSection(nlist.n_sect, nlist.n_value);
3588 N_FUN_addr_to_sym_idx.insert(
3589 std::make_pair(nlist.n_value, sym_idx));
3593 N_FUN_indexes.push_back(sym_idx);
3597 if (!N_FUN_indexes.empty()) {
3600 symtab.SymbolAtIndex(N_FUN_indexes.back())
3601 ->SetByteSize(nlist.n_value);
3602 N_FUN_indexes.pop_back();
3613 N_STSYM_addr_to_sym_idx.insert(
3614 std::make_pair(nlist.n_value, sym_idx));
3615 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3616 if (symbol_name && symbol_name[0]) {
3624 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3655 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3667 if (symbol_name ==
nullptr) {
3674 symbol_ptr = symtab.SymbolAtIndex(N_SO_index);
3675 symbol_ptr->SetByteSize(sym_idx);
3676 symbol_ptr->SetSizeIsSibling(
true);
3678 N_NSYM_indexes.clear();
3679 N_INCL_indexes.clear();
3680 N_BRAC_indexes.clear();
3681 N_COMM_indexes.clear();
3682 N_FUN_indexes.clear();
3688 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3689 if (N_SO_has_full_path) {
3690 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) {
3693 sym[sym_idx - 1].GetMangled().SetValue(
3695 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3700 N_SO_index = sym_idx;
3702 }
else if ((N_SO_index == sym_idx - 1) &&
3703 ((sym_idx - 1) < num_syms)) {
3707 llvm::StringRef so_path = sym[sym_idx - 1]
3711 if (!so_path.empty()) {
3712 std::string full_so_path(so_path);
3713 const size_t double_slash_pos = full_so_path.find(
"//");
3714 if (double_slash_pos != std::string::npos) {
3722 so_dir.SetFile(&full_so_path[double_slash_pos + 1],
3723 FileSpec::Style::native);
3726 full_so_path.erase(0, double_slash_pos + 1);
3730 if (*full_so_path.rbegin() !=
'/')
3731 full_so_path +=
'/';
3732 full_so_path += symbol_name;
3733 sym[sym_idx - 1].GetMangled().SetValue(
3736 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3740 N_SO_index = sym_idx;
3760 N_INCL_indexes.push_back(sym_idx);
3769 if (!N_INCL_indexes.empty()) {
3770 symbol_ptr = symtab.SymbolAtIndex(N_INCL_indexes.back());
3771 symbol_ptr->SetByteSize(sym_idx + 1);
3772 symbol_ptr->SetSizeIsSibling(
true);
3773 N_INCL_indexes.pop_back();
3808 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3817 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3818 N_BRAC_indexes.push_back(sym_idx);
3827 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3828 if (!N_BRAC_indexes.empty()) {
3829 symbol_ptr = symtab.SymbolAtIndex(N_BRAC_indexes.back());
3830 symbol_ptr->SetByteSize(sym_idx + 1);
3831 symbol_ptr->SetSizeIsSibling(
true);
3832 N_BRAC_indexes.pop_back();
3848 N_COMM_indexes.push_back(sym_idx);
3853 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3862 if (!N_COMM_indexes.empty()) {
3863 symbol_ptr = symtab.SymbolAtIndex(N_COMM_indexes.back());
3864 symbol_ptr->SetByteSize(sym_idx + 1);
3865 symbol_ptr->SetSizeIsSibling(
true);
3866 N_COMM_indexes.pop_back();
3880 uint8_t n_type = N_TYPE & nlist.n_type;
3881 sym[sym_idx].SetExternal((N_EXT & nlist.n_type) != 0);
3885 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3886 if (reexport_name_cstr && reexport_name_cstr[0] && symbol_name) {
3889 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3890 sym[sym_idx].SetReExportedSymbolName(reexport_name);
3892 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3893 indirect_symbol_names.insert(
3894 ConstString(symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
3900 if (symbol_name && symbol_name[0]) {
3902 ((symbol_name[0] ==
'_') ? 1 : 0));
3903 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3916 symbol_section = section_info.GetSection(nlist.n_sect, nlist.n_value);
3918 if (!symbol_section) {
3924 if (TEXT_eh_frame_sectID == nlist.n_sect) {
3927 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
3929 switch (section_type) {
3930 case S_CSTRING_LITERALS:
3933 case S_4BYTE_LITERALS:
3936 case S_8BYTE_LITERALS:
3939 case S_LITERAL_POINTERS:
3942 case S_NON_LAZY_SYMBOL_POINTERS:
3945 case S_LAZY_SYMBOL_POINTERS:
3948 case S_SYMBOL_STUBS:
3952 case S_MOD_INIT_FUNC_POINTERS:
3955 case S_MOD_TERM_FUNC_POINTERS:
3962 case S_16BYTE_LITERALS:
3968 case S_LAZY_DYLIB_SYMBOL_POINTERS:
3972 switch (symbol_section->GetType()) {
3994 llvm::StringRef symbol_sect_name = symbol_section->GetName();
3995 if (symbol_section->IsDescendant(text_section_sp.get())) {
3996 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
3997 S_ATTR_SELF_MODIFYING_CODE |
3998 S_ATTR_SOME_INSTRUCTIONS))
4002 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
4003 symbol_section->IsDescendant(
4004 data_dirty_section_sp.get()) ||
4005 symbol_section->IsDescendant(
4006 data_const_section_sp.get())) {
4007 if (symbol_sect_name.starts_with(
"__objc")) {
4011 symbol_name, symbol_name_non_abi_mangled, type))
4012 demangled_is_synthesized =
true;
4013 }
else if (symbol_sect_name.starts_with(
"__gcc_except_tab")) {
4018 }
else if (symbol_sect_name.starts_with(
"__IMPORT")) {
4020 }
else if (symbol_section->IsDescendant(objc_section_sp.get())) {
4022 if (symbol_name && symbol_name[0] ==
'.') {
4023 llvm::StringRef symbol_name_ref(symbol_name);
4024 llvm::StringRef g_objc_v1_prefix_class(
4025 ".objc_class_name_");
4026 if (symbol_name_ref.starts_with(g_objc_v1_prefix_class)) {
4027 symbol_name_non_abi_mangled = symbol_name;
4028 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4030 demangled_is_synthesized =
true;
4041 sym[sym_idx].Clear();
4045 uint64_t symbol_value = nlist.n_value;
4047 if (symbol_name_non_abi_mangled) {
4048 sym[sym_idx].GetMangled().SetMangledName(
4050 sym[sym_idx].GetMangled().SetDemangledName(
ConstString(symbol_name));
4053 if (symbol_name && symbol_name[0] ==
'_') {
4059 sym[sym_idx].GetMangled().SetValue(const_symbol_name);
4064 const char *gsym_name = sym[sym_idx]
4069 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4072 if (symbol_section) {
4073 const addr_t section_file_addr = symbol_section->GetFileAddress();
4074 symbol_value -= section_file_addr;
4083 std::pair<ValueToSymbolIndexMap::const_iterator,
4084 ValueToSymbolIndexMap::const_iterator>
4086 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4087 if (range.first != range.second) {
4088 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4089 pos != range.second; ++pos) {
4091 sym[pos->second].GetMangled().GetName(
4093 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4097 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4098 sym[pos->second].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4099 if (resolver_addresses.find(nlist.n_value) !=
4100 resolver_addresses.end())
4102 sym[sym_idx].Clear();
4107 if (resolver_addresses.find(nlist.n_value) !=
4108 resolver_addresses.end())
4118 std::pair<ValueToSymbolIndexMap::const_iterator,
4119 ValueToSymbolIndexMap::const_iterator>
4121 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4122 if (range.first != range.second) {
4123 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4124 pos != range.second; ++pos) {
4126 sym[pos->second].GetMangled().GetName(
4128 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4132 sym[pos->second].SetExternal(sym[sym_idx].IsExternal());
4133 sym[pos->second].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4134 sym[sym_idx].Clear();
4140 const char *gsym_name = sym[sym_idx]
4145 ConstNameToSymbolIndexMap::const_iterator pos =
4146 N_GSYM_name_to_sym_idx.find(gsym_name);
4147 if (pos != N_GSYM_name_to_sym_idx.end()) {
4148 const uint32_t GSYM_sym_idx = pos->second;
4149 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4152 sym[GSYM_sym_idx].GetAddressRef() =
4153 Address(symbol_section, symbol_value);
4155 sym[GSYM_sym_idx].GetAddress().GetFileAddress());
4159 sym[GSYM_sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4160 sym[sym_idx].Clear();
4168 sym[sym_idx].SetID(nlist_idx);
4169 sym[sym_idx].SetType(type);
4171 sym[sym_idx].GetAddressRef() =
Address(symbol_section, symbol_value);
4173 add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
4175 sym[sym_idx].SetFlags(nlist.n_type << 16 | nlist.n_desc);
4176 if (nlist.n_desc & N_WEAK_REF)
4177 sym[sym_idx].SetIsWeak(
true);
4179 if (demangled_is_synthesized)
4180 sym[sym_idx].SetDemangledNameIsSynthesized(
true);
4188 std::vector<struct nlist_64> nlists;
4189 nlists.reserve(std::min<uint64_t>(symtab_load_command.nsyms, max_nsyms));
4190 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) {
4192 ParseNList(nlist_data, nlist_data_offset, nlist_byte_size))
4193 nlists.push_back(*nlist);
4203 for (
auto &nlist : nlists) {
4204 if (!ParseSymbolLambda(nlist, nlist_idx++,
DebugSymbols))
4210 for (
auto &nlist : nlists) {
4215 for (
const auto &pos : reexport_shlib_needs_fixup) {
4216 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4217 if (undef_pos != undefined_name_to_desc.end()) {
4218 const uint8_t dylib_ordinal =
4219 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4220 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.GetSize())
4221 sym[pos.first].SetReExportedSymbolSharedLibrary(
4222 dylib_files.GetFileSpecAtIndex(dylib_ordinal - 1));
4228 int trie_symbol_table_augment_count = 0;
4229 for (
auto &e : external_sym_trie_entries) {
4230 if (!symbols_added.contains(e.entry.address))
4231 trie_symbol_table_augment_count++;
4234 if (num_syms < sym_idx + trie_symbol_table_augment_count) {
4235 num_syms = sym_idx + trie_symbol_table_augment_count;
4236 sym = symtab.Resize(num_syms);
4238 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4241 for (
auto &e : external_sym_trie_entries) {
4242 if (symbols_added.contains(e.entry.address))
4248 if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) {
4250 const char *symbol_name = e.entry.name.GetCString();
4251 bool demangled_is_synthesized =
false;
4253 GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp,
4254 data_section_sp, data_dirty_section_sp,
4255 data_const_section_sp, symbol_section);
4257 sym[sym_idx].SetType(type);
4258 if (symbol_section) {
4259 sym[sym_idx].SetID(synthetic_sym_id++);
4260 sym[sym_idx].GetMangled().SetMangledName(
ConstString(symbol_name));
4261 if (demangled_is_synthesized)
4262 sym[sym_idx].SetDemangledNameIsSynthesized(
true);
4263 sym[sym_idx].SetIsSynthetic(
true);
4264 sym[sym_idx].SetExternal(
true);
4265 sym[sym_idx].GetAddressRef() = symbol_addr;
4274 if (function_starts_count > 0) {
4275 uint32_t num_synthetic_function_symbols = 0;
4276 for (i = 0; i < function_starts_count; ++i) {
4277 if (!symbols_added.contains(function_starts.GetEntryRef(i).addr))
4278 ++num_synthetic_function_symbols;
4281 if (num_synthetic_function_symbols > 0) {
4282 if (num_syms < sym_idx + num_synthetic_function_symbols) {
4283 num_syms = sym_idx + num_synthetic_function_symbols;
4284 sym = symtab.Resize(num_syms);
4286 for (i = 0; i < function_starts_count; ++i) {
4287 const FunctionStarts::Entry *func_start_entry =
4288 function_starts.GetEntryAtIndex(i);
4289 if (!symbols_added.contains(func_start_entry->addr)) {
4290 addr_t symbol_file_addr = func_start_entry->addr;
4291 uint32_t symbol_flags = 0;
4292 if (func_start_entry->data)
4295 if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) {
4297 if (symbol_section) {
4298 sym[sym_idx].SetID(synthetic_sym_id++);
4302 sym[sym_idx].GetMangled().SetDemangledName(
ConstString());
4304 sym[sym_idx].SetIsSynthetic(
true);
4305 sym[sym_idx].GetAddressRef() = symbol_addr;
4308 sym[sym_idx].SetFlags(symbol_flags);
4319 if (sym_idx < num_syms) {
4321 sym = symtab.Resize(num_syms);
4325 if (m_dysymtab.nindirectsyms != 0) {
4326 if (indirect_symbol_index_data.GetByteSize()) {
4327 NListIndexToSymbolIndexMap::const_iterator end_index_pos =
4328 m_nlist_idx_to_sym_idx.end();
4330 for (uint32_t sect_idx = 1; sect_idx < m_mach_sections.size();
4332 if ((m_mach_sections[sect_idx].flags & SECTION_TYPE) ==
4334 uint32_t symbol_stub_byte_size = m_mach_sections[sect_idx].reserved2;
4335 if (symbol_stub_byte_size == 0)
4338 const uint32_t num_symbol_stubs =
4339 m_mach_sections[sect_idx].size / symbol_stub_byte_size;
4341 if (num_symbol_stubs == 0)
4344 const uint32_t symbol_stub_index_offset =
4345 m_mach_sections[sect_idx].reserved1;
4346 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) {
4347 const uint32_t symbol_stub_index =
4348 symbol_stub_index_offset + stub_idx;
4350 m_mach_sections[sect_idx].addr +
4351 (stub_idx * symbol_stub_byte_size);
4353 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(
4354 symbol_stub_offset, 4)) {
4355 const uint32_t stub_sym_id =
4356 indirect_symbol_index_data.GetU32(&symbol_stub_offset);
4357 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4360 NListIndexToSymbolIndexMap::const_iterator index_pos =
4361 m_nlist_idx_to_sym_idx.find(stub_sym_id);
4362 Symbol *stub_symbol =
nullptr;
4363 if (index_pos != end_index_pos) {
4366 stub_symbol = symtab.SymbolAtIndex(index_pos->second);
4370 stub_symbol = symtab.FindSymbolByID(stub_sym_id);
4374 Address so_addr(symbol_stub_addr, section_list);
4381 if (resolver_addresses.find(symbol_stub_addr) ==
4382 resolver_addresses.end())
4392 if (sym_idx >= num_syms) {
4393 sym = symtab.Resize(++num_syms);
4394 stub_symbol =
nullptr;
4396 sym[sym_idx].SetID(synthetic_sym_id++);
4397 sym[sym_idx].GetMangled() = stub_symbol_mangled_name;
4398 if (resolver_addresses.find(symbol_stub_addr) ==
4399 resolver_addresses.end())
4403 sym[sym_idx].SetIsSynthetic(
true);
4404 sym[sym_idx].GetAddressRef() = so_addr;
4405 add_symbol_addr(so_addr.GetFileAddress());
4406 sym[sym_idx].SetByteSize(symbol_stub_byte_size);
4411 "warning: symbol stub referencing symbol table "
4412 "symbol %u that isn't in our minimal symbol table, "
4423 if (!reexport_trie_entries.empty()) {
4424 for (
const auto &e : reexport_trie_entries) {
4425 if (e.entry.import_name) {
4428 if (indirect_symbol_names.find(e.entry.name) ==
4429 indirect_symbol_names.end()) {
4431 if (sym_idx >= num_syms)
4432 sym = symtab.Resize(++num_syms);
4433 sym[sym_idx].SetID(synthetic_sym_id++);
4434 sym[sym_idx].GetMangled() =
Mangled(e.entry.name);
4436 sym[sym_idx].SetIsSynthetic(
true);
4437 sym[sym_idx].SetReExportedSymbolName(e.entry.import_name);
4438 if (e.entry.other > 0 && e.entry.other <= dylib_files.GetSize()) {
4439 sym[sym_idx].SetReExportedSymbolSharedLibrary(
4440 dylib_files.GetFileSpecAtIndex(e.entry.other - 1));