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