Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: make symtab::compunit return a reference
@ 2026-06-18 18:39 simon.marchi
  2026-06-19 12:16 ` Tom de Vries
  0 siblings, 1 reply; 3+ messages in thread
From: simon.marchi @ 2026-06-18 18:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@polymtl.ca>

The compunit symtab backlink of a symtab is never null (the constructor
asserts it), so make symtab::compunit return a reference instead of a
pointer, and have the symtab constructor take the compunit_symtab as a
reference too.  Update all callers accordingly.

This came up earlier in review, where a caller would check the result of
`symtab->compunit ()` for nullptr, and I pointed out that it was
unnecessary.  Returning a reference makes this clear.

Change-Id: Idf3a6b5fb07a10cd161826ae8a7b826d95bd96c6
---
 gdb/ada-exp.y                     |  2 +-
 gdb/ada-lang.c                    |  6 +--
 gdb/amd64-tdep.c                  |  2 +-
 gdb/annotate.c                    |  4 +-
 gdb/breakpoint.c                  |  8 ++--
 gdb/c-exp.y                       |  2 +-
 gdb/cli/cli-cmds.c                |  8 ++--
 gdb/compile/compile-object-load.c |  2 +-
 gdb/compile/compile.c             |  2 +-
 gdb/disasm.c                      |  4 +-
 gdb/guile/scm-symtab.c            | 12 ++---
 gdb/linespec.c                    | 35 +++++++-------
 gdb/macroscope.c                  |  2 +-
 gdb/mi/mi-cmd-file.c              |  2 +-
 gdb/mi/mi-symbol-cmds.c           |  2 +-
 gdb/objfiles.c                    |  2 +-
 gdb/or1k-tdep.c                   |  5 +-
 gdb/p-exp.y                       |  2 +-
 gdb/parse.c                       |  2 +-
 gdb/python/py-linetable.c         |  4 +-
 gdb/python/py-symtab.c            | 28 +++++------
 gdb/record-btrace.c               |  2 +-
 gdb/source-cache.c                |  6 +--
 gdb/source.c                      | 79 +++++++++++++++----------------
 gdb/symfile.c                     |  2 +-
 gdb/symmisc.c                     | 28 +++++------
 gdb/symtab.c                      | 22 ++++-----
 gdb/symtab.h                      | 11 ++---
 gdb/tui/tui-source.c              |  2 +-
 gdb/tui/tui-winsource.c           |  2 +-
 gdb/z80-tdep.c                    |  4 +-
 31 files changed, 145 insertions(+), 149 deletions(-)

diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index 5c5d4675eb6c..f36426a20840 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -1498,7 +1498,7 @@ block_lookup (const struct block *context, const char *raw_name)
     symtab = NULL;
 
   if (symtab != NULL)
-    result = symtab->compunit ()->blockvector ()->static_block ();
+    result = symtab->compunit ().blockvector ()->static_block ();
   else if (syms.empty () || syms[0].symbol->loc_class () != LOC_BLOCK)
     {
       if (context == NULL)
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 564e4d6194a8..19d8e4ee13b7 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -4806,7 +4806,7 @@ cache_symbol (const char *name, domain_search_flags domain,
      against the global and static blocks of its associated symtab.  */
   if (sym != nullptr)
     {
-      const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
+      const blockvector &bv = *sym->symtab ()->compunit ().blockvector ();
 
       if (bv.global_block () != block && bv.static_block () != block)
 	return;
@@ -11869,8 +11869,8 @@ is_known_support_routine (const frame_info_ptr &frame)
       re_comp (known_runtime_file_name_patterns[i]);
       if (re_exec (lbasename (sal.symtab->filename ())))
 	return true;
-      if (sal.symtab->compunit ()->objfile () != NULL
-	  && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
+      if (sal.symtab->compunit ().objfile () != NULL
+	  && re_exec (objfile_name (sal.symtab->compunit ().objfile ())))
 	return true;
     }
 
diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
index 99448a98f61f..a982e610642f 100644
--- a/gdb/amd64-tdep.c
+++ b/gdb/amd64-tdep.c
@@ -2782,7 +2782,7 @@ amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
   start_pc_sal = find_sal_for_pc_sect (start_pc, NULL, 0);
   if (start_pc_sal.symtab == NULL
       || producer_is_gcc_ge_4 (start_pc_sal.symtab->compunit ()
-			       ->producer ()) < 6
+			       .producer ()) < 6
       || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
     return pc;
 
diff --git a/gdb/annotate.c b/gdb/annotate.c
index 1f3a6d114b18..e6c2f896942a 100644
--- a/gdb/annotate.c
+++ b/gdb/annotate.c
@@ -452,12 +452,12 @@ annotate_source_line (struct symtab *s, int line, int mid_statement,
 	return false;
 
       annotate_source (s->fullname (), line, (int) (*offsets)[line - 1],
-		       mid_statement, s->compunit ()->objfile ()->arch (),
+		       mid_statement, s->compunit ().objfile ()->arch (),
 		       pc);
 
       /* Update the current symtab and line.  */
       symtab_and_line sal;
-      sal.pspace = s->compunit ()->objfile ()->pspace ();
+      sal.pspace = s->compunit ().objfile ()->pspace ();
       sal.symtab = s;
       sal.line = line;
       set_current_source_symtab_and_line (sal);
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 72cfd3af4907..e4df4df04a70 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -7767,7 +7767,7 @@ get_sal_arch (struct symtab_and_line sal)
   if (sal.section != nullptr)
     return sal.section->objfile->arch ();
   if (sal.symtab != nullptr)
-    return sal.symtab->compunit ()->objfile ()->arch ();
+    return sal.symtab->compunit ().objfile ()->arch ();
 
   return nullptr;
 }
@@ -9502,13 +9502,13 @@ resolve_sal_pc (struct symtab_and_line *sal)
       struct symbol *sym;
 
       bv = blockvector_for_pc_sect (sal->pc, 0, &b,
-				    sal->symtab->compunit ());
+				    &sal->symtab->compunit ());
       if (bv != NULL)
 	{
 	  sym = b->linkage_function ();
 	  if (sym != NULL)
 	    sal->section
-	      = sym->obj_section (sal->symtab->compunit ()->objfile ());
+	      = sym->obj_section (sal->symtab->compunit ().objfile ());
 	  else
 	    {
 	      /* It really is worthwhile to have the section, so we'll
@@ -14740,7 +14740,7 @@ breakpoint_free_objfile (struct objfile *objfile)
   for (bp_location *loc : all_bp_locations ())
     {
       if (loc->symtab != nullptr
-	  && loc->symtab->compunit ()->objfile () == objfile)
+	  && loc->symtab->compunit ().objfile () == objfile)
 	{
 	  loc->symtab = nullptr;
 	  loc->symbol = nullptr;
diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 2829d8bccba2..e6a82e2aa3cc 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -3178,7 +3178,7 @@ classify_name (struct parser_state *par_state, const struct block *block,
 	      symtab != nullptr)
 	    {
 	      yylval.bval
-		= symtab->compunit ()->blockvector ()->static_block ();
+		= symtab->compunit ().blockvector ()->static_block ();
 
 	      return FILENAME;
 	    }
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 3abb2f136ec5..f5c85dfc352c 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -1044,7 +1044,7 @@ edit_command (const char *arg, int from_tty)
 	    error (_("No source file for address %s."),
 		   paddress (get_current_arch (), sal.pc));
 
-	  gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
+	  gdbarch = sal.symtab->compunit ().objfile ()->arch ();
 	  sym = find_symbol_for_pc (sal.pc);
 	  if (sym)
 	    gdb_printf ("%ps is in %ps (%ps:%ps).\n",
@@ -1506,7 +1506,7 @@ list_command (const char *arg, int from_tty)
 	error (_("No source file for address %s."),
 	       paddress (get_current_arch (), sal.pc));
 
-      struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
+      struct gdbarch *gdbarch = sal.symtab->compunit ().objfile ()->arch ();
       struct symbol *sym = find_symbol_for_pc (sal.pc);
       if (sym != nullptr)
 	gdb_printf ("%ps is in %s (%ps:%ps).\n",
@@ -2329,8 +2329,8 @@ ambiguous_line_spec (gdb::array_view<const symtab_and_line> sals,
 static int
 cmp_symtabs (const symtab_and_line &sala, const symtab_and_line &salb)
 {
-  const char *dira = sala.symtab->compunit ()->dirname ();
-  const char *dirb = salb.symtab->compunit ()->dirname ();
+  const char *dira = sala.symtab->compunit ().dirname ();
+  const char *dirb = salb.symtab->compunit ().dirname ();
   int r;
 
   if (dira == NULL)
diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
index 20e4c3e51cef..a70282c4e0cb 100644
--- a/gdb/compile/compile-object-load.c
+++ b/gdb/compile/compile-object-load.c
@@ -426,7 +426,7 @@ get_out_value_type (struct symbol *func_sym, struct objfile *objfile,
   lookup_name_info i_ptr_matcher (COMPILE_I_EXPR_PTR_TYPE,
 				  symbol_name_match_type::SEARCH_NAME);
 
-  bv = func_sym->symtab ()->compunit ()->blockvector ();
+  bv = func_sym->symtab ()->compunit ().blockvector ();
   nblocks = bv->num_blocks ();
 
   gdb_ptr_type_sym = NULL;
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index b52859b4bc8a..4a027de26259 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -364,7 +364,7 @@ get_expr_block_and_pc (CORE_ADDR *pc)
 	= get_current_source_symtab_and_line (current_program_space);
 
       if (cursal.symtab)
-	block = cursal.symtab->compunit ()->blockvector ()->static_block ();
+	block = cursal.symtab->compunit ().blockvector ()->static_block ();
 
       if (block != NULL)
 	*pc = block->entry_pc ();
diff --git a/gdb/disasm.c b/gdb/disasm.c
index 81c466c188af..a61bd203b6f1 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -553,7 +553,7 @@ do_mixed_source_and_assembly_deprecated
   mle = (struct deprecated_dis_line_entry *)
     alloca (nlines * sizeof (struct deprecated_dis_line_entry));
 
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
 
   unrelocated_addr unrel_low
     = unrelocated_addr (low - objfile->text_section_offset ());
@@ -701,7 +701,7 @@ do_mixed_source_and_assembly (struct gdbarch *gdbarch,
 
   gdb::unordered_set<dis_line_entry, dis_line_entry_hash> dis_line_table;
 
-  struct objfile *objfile = main_symtab->compunit ()->objfile ();
+  struct objfile *objfile = main_symtab->compunit ().objfile ();
 
   unrelocated_addr unrel_low
     = unrelocated_addr (low - objfile->text_section_offset ());
diff --git a/gdb/guile/scm-symtab.c b/gdb/guile/scm-symtab.c
index b3efcaca22e2..639b134d8e72 100644
--- a/gdb/guile/scm-symtab.c
+++ b/gdb/guile/scm-symtab.c
@@ -136,7 +136,7 @@ stscm_eq_symtab_smob (const void *ap, const void *bp)
 static htab_t
 stscm_objfile_symtab_map (struct symtab *symtab)
 {
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
   htab_t htab = stscm_objfile_data_key.get (objfile);
 
   if (htab == NULL)
@@ -346,7 +346,7 @@ gdbscm_symtab_objfile (SCM self)
     = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct symtab *symtab = st_smob->symtab;
 
-  return ofscm_scm_from_objfile (symtab->compunit ()->objfile ());
+  return ofscm_scm_from_objfile (symtab->compunit ().objfile ());
 }
 
 /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
@@ -360,10 +360,10 @@ gdbscm_symtab_global_block (SCM self)
   const struct symtab *symtab = st_smob->symtab;
   const struct blockvector *blockvector;
 
-  blockvector = symtab->compunit ()->blockvector ();
+  blockvector = symtab->compunit ().blockvector ();
   const struct block *block = blockvector->global_block ();
 
-  return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
+  return bkscm_scm_from_block (block, symtab->compunit ().objfile ());
 }
 
 /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
@@ -377,10 +377,10 @@ gdbscm_symtab_static_block (SCM self)
   const struct symtab *symtab = st_smob->symtab;
   const struct blockvector *blockvector;
 
-  blockvector = symtab->compunit ()->blockvector ();
+  blockvector = symtab->compunit ().blockvector ();
   const struct block *block = blockvector->static_block ();
 
-  return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
+  return bkscm_scm_from_block (block, symtab->compunit ().objfile ());
 }
 \f
 /* Administrivia for sal (symtab-and-line) smobs.  */
diff --git a/gdb/linespec.c b/gdb/linespec.c
index 5371d8426aec..fd7918df5a11 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -1149,7 +1149,7 @@ iterate_over_all_matching_symtabs
 		{
 		  const struct block *block;
 		  int i;
-		  const blockvector *bv = symtab->compunit ()->blockvector ();
+		  const blockvector *bv = symtab->compunit ().blockvector ();
 
 		  for (i = FIRST_LOCAL_BLOCK; i < bv->num_blocks (); i++)
 		    {
@@ -1197,7 +1197,7 @@ iterate_over_file_blocks
 {
   const struct block *block;
 
-  for (block = symtab->compunit ()->blockvector ()->static_block ();
+  for (block = symtab->compunit ().blockvector ()->static_block ();
        block != NULL;
        block = block->superblock ())
     current_language->for_each_symbol (block, name, domain, callback);
@@ -2198,7 +2198,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
       for (const auto &sym : ls->labels.label_symbols)
 	{
 	  struct program_space *pspace
-	    = sym.symbol->symtab ()->compunit ()->objfile ()->pspace ();
+	    = sym.symbol->symtab ()->compunit ().objfile ()->pspace ();
 	  std::optional<symtab_and_line> sal
 	    = symbol_to_sal (state->funfirstline, sym.symbol);
 
@@ -2221,7 +2221,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
 	  for (const auto &sym : ls->function_symbols)
 	    {
 	      program_space *pspace
-		= sym.symbol->symtab ()->compunit ()->objfile ()->pspace ();
+		= sym.symbol->symtab ()->compunit ().objfile ()->pspace ();
 	      set_current_program_space (pspace);
 
 	      /* Don't skip to the first line of the function if we
@@ -3403,7 +3403,7 @@ lookup_prefix_sym (struct linespec_state *state,
 	{
 	  /* Program spaces that are executing startup should have
 	     been filtered out earlier.  */
-	  program_space *pspace = elt->compunit ()->objfile ()->pspace ();
+	  program_space *pspace = elt->compunit ().objfile ()->pspace ();
 
 	  gdb_assert (!pspace->executing_startup);
 	  set_current_program_space (pspace);
@@ -3452,8 +3452,8 @@ compare_symbols (const block_symbol &a, const block_symbol &b)
      which gives unstable sorting results.  While the assumption is that this
      doesn't matter, play it safe and compare program space IDs instead.  */
   int cmp
-    = compare_pspace (a.symbol->symtab ()->compunit ()->objfile ()->pspace (),
-		      b.symbol->symtab ()->compunit ()->objfile ()->pspace ());
+    = compare_pspace (a.symbol->symtab ()->compunit ().objfile ()->pspace (),
+		      b.symbol->symtab ()->compunit ().objfile ()->pspace ());
   if (cmp == -1)
     return true;
   if (cmp == 1)
@@ -3560,7 +3560,7 @@ find_method (struct linespec_state *self,
 
       /* Program spaces that are executing startup should have
 	 been filtered out earlier.  */
-      pspace = sym->symtab ()->compunit ()->objfile ()->pspace ();
+      pspace = sym->symtab ()->compunit ().objfile ()->pspace ();
       gdb_assert (!pspace->executing_startup);
       set_current_program_space (pspace);
       t = check_typedef (sym->type ());
@@ -3572,7 +3572,7 @@ find_method (struct linespec_state *self,
       if (ix == sym_classes->size () - 1
 	  || (pspace
 	      != (sym_classes->at (ix + 1).symbol->symtab ()
-		  ->compunit ()->objfile ()->pspace ())))
+		  ->compunit ().objfile ()->pspace ())))
 	{
 	  /* If we did not find a direct implementation anywhere in
 	     this program space, consider superclasses.  */
@@ -3891,7 +3891,7 @@ find_label_symbols (struct linespec_state *self,
 	{
 	  fn_sym = elt.symbol;
 	  set_current_program_space
-	    (fn_sym->symtab ()->compunit ()->objfile ()->pspace ());
+	    (fn_sym->symtab ()->compunit ().objfile ()->pspace ());
 	  block = fn_sym->value_block ();
 
 	  find_label_symbols_in_block (block, name, fn_sym, completion_mode,
@@ -3918,7 +3918,7 @@ decode_digits_list_mode (linespec_state *self, linespec *ls, int line)
       /* The logic above should ensure this.  */
       gdb_assert (elt != NULL);
 
-      program_space *pspace = elt->compunit ()->objfile ()->pspace ();
+      program_space *pspace = elt->compunit ().objfile ()->pspace ();
       set_current_program_space (pspace);
 
       /* Simplistic search just for the list command.  */
@@ -3955,7 +3955,7 @@ decode_digits_ordinary (struct linespec_state *self,
       /* The logic above should ensure this.  */
       gdb_assert (elt != NULL);
 
-      objfile *objfile = elt->compunit ()->objfile ();
+      objfile *objfile = elt->compunit ().objfile ();
       program_space *pspace = objfile->pspace ();
       set_current_program_space (pspace);
 
@@ -4166,7 +4166,7 @@ search_minsyms_for_name (struct collect_info *info,
     }
   else
     {
-      objfile &objfile = *symtab->compunit ()->objfile ();
+      objfile &objfile = *symtab->compunit ().objfile ();
       program_space *pspace = objfile.pspace ();
 
       if (search_pspace == NULL || pspace == search_pspace)
@@ -4264,13 +4264,14 @@ add_matching_symbols_to_info (const char *name,
 					     add_symbol);
 	  search_minsyms_for_name (info, lookup_name, pspace, NULL);
 	}
-      else if (pspace == NULL || pspace == elt->compunit ()->objfile ()->pspace ())
+      else if (pspace == NULL
+	       || pspace == elt->compunit ().objfile ()->pspace ())
 	{
 	  int prev_len = info->symbols->size ();
 
 	  /* Program spaces that are executing startup should have
 	     been filtered out earlier.  */
-	  program_space *elt_pspace = elt->compunit ()->objfile ()->pspace ();
+	  program_space *elt_pspace = elt->compunit ().objfile ()->pspace ();
 	  gdb_assert (!elt_pspace->executing_startup);
 	  set_current_program_space (elt_pspace);
 	  iterate_over_file_blocks (elt, lookup_name, SEARCH_VFT, add_symbol);
@@ -4305,7 +4306,7 @@ symbol_to_sal (bool funfirstline, symbol *sym)
 	  result.symbol = sym;
 	  result.line = sym->line ();
 	  result.pc = sym->value_address ();
-	  result.pspace = result.symtab->compunit ()->objfile ()->pspace ();
+	  result.pspace = result.symtab->compunit ().objfile ()->pspace ();
 	  result.explicit_pc = 1;
 	  return result;
 	}
@@ -4321,7 +4322,7 @@ symbol_to_sal (bool funfirstline, symbol *sym)
 	  result.symbol = sym;
 	  result.line = sym->line ();
 	  result.pc = sym->value_address ();
-	  result.pspace = result.symtab->compunit ()->objfile ()->pspace ();
+	  result.pspace = result.symtab->compunit ().objfile ()->pspace ();
 	  return result;
 	}
     }
diff --git a/gdb/macroscope.c b/gdb/macroscope.c
index 50fc5c796915..6e01301484f0 100644
--- a/gdb/macroscope.c
+++ b/gdb/macroscope.c
@@ -44,7 +44,7 @@ sal_macro_scope (struct symtab_and_line sal)
   if (sal.symtab == NULL)
     return result;
 
-  cust = sal.symtab->compunit ();
+  cust = &sal.symtab->compunit ();
   if (cust->macro_table () == NULL)
     return result;
 
diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
index 2ce661ad6d26..8b2fb379ec85 100644
--- a/gdb/mi/mi-cmd-file.c
+++ b/gdb/mi/mi-cmd-file.c
@@ -56,7 +56,7 @@ mi_cmd_file_list_exec_source_file (const char *command,
   uiout->field_string ("fullname", symtab_to_fullname (st.symtab));
 
   uiout->field_signed ("macro-info",
-		       st.symtab->compunit ()->macro_table () != NULL);
+		       st.symtab->compunit ().macro_table () != NULL);
 }
 
 /* Implement -file-list-exec-source-files command.  */
diff --git a/gdb/mi/mi-symbol-cmds.c b/gdb/mi/mi-symbol-cmds.c
index d7535c51d2a7..e284fe017298 100644
--- a/gdb/mi/mi-symbol-cmds.c
+++ b/gdb/mi/mi-symbol-cmds.c
@@ -50,7 +50,7 @@ mi_cmd_symbol_list_lines (const char *command, const char *const *argv,
      already sorted by increasing values in the symbol table, so no
      need to perform any other sorting.  */
 
-  struct objfile *objfile = s->compunit ()->objfile ();
+  struct objfile *objfile = s->compunit ().objfile ();
   gdbarch = objfile->arch ();
 
   ui_out_emit_list list_emitter (uiout, "lines");
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 387434153e9a..408ddf1ff830 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -462,7 +462,7 @@ objfile::~objfile ()
      and if so, call clear_last_displayed_sal.  */
   if (symtab *last_displayed_symtab = get_last_displayed_symtab ();
       last_displayed_symtab != nullptr
-      && last_displayed_symtab->compunit ()->objfile () == this)
+      && last_displayed_symtab->compunit ().objfile () == this)
     clear_last_displayed_sal ();
 
   /* Rebuild section map next time we need it.  */
diff --git a/gdb/or1k-tdep.c b/gdb/or1k-tdep.c
index 32fef65dc376..1351dbe2d0c5 100644
--- a/gdb/or1k-tdep.c
+++ b/gdb/or1k-tdep.c
@@ -468,9 +468,8 @@ or1k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
       if (0 != prologue_end)
 	{
 	  struct symtab_and_line prologue_sal = find_sal_for_pc (start_pc, 0);
-	  struct compunit_symtab *compunit
-	    = prologue_sal.symtab->compunit ();
-	  const char *debug_format = compunit->debugformat ();
+	  const char *debug_format
+	    = prologue_sal.symtab->compunit ().debugformat ();
 
 	  if ((NULL != debug_format)
 	      && (strlen ("dwarf") <= strlen (debug_format))
diff --git a/gdb/p-exp.y b/gdb/p-exp.y
index c06ada3b7ff8..1a43837ae333 100644
--- a/gdb/p-exp.y
+++ b/gdb/p-exp.y
@@ -615,7 +615,7 @@ block	:	BLOCKNAME
 			      struct symtab *tem =
 				  lookup_symtab (current_program_space, copy.c_str ());
 			      if (tem)
-				$$ = (tem->compunit ()->blockvector ()
+				$$ = (tem->compunit ().blockvector ()
 				      ->static_block ());
 			      else
 				error (_("No file or function \"%s\"."),
diff --git a/gdb/parse.c b/gdb/parse.c
index fd190d6e15e7..ecb3e30c5cb5 100644
--- a/gdb/parse.c
+++ b/gdb/parse.c
@@ -386,7 +386,7 @@ parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
 
 	  if (cursal.symtab)
 	    expression_context_block
-	      = cursal.symtab->compunit ()->blockvector ()->static_block ();
+	      = cursal.symtab->compunit ().blockvector ()->static_block ();
 
 	  if (expression_context_block)
 	    expression_context_pc = expression_context_block->entry_pc ();
diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
index 1db13af4c12c..faaedc5e9e8f 100644
--- a/gdb/python/py-linetable.c
+++ b/gdb/python/py-linetable.c
@@ -179,7 +179,7 @@ ltpy_get_pcs_for_line (PyObject *self, PyObject *args)
       return gdbpy_handle_gdb_exception (nullptr, except);
     }
 
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
   return build_line_table_tuple_from_entries (objfile, entries);
 }
 
@@ -413,7 +413,7 @@ ltpy_iternext (PyObject *self)
       item = &(symtab->linetable ()->item[iter_obj->current_index]);
     }
 
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
   obj = build_linetable_entry (item->line, item->pc (objfile));
   iter_obj->current_index++;
 
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 9a343167624c..ce5feaeb3909 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -130,7 +130,7 @@ stpy_get_objfile (PyObject *self, void *closure)
 
   STPY_REQUIRE_VALID (self, symtab);
 
-  return objfile_to_objfile_object (symtab->compunit ()->objfile ()).release ();
+  return objfile_to_objfile_object (symtab->compunit ().objfile ()).release ();
 }
 
 /* Getter function for symtab.producer.  */
@@ -139,13 +139,13 @@ static PyObject *
 stpy_get_producer (PyObject *self, void *closure)
 {
   struct symtab *symtab = NULL;
-  struct compunit_symtab *cust;
 
   STPY_REQUIRE_VALID (self, symtab);
-  cust = symtab->compunit ();
-  if (cust->producer () != nullptr)
+  compunit_symtab &cust = symtab->compunit ();
+
+  if (cust.producer () != nullptr)
     {
-      const char *producer = cust->producer ();
+      const char *producer = cust.producer ();
 
       return host_string_to_python_string (producer).release ();
     }
@@ -191,11 +191,11 @@ stpy_global_block (PyObject *self, PyObject *args)
 
   STPY_REQUIRE_VALID (self, symtab);
 
-  blockvector = symtab->compunit ()->blockvector ();
+  blockvector = symtab->compunit ().blockvector ();
   const struct block *block = blockvector->global_block ();
 
   return block_to_block_object (block,
-				symtab->compunit ()->objfile ()).release ();
+				symtab->compunit ().objfile ()).release ();
 }
 
 /* Return the STATIC_BLOCK of the underlying symtab.  */
@@ -208,11 +208,11 @@ stpy_static_block (PyObject *self, PyObject *args)
 
   STPY_REQUIRE_VALID (self, symtab);
 
-  blockvector = symtab->compunit ()->blockvector ();
+  blockvector = symtab->compunit ().blockvector ();
   const struct block *block = blockvector->static_block ();
 
   return block_to_block_object (block,
-				symtab->compunit ()->objfile ()).release ();
+				symtab->compunit ().objfile ()).release ();
 }
 
 /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
@@ -361,7 +361,7 @@ stpy_dealloc (PyObject *obj)
   symtab_object *symtab_obj = (symtab_object *) obj;
 
   if (symtab_obj->symtab != nullptr)
-    stpy_registry.remove (symtab_obj->symtab->compunit ()->objfile(),
+    stpy_registry.remove (symtab_obj->symtab->compunit ().objfile (),
 			  symtab_obj);
 
   Py_TYPE (obj)->tp_free (obj);
@@ -438,7 +438,7 @@ salpy_dealloc (PyObject *self)
   sal_object *self_sal = (sal_object *) self;
 
   if (self_sal->sal != nullptr && self_sal->sal->symtab != nullptr)
-    salpy_registry.remove (self_sal->sal->symtab->compunit ()->objfile (),
+    salpy_registry.remove (self_sal->sal->symtab->compunit ().objfile (),
 			   self_sal);
 
   xfree (self_sal->sal);
@@ -463,7 +463,7 @@ set_sal (sal_object *sal_obj, struct symtab_and_line sal)
      objfile cleanup observer linked list.  */
   symtab *symtab = sal_obj->sal->symtab;
   if (symtab != nullptr)
-    salpy_registry.add (symtab->compunit ()->objfile (), sal_obj);
+    salpy_registry.add (symtab->compunit ().objfile (), sal_obj);
 }
 
 /* Given a symtab, and a symtab_object that has previously been
@@ -476,7 +476,7 @@ set_symtab (symtab_object *obj, struct symtab *symtab)
 {
   obj->symtab = symtab;
   if (symtab != nullptr)
-    stpy_registry.add (symtab->compunit ()->objfile (), obj);
+    stpy_registry.add (symtab->compunit ().objfile (), obj);
 }
 
 /* Create a new symbol table (gdb.Symtab) object that encapsulates the
@@ -491,7 +491,7 @@ symtab_to_symtab_object (struct symtab *symtab)
   if (symtab != nullptr)
     {
       gdbpy_ref<> result
-	= stpy_registry.lookup (symtab->compunit ()->objfile (), symtab);
+	= stpy_registry.lookup (symtab->compunit ().objfile (), symtab);
       if (result != nullptr)
 	return result;
     }
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 4bd755b24f24..dae243ee2dbd 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -729,7 +729,7 @@ btrace_find_line_range (CORE_ADDR pc)
   if (nlines <= 0)
     return btrace_mk_line_range (symtab, 0, 0);
 
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
   unrelocated_addr unrel_pc
     = unrelocated_addr (pc - objfile->text_section_offset ());
 
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index 6e8acab35044..804a6a08a8c3 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -110,9 +110,9 @@ source_cache::get_plain_source_lines (struct symtab *s,
     perror_with_name (symtab_to_filename_for_display (s));
 
   time_t mtime = 0;
-  if (s->compunit ()->objfile () != NULL
-      && s->compunit ()->objfile ()->obfd != NULL)
-    mtime = s->compunit ()->objfile ()->mtime;
+  if (s->compunit ().objfile () != NULL
+      && s->compunit ().objfile ()->obfd != NULL)
+    mtime = s->compunit ().objfile ()->mtime;
   else if (current_program_space->exec_bfd ())
     mtime = current_program_space->ebfd_mtime;
 
diff --git a/gdb/source.c b/gdb/source.c
index 6028c29deae6..5c7c344a1aab 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -299,7 +299,7 @@ clear_current_source_symtab_and_line (objfile *objfile)
     return;
 
   if (loc->symtab () != nullptr
-      && loc->symtab ()->compunit ()->objfile () == objfile)
+      && loc->symtab ()->compunit ().objfile () == objfile)
     clear_current_source_symtab_and_line (objfile->pspace ());
 }
 
@@ -671,7 +671,6 @@ info_source_command (const char *ignore, int from_tty)
   current_source_location *loc
     = get_source_location (current_program_space);
   struct symtab *s = loc->symtab ();
-  struct compunit_symtab *cust;
 
   if (!s)
     {
@@ -679,10 +678,11 @@ info_source_command (const char *ignore, int from_tty)
       return;
     }
 
-  cust = s->compunit ();
+  compunit_symtab &cust = s->compunit ();
+
   gdb_printf (_("Current source file is %s\n"), s->filename ());
-  if (s->compunit ()->dirname () != NULL)
-    gdb_printf (_("Compilation directory is %s\n"), s->compunit ()->dirname ());
+  if (s->compunit ().dirname () != NULL)
+    gdb_printf (_("Compilation directory is %s\n"), s->compunit ().dirname ());
   if (s->fullname () != nullptr)
     gdb_printf (_("Located in %s\n"), s->fullname ());
   if (std::optional<int> last_lineno = last_symtab_line (s);
@@ -693,12 +693,12 @@ info_source_command (const char *ignore, int from_tty)
   gdb_printf (_("Source language is %s.\n"),
 	      language_str (s->language ()));
   gdb_printf (_("Producer is %s.\n"),
-	      (cust->producer ()) != nullptr
-	      ? cust->producer () : _("unknown"));
+	      (cust.producer ()) != nullptr
+	      ? cust.producer () : _("unknown"));
   gdb_printf (_("Compiled with %s debugging format.\n"),
-	      cust->debugformat ());
+	      cust.debugformat ());
   gdb_printf (_("%s preprocessor macro info.\n"),
-	      (cust->macro_table () != nullptr
+	      (cust.macro_table () != nullptr
 	       ? "Includes" : "Does not include"));
 }
 \f
@@ -1117,44 +1117,41 @@ open_source_file (struct symtab *s)
 
   gdb::unique_xmalloc_ptr<char> fullname = s->release_fullname ();
   scoped_fd fd = find_and_open_source (s->filename (),
-				       s->compunit ()->dirname (),
+				       s->compunit ().dirname (),
 				       &fullname);
 
   if (fd.get () < 0)
     {
-      if (s->compunit () != nullptr)
+      const objfile *ofp = s->compunit ().objfile ();
+
+      std::string srcpath;
+      if (IS_ABSOLUTE_PATH (s->filename ()))
+	srcpath = s->filename ();
+      else if (s->compunit ().dirname () != nullptr)
 	{
-	  const objfile *ofp = s->compunit ()->objfile ();
+	  srcpath = s->compunit ().dirname ();
+	  srcpath += SLASH_STRING;
+	  srcpath += s->filename ();
+	}
 
-	  std::string srcpath;
-	  if (IS_ABSOLUTE_PATH (s->filename ()))
-	    srcpath = s->filename ();
-	  else if (s->compunit ()->dirname () != nullptr)
-	    {
-	      srcpath = s->compunit ()->dirname ();
-	      srcpath += SLASH_STRING;
-	      srcpath += s->filename ();
-	    }
+      const struct bfd_build_id *build_id
+	= build_id_bfd_get (ofp->obfd.get ());
 
-	  const struct bfd_build_id *build_id
-	    = build_id_bfd_get (ofp->obfd.get ());
+      /* Query debuginfod for the source file.  */
+      if (build_id != nullptr && !srcpath.empty ())
+	{
+	  scoped_fd query_fd
+	    = debuginfod_source_query (build_id->data,
+				       build_id->size,
+				       srcpath.c_str (),
+				       &fullname);
 
-	  /* Query debuginfod for the source file.  */
-	  if (build_id != nullptr && !srcpath.empty ())
+	  /* Don't return a negative errno from debuginfod_source_query.
+	     It handles the reporting of its own errors.  */
+	  if (query_fd.get () >= 0)
 	    {
-	      scoped_fd query_fd
-		= debuginfod_source_query (build_id->data,
-					   build_id->size,
-					   srcpath.c_str (),
-					   &fullname);
-
-	      /* Don't return a negative errno from debuginfod_source_query.
-		 It handles the reporting of its own errors.  */
-	      if (query_fd.get () >= 0)
-		{
-		  s->set_fullname (std::move (fullname));
-		  return query_fd;
-		}
+	      s->set_fullname (std::move (fullname));
+	      return query_fd;
 	    }
 	}
     }
@@ -1217,11 +1214,11 @@ symtab_to_fullname (struct symtab *s)
 	  /* rewrite_source_path would be applied by find_and_open_source, we
 	     should report the pathname where GDB tried to find the file.  */
 
-	  if (s->compunit ()->dirname () == nullptr
+	  if (s->compunit ().dirname () == nullptr
 	      || IS_ABSOLUTE_PATH (s->filename ()))
 	    fullname = make_unique_xstrdup (s->filename ());
 	  else
-	    fullname.reset (concat (s->compunit ()->dirname (), SLASH_STRING,
+	    fullname.reset (concat (s->compunit ().dirname (), SLASH_STRING,
 				    s->filename (), (char *) NULL));
 
 	  s->set_fullname (rewrite_source_path (fullname.get ()));
@@ -1527,7 +1524,7 @@ info_line_command (const char *arg, int from_tty)
       else if (sal.line > 0
 	       && find_pc_range_for_sal (sal, &start_pc, &end_pc))
 	{
-	  gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
+	  gdbarch *gdbarch = sal.symtab->compunit ().objfile ()->arch ();
 
 	  if (start_pc == end_pc)
 	    {
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 5f046a03567e..296795bda808 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2811,7 +2811,7 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename,
   struct objfile *objfile = cust->objfile ();
   struct symtab *symtab
     = obstack_new<struct symtab> (&objfile->objfile_obstack,
-				  cust,
+				  *cust,
 				  objfile->intern (filename),
 				  objfile->intern (filename_for_id),
 				  deduce_language_from_filename (filename));
diff --git a/gdb/symmisc.c b/gdb/symmisc.c
index 89374bd8a2ff..6d90765fe50b 100644
--- a/gdb/symmisc.c
+++ b/gdb/symmisc.c
@@ -135,7 +135,7 @@ dump_objfile (struct objfile *objfile)
 		      styled_string (file_name_style.style (),
 				     symtab_to_filename_for_display (symtab)),
 		      host_address_to_string (symtab));
-	  if (symtab->compunit ()->objfile () != objfile)
+	  if (symtab->compunit ().objfile () != objfile)
 	    gdb_printf (_(", NOT ON CHAIN!"));
 	  gdb_printf ("\n");
 	}
@@ -244,7 +244,7 @@ dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
 static void
 dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
 {
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
   struct gdbarch *gdbarch = objfile->arch ();
   const struct linetable *l;
   int depth;
@@ -254,10 +254,10 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
 			     symtab_to_filename_for_display (symtab)),
 	      host_address_to_string (symtab));
 
-  if (symtab->compunit ()->dirname () != NULL)
+  if (symtab->compunit ().dirname () != NULL)
     gdb_printf (outfile, _("Compilation directory is %ps\n"),
 		styled_string (file_name_style.style (),
-			       symtab->compunit ()->dirname ()));
+			       symtab->compunit ().dirname ()));
   gdb_printf (outfile, _("Read from object file %ps (%s)\n"),
 	      styled_string (file_name_style.style (),
 			     objfile_name (objfile)),
@@ -288,7 +288,7 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
   if (is_main_symtab_of_compunit_symtab (symtab))
     {
       gdb_printf (outfile, _("\nBlockvector:\n\n"));
-      const blockvector *bv = symtab->compunit ()->blockvector ();
+      const blockvector *bv = symtab->compunit ().blockvector ();
       for (int i = 0; i < bv->num_blocks (); i++)
 	{
 	  const block *b = bv->block (i);
@@ -342,9 +342,9 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
     }
   else
     {
-      compunit_symtab *compunit = symtab->compunit ();
+      compunit_symtab &compunit = symtab->compunit ();
       const char *compunit_filename
-	= symtab_to_filename_for_display (compunit->primary_filetab ());
+	= symtab_to_filename_for_display (compunit.primary_filetab ());
 
       gdb_printf (outfile,
 		  _("\nBlockvector same as owning compunit: %ps\n\n"),
@@ -356,14 +356,14 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
      compunit_symtabs included by this one. */
   if (is_main_symtab_of_compunit_symtab (symtab))
     {
-      struct compunit_symtab *cust = symtab->compunit ();
+      compunit_symtab &cust = symtab->compunit ();
 
-      if (cust->user != nullptr)
+      if (cust.user != nullptr)
 	gdb_printf (outfile, _("Compunit user: %s\n"),
-		    host_address_to_string (cust->user->primary_filetab ()));
+		    host_address_to_string (cust.user->primary_filetab ()));
 
 
-      for (compunit_symtab *include : cust->includes)
+      for (compunit_symtab *include : cust.includes)
 	gdb_printf (outfile, _("Compunit include: %s\n"),
 		    host_address_to_string (include->primary_filetab ()));
     }
@@ -978,14 +978,14 @@ maintenance_print_one_line_table (struct symtab *symtab, void *data)
   const struct linetable *linetable;
   struct objfile *objfile;
 
-  objfile = symtab->compunit ()->objfile ();
+  objfile = symtab->compunit ().objfile ();
   gdb_printf (_("objfile: %ps ((struct objfile *) %s)\n"),
 	      styled_string (file_name_style.style (),
 			     objfile_name (objfile)),
 	      host_address_to_string (objfile));
   gdb_printf (_("compunit_symtab: %s ((struct compunit_symtab *) %s)\n"),
-	      symtab->compunit ()->name,
-	      host_address_to_string (symtab->compunit ()));
+	      symtab->compunit ().name,
+	      host_address_to_string (&symtab->compunit ()));
   gdb_printf (_("symtab: %ps ((struct symtab *) %s)\n"),
 	      styled_string (file_name_style.style (),
 			     symtab_to_fullname (symtab)),
diff --git a/gdb/symtab.c b/gdb/symtab.c
index b38362e6fcce..4ca6f92ff9a0 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -3389,7 +3389,7 @@ find_pc_for_line (struct symtab *symtab, int line, CORE_ADDR *pc)
   if (symtab != NULL)
     {
       l = symtab->linetable ();
-      *pc = l->item[ind].pc (symtab->compunit ()->objfile ());
+      *pc = l->item[ind].pc (symtab->compunit ().objfile ());
       return true;
     }
   else
@@ -3511,10 +3511,10 @@ find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
   symtab_and_line sal = find_sal_for_pc_sect (func_addr, section, 0);
 
   if (funfirstline && sal.symtab != NULL
-      && (sal.symtab->compunit ()->locations_valid ()
+      && (sal.symtab->compunit ().locations_valid ()
 	  || sal.symtab->language () == language_asm))
     {
-      struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
+      struct gdbarch *gdbarch = sal.symtab->compunit ().objfile ()->arch ();
 
       sal.pc = func_addr;
       if (gdbarch_skip_entrypoint_p (gdbarch))
@@ -3592,7 +3592,7 @@ skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
   if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
     return func_addr;
 
-  struct objfile *objfile = symtab->compunit ()->objfile ();
+  struct objfile *objfile = symtab->compunit ().objfile ();
 
   /* Linetable entries are ordered by PC values, see the commentary in
      symtab.h where `struct linetable' is defined.  Thus, the first
@@ -3634,7 +3634,7 @@ skip_prologue_using_linetable (CORE_ADDR func_addr)
     {
       const linetable *linetable = prologue_sal.symtab->linetable ();
 
-      struct objfile *objfile = prologue_sal.symtab->compunit ()->objfile ();
+      struct objfile *objfile = prologue_sal.symtab->compunit ().objfile ();
 
       unrelocated_addr unrel_start
 	= unrelocated_addr (start_pc - objfile->text_section_offset ());
@@ -3727,7 +3727,7 @@ skip_prologue_sal (struct symtab_and_line *sal)
      have proven the CU (Compilation Unit) supports it.  sal->SYMTAB does not
      have to be set by the caller so we use SYM instead.  */
   if (sym != NULL
-      && sym->symtab ()->compunit ()->locations_valid ())
+      && sym->symtab ()->compunit ().locations_valid ())
     force_skip = 0;
 
   symtab_and_line start_sal;
@@ -3885,7 +3885,7 @@ skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
       if (prologue_sal.symtab->language () != language_asm)
 	{
 	  struct objfile *objfile
-	    = prologue_sal.symtab->compunit ()->objfile ();
+	    = prologue_sal.symtab->compunit ().objfile ();
 	  const linetable *linetable = prologue_sal.symtab->linetable ();
 	  gdb_assert (linetable->nitems > 0);
 	  int idx = 0;
@@ -3986,7 +3986,7 @@ find_epilogue_using_linetable (CORE_ADDR func_addr)
   const struct symtab_and_line sal = find_sal_for_pc (end_pc - 1, 0);
   if (sal.symtab != nullptr && sal.symtab->language () != language_asm)
     {
-      struct objfile *objfile = sal.symtab->compunit ()->objfile ();
+      struct objfile *objfile = sal.symtab->compunit ().objfile ();
       unrelocated_addr unrel_start
 	= unrelocated_addr (start_pc - objfile->text_section_offset ());
       unrelocated_addr unrel_end
@@ -6155,7 +6155,7 @@ collect_file_symbol_completion_matches (completion_tracker &tracker,
      for symbols which match.  */
   for_each_symtab (current_program_space, srcfile, [&] (symtab *s)
     {
-      add_symtab_completions (s->compunit (),
+      add_symtab_completions (&s->compunit (),
 			      tracker, mode, lookup_name,
 			      sym_text, word, TYPE_CODE_UNDEF);
     });
@@ -6560,7 +6560,7 @@ struct objfile *
 symbol::objfile () const
 {
   gdb_assert (is_objfile_owned ());
-  return owner.symtab->compunit ()->objfile ();
+  return owner.symtab->compunit ().objfile ();
 }
 
 /* See symtab.h.  */
@@ -6570,7 +6570,7 @@ symbol::arch () const
 {
   if (!is_objfile_owned ())
     return owner.arch;
-  return owner.symtab->compunit ()->objfile ()->arch ();
+  return owner.symtab->compunit ().objfile ()->arch ();
 }
 
 /* See symtab.h.  */
diff --git a/gdb/symtab.h b/gdb/symtab.h
index b29b72aba00d..fc2c1ed23aab 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -1671,21 +1671,20 @@ struct linetable
 
 struct symtab
 {
-  symtab (struct compunit_symtab *cust, const char *filename,
+  symtab (struct compunit_symtab &cust, const char *filename,
 	  const char *filename_for_id, enum language language)
     : m_filename (filename),
       m_filename_for_id (filename_for_id),
-      m_compunit (cust),
+      m_compunit (&cust),
       m_language (language)
   {
     gdb_assert (m_filename != nullptr);
     gdb_assert (m_filename_for_id != nullptr);
-    gdb_assert (m_compunit != nullptr);
   }
 
-  struct compunit_symtab *compunit () const
+  struct compunit_symtab &compunit () const
   {
-    return m_compunit;
+    return *m_compunit;
   }
 
   const struct linetable *linetable () const
@@ -2028,7 +2027,7 @@ struct compunit_symtab : intrusive_list_node<compunit_symtab>
 static inline bool
 is_main_symtab_of_compunit_symtab (struct symtab *symtab)
 {
-  return symtab == symtab->compunit ()->primary_filetab ();
+  return symtab == symtab->compunit ().primary_filetab ();
 }
 
 /* Return true if epilogue unwind info of CUST is valid.  */
diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
index 783d4b2a1c24..466e9972106d 100644
--- a/gdb/tui/tui-source.c
+++ b/gdb/tui/tui-source.c
@@ -79,7 +79,7 @@ tui_source_window::set_contents (struct gdbarch *arch,
   m_fullname = make_unique_xstrdup (symtab_to_fullname (s));
 
   cur_line = 0;
-  m_gdbarch = s->compunit ()->objfile ()->arch ();
+  m_gdbarch = s->compunit ().objfile ()->arch ();
   m_start_line_or_addr.loa = LOA_LINE;
   cur_line_no = m_start_line_or_addr.u.line_no = line_no;
 
diff --git a/gdb/tui/tui-winsource.c b/gdb/tui/tui-winsource.c
index 8609a7cd4fe2..e3f64892e271 100644
--- a/gdb/tui/tui-winsource.c
+++ b/gdb/tui/tui-winsource.c
@@ -209,7 +209,7 @@ tui_update_source_windows_with_line (struct symtab_and_line sal)
   if (sal.symtab != nullptr)
     {
       find_pc_for_line (sal.symtab, sal.line, &sal.pc);
-      gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
+      gdbarch = sal.symtab->compunit ().objfile ()->arch ();
     }
 
   for (struct tui_source_window_base *win_info : tui_source_windows ())
diff --git a/gdb/z80-tdep.c b/gdb/z80-tdep.c
index 9da18855d710..7359f9bcfb6d 100644
--- a/gdb/z80-tdep.c
+++ b/gdb/z80-tdep.c
@@ -498,8 +498,8 @@ z80_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
 
       if (prologue_sal.symtab != nullptr)
 	{
-	  struct compunit_symtab *compunit = prologue_sal.symtab->compunit ();
-	  const char *debug_format = compunit->debugformat ();
+	  const char *debug_format
+	    = prologue_sal.symtab->compunit ().debugformat ();
 
 	  if (debug_format != nullptr
 	      && !strncasecmp ("dwarf", debug_format, strlen ("dwarf")))

base-commit: e4d214db01492b035874eaac2c3a022e6c3d5bc8
-- 
2.54.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: make symtab::compunit return a reference
  2026-06-18 18:39 [PATCH] gdb: make symtab::compunit return a reference simon.marchi
@ 2026-06-19 12:16 ` Tom de Vries
  2026-06-19 14:22   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom de Vries @ 2026-06-19 12:16 UTC (permalink / raw)
  To: simon.marchi, gdb-patches

On 6/18/26 8:39 PM, simon.marchi@polymtl.ca wrote:
> From: Simon Marchi <simon.marchi@polymtl.ca>
> 
> The compunit symtab backlink of a symtab is never null (the constructor
> asserts it), so make symtab::compunit return a reference instead of a
> pointer, and have the symtab constructor take the compunit_symtab as a
> reference too.  Update all callers accordingly.
> 
> This came up earlier in review, where a caller would check the result of
> `symtab->compunit ()` for nullptr, and I pointed out that it was
> unnecessary.  Returning a reference makes this clear.
> 

Hi Simon,

Looks reasonable to me.

Reviewed-By: Tom de Vries <tdevries@suse.de>

Thanks,
- Tom

> Change-Id: Idf3a6b5fb07a10cd161826ae8a7b826d95bd96c6
> ---
>   gdb/ada-exp.y                     |  2 +-
>   gdb/ada-lang.c                    |  6 +--
>   gdb/amd64-tdep.c                  |  2 +-
>   gdb/annotate.c                    |  4 +-
>   gdb/breakpoint.c                  |  8 ++--
>   gdb/c-exp.y                       |  2 +-
>   gdb/cli/cli-cmds.c                |  8 ++--
>   gdb/compile/compile-object-load.c |  2 +-
>   gdb/compile/compile.c             |  2 +-
>   gdb/disasm.c                      |  4 +-
>   gdb/guile/scm-symtab.c            | 12 ++---
>   gdb/linespec.c                    | 35 +++++++-------
>   gdb/macroscope.c                  |  2 +-
>   gdb/mi/mi-cmd-file.c              |  2 +-
>   gdb/mi/mi-symbol-cmds.c           |  2 +-
>   gdb/objfiles.c                    |  2 +-
>   gdb/or1k-tdep.c                   |  5 +-
>   gdb/p-exp.y                       |  2 +-
>   gdb/parse.c                       |  2 +-
>   gdb/python/py-linetable.c         |  4 +-
>   gdb/python/py-symtab.c            | 28 +++++------
>   gdb/record-btrace.c               |  2 +-
>   gdb/source-cache.c                |  6 +--
>   gdb/source.c                      | 79 +++++++++++++++----------------
>   gdb/symfile.c                     |  2 +-
>   gdb/symmisc.c                     | 28 +++++------
>   gdb/symtab.c                      | 22 ++++-----
>   gdb/symtab.h                      | 11 ++---
>   gdb/tui/tui-source.c              |  2 +-
>   gdb/tui/tui-winsource.c           |  2 +-
>   gdb/z80-tdep.c                    |  4 +-
>   31 files changed, 145 insertions(+), 149 deletions(-)
> 
> diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
> index 5c5d4675eb6c..f36426a20840 100644
> --- a/gdb/ada-exp.y
> +++ b/gdb/ada-exp.y
> @@ -1498,7 +1498,7 @@ block_lookup (const struct block *context, const char *raw_name)
>       symtab = NULL;
>   
>     if (symtab != NULL)
> -    result = symtab->compunit ()->blockvector ()->static_block ();
> +    result = symtab->compunit ().blockvector ()->static_block ();
>     else if (syms.empty () || syms[0].symbol->loc_class () != LOC_BLOCK)
>       {
>         if (context == NULL)
> diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
> index 564e4d6194a8..19d8e4ee13b7 100644
> --- a/gdb/ada-lang.c
> +++ b/gdb/ada-lang.c
> @@ -4806,7 +4806,7 @@ cache_symbol (const char *name, domain_search_flags domain,
>        against the global and static blocks of its associated symtab.  */
>     if (sym != nullptr)
>       {
> -      const blockvector &bv = *sym->symtab ()->compunit ()->blockvector ();
> +      const blockvector &bv = *sym->symtab ()->compunit ().blockvector ();
>   
>         if (bv.global_block () != block && bv.static_block () != block)
>   	return;
> @@ -11869,8 +11869,8 @@ is_known_support_routine (const frame_info_ptr &frame)
>         re_comp (known_runtime_file_name_patterns[i]);
>         if (re_exec (lbasename (sal.symtab->filename ())))
>   	return true;
> -      if (sal.symtab->compunit ()->objfile () != NULL
> -	  && re_exec (objfile_name (sal.symtab->compunit ()->objfile ())))
> +      if (sal.symtab->compunit ().objfile () != NULL
> +	  && re_exec (objfile_name (sal.symtab->compunit ().objfile ())))
>   	return true;
>       }
>   
> diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c
> index 99448a98f61f..a982e610642f 100644
> --- a/gdb/amd64-tdep.c
> +++ b/gdb/amd64-tdep.c
> @@ -2782,7 +2782,7 @@ amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
>     start_pc_sal = find_sal_for_pc_sect (start_pc, NULL, 0);
>     if (start_pc_sal.symtab == NULL
>         || producer_is_gcc_ge_4 (start_pc_sal.symtab->compunit ()
> -			       ->producer ()) < 6
> +			       .producer ()) < 6
>         || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
>       return pc;
>   
> diff --git a/gdb/annotate.c b/gdb/annotate.c
> index 1f3a6d114b18..e6c2f896942a 100644
> --- a/gdb/annotate.c
> +++ b/gdb/annotate.c
> @@ -452,12 +452,12 @@ annotate_source_line (struct symtab *s, int line, int mid_statement,
>   	return false;
>   
>         annotate_source (s->fullname (), line, (int) (*offsets)[line - 1],
> -		       mid_statement, s->compunit ()->objfile ()->arch (),
> +		       mid_statement, s->compunit ().objfile ()->arch (),
>   		       pc);
>   
>         /* Update the current symtab and line.  */
>         symtab_and_line sal;
> -      sal.pspace = s->compunit ()->objfile ()->pspace ();
> +      sal.pspace = s->compunit ().objfile ()->pspace ();
>         sal.symtab = s;
>         sal.line = line;
>         set_current_source_symtab_and_line (sal);
> diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
> index 72cfd3af4907..e4df4df04a70 100644
> --- a/gdb/breakpoint.c
> +++ b/gdb/breakpoint.c
> @@ -7767,7 +7767,7 @@ get_sal_arch (struct symtab_and_line sal)
>     if (sal.section != nullptr)
>       return sal.section->objfile->arch ();
>     if (sal.symtab != nullptr)
> -    return sal.symtab->compunit ()->objfile ()->arch ();
> +    return sal.symtab->compunit ().objfile ()->arch ();
>   
>     return nullptr;
>   }
> @@ -9502,13 +9502,13 @@ resolve_sal_pc (struct symtab_and_line *sal)
>         struct symbol *sym;
>   
>         bv = blockvector_for_pc_sect (sal->pc, 0, &b,
> -				    sal->symtab->compunit ());
> +				    &sal->symtab->compunit ());
>         if (bv != NULL)
>   	{
>   	  sym = b->linkage_function ();
>   	  if (sym != NULL)
>   	    sal->section
> -	      = sym->obj_section (sal->symtab->compunit ()->objfile ());
> +	      = sym->obj_section (sal->symtab->compunit ().objfile ());
>   	  else
>   	    {
>   	      /* It really is worthwhile to have the section, so we'll
> @@ -14740,7 +14740,7 @@ breakpoint_free_objfile (struct objfile *objfile)
>     for (bp_location *loc : all_bp_locations ())
>       {
>         if (loc->symtab != nullptr
> -	  && loc->symtab->compunit ()->objfile () == objfile)
> +	  && loc->symtab->compunit ().objfile () == objfile)
>   	{
>   	  loc->symtab = nullptr;
>   	  loc->symbol = nullptr;
> diff --git a/gdb/c-exp.y b/gdb/c-exp.y
> index 2829d8bccba2..e6a82e2aa3cc 100644
> --- a/gdb/c-exp.y
> +++ b/gdb/c-exp.y
> @@ -3178,7 +3178,7 @@ classify_name (struct parser_state *par_state, const struct block *block,
>   	      symtab != nullptr)
>   	    {
>   	      yylval.bval
> -		= symtab->compunit ()->blockvector ()->static_block ();
> +		= symtab->compunit ().blockvector ()->static_block ();
>   
>   	      return FILENAME;
>   	    }
> diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
> index 3abb2f136ec5..f5c85dfc352c 100644
> --- a/gdb/cli/cli-cmds.c
> +++ b/gdb/cli/cli-cmds.c
> @@ -1044,7 +1044,7 @@ edit_command (const char *arg, int from_tty)
>   	    error (_("No source file for address %s."),
>   		   paddress (get_current_arch (), sal.pc));
>   
> -	  gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
> +	  gdbarch = sal.symtab->compunit ().objfile ()->arch ();
>   	  sym = find_symbol_for_pc (sal.pc);
>   	  if (sym)
>   	    gdb_printf ("%ps is in %ps (%ps:%ps).\n",
> @@ -1506,7 +1506,7 @@ list_command (const char *arg, int from_tty)
>   	error (_("No source file for address %s."),
>   	       paddress (get_current_arch (), sal.pc));
>   
> -      struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
> +      struct gdbarch *gdbarch = sal.symtab->compunit ().objfile ()->arch ();
>         struct symbol *sym = find_symbol_for_pc (sal.pc);
>         if (sym != nullptr)
>   	gdb_printf ("%ps is in %s (%ps:%ps).\n",
> @@ -2329,8 +2329,8 @@ ambiguous_line_spec (gdb::array_view<const symtab_and_line> sals,
>   static int
>   cmp_symtabs (const symtab_and_line &sala, const symtab_and_line &salb)
>   {
> -  const char *dira = sala.symtab->compunit ()->dirname ();
> -  const char *dirb = salb.symtab->compunit ()->dirname ();
> +  const char *dira = sala.symtab->compunit ().dirname ();
> +  const char *dirb = salb.symtab->compunit ().dirname ();
>     int r;
>   
>     if (dira == NULL)
> diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c
> index 20e4c3e51cef..a70282c4e0cb 100644
> --- a/gdb/compile/compile-object-load.c
> +++ b/gdb/compile/compile-object-load.c
> @@ -426,7 +426,7 @@ get_out_value_type (struct symbol *func_sym, struct objfile *objfile,
>     lookup_name_info i_ptr_matcher (COMPILE_I_EXPR_PTR_TYPE,
>   				  symbol_name_match_type::SEARCH_NAME);
>   
> -  bv = func_sym->symtab ()->compunit ()->blockvector ();
> +  bv = func_sym->symtab ()->compunit ().blockvector ();
>     nblocks = bv->num_blocks ();
>   
>     gdb_ptr_type_sym = NULL;
> diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
> index b52859b4bc8a..4a027de26259 100644
> --- a/gdb/compile/compile.c
> +++ b/gdb/compile/compile.c
> @@ -364,7 +364,7 @@ get_expr_block_and_pc (CORE_ADDR *pc)
>   	= get_current_source_symtab_and_line (current_program_space);
>   
>         if (cursal.symtab)
> -	block = cursal.symtab->compunit ()->blockvector ()->static_block ();
> +	block = cursal.symtab->compunit ().blockvector ()->static_block ();
>   
>         if (block != NULL)
>   	*pc = block->entry_pc ();
> diff --git a/gdb/disasm.c b/gdb/disasm.c
> index 81c466c188af..a61bd203b6f1 100644
> --- a/gdb/disasm.c
> +++ b/gdb/disasm.c
> @@ -553,7 +553,7 @@ do_mixed_source_and_assembly_deprecated
>     mle = (struct deprecated_dis_line_entry *)
>       alloca (nlines * sizeof (struct deprecated_dis_line_entry));
>   
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>   
>     unrelocated_addr unrel_low
>       = unrelocated_addr (low - objfile->text_section_offset ());
> @@ -701,7 +701,7 @@ do_mixed_source_and_assembly (struct gdbarch *gdbarch,
>   
>     gdb::unordered_set<dis_line_entry, dis_line_entry_hash> dis_line_table;
>   
> -  struct objfile *objfile = main_symtab->compunit ()->objfile ();
> +  struct objfile *objfile = main_symtab->compunit ().objfile ();
>   
>     unrelocated_addr unrel_low
>       = unrelocated_addr (low - objfile->text_section_offset ());
> diff --git a/gdb/guile/scm-symtab.c b/gdb/guile/scm-symtab.c
> index b3efcaca22e2..639b134d8e72 100644
> --- a/gdb/guile/scm-symtab.c
> +++ b/gdb/guile/scm-symtab.c
> @@ -136,7 +136,7 @@ stscm_eq_symtab_smob (const void *ap, const void *bp)
>   static htab_t
>   stscm_objfile_symtab_map (struct symtab *symtab)
>   {
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>     htab_t htab = stscm_objfile_data_key.get (objfile);
>   
>     if (htab == NULL)
> @@ -346,7 +346,7 @@ gdbscm_symtab_objfile (SCM self)
>       = stscm_get_valid_symtab_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
>     const struct symtab *symtab = st_smob->symtab;
>   
> -  return ofscm_scm_from_objfile (symtab->compunit ()->objfile ());
> +  return ofscm_scm_from_objfile (symtab->compunit ().objfile ());
>   }
>   
>   /* (symtab-global-block <gdb:symtab>) -> <gdb:block>
> @@ -360,10 +360,10 @@ gdbscm_symtab_global_block (SCM self)
>     const struct symtab *symtab = st_smob->symtab;
>     const struct blockvector *blockvector;
>   
> -  blockvector = symtab->compunit ()->blockvector ();
> +  blockvector = symtab->compunit ().blockvector ();
>     const struct block *block = blockvector->global_block ();
>   
> -  return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
> +  return bkscm_scm_from_block (block, symtab->compunit ().objfile ());
>   }
>   
>   /* (symtab-static-block <gdb:symtab>) -> <gdb:block>
> @@ -377,10 +377,10 @@ gdbscm_symtab_static_block (SCM self)
>     const struct symtab *symtab = st_smob->symtab;
>     const struct blockvector *blockvector;
>   
> -  blockvector = symtab->compunit ()->blockvector ();
> +  blockvector = symtab->compunit ().blockvector ();
>     const struct block *block = blockvector->static_block ();
>   
> -  return bkscm_scm_from_block (block, symtab->compunit ()->objfile ());
> +  return bkscm_scm_from_block (block, symtab->compunit ().objfile ());
>   }
>   \f
>   /* Administrivia for sal (symtab-and-line) smobs.  */
> diff --git a/gdb/linespec.c b/gdb/linespec.c
> index 5371d8426aec..fd7918df5a11 100644
> --- a/gdb/linespec.c
> +++ b/gdb/linespec.c
> @@ -1149,7 +1149,7 @@ iterate_over_all_matching_symtabs
>   		{
>   		  const struct block *block;
>   		  int i;
> -		  const blockvector *bv = symtab->compunit ()->blockvector ();
> +		  const blockvector *bv = symtab->compunit ().blockvector ();
>   
>   		  for (i = FIRST_LOCAL_BLOCK; i < bv->num_blocks (); i++)
>   		    {
> @@ -1197,7 +1197,7 @@ iterate_over_file_blocks
>   {
>     const struct block *block;
>   
> -  for (block = symtab->compunit ()->blockvector ()->static_block ();
> +  for (block = symtab->compunit ().blockvector ()->static_block ();
>          block != NULL;
>          block = block->superblock ())
>       current_language->for_each_symbol (block, name, domain, callback);
> @@ -2198,7 +2198,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
>         for (const auto &sym : ls->labels.label_symbols)
>   	{
>   	  struct program_space *pspace
> -	    = sym.symbol->symtab ()->compunit ()->objfile ()->pspace ();
> +	    = sym.symbol->symtab ()->compunit ().objfile ()->pspace ();
>   	  std::optional<symtab_and_line> sal
>   	    = symbol_to_sal (state->funfirstline, sym.symbol);
>   
> @@ -2221,7 +2221,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls)
>   	  for (const auto &sym : ls->function_symbols)
>   	    {
>   	      program_space *pspace
> -		= sym.symbol->symtab ()->compunit ()->objfile ()->pspace ();
> +		= sym.symbol->symtab ()->compunit ().objfile ()->pspace ();
>   	      set_current_program_space (pspace);
>   
>   	      /* Don't skip to the first line of the function if we
> @@ -3403,7 +3403,7 @@ lookup_prefix_sym (struct linespec_state *state,
>   	{
>   	  /* Program spaces that are executing startup should have
>   	     been filtered out earlier.  */
> -	  program_space *pspace = elt->compunit ()->objfile ()->pspace ();
> +	  program_space *pspace = elt->compunit ().objfile ()->pspace ();
>   
>   	  gdb_assert (!pspace->executing_startup);
>   	  set_current_program_space (pspace);
> @@ -3452,8 +3452,8 @@ compare_symbols (const block_symbol &a, const block_symbol &b)
>        which gives unstable sorting results.  While the assumption is that this
>        doesn't matter, play it safe and compare program space IDs instead.  */
>     int cmp
> -    = compare_pspace (a.symbol->symtab ()->compunit ()->objfile ()->pspace (),
> -		      b.symbol->symtab ()->compunit ()->objfile ()->pspace ());
> +    = compare_pspace (a.symbol->symtab ()->compunit ().objfile ()->pspace (),
> +		      b.symbol->symtab ()->compunit ().objfile ()->pspace ());
>     if (cmp == -1)
>       return true;
>     if (cmp == 1)
> @@ -3560,7 +3560,7 @@ find_method (struct linespec_state *self,
>   
>         /* Program spaces that are executing startup should have
>   	 been filtered out earlier.  */
> -      pspace = sym->symtab ()->compunit ()->objfile ()->pspace ();
> +      pspace = sym->symtab ()->compunit ().objfile ()->pspace ();
>         gdb_assert (!pspace->executing_startup);
>         set_current_program_space (pspace);
>         t = check_typedef (sym->type ());
> @@ -3572,7 +3572,7 @@ find_method (struct linespec_state *self,
>         if (ix == sym_classes->size () - 1
>   	  || (pspace
>   	      != (sym_classes->at (ix + 1).symbol->symtab ()
> -		  ->compunit ()->objfile ()->pspace ())))
> +		  ->compunit ().objfile ()->pspace ())))
>   	{
>   	  /* If we did not find a direct implementation anywhere in
>   	     this program space, consider superclasses.  */
> @@ -3891,7 +3891,7 @@ find_label_symbols (struct linespec_state *self,
>   	{
>   	  fn_sym = elt.symbol;
>   	  set_current_program_space
> -	    (fn_sym->symtab ()->compunit ()->objfile ()->pspace ());
> +	    (fn_sym->symtab ()->compunit ().objfile ()->pspace ());
>   	  block = fn_sym->value_block ();
>   
>   	  find_label_symbols_in_block (block, name, fn_sym, completion_mode,
> @@ -3918,7 +3918,7 @@ decode_digits_list_mode (linespec_state *self, linespec *ls, int line)
>         /* The logic above should ensure this.  */
>         gdb_assert (elt != NULL);
>   
> -      program_space *pspace = elt->compunit ()->objfile ()->pspace ();
> +      program_space *pspace = elt->compunit ().objfile ()->pspace ();
>         set_current_program_space (pspace);
>   
>         /* Simplistic search just for the list command.  */
> @@ -3955,7 +3955,7 @@ decode_digits_ordinary (struct linespec_state *self,
>         /* The logic above should ensure this.  */
>         gdb_assert (elt != NULL);
>   
> -      objfile *objfile = elt->compunit ()->objfile ();
> +      objfile *objfile = elt->compunit ().objfile ();
>         program_space *pspace = objfile->pspace ();
>         set_current_program_space (pspace);
>   
> @@ -4166,7 +4166,7 @@ search_minsyms_for_name (struct collect_info *info,
>       }
>     else
>       {
> -      objfile &objfile = *symtab->compunit ()->objfile ();
> +      objfile &objfile = *symtab->compunit ().objfile ();
>         program_space *pspace = objfile.pspace ();
>   
>         if (search_pspace == NULL || pspace == search_pspace)
> @@ -4264,13 +4264,14 @@ add_matching_symbols_to_info (const char *name,
>   					     add_symbol);
>   	  search_minsyms_for_name (info, lookup_name, pspace, NULL);
>   	}
> -      else if (pspace == NULL || pspace == elt->compunit ()->objfile ()->pspace ())
> +      else if (pspace == NULL
> +	       || pspace == elt->compunit ().objfile ()->pspace ())
>   	{
>   	  int prev_len = info->symbols->size ();
>   
>   	  /* Program spaces that are executing startup should have
>   	     been filtered out earlier.  */
> -	  program_space *elt_pspace = elt->compunit ()->objfile ()->pspace ();
> +	  program_space *elt_pspace = elt->compunit ().objfile ()->pspace ();
>   	  gdb_assert (!elt_pspace->executing_startup);
>   	  set_current_program_space (elt_pspace);
>   	  iterate_over_file_blocks (elt, lookup_name, SEARCH_VFT, add_symbol);
> @@ -4305,7 +4306,7 @@ symbol_to_sal (bool funfirstline, symbol *sym)
>   	  result.symbol = sym;
>   	  result.line = sym->line ();
>   	  result.pc = sym->value_address ();
> -	  result.pspace = result.symtab->compunit ()->objfile ()->pspace ();
> +	  result.pspace = result.symtab->compunit ().objfile ()->pspace ();
>   	  result.explicit_pc = 1;
>   	  return result;
>   	}
> @@ -4321,7 +4322,7 @@ symbol_to_sal (bool funfirstline, symbol *sym)
>   	  result.symbol = sym;
>   	  result.line = sym->line ();
>   	  result.pc = sym->value_address ();
> -	  result.pspace = result.symtab->compunit ()->objfile ()->pspace ();
> +	  result.pspace = result.symtab->compunit ().objfile ()->pspace ();
>   	  return result;
>   	}
>       }
> diff --git a/gdb/macroscope.c b/gdb/macroscope.c
> index 50fc5c796915..6e01301484f0 100644
> --- a/gdb/macroscope.c
> +++ b/gdb/macroscope.c
> @@ -44,7 +44,7 @@ sal_macro_scope (struct symtab_and_line sal)
>     if (sal.symtab == NULL)
>       return result;
>   
> -  cust = sal.symtab->compunit ();
> +  cust = &sal.symtab->compunit ();
>     if (cust->macro_table () == NULL)
>       return result;
>   
> diff --git a/gdb/mi/mi-cmd-file.c b/gdb/mi/mi-cmd-file.c
> index 2ce661ad6d26..8b2fb379ec85 100644
> --- a/gdb/mi/mi-cmd-file.c
> +++ b/gdb/mi/mi-cmd-file.c
> @@ -56,7 +56,7 @@ mi_cmd_file_list_exec_source_file (const char *command,
>     uiout->field_string ("fullname", symtab_to_fullname (st.symtab));
>   
>     uiout->field_signed ("macro-info",
> -		       st.symtab->compunit ()->macro_table () != NULL);
> +		       st.symtab->compunit ().macro_table () != NULL);
>   }
>   
>   /* Implement -file-list-exec-source-files command.  */
> diff --git a/gdb/mi/mi-symbol-cmds.c b/gdb/mi/mi-symbol-cmds.c
> index d7535c51d2a7..e284fe017298 100644
> --- a/gdb/mi/mi-symbol-cmds.c
> +++ b/gdb/mi/mi-symbol-cmds.c
> @@ -50,7 +50,7 @@ mi_cmd_symbol_list_lines (const char *command, const char *const *argv,
>        already sorted by increasing values in the symbol table, so no
>        need to perform any other sorting.  */
>   
> -  struct objfile *objfile = s->compunit ()->objfile ();
> +  struct objfile *objfile = s->compunit ().objfile ();
>     gdbarch = objfile->arch ();
>   
>     ui_out_emit_list list_emitter (uiout, "lines");
> diff --git a/gdb/objfiles.c b/gdb/objfiles.c
> index 387434153e9a..408ddf1ff830 100644
> --- a/gdb/objfiles.c
> +++ b/gdb/objfiles.c
> @@ -462,7 +462,7 @@ objfile::~objfile ()
>        and if so, call clear_last_displayed_sal.  */
>     if (symtab *last_displayed_symtab = get_last_displayed_symtab ();
>         last_displayed_symtab != nullptr
> -      && last_displayed_symtab->compunit ()->objfile () == this)
> +      && last_displayed_symtab->compunit ().objfile () == this)
>       clear_last_displayed_sal ();
>   
>     /* Rebuild section map next time we need it.  */
> diff --git a/gdb/or1k-tdep.c b/gdb/or1k-tdep.c
> index 32fef65dc376..1351dbe2d0c5 100644
> --- a/gdb/or1k-tdep.c
> +++ b/gdb/or1k-tdep.c
> @@ -468,9 +468,8 @@ or1k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>         if (0 != prologue_end)
>   	{
>   	  struct symtab_and_line prologue_sal = find_sal_for_pc (start_pc, 0);
> -	  struct compunit_symtab *compunit
> -	    = prologue_sal.symtab->compunit ();
> -	  const char *debug_format = compunit->debugformat ();
> +	  const char *debug_format
> +	    = prologue_sal.symtab->compunit ().debugformat ();
>   
>   	  if ((NULL != debug_format)
>   	      && (strlen ("dwarf") <= strlen (debug_format))
> diff --git a/gdb/p-exp.y b/gdb/p-exp.y
> index c06ada3b7ff8..1a43837ae333 100644
> --- a/gdb/p-exp.y
> +++ b/gdb/p-exp.y
> @@ -615,7 +615,7 @@ block	:	BLOCKNAME
>   			      struct symtab *tem =
>   				  lookup_symtab (current_program_space, copy.c_str ());
>   			      if (tem)
> -				$$ = (tem->compunit ()->blockvector ()
> +				$$ = (tem->compunit ().blockvector ()
>   				      ->static_block ());
>   			      else
>   				error (_("No file or function \"%s\"."),
> diff --git a/gdb/parse.c b/gdb/parse.c
> index fd190d6e15e7..ecb3e30c5cb5 100644
> --- a/gdb/parse.c
> +++ b/gdb/parse.c
> @@ -386,7 +386,7 @@ parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
>   
>   	  if (cursal.symtab)
>   	    expression_context_block
> -	      = cursal.symtab->compunit ()->blockvector ()->static_block ();
> +	      = cursal.symtab->compunit ().blockvector ()->static_block ();
>   
>   	  if (expression_context_block)
>   	    expression_context_pc = expression_context_block->entry_pc ();
> diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c
> index 1db13af4c12c..faaedc5e9e8f 100644
> --- a/gdb/python/py-linetable.c
> +++ b/gdb/python/py-linetable.c
> @@ -179,7 +179,7 @@ ltpy_get_pcs_for_line (PyObject *self, PyObject *args)
>         return gdbpy_handle_gdb_exception (nullptr, except);
>       }
>   
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>     return build_line_table_tuple_from_entries (objfile, entries);
>   }
>   
> @@ -413,7 +413,7 @@ ltpy_iternext (PyObject *self)
>         item = &(symtab->linetable ()->item[iter_obj->current_index]);
>       }
>   
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>     obj = build_linetable_entry (item->line, item->pc (objfile));
>     iter_obj->current_index++;
>   
> diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
> index 9a343167624c..ce5feaeb3909 100644
> --- a/gdb/python/py-symtab.c
> +++ b/gdb/python/py-symtab.c
> @@ -130,7 +130,7 @@ stpy_get_objfile (PyObject *self, void *closure)
>   
>     STPY_REQUIRE_VALID (self, symtab);
>   
> -  return objfile_to_objfile_object (symtab->compunit ()->objfile ()).release ();
> +  return objfile_to_objfile_object (symtab->compunit ().objfile ()).release ();
>   }
>   
>   /* Getter function for symtab.producer.  */
> @@ -139,13 +139,13 @@ static PyObject *
>   stpy_get_producer (PyObject *self, void *closure)
>   {
>     struct symtab *symtab = NULL;
> -  struct compunit_symtab *cust;
>   
>     STPY_REQUIRE_VALID (self, symtab);
> -  cust = symtab->compunit ();
> -  if (cust->producer () != nullptr)
> +  compunit_symtab &cust = symtab->compunit ();
> +
> +  if (cust.producer () != nullptr)
>       {
> -      const char *producer = cust->producer ();
> +      const char *producer = cust.producer ();
>   
>         return host_string_to_python_string (producer).release ();
>       }
> @@ -191,11 +191,11 @@ stpy_global_block (PyObject *self, PyObject *args)
>   
>     STPY_REQUIRE_VALID (self, symtab);
>   
> -  blockvector = symtab->compunit ()->blockvector ();
> +  blockvector = symtab->compunit ().blockvector ();
>     const struct block *block = blockvector->global_block ();
>   
>     return block_to_block_object (block,
> -				symtab->compunit ()->objfile ()).release ();
> +				symtab->compunit ().objfile ()).release ();
>   }
>   
>   /* Return the STATIC_BLOCK of the underlying symtab.  */
> @@ -208,11 +208,11 @@ stpy_static_block (PyObject *self, PyObject *args)
>   
>     STPY_REQUIRE_VALID (self, symtab);
>   
> -  blockvector = symtab->compunit ()->blockvector ();
> +  blockvector = symtab->compunit ().blockvector ();
>     const struct block *block = blockvector->static_block ();
>   
>     return block_to_block_object (block,
> -				symtab->compunit ()->objfile ()).release ();
> +				symtab->compunit ().objfile ()).release ();
>   }
>   
>   /* Implementation of gdb.Symtab.linetable (self) -> gdb.LineTable.
> @@ -361,7 +361,7 @@ stpy_dealloc (PyObject *obj)
>     symtab_object *symtab_obj = (symtab_object *) obj;
>   
>     if (symtab_obj->symtab != nullptr)
> -    stpy_registry.remove (symtab_obj->symtab->compunit ()->objfile(),
> +    stpy_registry.remove (symtab_obj->symtab->compunit ().objfile (),
>   			  symtab_obj);
>   
>     Py_TYPE (obj)->tp_free (obj);
> @@ -438,7 +438,7 @@ salpy_dealloc (PyObject *self)
>     sal_object *self_sal = (sal_object *) self;
>   
>     if (self_sal->sal != nullptr && self_sal->sal->symtab != nullptr)
> -    salpy_registry.remove (self_sal->sal->symtab->compunit ()->objfile (),
> +    salpy_registry.remove (self_sal->sal->symtab->compunit ().objfile (),
>   			   self_sal);
>   
>     xfree (self_sal->sal);
> @@ -463,7 +463,7 @@ set_sal (sal_object *sal_obj, struct symtab_and_line sal)
>        objfile cleanup observer linked list.  */
>     symtab *symtab = sal_obj->sal->symtab;
>     if (symtab != nullptr)
> -    salpy_registry.add (symtab->compunit ()->objfile (), sal_obj);
> +    salpy_registry.add (symtab->compunit ().objfile (), sal_obj);
>   }
>   
>   /* Given a symtab, and a symtab_object that has previously been
> @@ -476,7 +476,7 @@ set_symtab (symtab_object *obj, struct symtab *symtab)
>   {
>     obj->symtab = symtab;
>     if (symtab != nullptr)
> -    stpy_registry.add (symtab->compunit ()->objfile (), obj);
> +    stpy_registry.add (symtab->compunit ().objfile (), obj);
>   }
>   
>   /* Create a new symbol table (gdb.Symtab) object that encapsulates the
> @@ -491,7 +491,7 @@ symtab_to_symtab_object (struct symtab *symtab)
>     if (symtab != nullptr)
>       {
>         gdbpy_ref<> result
> -	= stpy_registry.lookup (symtab->compunit ()->objfile (), symtab);
> +	= stpy_registry.lookup (symtab->compunit ().objfile (), symtab);
>         if (result != nullptr)
>   	return result;
>       }
> diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
> index 4bd755b24f24..dae243ee2dbd 100644
> --- a/gdb/record-btrace.c
> +++ b/gdb/record-btrace.c
> @@ -729,7 +729,7 @@ btrace_find_line_range (CORE_ADDR pc)
>     if (nlines <= 0)
>       return btrace_mk_line_range (symtab, 0, 0);
>   
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>     unrelocated_addr unrel_pc
>       = unrelocated_addr (pc - objfile->text_section_offset ());
>   
> diff --git a/gdb/source-cache.c b/gdb/source-cache.c
> index 6e8acab35044..804a6a08a8c3 100644
> --- a/gdb/source-cache.c
> +++ b/gdb/source-cache.c
> @@ -110,9 +110,9 @@ source_cache::get_plain_source_lines (struct symtab *s,
>       perror_with_name (symtab_to_filename_for_display (s));
>   
>     time_t mtime = 0;
> -  if (s->compunit ()->objfile () != NULL
> -      && s->compunit ()->objfile ()->obfd != NULL)
> -    mtime = s->compunit ()->objfile ()->mtime;
> +  if (s->compunit ().objfile () != NULL
> +      && s->compunit ().objfile ()->obfd != NULL)
> +    mtime = s->compunit ().objfile ()->mtime;
>     else if (current_program_space->exec_bfd ())
>       mtime = current_program_space->ebfd_mtime;
>   
> diff --git a/gdb/source.c b/gdb/source.c
> index 6028c29deae6..5c7c344a1aab 100644
> --- a/gdb/source.c
> +++ b/gdb/source.c
> @@ -299,7 +299,7 @@ clear_current_source_symtab_and_line (objfile *objfile)
>       return;
>   
>     if (loc->symtab () != nullptr
> -      && loc->symtab ()->compunit ()->objfile () == objfile)
> +      && loc->symtab ()->compunit ().objfile () == objfile)
>       clear_current_source_symtab_and_line (objfile->pspace ());
>   }
>   
> @@ -671,7 +671,6 @@ info_source_command (const char *ignore, int from_tty)
>     current_source_location *loc
>       = get_source_location (current_program_space);
>     struct symtab *s = loc->symtab ();
> -  struct compunit_symtab *cust;
>   
>     if (!s)
>       {
> @@ -679,10 +678,11 @@ info_source_command (const char *ignore, int from_tty)
>         return;
>       }
>   
> -  cust = s->compunit ();
> +  compunit_symtab &cust = s->compunit ();
> +
>     gdb_printf (_("Current source file is %s\n"), s->filename ());
> -  if (s->compunit ()->dirname () != NULL)
> -    gdb_printf (_("Compilation directory is %s\n"), s->compunit ()->dirname ());
> +  if (s->compunit ().dirname () != NULL)
> +    gdb_printf (_("Compilation directory is %s\n"), s->compunit ().dirname ());
>     if (s->fullname () != nullptr)
>       gdb_printf (_("Located in %s\n"), s->fullname ());
>     if (std::optional<int> last_lineno = last_symtab_line (s);
> @@ -693,12 +693,12 @@ info_source_command (const char *ignore, int from_tty)
>     gdb_printf (_("Source language is %s.\n"),
>   	      language_str (s->language ()));
>     gdb_printf (_("Producer is %s.\n"),
> -	      (cust->producer ()) != nullptr
> -	      ? cust->producer () : _("unknown"));
> +	      (cust.producer ()) != nullptr
> +	      ? cust.producer () : _("unknown"));
>     gdb_printf (_("Compiled with %s debugging format.\n"),
> -	      cust->debugformat ());
> +	      cust.debugformat ());
>     gdb_printf (_("%s preprocessor macro info.\n"),
> -	      (cust->macro_table () != nullptr
> +	      (cust.macro_table () != nullptr
>   	       ? "Includes" : "Does not include"));
>   }
>   \f
> @@ -1117,44 +1117,41 @@ open_source_file (struct symtab *s)
>   
>     gdb::unique_xmalloc_ptr<char> fullname = s->release_fullname ();
>     scoped_fd fd = find_and_open_source (s->filename (),
> -				       s->compunit ()->dirname (),
> +				       s->compunit ().dirname (),
>   				       &fullname);
>   
>     if (fd.get () < 0)
>       {
> -      if (s->compunit () != nullptr)
> +      const objfile *ofp = s->compunit ().objfile ();
> +
> +      std::string srcpath;
> +      if (IS_ABSOLUTE_PATH (s->filename ()))
> +	srcpath = s->filename ();
> +      else if (s->compunit ().dirname () != nullptr)
>   	{
> -	  const objfile *ofp = s->compunit ()->objfile ();
> +	  srcpath = s->compunit ().dirname ();
> +	  srcpath += SLASH_STRING;
> +	  srcpath += s->filename ();
> +	}
>   
> -	  std::string srcpath;
> -	  if (IS_ABSOLUTE_PATH (s->filename ()))
> -	    srcpath = s->filename ();
> -	  else if (s->compunit ()->dirname () != nullptr)
> -	    {
> -	      srcpath = s->compunit ()->dirname ();
> -	      srcpath += SLASH_STRING;
> -	      srcpath += s->filename ();
> -	    }
> +      const struct bfd_build_id *build_id
> +	= build_id_bfd_get (ofp->obfd.get ());
>   
> -	  const struct bfd_build_id *build_id
> -	    = build_id_bfd_get (ofp->obfd.get ());
> +      /* Query debuginfod for the source file.  */
> +      if (build_id != nullptr && !srcpath.empty ())
> +	{
> +	  scoped_fd query_fd
> +	    = debuginfod_source_query (build_id->data,
> +				       build_id->size,
> +				       srcpath.c_str (),
> +				       &fullname);
>   
> -	  /* Query debuginfod for the source file.  */
> -	  if (build_id != nullptr && !srcpath.empty ())
> +	  /* Don't return a negative errno from debuginfod_source_query.
> +	     It handles the reporting of its own errors.  */
> +	  if (query_fd.get () >= 0)
>   	    {
> -	      scoped_fd query_fd
> -		= debuginfod_source_query (build_id->data,
> -					   build_id->size,
> -					   srcpath.c_str (),
> -					   &fullname);
> -
> -	      /* Don't return a negative errno from debuginfod_source_query.
> -		 It handles the reporting of its own errors.  */
> -	      if (query_fd.get () >= 0)
> -		{
> -		  s->set_fullname (std::move (fullname));
> -		  return query_fd;
> -		}
> +	      s->set_fullname (std::move (fullname));
> +	      return query_fd;
>   	    }
>   	}
>       }
> @@ -1217,11 +1214,11 @@ symtab_to_fullname (struct symtab *s)
>   	  /* rewrite_source_path would be applied by find_and_open_source, we
>   	     should report the pathname where GDB tried to find the file.  */
>   
> -	  if (s->compunit ()->dirname () == nullptr
> +	  if (s->compunit ().dirname () == nullptr
>   	      || IS_ABSOLUTE_PATH (s->filename ()))
>   	    fullname = make_unique_xstrdup (s->filename ());
>   	  else
> -	    fullname.reset (concat (s->compunit ()->dirname (), SLASH_STRING,
> +	    fullname.reset (concat (s->compunit ().dirname (), SLASH_STRING,
>   				    s->filename (), (char *) NULL));
>   
>   	  s->set_fullname (rewrite_source_path (fullname.get ()));
> @@ -1527,7 +1524,7 @@ info_line_command (const char *arg, int from_tty)
>         else if (sal.line > 0
>   	       && find_pc_range_for_sal (sal, &start_pc, &end_pc))
>   	{
> -	  gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
> +	  gdbarch *gdbarch = sal.symtab->compunit ().objfile ()->arch ();
>   
>   	  if (start_pc == end_pc)
>   	    {
> diff --git a/gdb/symfile.c b/gdb/symfile.c
> index 5f046a03567e..296795bda808 100644
> --- a/gdb/symfile.c
> +++ b/gdb/symfile.c
> @@ -2811,7 +2811,7 @@ allocate_symtab (struct compunit_symtab *cust, const char *filename,
>     struct objfile *objfile = cust->objfile ();
>     struct symtab *symtab
>       = obstack_new<struct symtab> (&objfile->objfile_obstack,
> -				  cust,
> +				  *cust,
>   				  objfile->intern (filename),
>   				  objfile->intern (filename_for_id),
>   				  deduce_language_from_filename (filename));
> diff --git a/gdb/symmisc.c b/gdb/symmisc.c
> index 89374bd8a2ff..6d90765fe50b 100644
> --- a/gdb/symmisc.c
> +++ b/gdb/symmisc.c
> @@ -135,7 +135,7 @@ dump_objfile (struct objfile *objfile)
>   		      styled_string (file_name_style.style (),
>   				     symtab_to_filename_for_display (symtab)),
>   		      host_address_to_string (symtab));
> -	  if (symtab->compunit ()->objfile () != objfile)
> +	  if (symtab->compunit ().objfile () != objfile)
>   	    gdb_printf (_(", NOT ON CHAIN!"));
>   	  gdb_printf ("\n");
>   	}
> @@ -244,7 +244,7 @@ dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
>   static void
>   dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
>   {
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>     struct gdbarch *gdbarch = objfile->arch ();
>     const struct linetable *l;
>     int depth;
> @@ -254,10 +254,10 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
>   			     symtab_to_filename_for_display (symtab)),
>   	      host_address_to_string (symtab));
>   
> -  if (symtab->compunit ()->dirname () != NULL)
> +  if (symtab->compunit ().dirname () != NULL)
>       gdb_printf (outfile, _("Compilation directory is %ps\n"),
>   		styled_string (file_name_style.style (),
> -			       symtab->compunit ()->dirname ()));
> +			       symtab->compunit ().dirname ()));
>     gdb_printf (outfile, _("Read from object file %ps (%s)\n"),
>   	      styled_string (file_name_style.style (),
>   			     objfile_name (objfile)),
> @@ -288,7 +288,7 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
>     if (is_main_symtab_of_compunit_symtab (symtab))
>       {
>         gdb_printf (outfile, _("\nBlockvector:\n\n"));
> -      const blockvector *bv = symtab->compunit ()->blockvector ();
> +      const blockvector *bv = symtab->compunit ().blockvector ();
>         for (int i = 0; i < bv->num_blocks (); i++)
>   	{
>   	  const block *b = bv->block (i);
> @@ -342,9 +342,9 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
>       }
>     else
>       {
> -      compunit_symtab *compunit = symtab->compunit ();
> +      compunit_symtab &compunit = symtab->compunit ();
>         const char *compunit_filename
> -	= symtab_to_filename_for_display (compunit->primary_filetab ());
> +	= symtab_to_filename_for_display (compunit.primary_filetab ());
>   
>         gdb_printf (outfile,
>   		  _("\nBlockvector same as owning compunit: %ps\n\n"),
> @@ -356,14 +356,14 @@ dump_symtab_1 (struct symtab *symtab, struct ui_file *outfile)
>        compunit_symtabs included by this one. */
>     if (is_main_symtab_of_compunit_symtab (symtab))
>       {
> -      struct compunit_symtab *cust = symtab->compunit ();
> +      compunit_symtab &cust = symtab->compunit ();
>   
> -      if (cust->user != nullptr)
> +      if (cust.user != nullptr)
>   	gdb_printf (outfile, _("Compunit user: %s\n"),
> -		    host_address_to_string (cust->user->primary_filetab ()));
> +		    host_address_to_string (cust.user->primary_filetab ()));
>   
>   
> -      for (compunit_symtab *include : cust->includes)
> +      for (compunit_symtab *include : cust.includes)
>   	gdb_printf (outfile, _("Compunit include: %s\n"),
>   		    host_address_to_string (include->primary_filetab ()));
>       }
> @@ -978,14 +978,14 @@ maintenance_print_one_line_table (struct symtab *symtab, void *data)
>     const struct linetable *linetable;
>     struct objfile *objfile;
>   
> -  objfile = symtab->compunit ()->objfile ();
> +  objfile = symtab->compunit ().objfile ();
>     gdb_printf (_("objfile: %ps ((struct objfile *) %s)\n"),
>   	      styled_string (file_name_style.style (),
>   			     objfile_name (objfile)),
>   	      host_address_to_string (objfile));
>     gdb_printf (_("compunit_symtab: %s ((struct compunit_symtab *) %s)\n"),
> -	      symtab->compunit ()->name,
> -	      host_address_to_string (symtab->compunit ()));
> +	      symtab->compunit ().name,
> +	      host_address_to_string (&symtab->compunit ()));
>     gdb_printf (_("symtab: %ps ((struct symtab *) %s)\n"),
>   	      styled_string (file_name_style.style (),
>   			     symtab_to_fullname (symtab)),
> diff --git a/gdb/symtab.c b/gdb/symtab.c
> index b38362e6fcce..4ca6f92ff9a0 100644
> --- a/gdb/symtab.c
> +++ b/gdb/symtab.c
> @@ -3389,7 +3389,7 @@ find_pc_for_line (struct symtab *symtab, int line, CORE_ADDR *pc)
>     if (symtab != NULL)
>       {
>         l = symtab->linetable ();
> -      *pc = l->item[ind].pc (symtab->compunit ()->objfile ());
> +      *pc = l->item[ind].pc (symtab->compunit ().objfile ());
>         return true;
>       }
>     else
> @@ -3511,10 +3511,10 @@ find_function_start_sal_1 (CORE_ADDR func_addr, obj_section *section,
>     symtab_and_line sal = find_sal_for_pc_sect (func_addr, section, 0);
>   
>     if (funfirstline && sal.symtab != NULL
> -      && (sal.symtab->compunit ()->locations_valid ()
> +      && (sal.symtab->compunit ().locations_valid ()
>   	  || sal.symtab->language () == language_asm))
>       {
> -      struct gdbarch *gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
> +      struct gdbarch *gdbarch = sal.symtab->compunit ().objfile ()->arch ();
>   
>         sal.pc = func_addr;
>         if (gdbarch_skip_entrypoint_p (gdbarch))
> @@ -3592,7 +3592,7 @@ skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
>     if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
>       return func_addr;
>   
> -  struct objfile *objfile = symtab->compunit ()->objfile ();
> +  struct objfile *objfile = symtab->compunit ().objfile ();
>   
>     /* Linetable entries are ordered by PC values, see the commentary in
>        symtab.h where `struct linetable' is defined.  Thus, the first
> @@ -3634,7 +3634,7 @@ skip_prologue_using_linetable (CORE_ADDR func_addr)
>       {
>         const linetable *linetable = prologue_sal.symtab->linetable ();
>   
> -      struct objfile *objfile = prologue_sal.symtab->compunit ()->objfile ();
> +      struct objfile *objfile = prologue_sal.symtab->compunit ().objfile ();
>   
>         unrelocated_addr unrel_start
>   	= unrelocated_addr (start_pc - objfile->text_section_offset ());
> @@ -3727,7 +3727,7 @@ skip_prologue_sal (struct symtab_and_line *sal)
>        have proven the CU (Compilation Unit) supports it.  sal->SYMTAB does not
>        have to be set by the caller so we use SYM instead.  */
>     if (sym != NULL
> -      && sym->symtab ()->compunit ()->locations_valid ())
> +      && sym->symtab ()->compunit ().locations_valid ())
>       force_skip = 0;
>   
>     symtab_and_line start_sal;
> @@ -3885,7 +3885,7 @@ skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
>         if (prologue_sal.symtab->language () != language_asm)
>   	{
>   	  struct objfile *objfile
> -	    = prologue_sal.symtab->compunit ()->objfile ();
> +	    = prologue_sal.symtab->compunit ().objfile ();
>   	  const linetable *linetable = prologue_sal.symtab->linetable ();
>   	  gdb_assert (linetable->nitems > 0);
>   	  int idx = 0;
> @@ -3986,7 +3986,7 @@ find_epilogue_using_linetable (CORE_ADDR func_addr)
>     const struct symtab_and_line sal = find_sal_for_pc (end_pc - 1, 0);
>     if (sal.symtab != nullptr && sal.symtab->language () != language_asm)
>       {
> -      struct objfile *objfile = sal.symtab->compunit ()->objfile ();
> +      struct objfile *objfile = sal.symtab->compunit ().objfile ();
>         unrelocated_addr unrel_start
>   	= unrelocated_addr (start_pc - objfile->text_section_offset ());
>         unrelocated_addr unrel_end
> @@ -6155,7 +6155,7 @@ collect_file_symbol_completion_matches (completion_tracker &tracker,
>        for symbols which match.  */
>     for_each_symtab (current_program_space, srcfile, [&] (symtab *s)
>       {
> -      add_symtab_completions (s->compunit (),
> +      add_symtab_completions (&s->compunit (),
>   			      tracker, mode, lookup_name,
>   			      sym_text, word, TYPE_CODE_UNDEF);
>       });
> @@ -6560,7 +6560,7 @@ struct objfile *
>   symbol::objfile () const
>   {
>     gdb_assert (is_objfile_owned ());
> -  return owner.symtab->compunit ()->objfile ();
> +  return owner.symtab->compunit ().objfile ();
>   }
>   
>   /* See symtab.h.  */
> @@ -6570,7 +6570,7 @@ symbol::arch () const
>   {
>     if (!is_objfile_owned ())
>       return owner.arch;
> -  return owner.symtab->compunit ()->objfile ()->arch ();
> +  return owner.symtab->compunit ().objfile ()->arch ();
>   }
>   
>   /* See symtab.h.  */
> diff --git a/gdb/symtab.h b/gdb/symtab.h
> index b29b72aba00d..fc2c1ed23aab 100644
> --- a/gdb/symtab.h
> +++ b/gdb/symtab.h
> @@ -1671,21 +1671,20 @@ struct linetable
>   
>   struct symtab
>   {
> -  symtab (struct compunit_symtab *cust, const char *filename,
> +  symtab (struct compunit_symtab &cust, const char *filename,
>   	  const char *filename_for_id, enum language language)
>       : m_filename (filename),
>         m_filename_for_id (filename_for_id),
> -      m_compunit (cust),
> +      m_compunit (&cust),
>         m_language (language)
>     {
>       gdb_assert (m_filename != nullptr);
>       gdb_assert (m_filename_for_id != nullptr);
> -    gdb_assert (m_compunit != nullptr);
>     }
>   
> -  struct compunit_symtab *compunit () const
> +  struct compunit_symtab &compunit () const
>     {
> -    return m_compunit;
> +    return *m_compunit;
>     }
>   
>     const struct linetable *linetable () const
> @@ -2028,7 +2027,7 @@ struct compunit_symtab : intrusive_list_node<compunit_symtab>
>   static inline bool
>   is_main_symtab_of_compunit_symtab (struct symtab *symtab)
>   {
> -  return symtab == symtab->compunit ()->primary_filetab ();
> +  return symtab == symtab->compunit ().primary_filetab ();
>   }
>   
>   /* Return true if epilogue unwind info of CUST is valid.  */
> diff --git a/gdb/tui/tui-source.c b/gdb/tui/tui-source.c
> index 783d4b2a1c24..466e9972106d 100644
> --- a/gdb/tui/tui-source.c
> +++ b/gdb/tui/tui-source.c
> @@ -79,7 +79,7 @@ tui_source_window::set_contents (struct gdbarch *arch,
>     m_fullname = make_unique_xstrdup (symtab_to_fullname (s));
>   
>     cur_line = 0;
> -  m_gdbarch = s->compunit ()->objfile ()->arch ();
> +  m_gdbarch = s->compunit ().objfile ()->arch ();
>     m_start_line_or_addr.loa = LOA_LINE;
>     cur_line_no = m_start_line_or_addr.u.line_no = line_no;
>   
> diff --git a/gdb/tui/tui-winsource.c b/gdb/tui/tui-winsource.c
> index 8609a7cd4fe2..e3f64892e271 100644
> --- a/gdb/tui/tui-winsource.c
> +++ b/gdb/tui/tui-winsource.c
> @@ -209,7 +209,7 @@ tui_update_source_windows_with_line (struct symtab_and_line sal)
>     if (sal.symtab != nullptr)
>       {
>         find_pc_for_line (sal.symtab, sal.line, &sal.pc);
> -      gdbarch = sal.symtab->compunit ()->objfile ()->arch ();
> +      gdbarch = sal.symtab->compunit ().objfile ()->arch ();
>       }
>   
>     for (struct tui_source_window_base *win_info : tui_source_windows ())
> diff --git a/gdb/z80-tdep.c b/gdb/z80-tdep.c
> index 9da18855d710..7359f9bcfb6d 100644
> --- a/gdb/z80-tdep.c
> +++ b/gdb/z80-tdep.c
> @@ -498,8 +498,8 @@ z80_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>   
>         if (prologue_sal.symtab != nullptr)
>   	{
> -	  struct compunit_symtab *compunit = prologue_sal.symtab->compunit ();
> -	  const char *debug_format = compunit->debugformat ();
> +	  const char *debug_format
> +	    = prologue_sal.symtab->compunit ().debugformat ();
>   
>   	  if (debug_format != nullptr
>   	      && !strncasecmp ("dwarf", debug_format, strlen ("dwarf")))
> 
> base-commit: e4d214db01492b035874eaac2c3a022e6c3d5bc8


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: make symtab::compunit return a reference
  2026-06-19 12:16 ` Tom de Vries
@ 2026-06-19 14:22   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2026-06-19 14:22 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches



On 2026-06-19 08:16, Tom de Vries wrote:
> On 6/18/26 8:39 PM, simon.marchi@polymtl.ca wrote:
>> From: Simon Marchi <simon.marchi@polymtl.ca>
>>
>> The compunit symtab backlink of a symtab is never null (the constructor
>> asserts it), so make symtab::compunit return a reference instead of a
>> pointer, and have the symtab constructor take the compunit_symtab as a
>> reference too.  Update all callers accordingly.
>>
>> This came up earlier in review, where a caller would check the result of
>> `symtab->compunit ()` for nullptr, and I pointed out that it was
>> unnecessary.  Returning a reference makes this clear.
>>
> 
> Hi Simon,
> 
> Looks reasonable to me.
> 
> Reviewed-By: Tom de Vries <tdevries@suse.de>

Thanks, pushed.

Simon

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-19 14:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-18 18:39 [PATCH] gdb: make symtab::compunit return a reference simon.marchi
2026-06-19 12:16 ` Tom de Vries
2026-06-19 14:22   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox