objcopy "Unable to recognise the format of the input file"

Alan Modra amodra@gmail.com
Tue Aug 26 12:54:16 GMT 2025


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 --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"

-- 
Alan Modra


More information about the Binutils mailing list