Skip to content

Commit 34c6c5e

Browse files
authored
[BOLT][AArch64] Fix PLT optimization (llvm#124192)
Preserve C++ exception metadata while running PLT optimization on AArch64.
1 parent 77c23fd commit 34c6c5e

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,11 +1426,12 @@ class MCPlusBuilder {
14261426
}
14271427

14281428
/// Creates an indirect call to the function within the \p DirectCall PLT
1429-
/// stub. The function's memory location is pointed by the \p TargetLocation
1429+
/// stub. The function's address location is pointed by the \p TargetLocation
14301430
/// symbol.
1431+
/// Move instruction annotations from \p DirectCall to the indirect call.
14311432
virtual InstructionListType
1432-
createIndirectPltCall(const MCInst &DirectCall,
1433-
const MCSymbol *TargetLocation, MCContext *Ctx) {
1433+
createIndirectPLTCall(MCInst &&DirectCall, const MCSymbol *TargetLocation,
1434+
MCContext *Ctx) {
14341435
llvm_unreachable("not implemented");
14351436
return {};
14361437
}

bolt/lib/Passes/PLTCall.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ Error PLTCall::runOnFunctions(BinaryContext &BC) {
7070
const BinaryFunction *CalleeBF = BC.getFunctionForSymbol(CallSymbol);
7171
if (!CalleeBF || !CalleeBF->isPLTFunction())
7272
continue;
73-
const InstructionListType NewCode = BC.MIB->createIndirectPltCall(
74-
*II, CalleeBF->getPLTSymbol(), BC.Ctx.get());
73+
const InstructionListType NewCode = BC.MIB->createIndirectPLTCall(
74+
std::move(*II), CalleeBF->getPLTSymbol(), BC.Ctx.get());
7575
II = BB.replaceInstruction(II, NewCode);
7676
assert(!NewCode.empty() && "PLT Call replacement must be non-empty");
7777
std::advance(II, NewCode.size() - 1);

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
12631263
return true;
12641264
}
12651265

1266-
InstructionListType createIndirectPltCall(const MCInst &DirectCall,
1266+
InstructionListType createIndirectPLTCall(MCInst &&DirectCall,
12671267
const MCSymbol *TargetLocation,
12681268
MCContext *Ctx) override {
12691269
const bool IsTailCall = isTailCall(DirectCall);
@@ -1297,8 +1297,7 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
12971297
MCInst InstCall;
12981298
InstCall.setOpcode(IsTailCall ? AArch64::BR : AArch64::BLR);
12991299
InstCall.addOperand(MCOperand::createReg(AArch64::X17));
1300-
if (IsTailCall)
1301-
setTailCall(InstCall);
1300+
moveAnnotations(std::move(DirectCall), InstCall);
13021301
Code.emplace_back(InstCall);
13031302

13041303
return Code;

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ class X86MCPlusBuilder : public MCPlusBuilder {
16051605
return true;
16061606
}
16071607

1608-
InstructionListType createIndirectPltCall(const MCInst &DirectCall,
1608+
InstructionListType createIndirectPLTCall(MCInst &&DirectCall,
16091609
const MCSymbol *TargetLocation,
16101610
MCContext *Ctx) override {
16111611
assert((DirectCall.getOpcode() == X86::CALL64pcrel32 ||

bolt/test/AArch64/exceptions-plt.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Verify that PLT optimization in BOLT preserves exception-handling info.
2+
3+
// REQUIRES: system-linux
4+
5+
// RUN: %clangxx %cxxflags -O1 -Wl,-q,-znow %s -o %t.exe
6+
// RUN: llvm-bolt %t.exe -o %t.bolt.exe --plt=all --print-only=.*main.* \
7+
// RUN: --print-finalized 2>&1 | FileCheck %s
8+
9+
// CHECK-LABEL: Binary Function
10+
// CHECK: adrp {{.*}}__cxa_throw
11+
// CHECK-NEXT: ldr {{.*}}__cxa_throw
12+
// CHECK-NEXT: blr x17 {{.*}} handler: {{.*}} PLTCall:
13+
14+
int main() {
15+
try {
16+
throw new int;
17+
} catch (...) {
18+
return 0;
19+
}
20+
return 1;
21+
}

bolt/test/runtime/exceptions-plt.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Verify that PLT optimization in BOLT preserves exception-handling info.
2+
3+
// REQUIRES: system-linux
4+
5+
// RUN: %clangxx %cxxflags -O1 -Wl,-q,-znow %s -o %t.exe
6+
// RUN: llvm-bolt %t.exe -o %t.bolt.exe --plt=all
7+
// RUN: %t.bolt.exe
8+
9+
int main() {
10+
try {
11+
throw new int;
12+
} catch (...) {
13+
return 0;
14+
}
15+
return 1;
16+
}

0 commit comments

Comments
 (0)