* budemang.c: New file, "demangle" function.
[binutils.git] / binutils / budemang.c
blob7e0b6124129ef21f5c0b074e125a67561cc727fe
1 /* demangle.c -- A wrapper calling libiberty cplus_demangle
2 Copyright 2002 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include <stdlib.h>
22 #include "bfd.h"
23 #include "libiberty.h"
24 #include "demangle.h"
25 #include "budemang.h"
27 /* Wrapper around cplus_demangle. Strips leading underscores and
28 other such chars that would otherwise confuse the demangler. */
30 char *
31 demangle (abfd, name)
32 bfd *abfd;
33 const char *name;
35 char *res;
36 const char *p;
38 if (abfd != NULL && bfd_get_symbol_leading_char (abfd) == name[0])
39 ++name;
41 /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
42 or the MS PE format. These formats have a number of leading '.'s
43 on at least some symbols, so we remove all dots to avoid
44 confusing the demangler. */
45 p = name;
46 while (*p == '.')
47 ++p;
49 res = cplus_demangle (p, DMGL_ANSI | DMGL_PARAMS);
50 if (res)
52 size_t dots = p - name;
54 /* Now put back any stripped dots. */
55 if (dots != 0)
57 size_t len = strlen (res) + 1;
58 char *add_dots = xmalloc (len + dots);
60 memcpy (add_dots, name, dots);
61 memcpy (add_dots + dots, res, len);
62 free (res);
63 res = add_dots;
65 return res;
68 return xstrdup (name);