Use gdb_bfd_ref_ptr in objfile

This changes struct objfile to use a gdb_bfd_ref_ptr.  In addition to
removing some manual memory management, this fixes a use-after-free
that was introduced by the registry rewrite series.  The issue there
was that, in some cases, registry shutdown could refer to memory that
had already been freed.  This help fix the bug by delaying the
destruction of the BFD reference (and thus the per-bfd object) until
after the registry has been shut down.



diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index d4c5beb..cf8b610 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -608,7 +608,7 @@
   sec = find_pc_section (memaddr);
   if (sec != NULL)
     {
-      arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd);
+      arm_per_bfd *data = arm_bfd_data_key.get (sec->objfile->obfd.get ());
       if (data != NULL)
 	{
 	  unsigned int section_idx = sec->the_bfd_section->index;
@@ -2451,38 +2451,39 @@
   LONGEST i;
 
   /* If we've already touched this file, do nothing.  */
-  if (!objfile || arm_exidx_data_key.get (objfile->obfd) != NULL)
+  if (!objfile || arm_exidx_data_key.get (objfile->obfd.get ()) != NULL)
     return;
 
   /* Read contents of exception table and index.  */
-  exidx = bfd_get_section_by_name (objfile->obfd, ELF_STRING_ARM_unwind);
+  exidx = bfd_get_section_by_name (objfile->obfd.get (),
+				   ELF_STRING_ARM_unwind);
   gdb::byte_vector exidx_data;
   if (exidx)
     {
       exidx_vma = bfd_section_vma (exidx);
       exidx_data.resize (bfd_section_size (exidx));
 
-      if (!bfd_get_section_contents (objfile->obfd, exidx,
+      if (!bfd_get_section_contents (objfile->obfd.get (), exidx,
 				     exidx_data.data (), 0,
 				     exidx_data.size ()))
 	return;
     }
 
-  extab = bfd_get_section_by_name (objfile->obfd, ".ARM.extab");
+  extab = bfd_get_section_by_name (objfile->obfd.get (), ".ARM.extab");
   gdb::byte_vector extab_data;
   if (extab)
     {
       extab_vma = bfd_section_vma (extab);
       extab_data.resize (bfd_section_size (extab));
 
-      if (!bfd_get_section_contents (objfile->obfd, extab,
+      if (!bfd_get_section_contents (objfile->obfd.get (), extab,
 				     extab_data.data (), 0,
 				     extab_data.size ()))
 	return;
     }
 
   /* Allocate exception table data structure.  */
-  data = arm_exidx_data_key.emplace (objfile->obfd);
+  data = arm_exidx_data_key.emplace (objfile->obfd.get ());
   data->section_maps.resize (objfile->obfd->section_count);
 
   /* Fill in exception table.  */
@@ -2654,7 +2655,7 @@
       struct arm_exidx_data *data;
       struct arm_exidx_entry map_key = { memaddr - sec->addr (), 0 };
 
-      data = arm_exidx_data_key.get (sec->objfile->obfd);
+      data = arm_exidx_data_key.get (sec->objfile->obfd.get ());
       if (data != NULL)
 	{
 	  std::vector<arm_exidx_entry> &map
@@ -9453,9 +9454,9 @@
   if (name[1] != 'a' && name[1] != 't' && name[1] != 'd')
     return;
 
-  data = arm_bfd_data_key.get (objfile->obfd);
+  data = arm_bfd_data_key.get (objfile->obfd.get ());
   if (data == NULL)
-    data = arm_bfd_data_key.emplace (objfile->obfd,
+    data = arm_bfd_data_key.emplace (objfile->obfd.get (),
 				     objfile->obfd->section_count);
   arm_mapping_symbol_vec &map
     = data->section_maps[bfd_asymbol_section (sym)->index];
diff --git a/gdb/auto-load.c b/gdb/auto-load.c
index 54ed73d..198bb07 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -858,7 +858,7 @@
     {
       unsigned long crc32;
       gdb::unique_xmalloc_ptr<char> debuglink
-	(bfd_get_debug_link_info (parent->obfd, &crc32));
+	(bfd_get_debug_link_info (parent->obfd.get (), &crc32));
 
       if (debuglink.get () != nullptr
 	  && strcmp (debuglink.get (), lbasename (realname.get ())) != 0)
@@ -1119,7 +1119,7 @@
 static void
 auto_load_section_scripts (struct objfile *objfile, const char *section_name)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   asection *scripts_sect;
   bfd_byte *data = NULL;
 
diff --git a/gdb/build-id.c b/gdb/build-id.c
index 345ed0e..0c5d65d 100644
--- a/gdb/build-id.c
+++ b/gdb/build-id.c
@@ -206,7 +206,7 @@
 {
   const struct bfd_build_id *build_id;
 
-  build_id = build_id_bfd_get (objfile->obfd);
+  build_id = build_id_bfd_get (objfile->obfd.get ());
   if (build_id != NULL)
     {
       if (separate_debug_file_debug)
diff --git a/gdb/coff-pe-read.c b/gdb/coff-pe-read.c
index 72bd43b..2f3b808 100644
--- a/gdb/coff-pe-read.c
+++ b/gdb/coff-pe-read.c
@@ -300,7 +300,7 @@
 read_pe_exported_syms (minimal_symbol_reader &reader,
 		       struct objfile *objfile)
 {
-  bfd *dll = objfile->obfd;
+  bfd *dll = objfile->obfd.get ();
   unsigned long nbnormal, nbforward;
   unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
   unsigned long export_opthdrrva, export_opthdrsize;
@@ -312,7 +312,7 @@
   int is_pe64 = 0;
   int is_pe32 = 0;
 
-  char const *target = bfd_get_target (objfile->obfd);
+  char const *target = bfd_get_target (objfile->obfd.get ());
 
   std::vector<struct read_pe_section_data> section_data
     (PE_SECTION_TABLE_SIZE);
diff --git a/gdb/coffread.c b/gdb/coffread.c
index f7f5bb0..45d41d0 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -278,7 +278,7 @@
 
   args.targ_index = cs->c_secnum;
   args.resultp = &sect;
-  bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
+  bfd_map_over_sections (objfile->obfd.get (), find_targ_sec, &args);
   return sect;
 }
 
@@ -290,7 +290,7 @@
 
   if (sect == NULL)
     return SECT_OFF_TEXT (objfile);
-  return gdb_bfd_section_index (objfile->obfd, sect);
+  return gdb_bfd_section_index (objfile->obfd.get (), sect);
 }
 
 /* Return the address of the section of a COFF symbol.  */
@@ -579,7 +579,8 @@
 		name1 = name + 6;
 	      if (name1 != NULL)
 		{
-		  int lead = bfd_get_symbol_leading_char (objfile->obfd);
+		  int lead
+		    = bfd_get_symbol_leading_char (objfile->obfd.get ());
 		  struct bound_minimal_symbol found;
 
 		  if (lead != '\0' && *name1 == lead)
@@ -610,7 +611,7 @@
 coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 {
   struct coff_symfile_info *info;
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   coff_data_type *cdata = coff_data (abfd);
   const char *filename = bfd_get_filename (abfd);
   int val;
@@ -655,8 +656,8 @@
      FIXME: We should use BFD to read the symbol table, and thus avoid
      this problem.  */
   pe_file =
-    startswith (bfd_get_target (objfile->obfd), "pe")
-    || startswith (bfd_get_target (objfile->obfd), "epoc-pe");
+    startswith (bfd_get_target (objfile->obfd.get ()), "pe")
+    || startswith (bfd_get_target (objfile->obfd.get ()), "epoc-pe");
 
   /* End of warning.  */
 
@@ -742,7 +743,7 @@
 	{
 	  gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (debugfile.c_str ()));
 
-	  symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
+	  symbol_file_add_separate (debug_bfd, debugfile.c_str (),
 				    symfile_flags, objfile);
 	}
     }
@@ -817,15 +818,15 @@
      FIXME: Find out if this has been reported to Sun, whether it has
      been fixed in a later release, etc.  */
 
-  bfd_seek (objfile->obfd, 0, 0);
+  bfd_seek (objfile->obfd.get (), 0, 0);
 
   /* Position to read the symbol table.  */
-  val = bfd_seek (objfile->obfd, symtab_offset, 0);
+  val = bfd_seek (objfile->obfd.get (), symtab_offset, 0);
   if (val < 0)
     perror_with_name (objfile_name (objfile));
 
   coffread_objfile = objfile;
-  nlist_bfd_global = objfile->obfd;
+  nlist_bfd_global = objfile->obfd.get ();
   nlist_nsyms_global = nsyms;
   set_last_source_file (NULL);
   memset (opaque_type_chain, 0, sizeof opaque_type_chain);
@@ -1565,7 +1566,7 @@
   char *name;
 
   name = cs->c_name;
-  name = EXTERNAL_NAME (name, objfile->obfd);
+  name = EXTERNAL_NAME (name, objfile->obfd.get ());
   sym->set_language (get_current_subfile ()->language,
 		     &objfile->objfile_obstack);
   sym->compute_and_set_names (name, true, objfile->per_bfd);
@@ -2001,7 +2002,7 @@
     {
       read_one_sym (ms, &sub_sym, &sub_aux);
       name = ms->c_name;
-      name = EXTERNAL_NAME (name, objfile->obfd);
+      name = EXTERNAL_NAME (name, objfile->obfd.get ());
 
       switch (ms->c_sclass)
 	{
@@ -2095,7 +2096,7 @@
     {
       read_one_sym (ms, &sub_sym, &sub_aux);
       name = ms->c_name;
-      name = EXTERNAL_NAME (name, objfile->obfd);
+      name = EXTERNAL_NAME (name, objfile->obfd.get ());
 
       switch (ms->c_sclass)
 	{
diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
index d393091..06e8c85 100644
--- a/gdb/compile/compile-object-load.c
+++ b/gdb/compile/compile-object-load.c
@@ -643,7 +643,7 @@
 
   /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
      "Reading symbols from ..." message for automatically generated file.  */
-  objfile_up objfile_holder (symbol_file_add_from_bfd (abfd.get (),
+  objfile_up objfile_holder (symbol_file_add_from_bfd (abfd,
 						       filename.get (),
 						       0, NULL, 0, NULL));
   objfile = objfile_holder.get ();
diff --git a/gdb/ctfread.c b/gdb/ctfread.c
index 0da4f0d..9436ce5 100644
--- a/gdb/ctfread.c
+++ b/gdb/ctfread.c
@@ -1235,7 +1235,7 @@
 static CORE_ADDR
 get_objfile_text_range (struct objfile *of, int *tsize)
 {
-  bfd *abfd = of->obfd;
+  bfd *abfd = of->obfd.get ();
   const asection *codes;
 
   codes = bfd_get_section_by_name (abfd, ".text");
@@ -1543,7 +1543,7 @@
 
   if (strcmp (fname, ".ctf") == 0)
     {
-      fname = bfd_get_filename (of->obfd);
+      fname = bfd_get_filename (of->obfd.get ());
       isparent = true;
     }
 
@@ -1602,7 +1602,7 @@
 elfctf_build_psymtabs (struct objfile *of)
 {
   struct ctf_per_tu_data pcu;
-  bfd *abfd = of->obfd;
+  bfd *abfd = of->obfd.get ();
   int err;
 
   ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
diff --git a/gdb/dbxread.c b/gdb/dbxread.c
index e1bf9a0..1f93eb5 100644
--- a/gdb/dbxread.c
+++ b/gdb/dbxread.c
@@ -485,7 +485,7 @@
       {
 	const char *tempstring = name;
 
-	if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd))
+	if (tempstring[0] == bfd_get_symbol_leading_char (objfile->obfd.get ()))
 	  ++tempstring;
 	if (is_vtable_name (tempstring))
 	  ms_type = mst_data;
@@ -520,7 +520,7 @@
   bfd *sym_bfd;
   int val;
 
-  sym_bfd = objfile->obfd;
+  sym_bfd = objfile->obfd.get ();
 
   /* .o and .nlm files are relocatables with text, data and bss segs based at
      0.  This flag disables special (Solaris stabs-in-elf only) fixups for
@@ -583,7 +583,7 @@
 dbx_symfile_init (struct objfile *objfile)
 {
   int val;
-  bfd *sym_bfd = objfile->obfd;
+  bfd *sym_bfd = objfile->obfd.get ();
   const char *name = bfd_get_filename (sym_bfd);
   asection *text_sect;
   unsigned char size_temp[DBX_STRINGTAB_SIZE_SIZE];
@@ -1010,8 +1010,8 @@
 
   lowest_text_address = (CORE_ADDR) -1;
 
-  symfile_bfd = objfile->obfd;	/* For next_text_symbol.  */
-  abfd = objfile->obfd;
+  symfile_bfd = objfile->obfd.get ();	/* For next_text_symbol.  */
+  abfd = objfile->obfd.get ();
   symbuf_end = symbuf_idx = 0;
   next_symbol_text_func = dbx_next_symbol_text;
   textlow_not_set = 1;
@@ -2120,7 +2120,7 @@
       symbol_size = SYMBOL_SIZE (pst);
 
       /* Read in this file's symbols.  */
-      bfd_seek (objfile->obfd, SYMBOL_OFFSET (pst), SEEK_SET);
+      bfd_seek (objfile->obfd.get (), SYMBOL_OFFSET (pst), SEEK_SET);
       read_ofile_symtab (objfile, pst);
     }
 
@@ -2187,8 +2187,8 @@
   stringtab_global = DBX_STRINGTAB (objfile);
   set_last_source_file (NULL);
 
-  abfd = objfile->obfd;
-  symfile_bfd = objfile->obfd;	/* Implicit param to next_text_symbol.  */
+  abfd = objfile->obfd.get ();
+  symfile_bfd = objfile->obfd.get ();	/* Implicit param to next_text_symbol.  */
   symbuf_end = symbuf_idx = 0;
   symbuf_read = 0;
   symbuf_left = sym_offset + sym_size;
@@ -2932,7 +2932,7 @@
 			 file_ptr stabstroffset, unsigned int stabstrsize)
 {
   int val;
-  bfd *sym_bfd = objfile->obfd;
+  bfd *sym_bfd = objfile->obfd.get ();
   const char *name = bfd_get_filename (sym_bfd);
   unsigned int stabsize;
 
@@ -3019,7 +3019,7 @@
 			file_ptr stabstroffset, unsigned int stabstrsize)
 {
   int val;
-  bfd *sym_bfd = objfile->obfd;
+  bfd *sym_bfd = objfile->obfd.get ();
   const char *name = bfd_get_filename (sym_bfd);
 
   stabsread_new_init ();
@@ -3100,7 +3100,7 @@
 			 char *stabstr_name, char *text_name)
 {
   int val;
-  bfd *sym_bfd = objfile->obfd;
+  bfd *sym_bfd = objfile->obfd.get ();
   const char *name = bfd_get_filename (sym_bfd);
   asection *stabsect;
   asection *stabstrsect;
diff --git a/gdb/dtrace-probe.c b/gdb/dtrace-probe.c
index 6f01edf..b1f6ce5 100644
--- a/gdb/dtrace-probe.c
+++ b/gdb/dtrace-probe.c
@@ -830,7 +830,7 @@
   (std::vector<std::unique_ptr<probe>> *probesp,
    struct objfile *objfile) const
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   asection *sect = NULL;
 
   /* Do nothing in case this is a .debug file, instead of the objfile
diff --git a/gdb/dwarf2/frame.c b/gdb/dwarf2/frame.c
index d7a0639..387a2bc 100644
--- a/gdb/dwarf2/frame.c
+++ b/gdb/dwarf2/frame.c
@@ -138,7 +138,7 @@
 struct comp_unit
 {
   comp_unit (struct objfile *objf)
-    : abfd (objf->obfd)
+    : abfd (objf->obfd.get ())
   {
   }
 
@@ -1534,7 +1534,7 @@
 static comp_unit *
 find_comp_unit (struct objfile *objfile)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   if (gdb_bfd_requires_relocations (abfd))
     return dwarf2_frame_objfile_data.get (objfile);
 
@@ -1547,7 +1547,7 @@
 static void
 set_comp_unit (struct objfile *objfile, struct comp_unit *unit)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   if (gdb_bfd_requires_relocations (abfd))
     return dwarf2_frame_objfile_data.set (objfile, unit);
 
diff --git a/gdb/dwarf2/index-cache.c b/gdb/dwarf2/index-cache.c
index a1f6ff5..6de5859 100644
--- a/gdb/dwarf2/index-cache.c
+++ b/gdb/dwarf2/index-cache.c
@@ -101,7 +101,7 @@
     return;
 
   /* Get build id of objfile.  */
-  const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
+  const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
   if (build_id == nullptr)
     {
       index_cache_debug ("objfile %s has no build id",
diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c
index efd154d..b6d8ddd 100644
--- a/gdb/dwarf2/index-write.c
+++ b/gdb/dwarf2/index-write.c
@@ -765,7 +765,7 @@
     /* Object constructor to be called for current DWARF2_PER_OBJFILE.
        All .debug_str section strings are automatically stored.  */
     debug_str_lookup (dwarf2_per_objfile *per_objfile)
-      : m_abfd (per_objfile->objfile->obfd),
+      : m_abfd (per_objfile->objfile->obfd.get ()),
 	m_per_objfile (per_objfile)
     {
       per_objfile->per_bfd->str.read (per_objfile->objfile);
diff --git a/gdb/dwarf2/loc.c b/gdb/dwarf2/loc.c
index f490b68..bf0df61 100644
--- a/gdb/dwarf2/loc.c
+++ b/gdb/dwarf2/loc.c
@@ -355,7 +355,7 @@
   struct gdbarch *gdbarch = objfile->arch ();
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   unsigned int addr_size = baton->per_cu->addr_size ();
-  int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
+  int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd.get ());
   /* Adjustment for relocatable objects.  */
   CORE_ADDR text_offset = baton->per_objfile->objfile->text_section_offset ();
   CORE_ADDR base_address = baton->base_address;
@@ -3952,7 +3952,7 @@
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   unsigned int addr_size = dlbaton->per_cu->addr_size ();
   int offset_size = dlbaton->per_cu->offset_size ();
-  int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
+  int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd.get ());
   /* Adjustment for relocatable objects.  */
   CORE_ADDR text_offset = objfile->text_section_offset ();
   CORE_ADDR base_address = dlbaton->base_address;
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 8c66cb8..f031519 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1569,23 +1569,24 @@
 	 We don't share with objfiles for which -readnow was requested,
 	 because it would complicate things when loading the same BFD with
 	 -readnow and then without -readnow.  */
-      if (!gdb_bfd_requires_relocations (objfile->obfd)
+      if (!gdb_bfd_requires_relocations (objfile->obfd.get ())
 	  && (objfile->flags & OBJF_READNOW) == 0)
 	{
 	  /* See if one has been created for this BFD yet.  */
-	  per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
+	  per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd.get ());
 
 	  if (per_bfd == nullptr)
 	    {
 	      /* No, create it now.  */
-	      per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
-	      dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
+	      per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names,
+					    can_copy);
+	      dwarf2_per_bfd_bfd_data_key.set (objfile->obfd.get (), per_bfd);
 	    }
 	}
       else
 	{
 	  /* No sharing possible, create one specifically for this objfile.  */
-	  per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
+	  per_bfd = new dwarf2_per_bfd (objfile->obfd.get (), names, can_copy);
 	  dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
 	}
 
@@ -2326,7 +2327,7 @@
 			   addrmap *mutable_map)
 {
   struct objfile *objfile = per_objfile->objfile;
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   struct gdbarch *gdbarch = objfile->arch ();
   const CORE_ADDR baseaddr = objfile->text_section_offset ();
   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
@@ -4926,7 +4927,7 @@
 
   dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
   struct objfile *objfile = m_per_objfile->objfile;
-  bfd *const abfd = objfile->obfd;
+  bfd *const abfd = objfile->obfd.get ();
 
  again:
 
@@ -5297,7 +5298,7 @@
 static gdb::array_view<const gdb_byte>
 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
 {
-  const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
+  const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
   if (build_id == nullptr)
     return {};
 
@@ -11118,7 +11119,7 @@
      This is important because things like demangled_names_hash lives in the
      objfile's per_bfd space and may have references to things like symbol
      names that live in the DWO/DWP file's per_bfd space.  PR 16426.  */
-  gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
+  gdb_bfd_record_inclusion (per_objfile->objfile->obfd.get (), sym_bfd.get ());
 
   return sym_bfd;
 }
@@ -12645,7 +12646,7 @@
 {
   dwarf2_per_objfile *per_objfile = cu->per_objfile;
   struct objfile *objfile = per_objfile->objfile;
-  bfd *obfd = objfile->obfd;
+  bfd *obfd = objfile->obfd.get ();
   /* Base address selection entry.  */
   gdb::optional<CORE_ADDR> base;
   const gdb_byte *buffer;
@@ -12841,7 +12842,7 @@
   dwarf2_per_objfile *per_objfile = cu->per_objfile;
   struct objfile *objfile = per_objfile->objfile;
   struct comp_unit_head *cu_header = &cu->header;
-  bfd *obfd = objfile->obfd;
+  bfd *obfd = objfile->obfd.get ();
   unsigned int addr_size = cu_header->addr_size;
   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
   /* Base address selection entry.  */
@@ -16683,7 +16684,8 @@
 	  if (ptr - blk->data + len <= blk->size)
 	    {
 	      mpz_import (value->val, len,
-			  bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
+			  bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
+			  ? 1 : -1,
 			  1, 0, 0, ptr);
 	      return;
 	    }
@@ -16696,7 +16698,8 @@
     {
       dwarf_block *blk = attr->as_block ();
       mpz_import (value->val, blk->size,
-		  bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
+		  bfd_big_endian (cu->per_objfile->objfile->obfd.get ())
+		  ? 1 : -1,
 		  1, 0, 0, blk->data);
     }
   else
@@ -18791,7 +18794,7 @@
 {
   dwarf2_per_objfile *per_objfile = cu->per_objfile;
   struct objfile *objfile = per_objfile->objfile;
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   ULONGEST loclist_header_size =
     (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
      : LOCLIST_HEADER_SIZE64);
@@ -18850,7 +18853,7 @@
 {
   struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   ULONGEST rnglist_header_size =
     (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
      : RNGLIST_HEADER_SIZE64);
@@ -19285,7 +19288,7 @@
 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
 				      unsigned int offset_size)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   ULONGEST str_offset = read_offset (abfd, buf, offset_size);
 
   return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
@@ -19298,7 +19301,7 @@
 				      const struct comp_unit_head *cu_header,
 				      unsigned int *bytes_read_ptr)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
 
   return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
@@ -19313,7 +19316,7 @@
 		   gdb::optional<ULONGEST> addr_base, int addr_size)
 {
   struct objfile *objfile = per_objfile->objfile;
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   const gdb_byte *info_ptr;
   ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
 
@@ -19349,7 +19352,7 @@
 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
 			     unsigned int *bytes_read)
 {
-  bfd *abfd = cu->per_objfile->objfile->obfd;
+  bfd *abfd = cu->per_objfile->objfile->obfd.get ();
   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
 
   return read_addr_index (cu, addr_index);
@@ -19411,7 +19414,7 @@
   dwarf2_per_objfile *per_objfile = cu->per_objfile;
   struct objfile *objfile = per_objfile->objfile;
   const char *objf_name = objfile_name (objfile);
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   const gdb_byte *info_ptr;
   ULONGEST str_offset;
   static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
@@ -20298,7 +20301,7 @@
   unsigned char op_code, extended_op;
   CORE_ADDR baseaddr;
   struct objfile *objfile = cu->per_objfile->objfile;
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   struct gdbarch *gdbarch = objfile->arch ();
 
   baseaddr = objfile->text_section_offset ();
@@ -20635,7 +20638,7 @@
 
 	  if (block->data[0] == DW_OP_addr)
 	    sym->set_value_address
-	      (cu->header.read_address (objfile->obfd, block->data + 1,
+	      (cu->header.read_address (objfile->obfd.get (), block->data + 1,
 					&dummy));
 	  else
 	    sym->set_value_address
@@ -21107,7 +21110,7 @@
 			 struct dwarf2_cu *cu, LONGEST *value, int bits)
 {
   struct objfile *objfile = cu->per_objfile->objfile;
-  enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
+  enum bfd_endian byte_order = bfd_big_endian (objfile->obfd.get ()) ?
 				BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
   LONGEST l = attr->constant_value (0);
 
@@ -21145,7 +21148,7 @@
   struct objfile *objfile = per_objfile->objfile;
   struct comp_unit_head *cu_header = &cu->header;
   struct dwarf_block *blk;
-  enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
+  enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd.get ()) ?
 				BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
 
   *value = 0;
@@ -22497,7 +22500,7 @@
   if (attr == NULL)
     return NULL;
 
-  byte_order = (bfd_big_endian (objfile->obfd)
+  byte_order = (bfd_big_endian (objfile->obfd.get ())
 		? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
 
   switch (attr->form)
@@ -23009,43 +23012,44 @@
 	  break;
 
 	case DW_OP_addr:
-	  stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
+	  stack[++stacki] = cu->header.read_address (objfile->obfd.get (),
+						     &data[i],
 						     &bytes_read);
 	  i += bytes_read;
 	  break;
 
 	case DW_OP_const1u:
-	  stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_1_byte (objfile->obfd.get (), &data[i]);
 	  i += 1;
 	  break;
 
 	case DW_OP_const1s:
-	  stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_1_signed_byte (objfile->obfd.get (), &data[i]);
 	  i += 1;
 	  break;
 
 	case DW_OP_const2u:
-	  stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_2_bytes (objfile->obfd.get (), &data[i]);
 	  i += 2;
 	  break;
 
 	case DW_OP_const2s:
-	  stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_2_signed_bytes (objfile->obfd.get (), &data[i]);
 	  i += 2;
 	  break;
 
 	case DW_OP_const4u:
-	  stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_4_bytes (objfile->obfd.get (), &data[i]);
 	  i += 4;
 	  break;
 
 	case DW_OP_const4s:
-	  stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_4_signed_bytes (objfile->obfd.get (), &data[i]);
 	  i += 4;
 	  break;
 
 	case DW_OP_const8u:
-	  stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
+	  stack[++stacki] = read_8_bytes (objfile->obfd.get (), &data[i]);
 	  i += 8;
 	  break;
 
diff --git a/gdb/elfread.c b/gdb/elfread.c
index e0de52c..08db208 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -211,7 +211,7 @@
      create an msymbol that references an uninitialised section object.  */
   int section_index = 0;
   if ((bfd_section_flags (bfd_section) & SEC_ALLOC) == SEC_ALLOC)
-    section_index = gdb_bfd_section_index (objfile->obfd, bfd_section);
+    section_index = gdb_bfd_section_index (objfile->obfd.get (), bfd_section);
 
   struct minimal_symbol *result
     = reader.record_full (name, copy_name, address, ms_type, section_index);
@@ -252,7 +252,7 @@
   /* Name of the last file symbol.  This is either a constant string or is
      saved on the objfile's filename cache.  */
   const char *filesymname = "";
-  int stripped = (bfd_get_symcount (objfile->obfd) == 0);
+  int stripped = (bfd_get_symcount (objfile->obfd.get ()) == 0);
   int elf_make_msymbol_special_p
     = gdbarch_elf_make_msymbol_special_p (gdbarch);
 
@@ -271,7 +271,7 @@
       /* Skip "special" symbols, e.g. ARM mapping symbols.  These are
 	 symbols which do not correspond to objects in the symbol table,
 	 but have some other target-specific meaning.  */
-      if (bfd_is_target_special_symbol (objfile->obfd, sym))
+      if (bfd_is_target_special_symbol (objfile->obfd.get (), sym))
 	{
 	  if (gdbarch_record_special_symbol_p (gdbarch))
 	    gdbarch_record_special_symbol (gdbarch, objfile, sym);
@@ -283,7 +283,7 @@
 	  && (sym->flags & BSF_FUNCTION))
 	{
 	  struct minimal_symbol *msym;
-	  bfd *abfd = objfile->obfd;
+	  bfd *abfd = objfile->obfd.get ();
 	  asection *sect;
 
 	  /* Symbol is a reference to a function defined in
@@ -547,7 +547,7 @@
 elf_rel_plt_read (minimal_symbol_reader &reader,
 		  struct objfile *objfile, asymbol **dyn_symbol_table)
 {
-  bfd *obfd = objfile->obfd;
+  bfd *obfd = objfile->obfd.get ();
   const struct elf_backend_data *bed = get_elf_backend_data (obfd);
   asection *relplt, *got_plt;
   bfd_size_type reloc_count, reloc;
@@ -816,7 +816,7 @@
 
   for (objfile *objfile : current_program_space->objfiles ())
     {
-      bfd *obfd = objfile->obfd;
+      bfd *obfd = objfile->obfd.get ();
       struct gdbarch *gdbarch = objfile->arch ();
       struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
       size_t ptr_size = TYPE_LENGTH (ptr_type);
@@ -1041,7 +1041,7 @@
 elf_read_minimal_symbols (struct objfile *objfile, int symfile_flags,
 			  const struct elfinfo *ei)
 {
-  bfd *synth_abfd, *abfd = objfile->obfd;
+  bfd *synth_abfd, *abfd = objfile->obfd.get ();
   long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
   asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
   asymbol *synthsyms;
@@ -1067,10 +1067,10 @@
 
   /* Process the normal ELF symbol table first.  */
 
-  storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
+  storage_needed = bfd_get_symtab_upper_bound (objfile->obfd.get ());
   if (storage_needed < 0)
     error (_("Can't read symbols from %s: %s"),
-	   bfd_get_filename (objfile->obfd),
+	   bfd_get_filename (objfile->obfd.get ()),
 	   bfd_errmsg (bfd_get_error ()));
 
   if (storage_needed > 0)
@@ -1079,11 +1079,11 @@
 	 bfd_canonicalize_symtab so it must not get freed before ABFD gets.  */
 
       symbol_table = (asymbol **) bfd_alloc (abfd, storage_needed);
-      symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
+      symcount = bfd_canonicalize_symtab (objfile->obfd.get (), symbol_table);
 
       if (symcount < 0)
 	error (_("Can't read symbols from %s: %s"),
-	       bfd_get_filename (objfile->obfd),
+	       bfd_get_filename (objfile->obfd.get ()),
 	       bfd_errmsg (bfd_get_error ()));
 
       elf_symtab_read (reader, objfile, ST_REGULAR, symcount, symbol_table,
@@ -1092,7 +1092,7 @@
 
   /* Add the dynamic symbols.  */
 
-  storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);
+  storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd.get ());
 
   if (storage_needed > 0)
     {
@@ -1104,12 +1104,12 @@
 	 implementation detail, though.  */
 
       dyn_symbol_table = (asymbol **) bfd_alloc (abfd, storage_needed);
-      dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
+      dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd.get (),
 						     dyn_symbol_table);
 
       if (dynsymcount < 0)
 	error (_("Can't read symbols from %s: %s"),
-	       bfd_get_filename (objfile->obfd),
+	       bfd_get_filename (objfile->obfd.get ()),
 	       bfd_errmsg (bfd_get_error ()));
 
       elf_symtab_read (reader, objfile, ST_DYNAMIC, dynsymcount,
@@ -1131,7 +1131,7 @@
      backlinked binary where it is valid.  */
 
   if (objfile->separate_debug_objfile_backlink)
-    synth_abfd = objfile->separate_debug_objfile_backlink->obfd;
+    synth_abfd = objfile->separate_debug_objfile_backlink->obfd.get ();
   else
     synth_abfd = abfd;
 
@@ -1193,7 +1193,7 @@
 static void
 elf_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   struct elfinfo ei;
   bool has_dwarf2 = true;
 
@@ -1271,13 +1271,14 @@
 	{
 	  gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (debugfile.c_str ()));
 
-	  symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
+	  symbol_file_add_separate (debug_bfd, debugfile.c_str (),
 				    symfile_flags, objfile);
 	}
       else
 	{
 	  has_dwarf2 = false;
-	  const struct bfd_build_id *build_id = build_id_bfd_get (objfile->obfd);
+	  const struct bfd_build_id *build_id
+	    = build_id_bfd_get (objfile->obfd.get ());
 
 	  if (build_id != nullptr)
 	    {
@@ -1297,7 +1298,7 @@
 			     objfile->original_name);
 		  else if (build_id_verify (debug_bfd.get (), build_id->size, build_id->data))
 		    {
-		      symbol_file_add_separate (debug_bfd.get (), symfile_path.get (),
+		      symbol_file_add_separate (debug_bfd, symfile_path.get (),
 						symfile_flags, objfile);
 		      has_dwarf2 = true;
 		    }
@@ -1348,11 +1349,11 @@
 static const elfread_data &
 elf_get_probes (struct objfile *objfile)
 {
-  elfread_data *probes_per_bfd = probe_key.get (objfile->obfd);
+  elfread_data *probes_per_bfd = probe_key.get (objfile->obfd.get ());
 
   if (probes_per_bfd == NULL)
     {
-      probes_per_bfd = probe_key.emplace (objfile->obfd);
+      probes_per_bfd = probe_key.emplace (objfile->obfd.get ());
 
       /* Here we try to gather information about all types of probes from the
 	 objfile.  */
diff --git a/gdb/gcore.c b/gdb/gcore.c
index b81ef81..5190077 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -407,7 +407,7 @@
       for (objfile *objfile : current_program_space->objfiles ())
 	ALL_OBJFILE_OSECTIONS (objfile, objsec)
 	  {
-	    bfd *abfd = objfile->obfd;
+	    bfd *abfd = objfile->obfd.get ();
 	    asection *asec = objsec->the_bfd_section;
 	    bfd_vma align = (bfd_vma) 1 << bfd_section_alignment (asec);
 	    bfd_vma start = objsec->addr () & -align;
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index 54b3d22..91f9cec 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -264,7 +264,7 @@
 	{
 	  low_text_segment_address = -1;
 
-	  bfd_map_over_sections (objfile->obfd,
+	  bfd_map_over_sections (objfile->obfd.get (),
 				 record_text_segment_lowaddr, 
 				 &low_text_segment_address);
 
@@ -275,7 +275,7 @@
 	  text_offset = tdep->solib_get_text_base (objfile);
 	}
 
-      bfd_get_section_contents (objfile->obfd, section, buf, 0, size);
+      bfd_get_section_contents (objfile->obfd.get (), section, buf, 0, size);
 
       /* Now internalize the information being careful to handle host/target
 	 endian issues.  */
@@ -379,7 +379,8 @@
 
   /* Now compute the size of the stub unwinds.  Note the ELF tools do not
      use stub unwinds at the current time.  */
-  stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
+  stub_unwind_sec = bfd_get_section_by_name (objfile->obfd.get (),
+					     "$UNWIND_END$");
 
   if (stub_unwind_sec)
     {
@@ -427,7 +428,7 @@
       char *buf = (char *) alloca (stub_unwind_size);
 
       /* Read in the stub unwind entries.  */
-      bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
+      bfd_get_section_contents (objfile->obfd.get (), stub_unwind_sec, buf,
 				0, stub_unwind_size);
 
       /* Now convert them into regular unwind entries.  */
diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c
index d500f69..d8e910b 100644
--- a/gdb/i386-tdep.c
+++ b/gdb/i386-tdep.c
@@ -2799,14 +2799,15 @@
       if (objf != nullptr)
 	{
 	  /* Get corresponding .got.plt or .got section.  */
-	  asect = bfd_get_section_by_name (objf->obfd, ".got.plt");
+	  asect = bfd_get_section_by_name (objf->obfd.get (), ".got.plt");
 	  if (asect == nullptr)
-	    asect = bfd_get_section_by_name (objf->obfd, ".got");
+	    asect = bfd_get_section_by_name (objf->obfd.get (), ".got");
 	}
 
       if (asect != nullptr)
 	/* Translate asection to obj_section.  */
-	osect = maint_obj_section_from_bfd_section (objf->obfd, asect, objf);
+	osect = maint_obj_section_from_bfd_section (objf->obfd.get (),
+						    asect, objf);
 
       if (osect != nullptr)
 	{
diff --git a/gdb/jit.c b/gdb/jit.c
index b4a070b..167ea8c 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -798,7 +798,7 @@
       }
 
   /* This call does not take ownership of SAI.  */
-  objfile = symbol_file_add_from_bfd (nbfd.get (),
+  objfile = symbol_file_add_from_bfd (nbfd,
 				      bfd_get_filename (nbfd.get ()), 0,
 				      &sai,
 				      OBJF_SHARED | OBJF_NOT_FILENAME, NULL);
diff --git a/gdb/machoread.c b/gdb/machoread.c
index 8c4b08f..15ce4ef 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -139,7 +139,7 @@
 	return;	/* Skip this symbol.  */
 
       reader.record_with_info (sym->name, symaddr, ms_type,
-			       gdb_bfd_section_index (objfile->obfd,
+			       gdb_bfd_section_index (objfile->obfd.get (),
 						      sym->section));
     }
 }
@@ -395,7 +395,7 @@
   struct bound_minimal_symbol msym;
   const char *name = sym->name;
 
-  if (name[0] == bfd_get_symbol_leading_char (main_objfile->obfd))
+  if (name[0] == bfd_get_symbol_leading_char (main_objfile->obfd.get ()))
     ++name;
   msym = lookup_minimal_symbol (name, NULL, main_objfile);
   if (msym.minsym == NULL)
@@ -584,7 +584,7 @@
   /* We need to clear SYMFILE_MAINLINE to avoid interactive question
      from symfile.c:symbol_file_add_with_addrs_or_offsets.  */
   symbol_file_add_from_bfd
-    (abfd.get (), name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE),
+    (abfd, name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE),
      NULL,
      main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
 			    | OBJF_READNOW | OBJF_USERLOADED),
@@ -742,7 +742,7 @@
   if (access (dsym_filename, R_OK) != 0)
     return NULL;
 
-  if (bfd_mach_o_lookup_command (objfile->obfd,
+  if (bfd_mach_o_lookup_command (objfile->obfd.get (),
 				 BFD_MACH_O_LC_UUID, &main_uuid) == 0)
     {
       warning (_("can't find UUID in %s"), objfile_name (objfile));
@@ -781,7 +781,7 @@
 static void
 macho_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   long storage_needed;
   std::vector<oso_el> oso_vector;
   /* We have to hold on to the symbol table until the call to
@@ -796,10 +796,10 @@
       std::string dsym_filename;
 
       /* Process the normal symbol table first.  */
-      storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
+      storage_needed = bfd_get_symtab_upper_bound (objfile->obfd.get ());
       if (storage_needed < 0)
 	error (_("Can't read symbols from %s: %s"),
-	       bfd_get_filename (objfile->obfd),
+	       bfd_get_filename (objfile->obfd.get ()),
 	       bfd_errmsg (bfd_get_error ()));
 
       if (storage_needed > 0)
@@ -810,12 +810,12 @@
 
 	  minimal_symbol_reader reader (objfile);
 
-	  symcount = bfd_canonicalize_symtab (objfile->obfd,
+	  symcount = bfd_canonicalize_symtab (objfile->obfd.get (),
 					      symbol_table.data ());
 
 	  if (symcount < 0)
 	    error (_("Can't read symbols from %s: %s"),
-		   bfd_get_filename (objfile->obfd),
+		   bfd_get_filename (objfile->obfd.get ()),
 		   bfd_errmsg (bfd_get_error ()));
 
 	  macho_symtab_read (reader, objfile, symcount, symbol_table.data (),
@@ -849,7 +849,7 @@
 	    }
 
 	  /* Add the dsym file as a separate file.  */
-	  symbol_file_add_separate (dsym_bfd.get (), dsym_filename.c_str (),
+	  symbol_file_add_separate (dsym_bfd, dsym_filename.c_str (),
 				    symfile_flags, objfile);
 
 	  /* Don't try to read dwarf2 from main file or shared libraries.  */
@@ -872,7 +872,7 @@
 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
 			bfd_byte *buf)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
 
   /* We're only interested in sections with relocation
      information.  */
@@ -898,7 +898,7 @@
   struct obj_section *osect;
 
   /* Allocate section_offsets.  */
-  objfile->section_offsets.assign (gdb_bfd_count_sections (objfile->obfd), 0);
+  objfile->section_offsets.assign (gdb_bfd_count_sections (objfile->obfd.get ()), 0);
 
   /* This code is run when we first add the objfile with
      symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
diff --git a/gdb/maint.c b/gdb/maint.c
index 2895609..76ac7be 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -452,9 +452,11 @@
   for (objfile *ofile : current_program_space->objfiles ())
     {
       if (ofile->obfd == current_program_space->exec_bfd ())
-	maint_print_all_sections (_("Exec file: "), ofile->obfd, ofile, arg);
+	maint_print_all_sections (_("Exec file: "), ofile->obfd.get (),
+				  ofile, arg);
       else if (opts.all_objects)
-	maint_print_all_sections (_("Object file: "), ofile->obfd, ofile, arg);
+	maint_print_all_sections (_("Object file: "), ofile->obfd.get (),
+				  ofile, arg);
     }
 
   if (core_bfd)
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 001042b..bfd5a6f 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -335,7 +335,7 @@
 		       const struct ecoff_debug_swap *swap,
 		       struct ecoff_debug_info *info)
 {
-  cur_bfd = objfile->obfd;
+  cur_bfd = objfile->obfd.get ();
   debug_swap = swap;
   debug_info = info;
 
@@ -357,7 +357,7 @@
 		 + info->symbolic_header.ifdMax * swap->external_fdr_size);
       fdr_ptr = info->fdr;
       for (; fdr_src < fdr_end; fdr_src += swap->external_fdr_size, fdr_ptr++)
-	(*swap->swap_fdr_in) (objfile->obfd, fdr_src, fdr_ptr);
+	(*swap->swap_fdr_in) (objfile->obfd.get (), fdr_src, fdr_ptr);
     }
 
   psymbol_functions *psf = new psymbol_functions ();
@@ -4764,7 +4764,7 @@
 elfmdebug_build_psymtabs (struct objfile *objfile,
 			  const struct ecoff_debug_swap *swap, asection *sec)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   struct ecoff_debug_info *info;
 
   /* FIXME: It's not clear whether we should be getting minimal symbol
diff --git a/gdb/minidebug.c b/gdb/minidebug.c
index dbbdf85..47928fa 100644
--- a/gdb/minidebug.c
+++ b/gdb/minidebug.c
@@ -268,12 +268,12 @@
   if (objfile->obfd == NULL)
     return NULL;
 
-  section = bfd_get_section_by_name (objfile->obfd, ".gnu_debugdata");
+  section = bfd_get_section_by_name (objfile->obfd.get (), ".gnu_debugdata");
   if (section == NULL)
     return NULL;
 
 #ifdef HAVE_LIBLZMA
-  gdb_bfd_ref_ptr *shared = gnu_debug_key.get (objfile->obfd);
+  gdb_bfd_ref_ptr *shared = gnu_debug_key.get (objfile->obfd.get ());
   if (shared != nullptr)
     return *shared;
 
@@ -291,7 +291,7 @@
       return NULL;
     }
 
-  gnu_debug_key.emplace (objfile->obfd, abfd);
+  gnu_debug_key.emplace (objfile->obfd.get (), abfd);
 
 #else
   warning (_("Cannot parse .gnu_debugdata section; LZMA support was "
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 3f4ad90..c6abec8 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -1062,7 +1062,7 @@
     {
       objfile *objf = current_program_space->symfile_object_file;
       if (objf->obfd != NULL)
-	return bfd_get_symbol_leading_char (objf->obfd);
+	return bfd_get_symbol_leading_char (objf->obfd.get ());
     }
   return 0;
 }
@@ -1178,7 +1178,7 @@
 
   /* It's safe to strip the leading char here once, since the name
      is also stored stripped in the minimal symbol table.  */
-  if (name[0] == get_symbol_leading_char (m_objfile->obfd))
+  if (name[0] == get_symbol_leading_char (m_objfile->obfd.get ()))
     name = name.substr (1);
 
   if (ms_type == mst_file_text && startswith (name, "__gnu_compiled"))
diff --git a/gdb/mipsread.c b/gdb/mipsread.c
index 868b13f..def7f5a 100644
--- a/gdb/mipsread.c
+++ b/gdb/mipsread.c
@@ -67,7 +67,7 @@
 static void
 mipscoff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
 
   minimal_symbol_reader reader (objfile);
 
@@ -175,7 +175,7 @@
 read_alphacoff_dynamic_symtab (minimal_symbol_reader &reader,
 			       struct objfile *objfile)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   struct alphacoff_dynsecinfo si;
   int sym_count;
   int i;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 7759311..3db9135a6 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -147,7 +147,7 @@
 void
 set_objfile_per_bfd (struct objfile *objfile)
 {
-  objfile->per_bfd = get_objfile_bfd_data (objfile->obfd);
+  objfile->per_bfd = get_objfile_bfd_data (objfile->obfd.get ());
 }
 
 /* Set the objfile's per-BFD notion of the "main" name and
@@ -284,20 +284,24 @@
 void
 build_objfile_section_table (struct objfile *objfile)
 {
-  int count = gdb_bfd_count_sections (objfile->obfd);
+  int count = gdb_bfd_count_sections (objfile->obfd.get ());
 
   objfile->sections = OBSTACK_CALLOC (&objfile->objfile_obstack,
 				      count,
 				      struct obj_section);
   objfile->sections_end = (objfile->sections + count);
   for (asection *sect : gdb_bfd_sections (objfile->obfd))
-    add_to_objfile_sections (objfile->obfd, sect, objfile, 0);
+    add_to_objfile_sections (objfile->obfd.get (), sect, objfile, 0);
 
   /* See gdb_bfd_section_index.  */
-  add_to_objfile_sections (objfile->obfd, bfd_com_section_ptr, objfile, 1);
-  add_to_objfile_sections (objfile->obfd, bfd_und_section_ptr, objfile, 1);
-  add_to_objfile_sections (objfile->obfd, bfd_abs_section_ptr, objfile, 1);
-  add_to_objfile_sections (objfile->obfd, bfd_ind_section_ptr, objfile, 1);
+  add_to_objfile_sections (objfile->obfd.get (), bfd_com_section_ptr,
+			   objfile, 1);
+  add_to_objfile_sections (objfile->obfd.get (), bfd_und_section_ptr,
+			   objfile, 1);
+  add_to_objfile_sections (objfile->obfd.get (), bfd_abs_section_ptr,
+			   objfile, 1);
+  add_to_objfile_sections (objfile->obfd.get (), bfd_ind_section_ptr,
+			   objfile, 1);
 }
 
 /* Given a pointer to an initialized bfd (ABFD) and some flag bits,
@@ -313,10 +317,10 @@
    requests for specific operations.  Other bits like OBJF_SHARED are
    simply copied through to the new objfile flags member.  */
 
-objfile::objfile (bfd *abfd, const char *name, objfile_flags flags_)
+objfile::objfile (gdb_bfd_ref_ptr bfd_, const char *name, objfile_flags flags_)
   : flags (flags_),
     pspace (current_program_space),
-    obfd (abfd)
+    obfd (std::move (bfd_))
 {
   const char *expanded_name;
 
@@ -327,7 +331,7 @@
   std::string name_holder;
   if (name == NULL)
     {
-      gdb_assert (abfd == NULL);
+      gdb_assert (obfd == nullptr);
       gdb_assert ((flags & OBJF_NOT_FILENAME) != 0);
       expanded_name = "<<anonymous objfile>>";
     }
@@ -345,16 +349,15 @@
      that any data that is reference is saved in the per-objfile data
      region.  */
 
-  gdb_bfd_ref (abfd);
-  if (abfd != NULL)
+  if (obfd != nullptr)
     {
-      mtime = bfd_get_mtime (abfd);
+      mtime = bfd_get_mtime (obfd.get ());
 
       /* Build section table.  */
       build_objfile_section_table (this);
     }
 
-  per_bfd = get_objfile_bfd_data (abfd);
+  per_bfd = get_objfile_bfd_data (obfd.get ());
 }
 
 /* If there is a valid and known entry point, function fills *ENTRY_P with it
@@ -454,10 +457,10 @@
 /* See objfiles.h.  */
 
 objfile *
-objfile::make (bfd *bfd_, const char *name_, objfile_flags flags_,
+objfile::make (gdb_bfd_ref_ptr bfd_, const char *name_, objfile_flags flags_,
 	       objfile *parent)
 {
-  objfile *result = new objfile (bfd_, name_, flags_);
+  objfile *result = new objfile (std::move (bfd_), name_, flags_);
   if (parent != nullptr)
     add_separate_debug_objfile (result, parent);
 
@@ -556,9 +559,7 @@
   if (sf != NULL)
     (*sf->sym_finish) (this);
 
-  if (obfd)
-    gdb_bfd_unref (obfd);
-  else
+  if (obfd == nullptr)
     delete per_bfd;
 
   /* Before the symbol table code was redone to make it easier to
@@ -709,7 +710,7 @@
     {
       int idx = s - objfile->sections;
 
-      exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
+      exec_set_section_address (bfd_get_filename (objfile->obfd.get ()), idx,
 				s->addr ());
     }
 
@@ -745,10 +746,10 @@
       /* Here OBJFILE_ADDRS contain the correct absolute addresses, the
 	 relative ones must be already created according to debug_objfile.  */
 
-      addr_info_make_relative (&objfile_addrs, debug_objfile->obfd);
+      addr_info_make_relative (&objfile_addrs, debug_objfile->obfd.get ());
 
       gdb_assert (debug_objfile->section_offsets.size ()
-		  == gdb_bfd_count_sections (debug_objfile->obfd));
+		  == gdb_bfd_count_sections (debug_objfile->obfd.get ()));
       section_offsets new_debug_offsets
 	(debug_objfile->section_offsets.size ());
       relative_addr_info_to_section_offsets (new_debug_offsets, objfile_addrs);
@@ -1130,7 +1131,7 @@
   alloc_size = 0;
   for (objfile *objfile : pspace->objfiles ())
     ALL_OBJFILE_OSECTIONS (objfile, s)
-      if (insert_section_p (objfile->obfd, s->the_bfd_section))
+      if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
 	alloc_size += 1;
 
   /* This happens on detach/attach (e.g. in gdb.base/attach.exp).  */
@@ -1146,7 +1147,7 @@
   i = 0;
   for (objfile *objfile : pspace->objfiles ())
     ALL_OBJFILE_OSECTIONS (objfile, s)
-      if (insert_section_p (objfile->obfd, s->the_bfd_section))
+      if (insert_section_p (objfile->obfd.get (), s->the_bfd_section))
 	map[i++] = s;
 
   std::sort (map, map + alloc_size, sort_cmp);
@@ -1322,7 +1323,7 @@
 objfile_name (const struct objfile *objfile)
 {
   if (objfile->obfd != NULL)
-    return bfd_get_filename (objfile->obfd);
+    return bfd_get_filename (objfile->obfd.get ());
 
   return objfile->original_name;
 }
@@ -1333,7 +1334,7 @@
 objfile_filename (const struct objfile *objfile)
 {
   if (objfile->obfd != NULL)
-    return bfd_get_filename (objfile->obfd);
+    return bfd_get_filename (objfile->obfd.get ());
 
   return NULL;
 }
@@ -1352,7 +1353,7 @@
 objfile_flavour_name (struct objfile *objfile)
 {
   if (objfile->obfd != NULL)
-    return bfd_flavour_name (bfd_get_flavour (objfile->obfd));
+    return bfd_flavour_name (bfd_get_flavour (objfile->obfd.get ()));
   return NULL;
 }
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index e724a4e..eb85ed4 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -401,7 +401,7 @@
 private:
 
   /* The only way to create an objfile is to call objfile::make.  */
-  objfile (bfd *, const char *, objfile_flags);
+  objfile (gdb_bfd_ref_ptr, const char *, objfile_flags);
 
 public:
 
@@ -414,8 +414,8 @@
   ~objfile ();
 
   /* Create an objfile.  */
-  static objfile *make (bfd *bfd_, const char *name_, objfile_flags flags_,
-			objfile *parent = nullptr);
+  static objfile *make (gdb_bfd_ref_ptr bfd_, const char *name_,
+			objfile_flags flags_, objfile *parent = nullptr);
 
   /* Remove an objfile from the current program space, and free
      it.  */
@@ -597,7 +597,7 @@
        section.  */
     gdb_assert (section->owner == nullptr || section->owner == this->obfd);
 
-    int idx = gdb_bfd_section_index (this->obfd, section);
+    int idx = gdb_bfd_section_index (this->obfd.get (), section);
     return this->section_offsets[idx];
   }
 
@@ -608,7 +608,7 @@
        section.  */
     gdb_assert (section->owner == nullptr || section->owner == this->obfd);
 
-    int idx = gdb_bfd_section_index (this->obfd, section);
+    int idx = gdb_bfd_section_index (this->obfd.get (), section);
     this->section_offsets[idx] = offset;
   }
 
@@ -651,7 +651,7 @@
   /* The object file's BFD.  Can be null if the objfile contains only
      minimal symbols, e.g. the run time common symbols for SunOS4.  */
 
-  bfd *obfd;
+  gdb_bfd_ref_ptr obfd;
 
   /* The per-BFD data.  Note that this is treated specially if OBFD
      is NULL.  */
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index 4cc5702..757f9aa 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -157,7 +157,7 @@
 
   try
     {
-      build_id = build_id_bfd_get (objfile->obfd);
+      build_id = build_id_bfd_get (objfile->obfd.get ());
     }
   catch (const gdb_exception &except)
     {
@@ -448,7 +448,7 @@
     {
       gdb_bfd_ref_ptr abfd (symfile_bfd_open (file_name));
 
-      symbol_file_add_separate (abfd.get (), file_name, 0, obj->objfile);
+      symbol_file_add_separate (abfd, file_name, 0, obj->objfile);
     }
   catch (const gdb_exception &except)
     {
@@ -625,7 +625,7 @@
       /* Don't return separate debug files.  */
       if (objfile->separate_debug_objfile_backlink != NULL)
 	continue;
-      obfd_build_id = build_id_bfd_get (objfile->obfd);
+      obfd_build_id = build_id_bfd_get (objfile->obfd.get ());
       if (obfd_build_id == NULL)
 	continue;
       if (objfpy_build_id_matches (obfd_build_id, build_id))
diff --git a/gdb/remote.c b/gdb/remote.c
index 49d26c2..04b283f 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4321,7 +4321,7 @@
   objfile *objf = current_program_space->symfile_object_file;
   section_offsets offs = objf->section_offsets;
 
-  symfile_segment_data_up data = get_symfile_segment_data (objf->obfd);
+  symfile_segment_data_up data = get_symfile_segment_data (objf->obfd.get ());
   do_segments = (data != NULL);
   do_sections = num_segments == 0;
 
@@ -4356,7 +4356,7 @@
 
   if (do_segments)
     {
-      int ret = symfile_map_offsets_to_segments (objf->obfd,
+      int ret = symfile_map_offsets_to_segments (objf->obfd.get (),
 						 data.get (), offs,
 						 num_segments, segments);
 
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 33b15a9..948bd0f 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -399,7 +399,7 @@
 solib_aix_get_section_offsets (struct objfile *objfile,
 			       lm_info_aix *info)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
 
   section_offsets offsets (objfile->section_offsets.size ());
 
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index a6d6fe0..df86b14 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -958,7 +958,7 @@
   /* Check the main executable.  */
   objfile *objf = current_program_space->symfile_object_file;
   addr = find_canonical_descriptor_in_load_object
-	   (entry_point, got_value, name, objf->obfd,
+	   (entry_point, got_value, name, objf->obfd.get (),
 	    main_executable_lm_info);
 
   /* If descriptor not found via main executable, check each load object
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index f9a43e2..6f8680f 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -2170,7 +2170,7 @@
 	  bfd *tmp_bfd;
 	  CORE_ADDR load_addr;
 
-	  tmp_bfd = os->objfile->obfd;
+	  tmp_bfd = os->objfile->obfd.get ();
 	  load_addr = os->objfile->text_section_offset ();
 
 	  interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
@@ -3149,7 +3149,7 @@
       if (current_objfile == current_program_space->symfile_object_file)
 	abfd = current_program_space->exec_bfd ();
       else
-	abfd = current_objfile->obfd;
+	abfd = current_objfile->obfd.get ();
 
       if (abfd != nullptr
 	  && gdb_bfd_scan_elf_dyntag (DT_SYMBOLIC, abfd, nullptr, nullptr) == 1)
diff --git a/gdb/solib.c b/gdb/solib.c
index b9ddd04..fc07f60 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -764,7 +764,9 @@
 	    {
 	      section_addr_info sap
 		= build_section_addr_info_from_section_table (*so->sections);
-	      so->objfile = symbol_file_add_from_bfd (so->abfd, so->so_name,
+	      gdb_bfd_ref_ptr tmp_bfd
+		(gdb_bfd_ref_ptr::new_reference (so->abfd));
+	      so->objfile = symbol_file_add_from_bfd (tmp_bfd, so->so_name,
 						      flags, &sap,
 						      OBJF_SHARED, NULL);
 	      so->objfile->addr_low = so->addr_low;
diff --git a/gdb/source.c b/gdb/source.c
index 425b02f..3f498d5 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -1203,7 +1203,8 @@
 	      srcpath += s->filename;
 	    }
 
-	  const struct bfd_build_id *build_id = build_id_bfd_get (ofp->obfd);
+	  const struct bfd_build_id *build_id
+	    = build_id_bfd_get (ofp->obfd.get ());
 
 	  /* Query debuginfod for the source file.  */
 	  if (build_id != nullptr && !srcpath.empty ())
diff --git a/gdb/stap-probe.c b/gdb/stap-probe.c
index 8488ddb..2b3eb37 100644
--- a/gdb/stap-probe.c
+++ b/gdb/stap-probe.c
@@ -1566,7 +1566,7 @@
 		   std::vector<std::unique_ptr<probe>> *probesp,
 		   CORE_ADDR base)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   int size = bfd_get_arch_size (abfd) / 8;
   struct gdbarch *gdbarch = objfile->arch ();
   struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
@@ -1678,7 +1678,7 @@
      SystemTap probe's information.  We basically have to count how many
      probes the objfile has, and then fill in the necessary information
      for each one.  */
-  bfd *obfd = objfile->obfd;
+  bfd *obfd = objfile->obfd.get ();
   bfd_vma base;
   struct sdt_note *iter;
   unsigned save_probesp_len = probesp->size ();
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index 73c5ee3..78c0e09 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -119,7 +119,7 @@
   if (from_tty)
     add_flags |= SYMFILE_VERBOSE;
 
-  objf = symbol_file_add_from_bfd (nbfd, bfd_get_filename (nbfd),
+  objf = symbol_file_add_from_bfd (nbfd_holder, bfd_get_filename (nbfd),
 				   add_flags, &sai, OBJF_SHARED, NULL);
 
   current_program_space->add_target_sections (objf);
@@ -144,7 +144,7 @@
 
   /* We need some representative bfd to know the target we are looking at.  */
   if (current_program_space->symfile_object_file != NULL)
-    templ = current_program_space->symfile_object_file->obfd;
+    templ = current_program_space->symfile_object_file->obfd.get ();
   else
     templ = current_program_space->exec_bfd ();
   if (templ == NULL)
diff --git a/gdb/symfile.c b/gdb/symfile.c
index aea8c76..361274e 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -263,7 +263,8 @@
   /* Before reread_symbols gets rewritten it is not safe to call:
      gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
      */
-  section_addr_info sap = build_section_addr_info_from_bfd (objfile->obfd);
+  section_addr_info sap
+    = build_section_addr_info_from_bfd (objfile->obfd.get ());
   for (i = 0; i < sap.size (); i++)
     {
       int sectindex = sap[i].sectindex;
@@ -281,19 +282,19 @@
   asection *sect;
   int i;
 
-  sect = bfd_get_section_by_name (objfile->obfd, ".text");
+  sect = bfd_get_section_by_name (objfile->obfd.get (), ".text");
   if (sect)
     objfile->sect_index_text = sect->index;
 
-  sect = bfd_get_section_by_name (objfile->obfd, ".data");
+  sect = bfd_get_section_by_name (objfile->obfd.get (), ".data");
   if (sect)
     objfile->sect_index_data = sect->index;
 
-  sect = bfd_get_section_by_name (objfile->obfd, ".bss");
+  sect = bfd_get_section_by_name (objfile->obfd.get (), ".bss");
   if (sect)
     objfile->sect_index_bss = sect->index;
 
-  sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
+  sect = bfd_get_section_by_name (objfile->obfd.get (), ".rodata");
   if (sect)
     objfile->sect_index_rodata = sect->index;
 
@@ -626,7 +627,7 @@
 default_symfile_offsets (struct objfile *objfile,
 			 const section_addr_info &addrs)
 {
-  objfile->section_offsets.resize (gdb_bfd_count_sections (objfile->obfd));
+  objfile->section_offsets.resize (gdb_bfd_count_sections (objfile->obfd.get ()));
   relative_addr_info_to_section_offsets (objfile->section_offsets, addrs);
 
   /* For relocatable files, all loadable sections will start at zero.
@@ -634,9 +635,9 @@
      that no loadable sections overlap.  This algorithm is quadratic,
      but the number of sections in a single object file is generally
      small.  */
-  if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
+  if ((bfd_get_file_flags (objfile->obfd.get ()) & (EXEC_P | DYNAMIC)) == 0)
     {
-      bfd *abfd = objfile->obfd;
+      bfd *abfd = objfile->obfd.get ();
       asection *cur_sec;
 
       for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
@@ -652,8 +653,8 @@
 	  /* Pick non-overlapping offsets for sections the user did not
 	     place explicitly.  */
 	  CORE_ADDR lowest = 0;
-	  for (asection *sect : gdb_bfd_sections (objfile->obfd))
-	    place_section (objfile->obfd, sect, objfile->section_offsets,
+	  for (asection *sect : gdb_bfd_sections (objfile->obfd.get ()))
+	    place_section (objfile->obfd.get (), sect, objfile->section_offsets,
 			   lowest);
 
 	  /* Correctly filling in the section offsets is not quite
@@ -786,8 +787,7 @@
 	     virtual section-as-bfd like the bfd filename containing the
 	     section.  Therefore use also non-canonical name form for the same
 	     file containing the section.  */
-	  symbol_file_add_separate (abfd.get (),
-				    bfd_get_filename (abfd.get ()),
+	  symbol_file_add_separate (abfd, bfd_get_filename (abfd.get ()),
 				    add_flags | SYMFILE_NOT_FILENAME, objfile);
 	}
     }
@@ -809,20 +809,20 @@
   /* Save startup file's range of PC addresses to help blockframe.c
      decide where the bottom of the stack is.  */
 
-  if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
+  if (bfd_get_file_flags (objfile->obfd.get ()) & EXEC_P)
     {
       /* Executable file -- record its entry point so we'll recognize
 	 the startup file because it contains the entry point.  */
-      ei->entry_point = bfd_get_start_address (objfile->obfd);
+      ei->entry_point = bfd_get_start_address (objfile->obfd.get ());
       ei->entry_point_p = 1;
     }
-  else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
-	   && bfd_get_start_address (objfile->obfd) != 0)
+  else if (bfd_get_file_flags (objfile->obfd.get ()) & DYNAMIC
+	   && bfd_get_start_address (objfile->obfd.get ()) != 0)
     {
       /* Some shared libraries may have entry points set and be
 	 runnable.  There's no clear way to indicate this, so just check
 	 for values other than zero.  */
-      ei->entry_point = bfd_get_start_address (objfile->obfd);
+      ei->entry_point = bfd_get_start_address (objfile->obfd.get ());
       ei->entry_point_p = 1;
     }
   else
@@ -857,7 +857,7 @@
 				+ bfd_section_size (sect)))
 	    {
 	      ei->the_bfd_section_index
-		= gdb_bfd_section_index (objfile->obfd, sect);
+		= gdb_bfd_section_index (objfile->obfd.get (), sect);
 	      found = 1;
 	      break;
 	    }
@@ -900,14 +900,14 @@
   section_addr_info local_addr;
   const int mainline = add_flags & SYMFILE_MAINLINE;
 
-  objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
+  objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd.get ()));
   objfile->qf.clear ();
 
   if (objfile->sf == NULL)
     {
       /* No symbols to load, but we still need to make sure
 	 that the section_offsets table is allocated.  */
-      int num_sections = gdb_bfd_count_sections (objfile->obfd);
+      int num_sections = gdb_bfd_count_sections (objfile->obfd.get ());
 
       objfile->section_offsets.assign (num_sections, 0);
       return;
@@ -954,7 +954,7 @@
      We no longer warn if the lowest section is not a text segment (as
      happens for the PA64 port.  */
   if (addrs->size () > 0)
-    addr_info_make_relative (addrs, objfile->obfd);
+    addr_info_make_relative (addrs, objfile->obfd.get ());
 
   /* Initialize symbol reading routines for this objfile, allow complaints to
      appear for this new file, and record how verbose to be, then do the
@@ -1033,7 +1033,7 @@
    Upon failure, jumps back to command level (never returns).  */
 
 static struct objfile *
-symbol_file_add_with_addrs (bfd *abfd, const char *name,
+symbol_file_add_with_addrs (const gdb_bfd_ref_ptr &abfd, const char *name,
 			    symfile_add_flags add_flags,
 			    section_addr_info *addrs,
 			    objfile_flags flags, struct objfile *parent)
@@ -1139,7 +1139,7 @@
    see the objfile constructor.  */
 
 void
-symbol_file_add_separate (bfd *bfd, const char *name,
+symbol_file_add_separate (const gdb_bfd_ref_ptr &bfd, const char *name,
 			  symfile_add_flags symfile_flags,
 			  struct objfile *objfile)
 {
@@ -1160,7 +1160,7 @@
    See symbol_file_add_with_addrs's comments for details.  */
 
 struct objfile *
-symbol_file_add_from_bfd (bfd *abfd, const char *name,
+symbol_file_add_from_bfd (const gdb_bfd_ref_ptr &abfd, const char *name,
 			  symfile_add_flags add_flags,
 			  section_addr_info *addrs,
 			  objfile_flags flags, struct objfile *parent)
@@ -1178,7 +1178,7 @@
 {
   gdb_bfd_ref_ptr bfd (symfile_bfd_open (name));
 
-  return symbol_file_add_from_bfd (bfd.get (), name, add_flags, addrs,
+  return symbol_file_add_from_bfd (bfd, name, add_flags, addrs,
 				   flags, NULL);
 }
 
@@ -1289,7 +1289,7 @@
 
   if (bfd_stat (abfd.get (), &abfd_stat) == 0
       && abfd_stat.st_ino != 0
-      && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
+      && bfd_stat (parent_objfile->obfd.get (), &parent_stat) == 0)
     {
       if (abfd_stat.st_dev == parent_stat.st_dev
 	  && abfd_stat.st_ino == parent_stat.st_ino)
@@ -1325,7 +1325,7 @@
 
       if (!verified_as_different)
 	{
-	  if (!gdb_bfd_crc (parent_objfile->obfd, &parent_crc))
+	  if (!gdb_bfd_crc (parent_objfile->obfd.get (), &parent_crc))
 	    {
 	      if (separate_debug_file_debug)
 		gdb_printf (gdb_stdlog,
@@ -1516,7 +1516,7 @@
   unsigned long crc32;
 
   gdb::unique_xmalloc_ptr<char> debuglink
-    (bfd_get_debug_link_info (objfile->obfd, &crc32));
+    (bfd_get_debug_link_info (objfile->obfd.get (), &crc32));
 
   if (debuglink == NULL)
     {
@@ -1747,7 +1747,7 @@
 int
 get_section_index (struct objfile *objfile, const char *section_name)
 {
-  asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
+  asection *sect = bfd_get_section_by_name (objfile->obfd.get (), section_name);
 
   if (sect)
     return sect->index;
@@ -2424,7 +2424,7 @@
 
   for (objfile *objfile : current_program_space->objfiles ())
     {
-      if (objfile->obfd == NULL)
+      if (objfile->obfd.get () == NULL)
 	continue;
 
       /* Separate debug objfiles are handled in the main objfile.  */
@@ -2468,12 +2468,12 @@
 	  clear_symtab_users_cleanup defer_clear_users (0);
 
 	  if (current_program_space->exec_bfd () != NULL
-	      && filename_cmp (bfd_get_filename (objfile->obfd),
+	      && filename_cmp (bfd_get_filename (objfile->obfd.get ()),
 			       bfd_get_filename (current_program_space->exec_bfd ())) == 0)
 	    {
 	      /* Reload EXEC_BFD without asking anything.  */
 
-	      exec_file_attach (bfd_get_filename (objfile->obfd), 0);
+	      exec_file_attach (bfd_get_filename (objfile->obfd.get ()), 0);
 	    }
 
 	  /* Keep the calls order approx. the same as in free_objfile.  */
@@ -2504,14 +2504,14 @@
 
 	  /* Clean up any state BFD has sitting around.  */
 	  {
-	    gdb_bfd_ref_ptr obfd (objfile->obfd);
+	    gdb_bfd_ref_ptr obfd = objfile->obfd;
 	    const char *obfd_filename;
 
-	    obfd_filename = bfd_get_filename (objfile->obfd);
+	    obfd_filename = bfd_get_filename (objfile->obfd.get ());
 	    /* Open the new BFD before freeing the old one, so that
 	       the filename remains live.  */
 	    gdb_bfd_ref_ptr temp (gdb_bfd_open (obfd_filename, gnutarget));
-	    objfile->obfd = temp.release ();
+	    objfile->obfd = std::move (temp);
 	    if (objfile->obfd == NULL)
 	      error (_("Can't open %s to read symbols."), obfd_filename);
 	  }
@@ -2519,7 +2519,7 @@
 	  std::string original_name = objfile->original_name;
 
 	  /* bfd_openr sets cacheable to true, which is what we want.  */
-	  if (!bfd_check_format (objfile->obfd, bfd_object))
+	  if (!bfd_check_format (objfile->obfd.get (), bfd_object))
 	    error (_("Can't read symbols from %s: %s."), objfile_name (objfile),
 		   bfd_errmsg (bfd_get_error ()));
 
@@ -2553,7 +2553,7 @@
 	  /* Reset the sym_fns pointer.  The ELF reader can change it
 	     based on whether .gdb_index is present, and we need it to
 	     start over.  PR symtab/15885  */
-	  objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
+	  objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd.get ()));
 	  objfile->qf.clear ();
 
 	  build_objfile_section_table (objfile);
@@ -3674,12 +3674,11 @@
 static void
 symfile_find_segment_sections (struct objfile *objfile)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   int i;
   asection *sect;
 
-  symfile_segment_data_up data
-    = get_symfile_segment_data (objfile->obfd);
+  symfile_segment_data_up data = get_symfile_segment_data (abfd);
   if (data == NULL)
     return;
 
diff --git a/gdb/symfile.h b/gdb/symfile.h
index 5216e85..1d13e82 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -233,12 +233,13 @@
 extern struct objfile *symbol_file_add (const char *, symfile_add_flags,
 					section_addr_info *, objfile_flags);
 
-extern struct objfile *symbol_file_add_from_bfd (bfd *, const char *, symfile_add_flags,
+extern struct objfile *symbol_file_add_from_bfd (const gdb_bfd_ref_ptr &,
+						 const char *, symfile_add_flags,
 						 section_addr_info *,
 						 objfile_flags, struct objfile *parent);
 
-extern void symbol_file_add_separate (bfd *, const char *, symfile_add_flags,
-				      struct objfile *);
+extern void symbol_file_add_separate (const gdb_bfd_ref_ptr &, const char *,
+				      symfile_add_flags, struct objfile *);
 
 extern std::string find_separate_debug_file_by_debuglink (struct objfile *);
 
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index 68b76bf..8508c06 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -116,7 +116,7 @@
   gdb_printf ("\nObject file %s:  ", objfile_name (objfile));
   gdb_printf ("Objfile at %s, bfd at %s, %d minsyms\n\n",
 	      host_address_to_string (objfile),
-	      host_address_to_string (objfile->obfd),
+	      host_address_to_string (objfile->obfd.get ()),
 	      objfile->per_bfd->minimal_symbol_count);
 
   objfile->dump ();
diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index 6be0a7c..b7d6577 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -301,7 +301,7 @@
   *bfd_sect = NULL;
   *secnum = SECT_OFF_TEXT (objfile);
 
-  bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
+  bfd_map_over_sections (objfile->obfd.get (), find_targ_sec, &args);
 }
 
 /* Return the section number (SECT_OFF_*) that N_SCNUM points to.  */
@@ -782,7 +782,7 @@
   else
     limit_offset -= 1;
 
-  abfd = objfile->obfd;
+  abfd = objfile->obfd.get ();
   linesz = coff_data (abfd)->local_linesz;
   ext_lnno = alloca (linesz);
 
@@ -892,7 +892,7 @@
   if (this_symtab_objfile)
     objfile = this_symtab_objfile;
 
-  bfd_coff_swap_sym_in (objfile->obfd, raw_symbol, &symbol);
+  bfd_coff_swap_sym_in (objfile->obfd.get (), raw_symbol, &symbol);
   if (symbol.n_zeroes)
     {
       complaint (_("Unexpected symbol continuation"));
@@ -923,7 +923,7 @@
 static void
 read_xcoff_symtab (struct objfile *objfile, legacy_psymtab *pst)
 {
-  bfd *abfd = objfile->obfd;
+  bfd *abfd = objfile->obfd.get ();
   char *raw_auxptr;		/* Pointer to first raw aux entry for sym.  */
   struct xcoff_symfile_info *xcoff = XCOFF_DATA (objfile);
   char *strtbl = xcoff->strtbl;
@@ -1628,7 +1628,7 @@
       symbol->n_scnum = -1;
       return;
     }
-  bfd_coff_swap_sym_in (this_symtab_objfile->obfd,
+  bfd_coff_swap_sym_in (this_symtab_objfile->obfd.get (),
 			stbl + (symno * local_symesz),
 			symbol);
 }
@@ -1704,7 +1704,7 @@
 gotit:
   /* Take aux entry and return its lineno.  */
   symno++;
-  bfd_coff_swap_aux_in (objfile->obfd, stbl + symno * local_symesz,
+  bfd_coff_swap_aux_in (objfile->obfd.get (), stbl + symno * local_symesz,
 			symbol->n_type, symbol->n_sclass,
 			0, symbol->n_numaux, main_aux);
 
@@ -1989,7 +1989,7 @@
 	  const char **name, char **raw, unsigned int *symnump,
 	  struct objfile *objfile)
 {
-  bfd_coff_swap_sym_in (objfile->obfd, *raw, symbol);
+  bfd_coff_swap_sym_in (objfile->obfd.get (), *raw, symbol);
   if (symbol->n_zeroes)
     {
       /* If it's exactly E_SYMNMLEN characters long it isn't
@@ -2023,7 +2023,7 @@
   *raw += coff_data (objfile->obfd)->local_symesz;
   if (symbol->n_numaux > 0)
     {
-      bfd_coff_swap_aux_in (objfile->obfd, *raw, symbol->n_type,
+      bfd_coff_swap_aux_in (objfile->obfd.get (), *raw, symbol->n_type,
 			    symbol->n_sclass, 0, symbol->n_numaux, aux);
 
       *symnump += symbol->n_numaux;
@@ -2090,7 +2090,7 @@
 
   set_last_source_file (NULL);
 
-  abfd = objfile->obfd;
+  abfd = objfile->obfd.get ();
   next_symbol_text_func = xcoff_next_symbol_text;
 
   sraw_symbol = XCOFF_DATA (objfile)->symtbl;
@@ -2120,7 +2120,7 @@
 	    if (symbol.n_numaux > 1)
 	      {
 		bfd_coff_swap_aux_in
-		  (objfile->obfd,
+		  (objfile->obfd.get (),
 		   sraw_symbol - coff_data (abfd)->local_symesz,
 		   symbol.n_type,
 		   symbol.n_sclass,
@@ -2795,7 +2795,7 @@
   unsigned int size;
 
   info = XCOFF_DATA (objfile);
-  symfile_bfd = abfd = objfile->obfd;
+  symfile_bfd = abfd = objfile->obfd.get ();
   name = objfile_name (objfile);
 
   num_symbols = bfd_get_symcount (abfd);	/* # of symbols */