]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ubsan: nds32 undefined shift
authorAlan Modra <amodra@gmail.com>
Mon, 31 Mar 2025 08:49:28 +0000 (19:19 +1030)
committerAlan Modra <amodra@gmail.com>
Tue, 1 Apr 2025 13:15:46 +0000 (23:45 +1030)
Avoid implementation defined behaviour right shift of negative values,
and undefined behaviour left shift of negative values.  While this
change might give different results in the top bit of a bfd_vma
(rightshift is 1), that doesn't matter as only the bottom 8 bits of
the relocation are used.

* elf32-nds32.c (nds32_elf_do_9_pcrel_reloc): Calculate relocation
using a bfd_vma type.

bfd/elf32-nds32.c

index e240c3118412859dee3358ec144183da7decf1c4..1aff60a250513cfd2a85ec90b51394cab0c36a77 100644 (file)
@@ -2692,7 +2692,7 @@ nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
                            bfd_vma             symbol_value,
                            bfd_vma             addend)
 {
-  bfd_signed_vma relocation;
+  bfd_vma relocation;
   unsigned short x;
   bfd_reloc_status_type status;
 
@@ -2708,7 +2708,7 @@ nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
      before doing pcrel calculations.  */
   relocation -= (offset & -(bfd_vma) 2);
 
-  if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
+  if (relocation + ACCURATE_8BIT_S1 >= 2 * ACCURATE_8BIT_S1)
     status = bfd_reloc_overflow;
   else
     status = bfd_reloc_ok;