[binutils-gdb] objcopy "Unable to recognise the format of the input file"

Alan Modra amodra@sourceware.org
Wed Aug 27 12:15:41 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=3fa891a80c5faba051afb0e6607f6b347bf61f9a

commit 3fa891a80c5faba051afb0e6607f6b347bf61f9a
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Aug 26 19:19:44 2025 +0930

    objcopy "Unable to recognise the format of the input file"
    
    This bogus error comes up when trying something like
    objcopy -O binary .../binutils/testsuite/binutils-all/tek2.obj xxx
    
    This is an annoying message, as HJ said in
    https://sourceware.org/pipermail/binutils/2002-August/021354.html
    and removed it for some cases, eg. I can make it go away by specifying
    -I tekhex.  The message is also untrue, as objcopy does in fact know
    the format of the input file.
    
    I think the message should be limited to ELF input files that are
    being handled by the elf64-little, elf64-big, elf32-little or
    elf32-big targets, due to libbfd being compiled with limited target
    support.  I'm also changing the message a litle.
    
            * objcopy.c (copy_object): Change "Unable to recognise format"
            message to "Unable to recognise architecture" and only report
            this error for ELF objects lacking their proper target support.
            * testsuite/binutils-all/x86-64/x86-64.exp: Update to suit.

Diff:
---
 binutils/objcopy.c                                | 21 ++++++++++++---------
 binutils/testsuite/binutils-all/x86-64/x86-64.exp |  2 +-
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index c6fc5082ba7..a3259f96d35 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -2804,17 +2804,20 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
       iarch = bed->arch;
       imach = 0;
     }
+  if (iarch == bfd_arch_unknown
+      && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
+      && ibfd->target_defaulted)
+    {
+      non_fatal (_("Unable to recognise the architecture of the input file `%s'"),
+		 bfd_get_archive_filename (ibfd));
+      return false;
+    }
   if (!bfd_set_arch_mach (obfd, iarch, imach)
-      && (ibfd->target_defaulted
-	  || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
+      && iarch != bfd_arch_unknown)
     {
-      if (bfd_get_arch (ibfd) == bfd_arch_unknown)
-	non_fatal (_("Unable to recognise the format of the input file `%s'"),
-		   bfd_get_archive_filename (ibfd));
-      else
-	non_fatal (_("Output file cannot represent architecture `%s'"),
-		   bfd_printable_arch_mach (bfd_get_arch (ibfd),
-					    bfd_get_mach (ibfd)));
+      non_fatal (_("Output file cannot represent architecture `%s'"),
+		 bfd_printable_arch_mach (bfd_get_arch (ibfd),
+					  bfd_get_mach (ibfd)));
       return false;
     }
 
diff --git a/binutils/testsuite/binutils-all/x86-64/x86-64.exp b/binutils/testsuite/binutils-all/x86-64/x86-64.exp
index 6d1b308eca6..3c98b03fb7c 100644
--- a/binutils/testsuite/binutils-all/x86-64/x86-64.exp
+++ b/binutils/testsuite/binutils-all/x86-64/x86-64.exp
@@ -295,7 +295,7 @@ proc run_pr33230_test { testname obj strip_flags run_readelf } {
 	    fail "$testname (${obj}.strip)"
 	    return
 	}
-    } elseif { ![regexp "Unable to recognise the format" $got] } then {
+    } elseif { ![regexp "Unable to recognise the architecture" $got] } then {
 	send_log "$got\n"
 	verbose "$got" 1
 	fail "$testname"


More information about the Binutils-cvs mailing list