(bfd_openw_with_cleanup): Use gdb_bfd_openw.
	* corelow.c (core_open): Use gdb_bfd_fopen.
	* dsrec.c (load_srec): Use gdb_bfd_openr.
	* exec.c (exec_file_attach): Use gdb_bfd_fopen.
	* gcore.c (gcore_memory_sections): Use gdb_bfd_openw.
	* gdb_bfd.c (gdb_bfd_fopen, gdb_bfd_openr, gdb_bfd_openw)
	(gdb_bfd_openr_iovec, gdb_bfd_openr_next_archived_file)
	(gdb_bfd_fdopenr): New functions.
	* gdb_bfd.h (gdb_bfd_fopen, gdb_bfd_openr, gdb_bfd_openw)
	(gdb_bfd_openr_iovec, gdb_bfd_openr_next_archived_file)
	(gdb_bfd_fdopenr): Declare.
	* jit.c (bfd_open_from_target_memory): Use gdb_bfd_openr_iovec.
	* m32-rom.c (m32r_load, m32r_upload_command): Use gdb_bfd_openr.
	* machoread.c (macho_symfile_read_all_oso): Use gdb_bfd_openr,
	gdb_bfd_openr_next_archived_file.
	(macho_check_dsym): Use gdb_bfd_openr.
	(macho_add_oso_symfile): Don't call gdb_bfd_stash_filename.
	* procfs.c (insert_dbx_link_bpt_in_file): Use gdb_bfd_fdopenr.
	* remote-m32r-sdi.c (m32r_load): Use gdb_bfd_openr.
	* remote-mips.c (mips_load_srec, pmon_load_fast): Use
	gdb_bfd_openr.
	* remote.c (remote_bfd_open): Use gdb_bfd_openr_iovec.
	* rs6000-nat.c (add_vmap): Use gdb_bfd_openr, gdb_bfd_fdopenr,
	gdb_bfd_openr_next_archived_file.
	* solib-darwin.c (darwin_solib_get_all_image_info_addr_at_init):
	Use gdb_bfd_openr.
	* solib-pa64.c (pa64_solib_create_inferior_hook): Use
	gdb_bfd_openr.
	* solib-spu.c (spu_bfd_fopen): Use gdb_bfd_openr_iovec.
	* solib.c (solib_bfd_fopen): Use gdb_bfd_fopen.
	* spu-linux-nat.c (spu_bfd_open): Use gdb_bfd_openr_iovec.
	* symfile.c (bfd_open_maybe_remote): Use gdb_bfd_openr.
	(symfile_bfd_open): Use gdb_bfd_fopen.
	(generic_load): Use gdb_bfd_openr.
	* windows-nat.c (windows_make_so): Use gdb_bfd_openr.
+2012-07-23  Tom Tromey  <tromey@redhat.com>
+
+       * cli/cli-dump.c (bfd_openr_with_cleanup): Use gdb_bfd_openr.
+       (bfd_openw_with_cleanup): Use gdb_bfd_openw.
+       * corelow.c (core_open): Use gdb_bfd_fopen.
+       * dsrec.c (load_srec): Use gdb_bfd_openr.
+       * exec.c (exec_file_attach): Use gdb_bfd_fopen.
+       * gcore.c (gcore_memory_sections): Use gdb_bfd_openw.
+       * gdb_bfd.c (gdb_bfd_fopen, gdb_bfd_openr, gdb_bfd_openw)
+       (gdb_bfd_openr_iovec, gdb_bfd_openr_next_archived_file)
+       (gdb_bfd_fdopenr): New functions.
+       * gdb_bfd.h (gdb_bfd_fopen, gdb_bfd_openr, gdb_bfd_openw)
+       (gdb_bfd_openr_iovec, gdb_bfd_openr_next_archived_file)
+       (gdb_bfd_fdopenr): Declare.
+       * jit.c (bfd_open_from_target_memory): Use gdb_bfd_openr_iovec.
+       * m32-rom.c (m32r_load, m32r_upload_command): Use gdb_bfd_openr.
+       * machoread.c (macho_symfile_read_all_oso): Use gdb_bfd_openr,
+       gdb_bfd_openr_next_archived_file.
+       (macho_check_dsym): Use gdb_bfd_openr.
+       (macho_add_oso_symfile): Don't call gdb_bfd_stash_filename.
+       * procfs.c (insert_dbx_link_bpt_in_file): Use gdb_bfd_fdopenr.
+       * remote-m32r-sdi.c (m32r_load): Use gdb_bfd_openr.
+       * remote-mips.c (mips_load_srec, pmon_load_fast): Use
+       gdb_bfd_openr.
+       * remote.c (remote_bfd_open): Use gdb_bfd_openr_iovec.
+       * rs6000-nat.c (add_vmap): Use gdb_bfd_openr, gdb_bfd_fdopenr,
+       gdb_bfd_openr_next_archived_file.
+       * solib-darwin.c (darwin_solib_get_all_image_info_addr_at_init):
+       Use gdb_bfd_openr.
+       * solib-pa64.c (pa64_solib_create_inferior_hook): Use
+       gdb_bfd_openr.
+       * solib-spu.c (spu_bfd_fopen): Use gdb_bfd_openr_iovec.
+       * solib.c (solib_bfd_fopen): Use gdb_bfd_fopen.
+       * spu-linux-nat.c (spu_bfd_open): Use gdb_bfd_openr_iovec.
+       * symfile.c (bfd_open_maybe_remote): Use gdb_bfd_openr.
+       (symfile_bfd_open): Use gdb_bfd_fopen.
+       (generic_load): Use gdb_bfd_openr.
+       * windows-nat.c (windows_make_so): Use gdb_bfd_openr.
+
 2012-07-23  Tom Tromey  <tromey@redhat.com>
 
        * bfd-target.c (target_bfd_reopen): Update.
 
 {
   bfd *ibfd;
 
-  ibfd = bfd_openr (filename, target);
-  gdb_bfd_ref (ibfd);
+  ibfd = gdb_bfd_openr (filename, target);
   if (ibfd == NULL)
     error (_("Failed to open %s: %s."), filename, 
           bfd_errmsg (bfd_get_error ()));
 
   if (*mode == 'w')    /* Write: create new file */
     {
-      obfd = bfd_openw (filename, target);
-      gdb_bfd_ref (obfd);
+      obfd = gdb_bfd_openw (filename, target);
       if (obfd == NULL)
        error (_("Failed to open %s: %s."), filename, 
               bfd_errmsg (bfd_get_error ()));
 
   if (scratch_chan < 0)
     perror_with_name (filename);
 
-  temp_bfd = bfd_fopen (filename, gnutarget, 
-                       write_files ? FOPEN_RUB : FOPEN_RB,
-                       scratch_chan);
-  gdb_bfd_ref (temp_bfd);
+  temp_bfd = gdb_bfd_fopen (filename, gnutarget, 
+                           write_files ? FOPEN_RUB : FOPEN_RB,
+                           scratch_chan);
   if (temp_bfd == NULL)
     perror_with_name (filename);
 
-  gdb_bfd_stash_filename (temp_bfd);
-
   if (!bfd_check_format (temp_bfd, bfd_core)
       && !gdb_check_format (temp_bfd))
     {
 
 
   srec = (char *) alloca (maxrecsize + 1);
 
-  abfd = bfd_openr (file, 0);
-  gdb_bfd_ref (abfd);
+  abfd = gdb_bfd_openr (file, 0);
   if (!abfd)
     {
       printf_filtered (_("Unable to open file %s\n"), file);
 
 
       if (scratch_chan < 0)
        perror_with_name (filename);
-      exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
-                           write_files ? FOPEN_RUB : FOPEN_RB,
-                           scratch_chan);
-      gdb_bfd_ref (exec_bfd);
+      exec_bfd = gdb_bfd_fopen (scratch_pathname, gnutarget,
+                               write_files ? FOPEN_RUB : FOPEN_RB,
+                               scratch_chan);
 
       if (!exec_bfd)
        {
                 gdb_bfd_errmsg (bfd_get_error (), matching));
        }
 
-      gdb_bfd_stash_filename (exec_bfd);
-
       /* FIXME - This should only be run for RS6000, but the ifdef is a poor
          way to accomplish.  */
 #ifdef DEPRECATED_IBM6000_TARGET
 
 bfd *
 create_gcore_bfd (char *filename)
 {
-  bfd *obfd = bfd_openw (filename, default_gcore_target ());
+  bfd *obfd = gdb_bfd_openw (filename, default_gcore_target ());
 
-  gdb_bfd_ref (obfd);
   if (!obfd)
     error (_("Failed to open '%s' for output."), filename);
-  gdb_bfd_stash_filename (obfd);
   bfd_set_format (obfd, bfd_core);
   bfd_set_arch_mach (obfd, default_gcore_arch (), default_gcore_mach ());
   return obfd;
 
   *size = descriptor->size;
   return descriptor->data;
 }
+
+\f
+
+/* See gdb_bfd.h.  */
+
+bfd *
+gdb_bfd_fopen (const char *filename, const char *target, const char *mode,
+              int fd)
+{
+  bfd *result = bfd_fopen (filename, target, mode, fd);
+
+  if (result)
+    {
+      gdb_bfd_stash_filename (result);
+      gdb_bfd_ref (result);
+    }
+
+  return result;
+}
+
+/* See gdb_bfd.h.  */
+
+bfd *
+gdb_bfd_openr (const char *filename, const char *target)
+{
+  bfd *result = bfd_openr (filename, target);
+
+  if (result)
+    {
+      gdb_bfd_stash_filename (result);
+      gdb_bfd_ref (result);
+    }
+
+  return result;
+}
+
+/* See gdb_bfd.h.  */
+
+bfd *
+gdb_bfd_openw (const char *filename, const char *target)
+{
+  bfd *result = bfd_openw (filename, target);
+
+  if (result)
+    {
+      gdb_bfd_stash_filename (result);
+      gdb_bfd_ref (result);
+    }
+
+  return result;
+}
+
+/* See gdb_bfd.h.  */
+
+bfd *
+gdb_bfd_openr_iovec (const char *filename, const char *target,
+                    void *(*open_func) (struct bfd *nbfd,
+                                        void *open_closure),
+                    void *open_closure,
+                    file_ptr (*pread_func) (struct bfd *nbfd,
+                                            void *stream,
+                                            void *buf,
+                                            file_ptr nbytes,
+                                            file_ptr offset),
+                    int (*close_func) (struct bfd *nbfd,
+                                       void *stream),
+                    int (*stat_func) (struct bfd *abfd,
+                                      void *stream,
+                                      struct stat *sb))
+{
+  bfd *result = bfd_openr_iovec (filename, target,
+                                open_func, open_closure,
+                                pread_func, close_func, stat_func);
+
+  if (result)
+    {
+      gdb_bfd_ref (result);
+      gdb_bfd_stash_filename (result);
+    }
+
+  return result;
+}
+
+/* See gdb_bfd.h.  */
+
+bfd *
+gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous)
+{
+  bfd *result = bfd_openr_next_archived_file (archive, previous);
+
+  if (result)
+    {
+      gdb_bfd_ref (result);
+      /* No need to stash the filename here.  */
+    }
+
+  return result;
+}
+
+/* See gdb_bfd.h.  */
+
+bfd *
+gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
+{
+  bfd *result = bfd_fdopenr (filename, target, fd);
+
+  if (result)
+    {
+      gdb_bfd_ref (result);
+      gdb_bfd_stash_filename (result);
+    }
+
+  return result;
+}
 
 
 const gdb_byte *gdb_bfd_map_section (asection *section, bfd_size_type *size);
 
+\f
+
+/* A wrapper for bfd_fopen that initializes the gdb-specific reference
+   count and calls gdb_bfd_stash_filename.  */
+
+bfd *gdb_bfd_fopen (const char *, const char *, const char *, int);
+
+/* A wrapper for bfd_openr that initializes the gdb-specific reference
+   count and calls gdb_bfd_stash_filename.  */
+
+bfd *gdb_bfd_openr (const char *, const char *);
+
+/* A wrapper for bfd_openw that initializes the gdb-specific reference
+   count and calls gdb_bfd_stash_filename.  */
+
+bfd *gdb_bfd_openw (const char *, const char *);
+
+/* A wrapper for bfd_openr_iovec that initializes the gdb-specific
+   reference count and calls gdb_bfd_stash_filename.  */
+
+bfd *gdb_bfd_openr_iovec (const char *filename, const char *target,
+                         void *(*open_func) (struct bfd *nbfd,
+                                             void *open_closure),
+                         void *open_closure,
+                         file_ptr (*pread_func) (struct bfd *nbfd,
+                                                 void *stream,
+                                                 void *buf,
+                                                 file_ptr nbytes,
+                                                 file_ptr offset),
+                         int (*close_func) (struct bfd *nbfd,
+                                            void *stream),
+                         int (*stat_func) (struct bfd *abfd,
+                                           void *stream,
+                                           struct stat *sb));
+
+/* A wrapper for bfd_openr_next_archived_file that initializes the
+   gdb-specific reference count and calls gdb_bfd_stash_filename.  */
+
+bfd *gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous);
+
+/* A wrapper for bfd_fdopenr that initializes the gdb-specific
+   reference count and calls gdb_bfd_stash_filename.  */
+
+bfd *gdb_bfd_fdopenr (const char *filename, const char *target, int fd);
+
 #endif /* GDB_BFD_H */
 
 
   buffer->base = addr;
   buffer->size = size;
-  return bfd_openr_iovec ("<in-memory>", target,
-                          mem_bfd_iovec_open,
-                          buffer,
-                          mem_bfd_iovec_pread,
-                          mem_bfd_iovec_close,
-                          mem_bfd_iovec_stat);
+  return gdb_bfd_openr_iovec ("<in-memory>", target,
+                             mem_bfd_iovec_open,
+                             buffer,
+                             mem_bfd_iovec_pread,
+                             mem_bfd_iovec_close,
+                             mem_bfd_iovec_stat);
 }
 
 /* One reader that has been loaded successfully, and can potentially be used to
       puts_unfiltered (_("Error opening JITed symbol file, ignoring it.\n"));
       return;
     }
-  gdb_bfd_ref (nbfd);
 
   /* Check the format.  NOTE: This initializes important data that GDB uses!
      We would segfault later without this line.  */
 
   if (filename == NULL || filename[0] == 0)
     filename = get_exec_file (1);
 
-  abfd = bfd_openr (filename, 0);
-  gdb_bfd_ref (abfd);
+  abfd = gdb_bfd_openr (filename, 0);
   if (!abfd)
     error (_("Unable to open file %s."), filename);
   cleanup = make_cleanup_bfd_unref (abfd);
     printf_filtered (" -- Ethernet load complete.\n");
 
   gettimeofday (&end_time, NULL);
-  abfd = bfd_openr (args, 0);
-  gdb_bfd_ref (abfd);
+  abfd = gdb_bfd_openr (args, 0);
   cleanup = make_cleanup_bfd_unref (abfd);
   if (abfd != NULL)
     {          /* Download is done -- print section statistics.  */
 
 
   bfd_hash_table_free (&table);
 
-  /* Make sure that the filename has the correct lifetime.  The
-     current filename comes either from an OSO symbol name or from an
-     archive name.  Memory for both is not managed by gdb.  */
-  gdb_bfd_stash_filename (abfd);
-
   /* We need to clear SYMFILE_MAINLINE to avoid interractive question
      from symfile.c:symbol_file_add_with_addrs_or_offsets.  */
   symbol_file_add_from_bfd
             }
 
          /* Open the archive and check the format.  */
-         archive_bfd = bfd_openr (archive_name, gnutarget);
-         gdb_bfd_ref (archive_bfd);
+         archive_bfd = gdb_bfd_openr (archive_name, gnutarget);
          if (archive_bfd == NULL)
            {
              warning (_("Could not open OSO archive file \"%s\""),
              continue;
            }
 
-         gdb_bfd_stash_filename (archive_bfd);
-
-         member_bfd = bfd_openr_next_archived_file (archive_bfd, NULL);
-         gdb_bfd_ref (member_bfd);
+         member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);
 
          if (member_bfd == NULL)
            {
                 }
 
               prev = member_bfd;
-             member_bfd = bfd_openr_next_archived_file (archive_bfd,
-                                                        member_bfd);
-             gdb_bfd_ref (member_bfd);
+             member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd,
+                                                            member_bfd);
 
               /* Free previous member if not referenced by an oso.  */
               if (ix2 >= last_ix)
        {
           bfd *abfd;
 
-         abfd = bfd_openr (oso->name, gnutarget);
-         gdb_bfd_ref (abfd);
+         abfd = gdb_bfd_openr (oso->name, gnutarget);
          if (!abfd)
             warning (_("`%s': can't open to read symbols: %s."), oso->name,
                      bfd_errmsg (bfd_get_error ()));
       warning (_("can't find UUID in %s"), objfile->name);
       return NULL;
     }
-  dsym_bfd = bfd_openr (dsym_filename, gnutarget);
-  gdb_bfd_ref (dsym_bfd);
+  dsym_bfd = gdb_bfd_openr (dsym_filename, gnutarget);
   if (dsym_bfd == NULL)
     {
       warning (_("can't open dsym file %s"), dsym_filename);
       return NULL;
     }
-  gdb_bfd_stash_filename (dsym_filename);
 
   if (!bfd_check_format (dsym_bfd, bfd_object))
     {
 
   long storage_needed;
   CORE_ADDR sym_addr;
 
-  abfd = bfd_fdopenr ("unamed", 0, fd);
-  gdb_bfd_ref (abfd);
+  abfd = gdb_bfd_fdopenr ("unamed", 0, fd);
   if (abfd == NULL)
     {
       warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ()));
 
   if (!filename)
     filename = get_exec_file (1);
 
-  pbfd = bfd_openr (filename, gnutarget);
-  gdb_bfd_ref (pbfd);
+  pbfd = gdb_bfd_openr (filename, gnutarget);
   if (pbfd == NULL)
     {
       perror_with_name (filename);
 
 
   buffer = alloca (srec_frame * 2 + 256);
 
-  abfd = bfd_openr (args, 0);
-  gdb_bfd_ref (abfd);
+  abfd = gdb_bfd_openr (args, 0);
   if (!abfd)
     {
       printf_filtered ("Unable to open file %s\n", args);
   buffer = (char *) xmalloc (MAXRECSIZE + 1);
   binbuf = (unsigned char *) xmalloc (BINCHUNK);
 
-  abfd = bfd_openr (file, 0);
-  gdb_bfd_ref (abfd);
+  abfd = gdb_bfd_openr (file, 0);
   if (!abfd)
     {
       printf_filtered ("Unable to open file %s\n", file);
 
 bfd *
 remote_bfd_open (const char *remote_file, const char *target)
 {
-  bfd *abfd = bfd_openr_iovec (remote_file, target,
-                              remote_bfd_iovec_open, NULL,
-                              remote_bfd_iovec_pread,
-                              remote_bfd_iovec_close,
-                              remote_bfd_iovec_stat);
-
-  if (abfd != NULL)
-    gdb_bfd_stash_filename (abfd);
+  bfd *abfd = gdb_bfd_openr_iovec (remote_file, target,
+                                  remote_bfd_iovec_open, NULL,
+                                  remote_bfd_iovec_pread,
+                                  remote_bfd_iovec_close,
+                                  remote_bfd_iovec_stat);
+
   return abfd;
 }
 
 
   if (fd < 0)
     /* Note that this opens it once for every member; a possible
        enhancement would be to only open it once for every object.  */
-    abfd = bfd_openr (filename, gnutarget);
+    abfd = gdb_bfd_openr (filename, gnutarget);
   else
-    abfd = bfd_fdopenr (filename, gnutarget, fd);
-  gdb_bfd_ref (abfd);
+    abfd = gdb_bfd_fdopenr (filename, gnutarget, fd);
   if (!abfd)
     {
       warning (_("Could not open `%s' as an executable file: %s"),
               filename, bfd_errmsg (bfd_get_error ()));
       return NULL;
     }
-  gdb_bfd_stash_filename (abfd);
 
   /* Make sure we have an object file.  */
 
     {
       last = 0;
       /* FIXME??? am I tossing BFDs?  bfd?  */
-      while ((last = bfd_openr_next_archived_file (abfd, last)))
+      while ((last = gdb_bfd_openr_next_archived_file (abfd, last)))
        {
-         gdb_bfd_ref (last);
          if (strcmp (mem, last->filename) == 0)
            break;
        }
 
   cleanup = make_cleanup (null_cleanup, NULL);
 
   /* Create a bfd for the interpreter.  */
-  dyld_bfd = bfd_openr (interp_name, gnutarget);
-  gdb_bfd_ref (dyld_bfd);
+  dyld_bfd = gdb_bfd_openr (interp_name, gnutarget);
   if (dyld_bfd)
     {
       bfd *sub;
 
         to find any magic formula to find it for Solaris (appears to
         be trivial on GNU/Linux).  Therefore, we have to try an alternate
         mechanism to find the dynamic linker's base address.  */
-      tmp_bfd = bfd_openr (buf, gnutarget);
-      gdb_bfd_ref (tmp_bfd);
+      tmp_bfd = gdb_bfd_openr (buf, gnutarget);
       if (tmp_bfd == NULL)
        return;
 
 
   CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
   *open_closure = addr;
 
-  nbfd = bfd_openr_iovec (name, "elf32-spu",
-                         spu_bfd_iovec_open, open_closure,
-                         spu_bfd_iovec_pread, spu_bfd_iovec_close,
-                         spu_bfd_iovec_stat);
-  gdb_bfd_ref (nbfd);
+  nbfd = gdb_bfd_openr_iovec (name, "elf32-spu",
+                             spu_bfd_iovec_open, open_closure,
+                             spu_bfd_iovec_pread, spu_bfd_iovec_close,
+                             spu_bfd_iovec_stat);
   if (!nbfd)
     return NULL;
 
       return NULL;
     }
 
-  gdb_bfd_stash_filename (nbfd);
   return nbfd;
 }
 
 
     }
   else
     {
-      abfd = bfd_fopen (pathname, gnutarget, FOPEN_RB, fd);
+      abfd = gdb_bfd_fopen (pathname, gnutarget, FOPEN_RB, fd);
 
       if (abfd)
        bfd_set_cacheable (abfd, 1);
             pathname, bfd_errmsg (bfd_get_error ()));
     }
 
-  gdb_bfd_stash_filename (abfd);
   xfree (pathname);
 
-  gdb_bfd_ref (abfd);
   return abfd;
 }
 
 
   ULONGEST *open_closure = xmalloc (sizeof (ULONGEST));
   *open_closure = addr;
 
-  nbfd = bfd_openr_iovec ("<in-memory>", "elf32-spu",
-                         spu_bfd_iovec_open, open_closure,
-                         spu_bfd_iovec_pread, spu_bfd_iovec_close,
-                         spu_bfd_iovec_stat);
+  nbfd = gdb_bfd_openr_iovec ("<in-memory>", "elf32-spu",
+                             spu_bfd_iovec_open, open_closure,
+                             spu_bfd_iovec_pread, spu_bfd_iovec_close,
+                             spu_bfd_iovec_stat);
   if (!nbfd)
     return NULL;
-  gdb_bfd_ref (nbfd);
 
   if (!bfd_check_format (nbfd, bfd_object))
     {
 
   if (remote_filename_p (name))
     result = remote_bfd_open (name, gnutarget);
   else
-    {
-      result = bfd_openr (name, gnutarget);
-      if (result != NULL)
-       gdb_bfd_stash_filename (result);
-    }
+    result = gdb_bfd_openr (name, gnutarget);
 
-  gdb_bfd_ref (result);
   return result;
 }
 
   if (remote_filename_p (name))
     {
       sym_bfd = remote_bfd_open (name, gnutarget);
-      gdb_bfd_ref (sym_bfd);
       if (!sym_bfd)
        error (_("`%s': can't open to read symbols: %s."), name,
               bfd_errmsg (bfd_get_error ()));
   name = absolute_name;
   make_cleanup (xfree, name);
 
-  sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
-  gdb_bfd_ref (sym_bfd);
+  sym_bfd = gdb_bfd_fopen (name, gnutarget, FOPEN_RB, desc);
   if (!sym_bfd)
     {
       make_cleanup (xfree, name);
             bfd_errmsg (bfd_get_error ()));
     }
 
-  gdb_bfd_stash_filename (sym_bfd);
-
   return sym_bfd;
 }
 
     }
 
   /* Open the file for loading.  */
-  loadfile_bfd = bfd_openr (filename, gnutarget);
-  gdb_bfd_ref (loadfile_bfd);
+  loadfile_bfd = gdb_bfd_openr (filename, gnutarget);
   if (loadfile_bfd == NULL)
     {
       perror_with_name (filename);
 
       asection *text = NULL;
       CORE_ADDR text_vma;
 
-      abfd = bfd_openr (so->so_name, "pei-i386");
-      gdb_bfd_ref (abfd);
+      abfd = gdb_bfd_openr (so->so_name, "pei-i386");
 
       if (!abfd)
        return so;