[v2] elf: Keep section symbols for Solaris binary

Message ID CAMe9rOrGztxs_1-vrBRtwnoNrP=Jv6m1f-Ae65R42sp8+W-c2Q@mail.gmail.com
State New
Headers
Series [v2] elf: Keep section symbols for Solaris binary |

Commit Message

H.J. Lu Dec. 6, 2025, 3:04 a.m. UTC
  On Fri, Dec 5, 2025 at 1:31 AM Ali Bahrami <Ali.Bahrami@oracle.com> wrote:
>
> [I've dropped the binutils alias for the moment]
>
> On 12/4/25 02:36, Jan Beulich wrote:
> > On 04.12.2025 02:20, H.J. Lu wrote:
> >> After
> >>
> >> commit d1bcae833b32f1408485ce69f844dcd7ded093a8
> >> Author: H.J. Lu <hjl.tools@gmail.com>
> >> Date:   Thu Jan 7 06:42:00 2021 -0800
> >>
> >>      ELF: Don't generate unused section symbols
> >>
> >> x86 ELF linkers no longer generate unused section symbols.  But Solaris
> >> symbol sort sections generated by Solaris linker use section symbols.
> >> When objcopy removes section symbols, it corrupts Solaris symbol sort
> >> sections.  Add section_symbol_used to bfd and set it when Solaris symbol
> >> sort sections are used.  Keep section symbols if section_symbol_used is
> >> set.
> >>
> >> PR binutils/33684
> >> * bfd-in2.h: Regenerated.
> >> * bfd.c (bfd): Add section_symbol_used.
> >> * elf.c (_bfd_elf_copy_private_section_data): Set
> >> section_symbol_used if there are Solaris symbol sort sections.
> >> * targets.c (bfd_keep_unused_section_symbols): Also return true
> >> if section_symbol_used is set.
> >
> > This looks okay to me, but I'd like to ask Ali or Rainer to confirm.
> >
> > Jan
>
>
>     As you'll see on the bug thread, I was pushing for
> a different solution last night that doesn't specifically
> check for the sort sections, but simply keys off the
> ELFOSABI_SOLARIS setting in the ELF header.
>
>      https://sourceware.org/bugzilla/show_bug.cgi?id=33684
>
> I understand that removing the section symbols is an x86
> binutils optimization, and I don't have a problem with that,
> but it's breaking expectations in our world, and I think it
> would be simpler to disable it, rather than maintain code
> that looks for specific sections. The OSABI setting is an
> easy way to recognize Solaris objects, and opt them out.
>
> I did however just apply the patch as it stands. While it
> applies cleanly, the result doesn't build on Solaris. I
> don't build binutils often, and may have botched something,
> so I'll ask Rainer to double check.
>
After

commit d1bcae833b32f1408485ce69f844dcd7ded093a8
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 7 06:42:00 2021 -0800

    ELF: Don't generate unused section symbols

x86 ELF binutils tools no longer generate unused section symbols.  But
Solaris binary generated by Solaris linker may include symbol sort
sections which hold indexes of data and function symbols, sorted by name
or by address.  When objcopy removes section symbols, it corrupts Solaris
symbol sort sections since symbol indexes are changed.  Keep section
symbols for Solaris binary.

PR binutils/33684
* objcopy.c (copy_object): Keep section symbols if input is a
Solaris binary and output target is the same as input target.
  

Comments

Ali Bahrami Dec. 6, 2025, 4:31 p.m. UTC | #1
On 12/5/25 20:04, H.J. Lu wrote:
> On Fri, Dec 5, 2025 at 1:31 AM Ali Bahrami <Ali.Bahrami@oracle.com> wrote:
>>
>> [I've dropped the binutils alias for the moment]
>>
>> On 12/4/25 02:36, Jan Beulich wrote:
>>> On 04.12.2025 02:20, H.J. Lu wrote:
>>>> After
>>>>
>>>> commit d1bcae833b32f1408485ce69f844dcd7ded093a8
>>>> Author: H.J. Lu <hjl.tools@gmail.com>
>>>> Date:   Thu Jan 7 06:42:00 2021 -0800
>>>>
>>>>       ELF: Don't generate unused section symbols
>>>>
>>>> x86 ELF linkers no longer generate unused section symbols.  But Solaris
>>>> symbol sort sections generated by Solaris linker use section symbols.
>>>> When objcopy removes section symbols, it corrupts Solaris symbol sort
>>>> sections.  Add section_symbol_used to bfd and set it when Solaris symbol
>>>> sort sections are used.  Keep section symbols if section_symbol_used is
>>>> set.
>>>>
>>>> PR binutils/33684
>>>> * bfd-in2.h: Regenerated.
>>>> * bfd.c (bfd): Add section_symbol_used.
>>>> * elf.c (_bfd_elf_copy_private_section_data): Set
>>>> section_symbol_used if there are Solaris symbol sort sections.
>>>> * targets.c (bfd_keep_unused_section_symbols): Also return true
>>>> if section_symbol_used is set.
>>>
>>> This looks okay to me, but I'd like to ask Ali or Rainer to confirm.
>>>
>>> Jan
>>
>>
>>      As you'll see on the bug thread, I was pushing for
>> a different solution last night that doesn't specifically
>> check for the sort sections, but simply keys off the
>> ELFOSABI_SOLARIS setting in the ELF header.
>>
>>       https://urldefense.com/v3/__https://sourceware.org/bugzilla/show_bug.cgi?id=33684__;!!ACWV5N9M2RV99hQ!JbrcfZ8ogu1uw3OkfNSJv-CLQ98-0w7fe4BHqaRp7w7nBPQA-tO6RHrRmsaoAXd8fUiCYgAU4U6Y2tWYtBo$
>>
>> I understand that removing the section symbols is an x86
>> binutils optimization, and I don't have a problem with that,
>> but it's breaking expectations in our world, and I think it
>> would be simpler to disable it, rather than maintain code
>> that looks for specific sections. The OSABI setting is an
>> easy way to recognize Solaris objects, and opt them out.
>>
>> I did however just apply the patch as it stands. While it
>> applies cleanly, the result doesn't build on Solaris. I
>> don't build binutils often, and may have botched something,
>> so I'll ask Rainer to double check.
>>
> After
> 
> commit d1bcae833b32f1408485ce69f844dcd7ded093a8
> Author: H.J. Lu <hjl.tools@gmail.com>
> Date:   Thu Jan 7 06:42:00 2021 -0800
> 
>      ELF: Don't generate unused section symbols
> 
> x86 ELF binutils tools no longer generate unused section symbols.  But
> Solaris binary generated by Solaris linker may include symbol sort
> sections which hold indexes of data and function symbols, sorted by name
> or by address.  When objcopy removes section symbols, it corrupts Solaris
> symbol sort sections since symbol indexes are changed.  Keep section
> symbols for Solaris binary.
> 
> PR binutils/33684
> * objcopy.c (copy_object): Keep section symbols if input is a
> Solaris binary and output target is the same as input target.
> 

    I added the patch for this latest version, which
takes the osabi approach, and built binutils for Solaris.
The resulting objcopy preserves the section symbol and
appears to resolve the issue. Thank You.

- Ali
  
Jan Beulich Dec. 8, 2025, 7:52 a.m. UTC | #2
On 06.12.2025 04:04, H.J. Lu wrote:
> After
> 
> commit d1bcae833b32f1408485ce69f844dcd7ded093a8
> Author: H.J. Lu <hjl.tools@gmail.com>
> Date:   Thu Jan 7 06:42:00 2021 -0800
> 
>     ELF: Don't generate unused section symbols
> 
> x86 ELF binutils tools no longer generate unused section symbols.  But
> Solaris binary generated by Solaris linker may include symbol sort
> sections which hold indexes of data and function symbols, sorted by name
> or by address.  When objcopy removes section symbols, it corrupts Solaris
> symbol sort sections since symbol indexes are changed.  Keep section
> symbols for Solaris binary.
> 
> PR binutils/33684
> * objcopy.c (copy_object): Keep section symbols if input is a
> Solaris binary and output target is the same as input target.

Hmm, an open-coded OSABI check in objcopy. I was expecting change in libbfd
to e.g. make bfd_keep_unused_section_symbols() on ibfd true for Solaris, to
then copy that property to obfd. Is there a reason to not try to somewhat
abstract what is wanted / needed?

Jan
  

Patch

From 9c9925d545b6abcff8cca4787b183900ca321fbc Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 3 Dec 2025 08:16:42 +0800
Subject: [PATCH v2] elf: Keep section symbols for Solaris binary

After

commit d1bcae833b32f1408485ce69f844dcd7ded093a8
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Jan 7 06:42:00 2021 -0800

    ELF: Don't generate unused section symbols

x86 ELF binutils tools no longer generate unused section symbols.  But
Solaris binary generated by Solaris linker may include symbol sort
sections which hold indexes of data and function symbols, sorted by name
or by address.  When objcopy removes section symbols, it corrupts Solaris
symbol sort sections since symbol indexes are changed.  Keep section
symbols for Solaris binary.

	PR binutils/33684
	* objcopy.c (copy_object): Keep section symbols if input is a
	Solaris binary and output target is the same as input target.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 binutils/objcopy.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 17d4b501185..14fb8debbdf 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -2947,6 +2947,11 @@  copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 	return false;
     }
 
+  int elf_osabi = -1;
+  if (ibfd->xvec == obfd->xvec
+      && bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
+    elf_osabi = elf_elfheader (ibfd)->e_ident[EI_OSABI];
+
   if (add_sections != NULL)
     {
       struct section_add *padd;
@@ -3323,7 +3328,10 @@  copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
   if ((obfd->flags & (EXEC_P | DYNAMIC)) != 0
       && (obfd->flags & HAS_RELOC) == 0)
     {
-      if (bfd_keep_unused_section_symbols (obfd) || keep_section_symbols)
+      /* Keep section symbols for Solaris binary.  */
+      if (elf_osabi == ELFOSABI_SOLARIS
+	  || bfd_keep_unused_section_symbols (obfd)
+	  || keep_section_symbols)
 	{
 	  /* Non-relocatable inputs may not have the unused section
 	     symbols.  Mark all section symbols as used to generate
-- 
2.52.0