[llvm] [llvm-objdump][macho] Add support for ObjC relative method lists (PR #84250)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 6 14:57:20 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-binary-utilities

Author: None (alx32)

<details>
<summary>Changes</summary>

For Mach-O, ld64 supports the `-fobjc-relative-method-lists` flag which generates relative method lists for Objc classes/categories.
This change adds support for decoding/dumping of method lists in this format. The output format is closest feasible match to XCode 15.1's otool output. Tests are included for both 32bit and 64bit binaries.

The code style was matched as close as possible to existing implementation of parsing non-delta method lists. 

Diff between llvm-objdump and XCode 15.1 otool:
![objdump_vs_otool](https://github.com/llvm/llvm-project/assets/103613512/4fc04228-ed35-473d-b633-364402411b91)


---
Full diff: https://github.com/llvm/llvm-project/pull/84250.diff


4 Files Affected:

- (added) llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64.dylib () 
- (added) llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64_32.dylib () 
- (added) llvm/test/tools/llvm-objdump/MachO/AArch64/macho-relative-method-lists.test (+57) 
- (modified) llvm/tools/llvm-objdump/MachODump.cpp (+132) 


``````````diff
diff --git a/llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64.dylib b/llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64.dylib
new file mode 100755
index 00000000000000..9b39fc98fb5d96
Binary files /dev/null and b/llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64.dylib differ
diff --git a/llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64_32.dylib b/llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64_32.dylib
new file mode 100755
index 00000000000000..d3a339057abc34
Binary files /dev/null and b/llvm/test/tools/llvm-objdump/MachO/AArch64/Inputs/rel-method-lists-arm64_32.dylib differ
diff --git a/llvm/test/tools/llvm-objdump/MachO/AArch64/macho-relative-method-lists.test b/llvm/test/tools/llvm-objdump/MachO/AArch64/macho-relative-method-lists.test
new file mode 100644
index 00000000000000..672720a8b2938d
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/MachO/AArch64/macho-relative-method-lists.test
@@ -0,0 +1,57 @@
+RUN: llvm-objdump --macho --objc-meta-data    %p/Inputs/rel-method-lists-arm64_32.dylib | FileCheck %s --check-prefix=CHK32
+RUN: llvm-otool -ov                           %p/Inputs/rel-method-lists-arm64_32.dylib | FileCheck %s --check-prefix=CHK32
+
+RUN: llvm-objdump --macho --objc-meta-data    %p/Inputs/rel-method-lists-arm64.dylib    | FileCheck %s --check-prefix=CHK64
+RUN: llvm-otool -ov                           %p/Inputs/rel-method-lists-arm64.dylib    | FileCheck %s --check-prefix=CHK64
+
+CHK32:                 baseMethods 0x660 (struct method_list_t *)
+CHK32-NEXT:                 entsize 12 (relative)
+CHK32-NEXT:                   count 3
+CHK32-NEXT:                    name 0x144 (0x7ac) instance_method_00
+CHK32-NEXT:                   types 0x91 (0x6fd) v8 at 0:4
+CHK32-NEXT:                     imp 0xffffff18 (0x588) -[MyClass instance_method_00]
+CHK32-NEXT:                    name 0x13c (0x7b0) instance_method_01
+CHK32-NEXT:                   types 0x85 (0x6fd) v8 at 0:4
+CHK32-NEXT:                     imp 0xffffff28 (0x5a4) -[MyClass instance_method_01]
+CHK32-NEXT:                    name 0x134 (0x7b4) instance_method_02
+CHK32-NEXT:                   types 0x79 (0x6fd) v8 at 0:4
+CHK32-NEXT:                     imp 0xffffff38 (0x5c0) -[MyClass instance_method_02]
+
+CHK32:                 baseMethods 0x630 (struct method_list_t *)
+CHK32-NEXT:                 entsize 12 (relative)
+CHK32-NEXT:                   count 3
+CHK32-NEXT:                    name 0x180 (0x7b8) class_method_00
+CHK32-NEXT:                   types 0xc1 (0x6fd) v8 at 0:4
+CHK32-NEXT:                     imp 0xffffff9c (0x5dc) +[MyClass class_method_00]
+CHK32-NEXT:                    name 0x178 (0x7bc) class_method_01
+CHK32-NEXT:                   types 0xb5 (0x6fd) v8 at 0:4
+CHK32-NEXT:                     imp 0xffffffac (0x5f8) +[MyClass class_method_01]
+CHK32-NEXT:                    name 0x170 (0x7c0) class_method_02
+CHK32-NEXT:                   types 0xa9 (0x6fd) v8 at 0:4
+CHK32-NEXT:                     imp 0xffffffbc (0x614) +[MyClass class_method_02]
+
+CHK64:                  baseMethods 0x7d8 (struct method_list_t *)
+CHK64-NEXT:                  entsize 24
+CHK64-NEXT:                    count 3
+CHK64-NEXT:                     name 0x6a4 instance_method_00
+CHK64-NEXT:                    types 0x6dd v16 at 0:8
+CHK64-NEXT:                      imp -[MyClass instance_method_00]
+CHK64-NEXT:                     name 0x6b7 instance_method_01
+CHK64-NEXT:                    types 0x6dd v16 at 0:8
+CHK64-NEXT:                      imp -[MyClass instance_method_01]
+CHK64-NEXT:                     name 0x6ca instance_method_02
+CHK64-NEXT:                    types 0x6dd v16 at 0:8
+CHK64-NEXT:                      imp -[MyClass instance_method_02]
+
+CHK64:                  baseMethods 0x740 (struct method_list_t *)
+CHK64-NEXT:                  entsize 24
+CHK64-NEXT:                    count 3
+CHK64-NEXT:                     name 0x674 class_method_00
+CHK64-NEXT:                    types 0x6dd v16 at 0:8
+CHK64-NEXT:                      imp +[MyClass class_method_00]
+CHK64-NEXT:                     name 0x684 class_method_01
+CHK64-NEXT:                    types 0x6dd v16 at 0:8
+CHK64-NEXT:                      imp +[MyClass class_method_01]
+CHK64-NEXT:                     name 0x694 class_method_02
+CHK64-NEXT:                    types 0x6dd v16 at 0:8
+CHK64-NEXT:                      imp +[MyClass class_method_02]
diff --git a/llvm/tools/llvm-objdump/MachODump.cpp b/llvm/tools/llvm-objdump/MachODump.cpp
index 0e6935c0ac5895..21e21e2a1e3ec2 100644
--- a/llvm/tools/llvm-objdump/MachODump.cpp
+++ b/llvm/tools/llvm-objdump/MachODump.cpp
@@ -3519,6 +3519,12 @@ static const char *get_pointer_64(uint64_t Address, uint32_t &offset,
   return nullptr;
 }
 
+static const char *get_value_32(uint32_t Address, uint32_t &offset,
+                                uint32_t &left, SectionRef &S,
+                                DisassembleInfo *info, bool objc_only = false) {
+  return get_pointer_64(Address, offset, left, S, info, objc_only);
+}
+
 static const char *get_pointer_32(uint32_t Address, uint32_t &offset,
                                   uint32_t &left, SectionRef &S,
                                   DisassembleInfo *info,
@@ -3661,6 +3667,10 @@ struct class_ro32_t {
 #define RO_ROOT (1 << 1)
 #define RO_HAS_CXX_STRUCTORS (1 << 2)
 
+/* Values for method_list{64,32,_delta}_t->entsize */
+#define ML_HAS_DELTAS (1 << 31)
+#define ML_ENTSIZE_MASK 0xFFFF
+
 struct method_list64_t {
   uint32_t entsize;
   uint32_t count;
@@ -3673,6 +3683,12 @@ struct method_list32_t {
   /* struct method32_t first;  These structures follow inline */
 };
 
+struct method_list_delta_t {
+  uint32_t entsize;
+  uint32_t count;
+  /* struct method_delta_t first;  These structures follow inline */
+};
+
 struct method64_t {
   uint64_t name;  /* SEL (64-bit pointer) */
   uint64_t types; /* const char * (64-bit pointer) */
@@ -3685,6 +3701,12 @@ struct method32_t {
   uint32_t imp;   /* IMP (32-bit pointer) */
 };
 
+struct method_delta_t {
+  int32_t name;  /* SEL (32-bit delta) */
+  int32_t types; /* const char * (32-bit delta) */
+  int32_t imp;   /* IMP (32-bit delta) */
+};
+
 struct protocol_list64_t {
   uint64_t count; /* uintptr_t (a 64-bit value) */
   /* struct protocol64_t * list[0];  These pointers follow inline */
@@ -3974,6 +3996,11 @@ inline void swapStruct(struct method_list32_t &ml) {
   sys::swapByteOrder(ml.count);
 }
 
+inline void swapStruct(struct method_list_delta_t &ml) {
+  sys::swapByteOrder(ml.entsize);
+  sys::swapByteOrder(ml.count);
+}
+
 inline void swapStruct(struct method64_t &m) {
   sys::swapByteOrder(m.name);
   sys::swapByteOrder(m.types);
@@ -3986,6 +4013,12 @@ inline void swapStruct(struct method32_t &m) {
   sys::swapByteOrder(m.imp);
 }
 
+inline void swapStruct(struct method_delta_t &m) {
+  sys::swapByteOrder(m.name);
+  sys::swapByteOrder(m.types);
+  sys::swapByteOrder(m.imp);
+}
+
 inline void swapStruct(struct protocol_list64_t &pl) {
   sys::swapByteOrder(pl.count);
 }
@@ -4440,8 +4473,103 @@ static void print_layout_map32(uint32_t p, struct DisassembleInfo *info) {
   print_layout_map(layout_map, left);
 }
 
+// Return true if this is a delta method list, false otherwise
+static bool print_method_list_delta_t(uint64_t p, struct DisassembleInfo *info,
+                                      const char *indent,
+                                      uint32_t pointerBits) {
+  struct method_list_delta_t ml;
+  struct method_delta_t m;
+  const char *r, *name;
+  uint32_t offset, xoffset, left, i;
+  SectionRef S, xS;
+
+  r = get_pointer_32(p, offset, left, S, info);
+  if (r == nullptr)
+    return false;
+  memset(&ml, '\0', sizeof(struct method_list_delta_t));
+  if (left < sizeof(struct method_list_delta_t)) {
+    memcpy(&ml, r, left);
+    outs() << "   (method_delta_t entends past the end of the section)\n";
+  } else
+    memcpy(&ml, r, sizeof(struct method_list_delta_t));
+  if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
+    swapStruct(ml);
+  if ((ml.entsize & ML_HAS_DELTAS) == 0)
+    return false;
+
+  outs() << indent << "\t\t   entsize " << (ml.entsize & ML_ENTSIZE_MASK)
+         << " (relative) \n";
+  outs() << indent << "\t\t     count " << ml.count << "\n";
+
+  p += sizeof(struct method_list_delta_t);
+  offset += sizeof(struct method_delta_t);
+  for (i = 0; i < ml.count; i++) {
+    r = get_value_32(p, offset, left, S, info);
+    if (r == nullptr)
+      return true;
+    memset(&m, '\0', sizeof(struct method_delta_t));
+    if (left < sizeof(struct method_delta_t)) {
+      memcpy(&ml, r, left);
+      outs() << indent << "   (method_t entends past the end of the section)\n";
+    } else
+      memcpy(&m, r, sizeof(struct method_delta_t));
+    if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
+      swapStruct(m);
+
+    outs() << indent << "\t\t      name " << format("0x%" PRIx32, m.name);
+    uint64_t relNameRefVA = p + offsetof(struct method_delta_t, name);
+    uint64_t absNameRefVA = relNameRefVA + m.name;
+    outs() << " (" << format("0x%" PRIx32, absNameRefVA) << ")";
+
+    // since this is a delta list, absNameRefVA is the address of the
+    // __objc_selrefs entry, so a pointer, not the actual name
+    const char *nameRefPtr =
+        get_pointer_32(absNameRefVA, xoffset, left, xS, info);
+    if (nameRefPtr) {
+      uint32_t pointerSize = pointerBits / CHAR_BIT;
+      if (left < pointerSize)
+        outs() << indent << " (nameRefPtr entends past the end of the section)";
+      else {
+        uint64_t nameVA = 0;
+        memcpy(&nameVA, nameRefPtr, pointerSize);
+        const char *name = get_pointer_32(nameVA, xoffset, left, xS, info);
+        if (name != nullptr)
+          outs() << format(" %.*s", left, name);
+      }
+    }
+    outs() << "\n";
+
+    outs() << indent << "\t\t     types " << format("0x%" PRIx32, m.types);
+    uint64_t relTypesVA = p + offsetof(struct method_delta_t, types);
+    uint64_t absTypesVA = relTypesVA + m.types;
+    outs() << " (" << format("0x%" PRIx32, absTypesVA) << ")";
+    name = get_pointer_32(absTypesVA, xoffset, left, xS, info);
+    if (name != nullptr)
+      outs() << format(" %.*s", left, name);
+    outs() << "\n";
+
+    outs() << indent << "\t\t       imp " << format("0x%" PRIx32, m.imp);
+    uint64_t relImpVA = p + offsetof(struct method_delta_t, imp);
+    uint64_t absImpVA = relImpVA + m.imp;
+    outs() << " (" << format("0x%" PRIx32, absImpVA) << ")";
+    name = GuessSymbolName(absImpVA, info->AddrMap);
+    if (name != nullptr)
+      outs() << " " << name;
+    outs() << "\n";
+
+    p += sizeof(struct method_delta_t);
+    offset += sizeof(struct method_delta_t);
+  }
+
+  return true;
+}
+
 static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
                                   const char *indent) {
+  // Try to parse it as a delta list. If successful, just return
+  if (print_method_list_delta_t(p, info, indent, /*pointerBits=*/64))
+    return;
+
   struct method_list64_t ml;
   struct method64_t m;
   const char *r;
@@ -4535,6 +4663,10 @@ static void print_method_list64_t(uint64_t p, struct DisassembleInfo *info,
 
 static void print_method_list32_t(uint64_t p, struct DisassembleInfo *info,
                                   const char *indent) {
+  // Try to parse it as a delta list. If successful, just return
+  if (print_method_list_delta_t(p, info, indent, /*pointerBits=*/32))
+    return;
+
   struct method_list32_t ml;
   struct method32_t m;
   const char *r, *name;

``````````

</details>


https://github.com/llvm/llvm-project/pull/84250


More information about the llvm-commits mailing list