https://github.com/oskarwirga updated https://github.com/llvm/llvm-project/pull/234003
>From 7bba1b7acca12fe3b4fb2dbed3cdf00e3fb09952 Mon Sep 17 00:00:00 2001 From: Oskar Wirga <[email protected]> Date: Wed, 7 Oct 2026 10:07:53 -0400 Subject: [PATCH 1/3] [lld-macho] Call through authenticated stubs on arm64e ld64 calls dylib functions from arm64e code through __auth_stubs rather than __stubs. Each stub is four instructions instead of three: adrp x17, slot@page add x17, x17, slot@pageoff ldr x16, [x17] braa x16, x17 where the slot is in __auth_got and holds a pointer that dyld signs with the IA key, blended with the slot's address. The raw target is only ever held in x16 and x17, so a stub never branches through an unauthenticated pointer loaded from writable memory. Do the same: on arm64e, name the stubs section __auth_stubs, make stubs 16 bytes, and put stub targets in __auth_got. Fast ObjC stubs load _objc_msgSend from __auth_got and braa to it in the same way, as ld64's do; small ones already branch to its stub. __auth_stubs gets __stubs' place directly after __text. That is not just ld64 parity: TextOutputSection::needsThunks() estimates the reach of branches to the stubs as the size of __text plus the size of the stubs, which only holds if nothing sits between them. Sorted as an unknown section, __auth_stubs lands after all of __TEXT's read-only data, the estimate misses it, and thunking silently switches off; a large arm64e app then fails to link with out-of-range BRANCH26 relocations to its stubs. Assisted-by: Claude Code --- lld/MachO/Arch/ARM64.cpp | 52 ++++++++++++++ lld/MachO/InputSection.h | 1 + lld/MachO/OutputSegment.cpp | 2 + lld/MachO/SyntheticSections.cpp | 29 +++++--- .../MachO/arm64e-auth-stubs-section-order.s | 35 ++++++++++ lld/test/MachO/arm64e-stubs.s | 70 +++++++++++++++---- 6 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 lld/test/MachO/arm64e-auth-stubs-section-order.s diff --git a/lld/MachO/Arch/ARM64.cpp b/lld/MachO/Arch/ARM64.cpp index 76b549981c4d0a..0ba5422c1788b0 100644 --- a/lld/MachO/Arch/ARM64.cpp +++ b/lld/MachO/Arch/ARM64.cpp @@ -44,6 +44,10 @@ struct ARM64 : ARM64Common { struct ARM64e final : ARM64 { ARM64e(); + void writeStub(uint8_t *buf, const Symbol &, uint64_t) const override; + void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t &stubOffset, uint64_t selrefVA, + Symbol *objcMsgSend) const override; }; } // namespace @@ -243,8 +247,56 @@ TargetInfo *macho::createARM64TargetInfo() { return &t; } +// arm64e stubs authenticate the target they load from __auth_got. +static constexpr uint32_t authStubCode[] = { + 0x90000011, // 00: adrp x17, __auth_got@page + 0x91000231, // 04: add x17, x17, __auth_got@pageoff + 0xf9400230, // 08: ldr x16, [x17] + 0xd71f0a11, // 0c: braa x16, x17 +}; + +void ARM64e::writeStub(uint8_t *buf8, const Symbol &sym, + uint64_t pointerVA) const { + auto *buf32 = reinterpret_cast<uint32_t *>(buf8); + SymbolDiagnostic d = {&sym, "stub"}; + uint64_t pcPageBits = pageBits(in.stubs->addr + sym.stubsIndex * stubSize); + encodePage21(&buf32[0], d, authStubCode[0], pageBits(pointerVA) - pcPageBits); + encodePageOff12(&buf32[1], d, authStubCode[1], pointerVA); + buf32[2] = authStubCode[2]; + buf32[3] = authStubCode[3]; +} + +// The fast stubs reach _objc_msgSend through __auth_got in the same way. +static constexpr uint32_t authObjcStubsFastCode[] = { + 0x90000001, // adrp x1, __objc_selrefs@page + 0xf9400021, // ldr x1, [x1, @selector("foo")@pageoff] + 0x90000011, // adrp x17, __auth_got@page + 0x91000231, // add x17, x17, _objc_msgSend@pageoff + 0xf9400230, // ldr x16, [x17] + 0xd71f0a11, // braa x16, x17 + 0xd4200020, // brk #0x1 + 0xd4200020, // brk #0x1 +}; + +void ARM64e::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, + uint64_t &stubOffset, uint64_t selrefVA, + Symbol *objcMsgSend) const { + // Small stubs branch to _objc_msgSend or its stub, as on arm64. + if (config->objcStubsMode == ObjCStubsMode::small) { + ARM64::writeObjCMsgSendStub(buf, sym, stubsAddr, stubOffset, selrefVA, + objcMsgSend); + return; + } + ::writeObjCMsgSendFastStub<LP64>(buf, authObjcStubsFastCode, sym, stubsAddr, + stubOffset, selrefVA, + in.authGot->getVA(objcMsgSend), + /*msgSendIndex=*/0); + stubOffset += objcStubsFastSize; +} + ARM64e::ARM64e() { cpuSubtype = CPU_SUBTYPE_ARM64E; + stubSize = sizeof(authStubCode); relocAttrs = relocAttrsArray; } diff --git a/lld/MachO/InputSection.h b/lld/MachO/InputSection.h index e18aa2dadef673..5a58bdc099ede4 100644 --- a/lld/MachO/InputSection.h +++ b/lld/MachO/InputSection.h @@ -373,6 +373,7 @@ constexpr const char staticInit[] = "__StaticInit"; constexpr const char stringTable[] = "__string_table"; constexpr const char stubHelper[] = "__stub_helper"; constexpr const char stubs[] = "__stubs"; +constexpr const char authStubs[] = "__auth_stubs"; constexpr const char swift[] = "__swift"; constexpr const char symbolTable[] = "__symbol_table"; constexpr const char textCoalNt[] = "__textcoal_nt"; diff --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp index 6d8eadb703f5a1..c0eb59dd3b0d85 100644 --- a/lld/MachO/OutputSegment.cpp +++ b/lld/MachO/OutputSegment.cpp @@ -110,6 +110,8 @@ static int sectionOrder(OutputSection *osec) { int knownPriority = StringSwitch<int>(osec->name) .Case(section_names::stubs, -4) + // needsThunks() assumes that the stubs follow __text. + .Case(section_names::authStubs, -4) .Case(section_names::stubHelper, -3) .Case(section_names::objcStubs, -2) .Case(section_names::initOffsets, -1) diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp index c12d2b4d618f29..9ebf5c23212728 100644 --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -835,7 +835,9 @@ void WeakBindingSection::writeTo(uint8_t *buf) const { } StubsSection::StubsSection() - : SyntheticSection(segment_names::text, section_names::stubs) { + : SyntheticSection(segment_names::text, config->arch() == AK_arm64e + ? section_names::authStubs + : section_names::stubs) { flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; // The stubs section comprises machine instructions, which are aligned to // 4 bytes on the archs we care about. @@ -850,8 +852,13 @@ uint64_t StubsSection::getSize() const { void StubsSection::writeTo(uint8_t *buf) const { size_t off = 0; for (const Symbol *sym : entries) { - uint64_t pointerVA = - config->emitChainedFixups ? sym->getGotVA() : sym->getLazyPtrVA(); + uint64_t pointerVA; + if (!config->emitChainedFixups) + pointerVA = sym->getLazyPtrVA(); + else if (config->arch() == AK_arm64e) + pointerVA = in.authGot->getVA(sym); + else + pointerVA = sym->getGotVA(); target->writeStub(buf + off, *sym, pointerVA); off += target->stubSize; } @@ -891,10 +898,12 @@ void StubsSection::addEntry(Symbol *sym) { if (inserted) { sym->stubsIndex = entries.size() - 1; - if (config->emitChainedFixups) - in.got->addEntry(sym); - else + if (!config->emitChainedFixups) addBindingsForStub(sym); + else if (config->arch() == AK_arm64e) + in.authGot->addEntry(sym); + else + in.got->addEntry(sym); } } @@ -1061,8 +1070,12 @@ void ObjCStubsSection::setUp() { "lazy binding (normally in libobjc.dylib)"); objcMsgSend->used = true; if (config->objcStubsMode == ObjCStubsMode::fast) { - in.got->addEntry(objcMsgSend); - assert(objcMsgSend->isInGot()); + if (config->arch() == AK_arm64e) { + in.authGot->addEntry(objcMsgSend); + } else { + in.got->addEntry(objcMsgSend); + assert(objcMsgSend->isInGot()); + } } else { assert(config->objcStubsMode == ObjCStubsMode::small); // In line with ld64's behavior, when objc_msgSend is a direct symbol, diff --git a/lld/test/MachO/arm64e-auth-stubs-section-order.s b/lld/test/MachO/arm64e-auth-stubs-section-order.s new file mode 100644 index 00000000000000..b78cd404395f6c --- /dev/null +++ b/lld/test/MachO/arm64e-auth-stubs-section-order.s @@ -0,0 +1,35 @@ +# REQUIRES: aarch64 +## arm64e names the stubs section __auth_stubs rather than __stubs. Like +## __stubs, it must come directly after __text: needsThunks() assumes that when +## it estimates the reach of branches to the stubs. + +# RUN: llvm-mc -filetype=obj -triple=arm64e-apple-darwin %s -o %t.o +# RUN: %lld -arch arm64e -lSystem -U _external_func -o %t.out %t.o +# RUN: llvm-objdump --section-headers %t.out | FileCheck %s + +## __auth_stubs must come directly after __text, before any read-only data. +# CHECK: __text +# CHECK-NEXT: __auth_stubs + +## Same placement for plain arm64, where the section is called __stubs. +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t64.o +# RUN: %lld -arch arm64 -lSystem -U _external_func -o %t64.out %t64.o +# RUN: llvm-objdump --section-headers %t64.out | FileCheck --check-prefix=ARM64 %s + +# ARM64: __text +# ARM64-NEXT: __stubs + +.globl _main +.p2align 2 +_main: + stp x29, x30, [sp, #-16]! + bl _external_func + ldp x29, x30, [sp], #16 + ret + +.section __TEXT,__const +.p2align 4 +_rodata: + .space 64 + +.subsections_via_symbols diff --git a/lld/test/MachO/arm64e-stubs.s b/lld/test/MachO/arm64e-stubs.s index 4ed21ab9e0d6fd..1965ffe3a2e0e7 100644 --- a/lld/test/MachO/arm64e-stubs.s +++ b/lld/test/MachO/arm64e-stubs.s @@ -1,6 +1,8 @@ # REQUIRES: aarch64 -## On arm64e, calls to dylib functions go through __stubs, which load the -## target from an unsigned __got slot, as on arm64. +## On arm64e, calls to dylib functions go through __auth_stubs, which load the +## target from a signed __auth_got slot and authenticate it against the slot's +## address with braa, as ld64 does. Fast ObjC stubs reach _objc_msgSend the +## same way; small ones branch to its stub. # RUN: rm -rf %t; split-file %s %t # RUN: llvm-mc -filetype=obj -triple=arm64e-apple-macos -o %t/foo.o %t/foo.s @@ -8,35 +10,75 @@ # RUN: %lld -arch arm64e -platform_version macos 13.0 13.0 -dylib \ # RUN: -install_name @executable_path/libfoo.dylib %t/foo.o -o %t/libfoo.dylib # RUN: %lld -arch arm64e -platform_version macos 13.0 13.0 %t/test.o \ -# RUN: %t/libfoo.dylib -o %t/test +# RUN: %t/libfoo.dylib -objc_stubs_fast -o %t/test # RUN: llvm-objdump --macho --section-headers %t/test > %t/out # RUN: llvm-objdump --no-print-imm-hex --macho -d --no-show-raw-insn \ -# RUN: --section=__TEXT,__stubs %t/test >> %t/out +# RUN: --section=__TEXT,__auth_stubs --section=__TEXT,__objc_stubs \ +# RUN: %t/test >> %t/out # RUN: llvm-objdump --macho --dyld-info %t/test >> %t/out -# RUN: FileCheck %s < %t/out --implicit-check-not=__auth +# RUN: FileCheck %s < %t/out --implicit-check-not=__stubs \ +# RUN: --implicit-check-not=__got -# CHECK: __stubs -# CHECK-NEXT: __got {{[0-9a-f]+}} {{0*}}[[#%x,GOT:]] DATA +# CHECK: __text +# CHECK-NEXT: __auth_stubs {{0*}}00000010 +# CHECK: __auth_got {{[0-9a-f]+}} {{0*}}[[#%x,GOT:]] DATA # CHECK-LABEL: _main: # CHECK-NEXT: bl {{.*}} ; symbol stub for: _foo -# CHECK-LABEL: Contents of (__TEXT,__stubs) section -# CHECK-NEXT: adrp x16, {{.*}} ; 0x[[#GOT]] -# CHECK-NEXT: ldr x16, [x16] ; literal pool symbol address: _foo -# CHECK-NEXT: br x16 +# CHECK-LABEL: Contents of (__TEXT,__auth_stubs) section +# CHECK-NEXT: adrp x17, {{.*}} ; 0x[[#GOT]] +# CHECK-NEXT: add x17, x17, #[[#%u,FOO:]] ; literal pool symbol address: _foo +# CHECK-NEXT: ldr x16, [x17] +# CHECK-NEXT: braa x16, x17 +# CHECK-LABEL: Contents of (__TEXT,__objc_stubs) section +# CHECK-NEXT: _objc_msgSend$foo: +# CHECK-NEXT: adrp x1 +# CHECK-NEXT: ldr x1, [x1 +# CHECK-NEXT: adrp x17, {{.*}} ; 0x[[#GOT]] +# CHECK-NEXT: add x17, x17, #[[#%u,MSGSEND:]] +# CHECK-NEXT: ldr x16, [x17] +# CHECK-NEXT: braa x16, x17 +# CHECK-NEXT: brk #0x1 +# CHECK-NEXT: brk #0x1 -## The __got slot is an unauthenticated bind of _foo. +## Both __auth_got slots are authenticated binds, of _objc_msgSend and _foo, +## with the IA key, address diversity and no extra discriminator. # CHECK-LABEL: dyld information: -# CHECK: __DATA_CONST __got 0x[[#%X,GOT]] 0x4000000000000000 bind 0x0 libfoo _foo +# CHECK: __DATA_CONST __auth_got 0x[[#%X,GOT]] 0xC009000000000000 bind 0x0000 true IA 0x0 libfoo _objc_msgSend +# CHECK-NEXT: __DATA_CONST __auth_got 0x[[#%X,GOT+8]] 0xC001000000000001 bind 0x0000 true IA 0x0 libfoo _foo + +## Small ObjC stubs keep branching to _objc_msgSend's stub. +# RUN: %lld -arch arm64e -platform_version macos 13.0 13.0 %t/test.o \ +# RUN: %t/libfoo.dylib -objc_stubs_small -o %t/small +# RUN: llvm-objdump --no-print-imm-hex --macho -d --no-show-raw-insn \ +# RUN: --section=__TEXT,__objc_stubs %t/small | FileCheck %s --check-prefix=SMALL + +# SMALL-LABEL: Contents of (__TEXT,__objc_stubs) section +# SMALL-NEXT: _objc_msgSend$foo: +# SMALL-NEXT: adrp x1 +# SMALL-NEXT: ldr x1, [x1 +# SMALL-NEXT: b {{.*}} ; symbol stub for: _objc_msgSend #--- foo.s -.globl _foo +.globl _foo, _objc_msgSend _foo: ret +_objc_msgSend: + ret #--- test.s +.section __TEXT,__objc_methname,cstring_literals +lselref1: + .asciz "foo" + +.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip +.p2align 3 +.quad lselref1 + +.text .globl _main .p2align 2 _main: bl _foo + bl _objc_msgSend$foo ret >From 29bbbef325eb2376a33eb807f6e11b1ca76278fd Mon Sep 17 00:00:00 2001 From: Oskar Wirga <[email protected]> Date: Fri, 30 Oct 2026 11:43:32 -0400 Subject: [PATCH 2/3] Give __stubs and __auth_stubs distinct section priorities The other code sections already had -5, so move the header, __text and the code sections down by one to make room for __stubs. The order of the sections is unchanged; only the ties are gone. Assisted-by: Claude Code --- lld/MachO/OutputSegment.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lld/MachO/OutputSegment.cpp b/lld/MachO/OutputSegment.cpp index c0eb59dd3b0d85..540571722e586e 100644 --- a/lld/MachO/OutputSegment.cpp +++ b/lld/MachO/OutputSegment.cpp @@ -96,12 +96,12 @@ static int sectionOrder(OutputSection *osec) { // Sections are uniquely identified by their segment + section name. if (segname == segment_names::text) { if (osec->name == section_names::header) - return -7; + return -8; // `__text` needs to precede the other code sections since its // expected to be the largest. This means in effect that it will // be the section that determines whether we need thunks or not. if (osec->name == section_names::text) - return -6; + return -7; // Prioritize specific section ordering based on our knowledge. This ensures // that certain sections are placed in a particular order, even if they @@ -109,7 +109,7 @@ static int sectionOrder(OutputSection *osec) { // precedence over the general code section ordering. int knownPriority = StringSwitch<int>(osec->name) - .Case(section_names::stubs, -4) + .Case(section_names::stubs, -5) // needsThunks() assumes that the stubs follow __text. .Case(section_names::authStubs, -4) .Case(section_names::stubHelper, -3) @@ -126,7 +126,7 @@ static int sectionOrder(OutputSection *osec) { // Ensure all code sections are contiguous with `__text` for thunk // calculations. if (sections::isCodeSection(osec->name, segment_names::text, osec->flags)) { - return -5; + return -6; } return osec->inputOrder; >From 0521621551a69ad9ce7cbc2f905648bc768f5350 Mon Sep 17 00:00:00 2001 From: Oskar Wirga <[email protected]> Date: Fri, 30 Oct 2026 12:13:19 -0400 Subject: [PATCH 3/3] Check which __auth_got slot each stub loads The ObjC stub's slot was captured but never checked, so a stub loading the wrong slot would have passed. Assisted-by: Claude Code --- lld/test/MachO/arm64e-stubs.s | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lld/test/MachO/arm64e-stubs.s b/lld/test/MachO/arm64e-stubs.s index 1965ffe3a2e0e7..e3971075ae34cf 100644 --- a/lld/test/MachO/arm64e-stubs.s +++ b/lld/test/MachO/arm64e-stubs.s @@ -27,7 +27,7 @@ # CHECK-NEXT: bl {{.*}} ; symbol stub for: _foo # CHECK-LABEL: Contents of (__TEXT,__auth_stubs) section # CHECK-NEXT: adrp x17, {{.*}} ; 0x[[#GOT]] -# CHECK-NEXT: add x17, x17, #[[#%u,FOO:]] ; literal pool symbol address: _foo +# CHECK-NEXT: add x17, x17, #{{[0-9]+}} ; literal pool symbol address: _foo # CHECK-NEXT: ldr x16, [x17] # CHECK-NEXT: braa x16, x17 # CHECK-LABEL: Contents of (__TEXT,__objc_stubs) section @@ -35,7 +35,7 @@ # CHECK-NEXT: adrp x1 # CHECK-NEXT: ldr x1, [x1 # CHECK-NEXT: adrp x17, {{.*}} ; 0x[[#GOT]] -# CHECK-NEXT: add x17, x17, #[[#%u,MSGSEND:]] +# CHECK-NEXT: add x17, x17, #{{[0-9]+}} ; literal pool symbol address: _objc_msgSend # CHECK-NEXT: ldr x16, [x17] # CHECK-NEXT: braa x16, x17 # CHECK-NEXT: brk #0x1 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
