[PATCH 0/9] Remove some macros from exec.h and progspace.h

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 0/9] Remove some macros from exec.h and progspace.h

Tom Tromey-2
This series removes some macros from exec.h and progspace.h.  In
particular, macros that hide that some global variable is used are
removed.

I find these macros somewhat obscure.  I think patches #2 and #6 show
why -- it turns out that some code here was easily parameterized and
did not need to use a global, or even save/restore the current program
space.

Let me know what you think.  Like the other series along these lines,
I don't see any need to check them in before the branch is made.

Tom


Reply | Threaded
Open this post in threaded view
|

[PATCH 1/9] Remove exec_filename macro

Tom Tromey-2
This removes the exec_filename macro, replacing it with uses of the
member of current_program_space.  This also renames that member, and
changes it to be a unique pointer.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * progspace.h (struct program_space) <exec_filename>: Rename from
        pspace_exec_filename.  Now a unique_xmalloc_ptr.
        * inferior.c (print_selected_inferior): Update.
        (print_inferior): Update.
        * mi/mi-main.c (print_one_inferior): Update.
        * exec.h (exec_filename): Remove macro.
        * corefile.c (get_exec_file): Update.
        * exec.c (exec_close): Update.
        (exec_file_attach): Update.
        * progspace.c (clone_program_space): Update.
        (print_program_space): Update.
---
 gdb/ChangeLog    | 14 ++++++++++++++
 gdb/corefile.c   |  4 ++--
 gdb/exec.c       | 11 ++++++-----
 gdb/exec.h       |  1 -
 gdb/inferior.c   |  6 +++---
 gdb/mi/mi-main.c |  4 ++--
 gdb/progspace.c  |  8 ++++----
 gdb/progspace.h  |  6 +++---
 8 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/gdb/corefile.c b/gdb/corefile.c
index fed0e4fe8ad..c1eec199342 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -144,8 +144,8 @@ validate_files (void)
 const char *
 get_exec_file (int err)
 {
-  if (exec_filename)
-    return exec_filename;
+  if (current_program_space->exec_filename != nullptr)
+    return current_program_space->exec_filename.get ();
   if (!err)
     return NULL;
 
diff --git a/gdb/exec.c b/gdb/exec.c
index 2ff5846c0e7..8ce7a567df1 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -167,8 +167,7 @@ exec_close (void)
 
       remove_target_sections (&exec_bfd);
 
-      xfree (exec_filename);
-      exec_filename = NULL;
+      current_program_space->exec_filename.reset (nullptr);
     }
 }
 
@@ -487,11 +486,13 @@ exec_file_attach (const char *filename, int from_tty)
 
       /* gdb_realpath_keepfile resolves symlinks on the local
  filesystem and so cannot be used for "target:" files.  */
-      gdb_assert (exec_filename == NULL);
+      gdb_assert (current_program_space->exec_filename == nullptr);
       if (load_via_target)
- exec_filename = xstrdup (bfd_get_filename (exec_bfd));
+ current_program_space->exec_filename
+  = make_unique_xstrdup (bfd_get_filename (exec_bfd));
       else
- exec_filename = gdb_realpath_keepfile (scratch_pathname).release ();
+ current_program_space->exec_filename
+  = gdb_realpath_keepfile (scratch_pathname);
 
       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
  {
diff --git a/gdb/exec.h b/gdb/exec.h
index 54e6ff4d9ba..66fcb1624a4 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -32,7 +32,6 @@ struct objfile;
 
 #define exec_bfd current_program_space->ebfd
 #define exec_bfd_mtime current_program_space->ebfd_mtime
-#define exec_filename current_program_space->pspace_exec_filename
 
 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
    Returns 0 if OK, 1 on error.  */
diff --git a/gdb/inferior.c b/gdb/inferior.c
index f775938721d..5c63dfa0bf1 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -415,7 +415,7 @@ void
 print_selected_inferior (struct ui_out *uiout)
 {
   struct inferior *inf = current_inferior ();
-  const char *filename = inf->pspace->pspace_exec_filename;
+  const char *filename = inf->pspace->exec_filename.get ();
 
   if (filename == NULL)
     filename = _("<noexec>");
@@ -518,8 +518,8 @@ print_inferior (struct ui_out *uiout, const char *requested_inferiors)
       std::string conn = uiout_field_connection (inf->process_target ());
       uiout->field_string ("connection-id", conn.c_str ());
 
-      if (inf->pspace->pspace_exec_filename != NULL)
- uiout->field_string ("exec", inf->pspace->pspace_exec_filename);
+      if (inf->pspace->exec_filename != nullptr)
+ uiout->field_string ("exec", inf->pspace->exec_filename.get ());
       else
  uiout->field_skip ("exec");
 
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index 55bb777ef77..8d4bff47938 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -649,10 +649,10 @@ print_one_inferior (struct inferior *inferior, bool recurse,
       if (inferior->pid != 0)
  uiout->field_signed ("pid", inferior->pid);
 
-      if (inferior->pspace->pspace_exec_filename != NULL)
+      if (inferior->pspace->exec_filename != nullptr)
  {
   uiout->field_string ("executable",
-       inferior->pspace->pspace_exec_filename);
+       inferior->pspace->exec_filename.get ());
  }
 
       if (inferior->pid != 0)
diff --git a/gdb/progspace.c b/gdb/progspace.c
index a0b14a6d2eb..9cd83e55b75 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -228,8 +228,8 @@ clone_program_space (struct program_space *dest, struct program_space *src)
 
   set_current_program_space (dest);
 
-  if (src->pspace_exec_filename != NULL)
-    exec_file_attach (src->pspace_exec_filename, 0);
+  if (src->exec_filename != NULL)
+    exec_file_attach (src->exec_filename.get (), 0);
 
   if (src->symfile_object_file != NULL)
     symbol_file_add_main (objfile_name (src->symfile_object_file),
@@ -311,8 +311,8 @@ print_program_space (struct ui_out *uiout, int requested)
 
       uiout->field_signed ("id", pspace->num);
 
-      if (pspace->pspace_exec_filename)
- uiout->field_string ("exec", pspace->pspace_exec_filename);
+      if (pspace->exec_filename != nullptr)
+ uiout->field_string ("exec", pspace->exec_filename.get ());
       else
  uiout->field_skip ("exec");
 
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 099b4dc0b3a..afccebbc00b 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -284,9 +284,9 @@ struct program_space
   /* The last-modified time, from when the exec was brought in.  */
   long ebfd_mtime = 0;
   /* Similar to bfd_get_filename (exec_bfd) but in original form given
-     by user, without symbolic links and pathname resolved.
-     It needs to be freed by xfree.  It is not NULL iff EBFD is not NULL.  */
-  char *pspace_exec_filename = NULL;
+     by user, without symbolic links and pathname resolved.  It is not
+     NULL iff EBFD is not NULL.  */
+  gdb::unique_xmalloc_ptr<char> exec_filename;
 
   /* Binary file diddling handle for the core file.  */
   gdb_bfd_ref_ptr cbfd;
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 2/9] Change exec_close to be a method on program_space

Tom Tromey-2
In reply to this post by Tom Tromey-2
exec_close uses the current program space, so it seemed cleaner to
change it to be a method on program_space.  This patch makes this
change.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * progspace.c (program_space::exec_close): New method, from
        exec_close in exec.c.
        * exec.c (exec_close): Move to progspace.c.
        (exec_target::close, exec_file_attach): Update.
        * progspace.h (struct program_space) <exec_close>: Declare
        method.
---
 gdb/ChangeLog   |  9 +++++++++
 gdb/exec.c      | 31 ++++---------------------------
 gdb/exec.h      |  2 --
 gdb/progspace.c | 20 ++++++++++++++++++++
 gdb/progspace.h |  3 +++
 5 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/gdb/exec.c b/gdb/exec.c
index 8ce7a567df1..a395b58da82 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -148,29 +148,6 @@ exec_target_open (const char *args, int from_tty)
   exec_file_attach (args, from_tty);
 }
 
-/* Close and clear exec_bfd.  If we end up with no target sections to
-   read memory from, this unpushes the exec_ops target.  */
-
-void
-exec_close (void)
-{
-  if (exec_bfd)
-    {
-      bfd *abfd = exec_bfd;
-
-      gdb_bfd_unref (abfd);
-
-      /* Removing target sections may close the exec_ops target.
- Clear exec_bfd before doing so to prevent recursion.  */
-      exec_bfd = NULL;
-      exec_bfd_mtime = 0;
-
-      remove_target_sections (&exec_bfd);
-
-      current_program_space->exec_filename.reset (nullptr);
-    }
-}
-
 /* This is the target_close implementation.  Clears all target
    sections and closes all executable bfds from all program spaces.  */
 
@@ -183,7 +160,7 @@ exec_target::close ()
     {
       set_current_program_space (ss);
       clear_section_table (current_target_sections);
-      exec_close ();
+      ss->exec_close ();
     }
 }
 
@@ -396,7 +373,7 @@ exec_file_attach (const char *filename, int from_tty)
   gdb_bfd_ref_ptr exec_bfd_holder = gdb_bfd_ref_ptr::new_reference (exec_bfd);
 
   /* Remove any previous exec file.  */
-  exec_close ();
+  current_program_space->exec_close ();
 
   /* Now open and digest the file the user requested, if any.  */
 
@@ -498,7 +475,7 @@ exec_file_attach (const char *filename, int from_tty)
  {
   /* Make sure to close exec_bfd, or else "run" might try to use
      it.  */
-  exec_close ();
+  current_program_space->exec_close ();
   error (_("\"%ps\": not in executable format: %s"),
  styled_string (file_name_style.style (), scratch_pathname),
  gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
@@ -508,7 +485,7 @@ exec_file_attach (const char *filename, int from_tty)
  {
   /* Make sure to close exec_bfd, or else "run" might try to use
      it.  */
-  exec_close ();
+  current_program_space->exec_close ();
   error (_("\"%ps\": can't find the file sections: %s"),
  styled_string (file_name_style.style (), scratch_pathname),
  bfd_errmsg (bfd_get_error ()));
diff --git a/gdb/exec.h b/gdb/exec.h
index 66fcb1624a4..daaf0d0d4a7 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -118,8 +118,6 @@ extern void add_target_sections_of_objfile (struct objfile *objfile);
 extern void print_section_info (struct target_section_table *table,
  bfd *abfd);
 
-extern void exec_close (void);
-
 /* Helper function that attempts to open the symbol file at EXEC_FILE_HOST.
    If successful, it proceeds to add the symbol file as the main symbol file.
 
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 9cd83e55b75..ca32a0a1b8d 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -218,6 +218,26 @@ program_space::solibs () const
   return next_adapter<struct so_list> (this->so_list);
 }
 
+/* See progspace.h.  */
+
+void
+program_space::exec_close ()
+{
+  if (ebfd)
+    {
+      gdb_bfd_unref (ebfd);
+
+      /* Removing target sections may close the exec_ops target.
+ Clear exec_bfd before doing so to prevent recursion.  */
+      ebfd = NULL;
+      ebfd_mtime = 0;
+
+      remove_target_sections (&ebfd);
+
+      exec_filename.reset (nullptr);
+    }
+}
+
 /* Copies program space SRC to DEST.  Copies the main executable file,
    and the main symbol file.  Returns DEST.  */
 
diff --git a/gdb/progspace.h b/gdb/progspace.h
index afccebbc00b..777bef2e38e 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -272,6 +272,9 @@ struct program_space
      for (so_list *so : pspace->solibs ()) { ... }  */
   next_adapter<struct so_list> solibs () const;
 
+  /* Close and clear exec_bfd.  If we end up with no target sections
+     to read memory from, this unpushes the exec_ops target.  */
+  void exec_close ();
 
   /* Unique ID number.  */
   int num = 0;
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 3/9] Remove commented-out code from gcore.c

Tom Tromey-2
In reply to this post by Tom Tromey-2
I found some code in gcore.c that has been commented out since

d3420b2fce5e (Mark Kettenis     2003-09-04 166) #if 1 /* See if this even matters...  */

This patch deletes this code.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * gcore.c (default_gcore_mach): Remove commented-out code.
---
 gdb/ChangeLog |  4 ++++
 gdb/gcore.c   | 12 ------------
 2 files changed, 4 insertions(+), 12 deletions(-)

diff --git a/gdb/gcore.c b/gdb/gcore.c
index 7b653fb74e3..8036ce52d50 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -163,19 +163,7 @@ gcore_command (const char *args, int from_tty)
 static unsigned long
 default_gcore_mach (void)
 {
-#if 1 /* See if this even matters...  */
   return 0;
-#else
-
-  const struct bfd_arch_info *bfdarch = gdbarch_bfd_arch_info (target_gdbarch ());
-
-  if (bfdarch != NULL)
-    return bfdarch->mach;
-  if (exec_bfd == NULL)
-    error (_("Can't find default bfd machine type (need execfile)."));
-
-  return bfd_get_mach (exec_bfd);
-#endif /* 1 */
 }
 
 static enum bfd_architecture
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 4/9] Remove exec_bfd_mtime define

Tom Tromey-2
In reply to this post by Tom Tromey-2
This removes the exec_bfd_mtime define, in favor of directly using the
appropriate member of the current program space.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * source-cache.c (source_cache::get_plain_source_lines): Use
        current_program_space.
        * corefile.c (reopen_exec_file): Use current_program_space.
        * exec.c (exec_file_attach): Use current_program_space.
        * exec.h (exec_bfd_mtime): Remove.
---
 gdb/ChangeLog      | 8 ++++++++
 gdb/corefile.c     | 4 +++-
 gdb/exec.c         | 2 +-
 gdb/exec.h         | 1 -
 gdb/source-cache.c | 2 +-
 5 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/gdb/corefile.c b/gdb/corefile.c
index c1eec199342..1586e9f175a 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -115,7 +115,9 @@ reopen_exec_file (void)
   std::string filename = bfd_get_filename (exec_bfd);
   res = stat (filename.c_str (), &st);
 
-  if (res == 0 && exec_bfd_mtime && exec_bfd_mtime != st.st_mtime)
+  if (res == 0
+      && current_program_space->ebfd_mtime
+      && current_program_space->ebfd_mtime != st.st_mtime)
     exec_file_attach (filename.c_str (), 0);
   else
     /* If we accessed the file since last opening it, close it now;
diff --git a/gdb/exec.c b/gdb/exec.c
index a395b58da82..6ca867ab53e 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -491,7 +491,7 @@ exec_file_attach (const char *filename, int from_tty)
  bfd_errmsg (bfd_get_error ()));
  }
 
-      exec_bfd_mtime = bfd_get_mtime (exec_bfd);
+      current_program_space->ebfd_mtime = bfd_get_mtime (exec_bfd);
 
       validate_files ();
 
diff --git a/gdb/exec.h b/gdb/exec.h
index daaf0d0d4a7..db03f3a24fb 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -31,7 +31,6 @@ struct bfd;
 struct objfile;
 
 #define exec_bfd current_program_space->ebfd
-#define exec_bfd_mtime current_program_space->ebfd_mtime
 
 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
    Returns 0 if OK, 1 on error.  */
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index 9196e3a19e3..c67d087c76d 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -69,7 +69,7 @@ source_cache::get_plain_source_lines (struct symtab *s,
   if (SYMTAB_OBJFILE (s) != NULL && SYMTAB_OBJFILE (s)->obfd != NULL)
     mtime = SYMTAB_OBJFILE (s)->mtime;
   else if (exec_bfd)
-    mtime = exec_bfd_mtime;
+    mtime = current_program_space->ebfd_mtime;
 
   if (mtime && mtime < st.st_mtime)
     warning (_("Source file is more recent than executable."));
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 5/9] Remove current_target_sections macro

Tom Tromey-2
In reply to this post by Tom Tromey-2
This removes the current_target_sections macro, replacing it with uses
of the appropriate member from current_program_space.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * progspace.h (current_target_sections): Remove macro.
        * solib-svr4.c (scan_dyntag): Update.
        * solib-dsbt.c (scan_dyntag): Update.
        * exec.c (exec_target::close): Update.
        (add_target_sections, add_target_sections_of_objfile)
        (remove_target_sections, exec_target::get_section_table)
        (exec_target::files_info, set_section_command)
        (exec_set_section_address, exec_target::has_memory)
        (exec_target::has_memory): Update.
---
 gdb/ChangeLog    | 12 ++++++++++++
 gdb/exec.c       | 23 +++++++++++++----------
 gdb/progspace.h  |  4 ----
 gdb/solib-dsbt.c |  6 +++---
 gdb/solib-svr4.c |  6 +++---
 5 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/gdb/exec.c b/gdb/exec.c
index 6ca867ab53e..e2a0cae787c 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -159,7 +159,7 @@ exec_target::close ()
   for (struct program_space *ss : program_spaces)
     {
       set_current_program_space (ss);
-      clear_section_table (current_target_sections);
+      clear_section_table (&ss->target_sections);
       ss->exec_close ();
     }
 }
@@ -660,7 +660,8 @@ add_target_sections (void *owner,
      struct target_section *sections_end)
 {
   int count;
-  struct target_section_table *table = current_target_sections;
+  struct target_section_table *table
+    = &current_program_space->target_sections;
 
   count = sections_end - sections;
 
@@ -700,7 +701,8 @@ add_target_sections (void *owner,
 void
 add_target_sections_of_objfile (struct objfile *objfile)
 {
-  struct target_section_table *table = current_target_sections;
+  struct target_section_table *table
+    = &current_program_space->target_sections;
   struct obj_section *osect;
   int space;
   unsigned count = 0;
@@ -747,7 +749,8 @@ void
 remove_target_sections (void *owner)
 {
   struct target_section *src, *dest;
-  struct target_section_table *table = current_target_sections;
+  struct target_section_table *table
+    = &current_program_space->target_sections;
 
   gdb_assert (owner != NULL);
 
@@ -1006,7 +1009,7 @@ section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
 struct target_section_table *
 exec_target::get_section_table ()
 {
-  return current_target_sections;
+  return &current_program_space->target_sections;
 }
 
 enum target_xfer_status
@@ -1105,7 +1108,7 @@ void
 exec_target::files_info ()
 {
   if (exec_bfd)
-    print_section_info (current_target_sections, exec_bfd);
+    print_section_info (&current_program_space->target_sections, exec_bfd);
   else
     puts_filtered (_("\t<no file loaded>\n"));
 }
@@ -1131,7 +1134,7 @@ set_section_command (const char *args, int from_tty)
   /* Parse out new virtual address.  */
   secaddr = parse_and_eval_address (args);
 
-  table = current_target_sections;
+  table = &current_program_space->target_sections;
   for (p = table->sections; p < table->sections_end; p++)
     {
       if (!strncmp (secname, bfd_section_name (p->the_bfd_section), seclen)
@@ -1161,7 +1164,7 @@ exec_set_section_address (const char *filename, int index, CORE_ADDR address)
   struct target_section *p;
   struct target_section_table *table;
 
-  table = current_target_sections;
+  table = &current_program_space->target_sections;
   for (p = table->sections; p < table->sections_end; p++)
     {
       if (filename_cmp (filename,
@@ -1179,8 +1182,8 @@ exec_target::has_memory ()
 {
   /* We can provide memory if we have any file/target sections to read
      from.  */
-  return (current_target_sections->sections
-  != current_target_sections->sections_end);
+  return (current_program_space->target_sections.sections
+  != current_program_space->target_sections.sections_end);
 }
 
 char *
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 777bef2e38e..ef39f8a1dab 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -365,10 +365,6 @@ struct address_space
 
 #define symfile_objfile current_program_space->symfile_object_file
 
-/* The set of target sections matching the sections mapped into the
-   current program space.  */
-#define current_target_sections (&current_program_space->target_sections)
-
 /* The list of all program spaces.  There's always at least one.  */
 extern std::vector<struct program_space *>program_spaces;
 
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 94a6ac83754..ae76bb74c05 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -424,12 +424,12 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
   if (sect == NULL)
     return 0;
 
-  for (target_section = current_target_sections->sections;
-       target_section < current_target_sections->sections_end;
+  for (target_section = current_program_space->target_sections->sections;
+       target_section < current_program_space->target_sections->sections_end;
        target_section++)
     if (sect == target_section->the_bfd_section)
       break;
-  if (target_section < current_target_sections->sections_end)
+  if (target_section < current_program_space->target_sections->sections_end)
     dyn_addr = target_section->addr;
   else
     {
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 570450c5400..520d9e8ed12 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -608,12 +608,12 @@ scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
   if (sect == NULL)
     return 0;
 
-  for (target_section = current_target_sections->sections;
-       target_section < current_target_sections->sections_end;
+  for (target_section = current_program_space->target_sections.sections;
+       target_section < current_program_space->target_sections.sections_end;
        target_section++)
     if (sect == target_section->the_bfd_section)
       break;
-  if (target_section < current_target_sections->sections_end)
+  if (target_section < current_program_space->target_sections.sections_end)
     dyn_addr = target_section->addr;
   else
     {
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 6/9] Don't change current program space in exec_target::close

Tom Tromey-2
In reply to this post by Tom Tromey-2
Now that we've removed the macros and changed exec_close to be a
method, it's clear that exec_target::close can operate on program
spaces without changing the current program space.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * exec.c (exec_target::close): Don't change current program
        space.
---
 gdb/ChangeLog | 5 +++++
 gdb/exec.c    | 3 ---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/gdb/exec.c b/gdb/exec.c
index e2a0cae787c..b2c7a58b27f 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -154,11 +154,8 @@ exec_target_open (const char *args, int from_tty)
 void
 exec_target::close ()
 {
-  scoped_restore_current_program_space restore_pspace;
-
   for (struct program_space *ss : program_spaces)
     {
-      set_current_program_space (ss);
       clear_section_table (&ss->target_sections);
       ss->exec_close ();
     }
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 7/9] Remove the exec_bfd macro

Tom Tromey-2
In reply to this post by Tom Tromey-2
This removes the exec_bfd macro, in favor of new accessors on
program_space.  In one spot the accessor can't be used; but this is
still a big improvement over the macro, IMO.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * windows-tdep.c (windows_solib_create_inferior_hook): Update.
        * symfile.c (reread_symbols): Update.
        * symfile-mem.c (add_symbol_file_from_memory_command)
        (add_vsyscall_page): Update.
        * source-cache.c (source_cache::get_plain_source_lines): Update.
        * solib-svr4.c (find_program_interpreter, elf_locate_base)
        (svr4_current_sos_direct, svr4_exec_displacement)
        (svr4_relocate_main_executable): Update.
        (svr4_iterate_over_objfiles_in_search_order): Update.
        * solib-frv.c (enable_break2, enable_break): Update.
        * solib-dsbt.c (lm_base, enable_break): Update.
        * solib-darwin.c (find_program_interpreter)
        (darwin_solib_create_inferior_hook): Update.
        * sol-thread.c (rw_common, ps_pdmodel): Update.
        * rs6000-nat.c (rs6000_nat_target::create_inferior): Update.
        * remote.c (compare_sections_command)
        (remote_target::trace_set_readonly_regions): Update.
        * remote-sim.c (get_sim_inferior_data)
        (gdbsim_target::create_inferior, gdbsim_target::create_inferior): Update.
        (gdbsim_target_open, gdbsim_target::files_info): Update.
        * exec.h (exec_bfd): Remove macro.
        * progspace.c (initialize_progspace): Update.
        * proc-service.c (ps_addr_to_core_addr, core_addr_to_ps_addr):
        Update.
        * nto-procfs.c (nto_procfs_target::post_attach)
        (nto_procfs_target::create_inferior): Update.
        * maint.c (maintenance_info_sections): Update.
        * linux-thread-db.c (thread_db_target::get_thread_local_address):
        Update.
        * infcmd.c (post_create_inferior): Update.
        * gcore.c (default_gcore_arch, default_gcore_target): Update.
        (objfile_find_memory_regions): Update.
        * exec.c (validate_exec_file, exec_file_attach)
        (exec_read_partial_read_only, print_section_info): Update.
        * corelow.c (core_target_open): Update.
        * corefile.c (reopen_exec_file, validate_files): Update.
        * arm-tdep.c (gdb_print_insn_arm): Update.
        * arch-utils.c (gdbarch_update_p, default_print_insn): Update.
        * progspace.h (struct program_space) <exec_bfd, set_exec_bfd>: New
        methods.
---
 gdb/ChangeLog         | 43 +++++++++++++++++++++++++++
 gdb/arch-utils.c      |  4 +--
 gdb/arm-tdep.c        |  4 +--
 gdb/corefile.c        | 10 +++----
 gdb/corelow.c         |  4 +--
 gdb/exec.c            | 38 +++++++++++++-----------
 gdb/exec.h            |  2 --
 gdb/gcore.c           | 12 ++++----
 gdb/infcmd.c          |  2 +-
 gdb/linux-thread-db.c |  4 +--
 gdb/maint.c           |  8 ++---
 gdb/nto-procfs.c      |  4 +--
 gdb/proc-service.c    |  6 ++--
 gdb/progspace.c       |  6 ++--
 gdb/progspace.h       | 12 ++++++++
 gdb/remote-sim.c      | 17 ++++++-----
 gdb/remote.c          | 12 ++++----
 gdb/rs6000-nat.c      |  6 ++--
 gdb/sol-thread.c      |  6 ++--
 gdb/solib-darwin.c    | 10 +++----
 gdb/solib-dsbt.c      | 11 +++----
 gdb/solib-frv.c       | 10 ++++---
 gdb/solib-svr4.c      | 68 ++++++++++++++++++++++++++-----------------
 gdb/source-cache.c    |  2 +-
 gdb/symfile-mem.c     |  6 ++--
 gdb/symfile.c         |  4 +--
 gdb/tracefile-tfile.c |  2 +-
 gdb/windows-tdep.c    |  3 +-
 28 files changed, 198 insertions(+), 118 deletions(-)

diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index 13ba50abe61..5cf97374386 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -527,7 +527,7 @@ gdbarch_update_p (struct gdbarch_info info)
 
   /* Check for the current file.  */
   if (info.abfd == NULL)
-    info.abfd = exec_bfd;
+    info.abfd = current_program_space->exec_bfd ();
   if (info.abfd == NULL)
     info.abfd = core_bfd;
 
@@ -989,7 +989,7 @@ default_print_insn (bfd_vma memaddr, disassemble_info *info)
   disassembler_ftype disassemble_fn;
 
   disassemble_fn = disassembler (info->arch, info->endian == BFD_ENDIAN_BIG,
- info->mach, exec_bfd);
+ info->mach, current_program_space->exec_bfd ());
 
   gdb_assert (disassemble_fn != NULL);
   return (*disassemble_fn) (memaddr, info);
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index 9cedcc85755..8bc8fcc62d1 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -7645,9 +7645,9 @@ gdb_print_insn_arm (bfd_vma memaddr, disassemble_info *info)
   /* GDB is able to get bfd_mach from the exe_bfd, info->mach is
      accurate, so mark USER_SPECIFIED_MACHINE_TYPE bit.  Otherwise,
      opcodes/arm-dis.c:print_insn reset info->mach, and it will trigger
-     the assert on the mismatch of info->mach and bfd_get_mach (exec_bfd)
+     the assert on the mismatch of info->mach and bfd_get_mach (current_program_space->exec_bfd ())
      in default_print_insn.  */
-  if (exec_bfd != NULL)
+  if (current_program_space->exec_bfd () != NULL)
     info->flags |= USER_SPECIFIED_MACHINE_TYPE;
 
   return default_print_insn (memaddr, info);
diff --git a/gdb/corefile.c b/gdb/corefile.c
index 1586e9f175a..09fec24c4cd 100644
--- a/gdb/corefile.c
+++ b/gdb/corefile.c
@@ -108,11 +108,11 @@ reopen_exec_file (void)
   struct stat st;
 
   /* Don't do anything if there isn't an exec file.  */
-  if (exec_bfd == NULL)
+  if (current_program_space->exec_bfd () == NULL)
     return;
 
   /* If the timestamp of the exec file has changed, reopen it.  */
-  std::string filename = bfd_get_filename (exec_bfd);
+  std::string filename = bfd_get_filename (current_program_space->exec_bfd ());
   res = stat (filename.c_str (), &st);
 
   if (res == 0
@@ -132,11 +132,11 @@ reopen_exec_file (void)
 void
 validate_files (void)
 {
-  if (exec_bfd && core_bfd)
+  if (current_program_space->exec_bfd () && core_bfd)
     {
-      if (!core_file_matches_executable_p (core_bfd, exec_bfd))
+      if (!core_file_matches_executable_p (core_bfd, current_program_space->exec_bfd ()))
  warning (_("core file may not match specified executable file."));
-      else if (bfd_get_mtime (exec_bfd) > bfd_get_mtime (core_bfd))
+      else if (bfd_get_mtime (current_program_space->exec_bfd ()) > bfd_get_mtime (core_bfd))
  warning (_("exec file is newer than core file."));
     }
 }
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 3958af1e67a..29992a0ae7d 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -333,7 +333,7 @@ core_target_open (const char *arg, int from_tty)
      core file.  We don't do this unconditionally since an exec file
      typically contains more information that helps us determine the
      architecture than a core file.  */
-  if (!exec_bfd)
+  if (!current_program_space->exec_bfd ())
     set_gdbarch_from_file (core_bfd);
 
   push_target (std::move (target_holder));
@@ -373,7 +373,7 @@ core_target_open (const char *arg, int from_tty)
       switch_to_thread (thread);
     }
 
-  if (exec_bfd == nullptr)
+  if (current_program_space->exec_bfd () == nullptr)
     locate_exec_from_corefile_build_id (core_bfd, from_tty);
 
   post_create_inferior (target, from_tty);
diff --git a/gdb/exec.c b/gdb/exec.c
index b2c7a58b27f..75f3990e0f0 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -237,7 +237,8 @@ validate_exec_file (int from_tty)
   reopen_exec_file ();
   current_exec_file = get_exec_file (0);
 
-  const bfd_build_id *exec_file_build_id = build_id_bfd_get (exec_bfd);
+  const bfd_build_id *exec_file_build_id
+    = build_id_bfd_get (current_program_space->exec_bfd ());
   if (exec_file_build_id != nullptr)
     {
       /* Prepend the target prefix, to force gdb_bfd_open to open the
@@ -364,10 +365,10 @@ exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
 void
 exec_file_attach (const char *filename, int from_tty)
 {
-  /* First, acquire a reference to the current exec_bfd.  We release
+  /* First, acquire a reference to the current current_program_space->exec_bfd ().  We release
      this at the end of the function; but acquiring it now lets the
      BFD cache return it if this call refers to the same file.  */
-  gdb_bfd_ref_ptr exec_bfd_holder = gdb_bfd_ref_ptr::new_reference (exec_bfd);
+  gdb_bfd_ref_ptr exec_bfd_holder = gdb_bfd_ref_ptr::new_reference (current_program_space->exec_bfd ());
 
   /* Remove any previous exec file.  */
   current_program_space->exec_close ();
@@ -449,9 +450,9 @@ exec_file_attach (const char *filename, int from_tty)
       FOPEN_RUB, scratch_chan);
       else
  temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
-      exec_bfd = temp.release ();
+      current_program_space->set_exec_bfd (temp.release ());
 
-      if (!exec_bfd)
+      if (!current_program_space->exec_bfd ())
  {
   error (_("\"%ps\": could not open as an executable file: %s."),
  styled_string (file_name_style.style (), scratch_pathname),
@@ -463,12 +464,12 @@ exec_file_attach (const char *filename, int from_tty)
       gdb_assert (current_program_space->exec_filename == nullptr);
       if (load_via_target)
  current_program_space->exec_filename
-  = make_unique_xstrdup (bfd_get_filename (exec_bfd));
+  = make_unique_xstrdup (bfd_get_filename (current_program_space->exec_bfd ()));
       else
  current_program_space->exec_filename
   = gdb_realpath_keepfile (scratch_pathname);
 
-      if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
+      if (!bfd_check_format_matches (current_program_space->exec_bfd (), bfd_object, &matching))
  {
   /* Make sure to close exec_bfd, or else "run" might try to use
      it.  */
@@ -478,7 +479,8 @@ exec_file_attach (const char *filename, int from_tty)
  gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
  }
 
-      if (build_section_table (exec_bfd, &sections, &sections_end))
+      if (build_section_table (current_program_space->exec_bfd (),
+       &sections, &sections_end))
  {
   /* Make sure to close exec_bfd, or else "run" might try to use
      it.  */
@@ -488,16 +490,18 @@ exec_file_attach (const char *filename, int from_tty)
  bfd_errmsg (bfd_get_error ()));
  }
 
-      current_program_space->ebfd_mtime = bfd_get_mtime (exec_bfd);
+      current_program_space->ebfd_mtime
+ = bfd_get_mtime (current_program_space->exec_bfd ());
 
       validate_files ();
 
-      set_gdbarch_from_file (exec_bfd);
+      set_gdbarch_from_file (current_program_space->exec_bfd ());
 
       /* Add the executable's sections to the current address spaces'
  list of sections.  This possibly pushes the exec_ops
  target.  */
-      add_target_sections (&exec_bfd, sections, sections_end);
+      add_target_sections (&current_program_space->ebfd,
+   sections, sections_end);
       xfree (sections);
 
       /* Tell display code (if any) about the changed file name.  */
@@ -811,13 +815,13 @@ exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
   /* It's unduly pedantic to refuse to look at the executable for
      read-only pieces; so do the equivalent of readonly regions aka
      QTro packet.  */
-  if (exec_bfd != NULL)
+  if (current_program_space->exec_bfd () != NULL)
     {
       asection *s;
       bfd_size_type size;
       bfd_vma vma;
 
-      for (s = exec_bfd->sections; s; s = s->next)
+      for (s = current_program_space->exec_bfd ()->sections; s; s = s->next)
  {
   if ((s->flags & SEC_LOAD) == 0
       || (s->flags & SEC_READONLY) == 0)
@@ -833,7 +837,7 @@ exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
       if (amt > len)
  amt = len;
 
-      amt = bfd_get_section_contents (exec_bfd, s,
+      amt = bfd_get_section_contents (current_program_space->exec_bfd (), s,
       readbuf, offset - vma, amt);
 
       if (amt == 0)
@@ -1041,7 +1045,7 @@ print_section_info (struct target_section_table *t, bfd *abfd)
   bfd_get_filename (abfd)));
   wrap_here ("        ");
   printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
-  if (abfd == exec_bfd)
+  if (abfd == current_program_space->exec_bfd ())
     {
       /* gcc-3.4 does not like the initialization in
  <p == t->sections_end>.  */
@@ -1104,8 +1108,8 @@ print_section_info (struct target_section_table *t, bfd *abfd)
 void
 exec_target::files_info ()
 {
-  if (exec_bfd)
-    print_section_info (&current_program_space->target_sections, exec_bfd);
+  if (current_program_space->exec_bfd ())
+    print_section_info (&current_program_space->target_sections, current_program_space->exec_bfd ());
   else
     puts_filtered (_("\t<no file loaded>\n"));
 }
diff --git a/gdb/exec.h b/gdb/exec.h
index db03f3a24fb..c0d34ddcd00 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -30,8 +30,6 @@ struct target_ops;
 struct bfd;
 struct objfile;
 
-#define exec_bfd current_program_space->ebfd
-
 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
    Returns 0 if OK, 1 on error.  */
 
diff --git a/gdb/gcore.c b/gdb/gcore.c
index 8036ce52d50..8ed04569b51 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -173,10 +173,10 @@ default_gcore_arch (void)
 
   if (bfdarch != NULL)
     return bfdarch->arch;
-  if (exec_bfd == NULL)
+  if (current_program_space->exec_bfd () == NULL)
     error (_("Can't find bfd architecture for corefile (need execfile)."));
 
-  return bfd_get_arch (exec_bfd);
+  return bfd_get_arch (current_program_space->exec_bfd ());
 }
 
 static const char *
@@ -186,12 +186,12 @@ default_gcore_target (void)
   if (gdbarch_gcore_bfd_target_p (target_gdbarch ()))
     return gdbarch_gcore_bfd_target (target_gdbarch ());
 
-  /* Otherwise, try to fall back to the exec_bfd target.  This will probably
+  /* Otherwise, try to fall back to the exec target.  This will probably
      not work for non-ELF targets.  */
-  if (exec_bfd == NULL)
+  if (current_program_space->exec_bfd () == NULL)
     return NULL;
   else
-    return bfd_get_target (exec_bfd);
+    return bfd_get_target (current_program_space->exec_bfd ());
 }
 
 /* Derive a reasonable stack segment by unwinding the target stack,
@@ -502,7 +502,7 @@ objfile_find_memory_regions (struct target_ops *self,
      obfd);
 
   /* Make a heap segment.  */
-  if (derive_heap_segment (exec_bfd, &temp_bottom, &temp_top))
+  if (derive_heap_segment (current_program_space->exec_bfd (), &temp_bottom, &temp_top))
     (*func) (temp_bottom, temp_top - temp_bottom,
      1, /* Heap section will be readable.  */
      1, /* Heap section will be writable.  */
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index cfc31699925..e48a1bb2e6e 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -308,7 +308,7 @@ post_create_inferior (struct target_ops *target, int from_tty)
  throw;
     }
 
-  if (exec_bfd)
+  if (current_program_space->exec_bfd ())
     {
       const unsigned solib_add_generation
  = current_program_space->solib_add_generation;
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index b3cda05cd6e..7c288db52a8 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1800,8 +1800,8 @@ thread_db_target::get_thread_local_address (ptid_t ptid,
 
       /* Cast assuming host == target.  Joy.  */
       /* Do proper sign extension for the target.  */
-      gdb_assert (exec_bfd);
-      return (bfd_get_sign_extend_vma (exec_bfd) > 0
+      gdb_assert (current_program_space->exec_bfd ());
+      return (bfd_get_sign_extend_vma (current_program_space->exec_bfd ()) > 0
       ? (CORE_ADDR) (intptr_t) address
       : (CORE_ADDR) (uintptr_t) address);
     }
diff --git a/gdb/maint.c b/gdb/maint.c
index b4890c34cab..51031003b72 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -403,14 +403,14 @@ print_bfd_section_info_maybe_relocated (bfd *abfd,
 static void
 maintenance_info_sections (const char *arg, int from_tty)
 {
-  if (exec_bfd)
+  if (current_program_space->exec_bfd ())
     {
       bool allobj = false;
 
       printf_filtered (_("Exec file:\n"));
-      printf_filtered ("    `%s', ", bfd_get_filename (exec_bfd));
+      printf_filtered ("    `%s', ", bfd_get_filename (current_program_space->exec_bfd ()));
       wrap_here ("        ");
-      printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd));
+      printf_filtered (_("file type %s.\n"), bfd_get_target (current_program_space->exec_bfd ()));
 
       /* Only this function cares about the 'ALLOBJ' argument;
  if 'ALLOBJ' is the only argument, discard it rather than
@@ -427,7 +427,7 @@ maintenance_info_sections (const char *arg, int from_tty)
   if (allobj)
     printf_filtered (_("  Object file: %s\n"),
      bfd_get_filename (ofile->obfd));
-  else if (ofile->obfd != exec_bfd)
+  else if (ofile->obfd != current_program_space->exec_bfd ())
     continue;
 
   maint_print_section_data print_data (ofile, arg, ofile->obfd);
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 91d2cc5914d..90cd67fd776 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -728,7 +728,7 @@ nto_procfs_target::attach (const char *args, int from_tty)
 void
 nto_procfs_target::post_attach (pid_t pid)
 {
-  if (exec_bfd)
+  if (current_program_space->exec_bfd ())
     solib_create_inferior_hook (0);
 }
 
@@ -1322,7 +1322,7 @@ nto_procfs_target::create_inferior (const char *exec_file,
     push_target (ops);
   target_terminal::init ();
 
-  if (exec_bfd != NULL
+  if (current_program_space->exec_bfd () != NULL
       || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
     solib_create_inferior_hook (0);
 }
diff --git a/gdb/proc-service.c b/gdb/proc-service.c
index e0383700a1d..2025f3a66c5 100644
--- a/gdb/proc-service.c
+++ b/gdb/proc-service.c
@@ -42,7 +42,8 @@
 static CORE_ADDR
 ps_addr_to_core_addr (psaddr_t addr)
 {
-  if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
+  if (current_program_space->exec_bfd ()
+      && bfd_get_sign_extend_vma (current_program_space->exec_bfd ()))
     return (intptr_t) addr;
   else
     return (uintptr_t) addr;
@@ -53,7 +54,8 @@ ps_addr_to_core_addr (psaddr_t addr)
 static psaddr_t
 core_addr_to_ps_addr (CORE_ADDR addr)
 {
-  if (exec_bfd && bfd_get_sign_extend_vma (exec_bfd))
+  if (current_program_space->exec_bfd ()
+      && bfd_get_sign_extend_vma (current_program_space->exec_bfd ()))
     return (psaddr_t) (intptr_t) addr;
   else
     return (psaddr_t) (uintptr_t) addr;
diff --git a/gdb/progspace.c b/gdb/progspace.c
index ca32a0a1b8d..49243a6394d 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -228,7 +228,7 @@ program_space::exec_close ()
       gdb_bfd_unref (ebfd);
 
       /* Removing target sections may close the exec_ops target.
- Clear exec_bfd before doing so to prevent recursion.  */
+ Clear ebfd before doing so to prevent recursion.  */
       ebfd = NULL;
       ebfd_mtime = 0;
 
@@ -460,7 +460,7 @@ initialize_progspace (void)
      _initialize_foo routines may need to install their per-pspace
      data keys.  We can only allocate a progspace when all those
      modules have done that.  Do this before
-     initialize_current_architecture, because that accesses exec_bfd,
-     which in turn dereferences current_program_space.  */
+     initialize_current_architecture, because that accesses the ebfd
+     of current_program_space.  */
   current_program_space = new program_space (new_address_space ());
 }
diff --git a/gdb/progspace.h b/gdb/progspace.h
index ef39f8a1dab..5f952ea9b84 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -276,6 +276,18 @@ struct program_space
      to read memory from, this unpushes the exec_ops target.  */
   void exec_close ();
 
+  /* Return the exec BFD for this program space.  */
+  bfd *exec_bfd () const
+  {
+    return ebfd;
+  }
+
+  /* Set the exec BFD for this program space to ABFD.  */
+  void set_exec_bfd (bfd *abfd)
+  {
+    ebfd = abfd;
+  }
+
   /* Unique ID number.  */
   int num = 0;
 
diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c
index 9af6486bca8..41b1214f045 100644
--- a/gdb/remote-sim.c
+++ b/gdb/remote-sim.c
@@ -211,7 +211,8 @@ get_sim_inferior_data (struct inferior *inf, int sim_instance_needed)
   if (sim_instance_needed == SIM_INSTANCE_NEEDED
       && (sim_data == NULL || sim_data->gdbsim_desc == NULL))
     {
-      sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);
+      sim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback,
+   current_program_space->exec_bfd (), sim_argv);
       if (sim_desc == NULL)
  error (_("Unable to create simulator instance for inferior %d."),
        inf->num);
@@ -620,7 +621,7 @@ gdbsim_target::create_inferior (const char *exec_file,
   char *arg_buf;
   const char *args = allargs.c_str ();
 
-  if (exec_file == 0 || exec_bfd == 0)
+  if (exec_file == 0 || current_program_space->exec_bfd () == 0)
     warning (_("No executable file specified."));
   if (!sim_data->program_loaded)
     warning (_("No program loaded."));
@@ -648,7 +649,8 @@ gdbsim_target::create_inferior (const char *exec_file,
       built_argv.reset (arg_buf);
     }
 
-  if (sim_create_inferior (sim_data->gdbsim_desc, exec_bfd,
+  if (sim_create_inferior (sim_data->gdbsim_desc,
+   current_program_space->exec_bfd (),
    built_argv.get (), env)
       != SIM_RC_OK)
     error (_("Unable to create sim inferior."));
@@ -738,7 +740,8 @@ gdbsim_target_open (const char *args, int from_tty)
   sim_argv = argv.release ();
 
   init_callbacks ();
-  gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback, exec_bfd, sim_argv);
+  gdbsim_desc = sim_open (SIM_OPEN_DEBUG, &gdb_callback,
+  current_program_space->exec_bfd (), sim_argv);
 
   if (gdbsim_desc == 0)
     {
@@ -1103,13 +1106,13 @@ gdbsim_target::files_info ()
     = get_sim_inferior_data (current_inferior (), SIM_INSTANCE_NEEDED);
   const char *file = "nothing";
 
-  if (exec_bfd)
-    file = bfd_get_filename (exec_bfd);
+  if (current_program_space->exec_bfd ())
+    file = bfd_get_filename (current_program_space->exec_bfd ());
 
   if (remote_debug)
     fprintf_unfiltered (gdb_stdlog, "gdbsim_files_info: file \"%s\"\n", file);
 
-  if (exec_bfd)
+  if (current_program_space->exec_bfd ())
     {
       fprintf_unfiltered (gdb_stdlog, "\tAttached to %s running program %s\n",
   target_shortname, file);
diff --git a/gdb/remote.c b/gdb/remote.c
index 59075cb09f2..d0836fff6e5 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -56,7 +56,7 @@
 #include <signal.h>
 #include "serial.h"
 
-#include "gdbcore.h" /* for exec_bfd */
+#include "gdbcore.h"
 
 #include "remote-fileio.h"
 #include "gdb/fileio.h"
@@ -10767,7 +10767,7 @@ compare_sections_command (const char *args, int from_tty)
   int res;
   int read_only = 0;
 
-  if (!exec_bfd)
+  if (!current_program_space->exec_bfd ())
     error (_("command cannot be used without an exec file"));
 
   if (args != NULL && strcmp (args, "-r") == 0)
@@ -10776,7 +10776,7 @@ compare_sections_command (const char *args, int from_tty)
       args = NULL;
     }
 
-  for (s = exec_bfd->sections; s; s = s->next)
+  for (s = current_program_space->exec_bfd ()->sections; s; s = s->next)
     {
       if (!(s->flags & SEC_LOAD))
  continue; /* Skip non-loadable section.  */
@@ -10796,7 +10796,7 @@ compare_sections_command (const char *args, int from_tty)
       lma = s->lma;
 
       gdb::byte_vector sectdata (size);
-      bfd_get_section_contents (exec_bfd, s, sectdata.data (), 0, size);
+      bfd_get_section_contents (current_program_space->exec_bfd (), s, sectdata.data (), 0, size);
 
       res = target_verify_memory (sectdata.data (), lma, size);
 
@@ -13171,14 +13171,14 @@ remote_target::trace_set_readonly_regions ()
   int anysecs = 0;
   int offset = 0;
 
-  if (!exec_bfd)
+  if (!current_program_space->exec_bfd ())
     return; /* No information to give.  */
 
   struct remote_state *rs = get_remote_state ();
 
   strcpy (rs->buf.data (), "QTro");
   offset = strlen (rs->buf.data ());
-  for (s = exec_bfd->sections; s; s = s->next)
+  for (s = current_program_space->exec_bfd ()->sections; s; s = s->next)
     {
       char tmp1[40], tmp2[40];
       int sec_length;
diff --git a/gdb/rs6000-nat.c b/gdb/rs6000-nat.c
index 654e06e3e4b..f7ec3a70ee1 100644
--- a/gdb/rs6000-nat.c
+++ b/gdb/rs6000-nat.c
@@ -581,11 +581,11 @@ rs6000_nat_target::create_inferior (const char *exec_file,
      Blindly calling rs6000_gdbarch_init used to work in older versions of
      GDB, as rs6000_gdbarch_init incorrectly used the previous tdep to
      determine the wordsize.  */
-  if (exec_bfd)
+  if (current_program_space->exec_bfd ())
     {
       const struct bfd_arch_info *exec_bfd_arch_info;
 
-      exec_bfd_arch_info = bfd_get_arch_info (exec_bfd);
+      exec_bfd_arch_info = bfd_get_arch_info (current_program_space->exec_bfd ());
       if (arch == exec_bfd_arch_info->arch)
  return;
     }
@@ -594,7 +594,7 @@ rs6000_nat_target::create_inferior (const char *exec_file,
 
   gdbarch_info_init (&info);
   info.bfd_arch_info = bfd_get_arch_info (&abfd);
-  info.abfd = exec_bfd;
+  info.abfd = current_program_space->exec_bfd ();
 
   if (!gdbarch_update_p (info))
     internal_error (__FILE__, __LINE__,
diff --git a/gdb/sol-thread.c b/gdb/sol-thread.c
index a24d51d1db2..01e052dccdf 100644
--- a/gdb/sol-thread.c
+++ b/gdb/sol-thread.c
@@ -794,7 +794,7 @@ rw_common (int dowrite, const struct ps_prochandle *ph, psaddr_t addr,
 #if defined (__sparcv9)
   /* For Sparc64 cross Sparc32, make sure the address has not been
      accidentally sign-extended (or whatever) to beyond 32 bits.  */
-  if (bfd_get_arch_size (exec_bfd) == 32)
+  if (bfd_get_arch_size (current_program_space->exec_bfd ()) == 32)
     addr &= 0xffffffff;
 #endif
 
@@ -950,9 +950,9 @@ ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
 ps_err_e
 ps_pdmodel (struct ps_prochandle *ph, int *data_model)
 {
-  if (exec_bfd == 0)
+  if (current_program_space->exec_bfd () == 0)
     *data_model = PR_MODEL_UNKNOWN;
-  else if (bfd_get_arch_size (exec_bfd) == 32)
+  else if (bfd_get_arch_size (current_program_space->exec_bfd ()) == 32)
     *data_model = PR_MODEL_ILP32;
   else
     *data_model = PR_MODEL_LP64;
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 3806c7e48cc..e55b2082949 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -192,12 +192,12 @@ find_program_interpreter (void)
 {
   char *buf = NULL;
 
-  /* If we have an exec_bfd, get the interpreter from the load commands.  */
-  if (exec_bfd)
+  /* If we have an current_program_space->exec_bfd (), get the interpreter from the load commands.  */
+  if (current_program_space->exec_bfd ())
     {
       bfd_mach_o_load_command *cmd;
 
-      if (bfd_mach_o_lookup_command (exec_bfd,
+      if (bfd_mach_o_lookup_command (current_program_space->exec_bfd (),
                                      BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
         return cmd->command.dylinker.name_str;
     }
@@ -543,7 +543,7 @@ darwin_solib_create_inferior_hook (int from_tty)
       CORE_ADDR vmaddr;
 
       /* Find the base address of the executable.  */
-      vmaddr = bfd_mach_o_get_base_address (exec_bfd);
+      vmaddr = bfd_mach_o_get_base_address (current_program_space->exec_bfd ());
 
       /* Relocate.  */
       if (vmaddr != load_addr)
@@ -557,7 +557,7 @@ darwin_solib_create_inferior_hook (int from_tty)
     {
       /* Dyld hasn't yet relocated itself, so the notifier address may
  be incorrect (as it has to be relocated).  */
-      CORE_ADDR start = bfd_get_start_address (exec_bfd);
+      CORE_ADDR start = bfd_get_start_address (current_program_space->exec_bfd ());
       if (start == 0)
  notifier = 0;
       else
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index ae76bb74c05..6b0f068191f 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -562,7 +562,7 @@ lm_base (void)
     "lm_base: get addr %x by _GLOBAL_OFFSET_TABLE_.\n",
     (unsigned int) addr);
     }
-  else if (scan_dyntag (DT_PLTGOT, exec_bfd, &addr))
+  else if (scan_dyntag (DT_PLTGOT, current_program_space->exec_bfd (), &addr))
     {
       struct int_elf32_dsbt_loadmap *ldm;
 
@@ -778,7 +778,7 @@ enable_break (void)
   asection *interp_sect;
   struct dsbt_info *info;
 
-  if (exec_bfd == NULL)
+  if (current_program_space->exec_bfd () == NULL)
     return 0;
 
   if (!target_has_execution)
@@ -793,7 +793,8 @@ enable_break (void)
 
   /* Find the .interp section; if not found, warn the user and drop
      into the old breakpoint at symbol code.  */
-  interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
+  interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
+ ".interp");
   if (interp_sect)
     {
       unsigned int interp_sect_size;
@@ -806,8 +807,8 @@ enable_break (void)
  the contents specify the dynamic linker this program uses.  */
       interp_sect_size = bfd_section_size (interp_sect);
       buf = (char *) alloca (interp_sect_size);
-      bfd_get_section_contents (exec_bfd, interp_sect,
- buf, 0, interp_sect_size);
+      bfd_get_section_contents (current_program_space->exec_bfd (),
+ interp_sect, buf, 0, interp_sect_size);
 
       /* Now we need to figure out where the dynamic linker was
  loaded so that we can load its symbols and place a breakpoint
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index bce33a3e4d6..08c9cf7ca5c 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -531,7 +531,8 @@ enable_break2 (void)
 
   /* Find the .interp section; if not found, warn the user and drop
      into the old breakpoint at symbol code.  */
-  interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
+  interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
+ ".interp");
   if (interp_sect)
     {
       unsigned int interp_sect_size;
@@ -545,8 +546,8 @@ enable_break2 (void)
          the contents specify the dynamic linker this program uses.  */
       interp_sect_size = bfd_section_size (interp_sect);
       buf = (char *) alloca (interp_sect_size);
-      bfd_get_section_contents (exec_bfd, interp_sect,
- buf, 0, interp_sect_size);
+      bfd_get_section_contents (current_program_space->exec_bfd (),
+ interp_sect, buf, 0, interp_sect_size);
 
       /* Now we need to figure out where the dynamic linker was
          loaded so that we can load its symbols and place a breakpoint
@@ -735,7 +736,8 @@ enable_break (void)
   /* Check for the presence of a .interp section.  If there is no
      such section, the executable is statically linked.  */
 
-  interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
+  interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
+ ".interp");
 
   if (interp_sect == NULL)
     {
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 520d9e8ed12..7b004828761 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -554,20 +554,22 @@ read_program_header (int type, int *p_arch_size, CORE_ADDR *base_addr)
 static gdb::optional<gdb::byte_vector>
 find_program_interpreter (void)
 {
-  /* If we have an exec_bfd, use its section table.  */
-  if (exec_bfd
-      && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
+  /* If we have an current_program_space->exec_bfd (), use its section table.  */
+  if (current_program_space->exec_bfd ()
+      && (bfd_get_flavour (current_program_space->exec_bfd ())
+  == bfd_target_elf_flavour))
    {
      struct bfd_section *interp_sect;
 
-     interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
+     interp_sect = bfd_get_section_by_name (current_program_space->exec_bfd (),
+    ".interp");
      if (interp_sect != NULL)
       {
  int sect_size = bfd_section_size (interp_sect);
 
  gdb::byte_vector buf (sect_size);
- bfd_get_section_contents (exec_bfd, interp_sect, buf.data (), 0,
-  sect_size);
+ bfd_get_section_contents (current_program_space->exec_bfd (),
+  interp_sect, buf.data (), 0, sect_size);
  return buf;
       }
    }
@@ -762,7 +764,8 @@ elf_locate_base (void)
   /* Look for DT_MIPS_RLD_MAP first.  MIPS executables use this
      instead of DT_DEBUG, although they sometimes contain an unused
      DT_DEBUG.  */
-  if (scan_dyntag (DT_MIPS_RLD_MAP, exec_bfd, &dyn_ptr, NULL)
+  if (scan_dyntag (DT_MIPS_RLD_MAP, current_program_space->exec_bfd (),
+   &dyn_ptr, NULL)
       || scan_dyntag_auxv (DT_MIPS_RLD_MAP, &dyn_ptr, NULL))
     {
       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
@@ -780,7 +783,8 @@ elf_locate_base (void)
   /* Then check DT_MIPS_RLD_MAP_REL.  MIPS executables now use this form
      because of needing to support PIE.  DT_MIPS_RLD_MAP will also exist
      in non-PIE.  */
-  if (scan_dyntag (DT_MIPS_RLD_MAP_REL, exec_bfd, &dyn_ptr, &dyn_ptr_addr)
+  if (scan_dyntag (DT_MIPS_RLD_MAP_REL, current_program_space->exec_bfd (),
+   &dyn_ptr, &dyn_ptr_addr)
       || scan_dyntag_auxv (DT_MIPS_RLD_MAP_REL, &dyn_ptr, &dyn_ptr_addr))
     {
       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
@@ -796,7 +800,7 @@ elf_locate_base (void)
     }
 
   /* Find DT_DEBUG.  */
-  if (scan_dyntag (DT_DEBUG, exec_bfd, &dyn_ptr, NULL)
+  if (scan_dyntag (DT_DEBUG, current_program_space->exec_bfd (), &dyn_ptr, NULL)
       || scan_dyntag_auxv (DT_DEBUG, &dyn_ptr, NULL))
     return dyn_ptr;
 
@@ -1399,7 +1403,9 @@ svr4_current_sos_direct (struct svr4_info *info)
 
   /* Assume that everything is a library if the dynamic loader was loaded
      late by a static executable.  */
-  if (exec_bfd && bfd_get_section_by_name (exec_bfd, ".dynamic") == NULL)
+  if (current_program_space->exec_bfd ()
+      && bfd_get_section_by_name (current_program_space->exec_bfd (),
+  ".dynamic") == NULL)
     ignore_first = 0;
   else
     ignore_first = 1;
@@ -2564,27 +2570,30 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
      a call to gdbarch_convert_from_func_ptr_addr.  */
   CORE_ADDR entry_point, exec_displacement;
 
-  if (exec_bfd == NULL)
+  if (current_program_space->exec_bfd () == NULL)
     return 0;
 
   /* Therefore for ELF it is ET_EXEC and not ET_DYN.  Both shared libraries
      being executed themselves and PIE (Position Independent Executable)
      executables are ET_DYN.  */
 
-  if ((bfd_get_file_flags (exec_bfd) & DYNAMIC) == 0)
+  if ((bfd_get_file_flags (current_program_space->exec_bfd ()) & DYNAMIC) == 0)
     return 0;
 
   if (target_auxv_search (current_top_target (), AT_ENTRY, &entry_point) <= 0)
     return 0;
 
-  exec_displacement = entry_point - bfd_get_start_address (exec_bfd);
+  exec_displacement
+    = entry_point - bfd_get_start_address (current_program_space->exec_bfd ());
 
   /* Verify the EXEC_DISPLACEMENT candidate complies with the required page
      alignment.  It is cheaper than the program headers comparison below.  */
 
-  if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
+  if (bfd_get_flavour (current_program_space->exec_bfd ())
+      == bfd_target_elf_flavour)
     {
-      const struct elf_backend_data *elf = get_elf_backend_data (exec_bfd);
+      const struct elf_backend_data *elf
+ = get_elf_backend_data (current_program_space->exec_bfd ());
 
       /* p_align of PT_LOAD segments does not specify any alignment but
  only congruency of addresses:
@@ -2601,7 +2610,8 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
      looking at a different file than the one used by the kernel - for
      instance, "gdb program" connected to "gdbserver :PORT ld.so program".  */
 
-  if (bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
+  if (bfd_get_flavour (current_program_space->exec_bfd ())
+      == bfd_target_elf_flavour)
     {
       /* Be optimistic and return 0 only if GDB was able to verify the headers
  really do not match.  */
@@ -2610,7 +2620,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
       gdb::optional<gdb::byte_vector> phdrs_target
  = read_program_header (-1, &arch_size, NULL);
       gdb::optional<gdb::byte_vector> phdrs_binary
- = read_program_headers_from_bfd (exec_bfd);
+ = read_program_headers_from_bfd (current_program_space->exec_bfd ());
       if (phdrs_target && phdrs_binary)
  {
   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
@@ -2629,14 +2639,16 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
      content offset for the verification purpose.  */
 
   if (phdrs_target->size () != phdrs_binary->size ()
-      || bfd_get_arch_size (exec_bfd) != arch_size)
+      || bfd_get_arch_size (current_program_space->exec_bfd ()) != arch_size)
     return 0;
   else if (arch_size == 32
    && phdrs_target->size () >= sizeof (Elf32_External_Phdr)
            && phdrs_target->size () % sizeof (Elf32_External_Phdr) == 0)
     {
-      Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
-      Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
+      Elf_Internal_Ehdr *ehdr2
+ = elf_tdata (current_program_space->exec_bfd ())->elf_header;
+      Elf_Internal_Phdr *phdr2
+ = elf_tdata (current_program_space->exec_bfd ())->phdr;
       CORE_ADDR displacement = 0;
       int i;
 
@@ -2736,6 +2748,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
     }
 
   /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
+  bfd *exec_bfd = current_program_space->exec_bfd ();
   plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
   if (plt2_asect)
     {
@@ -2770,8 +2783,8 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
    && phdrs_target->size () >= sizeof (Elf64_External_Phdr)
            && phdrs_target->size () % sizeof (Elf64_External_Phdr) == 0)
     {
-      Elf_Internal_Ehdr *ehdr2 = elf_tdata (exec_bfd)->elf_header;
-      Elf_Internal_Phdr *phdr2 = elf_tdata (exec_bfd)->phdr;
+      Elf_Internal_Ehdr *ehdr2 = elf_tdata (current_program_space->exec_bfd ())->elf_header;
+      Elf_Internal_Phdr *phdr2 = elf_tdata (current_program_space->exec_bfd ())->phdr;
       CORE_ADDR displacement = 0;
       int i;
 
@@ -2870,7 +2883,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
     }
 
   /* prelink can convert .plt SHT_NOBITS to SHT_PROGBITS.  */
-  plt2_asect = bfd_get_section_by_name (exec_bfd, ".plt");
+  plt2_asect = bfd_get_section_by_name (current_program_space->exec_bfd (), ".plt");
   if (plt2_asect)
     {
       int content2;
@@ -2883,7 +2896,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
       filesz = extract_unsigned_integer (buf_filesz_p, 8,
  byte_order);
 
-      /* PLT2_ASECT is from on-disk file (exec_bfd) while
+      /* PLT2_ASECT is from on-disk file (current_program_space->exec_bfd ()) while
  FILESZ is from the in-memory image.  */
       if (content2)
  filesz += bfd_section_size (plt2_asect);
@@ -2914,7 +2927,7 @@ svr4_exec_displacement (CORE_ADDR *displacementp)
       printf_unfiltered (_("Using PIE (Position Independent Executable) "
    "displacement %s for \"%s\".\n"),
  paddress (target_gdbarch (), exec_displacement),
- bfd_get_filename (exec_bfd));
+ bfd_get_filename (current_program_space->exec_bfd ()));
     }
 
   *displacementp = exec_displacement;
@@ -2969,10 +2982,11 @@ svr4_relocate_main_executable (void)
    displacement);
       objfile_relocate (symfile_objfile, new_offsets);
     }
-  else if (exec_bfd)
+  else if (current_program_space->exec_bfd ())
     {
       asection *asect;
 
+      bfd *exec_bfd = current_program_space->exec_bfd ();
       for (asect = exec_bfd->sections; asect != NULL; asect = asect->next)
  exec_set_section_address (bfd_get_filename (exec_bfd), asect->index,
   bfd_section_vma (asect) + displacement);
@@ -3228,7 +3242,7 @@ svr4_iterate_over_objfiles_in_search_order
         current_objfile = current_objfile->separate_debug_objfile_backlink;
 
       if (current_objfile == symfile_objfile)
- abfd = exec_bfd;
+ abfd = current_program_space->exec_bfd ();
       else
  abfd = current_objfile->obfd;
 
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index c67d087c76d..098fcf72e4e 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -68,7 +68,7 @@ source_cache::get_plain_source_lines (struct symtab *s,
   time_t mtime = 0;
   if (SYMTAB_OBJFILE (s) != NULL && SYMTAB_OBJFILE (s)->obfd != NULL)
     mtime = SYMTAB_OBJFILE (s)->mtime;
-  else if (exec_bfd)
+  else if (current_program_space->exec_bfd ())
     mtime = current_program_space->ebfd_mtime;
 
   if (mtime && mtime < st.st_mtime)
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index 78096fcbae1..fc5407c6372 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -145,7 +145,7 @@ add_symbol_file_from_memory_command (const char *args, int from_tty)
   if (symfile_objfile != NULL)
     templ = symfile_objfile->obfd;
   else
-    templ = exec_bfd;
+    templ = current_program_space->exec_bfd ();
   if (templ == NULL)
     error (_("Must use symbol-file or exec-file "
      "before add-symbol-file-from-memory."));
@@ -167,8 +167,8 @@ add_vsyscall_page (struct target_ops *target, int from_tty)
 
       if (core_bfd != NULL)
  bfd = core_bfd;
-      else if (exec_bfd != NULL)
- bfd = exec_bfd;
+      else if (current_program_space->exec_bfd () != NULL)
+ bfd = current_program_space->exec_bfd ();
       else
        /* FIXME: cagney/2004-05-06: Should not require an existing
   BFD when trying to create a run-time BFD of the VSYSCALL
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 2c38ce4431a..57ad9829dec 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2483,9 +2483,9 @@ reread_symbols (void)
   /* We need to do this whenever any symbols go away.  */
   clear_symtab_users_cleanup defer_clear_users (0);
 
-  if (exec_bfd != NULL
+  if (current_program_space->exec_bfd () != NULL
       && filename_cmp (bfd_get_filename (objfile->obfd),
-       bfd_get_filename (exec_bfd)) == 0)
+       bfd_get_filename (current_program_space->exec_bfd ())) == 0)
     {
       /* Reload EXEC_BFD without asking anything.  */
 
diff --git a/gdb/tracefile-tfile.c b/gdb/tracefile-tfile.c
index fd7bab822a1..167cc92b587 100644
--- a/gdb/tracefile-tfile.c
+++ b/gdb/tracefile-tfile.c
@@ -25,7 +25,7 @@
 #include "regcache.h"
 #include "inferior.h"
 #include "gdbthread.h"
-#include "exec.h" /* exec_bfd */
+#include "exec.h"
 #include "completer.h"
 #include "filenames.h"
 #include "remote.h"
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index aa0adeba99b..6fcb629fe73 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -910,7 +910,8 @@ windows_solib_create_inferior_hook (int from_tty)
   /* Rebase executable if the base address changed because of ASLR.  */
   if (symfile_objfile != nullptr && exec_base != 0)
     {
-      CORE_ADDR vmaddr = pe_data (exec_bfd)->pe_opthdr.ImageBase;
+      CORE_ADDR vmaddr
+ = pe_data (current_program_space->exec_bfd ())->pe_opthdr.ImageBase;
       if (vmaddr != exec_base)
  objfile_rebase (symfile_objfile, exec_base - vmaddr);
     }
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 8/9] Change program_space::ebfd to a gdb_bfd_ref_ptr

Tom Tromey-2
In reply to this post by Tom Tromey-2
This changes program_space::ebfd to a gdb_bfd_ref_ptr, removing some
manual management.

gdb/ChangeLog
2020-07-21  Tom Tromey  <[hidden email]>

        * exec.c (exec_file_attach): Update.
        * progspace.c (program_space::exec_close): Update.
        * progspace.h (struct program_space) <ebfd>: Now a
        gdb_bfd_ref_ptr.
        <set_exec_bfd>: Change argument type.
        <exec_bfd>: Update.
---
 gdb/ChangeLog   | 9 +++++++++
 gdb/exec.c      | 2 +-
 gdb/progspace.c | 6 ++----
 gdb/progspace.h | 8 ++++----
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/gdb/exec.c b/gdb/exec.c
index 75f3990e0f0..ee5e948ec75 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -450,7 +450,7 @@ exec_file_attach (const char *filename, int from_tty)
       FOPEN_RUB, scratch_chan);
       else
  temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
-      current_program_space->set_exec_bfd (temp.release ());
+      current_program_space->set_exec_bfd (std::move (temp));
 
       if (!current_program_space->exec_bfd ())
  {
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 49243a6394d..15cdc9f90e1 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -223,13 +223,11 @@ program_space::solibs () const
 void
 program_space::exec_close ()
 {
-  if (ebfd)
+  if (ebfd != nullptr)
     {
-      gdb_bfd_unref (ebfd);
-
       /* Removing target sections may close the exec_ops target.
  Clear ebfd before doing so to prevent recursion.  */
-      ebfd = NULL;
+      ebfd.reset (nullptr);
       ebfd_mtime = 0;
 
       remove_target_sections (&ebfd);
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 5f952ea9b84..70b32198c20 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -279,13 +279,13 @@ struct program_space
   /* Return the exec BFD for this program space.  */
   bfd *exec_bfd () const
   {
-    return ebfd;
+    return ebfd.get ();
   }
 
   /* Set the exec BFD for this program space to ABFD.  */
-  void set_exec_bfd (bfd *abfd)
+  void set_exec_bfd (gdb_bfd_ref_ptr &&abfd)
   {
-    ebfd = abfd;
+    ebfd = std::move (abfd);
   }
 
   /* Unique ID number.  */
@@ -295,7 +295,7 @@ struct program_space
      managed by the exec target.  */
 
   /* The BFD handle for the main executable.  */
-  bfd *ebfd = NULL;
+  gdb_bfd_ref_ptr ebfd;
   /* The last-modified time, from when the exec was brought in.  */
   long ebfd_mtime = 0;
   /* Similar to bfd_get_filename (exec_bfd) but in original form given
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

[PATCH 9/9] Remove symfile_objfile macro

Tom Tromey-2
In reply to this post by Tom Tromey-2
This removes the symfile_objfile macro, in favor of just spelling out
the member access.

2020-07-21  Tom Tromey  <[hidden email]>

        * windows-tdep.c (windows_solib_create_inferior_hook): Update.
        * target.c (info_target_command): Update.
        * symfile.c (syms_from_objfile_1, finish_new_objfile)
        (symbol_file_clear, reread_symbols): Update.
        * symfile-mem.c (add_symbol_file_from_memory_command): Update.
        * stabsread.c (scan_file_globals): Update.
        * solib.c (update_solib_list): Update.
        * solib-svr4.c (elf_locate_base, open_symbol_file_object)
        (svr4_fetch_objfile_link_map, enable_break)
        (svr4_relocate_main_executable)
        (svr4_iterate_over_objfiles_in_search_order): Update.
        * solib-frv.c (lm_base, enable_break)
        (frv_relocate_main_executable): Update.
        (main_got, frv_fdpic_find_canonical_descriptor): Update.
        (frv_fetch_objfile_link_map): Update.
        * solib-dsbt.c (lm_base, dsbt_relocate_main_executable): Update.
        * solib-darwin.c (darwin_solib_create_inferior_hook): Update.
        * solib-aix.c (solib_aix_solib_create_inferior_hook): Update.
        * remote.c (remote_target::get_offsets): Update.
        (remote_target::start_remote)
        (extended_remote_target::post_attach): Update.
        * objfiles.c (entry_point_address_query): Update.
        * nto-procfs.c (nto_procfs_target::create_inferior): Update.
        * minsyms.c (get_symbol_leading_char): Update.
        * frame.c (inside_main_func): Update.
        * progspace.h (symfile_objfile): Remove macro.
---
 gdb/ChangeLog      | 29 +++++++++++++++++++++++++++++
 gdb/frame.c        |  5 +++--
 gdb/minsyms.c      |  8 ++++++--
 gdb/nto-procfs.c   |  3 ++-
 gdb/objfiles.c     |  8 ++++----
 gdb/progspace.h    |  5 -----
 gdb/remote.c       | 23 ++++++++++++-----------
 gdb/solib-aix.c    |  9 +++++----
 gdb/solib-darwin.c |  5 +++--
 gdb/solib-dsbt.c   | 17 +++++++++--------
 gdb/solib-frv.c    | 28 +++++++++++++++-------------
 gdb/solib-svr4.c   | 21 ++++++++++++---------
 gdb/solib.c        |  3 ++-
 gdb/stabsread.c    |  5 +++--
 gdb/symfile-mem.c  |  4 ++--
 gdb/symfile.c      | 16 ++++++++--------
 gdb/target.c       |  9 ++++++---
 gdb/windows-tdep.c |  5 +++--
 18 files changed, 124 insertions(+), 79 deletions(-)

diff --git a/gdb/frame.c b/gdb/frame.c
index ac1016b083f..0a774ab005d 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -2235,9 +2235,10 @@ inside_main_func (struct frame_info *this_frame)
   struct bound_minimal_symbol msymbol;
   CORE_ADDR maddr;
 
-  if (symfile_objfile == 0)
+  if (current_program_space->symfile_object_file == 0)
     return 0;
-  msymbol = lookup_minimal_symbol (main_name (), NULL, symfile_objfile);
+  msymbol = lookup_minimal_symbol (main_name (), NULL,
+   current_program_space->symfile_object_file);
   if (msymbol.minsym == NULL)
     return 0;
   /* Make certain that the code, and not descriptor, address is
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index f4a2544eb19..0dec18d21ab 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -1008,8 +1008,12 @@ get_symbol_leading_char (bfd *abfd)
 {
   if (abfd != NULL)
     return bfd_get_symbol_leading_char (abfd);
-  if (symfile_objfile != NULL && symfile_objfile->obfd != NULL)
-    return bfd_get_symbol_leading_char (symfile_objfile->obfd);
+  if (current_program_space->symfile_object_file != NULL)
+    {
+      objfile *objf = current_program_space->symfile_object_file;
+      if (objf->obfd != NULL)
+ return bfd_get_symbol_leading_char (objf->obfd);
+    }
   return 0;
 }
 
diff --git a/gdb/nto-procfs.c b/gdb/nto-procfs.c
index 90cd67fd776..9e041b79f34 100644
--- a/gdb/nto-procfs.c
+++ b/gdb/nto-procfs.c
@@ -1323,7 +1323,8 @@ nto_procfs_target::create_inferior (const char *exec_file,
   target_terminal::init ();
 
   if (current_program_space->exec_bfd () != NULL
-      || (symfile_objfile != NULL && symfile_objfile->obfd != NULL))
+      || (current_program_space->symfile_object_file != NULL
+  && current_program_space->symfile_object_file->obfd != NULL))
     solib_create_inferior_hook (0);
 }
 
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 3aa7973e0d5..f91a4af36a1 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -380,12 +380,12 @@ objfile::objfile (bfd *abfd, const char *name, objfile_flags flags_)
 int
 entry_point_address_query (CORE_ADDR *entry_p)
 {
-  if (symfile_objfile == NULL || !symfile_objfile->per_bfd->ei.entry_point_p)
+  objfile *objf = current_program_space->symfile_object_file;
+  if (objf == NULL || !objf->per_bfd->ei.entry_point_p)
     return 0;
 
-  int idx = symfile_objfile->per_bfd->ei.the_bfd_section_index;
-  *entry_p = (symfile_objfile->per_bfd->ei.entry_point
-      + symfile_objfile->section_offsets[idx]);
+  int idx = objf->per_bfd->ei.the_bfd_section_index;
+  *entry_p = objf->per_bfd->ei.entry_point + objf->section_offsets[idx];
 
   return 1;
 }
diff --git a/gdb/progspace.h b/gdb/progspace.h
index 70b32198c20..aeba9b255f4 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -372,11 +372,6 @@ struct address_space
   REGISTRY_FIELDS;
 };
 
-/* The object file that the main symbol table was loaded from (e.g. the
-   argument to the "symbol-file" or "file" command).  */
-
-#define symfile_objfile current_program_space->symfile_object_file
-
 /* The list of all program spaces.  There's always at least one.  */
 extern std::vector<struct program_space *>program_spaces;
 
diff --git a/gdb/remote.c b/gdb/remote.c
index d0836fff6e5..4de06173562 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4106,7 +4106,7 @@ remote_target::get_offsets ()
   int lose, num_segments = 0, do_sections, do_segments;
   CORE_ADDR text_addr, data_addr, bss_addr, segments[2];
 
-  if (symfile_objfile == NULL)
+  if (current_program_space->symfile_object_file == NULL)
     return;
 
   putpkt ("qOffsets");
@@ -4182,10 +4182,10 @@ remote_target::get_offsets ()
   else if (*ptr != '\0')
     warning (_("Target reported unsupported offsets: %s"), buf);
 
-  section_offsets offs = symfile_objfile->section_offsets;
+  objfile *objf = current_program_space->symfile_object_file;
+  section_offsets offs = objf->section_offsets;
 
-  symfile_segment_data_up data
-    = get_symfile_segment_data (symfile_objfile->obfd);
+  symfile_segment_data_up data = get_symfile_segment_data (objf->obfd);
   do_segments = (data != NULL);
   do_sections = num_segments == 0;
 
@@ -4220,7 +4220,7 @@ remote_target::get_offsets ()
 
   if (do_segments)
     {
-      int ret = symfile_map_offsets_to_segments (symfile_objfile->obfd,
+      int ret = symfile_map_offsets_to_segments (objf->obfd,
  data.get (), offs,
  num_segments, segments);
 
@@ -4234,18 +4234,18 @@ remote_target::get_offsets ()
 
   if (do_sections)
     {
-      offs[SECT_OFF_TEXT (symfile_objfile)] = text_addr;
+      offs[SECT_OFF_TEXT (objf)] = text_addr;
 
       /* This is a temporary kludge to force data and bss to use the
  same offsets because that's what nlmconv does now.  The real
  solution requires changes to the stub and remote.c that I
  don't have time to do right now.  */
 
-      offs[SECT_OFF_DATA (symfile_objfile)] = data_addr;
-      offs[SECT_OFF_BSS (symfile_objfile)] = data_addr;
+      offs[SECT_OFF_DATA (objf)] = data_addr;
+      offs[SECT_OFF_BSS (objf)] = data_addr;
     }
 
-  objfile_relocate (symfile_objfile, offs);
+  objfile_relocate (objf, offs);
 }
 
 /* Send interrupt_sequence to remote target.  */
@@ -4845,7 +4845,8 @@ remote_target::start_remote (int from_tty, int extended_p)
   /* If we connected to a live target, do some additional setup.  */
   if (target_has_execution)
     {
-      if (symfile_objfile) /* No use without a symbol-file.  */
+      /* No use without a symbol-file.  */
+      if (current_program_space->symfile_object_file)
  remote_check_symbols ();
     }
 
@@ -5981,7 +5982,7 @@ extended_remote_target::post_attach (int pid)
      binary is not using shared libraries, the vsyscall page is not
      present (on Linux) and the binary itself hadn't changed since the
      debugging process was started.  */
-  if (symfile_objfile != NULL)
+  if (current_program_space->symfile_object_file != NULL)
     remote_check_symbols();
 }
 
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 344c1f57600..0897d067c40 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -462,12 +462,13 @@ solib_aix_solib_create_inferior_hook (int from_tty)
     }
 
   lm_info_aix &exec_info = (*library_list)[0];
-  if (symfile_objfile != NULL)
+  if (current_program_space->symfile_object_file != NULL)
     {
-      section_offsets offsets
- = solib_aix_get_section_offsets (symfile_objfile, &exec_info);
+      objfile *objf = current_program_space->symfile_object_file;
+      section_offsets offsets = solib_aix_get_section_offsets (objf,
+       &exec_info);
 
-      objfile_relocate (symfile_objfile, offsets);
+      objfile_relocate (objf, offsets);
     }
 }
 
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index e55b2082949..90d2be534c0 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -538,7 +538,7 @@ darwin_solib_create_inferior_hook (int from_tty)
       load_addr = darwin_read_exec_load_addr_at_init (info);
     }
 
-  if (load_addr != 0 && symfile_objfile != NULL)
+  if (load_addr != 0 && current_program_space->symfile_object_file != NULL)
     {
       CORE_ADDR vmaddr;
 
@@ -547,7 +547,8 @@ darwin_solib_create_inferior_hook (int from_tty)
 
       /* Relocate.  */
       if (vmaddr != load_addr)
- objfile_rebase (symfile_objfile, load_addr - vmaddr);
+ objfile_rebase (current_program_space->symfile_object_file,
+ load_addr - vmaddr);
     }
 
   /* Set solib notifier (to reload list of shared libraries).  */
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 6b0f068191f..6e38b9534e9 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -552,7 +552,7 @@ lm_base (void)
     return info->lm_base_cache;
 
   got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
-   symfile_objfile);
+   current_program_space->symfile_object_file);
 
   if (got_sym.minsym != 0)
     {
@@ -909,21 +909,22 @@ dsbt_relocate_main_executable (void)
   info->main_executable_lm_info = new lm_info_dsbt;
   info->main_executable_lm_info->map = ldm;
 
-  section_offsets new_offsets (symfile_objfile->section_offsets.size ());
+  objfile *objf = current_program_space->symfile_object_file;
+  section_offsets new_offsets (objf->section_offsets.size ());
   changed = 0;
 
-  ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
+  ALL_OBJFILE_OSECTIONS (objf, osect)
     {
       CORE_ADDR orig_addr, addr, offset;
       int osect_idx;
       int seg;
 
-      osect_idx = osect - symfile_objfile->sections;
+      osect_idx = osect - objf->sections;
 
       /* Current address of section.  */
       addr = obj_section_addr (osect);
       /* Offset from where this section started.  */
-      offset = symfile_objfile->section_offsets[osect_idx];
+      offset = objf->section_offsets[osect_idx];
       /* Original address prior to any past relocations.  */
       orig_addr = addr - offset;
 
@@ -943,10 +944,10 @@ dsbt_relocate_main_executable (void)
     }
 
   if (changed)
-    objfile_relocate (symfile_objfile, new_offsets);
+    objfile_relocate (objf, new_offsets);
 
-  /* Now that symfile_objfile has been relocated, we can compute the
-     GOT value and stash it away.  */
+  /* Now that OBJF has been relocated, we can compute the GOT value
+     and stash it away.  */
 }
 
 /* When gdb starts up the inferior, it nurses it along (through the
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 08c9cf7ca5c..6c9ab1757b2 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -287,7 +287,7 @@ lm_base (void)
     return lm_base_cache;
 
   got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL,
-                                   symfile_objfile);
+                                   current_program_space->symfile_object_file);
   if (got_sym.minsym == 0)
     {
       if (solib_frv_debug)
@@ -717,7 +717,7 @@ enable_break (void)
   asection *interp_sect;
   CORE_ADDR entry_point;
 
-  if (symfile_objfile == NULL)
+  if (current_program_space->symfile_object_file == NULL)
     {
       if (solib_frv_debug)
  fprintf_unfiltered (gdb_stdlog,
@@ -784,21 +784,22 @@ frv_relocate_main_executable (void)
   main_executable_lm_info = new lm_info_frv;
   main_executable_lm_info->map = ldm;
 
-  section_offsets new_offsets (symfile_objfile->section_offsets.size ());
+  objfile *objf = current_program_space->symfile_object_file;
+  section_offsets new_offsets (objf->section_offsets.size ());
   changed = 0;
 
-  ALL_OBJFILE_OSECTIONS (symfile_objfile, osect)
+  ALL_OBJFILE_OSECTIONS (objf, osect)
     {
       CORE_ADDR orig_addr, addr, offset;
       int osect_idx;
       int seg;
       
-      osect_idx = osect - symfile_objfile->sections;
+      osect_idx = osect - objf->sections;
 
       /* Current address of section.  */
       addr = obj_section_addr (osect);
       /* Offset from where this section started.  */
-      offset = symfile_objfile->section_offsets[osect_idx];
+      offset = objf->section_offsets[osect_idx];
       /* Original address prior to any past relocations.  */
       orig_addr = addr - offset;
 
@@ -818,10 +819,10 @@ frv_relocate_main_executable (void)
     }
 
   if (changed)
-    objfile_relocate (symfile_objfile, new_offsets);
+    objfile_relocate (objf, new_offsets);
 
-  /* Now that symfile_objfile has been relocated, we can compute the
-     GOT value and stash it away.  */
+  /* Now that OBJF has been relocated, we can compute the GOT value
+     and stash it away.  */
   main_executable_lm_info->got_value = main_got ();
 }
 
@@ -894,8 +895,8 @@ main_got (void)
 {
   struct bound_minimal_symbol got_sym;
 
-  got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_",
-   NULL, symfile_objfile);
+  objfile *objf = current_program_space->symfile_object_file;
+  got_sym = lookup_minimal_symbol ("_GLOBAL_OFFSET_TABLE_", NULL, objf);
   if (got_sym.minsym == 0)
     return 0;
 
@@ -955,8 +956,9 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point)
     name = sym->linkage_name ();
 
   /* Check the main executable.  */
+  objfile *objf = current_program_space->symfile_object_file;
   addr = find_canonical_descriptor_in_load_object
-           (entry_point, got_value, name, symfile_objfile->obfd,
+           (entry_point, got_value, name, objf->obfd,
     main_executable_lm_info);
 
   /* If descriptor not found via main executable, check each load object
@@ -1110,7 +1112,7 @@ frv_fetch_objfile_link_map (struct objfile *objfile)
     solib_add (0, 0, 1);
 
   /* frv_current_sos() will set main_lm_addr for the main executable.  */
-  if (objfile == symfile_objfile)
+  if (objfile == current_program_space->symfile_object_file)
     return main_lm_addr;
 
   /* The other link map addresses may be found by examining the list
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 7b004828761..909e806e09e 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -806,7 +806,8 @@ elf_locate_base (void)
 
   /* This may be a static executable.  Look for the symbol
      conventionally named _r_debug, as a last resort.  */
-  msymbol = lookup_minimal_symbol ("_r_debug", NULL, symfile_objfile);
+  msymbol = lookup_minimal_symbol ("_r_debug", NULL,
+   current_program_space->symfile_object_file);
   if (msymbol.minsym != NULL)
     return BMSYMBOL_VALUE_ADDRESS (msymbol);
 
@@ -971,7 +972,7 @@ open_symbol_file_object (int from_tty)
   if (from_tty)
     add_flags |= SYMFILE_VERBOSE;
 
-  if (symfile_objfile)
+  if (current_program_space->symfile_object_file)
     if (!query (_("Attempt to reload symbols from process? ")))
       return 0;
 
@@ -1544,7 +1545,7 @@ svr4_fetch_objfile_link_map (struct objfile *objfile)
     solib_add (NULL, 0, auto_solib_add);
 
   /* svr4_current_sos() will set main_lm_addr for the main executable.  */
-  if (objfile == symfile_objfile)
+  if (objfile == current_program_space->symfile_object_file)
     return info->main_lm_addr;
 
   /* If OBJFILE is a separate debug object file, look for the
@@ -2465,9 +2466,10 @@ enable_break (struct svr4_info *info, int from_tty)
   /* Scan through the lists of symbols, trying to look up the symbol and
      set a breakpoint there.  Terminate loop when we/if we succeed.  */
 
+  objfile *objf = current_program_space->symfile_object_file;
   for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
     {
-      msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
+      msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
       if ((msymbol.minsym != NULL)
   && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
  {
@@ -2485,7 +2487,7 @@ enable_break (struct svr4_info *info, int from_tty)
     {
       for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
  {
-  msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
+  msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, objf);
   if ((msymbol.minsym != NULL)
       && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
     {
@@ -2976,11 +2978,12 @@ svr4_relocate_main_executable (void)
   /* Even DISPLACEMENT 0 is a valid new difference of in-memory vs. in-file
      addresses.  */
 
-  if (symfile_objfile)
+  objfile *objf = current_program_space->symfile_object_file;
+  if (objf)
     {
-      section_offsets new_offsets (symfile_objfile->section_offsets.size (),
+      section_offsets new_offsets (objf->section_offsets.size (),
    displacement);
-      objfile_relocate (symfile_objfile, new_offsets);
+      objfile_relocate (objf, new_offsets);
     }
   else if (current_program_space->exec_bfd ())
     {
@@ -3241,7 +3244,7 @@ svr4_iterate_over_objfiles_in_search_order
       if (current_objfile->separate_debug_objfile_backlink != nullptr)
         current_objfile = current_objfile->separate_debug_objfile_backlink;
 
-      if (current_objfile == symfile_objfile)
+      if (current_objfile == current_program_space->symfile_object_file)
  abfd = current_program_space->exec_bfd ();
       else
  abfd = current_objfile->obfd;
diff --git a/gdb/solib.c b/gdb/solib.c
index cd410bb9e3e..9e3e59e8c6c 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -736,7 +736,8 @@ update_solib_list (int from_tty)
       /* If we are attaching to a running process for which we
  have not opened a symbol file, we may be able to get its
  symbols now!  */
-      if (inf->attach_flag && symfile_objfile == NULL)
+      if (inf->attach_flag
+  && current_program_space->symfile_object_file == NULL)
  {
   try
     {
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index d2ff54a47bd..8d859e5ff6f 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -4521,8 +4521,9 @@ scan_file_globals (struct objfile *objfile)
      If we are scanning the symbols for a shared library, try to resolve
      them from the minimal symbols of the main executable first.  */
 
-  if (symfile_objfile && objfile != symfile_objfile)
-    resolve_objfile = symfile_objfile;
+  if (current_program_space->symfile_object_file
+      && objfile != current_program_space->symfile_object_file)
+    resolve_objfile = current_program_space->symfile_object_file;
   else
     resolve_objfile = objfile;
 
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index fc5407c6372..9d9e6d5f152 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -142,8 +142,8 @@ add_symbol_file_from_memory_command (const char *args, int from_tty)
   addr = parse_and_eval_address (args);
 
   /* We need some representative bfd to know the target we are looking at.  */
-  if (symfile_objfile != NULL)
-    templ = symfile_objfile->obfd;
+  if (current_program_space->symfile_object_file != NULL)
+    templ = current_program_space->symfile_object_file->obfd;
   else
     templ = current_program_space->exec_bfd ();
   if (templ == NULL)
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 57ad9829dec..5dfecf944cb 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -943,10 +943,10 @@ syms_from_objfile_1 (struct objfile *objfile,
 
       /* Since no error yet, throw away the old symbol table.  */
 
-      if (symfile_objfile != NULL)
+      if (current_program_space->symfile_object_file != NULL)
  {
-  symfile_objfile->unlink ();
-  gdb_assert (symfile_objfile == NULL);
+  current_program_space->symfile_object_file->unlink ();
+  gdb_assert (current_program_space->symfile_object_file == NULL);
  }
 
       /* Currently we keep symbols from the add-symbol-file command.
@@ -1009,7 +1009,7 @@ finish_new_objfile (struct objfile *objfile, symfile_add_flags add_flags)
   if (add_flags & SYMFILE_MAINLINE)
     {
       /* OK, make it the "real" symbol file.  */
-      symfile_objfile = objfile;
+      current_program_space->symfile_object_file = objfile;
 
       clear_symtab_users (add_flags);
     }
@@ -1230,9 +1230,9 @@ symbol_file_clear (int from_tty)
 {
   if ((have_full_symbols () || have_partial_symbols ())
       && from_tty
-      && (symfile_objfile
+      && (current_program_space->symfile_object_file
   ? !query (_("Discard symbol table from `%s'? "),
-    objfile_name (symfile_objfile))
+    objfile_name (current_program_space->symfile_object_file))
   : !query (_("Discard symbol table? "))))
     error (_("Not confirmed."));
 
@@ -1244,7 +1244,7 @@ symbol_file_clear (int from_tty)
 
   clear_symtab_users (0);
 
-  gdb_assert (symfile_objfile == NULL);
+  gdb_assert (current_program_space->symfile_object_file == NULL);
   if (from_tty)
     printf_filtered (_("No symbol file now.\n"));
 }
@@ -2578,7 +2578,7 @@ reread_symbols (void)
   /* What the hell is sym_new_init for, anyway?  The concept of
      distinguishing between the main file and additional files
      in this way seems rather dubious.  */
-  if (objfile == symfile_objfile)
+  if (objfile == current_program_space->symfile_object_file)
     {
       (*objfile->sf->sym_new_init) (objfile);
     }
diff --git a/gdb/target.c b/gdb/target.c
index cd66675e8a4..277e6072e55 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -1838,9 +1838,12 @@ info_target_command (const char *args, int from_tty)
 {
   int has_all_mem = 0;
 
-  if (symfile_objfile != NULL)
-    printf_unfiltered (_("Symbols from \"%s\".\n"),
-       objfile_name (symfile_objfile));
+  if (current_program_space->symfile_object_file != NULL)
+    {
+      objfile *objf = current_program_space->symfile_object_file;
+      printf_unfiltered (_("Symbols from \"%s\".\n"),
+ objfile_name (objf));
+    }
 
   for (target_ops *t = current_top_target (); t != NULL; t = t->beneath ())
     {
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index 6fcb629fe73..f28fd5c030c 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -908,12 +908,13 @@ windows_solib_create_inferior_hook (int from_tty)
     }
 
   /* Rebase executable if the base address changed because of ASLR.  */
-  if (symfile_objfile != nullptr && exec_base != 0)
+  if (current_program_space->symfile_object_file != nullptr && exec_base != 0)
     {
       CORE_ADDR vmaddr
  = pe_data (current_program_space->exec_bfd ())->pe_opthdr.ImageBase;
       if (vmaddr != exec_base)
- objfile_rebase (symfile_objfile, exec_base - vmaddr);
+ objfile_rebase (current_program_space->symfile_object_file,
+ exec_base - vmaddr);
     }
 }
 
--
2.17.2

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 5/9] Remove current_target_sections macro

Simon Marchi-4
In reply to this post by Tom Tromey-2
On 2020-07-21 11:34 p.m., Tom Tromey wrote:

> This removes the current_target_sections macro, replacing it with uses
> of the appropriate member from current_program_space.
>
> gdb/ChangeLog
> 2020-07-21  Tom Tromey  <[hidden email]>
>
> * progspace.h (current_target_sections): Remove macro.
> * solib-svr4.c (scan_dyntag): Update.
> * solib-dsbt.c (scan_dyntag): Update.
> * exec.c (exec_target::close): Update.
> (add_target_sections, add_target_sections_of_objfile)
> (remove_target_sections, exec_target::get_section_table)
> (exec_target::files_info, set_section_command)
> (exec_set_section_address, exec_target::has_memory)
> (exec_target::has_memory): Update.
> ---
>  gdb/ChangeLog    | 12 ++++++++++++
>  gdb/exec.c       | 23 +++++++++++++----------
>  gdb/progspace.h  |  4 ----
>  gdb/solib-dsbt.c |  6 +++---
>  gdb/solib-svr4.c |  6 +++---
>  5 files changed, 31 insertions(+), 20 deletions(-)
>
> diff --git a/gdb/exec.c b/gdb/exec.c
> index 6ca867ab53e..e2a0cae787c 100644
> --- a/gdb/exec.c
> +++ b/gdb/exec.c
> @@ -159,7 +159,7 @@ exec_target::close ()
>    for (struct program_space *ss : program_spaces)
>      {
>        set_current_program_space (ss);
> -      clear_section_table (current_target_sections);
> +      clear_section_table (&ss->target_sections);
>        ss->exec_close ();

I hoped that the call to set_current_program_space could be removed, but no.  exec_close
ends up calling remove_target_sections, which uses current_program_space.

But once we make remove_target_sections take a program_space parameter, I think it will
be fine to remove it.

Simon

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 6/9] Don't change current program space in exec_target::close

Simon Marchi-4
In reply to this post by Tom Tromey-2
On 2020-07-21 11:34 p.m., Tom Tromey wrote:

> Now that we've removed the macros and changed exec_close to be a
> method, it's clear that exec_target::close can operate on program
> spaces without changing the current program space.
>
> gdb/ChangeLog
> 2020-07-21  Tom Tromey  <[hidden email]>
>
> * exec.c (exec_target::close): Don't change current program
> space.
> ---
>  gdb/ChangeLog | 5 +++++
>  gdb/exec.c    | 3 ---
>  2 files changed, 5 insertions(+), 3 deletions(-)
>
> diff --git a/gdb/exec.c b/gdb/exec.c
> index e2a0cae787c..b2c7a58b27f 100644
> --- a/gdb/exec.c
> +++ b/gdb/exec.c
> @@ -154,11 +154,8 @@ exec_target_open (const char *args, int from_tty)
>  void
>  exec_target::close ()
>  {
> -  scoped_restore_current_program_space restore_pspace;
> -
>    for (struct program_space *ss : program_spaces)
>      {
> -      set_current_program_space (ss);
>        clear_section_table (&ss->target_sections);
>        ss->exec_close ();
>      }

Ah, forget my comment on last patch.

So yeah, ss->exec_close calls remove_target_sections, which uses current_program_space.

I think that would need to be changed first, but there doesn't seem to be any blocker
for that.

Simon
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 0/9] Remove some macros from exec.h and progspace.h

Simon Marchi-4
In reply to this post by Tom Tromey-2
On 2020-07-21 11:34 p.m., Tom Tromey wrote:

> This series removes some macros from exec.h and progspace.h.  In
> particular, macros that hide that some global variable is used are
> removed.
>
> I find these macros somewhat obscure.  I think patches #2 and #6 show
> why -- it turns out that some code here was easily parameterized and
> did not need to use a global, or even save/restore the current program
> space.
>
> Let me know what you think.  Like the other series along these lines,
> I don't see any need to check them in before the branch is made.

Thanks for doing this!  That makes the dependency on current_program_space
more apparent.  I'm sure in many cases it will be possible to change the
reference to current_program_space to be a parameter, your patch enables
that.

Simon
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 5/9] Remove current_target_sections macro

Simon Marchi-4
In reply to this post by Simon Marchi-4
On 2020-07-22 8:20 a.m., Simon Marchi wrote:

> On 2020-07-21 11:34 p.m., Tom Tromey wrote:
>> This removes the current_target_sections macro, replacing it with uses
>> of the appropriate member from current_program_space.
>>
>> gdb/ChangeLog
>> 2020-07-21  Tom Tromey  <[hidden email]>
>>
>> * progspace.h (current_target_sections): Remove macro.
>> * solib-svr4.c (scan_dyntag): Update.
>> * solib-dsbt.c (scan_dyntag): Update.
>> * exec.c (exec_target::close): Update.
>> (add_target_sections, add_target_sections_of_objfile)
>> (remove_target_sections, exec_target::get_section_table)
>> (exec_target::files_info, set_section_command)
>> (exec_set_section_address, exec_target::has_memory)
>> (exec_target::has_memory): Update.
>> ---
>>  gdb/ChangeLog    | 12 ++++++++++++
>>  gdb/exec.c       | 23 +++++++++++++----------
>>  gdb/progspace.h  |  4 ----
>>  gdb/solib-dsbt.c |  6 +++---
>>  gdb/solib-svr4.c |  6 +++---
>>  5 files changed, 31 insertions(+), 20 deletions(-)
>>
>> diff --git a/gdb/exec.c b/gdb/exec.c
>> index 6ca867ab53e..e2a0cae787c 100644
>> --- a/gdb/exec.c
>> +++ b/gdb/exec.c
>> @@ -159,7 +159,7 @@ exec_target::close ()
>>    for (struct program_space *ss : program_spaces)
>>      {
>>        set_current_program_space (ss);
>> -      clear_section_table (current_target_sections);
>> +      clear_section_table (&ss->target_sections);
>>        ss->exec_close ();
>
> I hoped that the call to set_current_program_space could be removed, but no.  exec_close
> ends up calling remove_target_sections, which uses current_program_space.
>
> But once we make remove_target_sections take a program_space parameter, I think it will
> be fine to remove it.

This is handled in the next patch, so forget it.  Although I get a build failure on this
patch:

/home/smarchi/src/binutils-gdb/gdb/solib-dsbt.c: In function ‘int scan_dyntag(int, bfd*, CORE_ADDR*)’:
/home/smarchi/src/binutils-gdb/gdb/solib-dsbt.c:427:63: error: base operand of ‘->’ has non-pointer type ‘target_section_table’
  427 |   for (target_section = current_program_space->target_sections->sections;
      |                                                               ^~

Simon
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 5/9] Remove current_target_sections macro

Tom Tromey-2
>>>>> "Simon" == Simon Marchi <[hidden email]> writes:

Simon> This is handled in the next patch, so forget it.  Although I get a build failure on this
Simon> patch:

Simon> /home/smarchi/src/binutils-gdb/gdb/solib-dsbt.c: In function ‘int scan_dyntag(int, bfd*, CORE_ADDR*)’:
Simon> /home/smarchi/src/binutils-gdb/gdb/solib-dsbt.c:427:63: error: base operand of ‘->’ has non-pointer type ‘target_section_table’
Simon>   427 |   for (target_section = current_program_space->target_sections->sections;
Simon>       |                                                               ^~

Oops, either I sent the wrong version, or the fix ended up in a
different patch.  I'll have to check; but either way I'll fix it.

Tom