15aefb655Srie /*
25aefb655Srie * CDDL HEADER START
35aefb655Srie *
45aefb655Srie * The contents of this file are subject to the terms of the
55aefb655Srie * Common Development and Distribution License (the "License").
65aefb655Srie * You may not use this file except in compliance with the License.
75aefb655Srie *
85aefb655Srie * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95aefb655Srie * or http://www.opensolaris.org/os/licensing.
105aefb655Srie * See the License for the specific language governing permissions
115aefb655Srie * and limitations under the License.
125aefb655Srie *
135aefb655Srie * When distributing Covered Code, include this CDDL HEADER in each
145aefb655Srie * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155aefb655Srie * If applicable, add the following below this CDDL HEADER, with the
165aefb655Srie * fields enclosed by brackets "[]" replaced with your own identifying
175aefb655Srie * information: Portions Copyright [yyyy] [name of copyright owner]
185aefb655Srie *
195aefb655Srie * CDDL HEADER END
205aefb655Srie */
215aefb655Srie
225aefb655Srie /*
23*23a1cceaSRoger A. Faulkner * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
245aefb655Srie */
255aefb655Srie
265aefb655Srie #include <stdio.h>
275aefb655Srie #include <demangle.h>
285aefb655Srie #include "_conv.h"
295aefb655Srie #include "demangle_msg.h"
305aefb655Srie
315aefb655Srie /*
325aefb655Srie * Demangle C++ symbols.
335aefb655Srie *
345aefb655Srie * This routine acts as a generic routine for use by liblddbg (and hence tools
355aefb655Srie * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
365aefb655Srie *
375aefb655Srie * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
385aefb655Srie * it's possible the buffer won't be big enough (DEMANGLE_ESPACE) so here we
395aefb655Srie * try to allocate bigger buffers. However, we place a limit on this buffer
405aefb655Srie * size for fear of a C++ error sending us into an infinit loop.
415aefb655Srie *
425aefb655Srie * NOTE. we create and use a common buffer for use by cplus_demangle(), thus
435aefb655Srie * each call to this routine will override the contents of any existing call.
445aefb655Srie * Normally this is sufficient for typical error diagnostics referencing one
455aefb655Srie * symbol. For those diagnostics using more than one symbol name, all but the
465aefb655Srie * last name must be copied to a temporary buffer (regardless of whether
475aefb655Srie * demangling occurred, as the process of attempting to demangle may damage the
485aefb655Srie * buffer). One model is:
495aefb655Srie *
505aefb655Srie * if ((_name1 = demangle(name1)) != name1) {
51*23a1cceaSRoger A. Faulkner * char * __name1 = strdupa(_name1);
525aefb655Srie * name1 = (const char *)__name1;
535aefb655Srie * }
545aefb655Srie * name2 = demangle(name2);
555aefb655Srie * eprintf(format, name1, name2);
565aefb655Srie */
575aefb655Srie #define SYM_MAX 1000
585aefb655Srie
595aefb655Srie const char *
conv_demangle_name(const char * name)605aefb655Srie conv_demangle_name(const char *name)
615aefb655Srie {
625aefb655Srie static char _str[SYM_MAX], *str = _str;
635aefb655Srie static size_t size = SYM_MAX;
645aefb655Srie static int again = 1;
655aefb655Srie static int (*fptr)() = 0;
665aefb655Srie int error;
675aefb655Srie
685aefb655Srie if (str == 0)
695aefb655Srie return (name);
705aefb655Srie
715aefb655Srie /*
725aefb655Srie * If we haven't located the demangler yet try now (we do this rather
735aefb655Srie * than maintain a static dependency on libdemangle as it's part of an
745aefb655Srie * optional package). Null the str element out to reject any other
755aefb655Srie * callers until this operation is complete - under ld.so.1 we can get
765aefb655Srie * into serious recursion without this.
775aefb655Srie */
785aefb655Srie if (fptr == 0) {
795aefb655Srie void *hdl;
805aefb655Srie
815aefb655Srie str = 0;
825aefb655Srie if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
835aefb655Srie !(fptr = (int (*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
845aefb655Srie return (name);
855aefb655Srie str = _str;
865aefb655Srie }
875aefb655Srie
885aefb655Srie if ((error = (*fptr)(name, str, size)) == 0)
895aefb655Srie return ((const char *)str);
905aefb655Srie
915aefb655Srie while ((error == DEMANGLE_ESPACE) && again) {
925aefb655Srie char *_str;
935aefb655Srie size_t _size = size;
945aefb655Srie
955aefb655Srie /*
965aefb655Srie * If we haven't allocated our maximum try incrementing the
975aefb655Srie * present buffer size. Use malloc() rather than realloc() so
985aefb655Srie * that we at least have the old buffer on failure.
995aefb655Srie */
1005aefb655Srie if (((_size += SYM_MAX) > (SYM_MAX * 4)) ||
1015aefb655Srie ((_str = malloc(_size)) == 0)) {
1025aefb655Srie again = 0;
1035aefb655Srie break;
1045aefb655Srie }
1055aefb655Srie if (size != SYM_MAX) {
1065aefb655Srie free(str);
1075aefb655Srie }
1085aefb655Srie str = _str;
1095aefb655Srie size = _size;
1105aefb655Srie
1115aefb655Srie if ((error = (*fptr)(name, str, size)) == 0)
1125aefb655Srie return ((const char *)str);
1135aefb655Srie }
1145aefb655Srie return (name);
1155aefb655Srie }
116