[PATCH 2/3] RISC-V: Don't allocate dynamic relocation if it's section was discarded

Nelson Chu nelson@rivosinc.com
Sat Jun 7 08:36:40 GMT 2025


This can resolve the redundant nops in the rela.dyn.
---
 bfd/elfnn-riscv.c                       | 2 ++
 ld/testsuite/ld-riscv-elf/discard-pic.d | 4 ----
 ld/testsuite/ld-riscv-elf/discard-pie.d | 4 ----
 3 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index db9372c2b10..2341447c421 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -1441,6 +1441,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   /* Finally, allocate space.  */
   for (p = h->dyn_relocs; p != NULL; p = p->next)
     {
+      if (discarded_section (p->sec))
+	continue;
       asection *sreloc = elf_section_data (p->sec)->sreloc;
       sreloc->size += p->count * sizeof (ElfNN_External_Rela);
     }
diff --git a/ld/testsuite/ld-riscv-elf/discard-pic.d b/ld/testsuite/ld-riscv-elf/discard-pic.d
index f876cdf06ba..5eef63c40a3 100644
--- a/ld/testsuite/ld-riscv-elf/discard-pic.d
+++ b/ld/testsuite/ld-riscv-elf/discard-pic.d
@@ -7,9 +7,5 @@ Relocation section '\.rela\.dyn'.*
 [ 	]+Offset[ 	]+Info[ 	]+Type.*
 0000000000020010[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ 	]+10004
 0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
 0000000000020018[ 	]+[0-9a-f]+[ 	]+R_RISCV_64[ 	]+000000000001000c[ 	]+sym_global \+ 0
 0000000000020020[ 	]+[0-9a-f]+[ 	]+R_RISCV_64[ 	]+000000000001000c[ 	]+sym_global_keep \+ 0
diff --git a/ld/testsuite/ld-riscv-elf/discard-pie.d b/ld/testsuite/ld-riscv-elf/discard-pie.d
index 4e0d8cfb495..3fcca97fe86 100644
--- a/ld/testsuite/ld-riscv-elf/discard-pie.d
+++ b/ld/testsuite/ld-riscv-elf/discard-pie.d
@@ -9,7 +9,3 @@ Relocation section '\.rela\.dyn'.*
 0000000000020018[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ 	]+1000c
 0000000000020020[ 	]+[0-9a-f]+[ 	]+R_RISCV_RELATIVE[ 	]+1000c
 0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-0+0[ 	]+0+0[ 	]+R_RISCV_NONE[ 	]+0
-- 
2.39.5 (Apple Git-154)



More information about the Binutils mailing list