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