[RFC] ld: bfd: sframe: Update section size also for relocatable links

Message ID 20250312163640.218942-1-jremus@linux.ibm.com
State New
Headers
Series [RFC] ld: bfd: sframe: Update section size also for relocatable links |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm warning Skipped upon request
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 warning Skipped upon request

Commit Message

Jens Remus March 12, 2025, 4:36 p.m. UTC
  For relocatable links the output .sframe section size may be wrong.
This can be observed when dumping the SFrame information from the x86-64
sframe-reloc-1 test:

Name              Address          Off    Size
.sframe           0000000000000000 000110 00007f

Offset            Type               Symbol's Value  Symbol's Name + Addend
000000000000001c  R_X86_64_PC32      0000000000000000 .text + 1c
0000000000000030  R_X86_64_PC32      0000000000000000 .text + 65

0x00000000 e2de0201 0300f800 02000000 08000000 ................
0x00000010 1e000000 00000000 28000000 00000000 ........(.......
0x00000020 35000000 00000000 04000000 00000000 5...............
0x00000030 00000000 25000000 0f000000 04000000 ....%...........
            offset 1st FRE---^^^^^^^^ ^^^^^^^^---number of FREs
0x00000040 00000000 00030801 0510f004 0410f034 ...............4
FDE info---^^      | begin of FDEs
0x00000050 0508f000 03080105 10f00404 10f02405 ..............$.
                 11111112222222223333333334444---FRE 1, 2, 3, 4
0x00000060 08f00000 00000000 00000000 00000000 ................
           4444^^^^...
0x00000070 00000000 00000000 00000000 000000   ...............
                                   ...^^^^^^---excessive section

When running the x86-64 test cross build on a big-endian system, such
as s390x, objdump and readelf fail to dump the SFrame information with
the following error message:

Error: SFrame decode failure: Buffer does not contain SFrame data.

This is because the following check in flip_sframe() fails, which gets
only invoked if the endianness of the SFrame data is different from the
host system one:

/* All FDEs and FREs must have been endian flipped by now.  */
if ((j != ihp->sfh_num_fres) || (bytes_flipped != (buf_size - hdrsz)))
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With:
j=8, ihp->sfh_num_fres=8, bytes_flipped=70, buf_size=127, hdrsz=28

bfd/
	* elf-sframe.c (_bfd_elf_write_section_sframe): Update section
	size also for relocatable links.

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

Notes (jremus):
    Hello Indu,
    
    I ran into this while crafting a sample that shows the issue I see with
    FDE sorting in relocatable links with multiple text sections.  Please
    consider to add to or merge with your "[RFC 0/4] Fix relocatable links
    with SFrame section" series.
    
    Maybe it would make sense to add a check that would catch this when
    sframe_flip() is not invoked?
    
    Thanks and regards,
    Jens

 bfd/elf-sframe.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
  

Patch

diff --git a/bfd/elf-sframe.c b/bfd/elf-sframe.c
index e0bc7cf2a433..4fb6c3eb4624 100644
--- a/bfd/elf-sframe.c
+++ b/bfd/elf-sframe.c
@@ -636,13 +636,11 @@  _bfd_elf_write_section_sframe (bfd *abfd, struct bfd_link_info *info)
 				 (file_ptr) sec->output_offset,
 				 sec->size))
     retval = false;
-  else if (!bfd_link_relocatable (info))
+  else
     {
       Elf_Internal_Shdr *hdr = &elf_section_data (sec)->this_hdr;
       hdr->sh_size = sec->size;
     }
-  /* For relocatable links, do not update the section size as the section
-     contents have not been relocated.  */
 
   sframe_encoder_free (&sfe_ctx);