📄 cplus-dem.c
字号:
/* Demangler for GNU C++ Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. 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 demanglingThis file is part of the libiberty library.Libiberty is free software; you can redistribute it and/ormodify it under the terms of the GNU Library General PublicLicense as published by the Free Software Foundation; eitherversion 2 of the License, or (at your option) any later version.Libiberty is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULibrary General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with libiberty; see the file COPYING.LIB. Ifnot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. *//* This file exports two functions; cplus_mangle_opname and cplus_demangle. This file imports xmalloc and xrealloc, which are like malloc and realloc except that they generate a fatal error if there is no available memory. *//* 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"#include <sys/types.h>#include <string.h>#include <stdio.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#elsechar * malloc ();char * realloc ();#endif#include <demangle.h>#undef CURRENT_DEMANGLING_STYLE#define CURRENT_DEMANGLING_STYLE work->options#include "libiberty.h"static char *ada_demangle PARAMS ((const char *, int));static char *edg_demangle PARAMS ((const char *, int));#define min(X,Y) (((X) < (Y)) ? (X) : (Y))/* A value at least one greater than the maximum number of characters that will be output when using the `%d' format with `printf'. */#define INTBUF_SIZE 32extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;static const char *mystrstr PARAMS ((const char *, const char *));static const char *mystrstr (s1, s2) const char *s1, *s2;{ register const char *p = s1; register int len = strlen (s2); for (; (p = strchr (p, *s2)) != 0; p++) { if (strncmp (p, s2, len) == 0) { return (p); } } return (0);}/* In order to allow a single demangler executable to demangle strings using various common values of CPLUS_MARKER, as well as any specific one set at compile time, we maintain a string containing all the commonly used ones, and check to see if the marker we are looking for is in that string. CPLUS_MARKER is usually '$' on systems where the assembler can deal with that. Where the assembler can't, it's usually '.' (but on many systems '.' is used for other things). We put the current defined CPLUS_MARKER first (which defaults to '$'), followed by the next most common value, followed by an explicit '$' in case the value of CPLUS_MARKER is not '$'. We could avoid this if we could just get g++ to tell us what the actual cplus marker character is as part of the debug information, perhaps by ensuring that it is the character that terminates the gcc<n>_compiled marker symbol (FIXME). */#if !defined (CPLUS_MARKER)#define CPLUS_MARKER '$'#endifenum demangling_styles current_demangling_style = auto_demangling;static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };static char char_str[2] = { '\000', '\000' };voidset_cplus_marker_for_demangling (ch) int ch;{ cplus_markers[0] = ch;}typedef struct string /* Beware: these aren't required to be */{ /* '\0' terminated. */ char *b; /* pointer to start of string */ char *p; /* pointer after last character */ char *e; /* pointer after end of allocated space */} string;/* Stuff that is shared between sub-routines. Using a shared structure allows cplus_demangle to be reentrant. */struct work_stuff{ int options; char **typevec; char **ktypevec; char **btypevec; int numk; int numb; int ksize; int bsize; int ntypes; int typevec_size; int constructor; int destructor; int static_type; /* A static member function */ int temp_start; /* index in demangled to start of template args */ int type_quals; /* The type qualifiers. */ int dllimported; /* Symbol imported from a PE DLL */ char **tmpl_argvec; /* Template function arguments. */ int ntmpl_args; /* The number of template function arguments. */ int forgetting_types; /* Nonzero if we are not remembering the types we see. */ string* previous_argument; /* The last function argument demangled. */ int nrepeats; /* The number of times to repeat the previous argument. */};#define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)#define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)static const struct optable{ const char *in; const char *out; int flags;} optable[] = { {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */ {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */ {"new", " new", 0}, /* old (1.91, and 1.x) */ {"delete", " delete", 0}, /* old (1.91, and 1.x) */ {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */ {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */ {"as", "=", DMGL_ANSI}, /* ansi */ {"ne", "!=", DMGL_ANSI}, /* old, ansi */ {"eq", "==", DMGL_ANSI}, /* old, ansi */ {"ge", ">=", DMGL_ANSI}, /* old, ansi */ {"gt", ">", DMGL_ANSI}, /* old, ansi */ {"le", "<=", DMGL_ANSI}, /* old, ansi */ {"lt", "<", DMGL_ANSI}, /* old, ansi */ {"plus", "+", 0}, /* old */ {"pl", "+", DMGL_ANSI}, /* ansi */ {"apl", "+=", DMGL_ANSI}, /* ansi */ {"minus", "-", 0}, /* old */ {"mi", "-", DMGL_ANSI}, /* ansi */ {"ami", "-=", DMGL_ANSI}, /* ansi */ {"mult", "*", 0}, /* old */ {"ml", "*", DMGL_ANSI}, /* ansi */ {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */ {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */ {"convert", "+", 0}, /* old (unary +) */ {"negate", "-", 0}, /* old (unary -) */ {"trunc_mod", "%", 0}, /* old */ {"md", "%", DMGL_ANSI}, /* ansi */ {"amd", "%=", DMGL_ANSI}, /* ansi */ {"trunc_div", "/", 0}, /* old */ {"dv", "/", DMGL_ANSI}, /* ansi */ {"adv", "/=", DMGL_ANSI}, /* ansi */ {"truth_andif", "&&", 0}, /* old */ {"aa", "&&", DMGL_ANSI}, /* ansi */ {"truth_orif", "||", 0}, /* old */ {"oo", "||", DMGL_ANSI}, /* ansi */ {"truth_not", "!", 0}, /* old */ {"nt", "!", DMGL_ANSI}, /* ansi */ {"postincrement","++", 0}, /* old */ {"pp", "++", DMGL_ANSI}, /* ansi */ {"postdecrement","--", 0}, /* old */ {"mm", "--", DMGL_ANSI}, /* ansi */ {"bit_ior", "|", 0}, /* old */ {"or", "|", DMGL_ANSI}, /* ansi */ {"aor", "|=", DMGL_ANSI}, /* ansi */ {"bit_xor", "^", 0}, /* old */ {"er", "^", DMGL_ANSI}, /* ansi */ {"aer", "^=", DMGL_ANSI}, /* ansi */ {"bit_and", "&", 0}, /* old */ {"ad", "&", DMGL_ANSI}, /* ansi */ {"aad", "&=", DMGL_ANSI}, /* ansi */ {"bit_not", "~", 0}, /* old */ {"co", "~", DMGL_ANSI}, /* ansi */ {"call", "()", 0}, /* old */ {"cl", "()", DMGL_ANSI}, /* ansi */ {"alshift", "<<", 0}, /* old */ {"ls", "<<", DMGL_ANSI}, /* ansi */ {"als", "<<=", DMGL_ANSI}, /* ansi */ {"arshift", ">>", 0}, /* old */ {"rs", ">>", DMGL_ANSI}, /* ansi */ {"ars", ">>=", DMGL_ANSI}, /* ansi */ {"component", "->", 0}, /* old */ {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */ {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */ {"indirect", "*", 0}, /* old */ {"method_call", "->()", 0}, /* old */ {"addr", "&", 0}, /* old (unary &) */ {"array", "[]", 0}, /* old */ {"vc", "[]", DMGL_ANSI}, /* ansi */ {"compound", ", ", 0}, /* old */ {"cm", ", ", DMGL_ANSI}, /* ansi */ {"cond", "?:", 0}, /* old */ {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */ {"max", ">?", 0}, /* old */ {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */ {"min", "<?", 0}, /* old */ {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */ {"nop", "", 0}, /* old (for operator=) */ {"rm", "->*", DMGL_ANSI}, /* ansi */ {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */};/* These values are used to indicate the various type varieties. They are all non-zero so that they can be used as `success' values. */typedef enum type_kind_t{ tk_none, tk_pointer, tk_reference, tk_integral, tk_bool, tk_char, tk_real} type_kind_t;struct demangler_engine libiberty_demanglers[] ={ { AUTO_DEMANGLING_STYLE_STRING, auto_demangling, "Automatic selection based on executable" } , { GNU_DEMANGLING_STYLE_STRING, gnu_demangling, "GNU (g++) style demangling" } , { LUCID_DEMANGLING_STYLE_STRING, lucid_demangling, "Lucid (lcc) style demangling" } , { ARM_DEMANGLING_STYLE_STRING, arm_demangling, "ARM style demangling" } , { HP_DEMANGLING_STYLE_STRING, hp_demangling, "HP (aCC) style demangling" } , { EDG_DEMANGLING_STYLE_STRING, edg_demangling, "EDG style demangling" } , { GNU_V3_DEMANGLING_STYLE_STRING, gnu_v3_demangling, "GNU (g++) V3 ABI-style demangling" } , { JAVA_DEMANGLING_STYLE_STRING, java_demangling, "Java style demangling" } , { GNAT_DEMANGLING_STYLE_STRING, gnat_demangling, "GNAT style demangling" } , { NULL, unknown_demangling, NULL }};#define STRING_EMPTY(str) ((str) -> b == (str) -> p)#define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \ string_prepend(str, " ");}#define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \ string_append(str, " ");}#define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))/* The scope separator appropriate for the language being demangled. */#define SCOPE_STRING(work) ((work->options & DMGL_JAVA) ? "." : "::")#define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */#define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) *//* Prototypes for local functions */static voiddelete_work_stuff PARAMS ((struct work_stuff *));static voiddelete_non_B_K_work_stuff PARAMS ((struct work_stuff *));static char *mop_up PARAMS ((struct work_stuff *, string *, int));static voidsquangle_mop_up PARAMS ((struct work_stuff *));static voidwork_stuff_copy_to_from PARAMS ((struct work_stuff *, struct work_stuff *));#if 0static intdemangle_method_args PARAMS ((struct work_stuff *, const char **, string *));#endifstatic char *internal_cplus_demangle PARAMS ((struct work_stuff *, const char *));static intdemangle_template_template_parm PARAMS ((struct work_stuff *work, const char **, string *));static intdemangle_template PARAMS ((struct work_stuff *work, const char **, string *, string *, int, int));static intarm_pt PARAMS ((struct work_stuff *, const char *, int, const char **, const char **));static intdemangle_class_name PARAMS ((struct work_stuff *, const char **, string *));static intdemangle_qualified PARAMS ((struct work_stuff *, const char **, string *, int, int));static intdemangle_class PARAMS ((struct work_stuff *, const char **, string *));static intdemangle_fund_type PARAMS ((struct work_stuff *, const char **, string *));static intdemangle_signature PARAMS ((struct work_stuff *, const char **, string *));static intdemangle_prefix PARAMS ((struct work_stuff *, const char **, string *));static intgnu_special PARAMS ((struct work_stuff *, const char **, string *));static intarm_special PARAMS ((const char **, string *));static voidstring_need PARAMS ((string *, int));static voidstring_delete PARAMS ((string *));static voidstring_init PARAMS ((string *));static voidstring_clear PARAMS ((string *));#if 0static intstring_empty PARAMS ((string *));#endifstatic voidstring_append PARAMS ((string *, const char *));static voidstring_appends PARAMS ((string *, string *));static voidstring_appendn PARAMS ((string *, const char *, int));static voidstring_prepend PARAMS ((string *, const char *));static voidstring_prependn PARAMS ((string *, const char *, int));static voidstring_append_template_idx PARAMS ((string *, int));static intget_count PARAMS ((const char **, int *));static intconsume_count PARAMS ((const char **));static intconsume_count_with_underscores PARAMS ((const char**));static intdemangle_args PARAMS ((struct work_stuff *, const char **, string *));static intdemangle_nested_args PARAMS ((struct work_stuff*, const char**, string*));static intdo_type PARAMS ((struct work_stuff *, const char **, string *));static intdo_arg PARAMS ((struct work_stuff *, const char **, string *));static voiddemangle_function_name PARAMS ((struct work_stuff *, const char **, string *, const char *));static intiterate_demangle_function PARAMS ((struct work_stuff *, const char **, string *, const char *));static voidremember_type PARAMS ((struct work_stuff *, const char *, int));static voidremember_Btype PARAMS ((struct work_stuff *, const char *, int, int));static intregister_Btype PARAMS ((struct work_stuff *));static voidremember_Ktype PARAMS ((struct work_stuff *, const char *, int));static voidforget_types PARAMS ((struct work_stuff *));static voidforget_B_and_K_types PARAMS ((struct work_stuff *));static voidstring_prepends PARAMS ((string *, string *));static intdemangle_template_value_parm PARAMS ((struct work_stuff*, const char**, string*, type_kind_t));static intdo_hpacc_template_const_value PARAMS ((struct work_stuff *, const char **, string *));static intdo_hpacc_template_literal PARAMS ((struct work_stuff *, const char **, string *));static intsnarf_numeric_literal PARAMS ((const char **, string *));/* There is a TYPE_QUAL value for each type qualifier. They can be combined by bitwise-or to form the complete set of qualifiers for a type. */#define TYPE_UNQUALIFIED 0x0#define TYPE_QUAL_CONST 0x1#define TYPE_QUAL_VOLATILE 0x2#define TYPE_QUAL_RESTRICT 0x4static intcode_for_qualifier PARAMS ((int));static const char*qualifier_string PARAMS ((int));static const char*demangle_qualifier PARAMS ((int));static intdemangle_expression PARAMS ((struct work_stuff *, const char **, string *, type_kind_t));static intdemangle_integral_value PARAMS ((struct work_stuff *, const char **,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -