Skip to content

Commit 418e4b0

Browse files
authored
[BOLT] Detect incorrect update of dynamic relocations (#89681)
When we rewrite dynamic relocations, there could be cases where they reference code locations inside functions that were rewritten. When this happens, we need to precisely map old address to a new one. Until we can reliably perform the mapping, detect such condition and issue an error refusing to write a broken binary.
1 parent ea3eeb4 commit 418e4b0

File tree

5 files changed

+28
-29
lines changed

5 files changed

+28
-29
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5432,6 +5432,17 @@ uint64_t RewriteInstance::getNewFunctionOrDataAddress(uint64_t OldAddress) {
54325432
if (BD && BD->isMoved())
54335433
return BD->getOutputAddress();
54345434

5435+
if (const BinaryFunction *BF =
5436+
BC->getBinaryFunctionContainingAddress(OldAddress)) {
5437+
if (BF->isEmitted()) {
5438+
BC->errs() << "BOLT-ERROR: unable to get new address corresponding to "
5439+
"input address 0x"
5440+
<< Twine::utohexstr(OldAddress) << " in function " << *BF
5441+
<< ". Consider adding this function to --skip-funcs=...\n";
5442+
exit(1);
5443+
}
5444+
}
5445+
54355446
return 0;
54365447
}
54375448

bolt/test/X86/indirect-goto-pie.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Check that llvm-bolt fails to process PIC binaries with computed goto, as the
2+
# support is not there yet for correctly updating dynamic relocations
3+
# referencing code inside functions.
4+
5+
REQUIRES: x86_64-linux
6+
7+
RUN: %clang %S/Inputs/indirect_goto.c -o %t -fpic -pie -Wl,-q
8+
RUN: not llvm-bolt %t -o %t.bolt --relocs=1 --print-cfg --print-only=main \
9+
RUN: |& FileCheck %s
10+
11+
# Check that processing works if main() is skipped.
12+
RUN: llvm-bolt %t -o %t.bolt --relocs=1 --skip-funcs=main
13+
14+
CHECK: jmpq *%rax # UNKNOWN CONTROL FLOW
15+
16+
CHECK: BOLT-ERROR: unable to get new address

bolt/test/X86/shrinkwrapping-do-not-pessimize.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ end_if_1:
5353
.size _start, .-_start
5454

5555
.data
56-
rel: .quad end_if_1
56+
rel: .quad _start
5757

5858
# CHECK: BOLT-INFO: Shrink wrapping moved 0 spills inserting load/stores and 0 spills inserting push/pops

bolt/test/runtime/X86/Inputs/indirect_goto.c

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

bolt/test/runtime/X86/indirect-goto-pie.test

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

0 commit comments

Comments
 (0)