Skip to content

Commit 51e222e

Browse files
authored
[BOLT][AArch64] Fix crash for conditional tail calls (#140669)
When conditional tail call is located in old code while BOLT is operating in lite mode, the call will require optional pending relocation with a type that is currently not supported resulting in a build-time crash. Before a proper fix is implemented, ignore conditional tail calls for relocation purposes and mark their target functions to be patched, i.e. to be served as veneers/thunks.
1 parent 747620d commit 51e222e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,10 +1783,22 @@ bool BinaryFunction::scanExternalRefs() {
17831783
// On AArch64, we use instruction patches for fixing references. We make an
17841784
// exception for branch instructions since they require optional
17851785
// relocations.
1786-
if (BC.isAArch64() && !BranchTargetSymbol) {
1787-
LLVM_DEBUG(BC.printInstruction(dbgs(), Instruction, AbsoluteInstrAddr));
1788-
InstructionPatches.push_back({AbsoluteInstrAddr, Instruction});
1789-
continue;
1786+
if (BC.isAArch64()) {
1787+
if (!BranchTargetSymbol) {
1788+
LLVM_DEBUG(BC.printInstruction(dbgs(), Instruction, AbsoluteInstrAddr));
1789+
InstructionPatches.push_back({AbsoluteInstrAddr, Instruction});
1790+
continue;
1791+
}
1792+
1793+
// Conditional tail calls require new relocation types that are currently
1794+
// not supported. https://github.com/llvm/llvm-project/issues/138264
1795+
if (BC.MIB->isConditionalBranch(Instruction)) {
1796+
if (BinaryFunction *TargetBF =
1797+
BC.getFunctionForSymbol(BranchTargetSymbol)) {
1798+
TargetBF->setNeedsPatch(true);
1799+
continue;
1800+
}
1801+
}
17901802
}
17911803

17921804
// Emit the instruction using temp emitter and generate relocations.

bolt/test/AArch64/lite-mode.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ cold_function:
129129
# CHECK-INPUT-NEXT: b {{.*}} <_start>
130130
# CHECK-NEXT: b {{.*}} <_start.org.0>
131131

132+
## Quick test for conditional tail calls. A proper test is being added in:
133+
## https://github.com/llvm/llvm-project/pull/139565
134+
## For now check that llvm-bolt doesn't choke on CTCs.
135+
.ifndef COMPACT
136+
b.eq _start
137+
cbz x0, _start
138+
tbz x0, 42, _start
139+
.endif
140+
132141
.cfi_endproc
133142
.size cold_function, .-cold_function
134143

0 commit comments

Comments
 (0)