[13/15] Optimize lookup_minimal_symbol_text

Message ID 20231029173839.471514-14-tom@tromey.com
State New
Headers
Series Index DWARF in the background |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Testing failed
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Testing failed

Commit Message

Tom Tromey Oct. 29, 2023, 5:35 p.m. UTC
  lookup_minimal_symbol_text always loops over all objfiles, even when
an objfile is passed in as an argument.  This patch changes the
function to loop over the minimal number of objfiles in the latter
situation.
---
 gdb/minsyms.c | 69 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 41 insertions(+), 28 deletions(-)
  

Patch

diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 1d778822858..7e0522159b8 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -623,38 +623,51 @@  lookup_minimal_symbol_text (const char *name, struct objfile *objf)
 
   unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
 
-  for (objfile *objfile : current_program_space->objfiles ())
-    {
-      if (found_symbol.minsym != NULL)
-	break;
+  auto search = [&] (struct objfile *objfile)
+  {
+    for (msymbol = objfile->per_bfd->msymbol_hash[hash];
+	 msymbol != NULL && found_symbol.minsym == NULL;
+	 msymbol = msymbol->hash_next)
+      {
+	if (strcmp (msymbol->linkage_name (), name) == 0 &&
+	    (msymbol->type () == mst_text
+	     || msymbol->type () == mst_text_gnu_ifunc
+	     || msymbol->type () == mst_file_text))
+	  {
+	    switch (msymbol->type ())
+	      {
+	      case mst_file_text:
+		found_file_symbol.minsym = msymbol;
+		found_file_symbol.objfile = objfile;
+		break;
+	      default:
+		found_symbol.minsym = msymbol;
+		found_symbol.objfile = objfile;
+		break;
+	      }
+	  }
+      }
+  };
 
-      if (objf == NULL || objf == objfile
-	  || objf == objfile->separate_debug_objfile_backlink)
+  if (objf == nullptr)
+    {
+      for (objfile *objfile : current_program_space->objfiles ())
 	{
-	  for (msymbol = objfile->per_bfd->msymbol_hash[hash];
-	       msymbol != NULL && found_symbol.minsym == NULL;
-	       msymbol = msymbol->hash_next)
-	    {
-	      if (strcmp (msymbol->linkage_name (), name) == 0 &&
-		  (msymbol->type () == mst_text
-		   || msymbol->type () == mst_text_gnu_ifunc
-		   || msymbol->type () == mst_file_text))
-		{
-		  switch (msymbol->type ())
-		    {
-		    case mst_file_text:
-		      found_file_symbol.minsym = msymbol;
-		      found_file_symbol.objfile = objfile;
-		      break;
-		    default:
-		      found_symbol.minsym = msymbol;
-		      found_symbol.objfile = objfile;
-		      break;
-		    }
-		}
-	    }
+	  if (found_symbol.minsym != NULL)
+	    break;
+	  search (objfile);
 	}
     }
+  else
+    {
+      for (objfile *objfile : objf->separate_debug_objfiles ())
+	{
+	  if (found_symbol.minsym != NULL)
+	    break;
+	  search (objfile);
+	}
+    }
+
   /* External symbols are best.  */
   if (found_symbol.minsym)
     return found_symbol;