/* Chrysalide - Outil d'analyse de fichiers binaires * abi.c - décodage des noms d'éléments selon l'ABI C++ Itanium * * Copyright (C) 2013-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Chrysalide 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar. If not, see . */ #include "abi.h" #include #include #include "../../../analysis/types/basic.h" #include "../../../common/cpp.h" /* Liste des opérateurs reconnus */ #define IDT_NL(s) s, sizeof(s) - 1 const itanium_operator_info itanium_demangle_operators[] = { { "aN", IDT_NL("&="), 2 }, { "aS", IDT_NL("="), 2 }, { "aa", IDT_NL("&&"), 2 }, { "ad", IDT_NL("&"), 1 }, { "an", IDT_NL("&"), 2 }, { "at", IDT_NL("alignof "), 1 }, { "az", IDT_NL("alignof "), 1 }, { "cc", IDT_NL("const_cast"), 2 }, { "cl", IDT_NL("()"), 2 }, { "cm", IDT_NL(","), 2 }, { "co", IDT_NL("~"), 1 }, { "dV", IDT_NL("/="), 2 }, { "da", IDT_NL("delete[] "), 1 }, { "dc", IDT_NL("dynamic_cast"), 2 }, { "de", IDT_NL("*"), 1 }, { "dl", IDT_NL("delete "), 1 }, { "ds", IDT_NL(".*"), 2 }, { "dt", IDT_NL("."), 2 }, { "dv", IDT_NL("/"), 2 }, { "eO", IDT_NL("^="), 2 }, { "eo", IDT_NL("^"), 2 }, { "eq", IDT_NL("=="), 2 }, { "ge", IDT_NL(">="), 2 }, { "gs", IDT_NL("::"), 1 }, { "gt", IDT_NL(">"), 2 }, { "ix", IDT_NL("[]"), 2 }, { "lS", IDT_NL("<<="), 2 }, { "le", IDT_NL("<="), 2 }, { "li", IDT_NL("operator\"\" "), 1 }, { "ls", IDT_NL("<<"), 2 }, { "lt", IDT_NL("<"), 2 }, { "mI", IDT_NL("-="), 2 }, { "mL", IDT_NL("*="), 2 }, { "mi", IDT_NL("-"), 2 }, { "ml", IDT_NL("*"), 2 }, { "mm", IDT_NL("--"), 1 }, { "na", IDT_NL("new[]"), 3 }, { "ne", IDT_NL("!="), 2 }, { "ng", IDT_NL("-"), 1 }, { "nt", IDT_NL("!"), 1 }, { "nw", IDT_NL("new"), 3 }, { "oR", IDT_NL("|="), 2 }, { "oo", IDT_NL("||"), 2 }, { "or", IDT_NL("|"), 2 }, { "pL", IDT_NL("+="), 2 }, { "pl", IDT_NL("+"), 2 }, { "pm", IDT_NL("->*"), 2 }, { "pp", IDT_NL("++"), 1 }, { "ps", IDT_NL("+"), 1 }, { "pt", IDT_NL("->"), 2 }, { "qu", IDT_NL("?"), 3 }, { "rM", IDT_NL("%="), 2 }, { "rS", IDT_NL(">>="), 2 }, { "rc", IDT_NL("reinterpret_cast"), 2 }, { "rm", IDT_NL("%"), 2 }, { "rs", IDT_NL(">>"), 2 }, { "sc", IDT_NL("static_cast"), 2 }, { "st", IDT_NL("sizeof "), 1 }, { "sz", IDT_NL("sizeof "), 1 }, { "tr", IDT_NL("throw"), 0 }, { "tw", IDT_NL("throw "), 1 } }; /* Substitutions standards */ typedef struct _itanium_std_subst_info { char code; /* Identifiant associé */ const char *simple; /* Représentation simple */ size_t simple_len; /* Taille de cette représentat°*/ const char *full; /* Représentation complète */ size_t full_len; /* Taille de cette représentat°*/ const char *last_name; /* Pour les (con|de)structeurs */ size_t last_name_len; /* Taille de cette indication */ } itanium_std_subst_info; const itanium_std_subst_info itanium_standard_substitutions[] = { { 't', IDT_NL("std"), IDT_NL("std"), NULL, 0 }, { 'a', IDT_NL("std::allocator"), IDT_NL("std::allocator"), IDT_NL("allocator") }, { 'b', IDT_NL("std::basic_string"), IDT_NL("std::basic_string"), IDT_NL("basic_string") }, { 's', IDT_NL("std::string"), IDT_NL("std::basic_string, std::allocator>"), IDT_NL("basic_string") }, { 'i', IDT_NL("std::istream"), IDT_NL("std::basic_istream>"), IDT_NL("basic_istream") }, { 'o', IDT_NL("std::ostream"), IDT_NL("std::basic_ostream>"), IDT_NL("basic_ostream") }, { 'd', IDT_NL("std::iostream"), IDT_NL("std::basic_iostream>"), IDT_NL("basic_iostream") } }; /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_encoding(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_unscoped_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_unscoped_template_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_nested_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_prefix(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_template_prefix(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_unqualified_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_source_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static bool itd_number(GItaniumDContext *, ssize_t *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_identifier(GItaniumDContext *, size_t); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_operator_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_special_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_call_offset(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_nv_offset(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_v_offset(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_ctor_dtor_name(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_type(GItaniumDContext *); /* Extrait une propriété de composant pour un contexte Itanium. */ static TypeQualifier itd_cv_qualifiers(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_builtin_type(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_bare_function_type(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_class_enum_type(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_template_args(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_template_arg(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_value_to_string(GItaniumDContext *, bool); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_expr_primary(GItaniumDContext *); /* Extrait un composant dans un contexte Itanium. */ static bool itd_seq_id(GItaniumDContext *, char, size_t *); /* Extrait un composant dans un contexte Itanium. */ static itanium_component *itd_substitution(GItaniumDContext *); #define itd_template_param(ctx) NULL #define itd_local_name(ctx) NULL #define itd_expression(ctx) NULL /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ itanium_component *itd_mangled_name(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ /** * La règle traitée ici est la suivante : * * ::= _Z * */ if (!g_itanium_dcontext_check_char(context, '_')) return NULL; if (!g_itanium_dcontext_check_char(context, 'Z')) return NULL; result = itd_encoding(context); return result; } /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static itanium_component *itd_encoding(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ itd_state saved; /* Position d'analyse courante */ itanium_component *func; /* Composant 'function name' */ itanium_component *types; /* Composant 'bare-function...'*/ /** * La règle traitée ici est la suivante : * * ::= * ::= * ::= * */ result = NULL; g_itanium_dcontext_push_state(context, &saved); func = itd_name(context); if (func != NULL) { types = itd_bare_function_type(context); if (types != NULL) result = itd_make_binary(context, ICT_FUNCTION_ENCODING, func, types); else itd_unref_comp(func); } /* if (result == NULL) { g_itanium_dcontext_pop_state(context, &saved); result = itd_name(context); } */ if (result == NULL) { g_itanium_dcontext_pop_state(context, &saved); result = itd_special_name(context); } return result; } /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static itanium_component *itd_name(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ itd_state saved; /* Position d'analyse courante */ itanium_component *tname; /* Composant '...template-name'*/ itanium_component *targs; /* Composant 'template-args' */ /** * La règle traitée ici est la suivante : * * ::= * ::= * ::= * ::= * */ g_itanium_dcontext_push_state(context, &saved); result = itd_nested_name(context); if (result == NULL) { g_itanium_dcontext_pop_state(context, &saved); tname = itd_unscoped_template_name(context); if (tname != NULL) { targs = itd_template_args(context); if (targs != NULL) { g_itanium_dcontext_add_substitution(context, tname); result = itd_make_binary(context, ICT_TEMPLATE_NAME_ARGS, tname, targs); } else itd_unref_comp(tname); } } /** * La règle doit être traitée après , * car ces deux dernières ont une base commune et la seconde peut avoir besoin * d'aller plus loin avec la règle . * * On termine donc par moins gourmand si la règle la plus complexe n'a pas abouti. */ if (result == NULL) { g_itanium_dcontext_pop_state(context, &saved); result = itd_unscoped_name(context); } if (result == NULL) { g_itanium_dcontext_pop_state(context, &saved); result = itd_local_name(context); } return result; } /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static itanium_component *itd_unscoped_name(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ /** * La règle traitée ici est la suivante : * * ::= * ::= St # ::std:: * */ /* TODO : 'St' */ result = itd_unqualified_name(context); return result; } /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static itanium_component *itd_unscoped_template_name(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ itd_state saved; /* Position d'analyse courante */ /** * La règle traitée ici est la suivante : * * ::= * ::= * */ g_itanium_dcontext_push_state(context, &saved); result = itd_unscoped_name(context); if (result == NULL) { g_itanium_dcontext_pop_state(context, &saved); result = itd_substitution(context); } return result; } /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static itanium_component *itd_nested_name(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ TypeQualifier qualifier; /* Propriétés supplémentaires */ itd_state saved; /* Position d'analyse courante */ itanium_component *left; /* Première partie */ itanium_component *right; /* Seconde partie */ static int inner = 0; int val = inner++; printf("\n ### (%d) NESTED>> '%s'\n", val, g_itanium_dcontext_get_string(context, (size_t []){ 0 })); /** * La règle traitée ici est la suivante : * * ::= N [] E * ::= N [] E * */ if (!g_itanium_dcontext_check_char(context, 'N')) return NULL; qualifier = itd_cv_qualifiers(context); result = NULL; g_itanium_dcontext_push_state(context, &saved); printf("\n ----- (%d) nested prefix '%s'\n", val, g_itanium_dcontext_get_string(context, (size_t []){ 0 })); left = itd_prefix(context); if (left != NULL) { if (itd_get_component_type(left) != ICT_EMPTY) g_itanium_dcontext_add_substitution(context, left); right = itd_unqualified_name(context); if (right != NULL) { if (g_itanium_dcontext_check_char(context, 'E')) result = itd_make_binary(context, ICT_NESTED_NAME, left, right); else printf("=== (%d) nested/prefix : BAD E\n\n", val); //result = itd_make_binary(context, ICT_NESTED_NAME, left, right); } else itd_unref_comp(left); printf(" ---- (%d) nested prefix --> %p\n\n", val, result); } if (result == NULL) { printf("\n ----- (%d) nested template_arg '%s'\n", val, g_itanium_dcontext_get_string(context, (size_t []){ 0 })); g_itanium_dcontext_pop_state(context, &saved); left = itd_template_prefix(context); if (left != NULL) { g_itanium_dcontext_add_substitution(context, left); right = itd_template_args(context); if (right != NULL) { if (g_itanium_dcontext_check_char(context, 'E')) result = itd_make_binary(context, ICT_NESTED_NAME, left, right); else printf("=== (%d) nested/prefix : BAD E\n\n", val); } else itd_unref_comp(left); } printf(" ---- (%d) nested template_arg --> %p\n\n", val, result); } printf("(%d) nested/E >> '%s'\n", val, g_itanium_dcontext_get_string(context, (size_t []){ 0 })); /* if (!g_itanium_dcontext_check_char(context, 'E')) { printf("=== (%d) NESTED : BAD E\n\n", val); if (result != NULL) itd_unref_comp(result); return NULL; } */ if (result != NULL) printf("=== (%d) NESTED OK (%p)\n\n", val, result); inner--; return result; } /****************************************************************************** * * * Paramètres : context = contexte de décodage à utiliser. * * * * Description : Extrait un composant dans un contexte Itanium. * * * * Retour : Composant extrait ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static itanium_component *itd_prefix(GItaniumDContext *context) { itanium_component *result; /* Construction à retourner */ itanium_component *looping_rule; /* Extraction d'une boucle */ itd_state saved; /* Position d'analyse courante */ itanium_component *left; /* Première partie */ itanium_component *right; /* Seconde partie */ itd_state next_name_saved; /* Position d'analyse courante */ itanium_component *next_name; /* Eventuel complément #1 */ itd_state next_targs_saved; /* Position d'analyse courante */ itanium_component *next_targs; /* Eventuel complément #2 */ itd_state expected_saved; /* Position d'analyse suivante */ itanium_component *expected; /* Nom non-qualifié en réserve */ /** * Les deux règles traitées ici sont les suivantes : * * ::= * ::= * ::= * ::= # empty * ::= * * ::=