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 /*
2323a1cceaSRoger A. Faulkner * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24*4226f635SJason King * Copyright 2018, Joyent, Inc.
255aefb655Srie */
265aefb655Srie
275aefb655Srie #include <stdio.h>
28*4226f635SJason King #include <demangle-sys.h>
295aefb655Srie #include "_conv.h"
305aefb655Srie #include "demangle_msg.h"
315aefb655Srie
325aefb655Srie /*
335aefb655Srie * Demangle C++ symbols.
345aefb655Srie *
355aefb655Srie * This routine acts as a generic routine for use by liblddbg (and hence tools
365aefb655Srie * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
375aefb655Srie *
385aefb655Srie * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
395aefb655Srie * it's possible the buffer won't be big enough (DEMANGLE_ESPACE) so here we
405aefb655Srie * try to allocate bigger buffers. However, we place a limit on this buffer
415aefb655Srie * size for fear of a C++ error sending us into an infinit loop.
425aefb655Srie *
435aefb655Srie * NOTE. we create and use a common buffer for use by cplus_demangle(), thus
445aefb655Srie * each call to this routine will override the contents of any existing call.
455aefb655Srie * Normally this is sufficient for typical error diagnostics referencing one
465aefb655Srie * symbol. For those diagnostics using more than one symbol name, all but the
475aefb655Srie * last name must be copied to a temporary buffer (regardless of whether
485aefb655Srie * demangling occurred, as the process of attempting to demangle may damage the
495aefb655Srie * buffer). One model is:
505aefb655Srie *
515aefb655Srie * if ((_name1 = demangle(name1)) != name1) {
5223a1cceaSRoger A. Faulkner * char * __name1 = strdupa(_name1);
535aefb655Srie * name1 = (const char *)__name1;
545aefb655Srie * }
555aefb655Srie * name2 = demangle(name2);
565aefb655Srie * eprintf(format, name1, name2);
575aefb655Srie */
585aefb655Srie #define SYM_MAX 1000
595aefb655Srie
605aefb655Srie const char *
conv_demangle_name(const char * name)615aefb655Srie conv_demangle_name(const char *name)
625aefb655Srie {
63*4226f635SJason King static char *(*fptr)() = 0;
64*4226f635SJason King static volatile int loading = 0;
65*4226f635SJason King char *d;
665aefb655Srie
67*4226f635SJason King if (loading)
685aefb655Srie return (name);
695aefb655Srie
705aefb655Srie /*
715aefb655Srie * If we haven't located the demangler yet try now (we do this rather
725aefb655Srie * than maintain a static dependency on libdemangle as it's part of an
735aefb655Srie * optional package). Null the str element out to reject any other
745aefb655Srie * callers until this operation is complete - under ld.so.1 we can get
755aefb655Srie * into serious recursion without this.
765aefb655Srie */
775aefb655Srie if (fptr == 0) {
785aefb655Srie void *hdl;
795aefb655Srie
80*4226f635SJason King loading = 1;
815aefb655Srie if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
82*4226f635SJason King !(fptr = (char *(*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
835aefb655Srie return (name);
84*4226f635SJason King loading = 0;
855aefb655Srie }
865aefb655Srie
87*4226f635SJason King if ((d = fptr(name, SYSDEM_LANG_AUTO, NULL)) == NULL)
88*4226f635SJason King return (name);
895aefb655Srie
90*4226f635SJason King return (d);
915aefb655Srie }
92