libconv: remove libdemangle dependency
[unleashed.git] / usr / src / cmd / sgs / libconv / common / demangle.cc
bloba05f0bd43cc25b1852d98c2cd35f3d99e481f390
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <stdio.h>
27 #include <cxxabi.h>
28 #include "_conv.h"
29 #include "demangle_msg.h"
32 * Demangle C++ symbols.
34 * This routine acts as a generic routine for use by liblddbg (and hence tools
35 * like elfdump(1) and pvs(1)), ld(1) and ld.so.1(1).
37 * The C++ ABI-2 places no limits on symbol names, thus when demangling a name
38 * it's possible the buffer won't be big enough (return -1) so here we try to
39 * allocate bigger buffers. However, we place a limit on this buffer size for
40 * fear of a C++ error sending us into an infinit loop.
42 * NOTE. we create and use a common buffer for use by __cxa_demangle(), thus
43 * each call to this routine will override the contents of any existing call.
44 * Normally this is sufficient for typical error diagnostics referencing one
45 * symbol. For those diagnostics using more than one symbol name, all but the
46 * last name must be copied to a temporary buffer (regardless of whether
47 * demangling occurred, as the process of attempting to demangle may damage the
48 * buffer). One model is:
50 * if ((_name1 = demangle(name1)) != name1) {
51 * char * __name1 = strdupa(_name1);
52 * name1 = (const char *)__name1;
53 * }
54 * name2 = demangle(name2);
55 * eprintf(format, name1, name2);
57 #define SYM_MAX 1000
59 const char *
60 conv_demangle_name(const char *name)
62 static char _str[SYM_MAX], *str = _str;
63 static size_t size = SYM_MAX;
64 static int again = 1;
65 static char * (*fptr)(const char *, char *, size_t *, int *) = 0;
66 int error;
68 if (str == 0)
69 return (name);
72 * If we haven't located the demangler yet try now (we do this rather
73 * than maintain a static dependency on libstdc++). Null the str
74 * element out to reject any other callers until this operation is
75 * complete - under ld.so.1 we can get into serious recursion without
76 * this.
78 if (fptr == 0) {
79 void *hdl;
81 str = 0;
82 if (!(hdl = dlopen(MSG_ORIG(MSG_DEM_LIB), RTLD_LAZY)) ||
83 !(fptr = (char *(*)(const char *, char *, size_t *, int *))
84 dlsym(hdl, MSG_ORIG(MSG_DEM_SYM))))
85 return (name);
86 str = _str;
89 fptr(name, str, &size, &error);
90 if (str)
91 return ((const char *)str);
93 while ((error == -1) && again) {
94 char *_str;
95 size_t _size = size;
98 * If we haven't allocated our maximum try incrementing the
99 * present buffer size. Use malloc() rather than realloc() so
100 * that we at least have the old buffer on failure.
102 if (((_size += SYM_MAX) > (SYM_MAX * 4)) ||
103 ((_str = (char *)malloc(_size)) == 0)) {
104 again = 0;
105 break;
107 if (size != SYM_MAX) {
108 free(str);
110 str = _str;
111 size = _size;
113 fptr(name, str, &size, &error);
114 if (str)
115 return ((const char *)str);
117 return (name);