Skip to content

Commit 8d86963

Browse files
authored
[BOLT][AArch64] Fix error message for failed ADR relaxation (#142533)
Do not recommend the strict mode to the user when ADR relaxation fails on a non-simple function, i.e. a function with unknown CFG. We cannot rely on relocations to reconstruct compiler-generated jump tables for AArch64, hence strict mode does not work as intended.
1 parent b994299 commit 8d86963

File tree

6 files changed

+72
-85
lines changed

6 files changed

+72
-85
lines changed

bolt/lib/Passes/ADRRelaxationPass.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ void ADRRelaxationPass::runOnFunction(BinaryFunction &BF) {
8181
It = BB.eraseInstruction(std::prev(It));
8282
} else if (std::next(It) != BB.end() && BC.MIB->isNoop(*std::next(It))) {
8383
BB.eraseInstruction(std::next(It));
84-
} else if (!opts::StrictMode && !BF.isSimple()) {
84+
} else if (!BF.isSimple()) {
8585
// If the function is not simple, it may contain a jump table undetected
8686
// by us. This jump table may use an offset from the branch instruction
8787
// to land in the desired place. If we add new instructions, we
8888
// invalidate this offset, so we have to rely on linker-inserted NOP to
8989
// replace it with ADRP, and abort if it is not present.
9090
auto L = BC.scopeLock();
91-
BC.errs() << formatv(
92-
"BOLT-ERROR: Cannot relax adr in non-simple function "
93-
"{0}. Use --strict option to override\n",
94-
BF.getOneName());
91+
BC.errs() << "BOLT-ERROR: cannot relax ADR in non-simple function "
92+
<< BF << '\n';
9593
PassFailed = true;
9694
return;
9795
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Check that llvm-bolt generates a proper error message when ADR instruction
2+
## cannot be relaxed.
3+
4+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
5+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -static
6+
# RUN: not llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s
7+
# RUN: not llvm-bolt %t.exe -o %t.bolt --strict 2>&1 | FileCheck %s
8+
9+
# CHECK: BOLT-ERROR: cannot relax ADR in non-simple function _start
10+
11+
## The function contains unknown control flow and llvm-bolt fails to recover
12+
## CFG. As BOLT has to preserve the function layout, the ADR instruction cannot
13+
## be relaxed into ADRP+ADD.
14+
.text
15+
.globl _start
16+
.type _start, %function
17+
_start:
18+
.cfi_startproc
19+
adr x1, foo
20+
adr x2, .L1
21+
.L1:
22+
br x0
23+
ret x0
24+
.cfi_endproc
25+
.size _start, .-_start
26+
27+
.globl foo
28+
.type foo, %function
29+
foo:
30+
.cfi_startproc
31+
ret x0
32+
.cfi_endproc
33+
.size foo, .-foo

bolt/test/AArch64/adr-relaxation.s

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
44
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -static
55
# RUN: llvm-bolt %t.exe -o %t.bolt --split-functions --split-strategy=random2
6-
# RUN: llvm-objdump -d --disassemble-symbols=_start %t.bolt | FileCheck %s
6+
# RUN: llvm-objdump -d -j .text %t.bolt | FileCheck %s
77
# RUN: llvm-objdump -d --disassemble-symbols=foo.cold.0 %t.bolt \
88
# RUN: | FileCheck --check-prefix=CHECK-FOO %s
99

@@ -13,11 +13,10 @@
1313
.globl _start
1414
.type _start, %function
1515
_start:
16-
# CHECK: <_start>:
1716
.cfi_startproc
17+
# CHECK: <_start>:
18+
# CHECK-NEXT: adr
1819
adr x1, _start
19-
# CHECK-NOT: adrp
20-
# CHECK: adr
2120
cmp x1, x11
2221
b.hi .L1
2322
mov x0, #0x0
@@ -38,12 +37,27 @@ foo:
3837
mov x0, #0x0
3938
.L2:
4039
# CHECK-FOO: <foo.cold.0>:
41-
adr x1, foo
42-
# CHECK-FOO: adrp
40+
# CHECK-FOO-NEXT: adrp
4341
# CHECK-FOO-NEXT: add
42+
adr x1, foo
4443
ret x30
4544
.cfi_endproc
4645
.size foo, .-foo
4746

48-
## Force relocation mode.
49-
.reloc 0, R_AARCH64_NONE
47+
## bar is a non-simple function. We can still relax ADR, because it has a
48+
## preceding NOP.
49+
.globl bar
50+
.type bar, %function
51+
bar:
52+
.cfi_startproc
53+
# CHECK-LABEL: <bar>:
54+
# CHECK-NEXT: adrp
55+
# CHECK-NEXT: add
56+
nop
57+
adr x0, foo
58+
adr x1, .L3
59+
br x1
60+
.L3:
61+
ret x0
62+
.cfi_endproc
63+
.size bar, .-bar

bolt/test/AArch64/r_aarch64_prelxx.s

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
// CHECKPREL-NEXT: R_AARCH64_PREL32 {{.*}} _start + 4
1313
// CHECKPREL-NEXT: R_AARCH64_PREL64 {{.*}} _start + 8
1414

15-
// RUN: llvm-bolt %t.exe -o %t.bolt --strict
15+
// RUN: llvm-bolt %t.exe -o %t.bolt
1616
// RUN: llvm-objdump -D %t.bolt | FileCheck %s --check-prefix=CHECKPREL32
1717

1818
// CHECKPREL32: [[#%x,DATATABLEADDR:]] <datatable>:
1919
// CHECKPREL32-NEXT: 00:
2020
// CHECKPREL32-NEXT: 04: {{.*}} .word 0x[[#%x,VALUE:]]
2121

22-
// 4 is offset in datatable
23-
// 8 is addend
24-
// CHECKPREL32: [[#DATATABLEADDR + 4 - 8 + VALUE]] <_start>:
22+
// CHECKPREL32: [[#DATATABLEADDR + VALUE]] <_start>:
2523

2624
// RUN: llvm-objdump -D %t.bolt | FileCheck %s --check-prefix=CHECKPREL64
2725
// CHECKPREL64: [[#%x,DATATABLEADDR:]] <datatable>:
@@ -30,16 +28,15 @@
3028
// CHECKPREL64-NEXT: 08: {{.*}} .word 0x[[#%x,VALUE:]]
3129
// CHECKPREL64-NEXT: 0c: {{.*}} .word 0x00000000
3230

33-
// 8 is offset in datatable
34-
// 12 is addend
35-
// CHECKPREL64: [[#DATATABLEADDR + 8 - 12 + VALUE]] <_start>:
31+
// CHECKPREL64: [[#DATATABLEADDR + VALUE]] <_start>:
3632

3733
.section .text
3834
.align 4
3935
.globl _start
4036
.type _start, %function
4137
_start:
42-
adr x0, datatable
38+
adrp x0, datatable
39+
add x0, x0, :lo12:datable
4340
mov x0, #0
4441
ret
4542

bolt/test/AArch64/test-indirect-branch.s

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
// RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown -mattr=+pauth %s -o %t.o
99
// RUN: %clang %cflags --target=aarch64-unknown-linux %t.o -o %t.exe -Wl,-q
10-
// RUN: llvm-bolt %t.exe -o %t.bolt --print-cfg --strict --debug-only=mcplus \
11-
// RUN: -v=1 2>&1 | FileCheck %s
10+
// RUN: llvm-bolt %t.exe -o %t.bolt --print-cfg --debug-only=mcplus -v=1 2>&1 \
11+
// RUN: | FileCheck %s
1212

1313
// Pattern 1: there is no shift amount after the 'add' instruction.
1414
//
@@ -45,6 +45,7 @@ _start:
4545
.type test1, %function
4646
test1:
4747
mov x1, #0
48+
nop
4849
adr x3, datatable
4950
add x3, x3, x1, lsl #2
5051
ldr w2, [x3]
@@ -56,6 +57,11 @@ test1_1:
5657
ret
5758
test1_2:
5859
ret
60+
.size test1, .-test1
61+
62+
// Temporary workaround for PC-relative relocations from datatable leaking into
63+
// test2 function and creating phantom entry points.
64+
.skip 0x100
5965

6066
// Pattern 2
6167
// CHECK: BOLT-DEBUG: failed to match indirect branch: nop/adr instead of adrp/add
@@ -65,6 +71,7 @@ test2:
6571
nop
6672
adr x3, jump_table
6773
ldrh w3, [x3, x1, lsl #1]
74+
nop
6875
adr x1, test2_0
6976
add x3, x1, w3, sxth #2
7077
br x3

bolt/test/runtime/AArch64/adrrelaxationpass.s

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)