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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2018, Joyent, Inc.
25 */
26
27#include	<stdio.h>
28#include	<demangle-sys.h>
29#include	"_conv.h"
30#include	"demangle_msg.h"
31
32/*
33 * Demangle C++ symbols.
34 *
35 * This routine acts as a generic routine for use by liblddbg (and hence tools
36 * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
37 *
38 * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
39 * it's possible the buffer won't be big enough (DEMANGLE_ESPACE) so here we
40 * try to allocate bigger buffers.  However, we place a limit on this buffer
41 * size for fear of a C++ error sending us into an infinit loop.
42 *
43 * NOTE. we create and use a common buffer for use by cplus_demangle(), thus
44 * each call to this routine will override the contents of any existing call.
45 * Normally this is sufficient for typical error diagnostics referencing one
46 * symbol.  For those diagnostics using more than one symbol name, all but the
47 * last name must be copied to a temporary buffer (regardless of whether
48 * demangling occurred, as the process of attempting to demangle may damage the
49 * buffer).  One model is:
50 *
51 *	if ((_name1 = demangle(name1)) != name1) {
52 *		char *	__name1 = strdupa(_name1);
53 *		name1 = (const char *)__name1;
54 *	}
55 *	name2 = demangle(name2);
56 *	eprintf(format, name1, name2);
57 */
58#define	SYM_MAX	1000
59
60const char *
61conv_demangle_name(const char *name)
62{
63	static char	*(*fptr)() = 0;
64	static volatile int loading = 0;
65	char *d;
66
67	if (loading)
68		return (name);
69
70	/*
71	 * If we haven't located the demangler yet try now (we do this rather
72	 * than maintain a static dependency on libdemangle as it's part of an
73	 * optional package).  Null the str element out to reject any other
74	 * callers until this operation is complete - under ld.so.1 we can get
75	 * into serious recursion without this.
76	 */
77	if (fptr == 0) {
78		void	*hdl;
79
80		loading = 1;
81		if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
82		    !(fptr = (char *(*)())dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
83			return (name);
84		loading = 0;
85	}
86
87	if ((d = fptr(name, SYSDEM_LANG_AUTO, NULL)) == NULL)
88		return (name);
89
90	return (d);
91}
92