Skip to content

Commit 96e35d7

Browse files
committed
[LLD][AArch64] Add test for missing AArch64 BTI thunk
A follow up to PR llvm#116402 to add a regression test. The original change fixed the reproducer but that was not suitable to use as a regression test. This test case will fail with a LLD prior to llvm#116402. The disassembly for the thunk that starts as a short thunk but is later a long thunk isn't quite right. It is missing a $d mapping symbol. I think this can be fixed, but I've not done that in this patch to keep it test only. It is not a regression introduced in llvm#116402. I've also removed a spurious --threads=1 I noticed in the original test aarch64-thunk-bti.s
1 parent 52361d0 commit 96e35d7

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// REQUIRES: aarch64
2+
// RUN: rm -rf %t && split-file %s %t && cd %t
3+
// RUN: llvm-mc -filetype=obj -triple=aarch64 asm -o a.o
4+
// RUN: ld.lld --script=lds a.o -o out.exe
5+
// RUN: llvm-objdump -d --no-show-raw-insn out.exe | FileCheck %s
6+
7+
/// Test that a thunk that at creation time does not need to use a BTI
8+
/// compatible landing pad, but due to other thunk insertion ends up
9+
/// out of short-branch range so a BTI thunk is required after all.
10+
11+
//--- asm
12+
.section ".note.gnu.property", "a"
13+
.p2align 3
14+
.long 4
15+
.long 0x10
16+
.long 0x5
17+
.asciz "GNU"
18+
19+
/// Enable BTI.
20+
.long 0xc0000000 // GNU_PROPERTY_AARCH64_FEATURE_1_AND.
21+
.long 4
22+
.long 1 // GNU_PROPERTY_AARCH64_FEATURE_1_BTI.
23+
.long 0
24+
25+
.section .text.0, "ax", %progbits
26+
.balign 0x1000
27+
.global _start
28+
.type _start, %function
29+
_start:
30+
/// Call that requires a thunk.
31+
bl fn1
32+
/// padding so that we require thunks that can be placed after this section.
33+
/// The thunks are close enough to the target to be short.
34+
.space 0x1000
35+
/// Thunk for call to fn1 will be placed here. Initially it is in short Thunk
36+
/// range of fn1, but due to a thunk added after a later section it won't be
37+
/// and will need a long branch thunk, which in turn needs a BTI landing pad.
38+
39+
// CHECK-LABEL: <_start>:
40+
// CHECK-NEXT: 10001000: bl 0x10002004 <__AArch64AbsLongThunk_fn1>
41+
42+
/// FIXME, the 2nd ldr and udf are a result of mapping symbols being generated
43+
/// on Thunk insertion. When that is fixed in lld they will be data statements
44+
/// like in __AArch64AbsLongThunk_far below.
45+
// CHECK-LABEL: <__AArch64AbsLongThunk_fn1>:
46+
// CHECK-NEXT: 10002004: ldr x16, 0x1000200c <__AArch64AbsLongThunk_fn1+0x8>
47+
// CHECK-NEXT: br x16
48+
// CHECK-NEXT: ldr w0, 0x1000260c <__AArch64AbsLongThunk_fn1+0x608>
49+
// CHECK-NEXT: udf #0x0
50+
51+
52+
.section .text.1, "ax", %progbits
53+
.balign 0x1000
54+
.global farcall
55+
.type farcall, %function
56+
farcall:
57+
/// Call that requires a thunk.
58+
bl far
59+
/// Section is aligned to 0x1000 boundary with size multipe of 0x1000.
60+
.space 0x1000 - (. - farcall)
61+
/// Thunk for call to far will be placed here. This will force text.2
62+
/// on to the next alignment boundary, moving it further away from the
63+
/// thunk inserted in the .text_low output section.
64+
65+
// CHECK-LABEL: <farcall>:
66+
// CHECK-NEXT: 18001000: bl 0x18002000 <__AArch64AbsLongThunk_far>
67+
68+
// CHECK-LABEL: <__AArch64AbsLongThunk_far>:
69+
// CHECK-NEXT: 18002000: ldr x16, 0x18002008 <__AArch64AbsLongThunk_far+0x8>
70+
// CHECK-NEXT: br x16
71+
// CHECK-NEXT: 00 00 00 30 .word 0x30000000
72+
// CHECK-NEXT: 00 00 00 00 .word 0x00000000
73+
74+
.section .text.2, "ax", %progbits
75+
.balign 0x1000
76+
.global fn1
77+
.type fn1, %function
78+
fn1:
79+
ret
80+
81+
.section .text.far, "ax", %progbits
82+
.type far, %function
83+
.global far
84+
far:
85+
ret
86+
87+
// CHECK-LABEL: <__AArch64BTIThunk_fn1>:
88+
// CHECK-NEXT: 18003000: bti c
89+
// CHECK-NExT: b 0x18004000 <fn1>
90+
91+
// CHECK-LABEL: <fn1>:
92+
// CHECK-NEXT: 18004000: ret
93+
94+
// CHECK-LABEL: <__AArch64BTIThunk_far>:
95+
// CHECK-NEXT: 30000000: bti c
96+
97+
// CHECK-LABEL: <far>:
98+
// CHECK-NEXT: 30000004: ret
99+
100+
//--- lds
101+
PHDRS {
102+
low PT_LOAD FLAGS(0x1 | 0x4);
103+
mid PT_LOAD FLAGS(0x1 | 0x4);
104+
high PT_LOAD FLAGS(0x1 | 0x4);
105+
}
106+
SECTIONS {
107+
.rodata 0x10000000 : { *(.note.gnu.property) } :low
108+
.text_low : { *(.text.0) } :low
109+
.text 0x18001000 : { *(.text.1) } :mid
110+
.text_aligned : { *(.text.2) } :mid
111+
.text_high 0x30000000 : { *(.text.far) } :high
112+
}

lld/test/ELF/aarch64-thunk-bti.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// REQUIRES: aarch64
22
// RUN: rm -rf %t && split-file %s %t && cd %t
33
// RUN: llvm-mc -filetype=obj -triple=aarch64 asm -o a.o
4-
// RUN: ld.lld --threads=1 --shared --script=lds a.o -o out.so --defsym absolute=0xf0000000
4+
// RUN: ld.lld --shared --script=lds a.o -o out.so --defsym absolute=0xf0000000
55
// RUN: llvm-objdump -d --no-show-raw-insn out.so | FileCheck %s
66
// RUN: llvm-objdump -d --no-show-raw-insn out.so | FileCheck %s --check-prefix=CHECK-PADS
77
// RUN: llvm-mc -filetype=obj -triple=aarch64 shared -o shared.o

0 commit comments

Comments
 (0)