[03/12] Remove most uses of ALL_OBJFILES

Message ID 20181125165439.13773-4-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 most uses of ALL_OBJFILES, replacing them with ranged for
loops.  The remaining uses are all in macros, and will be removed in
subsequent patches.

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

	* symtab.c (iterate_over_symtabs, matching_obj_sections)
	(expand_symtab_containing_pc, lookup_static_symbol)
	(basic_lookup_transparent_type, find_pc_sect_compunit_symtab)
	(find_symbol_at_address, find_line_symtab, find_main_name): Use
	all_objfiles.
	* probe.c (find_probe_by_pc, collect_probes): Use all_objfiles.
	* breakpoint.c (create_overlay_event_breakpoint)
	(create_longjmp_master_breakpoint)
	(create_std_terminate_master_breakpoint)
	(create_exception_master_breakpoint): Use all_objfiles.
	* linux-thread-db.c (try_thread_db_load_from_pdir)
	(has_libpthread): Use all_objfiles.
	* ada-lang.c (add_nonlocal_symbols): Use all_objfiles.
	* linespec.c (iterate_over_all_matching_symtabs)
	(search_minsyms_for_name): Use all_objfiles.
	* maint.c (maintenance_info_sections): Use all_objfiles.
	* main.c (captured_main_1): Use all_objfiles.
	* spu-tdep.c (spu_objfile_from_frame): Use all_objfiles.
	* guile/scm-objfile.c (gdbscm_objfiles): Use all_objfiles.
	* guile/scm-pretty-print.c
	(ppscm_find_pretty_printer_from_objfiles): Use all_objfiles.
	* solib-spu.c (append_ocl_sos): Use all_objfiles.
	* symmisc.c (maintenance_print_symbols): Use all_objfiles.
	(maintenance_print_msymbols): Use all_objfiles.
	* source.c (select_source_symtab): Use all_objfiles.
	* jit.c (jit_find_objf_with_entry_addr): Use all_objfiles.
	* symfile.c (remove_symbol_file_command)
	(expand_symtabs_matching, map_symbol_filenames): Use
	all_objfiles.
	* ppc-linux-tdep.c (ppc_linux_spe_context_inferior_created): Use
	all_objfiles.
	* dwarf2-frame.c (dwarf2_frame_find_fde): Use all_objfiles.
	* objc-lang.c (find_methods): Use all_objfiles.
	* objfiles.c (have_partial_symbols, have_full_symbols)
	(have_minimal_symbols, qsort_cmp)
	(default_iterate_over_objfiles_in_search_order): Use
	all_objfiles.
	* hppa-tdep.c (find_unwind_entry): Use all_objfiles.
	* psymtab.c (maintenance_print_psymbols): Use all_objfiles.
	(maintenance_check_psymtabs): Use all_objfiles.
	(ALL_PSYMTABS): Remove.
	* compile/compile-object-run.c (do_module_cleanup): Use
	all_objfiles.
	* blockframe.c (find_pc_partial_function): Use all_objfiles.
	* cp-support.c (add_symbol_overload_list_qualified): Use
	all_objfiles.
	* windows-tdep.c (windows_iterate_over_objfiles_in_search_order):
	Use all_objfiles.
	* dwarf-index-write.c (save_gdb_index_command): Use all_objfiles.
	* python/py-xmethods.c (gdbpy_get_matching_xmethod_workers): Use
	all_objfiles.
	* python/py-objfile.c (objfpy_lookup_objfile_by_name)
	(objfpy_lookup_objfile_by_build_id): Use all_objfiles.
	* python/py-prettyprint.c (find_pretty_printer_from_objfiles):
	Uses all_objfiles.
	* solib.c (solib_read_symbols): Use all_objfiles
---
 gdb/ChangeLog                    |  59 +++++++++
 gdb/ada-lang.c                   |   5 +-
 gdb/blockframe.c                 |  26 ++--
 gdb/breakpoint.c                 | 211 +++++++++++++++----------------
 gdb/compile/compile-object-run.c |   3 +-
 gdb/cp-support.c                 |  10 +-
 gdb/dwarf-index-write.c          |  51 ++++----
 gdb/dwarf2-frame.c               |   4 +-
 gdb/guile/scm-objfile.c          |  11 +-
 gdb/guile/scm-pretty-print.c     |  25 ++--
 gdb/hppa-tdep.c                  |  99 +++++++--------
 gdb/jit.c                        |   4 +-
 gdb/linespec.c                   |  85 ++++++-------
 gdb/linux-thread-db.c            |   8 +-
 gdb/main.c                       |   3 +-
 gdb/maint.c                      |   3 +-
 gdb/objc-lang.c                  |   4 +-
 gdb/objfiles.c                   |  49 +++----
 gdb/ppc-linux-tdep.c             |   4 +-
 gdb/probe.c                      |  38 +++---
 gdb/psymtab.c                    | 159 +++++++++++------------
 gdb/python/py-objfile.c          |   8 +-
 gdb/python/py-prettyprint.c      |  38 +++---
 gdb/python/py-xmethods.c         |   4 +-
 gdb/solib-spu.c                  |   3 +-
 gdb/solib.c                      |  12 +-
 gdb/source.c                     |  14 +-
 gdb/spu-tdep.c                   |   3 +-
 gdb/symfile.c                    |  61 ++++-----
 gdb/symmisc.c                    |  18 ++-
 gdb/symtab.c                     | 192 ++++++++++++++--------------
 gdb/windows-tdep.c               |   3 +-
 32 files changed, 611 insertions(+), 606 deletions(-)

-- 
2.17.2

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 95bf6703ac..160184ddb3 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -5604,7 +5604,6 @@  add_nonlocal_symbols (struct obstack *obstackp,
 		      const lookup_name_info &lookup_name,
 		      domain_enum domain, int global)
 {
-  struct objfile *objfile;
   struct compunit_symtab *cu;
   struct match_data data;
 
@@ -5613,7 +5612,7 @@  add_nonlocal_symbols (struct obstack *obstackp,
 
   bool is_wild_match = lookup_name.ada ().wild_match_p ();
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       data.objfile = objfile;
 
@@ -5646,7 +5645,7 @@  add_nonlocal_symbols (struct obstack *obstackp,
       const char *name = ada_lookup_name (lookup_name);
       std::string name1 = std::string ("<_ada_") + name + '>';
 
-      ALL_OBJFILES (objfile)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
         {
 	  data.objfile = objfile;
 	  objfile->sf->qf->map_matching_symbols (objfile, name1.c_str (),
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
index f6dd861c36..e79490c616 100644
--- a/gdb/blockframe.c
+++ b/gdb/blockframe.c
@@ -217,7 +217,6 @@  find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
   struct symbol *f;
   struct bound_minimal_symbol msymbol;
   struct compunit_symtab *compunit_symtab = NULL;
-  struct objfile *objfile;
   CORE_ADDR mapped_pc;
 
   /* To ensure that the symbol returned belongs to the correct setion
@@ -237,18 +236,19 @@  find_pc_partial_function (CORE_ADDR pc, const char **name, CORE_ADDR *address,
     goto return_cached_value;
 
   msymbol = lookup_minimal_symbol_by_pc_section (mapped_pc, section);
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      {
-	compunit_symtab
-	  = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
-							   mapped_pc, section,
-							   0);
-      }
-    if (compunit_symtab != NULL)
-      break;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	{
+	  compunit_symtab
+	    = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
+							     mapped_pc,
+							     section,
+							     0);
+	}
+      if (compunit_symtab != NULL)
+	break;
+    }
 
   if (compunit_symtab != NULL)
     {
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 8af3d54a77..b992fbd11a 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -3202,10 +3202,9 @@  free_breakpoint_objfile_data (struct objfile *obj, void *data)
 static void
 create_overlay_event_breakpoint (void)
 {
-  struct objfile *objfile;
   const char *const func_name = "_ovly_debug_event";
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       struct breakpoint *b;
       struct breakpoint_objfile_data *bp_objfile_data;
@@ -3261,95 +3260,93 @@  create_longjmp_master_breakpoint (void)
 
   ALL_PSPACES (pspace)
   {
-    struct objfile *objfile;
-
     set_current_program_space (pspace);
 
-    ALL_OBJFILES (objfile)
-    {
-      int i;
-      struct gdbarch *gdbarch;
-      struct breakpoint_objfile_data *bp_objfile_data;
+    for (struct objfile *objfile : all_objfiles (current_program_space))
+      {
+	int i;
+	struct gdbarch *gdbarch;
+	struct breakpoint_objfile_data *bp_objfile_data;
 
-      gdbarch = get_objfile_arch (objfile);
+	gdbarch = get_objfile_arch (objfile);
 
-      bp_objfile_data = get_breakpoint_objfile_data (objfile);
+	bp_objfile_data = get_breakpoint_objfile_data (objfile);
 
-      if (!bp_objfile_data->longjmp_searched)
-	{
-	  std::vector<probe *> ret
-	    = find_probes_in_objfile (objfile, "libc", "longjmp");
+	if (!bp_objfile_data->longjmp_searched)
+	  {
+	    std::vector<probe *> ret
+	      = find_probes_in_objfile (objfile, "libc", "longjmp");
 
-	  if (!ret.empty ())
-	    {
-	      /* We are only interested in checking one element.  */
-	      probe *p = ret[0];
+	    if (!ret.empty ())
+	      {
+		/* We are only interested in checking one element.  */
+		probe *p = ret[0];
 
-	      if (!p->can_evaluate_arguments ())
-		{
-		  /* We cannot use the probe interface here, because it does
-		     not know how to evaluate arguments.  */
-		  ret.clear ();
-		}
-	    }
-	  bp_objfile_data->longjmp_probes = ret;
-	  bp_objfile_data->longjmp_searched = 1;
-	}
+		if (!p->can_evaluate_arguments ())
+		  {
+		    /* We cannot use the probe interface here, because it does
+		       not know how to evaluate arguments.  */
+		    ret.clear ();
+		  }
+	      }
+	    bp_objfile_data->longjmp_probes = ret;
+	    bp_objfile_data->longjmp_searched = 1;
+	  }
 
-      if (!bp_objfile_data->longjmp_probes.empty ())
-	{
-	  for (probe *p : bp_objfile_data->longjmp_probes)
-	    {
-	      struct breakpoint *b;
+	if (!bp_objfile_data->longjmp_probes.empty ())
+	  {
+	    for (probe *p : bp_objfile_data->longjmp_probes)
+	      {
+		struct breakpoint *b;
+
+		b = create_internal_breakpoint (gdbarch,
+						p->get_relocated_address (objfile),
+						bp_longjmp_master,
+						&internal_breakpoint_ops);
+		b->location = new_probe_location ("-probe-stap libc:longjmp");
+		b->enable_state = bp_disabled;
+	      }
 
-	      b = create_internal_breakpoint (gdbarch,
-					      p->get_relocated_address (objfile),
-					      bp_longjmp_master,
-					      &internal_breakpoint_ops);
-	      b->location = new_probe_location ("-probe-stap libc:longjmp");
-	      b->enable_state = bp_disabled;
-	    }
+	    continue;
+	  }
 
+	if (!gdbarch_get_longjmp_target_p (gdbarch))
 	  continue;
-	}
-
-      if (!gdbarch_get_longjmp_target_p (gdbarch))
-	continue;
 
-      for (i = 0; i < NUM_LONGJMP_NAMES; i++)
-	{
-	  struct breakpoint *b;
-	  const char *func_name;
-	  CORE_ADDR addr;
-	  struct explicit_location explicit_loc;
+	for (i = 0; i < NUM_LONGJMP_NAMES; i++)
+	  {
+	    struct breakpoint *b;
+	    const char *func_name;
+	    CORE_ADDR addr;
+	    struct explicit_location explicit_loc;
 
-	  if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
-	    continue;
+	    if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
+	      continue;
 
-	  func_name = longjmp_names[i];
-	  if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
-	    {
-	      struct bound_minimal_symbol m;
+	    func_name = longjmp_names[i];
+	    if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
+	      {
+		struct bound_minimal_symbol m;
 
-	      m = lookup_minimal_symbol_text (func_name, objfile);
-	      if (m.minsym == NULL)
-		{
-		  /* Prevent future lookups in this objfile.  */
-		  bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
-		  continue;
-		}
-	      bp_objfile_data->longjmp_msym[i] = m;
-	    }
+		m = lookup_minimal_symbol_text (func_name, objfile);
+		if (m.minsym == NULL)
+		  {
+		    /* Prevent future lookups in this objfile.  */
+		    bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
+		    continue;
+		  }
+		bp_objfile_data->longjmp_msym[i] = m;
+	      }
 
-	  addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
-	  b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
-					  &internal_breakpoint_ops);
-	  initialize_explicit_location (&explicit_loc);
-	  explicit_loc.function_name = ASTRDUP (func_name);
-	  b->location = new_explicit_location (&explicit_loc);
-	  b->enable_state = bp_disabled;
-	}
-    }
+	    addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
+	    b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
+					    &internal_breakpoint_ops);
+	    initialize_explicit_location (&explicit_loc);
+	    explicit_loc.function_name = ASTRDUP (func_name);
+	    b->location = new_explicit_location (&explicit_loc);
+	    b->enable_state = bp_disabled;
+	  }
+      }
   }
 }
 
@@ -3364,46 +3361,45 @@  create_std_terminate_master_breakpoint (void)
 
   ALL_PSPACES (pspace)
   {
-    struct objfile *objfile;
     CORE_ADDR addr;
 
     set_current_program_space (pspace);
 
-    ALL_OBJFILES (objfile)
-    {
-      struct breakpoint *b;
-      struct breakpoint_objfile_data *bp_objfile_data;
-      struct explicit_location explicit_loc;
+    for (struct objfile *objfile : all_objfiles (current_program_space))
+      {
+	struct breakpoint *b;
+	struct breakpoint_objfile_data *bp_objfile_data;
+	struct explicit_location explicit_loc;
 
-      bp_objfile_data = get_breakpoint_objfile_data (objfile);
+	bp_objfile_data = get_breakpoint_objfile_data (objfile);
 
-      if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
-	continue;
+	if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
+	  continue;
 
-      if (bp_objfile_data->terminate_msym.minsym == NULL)
-	{
-	  struct bound_minimal_symbol m;
+	if (bp_objfile_data->terminate_msym.minsym == NULL)
+	  {
+	    struct bound_minimal_symbol m;
 
-	  m = lookup_minimal_symbol (func_name, NULL, objfile);
-	  if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
-				   && MSYMBOL_TYPE (m.minsym) != mst_file_text))
-	    {
-	      /* Prevent future lookups in this objfile.  */
-	      bp_objfile_data->terminate_msym.minsym = &msym_not_found;
-	      continue;
-	    }
-	  bp_objfile_data->terminate_msym = m;
-	}
+	    m = lookup_minimal_symbol (func_name, NULL, objfile);
+	    if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
+				     && MSYMBOL_TYPE (m.minsym) != mst_file_text))
+	      {
+		/* Prevent future lookups in this objfile.  */
+		bp_objfile_data->terminate_msym.minsym = &msym_not_found;
+		continue;
+	      }
+	    bp_objfile_data->terminate_msym = m;
+	  }
 
-      addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
-      b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
-                                      bp_std_terminate_master,
-				      &internal_breakpoint_ops);
-      initialize_explicit_location (&explicit_loc);
-      explicit_loc.function_name = ASTRDUP (func_name);
-      b->location = new_explicit_location (&explicit_loc);
-      b->enable_state = bp_disabled;
-    }
+	addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
+	b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
+					bp_std_terminate_master,
+					&internal_breakpoint_ops);
+	initialize_explicit_location (&explicit_loc);
+	explicit_loc.function_name = ASTRDUP (func_name);
+	b->location = new_explicit_location (&explicit_loc);
+	b->enable_state = bp_disabled;
+      }
   }
 }
 
@@ -3412,10 +3408,9 @@  create_std_terminate_master_breakpoint (void)
 static void
 create_exception_master_breakpoint (void)
 {
-  struct objfile *objfile;
   const char *const func_name = "_Unwind_DebugHook";
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       struct breakpoint *b;
       struct gdbarch *gdbarch;
diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c
index e891e77fba..7bbfb64851 100644
--- a/gdb/compile/compile-object-run.c
+++ b/gdb/compile/compile-object-run.c
@@ -62,7 +62,6 @@  static void
 do_module_cleanup (void *arg, int registers_valid)
 {
   struct do_module_cleanup *data = (struct do_module_cleanup *) arg;
-  struct objfile *objfile;
 
   if (data->executedp != NULL)
     {
@@ -83,7 +82,7 @@  do_module_cleanup (void *arg, int registers_valid)
 	}
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     if ((objfile->flags & OBJF_USERLOADED) == 0
         && (strcmp (objfile_name (objfile), data->objfile_name_string) == 0))
       {
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 1f95253cb0..184d637da6 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1381,11 +1381,11 @@  add_symbol_overload_list_qualified (const char *func_name,
   /* Look through the partial symtabs for all symbols which begin by
      matching FUNC_NAME.  Make sure we read that symbol table in.  */
 
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      objfile->sf->qf->expand_symtabs_for_function (objfile, func_name);
-  }
+  for (struct objfile *objf : all_objfiles (current_program_space))
+    {
+      if (objf->sf)
+	objf->sf->qf->expand_symtabs_for_function (objf, func_name);
+    }
 
   /* Search upwards from currently selected frame (so that we can
      complete on local vars.  */
diff --git a/gdb/dwarf-index-write.c b/gdb/dwarf-index-write.c
index 8a4c1c7ea4..e2066c6cf1 100644
--- a/gdb/dwarf-index-write.c
+++ b/gdb/dwarf-index-write.c
@@ -1640,7 +1640,6 @@  write_psymtabs_to_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
 static void
 save_gdb_index_command (const char *arg, int from_tty)
 {
-  struct objfile *objfile;
   const char dwarf5space[] = "-dwarf-5 ";
   dw_index_kind index_kind = dw_index_kind::GDB_INDEX;
 
@@ -1658,35 +1657,35 @@  save_gdb_index_command (const char *arg, int from_tty)
   if (!*arg)
     error (_("usage: save gdb-index [-dwarf-5] DIRECTORY"));
 
-  ALL_OBJFILES (objfile)
-  {
-    struct stat st;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      struct stat st;
 
-    /* If the objfile does not correspond to an actual file, skip it.  */
-    if (stat (objfile_name (objfile), &st) < 0)
-      continue;
+      /* If the objfile does not correspond to an actual file, skip it.  */
+      if (stat (objfile_name (objfile), &st) < 0)
+	continue;
 
-    struct dwarf2_per_objfile *dwarf2_per_objfile
-      = get_dwarf2_per_objfile (objfile);
+      struct dwarf2_per_objfile *dwarf2_per_objfile
+	= get_dwarf2_per_objfile (objfile);
 
-    if (dwarf2_per_objfile != NULL)
-      {
-	TRY
-	  {
-	    const char *basename = lbasename (objfile_name (objfile));
-	    write_psymtabs_to_index (dwarf2_per_objfile, arg, basename,
-				     index_kind);
-	  }
-	CATCH (except, RETURN_MASK_ERROR)
-	  {
-	    exception_fprintf (gdb_stderr, except,
-			       _("Error while writing index for `%s': "),
-			       objfile_name (objfile));
-	  }
-	END_CATCH
-      }
+      if (dwarf2_per_objfile != NULL)
+	{
+	  TRY
+	    {
+	      const char *basename = lbasename (objfile_name (objfile));
+	      write_psymtabs_to_index (dwarf2_per_objfile, arg, basename,
+				       index_kind);
+	    }
+	  CATCH (except, RETURN_MASK_ERROR)
+	    {
+	      exception_fprintf (gdb_stderr, except,
+				 _("Error while writing index for `%s': "),
+				 objfile_name (objfile));
+	    }
+	  END_CATCH
+	    }
 
-  }
+    }
 }
 
 void
diff --git a/gdb/dwarf2-frame.c b/gdb/dwarf2-frame.c
index 2201c63659..d0b52af202 100644
--- a/gdb/dwarf2-frame.c
+++ b/gdb/dwarf2-frame.c
@@ -1702,9 +1702,7 @@  bsearch_fde_cmp (const void *key, const void *element)
 static struct dwarf2_fde *
 dwarf2_frame_find_fde (CORE_ADDR *pc, CORE_ADDR *out_offset)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       struct dwarf2_fde_table *fde_table;
       struct dwarf2_fde **p_fde;
diff --git a/gdb/guile/scm-objfile.c b/gdb/guile/scm-objfile.c
index ccf7c66d33..3bdf9555e6 100644
--- a/gdb/guile/scm-objfile.c
+++ b/gdb/guile/scm-objfile.c
@@ -366,17 +366,16 @@  gdbscm_get_current_objfile (void)
 static SCM
 gdbscm_objfiles (void)
 {
-  struct objfile *objf;
   SCM result;
 
   result = SCM_EOL;
 
-  ALL_OBJFILES (objf)
-  {
-    SCM item = ofscm_scm_from_objfile (objf);
+  for (struct objfile *objf : all_objfiles (current_program_space))
+    {
+      SCM item = ofscm_scm_from_objfile (objf);
 
-    result = scm_cons (item, result);
-  }
+      result = scm_cons (item, result);
+    }
 
   return scm_reverse_x (result, SCM_EOL);
 }
diff --git a/gdb/guile/scm-pretty-print.c b/gdb/guile/scm-pretty-print.c
index f292960dbb..20342dc4d8 100644
--- a/gdb/guile/scm-pretty-print.c
+++ b/gdb/guile/scm-pretty-print.c
@@ -427,19 +427,18 @@  ppscm_search_pp_list (SCM list, SCM value)
 static SCM
 ppscm_find_pretty_printer_from_objfiles (SCM value)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    objfile_smob *o_smob = ofscm_objfile_smob_from_objfile (objfile);
-    SCM pp = ppscm_search_pp_list (ofscm_objfile_smob_pretty_printers (o_smob),
-				   value);
-
-    /* Note: This will return if pp is a <gdb:exception> object,
-       which is what we want.  */
-    if (gdbscm_is_true (pp))
-      return pp;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      objfile_smob *o_smob = ofscm_objfile_smob_from_objfile (objfile);
+      SCM pp
+	= ppscm_search_pp_list (ofscm_objfile_smob_pretty_printers (o_smob),
+				value);
+
+      /* Note: This will return if pp is a <gdb:exception> object,
+	 which is what we want.  */
+      if (gdbscm_is_true (pp))
+	return pp;
+    }
 
   return SCM_BOOL_F;
 }
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index d22b2cf5d2..9f70ef9b73 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -483,7 +483,6 @@  struct unwind_table_entry *
 find_unwind_entry (CORE_ADDR pc)
 {
   int first, middle, last;
-  struct objfile *objfile;
   struct hppa_objfile_private *priv;
 
   if (hppa_debug)
@@ -498,61 +497,61 @@  find_unwind_entry (CORE_ADDR pc)
       return NULL;
     }
 
-  ALL_OBJFILES (objfile)
-  {
-    struct hppa_unwind_info *ui;
-    ui = NULL;
-    priv = ((struct hppa_objfile_private *)
-	    objfile_data (objfile, hppa_objfile_priv_data));
-    if (priv)
-      ui = ((struct hppa_objfile_private *) priv)->unwind_info;
-
-    if (!ui)
-      {
-	read_unwind_info (objfile);
-        priv = ((struct hppa_objfile_private *)
-		objfile_data (objfile, hppa_objfile_priv_data));
-	if (priv == NULL)
-	  error (_("Internal error reading unwind information."));
-        ui = ((struct hppa_objfile_private *) priv)->unwind_info;
-      }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      struct hppa_unwind_info *ui;
+      ui = NULL;
+      priv = ((struct hppa_objfile_private *)
+	      objfile_data (objfile, hppa_objfile_priv_data));
+      if (priv)
+	ui = ((struct hppa_objfile_private *) priv)->unwind_info;
+
+      if (!ui)
+	{
+	  read_unwind_info (objfile);
+	  priv = ((struct hppa_objfile_private *)
+		  objfile_data (objfile, hppa_objfile_priv_data));
+	  if (priv == NULL)
+	    error (_("Internal error reading unwind information."));
+	  ui = ((struct hppa_objfile_private *) priv)->unwind_info;
+	}
 
-    /* First, check the cache.  */
+      /* First, check the cache.  */
 
-    if (ui->cache
-	&& pc >= ui->cache->region_start
-	&& pc <= ui->cache->region_end)
-      {
-	if (hppa_debug)
-	  fprintf_unfiltered (gdb_stdlog, "%s (cached) }\n",
-            hex_string ((uintptr_t) ui->cache));
-        return ui->cache;
-      }
+      if (ui->cache
+	  && pc >= ui->cache->region_start
+	  && pc <= ui->cache->region_end)
+	{
+	  if (hppa_debug)
+	    fprintf_unfiltered (gdb_stdlog, "%s (cached) }\n",
+				hex_string ((uintptr_t) ui->cache));
+	  return ui->cache;
+	}
 
-    /* Not in the cache, do a binary search.  */
+      /* Not in the cache, do a binary search.  */
 
-    first = 0;
-    last = ui->last;
+      first = 0;
+      last = ui->last;
 
-    while (first <= last)
-      {
-	middle = (first + last) / 2;
-	if (pc >= ui->table[middle].region_start
-	    && pc <= ui->table[middle].region_end)
-	  {
-	    ui->cache = &ui->table[middle];
-	    if (hppa_debug)
-	      fprintf_unfiltered (gdb_stdlog, "%s }\n",
-                hex_string ((uintptr_t) ui->cache));
-	    return &ui->table[middle];
-	  }
+      while (first <= last)
+	{
+	  middle = (first + last) / 2;
+	  if (pc >= ui->table[middle].region_start
+	      && pc <= ui->table[middle].region_end)
+	    {
+	      ui->cache = &ui->table[middle];
+	      if (hppa_debug)
+		fprintf_unfiltered (gdb_stdlog, "%s }\n",
+				    hex_string ((uintptr_t) ui->cache));
+	      return &ui->table[middle];
+	    }
 
-	if (pc < ui->table[middle].region_start)
-	  last = middle - 1;
-	else
-	  first = middle + 1;
-      }
-  }				/* ALL_OBJFILES() */
+	  if (pc < ui->table[middle].region_start)
+	    last = middle - 1;
+	  else
+	    first = middle + 1;
+	}
+    }
 
   if (hppa_debug)
     fprintf_unfiltered (gdb_stdlog, "NULL (not found) }\n");
diff --git a/gdb/jit.c b/gdb/jit.c
index e6b3cc25ca..2f5f6f8ade 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -986,9 +986,7 @@  jit_unregister_code (struct objfile *objfile)
 static struct objfile *
 jit_find_objf_with_entry_addr (CORE_ADDR entry_addr)
 {
-  struct objfile *objf;
-
-  ALL_OBJFILES (objf)
+  for (struct objfile *objf : all_objfiles (current_program_space))
     {
       struct jit_objfile_data *objf_data;
 
diff --git a/gdb/linespec.c b/gdb/linespec.c
index 00f59f9c28..8e0fb866e8 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -1125,7 +1125,6 @@  iterate_over_all_matching_symtabs
    struct program_space *search_pspace, bool include_inline,
    gdb::function_view<symbol_found_callback_ftype> callback)
 {
-  struct objfile *objfile;
   struct program_space *pspace;
 
   ALL_PSPACES (pspace)
@@ -1137,46 +1136,48 @@  iterate_over_all_matching_symtabs
 
     set_current_program_space (pspace);
 
-    ALL_OBJFILES (objfile)
-    {
-      struct compunit_symtab *cu;
+    for (struct objfile *objfile : all_objfiles (current_program_space))
+      {
+	struct compunit_symtab *cu;
 
-      if (objfile->sf)
-	objfile->sf->qf->expand_symtabs_matching (objfile,
-						  NULL,
-						  lookup_name,
-						  NULL, NULL,
-						  search_domain);
+	if (objfile->sf)
+	  objfile->sf->qf->expand_symtabs_matching (objfile,
+						    NULL,
+						    lookup_name,
+						    NULL, NULL,
+						    search_domain);
 
-      ALL_OBJFILE_COMPUNITS (objfile, cu)
-	{
-	  struct symtab *symtab = COMPUNIT_FILETABS (cu);
+	ALL_OBJFILE_COMPUNITS (objfile, cu)
+	  {
+	    struct symtab *symtab = COMPUNIT_FILETABS (cu);
 
-	  iterate_over_file_blocks (symtab, lookup_name, name_domain, callback);
+	    iterate_over_file_blocks (symtab, lookup_name, name_domain,
+				      callback);
 
-	  if (include_inline)
-	    {
-	      struct block *block;
-	      int i;
+	    if (include_inline)
+	      {
+		struct block *block;
+		int i;
 
-	      for (i = FIRST_LOCAL_BLOCK;
-		   i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
-		   i++)
-		{
-		  block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
-		  state->language->la_iterate_over_symbols
-		    (block, lookup_name, name_domain, [&] (block_symbol *bsym)
-		     {
-		       /* Restrict calls to CALLBACK to symbols
-			  representing inline symbols only.  */
-		       if (SYMBOL_INLINED (bsym->symbol))
-			 return callback (bsym);
-		       return true;
-		     });
-		}
-	    }
-	}
-    }
+		for (i = FIRST_LOCAL_BLOCK;
+		     i < BLOCKVECTOR_NBLOCKS (SYMTAB_BLOCKVECTOR (symtab));
+		     i++)
+		  {
+		    block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (symtab), i);
+		    state->language->la_iterate_over_symbols
+		      (block, lookup_name, name_domain,
+		       [&] (block_symbol *bsym)
+		       {
+			 /* Restrict calls to CALLBACK to symbols
+			    representing inline symbols only.  */
+			 if (SYMBOL_INLINED (bsym->symbol))
+			   return callback (bsym);
+			 return true;
+		       });
+		  }
+	      }
+	  }
+      }
   }
 }
 
@@ -4400,8 +4401,6 @@  search_minsyms_for_name (struct collect_info *info,
 
       ALL_PSPACES (pspace)
       {
-	struct objfile *objfile;
-
 	if (search_pspace != NULL && search_pspace != pspace)
 	  continue;
 	if (pspace->executing_startup)
@@ -4409,17 +4408,17 @@  search_minsyms_for_name (struct collect_info *info,
 
 	set_current_program_space (pspace);
 
-	ALL_OBJFILES (objfile)
-	{
-	  iterate_over_minimal_symbols (objfile, name,
-					[&] (struct minimal_symbol *msym)
+	for (struct objfile *objfile : all_objfiles (current_program_space))
+	  {
+	    iterate_over_minimal_symbols (objfile, name,
+					  [&] (struct minimal_symbol *msym)
 					  {
 					    add_minsym (msym, objfile, nullptr,
 							info->state->list_mode,
 							&minsyms);
 					    return false;
 					  });
-	}
+	  }
       }
     }
   else
diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c
index 74acec2629..d4213c1f50 100644
--- a/gdb/linux-thread-db.c
+++ b/gdb/linux-thread-db.c
@@ -1044,12 +1044,10 @@  try_thread_db_load_from_pdir_1 (struct objfile *obj, const char *subdir)
 static int
 try_thread_db_load_from_pdir (const char *subdir)
 {
-  struct objfile *obj;
-
   if (!auto_load_thread_db)
     return 0;
 
-  ALL_OBJFILES (obj)
+  for (struct objfile *obj : all_objfiles (current_program_space))
     if (libpthread_name_p (objfile_name (obj)))
       {
 	if (try_thread_db_load_from_pdir_1 (obj, subdir))
@@ -1158,9 +1156,7 @@  thread_db_load_search (void)
 static int
 has_libpthread (void)
 {
-  struct objfile *obj;
-
-  ALL_OBJFILES (obj)
+  for (struct objfile *obj : all_objfiles (current_program_space))
     if (libpthread_name_p (objfile_name (obj)))
       return 1;
 
diff --git a/gdb/main.c b/gdb/main.c
index c71d5b557f..639230652b 100644
--- a/gdb/main.c
+++ b/gdb/main.c
@@ -485,7 +485,6 @@  captured_main_1 (struct captured_main_args *context)
 
   int i;
   int save_auto_load;
-  struct objfile *objfile;
   int ret = 1;
 
 #ifdef HAVE_USEFUL_SBRK
@@ -1124,7 +1123,7 @@  captured_main_1 (struct captured_main_args *context)
      We wait until now because it is common to add to the source search
      path in local_gdbinit.  */
   global_auto_load = save_auto_load;
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     load_auto_scripts_for_objfile (objfile);
 
   /* Process '-x' and '-ex' options.  */
diff --git a/gdb/maint.c b/gdb/maint.c
index 01a80f5d73..5a4aa83eaf 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -348,7 +348,6 @@  maintenance_info_sections (const char *arg, int from_tty)
       printf_filtered (_("file type %s.\n"), bfd_get_target (exec_bfd));
       if (arg && *arg && match_substring (arg, "ALLOBJ"))
 	{
-	  struct objfile *ofile;
 	  struct obj_section *osect;
 
 	  /* Only this function cares about the 'ALLOBJ' argument; 
@@ -358,7 +357,7 @@  maintenance_info_sections (const char *arg, int from_tty)
 	  if (strcmp (arg, "ALLOBJ") == 0)
 	    arg = NULL;
 
-	  ALL_OBJFILES (ofile)
+	  for (struct objfile *ofile : all_objfiles (current_program_space))
 	    {
 	      printf_filtered (_("  Object file: %s\n"), 
 			       bfd_get_filename (ofile->obfd));
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index d51362a4d3..4933e0c343 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -972,8 +972,6 @@  find_methods (char type, const char *theclass, const char *category,
 	      const char *selector,
 	      std::vector<const char *> *symbol_names)
 {
-  struct objfile *objfile = NULL;
-
   const char *symname = NULL;
 
   char ntype = '\0';
@@ -986,7 +984,7 @@  find_methods (char type, const char *theclass, const char *category,
 
   gdb_assert (symbol_names != NULL);
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       unsigned int *objc_csym;
       struct minimal_symbol *msymbol = NULL;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index dec92df816..90d2502dde 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -1016,13 +1016,11 @@  objfile_has_symbols (struct objfile *objfile)
 int
 have_partial_symbols (void)
 {
-  struct objfile *ofp;
-
-  ALL_OBJFILES (ofp)
-  {
-    if (objfile_has_partial_symbols (ofp))
-      return 1;
-  }
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    {
+      if (objfile_has_partial_symbols (ofp))
+	return 1;
+    }
   return 0;
 }
 
@@ -1033,13 +1031,11 @@  have_partial_symbols (void)
 int
 have_full_symbols (void)
 {
-  struct objfile *ofp;
-
-  ALL_OBJFILES (ofp)
-  {
-    if (objfile_has_full_symbols (ofp))
-      return 1;
-  }
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    {
+      if (objfile_has_full_symbols (ofp))
+	return 1;
+    }
   return 0;
 }
 
@@ -1072,15 +1068,13 @@  objfile_purge_solibs (void)
 int
 have_minimal_symbols (void)
 {
-  struct objfile *ofp;
-
-  ALL_OBJFILES (ofp)
-  {
-    if (ofp->per_bfd->minimal_symbol_count > 0)
-      {
-	return 1;
-      }
-  }
+  for (struct objfile *ofp : all_objfiles (current_program_space))
+    {
+      if (ofp->per_bfd->minimal_symbol_count > 0)
+	{
+	  return 1;
+	}
+    }
   return 0;
 }
 
@@ -1145,9 +1139,7 @@  qsort_cmp (const void *a, const void *b)
 	{
 	  /* Sort on sequence number of the objfile in the chain.  */
 
-	  const struct objfile *objfile;
-
-	  ALL_OBJFILES (objfile)
+	  for (struct objfile *objfile : all_objfiles (current_program_space))
 	    if (objfile == objfile1)
 	      return -1;
 	    else if (objfile == objfile2)
@@ -1502,7 +1494,7 @@  shared_objfile_contains_address_p (struct program_space *pspace,
 }
 
 /* The default implementation for the "iterate_over_objfiles_in_search_order"
-   gdbarch method.  It is equivalent to use the ALL_OBJFILES macro,
+   gdbarch method.  It is equivalent to use the all_objfiles iterable,
    searching the objfiles in the order they are stored internally,
    ignoring CURRENT_OBJFILE.
 
@@ -1516,9 +1508,8 @@  default_iterate_over_objfiles_in_search_order
    void *cb_data, struct objfile *current_objfile)
 {
   int stop = 0;
-  struct objfile *objfile;
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
        stop = cb (objfile, cb_data);
        if (stop)
diff --git a/gdb/ppc-linux-tdep.c b/gdb/ppc-linux-tdep.c
index ed44cbe555..c6d8546de1 100644
--- a/gdb/ppc-linux-tdep.c
+++ b/gdb/ppc-linux-tdep.c
@@ -1788,10 +1788,8 @@  ppc_linux_spe_context_lookup (struct objfile *objfile)
 static void
 ppc_linux_spe_context_inferior_created (struct target_ops *t, int from_tty)
 {
-  struct objfile *objfile;
-
   ppc_linux_spe_context_lookup (NULL);
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     ppc_linux_spe_context_lookup (objfile);
 }
 
diff --git a/gdb/probe.c b/gdb/probe.c
index ace8efb541..db10691093 100644
--- a/gdb/probe.c
+++ b/gdb/probe.c
@@ -244,29 +244,28 @@  find_probes_in_objfile (struct objfile *objfile, const char *provider,
 struct bound_probe
 find_probe_by_pc (CORE_ADDR pc)
 {
-  struct objfile *objfile;
   struct bound_probe result;
 
   result.objfile = NULL;
   result.prob = NULL;
 
-  ALL_OBJFILES (objfile)
-  {
-    if (!objfile->sf || !objfile->sf->sym_probe_fns
-	|| objfile->sect_index_text == -1)
-      continue;
-
-    /* If this proves too inefficient, we can replace with a hash.  */
-    const std::vector<probe *> &probes
-      = objfile->sf->sym_probe_fns->sym_get_probes (objfile);
-    for (probe *p : probes)
-      if (p->get_relocated_address (objfile) == pc)
-	{
-	  result.objfile = objfile;
-	  result.prob = p;
-	  return result;
-	}
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (!objfile->sf || !objfile->sf->sym_probe_fns
+	  || objfile->sect_index_text == -1)
+	continue;
+
+      /* If this proves too inefficient, we can replace with a hash.  */
+      const std::vector<probe *> &probes
+	= objfile->sf->sym_probe_fns->sym_get_probes (objfile);
+      for (probe *p : probes)
+	if (p->get_relocated_address (objfile) == pc)
+	  {
+	    result.objfile = objfile;
+	    result.prob = p;
+	    return result;
+	  }
+    }
 
   return result;
 }
@@ -282,7 +281,6 @@  static std::vector<bound_probe>
 collect_probes (const std::string &objname, const std::string &provider,
 		const std::string &probe_name, const static_probe_ops *spops)
 {
-  struct objfile *objfile;
   std::vector<bound_probe> result;
   gdb::optional<compiled_regex> obj_pat, prov_pat, probe_pat;
 
@@ -296,7 +294,7 @@  collect_probes (const std::string &objname, const std::string &provider,
     obj_pat.emplace (objname.c_str (), REG_NOSUB,
 		     _("Invalid object file regexp"));
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (! objfile->sf || ! objfile->sf->sym_probe_fns)
 	continue;
diff --git a/gdb/psymtab.c b/gdb/psymtab.c
index 0ee5c9d516..61fb0ebb70 100644
--- a/gdb/psymtab.c
+++ b/gdb/psymtab.c
@@ -108,12 +108,6 @@  require_partial_symbols (struct objfile *objfile, int verbose)
 
 #undef ALL_OBJFILE_PSYMTABS
 
-/* Traverse all psymtabs in all objfiles.  */
-
-#define ALL_PSYMTABS(objfile, p) \
-  ALL_OBJFILES (objfile)	 \
-    ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
-
 /* Helper function for psym_map_symtabs_matching_filename that
    expands the symtabs and calls the iterator.  */
 
@@ -1874,7 +1868,6 @@  maintenance_print_psymbols (const char *args, int from_tty)
 {
   struct ui_file *outfile = gdb_stdout;
   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
-  struct objfile *objfile;
   struct partial_symtab *ps;
   int i, outfile_idx, found;
   CORE_ADDR pc = 0;
@@ -1944,7 +1937,7 @@  maintenance_print_psymbols (const char *args, int from_tty)
     }
 
   found = 0;
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       int printed_objfile_header = 0;
       int print_for_objfile = 1;
@@ -2148,93 +2141,93 @@  maintenance_check_psymtabs (const char *ignore, int from_tty)
   struct compunit_symtab *cust = NULL;
   struct partial_symtab *ps;
   const struct blockvector *bv;
-  struct objfile *objfile;
   struct block *b;
   int length;
 
-  ALL_PSYMTABS (objfile, ps)
-  {
-    struct gdbarch *gdbarch = get_objfile_arch (objfile);
-
-    /* We don't call psymtab_to_symtab here because that may cause symtab
-       expansion.  When debugging a problem it helps if checkers leave
-       things unchanged.  */
-    cust = ps->compunit_symtab;
-
-    /* First do some checks that don't require the associated symtab.  */
-    if (ps->text_high (objfile) < ps->text_low (objfile))
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
       {
-	printf_filtered ("Psymtab ");
-	puts_filtered (ps->filename);
-	printf_filtered (" covers bad range ");
-	fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
-			gdb_stdout);
-	printf_filtered (" - ");
-	fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
-			gdb_stdout);
-	printf_filtered ("\n");
-	continue;
-      }
+	struct gdbarch *gdbarch = get_objfile_arch (objfile);
 
-    /* Now do checks requiring the associated symtab.  */
-    if (cust == NULL)
-      continue;
-    bv = COMPUNIT_BLOCKVECTOR (cust);
-    b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
-    partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
-    length = ps->n_static_syms;
-    while (length--)
-      {
-	sym = block_lookup_symbol (b, symbol_search_name (*psym),
-				   symbol_name_match_type::SEARCH_NAME,
-				   (*psym)->domain);
-	if (!sym)
+	/* We don't call psymtab_to_symtab here because that may cause symtab
+	   expansion.  When debugging a problem it helps if checkers leave
+	   things unchanged.  */
+	cust = ps->compunit_symtab;
+
+	/* First do some checks that don't require the associated symtab.  */
+	if (ps->text_high (objfile) < ps->text_low (objfile))
 	  {
-	    printf_filtered ("Static symbol `");
-	    puts_filtered ((*psym)->name);
-	    printf_filtered ("' only found in ");
+	    printf_filtered ("Psymtab ");
 	    puts_filtered (ps->filename);
-	    printf_filtered (" psymtab\n");
+	    printf_filtered (" covers bad range ");
+	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
+			    gdb_stdout);
+	    printf_filtered (" - ");
+	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
+			    gdb_stdout);
+	    printf_filtered ("\n");
+	    continue;
 	  }
-	psym++;
-      }
-    b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
-    psym = &objfile->global_psymbols[ps->globals_offset];
-    length = ps->n_global_syms;
-    while (length--)
-      {
-	sym = block_lookup_symbol (b, symbol_search_name (*psym),
-				   symbol_name_match_type::SEARCH_NAME,
-				   (*psym)->domain);
-	if (!sym)
+
+	/* Now do checks requiring the associated symtab.  */
+	if (cust == NULL)
+	  continue;
+	bv = COMPUNIT_BLOCKVECTOR (cust);
+	b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
+	partial_symbol **psym = &objfile->static_psymbols[ps->statics_offset];
+	length = ps->n_static_syms;
+	while (length--)
+	  {
+	    sym = block_lookup_symbol (b, symbol_search_name (*psym),
+				       symbol_name_match_type::SEARCH_NAME,
+				       (*psym)->domain);
+	    if (!sym)
+	      {
+		printf_filtered ("Static symbol `");
+		puts_filtered ((*psym)->name);
+		printf_filtered ("' only found in ");
+		puts_filtered (ps->filename);
+		printf_filtered (" psymtab\n");
+	      }
+	    psym++;
+	  }
+	b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
+	psym = &objfile->global_psymbols[ps->globals_offset];
+	length = ps->n_global_syms;
+	while (length--)
 	  {
-	    printf_filtered ("Global symbol `");
-	    puts_filtered ((*psym)->name);
-	    printf_filtered ("' only found in ");
+	    sym = block_lookup_symbol (b, symbol_search_name (*psym),
+				       symbol_name_match_type::SEARCH_NAME,
+				       (*psym)->domain);
+	    if (!sym)
+	      {
+		printf_filtered ("Global symbol `");
+		puts_filtered ((*psym)->name);
+		printf_filtered ("' only found in ");
+		puts_filtered (ps->filename);
+		printf_filtered (" psymtab\n");
+	      }
+	    psym++;
+	  }
+	if (ps->raw_text_high () != 0
+	    && (ps->text_low (objfile) < BLOCK_START (b)
+		|| ps->text_high (objfile) > BLOCK_END (b)))
+	  {
+	    printf_filtered ("Psymtab ");
 	    puts_filtered (ps->filename);
-	    printf_filtered (" psymtab\n");
+	    printf_filtered (" covers ");
+	    fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
+			    gdb_stdout);
+	    printf_filtered (" - ");
+	    fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
+			    gdb_stdout);
+	    printf_filtered (" but symtab covers only ");
+	    fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
+	    printf_filtered (" - ");
+	    fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
+	    printf_filtered ("\n");
 	  }
-	psym++;
       }
-    if (ps->raw_text_high () != 0
-	&& (ps->text_low (objfile) < BLOCK_START (b)
-	    || ps->text_high (objfile) > BLOCK_END (b)))
-      {
-	printf_filtered ("Psymtab ");
-	puts_filtered (ps->filename);
-	printf_filtered (" covers ");
-	fputs_filtered (paddress (gdbarch, ps->text_low (objfile)),
-			gdb_stdout);
-	printf_filtered (" - ");
-	fputs_filtered (paddress (gdbarch, ps->text_high (objfile)),
-			gdb_stdout);
-	printf_filtered (" but symtab covers only ");
-	fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
-	printf_filtered (" - ");
-	fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
-	printf_filtered ("\n");
-      }
-  }
 }
 
 void
diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c
index dc7f342aef..9ec4fa4778 100644
--- a/gdb/python/py-objfile.c
+++ b/gdb/python/py-objfile.c
@@ -511,9 +511,7 @@  objfpy_build_id_matches (const struct bfd_build_id *build_id,
 static struct objfile *
 objfpy_lookup_objfile_by_name (const char *name)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       const char *filename;
 
@@ -539,9 +537,7 @@  objfpy_lookup_objfile_by_name (const char *name)
 static struct objfile *
 objfpy_lookup_objfile_by_build_id (const char *build_id)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       const struct bfd_build_id *obfd_build_id;
 
diff --git a/gdb/python/py-prettyprint.c b/gdb/python/py-prettyprint.c
index fa4107c30d..679247ae73 100644
--- a/gdb/python/py-prettyprint.c
+++ b/gdb/python/py-prettyprint.c
@@ -93,28 +93,26 @@  search_pp_list (PyObject *list, PyObject *value)
 static PyObject *
 find_pretty_printer_from_objfiles (PyObject *value)
 {
-  struct objfile *obj;
+  for (struct objfile *obj : all_objfiles (current_program_space))
+    {
+      gdbpy_ref<> objf = objfile_to_objfile_object (obj);
+      if (objf == NULL)
+	{
+	  /* Ignore the error and continue.  */
+	  PyErr_Clear ();
+	  continue;
+	}
 
-  ALL_OBJFILES (obj)
-  {
-    gdbpy_ref<> objf = objfile_to_objfile_object (obj);
-    if (objf == NULL)
-      {
-	/* Ignore the error and continue.  */
-	PyErr_Clear ();
-	continue;
-      }
-
-    gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL));
-    gdbpy_ref<> function (search_pp_list (pp_list.get (), value));
-
-    /* If there is an error in any objfile list, abort the search and exit.  */
-    if (function == NULL)
-      return NULL;
+      gdbpy_ref<> pp_list (objfpy_get_printers (objf.get (), NULL));
+      gdbpy_ref<> function (search_pp_list (pp_list.get (), value));
 
-    if (function != Py_None)
-      return function.release ();
-  }
+      /* If there is an error in any objfile list, abort the search and exit.  */
+      if (function == NULL)
+	return NULL;
+
+      if (function != Py_None)
+	return function.release ();
+    }
 
   Py_RETURN_NONE;
 }
diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c
index 1c96b585f7..25e4e28a09 100644
--- a/gdb/python/py-xmethods.c
+++ b/gdb/python/py-xmethods.c
@@ -121,8 +121,6 @@  gdbpy_get_matching_xmethod_workers
    struct type *obj_type, const char *method_name,
    std::vector<xmethod_worker_up> *dm_vec)
 {
-  struct objfile *objfile;
-
   gdb_assert (obj_type != NULL && method_name != NULL);
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
@@ -145,7 +143,7 @@  gdbpy_get_matching_xmethod_workers
   /* Gather debug method matchers registered with the object files.
      This could be done differently by iterating over each objfile's matcher
      list individually, but there's no data yet to show it's needed.  */
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       gdbpy_ref<> py_objfile = objfile_to_objfile_object (objfile);
 
diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c
index 8419c2c139..61f9f94f8b 100644
--- a/gdb/solib-spu.c
+++ b/gdb/solib-spu.c
@@ -101,9 +101,8 @@  static void
 append_ocl_sos (struct so_list **link_ptr)
 {
   CORE_ADDR *ocl_program_addr_base;
-  struct objfile *objfile;
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       ocl_program_addr_base
 	= (CORE_ADDR *) objfile_data (objfile, ocl_program_data_key);
diff --git a/gdb/solib.c b/gdb/solib.c
index e1d237e5a4..36c744b7b1 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -677,11 +677,15 @@  solib_read_symbols (struct so_list *so, symfile_add_flags flags)
       TRY
 	{
 	  /* Have we already loaded this shared object?  */
-	  ALL_OBJFILES (so->objfile)
+	  so->objfile = nullptr;
+	  for (struct objfile *objfile : all_objfiles (current_program_space))
 	    {
-	      if (filename_cmp (objfile_name (so->objfile), so->so_name) == 0
-		  && so->objfile->addr_low == so->addr_low)
-		break;
+	      if (filename_cmp (objfile_name (objfile), so->so_name) == 0
+		  && objfile->addr_low == so->addr_low)
+		{
+		  so->objfile = objfile;
+		  break;
+		}
 	    }
 	  if (so->objfile == NULL)
 	    {
diff --git a/gdb/source.c b/gdb/source.c
index f60c7b5b5f..bef4ffe0ad 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -293,13 +293,13 @@  select_source_symtab (struct symtab *s)
   if (current_source_symtab)
     return;
 
-  ALL_OBJFILES (ofp)
-  {
-    if (ofp->sf)
-      s = ofp->sf->qf->find_last_source_symtab (ofp);
-    if (s)
-      current_source_symtab = s;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	s = objfile->sf->qf->find_last_source_symtab (objfile);
+      if (s)
+	current_source_symtab = s;
+    }
   if (current_source_symtab)
     return;
 
diff --git a/gdb/spu-tdep.c b/gdb/spu-tdep.c
index 78a93e34b0..57dd4f1ed0 100644
--- a/gdb/spu-tdep.c
+++ b/gdb/spu-tdep.c
@@ -2000,12 +2000,11 @@  spu_objfile_from_frame (struct frame_info *frame)
 {
   struct gdbarch *gdbarch = get_frame_arch (frame);
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
-  struct objfile *obj;
 
   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
     return NULL;
 
-  ALL_OBJFILES (obj)
+  for (struct objfile *obj : all_objfiles (current_program_space))
     {
       if (obj->sections != obj->sections_end
 	  && SPUADDR_SPU (obj_section_addr (obj->sections)) == tdep->id)
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 8ab6a25de7..55756c9da2 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2313,12 +2313,16 @@  remove_symbol_file_command (const char *args, int from_tty)
 
       addr = parse_and_eval_address (argv[1]);
 
-      ALL_OBJFILES (objf)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
 	{
-	  if ((objf->flags & OBJF_USERLOADED) != 0
-	      && (objf->flags & OBJF_SHARED) != 0
-	      && objf->pspace == pspace && is_addr_in_objfile (addr, objf))
-	    break;
+	  if ((objfile->flags & OBJF_USERLOADED) != 0
+	      && (objfile->flags & OBJF_SHARED) != 0
+	      && objfile->pspace == pspace
+	      && is_addr_in_objfile (addr, objfile))
+	    {
+	      objf = objfile;
+	      break;
+	    }
 	}
     }
   else if (argv[0] != NULL)
@@ -2330,13 +2334,16 @@  remove_symbol_file_command (const char *args, int from_tty)
 
       gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
 
-      ALL_OBJFILES (objf)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
 	{
-	  if ((objf->flags & OBJF_USERLOADED) != 0
-	      && (objf->flags & OBJF_SHARED) != 0
-	      && objf->pspace == pspace
-	      && filename_cmp (filename.get (), objfile_name (objf)) == 0)
-	    break;
+	  if ((objfile->flags & OBJF_USERLOADED) != 0
+	      && (objfile->flags & OBJF_SHARED) != 0
+	      && objfile->pspace == pspace
+	      && filename_cmp (filename.get (), objfile_name (objfile)) == 0)
+	    {
+	      objf = objfile;
+	      break;
+	    }
 	}
     }
 
@@ -3753,16 +3760,14 @@  expand_symtabs_matching
    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
    enum search_domain kind)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      objfile->sf->qf->expand_symtabs_matching (objfile, file_matcher,
-						lookup_name,
-						symbol_matcher,
-						expansion_notify, kind);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	objfile->sf->qf->expand_symtabs_matching (objfile, file_matcher,
+						  lookup_name,
+						  symbol_matcher,
+						  expansion_notify, kind);
+    }
 }
 
 /* Wrapper around the quick_symbol_functions map_symbol_filenames "method".
@@ -3773,14 +3778,12 @@  void
 map_symbol_filenames (symbol_filename_ftype *fun, void *data,
 		      int need_fullname)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf)
-      objfile->sf->qf->map_symbol_filenames (objfile, fun, data,
-					     need_fullname);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf)
+	objfile->sf->qf->map_symbol_filenames (objfile, fun, data,
+					       need_fullname);
+    }
 }
 
 #if GDB_SELF_TEST
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index fd1d298ec1..ac4552d478 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -470,12 +470,11 @@  maintenance_print_symbols (const char *args, int from_tty)
     }
   else
     {
-      struct objfile *objfile;
       struct compunit_symtab *cu;
       struct symtab *s;
       int found = 0;
 
-      ALL_OBJFILES (objfile)
+      for (struct objfile *objfile : all_objfiles (current_program_space))
 	{
 	  int print_for_objfile = 1;
 
@@ -690,7 +689,6 @@  maintenance_print_msymbols (const char *args, int from_tty)
 {
   struct ui_file *outfile = gdb_stdout;
   char *objfile_arg = NULL;
-  struct objfile *objfile;
   int i, outfile_idx;
 
   dont_repeat ();
@@ -734,13 +732,13 @@  maintenance_print_msymbols (const char *args, int from_tty)
       outfile = &arg_outfile;
     }
 
-  ALL_OBJFILES (objfile)
-  {
-    QUIT;
-    if (objfile_arg == NULL
-	|| compare_filenames_for_search (objfile_name (objfile), objfile_arg))
-      dump_msymbols (objfile, outfile);
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      QUIT;
+      if (objfile_arg == NULL
+	  || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
+	dump_msymbols (objfile, outfile);
+    }
 }
 
 static void
diff --git a/gdb/symtab.c b/gdb/symtab.c
index f8c755fdab..041b7f2ead 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -464,7 +464,6 @@  void
 iterate_over_symtabs (const char *name,
 		      gdb::function_view<bool (symtab *)> callback)
 {
-  struct objfile *objfile;
   gdb::unique_xmalloc_ptr<char> real_path;
 
   /* Here we are interested in canonicalizing an absolute path, not
@@ -475,7 +474,7 @@  iterate_over_symtabs (const char *name,
       gdb_assert (IS_ABSOLUTE_PATH (real_path.get ()));
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (iterate_over_some_symtabs (name, real_path.get (),
 				     objfile->compunit_symtabs, NULL,
@@ -486,7 +485,7 @@  iterate_over_symtabs (const char *name,
   /* Same search rules as above apply here, but now we look thru the
      psymtabs.  */
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (objfile->sf
 	  && objfile->sf->qf->map_symtabs_matching_filename (objfile,
@@ -1013,9 +1012,12 @@  matching_obj_sections (struct obj_section *obj_first,
 
   /* Otherwise check that they are in corresponding objfiles.  */
 
-  ALL_OBJFILES (obj)
-    if (obj->obfd == first->owner)
-      break;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    if (objfile->obfd == first->owner)
+      {
+	obj = objfile;
+	break;
+      }
   gdb_assert (obj != NULL);
 
   if (obj->separate_debug_objfile != NULL
@@ -1033,7 +1035,6 @@  matching_obj_sections (struct obj_section *obj_first,
 void
 expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
 {
-  struct objfile *objfile;
   struct bound_minimal_symbol msymbol;
 
   /* If we know that this is not a text address, return failure.  This is
@@ -1048,16 +1049,16 @@  expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
 	  || MSYMBOL_TYPE (msymbol.minsym) == mst_file_bss))
     return;
 
-  ALL_OBJFILES (objfile)
-  {
-    struct compunit_symtab *cust = NULL;
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      struct compunit_symtab *cust = NULL;
 
-    if (objfile->sf)
-      cust = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
-							    pc, section, 0);
-    if (cust)
-      return;
-  }
+      if (objfile->sf)
+	cust = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
+							      pc, section, 0);
+      if (cust)
+	return;
+    }
 }
 
 /* Hash function for the symbol cache.  */
@@ -2576,7 +2577,6 @@  struct block_symbol
 lookup_static_symbol (const char *name, const domain_enum domain)
 {
   struct symbol_cache *cache = get_symbol_cache (current_program_space);
-  struct objfile *objfile;
   struct block_symbol result;
   struct block_symbol_cache *bsc;
   struct symbol_cache_slot *slot;
@@ -2592,7 +2592,7 @@  lookup_static_symbol (const char *name, const domain_enum domain)
       return result;
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       result = lookup_symbol_in_objfile (objfile, STATIC_BLOCK, name, domain);
       if (result.symbol != NULL)
@@ -2793,7 +2793,6 @@  basic_lookup_transparent_type_1 (struct objfile *objfile, int block_index,
 struct type *
 basic_lookup_transparent_type (const char *name)
 {
-  struct objfile *objfile;
   struct type *t;
 
   /* Now search all the global symbols.  Do the symtab's first, then
@@ -2801,19 +2800,19 @@  basic_lookup_transparent_type (const char *name)
      of the desired name as a global, then do psymtab-to-symtab
      conversion on the fly and return the found symbol.  */
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_1 (objfile, GLOBAL_BLOCK, name);
+      if (t)
+	return t;
+    }
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
+      if (t)
+	return t;
+    }
 
   /* Now search the static file-level symbols.
      Not strictly correct, but more useful than an error.
@@ -2822,19 +2821,19 @@  basic_lookup_transparent_type (const char *name)
      of the desired name as a file-level static, then do psymtab-to-symtab
      conversion on the fly and return the found symbol.  */
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_1 (objfile, STATIC_BLOCK, name);
+      if (t)
+	return t;
+    }
 
-  ALL_OBJFILES (objfile)
-  {
-    t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
-    if (t)
-      return t;
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
+      if (t)
+	return t;
+    }
 
   return (struct type *) 0;
 }
@@ -2967,19 +2966,19 @@  find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
 
   /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs).  */
 
-  ALL_OBJFILES (objfile)
-  {
-    struct compunit_symtab *result;
-
-    if (!objfile->sf)
-      continue;
-    result = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile,
-							    msymbol,
-							    pc, section,
-							    1);
-    if (result != NULL)
-      return result;
-  }
+  for (struct objfile *objf : all_objfiles (current_program_space))
+    {
+      struct compunit_symtab *result;
+
+      if (!objf->sf)
+	continue;
+      result = objf->sf->qf->find_pc_sect_compunit_symtab (objf,
+							   msymbol,
+							   pc, section,
+							   1);
+      if (result != NULL)
+	return result;
+    }
 
   return NULL;
 }
@@ -2999,35 +2998,33 @@  find_pc_compunit_symtab (CORE_ADDR pc)
 struct symbol *
 find_symbol_at_address (CORE_ADDR address)
 {
-  struct objfile *objfile;
-
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->sf == NULL
-	|| objfile->sf->qf->find_compunit_symtab_by_address == NULL)
-      continue;
-
-    struct compunit_symtab *symtab
-      = objfile->sf->qf->find_compunit_symtab_by_address (objfile, address);
-    if (symtab != NULL)
-      {
-	const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (symtab);
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->sf == NULL
+	  || objfile->sf->qf->find_compunit_symtab_by_address == NULL)
+	continue;
 
-	for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
-	  {
-	    struct block *b = BLOCKVECTOR_BLOCK (bv, i);
-	    struct block_iterator iter;
-	    struct symbol *sym;
+      struct compunit_symtab *symtab
+	= objfile->sf->qf->find_compunit_symtab_by_address (objfile, address);
+      if (symtab != NULL)
+	{
+	  const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (symtab);
 
-	    ALL_BLOCK_SYMBOLS (b, iter, sym)
+	  for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
 	    {
-	      if (SYMBOL_CLASS (sym) == LOC_STATIC
-		  && SYMBOL_VALUE_ADDRESS (sym) == address)
-		return sym;
+	      struct block *b = BLOCKVECTOR_BLOCK (bv, i);
+	      struct block_iterator iter;
+	      struct symbol *sym;
+
+	      ALL_BLOCK_SYMBOLS (b, iter, sym)
+		{
+		  if (SYMBOL_CLASS (sym) == LOC_STATIC
+		      && SYMBOL_VALUE_ADDRESS (sym) == address)
+		    return sym;
+		}
 	    }
-	  }
-      }
-  }
+	}
+    }
 
   return NULL;
 }
@@ -3352,7 +3349,6 @@  find_line_symtab (struct symtab *symtab, int line,
          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
       int best;
 
-      struct objfile *objfile;
       struct compunit_symtab *cu;
       struct symtab *s;
 
@@ -3361,13 +3357,14 @@  find_line_symtab (struct symtab *symtab, int line,
       else
 	best = 0;
 
-      ALL_OBJFILES (objfile)
-      {
-	if (objfile->sf)
-	  objfile->sf->qf->expand_symtabs_with_fullname (objfile,
-						   symtab_to_fullname (symtab));
-      }
+      for (struct objfile *objfile : all_objfiles (current_program_space))
+	{
+	  if (objfile->sf)
+	    objfile->sf->qf->expand_symtabs_with_fullname
+	      (objfile, symtab_to_fullname (symtab));
+	}
 
+      struct objfile *objfile;
       ALL_FILETABS (objfile, cu, s)
       {
 	struct linetable *l;
@@ -5691,7 +5688,6 @@  static void
 find_main_name (void)
 {
   const char *new_main_name;
-  struct objfile *objfile;
 
   /* First check the objfiles to see whether a debuginfo reader has
      picked up the appropriate main name.  Historically the main name
@@ -5699,15 +5695,15 @@  find_main_name (void)
      relies on the order of objfile creation -- which still isn't
      guaranteed to get the correct answer, but is just probably more
      accurate.  */
-  ALL_OBJFILES (objfile)
-  {
-    if (objfile->per_bfd->name_of_main != NULL)
-      {
-	set_main_name (objfile->per_bfd->name_of_main,
-		       objfile->per_bfd->language_of_main);
-	return;
-      }
-  }
+  for (struct objfile *objfile : all_objfiles (current_program_space))
+    {
+      if (objfile->per_bfd->name_of_main != NULL)
+	{
+	  set_main_name (objfile->per_bfd->name_of_main,
+			 objfile->per_bfd->language_of_main);
+	  return;
+	}
+    }
 
   /* Try to see if the main procedure is in Ada.  */
   /* FIXME: brobecker/2005-03-07: Another way of doing this would
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index 41b9948f6d..dc38080547 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -415,7 +415,6 @@  windows_iterate_over_objfiles_in_search_order
    void *cb_data, struct objfile *current_objfile)
 {
   int stop;
-  struct objfile *objfile;
 
   if (current_objfile)
     {
@@ -424,7 +423,7 @@  windows_iterate_over_objfiles_in_search_order
 	return;
     }
 
-  ALL_OBJFILES (objfile)
+  for (struct objfile *objfile : all_objfiles (current_program_space))
     {
       if (objfile != current_objfile)
 	{