binutils-gdb/libiberty/cplus-dem.c

492 lines
13 KiB
C
Raw Normal View History

1999-05-03 09:29:11 +02:00
/* Demangler for GNU C++
Copyright (C) 1989-2019 Free Software Foundation, Inc.
1999-05-03 09:29:11 +02:00
Written by James Clark (jjc@jclark.uucp)
Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
2003-03-04 04:02:50 +01:00
In addition to the permissions in the GNU Library General Public
License, the Free Software Foundation gives you unlimited permission
to link the compiled version of this file into combinations with other
programs, and to distribute those combinations without any restriction
coming from the use of this file. (The Library Public License
restrictions do apply in other respects; for example, they cover
modification of the file, and distribution when not linked into a
combined executable.)
1999-05-03 09:29:11 +02:00
Libiberty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
1999-05-03 09:29:11 +02:00
/* This file lives in both GCC and libiberty. When making changes, please
try not to break either. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "safe-ctype.h"
1999-05-03 09:29:11 +02:00
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
2008-03-13 20:13:36 +01:00
void * malloc ();
void * realloc ();
1999-05-03 09:29:11 +02:00
#endif
#include <demangle.h>
#undef CURRENT_DEMANGLING_STYLE
#define CURRENT_DEMANGLING_STYLE options
1999-05-03 09:29:11 +02:00
#include "libiberty.h"
Synchronize libiberty sources with gcc mainline. * libiberty: Sync with gcc. Bring in: 2019-08-08 Martin Liska <mliska@suse.cz> PR bootstrap/91352 * lrealpath.c (is_valid_fd): New function. 2019-07-24 Martin Liska <mliska@suse.cz> PR lto/91228 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Find first '\0' starting from gnu_lto + 1. 2019-07-12 Ren Kimura <rkx1209dev@gmail.com> * simple-object-elf.c (simple_object_elf_match): Check zero value shstrndx. This fixes a Bug 90924. 2019-07-22 Martin Liska <mliska@suse.cz> * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Do not search for gnu_lto_v1, but search for first '\0'. 2019-07-18 Eduard-Mihai Burtescu <eddyb@lyken.rs> * cplus-dem.c: Include rust-demangle.h. * rust-demangle.c: Include rust-demangle.h. * rust-demangle.h: New file. 2019-05-31 Michael Forney <mforney@mforney.org> * cp-demangle.c: Don't define CP_DYNAMIC_ARRAYS if __STDC_NO_VLA__ is non-zero. 2019-04-30 Ben L <bobsayshilol@live.co.uk> * d-demangle.c (dlang_parse_assocarray): Correctly handle error result. * testsuite/d-demangle-expected: Add testcase. * d-demangle.c (dlang_parse_tuple): Correctly handle error result. * testsuite/d-demangle-expected: Add testcase. * d-demangle.c (dlang_parse_structlit): Correctly handle error result. * testsuite/d-demangle-expected: Add testcase. * d-demangle.c (dlang_parse_arrayliteral): Correctly handle error result. * testsuite/d-demangle-expected: Add testcase. * d-demangle.c (dlang_parse_integer): Fix stack underflow. * testsuite/d-demangle-expected: Add testcase. * cp-demangle (d_print_comp_inner): Guard against a NULL 'typed_name'. * testsuite/demangle-expected: Add testcase. * cp-demangle.c (d_encoding): Guard against NULL return values from d_right (dc). * testsuite/demangle-expected: Add testcase. 2019-04-29 Ben L <bobsayshilol@live.co.uk> * cp-demangle.c (d_expression_1): Don't peek ahead unless the current char is valid. * testsuite/demangle-expected: Add testcase. 2019-04-10 Nick Clifton <nickc@redhat.com> PR 89394 * cp-demangle.c (cplus_demangle_fill_name): Reject negative lengths. (d_count_templates_scopes): Replace num_templates and num_scopes parameters with a struct d_print_info pointer parameter. Adjust body of the function accordingly. Add recursion counter and check that the recursion limit is not reached. (d_print_init): Pass dpi parameter to d_count_templates_scopes. Reset recursion counter afterwards, unless the recursion limit was reached.
2019-08-09 17:16:18 +02:00
#include "rust-demangle.h"
1999-05-03 09:29:11 +02:00
enum demangling_styles current_demangling_style = auto_demangling;
1999-05-03 09:29:11 +02:00
2001-10-07 19:24:36 +02:00
const struct demangler_engine libiberty_demanglers[] =
{
{
NO_DEMANGLING_STYLE_STRING,
no_demangling,
"Demangling disabled"
}
,
{
AUTO_DEMANGLING_STYLE_STRING,
auto_demangling,
"Automatic selection based on executable"
}
,
{
GNU_V3_DEMANGLING_STYLE_STRING,
gnu_v3_demangling,
"GNU (g++) V3 (Itanium C++ ABI) style demangling"
}
,
{
JAVA_DEMANGLING_STYLE_STRING,
java_demangling,
"Java style demangling"
}
,
{
GNAT_DEMANGLING_STYLE_STRING,
gnat_demangling,
"GNAT style demangling"
}
,
Sync libiberty with upstream GCC. include/ChangeLog * libiberty.h (PEX_STDOUT_APPEND): New flag. (PEX_STDERR_APPEND): Likewise. * demangle.h (DMGL_DLANG): New macro. (DMGL_STYLE_MASK): Add DMGL_DLANG. (demangling_styles): Add dlang_demangling. (DLANG_DEMANGLING_STYLE_STRING): New macro. (DLANG_DEMANGLING): New macro. (dlang_demangle): New prototype. * longlong.h: Add __udiv_w_sdiv prototype. libiberty/ChangeLog * cp-demangle.c (d_substitution): Handle abi tags on abbreviation. * pex-common.h (struct pex_funcs): Add new parameter for open_write field. * pex-unix.c (pex_unix_open_write): Add support for new parameter. * pex-djgpp.c (pex_djgpp_open_write): Likewise. * pex-win32.c (pex_win32_open_write): Likewise. * pex-common.c (pex_run_in_environment): Likewise. * Makefile.in (CFILES): Add d-demangle.c. (REQUIRED_OFILES): Add d-demangle.o. * cplus-dem.c (libiberty_demanglers): Add dlang_demangling case. (cplus_demangle): Likewise. * d-demangle.c: New file. * testsuite/Makefile.in (really-check): Add check-d-demangle. * testsuite/d-demangle-expected: New file. * simple-object-elf.c (simple_object_elf_write_ehdr): Correctly handle objects with more than SHN_LORESERVE sections. (simple_object_elf_write_shdr): Add sh_link parameter. (simple_object_elf_write_to_file): Correctly handle objects with more than SHN_LORESERVE sections. * cp-demangle.c (d_dump): Only access field from s_fixed part of the union for DEMANGLE_COMPONENT_FIXED_TYPE. (d_count_templates_scopes): Likewise. * testsuite/demangler-fuzzer.c: New file. * testsuite/Makefile.in (fuzz-demangler): New rule. (demangler-fuzzer): Likewise. (mostlyclean): Clean up demangler fuzzer.
2014-10-11 11:29:10 +02:00
{
DLANG_DEMANGLING_STYLE_STRING,
dlang_demangling,
"DLANG style demangling"
}
,
{
RUST_DEMANGLING_STYLE_STRING,
rust_demangling,
"Rust style demangling"
}
,
{
NULL, unknown_demangling, NULL
}
};
/* Add a routine to set the demangling style to be sure it is valid and
allow for any demangler initialization that maybe necessary. */
enum demangling_styles
2005-03-27 07:28:42 +02:00
cplus_demangle_set_style (enum demangling_styles style)
{
2001-10-07 19:24:36 +02:00
const struct demangler_engine *demangler = libiberty_demanglers;
for (; demangler->demangling_style != unknown_demangling; ++demangler)
if (style == demangler->demangling_style)
{
current_demangling_style = style;
return current_demangling_style;
}
return unknown_demangling;
}
/* Do string name to style translation */
enum demangling_styles
2005-03-27 07:28:42 +02:00
cplus_demangle_name_to_style (const char *name)
{
2001-10-07 19:24:36 +02:00
const struct demangler_engine *demangler = libiberty_demanglers;
for (; demangler->demangling_style != unknown_demangling; ++demangler)
if (strcmp (name, demangler->demangling_style_name) == 0)
return demangler->demangling_style;
return unknown_demangling;
}
1999-05-03 09:29:11 +02:00
/* char *cplus_demangle (const char *mangled, int options)
If MANGLED is a mangled function name produced by GNU C++, then
2001-10-18 00:35:28 +02:00
a pointer to a @code{malloc}ed string giving a C++ representation
1999-05-03 09:29:11 +02:00
of the name will be returned; otherwise NULL will be returned.
It is the caller's responsibility to free the string which
is returned.
Note that any leading underscores, or other such characters prepended by
the compilation system, are presumed to have already been stripped from
MANGLED. */
char *
2005-03-27 07:28:42 +02:00
cplus_demangle (const char *mangled, int options)
1999-05-03 09:29:11 +02:00
{
char *ret;
if (current_demangling_style == no_demangling)
return xstrdup (mangled);
if ((options & DMGL_STYLE_MASK) == 0)
options |= (int) current_demangling_style & DMGL_STYLE_MASK;
1999-05-03 09:29:11 +02:00
/* The V3 ABI demangling is implemented elsewhere. */
if (GNU_V3_DEMANGLING || RUST_DEMANGLING || AUTO_DEMANGLING)
{
ret = cplus_demangle_v3 (mangled, options);
if (GNU_V3_DEMANGLING)
return ret;
if (ret)
{
/* Rust symbols are GNU_V3 mangled plus some extra subtitutions.
The subtitutions are always smaller, so do in place changes. */
if (rust_is_mangled (ret))
rust_demangle_sym (ret);
else if (RUST_DEMANGLING)
{
free (ret);
ret = NULL;
}
}
if (ret || RUST_DEMANGLING)
return ret;
}
2001-02-02 19:58:51 +01:00
if (JAVA_DEMANGLING)
{
ret = java_demangle_v3 (mangled);
if (ret)
return ret;
}
if (GNAT_DEMANGLING)
2010-01-11 20:12:47 +01:00
return ada_demangle (mangled, options);
Sync libiberty with upstream GCC. include/ChangeLog * libiberty.h (PEX_STDOUT_APPEND): New flag. (PEX_STDERR_APPEND): Likewise. * demangle.h (DMGL_DLANG): New macro. (DMGL_STYLE_MASK): Add DMGL_DLANG. (demangling_styles): Add dlang_demangling. (DLANG_DEMANGLING_STYLE_STRING): New macro. (DLANG_DEMANGLING): New macro. (dlang_demangle): New prototype. * longlong.h: Add __udiv_w_sdiv prototype. libiberty/ChangeLog * cp-demangle.c (d_substitution): Handle abi tags on abbreviation. * pex-common.h (struct pex_funcs): Add new parameter for open_write field. * pex-unix.c (pex_unix_open_write): Add support for new parameter. * pex-djgpp.c (pex_djgpp_open_write): Likewise. * pex-win32.c (pex_win32_open_write): Likewise. * pex-common.c (pex_run_in_environment): Likewise. * Makefile.in (CFILES): Add d-demangle.c. (REQUIRED_OFILES): Add d-demangle.o. * cplus-dem.c (libiberty_demanglers): Add dlang_demangling case. (cplus_demangle): Likewise. * d-demangle.c: New file. * testsuite/Makefile.in (really-check): Add check-d-demangle. * testsuite/d-demangle-expected: New file. * simple-object-elf.c (simple_object_elf_write_ehdr): Correctly handle objects with more than SHN_LORESERVE sections. (simple_object_elf_write_shdr): Add sh_link parameter. (simple_object_elf_write_to_file): Correctly handle objects with more than SHN_LORESERVE sections. * cp-demangle.c (d_dump): Only access field from s_fixed part of the union for DEMANGLE_COMPONENT_FIXED_TYPE. (d_count_templates_scopes): Likewise. * testsuite/demangler-fuzzer.c: New file. * testsuite/Makefile.in (fuzz-demangler): New rule. (demangler-fuzzer): Likewise. (mostlyclean): Clean up demangler fuzzer.
2014-10-11 11:29:10 +02:00
if (DLANG_DEMANGLING)
{
ret = dlang_demangle (mangled, options);
if (ret)
return ret;
}
1999-05-03 09:29:11 +02:00
return (ret);
}
char *
rust_demangle (const char *mangled, int options)
{
/* Rust symbols are GNU_V3 mangled plus some extra subtitutions. */
char *ret = cplus_demangle_v3 (mangled, options);
/* The Rust subtitutions are always smaller, so do in place changes. */
if (ret != NULL)
{
if (rust_is_mangled (ret))
rust_demangle_sym (ret);
else
{
free (ret);
ret = NULL;
}
}
return ret;
}
2010-01-11 20:12:47 +01:00
/* Demangle ada names. The encoding is documented in gcc/ada/exp_dbug.ads. */
1999-05-03 09:29:11 +02:00
2010-01-11 20:12:47 +01:00
char *
2005-03-27 07:28:42 +02:00
ada_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
{
int len0;
const char* p;
2010-01-11 20:12:47 +01:00
char *d;
char *demangled = NULL;
2010-01-11 20:12:47 +01:00
/* Discard leading _ada_, which is used for library level subprograms. */
if (strncmp (mangled, "_ada_", 5) == 0)
2010-01-11 20:12:47 +01:00
mangled += 5;
/* All ada unit names are lower-case. */
if (!ISLOWER (mangled[0]))
goto unknown;
/* Most of the demangling will trivially remove chars. Operator names
may add one char but because they are always preceeded by '__' which is
2010-09-27 23:01:18 +02:00
replaced by '.', they eventually never expand the size.
A few special names such as '___elabs' add a few chars (at most 7), but
they occur only once. */
len0 = strlen (mangled) + 7 + 1;
2010-01-11 20:12:47 +01:00
demangled = XNEWVEC (char, len0);
2010-01-11 20:12:47 +01:00
d = demangled;
p = mangled;
while (1)
{
2010-09-27 23:01:18 +02:00
/* An entity names is expected. */
2010-01-11 20:12:47 +01:00
if (ISLOWER (*p))
{
2010-09-27 23:01:18 +02:00
/* An identifier, which is always lower case. */
2010-01-11 20:12:47 +01:00
do
*d++ = *p++;
while (ISLOWER(*p) || ISDIGIT (*p)
|| (p[0] == '_' && (ISLOWER (p[1]) || ISDIGIT (p[1]))));
}
else if (p[0] == 'O')
{
2010-09-27 23:01:18 +02:00
/* An operator name. */
2010-01-11 20:12:47 +01:00
static const char * const operators[][2] =
{{"Oabs", "abs"}, {"Oand", "and"}, {"Omod", "mod"},
{"Onot", "not"}, {"Oor", "or"}, {"Orem", "rem"},
{"Oxor", "xor"}, {"Oeq", "="}, {"One", "/="},
{"Olt", "<"}, {"Ole", "<="}, {"Ogt", ">"},
{"Oge", ">="}, {"Oadd", "+"}, {"Osubtract", "-"},
{"Oconcat", "&"}, {"Omultiply", "*"}, {"Odivide", "/"},
{"Oexpon", "**"}, {NULL, NULL}};
int k;
2010-09-27 23:01:18 +02:00
for (k = 0; operators[k][0] != NULL; k++)
2010-01-11 20:12:47 +01:00
{
2010-09-27 23:01:18 +02:00
size_t slen = strlen (operators[k][0]);
if (strncmp (p, operators[k][0], slen) == 0)
2010-01-11 20:12:47 +01:00
{
2010-09-27 23:01:18 +02:00
p += slen;
slen = strlen (operators[k][1]);
2010-01-11 20:12:47 +01:00
*d++ = '"';
2010-09-27 23:01:18 +02:00
memcpy (d, operators[k][1], slen);
d += slen;
2010-01-11 20:12:47 +01:00
*d++ = '"';
break;
}
}
/* Operator not found. */
2010-09-27 23:01:18 +02:00
if (operators[k][0] == NULL)
2010-01-11 20:12:47 +01:00
goto unknown;
}
else
2010-01-11 20:12:47 +01:00
{
/* Not a GNAT encoding. */
goto unknown;
}
2010-09-27 23:01:18 +02:00
/* The name can be directly followed by some uppercase letters. */
if (p[0] == 'T' && p[1] == 'K')
{
/* Task stuff. */
if (p[2] == 'B' && p[3] == 0)
{
/* Subprogram for task body. */
break;
}
else if (p[2] == '_' && p[3] == '_')
{
/* Inner declarations in a task. */
p += 4;
*d++ = '.';
continue;
}
else
goto unknown;
}
if (p[0] == 'E' && p[1] == 0)
{
/* Exception name. */
goto unknown;
}
if ((p[0] == 'P' || p[0] == 'N') && p[1] == 0)
{
/* Protected type subprogram. */
break;
}
if ((*p == 'N' || *p == 'S') && p[1] == 0)
{
/* Enumerated type name table. */
goto unknown;
}
if (p[0] == 'X')
{
/* Body nested. */
p++;
while (p[0] == 'n' || p[0] == 'b')
p++;
}
if (p[0] == 'S' && p[1] != 0 && (p[2] == '_' || p[2] == 0))
{
/* Stream operations. */
const char *name;
switch (p[1])
{
case 'R':
name = "'Read";
break;
case 'W':
name = "'Write";
break;
case 'I':
name = "'Input";
break;
case 'O':
name = "'Output";
break;
default:
goto unknown;
}
p += 2;
strcpy (d, name);
d += strlen (name);
}
else if (p[0] == 'D')
{
/* Controlled type operation. */
const char *name;
switch (p[1])
{
case 'F':
name = ".Finalize";
break;
case 'A':
name = ".Adjust";
break;
default:
goto unknown;
}
strcpy (d, name);
d += strlen (name);
break;
}
2010-01-11 20:12:47 +01:00
if (p[0] == '_')
{
/* Separator. */
if (p[1] == '_')
{
/* Standard separator. Handled first. */
p += 2;
2010-09-27 23:01:18 +02:00
2010-01-11 20:12:47 +01:00
if (ISDIGIT (*p))
{
2010-09-27 23:01:18 +02:00
/* Overloading number. */
2010-01-11 20:12:47 +01:00
do
p++;
while (ISDIGIT (*p) || (p[0] == '_' && ISDIGIT (p[1])));
2010-09-27 23:01:18 +02:00
if (*p == 'X')
{
p++;
while (p[0] == 'n' || p[0] == 'b')
p++;
}
2010-01-11 20:12:47 +01:00
}
2010-09-27 23:01:18 +02:00
else if (p[0] == '_' && p[1] != '_')
2010-01-11 20:12:47 +01:00
{
2010-09-27 23:01:18 +02:00
/* Special names. */
static const char * const special[][2] = {
{ "_elabb", "'Elab_Body" },
{ "_elabs", "'Elab_Spec" },
{ "_size", "'Size" },
{ "_alignment", "'Alignment" },
{ "_assign", ".\":=\"" },
{ NULL, NULL }
};
int k;
for (k = 0; special[k][0] != NULL; k++)
{
size_t slen = strlen (special[k][0]);
if (strncmp (p, special[k][0], slen) == 0)
{
p += slen;
slen = strlen (special[k][1]);
memcpy (d, special[k][1], slen);
d += slen;
break;
}
}
if (special[k][0] != NULL)
break;
else
goto unknown;
2010-01-11 20:12:47 +01:00
}
else
{
*d++ = '.';
continue;
}
}
else if (p[1] == 'B' || p[1] == 'E')
{
/* Entry Body or barrier Evaluation. */
p += 2;
while (ISDIGIT (*p))
p++;
if (p[0] == 's' && p[1] == 0)
break;
else
goto unknown;
}
else
goto unknown;
}
if (p[0] == '.' && ISDIGIT (p[1]))
{
/* Nested subprogram. */
p += 2;
while (ISDIGIT (*p))
p++;
}
if (*p == 0)
{
/* End of mangled name. */
break;
}
else
2010-01-11 20:12:47 +01:00
goto unknown;
}
2010-01-11 20:12:47 +01:00
*d = 0;
return demangled;
2010-01-11 20:12:47 +01:00
unknown:
XDELETEVEC (demangled);
2010-01-11 20:12:47 +01:00
len0 = strlen (mangled);
demangled = XNEWVEC (char, len0 + 3);
2002-10-06 22:21:01 +02:00
if (mangled[0] == '<')
2011-03-25 17:38:27 +01:00
strcpy (demangled, mangled);
else
sprintf (demangled, "<%s>", mangled);
return demangled;
}