[binutils-gdb] Use a map for symfile.c:symtab_fns

Tom Tromey tromey@sourceware.org
Mon Jan 12 13:31:03 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b323045ab402a68e63252133d074e452e16edbad

commit b323045ab402a68e63252133d074e452e16edbad
Author: Tom Tromey <tromey@adacore.com>
Date:   Fri Nov 7 09:41:23 2025 -0700

    Use a map for symfile.c:symtab_fns
    
    This changes symfile.c:symtab_fns to use a map rather than a vector,
    letting us remove an otherwise extraneous type.
    
    Approved-By: Simon Marchi <simon.marchi@efficios.com>

Diff:
---
 gdb/symfile.c | 40 +++++++++++++---------------------------
 1 file changed, 13 insertions(+), 27 deletions(-)

diff --git a/gdb/symfile.c b/gdb/symfile.c
index d0bb61dda38..8366d27048f 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -101,24 +101,10 @@ static int simple_overlay_update_1 (struct obj_section *);
 
 static void symfile_find_segment_sections (struct objfile *objfile);
 
-/* List of all available sym_fns.  On gdb startup, each object file reader
-   calls add_symtab_fns() to register information on each format it is
-   prepared to read.  */
-
-struct registered_sym_fns
-{
-  registered_sym_fns (bfd_flavour sym_flavour_, const struct sym_fns *sym_fns_)
-  : sym_flavour (sym_flavour_), sym_fns (sym_fns_)
-  {}
-
-  /* BFD flavour that we handle.  */
-  enum bfd_flavour sym_flavour;
-
-  /* The "vtable" of symbol functions.  */
-  const struct sym_fns *sym_fns;
-};
-
-static std::vector<registered_sym_fns> symtab_fns;
+/* Map from a BFD flavour to the corresponding sym_fns instance.  On
+   gdb startup, each object file reader calls add_symtab_fns() to
+   register information on each format it is prepared to read.  */
+static gdb::unordered_map<bfd_flavour, const sym_fns *> symtab_fns;
 
 /* Values for "set print symbol-loading".  */
 
@@ -1758,7 +1744,7 @@ get_section_index (struct objfile *objfile, const char *section_name)
 void
 add_symtab_fns (enum bfd_flavour flavour, const struct sym_fns *sf)
 {
-  symtab_fns.emplace_back (flavour, sf);
+  symtab_fns.emplace (flavour, sf);
 }
 
 /* Initialize OBJFILE to read symbols from its associated BFD.  It
@@ -1771,14 +1757,9 @@ find_sym_fns (bfd *abfd)
 {
   enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
 
-  if (our_flavour == bfd_target_srec_flavour
-      || our_flavour == bfd_target_ihex_flavour
-      || our_flavour == bfd_target_tekhex_flavour)
-    return NULL;	/* No symbols.  */
-
-  for (const registered_sym_fns &rsf : symtab_fns)
-    if (our_flavour == rsf.sym_flavour)
-      return rsf.sym_fns;
+  if (auto iter = symtab_fns.find (our_flavour);
+      iter != symtab_fns.end ())
+    return iter->second;
 
   error (_("Object file %s could not be read.  Symbol format `%s' unknown."),
 	 abfd->filename, bfd_get_target (abfd));
@@ -3775,6 +3756,11 @@ INIT_GDB_FILE (symfile)
 
   gdb::observers::free_objfile.attach (symfile_free_objfile, "symfile");
 
+  /* Pre-register some BFD flavours as not having symbols.  */
+  add_symtab_fns (bfd_target_srec_flavour, nullptr);
+  add_symtab_fns (bfd_target_ihex_flavour, nullptr);
+  add_symtab_fns (bfd_target_tekhex_flavour, nullptr);
+
 #define READNOW_READNEVER_HELP \
   "The '-readnow' option will cause GDB to read the entire symbol file\n\
 immediately.  This makes the command slower, but may make future operations\n\


More information about the Gdb-cvs mailing list