bfd: include: sframe: fix PR ld/32789

Currently, when SFrame sections are emitted after linking the input
SFrame sections, the SFrame FDEs are sorted on start PC.  Doing so for
relocatable links has no effect (SFrame FDEs remain in place), because
the start PC is unrelocated data.  For relocatable links, then, the
emitted SFrame FDEs in the output section remain in the same order as
that in the respective input BFD.

The assembler does not guarantee the emission of SFrame FDEs in the same
order as the placement of the associated .text* sections,
(SFRAME_F_FDE_SORTED is not set in the ET_REL objs generated by GAS).
This means setting SFRAME_F_FDE_SORTED by the linker was wrong when:
  - doing relocatable link, and
  - the input bfds contain multiple .text sections, say .text.hot,
    .text.init, .text.unlikely etc.

For relocatable links, skip sorting the SFrame FDEs.  Do not set
SFRAME_F_FDE_SORTED for relocatable links.

This is achieved by adding an explicit argument (bool sort_fde_p) to the
sframe_encoder_write API.  Move the API from 2.0 to the 2.1 node as this
is an ABI-incompatible change.  Skip bumping the "current" in
libsframe/libtool-version ATM, we will do so closer to release.

When writing of SFrame data for PLT entries, indicate sort_fde_p to
false: these sections are like the other SFrame sections for any other
ET_REL binary.

Add a test in ld/testsuite/ld-sframe/sframe.exp, these tests are run for
all ABIs supported for SFrame.  In this test, for object file generated
for pr32789-1a.c:
  - the emitted SFrame FDEs by GAS are in the order of the .text* in the
    input assembly (i.e., .text.init, .text, .text.exit)
  - the emitted .text* sections by GAS are placed in the following order
    .text, .text.init, .text.exit.
  - GAS does not set SFRAME_F_FDE_SORTED, as expected.

Reviewed-by: Jens Remus <jremus@linux.ibm.com>

bfd/
	PR ld/32789
	* elf-sframe.c (_bfd_elf_write_section_sframe): Skip sorting the
	SFrame FDEs for relocatable links.
	* elf64-s390.c (_bfd_s390_elf_write_sframe_plt): Additional
	argument to sframe_encoder_write.
	* elfxx-x86.c (_bfd_x86_elf_write_sframe_plt): Likewise.
libsframe/
	* libsframe.ver: Move from 2.0 node to 2.1.
	* sframe.c (sframe_encoder_write_sframe): Conditionalize based
	on argument sort_fde_p.
	(sframe_encoder_write): New argument to indicate whether SFrame
	FDEs are to be sorted in output.
include/
	* sframe-api.h (sframe_encoder_write): New argument.
ld/testsuite/
	PR ld/32789
	* ld/testsuite/ld-sframe/sframe.exp: New test.
	* ld/testsuite/ld-sframe/pr32789-1.rd: New test.
	* ld/testsuite/ld-sframe/pr32789-1.sd: New test.
	* ld/testsuite/ld-sframe/pr32789-1a.c: New test.
	* ld/testsuite/ld-sframe/pr32789-1b.c: New test.
	* ld/testsuite/ld-x86-64/sframe-reloc-1.d: Remove
	SFRAME_F_FDE_SORTED.
This commit is contained in:
Indu Bhagat
2025-12-19 23:11:45 -08:00
parent ede64c814b
commit 4651bea119
12 changed files with 85 additions and 21 deletions

View File

@@ -692,7 +692,8 @@ _bfd_elf_write_section_sframe (bfd *abfd, struct bfd_link_info *info)
if (sec == NULL)
return true;
contents = sframe_encoder_write (sfe_ctx, &sec_size, &err);
bool sort_p = !bfd_link_relocatable (info);
contents = sframe_encoder_write (sfe_ctx, &sec_size, sort_p, &err);
sec->size = (bfd_size_type) sec_size;
if (!bfd_set_section_contents (abfd, sec->output_section, contents,

View File

@@ -1682,7 +1682,7 @@ _bfd_s390_elf_write_sframe_plt (struct bfd_link_info *info)
BFD_ASSERT (ectx);
void *contents = sframe_encoder_write (ectx, &sec_size, &err);
void *contents = sframe_encoder_write (ectx, &sec_size, false, &err);
sec->size = (bfd_size_type) sec_size;
sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);

View File

@@ -2016,7 +2016,7 @@ _bfd_x86_elf_write_sframe_plt (bfd *output_bfd,
BFD_ASSERT (ectx);
void *contents = sframe_encoder_write (ectx, &sec_size, &err);
void *contents = sframe_encoder_write (ectx, &sec_size, false, &err);
sec->size = (bfd_size_type) sec_size;
sec->contents = (unsigned char *) bfd_zalloc (dynobj, sec->size);

View File

@@ -309,11 +309,11 @@ sframe_encoder_add_funcdesc_v2 (sframe_encoder_ctx *ectx,
uint32_t num_fres);
/* Serialize the contents of the encoder context ECTX and return the buffer.
ENCODED_SIZE is updated to the size of the buffer.
Sets ERRP if failure. */
Sort the SFrame FDEs on start PC if SORT_FDE_P is true. ENCODED_SIZE is
updated to the size of the buffer. Sets ERRP if failure. */
extern char *
sframe_encoder_write (sframe_encoder_ctx *ectx,
size_t *encoded_size, int *errp);
sframe_encoder_write (sframe_encoder_ctx *ectx, size_t *encoded_size,
bool sort_fde_p, int *errp);
#ifdef __cplusplus
}

View File

@@ -0,0 +1,9 @@
#...
Relocation section '.rela.sframe' at offset 0x[0-9a-f]+ contains 5 entries:
+Offset +Info +Type +Sym.* Value +Sym.* Name \+ Addend
[0-9a-f ]+ R_.* +0+ .text.init \+ 0
[0-9a-f ]+ R_.* +0+ .text \+ 0
[0-9a-f ]+ R_.* +0+ .text.exit \+ 0
[0-9a-f ]+ R_.* +0+ .text \+ [0-9a-f]+
[0-9a-f ]+ R_.* +0+ .text \+ [0-9a-f]+
#...

View File

@@ -0,0 +1,4 @@
#failif
#...
.*\(SFRAME_F_FDE_SORTED\).*
#...

View File

@@ -0,0 +1,22 @@
static int a = 0;
extern int bar(void);
__attribute__((section((".text.init"))))
int foo_init(void)
{
return 10;
}
void foo (void)
{
foo_init ();
a++;
bar ();
}
__attribute__((section((".text.exit"))))
void foo_exit(void)
{
foo ();
}

View File

@@ -0,0 +1,12 @@
static int bar_var = 2;
int bar(void)
{
return 20;
}
void bar2(void)
{
bar_var++;
bar ();
}

View File

@@ -90,6 +90,22 @@ foreach sframe_test $sframe_test_list {
check_pr33401
# Run other compile and link tests.
if { [check_compiler_available] } {
run_cc_link_tests [list \
[list \
"Build pr32789-1a.o pr32789-1b.o" \
"-r" \
"-Wa,--gsframe" \
{ pr32789-1a.c pr32789-1b.c } \
{{readelf --sframe pr32789-1.sd}
{readelf -r pr32789-1.rd}} \
"pr32789-1.o" \
"c" \
] \
]
}
if {[info exists old_lc_all]} {
set env(LC_ALL) $old_lc_all
} else {

View File

@@ -11,8 +11,7 @@ Contents of the SFrame section .sframe:
Header :
Version: SFRAME_VERSION_2
Flags: SFRAME_F_FDE_SORTED,
SFRAME_F_FDE_FUNC_START_PCREL
Flags: SFRAME_F_FDE_FUNC_START_PCREL
CFA fixed RA offset: \-8
Num FDEs: 2
Num FREs: 8

View File

@@ -34,7 +34,6 @@ LIBSFRAME_2.0 {
sframe_encoder_add_fre;
sframe_encoder_add_funcdesc;
sframe_encoder_add_funcdesc_v2;
sframe_encoder_write;
dump_sframe;
sframe_errmsg;
@@ -45,4 +44,5 @@ LIBSFRAME_2.0 {
LIBSFRAME_2.1 {
global:
sframe_fre_get_ra_undefined_p;
sframe_encoder_write;
} LIBSFRAME_2.0;

View File

@@ -1975,11 +1975,11 @@ sframe_encoder_write_fre (char *contents, sframe_frame_row_entry *frep,
}
/* Serialize the core contents of the SFrame section and write out to the
output buffer held in the encoder context ECTX. Return SFRAME_ERR if
failure. */
output buffer held in the encoder context ECTX. Sort the SFrame FDEs on
start PC if SORT_FDE_P is true. Return SFRAME_ERR if failure. */
static int
sframe_encoder_write_sframe (sframe_encoder_ctx *ectx)
sframe_encoder_write_sframe (sframe_encoder_ctx *ectx, bool sort_fde_p)
{
char *contents;
size_t buf_size;
@@ -2059,12 +2059,13 @@ sframe_encoder_write_sframe (sframe_encoder_ctx *ectx)
sframe_assert ((size_t)(contents - ectx->sfe_data) == buf_size);
/* Sort the FDE table */
sframe_sort_funcdesc (ectx);
if (sort_fde_p)
sframe_sort_funcdesc (ectx);
/* Sanity checks:
- the FDE section must have been sorted by now on the start address
of each function. */
if (!(sframe_encoder_get_flags (ectx) & SFRAME_F_FDE_SORTED)
of each function, if sorting was needed. */
if ((sort_fde_p != (sframe_encoder_get_flags (ectx) & SFRAME_F_FDE_SORTED))
|| (fd_info == NULL))
return sframe_set_errno (&err, SFRAME_ERR_FDE_INVAL);
@@ -2082,12 +2083,12 @@ sframe_encoder_write_sframe (sframe_encoder_ctx *ectx)
}
/* Serialize the contents of the encoder context ECTX and return the buffer.
ENCODED_SIZE is updated to the size of the buffer.
Sets ERRP if failure. */
Sort the SFrame FDEs on start PC if SORT_FDE_P is true. ENCODED_SIZE is
updated to the size of the buffer. Sets ERRP if failure. */
char *
sframe_encoder_write (sframe_encoder_ctx *ectx,
size_t *encoded_size, int *errp)
sframe_encoder_write (sframe_encoder_ctx *ectx, size_t *encoded_size,
bool sort_fde_p, int *errp)
{
sframe_header *ehp;
size_t hdrsize, fsz, fresz, bufsize;
@@ -2128,7 +2129,7 @@ sframe_encoder_write (sframe_encoder_ctx *ectx,
foreign_endian = need_swapping (ehp->sfh_abi_arch);
/* Write out the FDE Index and the FRE table in the sfe_data. */
if (sframe_encoder_write_sframe (ectx))
if (sframe_encoder_write_sframe (ectx, sort_fde_p))
return sframe_ret_set_errno (errp, SFRAME_ERR_BUF_INVAL);
/* Endian flip the contents if necessary. */