[PATCH] Limit BFD_SUPPORTS_PLUGINS check to plugin.h and targets.c

H.J. Lu hjl.tools@gmail.com
Mon Aug 18 23:29:42 GMT 2025


On Mon, Aug 18, 2025 at 3:25 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Mon, Aug 18, 2025 at 06:16:19AM -0700, H.J. Lu wrote:
> > Minimize the BFD_SUPPORTS_PLUGINS check to make code more readable and
> > maintainable by:
> >
> > 1. Update bfd/plugin.h to define plugin functions as static inline if
> > BFD_SUPPORTS_PLUGINS is 0.
> > 2. Add get_plugin_target to return "plugin" if plugin is enabled.
> > 3. Remove BFD_SUPPORTS_PLUGINS check from all bfd and binutils files
> > except plugin.h and targets.c.
> > 4. Replace the remaining BFD_SUPPORTS_PLUGINS checks with a function so
> > that plugin availability is checked at run time.
>
> You neglected to compile this on a target that disables plugins, like
> i386-msdos.
>
> OK with the following on top of your patch, and the plugin.c changelog
> entry corrected.  I put back the #if BFD_SUPPORTS_PLUGINS in plugin.c
> so that unused code is not compiled into libbfd with
> --enable-targets=all (and so that --disable-plugins would catch errors
> like the plugin_vec reference in format.c).

Done.  Here is the v2 patch I am checking in.

Thanks.

> diff --git a/bfd/format.c b/bfd/format.c
> index b2cf416e0e2..81f29c5f54f 100644
> --- a/bfd/format.c
> +++ b/bfd/format.c
> @@ -516,18 +516,17 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
>       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 (abfd->format == bfd_object
> +  if (bfd_plugin_enabled ()
> +      && 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_ASSERT (save_targ != bfd_plugin_vec ());
> +      abfd->xvec = bfd_plugin_vec ();
>        bfd_set_error (bfd_error_no_error);
>        cleanup = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
>        if (cleanup)
> diff --git a/bfd/plugin.c b/bfd/plugin.c
> index 393011432a8..5e5a0b70a04 100644
> --- a/bfd/plugin.c
> +++ b/bfd/plugin.c
> @@ -21,6 +21,8 @@
>  #include "sysdep.h"
>  #include "bfd.h"
>
> +#if BFD_SUPPORTS_PLUGINS
> +
>  #include <assert.h>
>  #ifdef HAVE_DLFCN_H
>  #include <dlfcn.h>
> @@ -656,16 +658,6 @@ bfd_link_plugin_object_p (bfd *abfd)
>    return false;
>  }
>
> -extern const bfd_target plugin_vec;
> -
> -/* Return TRUE if TARGET is a pointer to plugin_vec.  */
> -
> -bool
> -bfd_plugin_target_p (const bfd_target *target)
> -{
> -  return target == &plugin_vec;
> -}
> -
>  /* Register OBJECT_P to be used by bfd_plugin_object_p.  */
>
>  void
> @@ -1030,3 +1022,4 @@ const bfd_target plugin_vec =
>
>    NULL                         /* backend_data.  */
>  };
> +#endif /* BFD_SUPPORTS_PLUGINS */
> diff --git a/bfd/plugin.h b/bfd/plugin.h
> index d78186ba7f6..f16c45c8fa0 100644
> --- a/bfd/plugin.h
> +++ b/bfd/plugin.h
> @@ -27,10 +27,22 @@ struct ld_plugin_input_file;
>  void bfd_plugin_set_program_name (const char *);
>  int bfd_plugin_open_input (bfd *, struct ld_plugin_input_file *);
>  void bfd_plugin_set_plugin (const char *);
> -bool bfd_plugin_target_p (const bfd_target *);
>  bool bfd_link_plugin_object_p (bfd *);
>  void register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *, bool));
>  void bfd_plugin_close_file_descriptor (bfd *, int);
> +
> +static inline const bfd_target *
> +bfd_plugin_vec (void)
> +{
> +  extern const bfd_target plugin_vec;
> +  return &plugin_vec;
> +}
> +
> +static inline bool
> +bfd_plugin_target_p (const bfd_target *target)
> +{
> +  return target == bfd_plugin_vec ();
> +}
>  #else
>  static inline void
>  bfd_plugin_set_program_name (const char *name ATTRIBUTE_UNUSED)
> @@ -49,12 +61,6 @@ bfd_plugin_set_plugin (const char *p ATTRIBUTE_UNUSED)
>  {
>  }
>
> -static inline bool
> -bfd_plugin_target_p (const bfd_target *target ATTRIBUTE_UNUSED)
> -{
> -  return false;
> -}
> -
>  static inline bool
>  bfd_link_plugin_object_p (bfd *abfd ATTRIBUTE_UNUSED)
>  {
> @@ -72,6 +78,18 @@ bfd_plugin_close_file_descriptor (bfd *abfd ATTRIBUTE_UNUSED,
>                                   int fd ATTRIBUTE_UNUSED)
>  {
>  }
> +
> +static inline const bfd_target *
> +bfd_plugin_vec (void)
> +{
> +  return NULL;
> +}
> +
> +static inline bool
> +bfd_plugin_target_p (const bfd_target *target ATTRIBUTE_UNUSED)
> +{
> +  return false;
> +}
>  #endif
>
>  #endif
>
> --
> Alan Modra



-- 
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v2-0001-Limit-BFD_SUPPORTS_PLUGINS-check-to-plugin.h-and-.patch
Type: text/x-patch
Size: 38774 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20250818/72454a10/attachment-0001.bin>


More information about the Binutils mailing list