Skip to content

Commit 843498d

Browse files
authored
[LLD][AArch64] Add test for missing AArch64 BTI thunk (#116665)
A follow up to PR #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 #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 #116402. I've also removed a spurious --threads=1 I noticed in the original test aarch64-thunk-bti.s
1 parent 95ab426 commit 843498d

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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
5+
// RUN: llvm-objdump -d --no-show-raw-insn out | 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 the thunk for fn1 is placed after this section is
33+
/// sufficiently close to the target to be within short range, but only
34+
/// just so that a small displacement will mean a long thunk is needed.
35+
.space 0x1000
36+
/// Thunk for call to fn1 will be placed here. Initially it is in short Thunk
37+
/// range of fn1, but due to a thunk added after a later section it won't be
38+
/// and will need a long branch thunk, which in turn needs a BTI landing pad.
39+
40+
// CHECK-LABEL: <_start>:
41+
// CHECK-NEXT: 10001000: bl 0x10002004 <__AArch64AbsLongThunk_fn1>
42+
43+
/// FIXME, the 2nd ldr and udf are a result of mapping symbols being generated
44+
/// on Thunk insertion. When that is fixed in lld they will be data statements
45+
/// like in __AArch64AbsLongThunk_far below.
46+
// CHECK-LABEL: <__AArch64AbsLongThunk_fn1>:
47+
// CHECK-NEXT: 10002004: ldr x16, 0x1000200c <__AArch64AbsLongThunk_fn1+0x8>
48+
// CHECK-NEXT: br x16
49+
// CHECK-NEXT: ldr w0, 0x1000260c <__AArch64AbsLongThunk_fn1+0x608>
50+
// CHECK-NEXT: udf #0x0
51+
52+
53+
.section .text.1, "ax", %progbits
54+
.balign 0x1000
55+
.global farcall
56+
.type farcall, %function
57+
farcall:
58+
/// Call that requires a thunk.
59+
bl far
60+
/// Section is aligned to 0x1000 boundary with size multipe of 0x1000.
61+
.space 0x1000 - (. - farcall)
62+
/// Thunk for call to far will be placed here. This will force text.2
63+
/// on to the next alignment boundary, moving it further away from the
64+
/// thunk inserted in the .text_low output section.
65+
66+
// CHECK-LABEL: <farcall>:
67+
// CHECK-NEXT: 18001000: bl 0x18002000 <__AArch64AbsLongThunk_far>
68+
69+
// CHECK-LABEL: <__AArch64AbsLongThunk_far>:
70+
// CHECK-NEXT: 18002000: ldr x16, 0x18002008 <__AArch64AbsLongThunk_far+0x8>
71+
// CHECK-NEXT: br x16
72+
// CHECK-NEXT: 00 00 00 30 .word 0x30000000
73+
// CHECK-NEXT: 00 00 00 00 .word 0x00000000
74+
75+
.section .text.2, "ax", %progbits
76+
.balign 0x1000
77+
.global fn1
78+
.type fn1, %function
79+
fn1:
80+
ret
81+
82+
.section .text.far, "ax", %progbits
83+
.type far, %function
84+
.global far
85+
far:
86+
ret
87+
88+
// CHECK-LABEL: <__AArch64BTIThunk_fn1>:
89+
// CHECK-NEXT: 18003000: bti c
90+
// CHECK-NExT: b 0x18004000 <fn1>
91+
92+
// CHECK-LABEL: <fn1>:
93+
// CHECK-NEXT: 18004000: ret
94+
95+
// CHECK-LABEL: <__AArch64BTIThunk_far>:
96+
// CHECK-NEXT: 30000000: bti c
97+
98+
// CHECK-LABEL: <far>:
99+
// CHECK-NEXT: 30000004: ret
100+
101+
//--- lds
102+
PHDRS {
103+
low PT_LOAD FLAGS(0x1 | 0x4);
104+
mid PT_LOAD FLAGS(0x1 | 0x4);
105+
high PT_LOAD FLAGS(0x1 | 0x4);
106+
}
107+
SECTIONS {
108+
.rodata 0x10000000 : { *(.note.gnu.property) } :low
109+
.text_low : { *(.text.0) } :low
110+
.text 0x18001000 : { *(.text.1) } :mid
111+
.text_aligned : { *(.text.2) } :mid
112+
.text_high 0x30000000 : { *(.text.far) } :high
113+
}

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)