#ifdef ENABLE_PLUGINS
 #include "plugin.h"
 #include "plugin-api.h"
-#include "libbfd.h"
 #endif /* ENABLE_PLUGINS */
 
 /* Somewhere above, sys/stat.h got included.  */
   if (link_info.lto_plugin_active && !no_more_claiming)
     {
       /* We must offer this archive member to the plugins to claim.  */
-      const char *filename = (bfd_my_archive (abfd) != NULL
-                             ? bfd_my_archive (abfd)->filename : abfd->filename);
-      int fd = open (filename, O_RDONLY | O_BINARY);
-      if (fd >= 0)
+      plugin_maybe_claim (input);
+      if (input->flags.claimed)
        {
-         struct ld_plugin_input_file file;
-
-         /* Offset and filesize must refer to the individual archive
-            member, not the whole file, and must exclude the header.
-            Fortunately for us, that is how the data is stored in the
-            origin field of the bfd and in the arelt_data.  */
-         file.name = filename;
-         file.offset = abfd->origin;
-         file.filesize = arelt_size (abfd);
-         file.fd = fd;
-         plugin_maybe_claim (&file, input);
-         if (input->flags.claimed)
-           {
-             input->flags.claim_archive = TRUE;
-             *subsbfd = input->the_bfd;
-           }
+         input->flags.claim_archive = TRUE;
+         *subsbfd = input->the_bfd;
        }
     }
 #endif /* ENABLE_PLUGINS */
 
 #include "sysdep.h"
 #include "libiberty.h"
 #include "bfd.h"
+#include "libbfd.h"
 #include "bfdlink.h"
 #include "bfdver.h"
 #include "ld.h"
   return plugin_error_p () ? -1 : 0;
 }
 
+/* Duplicates a character string with memory attached to ABFD.  */
+
+static char *
+plugin_strdup (bfd *abfd, const char *str)
+{
+  size_t strlength;
+  char *copy;
+  strlength = strlen (str) + 1;
+  copy = bfd_alloc (abfd, strlength);
+  if (copy == NULL)
+    einfo (_("%P%F: plugin_strdup failed to allocate memory: %s\n"),
+          bfd_get_error ());
+  memcpy (copy, str, strlength);
+  return copy;
+}
+
 void
-plugin_maybe_claim (struct ld_plugin_input_file *file,
-                   lang_input_statement_type *entry)
+plugin_maybe_claim (lang_input_statement_type *entry)
 {
   int claimed = 0;
   plugin_input_file_t *input;
-  size_t namelength;
+  off_t offset, filesize;
+  struct ld_plugin_input_file file;
+  bfd *abfd;
+  bfd *ibfd = entry->the_bfd;
+  bfd_boolean inarchive = bfd_my_archive (ibfd) != NULL;
+  const char *name
+    = inarchive ? bfd_my_archive (ibfd)->filename : ibfd->filename;
+  int fd = open (name, O_RDONLY | O_BINARY);
+
+  if (fd < 0)
+    return;
 
   /* We create a dummy BFD, initially empty, to house whatever symbols
      the plugin may want to add.  */
-  bfd *abfd = plugin_get_ir_dummy_bfd (entry->the_bfd->filename,
-                                      entry->the_bfd);
+  abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
 
   input = bfd_alloc (abfd, sizeof (*input));
   if (input == NULL)
     einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
           bfd_get_error ());
 
+  if (inarchive)
+    {
+      /* Offset and filesize must refer to the individual archive
+        member, not the whole file, and must exclude the header.
+        Fortunately for us, that is how the data is stored in the
+        origin field of the bfd and in the arelt_data.  */
+      offset = ibfd->origin;
+      filesize = arelt_size (ibfd);
+    }
+  else
+    {
+      offset = 0;
+      filesize = lseek (fd, 0, SEEK_END);
+
+      /* We must copy filename attached to ibfd if it is not an archive
+        member since it may be freed by bfd_close below.  */
+      name = plugin_strdup (abfd, name);
+    }
+
+  file.name = name;
+  file.offset = offset;
+  file.filesize = filesize;
+  file.fd = fd;
+  file.handle = input;
+
   input->abfd = abfd;
   input->view_buffer.addr = NULL;
   input->view_buffer.filesize = 0;
   input->view_buffer.offset = 0;
-  input->fd = file->fd;
-  input->offset  = file->offset;
-  input->filesize = file->filesize;
-  namelength = strlen (entry->the_bfd->filename) + 1;
-  input->name = bfd_alloc (abfd, namelength);
-  if (input->name == NULL)
-    einfo (_("%P%F: plugin failed to allocate memory for input filename: %s\n"),
-          bfd_get_error ());
-  memcpy (input->name, entry->the_bfd->filename, namelength);
-
-  file->handle = input;
+  input->fd = fd;
+  input->offset = offset;
+  input->filesize = filesize;
+  input->name = plugin_strdup (abfd, ibfd->filename);
 
-  if (plugin_call_claim_file (file, &claimed))
+  if (plugin_call_claim_file (&file, &claimed))
     einfo (_("%P%F: %s: plugin reported error claiming file\n"),
           plugin_error_plugin ());
 
         calls release_input_file after it is done, is stored in
         non-bfd_object file.  This scheme doesn't work when a plugin
         needs fd and its IR is stored in bfd_object file.  */
-      close (file->fd);
+      close (fd);
       input->fd = -1;
     }
 
 
       /* BFD archive handling caches elements so we can't call
         bfd_close for archives.  */
-      if (entry->the_bfd->my_archive == NULL)
-       bfd_close (entry->the_bfd);
+      if (!inarchive)
+       bfd_close (ibfd);
+      bfd_make_readable (abfd);
       entry->the_bfd = abfd;
       entry->flags.claimed = TRUE;
-      bfd_make_readable (entry->the_bfd);
     }
   else
     {