Delete PROGRESS macros
[binutils-gdb.git] / binutils / objcopy.c
index 6814e20a2fca86a8a393538cd1629da4c692122e..cf830442b3c1b6a0c2c43fcb16c08bd33170bfd5 100644 (file)
@@ -1,5 +1,5 @@
 /* objcopy.c -- copy object file from input to output, optionally massaging it.
-   Copyright (C) 1991-2022 Free Software Foundation, Inc.
+   Copyright (C) 1991-2023 Free Software Foundation, Inc.
 
    This file is part of GNU Binutils.
 
@@ -20,7 +20,6 @@
 \f
 #include "sysdep.h"
 #include "bfd.h"
-#include "progress.h"
 #include "getopt.h"
 #include "libiberty.h"
 #include "bucomm.h"
@@ -2035,19 +2034,19 @@ contained_by (objcopy_internal_note * needle,
   return needle->start >= haystack->start && needle->end <= haystack->end;
 }
 
-static bool
+static inline bool
 is_open_note (objcopy_internal_note * pnote)
 {
   return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
 }
 
-static bool
+static inline bool
 is_func_note (objcopy_internal_note * pnote)
 {
   return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_FUNC;
 }
 
-static bool
+static inline bool
 is_deleted_note (objcopy_internal_note * pnote)
 {
   return pnote->note.type == 0;
@@ -2403,6 +2402,8 @@ merge_gnu_build_notes (bfd *          abfd,
            other note then if they are both of the same type (open
            or func) then they can be merged and one deleted.  If
            they are of different types then they cannot be merged.  */
+  objcopy_internal_note * prev_note = NULL;
+
   for (pnote = pnotes; pnote < pnotes_end; pnote ++)
     {
       /* Skip already deleted notes.
@@ -2424,7 +2425,9 @@ merge_gnu_build_notes (bfd *          abfd,
       objcopy_internal_note * back;
 
       /* Rule 2: Check to see if there is an identical previous note.  */
-      for (iter = 0, back = pnote - 1; back >= pnotes; back --)
+      for (iter = 0, back = prev_note ? prev_note : pnote - 1;
+          back >= pnotes;
+          back --)
        {
          if (is_deleted_note (back))
            continue;
@@ -2486,11 +2489,17 @@ merge_gnu_build_notes (bfd *          abfd,
              break;
            }
        }
-#if DEBUG_MERGE
+
       if (! is_deleted_note (pnote))
-       merge_debug ("Unable to do anything with note at %#08lx\n",
-                    (pnote->note.namedata - (char *) contents) - 12);
+       {
+         /* Keep a pointer to this note, so that we can
+            start the next search for rule 2 matches here.  */
+         prev_note = pnote;
+#if DEBUG_MERGE
+         merge_debug ("Unable to do anything with note at %#08lx\n",
+                      (pnote->note.namedata - (char *) contents) - 12);
 #endif
+       }
     }
 
   /* Resort the notes.  */
@@ -2610,6 +2619,19 @@ check_new_section_flags (flagword flags, bfd * abfd, const char * secname)
   return flags;
 }
 
+static void
+set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
+{
+  /* This is only relevant to Coff targets.  */
+  if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
+    {
+      if (style == KEEP
+         && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
+       style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
+      bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
+    }
+}
+
 /* Copy object file IBFD onto OBFD.
    Returns TRUE upon success, FALSE otherwise.  */
 
@@ -2660,6 +2682,9 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
       return false;
     }
 
+  /* This is a no-op on non-Coff targets.  */
+  set_long_section_mode (obfd, ibfd, long_section_names);
+
   /* Set the Verilog output endianness based upon the input file's
      endianness.  We may not be producing verilog format output,
      but testing this just adds extra code this is not really
@@ -3328,9 +3353,13 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
 
   /* This has to happen before section positions are set.  */
   bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
+  if (status != 0)
+    return false;
 
   /* This has to happen after the symbol table has been set.  */
   bfd_map_over_sections (ibfd, copy_section, obfd);
+  if (status != 0)
+    return false;
 
   if (add_sections != NULL)
     {
@@ -3543,7 +3572,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
   bfd **ptr = &obfd->archive_head;
   bfd *this_element;
   char *dir;
-  const char *filename;
+  char *filename;
 
   /* PR 24281: It is not clear what should happen when copying a thin archive.
      One part is straight forward - if the output archive is in a different
@@ -3686,7 +3715,8 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
       if (!ok_object)
        del = !copy_unknown_object (this_element, output_bfd);
 
-      if (!(ok_object && !del ? bfd_close : bfd_close_all_done) (output_bfd))
+      if (!(ok_object && !del && !status
+           ? bfd_close : bfd_close_all_done) (output_bfd))
        {
          bfd_nonfatal_message (output_name, NULL, NULL, NULL);
          /* Error in new object file. Don't change archive.  */
@@ -3720,19 +3750,21 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
     }
   *ptr = NULL;
 
-  filename = bfd_get_filename (obfd);
+  filename = xstrdup (bfd_get_filename (obfd));
   if (!(status == 0 ? bfd_close : bfd_close_all_done) (obfd))
     {
       status = 1;
       bfd_nonfatal_message (filename, NULL, NULL, NULL);
     }
+  free (filename);
 
-  filename = bfd_get_filename (ibfd);
+  filename = xstrdup (bfd_get_filename (ibfd));
   if (!bfd_close (ibfd))
     {
       status = 1;
       bfd_nonfatal_message (filename, NULL, NULL, NULL);
     }
+  free (filename);
 
  cleanup_and_exit:
   /* Delete all the files that we opened.  */
@@ -3758,19 +3790,6 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
   free (dir);
 }
 
-static void
-set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
-{
-  /* This is only relevant to Coff targets.  */
-  if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
-    {
-      if (style == KEEP
-         && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
-       style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
-      bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
-    }
-}
-
 /* The top-level control.  */
 
 static void
@@ -3804,15 +3823,17 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
 
   switch (do_debug_sections)
     {
+    case compress_gnu_zlib:
+      ibfd->flags |= BFD_COMPRESS;
+      break;
     case compress:
     case compress_zlib:
-    case compress_gnu_zlib:
+      /* The above two cases ought to just set BFD_COMPRESS for non-ELF
+        but we can't tell whether a file is ELF or not until after
+        bfd_check_format_matches.  FIXME maybe: decide compression
+        style in BFD after bfd_check_format_matches.  */
     case compress_gabi_zlib:
-      ibfd->flags |= BFD_COMPRESS;
-      /* Don't check if input is ELF here since this information is
-        only available after bfd_check_format_matches is called.  */
-      if (do_debug_sections != compress_gnu_zlib)
-       ibfd->flags |= BFD_COMPRESS_GABI;
+      ibfd->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI;
       break;
     case compress_zstd:
       ibfd->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI | BFD_COMPRESS_ZSTD;
@@ -3876,9 +3897,6 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
          gnu_debuglink_filename = NULL;
        }
 
-      /* This is a no-op on non-Coff targets.  */
-      set_long_section_mode (obfd, ibfd, long_section_names);
-
       copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
     }
   else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
@@ -3904,9 +3922,6 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
          return;
        }
 
-      /* This is a no-op on non-Coff targets.  */
-      set_long_section_mode (obfd, ibfd, long_section_names);
-
       if (! copy_object (ibfd, obfd, input_arch))
        status = 1;
 
@@ -3918,14 +3933,12 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
        {
          status = 1;
          bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
-         return;
        }
 
       if (!bfd_close (ibfd))
        {
          status = 1;
          bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
-         return;
        }
     }
   else
@@ -4118,6 +4131,13 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
       flags &= ~clr;
     }
 
+  if (!bfd_convert_section_setup (ibfd, isection, obfd, &name, &size))
+    {
+      osection = NULL;
+      err = _("failed to create output section");
+      goto loser;
+    }
+
   osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
 
   if (osection == NULL)
@@ -4126,8 +4146,6 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
       goto loser;
     }
 
-  size = bfd_section_size (isection);
-  size = bfd_convert_section_size (ibfd, isection, obfd, size);
   if (copy_byte >= 0)
     size = (size + interleave - 1) / interleave * copy_width;
   else if (extract_symbol)
@@ -6034,8 +6052,6 @@ main (int argc, char *argv[])
   program_name = argv[0];
   xmalloc_set_program_name (program_name);
 
-  START_PROGRESS (program_name, 0);
-
   expandargv (&argc, &argv);
 
   strip_symbols = STRIP_UNDEF;
@@ -6070,7 +6086,6 @@ main (int argc, char *argv[])
   else
     copy_main (argc, argv);
 
-  END_PROGRESS (program_name);
-
+  xexit (status);
   return status;
 }