[binutils-gdb/binutils-2_45-branch] sframe: Allow input R_*_NONE relocations

H.J. Lu hjl@sourceware.org
Wed Jul 16 15:10:01 GMT 2025


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

commit b4782f2504f77d7aa4ce3e4007c13970963b5692
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Jul 13 04:56:16 2025 +0800

    sframe: Allow input R_*_NONE relocations
    
    "ld -r" generates R_*_NONE relocations in sframe section if input
    relocations in sframe section are against discarded section.  Allow
    input R_*_NONE relocations if there are more relocation entries than
    SFrame entries, instead of assuming number of SFrame entries == number
    of relocation entries.
    
    bfd/
    
            PR ld/33127
            * elf-sframe.c (sframe_decoder_init_func_bfdinfo): Allow input
            R_*_NONE relocations if there are more relocation entries than
            SFrame entries.
    
    ld/
    
            PR ld/33127
            * testsuite/ld-x86-64/sframe-reloc-2a.s: New file.
            * testsuite/ld-x86-64/sframe-reloc-2b.s: Likewise.
            * testsuite/ld-x86-64/x86-64.exp: Run PR ld/33127 tests.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
    (cherry picked from commit 5f9bf0cf711a153a0a20d6ff88181e9a6775bdba)

Diff:
---
 bfd/elf-sframe.c                         |  9 ++++++++-
 ld/testsuite/ld-x86-64/sframe-reloc-2a.s |  7 +++++++
 ld/testsuite/ld-x86-64/sframe-reloc-2b.s | 31 +++++++++++++++++++++++++++++++
 ld/testsuite/ld-x86-64/x86-64.exp        | 21 +++++++++++++++++++++
 4 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/bfd/elf-sframe.c b/bfd/elf-sframe.c
index b709e590a2d..2cb732c3016 100644
--- a/bfd/elf-sframe.c
+++ b/bfd/elf-sframe.c
@@ -120,7 +120,6 @@ sframe_decoder_init_func_bfdinfo (bfd *abfd,
   if ((sec->flags & SEC_LINKER_CREATED) && cookie->rels == NULL)
     return true;
 
-  BFD_ASSERT (cookie->rels + fde_count == cookie->relend);
   rel = cookie->rels;
   for (i = 0; i < fde_count; i++)
     {
@@ -132,6 +131,14 @@ sframe_decoder_init_func_bfdinfo (bfd *abfd,
       rel++;
     }
 
+  /* If there are more relocation entries, they must be R_*_NONE which
+     may be generated from relocations against discarded sections by
+     ld -r.  */
+  for (; rel < cookie->relend; rel++)
+   if (rel->r_info != 0)
+     break;
+  BFD_ASSERT (rel == cookie->relend);
+
   return true;
 }
 
diff --git a/ld/testsuite/ld-x86-64/sframe-reloc-2a.s b/ld/testsuite/ld-x86-64/sframe-reloc-2a.s
new file mode 100644
index 00000000000..8cd8694123e
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/sframe-reloc-2a.s
@@ -0,0 +1,7 @@
+	.section	sect, "axG", @progbits, sectgroup, comdat
+	.global	_start
+_start:
+	.cfi_startproc
+	call	foo
+	.cfi_endproc
+	.section	.note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/sframe-reloc-2b.s b/ld/testsuite/ld-x86-64/sframe-reloc-2b.s
new file mode 100644
index 00000000000..9484636144d
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/sframe-reloc-2b.s
@@ -0,0 +1,31 @@
+	.section	sect, "axG", @progbits, sectgroup, comdat
+	.cfi_startproc
+# Test intention is that LSDA must be provided by the discarded FDE.
+# DW_EH_PE_udata8 = 4
+# DW_EH_PE_udata4 = 3
+	.ifdef		ELF64
+	.cfi_lsda 4, lsda
+	.else
+	.cfi_lsda 3, lsda
+	.endif
+	.skip 16
+	.cfi_endproc
+
+	.text
+	.globl	foo
+	.type	foo, @function
+foo:
+	.cfi_startproc
+	pushq	%rbp
+	.cfi_def_cfa_offset 16
+	.cfi_offset 6, -16
+	movq	%rsp, %rbp
+	.cfi_def_cfa_register 6
+	popq	%rbp
+	.cfi_def_cfa 7, 8
+	ret
+	.cfi_endproc
+
+	.section	.gcc_except_table, "a", @progbits
+lsda:
+	.section	.note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index a682b1374fa..3d873a79b78 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -572,6 +572,27 @@ if { ![skip_sframe_tests] } {
     run_dump_test "sframe-ibt-plt-1"
     run_dump_test "sframe-pltgot-1"
     run_dump_test "sframe-pltgot-2"
+
+    run_ld_link_tests [list \
+	[list \
+	    "Build sframe-reloc-2.o" \
+	    "-m elf_x86_64 -r" \
+	    "" \
+	    "--64 --gsframe" \
+	    {sframe-reloc-2a.s sframe-reloc-2b.s} \
+	    "" \
+	    "sframe-reloc-2.o" \
+	] \
+	[list \
+	    "Build sframe-reloc-2" \
+	    "-m elf_x86_64 tmpdir/sframe-reloc-2.o" \
+	    "" \
+	    "--64 --gsframe" \
+	    {dummy.s} \
+	    "" \
+	    "sframe-reloc-2" \
+	] \
+    ]
 }
 
 if ![istarget "x86_64-*-linux*"] {


More information about the Binutils-cvs mailing list