undefined shift in m32r_elf_do_10_pcrel_reloc

Alan Modra amodra@gmail.com
Thu Nov 26 06:24:09 GMT 2026


The ISO C99 standard says that a left shift of a negative value is
undefined behaviour.  No exception is made for a shift of zero.  Avoid
the UB by using bfd_vma rather than bfd_signed_vma for "relocation".
Only bits contained in bits 0..31 of the value are ever placed in the
relocation field, so there is no need to do a signed right shift (and
if you do, you're relying on implementation defined behaviour).

diff --git a/bfd/elf32-m32r.c b/bfd/elf32-m32r.c
index 4cdcd9e4db3..aa2432c613e 100644
--- a/bfd/elf32-m32r.c
+++ b/bfd/elf32-m32r.c
@@ -93,7 +93,7 @@ m32r_elf_do_10_pcrel_reloc (bfd *abfd,
 			    bfd_vma symbol_value,
 			    bfd_vma addend)
 {
-  bfd_signed_vma relocation;
+  bfd_vma relocation;
   unsigned long x;
   bfd_reloc_status_type status;
 
@@ -109,7 +109,7 @@ m32r_elf_do_10_pcrel_reloc (bfd *abfd,
      before doing pcrel calculations.  */
   relocation -= (offset & -(bfd_vma) 4);
 
-  if (relocation < -0x200 || relocation > 0x1ff)
+  if (relocation + 0x200 > 0x3ff)
     status = bfd_reloc_overflow;
   else
     status = bfd_reloc_ok;

-- 
Alan Modra


More information about the Binutils mailing list