[PATCH] RISC-V: Check all referring LOs before relaxing the PC-relative HI

Luke Zhuang luke.zhuang@linux.alibaba.com
Fri Nov 27 09:41:33 GMT 2026


PC-relative relaxation can delete the HI without checking whether all
referring LOs are marked with R_RISCV_RELAX, causing
"%pcrel_lo missing matching %pcrel_hi".

The psABI forbids relaxing only part of a group:
https://riscv-non-isa.github.io/riscv-elf-psabi-doc/#_linker_relaxation_types

Before relaxing the HI, collect all its corresponding LOs and then do
safety checks. Note that the scanning only happens once per section
and per relax_section call, so the complexity should not be harmed.

This fix is also a prerequisite for the GOT load relaxation.

Assisted-by: Codex

bfd/
	* elfnn-riscv.c (riscv_pcrel_relax_lo_reloc)
	(riscv_pcrel_relax_group): New types.
	(riscv_pcgp_relocs): Add pcrel_relax_groups.
	(riscv_pcrel_relax_group_hash, riscv_pcrel_relax_group_eq)
	(riscv_pcrel_relax_group_free, riscv_record_pcrel_relax_groups):
	New functions.
	(riscv_init_pcgp_relocs, riscv_free_pcgp_relocs): Initialize and
	free the group index.
	(_bfd_riscv_relax_pc): Do safety checks before relaxing the HI.

ld/
	* testsuite/ld-riscv-elf/pcrel-relax-group.s: New test source.
	* testsuite/ld-riscv-elf/pcrel-relax-group.d: New test.
	* testsuite/ld-riscv-elf/ld-riscv-elf.exp: Run it.
---
 bfd/elfnn-riscv.c                             | 168 +++++++++++++++++-
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp    |   1 +
 ld/testsuite/ld-riscv-elf/pcrel-relax-group.d |  53 ++++++
 ld/testsuite/ld-riscv-elf/pcrel-relax-group.s | 105 +++++++++++
 4 files changed, 326 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-relax-group.d
 create mode 100644 ld/testsuite/ld-riscv-elf/pcrel-relax-group.s

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 4c4ddbb78f7..f15782cc37e 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -5193,12 +5193,148 @@ struct riscv_pcgp_lo_reloc
   riscv_pcgp_lo_reloc *next;
 };
 
+/* A linked list storing all the corresponding PCREL lo parts for a HI,
+   in the current input section.  This is useful in doing basic checks
+   for all the related LOs (like checking that all LOs are marked with
+   R_RISCV_RELAX) before actually relaxing the HI.  */
+
+typedef struct riscv_pcrel_relax_lo_reloc
+{
+  Elf_Internal_Rela *rel;
+  struct riscv_pcrel_relax_lo_reloc *next;
+} riscv_pcrel_relax_lo_reloc;
+
+typedef struct
+{
+  bfd_vma hi_sec_off;
+  riscv_pcrel_relax_lo_reloc *lo;
+} riscv_pcrel_relax_group;
+
 typedef struct
 {
   riscv_pcgp_hi_reloc *hi;
   riscv_pcgp_lo_reloc *lo;
+  htab_t pcrel_relax_groups;
 } riscv_pcgp_relocs;
 
+static hashval_t
+riscv_pcrel_relax_group_hash (const void *entry)
+{
+  const riscv_pcrel_relax_group *group = entry;
+  return (hashval_t) (group->hi_sec_off);
+}
+
+static int
+riscv_pcrel_relax_group_eq (const void *entry1, const void *entry2)
+{
+  const riscv_pcrel_relax_group *group1 = entry1, *group2 = entry2;
+  return group1->hi_sec_off == group2->hi_sec_off;
+}
+
+static void
+riscv_pcrel_relax_group_free (void *entry)
+{
+  riscv_pcrel_relax_group *group = entry;
+  riscv_pcrel_relax_lo_reloc *lo = group->lo;
+
+  while (lo != NULL)
+    {
+      riscv_pcrel_relax_lo_reloc *next = lo->next;
+      free (lo);
+      lo = next;
+    }
+  free (group);
+}
+
+/* Collect all the pcrel relaxation groups for a section, by scanning all the
+   relocations in the section.  A group corresponds to one HI and its LOs.
+   It uses the section offset of the HI as the hash key, and includes a
+   linked list of all its LOs.
+   For example, with .Lhi at section offset 0:
+
+     Offset  Type		     Symbol + Addend
+     0x00    R_RISCV_PCREL_HI20      var + 0
+     0x04    R_RISCV_PCREL_LO12_I    .Lhi + 0
+     0x08    R_RISCV_PCREL_LO12_S    .Lhi + 0
+
+   Both LOs refer to .Lhi, so they belong to a group with key 0.  */
+
+static bool
+riscv_record_pcrel_relax_groups (bfd *abfd, asection *sec,
+				 riscv_pcgp_relocs *p)
+{
+  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
+  Elf_Internal_Sym *local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
+  Elf_Internal_Rela *relocs = elf_section_data (sec)->relocs;
+
+  p->pcrel_relax_groups
+    = htab_create (10, riscv_pcrel_relax_group_hash,
+		   riscv_pcrel_relax_group_eq, riscv_pcrel_relax_group_free);
+  if (p->pcrel_relax_groups == NULL)
+    return false;
+
+  for (unsigned int i = 0; i < sec->reloc_count; i++)
+    {
+      Elf_Internal_Rela *rel = relocs + i;
+      unsigned int type = ELFNN_R_TYPE (rel->r_info);
+      unsigned long symndx = ELFNN_R_SYM (rel->r_info);
+      asection *sym_sec;
+      riscv_pcrel_relax_group key, *group;
+      riscv_pcrel_relax_lo_reloc *lo;
+      void **slot;
+
+      if (type != R_RISCV_PCREL_LO12_I && type != R_RISCV_PCREL_LO12_S)
+	continue;
+
+      /* Find the HI label, resolving non-local symbols.  */
+      if (riscv_is_local_symbol (symtab_hdr, symndx))
+	{
+	  Elf_Internal_Sym *sym = local_syms + symndx;
+	  sym_sec = bfd_section_from_elf_index (abfd, sym->st_shndx);
+	  key.hi_sec_off = sym->st_value;
+	}
+      else
+	{
+	  struct elf_link_hash_entry *h = elf_sym_hashes (abfd)
+					 [symndx - symtab_hdr->sh_info];
+	  while (h != NULL
+		 && (h->root.type == bfd_link_hash_indirect
+		     || h->root.type == bfd_link_hash_warning))
+	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+	  if (h == NULL || (h->root.type != bfd_link_hash_defined
+			    && h->root.type != bfd_link_hash_defweak))
+	    continue;
+	  sym_sec = h->root.u.def.section;
+	  key.hi_sec_off = h->root.u.def.value;
+	}
+
+      /* Only collect LO groups from the same section as HI.  */
+      if (sym_sec != sec)
+	continue;
+
+      slot = htab_find_slot (p->pcrel_relax_groups, &key, INSERT);
+      if (slot == NULL)
+	return false;
+      group = *slot;
+      if (group == NULL)
+	{
+	  group = bfd_zmalloc (sizeof (*group));
+	  if (group == NULL)
+	    return false;
+	  group->hi_sec_off = key.hi_sec_off;
+	  *slot = group;
+	}
+
+      lo = bfd_malloc (sizeof (*lo));
+      if (lo == NULL)
+	return false;
+      lo->rel = rel;
+      lo->next = group->lo;
+      group->lo = lo;
+    }
+  return true;
+}
+
 /* Initialize the pcgp reloc info in P.  */
 
 static bool
@@ -5206,6 +5342,7 @@ riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
 {
   p->hi = NULL;
   p->lo = NULL;
+  p->pcrel_relax_groups = NULL;
   return true;
 }
 
@@ -5219,6 +5356,9 @@ riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
   riscv_pcgp_hi_reloc *c;
   riscv_pcgp_lo_reloc *l;
 
+  if (p->pcrel_relax_groups != NULL)
+    htab_delete (p->pcrel_relax_groups);
+
   for (c = p->hi; c != NULL; )
     {
       riscv_pcgp_hi_reloc *next = c->next;
@@ -6604,7 +6744,7 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec,
 /* Relax PC-relative references to GP-relative references.  */
 
 static bool
-_bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
+_bfd_riscv_relax_pc (bfd *abfd,
 		     asection *sec,
 		     asection *sym_sec,
 		     struct bfd_link_info *link_info,
@@ -6736,6 +6876,32 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
 	  return true;
 
 	case R_RISCV_PCREL_HI20:
+	  /* Do safety checks before relaxing the HI.  We need to check that:
+	     1. At least one LO refers to it.
+	     2. All referring LOs must be paired with R_RISCV_RELAX.  */
+
+	  /* Note that we only build the table at most once per
+	     _bfd_riscv_relax_section, so the complexity is not harmed.  */
+	  if (pcgp_relocs->pcrel_relax_groups == NULL
+	      && !riscv_record_pcrel_relax_groups (abfd, sec, pcgp_relocs))
+	    return false;
+
+	  riscv_pcrel_relax_group key = { rel->r_offset, NULL };
+	  riscv_pcrel_relax_group *group
+	    = htab_find (pcgp_relocs->pcrel_relax_groups, &key);
+	  Elf_Internal_Rela *relend
+	    = elf_section_data (sec)->relocs + sec->reloc_count;
+	  if (group == NULL)
+	    return true;
+
+	  /* Removing the HI requires every associated LO to be relaxed.  */
+	  for (riscv_pcrel_relax_lo_reloc *lo = group->lo;
+	       lo != NULL; lo = lo->next)
+	    if (lo->rel + 1 == relend
+		|| ELFNN_R_TYPE ((lo->rel + 1)->r_info) != R_RISCV_RELAX
+		|| lo->rel->r_offset != (lo->rel + 1)->r_offset)
+	      return true;
+
 	  riscv_record_pcgp_hi_reloc (pcgp_relocs,
 				      rel->r_offset,
 				      rel->r_addend,
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index df2cd2bf260..4f5928fa43f 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -180,6 +180,7 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "pcgp-relax-01"
     run_dump_test "pcgp-relax-01-norelaxgp"
     run_dump_test "pcgp-relax-02"
+    run_dump_test "pcrel-relax-group"
     run_dump_test "c-lui"
     run_dump_test "c-lui-2"
     run_dump_test "disas-jalr"
diff --git a/ld/testsuite/ld-riscv-elf/pcrel-relax-group.d b/ld/testsuite/ld-riscv-elf/pcrel-relax-group.d
new file mode 100644
index 00000000000..3a0c0f2900f
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pcrel-relax-group.d
@@ -0,0 +1,53 @@
+#source: pcrel-relax-group.s
+#as:
+#ld: --relax --relax-gp
+#objdump: -d -Mno-aliases
+
+#...
+Disassembly of section \.text:
+
+[0-9a-f]+ <_start>:
+.*[ 	]auipc[ 	]+t0,0x[0-9a-f]+
+.*[ 	]lw[ 	]+a0,-?[0-9]+\(t0\) # [0-9a-f]+ <gp_data>
+.*[ 	]j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[0-9]+\(gp\) # [0-9a-f]+ <gp_data>
+.*[ 	]sw[ 	]+a0,-?[0-9]+\(gp\) # [0-9a-f]+ <gp_data\+0x4>
+.*[ 	]jalr[ 	]+zero,0\(ra\)
diff --git a/ld/testsuite/ld-riscv-elf/pcrel-relax-group.s b/ld/testsuite/ld-riscv-elf/pcrel-relax-group.s
new file mode 100644
index 00000000000..2d864306b1f
--- /dev/null
+++ b/ld/testsuite/ld-riscv-elf/pcrel-relax-group.s
@@ -0,0 +1,105 @@
+	.option norvc
+	.option relax
+	.text
+	.globl _start
+_start:
+	# A single LO which is not marked as relaxable, should not do
+	# pcgp-relaxation.
+single_unmarked:
+.Lsingle:
+	auipc t0, %pcrel_hi(gp_data)
+	.option push
+	.option norelax
+	lw a0, %pcrel_lo(.Lsingle)(t0)
+	.option pop
+	ret
+
+	# All marked: should do pcgp-relaxation as usual.
+	.globl all_marked
+all_marked:
+.Lgood:
+	auipc t0, %pcrel_hi(zero_sym)
+	addi a0, t0, %pcrel_lo(.Lgood)
+	lw a1, %pcrel_lo(.Lgood)(t0)
+	sw a1, %pcrel_lo(.Lgood + 4)(t0)
+	ret
+
+	# Two LOs, one of them is not marked as relaxable.
+	.globl mixed_load
+mixed_load:
+.Lmixed:
+	auipc t0, %pcrel_hi(zero_sym)
+	.option push
+	.option norelax
+	lw a0, %pcrel_lo(.Lmixed)(t0)
+	.option pop
+	lw a1, %pcrel_lo(.Lmixed)(t0)
+	ret
+
+	# Similar to the above, check stores.
+	.globl mixed_store
+mixed_store:
+.Lstore:
+	auipc t0, %pcrel_hi(gp_data)
+	lw a0, %pcrel_lo(.Lstore)(t0)
+	.option push
+	.option norelax
+	sw a0, %pcrel_lo(mixed_store)(t0)
+	.option pop
+	ret
+
+	# An unrelaxable LO appears before the HI in the relocation table.
+	.globl early_lo
+early_lo:
+	j .Llate_hi
+.Learly_lo:
+	.option push
+	.option norelax
+	lw a0, %pcrel_lo(.Llate_hi)(t0)
+	.option pop
+	ret
+.Llate_hi:
+	auipc t0, %pcrel_hi(zero_sym)
+	j .Learly_lo
+
+	# A HI without any LO must not be deleted.
+	.globl hi_only
+hi_only:
+	auipc a0, %pcrel_hi(zero_sym)
+	ret
+
+	# Interleaved case: one of them is unrelaxable while the other one
+	# should still be relaxed.
+	.globl interleaved
+interleaved:
+.Lkeep:
+	auipc t0, %pcrel_hi(zero_sym)
+.Lshrink:
+	auipc t1, %pcrel_hi(gp_data)
+	lw a0, %pcrel_lo(.Lkeep)(t0)
+	lw a1, %pcrel_lo(.Lshrink)(t1)
+	.option push
+	.option norelax
+	sw a0, %pcrel_lo(.Lkeep + 4)(t0)
+	.option pop
+	ret
+
+	# Check sections are handled separately, not affecting each other.
+	# Both this HI and .Lsingle have section offset 0.  This group must
+	# still relax even though .Lsingle's LO is not marked.
+	.section .text.other, "ax", @progbits
+	.globl other_section
+other_section:
+.Lother:
+	auipc t0, %pcrel_hi(gp_data)
+	lw a0, %pcrel_lo(.Lother)(t0)
+	sw a0, %pcrel_lo(.Lother + 4)(t0)
+	ret
+
+	.globl zero_sym
+	.set zero_sym, 0x600
+
+	.section .sdata, "aw", @progbits
+	.word 0
+gp_data:
+	.word 44, 45
-- 
2.25.1



More information about the Binutils mailing list