Commit: Better error messages for bad targets on objcopy's command line

Message ID 871pog12jb.fsf@redhat.com
State New
Headers
Series Commit: Better error messages for bad targets on objcopy's command line |

Commit Message

Nick Clifton Sept. 9, 2025, 7:57 a.m. UTC
  Hi Guys,

  Currently if a bad or unknown bfd target string is provided as a
  target on objcopy's command line, the error message shown will
  reference the file being copied rather than the target string:

     % objcopy --target=BAD ver.o ver.copy
     objcopy: ver.o: invalid bfd target

     % objcopy --input-target=BAD ver.o ver.copy
     objcopy: ver.o: invalid bfd target
     
     % objcopy --output-target=BAD ver.o ver.copy
     objcopy: ver.copy: invalid bfd target

  This is confusing since there may not be anything wrong with the files
  themselves.  Plus it does not show the unrecognised target string.

  So I am applying the patch below so that these messages are changed
  to:

    % objcopy --target=BAD ver.o ver.copy
    objcopy: BAD: invalid bfd target

    % objcopy --input-target=BAD ver.o ver.copy
    objcopy: BAD: invalid bfd target

    % objcopy --output-target=BAD ver.o ver.copy
    objcopy: BAD: invalid bfd target
    
Cheers
  Nick
  

Patch

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index a3259f96d35..3c1bcf622ff 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3876,7 +3876,10 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
   ibfd = bfd_openr (input_filename, target);
   if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0)
     {
-      bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
+      if (bfd_get_error () == bfd_error_invalid_target && target != NULL)
+	bfd_nonfatal_message (target, NULL, NULL, NULL);
+      else
+	bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
       if (ibfd != NULL)
 	bfd_close (ibfd);
       status = 1;
@@ -3951,7 +3954,10 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
 	{
 	  if (ofd >= 0)
 	    close (ofd);
-	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
+	  if (force_output_target && bfd_get_error () == bfd_error_invalid_target)
+	    bfd_nonfatal_message (output_target, NULL, NULL, NULL);
+	  else
+	    bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
 	  bfd_close (ibfd);
 	  status = 1;
 	  return;
@@ -4036,7 +4042,10 @@  copy_file (const char *input_filename, const char *output_filename, int ofd,
  	{
 	  if (ofd >= 0)
 	    close (ofd);
- 	  bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
+	  if (bfd_get_error () == bfd_error_invalid_target)
+	    bfd_nonfatal_message (output_target, NULL, NULL, NULL);
+	  else
+	    bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
 	  bfd_close (ibfd);
  	  status = 1;
  	  return;