cxxfilt.c 8.1 KB

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