[RFC,v2,2/2] objdump: Implement short name search

Message ID CAOscM-vAan1FOiwbQ15HnZHk+1F_VvU7OnqfesCOfM6wCHRKbA@mail.gmail.com
State New
Headers
Series objdump, libiberty: Implement short name search |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply

Commit Message

Mikołaj Piróg March 8, 2024, 6:13 p.m. UTC
  Implement short name search. When objdump is used like so:

objdump --disassemble=sym -C file

It will attempt to decide if "sym" is a short name or a regular name. If it
is a short name, it will search for symbols matching given name.

   {
@@ -3905,7 +3908,10 @@ disassemble_section (bfd *abfd, asection *section,
void *inf)
       if (do_demangle && name[0] != '\0')
  {
   /* Demangle the name.  */
-  alloc = bfd_demangle (abfd, name, demangle_flags);
+  if (short_name_search)
+    alloc = bfd_demangle (abfd, name, DMGL_TMPL_DROP);
+  else
+    alloc = bfd_demangle (abfd, name, demangle_flags);
   if (alloc != NULL)
     name = alloc;
  }
@@ -3923,7 +3929,7 @@ disassemble_section (bfd *abfd, asection *section,
void *inf)
    (*rel_pp)->address - rel_offset < sym_offset)
   ++rel_pp;

-  if (sym->flags & BSF_FUNCTION)
+  if (sym->flags & BSF_FUNCTION && !(short_name_search && do_demangle))
     {
       if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
   && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
@@ -5879,7 +5885,18 @@ display_file (char *filename, char *target, bool
last_file)
   else
     bfd_close_all_done (file);
 }
-
+
+/* Attempts to determine if a given name represents a short name of a
symbol.
+   Short name of a symbol is name without a signature and template
qualification. */
+static bool is_short_name(const char *name) {
+  char *begin = strchr(name, '(');
+  char *end = strchr(name, ')');
+  if (end - begin <= 1)
+    return true;
+
+  return false;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -6255,6 +6272,9 @@ main (int argc, char **argv)
  }
     }

+  if (disasm_sym != NULL && do_demangle)
+    short_name_search = is_short_name(disasm_sym);
+
   if (disassembler_color == on_if_terminal_output)
     disassembler_color = isatty (1) ? on : off;



pt., 8 mar 2024 o 19:04 Mikołaj Piróg <mikolajpirog@gmail.com> napisał(a):

> This patch refines my previous attempt of implementing short name search (
> https://sourceware.org/pipermail/binutils/2024-February/132713.html). The
> idea is to enable such usage:
>
> objdump --disassemble=ns::foo -C file
>
> The objdump will then output all symbols matching the name "ns::foo".
> I introduced new demangling option to remove template qualification, so for
> example, "--dissassemble=std::vector::push_back" will match all instances
> of push_back in the file.
>
> The motivation behind this usage is to ease a process of a quick
> disassembly of a binary in search for a particular function. Providing a
> function signature isn't comfortable.
>
>
>
>
>
>
>
  

Patch

diff --git a/binutils/objdump.c b/binutils/objdump.c
index 7beb221cb2f..21c4b86bf3d 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -138,6 +138,9 @@  static bool extended_color_output = false; /*
--visualize-jumps=extended-color.
 static int process_links = false;       /* --process-links.  */
 static int show_all_symbols;            /* --show-all-symbols.  */
 static bool decompressed_dumps = false; /* -Z, --decompress.  */
+static bool short_name_search = false;  /* True when demangling is used,
disasm_sym
+   is given and appears to be
+   a short name */

 static enum color_selection