elf: Keep section symbols for Solaris symbol sort sections

Message ID CAMe9rOqZuigUu6tqJpk0Ex3EEMth5BKHJ47ESrH=7PuuENSO+Q@mail.gmail.com
State New
Headers
Series elf: Keep section symbols for Solaris symbol sort sections |

Commit Message

H.J. Lu Dec. 4, 2025, 1:20 a.m. UTC
  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

Jan Beulich Dec. 4, 2025, 9:36 a.m. UTC | #1
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
  

Patch

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(-)

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 6c05c21a0da..01fe9e5ee16 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -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
diff --git a/bfd/bfd.c b/bfd/bfd.c
index 11ce75669d3..3b78cdf6abc 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -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
diff --git a/bfd/elf.c b/bfd/elf.c
index 33c2d269a9c..d55ce8493f8 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -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;
 }
 
diff --git a/bfd/targets.c b/bfd/targets.c
index 4ac70921c35..43424d2b0b2 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -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