X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tools%2Fmkbridge.c;h=29bdd9a55a708547c58d269fb284e68ac8fef7d8;hb=HEAD;hp=ae2aec3db27825c51d970b1bfe7ee19ff13694ba;hpb=3e52f54c9322fed87ea1b4c4d2fcfdf4c267afa6;p=ia32rtools.git diff --git a/tools/mkbridge.c b/tools/mkbridge.c index ae2aec3..29bdd9a 100644 --- a/tools/mkbridge.c +++ b/tools/mkbridge.c @@ -1,254 +1,22 @@ +/* + * ia32rtools + * (C) notaz, 2013,2014 + * + * This work is licensed under the terms of 3-clause BSD license. + * See COPYING file in the top-level directory. + */ + #include #include #include #include "my_assert.h" #include "my_str.h" +#include "common.h" -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) -#define IS(w, y) !strcmp(w, y) - -static int find_protostr(char *dst, size_t dlen, FILE *fhdr, - const char *sym, int *pline) -{ - int line = 0; - char *p; - - rewind(fhdr); - - while (fgets(dst, dlen, fhdr)) - { - line++; - if (strstr(dst, sym) != NULL) - break; - } - *pline = line; - - if (feof(fhdr)) - return -1; - - p = dst + strlen(dst); - for (p--; p > dst && my_isblank(*p); --p) - *p = 0; - - return 0; -} - -static int get_regparm(char *dst, size_t dlen, char *p) -{ - int i, o; - - if (*p != '<') - return 0; - - for (o = 0, i = 1; o < dlen; i++) { - if (p[i] == 0) - return 0; - if (p[i] == '>') - break; - dst[o++] = p[i]; - } - dst[o] = 0; - return i + 1; -} - -// hmh.. -static const char *known_types[] = { - "const void *", - "void *", - "char *", - "FILE *", - "unsigned __int8", - "unsigned __int16", - "unsigned int", - "signed int", - "char", - "__int8", - "__int16", - "int", - "bool", - "void", - "BYTE", - "WORD", - "DWORD", - "HMODULE", - "HANDLE", - "HWND", - "LPCSTR", - "size_t", -}; - -static int check_type(const char *name) -{ - int i, l; - - for (i = 0; i < ARRAY_SIZE(known_types); i++) { - l = strlen(known_types[i]); - if (strncmp(known_types[i], name, l) == 0) - return l; - } - - return 0; -} - -/* args are always expanded to 32bit */ -static const char *map_reg(const char *reg) -{ - const char *regs_f[] = { "eax", "ebx", "ecx", "edx", "esi", "edi" }; - const char *regs_w[] = { "ax", "bx", "cx", "dx", "si", "di" }; - const char *regs_b[] = { "al", "bl", "cl", "dl" }; - int i; - - for (i = 0; i < ARRAY_SIZE(regs_w); i++) - if (IS(reg, regs_w[i])) - return regs_f[i]; - - for (i = 0; i < ARRAY_SIZE(regs_b); i++) - if (IS(reg, regs_b[i])) - return regs_f[i]; - - return reg; -} - -static const char *hdrfn; -static int pline = 0; - -static int parse_protostr(char *protostr, char **reglist, int *cnt_out, - int *is_stdcall) -{ - char regparm[16]; - char buf[256]; - char cconv[32]; - int xarg = 0; - int ret; - char *p; - - p = protostr; - if (p[0] == '/' && p[1] == '/') { - //printf("warning: decl for sym '%s' is commented out\n", sym); - p = sskip(p + 2); - } - - ret = check_type(p); - if (ret <= 0) { - printf("%s:%d:%ld: unhandled return in '%s'\n", - hdrfn, pline, (p - protostr) + 1, protostr); - return 1; - } - p += ret; - p = sskip(p); - - p = next_word(cconv, sizeof(cconv), p); - p = sskip(p); - if (cconv[0] == 0) { - printf("%s:%d:%ld: cconv missing\n", - hdrfn, pline, (p - protostr) + 1); - return 1; - } - if (IS(cconv, "__cdecl")) - *is_stdcall = 0; - else if (IS(cconv, "__stdcall")) - *is_stdcall = 1; - else if (IS(cconv, "__fastcall")) - *is_stdcall = 1; - else if (IS(cconv, "__thiscall")) - *is_stdcall = 1; - else if (IS(cconv, "__userpurge")) - *is_stdcall = 1; // in all cases seen.. - else if (IS(cconv, "__usercall")) - *is_stdcall = 0; // ..or is it? - else { - printf("%s:%d:%ld: unhandled cconv: '%s'\n", - hdrfn, pline, (p - protostr) + 1, cconv); - return 1; - } - - p = next_idt(buf, sizeof(buf), p); - p = sskip(p); - if (buf[0] == 0) { - printf("%s:%d:%ld: func name missing\n", - hdrfn, pline, (p - protostr) + 1); - return 1; - } +#include "protoparse.h" - ret = get_regparm(regparm, sizeof(regparm), p); - if (ret > 0) { - if (!IS(regparm, "eax") && !IS(regparm, "ax") - && !IS(regparm, "al")) - { - printf("%s:%d:%ld: bad regparm: %s\n", - hdrfn, pline, (p - protostr) + 1, regparm); - return 1; - } - p += ret; - p = sskip(p); - } - - if (*p != '(') { - printf("%s:%d:%ld: '(' expected, got '%c'\n", - hdrfn, pline, (p - protostr) + 1, *p); - return 1; - } - p++; - - while (1) { - p = sskip(p); - if (*p == ')') - break; - if (*p == ',') - p = sskip(p + 1); - - xarg++; - - ret = check_type(p); - if (ret <= 0) { - printf("%s:%d:%ld: unhandled type for arg%d\n", - hdrfn, pline, (p - protostr) + 1, xarg); - return 1; - } - p += ret; - p = sskip(p); - - p = next_idt(buf, sizeof(buf), p); - p = sskip(p); -#if 0 - if (buf[0] == 0) { - printf("%s:%d:%ld: idt missing for arg%d\n", - hdrfn, pline, (p - protostr) + 1, xarg); - return 1; - } -#endif - reglist[xarg - 1] = NULL; - - ret = get_regparm(regparm, sizeof(regparm), p); - if (ret > 0) { - p += ret; - p = sskip(p); - - reglist[xarg - 1] = strdup(map_reg(regparm)); - } - } - - if (xarg > 0 && (IS(cconv, "__fastcall") || IS(cconv, "__thiscall"))) { - if (reglist[0] != NULL) { - printf("%s:%d: %s with arg1 spec %s?\n", - hdrfn, pline, cconv, reglist[0]); - } - reglist[0] = strdup("ecx"); - } - - if (xarg > 1 && IS(cconv, "__fastcall")) { - if (reglist[1] != NULL) { - printf("%s:%d: %s with arg2 spec %s?\n", - hdrfn, pline, cconv, reglist[1]); - } - reglist[1] = strdup("edx"); - } - - *cnt_out = xarg; - - return 0; -} +static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" }; static int is_x86_reg_saved(const char *reg) { @@ -263,55 +31,86 @@ static int is_x86_reg_saved(const char *reg) return !nosave; } -static void out_toasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, - int is_stdcall) +// output decorated name +static const char *pp_to_name(const struct parsed_proto *pp) +{ + static char buf[256]; + char atval[16]; + + if (!pp->is_fastcall && pp->argc_reg != 0) { + // can only be handled by __cdecl C func + snprintf(buf, sizeof(buf), "_%s", pp->name); + return buf; + } + + atval[0] = 0; + if (pp->is_stdcall) { + snprintf(atval, sizeof(atval), "@%d", + pp->argc * 4); + } + snprintf(buf, sizeof(buf), "%s%s%s", + pp->is_fastcall ? "@" : "_", + pp->name, atval); + + return buf; +} + +static void out_toasm_x86(FILE *f, const char *sym_out, + const struct parsed_proto *pp) { - int have_normal = 0; // normal args - int have_regs = 0; int must_save = 0; int sarg_ofs = 1; // stack offset to args, in DWORDs int args_repushed = 0; + int argc_repush; + const char *name; int i; - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] == NULL) { - have_normal++; - continue; - } + argc_repush = pp->argc; + if (pp->is_vararg) + argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? - have_regs++; - must_save |= is_x86_reg_saved(reg_list[i]); + for (i = 0; i < pp->argc; i++) { + if (pp->arg[i].reg != NULL) + must_save |= is_x86_reg_saved(pp->arg[i].reg); } - fprintf(f, ".global _%s\n", sym); - fprintf(f, "_%s:\n", sym); + name = pp_to_name(pp); + fprintf(f, ".global %s\n", name); + fprintf(f, "%s:\n", name); - if (!have_regs && !is_stdcall) { - fprintf(f, "\tjmp %s\n\n", sym); + if (pp->argc_reg == 0 || pp->is_fastcall) { + fprintf(f, "\t# %s\n", + pp->is_fastcall ? "__fastcall" : + (pp->is_stdcall ? "__stdcall" : "__cdecl")); + fprintf(f, "\tjmp %s\n\n", sym_out); return; } - if (!have_normal && !must_save && !is_stdcall) { + if (pp->argc_stack == 0 && !must_save && !pp->is_stdcall + && !pp->is_vararg && !pp->has_retreg) + { // load arg regs - for (i = 0; i < reg_cnt; i++) { + for (i = 0; i < pp->argc; i++) { fprintf(f, "\tmovl %d(%%esp), %%%s\n", - (i + sarg_ofs) * 4, reg_list[i]); + (i + sarg_ofs) * 4, pp->arg[i].reg); } - fprintf(f, "\tjmp %s\n\n", sym); + fprintf(f, "\tjmp %s\n\n", sym_out); return; } + // asm_stack_args | saved_regs | ra | args_from_c + // save the regs - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] != NULL && is_x86_reg_saved(reg_list[i])) { - fprintf(f, "\tpushl %%%s\n", reg_list[i]); - sarg_ofs++; - } + // because we don't always know what we are calling, + // be safe and save everything that has to be saved in __cdecl + for (i = 0; i < ARRAY_SIZE(c_save_regs); i++) { + fprintf(f, "\tpushl %%%s\n", c_save_regs[i]); + sarg_ofs++; } - // reconstruct arg stack - for (i = reg_cnt - 1; i >= 0; i--) { - if (reg_list[i] == NULL) { + // reconstruct arg stack for asm + for (i = argc_repush - 1; i >= 0; i--) { + if (pp->arg[i].reg == NULL) { fprintf(f, "\tmovl %d(%%esp), %%eax\n", (i + sarg_ofs) * 4); fprintf(f, "\tpushl %%eax\n"); @@ -319,112 +118,197 @@ static void out_toasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, args_repushed++; } } - my_assert(args_repushed, have_normal); // load arg regs - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] != NULL) { + for (i = 0; i < pp->argc; i++) { + if (pp->arg[i].reg != NULL) { fprintf(f, "\tmovl %d(%%esp), %%%s\n", - (i + sarg_ofs) * 4, reg_list[i]); + (i + sarg_ofs) * 4, pp->arg[i].reg); + if (pp->arg[i].type.is_retreg) + fprintf(f, "\tmovl (%%%s), %%%s\n", + pp->arg[i].reg, pp->arg[i].reg); } } - fprintf(f, "\n\t# %s\n", is_stdcall ? "__stdcall" : "__cdecl"); - fprintf(f, "\tcall %s\n\n", sym); + fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl"); + fprintf(f, "\tcall %s\n\n", sym_out); - if (args_repushed && !is_stdcall) + if (args_repushed && !pp->is_stdcall) { fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4); + sarg_ofs -= args_repushed; + } - // restore regs - for (i = reg_cnt - 1; i >= 0; i--) { - if (reg_list[i] != NULL && is_x86_reg_saved(reg_list[i])) - fprintf(f, "\tpopl %%%s\n", reg_list[i]); + // update the retreg regs + if (pp->has_retreg) { + for (i = 0; i < pp->argc; i++) { + if (pp->arg[i].type.is_retreg) { + fprintf(f, "\tmovl %d(%%esp), %%ecx\n" + "\tmovl %%%s, (%%ecx)\n", + (i + sarg_ofs) * 4, pp->arg[i].reg); + } + } } + // restore regs + for (i = ARRAY_SIZE(c_save_regs) - 1; i >= 0; i--) + fprintf(f, "\tpopl %%%s\n", c_save_regs[i]); + fprintf(f, "\tret\n\n"); } -static void out_fromasm_x86(FILE *f, char *sym, char *reg_list[], int reg_cnt, - int is_stdcall) +static void out_fromasm_x86(FILE *f, const char *sym, + const struct parsed_proto *pp) { - int have_normal = 0; // normal args - int have_regs = 0; + int reg_ofs[ARRAY_SIZE(pp->arg)]; int sarg_ofs = 1; // stack offset to args, in DWORDs + int saved_regs = 0; + int ecx_ofs = -1; + int edx_ofs = -1; + int c_is_stdcall; + int argc_repush; int stack_args; + int ret64; int i; - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] == NULL) { - have_normal++; - continue; - } - - have_regs++; + argc_repush = pp->argc; + stack_args = pp->argc_stack; + if (pp->is_vararg) { + argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough? + stack_args = argc_repush - pp->argc_reg; } - fprintf(f, "# %s\n", is_stdcall ? "__stdcall" : "__cdecl"); - fprintf(f, ".global %s\n", sym); + ret64 = strstr(pp->ret_type.name, "int64") != NULL; + + fprintf(f, "# %s", + pp->is_fastcall ? "__fastcall" : + (pp->is_stdcall ? "__stdcall" : "__cdecl")); + if (ret64) + fprintf(f, " ret64"); + if (!pp->is_fastcall && pp->argc_reg != 0) + fprintf(f, " +reg"); + + if (pp->is_stdcall && !pp->is_fastcall && pp->argc_reg != 0 + && !IS_START(sym, "sub_") && !IS_START(sym, "f_")) + { + // alias for possible .def export + char sym2[256]; + + snprintf(sym2, sizeof(sym2), "_%s@%d", + sym, pp->argc * 4); + fprintf(f, "\n.global %s # for .def\n", sym2); + fprintf(f, "%s:", sym2); + } + fprintf(f, "\n.global %s\n", sym); fprintf(f, "%s:\n", sym); - if (!have_regs && !is_stdcall) { - fprintf(f, "\tjmp _%s\n\n", sym); + if ((pp->argc_reg == 0 || pp->is_fastcall) + && !IS(pp->name, "storm_491")) // wants edx save :( + { + fprintf(f, "\tjmp %s\n\n", pp_to_name(pp)); return; } - fprintf(f, "\tpushl %%edx\n"); // just in case.. + c_is_stdcall = (pp->argc_reg == 0 && pp->is_stdcall); + + // at least sc sub_47B150 needs edx to be preserved + // int64 returns use edx:eax - no edx save + // we use ecx also as scratch + fprintf(f, "\tpushl %%ecx\n"); + saved_regs++; sarg_ofs++; + ecx_ofs = sarg_ofs; + if (!ret64) { + fprintf(f, "\tpushl %%edx\n"); + saved_regs++; + sarg_ofs++; + edx_ofs = sarg_ofs; + } + + // need space for retreg args + if (pp->has_retreg) { + for (i = 0; i < pp->argc; i++) { + if (!pp->arg[i].type.is_retreg) + continue; + if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) { + reg_ofs[i] = ecx_ofs; + continue; + } + if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) { + reg_ofs[i] = edx_ofs; + continue; + } + fprintf(f, "\tpushl %%%s\n", pp->arg[i].reg); + saved_regs++; + sarg_ofs++; + reg_ofs[i] = sarg_ofs; + } + } // construct arg stack - stack_args = have_normal; - for (i = reg_cnt - 1; i >= 0; i--) { - if (reg_list[i] == NULL) { - fprintf(f, "\tmovl %d(%%esp), %%edx\n", + for (i = argc_repush - 1; i >= 0; i--) { + if (pp->arg[i].reg == NULL) { + fprintf(f, "\tmovl %d(%%esp), %%ecx\n", (sarg_ofs + stack_args - 1) * 4); - fprintf(f, "\tpushl %%edx\n"); + fprintf(f, "\tpushl %%ecx\n"); stack_args--; } else { - fprintf(f, "\tpushl %%%s\n", reg_list[i]); + const char *reg = pp->arg[i].reg; + if (pp->arg[i].type.is_retreg) { + reg = "ecx"; + fprintf(f, "\tlea %d(%%esp), %%ecx\n", + (sarg_ofs - reg_ofs[i]) * 4); + } + else if (IS(reg, "ecx")) + // must reload original ecx + fprintf(f, "\tmovl %d(%%esp), %%ecx\n", + (sarg_ofs - 2) * 4); + + fprintf(f, "\tpushl %%%s\n", reg); } sarg_ofs++; } - // no worries about calling conventions - always __cdecl - fprintf(f, "\n\tcall _%s\n\n", sym); - - if (sarg_ofs > 2) - fprintf(f, "\tadd $%d,%%esp\n", (sarg_ofs - 2) * 4); + fprintf(f, "\n\tcall %s\n\n", pp_to_name(pp)); + + if (!c_is_stdcall && sarg_ofs > saved_regs + 1) + fprintf(f, "\tadd $%d,%%esp\n", + (sarg_ofs - (saved_regs + 1)) * 4); + + // pop retregs + if (pp->has_retreg) { + for (i = pp->argc - 1; i >= 0; i--) { + if (!pp->arg[i].type.is_retreg) + continue; + if (IS(pp->arg[i].reg, "ecx") && ecx_ofs >= 0) { + continue; + } + if (IS(pp->arg[i].reg, "edx") && edx_ofs >= 0) { + continue; + } + fprintf(f, "\tpopl %%%s\n", pp->arg[i].reg); + } + } - fprintf(f, "\tpopl %%edx\n"); + if (!ret64) + fprintf(f, "\tpopl %%edx\n"); + fprintf(f, "\tpopl %%ecx\n"); - if (is_stdcall && have_normal) - fprintf(f, "\tret $%d\n\n", have_normal * 4); + if (pp->is_stdcall && pp->argc_stack) + fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4); else fprintf(f, "\tret\n\n"); } -static void free_reglist(char *reg_list[], int reg_cnt) -{ - int i; - - for (i = 0; i < reg_cnt; i++) { - if (reg_list[i] == NULL) { - free(reg_list[i]); - reg_list[i] = NULL; - } - } -} - int main(int argc, char *argv[]) { FILE *fout, *fsyms_to, *fsyms_from, *fhdr; - char protostr[256]; + const struct parsed_proto *pp; char line[256]; + char sym_noat[256]; char sym[256]; - char *reg_list[16]; - int is_stdcall = 0; - int reg_cnt = 0; - int ret; + char *p; + int ret = 1; if (argc != 5) { printf("usage:\n%s \n", @@ -446,7 +330,7 @@ int main(int argc, char *argv[]) my_assert_not(fout, NULL); fprintf(fout, ".text\n\n"); - fprintf(fout, "# to asm\n\n"); + fprintf(fout, "# C -> asm\n\n"); while (fgets(line, sizeof(line), fsyms_to)) { @@ -454,23 +338,20 @@ int main(int argc, char *argv[]) if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') continue; - ret = find_protostr(protostr, sizeof(protostr), fhdr, - sym, &pline); - if (ret != 0) { - printf("%s: sym '%s' is missing\n", - hdrfn, sym); - goto out; - } + // IDA asm doesn't do '@' notation.. + strcpy(sym_noat, sym); + p = strchr(sym_noat, '@'); + if (p != NULL) + *p = 0; - ret = parse_protostr(protostr, reg_list, ®_cnt, &is_stdcall); - if (ret) + pp = proto_parse(fhdr, sym_noat, 0); + if (pp == NULL) goto out; - out_toasm_x86(fout, sym, reg_list, reg_cnt, is_stdcall); - free_reglist(reg_list, reg_cnt); + out_toasm_x86(fout, sym_noat, pp); } - fprintf(fout, "# from asm\n\n"); + fprintf(fout, "# asm -> C\n\n"); while (fgets(line, sizeof(line), fsyms_from)) { @@ -478,20 +359,11 @@ int main(int argc, char *argv[]) if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#') continue; - ret = find_protostr(protostr, sizeof(protostr), fhdr, - sym, &pline); - if (ret != 0) { - printf("%s: sym '%s' is missing\n", - hdrfn, sym); - goto out; - } - - ret = parse_protostr(protostr, reg_list, ®_cnt, &is_stdcall); - if (ret) + pp = proto_parse(fhdr, sym, 0); + if (pp == NULL) goto out; - out_fromasm_x86(fout, sym, reg_list, reg_cnt, is_stdcall); - free_reglist(reg_list, reg_cnt); + out_fromasm_x86(fout, sym, pp); } ret = 0;