HPUX gas testsuite fixes
[deliverable/binutils-gdb.git] / gdb / minsyms.c
index 6e7021a6809ed8710ce5a4417654d15313a910ab..21335080d3143954d58b85d4905664cea76ae81c 100644 (file)
@@ -1,5 +1,5 @@
 /* GDB routines for manipulating the minimal symbol tables.
-   Copyright (C) 1992-2019 Free Software Foundation, Inc.
+   Copyright (C) 1992-2020 Free Software Foundation, Inc.
    Contributed by Cygnus Support, using pieces from other GDB modules.
 
    This file is part of GDB.
 #include "gdbsupport/symbol.h"
 #include <algorithm>
 #include "safe-ctype.h"
+#include "gdbsupport/parallel-for.h"
+
+#if CXX_STD_THREAD
+#include <mutex>
+#endif
 
 /* See minsyms.h.  */
 
@@ -136,12 +141,12 @@ msymbol_hash (const char *string)
 /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE.  */
 static void
 add_minsym_to_hash_table (struct minimal_symbol *sym,
-                         struct minimal_symbol **table)
+                         struct minimal_symbol **table,
+                         unsigned int hash_value)
 {
   if (sym->hash_next == NULL)
     {
-      unsigned int hash
-       = msymbol_hash (sym->linkage_name ()) % MINIMAL_SYMBOL_HASH_SIZE;
+      unsigned int hash = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
 
       sym->hash_next = table[hash];
       table[hash] = sym;
@@ -152,18 +157,16 @@ add_minsym_to_hash_table (struct minimal_symbol *sym,
    TABLE.  */
 static void
 add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
-                                   struct objfile *objfile)
+                                   struct objfile *objfile,
+                                   unsigned int hash_value)
 {
   if (sym->demangled_hash_next == NULL)
     {
-      unsigned int hash = search_name_hash (MSYMBOL_LANGUAGE (sym),
-                                           sym->search_name ());
-
-      objfile->per_bfd->demangled_hash_languages.set (MSYMBOL_LANGUAGE (sym));
+      objfile->per_bfd->demangled_hash_languages.set (sym->language ());
 
       struct minimal_symbol **table
        = objfile->per_bfd->msymbol_demangled_hash;
-      unsigned int hash_index = hash % MINIMAL_SYMBOL_HASH_SIZE;
+      unsigned int hash_index = hash_value % MINIMAL_SYMBOL_HASH_SIZE;
       sym->demangled_hash_next = table[hash_index];
       table[hash_index] = sym;
     }
@@ -1125,14 +1128,14 @@ minimal_symbol_reader::record_full (gdb::string_view name,
       m_msym_bunch = newobj;
     }
   msymbol = &m_msym_bunch->contents[m_msym_bunch_index];
-  symbol_set_language (msymbol, language_auto,
-                      &m_objfile->per_bfd->storage_obstack);
+  msymbol->set_language (language_auto,
+                        &m_objfile->per_bfd->storage_obstack);
 
   if (copy_name)
-    msymbol->name = obstack_strndup (&m_objfile->per_bfd->storage_obstack,
-                                    name.data (), name.size ());
+    msymbol->m_name = obstack_strndup (&m_objfile->per_bfd->storage_obstack,
+                                      name.data (), name.size ());
   else
-    msymbol->name = name.data ();
+    msymbol->m_name = name.data ();
 
   SET_MSYMBOL_VALUE_ADDRESS (msymbol, address);
   MSYMBOL_SECTION (msymbol) = section;
@@ -1253,28 +1256,47 @@ clear_minimal_symbol_hash_tables (struct objfile *objfile)
     }
 }
 
+/* This struct is used to store values we compute for msymbols on the
+   background threads but don't need to keep around long term.  */
+struct computed_hash_values
+{
+  /* Length of the linkage_name of the symbol.  */
+  size_t name_length;
+  /* Hash code (using fast_hash) of the linkage_name.  */
+  hashval_t mangled_name_hash;
+  /* The msymbol_hash of the linkage_name.  */
+  unsigned int minsym_hash;
+  /* The msymbol_hash of the search_name.  */
+  unsigned int minsym_demangled_hash;
+};
+
 /* Build (or rebuild) the minimal symbol hash tables.  This is necessary
    after compacting or sorting the table since the entries move around
    thus causing the internal minimal_symbol pointers to become jumbled.  */
   
 static void
-build_minimal_symbol_hash_tables (struct objfile *objfile)
+build_minimal_symbol_hash_tables
+  (struct objfile *objfile,
+   const std::vector<computed_hash_values>& hash_values)
 {
   int i;
   struct minimal_symbol *msym;
 
   /* (Re)insert the actual entries.  */
-  for ((i = objfile->per_bfd->minimal_symbol_count,
+  int mcount = objfile->per_bfd->minimal_symbol_count;
+  for ((i = 0,
        msym = objfile->per_bfd->msymbols.get ());
-       i > 0;
-       i--, msym++)
+       i < mcount;
+       i++, msym++)
     {
       msym->hash_next = 0;
-      add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash);
+      add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash,
+                               hash_values[i].minsym_hash);
 
       msym->demangled_hash_next = 0;
       if (msym->search_name () != msym->linkage_name ())
-       add_minsym_to_demangled_hash_table (msym, objfile);
+       add_minsym_to_demangled_hash_table
+         (msym, objfile, hash_values[i].minsym_demangled_hash);
     }
 }
 
@@ -1359,18 +1381,68 @@ minimal_symbol_reader::install ()
       m_objfile->per_bfd->minimal_symbol_count = mcount;
       m_objfile->per_bfd->msymbols = std::move (msym_holder);
 
-      msymbols = m_objfile->per_bfd->msymbols.get ();
-      for (int i = 0; i < mcount; ++i)
-       {
-         if (!msymbols[i].name_set)
-           {
-             symbol_set_names (&msymbols[i], msymbols[i].name,
-                               false, m_objfile->per_bfd);
-             msymbols[i].name_set = 1;
-           }
-       }
+#if CXX_STD_THREAD
+      /* Mutex that is used when modifying or accessing the demangled
+        hash table.  */
+      std::mutex demangled_mutex;
+#endif
+
+      std::vector<computed_hash_values> hash_values (mcount);
 
-      build_minimal_symbol_hash_tables (m_objfile);
+      msymbols = m_objfile->per_bfd->msymbols.get ();
+      gdb::parallel_for_each
+       (&msymbols[0], &msymbols[mcount],
+        [&] (minimal_symbol *start, minimal_symbol *end)
+        {
+          for (minimal_symbol *msym = start; msym < end; ++msym)
+            {
+              size_t idx = msym - msymbols;
+              hash_values[idx].name_length = strlen (msym->linkage_name ());
+              if (!msym->name_set)
+                {
+                  /* This will be freed later, by compute_and_set_names.  */
+                  char *demangled_name
+                    = symbol_find_demangled_name (msym, msym->linkage_name ());
+                  symbol_set_demangled_name
+                    (msym, demangled_name,
+                     &m_objfile->per_bfd->storage_obstack);
+                  msym->name_set = 1;
+                }
+              /* This mangled_name_hash computation has to be outside of
+                 the name_set check, or compute_and_set_names below will
+                 be called with an invalid hash value.  */
+              hash_values[idx].mangled_name_hash
+                = fast_hash (msym->linkage_name (),
+                             hash_values[idx].name_length);
+              hash_values[idx].minsym_hash
+                = msymbol_hash (msym->linkage_name ());
+              /* We only use this hash code if the search name differs
+                 from the linkage name.  See the code in
+                 build_minimal_symbol_hash_tables.  */
+              if (msym->search_name () != msym->linkage_name ())
+                hash_values[idx].minsym_demangled_hash
+                  = search_name_hash (msym->language (), msym->search_name ());
+            }
+          {
+            /* To limit how long we hold the lock, we only acquire it here
+               and not while we demangle the names above.  */
+#if CXX_STD_THREAD
+            std::lock_guard<std::mutex> guard (demangled_mutex);
+#endif
+            for (minimal_symbol *msym = start; msym < end; ++msym)
+              {
+                size_t idx = msym - msymbols;
+                msym->compute_and_set_names
+                  (gdb::string_view (msym->linkage_name (),
+                                     hash_values[idx].name_length),
+                   false,
+                   m_objfile->per_bfd,
+                   hash_values[idx].mangled_name_hash);
+              }
+          }
+        });
+
+      build_minimal_symbol_hash_tables (m_objfile, hash_values);
     }
 }
 
This page took 0.029259 seconds and 4 git commands to generate.