xref: /dflybsd-src/contrib/binutils-2.27/binutils/cxxfilt.c (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj /* Demangler for GNU C++ - main program
2*a9fa9459Szrj    Copyright (C) 1989-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj    Written by James Clark (jjc@jclark.uucp)
4*a9fa9459Szrj    Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
5*a9fa9459Szrj    Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
6*a9fa9459Szrj 
7*a9fa9459Szrj    This file is part of GNU Binutils.
8*a9fa9459Szrj 
9*a9fa9459Szrj    This program is free software; you can redistribute it and/or modify
10*a9fa9459Szrj    it under the terms of the GNU General Public License as published by
11*a9fa9459Szrj    the Free Software Foundation; either version 3 of the License, or (at
12*a9fa9459Szrj    your option) any later version.
13*a9fa9459Szrj 
14*a9fa9459Szrj    This program is distributed in the hope that it will be useful,
15*a9fa9459Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*a9fa9459Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*a9fa9459Szrj    GNU General Public License for more details.
18*a9fa9459Szrj 
19*a9fa9459Szrj    You should have received a copy of the GNU General Public License
20*a9fa9459Szrj    along with GCC; see the file COPYING.  If not, write to the Free
21*a9fa9459Szrj    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22*a9fa9459Szrj    02110-1301, USA.  */
23*a9fa9459Szrj 
24*a9fa9459Szrj #include "sysdep.h"
25*a9fa9459Szrj #include "bfd.h"
26*a9fa9459Szrj #include "libiberty.h"
27*a9fa9459Szrj #include "demangle.h"
28*a9fa9459Szrj #include "getopt.h"
29*a9fa9459Szrj #include "safe-ctype.h"
30*a9fa9459Szrj #include "bucomm.h"
31*a9fa9459Szrj 
32*a9fa9459Szrj static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
33*a9fa9459Szrj static int strip_underscore = TARGET_PREPENDS_UNDERSCORE;
34*a9fa9459Szrj 
35*a9fa9459Szrj static const struct option long_options[] =
36*a9fa9459Szrj {
37*a9fa9459Szrj   {"strip-underscore", no_argument, NULL, '_'},
38*a9fa9459Szrj   {"format", required_argument, NULL, 's'},
39*a9fa9459Szrj   {"help", no_argument, NULL, 'h'},
40*a9fa9459Szrj   {"no-params", no_argument, NULL, 'p'},
41*a9fa9459Szrj   {"no-strip-underscores", no_argument, NULL, 'n'},
42*a9fa9459Szrj   {"no-verbose", no_argument, NULL, 'i'},
43*a9fa9459Szrj   {"types", no_argument, NULL, 't'},
44*a9fa9459Szrj   {"version", no_argument, NULL, 'v'},
45*a9fa9459Szrj   {NULL, no_argument, NULL, 0}
46*a9fa9459Szrj };
47*a9fa9459Szrj 
48*a9fa9459Szrj static void
demangle_it(char * mangled_name)49*a9fa9459Szrj demangle_it (char *mangled_name)
50*a9fa9459Szrj {
51*a9fa9459Szrj   char *result;
52*a9fa9459Szrj   unsigned int skip_first = 0;
53*a9fa9459Szrj 
54*a9fa9459Szrj   /* _ and $ are sometimes found at the start of function names
55*a9fa9459Szrj      in assembler sources in order to distinguish them from other
56*a9fa9459Szrj      names (eg register names).  So skip them here.  */
57*a9fa9459Szrj   if (mangled_name[0] == '.' || mangled_name[0] == '$')
58*a9fa9459Szrj     ++skip_first;
59*a9fa9459Szrj   if (strip_underscore && mangled_name[skip_first] == '_')
60*a9fa9459Szrj     ++skip_first;
61*a9fa9459Szrj 
62*a9fa9459Szrj   result = cplus_demangle (mangled_name + skip_first, flags);
63*a9fa9459Szrj 
64*a9fa9459Szrj   if (result == NULL)
65*a9fa9459Szrj     printf ("%s", mangled_name);
66*a9fa9459Szrj   else
67*a9fa9459Szrj     {
68*a9fa9459Szrj       if (mangled_name[0] == '.')
69*a9fa9459Szrj 	putchar ('.');
70*a9fa9459Szrj       printf ("%s", result);
71*a9fa9459Szrj       free (result);
72*a9fa9459Szrj     }
73*a9fa9459Szrj }
74*a9fa9459Szrj 
75*a9fa9459Szrj static void
print_demangler_list(FILE * stream)76*a9fa9459Szrj print_demangler_list (FILE *stream)
77*a9fa9459Szrj {
78*a9fa9459Szrj   const struct demangler_engine *demangler;
79*a9fa9459Szrj 
80*a9fa9459Szrj   fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
81*a9fa9459Szrj 
82*a9fa9459Szrj   for (demangler = libiberty_demanglers + 1;
83*a9fa9459Szrj        demangler->demangling_style != unknown_demangling;
84*a9fa9459Szrj        ++demangler)
85*a9fa9459Szrj     fprintf (stream, ",%s", demangler->demangling_style_name);
86*a9fa9459Szrj 
87*a9fa9459Szrj   fprintf (stream, "}");
88*a9fa9459Szrj }
89*a9fa9459Szrj 
90*a9fa9459Szrj static void
usage(FILE * stream,int status)91*a9fa9459Szrj usage (FILE *stream, int status)
92*a9fa9459Szrj {
93*a9fa9459Szrj   fprintf (stream, "\
94*a9fa9459Szrj Usage: %s [options] [mangled names]\n", program_name);
95*a9fa9459Szrj   fprintf (stream, "\
96*a9fa9459Szrj Options are:\n\
97*a9fa9459Szrj   [-_|--strip-underscore]     Ignore first leading underscore%s\n",
98*a9fa9459Szrj 	   TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
99*a9fa9459Szrj   fprintf (stream, "\
100*a9fa9459Szrj   [-n|--no-strip-underscore]  Do not ignore a leading underscore%s\n",
101*a9fa9459Szrj 	   TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
102*a9fa9459Szrj   fprintf (stream, "\
103*a9fa9459Szrj   [-p|--no-params]            Do not display function arguments\n\
104*a9fa9459Szrj   [-i|--no-verbose]           Do not show implementation details (if any)\n\
105*a9fa9459Szrj   [-t|--types]                Also attempt to demangle type encodings\n\
106*a9fa9459Szrj   [-s|--format ");
107*a9fa9459Szrj   print_demangler_list (stream);
108*a9fa9459Szrj   fprintf (stream, "]\n");
109*a9fa9459Szrj 
110*a9fa9459Szrj   fprintf (stream, "\
111*a9fa9459Szrj   [@<file>]                   Read extra options from <file>\n\
112*a9fa9459Szrj   [-h|--help]                 Display this information\n\
113*a9fa9459Szrj   [-v|--version]              Show the version information\n\
114*a9fa9459Szrj Demangled names are displayed to stdout.\n\
115*a9fa9459Szrj If a name cannot be demangled it is just echoed to stdout.\n\
116*a9fa9459Szrj If no names are provided on the command line, stdin is read.\n");
117*a9fa9459Szrj   if (REPORT_BUGS_TO[0] && status == 0)
118*a9fa9459Szrj     fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
119*a9fa9459Szrj   exit (status);
120*a9fa9459Szrj }
121*a9fa9459Szrj 
122*a9fa9459Szrj /* Return the string of non-alnum characters that may occur
123*a9fa9459Szrj    as a valid symbol component, in the standard assembler symbol
124*a9fa9459Szrj    syntax.  */
125*a9fa9459Szrj 
126*a9fa9459Szrj static const char *
standard_symbol_characters(void)127*a9fa9459Szrj standard_symbol_characters (void)
128*a9fa9459Szrj {
129*a9fa9459Szrj   return "_$.";
130*a9fa9459Szrj }
131*a9fa9459Szrj 
132*a9fa9459Szrj /* Return the string of non-alnum characters that may occur
133*a9fa9459Szrj    as a valid symbol name component in an HP object file.
134*a9fa9459Szrj 
135*a9fa9459Szrj    Note that, since HP's compiler generates object code straight from
136*a9fa9459Szrj    C++ source, without going through an assembler, its mangled
137*a9fa9459Szrj    identifiers can use all sorts of characters that no assembler would
138*a9fa9459Szrj    tolerate, so the alphabet this function creates is a little odd.
139*a9fa9459Szrj    Here are some sample mangled identifiers offered by HP:
140*a9fa9459Szrj 
141*a9fa9459Szrj 	typeid*__XT24AddressIndExpClassMember_
142*a9fa9459Szrj 	[Vftptr]key:__dt__32OrdinaryCompareIndExpClassMemberFv
143*a9fa9459Szrj 	__ct__Q2_9Elf64_Dyn18{unnamed.union.#1}Fv
144*a9fa9459Szrj 
145*a9fa9459Szrj    This still seems really weird to me, since nowhere else in this
146*a9fa9459Szrj    file is there anything to recognize curly brackets, parens, etc.
147*a9fa9459Szrj    I've talked with Srikanth <srikanth@cup.hp.com>, and he assures me
148*a9fa9459Szrj    this is right, but I still strongly suspect that there's a
149*a9fa9459Szrj    misunderstanding here.
150*a9fa9459Szrj 
151*a9fa9459Szrj    If we decide it's better for c++filt to use HP's assembler syntax
152*a9fa9459Szrj    to scrape identifiers out of its input, here's the definition of
153*a9fa9459Szrj    the symbol name syntax from the HP assembler manual:
154*a9fa9459Szrj 
155*a9fa9459Szrj        Symbols are composed of uppercase and lowercase letters, decimal
156*a9fa9459Szrj        digits, dollar symbol, period (.), ampersand (&), pound sign(#) and
157*a9fa9459Szrj        underscore (_). A symbol can begin with a letter, digit underscore or
158*a9fa9459Szrj        dollar sign. If a symbol begins with a digit, it must contain a
159*a9fa9459Szrj        non-digit character.
160*a9fa9459Szrj 
161*a9fa9459Szrj    So have fun.  */
162*a9fa9459Szrj static const char *
hp_symbol_characters(void)163*a9fa9459Szrj hp_symbol_characters (void)
164*a9fa9459Szrj {
165*a9fa9459Szrj   return "_$.<>#,*&[]:(){}";
166*a9fa9459Szrj }
167*a9fa9459Szrj 
168*a9fa9459Szrj extern int main (int, char **);
169*a9fa9459Szrj 
170*a9fa9459Szrj int
main(int argc,char ** argv)171*a9fa9459Szrj main (int argc, char **argv)
172*a9fa9459Szrj {
173*a9fa9459Szrj   int c;
174*a9fa9459Szrj   const char *valid_symbols;
175*a9fa9459Szrj   enum demangling_styles style = auto_demangling;
176*a9fa9459Szrj 
177*a9fa9459Szrj   program_name = argv[0];
178*a9fa9459Szrj   xmalloc_set_program_name (program_name);
179*a9fa9459Szrj   bfd_set_error_program_name (program_name);
180*a9fa9459Szrj 
181*a9fa9459Szrj   expandargv (&argc, &argv);
182*a9fa9459Szrj 
183*a9fa9459Szrj   while ((c = getopt_long (argc, argv, "_hinps:tv", long_options, (int *) 0)) != EOF)
184*a9fa9459Szrj     {
185*a9fa9459Szrj       switch (c)
186*a9fa9459Szrj 	{
187*a9fa9459Szrj 	case '?':
188*a9fa9459Szrj 	  usage (stderr, 1);
189*a9fa9459Szrj 	  break;
190*a9fa9459Szrj 	case 'h':
191*a9fa9459Szrj 	  usage (stdout, 0);
192*a9fa9459Szrj 	case 'n':
193*a9fa9459Szrj 	  strip_underscore = 0;
194*a9fa9459Szrj 	  break;
195*a9fa9459Szrj 	case 'p':
196*a9fa9459Szrj 	  flags &= ~ DMGL_PARAMS;
197*a9fa9459Szrj 	  break;
198*a9fa9459Szrj 	case 't':
199*a9fa9459Szrj 	  flags |= DMGL_TYPES;
200*a9fa9459Szrj 	  break;
201*a9fa9459Szrj 	case 'i':
202*a9fa9459Szrj 	  flags &= ~ DMGL_VERBOSE;
203*a9fa9459Szrj 	  break;
204*a9fa9459Szrj 	case 'v':
205*a9fa9459Szrj 	  print_version ("c++filt");
206*a9fa9459Szrj 	  return 0;
207*a9fa9459Szrj 	case '_':
208*a9fa9459Szrj 	  strip_underscore = 1;
209*a9fa9459Szrj 	  break;
210*a9fa9459Szrj 	case 's':
211*a9fa9459Szrj 	  style = cplus_demangle_name_to_style (optarg);
212*a9fa9459Szrj 	  if (style == unknown_demangling)
213*a9fa9459Szrj 	    {
214*a9fa9459Szrj 	      fprintf (stderr, "%s: unknown demangling style `%s'\n",
215*a9fa9459Szrj 		       program_name, optarg);
216*a9fa9459Szrj 	      return 1;
217*a9fa9459Szrj 	    }
218*a9fa9459Szrj 	  cplus_demangle_set_style (style);
219*a9fa9459Szrj 	  break;
220*a9fa9459Szrj 	}
221*a9fa9459Szrj     }
222*a9fa9459Szrj 
223*a9fa9459Szrj   if (optind < argc)
224*a9fa9459Szrj     {
225*a9fa9459Szrj       for ( ; optind < argc; optind++)
226*a9fa9459Szrj 	{
227*a9fa9459Szrj 	  demangle_it (argv[optind]);
228*a9fa9459Szrj 	  putchar ('\n');
229*a9fa9459Szrj 	}
230*a9fa9459Szrj 
231*a9fa9459Szrj       return 0;
232*a9fa9459Szrj     }
233*a9fa9459Szrj 
234*a9fa9459Szrj   switch (current_demangling_style)
235*a9fa9459Szrj     {
236*a9fa9459Szrj     case gnu_demangling:
237*a9fa9459Szrj     case lucid_demangling:
238*a9fa9459Szrj     case arm_demangling:
239*a9fa9459Szrj     case java_demangling:
240*a9fa9459Szrj     case edg_demangling:
241*a9fa9459Szrj     case gnat_demangling:
242*a9fa9459Szrj     case gnu_v3_demangling:
243*a9fa9459Szrj     case dlang_demangling:
244*a9fa9459Szrj     case auto_demangling:
245*a9fa9459Szrj       valid_symbols = standard_symbol_characters ();
246*a9fa9459Szrj       break;
247*a9fa9459Szrj     case hp_demangling:
248*a9fa9459Szrj       valid_symbols = hp_symbol_characters ();
249*a9fa9459Szrj       break;
250*a9fa9459Szrj     default:
251*a9fa9459Szrj       /* Folks should explicitly indicate the appropriate alphabet for
252*a9fa9459Szrj 	 each demangling.  Providing a default would allow the
253*a9fa9459Szrj 	 question to go unconsidered.  */
254*a9fa9459Szrj       fatal ("Internal error: no symbol alphabet for current style");
255*a9fa9459Szrj     }
256*a9fa9459Szrj 
257*a9fa9459Szrj   for (;;)
258*a9fa9459Szrj     {
259*a9fa9459Szrj       static char mbuffer[32767];
260*a9fa9459Szrj       unsigned i = 0;
261*a9fa9459Szrj 
262*a9fa9459Szrj       c = getchar ();
263*a9fa9459Szrj       /* Try to read a mangled name.  */
264*a9fa9459Szrj       while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
265*a9fa9459Szrj 	{
266*a9fa9459Szrj 	  if (i >= sizeof (mbuffer) - 1)
267*a9fa9459Szrj 	    break;
268*a9fa9459Szrj 	  mbuffer[i++] = c;
269*a9fa9459Szrj 	  c = getchar ();
270*a9fa9459Szrj 	}
271*a9fa9459Szrj 
272*a9fa9459Szrj       if (i > 0)
273*a9fa9459Szrj 	{
274*a9fa9459Szrj 	  mbuffer[i] = 0;
275*a9fa9459Szrj 	  demangle_it (mbuffer);
276*a9fa9459Szrj 	}
277*a9fa9459Szrj 
278*a9fa9459Szrj       if (c == EOF)
279*a9fa9459Szrj 	break;
280*a9fa9459Szrj 
281*a9fa9459Szrj       /* Echo the whitespace characters so that the output looks
282*a9fa9459Szrj 	 like the input, only with the mangled names demangled.  */
283*a9fa9459Szrj       putchar (c);
284*a9fa9459Szrj       if (c == '\n')
285*a9fa9459Szrj 	fflush (stdout);
286*a9fa9459Szrj     }
287*a9fa9459Szrj 
288*a9fa9459Szrj   fflush (stdout);
289*a9fa9459Szrj   return 0;
290*a9fa9459Szrj }
291