[PATCH] LoongArch: Discard unused .eh_frame sections in ld -r

mengqinggang mengqinggang@loongson.cn
Wed Nov 25 09:09:08 GMT 2026


Previously, bfd_elf_discard_info was skipped for relocatable links.
With --gc-sections, ld -r retained the FDE of a discarded section.
This left a stale .eh_frame relocation pointing beyond the end of the
section, so a following final link could fail with a bad relocation
offset.

Call bfd_elf_discard_info for relocatable links.  Move the test to
ld-loongarch-elf.exp and check that assembly, ld -r, and the final link
complete successfully.

ld/
        * emultempl/loongarchelf.em
        (gld${EMULATION_NAME}_after_allocation): Call bfd_elf_discard_info
        for relocatable links.
        * testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
        (run_eh_frame_discard_test): New.
        * testsuite/ld-loongarch-elf/eh-frame-discard.s: New test.
---
 ld/emultempl/loongarchelf.em                  | 14 +++------
 .../ld-loongarch-elf/eh-frame-discard.s       | 30 +++++++++++++++++++
 .../ld-loongarch-elf/ld-loongarch-elf.exp     | 25 ++++++++++++++++
 3 files changed, 59 insertions(+), 10 deletions(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/eh-frame-discard.s

diff --git a/ld/emultempl/loongarchelf.em b/ld/emultempl/loongarchelf.em
index b39507df292..dbd995a0461 100644
--- a/ld/emultempl/loongarchelf.em
+++ b/ld/emultempl/loongarchelf.em
@@ -194,17 +194,11 @@ static void
 gld${EMULATION_NAME}_after_allocation (void)
 {
   int need_layout = 0;
-
-  /* Don't attempt to discard unused .eh_frame sections until the final link,
-     as we can't reliably tell if they're used until after relaxation.  */
-  if (!bfd_link_relocatable (&link_info))
+  need_layout = bfd_elf_discard_info (&link_info);
+  if (need_layout < 0)
     {
-      need_layout = bfd_elf_discard_info (&link_info);
-      if (need_layout < 0)
-	{
-	  einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
-	  return;
-	}
+      einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
+      return;
     }
 
   /* If generating a relocatable output file, we have to add align
diff --git a/ld/testsuite/ld-loongarch-elf/eh-frame-discard.s b/ld/testsuite/ld-loongarch-elf/eh-frame-discard.s
new file mode 100644
index 00000000000..14b2c4cc623
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/eh-frame-discard.s
@@ -0,0 +1,30 @@
+# The dead function is removed by --gc-sections.  Its FDE must also be
+# removed by ld -r, otherwise relocations in .eh_frame can point beyond the
+# end of the section.
+.section .text.keep,"ax",@progbits
+.globl main
+.type main,@function
+main:
+.cfi_startproc
+  bl foo
+  jr $ra
+.cfi_endproc
+.size main, .-main
+
+.section .text.dead,"ax",@progbits
+.globl dead
+.type dead,@function
+dead:
+.cfi_startproc
+  jr $ra
+.cfi_endproc
+.size dead, .-dead
+
+.section .text.foo,"ax",@progbits
+.globl foo
+.type foo,@function
+foo:
+.cfi_startproc
+  jr $ra
+.cfi_endproc
+.size foo, .-foo
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index 8edbfb1f73a..d25482eb185 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -19,9 +19,34 @@
 # MA 02110-1301, USA.
 #
 
+proc run_eh_frame_discard_test {} {
+  global as
+  global ld
+  global srcdir
+  global subdir
+  global runtests
+
+  set testname "eh-frame-discard"
+  if ![runtest_file_p $runtests $testname] then {
+    return
+  }
+
+  if { ![ld_assemble $as "$srcdir/$subdir/$testname.s" \
+			 tmpdir/$testname.o]
+      || ![ld_link $ld tmpdir/$testname-r.o \
+		   "tmpdir/$testname.o -r --gc-sections -e main"]
+      || ![ld_link $ld tmpdir/$testname.exe \
+		   "tmpdir/$testname-r.o --gc-sections -e main"] } {
+    fail $testname
+  } else {
+    pass $testname
+  }
+}
+
 if [istarget loongarch*-*-*] {
   run_dump_test "dtprel"
   run_dump_test "reloc-offset"
+  run_eh_frame_discard_test
 }
 
 if [istarget "loongarch64-*-*"] {
-- 
2.34.1



More information about the Binutils mailing list