2215 LLDB_LOG(log,
"Parsing symbol table for {0}", file_name);
2216 Progress progress(
"Parsing symbol table", file_name);
2231 llvm::DenseSet<addr_t> symbols_added;
2235 auto add_symbol_addr = [&symbols_added](
lldb::addr_t file_addr) {
2239 symbols_added.insert(file_addr);
2241 FunctionStarts function_starts;
2247 for (i = 0; i <
m_header.ncmds; ++i) {
2250 llvm::MachO::load_command lc;
2251 if (
m_data_nsp->GetU32(&offset, &lc, 2) ==
nullptr)
2256 llvm::MachO::symtab_command lc_obj;
2257 if (
m_data_nsp->GetU32(&offset, &lc_obj.symoff, 4)) {
2258 lc_obj.cmd = lc.cmd;
2259 lc_obj.cmdsize = lc.cmdsize;
2260 symtab_load_command = lc_obj;
2265 case LC_DYLD_INFO_ONLY: {
2266 llvm::MachO::dyld_info_command lc_obj;
2267 if (
m_data_nsp->GetU32(&offset, &lc_obj.rebase_off, 10)) {
2268 lc_obj.cmd = lc.cmd;
2269 lc_obj.cmdsize = lc.cmdsize;
2275 case LC_LOAD_WEAK_DYLIB:
2276 case LC_REEXPORT_DYLIB:
2278 case LC_LOAD_UPWARD_DYLIB: {
2279 uint32_t name_offset = cmd_offset +
m_data_nsp->GetU32(&offset);
2280 const char *path =
m_data_nsp->PeekCStr(name_offset);
2288 if (lc.cmd == LC_REEXPORT_DYLIB) {
2292 dylib_files.
Append(file_spec);
2296 case LC_DYLD_EXPORTS_TRIE: {
2297 llvm::MachO::linkedit_data_command lc_obj;
2298 lc_obj.cmd = lc.cmd;
2299 lc_obj.cmdsize = lc.cmdsize;
2300 if (
m_data_nsp->GetU32(&offset, &lc_obj.dataoff, 2))
2301 exports_trie_load_command = lc_obj;
2303 case LC_FUNCTION_STARTS: {
2304 llvm::MachO::linkedit_data_command lc_obj;
2305 lc_obj.cmd = lc.cmd;
2306 lc_obj.cmdsize = lc.cmdsize;
2307 if (
m_data_nsp->GetU32(&offset, &lc_obj.dataoff, 2))
2308 function_starts_load_command = lc_obj;
2312 const uint8_t *uuid_bytes =
m_data_nsp->PeekData(offset, 16);
2315 image_uuid =
UUID(uuid_bytes, 16);
2322 offset = cmd_offset + lc.cmdsize;
2325 if (!symtab_load_command.
cmd)
2329 if (section_list ==
nullptr)
2332 const uint32_t addr_byte_size =
m_data_nsp->GetAddressByteSize();
2334 bool bit_width_32 = addr_byte_size == 4;
2335 const size_t nlist_byte_size =
2336 bit_width_32 ?
sizeof(
struct nlist) : sizeof(struct nlist_64);
2338 DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size);
2339 DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size);
2340 DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size);
2341 DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order,
2343 DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size);
2345 const
addr_t nlist_data_byte_size =
2346 symtab_load_command.nsyms * nlist_byte_size;
2347 const
addr_t strtab_data_byte_size = symtab_load_command.strsize;
2350 ProcessSP process_sp(m_process_wp.lock());
2351 Process *process = process_sp.get();
2355 bool is_local_shared_cache_image = is_shared_cache_image && !
IsInMemory();
2365 section_list->FindSectionByName(g_segment_name_TEXT));
2367 section_list->FindSectionByName(g_segment_name_DATA));
2369 section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2371 section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2373 section_list->FindSectionByName(g_segment_name_DATA_CONST));
2375 section_list->FindSectionByName(g_segment_name_OBJC));
2378 if (text_section_sp.get()) {
2379 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName(
2380 g_section_name_eh_frame);
2381 lldb_no_nlist_section_sp = text_section_sp->GetChildren().FindSectionByName(
2382 g_section_name_lldb_no_nlist);
2384 eh_frame_section_sp =
2386 lldb_no_nlist_section_sp =
2390 if (process &&
m_header.filetype != llvm::MachO::MH_OBJECT &&
2391 !is_local_shared_cache_image) {
2392 Target &target = process->GetTarget();
2401 if (lldb_no_nlist_section_sp)
2406 if (linkedit_section_sp) {
2407 addr_t linkedit_load_addr =
2408 linkedit_section_sp->GetLoadBaseAddress(&target);
2418 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2419 const addr_t symoff_addr = linkedit_load_addr +
2420 symtab_load_command.
symoff -
2421 linkedit_file_offset;
2422 strtab_addr = linkedit_load_addr + symtab_load_command.
stroff -
2423 linkedit_file_offset;
2431 m_header.filetype == llvm::MachO::MH_DYLINKER) {
2433 ReadMemory(process_sp, symoff_addr, nlist_data_byte_size));
2435 nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2437 const addr_t indirect_syms_addr = linkedit_load_addr +
2439 linkedit_file_offset;
2441 process_sp, indirect_syms_addr, dysymtab.
nindirectsyms * 4));
2442 if (indirect_syms_data_sp)
2443 indirect_symbol_index_data.SetData(
2444 indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2454 if (!is_shared_cache_image) {
2456 ReadMemory(process_sp, strtab_addr, strtab_data_byte_size));
2457 if (strtab_data_sp) {
2458 strtab_data.SetData(strtab_data_sp, 0,
2459 strtab_data_sp->GetByteSize());
2464 if (function_starts_load_command.
cmd) {
2465 const addr_t func_start_addr =
2466 linkedit_load_addr + function_starts_load_command.
dataoff -
2467 linkedit_file_offset;
2470 function_starts_load_command.
datasize));
2471 if (func_start_data_sp)
2472 function_starts_data.SetData(func_start_data_sp, 0,
2473 func_start_data_sp->GetByteSize());
2479 if (is_local_shared_cache_image) {
2487 lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset();
2490 symtab_load_command.
symoff += linkedit_slide;
2491 symtab_load_command.
stroff += linkedit_slide;
2494 function_starts_load_command.
dataoff += linkedit_slide;
2495 exports_trie_load_command.
dataoff += linkedit_slide;
2499 nlist_data_byte_size);
2500 strtab_data = *
m_data_nsp->GetSubsetExtractorSP(symtab_load_command.
stroff,
2501 strtab_data_byte_size);
2506 && (exports_trie_load_command.
datasize > 0)));
2510 }
else if (exports_trie_load_command.
datasize > 0) {
2513 exports_trie_load_command.
datasize);
2517 indirect_symbol_index_data = *
m_data_nsp->GetSubsetExtractorSP(
2520 if (function_starts_load_command.
cmd) {
2521 function_starts_data = *
m_data_nsp->GetSubsetExtractorSP(
2522 function_starts_load_command.
dataoff,
2523 function_starts_load_command.
datasize);
2527 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2529 const bool is_arm = (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2546 if (text_section_sp && function_starts_data.GetByteSize()) {
2547 FunctionStarts::Entry function_start_entry;
2548 function_start_entry.data =
false;
2550 function_start_entry.addr = text_section_sp->GetFileAddress();
2552 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) >
2555 function_start_entry.addr += delta;
2557 if (function_start_entry.addr & 1) {
2559 function_start_entry.data =
true;
2560 }
else if (always_thumb) {
2561 function_start_entry.data =
true;
2564 function_starts.Append(function_start_entry);
2572 if (text_section_sp.get() && eh_frame_section_sp.get() &&
2578 addr_t text_base_addr = text_section_sp->GetFileAddress();
2579 size_t count = functions.
GetSize();
2580 for (
size_t i = 0; i < count; ++i) {
2584 FunctionStarts::Entry function_start_entry;
2585 function_start_entry.addr = func->
base - text_base_addr;
2587 if (function_start_entry.addr & 1) {
2589 function_start_entry.data =
true;
2590 }
else if (always_thumb) {
2591 function_start_entry.data =
true;
2594 function_starts.Append(function_start_entry);
2600 const size_t function_starts_count = function_starts.GetSize();
2615 if (unwind_or_symbol_log)
2616 module_sp->LogMessage(
2617 unwind_or_symbol_log,
2618 "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2621 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get()
2622 ? eh_frame_section_sp->GetID()
2628 std::vector<uint32_t> N_FUN_indexes;
2629 std::vector<uint32_t> N_NSYM_indexes;
2630 std::vector<uint32_t> N_INCL_indexes;
2631 std::vector<uint32_t> N_BRAC_indexes;
2632 std::vector<uint32_t> N_COMM_indexes;
2633 typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap;
2634 typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2635 typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap;
2636 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2637 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2638 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2641 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2642 uint32_t nlist_idx = 0;
2643 Symbol *symbol_ptr =
nullptr;
2645 uint32_t sym_idx = 0;
2647 size_t num_syms = 0;
2648 std::string memory_symbol_name;
2649 uint32_t unmapped_local_symbols_found = 0;
2651 std::vector<TrieEntryWithOffset> reexport_trie_entries;
2652 std::vector<TrieEntryWithOffset> external_sym_trie_entries;
2653 std::set<lldb::addr_t> resolver_addresses;
2655 const size_t dyld_trie_data_size = dyld_trie_data.
GetByteSize();
2656 if (dyld_trie_data_size > 0) {
2657 LLDB_LOG(log,
"Parsing {0} bytes of dyld trie data", dyld_trie_data_size);
2661 if (text_segment_sp)
2662 text_segment_file_addr = text_segment_sp->GetFileAddress();
2663 std::vector<llvm::StringRef> nameSlices;
2665 nameSlices, resolver_addresses, reexport_trie_entries,
2666 external_sym_trie_entries);
2669 typedef std::set<ConstString> IndirectSymbols;
2670 IndirectSymbols indirect_symbol_names;
2693 UUID process_shared_cache_uuid;
2694 addr_t process_shared_cache_base_addr;
2698 process_shared_cache_uuid);
2701 __block
bool found_image =
false;
2702 __block
void *nlist_buffer =
nullptr;
2703 __block
unsigned nlist_count = 0;
2704 __block
char *string_table =
nullptr;
2705 __block vm_offset_t vm_nlist_memory = 0;
2706 __block mach_msg_type_number_t vm_nlist_bytes_read = 0;
2707 __block vm_offset_t vm_string_memory = 0;
2708 __block mach_msg_type_number_t vm_string_bytes_read = 0;
2710 llvm::scope_exit _(^{
2711 if (vm_nlist_memory)
2712 vm_deallocate(
mach_task_self(), vm_nlist_memory, vm_nlist_bytes_read);
2713 if (vm_string_memory)
2714 vm_deallocate(
mach_task_self(), vm_string_memory, vm_string_bytes_read);
2717 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
2718 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
2719 UndefinedNameToDescMap undefined_name_to_desc;
2720 SymbolIndexToName reexport_shlib_needs_fixup;
2722 dyld_for_each_installed_shared_cache(^(dyld_shared_cache_t shared_cache) {
2724 dyld_shared_cache_copy_uuid(shared_cache, &cache_uuid);
2728 if (process_shared_cache_uuid.
IsValid() &&
2729 process_shared_cache_uuid !=
UUID(&cache_uuid, 16))
2732 dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) {
2737 dyld_image_copy_uuid(image, &dsc_image_uuid);
2738 if (image_uuid !=
UUID(dsc_image_uuid, 16))
2745 dyld_image_local_nlist_content_4Symbolication(
2746 image, ^(
const void *nlistStart, uint64_t nlistCount,
2747 const char *stringTable) {
2748 if (!nlistStart || !nlistCount)
2756 nlist_byte_size * nlistCount, &vm_nlist_memory,
2757 &vm_nlist_bytes_read);
2760 assert(vm_nlist_bytes_read == nlist_byte_size * nlistCount);
2765 vm_address_t string_address = (vm_address_t)stringTable;
2766 vm_size_t region_size;
2767 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
2768 vm_region_basic_info_data_t info;
2769 memory_object_name_t object;
2771 ®ion_size, VM_REGION_BASIC_INFO_64,
2772 (vm_region_info_t)&info, &info_count, &
object);
2778 ((vm_address_t)stringTable - string_address),
2779 &vm_string_memory, &vm_string_bytes_read);
2783 nlist_buffer = (
void *)vm_nlist_memory;
2784 string_table = (
char *)vm_string_memory;
2785 nlist_count = nlistCount;
2791 nlist_count * nlist_byte_size,
2792 byte_order, addr_byte_size);
2793 unmapped_local_symbols_found = nlist_count;
2799 unmapped_local_symbols_found -
m_dysymtab.nlocalsym);
2804 for (uint32_t nlist_index = 0;
2805 nlist_index < nlist_count;
2809 std::optional<struct nlist_64> nlist_maybe =
2810 ParseNList(dsc_local_symbols_data, nlist_data_offset,
2814 struct nlist_64 nlist = *nlist_maybe;
2817 const char *symbol_name = string_table + nlist.n_strx;
2819 if (symbol_name == NULL) {
2824 "DSC unmapped local symbol[{0}] has invalid "
2825 "string table offset {1:x} in {2}, ignoring symbol",
2826 nlist_index, nlist.n_strx,
2827 module_sp->GetFileSpec().GetPath()));
2830 if (symbol_name[0] ==
'\0')
2833 const char *symbol_name_non_abi_mangled = NULL;
2836 bool add_nlist =
true;
2837 bool is_debug = ((nlist.n_type & N_STAB) != 0);
2838 bool demangled_is_synthesized =
false;
2839 bool is_gsym =
false;
2840 bool set_value =
true;
2842 assert(sym_idx < num_syms);
2847 switch (nlist.n_type) {
2867 symbol_name, symbol_name_non_abi_mangled,
2869 demangled_is_synthesized =
true;
2871 if (nlist.n_value != 0)
2873 nlist.n_sect, nlist.n_value);
2889 nlist.n_sect, nlist.n_value);
2891 N_FUN_addr_to_sym_idx.insert(
2892 std::make_pair(nlist.n_value, sym_idx));
2896 N_FUN_indexes.push_back(sym_idx);
2900 if (!N_FUN_indexes.empty()) {
2907 N_FUN_indexes.pop_back();
2919 N_STSYM_addr_to_sym_idx.insert(
2920 std::make_pair(nlist.n_value, sym_idx));
2921 symbol_section = section_info.
GetSection(nlist.n_sect,
2923 if (symbol_name && symbol_name[0]) {
2931 symbol_section = section_info.
GetSection(nlist.n_sect,
2965 symbol_section = section_info.
GetSection(nlist.n_sect,
2978 if (symbol_name == NULL) {
2989 N_NSYM_indexes.clear();
2990 N_INCL_indexes.clear();
2991 N_BRAC_indexes.clear();
2992 N_COMM_indexes.clear();
2993 N_FUN_indexes.clear();
2999 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3000 if (N_SO_has_full_path) {
3001 if ((N_SO_index == sym_idx - 1) &&
3002 ((sym_idx - 1) < num_syms)) {
3008 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3014 N_SO_index = sym_idx;
3016 }
else if ((N_SO_index == sym_idx - 1) &&
3017 ((sym_idx - 1) < num_syms)) {
3022 const char *so_path = sym[sym_idx - 1]
3026 if (so_path && so_path[0]) {
3027 std::string full_so_path(so_path);
3028 const size_t double_slash_pos =
3029 full_so_path.find(
"//");
3030 if (double_slash_pos != std::string::npos) {
3041 &full_so_path[double_slash_pos + 1],
3042 FileSpec::Style::native);
3045 full_so_path.erase(0, double_slash_pos + 1);
3049 if (*full_so_path.rbegin() !=
'/')
3050 full_so_path +=
'/';
3051 full_so_path += symbol_name;
3055 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3059 N_SO_index = sym_idx;
3080 N_INCL_indexes.push_back(sym_idx);
3090 if (!N_INCL_indexes.empty()) {
3095 N_INCL_indexes.pop_back();
3130 symbol_section = section_info.
GetSection(nlist.n_sect,
3141 symbol_section = section_info.
GetSection(nlist.n_sect,
3143 N_BRAC_indexes.push_back(sym_idx);
3153 symbol_section = section_info.
GetSection(nlist.n_sect,
3155 if (!N_BRAC_indexes.empty()) {
3160 N_BRAC_indexes.pop_back();
3177 N_COMM_indexes.push_back(sym_idx);
3182 symbol_section = section_info.
GetSection(nlist.n_sect,
3193 if (!N_COMM_indexes.empty()) {
3198 N_COMM_indexes.pop_back();
3213 uint8_t n_type = N_TYPE & nlist.n_type;
3214 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3218 const char *reexport_name_cstr =
3219 strtab_data.PeekCStr(nlist.n_value);
3220 if (reexport_name_cstr && reexport_name_cstr[0]) {
3223 reexport_name_cstr +
3224 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3227 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3229 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
3235 if (symbol_name && symbol_name[0]) {
3237 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0));
3238 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3250 symbol_section = section_info.
GetSection(nlist.n_sect,
3253 if (symbol_section == NULL) {
3259 if (TEXT_eh_frame_sectID == nlist.n_sect) {
3262 uint32_t section_type =
3263 symbol_section->Get() & SECTION_TYPE;
3265 switch (section_type) {
3266 case S_CSTRING_LITERALS:
3269 case S_4BYTE_LITERALS:
3272 case S_8BYTE_LITERALS:
3275 case S_LITERAL_POINTERS:
3278 case S_NON_LAZY_SYMBOL_POINTERS:
3282 case S_LAZY_SYMBOL_POINTERS:
3285 case S_SYMBOL_STUBS:
3289 case S_MOD_INIT_FUNC_POINTERS:
3293 case S_MOD_TERM_FUNC_POINTERS:
3301 case S_16BYTE_LITERALS:
3307 case S_LAZY_DYLIB_SYMBOL_POINTERS:
3311 switch (symbol_section->GetType()) {
3338 const char *symbol_sect_name =
3339 symbol_section->GetName().AsCString();
3340 if (symbol_section->IsDescendant(
3341 text_section_sp.get())) {
3342 if (symbol_section->IsClear(
3343 S_ATTR_PURE_INSTRUCTIONS |
3344 S_ATTR_SELF_MODIFYING_CODE |
3345 S_ATTR_SOME_INSTRUCTIONS))
3349 }
else if (symbol_section->IsDescendant(
3350 data_section_sp.get()) ||
3351 symbol_section->IsDescendant(
3352 data_dirty_section_sp.get()) ||
3353 symbol_section->IsDescendant(
3354 data_const_section_sp.get())) {
3355 if (symbol_sect_name &&
3356 ::strstr(symbol_sect_name,
"__objc") ==
3362 symbol_name_non_abi_mangled, type))
3363 demangled_is_synthesized =
true;
3364 }
else if (symbol_sect_name &&
3365 ::strstr(symbol_sect_name,
3366 "__gcc_except_tab") ==
3372 }
else if (symbol_sect_name &&
3373 ::strstr(symbol_sect_name,
"__IMPORT") ==
3376 }
else if (symbol_section->IsDescendant(
3377 objc_section_sp.get())) {
3379 if (symbol_name && symbol_name[0] ==
'.') {
3380 llvm::StringRef symbol_name_ref(symbol_name);
3382 g_objc_v1_prefix_class(
".objc_class_name_");
3383 if (symbol_name_ref.starts_with(
3384 g_objc_v1_prefix_class)) {
3385 symbol_name_non_abi_mangled = symbol_name;
3386 symbol_name = symbol_name +
3387 g_objc_v1_prefix_class.size();
3389 demangled_is_synthesized =
true;
3400 uint64_t symbol_value = nlist.n_value;
3401 if (symbol_name_non_abi_mangled) {
3407 if (symbol_name && symbol_name[0] ==
'_') {
3414 if (is_gsym && is_debug) {
3415 const char *gsym_name =
3421 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3425 if (symbol_section) {
3426 const addr_t section_file_addr =
3427 symbol_section->GetFileAddress();
3428 symbol_value -= section_file_addr;
3431 if (is_debug ==
false) {
3439 N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3440 if (range.first != range.second) {
3441 bool found_it =
false;
3442 for (
auto pos = range.first; pos != range.second;
3444 if (sym[sym_idx].GetMangled().
GetName(
3448 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3454 sym[sym_idx].IsExternal());
3455 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3457 if (resolver_addresses.find(nlist.n_value) !=
3458 resolver_addresses.end())
3460 sym[sym_idx].
Clear();
3468 if (resolver_addresses.find(nlist.n_value) !=
3469 resolver_addresses.end())
3481 auto range = N_STSYM_addr_to_sym_idx.equal_range(
3483 if (range.first != range.second) {
3484 bool found_it =
false;
3485 for (
auto pos = range.first; pos != range.second;
3487 if (sym[sym_idx].GetMangled().
GetName(
3491 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3497 sym[sym_idx].IsExternal());
3498 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3500 sym[sym_idx].
Clear();
3508 const char *gsym_name =
3516 ConstNameToSymbolIndexMap::const_iterator pos =
3517 N_GSYM_name_to_sym_idx.find(gsym_name);
3518 if (pos != N_GSYM_name_to_sym_idx.end()) {
3519 const uint32_t GSYM_sym_idx = pos->second;
3520 m_nlist_idx_to_sym_idx[nlist_idx] =
3529 add_symbol_addr(sym[GSYM_sym_idx]
3536 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 |
3538 sym[sym_idx].
Clear();
3546 sym[sym_idx].
SetID(nlist_idx);
3552 sym[sym_idx].GetAddress().GetFileAddress());
3554 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
3556 if (demangled_is_synthesized)
3560 sym[sym_idx].
Clear();
3567 for (
const auto &pos : reexport_shlib_needs_fixup) {
3568 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3569 if (undef_pos != undefined_name_to_desc.end()) {
3570 const uint8_t dylib_ordinal =
3571 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3572 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
3582 if (nlist_data.GetByteSize() > 0) {
3586 if (sym ==
nullptr) {
3592 if (unmapped_local_symbols_found) {
3594 nlist_data_offset += (
m_dysymtab.nlocalsym * nlist_byte_size);
3600 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
3601 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
3602 UndefinedNameToDescMap undefined_name_to_desc;
3603 SymbolIndexToName reexport_shlib_needs_fixup;
3611 auto ParseSymbolLambda = [&](
struct nlist_64 &nlist, uint32_t nlist_idx,
3613 const bool is_debug = ((nlist.n_type & N_STAB) != 0);
3614 if (is_debug != debug_only)
3617 const char *symbol_name_non_abi_mangled =
nullptr;
3618 const char *symbol_name =
nullptr;
3620 if (have_strtab_data) {
3621 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3623 if (symbol_name ==
nullptr) {
3627 "symbol[{0}] has invalid string table offset {1:x} in {2}, "
3629 nlist_idx, nlist.n_strx, module_sp->GetFileSpec().GetPath()));
3632 if (symbol_name[0] ==
'\0')
3633 symbol_name =
nullptr;
3635 const addr_t str_addr = strtab_addr + nlist.n_strx;
3637 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name,
3639 symbol_name = memory_symbol_name.c_str();
3644 bool add_nlist =
true;
3645 bool is_gsym =
false;
3646 bool demangled_is_synthesized =
false;
3647 bool set_value =
true;
3649 assert(sym_idx < num_syms);
3653 switch (nlist.n_type) {
3671 symbol_name_non_abi_mangled, type)) {
3672 demangled_is_synthesized =
true;
3674 if (nlist.n_value != 0)
3676 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3692 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3694 N_FUN_addr_to_sym_idx.insert(
3695 std::make_pair(nlist.n_value, sym_idx));
3699 N_FUN_indexes.push_back(sym_idx);
3703 if (!N_FUN_indexes.empty()) {
3708 N_FUN_indexes.pop_back();
3719 N_STSYM_addr_to_sym_idx.insert(
3720 std::make_pair(nlist.n_value, sym_idx));
3721 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3722 if (symbol_name && symbol_name[0]) {
3730 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3761 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3773 if (symbol_name ==
nullptr) {
3784 N_NSYM_indexes.clear();
3785 N_INCL_indexes.clear();
3786 N_BRAC_indexes.clear();
3787 N_COMM_indexes.clear();
3788 N_FUN_indexes.clear();
3794 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3795 if (N_SO_has_full_path) {
3796 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) {
3801 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3806 N_SO_index = sym_idx;
3808 }
else if ((N_SO_index == sym_idx - 1) &&
3809 ((sym_idx - 1) < num_syms)) {
3813 const char *so_path =
3815 if (so_path && so_path[0]) {
3816 std::string full_so_path(so_path);
3817 const size_t double_slash_pos = full_so_path.find(
"//");
3818 if (double_slash_pos != std::string::npos) {
3826 so_dir.
SetFile(&full_so_path[double_slash_pos + 1],
3827 FileSpec::Style::native);
3830 full_so_path.erase(0, double_slash_pos + 1);
3834 if (*full_so_path.rbegin() !=
'/')
3835 full_so_path +=
'/';
3836 full_so_path += symbol_name;
3840 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3844 N_SO_index = sym_idx;
3864 N_INCL_indexes.push_back(sym_idx);
3873 if (!N_INCL_indexes.empty()) {
3877 N_INCL_indexes.pop_back();
3912 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3921 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3922 N_BRAC_indexes.push_back(sym_idx);
3931 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3932 if (!N_BRAC_indexes.empty()) {
3936 N_BRAC_indexes.pop_back();
3952 N_COMM_indexes.push_back(sym_idx);
3957 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3966 if (!N_COMM_indexes.empty()) {
3970 N_COMM_indexes.pop_back();
3984 uint8_t n_type = N_TYPE & nlist.n_type;
3985 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3989 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3990 if (reexport_name_cstr && reexport_name_cstr[0] && symbol_name) {
3993 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3996 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3997 indirect_symbol_names.insert(
3998 ConstString(symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
4004 if (symbol_name && symbol_name[0]) {
4006 ((symbol_name[0] ==
'_') ? 1 : 0));
4007 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4020 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4022 if (!symbol_section) {
4028 if (TEXT_eh_frame_sectID == nlist.n_sect) {
4031 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
4033 switch (section_type) {
4034 case S_CSTRING_LITERALS:
4037 case S_4BYTE_LITERALS:
4040 case S_8BYTE_LITERALS:
4043 case S_LITERAL_POINTERS:
4046 case S_NON_LAZY_SYMBOL_POINTERS:
4049 case S_LAZY_SYMBOL_POINTERS:
4052 case S_SYMBOL_STUBS:
4056 case S_MOD_INIT_FUNC_POINTERS:
4059 case S_MOD_TERM_FUNC_POINTERS:
4066 case S_16BYTE_LITERALS:
4072 case S_LAZY_DYLIB_SYMBOL_POINTERS:
4076 switch (symbol_section->GetType()) {
4098 const char *symbol_sect_name =
4099 symbol_section->GetName().AsCString();
4100 if (symbol_section->IsDescendant(text_section_sp.get())) {
4101 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4102 S_ATTR_SELF_MODIFYING_CODE |
4103 S_ATTR_SOME_INSTRUCTIONS))
4107 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
4108 symbol_section->IsDescendant(
4109 data_dirty_section_sp.get()) ||
4110 symbol_section->IsDescendant(
4111 data_const_section_sp.get())) {
4112 if (symbol_sect_name &&
4113 ::strstr(symbol_sect_name,
"__objc") == symbol_sect_name) {
4117 symbol_name, symbol_name_non_abi_mangled, type))
4118 demangled_is_synthesized =
true;
4119 }
else if (symbol_sect_name &&
4120 ::strstr(symbol_sect_name,
"__gcc_except_tab") ==
4126 }
else if (symbol_sect_name &&
4127 ::strstr(symbol_sect_name,
"__IMPORT") ==
4130 }
else if (symbol_section->IsDescendant(objc_section_sp.get())) {
4132 if (symbol_name && symbol_name[0] ==
'.') {
4133 llvm::StringRef symbol_name_ref(symbol_name);
4134 llvm::StringRef g_objc_v1_prefix_class(
4135 ".objc_class_name_");
4136 if (symbol_name_ref.starts_with(g_objc_v1_prefix_class)) {
4137 symbol_name_non_abi_mangled = symbol_name;
4138 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4140 demangled_is_synthesized =
true;
4151 sym[sym_idx].
Clear();
4155 uint64_t symbol_value = nlist.n_value;
4157 if (symbol_name_non_abi_mangled) {
4163 if (symbol_name && symbol_name[0] ==
'_') {
4174 const char *gsym_name = sym[sym_idx]
4179 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4182 if (symbol_section) {
4183 const addr_t section_file_addr = symbol_section->GetFileAddress();
4184 symbol_value -= section_file_addr;
4193 std::pair<ValueToSymbolIndexMap::const_iterator,
4194 ValueToSymbolIndexMap::const_iterator>
4196 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4197 if (range.first != range.second) {
4198 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4199 pos != range.second; ++pos) {
4203 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4207 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4208 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4209 if (resolver_addresses.find(nlist.n_value) !=
4210 resolver_addresses.end())
4212 sym[sym_idx].
Clear();
4217 if (resolver_addresses.find(nlist.n_value) !=
4218 resolver_addresses.end())
4228 std::pair<ValueToSymbolIndexMap::const_iterator,
4229 ValueToSymbolIndexMap::const_iterator>
4231 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4232 if (range.first != range.second) {
4233 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4234 pos != range.second; ++pos) {
4238 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4242 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4243 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4244 sym[sym_idx].
Clear();
4250 const char *gsym_name = sym[sym_idx]
4255 ConstNameToSymbolIndexMap::const_iterator pos =
4256 N_GSYM_name_to_sym_idx.find(gsym_name);
4257 if (pos != N_GSYM_name_to_sym_idx.end()) {
4258 const uint32_t GSYM_sym_idx = pos->second;
4259 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4265 sym[GSYM_sym_idx].GetAddress().GetFileAddress());
4269 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4270 sym[sym_idx].
Clear();
4278 sym[sym_idx].
SetID(nlist_idx);
4284 add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
4286 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4287 if (nlist.n_desc & N_WEAK_REF)
4290 if (demangled_is_synthesized)
4299 std::vector<struct nlist_64> nlists;
4300 nlists.reserve(symtab_load_command.
nsyms);
4301 for (; nlist_idx < symtab_load_command.
nsyms; ++nlist_idx) {
4303 ParseNList(nlist_data, nlist_data_offset, nlist_byte_size))
4304 nlists.push_back(*nlist);
4314 for (
auto &nlist : nlists) {
4315 if (!ParseSymbolLambda(nlist, nlist_idx++,
DebugSymbols))
4321 for (
auto &nlist : nlists) {
4326 for (
const auto &pos : reexport_shlib_needs_fixup) {
4327 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4328 if (undef_pos != undefined_name_to_desc.end()) {
4329 const uint8_t dylib_ordinal =
4330 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4331 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
4339 int trie_symbol_table_augment_count = 0;
4340 for (
auto &e : external_sym_trie_entries) {
4341 if (!symbols_added.contains(e.entry.address))
4342 trie_symbol_table_augment_count++;
4345 if (num_syms < sym_idx + trie_symbol_table_augment_count) {
4346 num_syms = sym_idx + trie_symbol_table_augment_count;
4347 sym = symtab.
Resize(num_syms);
4349 uint32_t synthetic_sym_id = symtab_load_command.
nsyms;
4352 for (
auto &e : external_sym_trie_entries) {
4353 if (symbols_added.contains(e.entry.address))
4359 if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) {
4361 const char *symbol_name = e.entry.name.GetCString();
4362 bool demangled_is_synthesized =
false;
4364 GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp,
4365 data_section_sp, data_dirty_section_sp,
4366 data_const_section_sp, symbol_section);
4369 if (symbol_section) {
4370 sym[sym_idx].
SetID(synthetic_sym_id++);
4372 if (demangled_is_synthesized)
4385 if (function_starts_count > 0) {
4386 uint32_t num_synthetic_function_symbols = 0;
4387 for (i = 0; i < function_starts_count; ++i) {
4388 if (!symbols_added.contains(function_starts.GetEntryRef(i).addr))
4389 ++num_synthetic_function_symbols;
4392 if (num_synthetic_function_symbols > 0) {
4393 if (num_syms < sym_idx + num_synthetic_function_symbols) {
4394 num_syms = sym_idx + num_synthetic_function_symbols;
4395 sym = symtab.
Resize(num_syms);
4397 for (i = 0; i < function_starts_count; ++i) {
4398 const FunctionStarts::Entry *func_start_entry =
4399 function_starts.GetEntryAtIndex(i);
4400 if (!symbols_added.contains(func_start_entry->addr)) {
4401 addr_t symbol_file_addr = func_start_entry->addr;
4402 uint32_t symbol_flags = 0;
4403 if (func_start_entry->data)
4406 if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) {
4408 if (symbol_section) {
4409 sym[sym_idx].
SetID(synthetic_sym_id++);
4419 sym[sym_idx].
SetFlags(symbol_flags);
4430 if (sym_idx < num_syms) {
4432 sym = symtab.
Resize(num_syms);
4437 if (indirect_symbol_index_data.GetByteSize()) {
4438 NListIndexToSymbolIndexMap::const_iterator end_index_pos =
4439 m_nlist_idx_to_sym_idx.end();
4446 if (symbol_stub_byte_size == 0)
4449 const uint32_t num_symbol_stubs =
4452 if (num_symbol_stubs == 0)
4455 const uint32_t symbol_stub_index_offset =
4457 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) {
4458 const uint32_t symbol_stub_index =
4459 symbol_stub_index_offset + stub_idx;
4462 (stub_idx * symbol_stub_byte_size);
4464 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(
4465 symbol_stub_offset, 4)) {
4466 const uint32_t stub_sym_id =
4467 indirect_symbol_index_data.GetU32(&symbol_stub_offset);
4468 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4471 NListIndexToSymbolIndexMap::const_iterator index_pos =
4472 m_nlist_idx_to_sym_idx.find(stub_sym_id);
4473 Symbol *stub_symbol =
nullptr;
4474 if (index_pos != end_index_pos) {
4485 Address so_addr(symbol_stub_addr, section_list);
4492 if (resolver_addresses.find(symbol_stub_addr) ==
4493 resolver_addresses.end())
4503 if (sym_idx >= num_syms) {
4504 sym = symtab.
Resize(++num_syms);
4505 stub_symbol =
nullptr;
4507 sym[sym_idx].
SetID(synthetic_sym_id++);
4508 sym[sym_idx].
GetMangled() = stub_symbol_mangled_name;
4509 if (resolver_addresses.find(symbol_stub_addr) ==
4510 resolver_addresses.end())
4522 log->
Warning(
"symbol stub referencing symbol table symbol "
4523 "%u that isn't in our minimal symbol table, "
4534 if (!reexport_trie_entries.empty()) {
4535 for (
const auto &e : reexport_trie_entries) {
4536 if (e.entry.import_name) {
4539 if (indirect_symbol_names.find(e.entry.name) ==
4540 indirect_symbol_names.end()) {
4542 if (sym_idx >= num_syms)
4543 sym = symtab.
Resize(++num_syms);
4544 sym[sym_idx].
SetID(synthetic_sym_id++);
4549 if (e.entry.other > 0 && e.entry.other <= dylib_files.
GetSize()) {
6342 Target &target = process_sp->GetTarget();
6344 const llvm::Triple &target_triple = target_arch.
GetTriple();
6345 if (target_triple.getVendor() == llvm::Triple::Apple &&
6346 (target_triple.getOS() == llvm::Triple::MacOSX ||
6347 target_triple.getOS() == llvm::Triple::IOS ||
6348 target_triple.getOS() == llvm::Triple::WatchOS ||
6349 target_triple.getOS() == llvm::Triple::TvOS ||
6350 target_triple.getOS() == llvm::Triple::BridgeOS ||
6351 target_triple.getOS() == llvm::Triple::XROS)) {
6352 bool make_core =
false;
6354 case llvm::Triple::aarch64:
6355 case llvm::Triple::aarch64_32:
6356 case llvm::Triple::arm:
6357 case llvm::Triple::thumb:
6358 case llvm::Triple::x86:
6359 case llvm::Triple::x86_64:
6364 "unsupported core architecture: %s", target_triple.str().c_str());
6370 error = process_sp->CalculateCoreFileSaveRanges(options, core_ranges);
6371 if (
error.Success()) {
6374 std::vector<llvm::MachO::segment_command_64> segment_load_commands;
6375 for (
const auto &core_range_info : core_ranges) {
6377 const auto &core_range = core_range_info.data;
6378 uint32_t cmd_type = LC_SEGMENT_64;
6379 uint32_t segment_size =
sizeof(llvm::MachO::segment_command_64);
6380 if (addr_byte_size == 4) {
6381 cmd_type = LC_SEGMENT;
6382 segment_size =
sizeof(llvm::MachO::segment_command);
6386 if (core_range.lldb_permissions == 0 || core_range.range.size() == 0)
6388 uint32_t vm_prot = 0;
6389 if (core_range.lldb_permissions & ePermissionsReadable)
6390 vm_prot |= VM_PROT_READ;
6391 if (core_range.lldb_permissions & ePermissionsWritable)
6392 vm_prot |= VM_PROT_WRITE;
6393 if (core_range.lldb_permissions & ePermissionsExecutable)
6394 vm_prot |= VM_PROT_EXECUTE;
6395 const addr_t vm_addr = core_range.range.start();
6396 const addr_t vm_size = core_range.range.size();
6397 llvm::MachO::segment_command_64 segment = {
6409 segment_load_commands.push_back(segment);
6414 llvm::MachO::mach_header_64 mach_header;
6415 mach_header.magic = addr_byte_size == 8 ? MH_MAGIC_64 : MH_MAGIC;
6418 mach_header.filetype = MH_CORE;
6419 mach_header.ncmds = segment_load_commands.size();
6420 mach_header.flags = 0;
6421 mach_header.reserved = 0;
6422 ThreadList &thread_list = process_sp->GetThreadList();
6423 const uint32_t num_threads = thread_list.
GetSize();
6429 std::vector<StreamString> LC_THREAD_datas(num_threads);
6430 for (
auto &LC_THREAD_data : LC_THREAD_datas) {
6432 LC_THREAD_data.SetAddressByteSize(addr_byte_size);
6433 LC_THREAD_data.SetByteOrder(byte_order);
6435 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
6438 switch (mach_header.cputype) {
6439 case llvm::MachO::CPU_TYPE_ARM64:
6440 case llvm::MachO::CPU_TYPE_ARM64_32:
6442 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6445 case llvm::MachO::CPU_TYPE_ARM:
6447 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6450 case llvm::MachO::CPU_TYPE_X86_64:
6452 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6455 case llvm::MachO::CPU_TYPE_RISCV:
6457 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6464 if (addr_byte_size == 8) {
6465 mach_header.sizeofcmds = segment_load_commands.size() *
6466 sizeof(llvm::MachO::segment_command_64);
6468 mach_header.sizeofcmds = segment_load_commands.size() *
6469 sizeof(llvm::MachO::segment_command);
6473 for (
const auto &LC_THREAD_data : LC_THREAD_datas) {
6474 ++mach_header.ncmds;
6475 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
6480 uint64_t address_mask = process_sp->GetCodeAddressMask();
6483 mach_header.ncmds++;
6484 mach_header.sizeofcmds +=
sizeof(llvm::MachO::note_command);
6488 mach_header.ncmds++;
6489 mach_header.sizeofcmds +=
sizeof(llvm::MachO::note_command);
6492 mach_header.ncmds++;
6493 mach_header.sizeofcmds +=
sizeof(llvm::MachO::note_command);
6496 buffer.
PutHex32(mach_header.magic);
6497 buffer.
PutHex32(mach_header.cputype);
6498 buffer.
PutHex32(mach_header.cpusubtype);
6499 buffer.
PutHex32(mach_header.filetype);
6500 buffer.
PutHex32(mach_header.ncmds);
6501 buffer.
PutHex32(mach_header.sizeofcmds);
6502 buffer.
PutHex32(mach_header.flags);
6503 if (addr_byte_size == 8) {
6504 buffer.
PutHex32(mach_header.reserved);
6509 addr_t file_offset = buffer.
GetSize() + mach_header.sizeofcmds;
6511 file_offset = llvm::alignTo(file_offset, 16);
6512 std::vector<std::unique_ptr<LCNoteEntry>> lc_notes;
6516 std::unique_ptr<LCNoteEntry> addrable_bits_lcnote_up(
6518 addrable_bits_lcnote_up->name =
"addrable bits";
6519 addrable_bits_lcnote_up->payload_file_offset = file_offset;
6520 int bits = std::bitset<64>(~address_mask).count();
6521 addrable_bits_lcnote_up->payload.PutHex32(4);
6522 addrable_bits_lcnote_up->payload.PutHex32(
6524 addrable_bits_lcnote_up->payload.PutHex32(
6526 addrable_bits_lcnote_up->payload.PutHex32(0);
6528 file_offset += addrable_bits_lcnote_up->payload.GetSize();
6530 lc_notes.push_back(std::move(addrable_bits_lcnote_up));
6534 std::unique_ptr<LCNoteEntry> thread_extrainfo_lcnote_up(
6536 thread_extrainfo_lcnote_up->name =
"process metadata";
6537 thread_extrainfo_lcnote_up->payload_file_offset = file_offset;
6540 std::make_shared<StructuredData::Dictionary>());
6542 std::make_shared<StructuredData::Array>());
6544 process_sp->CalculateCoreFileThreadList(options)) {
6546 std::make_shared<StructuredData::Dictionary>());
6547 thread->AddIntegerItem(
"thread_id", thread_sp->GetID());
6548 threads->AddItem(thread);
6550 dict->AddItem(
"threads", threads);
6552 dict->Dump(strm,
false);
6553 thread_extrainfo_lcnote_up->payload.PutRawBytes(strm.
GetData(),
6556 file_offset += thread_extrainfo_lcnote_up->payload.GetSize();
6557 file_offset = llvm::alignTo(file_offset, 16);
6558 lc_notes.push_back(std::move(thread_extrainfo_lcnote_up));
6561 std::unique_ptr<LCNoteEntry> all_image_infos_lcnote_up(
6563 all_image_infos_lcnote_up->name =
"all image infos";
6564 all_image_infos_lcnote_up->payload_file_offset = file_offset;
6566 process_sp, file_offset, all_image_infos_lcnote_up->payload,
6568 lc_notes.push_back(std::move(all_image_infos_lcnote_up));
6571 for (
auto &lcnote : lc_notes) {
6574 buffer.
PutHex32(
sizeof(llvm::MachO::note_command));
6576 memset(namebuf, 0,
sizeof(namebuf));
6582 strncpy(namebuf, lcnote->name.c_str(),
sizeof(namebuf));
6584 buffer.
PutHex64(lcnote->payload_file_offset);
6585 buffer.
PutHex64(lcnote->payload.GetSize());
6589 file_offset = llvm::alignTo(file_offset, 4096);
6591 for (
auto &segment : segment_load_commands) {
6592 segment.fileoff = file_offset;
6593 file_offset += segment.filesize;
6597 for (
const auto &LC_THREAD_data : LC_THREAD_datas) {
6598 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
6600 buffer.
PutHex32(8 + LC_THREAD_data_size);
6601 buffer.
Write(LC_THREAD_data.GetString().data(), LC_THREAD_data_size);
6605 for (
const auto &segment : segment_load_commands) {
6608 buffer.
PutRawBytes(segment.segname,
sizeof(segment.segname));
6609 if (addr_byte_size == 8) {
6615 buffer.
PutHex32(
static_cast<uint32_t
>(segment.vmaddr));
6616 buffer.
PutHex32(
static_cast<uint32_t
>(segment.vmsize));
6617 buffer.
PutHex32(
static_cast<uint32_t
>(segment.fileoff));
6618 buffer.
PutHex32(
static_cast<uint32_t
>(segment.filesize));
6626 std::string core_file_path(outfile.
GetPath());
6634 uint8_t bytes[0x1000];
6636 size_t bytes_written = buffer.
GetString().size();
6638 core_file.get()->Write(buffer.
GetString().data(), bytes_written);
6639 if (
error.Success()) {
6641 for (
auto &lcnote : lc_notes) {
6642 if (core_file.get()->SeekFromStart(lcnote->payload_file_offset) ==
6645 "Unable to seek to corefile pos "
6646 "to write '%s' LC_NOTE payload",
6647 lcnote->name.c_str());
6650 bytes_written = lcnote->payload.GetSize();
6651 error = core_file.get()->Write(lcnote->payload.GetData(),
6653 if (!
error.Success())
6658 for (
const auto &segment : segment_load_commands) {
6659 if (core_file.get()->SeekFromStart(segment.fileoff) == -1) {
6661 "unable to seek to offset 0x%" PRIx64
" in '%s'",
6662 segment.fileoff, core_file_path.c_str());
6668 " bytes of data for memory region at 0x%" PRIx64
"\n",
6669 segment.vmsize, segment.vmaddr);
6670 addr_t bytes_left = segment.vmsize;
6671 addr_t addr = segment.vmaddr;
6673 while (bytes_left > 0 &&
error.Success()) {
6674 const size_t bytes_to_read =
6675 bytes_left >
sizeof(bytes) ?
sizeof(bytes) : bytes_left;
6680 const size_t bytes_read = process_sp->ReadMemoryFromInferior(
6683 if (bytes_read == bytes_to_read) {
6684 size_t bytes_written = bytes_read;
6685 error = core_file.get()->Write(bytes, bytes_written);
6686 bytes_left -= bytes_read;
6691 memset(bytes, 0, bytes_to_read);
6692 size_t bytes_written = bytes_to_read;
6693 error = core_file.get()->Write(bytes, bytes_written);
6694 bytes_left -= bytes_to_read;
6695 addr += bytes_to_read;