[PATCH] gold/aarch64: Fix adrp distance check

Cary Coutant ccoutant@gmail.com
Fri Jul 29 20:14:08 GMT 2022


> The offset between destination and location is a signed number,
> currently the offset is treated as unsigned number, thus mathematical
> shifting of negative value is performed incorrectly.

Do you have a test case to illustrate the problem?

+    int64_t offset
+        = static_cast<int64_t> (Reloc::Page (dest) - Reloc::Page (location));
+    int64_t adrp_imm = offset < 0 ? ~(~offset >> 12) : offset >> 12;

C++ conventions: No space before paren in a function call.

The cast is unnecessary here for assignment to an int64_t. Once the
result is in a signed int, I don't think you need to go to all that
extra trouble to shift it.

I'd suggest this instead:

    int64_t adrp_imm = Reloc::Page(dest) - Reloc::Page(location);
    adrp_imm >>= 12;

-cary


More information about the Binutils mailing list