[PATCH v3 1/1] aarch64: mingw: Prevent relocation types from being overwritten

Evgeny Karpov <[email protected]> Wed, 22 Jul 2026 13:04:00 +0200
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
rel->r_type = IMAGE_REL_ARM64_ABSOLUTE has been used on purpose to bypass
changes in processed relocations in a later call to
_bfd_coff_generic_relocate_section.
However, it impacts another relocation logic, as the initial relocation type
is no longer known.
To avoid extra processing, rel->r_type = IMAGE_REL_ARM64_ABSOLUTE has been
replaced with rel->r_vaddr = -1, and a condition to skip relocation processing
has been added to _bfd_coff_generic_relocate_section.

bfd/ChangeLog:

	* coff-aarch64.c (coff_pe_aarch64_relocate_section): Replace
	rel->r_type = IMAGE_REL_ARM64_ABSOLUTE with rel->r_vaddr = -1.
	* cofflink.c (_bfd_coff_generic_relocate_section): Update relocation
	processing.
---
 bfd/coff-aarch64.c | 33 +++++++++++----------------------
 bfd/cofflink.c     |  3 +++
 2 files changed, 14 insertions(+), 22 deletions(-)

diff --git a/bfd/coff-aarch64.c b/bfd/coff-aarch64.c
index c00448efc82..72e37dc424a 100644
--- a/bfd/coff-aarch64.c
+++ b/bfd/coff-aarch64.c
@@ -578,8 +578,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 		input_section, rel->r_vaddr - input_section->vma);
 
 	    bfd_putl32 (val, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -613,8 +612,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= val & 0x3ffffff;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -648,8 +646,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= (val & 0x7ffff) << 5;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -683,8 +680,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= (val & 0x3fff) << 5;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -720,8 +716,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= (val & 0x1ffffc) << 3;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -757,8 +752,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= (val & 0x1ffffc) << 3;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -786,8 +780,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 		input_section, rel->r_vaddr - input_section->vma);
 
 	    bfd_putl32 (val, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -831,8 +824,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= val << 10;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -854,8 +846,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 	    opcode |= val << 10;
 
 	    bfd_putl32 (opcode, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -875,8 +866,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 		input_section, rel->r_vaddr - input_section->vma);
 
 	    bfd_putl32 (val, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
@@ -900,8 +890,7 @@ coff_pe_aarch64_relocate_section (bfd *output_bfd,
 
 
 	    bfd_putl16 (idx, contents + rel->r_vaddr);
-	    rel->r_type = IMAGE_REL_ARM64_ABSOLUTE;
-
+	    rel->r_vaddr = -1;
 	    break;
 	  }
 
diff --git a/bfd/cofflink.c b/bfd/cofflink.c
index d029bec1678..41cb68895a1 100644
--- a/bfd/cofflink.c
+++ b/bfd/cofflink.c
@@ -3115,6 +3115,9 @@ _bfd_coff_generic_relocate_section (bfd *output_bfd,
 
       symndx = rel->r_symndx;
 
+      if (rel->r_vaddr == (bfd_vma) -1)
+	continue;
+
       if (symndx == -1)
 	{
 	  h = NULL;