Skip to content

Commit a0f4170

Browse files
linsinan1995tru
authored andcommitted
[BOLT] Support map other function entry address (llvm#101466)
Allow BOLT to map the old address to a new binary address if the old address is the entry of the function. (cherry picked from commit 734c048)
1 parent 1eae7f7 commit a0f4170

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5498,6 +5498,14 @@ uint64_t RewriteInstance::getNewFunctionOrDataAddress(uint64_t OldAddress) {
54985498
if (const BinaryFunction *BF =
54995499
BC->getBinaryFunctionContainingAddress(OldAddress)) {
55005500
if (BF->isEmitted()) {
5501+
// If OldAddress is the another entry point of
5502+
// the function, then BOLT could get the new address.
5503+
if (BF->isMultiEntry()) {
5504+
for (const BinaryBasicBlock &BB : *BF)
5505+
if (BB.isEntryPoint() &&
5506+
(BF->getAddress() + BB.getOffset()) == OldAddress)
5507+
return BF->getOutputAddress() + BB.getOffset();
5508+
}
55015509
BC->errs() << "BOLT-ERROR: unable to get new address corresponding to "
55025510
"input address 0x"
55035511
<< Twine::utohexstr(OldAddress) << " in function " << *BF
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This test examines whether BOLT can correctly process when
2+
// dynamic relocation points to other entry points of the
3+
// function.
4+
5+
# RUN: %clang %cflags -fPIC -pie %s -o %t.exe -nostdlib -Wl,-q
6+
# RUN: llvm-bolt %t.exe -o %t.bolt > %t.out.txt
7+
# RUN: readelf -r %t.bolt >> %t.out.txt
8+
# RUN: llvm-objdump --disassemble-symbols=chain %t.bolt >> %t.out.txt
9+
# RUN: FileCheck %s --input-file=%t.out.txt
10+
11+
## Check if the new address in `chain` is correctly updated by BOLT
12+
# CHECK: Relocation section '.rela.dyn' at offset 0x{{.*}} contains 1 entry:
13+
# CHECK: {{.*}} R_X86_64_RELATIVE [[#%x,ADDR:]]
14+
# CHECK: [[#ADDR]]: c3 retq
15+
.text
16+
.type chain, @function
17+
chain:
18+
movq $1, %rax
19+
Label:
20+
ret
21+
.size chain, .-chain
22+
23+
.type _start, @function
24+
.global _start
25+
_start:
26+
jmpq *.Lfoo(%rip)
27+
ret
28+
.size _start, .-_start
29+
30+
.data
31+
.Lfoo:
32+
.quad Label

0 commit comments

Comments
 (0)