xref: /freebsd/contrib/elftoolchain/cxxfilt/cxxfilt.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * Copyright (c) 2009 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *
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.
25  */
26 
27 #include <sys/param.h>
28 
29 #include <capsicum_helpers.h>
30 #include <ctype.h>
31 #include <err.h>
32 #include <getopt.h>
33 #include <libelftc.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #include "_elftc.h"
39 
40 ELFTC_VCSID("$Id: cxxfilt.c 3499 2016-11-25 16:06:29Z emaste $");
41 
42 #define	STRBUFSZ	8192
43 
44 static int stripus = 0;
45 static int noparam = 0;
46 static int format = 0;
47 
48 enum options
49 {
50 	OPTION_HELP,
51 	OPTION_VERSION
52 };
53 
54 static struct option longopts[] =
55 {
56 	{"format", required_argument, NULL, 's'},
57 	{"help", no_argument, NULL, OPTION_HELP},
58 	{"no-params", no_argument, NULL, 'p'},
59 	{"no-strip-underscores", no_argument, NULL, 'n'},
60 	{"strip-underscores", no_argument, NULL, '_'},
61 	{"version", no_argument, NULL, 'V'},
62 	{NULL, 0, NULL, 0}
63 };
64 
65 static struct {
66 	const char *fname;
67 	int fvalue;
68 } flist[] = {
69 	{"auto", 0},
70 	{"arm", ELFTC_DEM_ARM},
71 	{"gnu", ELFTC_DEM_GNU2},
72 	{"gnu-v3", ELFTC_DEM_GNU3}
73 };
74 
75 #define	USAGE_MESSAGE	"\
76 Usage: %s [options] [encoded-names...]\n\
77   Translate C++ symbol names to human-readable form.\n\n\
78   Options:\n\
79   -_ | --strip-underscores     Remove leading underscores prior to decoding.\n\
80   -n | --no-strip-underscores  Do not remove leading underscores.\n\
81   -p | --no-params             (Accepted but ignored).\n\
82   -s SCHEME | --format=SCHEME  Select the encoding scheme to use.\n\
83                                Valid schemes are: 'arm', 'auto', 'gnu' and\n\
84                                'gnu-v3'.\n\
85   --help                       Print a help message.\n\
86   --version                    Print a version identifier and exit.\n"
87 
88 static void
89 usage(void)
90 {
91 
92 	(void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
93 	exit(1);
94 }
95 
96 static void
97 version(void)
98 {
99 	fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
100 	exit(0);
101 }
102 
103 static int
104 find_format(const char *fstr)
105 {
106 	int i;
107 
108 	for (i = 0; (size_t) i < sizeof(flist) / sizeof(flist[0]); i++) {
109 		if (!strcmp(fstr, flist[i].fname))
110 		    return (flist[i].fvalue);
111 	}
112 
113 	return (-1);
114 }
115 
116 static char *
117 demangle(char *name)
118 {
119 	static char dem[STRBUFSZ];
120 
121 	if (stripus && *name == '_')
122 		name++;
123 
124 	if (strlen(name) == 0)
125 		return (NULL);
126 
127 	if (elftc_demangle(name, dem, sizeof(dem), (unsigned) format) < 0)
128 		return (NULL);
129 
130 	return (dem);
131 }
132 
133 int
134 main(int argc, char **argv)
135 {
136 	char *dem, buf[STRBUFSZ];
137 	size_t p;
138 	int c, n, opt;
139 
140 	while ((opt = getopt_long(argc, argv, "_nps:V", longopts, NULL)) !=
141 	    -1) {
142 		switch (opt) {
143 		case '_':
144 			stripus = 1;
145 			break;
146 		case 'n':
147 			stripus = 0;
148 			break;
149 		case 'p':
150 			noparam = 1;
151 			break;
152 		case 's':
153 			if ((format = find_format(optarg)) < 0)
154 				errx(EXIT_FAILURE, "unsupported format: %s",
155 				    optarg);
156 			break;
157 		case 'V':
158 			version();
159 			/* NOT REACHED */
160 		case OPTION_HELP:
161 		default:
162 			usage();
163 			/* NOT REACHED */
164 		}
165 	}
166 
167 	argv += optind;
168 	argc -= optind;
169 
170 	if (caph_limit_stdio() < 0)
171 		err(EXIT_FAILURE, "failed to limit stdio rights");
172 	if (caph_enter() < 0)
173 		err(EXIT_FAILURE, "failed to enter capability mode");
174 
175 	if (*argv != NULL) {
176 		for (n = 0; n < argc; n++) {
177 			if ((dem = demangle(argv[n])) == NULL)
178 				printf("%s\n", argv[n]);
179 			else
180 				printf("%s\n", dem);
181 		}
182 	} else {
183 		p = 0;
184 		for (;;) {
185 			setvbuf(stdout, NULL, _IOLBF, 0);
186 			c = fgetc(stdin);
187 			if (c == EOF || !(isalnum(c) || strchr(".$_", c))) {
188 				if (p > 0) {
189 					buf[p] = '\0';
190 					if ((dem = demangle(buf)) == NULL)
191 						printf("%s", buf);
192 					else
193 						printf("%s", dem);
194 					p = 0;
195 				}
196 				if (c == EOF)
197 					break;
198 				putchar(c);
199 			} else {
200 				if ((size_t) p >= sizeof(buf) - 1)
201 					warnx("buffer overflowed");
202 				else
203 					buf[p++] = (char) c;
204 			}
205 
206 		}
207 	}
208 
209 	exit(0);
210 }
211