xref: /onnv-gate/usr/src/cmd/sgs/libconv/common/demangle.c (revision 13093:48f2dbca79a2)
11618Srie /*
21618Srie  * CDDL HEADER START
31618Srie  *
41618Srie  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
71618Srie  *
81618Srie  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91618Srie  * or http://www.opensolaris.org/os/licensing.
101618Srie  * See the License for the specific language governing permissions
111618Srie  * and limitations under the License.
121618Srie  *
131618Srie  * When distributing Covered Code, include this CDDL HEADER in each
141618Srie  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151618Srie  * If applicable, add the following below this CDDL HEADER, with the
161618Srie  * fields enclosed by brackets "[]" replaced with your own identifying
171618Srie  * information: Portions Copyright [yyyy] [name of copyright owner]
181618Srie  *
191618Srie  * CDDL HEADER END
201618Srie  */
211618Srie 
221618Srie /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
241618Srie  */
251618Srie 
261618Srie #include	<stdio.h>
271618Srie #include	<demangle.h>
281618Srie #include	"_conv.h"
291618Srie #include	"demangle_msg.h"
301618Srie 
311618Srie /*
321618Srie  * Demangle C++ symbols.
331618Srie  *
341618Srie  * This routine acts as a generic routine for use by liblddbg (and hence tools
351618Srie  * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
361618Srie  *
371618Srie  * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
381618Srie  * it's possible the buffer won't be big enough (DEMANGLE_ESPACE) so here we
391618Srie  * try to allocate bigger buffers.  However, we place a limit on this buffer
401618Srie  * size for fear of a C++ error sending us into an infinit loop.
411618Srie  *
421618Srie  * NOTE. we create and use a common buffer for use by cplus_demangle(), thus
431618Srie  * each call to this routine will override the contents of any existing call.
441618Srie  * Normally this is sufficient for typical error diagnostics referencing one
451618Srie  * symbol.  For those diagnostics using more than one symbol name, all but the
461618Srie  * last name must be copied to a temporary buffer (regardless of whether
471618Srie  * demangling occurred, as the process of attempting to demangle may damage the
481618Srie  * buffer).  One model is:
491618Srie  *
501618Srie  *	if ((_name1 = demangle(name1)) != name1) {
51*13093SRoger.Faulkner@Oracle.COM  *		char *	__name1 = strdupa(_name1);
521618Srie  *		name1 = (const char *)__name1;
531618Srie  *	}
541618Srie  *	name2 = demangle(name2);
551618Srie  *	eprintf(format, name1, name2);
561618Srie  */
571618Srie #define	SYM_MAX	1000
581618Srie 
591618Srie const char *
conv_demangle_name(const char * name)601618Srie conv_demangle_name(const char *name)
611618Srie {
621618Srie 	static char	_str[SYM_MAX], *str = _str;
631618Srie 	static size_t	size = SYM_MAX;
641618Srie 	static int	again = 1;
651618Srie 	static int	(*fptr)() = 0;
661618Srie 	int		error;
671618Srie 
681618Srie 	if (str == 0)
691618Srie 		return (name);
701618Srie 
711618Srie 	/*
721618Srie 	 * If we haven't located the demangler yet try now (we do this rather
731618Srie 	 * than maintain a static dependency on libdemangle as it's part of an
741618Srie 	 * optional package).  Null the str element out to reject any other
751618Srie 	 * callers until this operation is complete - under ld.so.1 we can get
761618Srie 	 * into serious recursion without this.
771618Srie 	 */
781618Srie 	if (fptr == 0) {
791618Srie 		void	*hdl;
801618Srie 
811618Srie 		str = 0;
821618Srie 		if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
831618Srie 		    !(fptr = (int (*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
841618Srie 			return (name);
851618Srie 		str = _str;
861618Srie 	}
871618Srie 
881618Srie 	if ((error = (*fptr)(name, str, size)) == 0)
891618Srie 		return ((const char *)str);
901618Srie 
911618Srie 	while ((error == DEMANGLE_ESPACE) && again) {
921618Srie 		char	*_str;
931618Srie 		size_t	_size = size;
941618Srie 
951618Srie 		/*
961618Srie 		 * If we haven't allocated our maximum try incrementing the
971618Srie 		 * present buffer size. Use malloc() rather than realloc() so
981618Srie 		 * that we at least have the old buffer on failure.
991618Srie 		 */
1001618Srie 		if (((_size += SYM_MAX) > (SYM_MAX * 4)) ||
1011618Srie 		    ((_str = malloc(_size)) == 0)) {
1021618Srie 			again = 0;
1031618Srie 			break;
1041618Srie 		}
1051618Srie 		if (size != SYM_MAX) {
1061618Srie 			free(str);
1071618Srie 		}
1081618Srie 		str = _str;
1091618Srie 		size = _size;
1101618Srie 
1111618Srie 		if ((error = (*fptr)(name, str, size)) == 0)
1121618Srie 			return ((const char *)str);
1131618Srie 	}
1141618Srie 	return (name);
1151618Srie }
116