]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Don't choose plugin target in binutils/
authorAlan Modra <amodra@gmail.com>
Mon, 18 Aug 2025 09:03:54 +0000 (18:33 +0930)
committerAlan Modra <amodra@gmail.com>
Mon, 18 Aug 2025 13:35:43 +0000 (23:05 +0930)
Instead make bfd_check_format try the plugin target first when the
user hasn't supplied a target.

bfd/
* format.c (bfd_check_format_matches): Try for a plugin target
match first.
* targets.c (bfd_find_target): Don't specially treat "plugin".
binutils/
* ar.c (plugin_target): Delete.
(open_inarch): Don't set target of archive elements.
(replace_members): Use target rather than plugin_target when
opening replacement or additional files.
* arsup.c (plugin_target): Delete.  Replace all uses with NULL.
(ar_open): Don't set element target.
* bucomm.h (set_plugin_target): Delete.
* nm.c (plugin_target): Delete.
(display_archive): Don't set element target.
(display_file): Alway use target when opening file.
* objcopy.c (copy_archive): Don't use plugin target for output
elements.
* NEWS: Mention stricter target checking.

bfd/format.c
bfd/targets.c
binutils/NEWS
binutils/ar.c
binutils/arsup.c
binutils/bucomm.h
binutils/nm.c
binutils/objcopy.c

index 4dbe863e7427f872b619ed425af5e5498ed53977..81e8f1c411f02db1ece2d44be8d151efa4b0b2bc 100644 (file)
@@ -513,17 +513,39 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
   if (!bfd_preserve_save (abfd, &preserve, NULL))
     goto err_ret;
 
-  /* First try matching the current target.  The current target may
-     have been set due to a user option, or due to the linker trying
-     optimistically to load input files for the same target as the
-     output, or due to the plugin support setting "plugin", or failing
-     any of those bfd_find_target will have chosen a default target.
-     target_defaulted will be set in the last case, or when "plugin"
-     is the target (even if chosen by user option).  Note that
-     bfd_plugin_no excluding the plugin target condition is an
-     optimisation, and can be removed if desired.  */
+  /* First try matching the plugin target if appropriate.  Next try
+     the current target.  The current target may have been set due to
+     a user option, or due to the linker trying optimistically to load
+     input files for the same target as the output.  Either will
+     have target_defaulted false.  Failing that, bfd_find_target will
+     have chosen a default target, and target_defaulted will be true.  */
   fail_targ = NULL;
 #if BFD_SUPPORTS_PLUGINS
+  if (abfd->format == bfd_object
+      && abfd->target_defaulted
+      && !abfd->is_linker_input
+      && abfd->plugin_format != bfd_plugin_no)
+    {
+      extern const bfd_target plugin_vec;
+
+      if (bfd_seek (abfd, 0, SEEK_SET) != 0)
+       goto err_ret;
+
+      BFD_ASSERT (save_targ != &plugin_vec);
+      abfd->xvec = &plugin_vec;
+      bfd_set_error (bfd_error_no_error);
+      cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
+      if (cleanup)
+       goto ok_ret;
+
+      bfd_reinit (abfd, initial_section_id, &preserve, cleanup);
+      bfd_release (abfd, preserve.marker);
+      preserve.marker = bfd_alloc (abfd, 1);
+      abfd->xvec = save_targ;
+    }
+
+  /* bfd_plugin_no excluding the plugin target is an optimisation.
+     The test can be removed if desired.  */
   if (!(abfd->plugin_format == bfd_plugin_no
        && bfd_plugin_target_p (save_targ)))
 #endif
index a7b0450a0052eca0920f292fe4e69532c5c1ab4e..c2ee9179f374cdc5686389c85c7cc2c18a0e260d 100644 (file)
@@ -1572,11 +1572,7 @@ bfd_find_target (const char *target_name, bfd *abfd)
     }
 
   if (abfd)
-    /* Treating "plugin" specially here is due to the fact that some
-       of the binutils magically supply a "plugin" target.  That
-       really is a defaulted target, but unfortunately we can't
-       distinguish it from a user supplied "plugin" target.  */
-    abfd->target_defaulted = strcmp (targname, "plugin") == 0;
+    abfd->target_defaulted = false;
 
   target = find_target (targname);
   if (target == NULL)
index 5038851b4553723fe2fc47394742afdfbfa72a4e..ccda855989788a754c550748f7af014990635258 100644 (file)
@@ -1,5 +1,12 @@
 -*- text -*-
 
+* Internal changes to plugin support, and stricter target checking may result
+  in some errors being exposed in user options passed to the various binutils.
+  For example objcopy --target=TARGET now will only work if the input file is
+  for TARGET whereas prior versions of objcopy accepted other target input
+  files and produced a TARGET output.  If you do in fact want the old
+  behaviour the correct usage is objcopy --output-target=TARGET.
+
 * NaCl target support is removed.
 
 Changes in 2.45:
index 79f40db991ce535b48e5d95d64a3121a0bc5f707..a159ac10ce5a0ec89580bcaf94321961ce2cb7cf 100644 (file)
@@ -144,12 +144,6 @@ static int show_version = 0;
 
 static int show_help = 0;
 
-#if BFD_SUPPORTS_PLUGINS
-static const char *plugin_target = "plugin";
-#else
-static const char *plugin_target = NULL;
-#endif
-
 static const char *target = NULL;
 
 enum long_option_numbers
@@ -884,7 +878,7 @@ main (int argc, char **argv)
          if (! bfd_make_readable (libdeps_bfd))
            fatal (_("Cannot make libdeps object readable."));
 
-         if (bfd_find_target (plugin_target, libdeps_bfd) == NULL)
+         if (bfd_find_target (target, libdeps_bfd) == NULL)
            fatal (_("Cannot reset libdeps record type."));
 
          /* Insert our libdeps record in 2nd slot of the list of files
@@ -974,7 +968,6 @@ open_inarch (const char *archive_filename, const char *file)
   bfd *arch;
   char **matching;
   const char *arch_target = target;
-  const struct bfd_target *plugin_vec;
 
   bfd_set_error (bfd_error_no_error);
 
@@ -1064,21 +1057,12 @@ open_inarch (const char *archive_filename, const char *file)
        }
     }
 
-  /* We didn't open the archive using plugin_target, because the
-     plugin bfd_target does not support archives.  Select
-     plugin_target now for elements so that we can recognise LTO IR
-     files and read IR symbols for use in the archive map.  */
-  plugin_vec = NULL;
-  if (!target && plugin_target)
-    plugin_vec = bfd_find_target (plugin_target, NULL);
-
   /* Open all the archive contents.  */
   last_one = &(arch->archive_next);
   for (next_one = bfd_openr_next_archived_file (arch, NULL);
        next_one;
        next_one = bfd_openr_next_archived_file (arch, next_one))
     {
-      set_plugin_target (next_one, plugin_vec);
       *last_one = next_one;
       last_one = &next_one->archive_next;
     }
@@ -1570,9 +1554,8 @@ replace_members (bfd *arch, char **files_to_move, bool quick)
                    }
                  else
                    {
-                     const char *targ = target ? target : plugin_target;
                      replaced = ar_emul_replace (after_bfd, *files_to_move,
-                                                 targ, verbose);
+                                                 target, verbose);
                    }
                  if (replaced)
                    {
@@ -1598,8 +1581,7 @@ replace_members (bfd *arch, char **files_to_move, bool quick)
        }
       else
         {
-         const char *targ = target ? target : plugin_target;
-         changed |= ar_emul_append (after_bfd, *files_to_move, targ,
+         changed |= ar_emul_append (after_bfd, *files_to_move, target,
                                     verbose, make_thin_archive);
        }
 
index e71c860e9e1d1158d17d21d5be2f3f22675ffb91..c7b65642607aaa9cbb116e743c0cc9803347fec7 100644 (file)
@@ -41,12 +41,6 @@ static char *temp_name;
 static int temp_fd;
 static FILE *outfile;
 
-#if BFD_SUPPORTS_PLUGINS
-static const char *plugin_target = "plugin";
-#else
-static const char *plugin_target = NULL;
-#endif
-
 static void
 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
 {
@@ -203,15 +197,8 @@ ar_open (char *name, int t)
          ptr = &(obfd->archive_head);
          element = bfd_openr_next_archived_file (ibfd, NULL);
 
-#if BFD_SUPPORTS_PLUGINS
-         const struct bfd_target *plugin_vec
-           = bfd_find_target (plugin_target, NULL);
-#endif
          while (element)
            {
-#if BFD_SUPPORTS_PLUGINS
-             set_plugin_target (element, plugin_vec);
-#endif
              *ptr = element;
              ptr = &element->archive_next;
              element = bfd_openr_next_archived_file (ibfd, element);
@@ -270,7 +257,7 @@ ar_addmod (struct list *list)
        {
          bfd *abfd;
 
-         abfd = bfd_openr (list->name, plugin_target);
+         abfd = bfd_openr (list->name, NULL);
          if (!abfd)
            {
              fprintf (stderr, _("%s: can't open file %s\n"),
@@ -397,7 +384,7 @@ ar_replace (struct list *list)
              if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
                {
                  /* Found the one to replace.  */
-                 bfd *abfd = bfd_openr (list->name, plugin_target);
+                 bfd *abfd = bfd_openr (list->name, NULL);
 
                  if (!abfd)
                    {
@@ -421,7 +408,7 @@ ar_replace (struct list *list)
 
          if (!found)
            {
-             bfd *abfd = bfd_openr (list->name, plugin_target);
+             bfd *abfd = bfd_openr (list->name, NULL);
 
              fprintf (stderr,_("%s: can't find module file %s\n"),
                       program_name, list->name);
index 54f54d74286837e37fce42a4d321864527f0385a..9815e71f2cba283a70e55be1a452b870aaef6cb2 100644 (file)
@@ -84,19 +84,4 @@ extern int smart_rename (const char *, const char *, int,
   ((*res) = (a), (*res) *= (b), (b) != 0 && (*res) / (b) != (a))
 #endif
 
-/* Change ABFD target vector to TARG.  ABFD is an archive element.
-   TARG is plugin_vec, or NULL if plugins are not supported.  */
-static inline void
-set_plugin_target (bfd *abfd, const struct bfd_target *targ)
-{
-  /* Don't change the target for archives like pdb that handle
-     elements specially, as detected by my_archive being NULL.  */
-  if (abfd->my_archive && targ)
-    {
-      abfd->xvec = targ;
-      /* Don't fail if the element isn't recognised by the plugin.  */
-      abfd->target_defaulted = true;
-    }
-}
-
 #endif /* _BUCOMM_H */
index 130b6e6015e2866211ad67e49a9f13bfba6d55d6..8098ed31e7326b4727ee3e76b02a12f5e66b9aca 100644 (file)
@@ -222,11 +222,6 @@ static char other_format[] = "%02x";
 static char desc_format[] = "%04x";
 
 static char *target = NULL;
-#if BFD_SUPPORTS_PLUGINS
-static const char *plugin_target = "plugin";
-#else
-static const char *plugin_target = NULL;
-#endif
 
 typedef enum unicode_display_type
 {
@@ -1601,14 +1596,6 @@ display_archive (bfd *file)
   if (print_armap)
     print_symdef_entry (file);
 
-  /* We didn't open the archive using plugin_target, because the
-     plugin bfd_target does not support archives.  Select
-     plugin_target now for elements so that we can recognise LTO IR
-     files and print IR symbols.  */
-  const struct bfd_target *plugin_vec = NULL;
-  if (!target && plugin_target)
-    plugin_vec = bfd_find_target (plugin_target, NULL);
-
   bfd *last_arfile = NULL;
   for (;;)
     {
@@ -1626,8 +1613,6 @@ display_archive (bfd *file)
       if (last_arfile != NULL)
        bfd_close (last_arfile);
 
-      set_plugin_target (arfile, plugin_vec);
-
       char **matching;
       if (bfd_check_format_matches (arfile, bfd_object, &matching))
        {
@@ -1660,7 +1645,7 @@ display_file (char *filename)
   if (get_file_size (filename) < 1)
     return false;
 
-  file = bfd_openr (filename, target ? target : plugin_target);
+  file = bfd_openr (filename, target);
   if (file == NULL)
     {
       bfd_nonfatal (filename);
index d88422ee74888d70ceae71c8cf5c9167a044e1e2..d8648ebbe91b7c9a1279979b7c399c77bb3257b3 100644 (file)
@@ -3795,14 +3795,8 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
          if (preserve_dates && stat_status == 0)
            set_times (output_name, &buf);
 
-         /* Open the newly created output file and attach to our
-            list.  We must enable the plugin target here in order to
-            read IR symbols for the archive map.  */
-         const char *targ = output_target;
-#if BFD_SUPPORTS_PLUGINS
-         if (!force_output_target)
-           targ = "plugin";
-#endif
+         /* Open the newly created output file and attach to our list.  */
+         const char *targ = force_output_target ? output_target : NULL;
          output_element = bfd_openr (output_name, targ);
 
          list->obfd = output_element;