diff --git a/gdb/exec.c b/gdb/exec.c
index 0f9f9d076c68..f07d9d3ef338 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -492,8 +492,8 @@ exec_file_attach (const char *filename, int from_tty)
       /* Add the executable's sections to the current address spaces'
 	 list of sections.  This possibly pushes the exec_ops
 	 target.  */
-      current_program_space->add_target_sections (&current_program_space->ebfd,
-						  sections);
+      current_program_space->add_target_sections
+	(current_program_space->ebfd.get (), sections);
 
       /* Tell display code (if any) about the changed file name.  */
       if (deprecated_exec_file_display_hook)
@@ -599,8 +599,8 @@ build_section_table (struct bfd *some_bfd)
    current set of target sections.  */
 
 void
-program_space::add_target_sections (const void *owner,
-				    const std::vector<target_section> &sections)
+program_space::add_target_sections_1
+  (const void *owner, const std::vector<target_section> &sections)
 {
   if (!sections.empty ())
     {
@@ -651,7 +651,7 @@ program_space::add_target_sections (struct objfile *objfile)
    OWNER must be the same value passed to add_target_sections.  */
 
 void
-program_space::remove_target_sections (const void *owner)
+program_space::remove_target_sections_1 (const void *owner)
 {
   gdb_assert (owner != NULL);
 
diff --git a/gdb/progspace.c b/gdb/progspace.c
index cca651fdb6f8..839707e9d71f 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -207,10 +207,11 @@ program_space::exec_close ()
     {
       /* Removing target sections may close the exec_ops target.
 	 Clear ebfd before doing so to prevent recursion.  */
+      bfd *saved_ebfd = ebfd.get ();
       ebfd.reset (nullptr);
       ebfd_mtime = 0;
 
-      remove_target_sections (&ebfd);
+      remove_target_sections (saved_ebfd);
 
       exec_filename.reset (nullptr);
     }
diff --git a/gdb/progspace.h b/gdb/progspace.h
index a480f560a77a..4db544b7d2e6 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -284,12 +284,28 @@ struct program_space
   bool empty ();
 
   /* Remove all target sections owned by OWNER.  */
-  void remove_target_sections (const void *owner);
+  void remove_target_sections (const objfile *owner)
+  { remove_target_sections_1 ((const void *) owner); }
+
+  void remove_target_sections (const shobj *owner)
+  { remove_target_sections_1 ((const void *) owner); }
+
+  void remove_target_sections (const bfd *owner)
+  { remove_target_sections_1 ((const void *) owner); }
 
   /* Add the sections array defined by SECTIONS to the
      current set of target sections.  */
-  void add_target_sections (const void *owner,
-			    const std::vector<target_section> &sections);
+  void add_target_sections (const objfile *owner,
+			    const std::vector<target_section> &sections)
+  { add_target_sections_1 ((const void *) owner, sections); }
+
+  void add_target_sections (const shobj *owner,
+			    const std::vector<target_section> &sections)
+  { add_target_sections_1 ((const void *) owner, sections); }
+
+  void add_target_sections (const bfd *owner,
+			    const std::vector<target_section> &sections)
+  { add_target_sections_1 ((const void *) owner, sections); }
 
   /* Add the sections of OBJFILE to the current set of target
      sections.  They are given OBJFILE as the "owner".  */
@@ -376,6 +392,13 @@ struct program_space
   registry<program_space> registry_fields;
 
 private:
+  /* Helper for the public remove_target_sections methods.  */
+  void remove_target_sections_1 (const void *owner);
+
+  /* Helper for the public add_target_sections methods.  */
+  void add_target_sections_1 (const void *owner,
+			      const std::vector<target_section> &sections);
+
   /* The set of target sections matching the sections mapped into
      this program space.  Managed by both exec_ops and solib.c.  */
   std::vector<target_section> m_target_sections;
diff --git a/gdb/solib.c b/gdb/solib.c
index 71970636dc58..b9fb911a8101 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -1195,7 +1195,7 @@ clear_solib (void)
   current_program_space->so_list.clear_and_dispose ([] (shobj *so)
     {
       notify_solib_unloaded (current_program_space, *so);
-      current_program_space->remove_target_sections (&so);
+      current_program_space->remove_target_sections (so);
       delete so;
     });
 
