Skip to content

Commit f6435e5

Browse files
committed
[BOLT][AArch64] Create entry points for addresses referenced by dynamic relocations and allow getNewFunctionOrDataAddress to map addrs inside functions.
By adding addresses referenced by dynamic relocations as entry points, this patch fixes an issue where bolt fails on code using computing goto's. This also fixes a mapping issue with the bugfix from this PR: llvm#117766.
1 parent 9fc54c0 commit f6435e5

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,14 @@ void RewriteInstance::readDynamicRelocations(const SectionRef &Section,
24392439
if (Symbol)
24402440
SymbolIndex[Symbol] = getRelocationSymbol(InputFile, Rel);
24412441

2442+
const uint64_t SymAddress = SymbolAddress + Addend;
2443+
BinaryFunction *Func = BC->getBinaryFunctionContainingAddress(SymAddress);
2444+
if(Func){
2445+
const uint64_t FunctionOffset = SymAddress - Func->getAddress();
2446+
if(FunctionOffset)
2447+
Func->addEntryPointAtOffset(FunctionOffset);
2448+
}
2449+
24422450
BC->addDynamicRelocation(Rel.getOffset(), Symbol, RType, Addend);
24432451
}
24442452
}
@@ -5599,7 +5607,7 @@ uint64_t RewriteInstance::getNewFunctionOrDataAddress(uint64_t OldAddress) {
55995607
for (const BinaryBasicBlock &BB : *BF)
56005608
if (BB.isEntryPoint() &&
56015609
(BF->getAddress() + BB.getOffset()) == OldAddress)
5602-
return BF->getOutputAddress() + BB.getOffset();
5610+
return BB.getOutputStartAddress();
56035611
}
56045612
BC->errs() << "BOLT-ERROR: unable to get new address corresponding to "
56055613
"input address 0x"

bolt/test/AArch64/computed-goto.s

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
2+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
3+
# RUN: llvm-bolt %t.exe -o %t.bolt 2>&1 | FileCheck %s
4+
5+
## Before bolt could handle mapping addresses within moved functions, it
6+
## would bail out with an error of the form:
7+
## BOLT-ERROR: unable to get new address corresponding to input address 0x10390 in function main. Consider adding this function to --skip-funcs=...
8+
## These addresses arise if computed GOTO is in use.
9+
## Check that bolt does not emit any error.
10+
11+
# CHECK-NOT: BOLT-ERROR
12+
13+
.globl main
14+
.p2align 2
15+
.type main,@function
16+
main:
17+
.cfi_startproc
18+
adrp x8, .L__const.main.ptrs+8
19+
add x8, x8, :lo12:.L__const.main.ptrs+8
20+
ldr x9, [x8], #8
21+
br x9
22+
23+
.Label0: // Block address taken
24+
ldr x9, [x8], #8
25+
br x9
26+
27+
.Label1: // Block address taken
28+
mov w0, #42
29+
ret
30+
31+
.Lfunc_end0:
32+
.size main, .Lfunc_end0-main
33+
.cfi_endproc
34+
.type .L__const.main.ptrs,@object
35+
.section .data.rel.ro,"aw",@progbits
36+
.p2align 3, 0x0
37+
.L__const.main.ptrs:
38+
.xword .Label0
39+
.xword .Label1

0 commit comments

Comments
 (0)