Skip to content

Commit 5e5cb16

Browse files
committed
[BOLT] Fix bug in lookupStubFromGroup
The current implementation of `lookupStubFromGroup` is incorrect. The function is intended to find and return the closest stub using `lower_bound`, which identifies the first element in a sorted range that is not less than a specified value. However, if such an element is not found within `Candidates` and the list is not empty, the function returns `nullptr`. Instead, it should check whether the last element satisfies the condition.
1 parent 7544d3a commit 5e5cb16

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

bolt/lib/Passes/LongJmp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ BinaryBasicBlock *LongJmpPass::lookupStubFromGroup(
130130
const std::pair<uint64_t, BinaryBasicBlock *> &RHS) {
131131
return LHS.first < RHS.first;
132132
});
133-
if (Cand == Candidates.end())
134-
return nullptr;
135-
if (Cand != Candidates.begin()) {
133+
if (Cand == Candidates.end()) {
134+
Cand = std::prev(Cand);
135+
} else if (Cand != Candidates.begin()) {
136136
const StubTy *LeftCand = std::prev(Cand);
137137
if (Cand->first - DotAddress > DotAddress - LeftCand->first)
138138
Cand = LeftCand;

bolt/test/AArch64/one-stub.s

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# REQUIRES: system-linux, asserts
2+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
3+
# RUN: %clang %cflags -O0 -fPIC -pie %t.o -o %t.exe -nostdlib -Wl,-q
4+
# RUN: link_fdata %s %t.o %t.fdata
5+
# RUN: llvm-bolt %t.exe -o %t.bolt \
6+
# RUN: --data %t.fdata | FileCheck %s
7+
# CHECK: BOLT-INFO: Inserted 1 stubs in the hot area and 0 stubs in the cold area.
8+
9+
.section .text
10+
.global _start
11+
.global func
12+
13+
.align 4
14+
.global _start
15+
.type _start, %function
16+
_start:
17+
# FDATA: 0 [unknown] 0 1 _start 0 0 100
18+
bl func
19+
bl func
20+
ret
21+
.space 0x8000000
22+
.global func
23+
.type func, %function
24+
func:
25+
add x0, x0, #1
26+
ret
27+
28+
.reloc 0, R_AARCH64_NONE

0 commit comments

Comments
 (0)