Author: Jason Molenda Date: 2024-11-28T10:31:57-08:00 New Revision: 448ac7d3418a31d35b462440c8bf644287efac8a
URL: https://github.com/llvm/llvm-project/commit/448ac7d3418a31d35b462440c8bf644287efac8a DIFF: https://github.com/llvm/llvm-project/commit/448ac7d3418a31d35b462440c8bf644287efac8a.diff LOG: [lldb][Mach-O] Handle shared cache binaries correctly (#117832) The Mach-O load commands have an LC_SYMTAB / struct symtab_command which represents the offset of the symbol table (nlist records) and string table for this binary. In a mach-o binary on disk, these are file offsets. If a mach-o binary is loaded in memory with all segments consecutive, the `symoff` and `stroff` are the offsets from the TEXT segment (aka the mach-o header) virtual address to the virtual address of the start of these tables. However, if a Mach-O binary is a part of the shared cache, then the segments will be separated -- they will have different slide values. And it is possible for the LINKEDIT segment to be greater than 4GB away from the TEXT segment in the virtual address space, so these 32-bit offsets cannot express the offset from TEXT segment to these tables. Create separate uint64_t variables to track the offset to the symbol table and string table, instead of reusing the 32-bit ones in the symtab_command structure. rdar://140432279 Added: Modified: lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h Removed: ################################################################################ diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp index 079fd905037d45..4aa85a99edf01e 100644 --- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp +++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp @@ -2235,11 +2235,11 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) { LLDB_LOG(log, "Parsing symbol table for {0}", file_name); Progress progress("Parsing symbol table", file_name); - llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0}; llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0}; llvm::MachO::linkedit_data_command exports_trie_load_command = {0, 0, 0, 0}; llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; llvm::MachO::dysymtab_command dysymtab = m_dysymtab; + SymtabCommandLargeOffsets symtab_load_command; // The data element of type bool indicates that this entry is thumb // code. typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts; @@ -2276,12 +2276,20 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) { // Watch for the symbol table load command switch (lc.cmd) { case LC_SYMTAB: + // struct symtab_command { + // uint32_t cmd; /* LC_SYMTAB */ + // uint32_t cmdsize; /* sizeof(struct symtab_command) */ + // uint32_t symoff; /* symbol table offset */ + // uint32_t nsyms; /* number of symbol table entries */ + // uint32_t stroff; /* string table offset */ + // uint32_t strsize; /* string table size in bytes */ + // }; symtab_load_command.cmd = lc.cmd; symtab_load_command.cmdsize = lc.cmdsize; - // Read in the rest of the symtab load command - if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) == - nullptr) // fill in symoff, nsyms, stroff, strsize fields - return; + symtab_load_command.symoff = m_data.GetU32(&offset); + symtab_load_command.nsyms = m_data.GetU32(&offset); + symtab_load_command.stroff = m_data.GetU32(&offset); + symtab_load_command.strsize = m_data.GetU32(&offset); break; case LC_DYLD_INFO: diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h index be87112df7d898..27b2078b5a3fcd 100644 --- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h +++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h @@ -256,6 +256,19 @@ class ObjectFileMachO : public lldb_private::ObjectFile { bool IsValid() { return all_image_infos.size() > 0; } }; + // The LC_SYMTAB's symtab_command structure uses 32-bit file offsets + // for two fields, but ObjectFileMachO needs to calculate the offsets + // in virtual address layout from the start of the TEXT segment, and + // that span may be larger than 4GB. + struct SymtabCommandLargeOffsets { + uint32_t cmd = 0; /* LC_SYMTAB */ + uint32_t cmdsize = 0; /* sizeof(struct symtab_command) */ + lldb::offset_t symoff = 0; /* symbol table offset */ + uint32_t nsyms = 0; /* number of symbol table entries */ + lldb::offset_t stroff = 0; /* string table offset */ + uint32_t strsize = 0; /* string table size in bytes */ + }; + /// Get the list of binary images that were present in the process /// when the corefile was produced. /// \return _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits