gdb: remove core_bfd macro

Message ID 20240205211404.127770-1-simon.marchi@efficios.com
State New
Headers
Series gdb: remove core_bfd macro |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Simon Marchi Feb. 5, 2024, 9:13 p.m. UTC
  The core_bfd macro hides a use of current_program_space.  Remove it, so
that we have the opportunity to get the program space from the context,
if possible.  I guess that the macro was introduced at some point to
replace a global variable of the same name without changing all the
uses.

Change-Id: I971a65b29b5e5a5941f3cb7ea234547daa787268
---
 gdb/arch-utils.c      |   2 +-
 gdb/corefile.c        |   6 +-
 gdb/corelow.c         |  96 +++++++++++++------------
 gdb/fbsd-tdep.c       | 160 ++++++++++++++++++++++--------------------
 gdb/gdbcore.h         |   4 --
 gdb/i386-fbsd-tdep.c  |   3 +-
 gdb/i386-linux-tdep.c |   3 +-
 gdb/linux-tdep.c      |  41 ++++++-----
 gdb/linux-thread-db.c |   2 +-
 gdb/maint.c           |   5 +-
 gdb/progspace.h       |   7 +-
 gdb/record-full.c     |  41 ++++++-----
 gdb/rs6000-aix-tdep.c |   8 ++-
 gdb/sol-thread.c      |   2 +-
 gdb/solib-dsbt.c      |   3 +-
 gdb/solib-frv.c       |   3 +-
 gdb/symfile-mem.c     |   6 +-
 gdb/windows-tdep.c    |   2 +-
 18 files changed, 213 insertions(+), 181 deletions(-)


base-commit: 6cedf3bcbf9d8ac88147fdb9b0765a206191a282
  

Comments

Tom Tromey Feb. 6, 2024, 1:03 a.m. UTC | #1
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:

Simon> The core_bfd macro hides a use of current_program_space.  Remove it, so
Simon> that we have the opportunity to get the program space from the context,
Simon> if possible.  I guess that the macro was introduced at some point to
Simon> replace a global variable of the same name without changing all the
Simon> uses.

Thanks.
Approved-By: Tom Tromey <tom@tromey.com>

These kinds of macros are bad; though at the same time I've added them
on occasion to make some series simpler.  I may have even added this
one, I didn't look.

Tom
  
Simon Marchi Feb. 6, 2024, 3:57 p.m. UTC | #2
On 2/5/24 20:03, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:
> 
> Simon> The core_bfd macro hides a use of current_program_space.  Remove it, so
> Simon> that we have the opportunity to get the program space from the context,
> Simon> if possible.  I guess that the macro was introduced at some point to
> Simon> replace a global variable of the same name without changing all the
> Simon> uses.
> 
> Thanks.
> Approved-By: Tom Tromey <tom@tromey.com>

Thanks, will push.

> These kinds of macros are bad; though at the same time I've added them
> on occasion to make some series simpler.  I may have even added this
> one, I didn't look.

I understand they help go quicker, but I don't mind doing the legwork
to get rid of them, I think it's worth it in the long run.

Simon
  

Patch

diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index 37bd25a251a8..1faa013c16f0 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -590,7 +590,7 @@  gdbarch_update_p (struct gdbarch_info info)
   if (info.abfd == NULL)
     info.abfd = current_program_space->exec_bfd ();
   if (info.abfd == NULL)
-    info.abfd = core_bfd;
+    info.abfd = current_program_space->core_bfd ();
 
   /* Check for the current target description.  */
   if (info.target_desc == NULL)
diff --git a/gdb/corefile.c b/gdb/corefile.c
index bb07ef992a9d..169d4229e9c1 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -64,13 +64,13 @@  reopen_exec_file (void)
 void
 validate_files (void)
 {
-  if (current_program_space->exec_bfd () && core_bfd)
+  if (current_program_space->exec_bfd () && current_program_space->core_bfd ())
     {
-      if (!core_file_matches_executable_p (core_bfd,
+      if (!core_file_matches_executable_p (current_program_space->core_bfd (),
 					   current_program_space->exec_bfd ()))
 	warning (_("core file may not match specified executable file."));
       else if (bfd_get_mtime (current_program_space->exec_bfd ())
-	       > bfd_get_mtime (core_bfd))
+	       > bfd_get_mtime (current_program_space->core_bfd ()))
 	warning (_("exec file is newer than core file."));
     }
 }
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 0b93f3f6590a..43c1f69b1317 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -172,7 +172,7 @@  core_target::core_target ()
 {
   /* Find a first arch based on the BFD.  We need the initial gdbarch so
      we can setup the hooks to find a target description.  */
-  m_core_gdbarch = gdbarch_from_bfd (core_bfd);
+  m_core_gdbarch = gdbarch_from_bfd (current_program_space->core_bfd ());
 
   /* If the arch is able to read a target description from the core, it
      could yield a more specific gdbarch.  */
@@ -181,7 +181,7 @@  core_target::core_target ()
   if (tdesc != nullptr)
     {
       struct gdbarch_info info;
-      info.abfd = core_bfd;
+      info.abfd = current_program_space->core_bfd ();
       info.target_desc = tdesc;
       m_core_gdbarch = gdbarch_find_by_info (info);
     }
@@ -189,10 +189,10 @@  core_target::core_target ()
   if (!m_core_gdbarch
       || !gdbarch_iterate_over_regset_sections_p (m_core_gdbarch))
     error (_("\"%s\": Core file format not supported"),
-	   bfd_get_filename (core_bfd));
+	   bfd_get_filename (current_program_space->core_bfd ()));
 
   /* Find the data section */
-  m_core_section_table = build_section_table (core_bfd);
+  m_core_section_table = build_section_table (current_program_space->core_bfd ());
 
   build_file_mappings ();
 }
@@ -218,7 +218,8 @@  core_target::build_file_mappings ()
 
   /* See linux_read_core_file_mappings() in linux-tdep.c for an example
      read_core_file_mappings method.  */
-  gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
+  gdbarch_read_core_file_mappings (m_core_gdbarch,
+				   current_program_space->core_bfd (),
 
     /* After determining the number of mappings, read_core_file_mappings
        will invoke this lambda.  */
@@ -286,7 +287,7 @@  core_target::build_file_mappings ()
 	    /* Ensure that the bfd will be closed when core_bfd is closed. 
 	       This can be checked before/after a core file detach via
 	       "maint info bfds".  */
-	    gdb_bfd_record_inclusion (core_bfd, bfd);
+	    gdb_bfd_record_inclusion (current_program_space->core_bfd (), bfd);
 	    bfd_map[filename] = bfd;
 	  }
 
@@ -326,7 +327,7 @@  core_target::build_file_mappings ()
 void
 core_target::clear_core ()
 {
-  if (core_bfd)
+  if (current_program_space->core_bfd () != nullptr)
     {
       switch_to_no_thread ();    /* Avoid confusion from thread
 				    stuff.  */
@@ -397,10 +398,10 @@  core_file_command (const char *filename, int from_tty)
 
   if (filename == NULL)
     {
-      if (core_bfd != NULL)
+      if (current_program_space->core_bfd () != nullptr)
 	{
 	  target_detach (current_inferior (), from_tty);
-	  gdb_assert (core_bfd == NULL);
+	  gdb_assert (current_program_space->core_bfd () == nullptr);
 	}
       else
 	maybe_say_no_core_file_now (from_tty);
@@ -467,7 +468,7 @@  rename_vmcore_idle_reg_sections (bfd *abfd, inferior *inf)
   /* Look for all the .reg sections.  Record the section object and the
      lwpid which is extracted from the section name.  Spot if any have an
      lwpid of zero.  */
-  for (asection *sect : gdb_bfd_sections (core_bfd))
+  for (asection *sect : gdb_bfd_sections (current_program_space->core_bfd ()))
     {
       if (startswith (bfd_section_name (sect), ".reg/"))
 	{
@@ -500,7 +501,7 @@  rename_vmcore_idle_reg_sections (bfd *abfd, inferior *inf)
   std::string replacement_lwpid_str;
   auto iter = sections_and_lwpids.begin ();
   int replacement_lwpid = 0;
-  for (asection *sect : gdb_bfd_sections (core_bfd))
+  for (asection *sect : gdb_bfd_sections (current_program_space->core_bfd ()))
     {
       if (iter != sections_and_lwpids.end () && sect == iter->first)
 	{
@@ -611,7 +612,7 @@  core_target_open (const char *arg, int from_tty)
   target_preopen (from_tty);
   if (!arg)
     {
-      if (core_bfd)
+      if (current_program_space->core_bfd ())
 	error (_("No core file specified.  (Use `detach' "
 		 "to stop debugging a core file.)"));
       else
@@ -662,7 +663,7 @@  core_target_open (const char *arg, int from_tty)
      typically contains more information that helps us determine the
      architecture than a core file.  */
   if (!current_program_space->exec_bfd ())
-    set_gdbarch_from_file (core_bfd);
+    set_gdbarch_from_file (current_program_space->core_bfd ());
 
   current_inferior ()->push_target (std::move (target_holder));
 
@@ -679,7 +680,7 @@  core_target_open (const char *arg, int from_tty)
   /* Find (or fake) the pid for the process in this core file, and
      initialise the current inferior with that pid.  */
   bool fake_pid_p = false;
-  int pid = bfd_core_file_pid (core_bfd);
+  int pid = bfd_core_file_pid (current_program_space->core_bfd ());
   if (pid == 0)
     {
       fake_pid_p = true;
@@ -692,13 +693,14 @@  core_target_open (const char *arg, int from_tty)
   inf->fake_pid_p = fake_pid_p;
 
   /* Rename any .reg/0 sections, giving them each a fake lwpid.  */
-  rename_vmcore_idle_reg_sections (core_bfd, inf);
+  rename_vmcore_idle_reg_sections (current_program_space->core_bfd (), inf);
 
   /* Build up thread list from BFD sections, and possibly set the
      current thread to the .reg/NN section matching the .reg
      section.  */
-  asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg");
-  for (asection *sect : gdb_bfd_sections (core_bfd))
+  asection *reg_sect
+    = bfd_get_section_by_name (current_program_space->core_bfd (), ".reg");
+  for (asection *sect : gdb_bfd_sections (current_program_space->core_bfd ()))
     add_to_thread_list (sect, reg_sect, inf);
 
   if (inferior_ptid == null_ptid)
@@ -718,7 +720,8 @@  core_target_open (const char *arg, int from_tty)
     }
 
   if (current_program_space->exec_bfd () == nullptr)
-    locate_exec_from_corefile_build_id (core_bfd, from_tty);
+    locate_exec_from_corefile_build_id (current_program_space->core_bfd (),
+					from_tty);
 
   post_create_inferior (from_tty);
 
@@ -736,14 +739,14 @@  core_target_open (const char *arg, int from_tty)
       exception_print (gdb_stderr, except);
     }
 
-  p = bfd_core_file_failing_command (core_bfd);
+  p = bfd_core_file_failing_command (current_program_space->core_bfd ());
   if (p)
     gdb_printf (_("Core was generated by `%s'.\n"), p);
 
   /* Clearing any previous state of convenience variables.  */
   clear_exit_convenience_vars ();
 
-  siggy = bfd_core_file_failing_signal (core_bfd);
+  siggy = bfd_core_file_failing_signal (current_program_space->core_bfd ());
   if (siggy > 0)
     {
       gdbarch *core_gdbarch = target->core_gdbarch ();
@@ -846,7 +849,8 @@  core_target::get_core_register_section (struct regcache *regcache,
 
   thread_section_name section_name (name, regcache->ptid ());
 
-  section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
+  section = bfd_get_section_by_name (current_program_space->core_bfd (),
+				     section_name.c_str ());
   if (! section)
     {
       if (required)
@@ -869,8 +873,8 @@  core_target::get_core_register_section (struct regcache *regcache,
     }
 
   gdb::byte_vector contents (size);
-  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
-				 (file_ptr) 0, size))
+  if (!bfd_get_section_contents (current_program_space->core_bfd (), section,
+				 contents.data (), (file_ptr) 0, size))
     {
       warning (_("Couldn't read %s registers from `%s' section in core file."),
 	       human_name, section_name.c_str ());
@@ -953,7 +957,7 @@  core_target::fetch_registers (struct regcache *regcache, int regno)
 void
 core_target::files_info ()
 {
-  print_section_info (&m_core_section_table, core_bfd);
+  print_section_info (&m_core_section_table, current_program_space->core_bfd ());
 }
 
 /* Helper method for core_target::xfer_partial.  */
@@ -1076,7 +1080,8 @@  core_target::xfer_partial (enum target_object object, const char *annex,
 	  struct bfd_section *section;
 	  bfd_size_type size;
 
-	  section = bfd_get_section_by_name (core_bfd, ".auxv");
+	  section = bfd_get_section_by_name (current_program_space->core_bfd (),
+					     ".auxv");
 	  if (section == NULL)
 	    return TARGET_XFER_E_IO;
 
@@ -1089,8 +1094,9 @@  core_target::xfer_partial (enum target_object object, const char *annex,
 
 	  if (size == 0)
 	    return TARGET_XFER_EOF;
-	  if (!bfd_get_section_contents (core_bfd, section, readbuf,
-					 (file_ptr) offset, size))
+	  if (!bfd_get_section_contents (current_program_space->core_bfd (),
+					 section, readbuf, (file_ptr) offset,
+					 size))
 	    {
 	      warning (_("Couldn't read NT_AUXV note in core file."));
 	      return TARGET_XFER_E_IO;
@@ -1111,7 +1117,8 @@  core_target::xfer_partial (enum target_object object, const char *annex,
 	  struct bfd_section *section;
 	  bfd_size_type size;
 
-	  section = bfd_get_section_by_name (core_bfd, ".wcookie");
+	  section = bfd_get_section_by_name (current_program_space->core_bfd (),
+					     ".wcookie");
 	  if (section == NULL)
 	    return TARGET_XFER_E_IO;
 
@@ -1124,8 +1131,9 @@  core_target::xfer_partial (enum target_object object, const char *annex,
 
 	  if (size == 0)
 	    return TARGET_XFER_EOF;
-	  if (!bfd_get_section_contents (core_bfd, section, readbuf,
-					 (file_ptr) offset, size))
+	  if (!bfd_get_section_contents (current_program_space->core_bfd (),
+					 section, readbuf, (file_ptr) offset,
+					 size))
 	    {
 	      warning (_("Couldn't read StackGhost cookie in core file."));
 	      return TARGET_XFER_E_IO;
@@ -1230,22 +1238,22 @@  core_target::read_description ()
 {
   /* First check whether the target wants us to use the corefile target
      description notes.  */
-  if (gdbarch_use_target_description_from_corefile_notes (m_core_gdbarch,
-							  core_bfd))
+  if (gdbarch_use_target_description_from_corefile_notes
+	(m_core_gdbarch, current_program_space->core_bfd ()))
     {
       /* If the core file contains a target description note then go ahead and
 	 use that.  */
       bfd_size_type tdesc_note_size = 0;
       struct bfd_section *tdesc_note_section
-	= bfd_get_section_by_name (core_bfd, ".gdb-tdesc");
+	= bfd_get_section_by_name (current_program_space->core_bfd (), ".gdb-tdesc");
       if (tdesc_note_section != nullptr)
 	tdesc_note_size = bfd_section_size (tdesc_note_section);
       if (tdesc_note_size > 0)
 	{
 	  gdb::char_vector contents (tdesc_note_size + 1);
-	  if (bfd_get_section_contents (core_bfd, tdesc_note_section,
-					contents.data (), (file_ptr) 0,
-					tdesc_note_size))
+	  if (bfd_get_section_contents (current_program_space->core_bfd (),
+					tdesc_note_section, contents.data (),
+					(file_ptr) 0, tdesc_note_size))
 	    {
 	      /* Ensure we have a null terminator.  */
 	      contents[tdesc_note_size] = '\0';
@@ -1267,7 +1275,8 @@  core_target::read_description ()
     {
       const struct target_desc *result;
 
-      result = gdbarch_core_read_description (m_core_gdbarch, this, core_bfd);
+      result = gdbarch_core_read_description
+		 (m_core_gdbarch, this, current_program_space->core_bfd ());
       if (result != nullptr)
 	return result;
     }
@@ -1317,19 +1326,19 @@  core_target::thread_name (struct thread_info *thr)
 bool
 core_target::has_memory ()
 {
-  return (core_bfd != NULL);
+  return current_program_space->core_bfd () != nullptr;
 }
 
 bool
 core_target::has_stack ()
 {
-  return (core_bfd != NULL);
+  return current_program_space->core_bfd () != nullptr;
 }
 
 bool
 core_target::has_registers ()
 {
-  return (core_bfd != NULL);
+  return current_program_space->core_bfd () != nullptr;
 }
 
 /* Implement the to_info_proc method.  */
@@ -1355,7 +1364,8 @@  core_target::supports_memory_tagging ()
   /* Look for memory tag sections.  If they exist, that means this core file
      supports memory tagging.  */
 
-  return (bfd_get_section_by_name (core_bfd, "memtag") != nullptr);
+  return (bfd_get_section_by_name (current_program_space->core_bfd (), "memtag")
+	  != nullptr);
 }
 
 /* Implementation of the "fetch_memtags" target_ops method.  */
@@ -1374,8 +1384,8 @@  core_target::fetch_memtags (CORE_ADDR address, size_t len,
   memtag_section_info info;
   info.memtag_section = nullptr;
 
-  while (get_next_core_memtag_section (core_bfd, info.memtag_section,
-				       address, info))
+  while (get_next_core_memtag_section (current_program_space->core_bfd (),
+				       info.memtag_section, address, info))
   {
     size_t adjusted_length
       = (address + len < info.end_address) ? len : (info.end_address - address);
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index d8fae0786f87..e3ebcb82ed64 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -563,16 +563,16 @@  fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
 	 extract the null-terminated name from the start of the
 	 note.  */
       thread_section_name section_name (".thrmisc", thr->ptid);
+      bfd *cbfd = current_program_space->core_bfd ();
 
-      section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
+      section = bfd_get_section_by_name (cbfd, section_name.c_str ());
       if (section != NULL && bfd_section_size (section) > 0)
 	{
 	  /* Truncate the name if it is longer than "buf".  */
 	  size = bfd_section_size (section);
 	  if (size > sizeof buf - 1)
 	    size = sizeof buf - 1;
-	  if (bfd_get_section_contents (core_bfd, section, buf, (file_ptr) 0,
-					size)
+	  if (bfd_get_section_contents (cbfd, section, buf, (file_ptr) 0, size)
 	      && buf[0] != '\0')
 	    {
 	      buf[size] = '\0';
@@ -581,7 +581,7 @@  fbsd_core_thread_name (struct gdbarch *gdbarch, struct thread_info *thr)
 		 as its thread name instead of an empty name if a name
 		 has not been set explicitly.  Return a NULL name in
 		 that case.  */
-	      if (strcmp (buf, elf_tdata (core_bfd)->core->program) != 0)
+	      if (strcmp (buf, elf_tdata (cbfd)->core->program) != 0)
 		return buf;
 	    }
 	}
@@ -606,12 +606,13 @@  fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
     return -1;
 
   thread_section_name section_name (".note.freebsdcore.lwpinfo", inferior_ptid);
-  asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
+  bfd *cbfd = current_program_space->core_bfd ();
+  asection *section = bfd_get_section_by_name (cbfd, section_name.c_str ());
   if (section == NULL)
     return -1;
 
   gdb_byte buf[4];
-  if (!bfd_get_section_contents (core_bfd, section, buf,
+  if (!bfd_get_section_contents (cbfd, section, buf,
 				 LWPINFO_OFFSET + LWPINFO_PL_FLAGS, 4))
     return -1;
 
@@ -628,7 +629,7 @@  fbsd_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
   else
     siginfo_offset = LWPINFO_OFFSET + LWPINFO64_PL_SIGINFO;
 
-  if (!bfd_get_section_contents (core_bfd, section, readbuf,
+  if (!bfd_get_section_contents (cbfd, section, readbuf,
 				 siginfo_offset + offset, len))
     return -1;
 
@@ -1021,8 +1022,8 @@  fbsd_info_proc_files_entry (int kf_type, int kf_fd, int kf_flags,
 static void
 fbsd_core_info_proc_files (struct gdbarch *gdbarch)
 {
-  asection *section
-    = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
+  bfd *cbfd = current_program_space->core_bfd ();
+  asection *section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
   if (section == NULL)
     {
       warning (_("unable to find open files in core file"));
@@ -1034,7 +1035,7 @@  fbsd_core_info_proc_files (struct gdbarch *gdbarch)
     error (_("malformed core note - too short for header"));
 
   gdb::def_vector<unsigned char> contents (note_size);
-  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
+  if (!bfd_get_section_contents (cbfd, section, contents.data (),
 				 0, note_size))
     error (_("could not get core note contents"));
 
@@ -1048,20 +1049,18 @@  fbsd_core_info_proc_files (struct gdbarch *gdbarch)
 
   while (descdata + KF_PATH < descend)
     {
-      ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
+      ULONGEST structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
       if (structsize < KF_PATH)
 	error (_("malformed core note - file structure too small"));
 
-      LONGEST type = bfd_get_signed_32 (core_bfd, descdata + KF_TYPE);
-      LONGEST fd = bfd_get_signed_32 (core_bfd, descdata + KF_FD);
-      LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KF_FLAGS);
-      LONGEST offset = bfd_get_signed_64 (core_bfd, descdata + KF_OFFSET);
-      LONGEST vnode_type = bfd_get_signed_32 (core_bfd,
-					      descdata + KF_VNODE_TYPE);
-      LONGEST sock_domain = bfd_get_signed_32 (core_bfd,
-					       descdata + KF_SOCK_DOMAIN);
-      LONGEST sock_type = bfd_get_signed_32 (core_bfd, descdata + KF_SOCK_TYPE);
-      LONGEST sock_protocol = bfd_get_signed_32 (core_bfd,
+      LONGEST type = bfd_get_signed_32 (cbfd, descdata + KF_TYPE);
+      LONGEST fd = bfd_get_signed_32 (cbfd, descdata + KF_FD);
+      LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KF_FLAGS);
+      LONGEST offset = bfd_get_signed_64 (cbfd, descdata + KF_OFFSET);
+      LONGEST vnode_type = bfd_get_signed_32 (cbfd, descdata + KF_VNODE_TYPE);
+      LONGEST sock_domain = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_DOMAIN);
+      LONGEST sock_type = bfd_get_signed_32 (cbfd, descdata + KF_SOCK_TYPE);
+      LONGEST sock_protocol = bfd_get_signed_32 (cbfd,
 						 descdata + KF_SOCK_PROTOCOL);
       fbsd_info_proc_files_entry (type, fd, flags, offset, vnode_type,
 				  sock_domain, sock_type, sock_protocol,
@@ -1154,8 +1153,9 @@  fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
   asection *section;
   unsigned char *descdata, *descend;
   size_t note_size;
+  bfd *cbfd = current_program_space->core_bfd ();
 
-  section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.vmmap");
+  section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.vmmap");
   if (section == NULL)
     {
       warning (_("unable to find mappings in core file"));
@@ -1167,8 +1167,7 @@  fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
     error (_("malformed core note - too short for header"));
 
   gdb::def_vector<unsigned char> contents (note_size);
-  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
-				 0, note_size))
+  if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
     error (_("could not get core note contents"));
 
   descdata = contents.data ();
@@ -1180,15 +1179,15 @@  fbsd_core_info_proc_mappings (struct gdbarch *gdbarch)
   fbsd_info_proc_mappings_header (gdbarch_addr_bit (gdbarch));
   while (descdata + KVE_PATH < descend)
     {
-      ULONGEST structsize = bfd_get_32 (core_bfd, descdata + KVE_STRUCTSIZE);
+      ULONGEST structsize = bfd_get_32 (cbfd, descdata + KVE_STRUCTSIZE);
       if (structsize < KVE_PATH)
 	error (_("malformed core note - vmmap entry too small"));
 
-      ULONGEST start = bfd_get_64 (core_bfd, descdata + KVE_START);
-      ULONGEST end = bfd_get_64 (core_bfd, descdata + KVE_END);
-      ULONGEST offset = bfd_get_64 (core_bfd, descdata + KVE_OFFSET);
-      LONGEST flags = bfd_get_signed_32 (core_bfd, descdata + KVE_FLAGS);
-      LONGEST prot = bfd_get_signed_32 (core_bfd, descdata + KVE_PROTECTION);
+      ULONGEST start = bfd_get_64 (cbfd, descdata + KVE_START);
+      ULONGEST end = bfd_get_64 (cbfd, descdata + KVE_END);
+      ULONGEST offset = bfd_get_64 (cbfd, descdata + KVE_OFFSET);
+      LONGEST flags = bfd_get_signed_32 (cbfd, descdata + KVE_FLAGS);
+      LONGEST prot = bfd_get_signed_32 (cbfd, descdata + KVE_PROTECTION);
       fbsd_info_proc_mappings_entry (gdbarch_addr_bit (gdbarch), start, end,
 				     offset, flags, prot, descdata + KVE_PATH);
 
@@ -1205,8 +1204,9 @@  fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
   asection *section;
   unsigned char *descdata, *descend;
   size_t note_size;
+  bfd *cbfd = current_program_space->core_bfd ();
 
-  section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.files");
+  section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.files");
   if (section == NULL)
     return nullptr;
 
@@ -1215,8 +1215,7 @@  fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
     error (_("malformed core note - too short for header"));
 
   gdb::def_vector<unsigned char> contents (note_size);
-  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
-				 0, note_size))
+  if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
     error (_("could not get core note contents"));
 
   descdata = contents.data ();
@@ -1229,12 +1228,12 @@  fbsd_core_vnode_path (struct gdbarch *gdbarch, int fd)
     {
       ULONGEST structsize;
 
-      structsize = bfd_get_32 (core_bfd, descdata + KF_STRUCTSIZE);
+      structsize = bfd_get_32 (cbfd, descdata + KF_STRUCTSIZE);
       if (structsize < KF_PATH)
 	error (_("malformed core note - file structure too small"));
 
-      if (bfd_get_32 (core_bfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
-	  && bfd_get_signed_32 (core_bfd, descdata + KF_FD) == fd)
+      if (bfd_get_32 (cbfd, descdata + KF_TYPE) == KINFO_FILE_TYPE_VNODE
+	  && bfd_get_signed_32 (cbfd, descdata + KF_FD) == fd)
 	{
 	  char *path = (char *) descdata + KF_PATH;
 	  return make_unique_xstrdup (path);
@@ -1251,20 +1250,22 @@  static void
 fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
 			 LONGEST &sec, ULONGEST &usec)
 {
+  bfd *cbfd = current_program_space->core_bfd ();
+
   if (gdbarch_addr_bit (gdbarch) == 64)
     {
-      sec = bfd_get_signed_64 (core_bfd, data);
-      usec = bfd_get_64 (core_bfd, data + 8);
+      sec = bfd_get_signed_64 (cbfd, data);
+      usec = bfd_get_64 (cbfd, data + 8);
     }
-  else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
+  else if (bfd_get_arch (cbfd) == bfd_arch_i386)
     {
-      sec = bfd_get_signed_32 (core_bfd, data);
-      usec = bfd_get_32 (core_bfd, data + 4);
+      sec = bfd_get_signed_32 (cbfd, data);
+      usec = bfd_get_32 (cbfd, data + 4);
     }
   else
     {
-      sec = bfd_get_signed_64 (core_bfd, data);
-      usec = bfd_get_32 (core_bfd, data + 8);
+      sec = bfd_get_signed_64 (cbfd, data);
+      usec = bfd_get_32 (cbfd, data + 8);
     }
 }
 
@@ -1273,10 +1274,11 @@  fbsd_core_fetch_timeval (struct gdbarch *gdbarch, unsigned char *data,
 static void
 fbsd_print_sigset (const char *descr, unsigned char *sigset)
 {
+  bfd *cbfd = current_program_space->core_bfd ();
   gdb_printf ("%s: ", descr);
   for (int i = 0; i < SIG_WORDS; i++)
     gdb_printf ("%08x ",
-		(unsigned int) bfd_get_32 (core_bfd, sigset + i * 4));
+		(unsigned int) bfd_get_32 (cbfd, sigset + i * 4));
   gdb_printf ("\n");
 }
 
@@ -1292,8 +1294,9 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
   size_t note_size;
   ULONGEST value;
   LONGEST sec;
+  bfd *cbfd = current_program_space->core_bfd ();
 
-  section = bfd_get_section_by_name (core_bfd, ".note.freebsdcore.proc");
+  section = bfd_get_section_by_name (cbfd, ".note.freebsdcore.proc");
   if (section == NULL)
     {
       warning (_("unable to find process info in core file"));
@@ -1303,7 +1306,7 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
   addr_bit = gdbarch_addr_bit (gdbarch);
   if (addr_bit == 64)
     kp = &kinfo_proc_layout_64;
-  else if (bfd_get_arch (core_bfd) == bfd_arch_i386)
+  else if (bfd_get_arch (cbfd) == bfd_arch_i386)
     kp = &kinfo_proc_layout_i386;
   else
     kp = &kinfo_proc_layout_32;
@@ -1321,8 +1324,7 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
     error (_("malformed core note - too short"));
 
   gdb::def_vector<unsigned char> contents (note_size);
-  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
-				 0, note_size))
+  if (!bfd_get_section_contents (cbfd, section, contents.data (), 0, note_size))
     error (_("could not get core note contents"));
 
   descdata = contents.data ();
@@ -1331,7 +1333,7 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
   descdata += 4;
 
   /* Verify 'ki_layout' is 0.  */
-  if (bfd_get_32 (core_bfd, descdata + kp->ki_layout) != 0)
+  if (bfd_get_32 (cbfd, descdata + kp->ki_layout) != 0)
     {
       warning (_("unsupported process information in core file"));
       return;
@@ -1339,13 +1341,13 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
 
   gdb_printf ("Name: %.19s\n", descdata + kp->ki_comm);
   gdb_printf ("Process ID: %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pid)));
   gdb_printf ("Parent process: %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ppid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ppid)));
   gdb_printf ("Process group: %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_pgid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_pgid)));
   gdb_printf ("Session id: %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_sid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_sid)));
 
   /* FreeBSD 12.0 and later store a 64-bit dev_t at 'ki_tdev'.  Older
      kernels store a 32-bit dev_t at 'ki_tdev_freebsd11'.  In older
@@ -1353,38 +1355,38 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
      the structure that is cleared to zero.  Assume that a zero value
      in ki_tdev indicates a core dump from an older kernel and use the
      value in 'ki_tdev_freebsd11' instead.  */
-  value = bfd_get_64 (core_bfd, descdata + kp->ki_tdev);
+  value = bfd_get_64 (cbfd, descdata + kp->ki_tdev);
   if (value == 0)
-    value = bfd_get_32 (core_bfd, descdata + kp->ki_tdev_freebsd11);
+    value = bfd_get_32 (cbfd, descdata + kp->ki_tdev_freebsd11);
   gdb_printf ("TTY: %s\n", pulongest (value));
   gdb_printf ("TTY owner process group: %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_tpgid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_tpgid)));
   gdb_printf ("User IDs (real, effective, saved): %s %s %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_ruid)),
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_uid)),
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svuid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_ruid)),
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_uid)),
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svuid)));
   gdb_printf ("Group IDs (real, effective, saved): %s %s %s\n",
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_rgid)),
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_groups)),
-	      pulongest (bfd_get_32 (core_bfd, descdata + kp->ki_svgid)));
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_rgid)),
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_groups)),
+	      pulongest (bfd_get_32 (cbfd, descdata + kp->ki_svgid)));
   gdb_printf ("Groups: ");
-  uint16_t ngroups = bfd_get_16 (core_bfd, descdata + kp->ki_ngroups);
+  uint16_t ngroups = bfd_get_16 (cbfd, descdata + kp->ki_ngroups);
   for (int i = 0; i < ngroups; i++)
     gdb_printf ("%s ",
-		pulongest (bfd_get_32 (core_bfd,
+		pulongest (bfd_get_32 (cbfd,
 				       descdata + kp->ki_groups + i * 4)));
   gdb_printf ("\n");
-  value = bfd_get (long_bit, core_bfd,
+  value = bfd_get (long_bit, cbfd,
 		   descdata + kp->ki_rusage + kp->ru_minflt);
   gdb_printf ("Minor faults (no memory page): %s\n", pulongest (value));
-  value = bfd_get (long_bit, core_bfd,
+  value = bfd_get (long_bit, cbfd,
 		   descdata + kp->ki_rusage_ch + kp->ru_minflt);
   gdb_printf ("Minor faults, children: %s\n", pulongest (value));
-  value = bfd_get (long_bit, core_bfd,
+  value = bfd_get (long_bit, cbfd,
 		   descdata + kp->ki_rusage + kp->ru_majflt);
   gdb_printf ("Major faults (memory page faults): %s\n",
 	      pulongest (value));
-  value = bfd_get (long_bit, core_bfd,
+  value = bfd_get (long_bit, cbfd,
 		   descdata + kp->ki_rusage_ch + kp->ru_majflt);
   gdb_printf ("Major faults, children: %s\n", pulongest (value));
   fbsd_core_fetch_timeval (gdbarch,
@@ -1408,22 +1410,22 @@  fbsd_core_info_proc_status (struct gdbarch *gdbarch)
   fbsd_core_fetch_timeval (gdbarch, descdata + kp->ki_start, sec, value);
   gdb_printf ("Start time: %s.%06d\n", plongest (sec), (int) value);
   gdb_printf ("Virtual memory size: %s kB\n",
-	      pulongest (bfd_get (addr_bit, core_bfd,
+	      pulongest (bfd_get (addr_bit, cbfd,
 				  descdata + kp->ki_size) / 1024));
   gdb_printf ("Data size: %s pages\n",
-	      pulongest (bfd_get (addr_bit, core_bfd,
+	      pulongest (bfd_get (addr_bit, cbfd,
 				  descdata + kp->ki_dsize)));
   gdb_printf ("Stack size: %s pages\n",
-	      pulongest (bfd_get (addr_bit, core_bfd,
+	      pulongest (bfd_get (addr_bit, cbfd,
 				  descdata + kp->ki_ssize)));
   gdb_printf ("Text size: %s pages\n",
-	      pulongest (bfd_get (addr_bit, core_bfd,
+	      pulongest (bfd_get (addr_bit, cbfd,
 				  descdata + kp->ki_tsize)));
   gdb_printf ("Resident set size: %s pages\n",
-	      pulongest (bfd_get (addr_bit, core_bfd,
+	      pulongest (bfd_get (addr_bit, cbfd,
 				  descdata + kp->ki_rssize)));
   gdb_printf ("Maximum RSS: %s pages\n",
-	      pulongest (bfd_get (long_bit, core_bfd,
+	      pulongest (bfd_get (long_bit, cbfd,
 				  descdata + kp->ki_rusage
 				  + kp->ru_maxrss)));
   fbsd_print_sigset ("Ignored Signals", descdata + kp->ki_sigignore);
@@ -1482,7 +1484,8 @@  fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
       return;
     }
 
-  pid = bfd_core_file_pid (core_bfd);
+  bfd *cbfd = current_program_space->core_bfd ();
+  pid = bfd_core_file_pid (cbfd);
   if (pid != 0)
     gdb_printf (_("process %d\n"), pid);
 
@@ -1490,7 +1493,7 @@  fbsd_core_info_proc (struct gdbarch *gdbarch, const char *args,
     {
       const char *cmdline;
 
-      cmdline = bfd_core_file_failing_command (core_bfd);
+      cmdline = bfd_core_file_failing_command (cbfd);
       if (cmdline)
 	gdb_printf ("cmdline = '%s'\n", cmdline);
       else
@@ -2321,7 +2324,8 @@  fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
   if (!target_has_execution ())
     {
       /* Search for the ending address in the NT_PROCSTAT_VMMAP note. */
-      asection *section = bfd_get_section_by_name (core_bfd,
+      bfd *cbfd = current_program_space->core_bfd ();
+      asection *section = bfd_get_section_by_name (cbfd,
 						   ".note.freebsdcore.vmmap");
       if (section == nullptr)
 	return false;
@@ -2331,7 +2335,7 @@  fbsd_vdso_range (struct gdbarch *gdbarch, struct mem_range *range)
 	return false;
 
       gdb::def_vector<unsigned char> contents (note_size);
-      if (!bfd_get_section_contents (core_bfd, section, contents.data (),
+      if (!bfd_get_section_contents (cbfd, section, contents.data (),
 				     0, note_size))
 	return false;
 
diff --git a/gdb/gdbcore.h b/gdb/gdbcore.h
index 3b7eeba63aa4..d6aeb356e377 100644
--- a/gdb/gdbcore.h
+++ b/gdb/gdbcore.h
@@ -120,10 +120,6 @@  extern void write_memory_signed_integer (CORE_ADDR addr, int len,
 
 extern void (*deprecated_file_changed_hook) (const char *filename);
 
-/* Binary File Diddler for the core file.  */
-
-#define core_bfd (current_program_space->cbfd.get ())
-
 /* Whether to open exec and core files read-only or read-write.  */
 
 extern bool write_files;
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index 266e9f257c25..ea1db3abd6ba 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -278,7 +278,8 @@  bool
 i386_fbsd_core_read_x86_xsave_layout (struct gdbarch *gdbarch,
 				      x86_xsave_layout &layout)
 {
-  return i386_fbsd_core_read_xsave_info (core_bfd, layout) != 0;
+  return i386_fbsd_core_read_xsave_info (current_program_space->core_bfd (),
+					 layout) != 0;
 }
 
 /* Implement the core_read_description gdbarch method.  */
diff --git a/gdb/i386-linux-tdep.c b/gdb/i386-linux-tdep.c
index 27aea18c436c..996dda0077f8 100644
--- a/gdb/i386-linux-tdep.c
+++ b/gdb/i386-linux-tdep.c
@@ -675,7 +675,8 @@  bool
 i386_linux_core_read_x86_xsave_layout (struct gdbarch *gdbarch,
 				       x86_xsave_layout &layout)
 {
-  return i386_linux_core_read_xsave_info (core_bfd, layout) != 0;
+  return i386_linux_core_read_xsave_info (current_program_space->core_bfd (),
+					  layout) != 0;
 }
 
 /* See i386-linux-tdep.h.  */
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index a249305e68ca..701e3df7a279 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1149,8 +1149,8 @@  linux_read_core_file_mappings
     }
 
   gdb::byte_vector contents (note_size);
-  if (!bfd_get_section_contents (core_bfd, section, contents.data (),
-				 0, note_size))
+  if (!bfd_get_section_contents (current_program_space->core_bfd (), section,
+				 contents.data (), 0, note_size))
     {
       warning (_("could not get core note contents"));
       return;
@@ -1165,10 +1165,13 @@  linux_read_core_file_mappings
       return;
     }
 
-  ULONGEST count = bfd_get (addr_size_bits, core_bfd, descdata);
+  ULONGEST count = bfd_get (addr_size_bits, current_program_space->core_bfd (),
+			    descdata);
   descdata += addr_size;
 
-  ULONGEST page_size = bfd_get (addr_size_bits, core_bfd, descdata);
+  ULONGEST page_size = bfd_get (addr_size_bits,
+				current_program_space->core_bfd (),
+				descdata);
   descdata += addr_size;
 
   if (note_size < 2 * addr_size + count * 3 * addr_size)
@@ -1215,12 +1218,12 @@  linux_read_core_file_mappings
 
   for (int i = 0; i < count; i++)
     {
-      ULONGEST start = bfd_get (addr_size_bits, core_bfd, descdata);
+      ULONGEST start = bfd_get (addr_size_bits, current_program_space->core_bfd (), descdata);
       descdata += addr_size;
-      ULONGEST end = bfd_get (addr_size_bits, core_bfd, descdata);
+      ULONGEST end = bfd_get (addr_size_bits, current_program_space->core_bfd (), descdata);
       descdata += addr_size;
       ULONGEST file_ofs
-	= bfd_get (addr_size_bits, core_bfd, descdata) * page_size;
+	= bfd_get (addr_size_bits, current_program_space->core_bfd (), descdata) * page_size;
       descdata += addr_size;
       char * filename = filenames;
       filenames += strlen ((char *) filenames) + 1;
@@ -1239,7 +1242,7 @@  linux_read_core_file_mappings
 static void
 linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
 {
-  linux_read_core_file_mappings (gdbarch, core_bfd,
+  linux_read_core_file_mappings (gdbarch, current_program_space->core_bfd (),
     [=] (ULONGEST count)
       {
 	gdb_printf (_("Mapped address spaces:\n\n"));
@@ -1289,9 +1292,9 @@  linux_core_info_proc (struct gdbarch *gdbarch, const char *args,
 
   if (exe_f)
     {
-      const char *exe;
+      const char *exe
+	= bfd_core_file_failing_command (current_program_space->core_bfd ());
 
-      exe = bfd_core_file_failing_command (core_bfd);
       if (exe != NULL)
 	gdb_printf ("exe = '%s'\n", exe);
       else
@@ -1315,11 +1318,14 @@  linux_core_xfer_siginfo (struct gdbarch *gdbarch, gdb_byte *readbuf,
 			 ULONGEST offset, ULONGEST len)
 {
   thread_section_name section_name (".note.linuxcore.siginfo", inferior_ptid);
-  asection *section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
+  asection *section
+    = bfd_get_section_by_name (current_program_space->core_bfd (),
+			       section_name.c_str ());
   if (section == NULL)
     return -1;
 
-  if (!bfd_get_section_contents (core_bfd, section, readbuf, offset, len))
+  if (!bfd_get_section_contents (current_program_space->core_bfd (), section,
+				 readbuf, offset, len))
     return -1;
 
   return len;
@@ -1522,11 +1528,12 @@  linux_process_address_in_memtag_page (CORE_ADDR address)
 static bool
 linux_core_file_address_in_memtag_page (CORE_ADDR address)
 {
-  if (core_bfd == nullptr)
+  if (current_program_space->core_bfd () == nullptr)
     return false;
 
   memtag_section_info info;
-  return get_next_core_memtag_section (core_bfd, nullptr, address, info);
+  return get_next_core_memtag_section (current_program_space->core_bfd (),
+				       nullptr, address, info);
 }
 
 /* See linux-tdep.h.  */
@@ -2413,13 +2420,15 @@  linux_vsyscall_range_raw (struct gdbarch *gdbarch, struct mem_range *range)
       long phdrs_size;
       int num_phdrs, i;
 
-      phdrs_size = bfd_get_elf_phdr_upper_bound (core_bfd);
+      phdrs_size
+	= bfd_get_elf_phdr_upper_bound (current_program_space->core_bfd ());
       if (phdrs_size == -1)
 	return 0;
 
       gdb::unique_xmalloc_ptr<Elf_Internal_Phdr>
 	phdrs ((Elf_Internal_Phdr *) xmalloc (phdrs_size));
-      num_phdrs = bfd_get_elf_phdrs (core_bfd, phdrs.get ());
+      num_phdrs = bfd_get_elf_phdrs (current_program_space->core_bfd (),
+				     phdrs.get ());
       if (num_phdrs == -1)
 	return 0;
 
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index 9c756fefd209..34a39899eeb4 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1220,7 +1220,7 @@  thread_db_load (void)
     return false;
 
   /* Don't attempt to use thread_db for remote targets.  */
-  if (!(target_can_run () || core_bfd))
+  if (!(target_can_run () || current_program_space->core_bfd () != nullptr))
     return false;
 
   if (thread_db_load_search ())
diff --git a/gdb/maint.c b/gdb/maint.c
index b99423fcb3b9..f410cf11e697 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -459,8 +459,9 @@  maintenance_info_sections (const char *arg, int from_tty)
 				  ofile, arg);
     }
 
-  if (core_bfd)
-    maint_print_all_sections (_("Core file: "), core_bfd, nullptr, arg);
+  if (current_program_space->core_bfd () != nullptr)
+    maint_print_all_sections (_("Core file: "),
+			      current_program_space->core_bfd (), nullptr, arg);
 }
 
 /* Implement the "maintenance info target-sections" command.  */
diff --git a/gdb/progspace.h b/gdb/progspace.h
index e3ade5101f94..7f5e23df1264 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -300,9 +300,7 @@  struct program_space
 
   /* Return the exec BFD for this program space.  */
   bfd *exec_bfd () const
-  {
-    return ebfd.get ();
-  }
+  { return ebfd.get (); }
 
   /* Set the exec BFD for this program space to ABFD.  */
   void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
@@ -310,6 +308,9 @@  struct program_space
     ebfd = std::move (abfd);
   }
 
+  bfd *core_bfd () const
+  { return cbfd.get ();  }
+
   /* Reset saved solib data at the start of an solib event.  This lets
      us properly collect the data when calling solib_add, so it can then
      later be printed.  */
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 6abe2b252f50..24058824d749 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -924,7 +924,8 @@  record_full_core_open_1 (const char *name, int from_tty)
   for (i = 0; i < regnum; i ++)
     record_full_core_regbuf->raw_supply (i, *regcache);
 
-  record_full_core_sections = build_section_table (core_bfd);
+  record_full_core_sections
+    = build_section_table (current_program_space->core_bfd ());
 
   current_inferior ()->push_target (&record_full_core_ops);
   record_full_restore ();
@@ -970,7 +971,7 @@  record_full_open (const char *name, int from_tty)
   record_full_list = &record_full_first;
   record_full_list->next = NULL;
 
-  if (core_bfd)
+  if (current_program_space->core_bfd ())
     record_full_core_open_1 (name, from_tty);
   else
     record_full_open_1 (name, from_tty);
@@ -2328,7 +2329,7 @@  record_full_restore (void)
 
   /* We restore the execution log from the open core bfd,
      if there is one.  */
-  if (core_bfd == NULL)
+  if (current_program_space->core_bfd () == nullptr)
     return;
 
   /* "record_full_restore" can only be called when record list is empty.  */
@@ -2338,7 +2339,7 @@  record_full_restore (void)
     gdb_printf (gdb_stdlog, "Restoring recording from core file.\n");
 
   /* Now need to find our special note section.  */
-  osec = bfd_get_section_by_name (core_bfd, "null0");
+  osec = bfd_get_section_by_name (current_program_space->core_bfd (), "null0");
   if (record_debug)
     gdb_printf (gdb_stdlog, "Find precord section %s.\n",
 		osec ? "succeeded" : "failed");
@@ -2349,10 +2350,11 @@  record_full_restore (void)
     gdb_printf (gdb_stdlog, "%s", bfd_section_name (osec));
 
   /* Check the magic code.  */
-  bfdcore_read (core_bfd, osec, &magic, sizeof (magic), &bfd_offset);
+  bfdcore_read (current_program_space->core_bfd (), osec, &magic,
+		sizeof (magic), &bfd_offset);
   if (magic != RECORD_FULL_FILE_MAGIC)
     error (_("Version mis-match or file format error in core file %s."),
-	   bfd_get_filename (core_bfd));
+	   bfd_get_filename (current_program_space->core_bfd ()));
   if (record_debug)
     gdb_printf (gdb_stdlog,
 		"  Reading 4-byte magic cookie "
@@ -2378,21 +2380,23 @@  record_full_restore (void)
 	  /* We are finished when offset reaches osec_size.  */
 	  if (bfd_offset >= osec_size)
 	    break;
-	  bfdcore_read (core_bfd, osec, &rectype, sizeof (rectype), &bfd_offset);
+	  bfdcore_read (current_program_space->core_bfd (), osec, &rectype,
+			sizeof (rectype), &bfd_offset);
 
 	  switch (rectype)
 	    {
 	    case record_full_reg: /* reg */
 	      /* Get register number to regnum.  */
-	      bfdcore_read (core_bfd, osec, &regnum,
+	      bfdcore_read (current_program_space->core_bfd (), osec, &regnum,
 			    sizeof (regnum), &bfd_offset);
 	      regnum = netorder32 (regnum);
 
 	      rec = record_full_reg_alloc (regcache, regnum);
 
 	      /* Get val.  */
-	      bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
-			    rec->u.reg.len, &bfd_offset);
+	      bfdcore_read (current_program_space->core_bfd (), osec,
+			    record_full_get_loc (rec), rec->u.reg.len,
+			    &bfd_offset);
 
 	      if (record_debug)
 		gdb_printf (gdb_stdlog,
@@ -2405,20 +2409,21 @@  record_full_restore (void)
 
 	    case record_full_mem: /* mem */
 	      /* Get len.  */
-	      bfdcore_read (core_bfd, osec, &len,
+	      bfdcore_read (current_program_space->core_bfd (), osec, &len,
 			    sizeof (len), &bfd_offset);
 	      len = netorder32 (len);
 
 	      /* Get addr.  */
-	      bfdcore_read (core_bfd, osec, &addr,
+	      bfdcore_read (current_program_space->core_bfd (), osec, &addr,
 			    sizeof (addr), &bfd_offset);
 	      addr = netorder64 (addr);
 
 	      rec = record_full_mem_alloc (addr, len);
 
 	      /* Get val.  */
-	      bfdcore_read (core_bfd, osec, record_full_get_loc (rec),
-			    rec->u.mem.len, &bfd_offset);
+	      bfdcore_read (current_program_space->core_bfd (), osec,
+			    record_full_get_loc (rec), rec->u.mem.len,
+			    &bfd_offset);
 
 	      if (record_debug)
 		gdb_printf (gdb_stdlog,
@@ -2436,13 +2441,13 @@  record_full_restore (void)
 	      record_full_insn_num ++;
 
 	      /* Get signal value.  */
-	      bfdcore_read (core_bfd, osec, &signal,
+	      bfdcore_read (current_program_space->core_bfd (), osec, &signal,
 			    sizeof (signal), &bfd_offset);
 	      signal = netorder32 (signal);
 	      rec->u.end.sigval = (enum gdb_signal) signal;
 
 	      /* Get insn count.  */
-	      bfdcore_read (core_bfd, osec, &count,
+	      bfdcore_read (current_program_space->core_bfd (), osec, &count,
 			    sizeof (count), &bfd_offset);
 	      count = netorder32 (count);
 	      rec->u.end.insn_num = count;
@@ -2459,7 +2464,7 @@  record_full_restore (void)
 
 	    default:
 	      error (_("Bad entry type in core file %s."),
-		     bfd_get_filename (core_bfd));
+		     bfd_get_filename (current_program_space->core_bfd ()));
 	      break;
 	    }
 
@@ -2489,7 +2494,7 @@  record_full_restore (void)
 
   /* Succeeded.  */
   gdb_printf (_("Restored records from core file %s.\n"),
-	      bfd_get_filename (core_bfd));
+	      bfd_get_filename (current_program_space->core_bfd ()));
 
   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
 }
diff --git a/gdb/rs6000-aix-tdep.c b/gdb/rs6000-aix-tdep.c
index 5c7759567ed6..6fdbf63d6a86 100644
--- a/gdb/rs6000-aix-tdep.c
+++ b/gdb/rs6000-aix-tdep.c
@@ -1336,7 +1336,8 @@  rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
   struct bfd_section *ldinfo_sec;
   int ldinfo_size;
 
-  ldinfo_sec = bfd_get_section_by_name (core_bfd, ".ldinfo");
+  ldinfo_sec = bfd_get_section_by_name (current_program_space->core_bfd (),
+					".ldinfo");
   if (ldinfo_sec == NULL)
     error (_("cannot find .ldinfo section from core file: %s"),
 	   bfd_errmsg (bfd_get_error ()));
@@ -1344,8 +1345,9 @@  rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
 
   gdb::byte_vector ldinfo_buf (ldinfo_size);
 
-  if (! bfd_get_section_contents (core_bfd, ldinfo_sec,
-				  ldinfo_buf.data (), 0, ldinfo_size))
+  if (! bfd_get_section_contents (current_program_space->core_bfd (),
+				  ldinfo_sec, ldinfo_buf.data (), 0,
+				  ldinfo_size))
     error (_("unable to read .ldinfo section from core file: %s"),
 	  bfd_errmsg (bfd_get_error ()));
 
diff --git a/gdb/sol-thread.c b/gdb/sol-thread.c
index d2092f4eed1e..d58e2af0d22e 100644
--- a/gdb/sol-thread.c
+++ b/gdb/sol-thread.c
@@ -606,7 +606,7 @@  check_for_thread_db (void)
   ptid_t ptid;
 
   /* Don't attempt to use thread_db for remote targets.  */
-  if (!(target_can_run () || core_bfd))
+  if (!(target_can_run () || current_program_space->core_bfd () != nullptr))
     return;
 
   /* Do nothing if we couldn't load libthread_db.so.1.  */
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 1dc85450e5f4..f1c10d0f302d 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -532,7 +532,8 @@  dsbt_current_sos (void)
      dsbt_current_sos, and also precedes the call to
      solib_create_inferior_hook.   (See post_create_inferior in
      infcmd.c.)  */
-  if (info->main_executable_lm_info == 0 && core_bfd != NULL)
+  if (info->main_executable_lm_info == 0
+      && current_program_space->core_bfd () != nullptr)
     dsbt_relocate_main_executable ();
 
   /* Locate the address of the first link map struct.  */
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index c425b3376de0..f90a0a218d6c 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -325,7 +325,8 @@  frv_current_sos ()
      frv_current_sos, and also precedes the call to
      solib_create_inferior_hook().   (See post_create_inferior() in
      infcmd.c.)  */
-  if (main_executable_lm_info == 0 && core_bfd != NULL)
+  if (main_executable_lm_info == 0
+      && current_program_space->core_bfd () != nullptr)
     frv_relocate_main_executable ();
 
   /* Fetch the GOT corresponding to the main executable.  */
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index d2c64a2ffd3d..12a5f7d936b2 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -166,9 +166,9 @@  add_vsyscall_page (inferior *inf)
     {
       struct bfd *bfd;
 
-      if (core_bfd != NULL)
-	bfd = core_bfd;
-      else if (current_program_space->exec_bfd () != NULL)
+      if (current_program_space->core_bfd () != nullptr)
+	bfd = current_program_space->core_bfd ();
+      else if (current_program_space->exec_bfd () != nullptr)
 	bfd = current_program_space->exec_bfd ();
       else
        /* FIXME: cagney/2004-05-06: Should not require an existing
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index 2750e663b7f1..19a42a49a982 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -1154,7 +1154,7 @@  windows_core_xfer_shared_libraries (struct gdbarch *gdbarch,
 				    ULONGEST offset, ULONGEST len)
 {
   cpms_data data { gdbarch, "<library-list>\n", 0 };
-  bfd_map_over_sections (core_bfd,
+  bfd_map_over_sections (current_program_space->core_bfd (),
 			 core_process_module_section,
 			 &data);
   data.xml += "</library-list>\n";