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

