[PATCH] D97550: [LLD][ELF][ARM] Refactor inBranchRange to use addend for PC Bias

Peter Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 26 06:04:17 PST 2021


peter.smith created this revision.
peter.smith added reviewers: MaskRay, grimar.
Herald added subscribers: danielkiss, kristof.beyls, arichardson, emaste.
peter.smith requested review of this revision.

In AArch32 ARM, the PC reads two instructions ahead of the currently executiing instruction. This evaluates to 8 in ARM state and 4 in Thumb state. Branch instructions on AArch32 compensate for this by subtracting the PC bias from the addend. For a branch to symbol this will result in an addend of -8 in ARM state and -4 in Thumb state.

The existing ARM Target::inBranchRange function accounted for this implict addend within the function meaning that if the addend were to be taken into account by the caller then it would be double counted. This complicates the interface for all Targets as callers wanting to account for addends had to account for the ARM PC-bias.

In certain situations such as: https://github.com/ClangBuiltLinux/linux/issues/1305 the PC-bias compensation code didn't match up. In particular normalizeExistingThunk() didn't put the PC-bias back in as Arm thunks did not store the addend.

The simplest fix for the problem is to add the PC bias in normalizeExistingThunk when restoring the addend. However I think it is worth refactoring the Arm inBranchRange implementation so that fewer calls to getPCBias are needed for other Targets. I wasn't able to remove getPCBias completely but hopefully the Relocations.cpp code is simpler now.

In principle a test could be written to replicate the linux kernel build failure but I wasn't able to reproduce with a small example that I could build up from scratch.

Fixes https://github.com/ClangBuiltLinux/linux/issues/1305


https://reviews.llvm.org/D97550

Files:
  lld/ELF/Arch/ARM.cpp
  lld/ELF/Relocations.cpp
  lld/ELF/Relocations.h
  lld/ELF/Thunks.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97550.326668.patch
Type: text/x-patch
Size: 14047 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210226/507ad88b/attachment.bin>


More information about the llvm-commits mailing list