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

    <tr>
        <th>Summary</th>
        <td>
            ld64.lld: Linker failed on Xcode 14 when linking objc code
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    In Xcode 14 / ld 819, we are running into the following errors when linking against certain objective-c archive libraries:
```
ld64.lld: error: libUberTest_objc.a(UBDeallocObserver.o):(symbol +[UBRandomObserver observeRandomOn:callback:]+0x84): relocation BRANCH26 is out of range: 135504028 is not in [-134217728, 134217727]; references _objc_msgSend$copy
```

trace stack in llvm:
```
* thread #2, stop reason = breakpoint 5.1
    frame #0: 0x00000001011523b8 lld`lld::macho::reportRangeError(loc=0x00000003d2559d34, r=0x00000002ffc4e9d8, v=0x00000002ffc4e960, bits='\x1c', min=-134217728, max=134217727) at Relocations.cpp:93:16 [opt]
    frame #1: 0x00000001010836fc lld`void lld::macho::checkInt<lld::macho::Reloc>(loc=0x00000003d2559d34, d=Reloc @ 0x00000002ffc4e9d8, v=152416268, bits=28) at Relocations.h:93:5 [opt]
    frame #2: 0x0000000101082d74 lld`lld::macho::encodeBranch26(loc=0x00000003d2559d34, r=0x000000017b7bef18, base=2483027968, va=152416268) at ARM64Common.h:47:3 [opt]
  * frame #3: 0x0000000101082bd8 lld`lld::macho::ARM64Common::relocateOne(this=0x00000001052916c0, loc="", r=0x000000017b7bef18, value=174804288, pc=22388020) const at ARM64Common.cpp:66:5 [opt]
    frame #4: 0x000000010111e4d8 lld`lld::macho::ConcatInputSection::writeTo(this=0x000000012a1050b0, buf="\xff\xc3\U00000001\xd1\xfd{\U00000006\xa9\xfd\x83\U00000001\x91\xe8\U00000003\U00000001\xaa\xe1\U00000003\U00000002\xaa\xe3\U00000003") at InputSection.cpp:230:13 [opt]
    frame #5: 0x0000000101087980 lld`lld::macho::TextOutputSection::writeTo(this=0x000000017dfd9e00, buf="\xff\U00000003\U00000005\xd1\xe1\U00000003\U0000001e\xaa\xf5\U00000004") const at ConcatOutputSection.cpp:371:18 [opt]
    frame #6: 0x0000000101203900 lld`(anonymous namespace)::Writer::writeSections(this=0x000000016f58cff8, osec=0x000000017dfd9e00)::$_4::operator()(lld::macho::OutputSection const*) const at Writer.cpp:1153:11 [opt]
    frame #7: 0x00000001012038a0 lld`void llvm::parallelForEach<std::__1::__wrap_iter<lld::macho::OutputSection const**>, (anonymous namespace)::Writer::writeSections()::$_4>(this=0x000000016f58cfd8, I=1)::Writer::writeSections()::$_4)::'lambda'(unsigned long)::operator()(unsigned long) const at Parallel.h:234:47 [opt]
    frame #8: 0x0000000101203858 lld`void llvm::function_ref<void (unsigned long)>::callback_fn<void llvm::parallelForEach<std::__1::__wrap_iter<lld::macho::OutputSection const**>, (anonymous namespace)::Writer::writeSections(callable=6163058648, params=1)::$_4>(std::__1::__wrap_iter<lld::macho::OutputSection const**>, std::__1::__wrap_iter<lld::macho::OutputSection const**>, (anonymous namespace)::Writer::writeSections()::$_4)::'lambda'(unsigned long)>(long, unsigned long) at STLFunctionalExtras.h:45:12 [opt]
    frame #9: 0x00000001009c0918 lld`llvm::function_ref<void (unsigned long)>::operator(this=0x000000016f58cef8, params=1)(unsigned long) const at STLFunctionalExtras.h:68:12 [opt]
    frame #10: 0x00000001009cc9a8 lld`llvm::parallelFor(this=0x000060001bc83ae8)>)::$_1::operator()() const at Parallel.cpp:214:11 [opt]
    frame #11: 0x00000001009cc93c lld`decltype(__f=0x000060001bc83ae8)>)::$_1&>(fp)()) std::__1::__invoke<llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1&>(llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1&) at type_traits:3918:1 [opt]
    frame #12: 0x00000001009cc8f4 lld`void std::__1::__invoke_void_return_wrapper<void, true>::__call<llvm::parallelFor(__args=0x000060001bc83ae8)>)::$_1&>(llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1&) at invoke.h:61:9 [opt]
    frame #13: 0x00000001009cc8cc lld`std::__1::__function::__alloc_func<llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1, std::__1::allocator<llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1>, void ()>::operator(this=0x000060001bc83ae8)() at function.h:178:16 [opt]
    frame #14: 0x00000001009cb6d0 lld`std::__1::__function::__func<llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1, std::__1::allocator<llvm::parallelFor(unsigned long, unsigned long, llvm::function_ref<void (unsigned long)>)::$_1>, void ()>::operator(this=0x000060001bc83ae0)() at function.h:352:12 [opt]
    frame #15: 0x00000001009c89f8 lld`std::__1::__function::__value_func<void ()>::operator(this=0x000060002f41df88)() const at function.h:505:16 [opt]
    frame #16: 0x00000001009c0644 lld`std::__1::function<void ()>::operator(this=0x000060002f41df88)() const at function.h:1182:12 [opt]
    frame #17: 0x00000001009caacc lld`llvm::parallel::TaskGroup::spawn(this=0x000060002f41df88)>)::$_0::operator()() const at Parallel.cpp:177:7 [opt]
    frame #18: 0x00000001009caa8c lld`decltype(__f=0x000060002f41df88)>)::$_0&>(fp)()) std::__1::__invoke<llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0&>(llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0&) at type_traits:3918:1 [opt]
    frame #19: 0x00000001009caa44 lld`void std::__1::__invoke_void_return_wrapper<void, true>::__call<llvm::parallel::TaskGroup::spawn(__args=0x000060002f41df88)>)::$_0&>(llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0&) at invoke.h:61:9 [opt]
    frame #20: 0x00000001009caa1c lld`std::__1::__function::__alloc_func<llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0, std::__1::allocator<llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0>, void ()>::operator(this=0x000060002f41df88)() at function.h:178:16 [opt]
    frame #21: 0x00000001009c95c4 lld`std::__1::__function::__func<llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0, std::__1::allocator<llvm::parallel::TaskGroup::spawn(std::__1::function<void ()>)::$_0>, void ()>::operator(this=0x000060002f41df80)() at function.h:352:12 [opt]
    frame #22: 0x00000001009c89f8 lld`std::__1::__function::__value_func<void ()>::operator(this=0x00000002ffc4eeb8)() const at function.h:505:16 [opt]
    frame #23: 0x00000001009c0644 lld`std::__1::function<void ()>::operator(this= Lambda in File Parallel.cpp at Line 176)() const at function.h:1182:12 [opt]
    frame #24: 0x00000001009c3f8c lld`llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::work(this=0x00006000002368a0, S=(ThreadsRequested = 0, UseHyperThreads = true, Limit = false), ThreadID=0) at Parallel.cpp:117:7 [opt]
    frame #25: 0x00000001009c3e20 lld`llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(this=0x000060001b820028)::'lambda'()::operator()() const at Parallel.cpp:65:7 [opt]
    frame #26: 0x00000001009c3d24 lld`decltype(__f=0x000060001b820028)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()>(fp)()) std::__1::__invoke<llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()>(llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()&&) at type_traits:3918:1 [opt]
    frame #27: 0x00000001009c3cc0 lld`void std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()>(__t=size=2, (null)=__tuple_indices<> @ 0x00000002ffc4ef78)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()>&, std::__1::__tuple_indices<>) at thread:287:5 [opt]
    frame #28: 0x00000001009c3480 lld`void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, llvm::parallel::detail::(anonymous namespace)::ThreadPoolExecutor::ThreadPoolExecutor(llvm::ThreadPoolStrategy)::'lambda'()> >(__vp=0x000060001b820020) at thread:298:5 [opt]
    frame #29: 0x00000001945f426c libsystem_pthread.dylib`_pthread_start + 148
```

All the symbols are in our objc code,  we are able to mitigate this by disabling objc selector stubs with `-fno-objc-msgsend-selector-stubs` applied to all clang calls.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWltzozoS_jXOiyouJG7iwQ-57knV7M6pXGr3zSVA2JxgxCKRy_76bQlIbCzjOJeZM1WTchHQtfv7WupuUCzS59lVif6TiJQj7KEJuURFiiiOJuQMPXLEao7qpizzcoHyUgmklhxloijEoy7idS1qiR6XvERFXt7rMrZgeSkVSnit4A6J-C-eqPyBHycwXLKEO2gb16zOuZy4JxPnfOKcTAKn-5nHIg28aVGkUN9Oom-g113M61su1RxGTaZsQujd6TlnIE_yPZa8fuD1VExIpMclVD6vYlGAVqcT__Tu9JqVqVj17UAwc9OVltAlgYFiltzr3v45dHOeqNeOhmoOczCVixKdXp_86-wPEqBcItEoJDJUs3LBdTPs-r7jOYTqylIoQA3B5MfY9QgOQ0I1sP1DqGdxT2HsjNe8TLhERrH5Si5ueJlOiJeI6tmKUHtVNUs4kgqE1jMVxcNqF6QTcgLs1ZylAIhLtBxSiQomZxKUmrjnKIb7-0oA0cif4rYbgr-sZiuuezlaRefJaf-wg7FP3JgizVTgtHzBb8WSpWhva16JWl1reC4Mj4QCjjDZyyhuSnw_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