[PATCH] ELF: Properly copy and strip Solaris binaries
Alan Modra
amodra@gmail.com
Thu Jan 1 21:50:45 GMT 2026
On Wed, Dec 31, 2025 at 07:29:38AM +0800, H.J. Lu wrote:
> On Wed, Dec 31, 2025 at 6:57 AM Alan Modra <amodra@gmail.com> wrote:
> >
> > On Sat, Dec 27, 2025 at 10:14:03AM +0800, H.J. Lu wrote:
> > > * bfd.c (bfd_elf_target_os): New.
> > > (bfd): Add elf_target_os.
> > > * elf-bfd.h (elf_target_os): Removed.
> >
> > Why do we need another target_os? There's one in the linker hash
> > table and one in elf_backend_data. You have access to the latter and
> > EI_OSABI via the bfd.
>
> Since elf_strtab_flags is hardcoded in elf_backend_data which can't be
> changed, I need to mark Solaris binary in BFD for generic ELF targets
> so that elf_strtab_flags can be changed for Solaris binary when generic
> ELF targets are used.
You can do that without a new bfd flag.
Subject: Solaris .strtab and .shstrtab flags
These sections have SHF_STRINGS in sh_flags on Solaris. Commit
84865015459b in part implemented this variation by an elf_backend_data
field used to set the flags, but that only works of course if one of
the solaris targets is used. Which in some ways is fair enough. If
you want solaris support then it is reasonable to require the solaris
targets to be compiled in. However if they are not the default, other
ELF targets may be used even when the solaris targets are compiled in,
because many ELF targets allow any ELFOSABI object to match. (Which
is arguably a bug.)
So instead of the current scheme this patch implements the solaris
specific sh_flags in _bfd_elf_final_write_processing. That way either
a solaris target being used, or ELFOSABI_SOLARIS in the object will
get the correct sh_flags.
PR 19938
* elf-bfd.h (struct elf_backend_data): Delete elf_strtab_flags.
* elf.c (_bfd_elf_final_write_processing): Handle solaris
peculiarities here.
(_bfd_elf_compute_section_file_positions): Leave shstrtab sh_flags
zero, and don't re-zero other fields.
(swap_out_syms): Similarly for sym strtab.
* elflink.c (_bfd_elf_final_link): Likewise.
* elf32-i386.c (elf_backend_strtab_flags): Don't define.
* elf32-sparc.c: Likewise.
* elf64-sparc.c: Likewise.
* elf64-x86-64.c: Likewise.
* elfxx-target.h: Likewise.
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index e059af53875..c5a258e25ad 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -1712,9 +1712,6 @@ struct elf_backend_data
/* Alignment for the PT_GNU_STACK segment. */
unsigned stack_align;
- /* Flag bits to assign to a section of type SHT_STRTAB. */
- unsigned long elf_strtab_flags;
-
/* This is TRUE if the linker should act like collect and gather
global constructors and destructors by name. This is TRUE for
MIPS ELF because the Irix 5 tools can not handle the .init
diff --git a/bfd/elf.c b/bfd/elf.c
index a652e9bc41e..f327653e5f6 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4745,12 +4745,9 @@ _bfd_elf_compute_section_file_positions (bfd *abfd,
shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
/* sh_name was set in init_file_header. */
shstrtab_hdr->sh_type = SHT_STRTAB;
- shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
- shstrtab_hdr->sh_addr = 0;
+ /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
+ when tdata is allocated. */
/* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
- shstrtab_hdr->sh_entsize = 0;
- shstrtab_hdr->sh_link = 0;
- shstrtab_hdr->sh_info = 0;
/* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
shstrtab_hdr->sh_addralign = 1;
@@ -9080,11 +9077,6 @@ Unable to handle section index %x in ELF symbol. Using ABS instead."),
*sttp = stt;
symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
symstrtab_hdr->sh_type = SHT_STRTAB;
- symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
- symstrtab_hdr->sh_addr = 0;
- symstrtab_hdr->sh_entsize = 0;
- symstrtab_hdr->sh_link = 0;
- symstrtab_hdr->sh_info = 0;
symstrtab_hdr->sh_addralign = 1;
return true;
@@ -13491,12 +13483,18 @@ _bfd_elf_get_synthetic_symtab (bfd *abfd,
bool
_bfd_elf_final_write_processing (bfd *abfd)
{
- Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */
-
- i_ehdrp = elf_elfheader (abfd);
+ Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
- i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
+ i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
+
+ if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
+ || bed->target_os == is_solaris)
+ {
+ elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
+ elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
+ }
/* Set the osabi field to ELFOSABI_GNU if the binary contains
SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index cab9a76c120..d1843f501d5 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -4681,9 +4681,6 @@ elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
#undef elf_backend_want_plt_sym
#define elf_backend_want_plt_sym 1
-#undef elf_backend_strtab_flags
-#define elf_backend_strtab_flags SHF_STRINGS
-
#include "elf32-target.h"
/* Intel MCU support. */
@@ -4723,8 +4720,6 @@ elf32_iamcu_elf_object_p (bfd *abfd)
#undef elf_backend_want_plt_sym
#define elf_backend_want_plt_sym 0
-#undef elf_backend_strtab_flags
-
#include "elf32-target.h"
/* Restore defaults. */
diff --git a/bfd/elf32-sparc.c b/bfd/elf32-sparc.c
index b7ac618817d..37dc926d0de 100644
--- a/bfd/elf32-sparc.c
+++ b/bfd/elf32-sparc.c
@@ -291,9 +291,6 @@ elf32_sparc_reloc_type_class (const struct bfd_link_info *info,
#undef elf_backend_static_tls_alignment
#define elf_backend_static_tls_alignment 8
-#undef elf_backend_strtab_flags
-#define elf_backend_strtab_flags SHF_STRINGS
-
static bool
elf32_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
bfd *obfd ATTRIBUTE_UNUSED,
@@ -352,7 +349,6 @@ elf32_sparc_vxworks_final_write_processing (bfd *abfd)
#define elf_backend_final_write_processing \
elf32_sparc_vxworks_final_write_processing
#undef elf_backend_static_tls_alignment
-#undef elf_backend_strtab_flags
#undef elf_backend_copy_special_section_fields
#undef elf32_bed
diff --git a/bfd/elf64-sparc.c b/bfd/elf64-sparc.c
index 936b0ce4465..d249cbce6de 100644
--- a/bfd/elf64-sparc.c
+++ b/bfd/elf64-sparc.c
@@ -1022,9 +1022,6 @@ static const struct elf_size_info elf64_sparc_size_info =
#undef elf_backend_static_tls_alignment
#define elf_backend_static_tls_alignment 16
-#undef elf_backend_strtab_flags
-#define elf_backend_strtab_flags SHF_STRINGS
-
static bool
elf64_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
bfd *obfd ATTRIBUTE_UNUSED,
@@ -1041,5 +1038,4 @@ elf64_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSE
#include "elf64-target.h"
-#undef elf_backend_strtab_flags
#undef elf_backend_copy_special_section_fields
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 2e2687b99f7..4be86c3aa24 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -6464,9 +6464,6 @@ elf_x86_64_special_sections[]=
#undef elf_backend_want_plt_sym
#define elf_backend_want_plt_sym 1
-#undef elf_backend_strtab_flags
-#define elf_backend_strtab_flags SHF_STRINGS
-
#include "elf64-target.h"
/* Restore defaults. */
@@ -6474,7 +6471,6 @@ elf_x86_64_special_sections[]=
#undef elf_backend_static_tls_alignment
#undef elf_backend_want_plt_sym
#define elf_backend_want_plt_sym 0
-#undef elf_backend_strtab_flags
/* 32bit x86-64 support. */
diff --git a/bfd/elflink.c b/bfd/elflink.c
index e815e6b381b..d0b1ec5cb04 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -13475,15 +13475,8 @@ _bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
}
symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
- /* sh_name was set in prep_headers. */
symstrtab_hdr->sh_type = SHT_STRTAB;
- symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
- symstrtab_hdr->sh_addr = 0;
symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
- symstrtab_hdr->sh_entsize = 0;
- symstrtab_hdr->sh_link = 0;
- symstrtab_hdr->sh_info = 0;
- /* sh_offset is set just below. */
symstrtab_hdr->sh_addralign = 1;
off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h
index 00552826b4c..479ba47785d 100644
--- a/bfd/elfxx-target.h
+++ b/bfd/elfxx-target.h
@@ -142,9 +142,6 @@
#ifndef elf_backend_stack_align
#define elf_backend_stack_align 16
#endif
-#ifndef elf_backend_strtab_flags
-#define elf_backend_strtab_flags 0
-#endif
#ifndef elf_backend_use_mmap
#define elf_backend_use_mmap false
#endif
@@ -942,7 +939,6 @@ static const struct elf_backend_data elfNN_bed =
elf_backend_write_secondary_reloc_section,
elf_backend_static_tls_alignment,
elf_backend_stack_align,
- elf_backend_strtab_flags,
elf_backend_collect,
elf_backend_type_change_ok,
elf_backend_may_use_rel_p,
--
Alan Modra
More information about the Binutils
mailing list