2 * Copyright (c) 2009 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/param.h>
38 ELFTC_VCSID("$Id: cxxfilt.c 3174 2015-03-27 17:13:41Z emaste $");
42 static int stripus = 0;
43 static int noparam = 0;
44 static int format = 0;
52 static struct option longopts[] =
54 {"format", required_argument, NULL, 's'},
55 {"help", no_argument, NULL, OPTION_HELP},
56 {"no-params", no_argument, NULL, 'p'},
57 {"no-strip-underscores", no_argument, NULL, 'n'},
58 {"strip-underscores", no_argument, NULL, '_'},
59 {"version", no_argument, NULL, 'V'},
68 {"arm", ELFTC_DEM_ARM},
69 {"gnu", ELFTC_DEM_GNU2},
70 {"gnu-v3", ELFTC_DEM_GNU3}
73 #define USAGE_MESSAGE "\
74 Usage: %s [options] [encoded-names...]\n\
75 Translate C++ symbol names to human-readable form.\n\n\
77 -_ | --strip-underscores Remove leading underscores prior to decoding.\n\
78 -n | --no-strip-underscores Do not remove leading underscores.\n\
79 -p | --no-params (Accepted but ignored).\n\
80 -s SCHEME | --format=SCHEME Select the encoding scheme to use.\n\
81 Valid schemes are: 'arm', 'auto', 'gnu' and\n\
83 --help Print a help message.\n\
84 --version Print a version identifier and exit.\n"
90 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
97 fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
102 find_format(const char *fstr)
106 for (i = 0; (size_t) i < sizeof(flist) / sizeof(flist[0]); i++) {
107 if (!strcmp(fstr, flist[i].fname))
108 return (flist[i].fvalue);
115 demangle(char *name, int strict, int *pos)
117 static char dem[STRBUFSZ];
121 if (stripus && *name == '_') {
122 strncpy(nb, name + 1, sizeof(nb) - 1);
125 strncpy(nb, name, sizeof(nb) - 1);
128 nb[sizeof(nb) - 1] = '\0';
134 while (elftc_demangle(nb, dem, sizeof(dem), format) < 0) {
135 if (!strict && p > 1) {
143 *pos = t ? p + 1 : p;
149 main(int argc, char **argv)
151 char *dem, buf[STRBUFSZ];
154 while ((opt = getopt_long(argc, argv, "_nps:V", longopts, NULL)) !=
167 if ((format = find_format(optarg)) < 0)
168 errx(EXIT_FAILURE, "unsupported format: %s",
185 for (i = 0; i < argc; i++) {
186 if ((dem = demangle(argv[i], 1, NULL)) == NULL)
187 fprintf(stderr, "Failed: %s\n", argv[i]);
195 if (c == EOF || !isprint(c) || strchr(" \t\n", c)) {
198 if ((dem = demangle(buf, 0, &s)) ==
203 for (i = s; i < p; i++)
210 if (isprint(c) || strchr(" \t\n", c))
213 if ((size_t) p >= sizeof(buf) - 1)
214 warnx("buffer overflowed");