xref: /onnv-gate/usr/src/cmd/sgs/libelf/misc/demangle.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
24*0Sstevel@tonic-gate  *	  All Rights Reserved
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  *	Copyright (c) 1998 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  *	All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.2	*/
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <ctype.h>
33*0Sstevel@tonic-gate #include <setjmp.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <thread.h>
37*0Sstevel@tonic-gate #include <note.h>
38*0Sstevel@tonic-gate #include "elf_dem.h"
39*0Sstevel@tonic-gate #include "String.h"
40*0Sstevel@tonic-gate #include "msg.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /* The variable "hold" contains the pointer to the array initially
43*0Sstevel@tonic-gate  * handed to demangle.  It is returned if it is not possible to
44*0Sstevel@tonic-gate  * demangle the string.  NULL is returned if a memory allocation
45*0Sstevel@tonic-gate  * problem is encountered.  Thus one can do the following:
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * char *mn = "Some mangled name";
48*0Sstevel@tonic-gate  * char *dm = mangle(mn);
49*0Sstevel@tonic-gate  * if (dm == NULL)
50*0Sstevel@tonic-gate  *	printf("allocation error\n");
51*0Sstevel@tonic-gate  * else if (dm == mn)
52*0Sstevel@tonic-gate  * 	printf("name could not be demangled\n");
53*0Sstevel@tonic-gate  * else
54*0Sstevel@tonic-gate  *	printf("demangled name is: %s\n",dm);
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate static char *hold;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /* this String is the working buffer for the demangle
59*0Sstevel@tonic-gate  * routine.  A pointer into this String is returned
60*0Sstevel@tonic-gate  * from demangle when it is possible to demangle the
61*0Sstevel@tonic-gate  * String.  For this reason, the pointer should not
62*0Sstevel@tonic-gate  * be saved between calls of demangle(), nor freed.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate static String *s = 0;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static int
getint(c)67*0Sstevel@tonic-gate getint(c)
68*0Sstevel@tonic-gate char **c;
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	return strtol(*c, c, 10);
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /* If a mangled name has a __
74*0Sstevel@tonic-gate  * that is not at the very beginning
75*0Sstevel@tonic-gate  * of the string, then this routine
76*0Sstevel@tonic-gate  * is called to demangle that part
77*0Sstevel@tonic-gate  * of the name.  All overloaded functions,
78*0Sstevel@tonic-gate  * and class members fall into this category.
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  * c should start with two underscores followed by a non-zero digit or an F.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate static char *
second(c)83*0Sstevel@tonic-gate second(c)
84*0Sstevel@tonic-gate char *c;
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	int n;
87*0Sstevel@tonic-gate 	if(strncmp(c, MSG_ORIG(MSG_STR_DBLUNDBAR), 2))
88*0Sstevel@tonic-gate 		return hold;
89*0Sstevel@tonic-gate 	c += 2;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	if (!(isdigit(*c) || *c == 'F'))
92*0Sstevel@tonic-gate 		return hold;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (isdigit(*c)) {
95*0Sstevel@tonic-gate 		/* a member */
96*0Sstevel@tonic-gate 		n = getint(&c);
97*0Sstevel@tonic-gate 		if (n == 0 || (int) strlen(c) < n)
98*0Sstevel@tonic-gate 			return hold;
99*0Sstevel@tonic-gate 		s = prep_String(MSG_ORIG(MSG_STR_DBLCOL), s);
100*0Sstevel@tonic-gate 		s = nprep_String(c,s,n);
101*0Sstevel@tonic-gate 		c += n;
102*0Sstevel@tonic-gate 	}
103*0Sstevel@tonic-gate 	if(*c == 'F') {
104*0Sstevel@tonic-gate 		/* an overloaded function */
105*0Sstevel@tonic-gate 		switch (*++c) {
106*0Sstevel@tonic-gate 		case '\0':
107*0Sstevel@tonic-gate 			return hold;
108*0Sstevel@tonic-gate 		case 'v':
109*0Sstevel@tonic-gate 			s = app_String(s, MSG_ORIG(MSG_STR_OPENCLOSEPAR));
110*0Sstevel@tonic-gate 			break;
111*0Sstevel@tonic-gate 		default:
112*0Sstevel@tonic-gate 			if(demangle_doargs(&s,c) < 0)
113*0Sstevel@tonic-gate 				return hold;
114*0Sstevel@tonic-gate 		}
115*0Sstevel@tonic-gate 	}
116*0Sstevel@tonic-gate 	return PTR(s);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate char *
demangle(c)120*0Sstevel@tonic-gate demangle(c)
121*0Sstevel@tonic-gate char *c;
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate 	register int i = 0;
124*0Sstevel@tonic-gate 	extern jmp_buf jbuf;
125*0Sstevel@tonic-gate 	static mutex_t	mlock = DEFAULTMUTEX;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	NOTE(MUTEX_PROTECTS_DATA(mlock, String))
128*0Sstevel@tonic-gate 	(void) mutex_lock(&mlock);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	if (setjmp(jbuf)) {
131*0Sstevel@tonic-gate 		(void) mutex_unlock(&mlock);
132*0Sstevel@tonic-gate 		return 0;
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	hold = c;
136*0Sstevel@tonic-gate 	s = mk_String(s);
137*0Sstevel@tonic-gate 	s = set_String(s, MSG_ORIG(MSG_STR_EMPTY));
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if(c == 0 || *c == 0) {
140*0Sstevel@tonic-gate 		c = hold;
141*0Sstevel@tonic-gate 		(void) mutex_unlock(&mlock);
142*0Sstevel@tonic-gate 		return c;
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if(strncmp(c, MSG_ORIG(MSG_STR_DBLUNDBAR), 2) != 0) {
146*0Sstevel@tonic-gate 		/* If a name does not begin with a __
147*0Sstevel@tonic-gate 		 * but it does contain one, it is either
148*0Sstevel@tonic-gate 		 * a member or an overloaded function.
149*0Sstevel@tonic-gate 		 */
150*0Sstevel@tonic-gate 		while(c[i] && strncmp(c+i, MSG_ORIG(MSG_STR_DBLUNDBAR), 2))
151*0Sstevel@tonic-gate 			i++;
152*0Sstevel@tonic-gate 		if (c[i]) {
153*0Sstevel@tonic-gate 			/* Advance to first non-underscore */
154*0Sstevel@tonic-gate 			while (c[i+2] == '_')
155*0Sstevel@tonic-gate 				i++;
156*0Sstevel@tonic-gate 		}
157*0Sstevel@tonic-gate 		if(strncmp(c+i, MSG_ORIG(MSG_STR_DBLUNDBAR), 2) == 0) {
158*0Sstevel@tonic-gate 			/* Copy the simple name */
159*0Sstevel@tonic-gate 			s = napp_String(s,c,i);
160*0Sstevel@tonic-gate 			/* Process the signature */
161*0Sstevel@tonic-gate 			c = second(c+i);
162*0Sstevel@tonic-gate 			(void) mutex_unlock(&mlock);
163*0Sstevel@tonic-gate 			return c;
164*0Sstevel@tonic-gate 		} else {
165*0Sstevel@tonic-gate 			c = hold;
166*0Sstevel@tonic-gate 			(void) mutex_unlock(&mlock);
167*0Sstevel@tonic-gate 			return c;
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 	} else {
170*0Sstevel@tonic-gate 		const char *	x;
171*0Sstevel@tonic-gate 		int		oplen;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 		c += 2;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		/* For automatic variables, or internal static
176*0Sstevel@tonic-gate 		 * variables, a __(number) is prepended to the
177*0Sstevel@tonic-gate 		 * name.  If this is encountered, strip this off
178*0Sstevel@tonic-gate 		 * and return.
179*0Sstevel@tonic-gate 		 */
180*0Sstevel@tonic-gate 		if(isdigit(*c)) {
181*0Sstevel@tonic-gate 			while(isdigit(*c))
182*0Sstevel@tonic-gate 				c++;
183*0Sstevel@tonic-gate 			(void) mutex_unlock(&mlock);
184*0Sstevel@tonic-gate 			return c;
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 		/* Handle operator functions -- this
188*0Sstevel@tonic-gate 		 * automatically calls second, since
189*0Sstevel@tonic-gate 		 * all operator functions are overloaded.
190*0Sstevel@tonic-gate 		 */
191*0Sstevel@tonic-gate 		if(x = findop(c, &oplen)) {
192*0Sstevel@tonic-gate 			s = app_String(s, MSG_ORIG(MSG_STR_OPERATOR_1));
193*0Sstevel@tonic-gate 			s = app_String(s,x);
194*0Sstevel@tonic-gate 			c += oplen;
195*0Sstevel@tonic-gate 			c = second(c);
196*0Sstevel@tonic-gate 			(void) mutex_unlock(&mlock);
197*0Sstevel@tonic-gate 			return c;
198*0Sstevel@tonic-gate 		}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		/* Operator cast does not fit the mould
201*0Sstevel@tonic-gate 		 * of the other operators.  Its type name
202*0Sstevel@tonic-gate 		 * is encoded.  The cast function must
203*0Sstevel@tonic-gate 		 * take a void as an argument.
204*0Sstevel@tonic-gate 		 */
205*0Sstevel@tonic-gate 		if(strncmp(c, MSG_ORIG(MSG_STR_OP), 2) == 0) {
206*0Sstevel@tonic-gate 			int r;
207*0Sstevel@tonic-gate 			s = app_String(s, MSG_ORIG(MSG_STR_OPERATOR_2));
208*0Sstevel@tonic-gate 			c += 2;
209*0Sstevel@tonic-gate 			r = demangle_doarg(&s,c);
210*0Sstevel@tonic-gate 			if(r < 0) {
211*0Sstevel@tonic-gate 				c = hold;
212*0Sstevel@tonic-gate 				(void) mutex_unlock(&mlock);
213*0Sstevel@tonic-gate 				return c;
214*0Sstevel@tonic-gate 			}
215*0Sstevel@tonic-gate 			c += r;
216*0Sstevel@tonic-gate 			c = second(c);
217*0Sstevel@tonic-gate 			(void) mutex_unlock(&mlock);
218*0Sstevel@tonic-gate 			return c;
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		/* Constructors and Destructors are also
222*0Sstevel@tonic-gate 		 * a special case of operator name.  Note
223*0Sstevel@tonic-gate 		 * that the destructor, while overloaded,
224*0Sstevel@tonic-gate 		 * must always take the same arguments --
225*0Sstevel@tonic-gate 		 * none.
226*0Sstevel@tonic-gate 		 */
227*0Sstevel@tonic-gate 		if ((*c == 'c' || *c == 'd') && strncmp(c+1,
228*0Sstevel@tonic-gate 		    MSG_ORIG(MSG_STR_TDBLUNDBAR), 3) == 0) {
229*0Sstevel@tonic-gate 			int n;
230*0Sstevel@tonic-gate 			char *c2 = c+2;
231*0Sstevel@tonic-gate 			char cx = c[0];
232*0Sstevel@tonic-gate 			c += 4;
233*0Sstevel@tonic-gate 			n = getint(&c);
234*0Sstevel@tonic-gate 			if(n == 0) {
235*0Sstevel@tonic-gate 				c = hold;
236*0Sstevel@tonic-gate 				(void) mutex_unlock(&mlock);
237*0Sstevel@tonic-gate 				return c;
238*0Sstevel@tonic-gate 			}
239*0Sstevel@tonic-gate 			s = napp_String(s,c,n);
240*0Sstevel@tonic-gate 			if(cx == 'd')
241*0Sstevel@tonic-gate 				s = prep_String(MSG_ORIG(MSG_STR_TILDE), s);
242*0Sstevel@tonic-gate 			c = second(c2);
243*0Sstevel@tonic-gate 			(void) mutex_unlock(&mlock);
244*0Sstevel@tonic-gate 			return c;
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 		c = hold;
247*0Sstevel@tonic-gate 		(void) mutex_unlock(&mlock);
248*0Sstevel@tonic-gate 		return c;
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate }
251