Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2024

Conversation

alx32
Copy link
Contributor

@alx32 alx32 commented Mar 6, 2024

For Mach-O, ld64 supports the -fobjc-relative-method-lists flag which changes the format in which method lists are generated. The format uses delta encoding vs the original direct-pointer encoding.
This change adds support to llvm-objdump and llvm-otool for decoding/dumping of method lists in the delta format. Previously, if a binary with this information format was passed to the tooling, it would output invalid information, trying to parse the delta lists as pointer lists.
After this change, the tooling will output correct information if a binary in this format is encountered.
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

@alx32 alx32 force-pushed the 06_objdump_rel_sel branch from d33f060 to 3b97012 Compare March 6, 2024 22:54
@alx32 alx32 marked this pull request as ready for review March 6, 2024 22:56
@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2024

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

Author: None (alx32)

Changes

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


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 --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@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@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@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@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@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@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@0:8
+CHK64-NEXT:                      imp -[MyClass instance_method_00]
+CHK64-NEXT:                     name 0x6b7 instance_method_01
+CHK64-NEXT:                    types 0x6dd v16@0:8
+CHK64-NEXT:                      imp -[MyClass instance_method_01]
+CHK64-NEXT:                     name 0x6ca instance_method_02
+CHK64-NEXT:                    types 0x6dd v16@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@0:8
+CHK64-NEXT:                      imp +[MyClass class_method_00]
+CHK64-NEXT:                     name 0x684 class_method_01
+CHK64-NEXT:                    types 0x6dd v16@0:8
+CHK64-NEXT:                      imp +[MyClass class_method_01]
+CHK64-NEXT:                     name 0x694 class_method_02
+CHK64-NEXT:                    types 0x6dd v16@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;

@kyulee-com kyulee-com requested review from nico, keith and kyulee-com March 8, 2024 00:48
@alx32 alx32 force-pushed the 06_objdump_rel_sel branch from 3b97012 to 54df022 Compare March 8, 2024 22:56
@alx32 alx32 force-pushed the 06_objdump_rel_sel branch from a660c89 to 6080be0 Compare March 13, 2024 23:19
Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if others are happy.

@kyulee-com
Copy link
Contributor

LGTM

@kyulee-com kyulee-com merged commit aa61006 into llvm:main Mar 14, 2024
@antmox
Copy link
Contributor

antmox commented Mar 15, 2024

@alx32
Copy link
Contributor Author

alx32 commented Mar 15, 2024

Follow-up with fix for armv7 host: #85477

kyulee-com pushed a commit that referenced this pull request Mar 16, 2024
)

For Mach-O, ld64 supports the -fobjc-relative-method-lists flag which
changes the format in which method lists are generated. The format uses
delta encoding vs the original direct-pointer encoding.
This change adds support to llvm-objdump and llvm-otool for
decoding/dumping of method lists in the delta format. Previously, if a
binary with this information format was passed to the tooling, it would
output invalid information, trying to parse the delta lists as pointer
lists.
After this change, the tooling will output correct information if a
binary in this format is encountered.
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:

![image](https://github.com/llvm/llvm-project/assets/103613512/2277e3ff-d59c-4fff-b93a-e0587ee740a6)

Note: This is a retry of this PR:
#84250
On the original PR, the armv7+armv8 builds were failing due to absolute
offsets being different.

Co-authored-by: Alex B <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants