[RFC,v2,1/2] libiberty: implement option to drop templates during demangling

Message ID CAOscM-vo7gGrZ6n1hmn1hkzFbS71Er2AFPfM=t3tVYNV8ocWOg@mail.gmail.com
State New
Headers
Series objdump, libiberty: Implement short name search |

Checks

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

Commit Message

Mikołaj Piróg March 8, 2024, 6:08 p.m. UTC
  This patch introduces option that when given to cplus_demangle, drops
template from a symbol during demangling (in printing stage)

     d_append_char (dpi, '<');
@@ -5333,6 +5336,9 @@ d_print_comp_inner (struct d_print_info *dpi, int
options,
       }

     case DEMANGLE_COMPONENT_TEMPLATE_PARAM:
+      if ((options & DMGL_TMPL_DROP) != 0)
+ break;
+
       if (dpi->lambda_tpl_parms > dc->u.s_number.number + 1)
  {
   const struct demangle_component *a
@@ -5492,6 +5498,23 @@ d_print_comp_inner (struct d_print_info *dpi, int
options,
       return;

     case DEMANGLE_COMPONENT_SUB_STD:
+      /* Special-case substitions are stored wholly as a string. They need
+ to be stripped of templates manually */
+      if ((options & DMGL_TMPL_DROP) != 0)
+      {
+ char *template_begin = strchr(dc->u.s_string.string, '<');
+ if (template_begin != NULL)
+ {
+  d_append_buffer(dpi, dc->u.s_string.string, (template_begin -
dc->u.s_string.string));
+
+  char *template_end= strrchr(dc->u.s_string.string, '>');
+  if (template_end == NULL)
+    return;
+
+  d_append_buffer(dpi, template_end, dc->u.s_string.len - (template_end -
dc->u.s_string.string) - 1);
+  return;
+ }
+      }
       d_append_buffer (dpi, dc->u.s_string.string, dc->u.s_string.len);
       return;



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.
>
>
>
>
>
>
>
  

Comments

Nick Clifton April 12, 2024, 10:22 a.m. UTC | #1
Hi Mikołaj,

> This patch introduces option that when given to cplus_demangle, drops template from a symbol during demangling (in printing stage)

I am not sure if I have mentioned this already but the libiberty sources
are maintained by the GCC folks, not us.  So in order to get this patch
in you will need to submit it to them...

Cheers
   Nick
  

Patch

diff --git a/include/demangle.h b/include/demangle.h
index 49b84d4de88..89a3b64e436 100644
--- a/include/demangle.h
+++ b/include/demangle.h
@@ -51,6 +51,8 @@  extern "C" {
    types, even if present.  It applies
    only to the toplevel function type.
    */
+#define DMGL_TMPL_DROP   (1 << 7)       /* Supress printing of templates,
even
+   if present. */

 #define DMGL_AUTO (1 << 8)
 #define DMGL_GNU_V3 (1 << 14)
diff --git a/libiberty/cp-demangle.c b/libiberty/cp-demangle.c
index fc2cf64e6e0..ad873ed6177 100644
--- a/libiberty/cp-demangle.c
+++ b/libiberty/cp-demangle.c
@@ -5315,6 +5315,9 @@  d_print_comp_inner (struct d_print_info *dpi, int
options,
         else
           {
     d_print_comp (dpi, options, dcl);
+    if ((options & DMGL_TMPL_DROP) != 0)
+      return;
+
     if (d_last_char (dpi) == '<')
       d_append_char (dpi, ' ');