translate: detect funcs which get their address taken
[ia32rtools.git] / tools / translate.c
index eacb0e0..fb77c7a 100644 (file)
@@ -96,6 +96,8 @@ enum op_op {
        OP_MOVS,
        OP_CMPS,
        OP_SCAS,
+       OP_RDTSC,
+       OP_CPUID,
        OP_STD,
        OP_CLD,
        OP_RET,
@@ -282,8 +284,9 @@ enum ida_func_attr {
 enum sct_func_attr {
   SCTFA_CLEAR_SF   = (1 << 0), // clear stack frame
   SCTFA_CLEAR_REGS = (1 << 1), // clear registers (mask)
-  SCTFA_RM_REGS    = (1 << 2), // don't emit regs
+  SCTFA_RM_REGS    = (1 << 2), // don't emit regs (mask)
   SCTFA_NOWARN     = (1 << 3), // don't try to detect problems
+  SCTFA_ARGFRAME   = (1 << 4), // copy all args to a struct, in order
 };
 
 enum x87_const {
@@ -335,6 +338,7 @@ static int g_regmask_rm;
 static int g_skip_func;
 static int g_allow_regfunc;
 static int g_allow_user_icall;
+static int g_nowarn_reguse;
 static int g_quiet_pp;
 static int g_header_mode;
 
@@ -376,6 +380,7 @@ enum x86_regs {
 };
 
 #define mxAX     (1 << xAX)
+#define mxBX     (1 << xBX)
 #define mxCX     (1 << xCX)
 #define mxDX     (1 << xDX)
 #define mxSP     (1 << xSP)
@@ -954,7 +959,7 @@ static const struct {
   { "repz",   OPF_REP|OPF_REPZ },
   { "repne",  OPF_REP|OPF_REPNZ },
   { "repnz",  OPF_REP|OPF_REPNZ },
-  { "lock",   OPF_LOCK }, // ignored for now..
+  { "lock",   OPF_LOCK },
 };
 
 #define OPF_CJMP_CC (OPF_JMP|OPF_CJMP|OPF_CC)
@@ -998,6 +1003,8 @@ static const struct {
   { "scasb",OP_SCAS,   0, 0, OPF_DATA|OPF_FLAGS },
   { "scasw",OP_SCAS,   0, 0, OPF_DATA|OPF_FLAGS },
   { "scasd",OP_SCAS,   0, 0, OPF_DATA|OPF_FLAGS },
+  { "rdtsc",OP_RDTSC,  0, 0, OPF_DATA },
+  { "cpuid",OP_CPUID,  0, 0, OPF_DATA },
   { "std",  OP_STD,    0, 0, OPF_DATA }, // special flag
   { "cld",  OP_CLD,    0, 0, OPF_DATA },
   { "add",  OP_ADD,    2, 2, OPF_DATA|OPF_FLAGS },
@@ -1325,6 +1332,16 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
     op->regmask_dst = op->regmask_src;
     break;
 
+  case OP_RDTSC:
+    op->regmask_dst = mxAX | mxDX;
+    break;
+
+  case OP_CPUID:
+    // for now, ignore ecx dep for eax={4,7,b,d}
+    op->regmask_src = mxAX;
+    op->regmask_dst = mxAX | mxBX | mxCX | mxDX;
+    break;
+
   case OP_LOOP:
     op->regmask_dst = 1 << xCX;
     // fallthrough
@@ -1926,6 +1943,7 @@ static int stack_frame_access(struct parsed_op *po,
   const char *prefix = "";
   const char *bp_arg = NULL;
   char ofs_reg[16] = { 0, };
+  char argname[8];
   int i, arg_i, arg_s;
   int unaligned = 0;
   int stack_ra = 0;
@@ -1975,17 +1993,20 @@ static int stack_frame_access(struct parsed_op *po,
     popr->is_ptr = g_func_pp->arg[i].type.is_ptr;
     retval = i;
 
+    snprintf(argname, sizeof(argname), "%sa%d",
+      g_sct_func_attr & SCTFA_ARGFRAME ? "af." : "", i + 1);
+
     switch (popr->lmod)
     {
     case OPLM_BYTE:
       if (is_lea)
         ferr(po, "lea/byte to arg?\n");
       if (is_src && (offset & 3) == 0)
-        snprintf(buf, buf_size, "%sa%d",
-          simplify_cast(cast, "(u8)"), i + 1);
+        snprintf(buf, buf_size, "%s%s",
+          simplify_cast(cast, "(u8)"), argname);
       else
-        snprintf(buf, buf_size, "%sBYTE%d(a%d)",
-          cast, offset & 3, i + 1);
+        snprintf(buf, buf_size, "%sBYTE%d(%s)",
+          cast, offset & 3, argname);
       break;
 
     case OPLM_WORD:
@@ -1996,18 +2017,18 @@ static int stack_frame_access(struct parsed_op *po,
         if (!is_src) {
           if (offset & 2)
             ferr(po, "problematic arg store\n");
-          snprintf(buf, buf_size, "%s((char *)&a%d + 1)",
-            simplify_cast(cast, "*(u16 *)"), i + 1);
+          snprintf(buf, buf_size, "%s((char *)&%s + 1)",
+            simplify_cast(cast, "*(u16 *)"), argname);
         }
         else
           ferr(po, "unaligned arg word load\n");
       }
       else if (is_src && (offset & 2) == 0)
-        snprintf(buf, buf_size, "%sa%d",
-          simplify_cast(cast, "(u16)"), i + 1);
+        snprintf(buf, buf_size, "%s%s",
+          simplify_cast(cast, "(u16)"), argname);
       else
-        snprintf(buf, buf_size, "%s%sWORD(a%d)",
-          cast, (offset & 2) ? "HI" : "LO", i + 1);
+        snprintf(buf, buf_size, "%s%sWORD(%s)",
+          cast, (offset & 2) ? "HI" : "LO", argname);
       break;
 
     case OPLM_DWORD:
@@ -2019,19 +2040,19 @@ static int stack_frame_access(struct parsed_op *po,
       if (offset & 3) {
         unaligned = 1;
         if (is_lea)
-          snprintf(buf, buf_size, "(u32)&a%d + %d",
-            i + 1, offset & 3);
+          snprintf(buf, buf_size, "(u32)&%s + %d",
+            argname, offset & 3);
         else if (!is_src)
           ferr(po, "unaligned arg store\n");
         else {
           // mov edx, [ebp+arg_4+2]; movsx ecx, dx
-          snprintf(buf, buf_size, "%s(a%d >> %d)",
-            prefix, i + 1, (offset & 3) * 8);
+          snprintf(buf, buf_size, "%s(%s >> %d)",
+            prefix, argname, (offset & 3) * 8);
         }
       }
       else {
-        snprintf(buf, buf_size, "%s%sa%d",
-          prefix, is_lea ? "&" : "", i + 1);
+        snprintf(buf, buf_size, "%s%s%s",
+          prefix, is_lea ? "&" : "", argname);
       }
       break;
 
@@ -2039,8 +2060,8 @@ static int stack_frame_access(struct parsed_op *po,
       ferr_assert(po, !(offset & 7));
       if (cast[0])
         prefix = cast;
-      snprintf(buf, buf_size, "%s%sa%d",
-        prefix, is_lea ? "&" : "", i + 1);
+      snprintf(buf, buf_size, "%s%s%s",
+        prefix, is_lea ? "&" : "", argname);
       break;
 
     default:
@@ -3525,6 +3546,11 @@ static const struct parsed_proto *try_recover_pp(
   char buf[256];
   char *p;
 
+  if (po->pp != NULL && (po->flags & OPF_DATA)) {
+    // hint given in asm
+    return po->pp;
+  }
+
   // maybe an arg of g_func?
   if (opr->type == OPT_REGMEM && is_stack_access(po, opr))
   {
@@ -3917,8 +3943,10 @@ static void resolve_branches_parse_calls(int opcnt)
           && IS(po->operand[0].name, g_labels[l]))
       {
         if (l == i + 1 && po->op == OP_JMP) {
-          // yet another alignment type..
-          po->flags |= OPF_RMD|OPF_DONE;
+          // yet another alignment type...
+          po->flags |= OPF_RMD | OPF_DONE;
+          po->flags &= ~OPF_JMP;
+          po->op = OP_NOP;
           break;
         }
         add_label_ref(&g_label_refs[l], i);
@@ -5999,6 +6027,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
   int need_double = 0;
   int stack_align = 0;
   int stack_fsz_adj = 0;
+  int lock_handled = 0;
   int regmask_save = 0; // used regs saved/restored in this func
   int regmask_arg;      // regs from this function args (fastcall, etc)
   int regmask_ret;      // regs needed on ret
@@ -6325,7 +6354,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           pp->is_stdcall = 1;
       }
       if (!(po->flags & OPF_TAIL)
-          && !(g_sct_func_attr & SCTFA_NOWARN))
+          && !(g_sct_func_attr & SCTFA_NOWARN) && !g_nowarn_reguse)
       {
         // treat al write as overwrite to avoid many false positives
         if (IS(pp->ret_type.name, "void") || pp->ret_type.is_float) {
@@ -6417,6 +6446,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         need_double = 1;
       break;
 
+    case OP_RDTSC:
     case OPP_ALLSHL:
     case OPP_ALLSHR:
       need_tmp64 = 1;
@@ -6555,6 +6585,28 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     had_decl = 1;
   }
 
+  if ((g_sct_func_attr & SCTFA_ARGFRAME) && g_func_pp->argc_stack) {
+    fprintf(fout, "  struct { u32 ");
+    for (i = j = 0; i < g_func_pp->argc; i++) {
+      if (g_func_pp->arg[i].reg != NULL)
+        continue;
+      if (j++ != 0)
+        fprintf(fout, ", ");
+      fprintf(fout, "a%d", i + 1);
+    }
+    fprintf(fout, "; } af = {\n    ");
+    for (i = j = 0; i < g_func_pp->argc; i++) {
+      if (g_func_pp->arg[i].reg != NULL)
+        continue;
+      if (j++ != 0)
+        fprintf(fout, ", ");
+        if (g_func_pp->arg[i].type.is_ptr)
+          fprintf(fout, "(u32)");
+      fprintf(fout, "a%d", i + 1);
+    }
+    fprintf(fout, "\n  };\n");
+  }
+
   if (g_func_pp->is_userstack) {
     fprintf(fout, "  u32 fake_sf[US_SZ_%s / 4];\n", g_func_pp->name);
     fprintf(fout, "  u32 *esp = &fake_sf[sizeof(fake_sf) / 4];\n");
@@ -6742,6 +6794,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     if (po->flags & OPF_RMD)
       continue;
 
+    lock_handled = 0;
     no_output = 0;
 
     #define assert_operand_cnt(n_) \
@@ -6919,9 +6972,10 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           fprintf(fout, "  for (; ecx != 0; ecx--, edi %c= %d)\n",
             (po->flags & OPF_DF) ? '-' : '+',
             lmod_bytes(po, po->operand[1].lmod));
-          fprintf(fout, "    %sedi = eax;",
+          fprintf(fout, "    %sedi = eax;\n",
             lmod_cast_u_ptr(po, po->operand[1].lmod));
-          strcpy(g_comment, "rep stos");
+          fprintf(fout, "  barrier();");
+          strcpy(g_comment, "^ rep stos");
         }
         else {
           assert_operand_cnt(2);
@@ -6943,8 +6997,10 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             "  for (; ecx != 0; ecx--, edi %c= %d, esi %c= %d)\n",
             l, j, l, j);
           fprintf(fout,
-            "    %sedi = %sesi;", buf1, buf1);
-          strcpy(g_comment, "rep movs");
+            "    %sedi = %sesi;\n", buf1, buf1);
+          // this can overwrite many variables
+          fprintf(fout, "  barrier();");
+          strcpy(g_comment, "^ rep movs");
         }
         else {
           assert_operand_cnt(2);
@@ -7027,6 +7083,16 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         delayed_flag_op = NULL;
         break;
 
+      case OP_RDTSC:
+        fprintf(fout, "  tmp64 = ext_rdtsc();\n");
+        fprintf(fout, "  edx = tmp64 >> 32;\n");
+        fprintf(fout, "  eax = tmp64;");
+        break;
+
+      case OP_CPUID:
+        fprintf(fout, "  ext_cpuid(&eax, &ebx, &ecx, &edx);");
+        break;
+
       // arithmetic w/flags
       case OP_AND:
         if (po->operand[1].type == OPT_CONST && !po->operand[1].val)
@@ -7330,9 +7396,18 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
 
         out_dst_opr(buf1, sizeof(buf1), po, &po->operand[0]);
         if (po->operand[0].type == OPT_REG) {
+          ferr_assert(po, !(po->flags & OPF_LOCK));
           strcpy(buf2, po->op == OP_INC ? "++" : "--");
           fprintf(fout, "  %s%s;", buf1, buf2);
         }
+        else if (po->flags & OPF_LOCK) {
+          out_src_opr(buf2, sizeof(buf2), po, &po->operand[0], "", 1);
+          fprintf(fout, "  __sync_fetch_and_%s((%s *)(%s), 1);",
+            po->op == OP_INC ? "add" : "sub",
+            lmod_type_u(po, po->operand[0].lmod), buf2);
+          strcat(g_comment, " lock");
+          lock_handled = 1;
+        }
         else {
           strcpy(buf2, po->op == OP_INC ? "+" : "-");
           fprintf(fout, "  %s %s= 1;", buf1, buf2);
@@ -8192,6 +8267,9 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
     if (pfomask != 0)
       ferr(po, "missed flag calc, pfomask=%x\n", pfomask);
 
+    if ((po->flags & OPF_LOCK) && !lock_handled)
+      ferr(po, "unhandled lock\n");
+
     // see is delayed flag stuff is still valid
     if (delayed_flag_op != NULL && delayed_flag_op != po) {
       if (is_any_opr_modified(delayed_flag_op, po, 0))
@@ -8252,6 +8330,7 @@ struct func_prototype {
   unsigned int dep_resolved:1;
   unsigned int is_stdcall:1;
   unsigned int eax_pass:1;       // returns without touching eax
+  unsigned int ptr_taken:1;      // pointer taken of this func
   struct func_proto_dep *dep_func;
   int dep_func_cnt;
   const struct parsed_proto *pp; // seed pp, if any
@@ -8264,6 +8343,7 @@ struct func_proto_dep {
   unsigned int ret_dep:1;       // return from this is caller's return
   unsigned int has_ret:1;       // found from eax use after return
   unsigned int has_ret64:1;
+  unsigned int ptr_taken:1;     // pointer taken, not a call
 };
 
 static struct func_prototype *hg_fp;
@@ -8315,10 +8395,14 @@ static struct func_proto_dep *hg_fp_find_dep(struct func_prototype *fp,
   return NULL;
 }
 
-static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
+static void hg_fp_add_dep(struct func_prototype *fp, const char *name,
+  unsigned int ptr_taken)
 {
+  struct func_proto_dep * dep;
+
   // is it a dupe?
-  if (hg_fp_find_dep(fp, name))
+  dep = hg_fp_find_dep(fp, name);
+  if (dep != NULL && dep->ptr_taken == ptr_taken)
     return;
 
   if ((fp->dep_func_cnt & 0xff) == 0) {
@@ -8329,6 +8413,7 @@ static void hg_fp_add_dep(struct func_prototype *fp, const char *name)
       sizeof(fp->dep_func[0]) * 0x100);
   }
   fp->dep_func[fp->dep_func_cnt].name = strdup(name);
+  fp->dep_func[fp->dep_func_cnt].ptr_taken = ptr_taken;
   fp->dep_func_cnt++;
 }
 
@@ -8525,6 +8610,7 @@ static void gen_hdr(const char *funcn, int opcnt)
   struct func_prototype *fp;
   struct func_proto_dep *dep;
   struct parsed_op *po;
+  const char *tmpname;
   int regmask_dummy = 0;
   int regmask_dep;
   int regmask_use;
@@ -8556,6 +8642,7 @@ static void gen_hdr(const char *funcn, int opcnt)
   // pass3:
   // - remove dead labels
   // - collect calls
+  // - collect function ptr refs
   for (i = 0; i < opcnt; i++)
   {
     if (g_labels[i] != NULL && g_label_refs[i].i == -1) {
@@ -8569,9 +8656,19 @@ static void gen_hdr(const char *funcn, int opcnt)
 
     if (po->op == OP_CALL) {
       if (po->operand[0].type == OPT_LABEL)
-        hg_fp_add_dep(fp, opr_name(po, 0));
+        hg_fp_add_dep(fp, opr_name(po, 0), 0);
       else if (po->pp != NULL)
-        hg_fp_add_dep(fp, po->pp->name);
+        hg_fp_add_dep(fp, po->pp->name, 0);
+    }
+    else if (po->op == OP_MOV && po->operand[1].type == OPT_OFFSET) {
+      tmpname = opr_name(po, 1);
+      if (IS_START(tmpname, "p_") || IS_START(tmpname, "sub_"))
+        hg_fp_add_dep(fp, tmpname, 1);
+    }
+    else if (po->op == OP_PUSH && po->operand[0].type == OPT_OFFSET) {
+      tmpname = opr_name(po, 0);
+      if (IS_START(tmpname, "p_") || IS_START(tmpname, "sub_"))
+        hg_fp_add_dep(fp, tmpname, 1);
     }
   }
 
@@ -8737,6 +8834,11 @@ static void hg_fp_resolve_deps(struct func_prototype *fp)
     dep->proto = bsearch(&fp_s, hg_fp, hg_fp_cnt,
       sizeof(hg_fp[0]), hg_fp_cmp_name);
     if (dep->proto != NULL) {
+      if (dep->ptr_taken) {
+        dep->proto->ptr_taken = 1;
+        continue;
+      }
+
       if (!dep->proto->dep_resolved)
         hg_fp_resolve_deps(dep->proto);
 
@@ -8766,11 +8868,8 @@ static void do_func_refs_from_data(void)
     strcpy(fp_s.name, hg_refs[i]);
     fp = bsearch(&fp_s, hg_fp, hg_fp_cnt,
       sizeof(hg_fp[0]), hg_fp_cmp_name);
-    if (fp == NULL)
-      continue;
-
-    if (fp->argc_stack != 0 && (fp->regmask_dep & (mxCX | mxDX)))
-      fp->regmask_dep |= mxCX | mxDX;
+    if (fp != NULL)
+      fp->ptr_taken = 1;
   }
 }
 
@@ -8820,6 +8919,12 @@ static void output_hdr_fp(FILE *fout, const struct func_prototype *fp,
 
     regmask_dep = fp->regmask_dep;
     argc_normal = fp->argc_stack;
+    if (fp->ptr_taken && regmask_dep
+        && (regmask_dep & ~(mxCX|mxDX)) == 0)
+    {
+      if ((regmask_dep & mxDX) || fp->argc_stack > 0)
+        regmask_dep |= mxCX | mxDX;
+    }
 
     fprintf(fout, "%-5s",
       fp->pp ? fp->pp->ret_type.name :
@@ -9364,6 +9469,8 @@ int main(int argc, char *argv[])
       g_allow_regfunc = 1;
     else if (IS(argv[arg], "-uc"))
       g_allow_user_icall = 1;
+    else if (IS(argv[arg], "-wu"))
+      g_nowarn_reguse = 1;
     else if (IS(argv[arg], "-m"))
       multi_seg = 1;
     else if (IS(argv[arg], "-hdr"))
@@ -9373,13 +9480,14 @@ int main(int argc, char *argv[])
   }
 
   if (argc < arg + 3) {
-    printf("usage:\n%s [-v] [-rf] [-m] <.c> <.asm> <hdr.h> [rlist]*\n"
+    printf("usage:\n%s [options] <.c> <.asm> <hdr.h> [rlist]*\n"
            "%s -hdr <out.h> <.asm> <seed.h> [rlist]*\n"
            "options:\n"
            "  -hdr - header generation mode\n"
            "  -rf  - allow unannotated indirect calls\n"
            "  -uc  - allow ind. calls/refs to __usercall\n"
            "  -m   - allow multiple .text sections\n"
+           "  -wu  - don't warn about bad reg use\n"
            "[rlist] is a file with function names to skip,"
            " one per line\n",
       argv[0], argv[0]);
@@ -9524,6 +9632,7 @@ int main(int argc, char *argv[])
           "clear_regmask",
           "rm_regmask",
           "nowarn",
+          "argframe",
         };
 
         // parse manual attribute-list comment
@@ -9647,16 +9756,18 @@ parse_words:
           continue;
         goto parse_words; // lame
       }
-      if (IS_START(p, "; sctproto:")) {
-        sctproto = strdup(p + 11);
-      }
       else if (IS_START(p, "; sctend")) {
         end = 1;
         if (!pending_endp)
           break;
       }
+      else if (g_skip_func)
+        /* ignore remaining attrs */;
+      else if (IS_START(p, "; sctproto:")) {
+        sctproto = strdup(p + 11);
+      }
       else if (IS_START(p, "; sctskip_start")) {
-        if (in_func && !g_skip_func) {
+        if (in_func) {
           if (!skip_code) {
             ops[pi].op = OPP_ABORT;
             ops[pi].asmln = asmln;