Remove allocate_symbol et al

Message ID 20200426195126.16298-1-tom@tromey.com
State New
Headers show
Series
  • Remove allocate_symbol et al
Related show

Commit Message

Tom Tromey April 26, 2020, 7:51 p.m.
This removes allocate_symbol, allocate_template_symbol, and
initialize_objfile_symbol in favor of changing the default values for
symbol members, and updating the one per-arch caller.

gdb/ChangeLog
2020-04-26  Tom Tromey  <tom@tromey.com>

	* language.c (language_alloc_type_symbol): Set
	SYMBOL_SECTION.
	* symtab.c (initialize_objfile_symbol): Remove.
	(allocate_symbol): Remove.
	(allocate_template_symbol): Remove.
	* dwarf2/read.c (fixup_go_packaging): Use "new".
	(new_symbol): Use "new".
	(read_variable): Don't call initialize_objfile_symbol.  Use
	"new".
	(read_func_scope): Use "new".
	* xcoffread.c (process_xcoff_symbol): Don't call
	initialize_objfile_symbol.
	(SYMBOL_DUP): Remove.
	* coffread.c (process_coff_symbol, coff_read_enum_type): Use
	"new".
	* symtab.h (allocate_symbol, initialize_objfile_symbol)
	(allocate_template_symbol): Don't declare.
	(struct symbol): Add copy constructor.  Change defaults.
	* jit.c (finalize_symtab): Use "new".
	* ctfread.c (ctf_add_enum_member_cb, new_symbol, ctf_add_var_cb):
	Use "new".
	* stabsread.c (patch_block_stabs, define_symbol, read_enum_type)
	(common_block_end): Use "new".
	* mdebugread.c (parse_symbol): Use "new".
	(new_symbol): Likewise.
---
 gdb/ChangeLog     | 28 ++++++++++++++++++++++++++++
 gdb/coffread.c    |  4 ++--
 gdb/ctfread.c     |  6 +++---
 gdb/dwarf2/read.c |  9 ++++-----
 gdb/jit.c         |  2 +-
 gdb/language.c    |  1 +
 gdb/mdebugread.c  |  4 ++--
 gdb/stabsread.c   | 12 ++++++------
 gdb/symtab.c      | 36 ------------------------------------
 gdb/symtab.h      | 12 ++++--------
 gdb/xcoffread.c   |  9 +--------
 11 files changed, 52 insertions(+), 71 deletions(-)

-- 
2.17.2

Comments

Tankut Baris Aktemur via Gdb-patches April 27, 2020, 9:48 p.m. | #1
On Sun, Apr 26, 2020 at 2:51 PM Tom Tromey <tom@tromey.com> wrote:
>

> This removes allocate_symbol, allocate_template_symbol, and

> initialize_objfile_symbol in favor of changing the default values for

> symbol members, and updating the one per-arch caller.


Thanks for doing this! See comment below.

>

> gdb/ChangeLog

> 2020-04-26  Tom Tromey  <tom@tromey.com>

>

>         * language.c (language_alloc_type_symbol): Set

>         SYMBOL_SECTION.

>         * symtab.c (initialize_objfile_symbol): Remove.

>         (allocate_symbol): Remove.

>         (allocate_template_symbol): Remove.

>         * dwarf2/read.c (fixup_go_packaging): Use "new".

>         (new_symbol): Use "new".

>         (read_variable): Don't call initialize_objfile_symbol.  Use

>         "new".

>         (read_func_scope): Use "new".

>         * xcoffread.c (process_xcoff_symbol): Don't call

>         initialize_objfile_symbol.

>         (SYMBOL_DUP): Remove.

>         * coffread.c (process_coff_symbol, coff_read_enum_type): Use

>         "new".

>         * symtab.h (allocate_symbol, initialize_objfile_symbol)

>         (allocate_template_symbol): Don't declare.

>         (struct symbol): Add copy constructor.  Change defaults.

>         * jit.c (finalize_symtab): Use "new".

>         * ctfread.c (ctf_add_enum_member_cb, new_symbol, ctf_add_var_cb):

>         Use "new".

>         * stabsread.c (patch_block_stabs, define_symbol, read_enum_type)

>         (common_block_end): Use "new".

>         * mdebugread.c (parse_symbol): Use "new".

>         (new_symbol): Likewise.

> ---

>  gdb/ChangeLog     | 28 ++++++++++++++++++++++++++++

>  gdb/coffread.c    |  4 ++--

>  gdb/ctfread.c     |  6 +++---

>  gdb/dwarf2/read.c |  9 ++++-----

>  gdb/jit.c         |  2 +-

>  gdb/language.c    |  1 +

>  gdb/mdebugread.c  |  4 ++--

>  gdb/stabsread.c   | 12 ++++++------

>  gdb/symtab.c      | 36 ------------------------------------

>  gdb/symtab.h      | 12 ++++--------

>  gdb/xcoffread.c   |  9 +--------

>  11 files changed, 52 insertions(+), 71 deletions(-)

>

> diff --git a/gdb/coffread.c b/gdb/coffread.c

> index 7fbdcc4f68f..ca7db25101e 100644

> --- a/gdb/coffread.c

> +++ b/gdb/coffread.c

> @@ -1556,7 +1556,7 @@ process_coff_symbol (struct coff_symbol *cs,

>                      union internal_auxent *aux,

>                      struct objfile *objfile)

>  {

> -  struct symbol *sym = allocate_symbol (objfile);

> +  struct symbol *sym = new (&objfile->objfile_obstack) symbol;

>    char *name;

>

>    name = cs->c_name;

> @@ -2095,7 +2095,7 @@ coff_read_enum_type (int index, int length, int lastsym,

>        switch (ms->c_sclass)

>         {

>         case C_MOE:

> -         sym = allocate_symbol (objfile);

> +         sym = new (&objfile->objfile_obstack) symbol;

>

>           name = obstack_strdup (&objfile->objfile_obstack, name);

>           sym->set_linkage_name (name);

> diff --git a/gdb/ctfread.c b/gdb/ctfread.c

> index 8cc7271c075..d7e9625fb13 100644

> --- a/gdb/ctfread.c

> +++ b/gdb/ctfread.c

> @@ -407,7 +407,7 @@ ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)

>

>    if (name != NULL)

>      {

> -      struct symbol *sym = allocate_symbol (ccp->of);

> +      struct symbol *sym = new (&ccp->of->objfile_obstack) symbol;

>        OBJSTAT (ccp->of, n_syms++);

>

>        sym->set_language (language_c, &ccp->of->objfile_obstack);

> @@ -436,7 +436,7 @@ new_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)

>    gdb::unique_xmalloc_ptr<char> name (ctf_type_aname_raw (fp, tid));

>    if (name != NULL)

>      {

> -      sym = allocate_symbol (objfile);

> +      sym = new (&objfile->objfile_obstack) symbol;

>        OBJSTAT (objfile, n_syms++);

>

>        sym->set_language (language_c, &objfile->objfile_obstack);

> @@ -1071,7 +1071,7 @@ ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)

>           complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);

>           type = objfile_type (ccp->of)->builtin_error;

>         }

> -       sym = allocate_symbol (ccp->of);

> +       sym = new (&ccp->of->objfile_obstack) symbol;

>         OBJSTAT (ccp->of, n_syms++);

>         SYMBOL_TYPE (sym) = type;

>         SYMBOL_DOMAIN (sym) = VAR_DOMAIN;

> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c

> index 976261372bc..53361dce049 100644

> --- a/gdb/dwarf2/read.c

> +++ b/gdb/dwarf2/read.c

> @@ -9131,7 +9131,7 @@ fixup_go_packaging (struct dwarf2_cu *cu)

>                                      saved_package_name);

>        struct symbol *sym;

>

> -      sym = allocate_symbol (objfile);

> +      sym = new (&objfile->objfile_obstack) symbol;

>        sym->set_language (language_go, &objfile->objfile_obstack);

>        sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);

>        /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,

> @@ -12940,7 +12940,7 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)

>        if (child_die->tag == DW_TAG_template_type_param

>           || child_die->tag == DW_TAG_template_value_param)

>         {

> -         templ_func = allocate_template_symbol (objfile);

> +         templ_func = new (&objfile->objfile_obstack) template_symbol;

>           templ_func->subclass = SYMBOL_TEMPLATE;

>           break;

>         }

> @@ -13494,8 +13494,7 @@ read_variable (struct die_info *die, struct dwarf2_cu *cu)

>         {

>           struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;

>

> -         storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();

> -         initialize_objfile_symbol (storage);

> +         storage = new (&objfile->objfile_obstack) rust_vtable_symbol;

>           storage->concrete_type = containing_type;

>           storage->subclass = SYMBOL_RUST_VTABLE;

>         }

> @@ -20559,7 +20558,7 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,

>        if (space)

>         sym = space;

>        else

> -       sym = allocate_symbol (objfile);

> +       sym = new (&objfile->objfile_obstack) symbol;

>        OBJSTAT (objfile, n_syms++);

>

>        /* Cache this symbol's name and the name's demangled form (if any).  */

> diff --git a/gdb/jit.c b/gdb/jit.c

> index 07e9ce7ae24..1b5ef46469e 100644

> --- a/gdb/jit.c

> +++ b/gdb/jit.c

> @@ -655,7 +655,7 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)

>    for (gdb_block &gdb_block_iter : stab->blocks)

>      {

>        struct block *new_block = allocate_block (&objfile->objfile_obstack);

> -      struct symbol *block_name = allocate_symbol (objfile);

> +      struct symbol *block_name = new (&objfile->objfile_obstack) symbol;

>        struct type *block_type = arch_type (objfile->arch (),

>                                            TYPE_CODE_VOID,

>                                            TARGET_CHAR_BIT,

> diff --git a/gdb/language.c b/gdb/language.c

> index 769b3299793..fd711e13336 100644

> --- a/gdb/language.c

> +++ b/gdb/language.c

> @@ -1041,6 +1041,7 @@ language_alloc_type_symbol (enum language lang, struct type *type)

>    symbol->set_language (lang, nullptr);

>    symbol->owner.arch = gdbarch;

>    SYMBOL_OBJFILE_OWNED (symbol) = 0;

> +  SYMBOL_SECTION (symbol) = 0;


Does this not have to set objfile_owned to 0?

Similarly, ada-exp.y has a call:
  struct symbol *sym = new (&temp_parse_space) symbol ();
Does this not need updating?

>    SYMBOL_TYPE (symbol) = type;

>    SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;

>    SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;

> diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c

> index 5c4158cd6fb..9ed1d73a3ae 100644

> --- a/gdb/mdebugread.c

> +++ b/gdb/mdebugread.c

> @@ -1056,7 +1056,7 @@ parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,

>                 FIELD_NAME (*f) = debug_info->ss + cur_fdr->issBase + tsym.iss;

>                 FIELD_BITSIZE (*f) = 0;

>

> -               enum_sym = allocate_symbol (mdebugread_objfile);

> +               enum_sym = new (&mdebugread_objfile->objfile_obstack) symbol;

>                 enum_sym->set_linkage_name

>                   (obstack_strdup (&mdebugread_objfile->objfile_obstack,

>                                    f->name));

> @@ -4721,7 +4721,7 @@ new_block (enum block_type type, enum language language)

>  static struct symbol *

>  new_symbol (const char *name)

>  {

> -  struct symbol *s = allocate_symbol (mdebugread_objfile);

> +  struct symbol *s = new (&mdebugread_objfile->objfile_obstack) symbol;

>

>    s->set_language (psymtab_language, &mdebugread_objfile->objfile_obstack);

>    s->compute_and_set_names (name, true, mdebugread_objfile->per_bfd);

> diff --git a/gdb/stabsread.c b/gdb/stabsread.c

> index 5ef7748a9eb..2d9d56624fd 100644

> --- a/gdb/stabsread.c

> +++ b/gdb/stabsread.c

> @@ -423,7 +423,7 @@ patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,

>               /* On xcoff, if a global is defined and never referenced,

>                  ld will remove it from the executable.  There is then

>                  a N_GSYM stab for it, but no regular (C_EXT) symbol.  */

> -             sym = allocate_symbol (objfile);

> +             sym = new (&objfile->objfile_obstack) symbol;

>               SYMBOL_DOMAIN (sym) = VAR_DOMAIN;

>               SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;

>               sym->set_linkage_name

> @@ -687,7 +687,7 @@ define_symbol (CORE_ADDR valu, const char *string, int desc, int type,

>       e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */

>    nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));

>

> -  current_symbol = sym = allocate_symbol (objfile);

> +  current_symbol = sym = new (&objfile->objfile_obstack) symbol;

>

>    if (processing_gcc_compilation)

>      {

> @@ -1307,7 +1307,7 @@ define_symbol (CORE_ADDR valu, const char *string, int desc, int type,

>        if (synonym)

>          {

>            /* Create the STRUCT_DOMAIN clone.  */

> -          struct symbol *struct_sym = allocate_symbol (objfile);

> +          struct symbol *struct_sym = new (&objfile->objfile_obstack) symbol;

>

>            *struct_sym = *sym;

>            SYMBOL_ACLASS_INDEX (struct_sym) = LOC_TYPEDEF;

> @@ -1349,7 +1349,7 @@ define_symbol (CORE_ADDR valu, const char *string, int desc, int type,

>        if (synonym)

>         {

>           /* Clone the sym and then modify it.  */

> -         struct symbol *typedef_sym = allocate_symbol (objfile);

> +         struct symbol *typedef_sym = new (&objfile->objfile_obstack) symbol;

>

>           *typedef_sym = *sym;

>           SYMBOL_ACLASS_INDEX (typedef_sym) = LOC_TYPEDEF;

> @@ -3632,7 +3632,7 @@ read_enum_type (const char **pp, struct type *type,

>        if (nbits != 0)

>         return error_type (pp, objfile);

>

> -      sym = allocate_symbol (objfile);

> +      sym = new (&objfile->objfile_obstack) symbol;

>        sym->set_linkage_name (name);

>        sym->set_language (get_current_subfile ()->language,

>                          &objfile->objfile_obstack);

> @@ -4299,7 +4299,7 @@ common_block_end (struct objfile *objfile)

>        return;

>      }

>

> -  sym = allocate_symbol (objfile);

> +  sym = new (&objfile->objfile_obstack) symbol;

>    /* Note: common_block_name already saved on objfile_obstack.  */

>    sym->set_linkage_name (common_block_name);

>    SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;

> diff --git a/gdb/symtab.c b/gdb/symtab.c

> index 652384cd469..d2dd91e610d 100644

> --- a/gdb/symtab.c

> +++ b/gdb/symtab.c

> @@ -6363,42 +6363,6 @@ initialize_ordinary_address_classes (void)

>

>

>

> -/* Initialize the symbol SYM, and mark it as being owned by an objfile.  */

> -

> -void

> -initialize_objfile_symbol (struct symbol *sym)

> -{

> -  SYMBOL_OBJFILE_OWNED (sym) = 1;

> -  SYMBOL_SECTION (sym) = -1;

> -}

> -

> -/* Allocate and initialize a new 'struct symbol' on OBJFILE's

> -   obstack.  */

> -

> -struct symbol *

> -allocate_symbol (struct objfile *objfile)

> -{

> -  struct symbol *result = new (&objfile->objfile_obstack) symbol ();

> -

> -  initialize_objfile_symbol (result);

> -

> -  return result;

> -}

> -

> -/* Allocate and initialize a new 'struct template_symbol' on OBJFILE's

> -   obstack.  */

> -

> -struct template_symbol *

> -allocate_template_symbol (struct objfile *objfile)

> -{

> -  struct template_symbol *result;

> -

> -  result = new (&objfile->objfile_obstack) template_symbol ();

> -  initialize_objfile_symbol (result);

> -

> -  return result;

> -}

> -

>  /* See symtab.h.  */

>

>  struct objfile *

> diff --git a/gdb/symtab.h b/gdb/symtab.h

> index 5c5db0fabac..86efd3602d1 100644

> --- a/gdb/symtab.h

> +++ b/gdb/symtab.h

> @@ -1107,7 +1107,7 @@ struct symbol : public general_symbol_info, public allocate_on_obstack

>      /* Class-initialization of bitfields is only allowed in C++20.  */

>      : domain (UNDEF_DOMAIN),

>        aclass_index (0),

> -      is_objfile_owned (0),

> +      is_objfile_owned (1),

>        is_argument (0),

>        is_inlined (0),

>        maybe_copied (0),

> @@ -1120,12 +1120,14 @@ struct symbol : public general_symbol_info, public allocate_on_obstack

>        language_specific.obstack = nullptr;

>        m_language = language_unknown;

>        ada_mangled = 0;

> -      section = 0;

> +      section = -1;

>        /* GCC 4.8.5 (on CentOS 7) does not correctly compile class-

>           initialization of unions, so we initialize it manually here.  */

>        owner.symtab = nullptr;

>      }

>

> +  symbol (const symbol &) = default;

> +

>    /* Data type of value */

>

>    struct type *type = nullptr;

> @@ -2334,12 +2336,6 @@ const char *

>    demangle_for_lookup (const char *name, enum language lang,

>                        demangle_result_storage &storage);

>

> -struct symbol *allocate_symbol (struct objfile *);

> -

> -void initialize_objfile_symbol (struct symbol *);

> -

> -struct template_symbol *allocate_template_symbol (struct objfile *);

> -

>  /* Test to see if the symbol of language SYMBOL_LANGUAGE specified by

>     SYMNAME (which is already demangled for C++ symbols) matches

>     SYM_TEXT in the first SYM_TEXT_LEN characters.  If so, add it to

> diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c

> index 2c19dc8c822..93bdb9b6a65 100644

> --- a/gdb/xcoffread.c

> +++ b/gdb/xcoffread.c

> @@ -1521,11 +1521,6 @@ read_xcoff_symtab (struct objfile *objfile, legacy_psymtab *pst)

>      }

>  }

>

> -#define        SYMBOL_DUP(SYMBOL1, SYMBOL2)    \

> -  (SYMBOL2) = new (&objfile->objfile_obstack) symbol (); \

> -  *(SYMBOL2) = *(SYMBOL1);

> -

> -

>  #define        SYMNAME_ALLOC(NAME, ALLOCED)    \

>    ((ALLOCED) ? (NAME) : obstack_strdup (&objfile->objfile_obstack, \

>                                         (NAME)))

> @@ -1561,8 +1556,6 @@ process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)

>    if (name[0] == '.')

>      ++name;

>

> -  initialize_objfile_symbol (sym);

> -

>    /* default assumptions */

>    SET_SYMBOL_VALUE_ADDRESS (sym, cs->c_value + off);

>    SYMBOL_DOMAIN (sym) = VAR_DOMAIN;

> @@ -1578,7 +1571,7 @@ process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)

>        SYMBOL_TYPE (sym) = objfile_type (objfile)->nodebug_text_symbol;

>

>        SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;

> -      SYMBOL_DUP (sym, sym2);

> +      sym2 = new (&objfile->objfile_obstack) symbol (*sym);

>

>        if (cs->c_sclass == C_EXT || C_WEAKEXT)

>         add_symbol_to_list (sym2, get_global_symbols ());

> --

> 2.17.2

>
Tom Tromey April 28, 2020, 3:26 p.m. | #2
>>>>> "Christian" == Christian Biesinger via Gdb-patches <gdb-patches@sourceware.org> writes:


>> SYMBOL_OBJFILE_OWNED (symbol) = 0;

>> +  SYMBOL_SECTION (symbol) = 0;


Christian> Does this not have to set objfile_owned to 0?

It does, it's just in the context, not the patch itself.  It's the line

>> SYMBOL_OBJFILE_OWNED (symbol) = 0;



Christian> Similarly, ada-exp.y has a call:
Christian>   struct symbol *sym = new (&temp_parse_space) symbol ();
Christian> Does this not need updating?

Nice find.  This code seems very questionable.

After looking at it, I think special treatment isn't necessary here.
This creates a temporary placeholder symbol that doesn't outlive
expression resolution.

Tom
Tankut Baris Aktemur via Gdb-patches April 28, 2020, 4:22 p.m. | #3
On Tue, Apr 28, 2020 at 10:26 AM Tom Tromey <tom@tromey.com> wrote:
>

> >>>>> "Christian" == Christian Biesinger via Gdb-patches <gdb-patches@sourceware.org> writes:

>

> >> SYMBOL_OBJFILE_OWNED (symbol) = 0;

> >> +  SYMBOL_SECTION (symbol) = 0;

>

> Christian> Does this not have to set objfile_owned to 0?

>

> It does, it's just in the context, not the patch itself.  It's the line

>

> >> SYMBOL_OBJFILE_OWNED (symbol) = 0;


I can't believe I missed that...

> Christian> Similarly, ada-exp.y has a call:

> Christian>   struct symbol *sym = new (&temp_parse_space) symbol ();

> Christian> Does this not need updating?

>

> Nice find.  This code seems very questionable.

>

> After looking at it, I think special treatment isn't necessary here.

> This creates a temporary placeholder symbol that doesn't outlive

> expression resolution.


Good point. I should have looked more closely at this code. Come to
think of it, there's probably no reason to allocate this on the
obstack, could just be on the stack...

Christian
Tom Tromey April 28, 2020, 5:25 p.m. | #4
>>>>> "Christian" == Christian Biesinger <cbiesinger@google.com> writes:


>> After looking at it, I think special treatment isn't necessary here.

>> This creates a temporary placeholder symbol that doesn't outlive

>> expression resolution.


Christian> Good point. I should have looked more closely at this code. Come to
Christian> think of it, there's probably no reason to allocate this on the
Christian> obstack, could just be on the stack...

It actually has to live long enough to make it out of the parser and
through the resolution code.  It's then replaced by this code in
ada-lang.c:resolve_subexp:

          exp->elts[pc + 1].block = candidates[i].block;
          exp->elts[pc + 2].symbol = candidates[i].symbol;

This is why ada-exp.y does this funny thing where it clears its
temporary obstack in apparently reverse order:

  obstack_free (&temp_parse_space, NULL);
  obstack_init (&temp_parse_space);

  return yyparse ();

... this way the obstack remains live until the next call to ada_parse.

This probably could all be a lot better.
Sometimes I wonder if it's possible to remove this overload-selection
menu from Ada...

Tom
Tom Tromey May 15, 2020, 10:18 p.m. | #5
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:


Tom> This removes allocate_symbol, allocate_template_symbol, and
Tom> initialize_objfile_symbol in favor of changing the default values for
Tom> symbol members, and updating the one per-arch caller.

I'm checking this in.

Tom

Patch

diff --git a/gdb/coffread.c b/gdb/coffread.c
index 7fbdcc4f68f..ca7db25101e 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1556,7 +1556,7 @@  process_coff_symbol (struct coff_symbol *cs,
 		     union internal_auxent *aux,
 		     struct objfile *objfile)
 {
-  struct symbol *sym = allocate_symbol (objfile);
+  struct symbol *sym = new (&objfile->objfile_obstack) symbol;
   char *name;
 
   name = cs->c_name;
@@ -2095,7 +2095,7 @@  coff_read_enum_type (int index, int length, int lastsym,
       switch (ms->c_sclass)
 	{
 	case C_MOE:
-	  sym = allocate_symbol (objfile);
+	  sym = new (&objfile->objfile_obstack) symbol;
 
 	  name = obstack_strdup (&objfile->objfile_obstack, name);
 	  sym->set_linkage_name (name);
diff --git a/gdb/ctfread.c b/gdb/ctfread.c
index 8cc7271c075..d7e9625fb13 100644
--- a/gdb/ctfread.c
+++ b/gdb/ctfread.c
@@ -407,7 +407,7 @@  ctf_add_enum_member_cb (const char *name, int enum_value, void *arg)
 
   if (name != NULL)
     {
-      struct symbol *sym = allocate_symbol (ccp->of);
+      struct symbol *sym = new (&ccp->of->objfile_obstack) symbol;
       OBJSTAT (ccp->of, n_syms++);
 
       sym->set_language (language_c, &ccp->of->objfile_obstack);
@@ -436,7 +436,7 @@  new_symbol (struct ctf_context *ccp, struct type *type, ctf_id_t tid)
   gdb::unique_xmalloc_ptr<char> name (ctf_type_aname_raw (fp, tid));
   if (name != NULL)
     {
-      sym = allocate_symbol (objfile);
+      sym = new (&objfile->objfile_obstack) symbol;
       OBJSTAT (objfile, n_syms++);
 
       sym->set_language (language_c, &objfile->objfile_obstack);
@@ -1071,7 +1071,7 @@  ctf_add_var_cb (const char *name, ctf_id_t id, void *arg)
 	  complaint (_("ctf_add_var_cb: %s has NO type (%ld)"), name, id);
 	  type = objfile_type (ccp->of)->builtin_error;
 	}
-	sym = allocate_symbol (ccp->of);
+	sym = new (&ccp->of->objfile_obstack) symbol;
 	OBJSTAT (ccp->of, n_syms++);
 	SYMBOL_TYPE (sym) = type;
 	SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 976261372bc..53361dce049 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -9131,7 +9131,7 @@  fixup_go_packaging (struct dwarf2_cu *cu)
 				     saved_package_name);
       struct symbol *sym;
 
-      sym = allocate_symbol (objfile);
+      sym = new (&objfile->objfile_obstack) symbol;
       sym->set_language (language_go, &objfile->objfile_obstack);
       sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
@@ -12940,7 +12940,7 @@  read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
       if (child_die->tag == DW_TAG_template_type_param
 	  || child_die->tag == DW_TAG_template_value_param)
 	{
-	  templ_func = allocate_template_symbol (objfile);
+	  templ_func = new (&objfile->objfile_obstack) template_symbol;
 	  templ_func->subclass = SYMBOL_TEMPLATE;
 	  break;
 	}
@@ -13494,8 +13494,7 @@  read_variable (struct die_info *die, struct dwarf2_cu *cu)
 	{
 	  struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
 
-	  storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
-	  initialize_objfile_symbol (storage);
+	  storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
 	  storage->concrete_type = containing_type;
 	  storage->subclass = SYMBOL_RUST_VTABLE;
 	}
@@ -20559,7 +20558,7 @@  new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
       if (space)
 	sym = space;
       else
-	sym = allocate_symbol (objfile);
+	sym = new (&objfile->objfile_obstack) symbol;
       OBJSTAT (objfile, n_syms++);
 
       /* Cache this symbol's name and the name's demangled form (if any).  */
diff --git a/gdb/jit.c b/gdb/jit.c
index 07e9ce7ae24..1b5ef46469e 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -655,7 +655,7 @@  finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
   for (gdb_block &gdb_block_iter : stab->blocks)
     {
       struct block *new_block = allocate_block (&objfile->objfile_obstack);
-      struct symbol *block_name = allocate_symbol (objfile);
+      struct symbol *block_name = new (&objfile->objfile_obstack) symbol;
       struct type *block_type = arch_type (objfile->arch (),
 					   TYPE_CODE_VOID,
 					   TARGET_CHAR_BIT,
diff --git a/gdb/language.c b/gdb/language.c
index 769b3299793..fd711e13336 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -1041,6 +1041,7 @@  language_alloc_type_symbol (enum language lang, struct type *type)
   symbol->set_language (lang, nullptr);
   symbol->owner.arch = gdbarch;
   SYMBOL_OBJFILE_OWNED (symbol) = 0;
+  SYMBOL_SECTION (symbol) = 0;
   SYMBOL_TYPE (symbol) = type;
   SYMBOL_DOMAIN (symbol) = VAR_DOMAIN;
   SYMBOL_ACLASS_INDEX (symbol) = LOC_TYPEDEF;
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 5c4158cd6fb..9ed1d73a3ae 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -1056,7 +1056,7 @@  parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 		FIELD_NAME (*f) = debug_info->ss + cur_fdr->issBase + tsym.iss;
 		FIELD_BITSIZE (*f) = 0;
 
-		enum_sym = allocate_symbol (mdebugread_objfile);
+		enum_sym = new (&mdebugread_objfile->objfile_obstack) symbol;
 		enum_sym->set_linkage_name
 		  (obstack_strdup (&mdebugread_objfile->objfile_obstack,
 				   f->name));
@@ -4721,7 +4721,7 @@  new_block (enum block_type type, enum language language)
 static struct symbol *
 new_symbol (const char *name)
 {
-  struct symbol *s = allocate_symbol (mdebugread_objfile);
+  struct symbol *s = new (&mdebugread_objfile->objfile_obstack) symbol;
 
   s->set_language (psymtab_language, &mdebugread_objfile->objfile_obstack);
   s->compute_and_set_names (name, true, mdebugread_objfile->per_bfd);
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index 5ef7748a9eb..2d9d56624fd 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -423,7 +423,7 @@  patch_block_stabs (struct pending *symbols, struct pending_stabs *stabs,
 	      /* On xcoff, if a global is defined and never referenced,
 	         ld will remove it from the executable.  There is then
 	         a N_GSYM stab for it, but no regular (C_EXT) symbol.  */
-	      sym = allocate_symbol (objfile);
+	      sym = new (&objfile->objfile_obstack) symbol;
 	      SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
 	      SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
 	      sym->set_linkage_name
@@ -687,7 +687,7 @@  define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
      e.g. ":t10=*2" or a nameless enum like " :T16=ered:0,green:1,blue:2,;" */
   nameless = (p == string || ((string[0] == ' ') && (string[1] == ':')));
 
-  current_symbol = sym = allocate_symbol (objfile);
+  current_symbol = sym = new (&objfile->objfile_obstack) symbol;
 
   if (processing_gcc_compilation)
     {
@@ -1307,7 +1307,7 @@  define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
       if (synonym)
         {
           /* Create the STRUCT_DOMAIN clone.  */
-          struct symbol *struct_sym = allocate_symbol (objfile);
+          struct symbol *struct_sym = new (&objfile->objfile_obstack) symbol;
 
           *struct_sym = *sym;
           SYMBOL_ACLASS_INDEX (struct_sym) = LOC_TYPEDEF;
@@ -1349,7 +1349,7 @@  define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
       if (synonym)
 	{
 	  /* Clone the sym and then modify it.  */
-	  struct symbol *typedef_sym = allocate_symbol (objfile);
+	  struct symbol *typedef_sym = new (&objfile->objfile_obstack) symbol;
 
 	  *typedef_sym = *sym;
 	  SYMBOL_ACLASS_INDEX (typedef_sym) = LOC_TYPEDEF;
@@ -3632,7 +3632,7 @@  read_enum_type (const char **pp, struct type *type,
       if (nbits != 0)
 	return error_type (pp, objfile);
 
-      sym = allocate_symbol (objfile);
+      sym = new (&objfile->objfile_obstack) symbol;
       sym->set_linkage_name (name);
       sym->set_language (get_current_subfile ()->language,
 			 &objfile->objfile_obstack);
@@ -4299,7 +4299,7 @@  common_block_end (struct objfile *objfile)
       return;
     }
 
-  sym = allocate_symbol (objfile);
+  sym = new (&objfile->objfile_obstack) symbol;
   /* Note: common_block_name already saved on objfile_obstack.  */
   sym->set_linkage_name (common_block_name);
   SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 652384cd469..d2dd91e610d 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -6363,42 +6363,6 @@  initialize_ordinary_address_classes (void)
 
 
 
-/* Initialize the symbol SYM, and mark it as being owned by an objfile.  */
-
-void
-initialize_objfile_symbol (struct symbol *sym)
-{
-  SYMBOL_OBJFILE_OWNED (sym) = 1;
-  SYMBOL_SECTION (sym) = -1;
-}
-
-/* Allocate and initialize a new 'struct symbol' on OBJFILE's
-   obstack.  */
-
-struct symbol *
-allocate_symbol (struct objfile *objfile)
-{
-  struct symbol *result = new (&objfile->objfile_obstack) symbol ();
-
-  initialize_objfile_symbol (result);
-
-  return result;
-}
-
-/* Allocate and initialize a new 'struct template_symbol' on OBJFILE's
-   obstack.  */
-
-struct template_symbol *
-allocate_template_symbol (struct objfile *objfile)
-{
-  struct template_symbol *result;
-
-  result = new (&objfile->objfile_obstack) template_symbol ();
-  initialize_objfile_symbol (result);
-
-  return result;
-}
-
 /* See symtab.h.  */
 
 struct objfile *
diff --git a/gdb/symtab.h b/gdb/symtab.h
index 5c5db0fabac..86efd3602d1 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -1107,7 +1107,7 @@  struct symbol : public general_symbol_info, public allocate_on_obstack
     /* Class-initialization of bitfields is only allowed in C++20.  */
     : domain (UNDEF_DOMAIN),
       aclass_index (0),
-      is_objfile_owned (0),
+      is_objfile_owned (1),
       is_argument (0),
       is_inlined (0),
       maybe_copied (0),
@@ -1120,12 +1120,14 @@  struct symbol : public general_symbol_info, public allocate_on_obstack
       language_specific.obstack = nullptr;
       m_language = language_unknown;
       ada_mangled = 0;
-      section = 0;
+      section = -1;
       /* GCC 4.8.5 (on CentOS 7) does not correctly compile class-
          initialization of unions, so we initialize it manually here.  */
       owner.symtab = nullptr;
     }
 
+  symbol (const symbol &) = default;
+
   /* Data type of value */
 
   struct type *type = nullptr;
@@ -2334,12 +2336,6 @@  const char *
   demangle_for_lookup (const char *name, enum language lang,
 		       demangle_result_storage &storage);
 
-struct symbol *allocate_symbol (struct objfile *);
-
-void initialize_objfile_symbol (struct symbol *);
-
-struct template_symbol *allocate_template_symbol (struct objfile *);
-
 /* Test to see if the symbol of language SYMBOL_LANGUAGE specified by
    SYMNAME (which is already demangled for C++ symbols) matches
    SYM_TEXT in the first SYM_TEXT_LEN characters.  If so, add it to
diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index 2c19dc8c822..93bdb9b6a65 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -1521,11 +1521,6 @@  read_xcoff_symtab (struct objfile *objfile, legacy_psymtab *pst)
     }
 }
 
-#define	SYMBOL_DUP(SYMBOL1, SYMBOL2)	\
-  (SYMBOL2) = new (&objfile->objfile_obstack) symbol (); \
-  *(SYMBOL2) = *(SYMBOL1);
-
-
 #define	SYMNAME_ALLOC(NAME, ALLOCED)	\
   ((ALLOCED) ? (NAME) : obstack_strdup (&objfile->objfile_obstack, \
 					(NAME)))
@@ -1561,8 +1556,6 @@  process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
   if (name[0] == '.')
     ++name;
 
-  initialize_objfile_symbol (sym);
-
   /* default assumptions */
   SET_SYMBOL_VALUE_ADDRESS (sym, cs->c_value + off);
   SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
@@ -1578,7 +1571,7 @@  process_xcoff_symbol (struct coff_symbol *cs, struct objfile *objfile)
       SYMBOL_TYPE (sym) = objfile_type (objfile)->nodebug_text_symbol;
 
       SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
-      SYMBOL_DUP (sym, sym2);
+      sym2 = new (&objfile->objfile_obstack) symbol (*sym);
 
       if (cs->c_sclass == C_EXT || C_WEAKEXT)
 	add_symbol_to_list (sym2, get_global_symbols ());