gold/aarch64: Fix adrp distance check

Message ID 20221107112805.3332619-1-och95@yandex.ru
State Accepted
Headers
Series gold/aarch64: Fix adrp distance check |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Vladislav Khmelevsky Nov. 7, 2022, 11:28 a.m. UTC
  The offset between destination and location is a signed number,
currently the offset is treated as unsigned number, thus mathematical
shift of a negative value is performed incorrectly.
---
 gold/aarch64.cc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Cary Coutant Nov. 11, 2022, 12:49 a.m. UTC | #1
> The offset between destination and location is a signed number,
> currently the offset is treated as unsigned number, thus mathematical
> shift of a negative value is performed incorrectly.
> ---
>  gold/aarch64.cc | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

This is OK. I've committed the patch on your behalf. Thanks!

-cary
  

Patch

diff --git a/gold/aarch64.cc b/gold/aarch64.cc
index d2b0747ffdc..514fad96789 100644
--- a/gold/aarch64.cc
+++ b/gold/aarch64.cc
@@ -1182,7 +1182,8 @@  class Reloc_stub : public Stub_base<size, big_endian>
   aarch64_valid_for_adrp_p(AArch64_address location, AArch64_address dest)
   {
     typedef AArch64_relocate_functions<size, big_endian> Reloc;
-    int64_t adrp_imm = (Reloc::Page(dest) - Reloc::Page(location)) >> 12;
+    int64_t adrp_imm = Reloc::Page (dest) - Reloc::Page (location);
+    adrp_imm = adrp_imm < 0 ? ~(~adrp_imm >> 12) : adrp_imm >> 12;
     return adrp_imm >= MIN_ADRP_IMM && adrp_imm <= MAX_ADRP_IMM;
   }