plugin: rm all assume statements
[ia32rtools.git] / plugin / saveasm.cpp
index 9054260..5f704ef 100644 (file)
@@ -43,6 +43,8 @@ static const char *reserved_names[] = {
   "offset",
   "aam",
   "text",
+  "size",
+  "c",
 };
 
 static int is_name_reserved(const char *name)
@@ -81,10 +83,11 @@ static int is_insn_jmp(uint16 itype)
   return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz);
 }
 
-static void do_def_line(char *buf, size_t buf_size, const char *line)
+static void do_def_line(char *buf, size_t buf_size, const char *line,
+  ea_t ea)
 {
-  char *endp = NULL;
-  ea_t ea, *ea_ret;
+  ea_t *ea_ret;
+  char *p;
   int len;
 
   tag_remove(line, buf, buf_size); // remove color codes
@@ -95,17 +98,17 @@ static void do_def_line(char *buf, size_t buf_size, const char *line)
   }
   memmove(buf, buf + 9, len - 9 + 1); // rm address
 
-  if (IS_START(buf, "loc_")) {
-    ea = strtoul(buf + 4, &endp, 16);
-    if (ea != 0 && *endp == ':') {
-      ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
-        sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
-      if (ea_ret != 0) {
-        if (endp[1] != ' ')
-          msg("no trailing blank in '%s'\n", buf);
-        else
-          endp[1] = ':';
-      }
+  p = buf;
+  while (*p && *p != ' ' && *p != ':')
+    p++;
+  if (*p == ':') {
+    ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
+      sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
+    if (ea_ret != 0) {
+      if (p[1] != ' ')
+        msg("no trailing blank in '%s'\n", buf);
+      else
+        p[1] = ':';
     }
   }
 }
@@ -332,9 +335,26 @@ static void idaapi run(int /*arg*/)
       continue;
     }
 
-    if (is_name_reserved(name)) {
+    // rename vars with '?@' (funcs are ok)
+    int change_qat = 0;
+    ea_flags = get_flags_novalue(ea);
+    if (!isCode(ea_flags) && strpbrk(name, "?@"))
+      change_qat = 1;
+
+    if (change_qat || is_name_reserved(name)) {
       msg("%x: renaming name '%s'\n", ea, name);
       qsnprintf(buf, sizeof(buf), "%s_g", name);
+
+      if (change_qat) {
+        for (p = buf; *p != 0; p++) {
+          if (*p == '?' || *p == '@') {
+            qsnprintf(buf2, sizeof(buf2), "%02x", (unsigned char)*p);
+            memmove(p + 1, p, strlen(p) + 1);
+            memcpy(p, buf2, 2);
+          }
+        }
+      }
+
       set_name(ea, buf);
     }
   }
@@ -365,14 +385,9 @@ static void idaapi run(int /*arg*/)
   ln.set_place(&pl);
   n = ln.get_linecnt();
   for (i = 0; i < n - 1; i++) {
-    do_def_line(buf, sizeof(buf), ln.down());
+    do_def_line(buf, sizeof(buf), ln.down(), ea);
     if (strstr(buf, "include"))
       continue;
-    p = strstr(buf, "assume cs");
-    if (p != NULL) {
-      memmove(p + 1, p, strlen(p) + 1);
-      *p = ';';
-    }
 
     fout_line++;
     qfprintf(fout, "%s\n", buf);
@@ -388,14 +403,15 @@ static void idaapi run(int /*arg*/)
       qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf));
       fout_line++;
       qfprintf(fout, "\n%s\n", buf);
-      continue;
+      i++;
+      break;
     }
   }
   pl.lnnum = i;
 
   for (;;)
   {
-    int drop_large = 0, drop_rva = 0, set_scale = 0, jmp_near = 0;
+    int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0;
     int word_imm = 0, dword_imm = 0, do_pushf = 0;
 
     if ((ea >> 14) != ui_ea_block) {
@@ -458,13 +474,13 @@ static void idaapi run(int /*arg*/)
     }
     else { // not code
       if (isOff0(ea_flags))
-        drop_rva = 1;
+        do_rva = 1;
     }
 
 pass:
     n = ln.get_linecnt();
     for (i = pl.lnnum; i < n; i++) {
-      do_def_line(buf, sizeof(buf), ln.down());
+      do_def_line(buf, sizeof(buf), ln.down(), ea);
 
       char *fw;
       for (fw = buf; *fw != 0 && *fw == ' '; )
@@ -476,11 +492,12 @@ pass:
         if (p != NULL)
           memmove(p, p + 6, strlen(p + 6) + 1);
       }
-      while (drop_rva) {
+      while (do_rva) {
         p = strstr(fw, " rva ");
         if (p == NULL)
           break;
-        memmove(p, p + 4, strlen(p + 4) + 1);
+        memmove(p + 4 + 3, p + 4, strlen(p + 4) + 1);
+        memcpy(p + 1, "offset", 6);
       }
       if (set_scale) {
         p = strchr(fw, '[');
@@ -532,7 +549,12 @@ pass:
         }
       }
 
-      if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') {
+      if (fw[0] == 'a' && IS_START(fw, "assume cs")) {
+        // "assume cs" causes problems with ext syms
+        memmove(fw + 1, fw, strlen(fw) + 1);
+        *fw = ';';
+      }
+      else if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') {
         fout_line++;
         qfprintf(fout, "include public.inc\n\n");