mirrored from https://gitlab.winehq.org/wine/wine.git
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathundname.c
1789 lines (1638 loc) · 58.6 KB
/
undname.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Demangle VC++ symbols into C function prototypes
*
* Copyright 2000 Jon Griffiths
* 2004 Eric Pouech
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "msvcrt.h"
#include "winver.h"
#include "imagehlp.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
/* How data types qualifiers are stored:
* M (in the following definitions) is defined for
* 'A', 'B', 'C' and 'D' as follows
* {<A>}: ""
* {<B>}: "const "
* {<C>}: "volatile "
* {<D>}: "const volatile "
*
* in arguments:
* P<M>x {<M>}x*
* Q<M>x {<M>}x* const
* A<M>x {<M>}x&
* in data fields:
* same as for arguments and also the following
* ?<M>x {<M>}x
*
*/
#define UNDNAME_NO_COMPLEX_TYPE (0x8000)
struct array
{
unsigned start; /* first valid reference in array */
unsigned num; /* total number of used elts */
unsigned max;
unsigned alloc;
char** elts;
};
/* Structure holding a parsed symbol */
struct parsed_symbol
{
unsigned flags; /* the UNDNAME_ flags used for demangling */
malloc_func_t mem_alloc_ptr; /* internal allocator */
free_func_t mem_free_ptr; /* internal deallocator */
const char* current; /* pointer in input (mangled) string */
char* result; /* demangled string */
struct array names; /* array of names for back reference */
struct array args; /* array of arguments for back reference */
struct array stack; /* stack of parsed strings */
void* alloc_list; /* linked list of allocated blocks */
unsigned avail_in_first; /* number of available bytes in head block */
};
enum datatype_e
{
DT_NO_LEADING_WS = 0x01,
DT_NO_LRSEP_WS = 0x02,
};
/* Type for parsing mangled types */
struct datatype_t
{
const char* left;
const char* right;
enum datatype_e flags;
};
static BOOL symbol_demangle(struct parsed_symbol* sym);
static char* get_class_name(struct parsed_symbol* sym);
/******************************************************************
* und_alloc
*
* Internal allocator. Uses a simple linked list of large blocks
* where we use a poor-man allocator. It's fast, and since all
* allocation is pool, memory management is easy (esp. freeing).
*/
static void* und_alloc(struct parsed_symbol* sym, unsigned int len)
{
void* ptr;
#define BLOCK_SIZE 1024
#define AVAIL_SIZE (1024 - sizeof(void*))
if (len > AVAIL_SIZE)
{
/* allocate a specific block */
ptr = sym->mem_alloc_ptr(sizeof(void*) + len);
if (!ptr) return NULL;
*(void**)ptr = sym->alloc_list;
sym->alloc_list = ptr;
sym->avail_in_first = 0;
ptr = (char*)sym->alloc_list + sizeof(void*);
}
else
{
if (len > sym->avail_in_first)
{
/* add a new block */
ptr = sym->mem_alloc_ptr(BLOCK_SIZE);
if (!ptr) return NULL;
*(void**)ptr = sym->alloc_list;
sym->alloc_list = ptr;
sym->avail_in_first = AVAIL_SIZE;
}
/* grab memory from head block */
ptr = (char*)sym->alloc_list + BLOCK_SIZE - sym->avail_in_first;
sym->avail_in_first -= len;
}
return ptr;
#undef BLOCK_SIZE
#undef AVAIL_SIZE
}
/******************************************************************
* und_free
* Frees all the blocks in the list of large blocks allocated by
* und_alloc.
*/
static void und_free_all(struct parsed_symbol* sym)
{
void* next;
while (sym->alloc_list)
{
next = *(void**)sym->alloc_list;
if(sym->mem_free_ptr) sym->mem_free_ptr(sym->alloc_list);
sym->alloc_list = next;
}
sym->avail_in_first = 0;
}
/******************************************************************
* str_array_init
* Initialises an array of strings
*/
static void str_array_init(struct array* a)
{
a->start = a->num = a->max = a->alloc = 0;
a->elts = NULL;
}
/******************************************************************
* str_array_push
* Adding a new string to an array
*/
static BOOL str_array_push(struct parsed_symbol* sym, const char* ptr, int len,
struct array* a)
{
char** new;
assert(ptr);
assert(a);
if (!a->alloc)
{
new = und_alloc(sym, (a->alloc = 32) * sizeof(a->elts[0]));
if (!new) return FALSE;
a->elts = new;
}
else if (a->max >= a->alloc)
{
new = und_alloc(sym, (a->alloc * 2) * sizeof(a->elts[0]));
if (!new) return FALSE;
memcpy(new, a->elts, a->alloc * sizeof(a->elts[0]));
a->alloc *= 2;
a->elts = new;
}
if (len == -1) len = strlen(ptr);
a->elts[a->num] = und_alloc(sym, len + 1);
assert(a->elts[a->num]);
memcpy(a->elts[a->num], ptr, len);
a->elts[a->num][len] = '\0';
if (++a->num >= a->max) a->max = a->num;
if (TRACE_ON(msvcrt))
{
int i;
char c;
for (i = a->max - 1; i >= 0; i--)
{
c = '>';
if (i < a->start) c = '-';
else if (i >= a->num) c = '}';
TRACE("%p\t%d%c %s\n", a, i, c, debugstr_a(a->elts[i]));
}
}
return TRUE;
}
/******************************************************************
* str_array_get_ref
* Extracts a reference from an existing array (doing proper type
* checking)
*/
static char* str_array_get_ref(struct array* cref, unsigned idx)
{
assert(cref);
if (cref->start + idx >= cref->max)
{
WARN("Out of bounds: %p %d + %d >= %d\n",
cref, cref->start, idx, cref->max);
return NULL;
}
TRACE("Returning %p[%d] => %s\n",
cref, idx, debugstr_a(cref->elts[cref->start + idx]));
return cref->elts[cref->start + idx];
}
/******************************************************************
* str_printf
* Helper for printf type of command (only %s and %c are implemented)
* while dynamically allocating the buffer
*/
static char* WINAPIV str_printf(struct parsed_symbol* sym, const char* format, ...)
{
va_list args;
unsigned int len = 1, i, sz;
char* tmp;
char* p;
char* t;
va_start(args, format);
for (i = 0; format[i]; i++)
{
if (format[i] == '%')
{
switch (format[++i])
{
case 's': t = va_arg(args, char*); if (t) len += strlen(t); break;
case 'c': (void)va_arg(args, int); len++; break;
default: i--; /* fall through */
case '%': len++; break;
}
}
else len++;
}
va_end(args);
if (!(tmp = und_alloc(sym, len))) return NULL;
va_start(args, format);
for (p = tmp, i = 0; format[i]; i++)
{
if (format[i] == '%')
{
switch (format[++i])
{
case 's':
t = va_arg(args, char*);
if (t)
{
sz = strlen(t);
memcpy(p, t, sz);
p += sz;
}
break;
case 'c':
*p++ = (char)va_arg(args, int);
break;
default: i--; /* fall through */
case '%': *p++ = '%'; break;
}
}
else *p++ = format[i];
}
va_end(args);
*p = '\0';
return tmp;
}
enum datatype_flags
{
IN_ARGS = 0x01,
WS_AFTER_QUAL_IF = 0x02,
};
/* forward declaration */
static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
enum datatype_flags flags);
static const char* get_number(struct parsed_symbol* sym)
{
char* ptr;
BOOL sgn = FALSE;
if (*sym->current == '?')
{
sgn = TRUE;
sym->current++;
}
if (*sym->current >= '0' && *sym->current <= '8')
{
ptr = und_alloc(sym, 3);
if (sgn) ptr[0] = '-';
ptr[sgn ? 1 : 0] = *sym->current + 1;
ptr[sgn ? 2 : 1] = '\0';
sym->current++;
}
else if (*sym->current == '9')
{
ptr = und_alloc(sym, 4);
if (sgn) ptr[0] = '-';
ptr[sgn ? 1 : 0] = '1';
ptr[sgn ? 2 : 1] = '0';
ptr[sgn ? 3 : 2] = '\0';
sym->current++;
}
else if (*sym->current >= 'A' && *sym->current <= 'P')
{
int ret = 0;
while (*sym->current >= 'A' && *sym->current <= 'P')
{
ret *= 16;
ret += *sym->current++ - 'A';
}
if (*sym->current != '@') return NULL;
ptr = und_alloc(sym, 17);
sprintf(ptr, "%s%u", sgn ? "-" : "", ret);
sym->current++;
}
else return NULL;
return ptr;
}
/******************************************************************
* get_args
* Parses a list of function/method arguments, creates a string corresponding
* to the arguments' list.
*/
static char* get_args(struct parsed_symbol* sym, BOOL z_term,
char open_char, char close_char)
{
struct datatype_t ct;
struct array arg_collect;
char* args_str = NULL;
char* last;
unsigned int i;
const char *p;
if (z_term && sym->current[0] == 'X' && sym->current[1] == 'Z')
{
sym->current += 2;
return str_printf(sym, "%cvoid%c", open_char, close_char);
}
str_array_init(&arg_collect);
/* Now come the function arguments */
while (*sym->current)
{
p = sym->current;
/* Decode each data type and append it to the argument list */
if (*sym->current == '@')
{
sym->current++;
break;
}
if (z_term && sym->current[0] == 'Z')
{
sym->current++;
if (!str_array_push(sym, "...", -1, &arg_collect))
return NULL;
break;
}
/* Handle empty list in variadic template */
if (!z_term && sym->current[0] == '$' && sym->current[1] == '$' && sym->current[2] == 'V')
{
sym->current += 3;
continue;
}
if (!demangle_datatype(sym, &ct, IN_ARGS))
return NULL;
if (!str_array_push(sym, str_printf(sym, "%s%s", ct.left, ct.right), -1,
&arg_collect))
return NULL;
if (z_term && sym->current - p > 1 && sym->args.num < 20)
{
if (!str_array_push(sym, ct.left ? ct.left : "", -1, &sym->args) ||
!str_array_push(sym, ct.right ? ct.right : "", -1, &sym->args))
return NULL;
}
}
/* 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 (z_term && *sym->current++ != 'Z') return NULL;
if (!arg_collect.num) return str_printf(sym, "%c%c", open_char, close_char);
for (i = 1; i < arg_collect.num; i++)
{
args_str = str_printf(sym, "%s,%s", args_str, arg_collect.elts[i]);
}
last = args_str ? args_str : arg_collect.elts[0];
if (close_char == '>' && last[strlen(last) - 1] == '>')
args_str = str_printf(sym, "%c%s%s %c",
open_char, arg_collect.elts[0], args_str, close_char);
else
args_str = str_printf(sym, "%c%s%s%c",
open_char, arg_collect.elts[0], args_str, close_char);
return args_str;
}
static void append_extended_qualifier(struct parsed_symbol *sym, const char **where,
const char *str, BOOL is_ms_keyword)
{
if (!is_ms_keyword || !(sym->flags & UNDNAME_NO_MS_KEYWORDS))
{
if (is_ms_keyword && (sym->flags & UNDNAME_NO_LEADING_UNDERSCORES))
str += 2;
*where = *where ? str_printf(sym, "%s%s%s%s", *where, is_ms_keyword ? " " : "", str, is_ms_keyword ? "" : " ") :
str_printf(sym, "%s%s", str, is_ms_keyword ? "" : " ");
}
}
static void get_extended_qualifier(struct parsed_symbol *sym, struct datatype_t *xdt)
{
unsigned fl = 0;
xdt->left = xdt->right = NULL;
xdt->flags = 0;
for (;;)
{
switch (*sym->current)
{
case 'E': append_extended_qualifier(sym, &xdt->right, "__ptr64", TRUE); fl |= 2; break;
case 'F': append_extended_qualifier(sym, &xdt->left, "__unaligned", TRUE); fl |= 2; break;
#ifdef _UCRT
case 'G': append_extended_qualifier(sym, &xdt->right, "&", FALSE); fl |= 1; break;
case 'H': append_extended_qualifier(sym, &xdt->right, "&&", FALSE); fl |= 1; break;
#endif
case 'I': append_extended_qualifier(sym, &xdt->right, "__restrict", TRUE); fl |= 2; break;
default: if (fl == 1 || (fl == 3 && (sym->flags & UNDNAME_NO_MS_KEYWORDS))) xdt->flags = DT_NO_LRSEP_WS; return;
}
sym->current++;
}
}
/******************************************************************
* get_qualifier
* Parses the type qualifier. Always returns static strings.
*/
static BOOL get_qualifier(struct parsed_symbol *sym, struct datatype_t *xdt, const char** pclass)
{
char ch;
const char* qualif;
get_extended_qualifier(sym, xdt);
switch (ch = *sym->current++)
{
case 'A': qualif = NULL; break;
case 'B': qualif = "const"; break;
case 'C': qualif = "volatile"; break;
case 'D': qualif = "const volatile"; break;
case 'Q': qualif = NULL; break;
case 'R': qualif = "const"; break;
case 'S': qualif = "volatile"; break;
case 'T': qualif = "const volatile"; break;
default: return FALSE;
}
if (qualif)
{
xdt->flags &= ~DT_NO_LRSEP_WS;
xdt->left = xdt->left ? str_printf(sym, "%s %s", qualif, xdt->left) : qualif;
}
if (ch >= 'Q' && ch <= 'T') /* pointer to member, fetch class */
{
const char* class = get_class_name(sym);
if (!class) return FALSE;
if (!pclass)
{
FIXME("Got pointer to class %s member without storage\n", class);
return FALSE;
}
*pclass = class;
}
else if (pclass) *pclass = NULL;
return TRUE;
}
static BOOL get_function_qualifier(struct parsed_symbol *sym, const char** qualif)
{
struct datatype_t xdt;
if (!get_qualifier(sym, &xdt, NULL)) return FALSE;
*qualif = (xdt.left || xdt.right) ?
str_printf(sym, "%s%s%s", xdt.left, (xdt.flags & DT_NO_LRSEP_WS) ? "" : " ", xdt.right) : NULL;
return TRUE;
}
static BOOL get_qualified_type(struct datatype_t *ct, struct parsed_symbol* sym,
char qualif, enum datatype_flags flags)
{
struct datatype_t xdt1;
struct datatype_t xdt2;
const char* ref;
const char* str_qualif;
const char* class;
get_extended_qualifier(sym, &xdt1);
switch (qualif)
{
case 'A': ref = " &"; str_qualif = NULL; break;
case 'B': ref = " &"; str_qualif = " volatile"; break;
case 'P': ref = " *"; str_qualif = NULL; break;
case 'Q': ref = " *"; str_qualif = " const"; break;
case 'R': ref = " *"; str_qualif = " volatile"; break;
case 'S': ref = " *"; str_qualif = " const volatile"; break;
case '?': ref = NULL; str_qualif = NULL; break;
case '$': ref = " &&"; str_qualif = NULL; break;
default: return FALSE;
}
ct->right = NULL;
ct->flags = 0;
/* parse managed handle information */
if (sym->current[0] == '$' && sym->current[1] == 'A')
{
sym->current += 2;
switch (qualif)
{
case 'A':
case 'B':
ref = " %";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
ref = " ^";
break;
default:
return FALSE;
}
}
if (get_qualifier(sym, &xdt2, &class))
{
unsigned mark = sym->stack.num;
struct datatype_t sub_ct;
if (ref || str_qualif || xdt1.left || xdt1.right)
{
if (class)
ct->left = str_printf(sym, "%s%s%s%s::%s%s%s",
xdt1.left ? " " : NULL, xdt1.left,
class ? " " : NULL, class, ref ? ref + 1 : NULL,
xdt1.right ? " " : NULL, xdt1.right, str_qualif);
else
ct->left = str_printf(sym, "%s%s%s%s%s%s",
xdt1.left ? " " : NULL, xdt1.left, ref,
xdt1.right ? " " : NULL, xdt1.right, str_qualif);
}
else
ct->left = NULL;
/* multidimensional arrays */
if (*sym->current == 'Y')
{
const char* n1;
int num;
sym->current++;
if (!(n1 = get_number(sym))) return FALSE;
num = atoi(n1);
ct->left = str_printf(sym, " (%s%s", xdt2.left, ct->left && !xdt2.left ? ct->left + 1 : ct->left);
ct->right = ")";
xdt2.left = NULL;
while (num--)
ct->right = str_printf(sym, "%s[%s]", ct->right, get_number(sym));
}
/* Recurse to get the referred-to type */
if (!demangle_datatype(sym, &sub_ct, 0))
return FALSE;
if (sub_ct.flags & DT_NO_LEADING_WS)
ct->left++;
ct->left = str_printf(sym, "%s%s%s%s%s", sub_ct.left, xdt2.left ? " " : NULL,
xdt2.left, ct->left,
((xdt2.left || str_qualif) && (flags & WS_AFTER_QUAL_IF)) ? " " : NULL);
if (sub_ct.right) ct->right = str_printf(sym, "%s%s", ct->right, sub_ct.right);
sym->stack.num = mark;
}
else if (ref || str_qualif || xdt1.left || xdt1.right)
ct->left = str_printf(sym, "%s%s%s%s%s%s",
xdt1.left ? " " : NULL, xdt1.left, ref,
xdt1.right ? " " : NULL, xdt1.right, str_qualif);
else
ct->left = NULL;
return TRUE;
}
/******************************************************************
* get_literal_string
* Gets the literal name from the current position in the mangled
* symbol to the first '@' character. It pushes the parsed name to
* the symbol names stack and returns a pointer to it or NULL in
* case of an error.
*/
static char* get_literal_string(struct parsed_symbol* sym)
{
const char *ptr = sym->current;
do {
if (!((*sym->current >= 'A' && *sym->current <= 'Z') ||
(*sym->current >= 'a' && *sym->current <= 'z') ||
(*sym->current >= '0' && *sym->current <= '9') ||
*sym->current == '_' || *sym->current == '$' ||
*sym->current == '<' || *sym->current == '>')) {
TRACE("Failed at '%c' in %s\n", *sym->current, debugstr_a(ptr));
return NULL;
}
} while (*++sym->current != '@');
sym->current++;
if (!str_array_push(sym, ptr, sym->current - 1 - ptr, &sym->names))
return NULL;
return str_array_get_ref(&sym->names, sym->names.num - sym->names.start - 1);
}
/******************************************************************
* get_template_name
* Parses a name with a template argument list and returns it as
* a string.
* In a template argument list the back reference to the names
* table is separately created. '0' points to the class component
* name with the template arguments. We use the same stack array
* to hold the names but save/restore the stack state before/after
* parsing the template argument list.
*/
static char* get_template_name(struct parsed_symbol* sym)
{
char *name, *args;
unsigned num_mark = sym->names.num;
unsigned start_mark = sym->names.start;
unsigned stack_mark = sym->stack.num;
unsigned args_mark = sym->args.num;
sym->names.start = sym->names.num;
if (!(name = get_literal_string(sym))) {
sym->names.start = start_mark;
return FALSE;
}
args = get_args(sym, FALSE, '<', '>');
if (args != NULL)
name = str_printf(sym, "%s%s", name, args);
sym->names.num = num_mark;
sym->names.start = start_mark;
sym->stack.num = stack_mark;
sym->args.num = args_mark;
return name;
}
/******************************************************************
* get_class
* Parses class as a list of parent-classes, terminated by '@' and stores the
* result in 'a' array. Each parent-classes, as well as the inner element
* (either field/method name or class name), are represented in the mangled
* name by a literal name ([a-zA-Z0-9_]+ terminated by '@') or a back reference
* ([0-9]) or a name with template arguments ('?$' literal name followed by the
* template argument list). The class name components appear in the reverse
* order in the mangled name, e.g aaa@bbb@ccc@@ will be demangled to
* ccc::bbb::aaa
* For each of these class name components a string will be allocated in the
* array.
*/
static BOOL get_class(struct parsed_symbol* sym)
{
const char* name = NULL;
while (*sym->current != '@')
{
switch (*sym->current)
{
case '\0': return FALSE;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
name = str_array_get_ref(&sym->names, *sym->current++ - '0');
break;
case '?':
switch (*++sym->current)
{
case '$':
sym->current++;
if ((name = get_template_name(sym)) &&
!str_array_push(sym, name, -1, &sym->names))
return FALSE;
break;
case '?':
{
struct array stack = sym->stack;
unsigned int start = sym->names.start;
unsigned int num = sym->names.num;
str_array_init( &sym->stack );
if (symbol_demangle( sym )) name = str_printf( sym, "`%s'", sym->result );
sym->names.start = start;
sym->names.num = num;
sym->stack = stack;
}
break;
default:
if (!(name = get_number( sym ))) return FALSE;
name = str_printf( sym, "`%s'", name );
break;
}
break;
default:
name = get_literal_string(sym);
break;
}
if (!name || !str_array_push(sym, name, -1, &sym->stack))
return FALSE;
}
sym->current++;
return TRUE;
}
/******************************************************************
* get_class_string
* From an array collected by get_class in sym->stack, constructs the
* corresponding (allocated) string
*/
static char* get_class_string(struct parsed_symbol* sym, int start)
{
int i;
unsigned int len, sz;
char* ret;
struct array *a = &sym->stack;
for (len = 0, i = start; i < a->num; i++)
{
assert(a->elts[i]);
len += 2 + strlen(a->elts[i]);
}
if (!(ret = und_alloc(sym, len - 1))) return NULL;
for (len = 0, i = a->num - 1; i >= start; i--)
{
sz = strlen(a->elts[i]);
memcpy(ret + len, a->elts[i], sz);
len += sz;
if (i > start)
{
ret[len++] = ':';
ret[len++] = ':';
}
}
ret[len] = '\0';
return ret;
}
/******************************************************************
* get_class_name
* Wrapper around get_class and get_class_string.
*/
static char* get_class_name(struct parsed_symbol* sym)
{
unsigned mark = sym->stack.num;
char* s = NULL;
if (get_class(sym))
s = get_class_string(sym, mark);
sym->stack.num = mark;
return s;
}
/******************************************************************
* get_calling_convention
* Returns a static string corresponding to the calling convention described
* by char 'ch'. Sets export to TRUE iff the calling convention is exported.
*/
static BOOL get_calling_convention(char ch, const char** call_conv,
const char** exported, unsigned flags)
{
*call_conv = *exported = NULL;
if (!(flags & (UNDNAME_NO_MS_KEYWORDS | UNDNAME_NO_ALLOCATION_LANGUAGE)))
{
if (flags & UNDNAME_NO_LEADING_UNDERSCORES)
{
if (((ch - 'A') % 2) == 1) *exported = "dll_export ";
switch (ch)
{
case 'A': case 'B': *call_conv = "cdecl"; break;
case 'C': case 'D': *call_conv = "pascal"; break;
case 'E': case 'F': *call_conv = "thiscall"; break;
case 'G': case 'H': *call_conv = "stdcall"; break;
case 'I': case 'J': *call_conv = "fastcall"; break;
case 'K': case 'L': break;
case 'M': *call_conv = "clrcall"; break;
default: ERR("Unknown calling convention %c\n", ch); return FALSE;
}
}
else
{
if (((ch - 'A') % 2) == 1) *exported = "__dll_export ";
switch (ch)
{
case 'A': case 'B': *call_conv = "__cdecl"; break;
case 'C': case 'D': *call_conv = "__pascal"; break;
case 'E': case 'F': *call_conv = "__thiscall"; break;
case 'G': case 'H': *call_conv = "__stdcall"; break;
case 'I': case 'J': *call_conv = "__fastcall"; break;
case 'K': case 'L': break;
case 'M': *call_conv = "__clrcall"; break;
default: ERR("Unknown calling convention %c\n", ch); return FALSE;
}
}
}
return TRUE;
}
/*******************************************************************
* get_simple_type
* Return a string containing an allocated string for a simple data type
*/
static const char* get_simple_type(char c)
{
const char* type_string;
switch (c)
{
case 'C': type_string = "signed char"; break;
case 'D': type_string = "char"; break;
case 'E': type_string = "unsigned char"; break;
case 'F': type_string = "short"; break;
case 'G': type_string = "unsigned short"; break;
case 'H': type_string = "int"; break;
case 'I': type_string = "unsigned int"; break;
case 'J': type_string = "long"; break;
case 'K': type_string = "unsigned long"; break;
case 'M': type_string = "float"; break;
case 'N': type_string = "double"; break;
case 'O': type_string = "long double"; break;
case 'X': type_string = "void"; break;
/* case 'Z': (...) variadic function arguments. Handled in get_args() */
default: type_string = NULL; break;
}
return type_string;
}
/*******************************************************************
* get_extended_type
* Return a string containing an allocated string for a simple data type
*/
static const char* get_extended_type(char c)
{
const char* type_string;
switch (c)
{
case 'D': type_string = "__int8"; break;
case 'E': type_string = "unsigned __int8"; break;
case 'F': type_string = "__int16"; break;
case 'G': type_string = "unsigned __int16"; break;
case 'H': type_string = "__int32"; break;
case 'I': type_string = "unsigned __int32"; break;
case 'J': type_string = "__int64"; break;
case 'K': type_string = "unsigned __int64"; break;
case 'L': type_string = "__int128"; break;
case 'M': type_string = "unsigned __int128"; break;
case 'N': type_string = "bool"; break;
case 'Q': type_string = "char8_t"; break;
case 'S': type_string = "char16_t"; break;
case 'U': type_string = "char32_t"; break;
case 'W': type_string = "wchar_t"; break;
default: type_string = NULL; break;
}
return type_string;
}
struct function_signature
{
const char* call_conv;
const char* exported;
struct datatype_t return_ct;
const char* arguments;
};
static BOOL get_function_signature(struct parsed_symbol* sym, struct function_signature* fs)
{
unsigned mark = sym->stack.num;
if (!get_calling_convention(*sym->current++,
&fs->call_conv, &fs->exported,
sym->flags & ~UNDNAME_NO_ALLOCATION_LANGUAGE) ||
!demangle_datatype(sym, &fs->return_ct, 0))
return FALSE;
if (!(fs->arguments = get_args(sym, TRUE, '(', ')')))
return FALSE;
sym->stack.num = mark;
return TRUE;
}
/*******************************************************************
* demangle_datatype
*
* Attempt to demangle a C++ data type, which may be datatype.
* a datatype type is made up of a number of simple types. e.g:
* char** = (pointer to (pointer to (char)))
*/
static BOOL demangle_datatype(struct parsed_symbol* sym, struct datatype_t* ct,
enum datatype_flags flags)
{
char dt;
assert(ct);
ct->left = ct->right = NULL;
ct->flags = 0;
switch (dt = *sym->current++)
{
case '_':
/* MS type: __int8,__int16 etc */
ct->left = get_extended_type(*sym->current++);
break;
case 'C': case 'D': case 'E': case 'F': case 'G':
case 'H': case 'I': case 'J': case 'K': case 'M':
case 'N': case 'O': case 'X': case 'Z':
/* Simple data types */
ct->left = get_simple_type(dt);
break;
case 'T': /* union */
case 'U': /* struct */
case 'V': /* class */
case 'Y': /* cointerface */
/* Class/struct/union/cointerface */
{
const char* struct_name = NULL;
const char* type_name = NULL;
if (!(struct_name = get_class_name(sym)))
goto done;
if (!(sym->flags & UNDNAME_NO_COMPLEX_TYPE))
{
switch (dt)
{
case 'T': type_name = "union "; break;
case 'U': type_name = "struct "; break;
case 'V': type_name = "class "; break;
case 'Y': type_name = "cointerface "; break;
}
}
ct->left = str_printf(sym, "%s%s", type_name, struct_name);
}
break;
case '?':
/* not all the time is seems */
if (flags & IN_ARGS)
{
const char* ptr;
if (!(ptr = get_number(sym))) goto done;
ct->left = str_printf(sym, "`template-parameter-%s'", ptr);
}
else
{
if (!get_qualified_type(ct, sym, '?', flags)) goto done;
}
break;
case 'A': /* reference */
case 'B': /* volatile reference */
if (!get_qualified_type(ct, sym, dt, flags)) goto done;
break;