[05/12] Remove ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS

Message ID 20181125165439.13773-6-tom@tromey.com
State New
Headers show
Series
  • Remove some ALL_* iteration macros
Related show

Commit Message

Tom Tromey Nov. 25, 2018, 4:54 p.m.
This removes the ALL_MSYMBOLS and ALL_OBJFILE_MSYMBOLS macros,
replacing their uses with ranged for loops.

In a couple of spots, a new declaration was needed in order to work
around shadowing; these are just temporary and are removed in a
subsequent patch.

gdb/ChangeLog
2018-11-25  Tom Tromey  <tom@tromey.com>

	* symtab.c (search_symbols)
	(default_collect_symbol_completion_matches_break_on): Use
	objfile_msymbols.
	* ada-lang.c (ada_lookup_simple_minsym)
	(ada_collect_symbol_completion_matches): Use objfile_msymbols.
	* minsyms.c (find_solib_trampoline_target): Use objfile_msymbols.
	* hppa-tdep.c (hppa_lookup_stub_minimal_symbol): Use
	objfile_msymbols.
	* coffread.c (coff_symfile_read): Use objfile_msymbols.
	* symmisc.c (dump_msymbols): Use objfile_msymbols.
	* objc-lang.c (find_methods): Use objfile_msymbols.
	(info_selectors_command, info_classes_command): Likewise.
	* stabsread.c (scan_file_globals): Use objfile_msymbols.
	* objfiles.h (class objfile_msymbols): New.
	(ALL_OBJFILE_MSYMBOLS): Remove.
	(ALL_MSYMBOLS): Remove.
---
 gdb/ChangeLog   |  19 +++++
 gdb/ada-lang.c  |  77 +++++++++--------
 gdb/coffread.c  |   4 +-
 gdb/hppa-tdep.c |  31 ++++---
 gdb/minsyms.c   |  35 ++++----
 gdb/objc-lang.c | 153 +++++++++++++++++-----------------
 gdb/objfiles.h  |  90 +++++++++++++++++---
 gdb/stabsread.c |   3 +-
 gdb/symmisc.c   |   3 +-
 gdb/symtab.c    | 213 +++++++++++++++++++++++++-----------------------
 10 files changed, 357 insertions(+), 271 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 160184ddb3..0520ec144a 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -4913,8 +4913,6 @@  struct bound_minimal_symbol
 ada_lookup_simple_minsym (const char *name)
 {
   struct bound_minimal_symbol result;
-  struct objfile *objfile;
-  struct minimal_symbol *msymbol;
 
   memset (&result, 0, sizeof (result));
 
@@ -4924,16 +4922,17 @@  ada_lookup_simple_minsym (const char *name)
   symbol_name_matcher_ftype *match_name
     = ada_get_symbol_name_matcher (lookup_name);
 
-  ALL_MSYMBOLS (objfile, msymbol)
-  {
-    if (match_name (MSYMBOL_LINKAGE_NAME (msymbol), lookup_name, NULL)
-        && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
       {
-	result.minsym = msymbol;
-	result.objfile = objfile;
-	break;
+	if (match_name (MSYMBOL_LINKAGE_NAME (msymbol), lookup_name, NULL)
+	    && MSYMBOL_TYPE (msymbol) != mst_solib_trampoline)
+	  {
+	    result.minsym = msymbol;
+	    result.objfile = objfile;
+	    break;
+	  }
       }
-  }
 
   return result;
 }
@@ -6391,8 +6390,6 @@  ada_collect_symbol_completion_matches (completion_tracker &tracker,
 {
   struct symbol *sym;
   struct compunit_symtab *s;
-  struct minimal_symbol *msymbol;
-  struct objfile *objfile;
   const struct block *b, *surrounding_static_block = 0;
   struct block_iterator iter;
 
@@ -6412,35 +6409,36 @@  ada_collect_symbol_completion_matches (completion_tracker &tracker,
      anything that isn't a text symbol (everything else will be
      handled by the psymtab code above).  */
 
-  ALL_MSYMBOLS (objfile, msymbol)
-  {
-    QUIT;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+      {
+	QUIT;
 
-    if (completion_skip_symbol (mode, msymbol))
-      continue;
+	if (completion_skip_symbol (mode, msymbol))
+	  continue;
 
-    language symbol_language = MSYMBOL_LANGUAGE (msymbol);
-
-    /* Ada minimal symbols won't have their language set to Ada.  If
-       we let completion_list_add_name compare using the
-       default/C-like matcher, then when completing e.g., symbols in a
-       package named "pck", we'd match internal Ada symbols like
-       "pckS", which are invalid in an Ada expression, unless you wrap
-       them in '<' '>' to request a verbatim match.
-
-       Unfortunately, some Ada encoded names successfully demangle as
-       C++ symbols (using an old mangling scheme), such as "name__2Xn"
-       -> "Xn::name(void)" and thus some Ada minimal symbols end up
-       with the wrong language set.  Paper over that issue here.  */
-    if (symbol_language == language_auto
-	|| symbol_language == language_cplus)
-      symbol_language = language_ada;
-
-    completion_list_add_name (tracker,
-			      symbol_language,
-			      MSYMBOL_LINKAGE_NAME (msymbol),
-			      lookup_name, text, word);
-  }
+	language symbol_language = MSYMBOL_LANGUAGE (msymbol);
+
+	/* Ada minimal symbols won't have their language set to Ada.  If
+	   we let completion_list_add_name compare using the
+	   default/C-like matcher, then when completing e.g., symbols in a
+	   package named "pck", we'd match internal Ada symbols like
+	   "pckS", which are invalid in an Ada expression, unless you wrap
+	   them in '<' '>' to request a verbatim match.
+
+	   Unfortunately, some Ada encoded names successfully demangle as
+	   C++ symbols (using an old mangling scheme), such as "name__2Xn"
+	   -> "Xn::name(void)" and thus some Ada minimal symbols end up
+	   with the wrong language set.  Paper over that issue here.  */
+	if (symbol_language == language_auto
+	    || symbol_language == language_cplus)
+	  symbol_language = language_ada;
+
+	completion_list_add_name (tracker,
+				  symbol_language,
+				  MSYMBOL_LINKAGE_NAME (msymbol),
+				  lookup_name, text, word);
+      }
 
   /* Search upwards from currently selected frame (so that we can
      complete on local vars.  */
@@ -6465,6 +6463,7 @@  ada_collect_symbol_completion_matches (completion_tracker &tracker,
   /* Go through the symtabs and check the externs and statics for
      symbols which match.  */
 
+  struct objfile *objfile;
   ALL_COMPUNITS (objfile, s)
   {
     QUIT;
diff --git a/gdb/coffread.c b/gdb/coffread.c
index a473b78245..401aacbbe2 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -661,9 +661,7 @@  coff_symfile_read (struct objfile *objfile, symfile_add_flags symfile_flags)
 
   if (pe_file)
     {
-      struct minimal_symbol *msym;
-
-      ALL_OBJFILE_MSYMBOLS (objfile, msym)
+      for (struct minimal_symbol *msym : objfile_msymbols (objfile))
 	{
 	  const char *name = MSYMBOL_LINKAGE_NAME (msym);
 
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index 9f70ef9b73..6e8c2d64fe 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -2540,25 +2540,24 @@  struct bound_minimal_symbol
 hppa_lookup_stub_minimal_symbol (const char *name,
                                  enum unwind_stub_types stub_type)
 {
-  struct objfile *objfile;
-  struct minimal_symbol *msym;
   struct bound_minimal_symbol result = { NULL, NULL };
 
-  ALL_MSYMBOLS (objfile, msym)
-    {
-      if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
-        {
-          struct unwind_table_entry *u;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msym : objfile_msymbols (objfile))
+      {
+	if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
+	  {
+	    struct unwind_table_entry *u;
 
-          u = find_unwind_entry (MSYMBOL_VALUE (msym));
-          if (u != NULL && u->stub_unwind.stub_type == stub_type)
-	    {
-	      result.objfile = objfile;
-	      result.minsym = msym;
-	      return result;
-	    }
-        }
-    }
+	    u = find_unwind_entry (MSYMBOL_VALUE (msym));
+	    if (u != NULL && u->stub_unwind.stub_type == stub_type)
+	      {
+		result.objfile = objfile;
+		result.minsym = msym;
+		return result;
+	      }
+	  }
+      }
 
   return result;
 }
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 0f854422e0..f2d474efe4 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -1457,30 +1457,29 @@  lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
 CORE_ADDR
 find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
 {
-  struct objfile *objfile;
-  struct minimal_symbol *msymbol;
   struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
 
   if (tsymbol != NULL)
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-      {
-	/* Also handle minimal symbols pointing to function descriptors.  */
-	if ((MSYMBOL_TYPE (msymbol) == mst_text
-	     || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
-	     || MSYMBOL_TYPE (msymbol) == mst_data
-	     || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
-	    && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
-		       MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
 	  {
-	    CORE_ADDR func;
-
-	    /* Ignore data symbols that are not function
-	       descriptors.  */
-	    if (msymbol_is_function (objfile, msymbol, &func))
-	      return func;
+	    /* Also handle minimal symbols pointing to function descriptors.  */
+	    if ((MSYMBOL_TYPE (msymbol) == mst_text
+		 || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
+		 || MSYMBOL_TYPE (msymbol) == mst_data
+		 || MSYMBOL_TYPE (msymbol) == mst_data_gnu_ifunc)
+		&& strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
+			   MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
+	      {
+		CORE_ADDR func;
+
+		/* Ignore data symbols that are not function
+		   descriptors.  */
+		if (msymbol_is_function (objfile, msymbol, &func))
+		  return func;
+	      }
 	  }
-      }
     }
   return 0;
 }
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index 4933e0c343..c9fab64702 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -562,8 +562,6 @@  compare_selectors (const void *a, const void *b)
 static void
 info_selectors_command (const char *regexp, int from_tty)
 {
-  struct objfile	*objfile;
-  struct minimal_symbol *msymbol;
   const char            *name;
   char                  *val;
   int                    matches = 0;
@@ -607,36 +605,37 @@  info_selectors_command (const char *regexp, int from_tty)
     }
 
   /* First time thru is JUST to get max length and count.  */
-  ALL_MSYMBOLS (objfile, msymbol)
-    {
-      QUIT;
-      name = MSYMBOL_NATURAL_NAME (msymbol);
-      if (name
-          && (name[0] == '-' || name[0] == '+')
-	  && name[1] == '[')		/* Got a method name.  */
-	{
-	  /* Filter for class/instance methods.  */
-	  if (plusminus && name[0] != plusminus)
-	    continue;
-	  /* Find selector part.  */
-	  name = (char *) strchr (name+2, ' ');
-	  if (name == NULL)
-	    {
-	      complaint (_("Bad method name '%s'"),
-			 MSYMBOL_NATURAL_NAME (msymbol));
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+      {
+	QUIT;
+	name = MSYMBOL_NATURAL_NAME (msymbol);
+	if (name
+	    && (name[0] == '-' || name[0] == '+')
+	    && name[1] == '[')		/* Got a method name.  */
+	  {
+	    /* Filter for class/instance methods.  */
+	    if (plusminus && name[0] != plusminus)
 	      continue;
-	    }
-	  if (regexp == NULL || re_exec(++name) != 0)
-	    { 
-	      const char *mystart = name;
-	      const char *myend   = strchr (mystart, ']');
+	    /* Find selector part.  */
+	    name = (char *) strchr (name+2, ' ');
+	    if (name == NULL)
+	      {
+		complaint (_("Bad method name '%s'"),
+			   MSYMBOL_NATURAL_NAME (msymbol));
+		continue;
+	      }
+	    if (regexp == NULL || re_exec(++name) != 0)
+	      { 
+		const char *mystart = name;
+		const char *myend   = strchr (mystart, ']');
 	      
-	      if (myend && (myend - mystart > maxlen))
-		maxlen = myend - mystart;	/* Get longest selector.  */
-	      matches++;
-	    }
-	}
-    }
+		if (myend && (myend - mystart > maxlen))
+		  maxlen = myend - mystart;	/* Get longest selector.  */
+		matches++;
+	      }
+	  }
+      }
   if (matches)
     {
       printf_filtered (_("Selectors matching \"%s\":\n\n"), 
@@ -644,23 +643,24 @@  info_selectors_command (const char *regexp, int from_tty)
 
       sym_arr = XALLOCAVEC (struct symbol *, matches);
       matches = 0;
-      ALL_MSYMBOLS (objfile, msymbol)
-	{
-	  QUIT;
-	  name = MSYMBOL_NATURAL_NAME (msymbol);
-	  if (name &&
-	     (name[0] == '-' || name[0] == '+') &&
-	      name[1] == '[')		/* Got a method name.  */
-	    {
-	      /* Filter for class/instance methods.  */
-	      if (plusminus && name[0] != plusminus)
-		continue;
-	      /* Find selector part.  */
-	      name = (char *) strchr(name+2, ' ');
-	      if (regexp == NULL || re_exec(++name) != 0)
-		sym_arr[matches++] = (struct symbol *) msymbol;
-	    }
-	}
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
+	    name = MSYMBOL_NATURAL_NAME (msymbol);
+	    if (name &&
+		(name[0] == '-' || name[0] == '+') &&
+		name[1] == '[')		/* Got a method name.  */
+	      {
+		/* Filter for class/instance methods.  */
+		if (plusminus && name[0] != plusminus)
+		  continue;
+		/* Find selector part.  */
+		name = (char *) strchr(name+2, ' ');
+		if (regexp == NULL || re_exec(++name) != 0)
+		  sym_arr[matches++] = (struct symbol *) msymbol;
+	      }
+	  }
 
       qsort (sym_arr, matches, sizeof (struct minimal_symbol *), 
 	     compare_selectors);
@@ -723,8 +723,6 @@  compare_classes (const void *a, const void *b)
 static void
 info_classes_command (const char *regexp, int from_tty)
 {
-  struct objfile	*objfile;
-  struct minimal_symbol *msymbol;
   const char            *name;
   char                  *val;
   int                    matches = 0;
@@ -757,40 +755,42 @@  info_classes_command (const char *regexp, int from_tty)
     }
 
   /* First time thru is JUST to get max length and count.  */
-  ALL_MSYMBOLS (objfile, msymbol)
-    {
-      QUIT;
-      name = MSYMBOL_NATURAL_NAME (msymbol);
-      if (name &&
-	 (name[0] == '-' || name[0] == '+') &&
-	  name[1] == '[')			/* Got a method name.  */
-	if (regexp == NULL || re_exec(name+2) != 0)
-	  { 
-	    /* Compute length of classname part.  */
-	    const char *mystart = name + 2;
-	    const char *myend   = strchr (mystart, ' ');
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+      {
+	QUIT;
+	name = MSYMBOL_NATURAL_NAME (msymbol);
+	if (name &&
+	    (name[0] == '-' || name[0] == '+') &&
+	    name[1] == '[')			/* Got a method name.  */
+	  if (regexp == NULL || re_exec(name+2) != 0)
+	    { 
+	      /* Compute length of classname part.  */
+	      const char *mystart = name + 2;
+	      const char *myend   = strchr (mystart, ' ');
 	    
-	    if (myend && (myend - mystart > maxlen))
-	      maxlen = myend - mystart;
-	    matches++;
-	  }
-    }
+	      if (myend && (myend - mystart > maxlen))
+		maxlen = myend - mystart;
+	      matches++;
+	    }
+      }
   if (matches)
     {
       printf_filtered (_("Classes matching \"%s\":\n\n"), 
 		       regexp ? regexp : "*");
       sym_arr = XALLOCAVEC (struct symbol *, matches);
       matches = 0;
-      ALL_MSYMBOLS (objfile, msymbol)
-	{
-	  QUIT;
-	  name = MSYMBOL_NATURAL_NAME (msymbol);
-	  if (name &&
-	     (name[0] == '-' || name[0] == '+') &&
-	      name[1] == '[')			/* Got a method name.  */
-	    if (regexp == NULL || re_exec(name+2) != 0)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
+	    name = MSYMBOL_NATURAL_NAME (msymbol);
+	    if (name &&
+		(name[0] == '-' || name[0] == '+') &&
+		name[1] == '[')			/* Got a method name.  */
+	      if (regexp == NULL || re_exec(name+2) != 0)
 		sym_arr[matches++] = (struct symbol *) msymbol;
-	}
+	  }
 
       qsort (sym_arr, matches, sizeof (struct minimal_symbol *), 
 	     compare_classes);
@@ -987,7 +987,6 @@  find_methods (char type, const char *theclass, const char *category,
   for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       unsigned int *objc_csym;
-      struct minimal_symbol *msymbol = NULL;
 
       /* The objfile_csym variable counts the number of ObjC methods
 	 that this objfile defines.  We save that count as a private
@@ -1001,7 +1000,7 @@  find_methods (char type, const char *theclass, const char *category,
 	/* There are no ObjC symbols in this objfile.  Skip it entirely.  */
 	continue;
 
-      ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
+      for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
 	{
 	  QUIT;
 
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 1d310e5584..b24145f0ca 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -623,12 +623,85 @@  public:
 #define ALL_OBJFILE_COMPUNITS(objfile, cu) \
   for ((cu) = (objfile) -> compunit_symtabs; (cu) != NULL; (cu) = (cu) -> next)
 
-/* Traverse all minimal symbols in one objfile.  */
+/* A range adapter that makes it possible to iterate over all
+   minimal symbols of an objfile.  */
 
-#define	ALL_OBJFILE_MSYMBOLS(objfile, m)	\
-    for ((m) = (objfile)->per_bfd->msymbols;	\
-	 MSYMBOL_LINKAGE_NAME (m) != NULL;	\
-	 (m)++)
+class objfile_msymbols
+{
+public:
+
+  explicit objfile_msymbols (struct objfile *objfile)
+    : m_objfile (objfile)
+  {
+  }
+
+  struct iterator
+  {
+    typedef iterator self_type;
+    typedef struct minimal_symbol *value_type;
+    typedef struct minimal_symbol *&reference;
+    typedef struct minimal_symbol **pointer;
+    typedef std::forward_iterator_tag iterator_category;
+    typedef int difference_type;
+
+    explicit iterator (struct objfile *objfile)
+      : m_msym (objfile->per_bfd->msymbols)
+    {
+      /* Make sure to properly handle the case where there are no
+	 minsyms.  */
+      if (MSYMBOL_LINKAGE_NAME (m_msym) == nullptr)
+	m_msym = nullptr;
+    }
+
+    iterator ()
+      : m_msym (nullptr)
+    {
+    }
+    
+    value_type operator* () const
+    {
+      return m_msym;
+    }
+
+    bool operator== (const self_type &other) const
+    {
+      return m_msym == other.m_msym;
+    }
+
+    bool operator!= (const self_type &other) const
+    {
+      return m_msym != other.m_msym;
+    }
+
+    self_type &operator++ ()
+    {
+      if (m_msym != nullptr)
+	{
+	  ++m_msym;
+	  if (MSYMBOL_LINKAGE_NAME (m_msym) == nullptr)
+	    m_msym = nullptr;
+	}
+      return *this;
+    }
+
+  private:
+    struct minimal_symbol *m_msym;
+  };
+
+  iterator begin () const
+  {
+    return iterator (m_objfile);
+  }
+
+  iterator end () const
+  {
+    return iterator ();
+  }
+
+private:
+
+  struct objfile *m_objfile;
+};
 
 /* Traverse all symtabs in all objfiles in the current symbol
    space.  */
@@ -643,13 +716,6 @@  public:
   ALL_OBJFILES (objfile)		\
     ALL_OBJFILE_COMPUNITS (objfile, cu)
 
-/* Traverse all minimal symbols in all objfiles in the current symbol
-   space.  */
-
-#define	ALL_MSYMBOLS(objfile, m) \
-  ALL_OBJFILES (objfile)	 \
-    ALL_OBJFILE_MSYMBOLS (objfile, m)
-
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
   for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
     if (osect->the_bfd_section == NULL)					\
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index d355e54148..c1953ee54a 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -4593,7 +4593,6 @@  void
 scan_file_globals (struct objfile *objfile)
 {
   int hash;
-  struct minimal_symbol *msymbol;
   struct symbol *sym, *prev;
   struct objfile *resolve_objfile;
 
@@ -4619,7 +4618,7 @@  scan_file_globals (struct objfile *objfile)
       if (hash >= HASHSIZE)
 	return;
 
-      ALL_OBJFILE_MSYMBOLS (resolve_objfile, msymbol)
+      for (struct minimal_symbol *msymbol : objfile_msymbols (resolve_objfile))
 	{
 	  QUIT;
 
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index ac4552d478..ebad334d6c 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -183,7 +183,6 @@  static void
 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
 {
   struct gdbarch *gdbarch = get_objfile_arch (objfile);
-  struct minimal_symbol *msymbol;
   int index;
   char ms_type;
 
@@ -194,7 +193,7 @@  dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
       return;
     }
   index = 0;
-  ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
+  for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
     {
       struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 041b7f2ead..73ab0cb0b7 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -4344,8 +4344,6 @@  search_symbols (const char *regexp, enum search_domain kind,
   int i = 0;
   struct block_iterator iter;
   struct symbol *sym;
-  struct objfile *objfile;
-  struct minimal_symbol *msymbol;
   int found_misc = 0;
   static const enum minimal_symbol_type types[]
     = {mst_data, mst_text, mst_abs};
@@ -4454,81 +4452,89 @@  search_symbols (const char *regexp, enum search_domain kind,
 
   if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-      {
-        QUIT;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
 
-	if (msymbol->created_by_gdb)
-	  continue;
+	    if (msymbol->created_by_gdb)
+	      continue;
 
-	if (MSYMBOL_TYPE (msymbol) == ourtype
-	    || MSYMBOL_TYPE (msymbol) == ourtype2
-	    || MSYMBOL_TYPE (msymbol) == ourtype3
-	    || MSYMBOL_TYPE (msymbol) == ourtype4)
-	  {
-	    if (!preg.has_value ()
-		|| preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
-			       NULL, 0) == 0)
+	    if (MSYMBOL_TYPE (msymbol) == ourtype
+		|| MSYMBOL_TYPE (msymbol) == ourtype2
+		|| MSYMBOL_TYPE (msymbol) == ourtype3
+		|| MSYMBOL_TYPE (msymbol) == ourtype4)
 	      {
-		/* Note: An important side-effect of these lookup functions
-		   is to expand the symbol table if msymbol is found, for the
-		   benefit of the next loop on ALL_COMPUNITS.  */
-		if (kind == FUNCTIONS_DOMAIN
-		    ? (find_pc_compunit_symtab
-		       (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL)
-		    : (lookup_symbol_in_objfile_from_linkage_name
-		       (objfile, MSYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
-		       .symbol == NULL))
-		  found_misc = 1;
+		if (!preg.has_value ()
+		    || preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
+				   NULL, 0) == 0)
+		  {
+		    /* Note: An important side-effect of these lookup functions
+		       is to expand the symbol table if msymbol is found, for the
+		       benefit of the next loop on ALL_COMPUNITS.  */
+		    if (kind == FUNCTIONS_DOMAIN
+			? (find_pc_compunit_symtab
+			   (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL)
+			: (lookup_symbol_in_objfile_from_linkage_name
+			   (objfile, MSYMBOL_LINKAGE_NAME (msymbol),
+			    VAR_DOMAIN)
+			   .symbol == NULL))
+		      found_misc = 1;
+		  }
 	      }
 	  }
-      }
     }
 
-  ALL_COMPUNITS (objfile, cust)
   {
-    bv = COMPUNIT_BLOCKVECTOR (cust);
-    for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
+    struct objfile *objfile;
+    ALL_COMPUNITS (objfile, cust)
       {
-	b = BLOCKVECTOR_BLOCK (bv, i);
-	ALL_BLOCK_SYMBOLS (b, iter, sym)
+	bv = COMPUNIT_BLOCKVECTOR (cust);
+	for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
 	  {
-	    struct symtab *real_symtab = symbol_symtab (sym);
-
-	    QUIT;
-
-	    /* Check first sole REAL_SYMTAB->FILENAME.  It does not need to be
-	       a substring of symtab_to_fullname as it may contain "./" etc.  */
-	    if ((file_matches (real_symtab->filename, files, nfiles, 0)
-		 || ((basenames_may_differ
-		      || file_matches (lbasename (real_symtab->filename),
-				       files, nfiles, 1))
-		     && file_matches (symtab_to_fullname (real_symtab),
-				      files, nfiles, 0)))
-		&& ((!preg.has_value ()
-		     || preg->exec (SYMBOL_NATURAL_NAME (sym), 0,
-				    NULL, 0) == 0)
-		    && ((kind == VARIABLES_DOMAIN
-			 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
-			 && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
-			 && SYMBOL_CLASS (sym) != LOC_BLOCK
-			 /* LOC_CONST can be used for more than just enums,
-			    e.g., c++ static const members.
-			    We only want to skip enums here.  */
-			 && !(SYMBOL_CLASS (sym) == LOC_CONST
-			      && (TYPE_CODE (SYMBOL_TYPE (sym))
-				  == TYPE_CODE_ENUM))
-			 && (!treg.has_value ()
-			     || treg_matches_sym_type_name (*treg, sym)))
-			|| (kind == FUNCTIONS_DOMAIN
-			    && SYMBOL_CLASS (sym) == LOC_BLOCK
-			    && (!treg.has_value ()
-				|| treg_matches_sym_type_name (*treg, sym)))
-			|| (kind == TYPES_DOMAIN
-			    && SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
+	    b = BLOCKVECTOR_BLOCK (bv, i);
+	    ALL_BLOCK_SYMBOLS (b, iter, sym)
 	      {
-		/* match */
-		result.emplace_back (i, sym);
+		struct symtab *real_symtab = symbol_symtab (sym);
+
+		QUIT;
+
+		/* Check first sole REAL_SYMTAB->FILENAME.  It does
+		   not need to be a substring of symtab_to_fullname as
+		   it may contain "./" etc.  */
+		if ((file_matches (real_symtab->filename, files, nfiles, 0)
+		     || ((basenames_may_differ
+			  || file_matches (lbasename (real_symtab->filename),
+					   files, nfiles, 1))
+			 && file_matches (symtab_to_fullname (real_symtab),
+					  files, nfiles, 0)))
+		    && ((!preg.has_value ()
+			 || preg->exec (SYMBOL_NATURAL_NAME (sym), 0,
+					NULL, 0) == 0)
+			&& ((kind == VARIABLES_DOMAIN
+			     && SYMBOL_CLASS (sym) != LOC_TYPEDEF
+			     && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
+			     && SYMBOL_CLASS (sym) != LOC_BLOCK
+			     /* LOC_CONST can be used for more than
+				just enums, e.g., c++ static const
+				members.  We only want to skip enums
+				here.  */
+			     && !(SYMBOL_CLASS (sym) == LOC_CONST
+				  && (TYPE_CODE (SYMBOL_TYPE (sym))
+				      == TYPE_CODE_ENUM))
+			     && (!treg.has_value ()
+				 || treg_matches_sym_type_name (*treg, sym)))
+			    || (kind == FUNCTIONS_DOMAIN
+				&& SYMBOL_CLASS (sym) == LOC_BLOCK
+				&& (!treg.has_value ()
+				    || treg_matches_sym_type_name (*treg,
+								   sym)))
+			    || (kind == TYPES_DOMAIN
+				&& SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
+		  {
+		    /* match */
+		    result.emplace_back (i, sym);
+		  }
 	      }
 	  }
       }
@@ -4545,39 +4551,42 @@  search_symbols (const char *regexp, enum search_domain kind,
   if ((found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
       && !treg.has_value ())
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-      {
-        QUIT;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
 
-	if (msymbol->created_by_gdb)
-	  continue;
+	    if (msymbol->created_by_gdb)
+	      continue;
 
-	if (MSYMBOL_TYPE (msymbol) == ourtype
-	    || MSYMBOL_TYPE (msymbol) == ourtype2
-	    || MSYMBOL_TYPE (msymbol) == ourtype3
-	    || MSYMBOL_TYPE (msymbol) == ourtype4)
-	  {
-	    if (!preg.has_value ()
-		|| preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
-			       NULL, 0) == 0)
+	    if (MSYMBOL_TYPE (msymbol) == ourtype
+		|| MSYMBOL_TYPE (msymbol) == ourtype2
+		|| MSYMBOL_TYPE (msymbol) == ourtype3
+		|| MSYMBOL_TYPE (msymbol) == ourtype4)
 	      {
-		/* For functions we can do a quick check of whether the
-		   symbol might be found via find_pc_symtab.  */
-		if (kind != FUNCTIONS_DOMAIN
-		    || (find_pc_compunit_symtab
-			(MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL))
+		if (!preg.has_value ()
+		    || preg->exec (MSYMBOL_NATURAL_NAME (msymbol), 0,
+				   NULL, 0) == 0)
 		  {
-		    if (lookup_symbol_in_objfile_from_linkage_name
-			(objfile, MSYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
-			.symbol == NULL)
+		    /* For functions we can do a quick check of whether the
+		       symbol might be found via find_pc_symtab.  */
+		    if (kind != FUNCTIONS_DOMAIN
+			|| (find_pc_compunit_symtab
+			    (MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
+			    == NULL))
 		      {
-			/* match */
-			result.emplace_back (i, msymbol, objfile);
+			if (lookup_symbol_in_objfile_from_linkage_name
+			    (objfile, MSYMBOL_LINKAGE_NAME (msymbol),
+			     VAR_DOMAIN)
+			    .symbol == NULL)
+			  {
+			    /* match */
+			    result.emplace_back (i, msymbol, objfile);
+			  }
 		      }
 		  }
 	      }
 	  }
-      }
     }
 
   return result;
@@ -5188,8 +5197,6 @@  default_collect_symbol_completion_matches_break_on
 
   struct symbol *sym;
   struct compunit_symtab *cust;
-  struct minimal_symbol *msymbol;
-  struct objfile *objfile;
   const struct block *b;
   const struct block *surrounding_static_block, *surrounding_global_block;
   struct block_iterator iter;
@@ -5259,22 +5266,24 @@  default_collect_symbol_completion_matches_break_on
 
   if (code == TYPE_CODE_UNDEF)
     {
-      ALL_MSYMBOLS (objfile, msymbol)
-	{
-	  QUIT;
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	for (struct minimal_symbol *msymbol : objfile_msymbols (objfile))
+	  {
+	    QUIT;
 
-	  if (completion_skip_symbol (mode, msymbol))
-	    continue;
+	    if (completion_skip_symbol (mode, msymbol))
+	      continue;
 
-	  completion_list_add_msymbol (tracker, msymbol, lookup_name,
-				       sym_text, word);
+	    completion_list_add_msymbol (tracker, msymbol, lookup_name,
+					 sym_text, word);
 
-	  completion_list_objc_symbol (tracker, msymbol, lookup_name,
-				       sym_text, word);
-	}
+	    completion_list_objc_symbol (tracker, msymbol, lookup_name,
+					 sym_text, word);
+	  }
     }
 
   /* Add completions for all currently loaded symbol tables.  */
+  struct objfile *objfile;
   ALL_COMPUNITS (objfile, cust)
     add_symtab_completions (cust, tracker, mode, lookup_name,
 			    sym_text, word, code);