From 0d64622696e02ad649d048f4af3a3f293481f89f Mon Sep 17 00:00:00 2001
From: Alan Modra <amodra@gmail.com>
Date: Thu, 11 Nov 2021 20:21:32 +1030
Subject: [PATCH] Fix demangle style usage info

Extract allowed styles from libiberty, so we don't have to worry about
our help messages getting out of date.  The function probably belongs
in libiberty/cplus-dem.c but it can be here for a while to iron out
bugs.

    PR 28581
    * demanguse.c: New file.
    * demanguse.h: New file.
    * nm.c (usage): Break up output.  Use display_demangler_styles.
    * objdump.c (usage): Use display_demangler_styles.
    * Makefile.am: Add demanguse.c and demanguse.h.
    * Makefile.in: Regenerate.
    * po/POTFILESin: Regenerate.
---
 binutils/Makefile.am    | 10 ++++----
 binutils/Makefile.in    | 19 +++++++++------
 binutils/demanguse.c    | 54 +++++++++++++++++++++++++++++++++++++++++
 binutils/demanguse.h    | 34 ++++++++++++++++++++++++++
 binutils/nm.c           | 12 +++++----
 binutils/objdump.c      | 12 +++++----
 binutils/po/POTFILES.in |  2 ++
 7 files changed, 120 insertions(+), 23 deletions(-)
 create mode 100644 binutils/demanguse.c
 create mode 100644 binutils/demanguse.h

diff --git a/binutils/Makefile.am b/binutils/Makefile.am
index 2d3c6f46..6bc7bef6 100644
--- a/binutils/Makefile.am
+++ b/binutils/Makefile.am
@@ -122,7 +122,7 @@ AM_CPPFLAGS = -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) \

 HFILES = \
 	arsup.h binemul.h bucomm.h budbg.h \
-	coffgrok.h debug.h dlltool.h dwarf.h elfcomm.h \
+	coffgrok.h debug.h demanguse.h dlltool.h dwarf.h elfcomm.h \
 	objdump.h sysdep.h unwind-ia64.h windres.h winduni.h windint.h \
 	windmc.h

@@ -132,7 +132,7 @@ BUILT_SOURCES = $(GENERATED_HFILES)
 CFILES = \
 	addr2line.c ar.c arsup.c bin2c.c binemul.c bucomm.c \
 	coffdump.c coffgrok.c cxxfilt.c \
-	dwarf.c debug.c dlltool.c dllwrap.c \
+	dwarf.c debug.c demanguse.c dlltool.c dllwrap.c \
 	elfcomm.c emul_aix.c emul_vanilla.c filemode.c \
 	is-ranlib.c is-strip.c maybe-ranlib.c maybe-strip.c \
 	nm.c not-ranlib.c not-strip.c \
@@ -247,7 +247,7 @@ objcopy_SOURCES = objcopy.c not-strip.c rename.c $(WRITE_DEBUG_SRCS) $(BULIBS)

 strings_SOURCES = strings.c $(BULIBS)

-readelf_SOURCES = readelf.c version.c unwind-ia64.c dwarf.c $(ELFLIBS)
+readelf_SOURCES = readelf.c version.c unwind-ia64.c dwarf.c demanguse.c $(ELFLIBS)
 readelf_LDADD   = $(LIBINTL) $(LIBCTF_NOBFD) $(LIBIBERTY) $(ZLIB) $(LIBDEBUGINFOD)

 elfedit_SOURCES = elfedit.c version.c $(ELFLIBS)
@@ -255,9 +255,9 @@ elfedit_LDADD = $(LIBINTL) $(LIBIBERTY)

 strip_new_SOURCES = objcopy.c is-strip.c rename.c $(WRITE_DEBUG_SRCS) $(BULIBS)

-nm_new_SOURCES = nm.c $(BULIBS)
+nm_new_SOURCES = nm.c demanguse.c $(BULIBS)

-objdump_SOURCES = objdump.c dwarf.c prdbg.c $(DEBUG_SRCS) $(BULIBS) $(ELFLIBS)
+objdump_SOURCES = objdump.c dwarf.c prdbg.c demanguse.c $(DEBUG_SRCS) $(BULIBS) $(ELFLIBS)
 EXTRA_objdump_SOURCES = od-xcoff.c
 objdump_LDADD = $(OBJDUMP_PRIVATE_OFILES) $(OPCODES) $(LIBCTF) $(BFDLIB) $(LIBIBERTY) $(LIBINTL) $(LIBDEBUGINFOD)

diff --git a/binutils/Makefile.in b/binutils/Makefile.in
index f03ca3c3..de7f47e3 100644
--- a/binutils/Makefile.in
+++ b/binutils/Makefile.in
@@ -209,7 +209,7 @@ am__objects_2 = elfcomm.$(OBJEXT)
 am_elfedit_OBJECTS = elfedit.$(OBJEXT) version.$(OBJEXT) \
 	$(am__objects_2)
 elfedit_OBJECTS = $(am_elfedit_OBJECTS)
-am_nm_new_OBJECTS = nm.$(OBJEXT) $(am__objects_1)
+am_nm_new_OBJECTS = nm.$(OBJEXT) demanguse.$(OBJEXT) $(am__objects_1)
 nm_new_OBJECTS = $(am_nm_new_OBJECTS)
 nm_new_LDADD = $(LDADD)
 am__objects_3 = rddbg.$(OBJEXT) debug.$(OBJEXT) stabs.$(OBJEXT) \
@@ -220,14 +220,16 @@ am_objcopy_OBJECTS = objcopy.$(OBJEXT) not-strip.$(OBJEXT) \
 objcopy_OBJECTS = $(am_objcopy_OBJECTS)
 objcopy_LDADD = $(LDADD)
 am_objdump_OBJECTS = objdump.$(OBJEXT) dwarf.$(OBJEXT) prdbg.$(OBJEXT) \
-	$(am__objects_3) $(am__objects_1) $(am__objects_2)
+	demanguse.$(OBJEXT) $(am__objects_3) $(am__objects_1) \
+	$(am__objects_2)
 objdump_OBJECTS = $(am_objdump_OBJECTS)
 am_ranlib_OBJECTS = ar.$(OBJEXT) is-ranlib.$(OBJEXT) arparse.$(OBJEXT) \
 	arlex.$(OBJEXT) arsup.$(OBJEXT) rename.$(OBJEXT) \
 	binemul.$(OBJEXT) emul_$(EMULATION).$(OBJEXT) $(am__objects_1)
 ranlib_OBJECTS = $(am_ranlib_OBJECTS)
 am_readelf_OBJECTS = readelf.$(OBJEXT) version.$(OBJEXT) \
-	unwind-ia64.$(OBJEXT) dwarf.$(OBJEXT) $(am__objects_2)
+	unwind-ia64.$(OBJEXT) dwarf.$(OBJEXT) demanguse.$(OBJEXT) \
+	$(am__objects_2)
 readelf_OBJECTS = $(am_readelf_OBJECTS)
 am_size_OBJECTS = size.$(OBJEXT) $(am__objects_1)
 size_OBJECTS = $(am_size_OBJECTS)
@@ -622,7 +624,7 @@ AM_CPPFLAGS = -I. -I$(srcdir) -I../bfd -I$(BFDDIR) -I$(INCDIR) \

 HFILES = \
 	arsup.h binemul.h bucomm.h budbg.h \
-	coffgrok.h debug.h dlltool.h dwarf.h elfcomm.h \
+	coffgrok.h debug.h demanguse.h dlltool.h dwarf.h elfcomm.h \
 	objdump.h sysdep.h unwind-ia64.h windres.h winduni.h windint.h \
 	windmc.h

@@ -631,7 +633,7 @@ BUILT_SOURCES = $(GENERATED_HFILES)
 CFILES = \
 	addr2line.c ar.c arsup.c bin2c.c binemul.c bucomm.c \
 	coffdump.c coffgrok.c cxxfilt.c \
-	dwarf.c debug.c dlltool.c dllwrap.c \
+	dwarf.c debug.c demanguse.c dlltool.c dllwrap.c \
 	elfcomm.c emul_aix.c emul_vanilla.c filemode.c \
 	is-ranlib.c is-strip.c maybe-ranlib.c maybe-strip.c \
 	nm.c not-ranlib.c not-strip.c \
@@ -708,13 +710,13 @@ LDADD = $(BFDLIB) $(LIBIBERTY) $(LIBINTL)
 size_SOURCES = size.c $(BULIBS)
 objcopy_SOURCES = objcopy.c not-strip.c rename.c $(WRITE_DEBUG_SRCS) $(BULIBS)
 strings_SOURCES = strings.c $(BULIBS)
-readelf_SOURCES = readelf.c version.c unwind-ia64.c dwarf.c $(ELFLIBS)
+readelf_SOURCES = readelf.c version.c unwind-ia64.c dwarf.c demanguse.c $(ELFLIBS)
 readelf_LDADD = $(LIBINTL) $(LIBCTF_NOBFD) $(LIBIBERTY) $(ZLIB) $(LIBDEBUGINFOD)
 elfedit_SOURCES = elfedit.c version.c $(ELFLIBS)
 elfedit_LDADD = $(LIBINTL) $(LIBIBERTY)
 strip_new_SOURCES = objcopy.c is-strip.c rename.c $(WRITE_DEBUG_SRCS) $(BULIBS)
-nm_new_SOURCES = nm.c $(BULIBS)
-objdump_SOURCES = objdump.c dwarf.c prdbg.c $(DEBUG_SRCS) $(BULIBS) $(ELFLIBS)
+nm_new_SOURCES = nm.c demanguse.c $(BULIBS)
+objdump_SOURCES = objdump.c dwarf.c prdbg.c demanguse.c $(DEBUG_SRCS) $(BULIBS) $(ELFLIBS)
 EXTRA_objdump_SOURCES = od-xcoff.c
 objdump_LDADD = $(OBJDUMP_PRIVATE_OFILES) $(OPCODES) $(LIBCTF) $(BFDLIB) $(LIBIBERTY) $(LIBINTL) $(LIBDEBUGINFOD)
 cxxfilt_SOURCES = cxxfilt.c $(BULIBS)
@@ -1037,6 +1039,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/debug.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/deflex.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/defparse.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/demanguse.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dlltool.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dllwrap.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dwarf.Po@am__quote@
diff --git a/binutils/demanguse.c b/binutils/demanguse.c
new file mode 100644
index 00000000..578a3ef5
--- /dev/null
+++ b/binutils/demanguse.c
@@ -0,0 +1,54 @@
+/* demanguse.c -- libiberty demangler usage
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GNU Binutils.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
+
+#include <stdio.h>
+#include <string.h>
+#include "demangle.h"
+#include "demanguse.h"
+
+/* Print the list of demangling styles to STREAM.  A one line MSG is
+   printed before the styles.  Output is limited to 80 columns, with
+   continuation lines being indented by leading spaces in MSG.  */
+
+void
+display_demangler_styles (FILE *stream, const char *msg)
+{
+  const struct demangler_engine *info = libiberty_demanglers;
+  int col;
+  int lead_spaces = 0;
+  const char *cont = "";
+
+  while (msg[lead_spaces] == ' ')
+    ++lead_spaces;
+  col = fprintf (stream, "%s", msg);
+  while (info->demangling_style_name)
+    {
+      if (col + strlen (info->demangling_style_name) >= 75)
+	{
+	  fprintf (stream, "%.1s\n", cont);
+	  col = fprintf (stream, "%.*s", lead_spaces, msg);
+	  cont = "";
+	}
+      col += fprintf (stream, "%s\"%s\"", cont, info->demangling_style_name);
+      cont = ", ";
+      ++info;
+    }
+  fprintf (stream, "\n");
+}
diff --git a/binutils/demanguse.h b/binutils/demanguse.h
new file mode 100644
index 00000000..965f1166
--- /dev/null
+++ b/binutils/demanguse.h
@@ -0,0 +1,34 @@
+/* demanguse.h -- libiberty demangler usage
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GNU Binutils.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
+
+#ifndef DEMANGUSE_H
+#define DEMANGUSE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void display_demangler_styles (FILE *, const char *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* DEMANGUSE_H */
diff --git a/binutils/nm.c b/binutils/nm.c
index 0ee3f883..1c3e8666 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -36,6 +36,7 @@
 #include "coff/internal.h"
 #include "libcoff.h"
 #include "bucomm.h"
+#include "demanguse.h"
 #include "plugin-api.h"
 #include "plugin.h"

@@ -241,11 +242,12 @@ usage (FILE *stream, int status)
   fprintf (stream, _(" The options are:\n\
   -a, --debug-syms       Display debugger-only symbols\n\
   -A, --print-file-name  Print name of the input file before every symbol\n\
-  -B                     Same as --format=bsd\n\
-  -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
-                          The STYLE, if specified, can be `auto' (the default),\n\
-                          `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
-                          or `gnat'\n\
+  -B                     Same as --format=bsd\n"));
+  fprintf (stream, _("\
+  -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
+  display_demangler_styles (stream, _("\
+                           STYLE can be "));
+  fprintf (stream, _("\
       --no-demangle      Do not demangle low-level symbol names\n\
       --recurse-limit    Enable a demangling recursion limit.  This is the default.\n\
       --no-recurse-limit Disable a demangling recursion limit.\n\
diff --git a/binutils/objdump.c b/binutils/objdump.c
index 60a39671..e99d6633 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -55,6 +55,7 @@
 #include "progress.h"
 #include "bucomm.h"
 #include "elfcomm.h"
+#include "demanguse.h"
 #include "dwarf.h"
 #include "ctf-api.h"
 #include "getopt.h"
@@ -259,11 +260,12 @@ usage (FILE *stream, int status)
       --file-start-context       Include context from start of file (with -S)\n\
   -I, --include=DIR              Add DIR to search list for source files\n\
   -l, --line-numbers             Include line numbers and filenames in output\n\
-  -F, --file-offsets             Include file offsets when displaying information\n\
-  -C, --demangle[=STYLE]         Decode mangled/processed symbol names\n\
-                                  The STYLE, if specified, can be `auto', `gnu',\n\
-                                  `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
-                                  or `gnat'\n\
+  -F, --file-offsets             Include file offsets when displaying information\n"));
+      fprintf (stream, _("\
+  -C, --demangle[=STYLE]         Decode mangled/processed symbol names\n"));
+      display_demangler_styles (stream, _("\
+                                   STYLE can be "));
+      fprintf (stream, _("\
       --recurse-limit            Enable a limit on recursion whilst demangling.  [Default]\n\
       --no-recurse-limit         Disable a limit on recursion whilst demangling\n\
   -w, --wide                     Format output for more than 80 columns\n\
diff --git a/binutils/po/POTFILES.in b/binutils/po/POTFILES.in
index 6910e93c..592c6056 100644
--- a/binutils/po/POTFILES.in
+++ b/binutils/po/POTFILES.in
@@ -15,6 +15,8 @@ cxxfilt.c
 debug.c
 debug.c
 debug.h
+demanguse.c
+demanguse.h
 dlltool.c
 dlltool.h
 dllwrap.c
--
2.27.0
