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[32];
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..
232 "PMEMORY_BASIC_INFORMATION",
243 "SERVICE_STATUS_HANDLE",
252 static const char *ignored_keywords[] = {
260 static int typecmp(const char *n, const char *t)
262 for (; *t != 0; n++, t++) {
263 while (n[0] == ' ' && (n[1] == ' ' || n[1] == '*'))
265 while (t[0] == ' ' && (t[1] == ' ' || t[1] == '*'))
274 static const char *skip_type_mod(const char *n)
279 for (i = 0; i < ARRAY_SIZE(known_type_mod); i++) {
280 len = strlen(known_type_mod[i]);
281 if (strncmp(n, known_type_mod[i], len) != 0)
283 if (!my_isblank(n[len]))
287 while (my_isblank(*n))
295 static int check_type(const char *name, struct parsed_type *type)
301 n = skip_type_mod(name);
303 if (!strncmp(n, "struct", 6) && my_isblank(n[6])) {
307 while (my_isblank(*n))
311 for (i = 0; i < ARRAY_SIZE(known_ptr_types); i++) {
312 if (typecmp(n, known_ptr_types[i]))
319 if (n[0] == 'L' && n[1] == 'P' && strncmp(n, "LPARAM", 6))
322 // assume single word
323 while (!my_isblank(*n) && !my_issep(*n))
328 while (my_isblank(*n))
339 type->name = strndup(name, ret);
340 if (IS(type->name, "__VALIST") || IS(type->name, "va_list"))
341 type->is_va_list = 1;
342 if (IS(type->name, "VOID"))
343 memcpy(type->name, "void", 4);
348 /* args are always expanded to 32bit */
349 static const char *map_reg(const char *reg)
351 const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" };
352 const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" };
353 const char *regs_b[] = { "al", "bl", "cl", "dl" };
356 for (i = 0; i < ARRAY_SIZE(regs_w); i++)
357 if (IS(reg, regs_w[i]))
360 for (i = 0; i < ARRAY_SIZE(regs_b); i++)
361 if (IS(reg, regs_b[i]))
367 static int check_struct_arg(struct parsed_proto_arg *arg)
369 if (IS(arg->type.name, "POINT"))
375 static int parse_protostr(char *protostr, struct parsed_proto *pp);
377 static int parse_arg(char **p_, struct parsed_proto_arg *arg, int xarg)
384 arg->pp = calloc(1, sizeof(*arg->pp));
385 my_assert_not(arg->pp, NULL);
390 pe = strpbrk(pe, ",()");
393 if (*pe == ',' || *pe == ')')
395 pe = strchr(pe, ')');
401 if (pe - p > sizeof(buf) - 1)
403 memcpy(buf, p, pe - p);
406 ret = parse_protostr(buf, arg->pp);
410 if (IS_START(arg->pp->name, "guess"))
411 arg->pp->is_guessed = 1;
413 // we don't use actual names right now...
414 snprintf(arg->pp->name, sizeof(arg->pp->name), "a%d", xarg);
416 if (!arg->type.is_struct)
417 // we'll treat it as void * for non-calls
418 arg->type.name = strdup("void *");
419 arg->type.is_ptr = 1;
426 static int parse_protostr(char *protostr, struct parsed_proto *pp)
428 struct parsed_proto_arg *arg;
439 if (p[0] == '/' && p[1] == '/') {
440 printf("%s:%d: commented out?\n", hdrfn, hdrfline);
444 // allow start of line comment
445 if (p[0] == '/' && p[1] == '*') {
446 p = strstr(p + 2, "*/");
448 printf("%s:%d: multiline comments unsupported\n",
455 // we need remaining hints in comments, so strip / *
456 for (p1 = p; p1[0] != 0 && p1[1] != 0; p1++) {
457 if ((p1[0] == '/' && p1[1] == '*')
458 || (p1[0] == '*' && p1[1] == '/'))
462 if (!strncmp(p, "DECLSPEC_NORETURN ", 18)) {
467 for (i = 0; i < ARRAY_SIZE(ignored_keywords); i++) {
468 l = strlen(ignored_keywords[i]);
469 if (!strncmp(p, ignored_keywords[i], l) && my_isblank(p[l]))
470 p = sskip(p + l + 1);
473 if (IS_START(p, "DECL_IMPORT ")) {
478 ret = check_type(p, &pp->ret_type);
480 printf("%s:%d:%zd: unhandled return in '%s'\n",
481 hdrfn, hdrfline, (p - protostr) + 1, protostr);
486 if (!strncmp(p, "noreturn ", 9)) {
491 if (!strchr(p, ')')) {
492 p = next_idt(buf, sizeof(buf), p);
494 if (!pp->is_arg && buf[0] == 0) {
495 printf("%s:%d:%zd: var name is missing\n",
496 hdrfn, hdrfline, (p - protostr) + 1);
499 strcpy(pp->name, buf);
504 pp->ret_type.is_array = 1;
516 p = next_word(cconv, sizeof(cconv), p);
519 printf("%s:%d:%zd: cconv missing\n",
520 hdrfn, hdrfline, (p - protostr) + 1);
523 if (IS(cconv, "__cdecl"))
525 else if (IS(cconv, "__stdcall"))
527 else if (IS(cconv, "__fastcall")) {
529 pp->is_stdcall = 1; // sort of..
531 else if (IS(cconv, "__thiscall"))
533 else if (IS(cconv, "__userpurge"))
534 pp->is_stdcall = 1; // IDA
535 else if (IS(cconv, "__usercall"))
536 pp->is_stdcall = 0; // IDA
537 else if (IS(cconv, "__userstack")) {
538 pp->is_stdcall = 0; // custom
539 pp->is_userstack = 1;
541 else if (IS(cconv, "WINAPI") || IS(cconv, "PASCAL"))
544 printf("%s:%d:%zd: unhandled cconv: '%s'\n",
545 hdrfn, hdrfline, (p - protostr) + 1, cconv);
551 printf("%s:%d:%zd: '*' expected\n",
552 hdrfn, hdrfline, (p - protostr) + 1);
556 // XXX: skipping extra asterisks, for now
562 p = next_idt(buf, sizeof(buf), p);
565 //printf("%s:%d:%zd: func name missing\n",
566 // hdrfn, hdrfline, (p - protostr) + 1);
569 strcpy(pp->name, buf);
571 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
573 if (!IS(regparm, "eax") && !IS(regparm, "ax")
574 && !IS(regparm, "al") && !IS(regparm, "edx:eax"))
576 printf("%s:%d:%zd: bad regparm: %s\n",
577 hdrfn, hdrfline, (p - protostr) + 1, regparm);
586 // not really ret_type is array, but ohwell
587 pp->ret_type.is_array = 1;
588 p = strchr(p + 1, ']');
590 printf("%s:%d:%zd: ']' expected\n",
591 hdrfn, hdrfline, (p - protostr) + 1);
597 printf("%s:%d:%zd: ')' expected\n",
598 hdrfn, hdrfline, (p - protostr) + 1);
605 printf("%s:%d:%zd: '(' expected, got '%c'\n",
606 hdrfn, hdrfline, (p - protostr) + 1, *p);
613 if ((!strncmp(p, "void", 4) || !strncmp(p, "VOID", 4))
614 && *sskip(p + 4) == ')')
625 printf("%s:%d:%zd: ',' expected\n",
626 hdrfn, hdrfline, (p - protostr) + 1);
632 if (!strncmp(p, "...", 3)) {
639 printf("%s:%d:%zd: ')' expected\n",
640 hdrfn, hdrfline, (p - protostr) + 1);
644 if (xarg >= ARRAY_SIZE(pp->arg)) {
645 printf("%s:%d:%zd: too many args\n",
646 hdrfn, hdrfline, (p - protostr) + 1);
650 arg = &pp->arg[xarg];
654 ret = check_type(p, &arg->type);
656 printf("%s:%d:%zd: unhandled type for arg%d\n",
657 hdrfn, hdrfline, (p - protostr) + 1, xarg);
662 if (*p == '(' || arg->type.is_struct) {
663 // func ptr or struct
664 ret = parse_arg(&p1, arg, xarg);
666 printf("%s:%d:%zd: funcarg parse failed\n",
667 hdrfn, hdrfline, p1 - protostr);
673 p = next_idt(buf, sizeof(buf), p);
677 printf("%s:%d:%zd: idt missing for arg%d\n",
678 hdrfn, hdrfline, (p - protostr) + 1, xarg);
684 ret = get_regparm(regparm, sizeof(regparm), p, &is_retreg);
689 arg->reg = strdup(map_reg(regparm));
690 arg->type.is_retreg = is_retreg;
691 pp->has_retreg |= is_retreg;
694 if (IS(arg->type.name, "float")
695 || IS(arg->type.name, "double"))
697 arg->type.is_float = 1;
700 if (!arg->type.is_ptr && (strstr(arg->type.name, "int64")
701 || IS(arg->type.name, "double")))
703 arg->type.is_64bit = 1;
705 pp_copy_arg(&pp->arg[xarg], arg);
706 arg = &pp->arg[xarg];
708 free(arg->type.name);
709 arg->type.name = strdup("dummy");
712 ret = check_struct_arg(arg);
714 pp->has_structarg = 1;
715 arg->type.is_struct = 1;
716 free(arg->type.name);
717 arg->type.name = strdup("int");
718 for (l = 0; l < ret; l++) {
719 pp_copy_arg(&pp->arg[xarg], arg);
725 if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) {
726 if (pp->arg[0].reg != NULL) {
727 printf("%s:%d: %s with arg1 spec %s?\n",
728 hdrfn, hdrfline, cconv, pp->arg[0].reg);
730 pp->arg[0].reg = strdup("ecx");
733 if (xarg > 1 && IS(cconv, "__fastcall")) {
734 if (pp->arg[1].reg != NULL) {
735 printf("%s:%d: %s with arg2 spec %s?\n",
736 hdrfn, hdrfline, cconv, pp->arg[1].reg);
738 pp->arg[1].reg = strdup("edx");
743 for (i = 0; i < pp->argc; i++) {
744 if (pp->arg[i].reg == NULL)
750 if (pp->argc == 1 && pp->arg[0].reg != NULL
751 && IS(pp->arg[0].reg, "ecx"))
755 else if (pp->argc_reg == 2
756 && pp->arg[0].reg != NULL && IS(pp->arg[0].reg, "ecx")
757 && pp->arg[1].reg != NULL && IS(pp->arg[1].reg, "edx"))
762 if (pp->is_vararg && (pp->is_stdcall || pp->is_fastcall)) {
763 printf("%s:%d: vararg %s?\n", hdrfn, hdrfline, cconv);
770 static int pp_name_cmp(const void *p1, const void *p2)
772 const struct parsed_proto *pp1 = p1, *pp2 = p2;
773 return strcmp(pp1->name, pp2->name);
776 static int ps_name_cmp(const void *p1, const void *p2)
778 const struct parsed_struct *ps1 = p1, *ps2 = p2;
779 return strcmp(ps1->name, ps2->name);
782 // parsed struct cache
783 static struct parsed_struct *ps_cache;
784 static int ps_cache_size;
785 static int ps_cache_alloc;
787 static int struct_handler(FILE *fhdr, char *proto, int *line)
789 struct parsed_struct *ps;
795 if (ps_cache_size >= ps_cache_alloc) {
796 ps_cache_alloc = ps_cache_alloc * 2 + 64;
797 ps_cache = realloc(ps_cache, ps_cache_alloc
798 * sizeof(ps_cache[0]));
799 my_assert_not(ps_cache, NULL);
800 memset(ps_cache + ps_cache_size, 0,
801 (ps_cache_alloc - ps_cache_size)
802 * sizeof(ps_cache[0]));
805 ps = &ps_cache[ps_cache_size++];
806 ret = sscanf(proto, "struct %255s {", ps->name);
808 printf("%s:%d: struct parse failed\n", hdrfn, *line);
812 while (fgets(lstr, sizeof(lstr), fhdr))
817 if (p[0] == '/' && p[1] == '/')
822 if (m >= ARRAY_SIZE(ps->members)) {
823 printf("%s:%d: too many struct members\n",
829 ret = parse_protostr(p, &ps->members[m].pp);
831 printf("%s:%d: struct member #%d/%02x "
832 "doesn't parse\n", hdrfn, *line,
836 ps->members[m].offset = offset;
841 ps->member_count = m;
846 // parsed proto cache
847 static struct parsed_proto *pp_cache;
848 static int pp_cache_size;
849 static int pp_cache_alloc;
851 static int b_pp_c_handler(char *proto, const char *fname,
852 int is_include, int is_osinc, int is_cinc)
856 if (pp_cache_size >= pp_cache_alloc) {
857 pp_cache_alloc = pp_cache_alloc * 2 + 64;
858 pp_cache = realloc(pp_cache, pp_cache_alloc
859 * sizeof(pp_cache[0]));
860 my_assert_not(pp_cache, NULL);
861 memset(pp_cache + pp_cache_size, 0,
862 (pp_cache_alloc - pp_cache_size)
863 * sizeof(pp_cache[0]));
866 ret = parse_protostr(proto, &pp_cache[pp_cache_size]);
870 pp_cache[pp_cache_size].is_include = is_include;
871 pp_cache[pp_cache_size].is_osinc = is_osinc;
872 pp_cache[pp_cache_size].is_cinc = is_cinc;
877 static void build_caches(FILE *fhdr)
885 ret = do_protostrs(fhdr, hdrfn, 0);
889 qsort(pp_cache, pp_cache_size, sizeof(pp_cache[0]), pp_name_cmp);
890 qsort(ps_cache, ps_cache_size, sizeof(ps_cache[0]), ps_name_cmp);
891 fseek(fhdr, pos, SEEK_SET);
894 static const struct parsed_proto *proto_parse(FILE *fhdr, const char *sym,
897 const struct parsed_proto *pp_ret;
898 struct parsed_proto pp_search;
901 if (pp_cache == NULL)
905 if (sym[0] == '_' && !IS_START(sym, "__W"))
908 strcpy(pp_search.name, sym);
909 p = strchr(pp_search.name, '@');
913 pp_ret = bsearch(&pp_search, pp_cache, pp_cache_size,
914 sizeof(pp_cache[0]), pp_name_cmp);
915 if (pp_ret == NULL && !quiet)
916 printf("%s: sym '%s' is missing\n", hdrfn, sym);
921 static const struct parsed_proto *proto_lookup_struct(FILE *fhdr,
922 const char *type, int offset)
924 struct parsed_struct ps_search, *ps;
927 if (pp_cache == NULL)
929 if (ps_cache_size == 0)
932 while (my_isblank(*type))
934 if (!strncmp(type, "struct", 6) && my_isblank(type[6]))
937 if (sscanf(type, "%255s", ps_search.name) != 1)
940 ps = bsearch(&ps_search, ps_cache, ps_cache_size,
941 sizeof(ps_cache[0]), ps_name_cmp);
943 printf("%s: struct '%s' is missing\n",
944 hdrfn, ps_search.name);
948 for (m = 0; m < ps->member_count; m++) {
949 if (ps->members[m].offset == offset)
950 return &ps->members[m].pp;
956 static void pp_copy_arg(struct parsed_proto_arg *d,
957 const struct parsed_proto_arg *s)
959 memcpy(d, s, sizeof(*d));
961 if (s->reg != NULL) {
962 d->reg = strdup(s->reg);
963 my_assert_not(d->reg, NULL);
965 if (s->type.name != NULL) {
966 d->type.name = strdup(s->type.name);
967 my_assert_not(d->type.name, NULL);
970 d->pp = malloc(sizeof(*d->pp));
971 my_assert_not(d->pp, NULL);
972 memcpy(d->pp, s->pp, sizeof(*d->pp));
976 struct parsed_proto *proto_clone(const struct parsed_proto *pp_c)
978 struct parsed_proto *pp;
981 pp = malloc(sizeof(*pp));
982 my_assert_not(pp, NULL);
983 memcpy(pp, pp_c, sizeof(*pp)); // lazy..
985 // do the actual deep copy..
986 for (i = 0; i < pp_c->argc; i++)
987 pp_copy_arg(&pp->arg[i], &pp_c->arg[i]);
988 if (pp_c->ret_type.name != NULL)
989 pp->ret_type.name = strdup(pp_c->ret_type.name);
995 static inline int pp_cmp_func(const struct parsed_proto *pp1,
996 const struct parsed_proto *pp2)
1000 if (pp1->argc != pp2->argc || pp1->argc_reg != pp2->argc_reg)
1003 for (i = 0; i < pp1->argc; i++) {
1004 if ((pp1->arg[i].reg != NULL) != (pp2->arg[i].reg != NULL))
1007 if ((pp1->arg[i].reg != NULL)
1008 && !IS(pp1->arg[i].reg, pp2->arg[i].reg))
1018 static inline void pp_print(char *buf, size_t buf_size,
1019 const struct parsed_proto *pp)
1024 snprintf(buf, buf_size, "%s %s(", pp->ret_type.name, pp->name);
1027 for (i = 0; i < pp->argc_reg; i++) {
1028 snprintf(buf + l, buf_size - l, "%s%s",
1029 i == 0 ? "" : ", ", pp->arg[i].reg);
1032 if (pp->argc_stack > 0) {
1033 snprintf(buf + l, buf_size - l, "%s{%d stack}",
1034 i == 0 ? "" : ", ", pp->argc_stack);
1037 snprintf(buf + l, buf_size - l, ")");
1040 static inline void proto_release(struct parsed_proto *pp)
1044 for (i = 0; i < pp->argc; i++) {
1045 if (pp->arg[i].reg != NULL)
1046 free(pp->arg[i].reg);
1047 if (pp->arg[i].type.name != NULL)
1048 free(pp->arg[i].type.name);
1049 if (pp->arg[i].pp != NULL)
1050 free(pp->arg[i].pp);
1052 if (pp->ret_type.name != NULL)
1053 free(pp->ret_type.name);
1056 (void)proto_lookup_struct;