]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ELF OSABI matching
authorAlan Modra <amodra@gmail.com>
Sat, 3 Jan 2026 03:21:21 +0000 (13:51 +1030)
committerAlan Modra <amodra@gmail.com>
Sun, 4 Jan 2026 02:44:22 +0000 (13:14 +1030)
Currently a binutils ELF target with ELF_OSABI defined to something
other than the default ELFOSABI_NONE will only accept object files
with e_ident[EI_OSABI] having the value ELF_OSABI.  An ELF target with
ELF_OSABI as ELFOSABI_NONE will match any e_ident[EI_OSABI] value, and
typically that target's object files will have ELFOSABI_NONE or one
other value in e_ident[EI_OSABI].  Given an object file with that
"other value" we'd like to be able to choose the correct target but
currently have no way to do so.  This patch is a step towards
implementing better target matching.

It does so by adding an ELF_OSABI_EXACT to the target description,
setting that true for all targets that currently define ELF_OSABI, and
testing the new flag for exact matching.  This will allow a future
patch to define ELF_OSABI without forcing exact matching but giving a
hint as to the best target match.

Some other tweaks are done as shown by the changelog below but no
user functional changes should be seen with this patch.

* elf-bfd.h (struct elf_backend_data): Add osabi_exact.
* elf.c (_bfd_elf_final_write_processing): Only set e_ident
from elf_osabi when osabi_exact.
* elfcode.h (elf_object_p): Test osabi_exact to reject
mismatching e_ident.  Remove unnecessary EM_NONE test.
* elfcore.h (elf_core_file_p): Likewise.
* elfxx-target.h (ELF_OSABI_EXACT): Provide default of 0,
and init elfNN_bed.
(elf_match_priority): Handle ELF_OSABI_EXACT.
* elf32-arm.c (ELF_OSABI_EXACT): Define and undef along with
ELF_OSABI.
* elf32-hppa.c: Likewise.
* elf32-i386.c: Likewise.
* elf32-mips.c: Likewise.
* elf32-ppc.c: Likewise.
* elf32-tic6x.c: Likewise.
* elf32-visium.c: Likewise.
* elf64-alpha.c: Likewise.
* elf64-hppa.c: Likewise.
* elf64-ia64-vms.c: Likewise.
* elf64-mips.c: Likewise.
* elf64-ppc.c: Likewise.
* elf64-sparc.c: Likewise.
* elf64-x86-64.c: Likewise.
* elfn32-mips.c: Likewise.
* elfnn-ia64.c: Likewise.
* elf32-msp430.c: Likewise.  Don't define ELF_OSABI to
ELFOSABI_NONE.

22 files changed:
bfd/elf-bfd.h
bfd/elf.c
bfd/elf32-arm.c
bfd/elf32-hppa.c
bfd/elf32-i386.c
bfd/elf32-mips.c
bfd/elf32-msp430.c
bfd/elf32-ppc.c
bfd/elf32-tic6x.c
bfd/elf32-visium.c
bfd/elf64-alpha.c
bfd/elf64-hppa.c
bfd/elf64-ia64-vms.c
bfd/elf64-mips.c
bfd/elf64-ppc.c
bfd/elf64-sparc.c
bfd/elf64-x86-64.c
bfd/elfcode.h
bfd/elfcore.h
bfd/elfn32-mips.c
bfd/elfnn-ia64.c
bfd/elfxx-target.h

index efe40e10605893d126425ea4741c6d72a407480a..b2aeb2ba0cace42ada2cb57301b5a17abc116093 100644 (file)
@@ -1023,6 +1023,10 @@ struct elf_backend_data
   /* Target OS.  */
   ENUM_BITFIELD (elf_target_os) target_os : 2;
 
+  /* True if object files must have exactly matching osabi.  False if
+     other osabi values are allowed.  */
+  unsigned osabi_exact : 1;
+
   /* The maximum page size for this backend.  */
   unsigned maxpagesize;
 
index d2cf2bb4b9c23a8cbed3789ece89f0d55362f5d1..8b6dd3ce37024e4d41ae4639a6a9692d23f45289 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -13486,7 +13486,7 @@ _bfd_elf_final_write_processing (bfd *abfd)
   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   elf_backend_data *bed = get_elf_backend_data (abfd);
 
-  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
+  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
     i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
 
   if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
index 9465efc5953414ebb44e458d25151763d00c4199..e0573d53ef07b1f0178512ae9ae4fe0d33f999b4 100644 (file)
@@ -20277,6 +20277,8 @@ elf32_arm_backend_symbol_processing (bfd *abfd, asymbol *sym)
 #define elf_match_priority             128
 #undef ELF_OSABI
 #define ELF_OSABI              ELFOSABI_ARM_FDPIC
+#undef ELF_OSABI_EXACT
+#define ELF_OSABI_EXACT                1
 
 /* Like elf32_arm_link_hash_table_create -- but overrides
    appropriately for FDPIC.  */
@@ -20335,6 +20337,7 @@ elf32_arm_fdpic_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
 
 #undef elf_match_priority
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 #undef elf_backend_omit_section_dynsym
 
 /* VxWorks Targets.  */
index 260844985d3326045f896fd4e5cb21e29d6d47f9..e3aa67c3fc6e6ff3e61c97bda88eb2673ddaa38a 100644 (file)
@@ -4539,6 +4539,7 @@ elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
 #define ELF_MACHINE_CODE       EM_PARISC
 #define ELF_MAXPAGESIZE                0x1000
 #define ELF_OSABI              ELFOSABI_HPUX
+#define ELF_OSABI_EXACT                1
 #define elf32_bed              elf32_hppa_hpux_bed
 
 #include "elf32-target.h"
index a367fd9f6f0d0f5748e726a49fa9a153bb855a8c..be8c676c833145d8d2504be9063cbd700ec58668 100644 (file)
@@ -4617,6 +4617,8 @@ elf_i386_add_glibc_version_dependency
 #define        TARGET_LITTLE_NAME              "elf32-i386-freebsd"
 #undef ELF_OSABI
 #define        ELF_OSABI                       ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT                 1
 
 /* The kernel recognizes executables as valid only if they carry a
    "FreeBSD" label in the ELF header.  So we put this label on all
@@ -4665,6 +4667,7 @@ elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
    objects won't be recognized.  */
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef elf32_bed
 #define        elf32_bed                       elf32_i386_sol2_bed
@@ -4708,6 +4711,7 @@ elf32_iamcu_elf_object_p (bfd *abfd)
 
 #undef ELF_TARGET_OS
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef  elf32_bed
 #define elf32_bed                      elf32_iamcu_bed
@@ -4735,7 +4739,6 @@ elf32_iamcu_elf_object_p (bfd *abfd)
 #define TARGET_LITTLE_SYM              i386_elf32_vxworks_vec
 #undef TARGET_LITTLE_NAME
 #define TARGET_LITTLE_NAME             "elf32-i386-vxworks"
-#undef ELF_OSABI
 #undef ELF_MAXPAGESIZE
 #define ELF_MAXPAGESIZE                        0x1000
 #undef elf_backend_plt_alignment
@@ -4743,6 +4746,8 @@ elf32_iamcu_elf_object_p (bfd *abfd)
 
 #undef ELF_TARGET_OS
 #define ELF_TARGET_OS          is_vxworks
+#undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef elf_backend_relocs_compatible
 #undef elf_backend_add_symbol_hook
index 1df60bda04b8bb6103e8c42b74feda348ae4bab0..12a95b08ff8e1aef622de83f94aa8937f4890267 100644 (file)
@@ -4165,6 +4165,8 @@ static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
 
 #undef ELF_OSABI
 #define        ELF_OSABI                       ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT                 1
 
 #undef elf32_bed
 #define elf32_bed                              elf32_fbsd_tradbed
@@ -4194,6 +4196,7 @@ mips_vxworks_final_write_processing (bfd *abfd)
 #define TARGET_BIG_SYM                 mips_elf32_vxworks_be_vec
 #define TARGET_BIG_NAME                        "elf32-bigmips-vxworks"
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef elf32_bed
 #define elf32_bed                      elf32_mips_vxworks_bed
index cb83f5692be56a5aa11c640ddb6a9128a43a72b4..d7d7730063fd6b2c7d201c43d16c0719a3e03627 100644 (file)
@@ -2953,6 +2953,7 @@ elf32_msp430_eh_frame_address_size (bfd *abfd,
 #define ELF_MACHINE_ALT1       EM_MSP430_OLD
 #define ELF_MAXPAGESIZE                4
 #define        ELF_OSABI               ELFOSABI_STANDALONE
+#define ELF_OSABI_EXACT                1
 
 #define TARGET_LITTLE_SYM      msp430_elf32_vec
 #define TARGET_LITTLE_NAME     "elf32-msp430"
@@ -2980,7 +2981,7 @@ elf32_msp430_eh_frame_address_size (bfd *abfd,
 #define elf32_bed              elf32_msp430_ti_bed
 
 #undef ELF_OSABI
-#define        ELF_OSABI               ELFOSABI_NONE
+#undef ELF_OSABI_EXACT
 
 static const struct bfd_elf_special_section msp430_ti_elf_special_sections[] =
 {
index ecbdb91c6679a2e738147c48749d9c1f12549c17..ba9e59ab3b91d80dc11738230ee96b43fd937ab0 100644 (file)
@@ -10433,6 +10433,8 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 
 #undef  ELF_OSABI
 #define ELF_OSABI      ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT 1
 
 #undef  elf32_bed
 #define elf32_bed      elf32_powerpc_fbsd_bed
@@ -10450,6 +10452,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 #define TARGET_BIG_NAME                "elf32-powerpc-vxworks"
 
 #undef  ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef ELF_TARGET_OS
 #define ELF_TARGET_OS          is_vxworks
index c776f31092e1c6e02c6becf38e097d3ab48c9e28..d5f1259d1f4804bbc8a39a320894165be47b4427 100644 (file)
@@ -4285,6 +4285,8 @@ elf32_tic6x_write_section (bfd *output_bfd,
 #define        TARGET_BIG_NAME                 "elf32-tic6x-linux-be"
 #undef ELF_OSABI
 #define        ELF_OSABI                       ELFOSABI_C6000_LINUX
+#undef ELF_OSABI_EXACT
+#define ELF_OSABI_EXACT                        1
 
 #include "elf32-target.h"
 
@@ -4301,5 +4303,7 @@ elf32_tic6x_write_section (bfd *output_bfd,
 #define        TARGET_BIG_NAME                 "elf32-tic6x-elf-be"
 #undef ELF_OSABI
 #define        ELF_OSABI                       ELFOSABI_C6000_ELFABI
+#undef ELF_OSABI_EXACT
+#define ELF_OSABI_EXACT                        1
 
 #include "elf32-target.h"
index 1771c444c22553fabf633ef82327c90e1cc1174e..7f28c81fda34dafdebd334b279e7634af25974ad 100644 (file)
@@ -852,6 +852,7 @@ visium_elf_print_private_bfd_data (bfd *abfd, void *ptr)
 #define ELF_ARCH               bfd_arch_visium
 #define ELF_MACHINE_CODE       EM_VISIUM
 #define ELF_OSABI              ELFOSABI_STANDALONE
+#define        ELF_OSABI_EXACT         1
 #define ELF_MAXPAGESIZE                1
 
 #define TARGET_BIG_SYM         visium_elf32_vec
index 71b2cb4e57dd0ee57b77d634001b65313d5ddcf1..199505d17a5f8c5b40331c214e8431624a2f9697 100644 (file)
@@ -5499,6 +5499,8 @@ static const struct elf_size_info alpha_elf_size_info =
 #define TARGET_LITTLE_NAME     "elf64-alpha-freebsd"
 #undef ELF_OSABI
 #define        ELF_OSABI               ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT         1
 
 /* The kernel recognizes executables as valid only if they carry a
    "FreeBSD" label in the ELF header.  So we put this label on all
index d0bc3c33d68ab5e703ed3b2c6143f0d90f416970..2df62923ee5ad433dc0a12c39ab6cccf5cf65e70 100644 (file)
@@ -4166,6 +4166,7 @@ static const struct elf_size_info hppa64_elf_size_info =
    64M.  But everything still uses 4k.  */
 #define ELF_MAXPAGESIZE                        0x1000
 #define ELF_OSABI                      ELFOSABI_HPUX
+#define ELF_OSABI_EXACT                        1
 
 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
index bdd1b5b97533f3d6331b63277e2ba58c6edca97a..4403688ec9681fe250c2aecc919c461bd1496d02 100644 (file)
@@ -5568,6 +5568,8 @@ static const struct elf_size_info elf64_ia64_vms_size_info = {
 
 #undef  ELF_OSABI
 #define ELF_OSABI                      ELFOSABI_OPENVMS
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT                 1
 
 #undef  ELF_MAXPAGESIZE
 #define ELF_MAXPAGESIZE                        0x10000 /* 64KB */
index 26e3c602bba401b21d532d1bca47a900317b3468..8824214f18a45e5dc8643f956ed7bebce61c5a83 100644 (file)
@@ -4868,6 +4868,8 @@ static const struct elf_size_info mips_elf64_size_info =
 
 #undef ELF_OSABI
 #define        ELF_OSABI                       ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT                 1
 
 #undef elf64_bed
 #define elf64_bed                              elf64_fbsd_tradbed
index be41efe10bd5c0e452d4b0388ef9590356f4ea38..7e41085ed9150092ddbb1334fabb50e6bee0d43f 100644 (file)
@@ -18451,6 +18451,8 @@ ppc64_elf_free_cached_info (bfd *abfd)
 
 #undef  ELF_OSABI
 #define        ELF_OSABI       ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT 1
 
 #undef  elf64_bed
 #define elf64_bed      elf64_powerpc_fbsd_bed
index 06c4e492ff85a58fa5e026f7140945cd34ee7258..cf0826f52b8abe3bbc4e30e2d00a44c2c8325532 100644 (file)
@@ -994,6 +994,8 @@ static const struct elf_size_info elf64_sparc_size_info =
 #define TARGET_BIG_NAME "elf64-sparc-freebsd"
 #undef ELF_OSABI
 #define        ELF_OSABI ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT 1
 
 #undef  elf64_bed
 #define elf64_bed                              elf64_sparc_fbsd_bed
@@ -1013,6 +1015,7 @@ static const struct elf_size_info elf64_sparc_size_info =
 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
    objects won't be recognized.  */
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef  elf64_bed
 #define elf64_bed                              elf64_sparc_sol2_bed
index 72a828a8c13f6673ccb6dfbfc0c6ce9b8ee0a70e..47d24e0e77625576f44f1dafce283fceda5413c4 100644 (file)
@@ -6426,6 +6426,8 @@ elf_x86_64_special_sections[]=
 
 #undef ELF_OSABI
 #define        ELF_OSABI                           ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT                     1
 
 #undef elf64_bed
 #define elf64_bed elf64_x86_64_fbsd_bed
@@ -6448,6 +6450,7 @@ elf_x86_64_special_sections[]=
 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
    objects won't be recognized.  */
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #undef  elf64_bed
 #define elf64_bed                          elf64_x86_64_sol2_bed
@@ -6468,6 +6471,7 @@ elf_x86_64_special_sections[]=
 
 /* Restore defaults.  */
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 #undef elf_backend_static_tls_alignment
 #undef elf_backend_want_plt_sym
 #define elf_backend_want_plt_sym       0
@@ -6489,6 +6493,7 @@ elf_x86_64_special_sections[]=
 
 #undef ELF_TARGET_OS
 #undef ELF_OSABI
+#undef ELF_OSABI_EXACT
 
 #define bfd_elf32_bfd_copy_private_section_data \
   elf_x86_64_copy_private_section_data
index 3fde79c9c53cfd60731840f73f701a8f43e356c3..d7c82f0797cd5fd4ff2ee5bea0590fc6f7d50709 100644 (file)
@@ -628,9 +628,7 @@ elf_object_p (bfd *abfd)
        goto got_no_match;
     }
 
-  if (ebd->elf_machine_code != EM_NONE
-      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
-      && ebd->elf_osabi != ELFOSABI_NONE)
+  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
     goto got_wrong_format_error;
 
   if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
index c570479c2ac9e228c009cbee8f0600ddaa6c6123..7932faff2877d028ac0b25f159b88433a2192dbc 100644 (file)
@@ -152,9 +152,7 @@ elf_core_file_p (bfd *abfd)
       && ebd->elf_machine_code != EM_NONE)
     goto wrong;
 
-  if (ebd->elf_machine_code != EM_NONE
-      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
-      && ebd->elf_osabi != ELFOSABI_NONE)
+  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
     goto wrong;
 
   /* If there is no program header, or the type is not a core file, then
index 97c859f7d2563e3f21906aeabcafa393f7d0367d..a23333765a6a9c84fa0f3f772b06b14fa145fdd6 100644 (file)
@@ -4254,6 +4254,8 @@ static const struct ecoff_debug_swap mips_elf32_ecoff_debug_swap = {
 
 #undef ELF_OSABI
 #define        ELF_OSABI                       ELFOSABI_FREEBSD
+#undef ELF_OSABI_EXACT
+#define        ELF_OSABI_EXACT                 1
 
 #undef elf32_bed
 #define elf32_bed                              elf32_fbsd_tradbed
index fd1ac180b0693e9efe99e878e4a6dbe35e7536d8..03a31f41beb231d9f765b820a5c7dab35ba6717c 100644 (file)
@@ -5108,6 +5108,8 @@ ignore_errors (const char *fmt ATTRIBUTE_UNUSED, ...)
 #undef ELF_COMMONPAGESIZE
 #undef ELF_OSABI
 #define ELF_OSABI                      ELFOSABI_HPUX
+#undef ELF_OSABI_EXACT
+#define ELF_OSABI_EXACT                        1
 
 #undef  elfNN_bed
 #define elfNN_bed elfNN_ia64_hpux_bed
index 88f8a041ea85b2d61ca6199f5c9ddf3cc819d70a..48fb3082e8e6c3a8d7ee68f975793eced92cdfa0 100644 (file)
 #define ELF_OSABI ELFOSABI_NONE
 #endif
 
+#ifndef ELF_OSABI_EXACT
+#define ELF_OSABI_EXACT 0
+#endif
+
 #ifndef ELF_MAXPAGESIZE
 # error ELF_MAXPAGESIZE is not defined
 #define ELF_MAXPAGESIZE 1
  
 #ifndef elf_match_priority
 #define elf_match_priority \
-  (ELF_ARCH == bfd_arch_unknown ? 2 : ELF_OSABI == ELFOSABI_NONE ? 1 : 0)
+  (ELF_ARCH == bfd_arch_unknown ? 2 \
+   : ELF_OSABI == ELFOSABI_NONE || !ELF_OSABI_EXACT ? 1 \
+   : 0)
 #endif
 
 extern const struct elf_size_info _bfd_elfNN_size_info ATTRIBUTE_HIDDEN;
@@ -811,6 +817,7 @@ static const struct elf_backend_data elfNN_bed =
   ELF_MACHINE_CODE,
   ELF_TARGET_ID,
   ELF_TARGET_OS,
+  ELF_OSABI_EXACT,
   ELF_MAXPAGESIZE,
   ELF_MINPAGESIZE,
   ELF_COMMONPAGESIZE,