Skip to content

arm: account for PC bias when resolving branch relocs inside a function - #437

Open
hmatt1 wants to merge 1 commit into
encounter:mainfrom
hmatt1:arm-branch-pc-bias
Open

hmatt1 wants to merge 1 commit into
encounter:mainfrom
hmatt1:arm-branch-pc-bias

Conversation

@hmatt1

@hmatt1 hmatt1 commented Nov 30, 2026

Copy link
Copy Markdown

Fixes #436

Summary

  • Adds Arch::reloc_branch_pc_bias (default 0) returning the bias to add to reloc.addend to cancel out a PC-relative branch's pipeline PC offset (+4 for Thumb branch relocations, +8 for ARM branch relocations).
  • Uses checked_add in Arch::scan_instructions before checking whether a relocation target falls within [function_start, function_end), preventing calls to an adjacent function in the same .text section from being misclassified as internal branch destinations.
  • Adds R_ARM_JUMP24 to reloc_name, data_reloc_size, and write_branch_target in ArchArm for consistency with relocation_override.
  • Updates the read_arm snapshots where LinkStateItem::OnStateLeave's bl LinkStateItem::GetEquipBombchu() was previously misclassified as a branch to a literal-pool word (0x1a8).
  • Adds focused unit tests covering both ARM (-8) and Thumb (-4) adjacent-function calls and intra-function branch relocations.

Verification

  • cargo test (all 53 tests pass)
  • cargo clippy -p objdiff-core --all-targets --all-features -- -D warnings (0 warnings)

scan_instructions converts a relocation whose target (symbol address +
addend) lies within [function_start, function_end) into an internal branch
destination. For ARM/Thumb PC-relative branch relocations, the addend
includes the pipeline's PC bias (-4 for Thumb, -8 for ARM) rather than an
offset into the target, so the computed address is 4 or 8 bytes before the
real target. A call to a function starting immediately after the caller in
the same section then lands inside the caller's trailing bytes and becomes
a BranchDest, while the other side (with per-function sections) keeps it as
a Reloc, causing a false mismatch.

Add Arch::reloc_branch_pc_bias (default 0) returning the bias to add to
reloc.addend to cancel out the pipeline offset (+4 for Thumb branch relocs,
+8 for ARM branch relocs), and use checked_add in scan_instructions. Also
add R_ARM_JUMP24 to reloc_name, data_reloc_size, and write_branch_target
for consistency, and add unit tests covering both ARM (-8) and Thumb (-4)
adjacent-function calls and intra-function branch relocations.

Fixes encounter#436
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ARM: PC-relative branch relocations to adjacent functions are misclassified as internal branches due to PC bias

1 participant