elf: Keep section symbols for Solaris symbol sort sections
Commit Message
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.
Comments
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
From 61f39874e9b5577e0b810bfc19f0af720e0e0f89 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] elf: Keep section symbols for Solaris symbol sort sections
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.
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
bfd/bfd-in2.h | 6 +++++-
bfd/bfd.c | 3 +++
bfd/elf.c | 14 ++++++++++++++
bfd/targets.c | 3 ++-
4 files changed, 24 insertions(+), 2 deletions(-)
@@ -2152,6 +2152,9 @@ struct bfd
/* LTO object type. */
ENUM_BITFIELD (bfd_lto_object_type) lto_type : 3;
+ /* Set if section symbols are used. */
+ unsigned int section_symbol_used : 1;
+
/* Set if this BFD is currently being processed by
bfd_check_format_matches. This is checked by the cache to
avoid closing the BFD in this case. This should only be
@@ -8010,7 +8013,8 @@ bfd_asymbol_flavour (const asymbol *sy)
static inline bool
bfd_keep_unused_section_symbols (const bfd *abfd)
{
- return abfd->xvec->keep_unused_section_symbols;
+ return (abfd->section_symbol_used
+ || abfd->xvec->keep_unused_section_symbols);
}
static inline bool
@@ -309,6 +309,9 @@ CODE_FRAGMENT
. {* LTO object type. *}
. ENUM_BITFIELD (bfd_lto_object_type) lto_type : 3;
.
+. {* Set if section symbols are used. *}
+. unsigned int section_symbol_used : 1;
+.
. {* Set if this BFD is currently being processed by
. bfd_check_format_matches. This is checked by the cache to
. avoid closing the BFD in this case. This should only be
@@ -8551,6 +8551,20 @@ _bfd_elf_copy_private_section_data (bfd *ibfd,
osec->use_rela_p = isec->use_rela_p;
+ if (get_elf_backend_data (obfd)->target_os == is_solaris
+ && !obfd->section_symbol_used)
+ switch (elf_section_type (osec))
+ {
+ case SHT_SUNW_symtabnsort:
+ case SHT_SUNW_symsort:
+ case SHT_SUNW_tlssort:
+ /* Section symbols are used by Solaris symbol sort sections. */
+ obfd->section_symbol_used = 1;
+ break;
+ default:
+ break;
+ }
+
return true;
}
@@ -661,7 +661,8 @@ to find an alternative output format that is suitable.
.static inline bool
.bfd_keep_unused_section_symbols (const bfd *abfd)
.{
-. return abfd->xvec->keep_unused_section_symbols;
+. return (abfd->section_symbol_used
+. || abfd->xvec->keep_unused_section_symbols);
.}
.
.static inline bool
--
2.52.0