xref: /titanic_51/usr/src/cmd/sgs/libconv/common/demangle.c (revision dd4eeefdb8e4583c47e28a7f315db6087931ef06)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<stdio.h>
29 #include	<demangle.h>
30 #include	"_conv.h"
31 #include	"demangle_msg.h"
32 
33 /*
34  * Demangle C++ symbols.
35  *
36  * This routine acts as a generic routine for use by liblddbg (and hence tools
37  * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
38  *
39  * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
40  * it's possible the buffer won't be big enough (DEMANGLE_ESPACE) so here we
41  * try to allocate bigger buffers.  However, we place a limit on this buffer
42  * size for fear of a C++ error sending us into an infinit loop.
43  *
44  * NOTE. we create and use a common buffer for use by cplus_demangle(), thus
45  * each call to this routine will override the contents of any existing call.
46  * Normally this is sufficient for typical error diagnostics referencing one
47  * symbol.  For those diagnostics using more than one symbol name, all but the
48  * last name must be copied to a temporary buffer (regardless of whether
49  * demangling occurred, as the process of attempting to demangle may damage the
50  * buffer).  One model is:
51  *
52  *	if ((_name1 = demangle(name1)) != name1) {
53  *		char *	__name1 = alloca(strlen(_name1) + 1);
54  *		(void) strcpy(__name1, _name1);
55  *		name1 = (const char *)__name1;
56  *	}
57  *	name2 = demangle(name2);
58  *	eprintf(format, name1, name2);
59  */
60 #define	SYM_MAX	1000
61 
62 const char *
63 conv_demangle_name(const char *name)
64 {
65 	static char	_str[SYM_MAX], *str = _str;
66 	static size_t	size = SYM_MAX;
67 	static int	again = 1;
68 	static int	(*fptr)() = 0;
69 	int		error;
70 
71 	if (str == 0)
72 		return (name);
73 
74 	/*
75 	 * If we haven't located the demangler yet try now (we do this rather
76 	 * than maintain a static dependency on libdemangle as it's part of an
77 	 * optional package).  Null the str element out to reject any other
78 	 * callers until this operation is complete - under ld.so.1 we can get
79 	 * into serious recursion without this.
80 	 */
81 	if (fptr == 0) {
82 		void	*hdl;
83 
84 		str = 0;
85 		if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
86 		    !(fptr = (int (*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
87 			return (name);
88 		str = _str;
89 	}
90 
91 	if ((error = (*fptr)(name, str, size)) == 0)
92 		return ((const char *)str);
93 
94 	while ((error == DEMANGLE_ESPACE) && again) {
95 		char	*_str;
96 		size_t	_size = size;
97 
98 		/*
99 		 * If we haven't allocated our maximum try incrementing the
100 		 * present buffer size. Use malloc() rather than realloc() so
101 		 * that we at least have the old buffer on failure.
102 		 */
103 		if (((_size += SYM_MAX) > (SYM_MAX * 4)) ||
104 		    ((_str = malloc(_size)) == 0)) {
105 			again = 0;
106 			break;
107 		}
108 		if (size != SYM_MAX) {
109 			free(str);
110 		}
111 		str = _str;
112 		size = _size;
113 
114 		if ((error = (*fptr)(name, str, size)) == 0)
115 			return ((const char *)str);
116 	}
117 	return (name);
118 }
119