plugin: more aggressive name change to avoid conflicts
[ia32rtools.git] / tools / mkbridge.c
index e3b0068..29bdd9a 100644 (file)
+/*
+ * 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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "my_assert.h"
 #include "my_str.h"
+#include "common.h"
+
+#include "protoparse.h"
+
+static const char *c_save_regs[] = { "ebx", "esi", "edi", "ebp" };
 
-static int find_protostr(char *dst, size_t dlen, FILE *fhdr,
-       const char *sym, int *pline)
+static int is_x86_reg_saved(const char *reg)
 {
-       int line = 0;
-       char *p;
+       static const char *nosave_regs[] = { "eax", "edx", "ecx" };
+       int nosave = 0;
+       int r;
 
-       while (fgets(dst, dlen, fhdr))
-       {
-               line++;
-               if (strstr(dst, sym) != NULL)
-                       break;
-       }
-       *pline = line;
+       for (r = 0; r < ARRAY_SIZE(nosave_regs); r++)
+               if (strcmp(reg, nosave_regs[r]) == 0)
+                       nosave = 1;
 
-       if (feof(fhdr))
-               return -1;
+       return !nosave;
+}
+
+// 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;
+       }
 
-       p = dst + strlen(dst);
-       for (p--; p > dst && my_isblank(*p); --p)
-               *p = 0;
+       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 0;
+       return buf;
 }
 
-static int get_regparm(char *dst, size_t dlen, char *p)
+static void out_toasm_x86(FILE *f, const char *sym_out,
+       const struct parsed_proto *pp)
 {
-       int i, o;
+       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;
+
+       argc_repush = pp->argc;
+       if (pp->is_vararg)
+               argc_repush = ARRAY_SIZE(pp->arg); // hopefully enough?
+
+       for (i = 0; i < pp->argc; i++) {
+               if (pp->arg[i].reg != NULL)
+                       must_save |= is_x86_reg_saved(pp->arg[i].reg);
+       }
+
+       name = pp_to_name(pp);
+       fprintf(f, ".global %s\n", name);
+       fprintf(f, "%s:\n", name);
+
+       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 (pp->argc_stack == 0 && !must_save && !pp->is_stdcall
+            && !pp->is_vararg && !pp->has_retreg)
+       {
+               // load arg regs
+               for (i = 0; i < pp->argc; i++) {
+                       fprintf(f, "\tmovl %d(%%esp), %%%s\n",
+                               (i + sarg_ofs) * 4, pp->arg[i].reg);
+               }
+               fprintf(f, "\tjmp %s\n\n", sym_out);
+               return;
+       }
+
+       // asm_stack_args | saved_regs | ra | args_from_c
+
+       // save the regs
+       // 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 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");
+                       sarg_ofs++;
+                       args_repushed++;
+               }
+       }
 
-       if (*p != '<')
-               return 0;
+       // load arg regs
+       for (i = 0; i < pp->argc; i++) {
+               if (pp->arg[i].reg != NULL) {
+                       fprintf(f, "\tmovl %d(%%esp), %%%s\n",
+                               (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);
+               }
+       }
 
-       for (o = 0, i = 1; o < dlen; i++) {
-               if (p[i] == 0)
-                       return 0;
-               if (p[i] == '>')
-                       break;
-               dst[o++] = p[i];
+       fprintf(f, "\n\t# %s\n", pp->is_stdcall ? "__stdcall" : "__cdecl");
+       fprintf(f, "\tcall %s\n\n", sym_out);
+
+       if (args_repushed && !pp->is_stdcall) {
+               fprintf(f, "\tadd $%d,%%esp\n", args_repushed * 4);
+               sarg_ofs -= args_repushed;
+       }
+
+       // 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);
+                       }
+               }
        }
-       dst[o] = 0;
-       return i + 1;
+
+       // 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 const char *known_types[] = {
-       "unsigned int",
-       "signed int",
-       "int",
-       "void",
-       "DWORD",
-       "HMODULE",
-       "HANDLE",
-       "HWND",
-};
-
-static int check_type(const char *name)
+static void out_fromasm_x86(FILE *f, const char *sym,
+       const struct parsed_proto *pp)
 {
-       int i, l;
+       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;
+
+       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;
+       }
 
-       for (i = 0; i < sizeof(known_types) / sizeof(known_types[0]); i++) {
-               l = strlen(known_types[i]);
-               if (strncmp(known_types[i], name, l) == 0)
-                       return l;
+       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);
 
-       return 0;
+       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;
+       }
+
+       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
+       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 %%ecx\n");
+                       stack_args--;
+               }
+               else {
+                       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++;
+       }
+
+       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);
+               }
+       }
+
+       if (!ret64)
+               fprintf(f, "\tpopl %%edx\n");
+       fprintf(f, "\tpopl %%ecx\n");
+
+       if (pp->is_stdcall && pp->argc_stack)
+               fprintf(f, "\tret $%d\n\n", pp->argc_stack * 4);
+       else
+               fprintf(f, "\tret\n\n");
 }
 
 int main(int argc, char *argv[])
 {
-       FILE *fout, *fsyms, *fhdr;
-       const char *hdrfn;
-       char protostr[256];
+       FILE *fout, *fsyms_to, *fsyms_from, *fhdr;
+       const struct parsed_proto *pp;
        char line[256];
+       char sym_noat[256];
        char sym[256];
-       char buf[256];
-       char regparm[16];
        char *p;
-       int first_regparm = 0;
-       int have_regparm;
-       int pline = 0;
-       int xarg;
-       int ret;
-
-       if (argc != 4) {
-               // -c - patch callsites
-               printf("usage:\n%s <bridge.s> <symf> <hdrf>\n",
+       int ret = 1;
+
+       if (argc != 5) {
+               printf("usage:\n%s <bridge.s> <toasm_symf> <fromasm_symf> <hdrf>\n",
                        argv[0]);
                return 1;
        }
 
-       hdrfn = argv[3];
+       hdrfn = argv[4];
        fhdr = fopen(hdrfn, "r");
        my_assert_not(fhdr, NULL);
 
-       fsyms = fopen(argv[2], "r");
-       my_assert_not(fsyms, NULL);
+       fsyms_from = fopen(argv[3], "r");
+       my_assert_not(fsyms_from, NULL);
+
+       fsyms_to = fopen(argv[2], "r");
+       my_assert_not(fsyms_to, NULL);
 
        fout = fopen(argv[1], "w");
        my_assert_not(fout, NULL);
 
        fprintf(fout, ".text\n\n");
+       fprintf(fout, "# C -> asm\n\n");
 
-       while (fgets(line, sizeof(line), fsyms))
+       while (fgets(line, sizeof(line), fsyms_to))
        {
                next_word(sym, sizeof(sym), line);
                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);
-                       return 1;
-               }
-
-               p = protostr;
-               if (p[0] == '/' && p[1] == '/') {
-                       printf("warning: decl for sym '%s' is commented out\n", sym);
-                       p = sskip(p + 2);
-               }
+               // IDA asm doesn't do '@' notation..
+               strcpy(sym_noat, sym);
+               p = strchr(sym_noat, '@');
+               if (p != NULL)
+                       *p = 0;
 
-               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);
-
-               // ignore calling convention specifier, for now
-               p = next_word(buf, sizeof(buf), p);
-               p = sskip(p);
-               if (buf[0] == 0) {
-                       printf("%s:%d:%ld: cconv missing\n",
-                               hdrfn, pline, (p - protostr) + 1);
-                       return 1;
-               }
+               pp = proto_parse(fhdr, sym_noat, 0);
+               if (pp == NULL)
+                       goto out;
 
-               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;
-               }
+               out_toasm_x86(fout, sym_noat, pp);
+       }
 
-               ret = get_regparm(regparm, sizeof(regparm), p);
-               if (ret > 0) {
-                       if (strcmp(regparm, "eax") && strcmp(regparm, "ax")) {
-                               printf("%s:%d:%ld: bad regparm: %s\n",
-                                       hdrfn, pline, (p - protostr) + 1, regparm);
-                               return 1;
-                       }
-                       p += ret;
-                       p = sskip(p);
-               }
+       fprintf(fout, "# asm -> C\n\n");
 
-               if (*p != '(') {
-                       printf("%s:%d:%ld: '(' expected, got '%c'\n",
-                               hdrfn, pline, (p - protostr) + 1, *p);
-                       return 1;
-               }
-               p++;
-
-               fprintf(fout, ".global _asm_%s\n", sym);
-               fprintf(fout, "_asm_%s:\n", sym);
-
-               xarg = 1;
-               while (1) {
-                       p = sskip(p);
-                       if (*p == ')')
-                               break;
-                       if (*p == ',')
-                               p = sskip(p + 1);
-
-                       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 (buf[0] == 0) {
-                               printf("%s:%d:%ld: idt missing for arg%d\n",
-                                       hdrfn, pline, (p - protostr) + 1, xarg);
-                               return 1;
-                       }
-
-                       have_regparm = 0;
-                       ret = get_regparm(regparm, sizeof(regparm), p);
-                       if (ret > 0) {
-                               p += ret;
-                               p = sskip(p);
+       while (fgets(line, sizeof(line), fsyms_from))
+       {
+               next_word(sym, sizeof(sym), line);
+               if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
+                       continue;
 
-                               have_regparm = 1;
-                               fprintf(fout, "\t movl %d(%%esp), %%%s\n",
-                                       xarg * 4, regparm);
-                       }
-                       if (xarg == 1)
-                               first_regparm = have_regparm;
-                       else if (have_regparm != first_regparm) {
-                               printf("%s:%d:%ld: mixed regparm is unhandled\n",
-                                       hdrfn, pline, (p - protostr) + 1);
-                               return 1;
-                       }
-               }
+               pp = proto_parse(fhdr, sym, 0);
+               if (pp == NULL)
+                       goto out;
 
-               fprintf(fout, "\t jmp %s\n\n", sym);
+               out_fromasm_x86(fout, sym, pp);
        }
 
+       ret = 0;
+out:
        fclose(fout);
-       return 0;
+       fclose(fsyms_to);
+       fclose(fsyms_from);
+       fclose(fhdr);
+       if (ret)
+               remove(argv[1]);
+
+       return ret;
 }