[PATCH v3 4/8] m68hc11: return bool from relax delete helpers

Harshit Kumar harshit7kr@gmail.com
Sat Nov 7 07:50:59 GMT 2026


Convert m68hc11_elf_relax_delete_bytes and m68hc11_relax_group to
return bool.  Validate deletion range with _bfd_elf_relax_delete_range_ok
and propagate deletion failures in m68hc11_elf_relax_section.

Signed-off-by: Harshit Kumar <harshit7kr@gmail.com>
---
v3:
- Unchanged from v2.

 bfd/elf32-m68hc11.c | 52 ++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 19 deletions(-)

diff --git a/bfd/elf32-m68hc11.c b/bfd/elf32-m68hc11.c
index 483788be..1100647b 100644
--- a/bfd/elf32-m68hc11.c
+++ b/bfd/elf32-m68hc11.c
@@ -46,9 +46,9 @@ static struct bfd_link_hash_table* m68hc11_elf_bfd_link_hash_table_create
 /* Linker relaxation.  */
 static bool m68hc11_elf_relax_section
   (bfd *, asection *, struct bfd_link_info *, bool *);
-static void m68hc11_elf_relax_delete_bytes
+static bool m68hc11_elf_relax_delete_bytes
   (bfd *, asection *, bfd_vma, int);
-static void m68hc11_relax_group
+static bool m68hc11_relax_group
   (bfd *, asection *, bfd_byte *, unsigned, unsigned long, unsigned long);
 static int compare_reloc (const void *, const void *);
 
@@ -574,7 +574,7 @@ compare_reloc (const void *e1, const void *e2)
 
 #define M6811_OP_LDX_IMMEDIATE (0xCE)
 
-static void
+static bool
 m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
 		     unsigned value, unsigned long offset,
 		     unsigned long end_group)
@@ -599,7 +599,7 @@ m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
   ldx_size = offset - ldx_offset + 3;
   offset += 3;
   if (code != M6811_OP_LDX_IMMEDIATE || offset >= end_group)
-    return;
+    return true;
 
 
   /* We can remove the LDX/LDY only when all bset/brclr instructions
@@ -653,7 +653,7 @@ m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
 	     at end of the relax group.  Ignore and don't remove
 	     the first LDX (we don't know what it is used for...).  */
 	default:
-	  return;
+	  return true;
 	}
       new_value = (unsigned) bfd_get_8 (abfd, contents + offset + 1);
       new_value += value;
@@ -663,8 +663,9 @@ m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
 	  bfd_put_8 (abfd, new_value, contents + offset + 1);
 	  if (start_offset != offset)
 	    {
-	      m68hc11_elf_relax_delete_bytes (abfd, sec, start_offset,
-					      offset - start_offset);
+	      if (! m68hc11_elf_relax_delete_bytes (abfd, sec, start_offset,
+						    offset - start_offset))
+		return false;
 	      end_group--;
 	    }
 	}
@@ -677,8 +678,11 @@ m68hc11_relax_group (bfd *abfd, asection *sec, bfd_byte *contents,
   if (can_delete_ldx)
     {
       /* Remove the move instruction (3 or 4 bytes win).  */
-      m68hc11_elf_relax_delete_bytes (abfd, sec, ldx_offset, ldx_size);
+      if (! m68hc11_elf_relax_delete_bytes (abfd, sec, ldx_offset, ldx_size))
+	return false;
     }
+
+  return true;
 }
 
 /* This function handles relaxing for the 68HC11.
@@ -937,8 +941,9 @@ m68hc11_elf_relax_section (bfd *abfd, asection *sec,
 	      irel->r_offset = prev_insn_branch->r_offset + 1;
 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
 					   R_M68HC11_PCREL_8);
-	      m68hc11_elf_relax_delete_bytes (abfd, sec,
-					      irel->r_offset + 1, 1);
+	      if (! m68hc11_elf_relax_delete_bytes (abfd, sec,
+						    irel->r_offset + 1, 1))
+		goto error_return;
 	    }
 	  else
 	    {
@@ -949,8 +954,9 @@ m68hc11_elf_relax_section (bfd *abfd, asection *sec,
 	      irel->r_offset = prev_insn_branch->r_offset + 1;
 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
 					   R_M68HC11_PCREL_8);
-	      m68hc11_elf_relax_delete_bytes (abfd, sec,
-					      irel->r_offset + 1, 3);
+	      if (! m68hc11_elf_relax_delete_bytes (abfd, sec,
+						    irel->r_offset + 1, 3))
+		goto error_return;
 	    }
 	  prev_insn_branch = 0;
 	  *again = true;
@@ -987,9 +993,10 @@ m68hc11_elf_relax_section (bfd *abfd, asection *sec,
 	      symtab_hdr->contents = (bfd_byte *) isymbuf;
 	      free_extsyms = NULL;
 
-	      m68hc11_relax_group (abfd, sec, contents, offset,
-				   prev_insn_group->r_offset,
-				   insn_group_value);
+	      if (! m68hc11_relax_group (abfd, sec, contents, offset,
+					  prev_insn_group->r_offset,
+					  insn_group_value))
+		goto error_return;
 	      irel = prev_insn_group;
 	      prev_insn_group = 0;
 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
@@ -1025,7 +1032,8 @@ m68hc11_elf_relax_section (bfd *abfd, asection *sec,
 		     contents + irel->r_offset - 1);
 
 	  /* Delete one byte of data (upper byte of address).  */
-	  m68hc11_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 1);
+	  if (! m68hc11_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 1))
+	    goto error_return;
 
 	  /* Fix the relocation's type.  */
 	  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
@@ -1071,8 +1079,9 @@ m68hc11_elf_relax_section (bfd *abfd, asection *sec,
 			     contents + irel->r_offset);
 		  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
 					       R_M68HC11_PCREL_8);
-		  m68hc11_elf_relax_delete_bytes (abfd, sec,
-						  irel->r_offset + 1, 1);
+		  if (! m68hc11_elf_relax_delete_bytes (abfd, sec,
+							irel->r_offset + 1, 1))
+		    goto error_return;
 		  /* That will change things, so, we should relax again.  */
 		  *again = true;
 		}
@@ -1120,7 +1129,7 @@ m68hc11_elf_relax_section (bfd *abfd, asection *sec,
 
 /* Delete some bytes from a section while relaxing.  */
 
-static void
+static bool
 m68hc11_elf_relax_delete_bytes (bfd *abfd, asection *sec,
 				bfd_vma addr, int count)
 {
@@ -1146,6 +1155,9 @@ m68hc11_elf_relax_delete_bytes (bfd *abfd, asection *sec,
   irel = elf_section_data (sec)->relocs;
   irelend = irel + sec->reloc_count;
 
+  if (! _bfd_elf_relax_delete_range_ok (abfd, sec, addr, count, toaddr))
+    return false;
+
   /* Actually delete the bytes.  */
   memmove (contents + addr, contents + addr + count,
 	   (size_t) (toaddr - addr - count));
@@ -1276,6 +1288,8 @@ m68hc11_elf_relax_delete_bytes (bfd *abfd, asection *sec,
 	  sym_hash->root.u.def.value -= count;
 	}
     }
+
+  return true;
 }
 
 /* Specific sections:
-- 
2.55.0




More information about the Binutils mailing list