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