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;
146 const
char *alt_name,
size_t reg_byte_size,
148 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName(name);
149 if (reg_info ==
nullptr)
150 reg_info = reg_ctx->GetRegisterInfoByName(alt_name);
153 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
154 if (reg_info->
byte_size >= reg_byte_size)
155 data.Write(reg_value.
GetBytes(), reg_byte_size);
158 for (
size_t i = 0, n = reg_byte_size - reg_info->
byte_size; i < n; ++i)
165 for (
size_t i = 0; i < reg_byte_size; ++i)
189 int flavor = data.
GetU32(&offset);
194 uint32_t count = data.
GetU32(&offset);
197 for (i = 0; i < count; ++i)
394 int flavor = data.
GetU32(&offset);
399 uint32_t count = data.
GetU32(&offset);
402 for (i = 0; i < count; ++i)
510 int flavor = data.
GetU32(&offset);
511 uint32_t count = data.
GetU32(&offset);
517 uint32_t gpr_buf_count = (
sizeof(
gpr.
r) /
sizeof(
gpr.
r[0])) + 1;
518 if (count == gpr_buf_count) {
519 for (uint32_t i = 0; i < (count - 1); ++i) {
527 offset = next_thread_state;
531 uint8_t *fpu_reg_buf = (uint8_t *)&
fpu.
floats;
532 const int fpu_reg_buf_size =
sizeof(
fpu.
floats);
534 fpu_reg_buf) == fpu_reg_buf_size) {
535 offset += fpu_reg_buf_size;
542 offset = next_thread_state;
553 offset = next_thread_state;
644 int flavor = data.
GetU32(&offset);
645 uint32_t count = data.
GetU32(&offset);
651 if (count >= (33 * 2) + 1) {
652 for (uint32_t i = 0; i < 29; ++i)
661 offset = next_thread_state;
664 uint8_t *fpu_reg_buf = (uint8_t *)&
fpu.
v[0];
665 const int fpu_reg_buf_size =
sizeof(
fpu);
666 if (fpu_reg_buf_size == count *
sizeof(uint32_t) &&
668 fpu_reg_buf) == fpu_reg_buf_size) {
674 offset = next_thread_state;
683 offset = next_thread_state;
776 return sizeof(
struct llvm::MachO::mach_header);
780 return sizeof(
struct llvm::MachO::mach_header_64);
789#define MACHO_NLIST_ARM_SYMBOL_IS_THUMB 0x0008
820 if (data_sp->GetByteSize() < length) {
826 auto objfile_up = std::make_unique<ObjectFileMachO>(
827 module_sp, data_sp, data_offset, file, file_offset, length);
828 if (!objfile_up || !objfile_up->ParseHeader())
831 return objfile_up.release();
838 std::unique_ptr<ObjectFile> objfile_up(
840 if (objfile_up.get() && objfile_up->ParseHeader())
841 return objfile_up.release();
850 const size_t initial_count = specs.
GetSize();
855 llvm::MachO::mach_header header;
857 size_t header_and_load_cmds =
859 if (header_and_load_cmds >= data_sp->GetByteSize()) {
860 data_sp =
MapFileData(file, header_and_load_cmds, file_offset);
873 return specs.
GetSize() - initial_count;
878 return g_segment_name_TEXT;
883 return g_segment_name_DATA;
888 return g_segment_name;
893 return g_segment_name;
898 return g_segment_name_OBJC;
902 static ConstString g_section_name_LINKEDIT(
"__LINKEDIT");
903 return g_section_name_LINKEDIT;
908 return g_section_name;
913 return g_section_name;
917 static ConstString g_section_name_eh_frame(
"__eh_frame");
918 return g_section_name_eh_frame;
925 data.
SetData(data_sp, data_offset, data_length);
927 uint32_t magic = data.
GetU32(&offset);
931 uint32_t filetype = data.
GetU32(&offset);
936 if (filetype == llvm::MachO::MH_FILESET)
948 :
ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset),
949 m_mach_sections(), m_entry_point_address(), m_thread_context_offsets(),
950 m_thread_context_offsets_valid(false), m_reexported_dylibs(),
951 m_allow_assembly_emulation_unwind_plans(true) {
960 :
ObjectFile(module_sp, process_sp, header_addr, header_data_sp),
961 m_mach_sections(), m_entry_point_address(), m_thread_context_offsets(),
962 m_thread_context_offsets_valid(false), m_reexported_dylibs(),
963 m_allow_assembly_emulation_unwind_plans(true) {
970 llvm::MachO::mach_header &header) {
973 header.magic = data.
GetU32(data_offset_ptr);
974 bool can_parse =
false;
975 bool is_64_bit =
false;
976 switch (header.magic) {
1012 data.
GetU32(data_offset_ptr, &header.cputype, 6);
1014 *data_offset_ptr += 4;
1017 memset(&header, 0,
sizeof(header));
1027 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
1028 bool can_parse =
false;
1072 base_spec, all_specs);
1074 for (
unsigned i = 0, e = all_specs.
GetSize(); i != e; ++i) {
1079 const ArchSpec &module_arch = module_sp->GetArchitecture();
1084 const size_t header_and_lc_size =
1094 if (data_sp->GetByteSize() != header_and_lc_size)
1106 memset(&
m_header, 0,
sizeof(
struct llvm::MachO::mach_header));
1116 return m_header.filetype == MH_EXECUTE;
1120 return m_header.filetype == MH_DYLINKER;
1124 return m_header.flags & MH_DYLIB_IN_CACHE;
1128 return m_header.filetype == MH_KEXT_BUNDLE;
1138 return AddressClass::eUnknown;
1146 switch (section_type) {
1148 return AddressClass::eUnknown;
1151 if (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM) {
1155 return AddressClass::eCodeAlternateISA;
1157 return AddressClass::eCode;
1160 return AddressClass::eUnknown;
1174 return AddressClass::eData;
1213 return AddressClass::eDebug;
1219 return AddressClass::eRuntime;
1227 return AddressClass::eUnknown;
1233 switch (symbol_type) {
1235 return AddressClass::eUnknown;
1237 return AddressClass::eUnknown;
1242 if (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM) {
1246 return AddressClass::eCodeAlternateISA;
1248 return AddressClass::eCode;
1251 return AddressClass::eData;
1253 return AddressClass::eRuntime;
1255 return AddressClass::eRuntime;
1257 return AddressClass::eDebug;
1259 return AddressClass::eDebug;
1261 return AddressClass::eDebug;
1263 return AddressClass::eDebug;
1265 return AddressClass::eDebug;
1267 return AddressClass::eData;
1269 return AddressClass::eData;
1271 return AddressClass::eData;
1273 return AddressClass::eDebug;
1275 return AddressClass::eDebug;
1277 return AddressClass::eDebug;
1279 return AddressClass::eDebug;
1281 return AddressClass::eDebug;
1283 return AddressClass::eUnknown;
1285 return AddressClass::eDebug;
1287 return AddressClass::eDebug;
1289 return AddressClass::eUnknown;
1291 return AddressClass::eRuntime;
1293 return AddressClass::eRuntime;
1295 return AddressClass::eRuntime;
1297 return AddressClass::eRuntime;
1300 return AddressClass::eUnknown;
1308 for (uint32_t i = 0; i <
m_header.ncmds; ++i) {
1311 llvm::MachO::load_command lc = {};
1314 if (lc.cmd == LC_DYSYMTAB) {
1318 (
sizeof(
m_dysymtab) /
sizeof(uint32_t)) - 2) ==
1325 offset = load_cmd_offset + lc.cmdsize;
1338 llvm::MachO::encryption_info_command encryption_cmd;
1339 for (uint32_t i = 0; i <
m_header.ncmds; ++i) {
1341 if (
m_data.
GetU32(&offset, &encryption_cmd, 2) ==
nullptr)
1346 if (encryption_cmd.cmd == LC_ENCRYPTION_INFO ||
1347 encryption_cmd.cmd == LC_ENCRYPTION_INFO_64) {
1348 if (
m_data.
GetU32(&offset, &encryption_cmd.cryptoff, 3)) {
1349 if (encryption_cmd.cryptid != 0) {
1357 offset = load_cmd_offset + encryption_cmd.cmdsize;
1364 llvm::MachO::segment_command_64 &seg_cmd, uint32_t cmd_idx) {
1365 if (
m_length == 0 || seg_cmd.filesize == 0)
1374 sizeof(seg_cmd.segname)) == 0)
1377 sizeof(seg_cmd.segname)) == 0)
1390 const char *lc_segment_name =
1391 seg_cmd.cmd == LC_SEGMENT_64 ?
"LC_SEGMENT_64" :
"LC_SEGMENT";
1393 "load command {0} {1} has a fileoff ({2:x16}) that extends beyond "
1394 "the end of the file ({3:x16}), ignoring this section",
1395 cmd_idx, lc_segment_name, seg_cmd.fileoff,
m_length);
1397 seg_cmd.fileoff = 0;
1398 seg_cmd.filesize = 0;
1401 if (seg_cmd.fileoff + seg_cmd.filesize >
m_length) {
1408 const char *lc_segment_name =
1409 seg_cmd.cmd == LC_SEGMENT_64 ?
"LC_SEGMENT_64" :
"LC_SEGMENT";
1411 "load command {0} {1} has a fileoff + filesize ({2:x16}) that "
1412 "extends beyond the end of the file ({4:x16}), the segment will be "
1413 "truncated to match",
1414 cmd_idx, lc_segment_name, seg_cmd.fileoff + seg_cmd.filesize,
m_length);
1417 seg_cmd.filesize =
m_length - seg_cmd.fileoff;
1423 uint32_t result = 0;
1424 if (seg_cmd.initprot & VM_PROT_READ)
1425 result |= ePermissionsReadable;
1426 if (seg_cmd.initprot & VM_PROT_WRITE)
1427 result |= ePermissionsWritable;
1428 if (seg_cmd.initprot & VM_PROT_EXECUTE)
1429 result |= ePermissionsExecutable;
1436 if (flags & (S_ATTR_PURE_INSTRUCTIONS | S_ATTR_SOME_INSTRUCTIONS))
1439 uint32_t mach_sect_type = flags & SECTION_TYPE;
1440 static ConstString g_sect_name_objc_data(
"__objc_data");
1441 static ConstString g_sect_name_objc_msgrefs(
"__objc_msgrefs");
1442 static ConstString g_sect_name_objc_selrefs(
"__objc_selrefs");
1443 static ConstString g_sect_name_objc_classrefs(
"__objc_classrefs");
1444 static ConstString g_sect_name_objc_superrefs(
"__objc_superrefs");
1445 static ConstString g_sect_name_objc_const(
"__objc_const");
1446 static ConstString g_sect_name_objc_classlist(
"__objc_classlist");
1447 static ConstString g_sect_name_cfstring(
"__cfstring");
1449 static ConstString g_sect_name_dwarf_debug_abbrev(
"__debug_abbrev");
1450 static ConstString g_sect_name_dwarf_debug_abbrev_dwo(
"__debug_abbrev.dwo");
1451 static ConstString g_sect_name_dwarf_debug_addr(
"__debug_addr");
1452 static ConstString g_sect_name_dwarf_debug_aranges(
"__debug_aranges");
1453 static ConstString g_sect_name_dwarf_debug_cu_index(
"__debug_cu_index");
1454 static ConstString g_sect_name_dwarf_debug_frame(
"__debug_frame");
1455 static ConstString g_sect_name_dwarf_debug_info(
"__debug_info");
1456 static ConstString g_sect_name_dwarf_debug_info_dwo(
"__debug_info.dwo");
1457 static ConstString g_sect_name_dwarf_debug_line(
"__debug_line");
1458 static ConstString g_sect_name_dwarf_debug_line_dwo(
"__debug_line.dwo");
1459 static ConstString g_sect_name_dwarf_debug_line_str(
"__debug_line_str");
1460 static ConstString g_sect_name_dwarf_debug_loc(
"__debug_loc");
1461 static ConstString g_sect_name_dwarf_debug_loclists(
"__debug_loclists");
1462 static ConstString g_sect_name_dwarf_debug_loclists_dwo(
"__debug_loclists.dwo");
1463 static ConstString g_sect_name_dwarf_debug_macinfo(
"__debug_macinfo");
1464 static ConstString g_sect_name_dwarf_debug_macro(
"__debug_macro");
1465 static ConstString g_sect_name_dwarf_debug_macro_dwo(
"__debug_macro.dwo");
1466 static ConstString g_sect_name_dwarf_debug_names(
"__debug_names");
1467 static ConstString g_sect_name_dwarf_debug_pubnames(
"__debug_pubnames");
1468 static ConstString g_sect_name_dwarf_debug_pubtypes(
"__debug_pubtypes");
1469 static ConstString g_sect_name_dwarf_debug_ranges(
"__debug_ranges");
1470 static ConstString g_sect_name_dwarf_debug_rnglists(
"__debug_rnglists");
1471 static ConstString g_sect_name_dwarf_debug_str(
"__debug_str");
1472 static ConstString g_sect_name_dwarf_debug_str_dwo(
"__debug_str.dwo");
1473 static ConstString g_sect_name_dwarf_debug_str_offs(
"__debug_str_offs");
1474 static ConstString g_sect_name_dwarf_debug_str_offs_dwo(
"__debug_str_offs.dwo");
1475 static ConstString g_sect_name_dwarf_debug_tu_index(
"__debug_tu_index");
1476 static ConstString g_sect_name_dwarf_debug_types(
"__debug_types");
1477 static ConstString g_sect_name_dwarf_apple_names(
"__apple_names");
1478 static ConstString g_sect_name_dwarf_apple_types(
"__apple_types");
1479 static ConstString g_sect_name_dwarf_apple_namespaces(
"__apple_namespac");
1480 static ConstString g_sect_name_dwarf_apple_objc(
"__apple_objc");
1481 static ConstString g_sect_name_eh_frame(
"__eh_frame");
1482 static ConstString g_sect_name_compact_unwind(
"__unwind_info");
1485 static ConstString g_sect_name_go_symtab(
"__gosymtab");
1487 static ConstString g_sect_name_swift_ast(
"__swift_ast");
1489 if (section_name == g_sect_name_dwarf_debug_abbrev)
1491 if (section_name == g_sect_name_dwarf_debug_abbrev_dwo)
1493 if (section_name == g_sect_name_dwarf_debug_addr)
1495 if (section_name == g_sect_name_dwarf_debug_aranges)
1497 if (section_name == g_sect_name_dwarf_debug_cu_index)
1499 if (section_name == g_sect_name_dwarf_debug_frame)
1501 if (section_name == g_sect_name_dwarf_debug_info)
1503 if (section_name == g_sect_name_dwarf_debug_info_dwo)
1505 if (section_name == g_sect_name_dwarf_debug_line)
1507 if (section_name == g_sect_name_dwarf_debug_line_dwo)
1509 if (section_name == g_sect_name_dwarf_debug_line_str)
1511 if (section_name == g_sect_name_dwarf_debug_loc)
1513 if (section_name == g_sect_name_dwarf_debug_loclists)
1515 if (section_name == g_sect_name_dwarf_debug_loclists_dwo)
1517 if (section_name == g_sect_name_dwarf_debug_macinfo)
1519 if (section_name == g_sect_name_dwarf_debug_macro)
1521 if (section_name == g_sect_name_dwarf_debug_macro_dwo)
1523 if (section_name == g_sect_name_dwarf_debug_names)
1525 if (section_name == g_sect_name_dwarf_debug_pubnames)
1527 if (section_name == g_sect_name_dwarf_debug_pubtypes)
1529 if (section_name == g_sect_name_dwarf_debug_ranges)
1531 if (section_name == g_sect_name_dwarf_debug_rnglists)
1533 if (section_name == g_sect_name_dwarf_debug_str)
1535 if (section_name == g_sect_name_dwarf_debug_str_dwo)
1537 if (section_name == g_sect_name_dwarf_debug_str_offs)
1539 if (section_name == g_sect_name_dwarf_debug_str_offs_dwo)
1541 if (section_name == g_sect_name_dwarf_debug_tu_index)
1543 if (section_name == g_sect_name_dwarf_debug_types)
1545 if (section_name == g_sect_name_dwarf_apple_names)
1547 if (section_name == g_sect_name_dwarf_apple_types)
1549 if (section_name == g_sect_name_dwarf_apple_namespaces)
1551 if (section_name == g_sect_name_dwarf_apple_objc)
1553 if (section_name == g_sect_name_objc_selrefs)
1555 if (section_name == g_sect_name_objc_msgrefs)
1557 if (section_name == g_sect_name_eh_frame)
1559 if (section_name == g_sect_name_compact_unwind)
1561 if (section_name == g_sect_name_cfstring)
1563 if (section_name == g_sect_name_go_symtab)
1565 if (section_name == g_sect_name_ctf)
1567 if (section_name == g_sect_name_swift_ast)
1569 if (section_name == g_sect_name_objc_data ||
1570 section_name == g_sect_name_objc_classrefs ||
1571 section_name == g_sect_name_objc_superrefs ||
1572 section_name == g_sect_name_objc_const ||
1573 section_name == g_sect_name_objc_classlist) {
1577 switch (mach_sect_type) {
1580 if (section_name == g_sect_name_text)
1582 if (section_name == g_sect_name_data)
1587 case S_CSTRING_LITERALS:
1589 case S_4BYTE_LITERALS:
1591 case S_8BYTE_LITERALS:
1593 case S_LITERAL_POINTERS:
1595 case S_NON_LAZY_SYMBOL_POINTERS:
1597 case S_LAZY_SYMBOL_POINTERS:
1599 case S_SYMBOL_STUBS:
1602 case S_MOD_INIT_FUNC_POINTERS:
1605 case S_MOD_TERM_FUNC_POINTERS:
1615 case S_16BYTE_LITERALS:
1619 case S_LAZY_DYLIB_SYMBOL_POINTERS:
1639 const llvm::MachO::load_command &load_cmd_,
lldb::offset_t offset,
1641 llvm::MachO::segment_command_64 load_cmd;
1642 memcpy(&load_cmd, &load_cmd_,
sizeof(load_cmd_));
1644 if (!
m_data.
GetU8(&offset, (uint8_t *)load_cmd.segname, 16))
1649 const bool is_dsym = (
m_header.filetype == MH_DSYM);
1650 bool add_section =
true;
1651 bool add_to_unified =
true;
1653 load_cmd.segname, strnlen(load_cmd.segname,
sizeof(load_cmd.segname)));
1657 if (is_dsym && unified_section_sp) {
1661 add_to_unified =
false;
1665 add_section =
false;
1678 const bool segment_is_encrypted =
1679 (load_cmd.flags & SG_PROTECTED_VERSION_1) != 0;
1684 if (add_section && (const_segname || is_core)) {
1685 segment_sp = std::make_shared<Section>(
1705 segment_sp->SetIsEncrypted(segment_is_encrypted);
1707 segment_sp->SetPermissions(segment_permissions);
1710 }
else if (unified_section_sp) {
1720 if (is_dsym && unified_section_sp->GetFileAddress() != load_cmd.vmaddr) {
1724 "Installing dSYM's %s segment file address over ObjectFile's "
1725 "so symbol table/debug info resolves correctly for %s",
1727 module_sp->GetFileSpec().GetFilename().AsCString());
1732 module_sp->GetObjectFile()->GetSymtab();
1738 unified_section_sp->SetFileAddress(load_cmd.vmaddr);
1747 llvm::MachO::section_64 sect64;
1748 ::memset(§64, 0,
sizeof(sect64));
1753 uint32_t segment_sect_idx;
1756 const uint32_t num_u32s = load_cmd.cmd == LC_SEGMENT ? 7 : 8;
1757 for (segment_sect_idx = 0; segment_sect_idx < load_cmd.nsects;
1758 ++segment_sect_idx) {
1759 if (
m_data.
GetU8(&offset, (uint8_t *)sect64.sectname,
1760 sizeof(sect64.sectname)) ==
nullptr)
1762 if (
m_data.
GetU8(&offset, (uint8_t *)sect64.segname,
1763 sizeof(sect64.segname)) ==
nullptr)
1768 if (
m_data.
GetU32(&offset, §64.offset, num_u32s) ==
nullptr)
1781 sect64.sectname, strnlen(sect64.sectname,
sizeof(sect64.sectname)));
1782 if (!const_segname) {
1790 sizeof(sect64.segname));
1792 if (segment_sp.get()) {
1793 Section *segment = segment_sp.get();
1796 const lldb::addr_t sect64_max_addr = sect64_min_addr + sect64.size;
1800 curr_seg_min_addr + curr_seg_byte_size;
1801 if (sect64_min_addr >= curr_seg_min_addr) {
1803 sect64_max_addr - curr_seg_min_addr;
1805 if (new_seg_byte_size > curr_seg_byte_size)
1811 sect64_min_addr - curr_seg_min_addr;
1812 segment->
Slide(slide_amount,
false);
1814 segment->
SetByteSize(curr_seg_max_addr - sect64_min_addr);
1818 if (sect64.offset) {
1824 const lldb::addr_t section_min_file_offset = sect64.offset;
1826 section_min_file_offset + sect64.size;
1828 std::min(section_min_file_offset, segment_min_file_offset);
1830 std::max(section_max_file_offset, segment_max_file_offset) -
1837 segment_sp = std::make_shared<Section>(
1854 sect64.offset ? sect64.size : 0,
1859 segment_sp->SetIsFake(
true);
1860 segment_sp->SetPermissions(segment_permissions);
1864 segment_sp->SetIsEncrypted(segment_is_encrypted);
1867 assert(segment_sp.get());
1872 segment_sp, module_sp,
this, ++context.
NextSectionIdx, section_name,
1873 sect_type, sect64.addr - segment_sp->GetFileAddress(), sect64.size,
1874 sect64.offset, sect64.offset == 0 ? 0 : sect64.size, sect64.align,
1878 bool section_is_encrypted =
false;
1879 if (!segment_is_encrypted && load_cmd.filesize != 0)
1881 sect64.offset) !=
nullptr;
1883 section_sp->SetIsEncrypted(segment_is_encrypted || section_is_encrypted);
1884 section_sp->SetPermissions(segment_permissions);
1885 segment_sp->GetChildren().AddSection(section_sp);
1887 if (segment_sp->IsFake()) {
1889 const_segname.
Clear();
1893 if (segment_sp && is_dsym) {
1896 for (sect_uid = first_segment_sectID; sect_uid <= context.
NextSectionIdx;
1899 segment_sp->GetChildren().FindSectionByID(sect_uid));
1903 segment_sp->GetChildren().FindSectionByID(sect_uid + 1);
1905 if (curr_section_sp.get()) {
1906 if (curr_section_sp->GetByteSize() == 0) {
1907 if (next_section_sp.get() !=
nullptr)
1908 curr_section_sp->SetByteSize(next_section_sp->GetFileAddress() -
1909 curr_section_sp->GetFileAddress());
1911 curr_section_sp->SetByteSize(load_cmd.vmsize);
1920 const llvm::MachO::load_command &load_cmd,
lldb::offset_t offset) {
1924 (
sizeof(
m_dysymtab) /
sizeof(uint32_t)) - 2);
1940 llvm::MachO::load_command load_cmd;
1941 for (uint32_t i = 0; i <
m_header.ncmds; ++i) {
1946 if (load_cmd.cmd == LC_SEGMENT || load_cmd.cmd == LC_SEGMENT_64)
1948 else if (load_cmd.cmd == LC_DYSYMTAB)
1951 offset = load_cmd_offset + load_cmd.cmdsize;
1955 module_sp->SectionFileAddressesChanged();
1977 section_sp->GetFileAddress());
1979 section_sp->GetByteSize());
1981 std::string filename =
"<unknown>";
1983 if (first_section_sp)
1984 filename = first_section_sp->GetObjectFile()->GetFileSpec().GetPath();
1987 llvm::formatv(
"unable to find section {0} for a symbol in "
1988 "{1}, corrupt file?",
2018#define TRIE_SYMBOL_IS_THUMB (1ULL << 63)
2021 printf(
"0x%16.16llx 0x%16.16llx 0x%16.16llx \"%s\"",
2022 static_cast<unsigned long long>(
address),
2023 static_cast<unsigned long long>(
flags),
2046 printf(
"[%3u] 0x%16.16llx: ", idx,
2047 static_cast<unsigned long long>(
nodeOffset));
2057 const bool is_arm,
addr_t text_seg_base_addr,
2058 std::vector<llvm::StringRef> &nameSlices,
2059 std::set<lldb::addr_t> &resolver_addresses,
2060 std::vector<TrieEntryWithOffset> &reexports,
2061 std::vector<TrieEntryWithOffset> &ext_symbols) {
2067 const uint64_t terminalSize = data.
GetULEB128(&offset);
2069 if (terminalSize != 0) {
2072 const char *import_name =
nullptr;
2073 if (e.
entry.
flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
2076 import_name = data.
GetCStr(&offset);
2081 if (e.
entry.
flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
2085 resolver_addr += text_seg_base_addr;
2088 resolver_addresses.insert(resolver_addr);
2092 bool add_this_entry =
false;
2094 import_name && import_name[0]) {
2096 add_this_entry =
true;
2098 (import_name ==
nullptr || import_name[0] ==
'\0')) {
2101 add_this_entry =
true;
2103 if (add_this_entry) {
2105 if (!nameSlices.empty()) {
2106 for (
auto name_slice : nameSlices)
2107 name.append(name_slice.data(), name_slice.size());
2109 if (name.size() > 1) {
2118 reexports.push_back(e);
2124 ext_symbols.push_back(e);
2129 const uint8_t childrenCount = data.
GetU8(&children_offset);
2130 for (uint8_t i = 0; i < childrenCount; ++i) {
2131 const char *cstr = data.
GetCStr(&children_offset);
2133 nameSlices.push_back(llvm::StringRef(cstr));
2137 if (childNodeOffset) {
2139 nameSlices, resolver_addresses, reexports,
2144 nameSlices.pop_back();
2150 bool &demangled_is_synthesized,
2158 const char *symbol_sect_name = symbol_section->GetName().AsCString();
2159 if (symbol_section->IsDescendant(text_section_sp.get())) {
2160 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
2161 S_ATTR_SELF_MODIFYING_CODE |
2162 S_ATTR_SOME_INSTRUCTIONS))
2166 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
2167 symbol_section->IsDescendant(data_dirty_section_sp.get()) ||
2168 symbol_section->IsDescendant(data_const_section_sp.get())) {
2169 if (symbol_sect_name &&
2170 ::strstr(symbol_sect_name,
"__objc") == symbol_sect_name) {
2174 llvm::StringRef symbol_name_ref(symbol_name);
2175 if (symbol_name_ref.starts_with(
"OBJC_")) {
2176 static const llvm::StringRef g_objc_v2_prefix_class(
"OBJC_CLASS_$_");
2177 static const llvm::StringRef g_objc_v2_prefix_metaclass(
2178 "OBJC_METACLASS_$_");
2179 static const llvm::StringRef g_objc_v2_prefix_ivar(
"OBJC_IVAR_$_");
2180 if (symbol_name_ref.starts_with(g_objc_v2_prefix_class)) {
2181 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
2183 demangled_is_synthesized =
true;
2184 }
else if (symbol_name_ref.starts_with(g_objc_v2_prefix_metaclass)) {
2185 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
2187 demangled_is_synthesized =
true;
2188 }
else if (symbol_name_ref.starts_with(g_objc_v2_prefix_ivar)) {
2189 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
2191 demangled_is_synthesized =
true;
2195 }
else if (symbol_sect_name &&
2196 ::strstr(symbol_sect_name,
"__gcc_except_tab") ==
2202 }
else if (symbol_sect_name &&
2203 ::strstr(symbol_sect_name,
"__IMPORT") == symbol_sect_name) {
2209static std::optional<struct nlist_64>
2211 size_t nlist_byte_size) {
2212 struct nlist_64 nlist;
2235 LLDB_LOG(log,
"Parsing symbol table for {0}", file_name);
2236 Progress progress(
"Parsing symbol table", file_name);
2238 llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0};
2239 llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0};
2240 llvm::MachO::linkedit_data_command exports_trie_load_command = {0, 0, 0, 0};
2241 llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2242 llvm::MachO::dysymtab_command dysymtab =
m_dysymtab;
2251 llvm::DenseSet<addr_t> symbols_added;
2255 auto add_symbol_addr = [&symbols_added](
lldb::addr_t file_addr) {
2259 symbols_added.insert(file_addr);
2261 FunctionStarts function_starts;
2265 llvm::StringRef g_objc_v2_prefix_class(
"_OBJC_CLASS_$_");
2266 llvm::StringRef g_objc_v2_prefix_metaclass(
"_OBJC_METACLASS_$_");
2267 llvm::StringRef g_objc_v2_prefix_ivar(
"_OBJC_IVAR_$_");
2270 for (i = 0; i <
m_header.ncmds; ++i) {
2273 llvm::MachO::load_command lc;
2279 symtab_load_command.cmd = lc.cmd;
2280 symtab_load_command.cmdsize = lc.cmdsize;
2282 if (
m_data.
GetU32(&offset, &symtab_load_command.symoff, 4) ==
2288 case LC_DYLD_INFO_ONLY:
2289 if (
m_data.
GetU32(&offset, &dyld_info.rebase_off, 10)) {
2290 dyld_info.cmd = lc.cmd;
2291 dyld_info.cmdsize = lc.cmdsize;
2293 memset(&dyld_info, 0,
sizeof(dyld_info));
2298 case LC_LOAD_WEAK_DYLIB:
2299 case LC_REEXPORT_DYLIB:
2301 case LC_LOAD_UPWARD_DYLIB: {
2302 uint32_t name_offset = cmd_offset +
m_data.
GetU32(&offset);
2311 if (lc.cmd == LC_REEXPORT_DYLIB) {
2315 dylib_files.
Append(file_spec);
2319 case LC_DYLD_EXPORTS_TRIE:
2320 exports_trie_load_command.cmd = lc.cmd;
2321 exports_trie_load_command.cmdsize = lc.cmdsize;
2322 if (
m_data.
GetU32(&offset, &exports_trie_load_command.dataoff, 2) ==
2324 memset(&exports_trie_load_command, 0,
2325 sizeof(exports_trie_load_command));
2327 case LC_FUNCTION_STARTS:
2328 function_starts_load_command.cmd = lc.cmd;
2329 function_starts_load_command.cmdsize = lc.cmdsize;
2330 if (
m_data.
GetU32(&offset, &function_starts_load_command.dataoff, 2) ==
2332 memset(&function_starts_load_command, 0,
2333 sizeof(function_starts_load_command));
2340 image_uuid =
UUID(uuid_bytes, 16);
2347 offset = cmd_offset + lc.cmdsize;
2350 if (!symtab_load_command.cmd)
2354 if (section_list ==
nullptr)
2359 bool bit_width_32 = addr_byte_size == 4;
2360 const size_t nlist_byte_size =
2361 bit_width_32 ?
sizeof(
struct nlist) : sizeof(struct nlist_64);
2363 DataExtractor nlist_data(nullptr, 0, byte_order, addr_byte_size);
2364 DataExtractor strtab_data(nullptr, 0, byte_order, addr_byte_size);
2365 DataExtractor function_starts_data(nullptr, 0, byte_order, addr_byte_size);
2366 DataExtractor indirect_symbol_index_data(nullptr, 0, byte_order,
2368 DataExtractor dyld_trie_data(nullptr, 0, byte_order, addr_byte_size);
2370 const
addr_t nlist_data_byte_size =
2371 symtab_load_command.nsyms * nlist_byte_size;
2372 const
addr_t strtab_data_byte_size = symtab_load_command.strsize;
2375 ProcessSP process_sp(m_process_wp.lock());
2376 Process *process = process_sp.get();
2380 bool is_local_shared_cache_image = is_shared_cache_image && !
IsInMemory();
2382 section_list->FindSectionByName(GetSegmentNameLINKEDIT()));
2384 if (process && m_header.filetype != llvm::MachO::MH_OBJECT &&
2385 !is_local_shared_cache_image) {
2386 Target &target = process->GetTarget();
2392 if (linkedit_section_sp) {
2393 addr_t linkedit_load_addr =
2394 linkedit_section_sp->GetLoadBaseAddress(&target);
2400 linkedit_load_addr = CalculateSectionLoadAddressForMemoryImage(
2401 m_memory_addr, GetMachHeaderSection(), linkedit_section_sp.get());
2404 const addr_t linkedit_file_offset = linkedit_section_sp->GetFileOffset();
2405 const addr_t symoff_addr = linkedit_load_addr +
2406 symtab_load_command.symoff -
2407 linkedit_file_offset;
2408 strtab_addr = linkedit_load_addr + symtab_load_command.stroff -
2409 linkedit_file_offset;
2417 m_header.filetype == llvm::MachO::MH_DYLINKER) {
2419 ReadMemory(process_sp, symoff_addr, nlist_data_byte_size));
2421 nlist_data.SetData(nlist_data_sp, 0, nlist_data_sp->GetByteSize());
2422 if (dysymtab.nindirectsyms != 0) {
2423 const addr_t indirect_syms_addr = linkedit_load_addr +
2424 dysymtab.indirectsymoff -
2425 linkedit_file_offset;
2427 process_sp, indirect_syms_addr, dysymtab.nindirectsyms * 4));
2428 if (indirect_syms_data_sp)
2429 indirect_symbol_index_data.SetData(
2430 indirect_syms_data_sp, 0,
2431 indirect_syms_data_sp->GetByteSize());
2441 if (!is_shared_cache_image) {
2443 ReadMemory(process_sp, strtab_addr, strtab_data_byte_size));
2444 if (strtab_data_sp) {
2445 strtab_data.SetData(strtab_data_sp, 0,
2446 strtab_data_sp->GetByteSize());
2451 if (function_starts_load_command.cmd) {
2452 const addr_t func_start_addr =
2453 linkedit_load_addr + function_starts_load_command.dataoff -
2454 linkedit_file_offset;
2456 ReadMemory(process_sp, func_start_addr,
2457 function_starts_load_command.datasize));
2458 if (func_start_data_sp)
2459 function_starts_data.SetData(func_start_data_sp, 0,
2460 func_start_data_sp->GetByteSize());
2466 if (is_local_shared_cache_image) {
2474 lldb::addr_t linkedit_offset = linkedit_section_sp->GetFileOffset();
2477 symtab_load_command.symoff += linkedit_slide;
2478 symtab_load_command.stroff += linkedit_slide;
2479 dyld_info.export_off += linkedit_slide;
2480 dysymtab.indirectsymoff += linkedit_slide;
2481 function_starts_load_command.dataoff += linkedit_slide;
2482 exports_trie_load_command.dataoff += linkedit_slide;
2485 nlist_data.SetData(
m_data, symtab_load_command.symoff,
2486 nlist_data_byte_size);
2487 strtab_data.SetData(
m_data, symtab_load_command.stroff,
2488 strtab_data_byte_size);
2493 && (exports_trie_load_command.datasize > 0)));
2494 if (dyld_info.export_size > 0) {
2495 dyld_trie_data.SetData(
m_data, dyld_info.export_off,
2496 dyld_info.export_size);
2497 }
else if (exports_trie_load_command.datasize > 0) {
2498 dyld_trie_data.SetData(
m_data, exports_trie_load_command.dataoff,
2499 exports_trie_load_command.datasize);
2502 if (dysymtab.nindirectsyms != 0) {
2503 indirect_symbol_index_data.SetData(
m_data, dysymtab.indirectsymoff,
2504 dysymtab.nindirectsyms * 4);
2506 if (function_starts_load_command.cmd) {
2507 function_starts_data.SetData(
m_data, function_starts_load_command.dataoff,
2508 function_starts_load_command.datasize);
2512 const bool have_strtab_data = strtab_data.GetByteSize() > 0;
2531 if (text_section_sp.get())
2532 eh_frame_section_sp = text_section_sp->GetChildren().FindSectionByName(
2533 g_section_name_eh_frame);
2535 eh_frame_section_sp =
2538 const bool is_arm = (
m_header.cputype == llvm::MachO::CPU_TYPE_ARM);
2555 if (text_section_sp && function_starts_data.GetByteSize()) {
2556 FunctionStarts::Entry function_start_entry;
2557 function_start_entry.data =
false;
2559 function_start_entry.addr = text_section_sp->GetFileAddress();
2561 while ((delta = function_starts_data.GetULEB128(&function_start_offset)) >
2564 function_start_entry.addr += delta;
2566 if (function_start_entry.addr & 1) {
2568 function_start_entry.data =
true;
2569 }
else if (always_thumb) {
2570 function_start_entry.data =
true;
2573 function_starts.Append(function_start_entry);
2581 if (text_section_sp.get() && eh_frame_section_sp.get() &&
2587 addr_t text_base_addr = text_section_sp->GetFileAddress();
2588 size_t count = functions.
GetSize();
2589 for (
size_t i = 0; i < count; ++i) {
2593 FunctionStarts::Entry function_start_entry;
2594 function_start_entry.addr = func->
base - text_base_addr;
2596 if (function_start_entry.addr & 1) {
2598 function_start_entry.data =
true;
2599 }
else if (always_thumb) {
2600 function_start_entry.data =
true;
2603 function_starts.Append(function_start_entry);
2609 const size_t function_starts_count = function_starts.GetSize();
2622 Log *unwind_or_symbol_log(
GetLog(LLDBLog::Symbols | LLDBLog::Unwind));
2624 if (unwind_or_symbol_log)
2625 module_sp->LogMessage(
2626 unwind_or_symbol_log,
2627 "no LC_FUNCTION_STARTS, will not allow assembly profiled unwinds");
2630 const user_id_t TEXT_eh_frame_sectID = eh_frame_section_sp.get()
2631 ? eh_frame_section_sp->GetID()
2637 std::vector<uint32_t> N_FUN_indexes;
2638 std::vector<uint32_t> N_NSYM_indexes;
2639 std::vector<uint32_t> N_INCL_indexes;
2640 std::vector<uint32_t> N_BRAC_indexes;
2641 std::vector<uint32_t> N_COMM_indexes;
2642 typedef std::multimap<uint64_t, uint32_t> ValueToSymbolIndexMap;
2643 typedef llvm::DenseMap<uint32_t, uint32_t> NListIndexToSymbolIndexMap;
2644 typedef llvm::DenseMap<const char *, uint32_t> ConstNameToSymbolIndexMap;
2645 ValueToSymbolIndexMap N_FUN_addr_to_sym_idx;
2646 ValueToSymbolIndexMap N_STSYM_addr_to_sym_idx;
2647 ConstNameToSymbolIndexMap N_GSYM_name_to_sym_idx;
2650 NListIndexToSymbolIndexMap m_nlist_idx_to_sym_idx;
2651 uint32_t nlist_idx = 0;
2652 Symbol *symbol_ptr =
nullptr;
2654 uint32_t sym_idx = 0;
2656 size_t num_syms = 0;
2657 std::string memory_symbol_name;
2658 uint32_t unmapped_local_symbols_found = 0;
2660 std::vector<TrieEntryWithOffset> reexport_trie_entries;
2661 std::vector<TrieEntryWithOffset> external_sym_trie_entries;
2662 std::set<lldb::addr_t> resolver_addresses;
2664 const size_t dyld_trie_data_size = dyld_trie_data.
GetByteSize();
2665 if (dyld_trie_data_size > 0) {
2666 LLDB_LOG(log,
"Parsing {0} bytes of dyld trie data", dyld_trie_data_size);
2670 if (text_segment_sp)
2671 text_segment_file_addr = text_segment_sp->GetFileAddress();
2672 std::vector<llvm::StringRef> nameSlices;
2674 nameSlices, resolver_addresses, reexport_trie_entries,
2675 external_sym_trie_entries);
2678 typedef std::set<ConstString> IndirectSymbols;
2679 IndirectSymbols indirect_symbol_names;
2702 UUID process_shared_cache_uuid;
2703 addr_t process_shared_cache_base_addr;
2707 process_shared_cache_uuid);
2710 __block
bool found_image =
false;
2711 __block
void *nlist_buffer =
nullptr;
2712 __block
unsigned nlist_count = 0;
2713 __block
char *string_table =
nullptr;
2714 __block vm_offset_t vm_nlist_memory = 0;
2715 __block mach_msg_type_number_t vm_nlist_bytes_read = 0;
2716 __block vm_offset_t vm_string_memory = 0;
2717 __block mach_msg_type_number_t vm_string_bytes_read = 0;
2719 auto _ = llvm::make_scope_exit(^{
2720 if (vm_nlist_memory)
2721 vm_deallocate(
mach_task_self(), vm_nlist_memory, vm_nlist_bytes_read);
2722 if (vm_string_memory)
2723 vm_deallocate(
mach_task_self(), vm_string_memory, vm_string_bytes_read);
2726 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
2727 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
2728 UndefinedNameToDescMap undefined_name_to_desc;
2729 SymbolIndexToName reexport_shlib_needs_fixup;
2731 dyld_for_each_installed_shared_cache(^(dyld_shared_cache_t shared_cache) {
2733 dyld_shared_cache_copy_uuid(shared_cache, &cache_uuid);
2737 if (process_shared_cache_uuid.
IsValid() &&
2738 process_shared_cache_uuid != UUID::fromData(&cache_uuid, 16))
2741 dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) {
2746 dyld_image_copy_uuid(image, &dsc_image_uuid);
2747 if (image_uuid != UUID::fromData(dsc_image_uuid, 16))
2754 dyld_image_local_nlist_content_4Symbolication(
2755 image, ^(
const void *nlistStart, uint64_t nlistCount,
2756 const char *stringTable) {
2757 if (!nlistStart || !nlistCount)
2765 nlist_byte_size * nlistCount, &vm_nlist_memory,
2766 &vm_nlist_bytes_read);
2769 assert(vm_nlist_bytes_read == nlist_byte_size * nlistCount);
2774 vm_address_t string_address = (vm_address_t)stringTable;
2775 vm_size_t region_size;
2776 mach_msg_type_number_t info_count = VM_REGION_BASIC_INFO_COUNT_64;
2777 vm_region_basic_info_data_t info;
2778 memory_object_name_t object;
2780 ®ion_size, VM_REGION_BASIC_INFO_64,
2781 (vm_region_info_t)&info, &info_count, &
object);
2787 ((vm_address_t)stringTable - string_address),
2788 &vm_string_memory, &vm_string_bytes_read);
2792 nlist_buffer = (
void *)vm_nlist_memory;
2793 string_table = (
char *)vm_string_memory;
2794 nlist_count = nlistCount;
2800 nlist_count * nlist_byte_size,
2801 byte_order, addr_byte_size);
2802 unmapped_local_symbols_found = nlist_count;
2807 symtab_load_command.nsyms +
m_dysymtab.nindirectsyms +
2808 unmapped_local_symbols_found -
m_dysymtab.nlocalsym);
2813 for (uint32_t nlist_index = 0;
2814 nlist_index < nlist_count;
2818 std::optional<struct nlist_64> nlist_maybe =
2819 ParseNList(dsc_local_symbols_data, nlist_data_offset,
2823 struct nlist_64 nlist = *nlist_maybe;
2826 const char *symbol_name = string_table + nlist.n_strx;
2828 if (symbol_name == NULL) {
2833 "DSC unmapped local symbol[{0}] has invalid "
2834 "string table offset {1:x} in {2}, ignoring symbol",
2835 nlist_index, nlist.n_strx,
2836 module_sp->GetFileSpec().GetPath());
2839 if (symbol_name[0] ==
'\0')
2842 const char *symbol_name_non_abi_mangled = NULL;
2845 uint32_t symbol_byte_size = 0;
2846 bool add_nlist =
true;
2847 bool is_debug = ((nlist.n_type & N_STAB) != 0);
2848 bool demangled_is_synthesized =
false;
2849 bool is_gsym =
false;
2850 bool set_value =
true;
2852 assert(sym_idx < num_syms);
2857 switch (nlist.n_type) {
2876 if (symbol_name && symbol_name[0] ==
'_' &&
2877 symbol_name[1] ==
'O') {
2878 llvm::StringRef symbol_name_ref(symbol_name);
2879 if (symbol_name_ref.starts_with(
2880 g_objc_v2_prefix_class)) {
2881 symbol_name_non_abi_mangled = symbol_name + 1;
2883 symbol_name + g_objc_v2_prefix_class.size();
2885 demangled_is_synthesized =
true;
2887 }
else if (symbol_name_ref.starts_with(
2888 g_objc_v2_prefix_metaclass)) {
2889 symbol_name_non_abi_mangled = symbol_name + 1;
2891 symbol_name + g_objc_v2_prefix_metaclass.size();
2893 demangled_is_synthesized =
true;
2894 }
else if (symbol_name_ref.starts_with(
2895 g_objc_v2_prefix_ivar)) {
2896 symbol_name_non_abi_mangled = symbol_name + 1;
2898 symbol_name + g_objc_v2_prefix_ivar.size();
2900 demangled_is_synthesized =
true;
2903 if (nlist.n_value != 0)
2905 nlist.n_sect, nlist.n_value);
2920 nlist.n_sect, nlist.n_value);
2922 N_FUN_addr_to_sym_idx.insert(
2923 std::make_pair(nlist.n_value, sym_idx));
2927 N_FUN_indexes.push_back(sym_idx);
2931 if (!N_FUN_indexes.empty()) {
2938 N_FUN_indexes.pop_back();
2950 N_STSYM_addr_to_sym_idx.insert(
2951 std::make_pair(nlist.n_value, sym_idx));
2952 symbol_section = section_info.
GetSection(nlist.n_sect,
2954 if (symbol_name && symbol_name[0]) {
2962 symbol_section = section_info.
GetSection(nlist.n_sect,
2996 symbol_section = section_info.
GetSection(nlist.n_sect,
3009 if (symbol_name == NULL) {
3020 N_NSYM_indexes.clear();
3021 N_INCL_indexes.clear();
3022 N_BRAC_indexes.clear();
3023 N_COMM_indexes.clear();
3024 N_FUN_indexes.clear();
3030 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3031 if (N_SO_has_full_path) {
3032 if ((N_SO_index == sym_idx - 1) &&
3033 ((sym_idx - 1) < num_syms)) {
3039 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3045 N_SO_index = sym_idx;
3047 }
else if ((N_SO_index == sym_idx - 1) &&
3048 ((sym_idx - 1) < num_syms)) {
3053 const char *so_path = sym[sym_idx - 1]
3057 if (so_path && so_path[0]) {
3058 std::string full_so_path(so_path);
3059 const size_t double_slash_pos =
3060 full_so_path.find(
"//");
3061 if (double_slash_pos != std::string::npos) {
3072 &full_so_path[double_slash_pos + 1],
3073 FileSpec::Style::native);
3076 full_so_path.erase(0, double_slash_pos + 1);
3080 if (*full_so_path.rbegin() !=
'/')
3081 full_so_path +=
'/';
3082 full_so_path += symbol_name;
3086 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3090 N_SO_index = sym_idx;
3111 N_INCL_indexes.push_back(sym_idx);
3121 if (!N_INCL_indexes.empty()) {
3126 N_INCL_indexes.pop_back();
3161 symbol_section = section_info.
GetSection(nlist.n_sect,
3172 symbol_section = section_info.
GetSection(nlist.n_sect,
3174 N_BRAC_indexes.push_back(sym_idx);
3184 symbol_section = section_info.
GetSection(nlist.n_sect,
3186 if (!N_BRAC_indexes.empty()) {
3191 N_BRAC_indexes.pop_back();
3208 N_COMM_indexes.push_back(sym_idx);
3213 symbol_section = section_info.
GetSection(nlist.n_sect,
3224 if (!N_COMM_indexes.empty()) {
3229 N_COMM_indexes.pop_back();
3244 uint8_t n_type = N_TYPE & nlist.n_type;
3245 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
3249 const char *reexport_name_cstr =
3250 strtab_data.PeekCStr(nlist.n_value);
3251 if (reexport_name_cstr && reexport_name_cstr[0]) {
3254 reexport_name_cstr +
3255 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
3258 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
3260 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
3266 if (symbol_name && symbol_name[0]) {
3268 symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0));
3269 undefined_name_to_desc[undefined_name] = nlist.n_desc;
3281 symbol_section = section_info.
GetSection(nlist.n_sect,
3284 if (symbol_section == NULL) {
3290 if (TEXT_eh_frame_sectID == nlist.n_sect) {
3293 uint32_t section_type =
3294 symbol_section->Get() & SECTION_TYPE;
3296 switch (section_type) {
3297 case S_CSTRING_LITERALS:
3300 case S_4BYTE_LITERALS:
3303 case S_8BYTE_LITERALS:
3306 case S_LITERAL_POINTERS:
3309 case S_NON_LAZY_SYMBOL_POINTERS:
3313 case S_LAZY_SYMBOL_POINTERS:
3316 case S_SYMBOL_STUBS:
3320 case S_MOD_INIT_FUNC_POINTERS:
3324 case S_MOD_TERM_FUNC_POINTERS:
3332 case S_16BYTE_LITERALS:
3338 case S_LAZY_DYLIB_SYMBOL_POINTERS:
3342 switch (symbol_section->GetType()) {
3369 const char *symbol_sect_name =
3370 symbol_section->GetName().AsCString();
3371 if (symbol_section->IsDescendant(
3372 text_section_sp.get())) {
3373 if (symbol_section->IsClear(
3374 S_ATTR_PURE_INSTRUCTIONS |
3375 S_ATTR_SELF_MODIFYING_CODE |
3376 S_ATTR_SOME_INSTRUCTIONS))
3380 }
else if (symbol_section->IsDescendant(
3381 data_section_sp.get()) ||
3382 symbol_section->IsDescendant(
3383 data_dirty_section_sp.get()) ||
3384 symbol_section->IsDescendant(
3385 data_const_section_sp.get())) {
3386 if (symbol_sect_name &&
3387 ::strstr(symbol_sect_name,
"__objc") ==
3392 llvm::StringRef symbol_name_ref(symbol_name);
3393 if (symbol_name_ref.starts_with(
"_OBJC_")) {
3395 g_objc_v2_prefix_class(
3398 g_objc_v2_prefix_metaclass(
3399 "_OBJC_METACLASS_$_");
3401 g_objc_v2_prefix_ivar(
"_OBJC_IVAR_$_");
3402 if (symbol_name_ref.starts_with(
3403 g_objc_v2_prefix_class)) {
3404 symbol_name_non_abi_mangled =
3408 g_objc_v2_prefix_class.size();
3410 demangled_is_synthesized =
true;
3412 symbol_name_ref.starts_with(
3413 g_objc_v2_prefix_metaclass)) {
3414 symbol_name_non_abi_mangled =
3418 g_objc_v2_prefix_metaclass.size();
3420 demangled_is_synthesized =
true;
3421 }
else if (symbol_name_ref.starts_with(
3422 g_objc_v2_prefix_ivar)) {
3423 symbol_name_non_abi_mangled =
3427 g_objc_v2_prefix_ivar.size();
3429 demangled_is_synthesized =
true;
3433 }
else if (symbol_sect_name &&
3434 ::strstr(symbol_sect_name,
3435 "__gcc_except_tab") ==
3441 }
else if (symbol_sect_name &&
3442 ::strstr(symbol_sect_name,
"__IMPORT") ==
3445 }
else if (symbol_section->IsDescendant(
3446 objc_section_sp.get())) {
3448 if (symbol_name && symbol_name[0] ==
'.') {
3449 llvm::StringRef symbol_name_ref(symbol_name);
3451 g_objc_v1_prefix_class(
".objc_class_name_");
3452 if (symbol_name_ref.starts_with(
3453 g_objc_v1_prefix_class)) {
3454 symbol_name_non_abi_mangled = symbol_name;
3455 symbol_name = symbol_name +
3456 g_objc_v1_prefix_class.size();
3458 demangled_is_synthesized =
true;
3469 uint64_t symbol_value = nlist.n_value;
3470 if (symbol_name_non_abi_mangled) {
3476 if (symbol_name && symbol_name[0] ==
'_') {
3483 if (is_gsym && is_debug) {
3484 const char *gsym_name =
3490 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
3494 if (symbol_section) {
3495 const addr_t section_file_addr =
3496 symbol_section->GetFileAddress();
3497 if (symbol_byte_size == 0 &&
3498 function_starts_count > 0) {
3499 addr_t symbol_lookup_file_addr = nlist.n_value;
3504 FunctionStarts::Entry *func_start_entry =
3505 function_starts.FindEntry(symbol_lookup_file_addr,
3507 if (is_arm && func_start_entry) {
3511 if (func_start_entry->addr !=
3512 symbol_lookup_file_addr &&
3513 func_start_entry->addr !=
3514 (symbol_lookup_file_addr + 1)) {
3516 func_start_entry = NULL;
3519 if (func_start_entry) {
3520 func_start_entry->data =
true;
3522 addr_t symbol_file_addr = func_start_entry->addr;
3523 uint32_t symbol_flags = 0;
3525 if (symbol_file_addr & 1)
3530 const FunctionStarts::Entry *next_func_start_entry =
3531 function_starts.FindNextEntry(func_start_entry);
3532 const addr_t section_end_file_addr =
3534 symbol_section->GetByteSize();
3535 if (next_func_start_entry) {
3536 addr_t next_symbol_file_addr =
3537 next_func_start_entry->addr;
3543 symbol_byte_size = std::min<lldb::addr_t>(
3544 next_symbol_file_addr - symbol_file_addr,
3545 section_end_file_addr - symbol_file_addr);
3548 section_end_file_addr - symbol_file_addr;
3552 symbol_value -= section_file_addr;
3555 if (is_debug ==
false) {
3563 N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
3564 if (range.first != range.second) {
3565 bool found_it =
false;
3566 for (
auto pos = range.first; pos != range.second;
3568 if (sym[sym_idx].GetMangled().
GetName(
3572 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3578 sym[sym_idx].IsExternal());
3579 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3581 if (resolver_addresses.find(nlist.n_value) !=
3582 resolver_addresses.end())
3584 sym[sym_idx].
Clear();
3592 if (resolver_addresses.find(nlist.n_value) !=
3593 resolver_addresses.end())
3605 auto range = N_STSYM_addr_to_sym_idx.equal_range(
3607 if (range.first != range.second) {
3608 bool found_it =
false;
3609 for (
auto pos = range.first; pos != range.second;
3611 if (sym[sym_idx].GetMangled().
GetName(
3615 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
3621 sym[sym_idx].IsExternal());
3622 sym[pos->second].
SetFlags(nlist.n_type << 16 |
3624 sym[sym_idx].
Clear();
3632 const char *gsym_name =
3640 ConstNameToSymbolIndexMap::const_iterator pos =
3641 N_GSYM_name_to_sym_idx.find(gsym_name);
3642 if (pos != N_GSYM_name_to_sym_idx.end()) {
3643 const uint32_t GSYM_sym_idx = pos->second;
3644 m_nlist_idx_to_sym_idx[nlist_idx] =
3653 add_symbol_addr(sym[GSYM_sym_idx]
3660 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 |
3662 sym[sym_idx].
Clear();
3670 sym[sym_idx].
SetID(nlist_idx);
3676 sym[sym_idx].GetAddress().GetFileAddress());
3678 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
3680 if (symbol_byte_size > 0)
3683 if (demangled_is_synthesized)
3687 sym[sym_idx].
Clear();
3694 for (
const auto &pos : reexport_shlib_needs_fixup) {
3695 const auto undef_pos = undefined_name_to_desc.find(pos.second);
3696 if (undef_pos != undefined_name_to_desc.end()) {
3697 const uint8_t dylib_ordinal =
3698 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
3699 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
3709 if (nlist_data.GetByteSize() > 0) {
3713 if (sym ==
nullptr) {
3719 if (unmapped_local_symbols_found) {
3721 nlist_data_offset += (
m_dysymtab.nlocalsym * nlist_byte_size);
3727 typedef llvm::DenseMap<ConstString, uint16_t> UndefinedNameToDescMap;
3728 typedef llvm::DenseMap<uint32_t, ConstString> SymbolIndexToName;
3729 UndefinedNameToDescMap undefined_name_to_desc;
3730 SymbolIndexToName reexport_shlib_needs_fixup;
3738 auto ParseSymbolLambda = [&](
struct nlist_64 &nlist, uint32_t nlist_idx,
3740 const bool is_debug = ((nlist.n_type & N_STAB) != 0);
3741 if (is_debug != debug_only)
3744 const char *symbol_name_non_abi_mangled =
nullptr;
3745 const char *symbol_name =
nullptr;
3747 if (have_strtab_data) {
3748 symbol_name = strtab_data.PeekCStr(nlist.n_strx);
3750 if (symbol_name ==
nullptr) {
3754 "symbol[{0}] has invalid string table offset {1:x} in {2}, "
3756 nlist_idx, nlist.n_strx, module_sp->GetFileSpec().GetPath()));
3759 if (symbol_name[0] ==
'\0')
3760 symbol_name =
nullptr;
3762 const addr_t str_addr = strtab_addr + nlist.n_strx;
3764 if (process->ReadCStringFromMemory(str_addr, memory_symbol_name,
3766 symbol_name = memory_symbol_name.c_str();
3772 bool add_nlist =
true;
3773 bool is_gsym =
false;
3774 bool demangled_is_synthesized =
false;
3775 bool set_value =
true;
3777 assert(sym_idx < num_syms);
3781 switch (nlist.n_type) {
3798 if (symbol_name && symbol_name[0] ==
'_' && symbol_name[1] ==
'O') {
3799 llvm::StringRef symbol_name_ref(symbol_name);
3800 if (symbol_name_ref.starts_with(g_objc_v2_prefix_class)) {
3801 symbol_name_non_abi_mangled = symbol_name + 1;
3802 symbol_name = symbol_name + g_objc_v2_prefix_class.size();
3804 demangled_is_synthesized =
true;
3806 }
else if (symbol_name_ref.starts_with(
3807 g_objc_v2_prefix_metaclass)) {
3808 symbol_name_non_abi_mangled = symbol_name + 1;
3809 symbol_name = symbol_name + g_objc_v2_prefix_metaclass.size();
3811 demangled_is_synthesized =
true;
3812 }
else if (symbol_name_ref.starts_with(g_objc_v2_prefix_ivar)) {
3813 symbol_name_non_abi_mangled = symbol_name + 1;
3814 symbol_name = symbol_name + g_objc_v2_prefix_ivar.size();
3816 demangled_is_synthesized =
true;
3819 if (nlist.n_value != 0)
3821 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3836 section_info.
GetSection(nlist.n_sect, nlist.n_value);
3838 N_FUN_addr_to_sym_idx.insert(
3839 std::make_pair(nlist.n_value, sym_idx));
3843 N_FUN_indexes.push_back(sym_idx);
3847 if (!N_FUN_indexes.empty()) {
3852 N_FUN_indexes.pop_back();
3863 N_STSYM_addr_to_sym_idx.insert(
3864 std::make_pair(nlist.n_value, sym_idx));
3865 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3866 if (symbol_name && symbol_name[0]) {
3874 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3905 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
3917 if (symbol_name ==
nullptr) {
3928 N_NSYM_indexes.clear();
3929 N_INCL_indexes.clear();
3930 N_BRAC_indexes.clear();
3931 N_COMM_indexes.clear();
3932 N_FUN_indexes.clear();
3938 const bool N_SO_has_full_path = symbol_name[0] ==
'/';
3939 if (N_SO_has_full_path) {
3940 if ((N_SO_index == sym_idx - 1) && ((sym_idx - 1) < num_syms)) {
3945 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3950 N_SO_index = sym_idx;
3952 }
else if ((N_SO_index == sym_idx - 1) &&
3953 ((sym_idx - 1) < num_syms)) {
3957 const char *so_path =
3959 if (so_path && so_path[0]) {
3960 std::string full_so_path(so_path);
3961 const size_t double_slash_pos = full_so_path.find(
"//");
3962 if (double_slash_pos != std::string::npos) {
3970 so_dir.
SetFile(&full_so_path[double_slash_pos + 1],
3971 FileSpec::Style::native);
3974 full_so_path.erase(0, double_slash_pos + 1);
3978 if (*full_so_path.rbegin() !=
'/')
3979 full_so_path +=
'/';
3980 full_so_path += symbol_name;
3984 m_nlist_idx_to_sym_idx[nlist_idx] = sym_idx - 1;
3988 N_SO_index = sym_idx;
4008 N_INCL_indexes.push_back(sym_idx);
4017 if (!N_INCL_indexes.empty()) {
4021 N_INCL_indexes.pop_back();
4056 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4065 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4066 N_BRAC_indexes.push_back(sym_idx);
4075 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4076 if (!N_BRAC_indexes.empty()) {
4080 N_BRAC_indexes.pop_back();
4096 N_COMM_indexes.push_back(sym_idx);
4101 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4110 if (!N_COMM_indexes.empty()) {
4114 N_COMM_indexes.pop_back();
4128 uint8_t n_type = N_TYPE & nlist.n_type;
4129 sym[sym_idx].
SetExternal((N_EXT & nlist.n_type) != 0);
4133 const char *reexport_name_cstr = strtab_data.PeekCStr(nlist.n_value);
4134 if (reexport_name_cstr && reexport_name_cstr[0] && symbol_name) {
4137 ((reexport_name_cstr[0] ==
'_') ? 1 : 0));
4140 reexport_shlib_needs_fixup[sym_idx] = reexport_name;
4141 indirect_symbol_names.insert(
4142 ConstString(symbol_name + ((symbol_name[0] ==
'_') ? 1 : 0)));
4148 if (symbol_name && symbol_name[0]) {
4150 ((symbol_name[0] ==
'_') ? 1 : 0));
4151 undefined_name_to_desc[undefined_name] = nlist.n_desc;
4164 symbol_section = section_info.
GetSection(nlist.n_sect, nlist.n_value);
4166 if (!symbol_section) {
4172 if (TEXT_eh_frame_sectID == nlist.n_sect) {
4175 uint32_t section_type = symbol_section->Get() & SECTION_TYPE;
4177 switch (section_type) {
4178 case S_CSTRING_LITERALS:
4181 case S_4BYTE_LITERALS:
4184 case S_8BYTE_LITERALS:
4187 case S_LITERAL_POINTERS:
4190 case S_NON_LAZY_SYMBOL_POINTERS:
4193 case S_LAZY_SYMBOL_POINTERS:
4196 case S_SYMBOL_STUBS:
4200 case S_MOD_INIT_FUNC_POINTERS:
4203 case S_MOD_TERM_FUNC_POINTERS:
4210 case S_16BYTE_LITERALS:
4216 case S_LAZY_DYLIB_SYMBOL_POINTERS:
4220 switch (symbol_section->GetType()) {
4242 const char *symbol_sect_name =
4243 symbol_section->GetName().AsCString();
4244 if (symbol_section->IsDescendant(text_section_sp.get())) {
4245 if (symbol_section->IsClear(S_ATTR_PURE_INSTRUCTIONS |
4246 S_ATTR_SELF_MODIFYING_CODE |
4247 S_ATTR_SOME_INSTRUCTIONS))
4251 }
else if (symbol_section->IsDescendant(data_section_sp.get()) ||
4252 symbol_section->IsDescendant(
4253 data_dirty_section_sp.get()) ||
4254 symbol_section->IsDescendant(
4255 data_const_section_sp.get())) {
4256 if (symbol_sect_name &&
4257 ::strstr(symbol_sect_name,
"__objc") == symbol_sect_name) {
4261 llvm::StringRef symbol_name_ref(symbol_name);
4262 if (symbol_name_ref.starts_with(
"_OBJC_")) {
4263 llvm::StringRef g_objc_v2_prefix_class(
4265 llvm::StringRef g_objc_v2_prefix_metaclass(
4266 "_OBJC_METACLASS_$_");
4267 llvm::StringRef g_objc_v2_prefix_ivar(
4269 if (symbol_name_ref.starts_with(g_objc_v2_prefix_class)) {
4270 symbol_name_non_abi_mangled = symbol_name + 1;
4272 symbol_name + g_objc_v2_prefix_class.size();
4274 demangled_is_synthesized =
true;
4275 }
else if (symbol_name_ref.starts_with(
4276 g_objc_v2_prefix_metaclass)) {
4277 symbol_name_non_abi_mangled = symbol_name + 1;
4279 symbol_name + g_objc_v2_prefix_metaclass.size();
4281 demangled_is_synthesized =
true;
4282 }
else if (symbol_name_ref.starts_with(
4283 g_objc_v2_prefix_ivar)) {
4284 symbol_name_non_abi_mangled = symbol_name + 1;
4286 symbol_name + g_objc_v2_prefix_ivar.size();
4288 demangled_is_synthesized =
true;
4292 }
else if (symbol_sect_name &&
4293 ::strstr(symbol_sect_name,
"__gcc_except_tab") ==
4299 }
else if (symbol_sect_name &&
4300 ::strstr(symbol_sect_name,
"__IMPORT") ==
4303 }
else if (symbol_section->IsDescendant(objc_section_sp.get())) {
4305 if (symbol_name && symbol_name[0] ==
'.') {
4306 llvm::StringRef symbol_name_ref(symbol_name);
4307 llvm::StringRef g_objc_v1_prefix_class(
4308 ".objc_class_name_");
4309 if (symbol_name_ref.starts_with(g_objc_v1_prefix_class)) {
4310 symbol_name_non_abi_mangled = symbol_name;
4311 symbol_name = symbol_name + g_objc_v1_prefix_class.size();
4313 demangled_is_synthesized =
true;
4324 sym[sym_idx].
Clear();
4328 uint64_t symbol_value = nlist.n_value;
4330 if (symbol_name_non_abi_mangled) {
4336 if (symbol_name && symbol_name[0] ==
'_') {
4347 const char *gsym_name = sym[sym_idx]
4352 N_GSYM_name_to_sym_idx[gsym_name] = sym_idx;
4355 if (symbol_section) {
4356 const addr_t section_file_addr = symbol_section->GetFileAddress();
4357 if (symbol_byte_size == 0 && function_starts_count > 0) {
4358 addr_t symbol_lookup_file_addr = nlist.n_value;
4362 FunctionStarts::Entry *func_start_entry =
4363 function_starts.FindEntry(symbol_lookup_file_addr, !is_arm);
4364 if (is_arm && func_start_entry) {
4367 if (func_start_entry->addr != symbol_lookup_file_addr &&
4368 func_start_entry->addr != (symbol_lookup_file_addr + 1)) {
4370 func_start_entry =
nullptr;
4373 if (func_start_entry) {
4374 func_start_entry->data =
true;
4376 addr_t symbol_file_addr = func_start_entry->addr;
4380 const FunctionStarts::Entry *next_func_start_entry =
4381 function_starts.FindNextEntry(func_start_entry);
4382 const addr_t section_end_file_addr =
4383 section_file_addr + symbol_section->GetByteSize();
4384 if (next_func_start_entry) {
4385 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4390 symbol_byte_size = std::min<lldb::addr_t>(
4391 next_symbol_file_addr - symbol_file_addr,
4392 section_end_file_addr - symbol_file_addr);
4394 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4398 symbol_value -= section_file_addr;
4407 std::pair<ValueToSymbolIndexMap::const_iterator,
4408 ValueToSymbolIndexMap::const_iterator>
4410 range = N_FUN_addr_to_sym_idx.equal_range(nlist.n_value);
4411 if (range.first != range.second) {
4412 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4413 pos != range.second; ++pos) {
4417 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4421 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4422 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4423 if (resolver_addresses.find(nlist.n_value) !=
4424 resolver_addresses.end())
4426 sym[sym_idx].
Clear();
4431 if (resolver_addresses.find(nlist.n_value) !=
4432 resolver_addresses.end())
4442 std::pair<ValueToSymbolIndexMap::const_iterator,
4443 ValueToSymbolIndexMap::const_iterator>
4445 range = N_STSYM_addr_to_sym_idx.equal_range(nlist.n_value);
4446 if (range.first != range.second) {
4447 for (ValueToSymbolIndexMap::const_iterator pos = range.first;
4448 pos != range.second; ++pos) {
4452 m_nlist_idx_to_sym_idx[nlist_idx] = pos->second;
4456 sym[pos->second].
SetExternal(sym[sym_idx].IsExternal());
4457 sym[pos->second].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4458 sym[sym_idx].
Clear();
4464 const char *gsym_name = sym[sym_idx]
4469 ConstNameToSymbolIndexMap::const_iterator pos =
4470 N_GSYM_name_to_sym_idx.find(gsym_name);
4471 if (pos != N_GSYM_name_to_sym_idx.end()) {
4472 const uint32_t GSYM_sym_idx = pos->second;
4473 m_nlist_idx_to_sym_idx[nlist_idx] = GSYM_sym_idx;
4479 sym[GSYM_sym_idx].GetAddress().GetFileAddress());
4483 sym[GSYM_sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4484 sym[sym_idx].
Clear();
4492 sym[sym_idx].
SetID(nlist_idx);
4498 add_symbol_addr(sym[sym_idx].GetAddress().GetFileAddress());
4500 sym[sym_idx].
SetFlags(nlist.n_type << 16 | nlist.n_desc);
4501 if (nlist.n_desc & N_WEAK_REF)
4504 if (symbol_byte_size > 0)
4507 if (demangled_is_synthesized)
4516 std::vector<struct nlist_64> nlists;
4517 nlists.reserve(symtab_load_command.nsyms);
4518 for (; nlist_idx < symtab_load_command.nsyms; ++nlist_idx) {
4520 ParseNList(nlist_data, nlist_data_offset, nlist_byte_size))
4521 nlists.push_back(*nlist);
4531 for (
auto &nlist : nlists) {
4532 if (!ParseSymbolLambda(nlist, nlist_idx++,
DebugSymbols))
4538 for (
auto &nlist : nlists) {
4543 for (
const auto &pos : reexport_shlib_needs_fixup) {
4544 const auto undef_pos = undefined_name_to_desc.find(pos.second);
4545 if (undef_pos != undefined_name_to_desc.end()) {
4546 const uint8_t dylib_ordinal =
4547 llvm::MachO::GET_LIBRARY_ORDINAL(undef_pos->second);
4548 if (dylib_ordinal > 0 && dylib_ordinal < dylib_files.
GetSize())
4556 int trie_symbol_table_augment_count = 0;
4557 for (
auto &e : external_sym_trie_entries) {
4558 if (!symbols_added.contains(e.entry.address))
4559 trie_symbol_table_augment_count++;
4562 if (num_syms < sym_idx + trie_symbol_table_augment_count) {
4563 num_syms = sym_idx + trie_symbol_table_augment_count;
4564 sym = symtab.
Resize(num_syms);
4566 uint32_t synthetic_sym_id = symtab_load_command.nsyms;
4569 for (
auto &e : external_sym_trie_entries) {
4570 if (symbols_added.contains(e.entry.address))
4576 if (module_sp->ResolveFileAddress(e.entry.address, symbol_addr)) {
4578 const char *symbol_name = e.entry.name.GetCString();
4579 bool demangled_is_synthesized =
false;
4581 GetSymbolType(symbol_name, demangled_is_synthesized, text_section_sp,
4582 data_section_sp, data_dirty_section_sp,
4583 data_const_section_sp, symbol_section);
4586 if (symbol_section) {
4587 sym[sym_idx].
SetID(synthetic_sym_id++);
4589 if (demangled_is_synthesized)
4602 if (function_starts_count > 0) {
4603 uint32_t num_synthetic_function_symbols = 0;
4604 for (i = 0; i < function_starts_count; ++i) {
4605 if (!symbols_added.contains(function_starts.GetEntryRef(i).addr))
4606 ++num_synthetic_function_symbols;
4609 if (num_synthetic_function_symbols > 0) {
4610 if (num_syms < sym_idx + num_synthetic_function_symbols) {
4611 num_syms = sym_idx + num_synthetic_function_symbols;
4612 sym = symtab.
Resize(num_syms);
4614 for (i = 0; i < function_starts_count; ++i) {
4615 const FunctionStarts::Entry *func_start_entry =
4616 function_starts.GetEntryAtIndex(i);
4617 if (!symbols_added.contains(func_start_entry->addr)) {
4618 addr_t symbol_file_addr = func_start_entry->addr;
4619 uint32_t symbol_flags = 0;
4620 if (func_start_entry->data)
4623 if (module_sp->ResolveFileAddress(symbol_file_addr, symbol_addr)) {
4625 uint32_t symbol_byte_size = 0;
4626 if (symbol_section) {
4627 const addr_t section_file_addr = symbol_section->GetFileAddress();
4628 const FunctionStarts::Entry *next_func_start_entry =
4629 function_starts.FindNextEntry(func_start_entry);
4630 const addr_t section_end_file_addr =
4631 section_file_addr + symbol_section->GetByteSize();
4632 if (next_func_start_entry) {
4633 addr_t next_symbol_file_addr = next_func_start_entry->addr;
4636 symbol_byte_size = std::min<lldb::addr_t>(
4637 next_symbol_file_addr - symbol_file_addr,
4638 section_end_file_addr - symbol_file_addr);
4640 symbol_byte_size = section_end_file_addr - symbol_file_addr;
4642 sym[sym_idx].
SetID(synthetic_sym_id++);
4652 sym[sym_idx].
SetFlags(symbol_flags);
4653 if (symbol_byte_size)
4665 if (sym_idx < num_syms) {
4667 sym = symtab.
Resize(num_syms);
4672 if (indirect_symbol_index_data.GetByteSize()) {
4673 NListIndexToSymbolIndexMap::const_iterator end_index_pos =
4674 m_nlist_idx_to_sym_idx.end();
4681 if (symbol_stub_byte_size == 0)
4684 const uint32_t num_symbol_stubs =
4687 if (num_symbol_stubs == 0)
4690 const uint32_t symbol_stub_index_offset =
4692 for (uint32_t stub_idx = 0; stub_idx < num_symbol_stubs; ++stub_idx) {
4693 const uint32_t symbol_stub_index =
4694 symbol_stub_index_offset + stub_idx;
4697 (stub_idx * symbol_stub_byte_size);
4699 if (indirect_symbol_index_data.ValidOffsetForDataOfSize(
4700 symbol_stub_offset, 4)) {
4701 const uint32_t stub_sym_id =
4702 indirect_symbol_index_data.GetU32(&symbol_stub_offset);
4703 if (stub_sym_id & (INDIRECT_SYMBOL_ABS | INDIRECT_SYMBOL_LOCAL))
4706 NListIndexToSymbolIndexMap::const_iterator index_pos =
4707 m_nlist_idx_to_sym_idx.find(stub_sym_id);
4708 Symbol *stub_symbol =
nullptr;
4709 if (index_pos != end_index_pos) {
4720 Address so_addr(symbol_stub_addr, section_list);
4727 if (resolver_addresses.find(symbol_stub_addr) ==
4728 resolver_addresses.end())
4738 if (sym_idx >= num_syms) {
4739 sym = symtab.
Resize(++num_syms);
4740 stub_symbol =
nullptr;
4742 sym[sym_idx].
SetID(synthetic_sym_id++);
4743 sym[sym_idx].
GetMangled() = stub_symbol_mangled_name;
4744 if (resolver_addresses.find(symbol_stub_addr) ==
4745 resolver_addresses.end())
4757 log->
Warning(
"symbol stub referencing symbol table symbol "
4758 "%u that isn't in our minimal symbol table, "
4769 if (!reexport_trie_entries.empty()) {
4770 for (
const auto &e : reexport_trie_entries) {
4771 if (e.entry.import_name) {
4774 if (indirect_symbol_names.find(e.entry.name) ==
4775 indirect_symbol_names.end()) {
4777 if (sym_idx >= num_syms)
4778 sym = symtab.
Resize(++num_syms);
4779 sym[sym_idx].
SetID(synthetic_sym_id++);
4784 if (e.entry.other > 0 && e.entry.other <= dylib_files.
GetSize()) {
4798 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
4799 s->
Printf(
"%p: ",
static_cast<void *
>(
this));
4806 *s <<
", file = '" <<
m_file;
4810 base_spec, all_specs);
4811 for (
unsigned i = 0, e = all_specs.
GetSize(); i != e; ++i) {
4836 llvm::MachO::uuid_command load_cmd;
4839 for (i = 0; i < header.ncmds; ++i) {
4841 if (data.
GetU32(&offset, &load_cmd, 2) ==
nullptr)
4844 if (load_cmd.cmd == LC_UUID) {
4845 const uint8_t *uuid_bytes = data.
PeekData(offset, 16);
4851 const uint8_t opencl_uuid[] = {0x8c, 0x8e, 0xb3, 0x9b, 0x3b, 0xa8,
4852 0x4b, 0x16, 0xb6, 0xa4, 0x27, 0x63,
4853 0xbb, 0x14, 0xf0, 0x0d};
4855 if (!memcmp(uuid_bytes, opencl_uuid, 16))
4858 return UUID(uuid_bytes, 16);
4862 offset = cmd_offset + load_cmd.cmdsize;
4869 case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
4870 return llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4871 case llvm::MachO::LC_VERSION_MIN_MACOSX:
4872 return llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
4873 case llvm::MachO::LC_VERSION_MIN_TVOS:
4874 return llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4875 case llvm::MachO::LC_VERSION_MIN_WATCHOS:
4876 return llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4878 llvm_unreachable(
"unexpected LC_VERSION load command");
4884 llvm::StringRef os_type;
4885 llvm::StringRef environment;
4886 OSEnv(uint32_t cmd) {
4888 case llvm::MachO::PLATFORM_MACOS:
4889 os_type = llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
4891 case llvm::MachO::PLATFORM_IOS:
4892 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4894 case llvm::MachO::PLATFORM_TVOS:
4895 os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4897 case llvm::MachO::PLATFORM_WATCHOS:
4898 os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4900 case llvm::MachO::PLATFORM_BRIDGEOS:
4901 os_type = llvm::Triple::getOSTypeName(llvm::Triple::BridgeOS);
4903 case llvm::MachO::PLATFORM_DRIVERKIT:
4904 os_type = llvm::Triple::getOSTypeName(llvm::Triple::DriverKit);
4906 case llvm::MachO::PLATFORM_MACCATALYST:
4907 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4908 environment = llvm::Triple::getEnvironmentTypeName(llvm::Triple::MacABI);
4910 case llvm::MachO::PLATFORM_IOSSIMULATOR:
4911 os_type = llvm::Triple::getOSTypeName(llvm::Triple::IOS);
4913 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4915 case llvm::MachO::PLATFORM_TVOSSIMULATOR:
4916 os_type = llvm::Triple::getOSTypeName(llvm::Triple::TvOS);
4918 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4920 case llvm::MachO::PLATFORM_WATCHOSSIMULATOR:
4921 os_type = llvm::Triple::getOSTypeName(llvm::Triple::WatchOS);
4923 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4925 case llvm::MachO::PLATFORM_XROS:
4926 os_type = llvm::Triple::getOSTypeName(llvm::Triple::XROS);
4928 case llvm::MachO::PLATFORM_XROS_SIMULATOR:
4929 os_type = llvm::Triple::getOSTypeName(llvm::Triple::XROS);
4931 llvm::Triple::getEnvironmentTypeName(llvm::Triple::Simulator);
4934 Log *log(
GetLog(LLDBLog::Symbols | LLDBLog::Process));
4935 LLDB_LOGF(log,
"unsupported platform in LC_BUILD_VERSION");
4942 uint32_t major_version, minor_version, patch_version;
4943 MinOS(uint32_t version)
4944 : major_version(version >> 16), minor_version((version >> 8) & 0xffu),
4945 patch_version(version & 0xffu) {}
4956 if (!base_arch.IsValid())
4959 bool found_any =
false;
4960 auto add_triple = [&](
const llvm::Triple &triple) {
4961 auto spec = base_spec;
4963 if (spec.GetArchitecture().IsValid()) {
4971 llvm::Triple base_triple = base_arch.GetTriple();
4972 base_triple.setOS(llvm::Triple::UnknownOS);
4973 base_triple.setOSName(llvm::StringRef());
4975 if (header.filetype == MH_PRELOAD) {
4976 if (header.cputype == CPU_TYPE_ARM) {
4982 base_triple.setVendor(llvm::Triple::Apple);
4987 base_triple.setVendor(llvm::Triple::UnknownVendor);
4988 base_triple.setVendorName(llvm::StringRef());
4990 return add_triple(base_triple);
4993 llvm::MachO::load_command load_cmd;
4998 for (uint32_t i = 0; i < header.ncmds; ++i) {
5000 if (data.
GetU32(&offset, &load_cmd, 2) ==
nullptr)
5003 llvm::MachO::version_min_command version_min;
5004 switch (load_cmd.cmd) {
5005 case llvm::MachO::LC_VERSION_MIN_MACOSX:
5006 case llvm::MachO::LC_VERSION_MIN_IPHONEOS:
5007 case llvm::MachO::LC_VERSION_MIN_TVOS:
5008 case llvm::MachO::LC_VERSION_MIN_WATCHOS: {
5009 if (load_cmd.cmdsize !=
sizeof(version_min))
5014 MinOS min_os(version_min.version);
5015 llvm::SmallString<32> os_name;
5016 llvm::raw_svector_ostream os(os_name);
5017 os <<
GetOSName(load_cmd.cmd) << min_os.major_version <<
'.'
5018 << min_os.minor_version <<
'.' << min_os.patch_version;
5020 auto triple = base_triple;
5021 triple.setOSName(os.str());
5024 if (load_cmd.cmd != llvm::MachO::LC_VERSION_MIN_MACOSX &&
5025 (base_triple.getArch() == llvm::Triple::x86_64 ||
5026 base_triple.getArch() == llvm::Triple::x86)) {
5033 triple.setEnvironment(llvm::Triple::Simulator);
5042 offset = cmd_offset + load_cmd.cmdsize;
5048 for (uint32_t i = 0; i < header.ncmds; ++i) {
5050 if (data.
GetU32(&offset, &load_cmd, 2) ==
nullptr)
5054 if (load_cmd.cmd == llvm::MachO::LC_BUILD_VERSION) {
5055 llvm::MachO::build_version_command build_version;
5056 if (load_cmd.cmdsize <
sizeof(build_version)) {
5060 if (data.
ExtractBytes(cmd_offset,
sizeof(build_version),
5063 MinOS min_os(build_version.minos);
5064 OSEnv os_env(build_version.platform);
5065 llvm::SmallString<16> os_name;
5066 llvm::raw_svector_ostream os(os_name);
5067 os << os_env.os_type << min_os.major_version <<
'.'
5068 << min_os.minor_version <<
'.' << min_os.patch_version;
5069 auto triple = base_triple;
5070 triple.setOSName(os.str());
5072 if (!os_env.environment.empty())
5073 triple.setEnvironmentName(os_env.environment);
5077 offset = cmd_offset + load_cmd.cmdsize;
5081 add_triple(base_triple);
5086 ModuleSP module_sp,
const llvm::MachO::mach_header &header,
5091 base_spec, all_specs);
5096 const ArchSpec &module_arch = module_sp->GetArchitecture();
5097 for (
unsigned i = 0, e = all_specs.
GetSize(); i != e; ++i) {
5114 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
5127 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
5128 llvm::MachO::load_command load_cmd;
5130 std::vector<std::string> rpath_paths;
5131 std::vector<std::string> rpath_relative_paths;
5132 std::vector<std::string> at_exec_relative_paths;
5134 for (i = 0; i <
m_header.ncmds; ++i) {
5135 const uint32_t cmd_offset = offset;
5139 switch (load_cmd.cmd) {
5142 case LC_LOAD_WEAK_DYLIB:
5143 case LC_REEXPORT_DYLIB:
5144 case LC_LOAD_DYLINKER:
5146 case LC_LOAD_UPWARD_DYLIB: {
5147 uint32_t name_offset = cmd_offset +
m_data.
GetU32(&offset);
5152 bool is_delayed_init =
false;
5154 if (use_command_marker == 0x1a741800 ) {
5163 is_delayed_init =
true;
5166 if (path && !is_delayed_init) {
5167 if (load_cmd.cmd == LC_RPATH)
5168 rpath_paths.push_back(path);
5170 if (path[0] ==
'@') {
5171 if (strncmp(path,
"@rpath", strlen(
"@rpath")) == 0)
5172 rpath_relative_paths.push_back(path + strlen(
"@rpath"));
5173 else if (strncmp(path,
"@executable_path",
5174 strlen(
"@executable_path")) == 0)
5175 at_exec_relative_paths.push_back(path +
5176 strlen(
"@executable_path"));
5189 offset = cmd_offset + load_cmd.cmdsize;
5195 if (!rpath_paths.empty()) {
5197 const std::string this_directory =
5199 for (
auto &rpath : rpath_paths) {
5201 rpath = this_directory + rpath.substr(
g_loader_path.size());
5206 for (
const auto &rpath_relative_path : rpath_relative_paths) {
5207 for (
const auto &rpath : rpath_paths) {
5208 std::string path = rpath;
5209 path += rpath_relative_path;
5229 for (
const auto &at_exec_relative_path : at_exec_relative_paths) {