2214 LLDB_LOG(log,
"Parsing symbol table for {0}", file_name);
2215 Progress progress(
"Parsing symbol table", file_name);
2230 llvm::DenseSet<addr_t> symbols_added;
2234 auto add_symbol_addr = [&symbols_added](
lldb::addr_t file_addr) {
2238 symbols_added.insert(file_addr);
2240 FunctionStarts function_starts;
2246 for (i = 0; i <
m_header.ncmds; ++i) {
2249 llvm::MachO::load_command lc;
2250 if (
m_data_nsp->GetU32(&offset, &lc, 2) ==
nullptr)
2255 llvm::MachO::symtab_command lc_obj;
2256 if (
m_data_nsp->GetU32(&offset, &lc_obj.symoff, 4)) {
2257 lc_obj.cmd = lc.cmd;
2258 lc_obj.cmdsize = lc.cmdsize;
2259 symtab_load_command = lc_obj;
2264 case LC_DYLD_INFO_ONLY: {
2265 llvm::MachO::dyld_info_command lc_obj;
2266 if (
m_data_nsp->GetU32(&offset, &lc_obj.rebase_off, 10)) {
2267 lc_obj.cmd = lc.cmd;
2268 lc_obj.cmdsize = lc.cmdsize;
2274 case LC_LOAD_WEAK_DYLIB:
2275 case LC_REEXPORT_DYLIB:
2277 case LC_LOAD_UPWARD_DYLIB: {
2278 uint32_t name_offset = cmd_offset +
m_data_nsp->GetU32(&offset);
2279 const char *path =
m_data_nsp->PeekCStr(name_offset);
2287 if (lc.cmd == LC_REEXPORT_DYLIB) {
2291 dylib_files.
Append(file_spec);
2295 case LC_DYLD_EXPORTS_TRIE: {
2296 llvm::MachO::linkedit_data_command lc_obj;
2297 lc_obj.cmd = lc.cmd;
2298 lc_obj.cmdsize = lc.cmdsize;
2299 if (
m_data_nsp->GetU32(&offset, &lc_obj.dataoff, 2))
2300 exports_trie_load_command = lc_obj;
2302 case LC_FUNCTION_STARTS: {
2303 llvm::MachO::linkedit_data_command lc_obj;
2304 lc_obj.cmd = lc.cmd;
2305 lc_obj.cmdsize = lc.cmdsize;
2306 if (
m_data_nsp->GetU32(&offset, &lc_obj.dataoff, 2))
2307 function_starts_load_command = lc_obj;
2311 const uint8_t *uuid_bytes =
m_data_nsp->PeekData(offset, 16);
2314 image_uuid =
UUID(uuid_bytes, 16);
2321 offset = cmd_offset + lc.cmdsize;
2324 if (!symtab_load_command.
cmd)
2328 if (section_list ==
nullptr)
2331 const uint32_t addr_byte_size =
m_data_nsp->GetAddressByteSize();
2333 bool bit_width_32 = addr_byte_size == 4;
2334 const size_t nlist_byte_size =
2335 bit_width_32 ?
sizeof(
struct nlist) : sizeof(struct nlist_64);
2337 DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size);
2338 DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size);
2339 DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size);
2340 DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order,
2342 DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size);
2344 const
addr_t nlist_data_byte_size =
2345 symtab_load_command.nsyms * nlist_byte_size;
2346 const
addr_t strtab_data_byte_size = symtab_load_command.strsize;
2349 ProcessSP process_sp(m_process_wp.lock());
2350 Process *process = process_sp.get();
2354 bool is_local_shared_cache_image = is_shared_cache_image && !
IsInMemory();
2364 section_list->FindSectionByName(g_segment_name_TEXT));
2366 section_list->FindSectionByName(g_segment_name_DATA));
2368 section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2370 section_list->FindSectionByName(g_segment_name_DATA_DIRTY));
2372 section_list->FindSectionByName(g_segment_name_DATA_CONST));
2374 section_list->FindSectionByName(g_segment_name_OBJC));
2377 if (text_section_sp.get()) {
2378 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName(
2379 g_section_name_eh_frame);
2380 lldb_no_nlist_section_sp = text_section_sp->GetChildren().FindSectionByName(
2381 g_section_name_lldb_no_nlist);
2383 eh_frame_section_sp =
2385 lldb_no_nlist_section_sp =
2389 if (process &&
m_header.filetype != llvm::MachO::MH_OBJECT &&
2390 !is_local_shared_cache_image) {
2391 Target &target = process->GetTarget();
2400 if (lldb_no_nlist_section_sp)
2405 if (linkedit_section_sp) {
2406 addr_t linkedit_load_addr =
2407 linkedit_section_sp->GetLoadBaseAddress(&target);
2417 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2418 const addr_t symoff_addr = linkedit_load_addr +
2419 symtab_load_command.
symoff -
2420 linkedit_file_offset;
2421 strtab_addr = linkedit_load_addr + symtab_load_command.
stroff -
2422 linkedit_file_offset;
2430 m_header.filetype == llvm::MachO::MH_DYLINKER) {
2432 ReadMemory(process_sp, symoff_addr, nlist_data_byte_size));
2434 nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2436 const addr_t indirect_syms_addr = linkedit_load_addr +
2438 linkedit_file_offset;
2440 process_sp, indirect_syms_addr, dysymtab.
nindirectsyms * 4));
2441 if (indirect_syms_data_sp)
2442 indirect_symbol_index_data.SetData(
2443 indirect_syms_data_sp, 0, indirect_syms_data_sp->GetByteSize());
2453 if (!is_shared_cache_image) {
2455 ReadMemory(process_sp, strtab_addr, strtab_data_byte_size));
2456 if (strtab_data_sp) {
2457 strtab_data.SetData(strtab_data_sp, 0,
2458 strtab_data_sp->GetByteSize());
2463 if (function_starts_load_command.
cmd) {
2464 const addr_t func_start_addr =
2465 linkedit_load_addr + function_starts_load_command.
dataoff -
2466 linkedit_file_offset;
2469 function_starts_load_command.
datasize));
2470 if (func_start_data_sp)
2471 function_starts_data.SetData(func_start_data_sp, 0,
2472 func_start_data_sp->GetByteSize());
2478 if (is_local_shared_cache_image) {
2486 lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset();
2489 symtab_load_command.
symoff += linkedit_slide;
2490 symtab_load_command.
stroff += linkedit_slide;
2493 function_starts_load_command.
dataoff += linkedit_slide;
2494 exports_trie_load_command.
dataoff += linkedit_slide;
2498 nlist_data_byte_size);
2499 strtab_data = *
m_data_nsp->GetSubsetExtractorSP(symtab_load_command.
stroff,
2500 strtab_data_byte_size);
2505 && (exports_trie_load_command.
datasize > 0)));
2509 }
else if (exports_trie_load_command.
datasize > 0) {
2512 exports_trie_load_command.
datasize);
2516 indirect_symbol_index_data = *
m_data_nsp->GetSubsetExtractorSP(
2519 if (function_starts_load_command.
cmd) {
2520 function_starts_data = *
m_data_nsp->GetSubsetExtractorSP(
2521 function_starts_load_command.
dataoff,
2522 function_starts_load_command.
datasize);
2526 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2528 const bool is_arm = (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2545 if (text_section_sp && function_starts_data.GetByteSize()) {
2546 FunctionStarts::Entry function_start_entry;
2547 function_start_entry.data =
false;
2549 function_start_entry.addr = text_section_sp->GetFileAddress();
2551 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) >
2554 function_start_entry.addr += delta;
2556 if (function_start_entry.addr & 1) {
2558 function_start_entry.data =
true;
2559 }
else if (always_thumb) {
2560 function_start_entry.data =
true;
2563 function_starts.Append(function_start_entry);
2571 if (text_section_sp.get() && eh_frame_section_sp.get() &&
2577 addr_t text_base_addr = text_section_sp->GetFileAddress();
2578 size_t count = functions.
GetSize();
2579 for (
size_t i = 0; i < count; ++i) {
2583 FunctionStarts::Entry function_start_entry;
2584 function_start_entry.addr = func->
base - text_base_addr;
2586 if (function_start_entry.addr & 1) {
2588 function_start_entry.data =
true;
2589 }
else if (always_thumb) {
2590 function_start_entry.data =
true;
2593 function_starts.Append(function_start_entry);
2599 const size_t function_starts_count = function_starts.GetSize();
2614 if (unwind_or_symbol_log)
2615 module_sp->LogMessage(
2616 unwind_or_symbol_log,
2617 "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2620 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get()
2621 ? eh_frame_section_sp->GetID()
2627 std::vector<uint32_t> N_FUN_indexes;
2628 std::vector<uint32_t> N_NSYM_indexes;
2629 std::vector<uint32_t> N_INCL_indexes;
2630 std::vector<uint32_t> N_BRAC_indexes;
2631 std::vector<uint32_t> N_COMM_indexes;
2632 typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap;
2633 typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2634 typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap;
2635 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2636 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2637 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2640 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2641 uint32_t nlist_idx = 0;
2642 Symbol *symbol_ptr =
nullptr;
2644 uint32_t sym_idx = 0;
2646 size_t num_syms = 0;
2647 std::string memory_symbol_name;
2648 uint32_t unmapped_local_symbols_found = 0;
2650 std::vector<TrieEntryWithOffset> reexport_trie_entries;
2651 std::vector<TrieEntryWithOffset> external_sym_trie_entries;
2652 std::set<lldb::addr_t> resolver_addresses;
2654 const size_t dyld_trie_data_size = dyld_trie_data.
GetByteSize();
2655 if (dyld_trie_data_size > 0) {
2656 LLDB_LOG(log,
"Parsing {0} bytes of dyld trie data", dyld_trie_data_size);
2660 if (text_segment_sp)
2661 text_segment_file_addr = text_segment_sp->GetFileAddress();
2662 std::vector<llvm::StringRef> nameSlices;
2664 nameSlices, resolver_addresses, reexport_trie_entries,
2665 external_sym_trie_entries);
2668 typedef std::set<ConstString> IndirectSymbols;
2669 IndirectSymbols indirect_symbol_names;
2692 UUID process_shared_cache_uuid;
2693 addr_t process_shared_cache_base_addr;
2697 process_shared_cache_uuid);
2700 __block
bool found_image =
false;
2701 __block
void *nlist_buffer =
nullptr;
2702 __block
unsigned nlist_count = 0;
2703 __block
char *string_table =
nullptr;
2704 __block vm_offset_t vm_nlist_memory = 0;
2705 __block mach_msg_type_number_t vm_nlist_bytes_read = 0;
2706 __block vm_offset_t vm_string_memory = 0;
2707 __block mach_msg_type_number_t vm_string_bytes_read = 0;
2709 llvm::scope_exit _(^{
2710 if (vm_nlist_memory)
2711 vm_deallocate(
mach_task_self(), vm_nlist_memory, vm_nlist_bytes_read);
2712 if (vm_string_memory)
2713 vm_deallocate(
mach_task_self(), vm_string_memory, vm_string_bytes_read);
2716 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
2717 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
2718 UndefinedNameToDescMap undefined_name_to_desc;
2719 SymbolIndexToName reexport_shlib_needs_fixup;
2727 if (process_shared_cache_uuid.
IsValid() &&
2728 process_shared_cache_uuid !=
UUID(&cache_uuid, 16))
2737 if (image_uuid !=
UUID(dsc_image_uuid, 16))
2744 dyld_image_local_nlist_content_4Symbolication(
2745 image, ^(
const void *nlistStart, uint64_t nlistCount,
2746 const char *stringTable) {
2747 if (!nlistStart || !nlistCount)
2755 nlist_byte_size * nlistCount, &vm_nlist_memory,
2756 &vm_nlist_bytes_read);
2759 assert(vm_nlist_bytes_read == nlist_byte_size * nlistCount);
2764 vm_address_t string_address = (vm_address_t)stringTable;
2765 vm_size_t region_size;
2766 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
2767 vm_region_basic_info_data_t info;
2768 memory_object_name_t object;
2770 ®ion_size, VM_REGION_BASIC_INFO_64,
2771 (vm_region_info_t)&info, &info_count, &
object);
2777 ((vm_address_t)stringTable - string_address),
2778 &vm_string_memory, &vm_string_bytes_read);
2782 nlist_buffer = (
void *)vm_nlist_memory;
2783 string_table = (
char *)vm_string_memory;
2784 nlist_count = nlistCount;
2790 nlist_count * nlist_byte_size,
2791 byte_order, addr_byte_size);
2792 unmapped_local_symbols_found = nlist_count;
2798 unmapped_local_symbols_found -
m_dysymtab.nlocalsym);
2803 for (uint32_t nlist_index = 0;
2804 nlist_index < nlist_count;
2808 std::optional<struct nlist_64> nlist_maybe =
2809 ParseNList(dsc_local_symbols_data, nlist_data_offset,
2813 struct nlist_64 nlist = *nlist_maybe;
2816 const char *symbol_name = string_table + nlist.n_strx;
2818 if (symbol_name == NULL) {
2823 "DSC unmapped local symbol[{0}] has invalid "
2824 "string table offset {1:x} in {2}, ignoring symbol",
2825 nlist_index, nlist.n_strx,
2826 module_sp->GetFileSpec().GetPath()));
2829 if (symbol_name[0] ==
'\0')
2832 const char *symbol_name_non_abi_mangled = NULL;
2835 bool add_nlist =
true;
2836 bool is_debug = ((nlist.n_type & N_STAB) != 0);
2837 bool demangled_is_synthesized =
false;
2838 bool is_gsym =
false;
2839 bool set_value =
true;
2841 assert(sym_idx < num_syms);
2846 switch (nlist.n_type) {
2866 symbol_name, symbol_name_non_abi_mangled,
2868 demangled_is_synthesized =
true;
2870 if (nlist.n_value != 0)
2872 nlist.n_sect, nlist.n_value);
2888 nlist.n_sect, nlist.n_value);
2890 N_FUN_addr_to_sym_idx.insert(
2891 std::make_pair(nlist.n_value, sym_idx));
2895 N_FUN_indexes.push_back(sym_idx);
2899 if (!N_FUN_indexes.empty()) {
2906 N_FUN_indexes.pop_back();
2918 N_STSYM_addr_to_sym_idx.insert(
2919 std::make_pair(nlist.n_value, sym_idx));
2920 symbol_section = section_info.
GetSection(nlist.n_sect,
2922 if (symbol_name && symbol_name[0]) {
2930 symbol_section = section_info.
GetSection(nlist.n_sect,
2964 symbol_section = section_info.
GetSection(nlist.n_sect,
2977 if (symbol_name == NULL) {
2988 N_NSYM_indexes.clear();
2989 N_INCL_indexes.clear();
2990 N_BRAC_indexes.clear();
2991 N_COMM_indexes.clear();
2992 N_FUN_indexes.clear();
2998 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
2999 if (N_SO_has_full_path) {
3000 if ((N_SO_index == sym_idx - 1) &&
3001 ((sym_idx - 1) < num_syms)) {
3007 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3013 N_SO_index = sym_idx;
3015 }
else if ((N_SO_index == sym_idx - 1) &&
3016 ((sym_idx - 1) < num_syms)) {
3021 const char *so_path = sym[sym_idx - 1]
3025 if (so_path && so_path[0]) {
3026 std::string full_so_path(so_path);
3027 const size_t double_slash_pos =
3028 full_so_path.find(
"//");
3029 if (double_slash_pos != std::string::npos) {
3040 &full_so_path[double_slash_pos + 1],
3041 FileSpec::Style::native);
3044 full_so_path.erase(0, double_slash_pos + 1);
3048 if (*full_so_path.rbegin() !=
'/')
3049 full_so_path +=
'/';
3050 full_so_path += symbol_name;
3054 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3058 N_SO_index = sym_idx;
3079 N_INCL_indexes.push_back(sym_idx);
3089 if (!N_INCL_indexes.empty()) {
3094 N_INCL_indexes.pop_back();
3129 symbol_section = section_info.
GetSection(nlist.n_sect,
3140 symbol_section = section_info.
GetSection(nlist.n_sect,
3142 N_BRAC_indexes.push_back(sym_idx);
3152 symbol_section = section_info.
GetSection(nlist.n_sect,
3154 if (!N_BRAC_indexes.empty()) {
3159 N_BRAC_indexes.pop_back();
3176 N_COMM_indexes.push_back(sym_idx);
3181 symbol_section = section_info.
GetSection(nlist.n_sect,
3192 if (!N_COMM_indexes.empty()) {
3197 N_COMM_indexes.pop_back();
3212 uint8_t n_type = N_TYPE & nlist.n_type;
3213 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3217 const char *reexport_name_cstr =
3218 strtab_data.PeekCStr(nlist.n_value);
3219 if (reexport_name_cstr && reexport_name_cstr[0]) {
3222 reexport_name_cstr +
3223 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3226 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3228 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
3234 if (symbol_name && symbol_name[0]) {
3236 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0));
3237 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3249 symbol_section = section_info.
GetSection(nlist.n_sect,
3252 if (symbol_section == NULL) {
3258 if (TEXT_eh_frame_sectID == nlist.n_sect) {
3261 uint32_t section_type =
3262 symbol_section->Get() & SECTION_TYPE;
3264 switch (section_type) {
3265 case S_CSTRING_LITERALS:
3268 case S_4BYTE_LITERALS:
3271 case S_8BYTE_LITERALS:
3274 case S_LITERAL_POINTERS:
3277 case S_NON_LAZY_SYMBOL_POINTERS:
3281 case S_LAZY_SYMBOL_POINTERS:
3284 case S_SYMBOL_STUBS:
3288 case S_MOD_INIT_FUNC_POINTERS:
3292 case S_MOD_TERM_FUNC_POINTERS:
3300 case S_16BYTE_LITERALS:
3306 case S_LAZY_DYLIB_SYMBOL_POINTERS:
3310 switch (symbol_section->GetType()) {
3337 const char *symbol_sect_name =
3338 symbol_section->GetName().AsCString(
nullptr);
3339 if (symbol_section->IsDescendant(
3340 text_section_sp.get())) {
3341 if (symbol_section->IsClear(
3342 S_ATTR_PURE_INSTRUCTIONS |
3343 S_ATTR_SELF_MODIFYING_CODE |
3344 S_ATTR_SOME_INSTRUCTIONS))
3348 }
else if (symbol_section->IsDescendant(
3349 data_section_sp.get()) ||
3350 symbol_section->IsDescendant(
3351 data_dirty_section_sp.get()) ||
3352 symbol_section->IsDescendant(
3353 data_const_section_sp.get())) {
3354 if (symbol_sect_name &&
3355 ::strstr(symbol_sect_name,
"__objc") ==
3361 symbol_name_non_abi_mangled, type))
3362 demangled_is_synthesized =
true;
3363 }
else if (symbol_sect_name &&
3364 ::strstr(symbol_sect_name,
3365 "__gcc_except_tab") ==
3371 }
else if (symbol_sect_name &&
3372 ::strstr(symbol_sect_name,
"__IMPORT") ==
3375 }
else if (symbol_section->IsDescendant(
3376 objc_section_sp.get())) {
3378 if (symbol_name && symbol_name[0] ==
'.') {
3379 llvm::StringRef symbol_name_ref(symbol_name);
3381 g_objc_v1_prefix_class(
".objc_class_name_");
3382 if (symbol_name_ref.starts_with(
3383 g_objc_v1_prefix_class)) {
3384 symbol_name_non_abi_mangled = symbol_name;
3385 symbol_name = symbol_name +
3386 g_objc_v1_prefix_class.size();
3388 demangled_is_synthesized =
true;
3399 uint64_t symbol_value = nlist.n_value;
3400 if (symbol_name_non_abi_mangled) {
3406 if (symbol_name && symbol_name[0] ==
'_') {
3413 if (is_gsym && is_debug) {
3414 const char *gsym_name =
3420 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3424 if (symbol_section) {
3425 const addr_t section_file_addr =
3426 symbol_section->GetFileAddress();
3427 symbol_value -= section_file_addr;
3430 if (is_debug ==
false) {
3438 N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3439 if (range.first != range.second) {
3440 bool found_it =
false;
3441 for (
auto pos = range.first; pos != range.second;
3443 if (sym[sym_idx].GetMangled().
GetName(
3447 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3453 sym[sym_idx].IsExternal());
3454 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3456 if (resolver_addresses.find(nlist.n_value) !=
3457 resolver_addresses.end())
3459 sym[sym_idx].
Clear();
3467 if (resolver_addresses.find(nlist.n_value) !=
3468 resolver_addresses.end())
3480 auto range = N_STSYM_addr_to_sym_idx.equal_range(
3482 if (range.first != range.second) {
3483 bool found_it =
false;
3484 for (
auto pos = range.first; pos != range.second;
3486 if (sym[sym_idx].GetMangled().
GetName(
3490 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3496 sym[sym_idx].IsExternal());
3497 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3499 sym[sym_idx].
Clear();
3507 const char *gsym_name =
3515 ConstNameToSymbolIndexMap::const_iterator pos =
3516 N_GSYM_name_to_sym_idx.find(gsym_name);
3517 if (pos != N_GSYM_name_to_sym_idx.end()) {
3518 const uint32_t GSYM_sym_idx = pos->second;
3519 m_nlist_idx_to_sym_idx[nlist_idx] =
3525 Address(symbol_section, symbol_value);
3526 add_symbol_addr(sym[GSYM_sym_idx]
3533 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 |
3535 sym[sym_idx].
Clear();
3543 sym[sym_idx].
SetID(nlist_idx);
3547 Address(symbol_section, symbol_value);
3549 sym[sym_idx].GetAddress().GetFileAddress());
3551 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
3553 if (demangled_is_synthesized)
3557 sym[sym_idx].
Clear();
3564 for (
const auto &pos : reexport_shlib_needs_fixup) {
3565 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3566 if (undef_pos != undefined_name_to_desc.end()) {
3567 const uint8_t dylib_ordinal =
3568 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3569 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
3579 if (nlist_data.GetByteSize() > 0) {
3583 if (sym ==
nullptr) {
3589 if (unmapped_local_symbols_found) {
3591 nlist_data_offset += (
m_dysymtab.nlocalsym * nlist_byte_size);
3597 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
3598 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
3599 UndefinedNameToDescMap undefined_name_to_desc;
3600 SymbolIndexToName reexport_shlib_needs_fixup;
3608 auto ParseSymbolLambda = [&](
struct nlist_64 &nlist, uint32_t nlist_idx,
3610 const bool is_debug = ((nlist.n_type & N_STAB) != 0);
3611 if (is_debug != debug_only)
3614 const char *symbol_name_non_abi_mangled =
nullptr;
3615 const char *symbol_name =
nullptr;
3617 if (have_strtab_data) {
3618 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3620 if (symbol_name ==
nullptr) {
3624 "symbol[{0}] has invalid string table offset {1:x} in {2}, "
3626 nlist_idx, nlist.n_strx, module_sp->GetFileSpec().GetPath()));
3629 if (symbol_name[0] ==
'\0')
3630 symbol_name =
nullptr;
3632 const addr_t str_addr = strtab_addr + nlist.n_strx;
3634 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name,
3636 symbol_name = memory_symbol_name.c_str();
3641 bool add_nlist =
true;
3642 bool is_gsym =
false;
3643 bool demangled_is_synthesized =
false;
3644 bool set_value =
true;
3646 assert(sym_idx < num_syms);
3650 switch (nlist.n_type) {
3668 symbol_name_non_abi_mangled, type)) {
3669 demangled_is_synthesized =
true;
3671 if (nlist.n_value != 0)
3673 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3689 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3691 N_FUN_addr_to_sym_idx.insert(
3692 std::make_pair(nlist.n_value, sym_idx));
3696 N_FUN_indexes.push_back(sym_idx);
3700 if (!N_FUN_indexes.empty()) {
3705 N_FUN_indexes.pop_back();
3716 N_STSYM_addr_to_sym_idx.insert(
3717 std::make_pair(nlist.n_value, sym_idx));
3718 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3719 if (symbol_name && symbol_name[0]) {
3727 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3758 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3770 if (symbol_name ==
nullptr) {
3781 N_NSYM_indexes.clear();
3782 N_INCL_indexes.clear();
3783 N_BRAC_indexes.clear();
3784 N_COMM_indexes.clear();
3785 N_FUN_indexes.clear();
3791 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3792 if (N_SO_has_full_path) {
3793 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) {
3798 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3803 N_SO_index = sym_idx;
3805 }
else if ((N_SO_index == sym_idx - 1) &&
3806 ((sym_idx - 1) < num_syms)) {
3810 llvm::StringRef so_path = sym[sym_idx - 1]
3814 if (!so_path.empty()) {
3815 std::string full_so_path(so_path);
3816 const size_t double_slash_pos = full_so_path.find(
"//");
3817 if (double_slash_pos != std::string::npos) {
3825 so_dir.
SetFile(&full_so_path[double_slash_pos + 1],
3826 FileSpec::Style::native);
3829 full_so_path.erase(0, double_slash_pos + 1);
3833 if (*full_so_path.rbegin() !=
'/')
3834 full_so_path +=
'/';
3835 full_so_path += symbol_name;
3839 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3843 N_SO_index = sym_idx;
3863 N_INCL_indexes.push_back(sym_idx);
3872 if (!N_INCL_indexes.empty()) {
3876 N_INCL_indexes.pop_back();
3911 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3920 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3921 N_BRAC_indexes.push_back(sym_idx);
3930 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3931 if (!N_BRAC_indexes.empty()) {
3935 N_BRAC_indexes.pop_back();
3951 N_COMM_indexes.push_back(sym_idx);
3956 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3965 if (!N_COMM_indexes.empty()) {
3969 N_COMM_indexes.pop_back();
3983 uint8_t n_type = N_TYPE & nlist.n_type;
3984 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3988 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
3989 if (reexport_name_cstr && reexport_name_cstr[0] && symbol_name) {
3992 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3995 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3996 indirect_symbol_names.insert(
3997 ConstString(symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
4003 if (symbol_name && symbol_name[0]) {
4005 ((symbol_name[0] ==
'_') ? 1 : 0));
4006 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4019 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4021 if (!symbol_section) {
4027 if (TEXT_eh_frame_sectID == nlist.n_sect) {
4030 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
4032 switch (section_type) {
4033 case S_CSTRING_LITERALS:
4036 case S_4BYTE_LITERALS:
4039 case S_8BYTE_LITERALS:
4042 case S_LITERAL_POINTERS:
4045 case S_NON_LAZY_SYMBOL_POINTERS:
4048 case S_LAZY_SYMBOL_POINTERS:
4051 case S_SYMBOL_STUBS:
4055 case S_MOD_INIT_FUNC_POINTERS:
4058 case S_MOD_TERM_FUNC_POINTERS:
4065 case S_16BYTE_LITERALS:
4071 case S_LAZY_DYLIB_SYMBOL_POINTERS:
4075 switch (symbol_section->GetType()) {
4097 const char *symbol_sect_name =
4098 symbol_section->GetName().AsCString(
nullptr);
4099 if (symbol_section->IsDescendant(text_section_sp.get())) {
4100 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4101 S_ATTR_SELF_MODIFYING_CODE |
4102 S_ATTR_SOME_INSTRUCTIONS))
4106 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
4107 symbol_section->IsDescendant(
4108 data_dirty_section_sp.get()) ||
4109 symbol_section->IsDescendant(
4110 data_const_section_sp.get())) {
4111 if (symbol_sect_name &&
4112 ::strstr(symbol_sect_name,
"__objc") == symbol_sect_name) {
4116 symbol_name, symbol_name_non_abi_mangled, type))
4117 demangled_is_synthesized =
true;
4118 }
else if (symbol_sect_name &&
4119 ::strstr(symbol_sect_name,
"__gcc_except_tab") ==
4125 }
else if (symbol_sect_name &&
4126 ::strstr(symbol_sect_name,
"__IMPORT") ==
4129 }
else if (symbol_section->IsDescendant(objc_section_sp.get())) {
4131 if (symbol_name && symbol_name[0] ==
'.') {
4132 llvm::StringRef symbol_name_ref(symbol_name);
4133 llvm::StringRef g_objc_v1_prefix_class(
4134 ".objc_class_name_");
4135 if (symbol_name_ref.starts_with(g_objc_v1_prefix_class)) {
4136 symbol_name_non_abi_mangled = symbol_name;
4137 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4139 demangled_is_synthesized =
true;
4150 sym[sym_idx].
Clear();
4154 uint64_t symbol_value = nlist.n_value;
4156 if (symbol_name_non_abi_mangled) {
4162 if (symbol_name && symbol_name[0] ==
'_') {
4173 const char *gsym_name = sym[sym_idx]
4178 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4181 if (symbol_section) {
4182 const addr_t section_file_addr = symbol_section->GetFileAddress();
4183 symbol_value -= section_file_addr;
4192 std::pair<ValueToSymbolIndexMap::const_iterator,
4193 ValueToSymbolIndexMap::const_iterator>
4195 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4196 if (range.first != range.second) {
4197 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4198 pos != range.second; ++pos) {
4202 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4206 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4207 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4208 if (resolver_addresses.find(nlist.n_value) !=
4209 resolver_addresses.end())
4211 sym[sym_idx].
Clear();
4216 if (resolver_addresses.find(nlist.n_value) !=
4217 resolver_addresses.end())
4227 std::pair<ValueToSymbolIndexMap::const_iterator,
4228 ValueToSymbolIndexMap::const_iterator>
4230 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4231 if (range.first != range.second) {
4232 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4233 pos != range.second; ++pos) {
4237 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4241 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4242 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4243 sym[sym_idx].
Clear();
4249 const char *gsym_name = sym[sym_idx]
4254 ConstNameToSymbolIndexMap::const_iterator pos =
4255 N_GSYM_name_to_sym_idx.find(gsym_name);
4256 if (pos != N_GSYM_name_to_sym_idx.end()) {
4257 const uint32_t GSYM_sym_idx = pos->second;
4258 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4262 Address(symbol_section, symbol_value);
4264 sym[GSYM_sym_idx].GetAddress().GetFileAddress());
4268 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4269 sym[sym_idx].
Clear();
4277 sym[sym_idx].
SetID(nlist_idx);
4282 add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
4284 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4285 if (nlist.n_desc & N_WEAK_REF)
4288 if (demangled_is_synthesized)
4297 std::vector<struct nlist_64> nlists;
4298 nlists.reserve(symtab_load_command.
nsyms);
4299 for (; nlist_idx < symtab_load_command.
nsyms; ++nlist_idx) {
4301 ParseNList(nlist_data, nlist_data_offset, nlist_byte_size))
4302 nlists.push_back(*nlist);
4312 for (
auto &nlist : nlists) {
4313 if (!ParseSymbolLambda(nlist, nlist_idx++,
DebugSymbols))
4319 for (
auto &nlist : nlists) {
4324 for (
const auto &pos : reexport_shlib_needs_fixup) {
4325 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4326 if (undef_pos != undefined_name_to_desc.end()) {
4327 const uint8_t dylib_ordinal =
4328 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4329 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
4337 int trie_symbol_table_augment_count = 0;
4338 for (
auto &e : external_sym_trie_entries) {
4339 if (!symbols_added.contains(e.entry.address))
4340 trie_symbol_table_augment_count++;
4343 if (num_syms < sym_idx + trie_symbol_table_augment_count) {
4344 num_syms = sym_idx + trie_symbol_table_augment_count;
4345 sym = symtab.
Resize(num_syms);
4347 uint32_t synthetic_sym_id = symtab_load_command.
nsyms;
4350 for (
auto &e : external_sym_trie_entries) {
4351 if (symbols_added.contains(e.entry.address))
4357 if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) {
4359 const char *symbol_name = e.entry.name.GetCString();
4360 bool demangled_is_synthesized =
false;
4362 GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp,
4363 data_section_sp, data_dirty_section_sp,
4364 data_const_section_sp, symbol_section);
4367 if (symbol_section) {
4368 sym[sym_idx].
SetID(synthetic_sym_id++);
4370 if (demangled_is_synthesized)
4383 if (function_starts_count > 0) {
4384 uint32_t num_synthetic_function_symbols = 0;
4385 for (i = 0; i < function_starts_count; ++i) {
4386 if (!symbols_added.contains(function_starts.GetEntryRef(i).addr))
4387 ++num_synthetic_function_symbols;
4390 if (num_synthetic_function_symbols > 0) {
4391 if (num_syms < sym_idx + num_synthetic_function_symbols) {
4392 num_syms = sym_idx + num_synthetic_function_symbols;
4393 sym = symtab.
Resize(num_syms);
4395 for (i = 0; i < function_starts_count; ++i) {
4396 const FunctionStarts::Entry *func_start_entry =
4397 function_starts.GetEntryAtIndex(i);
4398 if (!symbols_added.contains(func_start_entry->addr)) {
4399 addr_t symbol_file_addr = func_start_entry->addr;
4400 uint32_t symbol_flags = 0;
4401 if (func_start_entry->data)
4404 if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) {
4406 if (symbol_section) {
4407 sym[sym_idx].
SetID(synthetic_sym_id++);
4417 sym[sym_idx].
SetFlags(symbol_flags);
4428 if (sym_idx < num_syms) {
4430 sym = symtab.
Resize(num_syms);
4435 if (indirect_symbol_index_data.GetByteSize()) {
4436 NListIndexToSymbolIndexMap::const_iterator end_index_pos =
4437 m_nlist_idx_to_sym_idx.end();
4444 if (symbol_stub_byte_size == 0)
4447 const uint32_t num_symbol_stubs =
4450 if (num_symbol_stubs == 0)
4453 const uint32_t symbol_stub_index_offset =
4455 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) {
4456 const uint32_t symbol_stub_index =
4457 symbol_stub_index_offset + stub_idx;
4460 (stub_idx * symbol_stub_byte_size);
4462 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(
4463 symbol_stub_offset, 4)) {
4464 const uint32_t stub_sym_id =
4465 indirect_symbol_index_data.GetU32(&symbol_stub_offset);
4466 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4469 NListIndexToSymbolIndexMap::const_iterator index_pos =
4470 m_nlist_idx_to_sym_idx.find(stub_sym_id);
4471 Symbol *stub_symbol =
nullptr;
4472 if (index_pos != end_index_pos) {
4483 Address so_addr(symbol_stub_addr, section_list);
4490 if (resolver_addresses.find(symbol_stub_addr) ==
4491 resolver_addresses.end())
4501 if (sym_idx >= num_syms) {
4502 sym = symtab.
Resize(++num_syms);
4503 stub_symbol =
nullptr;
4505 sym[sym_idx].
SetID(synthetic_sym_id++);
4506 sym[sym_idx].
GetMangled() = stub_symbol_mangled_name;
4507 if (resolver_addresses.find(symbol_stub_addr) ==
4508 resolver_addresses.end())
4520 "warning: symbol stub referencing symbol table "
4521 "symbol %u that isn't in our minimal symbol table, "
4532 if (!reexport_trie_entries.empty()) {
4533 for (
const auto &e : reexport_trie_entries) {
4534 if (e.entry.import_name) {
4537 if (indirect_symbol_names.find(e.entry.name) ==
4538 indirect_symbol_names.end()) {
4540 if (sym_idx >= num_syms)
4541 sym = symtab.
Resize(++num_syms);
4542 sym[sym_idx].
SetID(synthetic_sym_id++);
4547 if (e.entry.other > 0 && e.entry.other <= dylib_files.
GetSize()) {
6339 Target &target = process_sp->GetTarget();
6341 const llvm::Triple &target_triple = target_arch.
GetTriple();
6342 if (target_triple.getVendor() == llvm::Triple::Apple &&
6343 (target_triple.getOS() == llvm::Triple::MacOSX ||
6344 target_triple.getOS() == llvm::Triple::IOS ||
6345 target_triple.getOS() == llvm::Triple::WatchOS ||
6346 target_triple.getOS() == llvm::Triple::TvOS ||
6347 target_triple.getOS() == llvm::Triple::BridgeOS ||
6348 target_triple.getOS() == llvm::Triple::XROS)) {
6349 bool make_core =
false;
6351 case llvm::Triple::aarch64:
6352 case llvm::Triple::aarch64_32:
6353 case llvm::Triple::arm:
6354 case llvm::Triple::thumb:
6355 case llvm::Triple::x86:
6356 case llvm::Triple::x86_64:
6361 "unsupported core architecture: %s", target_triple.str().c_str());
6367 error = process_sp->CalculateCoreFileSaveRanges(options, core_ranges);
6368 if (
error.Success()) {
6371 std::vector<llvm::MachO::segment_command_64> segment_load_commands;
6372 for (
const auto &core_range_info : core_ranges) {
6374 const auto &core_range = core_range_info.data;
6375 uint32_t cmd_type = LC_SEGMENT_64;
6376 uint32_t segment_size =
sizeof(llvm::MachO::segment_command_64);
6377 if (addr_byte_size == 4) {
6378 cmd_type = LC_SEGMENT;
6379 segment_size =
sizeof(llvm::MachO::segment_command);
6383 if (core_range.lldb_permissions == 0 || core_range.range.size() == 0)
6385 uint32_t vm_prot = 0;
6386 if (core_range.lldb_permissions & ePermissionsReadable)
6387 vm_prot |= VM_PROT_READ;
6388 if (core_range.lldb_permissions & ePermissionsWritable)
6389 vm_prot |= VM_PROT_WRITE;
6390 if (core_range.lldb_permissions & ePermissionsExecutable)
6391 vm_prot |= VM_PROT_EXECUTE;
6392 const addr_t vm_addr = core_range.range.start();
6393 const addr_t vm_size = core_range.range.size();
6394 llvm::MachO::segment_command_64
segment = {
6406 segment_load_commands.push_back(
segment);
6411 llvm::MachO::mach_header_64 mach_header;
6412 mach_header.magic = addr_byte_size == 8 ? MH_MAGIC_64 : MH_MAGIC;
6415 mach_header.filetype = MH_CORE;
6416 mach_header.ncmds = segment_load_commands.size();
6417 mach_header.flags = 0;
6418 mach_header.reserved = 0;
6419 ThreadList &thread_list = process_sp->GetThreadList();
6420 const uint32_t num_threads = thread_list.
GetSize();
6426 std::vector<StreamString> LC_THREAD_datas(num_threads);
6427 for (
auto &LC_THREAD_data : LC_THREAD_datas) {
6429 LC_THREAD_data.SetByteOrder(byte_order);
6431 for (uint32_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
6434 switch (mach_header.cputype) {
6435 case llvm::MachO::CPU_TYPE_ARM64:
6436 case llvm::MachO::CPU_TYPE_ARM64_32:
6438 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6441 case llvm::MachO::CPU_TYPE_ARM:
6443 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6446 case llvm::MachO::CPU_TYPE_X86_64:
6448 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6451 case llvm::MachO::CPU_TYPE_RISCV:
6453 thread_sp.get(), LC_THREAD_datas[thread_idx]);
6460 if (addr_byte_size == 8) {
6461 mach_header.sizeofcmds = segment_load_commands.size() *
6462 sizeof(llvm::MachO::segment_command_64);
6464 mach_header.sizeofcmds = segment_load_commands.size() *
6465 sizeof(llvm::MachO::segment_command);
6469 for (
const auto &LC_THREAD_data : LC_THREAD_datas) {
6470 ++mach_header.ncmds;
6471 mach_header.sizeofcmds += 8 + LC_THREAD_data.GetSize();
6476 uint64_t address_mask = process_sp->GetCodeAddressMask();
6479 mach_header.ncmds++;
6480 mach_header.sizeofcmds +=
sizeof(llvm::MachO::note_command);
6484 mach_header.ncmds++;
6485 mach_header.sizeofcmds +=
sizeof(llvm::MachO::note_command);
6488 mach_header.ncmds++;
6489 mach_header.sizeofcmds +=
sizeof(llvm::MachO::note_command);
6492 buffer.
PutHex32(mach_header.magic);
6493 buffer.
PutHex32(mach_header.cputype);
6494 buffer.
PutHex32(mach_header.cpusubtype);
6495 buffer.
PutHex32(mach_header.filetype);
6496 buffer.
PutHex32(mach_header.ncmds);
6497 buffer.
PutHex32(mach_header.sizeofcmds);
6498 buffer.
PutHex32(mach_header.flags);
6499 if (addr_byte_size == 8) {
6500 buffer.
PutHex32(mach_header.reserved);
6505 addr_t file_offset = buffer.
GetSize() + mach_header.sizeofcmds;
6507 file_offset = llvm::alignTo(file_offset, 16);
6508 std::vector<std::unique_ptr<LCNoteEntry>> lc_notes;
6512 std::unique_ptr<LCNoteEntry> addrable_bits_lcnote_up(
6514 addrable_bits_lcnote_up->name =
"addrable bits";
6515 addrable_bits_lcnote_up->payload_file_offset = file_offset;
6516 int bits = std::bitset<64>(~address_mask).count();
6517 addrable_bits_lcnote_up->payload.PutHex32(4);
6518 addrable_bits_lcnote_up->payload.PutHex32(
6520 addrable_bits_lcnote_up->payload.PutHex32(
6522 addrable_bits_lcnote_up->payload.PutHex32(0);
6524 file_offset += addrable_bits_lcnote_up->payload.GetSize();
6526 lc_notes.push_back(std::move(addrable_bits_lcnote_up));
6530 std::unique_ptr<LCNoteEntry> thread_extrainfo_lcnote_up(
6532 thread_extrainfo_lcnote_up->name =
"process metadata";
6533 thread_extrainfo_lcnote_up->payload_file_offset = file_offset;
6536 std::make_shared<StructuredData::Dictionary>());
6538 std::make_shared<StructuredData::Array>());
6540 process_sp->CalculateCoreFileThreadList(options)) {
6542 std::make_shared<StructuredData::Dictionary>());
6543 thread->AddIntegerItem(
"thread_id", thread_sp->GetID());
6544 threads->AddItem(thread);
6546 dict->AddItem(
"threads", threads);
6548 dict->Dump(strm,
false);
6549 thread_extrainfo_lcnote_up->payload.PutRawBytes(strm.
GetData(),
6552 file_offset += thread_extrainfo_lcnote_up->payload.GetSize();
6553 file_offset = llvm::alignTo(file_offset, 16);
6554 lc_notes.push_back(std::move(thread_extrainfo_lcnote_up));
6557 std::unique_ptr<LCNoteEntry> all_image_infos_lcnote_up(
6559 all_image_infos_lcnote_up->name =
"all image infos";
6560 all_image_infos_lcnote_up->payload_file_offset = file_offset;
6562 process_sp, file_offset, all_image_infos_lcnote_up->payload,
6564 lc_notes.push_back(std::move(all_image_infos_lcnote_up));
6567 for (
auto &lcnote : lc_notes) {
6570 buffer.
PutHex32(
sizeof(llvm::MachO::note_command));
6572 memset(namebuf, 0,
sizeof(namebuf));
6578 strncpy(namebuf, lcnote->name.c_str(),
sizeof(namebuf));
6580 buffer.
PutHex64(lcnote->payload_file_offset);
6581 buffer.
PutHex64(lcnote->payload.GetSize());
6585 file_offset = llvm::alignTo(file_offset, 4096);
6587 for (
auto &
segment : segment_load_commands) {
6588 segment.fileoff = file_offset;
6589 file_offset +=
segment.filesize;
6593 for (
const auto &LC_THREAD_data : LC_THREAD_datas) {
6594 const size_t LC_THREAD_data_size = LC_THREAD_data.GetSize();
6596 buffer.
PutHex32(8 + LC_THREAD_data_size);
6597 buffer.
Write(LC_THREAD_data.GetString().data(), LC_THREAD_data_size);
6601 for (
const auto &
segment : segment_load_commands) {
6605 if (addr_byte_size == 8) {
6622 std::string core_file_path(outfile.
GetPath());
6630 uint8_t bytes[0x1000];
6632 size_t bytes_written = buffer.
GetString().size();
6634 core_file.get()->Write(buffer.
GetString().data(), bytes_written);
6635 if (
error.Success()) {
6637 for (
auto &lcnote : lc_notes) {
6638 if (core_file.get()->SeekFromStart(lcnote->payload_file_offset) ==
6641 "Unable to seek to corefile pos "
6642 "to write '%s' LC_NOTE payload",
6643 lcnote->name.c_str());
6646 bytes_written = lcnote->payload.GetSize();
6647 error = core_file.get()->Write(lcnote->payload.GetData(),
6649 if (!
error.Success())
6654 for (
const auto &
segment : segment_load_commands) {
6655 if (core_file.get()->SeekFromStart(
segment.fileoff) == -1) {
6657 "unable to seek to offset 0x%" PRIx64
" in '%s'",
6658 segment.fileoff, core_file_path.c_str());
6664 " bytes of data for memory region at 0x%" PRIx64
"\n",
6669 while (bytes_left > 0 &&
error.Success()) {
6670 const size_t bytes_to_read =
6671 bytes_left >
sizeof(bytes) ?
sizeof(bytes) : bytes_left;
6676 const size_t bytes_read = process_sp->ReadMemoryFromInferior(
6679 if (bytes_read == bytes_to_read) {
6680 size_t bytes_written = bytes_read;
6681 error = core_file.get()->Write(bytes, bytes_written);
6682 bytes_left -= bytes_read;
6687 memset(bytes, 0, bytes_to_read);
6688 size_t bytes_written = bytes_to_read;
6689 error = core_file.get()->Write(bytes, bytes_written);
6690 bytes_left -= bytes_to_read;
6691 addr += bytes_to_read;