Skip to content

Commit

Permalink
Add a safety margin when computing branch offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Jan 2, 2025
1 parent f609510 commit d0bcd35
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class Thunk<E> {

template <needs_thunk E> void gather_thunk_addresses(Context<E> &);

// Returns the maximum branch reach in bytes for a given target.
template <needs_thunk E>
static consteval i64 get_branch_distance() {
// ARM64's branch has 26 bits immediate. The immediate is padded with
Expand All @@ -144,8 +143,15 @@ static consteval i64 get_branch_distance() {
return 1 << 25;
}

// The maximum distance of branch instructions used for function calls.
//
// The exact origin for computing a destination varies slightly depending
// on the target architecture. For example, ARM32's B instruction jumps to
// the branch's address + immediate + 4 (i.e., B with offset 0 jumps to
// the next instruction), while RISC-V has no such implicit bias. Here, we
// subtract 16 as a safety margin.
template <needs_thunk E>
static constexpr i64 branch_distance = get_branch_distance<E>();
static constexpr i64 branch_distance = get_branch_distance<E>() - 16;

//
// input-sections.cc
Expand Down

0 comments on commit d0bcd35

Please sign in to comment.