5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
13 unsigned int is_array:1;
14 unsigned int is_ptr:1;
15 unsigned int is_struct:1; // split for args
16 unsigned int is_retreg:1; // register to return to caller
17 unsigned int is_va_list:1;
18 unsigned int is_64bit:1;
19 unsigned int is_float:1; // float, double
22 struct parsed_proto_arg {
24 struct parsed_type type;
25 struct parsed_proto *pp; // fptr or struct
26 unsigned int is_saved:1; // not set here, for tool use
34 struct parsed_type ret_type;
35 struct parsed_type type;
37 struct parsed_proto_arg arg[32];
41 unsigned int is_func:1;
42 unsigned int is_stdcall:1;
43 unsigned int is_fastcall:1;
44 unsigned int is_vararg:1; // vararg func
45 unsigned int is_fptr:1;
46 unsigned int is_import:1; // data import
47 unsigned int is_noreturn:1;
48 unsigned int is_unresolved:1;
49 unsigned int is_guessed:1; // for extra checking
50 unsigned int is_userstack:1;
51 unsigned int is_include:1; // not from top-level header
52 unsigned int is_osinc:1; // OS/system library func
53 unsigned int is_cinc:1; // crt library func
54 unsigned int is_arg:1; // declared in some func arg
55 unsigned int has_structarg:1;
56 unsigned int has_retreg:1;
59 struct parsed_struct {
63 struct parsed_proto pp;
68 static const char *hdrfn;
69 static int hdrfline = 0;
71 static void pp_copy_arg(struct parsed_proto_arg *d,
72 const struct parsed_proto_arg *s);
74 static int b_pp_c_handler(char *proto, const char *fname,
75 int is_include, int is_osinc, int is_cinc);
76 static int struct_handler(FILE *fhdr, char *proto, int *line);
78 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
80 const char *finc_name;
81 const char *hdrfn_saved;
95 is_cinc = strstr(fname, "stdc.hlist") != NULL;
96 is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
98 while (fgets(protostr, sizeof(protostr), fhdr))
101 if (strncmp(protostr, "//#include ", 11) == 0) {
102 finc_name = protostr + 11;
103 p = strpbrk(finc_name, "\r\n ");
108 p = strrchr(hdrfn_saved, '/');
110 memcpy(path, hdrfn_saved,
111 p - hdrfn_saved + 1);
112 path[p - hdrfn_saved + 1] = 0;
114 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
116 finc = fopen(fname_inc, "r");
118 printf("%s:%d: can't open '%s'\n",
119 fname_inc, line, finc_name);
122 ret = do_protostrs(finc, finc_name, 1);
128 if (strncmp(sskip(protostr), "//", 2) == 0)
131 p = protostr + strlen(protostr);
132 for (p--; p >= protostr && my_isblank(*p); --p)
139 if (!strncmp(protostr, "struct", 6)
140 && strchr(protostr, '{') != NULL)
141 ret = struct_handler(fhdr, protostr, &line);
143 ret = b_pp_c_handler(protostr, hdrfn,
144 is_include, is_osinc, is_cinc);
157 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
172 for (o = 0; o < dlen; i++) {
184 static const char *known_type_mod[] = {
193 static const char *known_ptr_types[] = {
210 "HIMC", // DWORD in mingw, ptr in wine..
234 "PMEMORY_BASIC_INFORMATION",
245 "SERVICE_STATUS_HANDLE",
254 static const char *ignored_keywords[] = {
263 static int typecmp(const char *n, const char *t)
265 for (; *t != 0; n++, t++) {
266 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
268 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
277 static const char *skip_type_mod(const char *n)
282 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
283 len = strlen(known_type_mod[i]);
284 if (strncmp(n, known_type_mod[i], len) != 0)
286 if (!my_isblank(n[len]))
290 while (my_isblank(*n))
298 static int check_type(const char *name, struct parsed_type *type)
304 n = skip_type_mod(name);
306 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
310 while (my_isblank(*n))
314 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
315 if (typecmp(n, known_ptr_types[i]))
322 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
325 // assume single word
326 while (!my_isblank(*n) && !my_issep(*n))
331 while (my_isblank(*n))
342 type->name = strndup(name, ret);
343 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
344 type->is_va_list = 1;
345 if (IS(type->name, "VOID"))
346 memcpy(type->name, "void", 4);
351 /* args are always expanded to 32bit */
352 static const char *map_reg(const char *reg)
354 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
355 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
356 const char *regs_b[] = { "al", "bl", "cl", "dl" };
359 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
360 if (IS(reg, regs_w[i]))
363 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
364 if (IS(reg, regs_b[i]))
370 static int check_struct_arg(struct parsed_proto_arg *arg)
372 if (IS(arg->type.name, "POINT"))
378 static int parse_protostr(char *protostr, struct parsed_proto *pp);
380 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
387 arg->pp = calloc(1, sizeof(*arg->pp));
388 my_assert_not(arg->pp, NULL);
393 pe = strpbrk(pe, ",()");
396 if (*pe == ',' || *pe == ')')
398 pe = strchr(pe, ')');
404 if (pe - p > sizeof(buf) - 1)
406 memcpy(buf, p, pe - p);
409 ret = parse_protostr(buf, arg->pp);
413 if (IS_START(arg->pp->name, "guess"))
414 arg->pp->is_guessed = 1;
416 // we don't use actual names right now...
417 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
419 if (!arg->type.is_struct)
420 // we'll treat it as void * for non-calls
421 arg->type.name = strdup("void *");
422 arg->type.is_ptr = 1;
429 static int parse_protostr(char *protostr, struct parsed_proto *pp)
431 struct parsed_proto_arg *arg;
442 if (p[0] == '/' && p[1] == '/') {
443 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
447 // allow start of line comment
448 if (p[0] == '/' && p[1] == '*') {
449 p = strstr(p + 2, "*/");
451 printf("%s:%d: multiline comments unsupported\n",
458 // we need remaining hints in comments, so strip / *
459 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
460 if ((p1[0] == '/' && p1[1] == '*')
461 || (p1[0] == '*' && p1[1] == '/'))
465 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
470 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
471 l = strlen(ignored_keywords[i]);
472 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
473 p = sskip(p + l + 1);
476 if (IS_START(p, "DECL_IMPORT ")) {
481 ret = check_type(p, &pp->ret_type);
483 printf("%s:%d:%zd: unhandled return in '%s'\n",
484 hdrfn, hdrfline, (p - protostr) + 1, protostr);
489 if (!strncmp(p, "noreturn ", 9)) {
494 if (!strchr(p, ')')) {
495 p = next_idt(buf, sizeof(buf), p);
497 if (!pp->is_arg && buf[0] == 0) {
498 printf("%s:%d:%zd: var name is missing\n",
499 hdrfn, hdrfline, (p - protostr) + 1);
502 strcpy(pp->name, buf);
507 pp->ret_type.is_array = 1;
519 p = next_word(cconv, sizeof(cconv), p);
522 printf("%s:%d:%zd: cconv missing\n",
523 hdrfn, hdrfline, (p - protostr) + 1);
526 if (IS(cconv, "__cdecl"))
528 else if (IS(cconv, "__stdcall"))
530 else if (IS(cconv, "__fastcall")) {
532 pp->is_stdcall = 1; // sort of..
534 else if (IS(cconv, "__thiscall"))
536 else if (IS(cconv, "__userpurge"))
537 pp->is_stdcall = 1; // IDA
538 else if (IS(cconv, "__usercall"))
539 pp->is_stdcall = 0; // IDA
540 else if (IS(cconv, "__userstack")) {
541 pp->is_stdcall = 0; // custom
542 pp->is_userstack = 1;
544 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
547 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
548 hdrfn, hdrfline, (p - protostr) + 1, cconv);
554 printf("%s:%d:%zd: '*' expected\n",
555 hdrfn, hdrfline, (p - protostr) + 1);
559 // XXX: skipping extra asterisks, for now
565 p = next_idt(buf, sizeof(buf), p);
568 //printf("%s:%d:%zd: func name missing\n",
569 // hdrfn, hdrfline, (p - protostr) + 1);
572 strcpy(pp->name, buf);
574 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
576 if (!IS(regparm, "eax") && !IS(regparm, "ax")
577 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
579 printf("%s:%d:%zd: bad regparm: %s\n",
580 hdrfn, hdrfline, (p - protostr) + 1, regparm);
589 // not really ret_type is array, but ohwell
590 pp->ret_type.is_array = 1;
591 p = strchr(p + 1, ']');
593 printf("%s:%d:%zd: ']' expected\n",
594 hdrfn, hdrfline, (p - protostr) + 1);
600 printf("%s:%d:%zd: ')' expected\n",
601 hdrfn, hdrfline, (p - protostr) + 1);
608 printf("%s:%d:%zd: '(' expected, got '%c'\n",
609 hdrfn, hdrfline, (p - protostr) + 1, *p);
616 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
617 && *sskip(p + 4) == ')')
628 printf("%s:%d:%zd: ',' expected\n",
629 hdrfn, hdrfline, (p - protostr) + 1);
635 if (!strncmp(p, "...", 3)) {
642 printf("%s:%d:%zd: ')' expected\n",
643 hdrfn, hdrfline, (p - protostr) + 1);
647 if (xarg >= ARRAY_SIZE(pp->arg)) {
648 printf("%s:%d:%zd: too many args\n",
649 hdrfn, hdrfline, (p - protostr) + 1);
653 arg = &pp->arg[xarg];
657 ret = check_type(p, &arg->type);
659 printf("%s:%d:%zd: unhandled type for arg%d\n",
660 hdrfn, hdrfline, (p - protostr) + 1, xarg);
665 if (*p == '(' || arg->type.is_struct) {
666 // func ptr or struct
667 ret = parse_arg(&p1, arg, xarg);
669 printf("%s:%d:%zd: funcarg parse failed\n",
670 hdrfn, hdrfline, p1 - protostr);
676 p = next_idt(buf, sizeof(buf), p);
680 printf("%s:%d:%zd: idt missing for arg%d\n",
681 hdrfn, hdrfline, (p - protostr) + 1, xarg);
687 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
692 arg->reg = strdup(map_reg(regparm));
693 arg->type.is_retreg = is_retreg;
694 pp->has_retreg |= is_retreg;
697 if (IS(arg->type.name, "float")
698 || IS(arg->type.name, "double"))
700 arg->type.is_float = 1;
703 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
704 || IS(arg->type.name, "double")))
706 arg->type.is_64bit = 1;
708 pp_copy_arg(&pp->arg[xarg], arg);
709 arg = &pp->arg[xarg];
711 free(arg->type.name);
712 arg->type.name = strdup("dummy");
715 ret = check_struct_arg(arg);
717 pp->has_structarg = 1;
718 arg->type.is_struct = 1;
719 free(arg->type.name);
720 arg->type.name = strdup("int");
721 for (l = 0; l < ret; l++) {
722 pp_copy_arg(&pp->arg[xarg], arg);
728 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
729 if (pp->arg[0].reg != NULL) {
730 printf("%s:%d: %s with arg1 spec %s?\n",
731 hdrfn, hdrfline, cconv, pp->arg[0].reg);
733 pp->arg[0].reg = strdup("ecx");
736 if (xarg > 1 && IS(cconv, "__fastcall")) {
737 if (pp->arg[1].reg != NULL) {
738 printf("%s:%d: %s with arg2 spec %s?\n",
739 hdrfn, hdrfline, cconv, pp->arg[1].reg);
741 pp->arg[1].reg = strdup("edx");
746 for (i = 0; i < pp->argc; i++) {
747 if (pp->arg[i].reg == NULL)
753 if (pp->argc == 1 && pp->arg[0].reg != NULL
754 && IS(pp->arg[0].reg, "ecx"))
758 else if (pp->argc_reg == 2
759 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
760 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
765 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
766 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
773 static int pp_name_cmp(const void *p1, const void *p2)
775 const struct parsed_proto *pp1 = p1, *pp2 = p2;
776 return strcmp(pp1->name, pp2->name);
779 static int ps_name_cmp(const void *p1, const void *p2)
781 const struct parsed_struct *ps1 = p1, *ps2 = p2;
782 return strcmp(ps1->name, ps2->name);
785 // parsed struct cache
786 static struct parsed_struct *ps_cache;
787 static int ps_cache_size;
788 static int ps_cache_alloc;
790 static int struct_handler(FILE *fhdr, char *proto, int *line)
792 struct parsed_struct *ps;
798 if (ps_cache_size >= ps_cache_alloc) {
799 ps_cache_alloc = ps_cache_alloc * 2 + 64;
800 ps_cache = realloc(ps_cache, ps_cache_alloc
801 * sizeof(ps_cache[0]));
802 my_assert_not(ps_cache, NULL);
803 memset(ps_cache + ps_cache_size, 0,
804 (ps_cache_alloc - ps_cache_size)
805 * sizeof(ps_cache[0]));
808 ps = &ps_cache[ps_cache_size++];
809 ret = sscanf(proto, "struct %255s {", ps->name);
811 printf("%s:%d: struct parse failed\n", hdrfn, *line);
815 while (fgets(lstr, sizeof(lstr), fhdr))
820 if (p[0] == '/' && p[1] == '/')
825 if (m >= ARRAY_SIZE(ps->members)) {
826 printf("%s:%d: too many struct members\n",
832 ret = parse_protostr(p, &ps->members[m].pp);
834 printf("%s:%d: struct member #%d/%02x "
835 "doesn't parse\n", hdrfn, *line,
839 ps->members[m].offset = offset;
844 ps->member_count = m;
849 // parsed proto cache
850 static struct parsed_proto *pp_cache;
851 static int pp_cache_size;
852 static int pp_cache_alloc;
854 static int b_pp_c_handler(char *proto, const char *fname,
855 int is_include, int is_osinc, int is_cinc)
859 if (pp_cache_size >= pp_cache_alloc) {
860 pp_cache_alloc = pp_cache_alloc * 2 + 64;
861 pp_cache = realloc(pp_cache, pp_cache_alloc
862 * sizeof(pp_cache[0]));
863 my_assert_not(pp_cache, NULL);
864 memset(pp_cache + pp_cache_size, 0,
865 (pp_cache_alloc - pp_cache_size)
866 * sizeof(pp_cache[0]));
869 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
873 pp_cache[pp_cache_size].is_include = is_include;
874 pp_cache[pp_cache_size].is_osinc = is_osinc;
875 pp_cache[pp_cache_size].is_cinc = is_cinc;
880 static void build_caches(FILE *fhdr)
888 ret = do_protostrs(fhdr, hdrfn, 0);
892 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
893 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
894 fseek(fhdr, pos, SEEK_SET);
897 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
900 const struct parsed_proto *pp_ret;
901 struct parsed_proto pp_search;
904 if (pp_cache == NULL)
908 if (sym[0] == '_' && !IS_START(sym, "__W"))
911 strcpy(pp_search.name, sym);
912 p = strchr(pp_search.name, '@');
916 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
917 sizeof(pp_cache[0]), pp_name_cmp);
918 if (pp_ret == NULL && !quiet)
919 printf("%s: sym '%s' is missing\n", hdrfn, sym);
924 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
925 const char *type, int offset)
927 struct parsed_struct ps_search, *ps;
930 if (pp_cache == NULL)
932 if (ps_cache_size == 0)
935 while (my_isblank(*type))
937 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
940 if (sscanf(type, "%255s", ps_search.name) != 1)
943 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
944 sizeof(ps_cache[0]), ps_name_cmp);
946 printf("%s: struct '%s' is missing\n",
947 hdrfn, ps_search.name);
951 for (m = 0; m < ps->member_count; m++) {
952 if (ps->members[m].offset == offset)
953 return &ps->members[m].pp;
959 static void pp_copy_arg(struct parsed_proto_arg *d,
960 const struct parsed_proto_arg *s)
962 memcpy(d, s, sizeof(*d));
964 if (s->reg != NULL) {
965 d->reg = strdup(s->reg);
966 my_assert_not(d->reg, NULL);
968 if (s->type.name != NULL) {
969 d->type.name = strdup(s->type.name);
970 my_assert_not(d->type.name, NULL);
973 d->pp = malloc(sizeof(*d->pp));
974 my_assert_not(d->pp, NULL);
975 memcpy(d->pp, s->pp, sizeof(*d->pp));
979 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
981 struct parsed_proto *pp;
984 pp = malloc(sizeof(*pp));
985 my_assert_not(pp, NULL);
986 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
988 // do the actual deep copy..
989 for (i = 0; i < pp_c->argc; i++)
990 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
991 if (pp_c->ret_type.name != NULL)
992 pp->ret_type.name = strdup(pp_c->ret_type.name);
998 static inline int pp_cmp_func(const struct parsed_proto *pp1,
999 const struct parsed_proto *pp2)
1003 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
1005 if (pp1->is_stdcall != pp2->is_stdcall)
1008 // because of poor void return detection, return is not
1009 // checked for now to avoid heaps of false positives
1011 for (i = 0; i < pp1->argc; i++) {
1012 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
1015 if ((pp1->arg[i].reg != NULL)
1016 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
1025 static inline int pp_compatible_func(
1026 const struct parsed_proto *pp_site,
1027 const struct parsed_proto *pp_callee)
1029 if (pp_cmp_func(pp_site, pp_callee) == 0)
1032 if (pp_site->argc_stack == 0 && pp_site->is_fastcall
1033 && pp_callee->argc_stack == 0
1034 && (pp_callee->is_fastcall || pp_callee->argc_reg == 0)
1035 && pp_site->argc_reg > pp_callee->argc_reg)
1036 /* fascall compatible callee doesn't use all args -> ok */
1042 static inline void pp_print(char *buf, size_t buf_size,
1043 const struct parsed_proto *pp)
1048 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
1051 for (i = 0; i < pp->argc_reg; i++) {
1052 snprintf(buf + l, buf_size - l, "%s%s",
1053 i == 0 ? "" : ", ", pp->arg[i].reg);
1056 if (pp->argc_stack > 0) {
1057 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1058 i == 0 ? "" : ", ", pp->argc_stack);
1061 snprintf(buf + l, buf_size - l, ")");
1064 static inline void proto_release(struct parsed_proto *pp)
1068 for (i = 0; i < pp->argc; i++) {
1069 free(pp->arg[i].reg);
1070 free(pp->arg[i].type.name);
1071 free(pp->arg[i].pp);
1072 free(pp->arg[i].push_refs);
1074 if (pp->ret_type.name != NULL)
1075 free(pp->ret_type.name);
1078 (void)proto_lookup_struct;