2 * Demangle VC++ symbols into C function prototypes
4 * Copyright 2000 Jon Griffiths
8 /* Type for parsing mangled types */
9 typedef struct _compound_type
18 /* Initialise a compound type structure */
19 #define INIT_CT(ct) do { memset (&ct, 0, sizeof (ct)); } while (0)
21 /* free the memory used by a compound structure */
22 #define FREE_CT(ct) do { if (ct.expression) free (ct.expression); } while (0)
24 /* Flags for data types */
25 #define DATA_VTABLE 0x1
27 /* Internal functions */
28 static char *demangle_datatype (char **str
, compound_type
*ct
,
31 static char *get_constraints_convention_1 (char **str
, compound_type
*ct
);
33 static char *get_constraints_convention_2 (char **str
, compound_type
*ct
);
35 static char *get_type_string (const char c
, const int constraints
);
37 static int get_type_constant (const char c
, const int constraints
);
39 static char *get_pointer_type_string (compound_type
*ct
,
40 const char *expression
);
43 /*******************************************************************
46 * Demangle a C++ linker symbol into a C prototype
48 int symbol_demangle (parsed_symbol
*sym
)
51 int is_static
= 0, is_const
= 0;
52 char *function_name
= NULL
;
53 char *class_name
= NULL
;
54 char *name
, *const_status
;
55 static unsigned int hash
= 0; /* In case of overloaded functions */
56 unsigned int data_flags
= 0;
58 assert (globals
.do_code
);
59 assert (sym
&& sym
->symbol
);
63 /* MS mangled names always begin with '?' */
69 puts ("Attempting to demangle symbol");
71 /* Then function name or operator code */
74 /* C++ operator code (one character, or two if the first is '_') */
77 case '0': function_name
= strdup ("ctor"); break;
78 case '1': function_name
= strdup ("dtor"); break;
79 case '2': function_name
= strdup ("operator_new"); break;
80 case '3': function_name
= strdup ("operator_delete"); break;
81 case '4': function_name
= strdup ("operator_equals"); break;
82 case '5': function_name
= strdup ("operator_shiftright"); break;
83 case '6': function_name
= strdup ("operator_shiftleft"); break;
84 case '7': function_name
= strdup ("operator_not"); break;
85 case '8': function_name
= strdup ("operator_equalsequals"); break;
86 case '9': function_name
= strdup ("operator_notequals"); break;
87 case 'A': function_name
= strdup ("operator_array"); break;
88 case 'C': function_name
= strdup ("operator_dereference"); break;
89 case 'D': function_name
= strdup ("operator_multiply"); break;
90 case 'E': function_name
= strdup ("operator_plusplus"); break;
91 case 'F': function_name
= strdup ("operator_minusminus"); break;
92 case 'G': function_name
= strdup ("operator_minus"); break;
93 case 'H': function_name
= strdup ("operator_plus"); break;
94 case 'I': function_name
= strdup ("operator_address"); break;
95 case 'J': function_name
= strdup ("operator_dereferencememberptr"); break;
96 case 'K': function_name
= strdup ("operator_divide"); break;
97 case 'L': function_name
= strdup ("operator_modulo"); break;
98 case 'M': function_name
= strdup ("operator_lessthan"); break;
99 case 'N': function_name
= strdup ("operator_lessthanequal"); break;
100 case 'O': function_name
= strdup ("operator_greaterthan"); break;
101 case 'P': function_name
= strdup ("operator_greaterthanequal"); break;
102 case 'Q': function_name
= strdup ("operator_comma"); break;
103 case 'R': function_name
= strdup ("operator_functioncall"); break;
104 case 'S': function_name
= strdup ("operator_complement"); break;
105 case 'T': function_name
= strdup ("operator_xor"); break;
106 case 'U': function_name
= strdup ("operator_logicalor"); break;
107 case 'V': function_name
= strdup ("operator_logicaland"); break;
108 case 'W': function_name
= strdup ("operator_or"); break;
109 case 'X': function_name
= strdup ("operator_multiplyequals"); break;
110 case 'Y': function_name
= strdup ("operator_plusequals"); break;
111 case 'Z': function_name
= strdup ("operator_minusequals"); break;
115 case '0': function_name
= strdup ("operator_divideequals"); break;
116 case '1': function_name
= strdup ("operator_moduloequals"); break;
117 case '2': function_name
= strdup ("operator_shiftrightequals"); break;
118 case '3': function_name
= strdup ("operator_shiftleftequals"); break;
119 case '4': function_name
= strdup ("operator_andequals"); break;
120 case '5': function_name
= strdup ("operator_orequals"); break;
121 case '6': function_name
= strdup ("operator_xorequals"); break;
122 case '7': function_name
= strdup ("vftable"); data_flags
= DATA_VTABLE
; break;
123 case '8': function_name
= strdup ("vbtable"); data_flags
= DATA_VTABLE
; break;
124 case '9': function_name
= strdup ("vcall"); data_flags
= DATA_VTABLE
; break;
125 case 'A': function_name
= strdup ("typeof"); data_flags
= DATA_VTABLE
; break;
126 case 'B': function_name
= strdup ("local_static_guard"); data_flags
= DATA_VTABLE
; break;
127 case 'C': function_name
= strdup ("string"); data_flags
= DATA_VTABLE
; break;
128 case 'D': function_name
= strdup ("vbase_dtor"); data_flags
= DATA_VTABLE
; break;
129 case 'E': function_name
= strdup ("vector_dtor"); break;
130 case 'G': function_name
= strdup ("scalar_dtor"); break;
131 case 'H': function_name
= strdup ("vector_ctor_iter"); break;
132 case 'I': function_name
= strdup ("vector_dtor_iter"); break;
133 case 'J': function_name
= strdup ("vector_vbase_ctor_iter"); break;
134 case 'L': function_name
= strdup ("eh_vector_ctor_iter"); break;
135 case 'M': function_name
= strdup ("eh_vector_dtor_iter"); break;
136 case 'N': function_name
= strdup ("eh_vector_vbase_ctor_iter"); break;
137 case 'O': function_name
= strdup ("copy_ctor_closure"); break;
138 case 'S': function_name
= strdup ("local_vftable"); data_flags
= DATA_VTABLE
; break;
139 case 'T': function_name
= strdup ("local_vftable_ctor_closure"); break;
140 case 'U': function_name
= strdup ("operator_new_vector"); break;
141 case 'V': function_name
= strdup ("operator_delete_vector"); break;
142 case 'X': function_name
= strdup ("placement_new_closure"); break;
143 case 'Y': function_name
= strdup ("placement_delete_closure"); break;
149 /* FIXME: Other operators */
156 /* Type or function name terminated by '@' */
157 function_name
= name
;
158 while (*name
&& *name
++ != '@') ;
161 function_name
= str_substring (function_name
, name
- 1);
164 /* Either a class name, or '@' if the symbol is not a class member */
167 class_name
= strdup ("global"); /* Non member function (or a datatype) */
172 /* Class the function is associated with, terminated by '@@' */
174 while (*name
&& *name
++ != '@') ;
177 class_name
= str_substring (class_name
, name
- 2);
180 /* Function/Data type and access level */
181 /* FIXME: why 2 possible letters for each option? */
186 case '0' : /* private static */
187 case '1' : /* protected static */
188 case '2' : /* public static */
191 case '3' : /* non static */
192 case '4' : /* non static */
193 /* Data members need to be implemented: report */
195 if (!demangle_datatype (&name
, &ct
, sym
))
198 printf ("/*FIXME: %s: unknown data*/\n", sym
->symbol
);
201 sym
->flags
|= SYM_DATA
;
203 sym
->arg_name
[0] = str_create (5, OUTPUT_UC_DLL_NAME
, "_", class_name
,
204 is_static
? "static_" : "_", function_name
);
205 sym
->arg_text
[0] = str_create (3, ct
.expression
, " ", sym
->arg_name
[0]);
210 case '6' : /* compiler generated static */
211 case '7' : /* compiler generated static */
212 if (data_flags
& DATA_VTABLE
)
214 sym
->flags
|= SYM_DATA
;
216 sym
->arg_name
[0] = str_create (5, OUTPUT_UC_DLL_NAME
, "_", class_name
,
218 sym
->arg_text
[0] = str_create (2, "void *", sym
->arg_name
[0]);
221 puts ("Demangled symbol OK [vtable]");
229 case 'E' : /* private virtual */
230 case 'F' : /* private virtual */
231 case 'M' : /* protected virtual */
232 case 'N' : /* protected virtual */
233 case 'U' : /* public virtual */
234 case 'V' : /* public virtual */
235 /* Virtual functions need to be added to the exported vtable: report */
237 printf ("/*FIXME %s: %s::%s is virtual-add to vftable*/\n", sym
->symbol
,
238 class_name
, function_name
);
240 case 'A' : /* private */
241 case 'B' : /* private */
242 case 'I' : /* protected */
243 case 'J' : /* protected */
244 case 'Q' : /* public */
245 case 'R' : /* public */
246 /* Implicit 'this' pointer */
247 sym
->arg_text
[sym
->argc
] = str_create (3, "struct ", class_name
, " *");
248 sym
->arg_type
[sym
->argc
] = ARG_POINTER
;
249 sym
->arg_flag
[sym
->argc
] = 0;
250 sym
->arg_name
[sym
->argc
++] = strdup ("_this");
251 /* New struct definitions can be 'grep'ed out for making a fixup header */
253 printf ("struct %s { void **vtable; /*FIXME: class definition */ };\n", class_name
);
255 case 'C' : /* private: static */
256 case 'D' : /* private: static */
257 case 'K' : /* protected: static */
258 case 'L' : /* protected: static */
259 case 'S' : /* public: static */
260 case 'T' : /* public: static */
261 is_static
= 1; /* No implicit this pointer */
266 /* FIXME: G,H / O,P / W,X are private / protected / public thunks */
271 /* If there is an implicit this pointer, const status follows */
276 case 'A': break; /* non-const */
277 case 'B': is_const
= CT_CONST
; break;
278 case 'C': is_const
= CT_VOLATILE
; break;
279 case 'D': is_const
= (CT_CONST
| CT_VOLATILE
); break;
285 /* Next is the calling convention */
288 case 'A': /* __cdecl */
289 case 'B': /* __cdecl __declspec(dllexport) */
292 sym
->flags
|= SYM_CDECL
;
295 /* Else fall through */
296 case 'C': /* __pascal */
297 case 'D': /* __pascal __declspec(dllexport) */
298 case 'E': /* __thiscall */
299 case 'F': /* __thiscall __declspec(dllexport) */
300 case 'G': /* __stdcall */
301 case 'H': /* __stdcall __declspec(dllexport) */
302 case 'I': /* __fastcall */
303 case 'J': /* __fastcall __declspec(dllexport)*/
304 case 'K': /* default (none given) */
306 sym
->flags
|= SYM_THISCALL
;
308 sym
->flags
|= SYM_STDCALL
;
314 /* Return type, or @ if 'void' */
317 sym
->return_text
= strdup ("void");
318 sym
->return_type
= ARG_VOID
;
324 if (!demangle_datatype (&name
, &ct
, sym
))
326 sym
->return_text
= ct
.expression
;
327 sym
->return_type
= get_type_constant(ct
.dest_type
, ct
.flags
);
328 ct
.expression
= NULL
;
332 /* Now come the function arguments */
333 while (*name
&& *name
!= 'Z')
335 /* Decode each data type and append it to the argument list */
339 if (!demangle_datatype(&name
, &ct
, sym
))
342 if (strcmp (ct
.expression
, "void"))
344 sym
->arg_text
[sym
->argc
] = ct
.expression
;
345 ct
.expression
= NULL
;
346 sym
->arg_type
[sym
->argc
] = get_type_constant (ct
.dest_type
, ct
.flags
);
347 sym
->arg_flag
[sym
->argc
] = ct
.flags
;
348 sym
->arg_name
[sym
->argc
] = str_create_num (1, sym
->argc
, "arg");
352 break; /* 'void' terminates an argument list */
362 /* Functions are always terminated by 'Z'. If we made it this far and
363 * Don't find it, we have incorrectly identified a data type.
368 /* Note: '()' after 'Z' means 'throws', but we don't care here */
370 /* Create the function name. Include a unique number because otherwise
371 * overloaded functions could have the same c signature.
375 case (CT_CONST
| CT_VOLATILE
): const_status
= "_const_volatile"; break;
376 case CT_CONST
: const_status
= "_const"; break;
377 case CT_VOLATILE
: const_status
= "_volatile"; break;
378 default: const_status
= "_"; break;
380 sym
->function_name
= str_create_num (4, hash
, class_name
, "_",
381 function_name
, is_static
? "_static" : const_status
);
383 assert (sym
->return_text
);
385 assert (sym
->function_name
);
388 free (function_name
);
391 puts ("Demangled symbol OK");
397 /*******************************************************************
400 * Attempt to demangle a C++ data type, which may be compound.
401 * a compound type is made up of a number of simple types. e.g:
402 * char** = (pointer to (pointer to (char)))
404 * Uses a simple recursive descent algorithm that is broken
405 * and/or incomplete, without a doubt ;-)
407 static char *demangle_datatype (char **str
, compound_type
*ct
,
412 assert (str
&& *str
);
417 if (!get_constraints_convention_1 (&iter
, ct
))
422 /* MS type: __int8,__int16 etc */
423 ct
->flags
|= CT_EXTENDED
;
429 case 'C': case 'D': case 'E': case 'F': case 'G':
430 case 'H': case 'I': case 'J': case 'K': case 'M':
431 case 'N': case 'O': case 'X': case 'Z':
432 /* Simple data types */
433 ct
->dest_type
= *iter
++;
434 if (!get_constraints_convention_2 (&iter
, ct
))
436 ct
->expression
= get_type_string (ct
->dest_type
, ct
->flags
);
440 /* Class/struct/union */
441 ct
->dest_type
= *iter
++;
442 if (*iter
== '0' || *iter
== '1')
444 /* Referring to class type (implicit 'this') */
450 /* Apply our constraints to the base type (struct xxx *) */
451 stripped
= strdup (sym
->arg_text
[0]);
453 fatal ("Out of Memory");
455 /* If we're a reference, re-use the pointer already in the type */
456 if (!ct
->flags
& CT_BY_REFERENCE
)
457 stripped
[ strlen (stripped
) - 2] = '\0'; /* otherwise, strip it */
459 ct
->expression
= str_create (2, ct
->flags
& CT_CONST
? "const " :
460 ct
->flags
& CT_VOLATILE
? "volatile " : "", stripped
);
463 else if (*iter
!= '@')
465 /* The name of the class/struct, followed by '@@' */
466 char *struct_name
= iter
;
467 while (*iter
&& *iter
++ != '@') ;
470 struct_name
= str_substring (struct_name
, iter
- 2);
471 ct
->expression
= str_create (4, ct
->flags
& CT_CONST
? "const " :
472 ct
->flags
& CT_VOLATILE
? "volatile " : "", "struct ",
473 struct_name
, ct
->flags
& CT_BY_REFERENCE
? " *" : "");
477 case 'Q': /* FIXME: Array Just treated as pointer currently */
478 case 'P': /* Pointer */
480 compound_type sub_ct
;
483 ct
->dest_type
= *iter
++;
484 if (!get_constraints_convention_2 (&iter
, ct
))
487 /* FIXME: P6 = Function pointer, others who knows.. */
492 /* FIXME: there are a tons of memory leaks here */
493 /* FIXME: this is still broken in some cases and it has to be
494 * merged with the function prototype parsing above...
496 iter
+= 3; /* FIXME */
497 if (!demangle_datatype (&iter
, &sub_ct
, sym
))
499 ct
->expression
= str_create(2, sub_ct
.expression
, " (*)(");
506 if (!demangle_datatype (&iter
, &sub_ct
, sym
))
508 ct
->expression
= str_create(3, ct
->expression
, ", ", sub_ct
.expression
);
509 while (*iter
== '@') iter
++;
511 } else while (*iter
== '@') iter
++;
513 ct
->expression
= str_create(2, ct
->expression
, ")");
521 /* Recurse to get the pointed-to type */
522 if (!demangle_datatype (&iter
, &sub_ct
, sym
))
525 ct
->expression
= get_pointer_type_string (ct
, sub_ct
.expression
);
531 case '0': case '1': case '2': case '3': case '4':
532 case '5': case '6': case '7': case '8': case '9':
533 /* Referring back to previously parsed type */
534 if (sym
->argc
>= (size_t)('0' - *iter
))
536 ct
->dest_type
= sym
->arg_type
['0' - *iter
];
537 ct
->expression
= strdup (sym
->arg_text
['0' - *iter
]);
546 return (char *)(*str
= iter
);
551 * There are two conventions for specifying data type constaints. I
552 * don't know how the compiler chooses between them, but I suspect it
553 * is based on ensuring that linker names are unique.
554 * Convention 1. The data type modifier is given first, followed
555 * by the data type it operates on. '?' means passed by value,
556 * 'A' means passed by reference. Note neither of these characters
557 * is a valid base data type. This is then followed by a character
558 * specifying constness or volatilty.
559 * Convention 2. The base data type (which is never '?' or 'A') is
560 * given first. The character modifier is optionally given after
561 * the base type character. If a valid character mofifier is present,
562 * then it only applies to the current data type if the character
563 * after that is not 'A' 'B' or 'C' (Because this makes a convention 1
564 * constraint for the next data type).
566 * The conventions are usually mixed within the same symbol.
567 * Since 'C' is both a qualifier and a data type, I suspect that
568 * convention 1 allows specifying e.g. 'volatile signed char*'. In
569 * convention 2 this would be 'CC' which is ambigious (i.e. Is it two
570 * pointers, or a single pointer + modifier?). In convention 1 it
571 * is encoded as '?CC' which is not ambigious. This probably
572 * holds true for some other types as well.
575 /*******************************************************************
576 * get_constraints_convention_1
578 * Get type constraint information for a data type
580 static char *get_constraints_convention_1 (char **str
, compound_type
*ct
)
582 char *iter
= *str
, **retval
= str
;
584 if (ct
->have_qualifiers
)
585 return (char *)*str
; /* Previously got constraints for this type */
587 if (*iter
== '?' || *iter
== 'A')
589 ct
->have_qualifiers
= 1;
590 ct
->flags
|= (*iter
++ == '?' ? 0 : CT_BY_REFERENCE
);
595 break; /* non-const, non-volatile */
597 ct
->flags
|= CT_CONST
;
600 ct
->flags
|= CT_VOLATILE
;
607 return (char *)(*retval
= iter
);
611 /*******************************************************************
612 * get_constraints_convention_2
614 * Get type constraint information for a data type
616 static char *get_constraints_convention_2 (char **str
, compound_type
*ct
)
618 char *iter
= *str
, **retval
= str
;
620 /* FIXME: Why do arrays have both convention 1 & 2 constraints? */
621 if (ct
->have_qualifiers
&& ct
->dest_type
!= 'Q')
622 return (char *)*str
; /* Previously got constraints for this type */
624 ct
->have_qualifiers
= 1; /* Even if none, we've got all we're getting */
629 if (iter
[1] != 'A' && iter
[1] != 'B' && iter
[1] != 'C')
633 ct
->flags
|= CT_CONST
;
637 /* See note above, if we find 'C' it is _not_ a signed char */
638 ct
->flags
|= CT_VOLATILE
;
643 return (char *)(*retval
= iter
);
647 /*******************************************************************
650 * Return a string containing the name of a data type
652 static char *get_type_string (const char c
, const int constraints
)
656 if (constraints
& CT_EXTENDED
)
660 case 'D': type_string
= "__int8"; break;
661 case 'E': type_string
= "__uint8"; break;
662 case 'F': type_string
= "__int16"; break;
663 case 'G': type_string
= "__uint16"; break;
664 case 'H': type_string
= "__int32"; break;
665 case 'I': type_string
= "__uint32"; break;
666 case 'J': type_string
= "__int64"; break;
667 case 'K': type_string
= "__uint64"; break;
668 case 'L': type_string
= "__int128"; break;
669 case 'M': type_string
= "__uint128"; break;
670 case 'N': type_string
= "int"; break; /* bool */
671 case 'W': type_string
= "WCHAR"; break; /* wchar_t */
680 case 'C': /* Signed char, fall through */
681 case 'D': type_string
= "char"; break;
682 case 'E': type_string
= "unsigned char"; break;
683 case 'F': type_string
= "short int"; break;
684 case 'G': type_string
= "unsigned short int"; break;
685 case 'H': type_string
= "int"; break;
686 case 'I': type_string
= "unsigned int"; break;
687 case 'J': type_string
= "long"; break;
688 case 'K': type_string
= "unsigned long"; break;
689 case 'M': type_string
= "float"; break;
690 case 'N': type_string
= "double"; break;
691 case 'O': type_string
= "long double"; break;
692 /* FIXME: T = union */
694 case 'V': type_string
= "struct"; break;
695 case 'X': return strdup ("void");
696 case 'Z': return strdup ("...");
702 return str_create (3, constraints
& CT_CONST
? "const " :
703 constraints
& CT_VOLATILE
? "volatile " : "", type_string
,
704 constraints
& CT_BY_REFERENCE
? " *" : "");
708 /*******************************************************************
711 * Get the ARG_* constant for this data type
713 static int get_type_constant (const char c
, const int constraints
)
715 /* Any reference type is really a pointer */
716 if (constraints
& CT_BY_REFERENCE
)
721 case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I':
725 return -1; /* FIXME */
741 /*******************************************************************
742 * get_pointer_type_string
744 * Return a string containing 'pointer to expression'
746 static char *get_pointer_type_string (compound_type
*ct
,
747 const char *expression
)
749 /* FIXME: set a compound flag for bracketing expression if needed */
750 return str_create (3, ct
->flags
& CT_CONST
? "const " :
751 ct
->flags
& CT_VOLATILE
? "volatile " : "", expression
,
752 ct
->flags
& CT_BY_REFERENCE
? " **" : " *");