X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=plugin%2Fsaveasm.cpp;h=8e1bc953864afa5615a84fa3e4d757d62ec72590;hb=afdd4566574318873cf85edd41fb3c7d80f98b28;hp=efe8e9d8b26d8c22677b68a0715a651b1d4c775d;hpb=d53d4cc79a05e833e791effdd14e5209222f483f;p=ia32rtools.git diff --git a/plugin/saveasm.cpp b/plugin/saveasm.cpp index efe8e9d..8e1bc95 100644 --- a/plugin/saveasm.cpp +++ b/plugin/saveasm.cpp @@ -1,3 +1,11 @@ +/* + * 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. + */ + #define NO_OBSOLETE_FUNCS #include #include @@ -8,6 +16,7 @@ #include #include #include +#include #include #include @@ -43,6 +52,8 @@ static const char *reserved_names[] = { "offset", "aam", "text", + "size", + "c", }; static int is_name_reserved(const char *name) @@ -237,7 +248,10 @@ static void idaapi run(int /*arg*/) if (cmd.Operands[o].type == o_mem) { tmp_ea = cmd.Operands[o].addr; flags_t tmp_ea_flags = get_flags_novalue(tmp_ea); - if (!isUnknown(tmp_ea_flags)) { + // ..but base float is ok.. + int is_flt = isDwrd(tmp_ea_flags) || isFloat(tmp_ea_flags); + if (!is_flt && !isUnknown(tmp_ea_flags)) + { buf[0] = 0; get_name(ea, tmp_ea, buf, sizeof(buf)); msg("%x: undefining %x '%s'\n", ea, tmp_ea, buf); @@ -246,8 +260,8 @@ static void idaapi run(int /*arg*/) } } } - // detect code alignment else if (cmd.itype == NN_lea) { + // detect code alignment if (cmd.Operands[0].reg == cmd.Operands[1].reg && cmd.Operands[1].type == o_displ && cmd.Operands[1].addr == 0) @@ -262,6 +276,24 @@ static void idaapi run(int /*arg*/) doAlign(ea, tmp_ea - ea, n); } } + else if (!isDefArg1(ea_flags) + && cmd.Operands[1].type == o_mem // why o_mem? + && cmd.Operands[1].dtyp == dt_dword) + { + if (inf.minEA <= cmd.Operands[1].addr + && cmd.Operands[1].addr < inf.maxEA) + { + // lea to segments, like ds:58D6A8h[edx*8] + msg("%x: lea offset to %x\n", ea, cmd.Operands[1].addr); + op_offset(ea, 1, REF_OFF32); + } + else + { + // ds:0[eax*8] -> [eax*8+0] + msg("%x: dropping ds: for %x\n", ea, cmd.Operands[1].addr); + op_hex(ea, 1); + } + } } // find non-local branches @@ -333,9 +365,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); } } @@ -369,11 +418,6 @@ static void idaapi run(int /*arg*/) 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); @@ -389,15 +433,16 @@ 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 word_imm = 0, dword_imm = 0, do_pushf = 0; + int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0; + int word_imm = 0, dword_imm = 0, do_pushf = 0, do_nops = 0; if ((ea >> 14) != ui_ea_block) { ui_ea_block = ea >> 14; @@ -455,11 +500,17 @@ static void idaapi run(int /*arg*/) if (get_word(ea + opr.offb) == (ushort)opr.value) word_imm = 1; } + else if (opr.type == o_displ && opr.addr == 0 + && opr.offb != 0 && opr.hasSIB && opr.sib == 0x24) + { + // uses [esp+0] with 0 encoded into op + do_nops++; + } } } else { // not code if (isOff0(ea_flags)) - drop_rva = 1; + do_rva = 1; } pass: @@ -477,11 +528,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, '['); @@ -496,7 +548,9 @@ pass: } } else if (jmp_near) { - p = strchr(fw, 'j'); + p = NULL; + if (fw != buf && fw[0] == 'j') + p = fw; while (p && *p != ' ') p++; while (p && *p == ' ') @@ -504,6 +558,7 @@ pass: if (p != NULL) { memmove(p + 9, p, strlen(p) + 1); memcpy(p, "near ptr ", 9); + jmp_near = 0; } } if (word_imm) { @@ -533,7 +588,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"); @@ -545,6 +605,9 @@ pass: qfprintf(fout, "%s\n", buf); } + while (do_nops-- > 0) + qfprintf(fout, " nop ; adj\n"); + // note: next_head skips some undefined stuff ea = next_not_tail(ea); // correct? if (ea == BADADDR)