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