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
27 unsigned int is_saved:1; // not set here, for tool use
33 struct parsed_type ret_type;
34 struct parsed_type type;
36 struct parsed_proto_arg arg[16];
40 unsigned int is_func:1;
41 unsigned int is_stdcall:1;
42 unsigned int is_fastcall:1;
43 unsigned int is_vararg:1; // vararg func
44 unsigned int is_fptr:1;
45 unsigned int is_import:1; // data import
46 unsigned int is_noreturn:1;
47 unsigned int is_unresolved:1;
48 unsigned int is_guessed:1; // for extra checking
49 unsigned int is_userstack:1;
50 unsigned int is_include:1; // not from top-level header
51 unsigned int is_osinc:1; // OS/system library func
52 unsigned int is_cinc:1; // crt library func
53 unsigned int is_arg:1; // declared in some func arg
54 unsigned int has_structarg:1;
55 unsigned int has_retreg:1;
58 struct parsed_struct {
62 struct parsed_proto pp;
67 static const char *hdrfn;
68 static int hdrfline = 0;
70 static void pp_copy_arg(struct parsed_proto_arg *d,
71 const struct parsed_proto_arg *s);
73 static int b_pp_c_handler(char *proto, const char *fname,
74 int is_include, int is_osinc, int is_cinc);
75 static int struct_handler(FILE *fhdr, char *proto, int *line);
77 static int do_protostrs(FILE *fhdr, const char *fname, int is_include)
79 const char *finc_name;
80 const char *hdrfn_saved;
94 is_cinc = strstr(fname, "stdc.hlist") != NULL;
95 is_osinc = is_cinc || strstr(fname, "win32.hlist") != NULL;
97 while (fgets(protostr, sizeof(protostr), fhdr))
100 if (strncmp(protostr, "//#include ", 11) == 0) {
101 finc_name = protostr + 11;
102 p = strpbrk(finc_name, "\r\n ");
107 p = strrchr(hdrfn_saved, '/');
109 memcpy(path, hdrfn_saved,
110 p - hdrfn_saved + 1);
111 path[p - hdrfn_saved + 1] = 0;
113 snprintf(fname_inc, sizeof(fname_inc), "%s%s",
115 finc = fopen(fname_inc, "r");
117 printf("%s:%d: can't open '%s'\n",
118 fname_inc, line, finc_name);
121 ret = do_protostrs(finc, finc_name, 1);
127 if (strncmp(sskip(protostr), "//", 2) == 0)
130 p = protostr + strlen(protostr);
131 for (p--; p >= protostr && my_isblank(*p); --p)
138 if (!strncmp(protostr, "struct", 6)
139 && strchr(protostr, '{') != NULL)
140 ret = struct_handler(fhdr, protostr, &line);
142 ret = b_pp_c_handler(protostr, hdrfn,
143 is_include, is_osinc, is_cinc);
156 static int get_regparm(char *dst, size_t dlen, char *p, int *retreg)
171 for (o = 0; o < dlen; i++) {
183 static const char *known_type_mod[] = {
192 static const char *known_ptr_types[] = {
209 "HIMC", // DWORD in mingw, ptr in wine..
231 "PMEMORY_BASIC_INFORMATION",
249 static const char *ignored_keywords[] = {
257 static int typecmp(const char *n, const char *t)
259 for (; *t != 0; n++, t++) {
260 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
262 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
271 static const char *skip_type_mod(const char *n)
276 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
277 len = strlen(known_type_mod[i]);
278 if (strncmp(n, known_type_mod[i], len) != 0)
280 if (!my_isblank(n[len]))
284 while (my_isblank(*n))
292 static int check_type(const char *name, struct parsed_type *type)
298 n = skip_type_mod(name);
300 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
304 while (my_isblank(*n))
308 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
309 if (typecmp(n, known_ptr_types[i]))
316 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
319 // assume single word
320 while (!my_isblank(*n) && !my_issep(*n))
325 while (my_isblank(*n))
336 type->name = strndup(name, ret);
337 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
338 type->is_va_list = 1;
339 if (IS(type->name, "VOID"))
340 memcpy(type->name, "void", 4);
345 /* args are always expanded to 32bit */
346 static const char *map_reg(const char *reg)
348 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
349 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
350 const char *regs_b[] = { "al", "bl", "cl", "dl" };
353 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
354 if (IS(reg, regs_w[i]))
357 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
358 if (IS(reg, regs_b[i]))
364 static int check_struct_arg(struct parsed_proto_arg *arg)
366 if (IS(arg->type.name, "POINT"))
372 static int parse_protostr(char *protostr, struct parsed_proto *pp);
374 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
381 arg->pp = calloc(1, sizeof(*arg->pp));
382 my_assert_not(arg->pp, NULL);
387 pe = strpbrk(pe, ",()");
390 if (*pe == ',' || *pe == ')')
392 pe = strchr(pe, ')');
398 if (pe - p > sizeof(buf) - 1)
400 memcpy(buf, p, pe - p);
403 ret = parse_protostr(buf, arg->pp);
407 if (IS_START(arg->pp->name, "guess"))
408 arg->pp->is_guessed = 1;
410 // we don't use actual names right now...
411 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
413 if (!arg->type.is_struct)
414 // we'll treat it as void * for non-calls
415 arg->type.name = strdup("void *");
416 arg->type.is_ptr = 1;
423 static int parse_protostr(char *protostr, struct parsed_proto *pp)
425 struct parsed_proto_arg *arg;
436 if (p[0] == '/' && p[1] == '/') {
437 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
441 // allow start of line comment
442 if (p[0] == '/' && p[1] == '*') {
443 p = strstr(p + 2, "*/");
445 printf("%s:%d: multiline comments unsupported\n",
452 // we need remaining hints in comments, so strip / *
453 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
454 if ((p1[0] == '/' && p1[1] == '*')
455 || (p1[0] == '*' && p1[1] == '/'))
459 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
464 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
465 l = strlen(ignored_keywords[i]);
466 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
467 p = sskip(p + l + 1);
470 if (IS_START(p, "DECL_IMPORT ")) {
475 ret = check_type(p, &pp->ret_type);
477 printf("%s:%d:%zd: unhandled return in '%s'\n",
478 hdrfn, hdrfline, (p - protostr) + 1, protostr);
483 if (!strncmp(p, "noreturn ", 9)) {
488 if (!strchr(p, ')')) {
489 p = next_idt(buf, sizeof(buf), p);
491 if (!pp->is_arg && buf[0] == 0) {
492 printf("%s:%d:%zd: var name is missing\n",
493 hdrfn, hdrfline, (p - protostr) + 1);
496 strcpy(pp->name, buf);
501 pp->ret_type.is_array = 1;
513 p = next_word(cconv, sizeof(cconv), p);
516 printf("%s:%d:%zd: cconv missing\n",
517 hdrfn, hdrfline, (p - protostr) + 1);
520 if (IS(cconv, "__cdecl"))
522 else if (IS(cconv, "__stdcall"))
524 else if (IS(cconv, "__fastcall")) {
526 pp->is_stdcall = 1; // sort of..
528 else if (IS(cconv, "__thiscall"))
530 else if (IS(cconv, "__userpurge"))
531 pp->is_stdcall = 1; // IDA
532 else if (IS(cconv, "__usercall"))
533 pp->is_stdcall = 0; // IDA
534 else if (IS(cconv, "__userstack")) {
535 pp->is_stdcall = 0; // custom
536 pp->is_userstack = 1;
538 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
541 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
542 hdrfn, hdrfline, (p - protostr) + 1, cconv);
548 printf("%s:%d:%zd: '*' expected\n",
549 hdrfn, hdrfline, (p - protostr) + 1);
553 // XXX: skipping extra asterisks, for now
559 p = next_idt(buf, sizeof(buf), p);
562 //printf("%s:%d:%zd: func name missing\n",
563 // hdrfn, hdrfline, (p - protostr) + 1);
566 strcpy(pp->name, buf);
568 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
570 if (!IS(regparm, "eax") && !IS(regparm, "ax")
571 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
573 printf("%s:%d:%zd: bad regparm: %s\n",
574 hdrfn, hdrfline, (p - protostr) + 1, regparm);
583 // not really ret_type is array, but ohwell
584 pp->ret_type.is_array = 1;
585 p = strchr(p + 1, ']');
587 printf("%s:%d:%zd: ']' expected\n",
588 hdrfn, hdrfline, (p - protostr) + 1);
594 printf("%s:%d:%zd: ')' expected\n",
595 hdrfn, hdrfline, (p - protostr) + 1);
602 printf("%s:%d:%zd: '(' expected, got '%c'\n",
603 hdrfn, hdrfline, (p - protostr) + 1, *p);
610 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
611 && *sskip(p + 4) == ')')
622 printf("%s:%d:%zd: ',' expected\n",
623 hdrfn, hdrfline, (p - protostr) + 1);
629 if (!strncmp(p, "...", 3)) {
636 printf("%s:%d:%zd: ')' expected\n",
637 hdrfn, hdrfline, (p - protostr) + 1);
641 arg = &pp->arg[xarg];
645 ret = check_type(p, &arg->type);
647 printf("%s:%d:%zd: unhandled type for arg%d\n",
648 hdrfn, hdrfline, (p - protostr) + 1, xarg);
653 if (*p == '(' || arg->type.is_struct) {
654 // func ptr or struct
655 ret = parse_arg(&p1, arg, xarg);
657 printf("%s:%d:%zd: funcarg parse failed\n",
658 hdrfn, hdrfline, p1 - protostr);
664 p = next_idt(buf, sizeof(buf), p);
668 printf("%s:%d:%zd: idt missing for arg%d\n",
669 hdrfn, hdrfline, (p - protostr) + 1, xarg);
675 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
680 arg->reg = strdup(map_reg(regparm));
681 arg->type.is_retreg = is_retreg;
682 pp->has_retreg |= is_retreg;
685 if (IS(arg->type.name, "float")
686 || IS(arg->type.name, "double"))
688 arg->type.is_float = 1;
691 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
692 || IS(arg->type.name, "double")))
694 arg->type.is_64bit = 1;
696 pp_copy_arg(&pp->arg[xarg], arg);
697 arg = &pp->arg[xarg];
699 free(arg->type.name);
700 arg->type.name = strdup("dummy");
703 ret = check_struct_arg(arg);
705 pp->has_structarg = 1;
706 arg->type.is_struct = 1;
707 free(arg->type.name);
708 arg->type.name = strdup("int");
709 for (l = 0; l < ret; l++) {
710 pp_copy_arg(&pp->arg[xarg], arg);
716 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
717 if (pp->arg[0].reg != NULL) {
718 printf("%s:%d: %s with arg1 spec %s?\n",
719 hdrfn, hdrfline, cconv, pp->arg[0].reg);
721 pp->arg[0].reg = strdup("ecx");
724 if (xarg > 1 && IS(cconv, "__fastcall")) {
725 if (pp->arg[1].reg != NULL) {
726 printf("%s:%d: %s with arg2 spec %s?\n",
727 hdrfn, hdrfline, cconv, pp->arg[1].reg);
729 pp->arg[1].reg = strdup("edx");
734 for (i = 0; i < pp->argc; i++) {
735 if (pp->arg[i].reg == NULL)
741 if (pp->argc == 1 && pp->arg[0].reg != NULL
742 && IS(pp->arg[0].reg, "ecx"))
746 else if (pp->argc_reg == 2
747 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
748 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
753 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
754 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
761 static int pp_name_cmp(const void *p1, const void *p2)
763 const struct parsed_proto *pp1 = p1, *pp2 = p2;
764 return strcmp(pp1->name, pp2->name);
767 static int ps_name_cmp(const void *p1, const void *p2)
769 const struct parsed_struct *ps1 = p1, *ps2 = p2;
770 return strcmp(ps1->name, ps2->name);
773 // parsed struct cache
774 static struct parsed_struct *ps_cache;
775 static int ps_cache_size;
776 static int ps_cache_alloc;
778 static int struct_handler(FILE *fhdr, char *proto, int *line)
780 struct parsed_struct *ps;
786 if (ps_cache_size >= ps_cache_alloc) {
787 ps_cache_alloc = ps_cache_alloc * 2 + 64;
788 ps_cache = realloc(ps_cache, ps_cache_alloc
789 * sizeof(ps_cache[0]));
790 my_assert_not(ps_cache, NULL);
791 memset(ps_cache + ps_cache_size, 0,
792 (ps_cache_alloc - ps_cache_size)
793 * sizeof(ps_cache[0]));
796 ps = &ps_cache[ps_cache_size++];
797 ret = sscanf(proto, "struct %255s {", ps->name);
799 printf("%s:%d: struct parse failed\n", hdrfn, *line);
803 while (fgets(lstr, sizeof(lstr), fhdr))
808 if (p[0] == '/' && p[1] == '/')
813 if (m >= ARRAY_SIZE(ps->members)) {
814 printf("%s:%d: too many struct members\n",
820 ret = parse_protostr(p, &ps->members[m].pp);
822 printf("%s:%d: struct member #%d/%02x "
823 "doesn't parse\n", hdrfn, *line,
827 ps->members[m].offset = offset;
832 ps->member_count = m;
837 // parsed proto cache
838 static struct parsed_proto *pp_cache;
839 static int pp_cache_size;
840 static int pp_cache_alloc;
842 static int b_pp_c_handler(char *proto, const char *fname,
843 int is_include, int is_osinc, int is_cinc)
847 if (pp_cache_size >= pp_cache_alloc) {
848 pp_cache_alloc = pp_cache_alloc * 2 + 64;
849 pp_cache = realloc(pp_cache, pp_cache_alloc
850 * sizeof(pp_cache[0]));
851 my_assert_not(pp_cache, NULL);
852 memset(pp_cache + pp_cache_size, 0,
853 (pp_cache_alloc - pp_cache_size)
854 * sizeof(pp_cache[0]));
857 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
861 pp_cache[pp_cache_size].is_include = is_include;
862 pp_cache[pp_cache_size].is_osinc = is_osinc;
863 pp_cache[pp_cache_size].is_cinc = is_cinc;
868 static void build_caches(FILE *fhdr)
876 ret = do_protostrs(fhdr, hdrfn, 0);
880 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
881 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
882 fseek(fhdr, pos, SEEK_SET);
885 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
888 const struct parsed_proto *pp_ret;
889 struct parsed_proto pp_search;
892 if (pp_cache == NULL)
896 if (sym[0] == '_' && !IS_START(sym, "__W"))
899 strcpy(pp_search.name, sym);
900 p = strchr(pp_search.name, '@');
904 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
905 sizeof(pp_cache[0]), pp_name_cmp);
906 if (pp_ret == NULL && !quiet)
907 printf("%s: sym '%s' is missing\n", hdrfn, sym);
912 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
913 const char *type, int offset)
915 struct parsed_struct ps_search, *ps;
918 if (pp_cache == NULL)
920 if (ps_cache_size == 0)
923 while (my_isblank(*type))
925 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
928 if (sscanf(type, "%255s", ps_search.name) != 1)
931 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
932 sizeof(ps_cache[0]), ps_name_cmp);
934 printf("%s: struct '%s' is missing\n",
935 hdrfn, ps_search.name);
939 for (m = 0; m < ps->member_count; m++) {
940 if (ps->members[m].offset == offset)
941 return &ps->members[m].pp;
947 static void pp_copy_arg(struct parsed_proto_arg *d,
948 const struct parsed_proto_arg *s)
950 memcpy(d, s, sizeof(*d));
952 if (s->reg != NULL) {
953 d->reg = strdup(s->reg);
954 my_assert_not(d->reg, NULL);
956 if (s->type.name != NULL) {
957 d->type.name = strdup(s->type.name);
958 my_assert_not(d->type.name, NULL);
961 d->pp = malloc(sizeof(*d->pp));
962 my_assert_not(d->pp, NULL);
963 memcpy(d->pp, s->pp, sizeof(*d->pp));
967 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
969 struct parsed_proto *pp;
972 pp = malloc(sizeof(*pp));
973 my_assert_not(pp, NULL);
974 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
976 // do the actual deep copy..
977 for (i = 0; i < pp_c->argc; i++)
978 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
979 if (pp_c->ret_type.name != NULL)
980 pp->ret_type.name = strdup(pp_c->ret_type.name);
986 static inline int pp_cmp_func(const struct parsed_proto *pp1,
987 const struct parsed_proto *pp2)
991 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
994 for (i = 0; i < pp1->argc; i++) {
995 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
998 if ((pp1->arg[i].reg != NULL)
999 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
1009 static inline void pp_print(char *buf, size_t buf_size,
1010 const struct parsed_proto *pp)
1015 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
1018 for (i = 0; i < pp->argc_reg; i++) {
1019 snprintf(buf + l, buf_size - l, "%s%s",
1020 i == 0 ? "" : ", ", pp->arg[i].reg);
1023 if (pp->argc_stack > 0) {
1024 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1025 i == 0 ? "" : ", ", pp->argc_stack);
1028 snprintf(buf + l, buf_size - l, ")");
1031 static inline void proto_release(struct parsed_proto *pp)
1035 for (i = 0; i < pp->argc; i++) {
1036 if (pp->arg[i].reg != NULL)
1037 free(pp->arg[i].reg);
1038 if (pp->arg[i].type.name != NULL)
1039 free(pp->arg[i].type.name);
1040 if (pp->arg[i].pp != NULL)
1041 free(pp->arg[i].pp);
1043 if (pp->ret_type.name != NULL)
1044 free(pp->ret_type.name);
1047 (void)proto_lookup_struct;