Skip to content

Commit 5de1d00

Browse files
authored
[lld-macho] Fix for objc_msgSend stubs (llvm#78557)
This commit corrects the address computation for objc_msgSend stubs. Previously, the address computation was incidentally correct due to objc_msgSend often being the first entry in the got section, resulting in a 0 index. This commit ensures accurate address computation regardless of the objc_msgSend stub's position in the got section.
1 parent 45d1cca commit 5de1d00

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lld/MachO/Arch/ARM64Common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,10 @@ writeObjCMsgSendStub(uint8_t *buf, const uint32_t objcStubsFastCode[8],
170170
pageBits(selrefsVA + selectorOffset) - pcPageBits(0));
171171
encodePageOff12(&buf32[1], d, objcStubsFastCode[1],
172172
selrefsVA + selectorOffset);
173+
uint64_t gotOffset = msgSendIndex * LP::wordSize;
173174
encodePage21(&buf32[2], d, objcStubsFastCode[2],
174-
pageBits(gotAddr) - pcPageBits(2));
175-
encodePage21(&buf32[3], d, objcStubsFastCode[3], msgSendIndex * LP::wordSize);
175+
pageBits(gotAddr + gotOffset) - pcPageBits(2));
176+
encodePageOff12(&buf32[3], d, objcStubsFastCode[3], gotAddr + gotOffset);
176177
buf32[4] = objcStubsFastCode[4];
177178
buf32[5] = objcStubsFastCode[5];
178179
buf32[6] = objcStubsFastCode[6];

lld/test/MachO/arm64-objc-stubs-fix.s

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# REQUIRES: aarch64
2+
3+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.o
4+
# RUN: %lld -arch arm64 -lSystem -fixup_chains -o %t.out %t.o
5+
# RUN: llvm-otool -vs __TEXT __objc_stubs %t.out | FileCheck %s --check-prefix=CHECK --check-prefix=FIRST
6+
7+
# Prepend a dummy entry to check if the address for _objc_msgSend is valid.
8+
# RUN: %lld -arch arm64 -lSystem -fixup_chains -e _dummy -U _dummy -o %t.out %t.o
9+
# RUN: llvm-otool -vs __TEXT __objc_stubs %t.out | FileCheck %s --check-prefix=CHECK --check-prefix=SECOND
10+
11+
# CHECK: Contents of (__TEXT,__objc_stubs) section
12+
13+
# CHECK-NEXT: _objc_msgSend$foo:
14+
# CHECK-NEXT: adrp x1, 8 ; 0x100008000
15+
# CHECK-NEXT: ldr x1, [x1]
16+
# CHECK-NEXT: adrp x16, 4 ; 0x100004000
17+
# FIRST-NEXT: ldr x16, [x16]
18+
# SECOND-NEXT:ldr x16, [x16, #0x8]
19+
# CHECK-NEXT: br x16
20+
# CHECK-NEXT: brk #0x1
21+
# CHECK-NEXT: brk #0x1
22+
# CHECK-NEXT: brk #0x1
23+
24+
# CHECK-EMPTY:
25+
26+
.text
27+
.globl _objc_msgSend
28+
_objc_msgSend:
29+
ret
30+
31+
.globl _main
32+
_main:
33+
bl _objc_msgSend$foo
34+
ret

0 commit comments

Comments
 (0)