<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57809>57809</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            ld64.lld: Support synthesizing relative ObjC method lists
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld:MachO
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          BertalanD
      </td>
    </tr>
</table>

<pre>
    Starting with macOS 11, a more compact encoding is available for ObjC method lists. Previously, each method had a descriptor consisting of 3 pointers:
```c
struct _objc_method {
    SEL _cmd;
    char *method_type;
    char *_imp;
}
```

In the new format, `__TEXT,__objc_methlist` stores 32-bit offsets instead.

```c++
struct _objc_method_rel {
    int32_t _cmd;
    int32_t method_type;
    int32_t _imp;
}
```

This transformation can be controlled with the `-objc_relative_method_lists`/`-no_objc_relative_method_lists` linker flags.

Currently, method lists are stored in `__DATA,__objc_const`. With this transformation, Chromium Framework's data segment will shrink by about 40 KB, meaning that 3 fewer pages will have to be relocated at startup.

<details>

<summary>Sidenote: AppleClang itself can be coaxed into generating the relative encoding with `-fobjc-relative-method-lists`</summary>

```objc
// test.m
#include <Foundation/Foundation.h>

int main() {
    NSObject *foo = [[NSObject alloc] init];
}
```

```
❯ clang test.m  -S -emit-llvm  -o - | grep _objc_method
%struct.__method_list_t = type { i32, i32, [0 x %struct._objc_method] }
%struct._objc_method = type { i8*, i8*, i8* }

❯ clang test.m  -S -emit-llvm  -o - -fobjc-relative-method-lists | grep _objc_method
%struct.__method_list_t = type { i32, i32, [0 x %struct._objc_method_rel] }
%struct._objc_method_rel = type { i32, i32, i32 }
```
</details>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VsuOqzgQ_RqyKYEI5MWCRR4daTSPHiktzeyQwZXge42NbNN9M18_Zci7u696NhOhEFN2UefUOXZKzY_5zjHjhDrAm3A1NKx63sF4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