Skip to content

Commit 8aee057

Browse files
committed
[lldb][Mach-O] Handle shared cache binaries correctly (llvm#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 (cherry picked from commit 448ac7d)
1 parent 0123db6 commit 8aee057

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,11 +2241,11 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
22412241
LLDB_LOG(log, "Parsing symbol table for {0}", file_name);
22422242
Progress progress("Parsing symbol table", file_name);
22432243

2244-
llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0};
22452244
llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0};
22462245
llvm::MachO::linkedit_data_command exports_trie_load_command = {0, 0, 0, 0};
22472246
llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
22482247
llvm::MachO::dysymtab_command dysymtab = m_dysymtab;
2248+
SymtabCommandLargeOffsets symtab_load_command;
22492249
// The data element of type bool indicates that this entry is thumb
22502250
// code.
22512251
typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
@@ -2282,12 +2282,20 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
22822282
// Watch for the symbol table load command
22832283
switch (lc.cmd) {
22842284
case LC_SYMTAB:
2285+
// struct symtab_command {
2286+
// uint32_t cmd; /* LC_SYMTAB */
2287+
// uint32_t cmdsize; /* sizeof(struct symtab_command) */
2288+
// uint32_t symoff; /* symbol table offset */
2289+
// uint32_t nsyms; /* number of symbol table entries */
2290+
// uint32_t stroff; /* string table offset */
2291+
// uint32_t strsize; /* string table size in bytes */
2292+
// };
22852293
symtab_load_command.cmd = lc.cmd;
22862294
symtab_load_command.cmdsize = lc.cmdsize;
2287-
// Read in the rest of the symtab load command
2288-
if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) ==
2289-
nullptr) // fill in symoff, nsyms, stroff, strsize fields
2290-
return;
2295+
symtab_load_command.symoff = m_data.GetU32(&offset);
2296+
symtab_load_command.nsyms = m_data.GetU32(&offset);
2297+
symtab_load_command.stroff = m_data.GetU32(&offset);
2298+
symtab_load_command.strsize = m_data.GetU32(&offset);
22912299
break;
22922300

22932301
case LC_DYLD_INFO:

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
255255
bool IsValid() { return all_image_infos.size() > 0; }
256256
};
257257

258+
// The LC_SYMTAB's symtab_command structure uses 32-bit file offsets
259+
// for two fields, but ObjectFileMachO needs to calculate the offsets
260+
// in virtual address layout from the start of the TEXT segment, and
261+
// that span may be larger than 4GB.
262+
struct SymtabCommandLargeOffsets {
263+
uint32_t cmd = 0; /* LC_SYMTAB */
264+
uint32_t cmdsize = 0; /* sizeof(struct symtab_command) */
265+
lldb::offset_t symoff = 0; /* symbol table offset */
266+
uint32_t nsyms = 0; /* number of symbol table entries */
267+
lldb::offset_t stroff = 0; /* string table offset */
268+
uint32_t strsize = 0; /* string table size in bytes */
269+
};
270+
258271
/// Get the list of binary images that were present in the process
259272
/// when the corefile was produced.
260273
/// \return

0 commit comments

Comments
 (0)