* [RFC][PATCH v2 1/2] libiberty: implement option to drop templates during demangling
2024-03-08 18:04 [RFC][PATCH v2 0/2] objdump, libiberty: Implement short name search Mikołaj Piróg
@ 2024-03-08 18:08 ` Mikołaj Piróg
2024-04-12 10:22 ` Nick Clifton
2024-03-08 18:13 ` [RFC][PATCH v2 2/2] objdump: Implement short name search Mikołaj Piróg
1 sibling, 1 reply; 4+ messages in thread
From: Mikołaj Piróg @ 2024-03-08 18:08 UTC (permalink / raw)
To: binutils
[-- Attachment #1: Type: text/plain, Size: 2983 bytes --]
This patch introduces option that when given to cplus_demangle, drops
template from a symbol during demangling (in printing stage)
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, ' ');
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.
>
>
>
>
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [RFC][PATCH v2 2/2] objdump: Implement short name search
2024-03-08 18:04 [RFC][PATCH v2 0/2] objdump, libiberty: Implement short name search Mikołaj Piróg
2024-03-08 18:08 ` [RFC][PATCH v2 1/2] libiberty: implement option to drop templates during demangling Mikołaj Piróg
@ 2024-03-08 18:13 ` Mikołaj Piróg
1 sibling, 0 replies; 4+ messages in thread
From: Mikołaj Piróg @ 2024-03-08 18:13 UTC (permalink / raw)
To: binutils
[-- Attachment #1: Type: text/plain, Size: 3283 bytes --]
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.
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
{
@@ -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.
>
>
>
>
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread