Skip to content

Commit 448ac7d

Browse files
authored
[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
1 parent 611ccfc commit 448ac7d

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
@@ -2235,11 +2235,11 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
22352235
LLDB_LOG(log, "Parsing symbol table for {0}", file_name);
22362236
Progress progress("Parsing symbol table", file_name);
22372237

2238-
llvm::MachO::symtab_command symtab_load_command = {0, 0, 0, 0, 0, 0};
22392238
llvm::MachO::linkedit_data_command function_starts_load_command = {0, 0, 0, 0};
22402239
llvm::MachO::linkedit_data_command exports_trie_load_command = {0, 0, 0, 0};
22412240
llvm::MachO::dyld_info_command dyld_info = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
22422241
llvm::MachO::dysymtab_command dysymtab = m_dysymtab;
2242+
SymtabCommandLargeOffsets symtab_load_command;
22432243
// The data element of type bool indicates that this entry is thumb
22442244
// code.
22452245
typedef AddressDataArray<lldb::addr_t, bool, 100> FunctionStarts;
@@ -2276,12 +2276,20 @@ void ObjectFileMachO::ParseSymtab(Symtab &symtab) {
22762276
// Watch for the symbol table load command
22772277
switch (lc.cmd) {
22782278
case LC_SYMTAB:
2279+
// struct symtab_command {
2280+
// uint32_t cmd; /* LC_SYMTAB */
2281+
// uint32_t cmdsize; /* sizeof(struct symtab_command) */
2282+
// uint32_t symoff; /* symbol table offset */
2283+
// uint32_t nsyms; /* number of symbol table entries */
2284+
// uint32_t stroff; /* string table offset */
2285+
// uint32_t strsize; /* string table size in bytes */
2286+
// };
22792287
symtab_load_command.cmd = lc.cmd;
22802288
symtab_load_command.cmdsize = lc.cmdsize;
2281-
// Read in the rest of the symtab load command
2282-
if (m_data.GetU32(&offset, &symtab_load_command.symoff, 4) ==
2283-
nullptr) // fill in symoff, nsyms, stroff, strsize fields
2284-
return;
2289+
symtab_load_command.symoff = m_data.GetU32(&offset);
2290+
symtab_load_command.nsyms = m_data.GetU32(&offset);
2291+
symtab_load_command.stroff = m_data.GetU32(&offset);
2292+
symtab_load_command.strsize = m_data.GetU32(&offset);
22852293
break;
22862294

22872295
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
@@ -256,6 +256,19 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
256256
bool IsValid() { return all_image_infos.size() > 0; }
257257
};
258258

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

0 commit comments

Comments
 (0)