/******************************************************************* * demangle_symbol * * Demangle a C++ linker symbol into a C prototype */ int symbol_demangle (parsed_symbol *sym) { compound_type ct; int is_static = 0, is_const = 0; char *function_name = NULL; char *class_name = NULL; char *name; const char *const_status; static unsigned int hash = 0; /* In case of overloaded functions */ unsigned int data_flags = 0; assert (globals.do_code); assert (sym && sym->symbol); hash++; /* MS mangled names always begin with '?' */ name = sym->symbol; if (*name++ != '?') return -1; if (VERBOSE) puts ("Attempting to demangle symbol"); /* Then function name or operator code */ if (*name == '?') { /* C++ operator code (one character, or two if the first is '_') */ switch (*++name) { case '0': function_name = strdup ("ctor"); break; case '1': function_name = strdup ("dtor"); break; case '2': function_name = strdup ("operator_new"); break; case '3': function_name = strdup ("operator_delete"); break; case '4': function_name = strdup ("operator_equals"); break; case '5': function_name = strdup ("operator_shiftright"); break; case '6': function_name = strdup ("operator_shiftleft"); break; case '7': function_name = strdup ("operator_not"); break; case '8': function_name = strdup ("operator_equalsequals"); break; case '9': function_name = strdup ("operator_notequals"); break; case 'A': function_name = strdup ("operator_array"); break; case 'C': function_name = strdup ("operator_dereference"); break; case 'D': function_name = strdup ("operator_multiply"); break; case 'E': function_name = strdup ("operator_plusplus"); break; case 'F': function_name = strdup ("operator_minusminus"); break; case 'G': function_name = strdup ("operator_minus"); break; case 'H': function_name = strdup ("operator_plus"); break; case 'I': function_name = strdup ("operator_address"); break; case 'J': function_name = strdup ("operator_dereferencememberptr"); break; case 'K': function_name = strdup ("operator_divide"); break; case 'L': function_name = strdup ("operator_modulo"); break; case 'M': function_name = strdup ("operator_lessthan"); break; case 'N': function_name = strdup ("operator_lessthanequal"); break; case 'O': function_name = strdup ("operator_greaterthan"); break; case 'P': function_name = strdup ("operator_greaterthanequal"); break; case 'Q': function_name = strdup ("operator_comma"); break; case 'R': function_name = strdup ("operator_functioncall"); break; case 'S': function_name = strdup ("operator_complement"); break; case 'T': function_name = strdup ("operator_xor"); break; case 'U': function_name = strdup ("operator_logicalor"); break; case 'V': function_name = strdup ("operator_logicaland"); break; case 'W': function_name = strdup ("operator_or"); break; case 'X': function_name = strdup ("operator_multiplyequals"); break; case 'Y': function_name = strdup ("operator_plusequals"); break; case 'Z': function_name = strdup ("operator_minusequals"); break; case '_': switch (*++name) { case '0': function_name = strdup ("operator_divideequals"); break; case '1': function_name = strdup ("operator_moduloequals"); break; case '2': function_name = strdup ("operator_shiftrightequals"); break; case '3': function_name = strdup ("operator_shiftleftequals"); break; case '4': function_name = strdup ("operator_andequals"); break; case '5': function_name = strdup ("operator_orequals"); break; case '6': function_name = strdup ("operator_xorequals"); break; case '7': function_name = strdup ("vftable"); data_flags = DATA_VTABLE; break; case '8': function_name = strdup ("vbtable"); data_flags = DATA_VTABLE; break; case '9': function_name = strdup ("vcall"); data_flags = DATA_VTABLE; break; case 'A': function_name = strdup ("typeof"); data_flags = DATA_VTABLE; break; case 'B': function_name = strdup ("local_static_guard"); data_flags = DATA_VTABLE; break; case 'C': function_name = strdup ("string"); data_flags = DATA_VTABLE; break; case 'D': function_name = strdup ("vbase_dtor"); data_flags = DATA_VTABLE; break; case 'E': function_name = strdup ("vector_dtor"); break; case 'G': function_name = strdup ("scalar_dtor"); break; case 'H': function_name = strdup ("vector_ctor_iter"); break; case 'I': function_name = strdup ("vector_dtor_iter"); break; case 'J': function_name = strdup ("vector_vbase_ctor_iter"); break; case 'L': function_name = strdup ("eh_vector_ctor_iter"); break; case 'M': function_name = strdup ("eh_vector_dtor_iter"); break; case 'N': function_name = strdup ("eh_vector_vbase_ctor_iter"); break; case 'O': function_name = strdup ("copy_ctor_closure"); break; case 'S': function_name = strdup ("local_vftable"); data_flags = DATA_VTABLE; break; case 'T': function_name = strdup ("local_vftable_ctor_closure"); break; case 'U': function_name = strdup ("operator_new_vector"); break; case 'V': function_name = strdup ("operator_delete_vector"); break; case 'X': function_name = strdup ("placement_new_closure"); break; case 'Y': function_name = strdup ("placement_delete_closure"); break; default: return -1; } break; default: /* FIXME: Other operators */ return -1; } name++; } else { /* Type or function name terminated by '@' */ function_name = name; while (*name && *name++ != '@') ; if (!*name) return -1; function_name = str_substring (function_name, name - 1); } /* Either a class name, or '@' if the symbol is not a class member */ if (*name == '@') { class_name = strdup ("global"); /* Non member function (or a datatype) */ name++; } else { /* Class the function is associated with, terminated by '@@' */ class_name = name; while (*name && *name++ != '@') ; if (*name++ != '@') { free (function_name); return -1; } class_name = str_substring (class_name, name - 2); /* Allocates a new string */ } /* Function/Data type and access level */ /* FIXME: why 2 possible letters for each option? */ switch(*name++) { /* Data */ case '0' : /* private static */ case '1' : /* protected static */ case '2' : /* public static */ is_static = 1; /* Fall through */ case '3' : /* non static */ case '4' : /* non static */ /* Data members need to be implemented: report */ INIT_CT (ct); if (!demangle_datatype (&name, &ct, sym)) { if (VERBOSE) printf ("/*FIXME: %s: unknown data*/\n", sym->symbol); free (function_name); free (class_name); return -1; } sym->flags |= SYM_DATA; sym->argc = 1; sym->arg_name[0] = str_create (5, OUTPUT_UC_DLL_NAME, "_", class_name, is_static ? "static_" : "_", function_name); sym->arg_text[0] = str_create (3, ct.expression, " ", sym->arg_name[0]); FREE_CT (ct); free (function_name); free (class_name); return 0; case '6' : /* compiler generated static */ case '7' : /* compiler generated static */ if (data_flags & DATA_VTABLE) { sym->flags |= SYM_DATA; sym->argc = 1; sym->arg_name[0] = str_create (5, OUTPUT_UC_DLL_NAME, "_", class_name, "_", function_name); sym->arg_text[0] = str_create (2, "void *", sym->arg_name[0]); if (VERBOSE) puts ("Demangled symbol OK [vtable]"); free (function_name); free (class_name); return 0; } free (function_name); free (class_name); return -1; /* Functions */ case 'E' : /* private virtual */ case 'F' : /* private virtual */ case 'M' : /* protected virtual */ case 'N' : /* protected virtual */ case 'U' : /* public virtual */ case 'V' : /* public virtual */ /* Virtual functions need to be added to the exported vtable: report */ if (VERBOSE) printf ("/*FIXME %s: %s::%s is virtual-add to vftable*/\n", sym->symbol, class_name, function_name); /* Fall through */ case 'A' : /* private */ case 'B' : /* private */ case 'I' : /* protected */ case 'J' : /* protected */ case 'Q' : /* public */ case 'R' : /* public */ /* Implicit 'this' pointer */ sym->arg_text [sym->argc] = str_create (3, "struct ", class_name, " *"); sym->arg_type [sym->argc] = ARG_POINTER; sym->arg_flag [sym->argc] = 0; sym->arg_name [sym->argc++] = strdup ("_this"); /* New struct definitions can be 'grep'ed out for making a fixup header */ if (VERBOSE) printf ("struct %s { void **vtable; /*FIXME: class definition */ };\n", class_name); break; case 'C' : /* private: static */ case 'D' : /* private: static */ case 'K' : /* protected: static */ case 'L' : /* protected: static */ case 'S' : /* public: static */ case 'T' : /* public: static */ is_static = 1; /* No implicit this pointer */ break; case 'Y' : case 'Z' : break; /* FIXME: G,H / O,P / W,X are private / protected / public thunks */ default: free (function_name); free (class_name); return -1; } /* If there is an implicit this pointer, const status follows */ if (sym->argc) { switch (*name++) { case 'A': break; /* non-const */ case 'B': is_const = CT_CONST; break; case 'C': is_const = CT_VOLATILE; break; case 'D': is_const = (CT_CONST | CT_VOLATILE); break; default: free (function_name); free (class_name); return -1; } } /* Next is the calling convention */ switch (*name++) { case 'A': /* __cdecl */ case 'B': /* __cdecl __declspec(dllexport) */ if (!sym->argc) { sym->flags |= SYM_CDECL; break; } /* Else fall through */ case 'C': /* __pascal */ case 'D': /* __pascal __declspec(dllexport) */ case 'E': /* __thiscall */ case 'F': /* __thiscall __declspec(dllexport) */ case 'G': /* __stdcall */ case 'H': /* __stdcall __declspec(dllexport) */ case 'I': /* __fastcall */ case 'J': /* __fastcall __declspec(dllexport)*/ case 'K': /* default (none given) */ if (sym->argc) sym->flags |= SYM_THISCALL; else sym->flags |= SYM_STDCALL; break; default: free (function_name); free (class_name); return -1; } /* Return type, or @ if 'void' */ if (*name == '@') { sym->return_text = strdup ("void"); sym->return_type = ARG_VOID; name++; } else { INIT_CT (ct); if (!demangle_datatype (&name, &ct, sym)) { free (function_name); free (class_name); return -1; } sym->return_text = ct.expression; sym->return_type = get_type_constant(ct.dest_type, ct.flags); ct.expression = NULL; FREE_CT (ct); } /* Now come the function arguments */ while (*name && *name != 'Z') { /* Decode each data type and append it to the argument list */ if (*name != '@') { INIT_CT (ct); if (!demangle_datatype(&name, &ct, sym)) { free (function_name); free (class_name); return -1; } if (strcmp (ct.expression, "void")) { sym->arg_text [sym->argc] = ct.expression; ct.expression = NULL; sym->arg_type [sym->argc] = get_type_constant (ct.dest_type, ct.flags); sym->arg_flag [sym->argc] = ct.flags; sym->arg_name[sym->argc] = str_create_num (1, sym->argc, "arg"); sym->argc++; } else break; /* 'void' terminates an argument list */ FREE_CT (ct); } else name++; } while (*name == '@') name++; /* Functions are always terminated by 'Z'. If we made it this far and * Don't find it, we have incorrectly identified a data type. */ if (*name != 'Z') { free (function_name); free (class_name); return -1; } /* Note: '()' after 'Z' means 'throws', but we don't care here */ /* Create the function name. Include a unique number because otherwise * overloaded functions could have the same c signature. */ switch (is_const) { case (CT_CONST | CT_VOLATILE): const_status = "_const_volatile"; break; case CT_CONST: const_status = "_const"; break; case CT_VOLATILE: const_status = "_volatile"; break; default: const_status = "_"; break; } sym->function_name = str_create_num (4, hash, class_name, "_", function_name, is_static ? "_static" : const_status); assert (sym->return_text); assert (sym->flags); assert (sym->function_name); free (class_name); free (function_name); if (VERBOSE) puts ("Demangled symbol OK"); return 0; }
/******************************************************************* * get_type * * Read a type from a prototype */ static const char *get_type (parsed_symbol *sym, const char *proto, int arg) { int is_const, is_volatile, is_struct, is_signed, is_unsigned, ptrs = 0; const char *iter, *type_str, *base_type, *catch_unsigned; char dest_type; assert (sym && sym->symbol); assert (proto && *proto); assert (arg < 0 || (unsigned)arg == sym->argc); type_str = proto; proto = str_match (proto, "const", &is_const); proto = str_match (proto, "volatile", &is_volatile); proto = str_match (proto, "struct", &is_struct); if (!is_struct) proto = str_match (proto, "union", &is_struct); catch_unsigned = proto; proto = str_match (proto, "unsigned", &is_unsigned); proto = str_match (proto, "signed", &is_signed); /* Can have 'unsigned const' or 'const unsigned' etc */ if (!is_const) proto = str_match (proto, "const", &is_const); if (!is_volatile) proto = str_match (proto, "volatile", &is_volatile); base_type = proto; iter = str_find_set (proto, " ,*)"); if (!iter) return NULL; if (arg < 0 && (is_signed || is_unsigned)) { /* Prevent calling convention from being swallowed by 'un/signed' alone */ if (strncmp (base_type, "int", 3) && strncmp (base_type, "long", 4) && strncmp (base_type, "short", 5) && strncmp (base_type, "char", 4)) { iter = proto; base_type = catch_unsigned; } else catch_unsigned = NULL; } else catch_unsigned = NULL; /* FIXME: skip const/volatile here too */ for (proto = iter; *proto; proto++) if (*proto == '*') ptrs++; else if (*proto != ' ') break; if (!*proto) return NULL; type_str = str_substring (type_str, proto); if (iter == base_type || catch_unsigned) { /* 'unsigned' with no type */ char *tmp = str_create (2, type_str, " int"); free ((char*)type_str); type_str = tmp; } symbol_clean_string (type_str); dest_type = symbol_get_type (type_str); if (arg < 0) { sym->return_text = (char*)type_str; sym->return_type = dest_type; } else { sym->arg_type [arg] = dest_type; sym->arg_flag [arg] = is_const ? CT_CONST : is_volatile ? CT_VOLATILE : 0; if (*proto == ',' || *proto == ')') sym->arg_name [arg] = str_create_num (1, arg, "arg"); else { iter = str_find_set (proto, " ,)"); if (!iter) { free ((char*)type_str); return NULL; } sym->arg_name [arg] = str_substring (proto, iter); proto = iter; } sym->arg_text [arg] = (char*)type_str; } return proto; }