[binutils-gdb] RISC-V: Don't allocate dynamic relocation if it's section was discarded

Nelson Chu nelsonc1225@sourceware.org
Tue Jul 22 06:34:02 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f6fc2b523161b7c459e7fc9ae80b288727071be9

commit f6fc2b523161b7c459e7fc9ae80b288727071be9
Author: Nelson Chu <nelson@rivosinc.com>
Date:   Thu Jul 3 11:16:25 2025 +0800

    RISC-V: Don't allocate dynamic relocation if it's section was discarded

Diff:
---
 bfd/elfnn-riscv.c                   |  2 ++
 ld/testsuite/ld-riscv-elf/discard.s | 45 +++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 84f121c1b14..af08f7d46f6 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -1632,6 +1632,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.s b/ld/testsuite/ld-riscv-elf/discard.s
index 8dd15a83906..f03ea2b4590 100644
--- a/ld/testsuite/ld-riscv-elf/discard.s
+++ b/ld/testsuite/ld-riscv-elf/discard.s
@@ -13,8 +13,53 @@ sym_local:
 sym_global:
 	nop
 
+.global sym_hidden
+.hidden sym_hidden
+sym_hidden:
+	nop
+
+.global sym_global_abs
+.set sym_global_abs, 42
+
+.global sym_weak_undef
+.weak sym_weak_undef
+
+# Got
+
 .section .discard.got_local, "ax"
 	la	x1, sym_local
 
 .section .discard.got_global, "ax"
 	la	x1, sym_global
+
+# Data
+
+.section .discard.local,"a"
+.p2align 1
+discard_local:
+.quad sym_local
+
+.section .discard.global,"a"
+.p2align 1
+discard_global:
+.quad sym_global
+
+.section .discard.hidden,"a"
+.p2align 1
+discard_hidden:
+.quad sym_hidden
+
+.section .discard.global_abs,"a"
+.p2align 1
+discard_global_abs:
+.quad sym_global_abs
+
+.section .discard.weak_undef,"a"
+.p2align 1
+discard_weak_undef:
+.quad sym_weak_undef
+
+.section .discard._DYNAMIC,"a"
+.p2align 1
+discard_DYNAMIC:
+.quad _DYNAMIC


More information about the Binutils-cvs mailing list