gdb: split iterate_over_minimal_symbols into for_each_minimal_symbol and find_minimal_symbol

Based on the same rationale as the previous patches, split
iterate_over_minimal_symbols in two.

Implement for_each_minimal_symbol using find_minimal_symbol, since that
one is really not trivial.

Change-Id: Ie02e67278359454f7aa583200ec68d2f429f7ebe
Approved-By: Andrew Burgess <aburgess@redhat.com>
This commit is contained in:
Simon Marchi 2026-04-16 16:16:17 -04:00 committed by Simon Marchi
parent cfe0642a7f
commit 530a683df2
4 changed files with 74 additions and 41 deletions

View file

@ -4150,33 +4150,30 @@ search_minsyms_for_name (struct collect_info *info,
set_current_program_space (pspace);
for (objfile &objfile : pspace->objfiles ())
{
iterate_over_minimal_symbols (&objfile, name,
[&] (struct minimal_symbol *msym)
{
add_minsym (msym, &objfile, nullptr,
info->state->list_mode,
&minsyms);
return false;
});
}
for_each_minimal_symbol (&objfile, name,
[&] (minimal_symbol *msym)
{
add_minsym (msym, &objfile, nullptr,
info->state->list_mode,
&minsyms);
});
}
}
else
{
program_space *pspace = symtab->compunit ()->objfile ()->pspace ();
objfile &objfile = *symtab->compunit ()->objfile ();
program_space *pspace = objfile.pspace ();
if (search_pspace == NULL || pspace == search_pspace)
{
set_current_program_space (pspace);
iterate_over_minimal_symbols
(symtab->compunit ()->objfile (), name,
[&] (struct minimal_symbol *msym)
{
add_minsym (msym, symtab->compunit ()->objfile (), symtab,
info->state->list_mode, &minsyms);
return false;
});
for_each_minimal_symbol (&objfile, name,
[&] (minimal_symbol *msym)
{
add_minsym (msym, &objfile, symtab,
info->state->list_mode,
&minsyms);
});
}
}

View file

@ -496,9 +496,22 @@ linkage_name_str (const lookup_name_info &lookup_name)
/* See minsyms.h. */
void
iterate_over_minimal_symbols
(struct objfile *objf, const lookup_name_info &lookup_name,
gdb::function_view<bool (struct minimal_symbol *)> callback)
for_each_minimal_symbol (struct objfile *objf, const lookup_name_info &name,
for_each_minimal_symbol_callback_ftype callback)
{
find_minimal_symbol (objf, name,
[&] (struct minimal_symbol *msym)
{
callback (msym);
return false;
});
}
/* See minsyms.h. */
minimal_symbol *
find_minimal_symbol (struct objfile *objf, const lookup_name_info &lookup_name,
find_minimal_symbol_callback_ftype callback)
{
/* The first pass is over the ordinary hash table. */
{
@ -515,7 +528,7 @@ iterate_over_minimal_symbols
{
if (mangled_cmp (iter->linkage_name (), name) == 0)
if (callback (iter))
return;
return iter;
}
}
@ -539,8 +552,10 @@ iterate_over_minimal_symbols
iter = iter->demangled_hash_next)
if (name_match (iter->search_name (), lookup_name, NULL))
if (callback (iter))
return;
return iter;
}
return nullptr;
}
/* See minsyms.h. */

View file

@ -20,6 +20,7 @@
#ifndef GDB_MINSYMS_H
#define GDB_MINSYMS_H
#include "gdbsupport/function-view.h"
#include <deque>
struct program_space;
@ -279,16 +280,36 @@ bound_minimal_symbol lookup_minimal_symbol_by_pc_section
bound_minimal_symbol lookup_minimal_symbol_by_pc (CORE_ADDR);
/* Iterate over all the minimal symbols in the objfile OBJF which
match NAME. Both the ordinary and demangled names of each symbol
are considered. The caller is responsible for canonicalizing NAME,
should that need to be done.
/* Callback type for function for_each_minimal_symbol. */
For each matching symbol, CALLBACK is called with the symbol. */
using for_each_minimal_symbol_callback_ftype
= gdb::function_view<void (struct minimal_symbol *)>;
void iterate_over_minimal_symbols
(struct objfile *objf, const lookup_name_info &name,
gdb::function_view<bool (struct minimal_symbol *)> callback);
/* Call CALLBACK for all minimal symbols in objfile OBJF which match NAME.
Both the ordinary and demangled names of each symbol are considered. The
caller is responsible for canonicalizing NAME, should that need to be
done. */
void for_each_minimal_symbol (struct objfile *objf,
const lookup_name_info &name,
for_each_minimal_symbol_callback_ftype callback);
/* Callback type for function find_minimal_symbol. */
using find_minimal_symbol_callback_ftype
= gdb::function_view<bool (struct minimal_symbol *)>;
/* Find the first minimal symbol for objfile OBJF which matches NAME and for
which CALLBACK returns true.
Both the ordinary and demangled names of each symbol are considered. The
caller is responsible for canonicalizing NAME, should that need to be
done. */
minimal_symbol *find_minimal_symbol
(struct objfile *objf, const lookup_name_info &name,
find_minimal_symbol_callback_ftype callback);
/* Compute the upper bound of MINSYM. The upper bound is the last
address thought to be part of the symbol. If the symbol has a

View file

@ -5782,32 +5782,32 @@ find_gnu_ifunc (const symbol *sym)
struct objfile *objfile = sym->objfile ();
CORE_ADDR address = sym->value_block ()->entry_pc ();
minimal_symbol *ifunc = NULL;
iterate_over_minimal_symbols (objfile, lookup_name,
[&] (minimal_symbol *minsym)
minimal_symbol *ifunc
= find_minimal_symbol (objfile, lookup_name,
[&] (minimal_symbol *minsym)
{
if (minsym->type () == mst_text_gnu_ifunc
|| minsym->type () == mst_data_gnu_ifunc)
{
CORE_ADDR msym_addr = minsym->value_address (objfile);
if (minsym->type () == mst_data_gnu_ifunc)
{
struct gdbarch *gdbarch = objfile->arch ();
msym_addr = gdbarch_convert_from_func_ptr_addr
(gdbarch, msym_addr, current_inferior ()->top_target ());
}
if (msym_addr == address)
{
ifunc = minsym;
return true;
}
return true;
}
return false;
});
if (ifunc != NULL)
if (ifunc != nullptr)
return {ifunc, objfile};
return {};
}