[PATCH] readelf: Add -C/--demangle option.

Fangrui Song maskray@google.com
Sun Aug 2 22:04:10 GMT 2020


	PR binutils/26331
	* readelf.c (print_symbol): Handle -C.
	(options): Add -C/--demangle option.
	(parse_args): Handle the option.
	* doc/binutils.texi: Document the -C option to readelf.
	* NEWS: Mention the new feature.
---
 binutils/NEWS              |  4 ++++
 binutils/doc/binutils.texi | 10 ++++++++++
 binutils/readelf.c         | 32 +++++++++++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/binutils/NEWS b/binutils/NEWS
index e776f5a826..d1cbacbbc0 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -1,5 +1,9 @@
 -*- text -*-
 
+Changes in 2.36:
+
+* Added -C/--demangle option to readelf.
+
 Changes in 2.35:
 
 * Changed readelf's display of symbol names when wide mode is not enabled.
diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index 4a11bf1f1a..de86cae063 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -4708,6 +4708,7 @@ readelf [@option{-a}|@option{--all}]
         [@option{-d}|@option{--dynamic}]
         [@option{-V}|@option{--version-info}]
         [@option{-A}|@option{--arch-specific}]
+        [@option{-C}|@option{--demangle}]
         [@option{-D}|@option{--use-dynamic}]
         [@option{-L}|@option{--lint}|@option{--enable-checks}]
         [@option{-x} <number or name>|@option{--hex-dump=}<number or name>]
@@ -4858,6 +4859,15 @@ exist.
 Displays architecture-specific information in the file, if there
 is any.
 
+@item -C
+@itemx --demangle[=@var{style}]
+Decode (@dfn{demangle}) low-level symbol names into user-level names.
+Besides removing any initial underscore prepended by the system, this
+makes C++ function names readable.  Different compilers have different
+mangling styles. The optional demangling style argument can be used to
+choose an appropriate demangling style for your compiler. @xref{c++filt},
+for more information on demangling.
+
 @item -D
 @itemx --use-dynamic
 When displaying symbols, this option makes @command{readelf} use the
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 421992d12d..67785444ff 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -58,6 +58,7 @@
 
 #include "bfd.h"
 #include "bucomm.h"
+#include "demangle.h"
 #include "elfcomm.h"
 #include "dwarf.h"
 #include "ctf-api.h"
@@ -212,6 +213,8 @@ static struct dump_list_entry * dump_sects_byname;
 char * program_name = "readelf";
 
 static bfd_boolean show_name = FALSE;
+static int do_demangle = 0; /* -C, --demangle  */
+static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
 static bfd_boolean do_dynamic = FALSE;
 static bfd_boolean do_syms = FALSE;
 static bfd_boolean do_dyn_syms = FALSE;
@@ -552,6 +555,14 @@ print_symbol (signed int width, const char * symbol)
   mbstate_t state;
 #endif
   unsigned int width_remaining;
+  char *alloc = NULL;
+
+  if (do_demangle)
+    {
+      alloc = cplus_demangle (symbol, demangle_flags);
+      if (alloc != NULL)
+	symbol = alloc;
+    }
 
   if (width < 0)
     {
@@ -643,6 +654,7 @@ print_symbol (signed int width, const char * symbol)
       num_printed = width;
     }
 
+  free (alloc);
   return num_printed;
 }
 
@@ -4499,6 +4511,7 @@ static struct option options[] =
   {"dyn-syms",	       no_argument, 0, OPTION_DYN_SYMS},
   {"relocs",	       no_argument, 0, 'r'},
   {"notes",	       no_argument, 0, 'n'},
+  {"demangle",         optional_argument, 0, 'C'},
   {"dynamic",	       no_argument, 0, 'd'},
   {"lint",             no_argument, 0, 'L'},
   {"enable-checks",    no_argument, 0, 'L'},
@@ -4560,6 +4573,9 @@ usage (FILE * stream)
   -V --version-info      Display the version sections (if present)\n\
   -A --arch-specific     Display architecture specific information (if any)\n\
   -c --archive-index     Display the symbol/file index in an archive\n\
+  -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
+			 The STYLE, if specified, can be `none', `auto', \n\
+			 `gnu-v3', `java', `gnat', `dlang' or `rust'\n\
   -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
   -L --lint|--enable-checks  Display warning messages for possible problems\n\
   -x --hex-dump=<number|name>\n\
@@ -4693,7 +4709,7 @@ parse_args (struct dump_data *dumpdata, int argc, char ** argv)
     usage (stderr);
 
   while ((c = getopt_long
-	  (argc, argv, "ADHILNR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
+	  (argc, argv, "ACDHILNR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
     {
       switch (c)
 	{
@@ -4734,6 +4750,20 @@ parse_args (struct dump_data *dumpdata, int argc, char ** argv)
 	case 'A':
 	  do_arch = TRUE;
 	  break;
+	case 'C':
+	  do_demangle = 1;
+	  if (optarg != NULL)
+	    {
+	      enum demangling_styles style;
+	      style = cplus_demangle_name_to_style (optarg);
+	      if (style == unknown_demangling)
+		{
+		  error (_ ("unknown demangling style `%s'\n"), optarg);
+		  exit (1);
+		}
+	      cplus_demangle_set_style (style);
+	    }
+	  break;
 	case 'D':
 	  do_using_dynamic = TRUE;
 	  break;
-- 
2.28.0.163.g6104cc2f0b6-goog



More information about the Binutils mailing list