9#include "llvm/ADT/ScopeExit.h"
10#include "llvm/ADT/StringRef.h"
48#include "llvm/ADT/DenseSet.h"
49#include "llvm/Support/FormatVariadic.h"
50#include "llvm/Support/MemoryBuffer.h"
55#include <TargetConditionals.h>
58#include <mach/mach_init.h>
59#include <mach/vm_map.h>
80#ifdef CPU_TYPE_ARM64_32
81#undef CPU_TYPE_ARM64_32
95#ifdef LC_VERSION_MIN_MACOSX
96#undef LC_VERSION_MIN_MACOSX
98#ifdef LC_VERSION_MIN_IPHONEOS
99#undef LC_VERSION_MIN_IPHONEOS
101#ifdef LC_VERSION_MIN_TVOS
102#undef LC_VERSION_MIN_TVOS
104#ifdef LC_VERSION_MIN_WATCHOS
105#undef LC_VERSION_MIN_WATCHOS
107#ifdef LC_BUILD_VERSION
108#undef LC_BUILD_VERSION
113#ifdef PLATFORM_MACCATALYST
114#undef PLATFORM_MACCATALYST
119#ifdef PLATFORM_IOSSIMULATOR
120#undef PLATFORM_IOSSIMULATOR
125#ifdef PLATFORM_TVOSSIMULATOR
126#undef PLATFORM_TVOSSIMULATOR
128#ifdef PLATFORM_WATCHOS
129#undef PLATFORM_WATCHOS
131#ifdef PLATFORM_WATCHOSSIMULATOR
132#undef PLATFORM_WATCHOSSIMULATOR
135#define THUMB_ADDRESS_BIT_MASK 0xfffffffffffffffeull
138using namespace llvm::MachO;
143 const
char *alt_name,
size_t reg_byte_size,
145 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
146 if (reg_info ==
nullptr)
147 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
150 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
151 if (reg_info->
byte_size >= reg_byte_size)
152 data.Write(reg_value.
GetBytes(), reg_byte_size);
155 for (
size_t i = 0, n = reg_byte_size - reg_info->
byte_size; i < n; ++i)
162 for (
size_t i = 0; i < reg_byte_size; ++i)
186 int flavor = data.
GetU32(&offset);
191 uint32_t count = data.
GetU32(&offset);
194 for (i = 0; i < count; ++i)
391 int flavor = data.
GetU32(&offset);
396 uint32_t count = data.
GetU32(&offset);
399 for (i = 0; i < count; ++i)
507 int flavor = data.
GetU32(&offset);
508 uint32_t count = data.
GetU32(&offset);
514 uint32_t gpr_buf_count = (
sizeof(
gpr.
r) /
sizeof(
gpr.
r[0])) + 1;
515 if (count == gpr_buf_count) {
516 for (uint32_t i = 0; i < (count - 1); ++i) {
524 offset = next_thread_state;
528 uint8_t *fpu_reg_buf = (uint8_t *)&
fpu.
floats;
529 const int fpu_reg_buf_size =
sizeof(
fpu.
floats);
531 fpu_reg_buf) == fpu_reg_buf_size) {
532 offset += fpu_reg_buf_size;
539 offset = next_thread_state;
550 offset = next_thread_state;
641 int flavor = data.
GetU32(&offset);
642 uint32_t count = data.
GetU32(&offset);
648 if (count >= (33 * 2) + 1) {
649 for (uint32_t i = 0; i < 29; ++i)
658 offset = next_thread_state;
661 uint8_t *fpu_reg_buf = (uint8_t *)&
fpu.
v[0];
662 const int fpu_reg_buf_size =
sizeof(
fpu);
663 if (fpu_reg_buf_size == count *
sizeof(uint32_t) &&
665 fpu_reg_buf) == fpu_reg_buf_size) {
671 offset = next_thread_state;
680 offset = next_thread_state;
773 return sizeof(
struct llvm::MachO::mach_header);
777 return sizeof(
struct llvm::MachO::mach_header_64);
786#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
817 if (data_sp->GetByteSize() < length) {
823 auto objfile_up = std::make_unique<ObjectFileMachO>(
824 module_sp, data_sp, data_offset, file, file_offset, length);
825 if (!objfile_up || !objfile_up->ParseHeader())
828 return objfile_up.release();
835 std::unique_ptr<ObjectFile> objfile_up(
837 if (objfile_up.get() && objfile_up->ParseHeader())
838 return objfile_up.release();
847 const size_t initial_count = specs.
GetSize();
852 llvm::MachO::mach_header header;
854 size_t header_and_load_cmds =
856 if (header_and_load_cmds >= data_sp->GetByteSize()) {
857 data_sp =
MapFileData(file, header_and_load_cmds, file_offset);
870 return specs.
GetSize() - initial_count;
875 return g_segment_name_TEXT;
880 return g_segment_name_DATA;
885 return g_segment_name;
890 return g_segment_name;
895 return g_segment_name_OBJC;
899 static ConstString g_section_name_LINKEDIT(
"__LINKEDIT");
900 return g_section_name_LINKEDIT;
905 return g_section_name;
909 static ConstString g_section_name_eh_frame(
"__eh_frame");
910 return g_section_name_eh_frame;
917 data.
SetData(data_sp, data_offset, data_length);
919 uint32_t magic = data.
GetU32(&offset);
923 uint32_t filetype = data.
GetU32(&offset);
928 if (filetype == llvm::MachO::MH_FILESET)
940 :
ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
941 m_mach_sections(), m_entry_point_address(), m_thread_context_offsets(),
942 m_thread_context_offsets_valid(false), m_reexported_dylibs(),
943 m_allow_assembly_emulation_unwind_plans(true) {
952 :
ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
953 m_mach_sections(), m_entry_point_address(), m_thread_context_offsets(),
954 m_thread_context_offsets_valid(false), m_reexported_dylibs(),
955 m_allow_assembly_emulation_unwind_plans(true) {
962 llvm::MachO::mach_header &header) {
965 header.magic = data.
GetU32(data_offset_ptr);
966 bool can_parse =
false;
967 bool is_64_bit =
false;
968 switch (header.magic) {
1004 data.
GetU32(data_offset_ptr, &header.cputype, 6);
1006 *data_offset_ptr += 4;
1009 memset(&header, 0,
sizeof(header));
1019 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
1020 bool can_parse =
false;
1064 base_spec, all_specs);
1066 for (
unsigned i = 0, e = all_specs.
GetSize(); i != e; ++i) {
1071 const ArchSpec &module_arch = module_sp->GetArchitecture();
1076 const size_t header_and_lc_size =
1086 if (data_sp->GetByteSize() != header_and_lc_size)
1098 memset(&
m_header, 0,
sizeof(
struct llvm::MachO::mach_header));
1108 return m_header.filetype == MH_EXECUTE;
1112 return m_header.filetype == MH_DYLINKER;
1116 return m_header.flags & MH_DYLIB_IN_CACHE;
1120 return m_header.filetype == MH_KEXT_BUNDLE;
1130 return AddressClass::eUnknown;
1138 switch (section_type) {
1140 return AddressClass::eUnknown;
1143 if (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM) {
1147 return AddressClass::eCodeAlternateISA;
1149 return AddressClass::eCode;
1152 return AddressClass::eUnknown;
1166 return AddressClass::eData;
1205 return AddressClass::eDebug;
1211 return AddressClass::eRuntime;
1219 return AddressClass::eUnknown;
1225 switch (symbol_type) {
1227 return AddressClass::eUnknown;
1229 return AddressClass::eUnknown;
1234 if (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM) {
1238 return AddressClass::eCodeAlternateISA;
1240 return AddressClass::eCode;
1243 return AddressClass::eData;
1245 return AddressClass::eRuntime;
1247 return AddressClass::eRuntime;
1249 return AddressClass::eDebug;
1251 return AddressClass::eDebug;
1253 return AddressClass::eDebug;
1255 return AddressClass::eDebug;
1257 return AddressClass::eDebug;
1259 return AddressClass::eData;
1261 return AddressClass::eData;
1263 return AddressClass::eData;
1265 return AddressClass::eDebug;
1267 return AddressClass::eDebug;
1269 return AddressClass::eDebug;
1271 return AddressClass::eDebug;
1273 return AddressClass::eDebug;
1275 return AddressClass::eUnknown;
1277 return AddressClass::eDebug;
1279 return AddressClass::eDebug;
1281 return AddressClass::eUnknown;
1283 return AddressClass::eRuntime;
1285 return AddressClass::eRuntime;
1287 return AddressClass::eRuntime;
1289 return AddressClass::eRuntime;
1292 return AddressClass::eUnknown;
1300 for (uint32_t i = 0; i <
m_header.ncmds; ++i) {
1303 llvm::MachO::load_command lc = {};
1306 if (lc.cmd == LC_DYSYMTAB) {
1310 (
sizeof(
m_dysymtab) /
sizeof(uint32_t)) - 2) ==
1317 offset = load_cmd_offset + lc.cmdsize;
1330 llvm::MachO::encryption_info_command encryption_cmd;
1331 for (uint32_t i = 0; i <
m_header.ncmds; ++i) {
1333 if (
m_data.
GetU32(&offset, &encryption_cmd, 2) ==
nullptr)
1338 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO ||
1339 encryption_cmd.cmd == LC_ENCRYPTION_INFO_64) {
1340 if (
m_data.
GetU32(&offset, &encryption_cmd.cryptoff, 3)) {
1341 if (encryption_cmd.cryptid != 0) {
1349 offset = load_cmd_offset + encryption_cmd.cmdsize;
1356 llvm::MachO::segment_command_64 &seg_cmd, uint32_t cmd_idx) {
1357 if (
m_length == 0 || seg_cmd.filesize == 0)
1366 sizeof(seg_cmd.segname)) == 0)
1369 sizeof(seg_cmd.segname)) == 0)
1382 const char *lc_segment_name =
1383 seg_cmd.cmd == LC_SEGMENT_64 ?
"LC_SEGMENT_64" :
"LC_SEGMENT";
1385 "load command {0} {1} has a fileoff ({2:x16}) that extends beyond "
1386 "the end of the file ({3:x16}), ignoring this section",
1387 cmd_idx, lc_segment_name, seg_cmd.fileoff,
m_length);
1389 seg_cmd.fileoff = 0;
1390 seg_cmd.filesize = 0;
1393 if (seg_cmd.fileoff + seg_cmd.filesize >
m_length) {
1400 const char *lc_segment_name =
1401 seg_cmd.cmd == LC_SEGMENT_64 ?
"LC_SEGMENT_64" :
"LC_SEGMENT";
1403 "load command {0} {1} has a fileoff + filesize ({2:x16}) that "
1404 "extends beyond the end of the file ({4:x16}), the segment will be "
1405 "truncated to match",
1406 cmd_idx, lc_segment_name, seg_cmd.fileoff + seg_cmd.filesize,
m_length);
1409 seg_cmd.filesize =
m_length - seg_cmd.fileoff;
1415 uint32_t result = 0;
1416 if (seg_cmd.initprot & VM_PROT_READ)
1417 result |= ePermissionsReadable;
1418 if (seg_cmd.initprot & VM_PROT_WRITE)
1419 result |= ePermissionsWritable;
1420 if (seg_cmd.initprot & VM_PROT_EXECUTE)
1421 result |= ePermissionsExecutable;
1428 if (flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1431 uint32_t mach_sect_type = flags & SECTION_TYPE;
1432 static ConstString g_sect_name_objc_data(
"__objc_data");
1433 static ConstString g_sect_name_objc_msgrefs(
"__objc_msgrefs");
1434 static ConstString g_sect_name_objc_selrefs(
"__objc_selrefs");
1435 static ConstString g_sect_name_objc_classrefs(
"__objc_classrefs");
1436 static ConstString g_sect_name_objc_superrefs(
"__objc_superrefs");
1437 static ConstString g_sect_name_objc_const(
"__objc_const");
1438 static ConstString g_sect_name_objc_classlist(
"__objc_classlist");
1439 static ConstString g_sect_name_cfstring(
"__cfstring");
1441 static ConstString g_sect_name_dwarf_debug_abbrev(
"__debug_abbrev");
1442 static ConstString g_sect_name_dwarf_debug_abbrev_dwo(
"__debug_abbrev.dwo");
1443 static ConstString g_sect_name_dwarf_debug_addr(
"__debug_addr");
1444 static ConstString g_sect_name_dwarf_debug_aranges(
"__debug_aranges");
1445 static ConstString g_sect_name_dwarf_debug_cu_index(
"__debug_cu_index");
1446 static ConstString g_sect_name_dwarf_debug_frame(
"__debug_frame");
1447 static ConstString g_sect_name_dwarf_debug_info(
"__debug_info");
1448 static ConstString g_sect_name_dwarf_debug_info_dwo(
"__debug_info.dwo");
1449 static ConstString g_sect_name_dwarf_debug_line(
"__debug_line");
1450 static ConstString g_sect_name_dwarf_debug_line_dwo(
"__debug_line.dwo");
1451 static ConstString g_sect_name_dwarf_debug_line_str(
"__debug_line_str");
1452 static ConstString g_sect_name_dwarf_debug_loc(
"__debug_loc");
1453 static ConstString g_sect_name_dwarf_debug_loclists(
"__debug_loclists");
1454 static ConstString g_sect_name_dwarf_debug_loclists_dwo(
"__debug_loclists.dwo");
1455 static ConstString g_sect_name_dwarf_debug_macinfo(
"__debug_macinfo");
1456 static ConstString g_sect_name_dwarf_debug_macro(
"__debug_macro");
1457 static ConstString g_sect_name_dwarf_debug_macro_dwo(
"__debug_macro.dwo");
1458 static ConstString g_sect_name_dwarf_debug_names(
"__debug_names");
1459 static ConstString g_sect_name_dwarf_debug_pubnames(
"__debug_pubnames");
1460 static ConstString g_sect_name_dwarf_debug_pubtypes(
"__debug_pubtypes");
1461 static ConstString g_sect_name_dwarf_debug_ranges(
"__debug_ranges");
1462 static ConstString g_sect_name_dwarf_debug_rnglists(
"__debug_rnglists");
1463 static ConstString g_sect_name_dwarf_debug_str(
"__debug_str");
1464 static ConstString g_sect_name_dwarf_debug_str_dwo(
"__debug_str.dwo");
1465 static ConstString g_sect_name_dwarf_debug_str_offs(
"__debug_str_offs");
1466 static ConstString g_sect_name_dwarf_debug_str_offs_dwo(
"__debug_str_offs.dwo");
1467 static ConstString g_sect_name_dwarf_debug_tu_index(
"__debug_tu_index");
1468 static ConstString g_sect_name_dwarf_debug_types(
"__debug_types");
1469 static ConstString g_sect_name_dwarf_apple_names(
"__apple_names");
1470 static ConstString g_sect_name_dwarf_apple_types(
"__apple_types");
1471 static ConstString g_sect_name_dwarf_apple_namespaces(
"__apple_namespac");
1472 static ConstString g_sect_name_dwarf_apple_objc(
"__apple_objc");
1473 static ConstString g_sect_name_eh_frame(
"__eh_frame");
1474 static ConstString g_sect_name_compact_unwind(
"__unwind_info");
1477 static ConstString g_sect_name_go_symtab(
"__gosymtab");
1479 static ConstString g_sect_name_swift_ast(
"__swift_ast");
1481 if (section_name == g_sect_name_dwarf_debug_abbrev)
1483 if (section_name == g_sect_name_dwarf_debug_abbrev_dwo)
1485 if (section_name == g_sect_name_dwarf_debug_addr)
1487 if (section_name == g_sect_name_dwarf_debug_aranges)
1489 if (section_name == g_sect_name_dwarf_debug_cu_index)
1491 if (section_name == g_sect_name_dwarf_debug_frame)
1493 if (section_name == g_sect_name_dwarf_debug_info)
1495 if (section_name == g_sect_name_dwarf_debug_info_dwo)
1497 if (section_name == g_sect_name_dwarf_debug_line)
1499 if (section_name == g_sect_name_dwarf_debug_line_dwo)
1501 if (section_name == g_sect_name_dwarf_debug_line_str)
1503 if (section_name == g_sect_name_dwarf_debug_loc)
1505 if (section_name == g_sect_name_dwarf_debug_loclists)
1507 if (section_name == g_sect_name_dwarf_debug_loclists_dwo)
1509 if (section_name == g_sect_name_dwarf_debug_macinfo)
1511 if (section_name == g_sect_name_dwarf_debug_macro)
1513 if (section_name == g_sect_name_dwarf_debug_macro_dwo)
1515 if (section_name == g_sect_name_dwarf_debug_names)
1517 if (section_name == g_sect_name_dwarf_debug_pubnames)
1519 if (section_name == g_sect_name_dwarf_debug_pubtypes)
1521 if (section_name == g_sect_name_dwarf_debug_ranges)
1523 if (section_name == g_sect_name_dwarf_debug_rnglists)
1525 if (section_name == g_sect_name_dwarf_debug_str)
1527 if (section_name == g_sect_name_dwarf_debug_str_dwo)
1529 if (section_name == g_sect_name_dwarf_debug_str_offs)
1531 if (section_name == g_sect_name_dwarf_debug_str_offs_dwo)
1533 if (section_name == g_sect_name_dwarf_debug_tu_index)
1535 if (section_name == g_sect_name_dwarf_debug_types)
1537 if (section_name == g_sect_name_dwarf_apple_names)
1539 if (section_name == g_sect_name_dwarf_apple_types)
1541 if (section_name == g_sect_name_dwarf_apple_namespaces)
1543 if (section_name == g_sect_name_dwarf_apple_objc)
1545 if (section_name == g_sect_name_objc_selrefs)
1547 if (section_name == g_sect_name_objc_msgrefs)
1549 if (section_name == g_sect_name_eh_frame)
1551 if (section_name == g_sect_name_compact_unwind)
1553 if (section_name == g_sect_name_cfstring)
1555 if (section_name == g_sect_name_go_symtab)
1557 if (section_name == g_sect_name_ctf)
1559 if (section_name == g_sect_name_swift_ast)
1561 if (section_name == g_sect_name_objc_data ||
1562 section_name == g_sect_name_objc_classrefs ||
1563 section_name == g_sect_name_objc_superrefs ||
1564 section_name == g_sect_name_objc_const ||
1565 section_name == g_sect_name_objc_classlist) {
1569 switch (mach_sect_type) {
1572 if (section_name == g_sect_name_text)
1574 if (section_name == g_sect_name_data)
1579 case S_CSTRING_LITERALS:
1581 case S_4BYTE_LITERALS:
1583 case S_8BYTE_LITERALS:
1585 case S_LITERAL_POINTERS:
1587 case S_NON_LAZY_SYMBOL_POINTERS:
1589 case S_LAZY_SYMBOL_POINTERS:
1591 case S_SYMBOL_STUBS:
1594 case S_MOD_INIT_FUNC_POINTERS:
1597 case S_MOD_TERM_FUNC_POINTERS:
1607 case S_16BYTE_LITERALS:
1611 case S_LAZY_DYLIB_SYMBOL_POINTERS:
1631 const llvm::MachO::load_command &load_cmd_,
lldb::offset_t offset,
1633 llvm::MachO::segment_command_64 load_cmd;
1634 memcpy(&load_cmd, &load_cmd_,
sizeof(load_cmd_));
1636 if (!
m_data.
GetU8(&offset, (uint8_t *)load_cmd.segname, 16))
1641 const bool is_dsym = (
m_header.filetype == MH_DSYM);
1642 bool add_section =
true;
1643 bool add_to_unified =
true;
1645 load_cmd.segname, strnlen(load_cmd.segname,
sizeof(load_cmd.segname)));
1649 if (is_dsym && unified_section_sp) {
1653 add_to_unified =
false;
1657 add_section =
false;
1670 const bool segment_is_encrypted =
1671 (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
1676 if (add_section && (const_segname || is_core)) {
1677 segment_sp = std::make_shared<Section>(
1697 segment_sp->SetIsEncrypted(segment_is_encrypted);
1699 segment_sp->SetPermissions(segment_permissions);
1702 }
else if (unified_section_sp) {
1712 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr) {
1716 "Installing dSYM's %s segment file address over ObjectFile's "
1717 "so symbol table/debug info resolves correctly for %s",
1719 module_sp->GetFileSpec().GetFilename().AsCString());
1724 module_sp->GetObjectFile()->GetSymtab();
1730 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1739 llvm::MachO::section_64 sect64;
1740 ::memset(§64, 0,
sizeof(sect64));
1745 uint32_t segment_sect_idx;
1748 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
1749 for (segment_sect_idx = 0; segment_sect_idx < load_cmd.nsects;
1750 ++segment_sect_idx) {
1751 if (
m_data.
GetU8(&offset, (uint8_t *)sect64.sectname,
1752 sizeof(sect64.sectname)) ==
nullptr)
1754 if (
m_data.
GetU8(&offset, (uint8_t *)sect64.segname,
1755 sizeof(sect64.segname)) ==
nullptr)
1760 if (
m_data.
GetU32(&offset, §64.offset, num_u32s) ==
nullptr)
1773 sect64.sectname, strnlen(sect64.sectname,
sizeof(sect64.sectname)));
1774 if (!const_segname) {
1782 sizeof(sect64.segname));
1784 if (segment_sp.get()) {
1785 Section *segment = segment_sp.get();
1788 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1792 curr_seg_min_addr + curr_seg_byte_size;
1793 if (sect64_min_addr >= curr_seg_min_addr) {
1795 sect64_max_addr - curr_seg_min_addr;
1797 if (new_seg_byte_size > curr_seg_byte_size)
1803 sect64_min_addr - curr_seg_min_addr;
1804 segment->
Slide(slide_amount,
false);
1806 segment->
SetByteSize(curr_seg_max_addr - sect64_min_addr);
1810 if (sect64.offset) {
1816 const lldb::addr_t section_min_file_offset = sect64.offset;
1818 section_min_file_offset + sect64.size;
1820 std::min(section_min_file_offset, segment_min_file_offset);
1822 std::max(section_max_file_offset, segment_max_file_offset) -
1829 segment_sp = std::make_shared<Section>(
1846 sect64.offset ? sect64.size : 0,
1851 segment_sp->SetIsFake(
true);
1852 segment_sp->SetPermissions(segment_permissions);
1856 segment_sp->SetIsEncrypted(segment_is_encrypted);
1859 assert(segment_sp.get());
1864 segment_sp, module_sp,
this, ++context.
NextSectionIdx, section_name,
1865 sect_type, sect64.addr - segment_sp->GetFileAddress(), sect64.size,
1866 sect64.offset, sect64.offset == 0 ? 0 : sect64.size, sect64.align,
1870 bool section_is_encrypted =
false;
1871 if (!segment_is_encrypted && load_cmd.filesize != 0)
1873 sect64.offset) !=
nullptr;
1875 section_sp->SetIsEncrypted(segment_is_encrypted || section_is_encrypted);
1876 section_sp->SetPermissions(segment_permissions);
1877 segment_sp->GetChildren().AddSection(section_sp);
1879 if (segment_sp->IsFake()) {
1881 const_segname.
Clear();
1885 if (segment_sp && is_dsym) {
1888 for (sect_uid = first_segment_sectID; sect_uid <= context.
NextSectionIdx;
1891 segment_sp->GetChildren().FindSectionByID(sect_uid));
1895 segment_sp->GetChildren().FindSectionByID(sect_uid + 1);
1897 if (curr_section_sp.get()) {
1898 if (curr_section_sp->GetByteSize() == 0) {
1899 if (next_section_sp.get() !=
nullptr)
1900 curr_section_sp->SetByteSize(next_section_sp->GetFileAddress() -
1901 curr_section_sp->GetFileAddress());
1903 curr_section_sp->SetByteSize(load_cmd.vmsize);
1912 const llvm::MachO::load_command &load_cmd,
lldb::offset_t offset) {
1916 (
sizeof(
m_dysymtab) /
sizeof(uint32_t)) - 2);
1932 llvm::MachO::load_command load_cmd;
1933 for (uint32_t i = 0; i <
m_header.ncmds; ++i) {
1938 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
1940 else if (load_cmd.cmd == LC_DYSYMTAB)
1943 offset = load_cmd_offset + load_cmd.cmdsize;
1947 module_sp->SectionFileAddressesChanged();
1969 section_sp->GetFileAddress());
1971 section_sp->GetByteSize());
1973 std::string filename =
"<unknown>";
1975 if (first_section_sp)
1976 filename = first_section_sp->GetObjectFile()->GetFileSpec().GetPath();
1979 llvm::formatv(
"unable to find section {0} for a symbol in "
1980 "{1}, corrupt file?",
2010#define TRIE_SYMBOL_IS_THUMB (1ULL << 63)
2013 printf(
"0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2014 static_cast<unsigned long long>(
address),
2015 static_cast<unsigned long long>(
flags),
2038 printf(
"[%3u] 0x%16.16llx: ", idx,
2039 static_cast<unsigned long long>(
nodeOffset));
2049 const bool is_arm,
addr_t text_seg_base_addr,
2050 std::vector<llvm::StringRef> &nameSlices,
2051 std::set<lldb::addr_t> &resolver_addresses,
2052 std::vector<TrieEntryWithOffset> &reexports,
2053 std::vector<TrieEntryWithOffset> &ext_symbols) {
2059 const uint64_t terminalSize = data.
GetULEB128(&offset);
2061 if (terminalSize != 0) {
2064 const char *import_name =
nullptr;
2065 if (e.
entry.
flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
2068 import_name = data.
GetCStr(&offset);
2073 if (e.
entry.
flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
2077 resolver_addr += text_seg_base_addr;
2080 resolver_addresses.insert(resolver_addr);
2084 bool add_this_entry =
false;
2086 import_name && import_name[0]) {
2088 add_this_entry =
true;
2090 (import_name ==
nullptr || import_name[0] ==
'\0')) {
2093 add_this_entry =
true;
2095 if (add_this_entry) {
2097 if (!nameSlices.empty()) {
2098 for (
auto name_slice : nameSlices)
2099 name.append(name_slice.data(), name_slice.size());
2101 if (name.size() > 1) {
2110 reexports.push_back(e);
2116 ext_symbols.push_back(e);
2121 const uint8_t childrenCount = data.
GetU8(&children_offset);
2122 for (uint8_t i = 0; i < childrenCount; ++i) {
2123 const char *cstr = data.
GetCStr(&children_offset);
2125 nameSlices.push_back(llvm::StringRef(cstr));
2129 if (childNodeOffset) {
2131 nameSlices, resolver_addresses, reexports,
2136 nameSlices.pop_back();
2142 bool &demangled_is_synthesized,
2150 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2151 if (symbol_section->IsDescendant(text_section_sp.get())) {
2152 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2153 S_ATTR_SELF_MODIFYING_CODE |
2154 S_ATTR_SOME_INSTRUCTIONS))
2158 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
2159 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
2160 symbol_section->IsDescendant(data_const_section_sp.get())) {
2161 if (symbol_sect_name &&
2162 ::strstr(symbol_sect_name,
"__objc") == symbol_sect_name) {
2166 llvm::StringRef symbol_name_ref(symbol_name);
2167 if (symbol_name_ref.startswith(
"OBJC_")) {
2168 static const llvm::StringRef g_objc_v2_prefix_class(
"OBJC_CLASS_$_");
2169 static const llvm::StringRef g_objc_v2_prefix_metaclass(
2170 "OBJC_METACLASS_$_");
2171 static const llvm::StringRef g_objc_v2_prefix_ivar(
"OBJC_IVAR_$_");
2172 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) {
2173 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2175 demangled_is_synthesized =
true;
2176 }
else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) {
2177 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2179 demangled_is_synthesized =
true;
2180 }
else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) {
2181 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2183 demangled_is_synthesized =
true;
2187 }
else if (symbol_sect_name &&
2188 ::strstr(symbol_sect_name,
"__gcc_except_tab") ==
2194 }
else if (symbol_sect_name &&
2195 ::strstr(symbol_sect_name,
"__IMPORT") == symbol_sect_name) {
2201static std::optional<struct nlist_64>
2203 size_t nlist_byte_size) {
2204 struct nlist_64 nlist;
2227 LLDB_LOG(log,
"Parsing symbol table for {0}", file_name);
2228 Progress progress(llvm::formatv(
"Parsing symbol table for {0}", file_name));
2230 llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0};
2231 llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0};
2232 llvm::MachO::linkedit_data_command exports_trie_load_command = {0, 0, 0, 0};
2233 llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2234 llvm::MachO::dysymtab_command dysymtab =
m_dysymtab;
2243 llvm::DenseSet<addr_t> symbols_added;
2247 auto add_symbol_addr = [&symbols_added](
lldb::addr_t file_addr) {
2251 symbols_added.insert(file_addr);
2253 FunctionStarts function_starts;
2257 llvm::StringRef g_objc_v2_prefix_class(
"_OBJC_CLASS_$_");
2258 llvm::StringRef g_objc_v2_prefix_metaclass(
"_OBJC_METACLASS_$_");
2259 llvm::StringRef g_objc_v2_prefix_ivar(
"_OBJC_IVAR_$_");
2262 for (i = 0; i <
m_header.ncmds; ++i) {
2265 llvm::MachO::load_command lc;
2271 symtab_load_command.cmd = lc.cmd;
2272 symtab_load_command.cmdsize = lc.cmdsize;
2274 if (
m_data.
GetU32(&offset, &symtab_load_command.symoff, 4) ==
2280 case LC_DYLD_INFO_ONLY:
2281 if (
m_data.
GetU32(&offset, &dyld_info.rebase_off, 10)) {
2282 dyld_info.cmd = lc.cmd;
2283 dyld_info.cmdsize = lc.cmdsize;
2285 memset(&dyld_info, 0,
sizeof(dyld_info));
2290 case LC_LOAD_WEAK_DYLIB:
2291 case LC_REEXPORT_DYLIB:
2293 case LC_LOAD_UPWARD_DYLIB: {
2294 uint32_t name_offset = cmd_offset +
m_data.
GetU32(&offset);
2303 if (lc.cmd == LC_REEXPORT_DYLIB) {
2307 dylib_files.
Append(file_spec);
2311 case LC_DYLD_EXPORTS_TRIE:
2312 exports_trie_load_command.cmd = lc.cmd;
2313 exports_trie_load_command.cmdsize = lc.cmdsize;
2314 if (
m_data.
GetU32(&offset, &exports_trie_load_command.dataoff, 2) ==
2316 memset(&exports_trie_load_command, 0,
2317 sizeof(exports_trie_load_command));
2319 case LC_FUNCTION_STARTS:
2320 function_starts_load_command.cmd = lc.cmd;
2321 function_starts_load_command.cmdsize = lc.cmdsize;
2322 if (
m_data.
GetU32(&offset, &function_starts_load_command.dataoff, 2) ==
2324 memset(&function_starts_load_command, 0,
2325 sizeof(function_starts_load_command));
2332 image_uuid =
UUID(uuid_bytes, 16);
2339 offset = cmd_offset + lc.cmdsize;
2342 if (!symtab_load_command.cmd)
2346 if (section_list ==
nullptr)
2351 bool bit_width_32 = addr_byte_size == 4;
2352 const size_t nlist_byte_size =
2353 bit_width_32 ?
sizeof(
struct nlist) : sizeof(struct nlist_64);
2355 DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size);
2356 DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size);
2357 DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size);
2358 DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order,
2360 DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size);
2362 const
addr_t nlist_data_byte_size =
2363 symtab_load_command.nsyms * nlist_byte_size;
2364 const
addr_t strtab_data_byte_size = symtab_load_command.strsize;
2367 ProcessSP process_sp(m_process_wp.lock());
2368 Process *process = process_sp.get();
2372 bool is_local_shared_cache_image = is_shared_cache_image && !
IsInMemory();
2374 section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2376 if (process && m_header.filetype != llvm::MachO::MH_OBJECT &&
2377 !is_local_shared_cache_image) {
2378 Target &target = process->GetTarget();
2384 if (linkedit_section_sp) {
2385 addr_t linkedit_load_addr =
2386 linkedit_section_sp->GetLoadBaseAddress(&target);
2392 linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(
2393 m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2396 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2397 const addr_t symoff_addr = linkedit_load_addr +
2398 symtab_load_command.symoff -
2399 linkedit_file_offset;
2400 strtab_addr = linkedit_load_addr + symtab_load_command.stroff -
2401 linkedit_file_offset;
2409 m_header.filetype == llvm::MachO::MH_DYLINKER) {
2411 ReadMemory(process_sp, symoff_addr, nlist_data_byte_size));
2413 nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2414 if (dysymtab.nindirectsyms != 0) {
2415 const addr_t indirect_syms_addr = linkedit_load_addr +
2416 dysymtab.indirectsymoff -
2417 linkedit_file_offset;
2419 process_sp, indirect_syms_addr, dysymtab.nindirectsyms * 4));
2420 if (indirect_syms_data_sp)
2421 indirect_symbol_index_data.SetData(
2422 indirect_syms_data_sp, 0,
2423 indirect_syms_data_sp->GetByteSize());
2433 if (!is_shared_cache_image) {
2435 ReadMemory(process_sp, strtab_addr, strtab_data_byte_size));
2436 if (strtab_data_sp) {
2437 strtab_data.SetData(strtab_data_sp, 0,
2438 strtab_data_sp->GetByteSize());
2443 if (function_starts_load_command.cmd) {
2444 const addr_t func_start_addr =
2445 linkedit_load_addr + function_starts_load_command.dataoff -
2446 linkedit_file_offset;
2448 ReadMemory(process_sp, func_start_addr,
2449 function_starts_load_command.datasize));
2450 if (func_start_data_sp)
2451 function_starts_data.SetData(func_start_data_sp, 0,
2452 func_start_data_sp->GetByteSize());
2458 if (is_local_shared_cache_image) {
2466 lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset();
2469 symtab_load_command.symoff += linkedit_slide;
2470 symtab_load_command.stroff += linkedit_slide;
2471 dyld_info.export_off += linkedit_slide;
2472 dysymtab.indirectsymoff += linkedit_slide;
2473 function_starts_load_command.dataoff += linkedit_slide;
2474 exports_trie_load_command.dataoff += linkedit_slide;
2477 nlist_data.SetData(
m_data, symtab_load_command.symoff,
2478 nlist_data_byte_size);
2479 strtab_data.SetData(
m_data, symtab_load_command.stroff,
2480 strtab_data_byte_size);
2485 && (exports_trie_load_command.datasize > 0)));
2486 if (dyld_info.export_size > 0) {
2487 dyld_trie_data.SetData(
m_data, dyld_info.export_off,
2488 dyld_info.export_size);
2489 }
else if (exports_trie_load_command.datasize > 0) {
2490 dyld_trie_data.SetData(
m_data, exports_trie_load_command.dataoff,
2491 exports_trie_load_command.datasize);
2494 if (dysymtab.nindirectsyms != 0) {
2495 indirect_symbol_index_data.SetData(
m_data, dysymtab.indirectsymoff,
2496 dysymtab.nindirectsyms * 4);
2498 if (function_starts_load_command.cmd) {
2499 function_starts_data.SetData(
m_data, function_starts_load_command.dataoff,
2500 function_starts_load_command.datasize);
2504 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2523 if (text_section_sp.get())
2524 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName(
2525 g_section_name_eh_frame);
2527 eh_frame_section_sp =
2530 const bool is_arm = (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2547 if (text_section_sp && function_starts_data.GetByteSize()) {
2548 FunctionStarts::Entry function_start_entry;
2549 function_start_entry.data =
false;
2551 function_start_entry.addr = text_section_sp->GetFileAddress();
2553 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) >
2556 function_start_entry.addr += delta;
2558 if (function_start_entry.addr & 1) {
2560 function_start_entry.data =
true;
2561 }
else if (always_thumb) {
2562 function_start_entry.data =
true;
2565 function_starts.Append(function_start_entry);
2573 if (text_section_sp.get() && eh_frame_section_sp.get() &&
2579 addr_t text_base_addr = text_section_sp->GetFileAddress();
2580 size_t count = functions.
GetSize();
2581 for (
size_t i = 0; i < count; ++i) {
2585 FunctionStarts::Entry function_start_entry;
2586 function_start_entry.addr = func->
base - text_base_addr;
2588 if (function_start_entry.addr & 1) {
2590 function_start_entry.data =
true;
2591 }
else if (always_thumb) {
2592 function_start_entry.data =
true;
2595 function_starts.Append(function_start_entry);
2601 const size_t function_starts_count = function_starts.GetSize();
2614 Log *unwind_or_symbol_log(
GetLog(LLDBLog::Symbols | LLDBLog::Unwind));
2616 if (unwind_or_symbol_log)
2617 module_sp->LogMessage(
2618 unwind_or_symbol_log,
2619 "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2622 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get()
2623 ? eh_frame_section_sp->GetID()
2629 std::vector<uint32_t> N_FUN_indexes;
2630 std::vector<uint32_t> N_NSYM_indexes;
2631 std::vector<uint32_t> N_INCL_indexes;
2632 std::vector<uint32_t> N_BRAC_indexes;
2633 std::vector<uint32_t> N_COMM_indexes;
2634 typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap;
2635 typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2636 typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap;
2637 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2638 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2639 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2642 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2643 uint32_t nlist_idx = 0;
2644 Symbol *symbol_ptr =
nullptr;
2646 uint32_t sym_idx = 0;
2648 size_t num_syms = 0;
2649 std::string memory_symbol_name;
2650 uint32_t unmapped_local_symbols_found = 0;
2652 std::vector<TrieEntryWithOffset> reexport_trie_entries;
2653 std::vector<TrieEntryWithOffset> external_sym_trie_entries;
2654 std::set<lldb::addr_t> resolver_addresses;
2656 const size_t dyld_trie_data_size = dyld_trie_data.
GetByteSize();
2657 if (dyld_trie_data_size > 0) {
2658 LLDB_LOG(log,
"Parsing {0} bytes of dyld trie data", dyld_trie_data_size);
2662 if (text_segment_sp)
2663 text_segment_file_addr = text_segment_sp->GetFileAddress();
2664 std::vector<llvm::StringRef> nameSlices;
2666 nameSlices, resolver_addresses, reexport_trie_entries,
2667 external_sym_trie_entries);
2670 typedef std::set<ConstString> IndirectSymbols;
2671 IndirectSymbols indirect_symbol_names;
2694 UUID process_shared_cache_uuid;
2695 addr_t process_shared_cache_base_addr;
2699 process_shared_cache_uuid);
2702 __block
bool found_image =
false;
2703 __block
void *nlist_buffer =
nullptr;
2704 __block
unsigned nlist_count = 0;
2705 __block
char *string_table =
nullptr;
2706 __block vm_offset_t vm_nlist_memory = 0;
2707 __block mach_msg_type_number_t vm_nlist_bytes_read = 0;
2708 __block vm_offset_t vm_string_memory = 0;
2709 __block mach_msg_type_number_t vm_string_bytes_read = 0;
2711 auto _ = llvm::make_scope_exit(^{
2712 if (vm_nlist_memory)
2713 vm_deallocate(
mach_task_self(), vm_nlist_memory, vm_nlist_bytes_read);
2714 if (vm_string_memory)
2715 vm_deallocate(
mach_task_self(), vm_string_memory, vm_string_bytes_read);
2718 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
2719 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
2720 UndefinedNameToDescMap undefined_name_to_desc;
2721 SymbolIndexToName reexport_shlib_needs_fixup;
2723 dyld_for_each_installed_shared_cache(^(dyld_shared_cache_t shared_cache) {
2725 dyld_shared_cache_copy_uuid(shared_cache, &cache_uuid);
2729 if (process_shared_cache_uuid.
IsValid() &&
2730 process_shared_cache_uuid != UUID::fromData(&cache_uuid, 16))
2733 dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) {
2738 dyld_image_copy_uuid(image, &dsc_image_uuid);
2739 if (image_uuid != UUID::fromData(dsc_image_uuid, 16))
2746 dyld_image_local_nlist_content_4Symbolication(
2747 image, ^(
const void *nlistStart, uint64_t nlistCount,
2748 const char *stringTable) {
2749 if (!nlistStart || !nlistCount)
2757 nlist_byte_size * nlistCount, &vm_nlist_memory,
2758 &vm_nlist_bytes_read);
2761 assert(vm_nlist_bytes_read == nlist_byte_size * nlistCount);
2766 vm_address_t string_address = (vm_address_t)stringTable;
2767 vm_size_t region_size;
2768 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
2769 vm_region_basic_info_data_t info;
2770 memory_object_name_t object;
2772 ®ion_size, VM_REGION_BASIC_INFO_64,
2773 (vm_region_info_t)&info, &info_count, &
object);
2779 ((vm_address_t)stringTable - string_address),
2780 &vm_string_memory, &vm_string_bytes_read);
2784 nlist_buffer = (
void *)vm_nlist_memory;
2785 string_table = (
char *)vm_string_memory;
2786 nlist_count = nlistCount;
2792 nlist_count * nlist_byte_size,
2793 byte_order, addr_byte_size);
2794 unmapped_local_symbols_found = nlist_count;
2799 symtab_load_command.nsyms +
m_dysymtab.nindirectsyms +
2800 unmapped_local_symbols_found -
m_dysymtab.nlocalsym);
2805 for (uint32_t nlist_index = 0;
2806 nlist_index < nlist_count;
2810 std::optional<struct nlist_64> nlist_maybe =
2811 ParseNList(dsc_local_symbols_data, nlist_data_offset,
2815 struct nlist_64 nlist = *nlist_maybe;
2818 const char *symbol_name = string_table + nlist.n_strx;
2820 if (symbol_name == NULL) {
2825 "DSC unmapped local symbol[{0}] has invalid "
2826 "string table offset {1:x} in {2}, ignoring symbol",
2827 nlist_index, nlist.n_strx,
2828 module_sp->GetFileSpec().GetPath());
2831 if (symbol_name[0] ==
'\0')
2834 const char *symbol_name_non_abi_mangled = NULL;
2837 uint32_t symbol_byte_size = 0;
2838 bool add_nlist =
true;
2839 bool is_debug = ((nlist.n_type & N_STAB) != 0);
2840 bool demangled_is_synthesized =
false;
2841 bool is_gsym =
false;
2842 bool set_value =
true;
2844 assert(sym_idx < num_syms);
2849 switch (nlist.n_type) {
2868 if (symbol_name && symbol_name[0] ==
'_' &&
2869 symbol_name[1] ==
'O') {
2870 llvm::StringRef symbol_name_ref(symbol_name);
2871 if (symbol_name_ref.startswith(
2872 g_objc_v2_prefix_class)) {
2873 symbol_name_non_abi_mangled = symbol_name + 1;
2875 symbol_name + g_objc_v2_prefix_class.size();
2877 demangled_is_synthesized =
true;
2879 }
else if (symbol_name_ref.startswith(
2880 g_objc_v2_prefix_metaclass)) {
2881 symbol_name_non_abi_mangled = symbol_name + 1;
2883 symbol_name + g_objc_v2_prefix_metaclass.size();
2885 demangled_is_synthesized =
true;
2886 }
else if (symbol_name_ref.startswith(
2887 g_objc_v2_prefix_ivar)) {
2888 symbol_name_non_abi_mangled = symbol_name + 1;
2890 symbol_name + g_objc_v2_prefix_ivar.size();
2892 demangled_is_synthesized =
true;
2895 if (nlist.n_value != 0)
2897 nlist.n_sect, nlist.n_value);
2912 nlist.n_sect, nlist.n_value);
2914 N_FUN_addr_to_sym_idx.insert(
2915 std::make_pair(nlist.n_value, sym_idx));
2919 N_FUN_indexes.push_back(sym_idx);
2923 if (!N_FUN_indexes.empty()) {
2930 N_FUN_indexes.pop_back();
2942 N_STSYM_addr_to_sym_idx.insert(
2943 std::make_pair(nlist.n_value, sym_idx));
2944 symbol_section = section_info.
GetSection(nlist.n_sect,
2946 if (symbol_name && symbol_name[0]) {
2954 symbol_section = section_info.
GetSection(nlist.n_sect,
2988 symbol_section = section_info.
GetSection(nlist.n_sect,
3001 if (symbol_name == NULL) {
3012 N_NSYM_indexes.clear();
3013 N_INCL_indexes.clear();
3014 N_BRAC_indexes.clear();
3015 N_COMM_indexes.clear();
3016 N_FUN_indexes.clear();
3022 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3023 if (N_SO_has_full_path) {
3024 if ((N_SO_index == sym_idx - 1) &&
3025 ((sym_idx - 1) < num_syms)) {
3031 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3037 N_SO_index = sym_idx;
3039 }
else if ((N_SO_index == sym_idx - 1) &&
3040 ((sym_idx - 1) < num_syms)) {
3045 const char *so_path = sym[sym_idx - 1]
3049 if (so_path && so_path[0]) {
3050 std::string full_so_path(so_path);
3051 const size_t double_slash_pos =
3052 full_so_path.find(
"//");
3053 if (double_slash_pos != std::string::npos) {
3064 &full_so_path[double_slash_pos + 1],
3065 FileSpec::Style::native);
3068 full_so_path.erase(0, double_slash_pos + 1);
3072 if (*full_so_path.rbegin() !=
'/')
3073 full_so_path +=
'/';
3074 full_so_path += symbol_name;
3078 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3082 N_SO_index = sym_idx;
3103 N_INCL_indexes.push_back(sym_idx);
3113 if (!N_INCL_indexes.empty()) {
3118 N_INCL_indexes.pop_back();
3153 symbol_section = section_info.
GetSection(nlist.n_sect,
3164 symbol_section = section_info.
GetSection(nlist.n_sect,
3166 N_BRAC_indexes.push_back(sym_idx);
3176 symbol_section = section_info.
GetSection(nlist.n_sect,
3178 if (!N_BRAC_indexes.empty()) {
3183 N_BRAC_indexes.pop_back();
3200 N_COMM_indexes.push_back(sym_idx);
3205 symbol_section = section_info.
GetSection(nlist.n_sect,
3216 if (!N_COMM_indexes.empty()) {
3221 N_COMM_indexes.pop_back();
3236 uint8_t n_type = N_TYPE & nlist.n_type;
3237 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3241 const char *reexport_name_cstr =
3242 strtab_data.PeekCStr(nlist.n_value);
3243 if (reexport_name_cstr && reexport_name_cstr[0]) {
3246 reexport_name_cstr +
3247 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3250 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3252 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
3258 if (symbol_name && symbol_name[0]) {
3260 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0));
3261 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3273 symbol_section = section_info.
GetSection(nlist.n_sect,
3276 if (symbol_section == NULL) {
3282 if (TEXT_eh_frame_sectID == nlist.n_sect) {
3285 uint32_t section_type =
3286 symbol_section->Get() & SECTION_TYPE;
3288 switch (section_type) {
3289 case S_CSTRING_LITERALS:
3292 case S_4BYTE_LITERALS:
3295 case S_8BYTE_LITERALS:
3298 case S_LITERAL_POINTERS:
3301 case S_NON_LAZY_SYMBOL_POINTERS:
3305 case S_LAZY_SYMBOL_POINTERS:
3308 case S_SYMBOL_STUBS:
3312 case S_MOD_INIT_FUNC_POINTERS:
3316 case S_MOD_TERM_FUNC_POINTERS:
3324 case S_16BYTE_LITERALS:
3330 case S_LAZY_DYLIB_SYMBOL_POINTERS:
3334 switch (symbol_section->GetType()) {
3361 const char *symbol_sect_name =
3362 symbol_section->GetName().AsCString();
3363 if (symbol_section->IsDescendant(
3364 text_section_sp.get())) {
3365 if (symbol_section->IsClear(
3366 S_ATTR_PURE_INSTRUCTIONS |
3367 S_ATTR_SELF_MODIFYING_CODE |
3368 S_ATTR_SOME_INSTRUCTIONS))
3372 }
else if (symbol_section->IsDescendant(
3373 data_section_sp.get()) ||
3374 symbol_section->IsDescendant(
3375 data_dirty_section_sp.get()) ||
3376 symbol_section->IsDescendant(
3377 data_const_section_sp.get())) {
3378 if (symbol_sect_name &&
3379 ::strstr(symbol_sect_name,
"__objc") ==
3384 llvm::StringRef symbol_name_ref(symbol_name);
3385 if (symbol_name_ref.startswith(
"_OBJC_")) {
3387 g_objc_v2_prefix_class(
3390 g_objc_v2_prefix_metaclass(
3391 "_OBJC_METACLASS_$_");
3393 g_objc_v2_prefix_ivar(
"_OBJC_IVAR_$_");
3394 if (symbol_name_ref.startswith(
3395 g_objc_v2_prefix_class)) {
3396 symbol_name_non_abi_mangled =
3400 g_objc_v2_prefix_class.size();
3402 demangled_is_synthesized =
true;
3404 symbol_name_ref.startswith(
3405 g_objc_v2_prefix_metaclass)) {
3406 symbol_name_non_abi_mangled =
3410 g_objc_v2_prefix_metaclass.size();
3412 demangled_is_synthesized =
true;
3413 }
else if (symbol_name_ref.startswith(
3414 g_objc_v2_prefix_ivar)) {
3415 symbol_name_non_abi_mangled =
3419 g_objc_v2_prefix_ivar.size();
3421 demangled_is_synthesized =
true;
3425 }
else if (symbol_sect_name &&
3426 ::strstr(symbol_sect_name,
3427 "__gcc_except_tab") ==
3433 }
else if (symbol_sect_name &&
3434 ::strstr(symbol_sect_name,
"__IMPORT") ==
3437 }
else if (symbol_section->IsDescendant(
3438 objc_section_sp.get())) {
3440 if (symbol_name && symbol_name[0] ==
'.') {
3441 llvm::StringRef symbol_name_ref(symbol_name);
3443 g_objc_v1_prefix_class(
".objc_class_name_");
3444 if (symbol_name_ref.startswith(
3445 g_objc_v1_prefix_class)) {
3446 symbol_name_non_abi_mangled = symbol_name;
3447 symbol_name = symbol_name +
3448 g_objc_v1_prefix_class.size();
3450 demangled_is_synthesized =
true;
3461 uint64_t symbol_value = nlist.n_value;
3462 if (symbol_name_non_abi_mangled) {
3468 if (symbol_name && symbol_name[0] ==
'_') {
3475 if (is_gsym && is_debug) {
3476 const char *gsym_name =
3482 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3486 if (symbol_section) {
3487 const addr_t section_file_addr =
3488 symbol_section->GetFileAddress();
3489 if (symbol_byte_size == 0 &&
3490 function_starts_count > 0) {
3491 addr_t symbol_lookup_file_addr = nlist.n_value;
3496 FunctionStarts::Entry *func_start_entry =
3497 function_starts.FindEntry(symbol_lookup_file_addr,
3499 if (is_arm && func_start_entry) {
3503 if (func_start_entry->addr !=
3504 symbol_lookup_file_addr &&
3505 func_start_entry->addr !=
3506 (symbol_lookup_file_addr + 1)) {
3508 func_start_entry = NULL;
3511 if (func_start_entry) {
3512 func_start_entry->data =
true;
3514 addr_t symbol_file_addr = func_start_entry->addr;
3515 uint32_t symbol_flags = 0;
3517 if (symbol_file_addr & 1)
3522 const FunctionStarts::Entry *next_func_start_entry =
3523 function_starts.FindNextEntry(func_start_entry);
3524 const addr_t section_end_file_addr =
3526 symbol_section->GetByteSize();
3527 if (next_func_start_entry) {
3528 addr_t next_symbol_file_addr =
3529 next_func_start_entry->addr;
3535 symbol_byte_size = std::min<lldb::addr_t>(
3536 next_symbol_file_addr - symbol_file_addr,
3537 section_end_file_addr - symbol_file_addr);
3540 section_end_file_addr - symbol_file_addr;
3544 symbol_value -= section_file_addr;
3547 if (is_debug ==
false) {
3555 N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3556 if (range.first != range.second) {
3557 bool found_it =
false;
3558 for (
auto pos = range.first; pos != range.second;
3560 if (sym[sym_idx].GetMangled().
GetName(
3564 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3570 sym[sym_idx].IsExternal());
3571 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3573 if (resolver_addresses.find(nlist.n_value) !=
3574 resolver_addresses.end())
3576 sym[sym_idx].
Clear();
3584 if (resolver_addresses.find(nlist.n_value) !=
3585 resolver_addresses.end())
3597 auto range = N_STSYM_addr_to_sym_idx.equal_range(
3599 if (range.first != range.second) {
3600 bool found_it =
false;
3601 for (
auto pos = range.first; pos != range.second;
3603 if (sym[sym_idx].GetMangled().
GetName(
3607 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3613 sym[sym_idx].IsExternal());
3614 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3616 sym[sym_idx].
Clear();
3624 const char *gsym_name =
3632 ConstNameToSymbolIndexMap::const_iterator pos =
3633 N_GSYM_name_to_sym_idx.find(gsym_name);
3634 if (pos != N_GSYM_name_to_sym_idx.end()) {
3635 const uint32_t GSYM_sym_idx = pos->second;
3636 m_nlist_idx_to_sym_idx[nlist_idx] =
3645 add_symbol_addr(sym[GSYM_sym_idx]
3652 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 |
3654 sym[sym_idx].
Clear();
3662 sym[sym_idx].
SetID(nlist_idx);
3668 sym[sym_idx].GetAddress().GetFileAddress());
3670 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
3672 if (symbol_byte_size > 0)
3675 if (demangled_is_synthesized)
3679 sym[sym_idx].
Clear();
3686 for (
const auto &pos : reexport_shlib_needs_fixup) {
3687 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3688 if (undef_pos != undefined_name_to_desc.end()) {
3689 const uint8_t dylib_ordinal =
3690 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3691 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
3701 if (nlist_data.GetByteSize() > 0) {
3705 if (sym ==
nullptr) {
3711 if (unmapped_local_symbols_found) {
3713 nlist_data_offset += (
m_dysymtab.nlocalsym * nlist_byte_size);
3719 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
3720 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
3721 UndefinedNameToDescMap undefined_name_to_desc;
3722 SymbolIndexToName reexport_shlib_needs_fixup;
3730 auto ParseSymbolLambda = [&](
struct nlist_64 &nlist, uint32_t nlist_idx,
3732 const bool is_debug = ((nlist.n_type & N_STAB) != 0);
3733 if (is_debug != debug_only)
3736 const char *symbol_name_non_abi_mangled =
nullptr;
3737 const char *symbol_name =
nullptr;
3739 if (have_strtab_data) {
3740 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3742 if (symbol_name ==
nullptr) {
3746 "symbol[{0}] has invalid string table offset {1:x} in {2}, "
3748 nlist_idx, nlist.n_strx, module_sp->GetFileSpec().GetPath()));
3751 if (symbol_name[0] ==
'\0')
3752 symbol_name =
nullptr;
3754 const addr_t str_addr = strtab_addr + nlist.n_strx;
3756 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name,
3758 symbol_name = memory_symbol_name.c_str();
3764 bool add_nlist =
true;
3765 bool is_gsym =
false;
3766 bool demangled_is_synthesized =
false;
3767 bool set_value =
true;
3769 assert(sym_idx < num_syms);
3773 switch (nlist.n_type) {
3790 if (symbol_name && symbol_name[0] ==
'_' && symbol_name[1] ==
'O') {
3791 llvm::StringRef symbol_name_ref(symbol_name);
3792 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) {
3793 symbol_name_non_abi_mangled = symbol_name + 1;
3794 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3796 demangled_is_synthesized =
true;
3798 }
else if (symbol_name_ref.startswith(g_objc_v2_prefix_metaclass)) {
3799 symbol_name_non_abi_mangled = symbol_name + 1;
3800 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3802 demangled_is_synthesized =
true;
3803 }
else if (symbol_name_ref.startswith(g_objc_v2_prefix_ivar)) {
3804 symbol_name_non_abi_mangled = symbol_name + 1;
3805 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3807 demangled_is_synthesized =
true;
3810 if (nlist.n_value != 0)
3812 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3827 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3829 N_FUN_addr_to_sym_idx.insert(
3830 std::make_pair(nlist.n_value, sym_idx));
3834 N_FUN_indexes.push_back(sym_idx);
3838 if (!N_FUN_indexes.empty()) {
3843 N_FUN_indexes.pop_back();
3854 N_STSYM_addr_to_sym_idx.insert(
3855 std::make_pair(nlist.n_value, sym_idx));
3856 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3857 if (symbol_name && symbol_name[0]) {
3865 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3896 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3908 if (symbol_name ==
nullptr) {
3919 N_NSYM_indexes.clear();
3920 N_INCL_indexes.clear();
3921 N_BRAC_indexes.clear();
3922 N_COMM_indexes.clear();
3923 N_FUN_indexes.clear();
3929 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3930 if (N_SO_has_full_path) {
3931 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) {
3936 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3941 N_SO_index = sym_idx;
3943 }
else if ((N_SO_index == sym_idx - 1) &&
3944 ((sym_idx - 1) < num_syms)) {
3948 const char *so_path =
3950 if (so_path && so_path[0]) {
3951 std::string full_so_path(so_path);
3952 const size_t double_slash_pos = full_so_path.find(
"//");
3953 if (double_slash_pos != std::string::npos) {
3961 so_dir.
SetFile(&full_so_path[double_slash_pos + 1],
3962 FileSpec::Style::native);
3965 full_so_path.erase(0, double_slash_pos + 1);
3969 if (*full_so_path.rbegin() !=
'/')
3970 full_so_path +=
'/';
3971 full_so_path += symbol_name;
3975 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3979 N_SO_index = sym_idx;
3999 N_INCL_indexes.push_back(sym_idx);
4008 if (!N_INCL_indexes.empty()) {
4012 N_INCL_indexes.pop_back();
4047 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4056 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4057 N_BRAC_indexes.push_back(sym_idx);
4066 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4067 if (!N_BRAC_indexes.empty()) {
4071 N_BRAC_indexes.pop_back();
4087 N_COMM_indexes.push_back(sym_idx);
4092 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4101 if (!N_COMM_indexes.empty()) {
4105 N_COMM_indexes.pop_back();
4119 uint8_t n_type = N_TYPE & nlist.n_type;
4120 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
4124 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4125 if (reexport_name_cstr && reexport_name_cstr[0] && symbol_name) {
4128 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
4131 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4132 indirect_symbol_names.insert(
4133 ConstString(symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
4139 if (symbol_name && symbol_name[0]) {
4141 ((symbol_name[0] ==
'_') ? 1 : 0));
4142 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4155 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4157 if (!symbol_section) {
4163 if (TEXT_eh_frame_sectID == nlist.n_sect) {
4166 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
4168 switch (section_type) {
4169 case S_CSTRING_LITERALS:
4172 case S_4BYTE_LITERALS:
4175 case S_8BYTE_LITERALS:
4178 case S_LITERAL_POINTERS:
4181 case S_NON_LAZY_SYMBOL_POINTERS:
4184 case S_LAZY_SYMBOL_POINTERS:
4187 case S_SYMBOL_STUBS:
4191 case S_MOD_INIT_FUNC_POINTERS:
4194 case S_MOD_TERM_FUNC_POINTERS:
4201 case S_16BYTE_LITERALS:
4207 case S_LAZY_DYLIB_SYMBOL_POINTERS:
4211 switch (symbol_section->GetType()) {
4233 const char *symbol_sect_name =
4234 symbol_section->GetName().AsCString();
4235 if (symbol_section->IsDescendant(text_section_sp.get())) {
4236 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4237 S_ATTR_SELF_MODIFYING_CODE |
4238 S_ATTR_SOME_INSTRUCTIONS))
4242 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
4243 symbol_section->IsDescendant(
4244 data_dirty_section_sp.get()) ||
4245 symbol_section->IsDescendant(
4246 data_const_section_sp.get())) {
4247 if (symbol_sect_name &&
4248 ::strstr(symbol_sect_name,
"__objc") == symbol_sect_name) {
4252 llvm::StringRef symbol_name_ref(symbol_name);
4253 if (symbol_name_ref.startswith(
"_OBJC_")) {
4254 llvm::StringRef g_objc_v2_prefix_class(
4256 llvm::StringRef g_objc_v2_prefix_metaclass(
4257 "_OBJC_METACLASS_$_");
4258 llvm::StringRef g_objc_v2_prefix_ivar(
4260 if (symbol_name_ref.startswith(g_objc_v2_prefix_class)) {
4261 symbol_name_non_abi_mangled = symbol_name + 1;
4263 symbol_name + g_objc_v2_prefix_class.size();
4265 demangled_is_synthesized =
true;
4266 }
else if (symbol_name_ref.startswith(
4267 g_objc_v2_prefix_metaclass)) {
4268 symbol_name_non_abi_mangled = symbol_name + 1;
4270 symbol_name + g_objc_v2_prefix_metaclass.size();
4272 demangled_is_synthesized =
true;
4273 }
else if (symbol_name_ref.startswith(
4274 g_objc_v2_prefix_ivar)) {
4275 symbol_name_non_abi_mangled = symbol_name + 1;
4277 symbol_name + g_objc_v2_prefix_ivar.size();
4279 demangled_is_synthesized =
true;
4283 }
else if (symbol_sect_name &&
4284 ::strstr(symbol_sect_name,
"__gcc_except_tab") ==
4290 }
else if (symbol_sect_name &&
4291 ::strstr(symbol_sect_name,
"__IMPORT") ==
4294 }
else if (symbol_section->IsDescendant(objc_section_sp.get())) {
4296 if (symbol_name && symbol_name[0] ==
'.') {
4297 llvm::StringRef symbol_name_ref(symbol_name);
4298 llvm::StringRef g_objc_v1_prefix_class(
4299 ".objc_class_name_");
4300 if (symbol_name_ref.startswith(g_objc_v1_prefix_class)) {
4301 symbol_name_non_abi_mangled = symbol_name;
4302 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4304 demangled_is_synthesized =
true;
4315 sym[sym_idx].
Clear();
4319 uint64_t symbol_value = nlist.n_value;
4321 if (symbol_name_non_abi_mangled) {
4327 if (symbol_name && symbol_name[0] ==
'_') {
4338 const char *gsym_name = sym[sym_idx]
4343 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4346 if (symbol_section) {
4347 const addr_t section_file_addr = symbol_section->GetFileAddress();
4348 if (symbol_byte_size == 0 && function_starts_count > 0) {
4349 addr_t symbol_lookup_file_addr = nlist.n_value;
4353 FunctionStarts::Entry *func_start_entry =
4354 function_starts.FindEntry(symbol_lookup_file_addr, !is_arm);
4355 if (is_arm && func_start_entry) {
4358 if (func_start_entry->addr != symbol_lookup_file_addr &&
4359 func_start_entry->addr != (symbol_lookup_file_addr + 1)) {
4361 func_start_entry =
nullptr;
4364 if (func_start_entry) {
4365 func_start_entry->data =
true;
4367 addr_t symbol_file_addr = func_start_entry->addr;
4371 const FunctionStarts::Entry *next_func_start_entry =
4372 function_starts.FindNextEntry(func_start_entry);
4373 const addr_t section_end_file_addr =
4374 section_file_addr + symbol_section->GetByteSize();
4375 if (next_func_start_entry) {
4376 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4381 symbol_byte_size = std::min<lldb::addr_t>(
4382 next_symbol_file_addr - symbol_file_addr,
4383 section_end_file_addr - symbol_file_addr);
4385 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4389 symbol_value -= section_file_addr;
4398 std::pair<ValueToSymbolIndexMap::const_iterator,
4399 ValueToSymbolIndexMap::const_iterator>
4401 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4402 if (range.first != range.second) {
4403 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4404 pos != range.second; ++pos) {
4408 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4412 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4413 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4414 if (resolver_addresses.find(nlist.n_value) !=
4415 resolver_addresses.end())
4417 sym[sym_idx].
Clear();
4422 if (resolver_addresses.find(nlist.n_value) !=
4423 resolver_addresses.end())
4433 std::pair<ValueToSymbolIndexMap::const_iterator,
4434 ValueToSymbolIndexMap::const_iterator>
4436 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4437 if (range.first != range.second) {
4438 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4439 pos != range.second; ++pos) {
4443 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4447 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4448 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4449 sym[sym_idx].
Clear();
4455 const char *gsym_name = sym[sym_idx]
4460 ConstNameToSymbolIndexMap::const_iterator pos =
4461 N_GSYM_name_to_sym_idx.find(gsym_name);
4462 if (pos != N_GSYM_name_to_sym_idx.end()) {
4463 const uint32_t GSYM_sym_idx = pos->second;
4464 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4470 sym[GSYM_sym_idx].GetAddress().GetFileAddress());
4474 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4475 sym[sym_idx].
Clear();
4483 sym[sym_idx].
SetID(nlist_idx);
4489 add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
4491 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4492 if (nlist.n_desc & N_WEAK_REF)
4495 if (symbol_byte_size > 0)
4498 if (demangled_is_synthesized)
4507 std::vector<struct nlist_64> nlists;
4508 nlists.reserve(symtab_load_command.nsyms);
4509 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) {
4511 ParseNList(nlist_data, nlist_data_offset, nlist_byte_size))
4512 nlists.push_back(*nlist);
4522 for (
auto &nlist : nlists) {
4523 if (!ParseSymbolLambda(nlist, nlist_idx++,
DebugSymbols))
4529 for (
auto &nlist : nlists) {
4534 for (
const auto &pos : reexport_shlib_needs_fixup) {
4535 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4536 if (undef_pos != undefined_name_to_desc.end()) {
4537 const uint8_t dylib_ordinal =
4538 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4539 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
4547 int trie_symbol_table_augment_count = 0;
4548 for (
auto &e : external_sym_trie_entries) {
4549 if (!symbols_added.contains(e.entry.address))
4550 trie_symbol_table_augment_count++;
4553 if (num_syms < sym_idx + trie_symbol_table_augment_count) {
4554 num_syms = sym_idx + trie_symbol_table_augment_count;
4555 sym = symtab.
Resize(num_syms);
4557 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4560 for (
auto &e : external_sym_trie_entries) {
4561 if (symbols_added.contains(e.entry.address))
4567 if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) {
4569 const char *symbol_name = e.entry.name.GetCString();
4570 bool demangled_is_synthesized =
false;
4572 GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp,
4573 data_section_sp, data_dirty_section_sp,
4574 data_const_section_sp, symbol_section);
4577 if (symbol_section) {
4578 sym[sym_idx].
SetID(synthetic_sym_id++);
4580 if (demangled_is_synthesized)
4593 if (function_starts_count > 0) {
4594 uint32_t num_synthetic_function_symbols = 0;
4595 for (i = 0; i < function_starts_count; ++i) {
4596 if (!symbols_added.contains(function_starts.GetEntryRef(i).addr))
4597 ++num_synthetic_function_symbols;
4600 if (num_synthetic_function_symbols > 0) {
4601 if (num_syms < sym_idx + num_synthetic_function_symbols) {
4602 num_syms = sym_idx + num_synthetic_function_symbols;
4603 sym = symtab.
Resize(num_syms);
4605 for (i = 0; i < function_starts_count; ++i) {
4606 const FunctionStarts::Entry *func_start_entry =
4607 function_starts.GetEntryAtIndex(i);
4608 if (!symbols_added.contains(func_start_entry->addr)) {
4609 addr_t symbol_file_addr = func_start_entry->addr;
4610 uint32_t symbol_flags = 0;
4611 if (func_start_entry->data)
4614 if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) {
4616 uint32_t symbol_byte_size = 0;
4617 if (symbol_section) {
4618 const addr_t section_file_addr = symbol_section->GetFileAddress();
4619 const FunctionStarts::Entry *next_func_start_entry =
4620 function_starts.FindNextEntry(func_start_entry);
4621 const addr_t section_end_file_addr =
4622 section_file_addr + symbol_section->GetByteSize();
4623 if (next_func_start_entry) {
4624 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4627 symbol_byte_size = std::min<lldb::addr_t>(
4628 next_symbol_file_addr - symbol_file_addr,
4629 section_end_file_addr - symbol_file_addr);
4631 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4633 sym[sym_idx].
SetID(synthetic_sym_id++);
4643 sym[sym_idx].
SetFlags(symbol_flags);
4644 if (symbol_byte_size)
4656 if (sym_idx < num_syms) {
4658 sym = symtab.
Resize(num_syms);
4663 if (indirect_symbol_index_data.GetByteSize()) {
4664 NListIndexToSymbolIndexMap::const_iterator end_index_pos =
4665 m_nlist_idx_to_sym_idx.end();
4672 if (symbol_stub_byte_size == 0)
4675 const uint32_t num_symbol_stubs =
4678 if (num_symbol_stubs == 0)
4681 const uint32_t symbol_stub_index_offset =
4683 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) {
4684 const uint32_t symbol_stub_index =
4685 symbol_stub_index_offset + stub_idx;
4688 (stub_idx * symbol_stub_byte_size);
4690 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(
4691 symbol_stub_offset, 4)) {
4692 const uint32_t stub_sym_id =
4693 indirect_symbol_index_data.GetU32(&symbol_stub_offset);
4694 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4697 NListIndexToSymbolIndexMap::const_iterator index_pos =
4698 m_nlist_idx_to_sym_idx.find(stub_sym_id);
4699 Symbol *stub_symbol =
nullptr;
4700 if (index_pos != end_index_pos) {
4711 Address so_addr(symbol_stub_addr, section_list);
4718 if (resolver_addresses.find(symbol_stub_addr) ==
4719 resolver_addresses.end())
4729 if (sym_idx >= num_syms) {
4730 sym = symtab.
Resize(++num_syms);
4731 stub_symbol =
nullptr;
4733 sym[sym_idx].
SetID(synthetic_sym_id++);
4734 sym[sym_idx].
GetMangled() = stub_symbol_mangled_name;
4735 if (resolver_addresses.find(symbol_stub_addr) ==
4736 resolver_addresses.end())
4748 log->
Warning(
"symbol stub referencing symbol table symbol "
4749 "%u that isn't in our minimal symbol table, "
4760 if (!reexport_trie_entries.empty()) {
4761 for (
const auto &e : reexport_trie_entries) {
4762 if (e.entry.import_name) {
4765 if (indirect_symbol_names.find(e.entry.name) ==
4766 indirect_symbol_names.end()) {
4768 if (sym_idx >= num_syms)
4769 sym = symtab.
Resize(++num_syms);
4770 sym[sym_idx].
SetID(synthetic_sym_id++);
4775 if (e.entry.other > 0 && e.entry.other <= dylib_files.
GetSize()) {
4789 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
4790 s->
Printf(
"%p: ",
static_cast<void *
>(
this));
4797 *s <<
", file = '" <<
m_file;
4801 base_spec, all_specs);
4802 for (
unsigned i = 0, e = all_specs.
GetSize(); i != e; ++i) {
4827 llvm::MachO::uuid_command load_cmd;
4830 for (i = 0; i < header.ncmds; ++i) {
4832 if (data.
GetU32(&offset, &load_cmd, 2) ==
nullptr)
4835 if (load_cmd.cmd == LC_UUID) {
4836 const uint8_t *uuid_bytes = data.
PeekData(offset, 16);
4842 const uint8_t opencl_uuid[] = {0x8c, 0x8e, 0xb3, 0x9b, 0x3b, 0xa8,
4843 0x4b, 0x16, 0xb6, 0xa4, 0x27, 0x63,
4844 0xbb, 0x14, 0xf0, 0x0d};
4846 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4849 return UUID(uuid_bytes, 16);
4853 offset = cmd_offset + load_cmd.cmdsize;
4860 case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
4861 return llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4862 case llvm::MachO::LC_VERSION_MIN_MACOSX:
4863 return llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
4864 case llvm::MachO::LC_VERSION_MIN_TVOS:
4865 return llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4866 case llvm::MachO::LC_VERSION_MIN_WATCHOS:
4867 return llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4869 llvm_unreachable(
"unexpected LC_VERSION load command");
4875 llvm::StringRef os_type;
4876 llvm::StringRef environment;
4877 OSEnv(uint32_t cmd) {
4879 case llvm::MachO::PLATFORM_MACOS:
4880 os_type = llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
4882 case llvm::MachO::PLATFORM_IOS:
4883 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4885 case llvm::MachO::PLATFORM_TVOS:
4886 os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4888 case llvm::MachO::PLATFORM_WATCHOS:
4889 os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4899 case llvm::MachO::PLATFORM_MACCATALYST:
4900 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4901 environment = llvm::Triple::getEnvironmentTypeName(llvm::Triple::MacABI);
4903 case llvm::MachO::PLATFORM_IOSSIMULATOR:
4904 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4906 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4908 case llvm::MachO::PLATFORM_TVOSSIMULATOR:
4909 os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4911 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4913 case llvm::MachO::PLATFORM_WATCHOSSIMULATOR:
4914 os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4916 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4919 Log *log(
GetLog(LLDBLog::Symbols | LLDBLog::Process));
4920 LLDB_LOGF(log,
"unsupported platform in LC_BUILD_VERSION");
4927 uint32_t major_version, minor_version, patch_version;
4928 MinOS(uint32_t version)
4929 : major_version(version >> 16), minor_version((version >> 8) & 0xffu),
4930 patch_version(version & 0xffu) {}
4941 if (!base_arch.IsValid())
4944 bool found_any =
false;
4945 auto add_triple = [&](
const llvm::Triple &triple) {
4946 auto spec = base_spec;
4948 if (spec.GetArchitecture().IsValid()) {
4956 llvm::Triple base_triple = base_arch.GetTriple();
4957 base_triple.setOS(llvm::Triple::UnknownOS);
4958 base_triple.setOSName(llvm::StringRef());
4960 if (header.filetype == MH_PRELOAD) {
4961 if (header.cputype == CPU_TYPE_ARM) {
4967 base_triple.setVendor(llvm::Triple::Apple);
4972 base_triple.setVendor(llvm::Triple::UnknownVendor);
4973 base_triple.setVendorName(llvm::StringRef());
4975 return add_triple(base_triple);
4978 llvm::MachO::load_command load_cmd;
4983 for (uint32_t i = 0; i < header.ncmds; ++i) {
4985 if (data.
GetU32(&offset, &load_cmd, 2) ==
nullptr)
4988 llvm::MachO::version_min_command version_min;
4989 switch (load_cmd.cmd) {
4990 case llvm::MachO::LC_VERSION_MIN_MACOSX:
4991 case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
4992 case llvm::MachO::LC_VERSION_MIN_TVOS:
4993 case llvm::MachO::LC_VERSION_MIN_WATCHOS: {
4994 if (load_cmd.cmdsize !=
sizeof(version_min))
4999 MinOS min_os(version_min.version);
5000 llvm::SmallString<32> os_name;
5001 llvm::raw_svector_ostream os(os_name);
5002 os <<
GetOSName(load_cmd.cmd) << min_os.major_version <<
'.'
5003 << min_os.minor_version <<
'.' << min_os.patch_version;
5005 auto triple = base_triple;
5006 triple.setOSName(os.str());
5009 if (load_cmd.cmd != llvm::MachO::LC_VERSION_MIN_MACOSX &&
5010 (base_triple.getArch() == llvm::Triple::x86_64 ||
5011 base_triple.getArch() == llvm::Triple::x86)) {
5018 triple.setEnvironment(llvm::Triple::Simulator);
5027 offset = cmd_offset + load_cmd.cmdsize;
5033 for (uint32_t i = 0; i < header.ncmds; ++i) {
5035 if (data.
GetU32(&offset, &load_cmd, 2) ==
nullptr)
5039 if (load_cmd.cmd == llvm::MachO::LC_BUILD_VERSION) {
5040 llvm::MachO::build_version_command build_version;
5041 if (load_cmd.cmdsize <
sizeof(build_version)) {
5045 if (data.
ExtractBytes(cmd_offset,
sizeof(build_version),
5048 MinOS min_os(build_version.minos);
5049 OSEnv os_env(build_version.platform);
5050 llvm::SmallString<16> os_name;
5051 llvm::raw_svector_ostream os(os_name);
5052 os << os_env.os_type << min_os.major_version <<
'.'
5053 << min_os.minor_version <<
'.' << min_os.patch_version;
5054 auto triple = base_triple;
5055 triple.setOSName(os.str());
5057 if (!os_env.environment.empty())
5058 triple.setEnvironmentName(os_env.environment);
5062 offset = cmd_offset + load_cmd.cmdsize;
5066 add_triple(base_triple);
5071 ModuleSP module_sp,
const llvm::MachO::mach_header &header,
5076 base_spec, all_specs);
5081 const ArchSpec &module_arch = module_sp->GetArchitecture();
5082 for (
unsigned i = 0, e = all_specs.
GetSize(); i != e; ++i) {
5099 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
5110 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
5111 llvm::MachO::load_command load_cmd;
5113 std::vector<std::string> rpath_paths;
5114 std::vector<std::string> rpath_relative_paths;
5115 std::vector<std::string> at_exec_relative_paths;
5117 for (i = 0; i <
m_header.ncmds; ++i) {
5118 const uint32_t cmd_offset = offset;
5122 switch (load_cmd.cmd) {
5125 case LC_LOAD_WEAK_DYLIB:
5126 case LC_REEXPORT_DYLIB:
5127 case LC_LOAD_DYLINKER:
5129 case LC_LOAD_UPWARD_DYLIB: {
5130 uint32_t name_offset = cmd_offset +
m_data.
GetU32(&offset);
5133 if (load_cmd.cmd == LC_RPATH)
5134 rpath_paths.push_back(path);
5136 if (path[0] ==
'@') {
5137 if (strncmp(path,
"@rpath", strlen(
"@rpath")) == 0)
5138 rpath_relative_paths.push_back(path + strlen(
"@rpath"));
5139 else if (strncmp(path,
"@executable_path",
5140 strlen(
"@executable_path")) == 0)
5141 at_exec_relative_paths.push_back(path +
5142 strlen(
"@executable_path"));
5155 offset = cmd_offset + load_cmd.cmdsize;
5161 if (!rpath_paths.empty()) {
5163 std::string loader_path(
"@loader_path");
5164 std::string executable_path(
"@executable_path");
5165 for (
auto &rpath : rpath_paths) {
5166 if (llvm::StringRef(rpath).startswith(loader_path)) {
5167 rpath.erase(0, loader_path.size());
5169 }
else if (llvm::StringRef(rpath).startswith(executable_path)) {
5170 rpath.erase(0, executable_path.size());
5175 for (
const auto &rpath_relative_path : rpath_relative_paths) {
5176 for (
const auto &rpath : rpath_paths) {
5177 std::string path = rpath;
5178 path += rpath_relative_path;
5198 for (
const auto &at_exec_relative_path : at_exec_relative_paths) {