From f6fc2b523161b7c459e7fc9ae80b288727071be9 Mon Sep 17 00:00:00 2001 From: Nelson Chu Date: Thu, 3 Jul 2025 11:16:25 +0800 Subject: [PATCH] RISC-V: Don't allocate dynamic relocation if it's section was discarded --- 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 -- 2.47.2