5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
9 #define NO_OBSOLETE_FUNCS
14 #include <kernwin.hpp>
23 #define IS_START(w, y) !strncmp(w, y, strlen(y))
24 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
26 static char **name_cache;
27 static size_t name_cache_size;
29 // non-local branch targets
30 static ea_t *nonlocal_bt;
31 static int nonlocal_bt_alloc;
32 static int nonlocal_bt_cnt;
34 //--------------------------------------------------------------------------
35 static int idaapi init(void)
40 //--------------------------------------------------------------------------
41 static void idaapi term(void)
45 if (nonlocal_bt != NULL) {
49 nonlocal_bt_alloc = 0;
51 if (name_cache != NULL) {
52 for (i = 0; i < name_cache_size; i++)
60 //--------------------------------------------------------------------------
62 static const char *reserved_names[] = {
73 static int is_name_reserved(const char *name)
76 for (i = 0; i < ARRAY_SIZE(reserved_names); i++)
77 if (strcasecmp(name, reserved_names[i]) == 0)
83 static int nonlocal_bt_cmp(const void *p1, const void *p2)
85 const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2;
89 static void nonlocal_add(ea_t ea)
91 if (nonlocal_bt_cnt >= nonlocal_bt_alloc) {
92 nonlocal_bt_alloc += nonlocal_bt_alloc * 2 + 64;
93 nonlocal_bt = (ea_t *)realloc(nonlocal_bt,
94 nonlocal_bt_alloc * sizeof(nonlocal_bt[0]));
95 if (nonlocal_bt == NULL) {
100 nonlocal_bt[nonlocal_bt_cnt++] = ea;
103 // is instruction a (un)conditional jump (not call)?
104 static int is_insn_jmp(uint16 itype)
106 return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz);
109 static void do_def_line(char *buf, size_t buf_size, const char *line,
116 tag_remove(line, buf, buf_size); // remove color codes
122 memmove(buf, buf + 9, len - 9 + 1); // rm address
125 while (*p && *p != ' ' && *p != ':')
128 ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
129 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
132 msg("no trailing blank in '%s'\n", buf);
139 static int name_cache_cmp(const void *p1, const void *p2)
141 // masm ignores case, so do we
142 return stricmp(*(char * const *)p1, *(char * const *)p2);
145 static void rebuild_name_cache(void)
150 // build a sorted name cache
151 newsize = get_nlist_size();
152 if (newsize > name_cache_size) {
153 tmp = realloc(name_cache, newsize * sizeof(name_cache[0]));
155 msg("OOM for name cache\n");
158 name_cache = (char **)tmp;
160 for (i = 0; i < name_cache_size; i++)
162 for (i = 0; i < newsize; i++)
163 name_cache[i] = strdup(get_nlist_name(i));
165 name_cache_size = newsize;
166 qsort(name_cache, name_cache_size, sizeof(name_cache[0]),
170 static void my_rename(ea_t ea, char *name)
176 qsnprintf(buf, sizeof(buf), "%s", name);
179 pp = (char **)bsearch(&p, name_cache, name_cache_size,
180 sizeof(name_cache[0]), name_cache_cmp);
184 qsnprintf(buf, sizeof(buf), "%s_g%d", name, n);
190 msg("rename failure? '%s'\n", name);
192 do_name_anyway(ea, buf);
193 rebuild_name_cache();
196 static void make_align(ea_t ea)
201 tmp_ea = next_head(ea, inf.maxEA);
202 if ((tmp_ea & 0x03) == 0) {
203 n = calc_max_align(tmp_ea);
204 if (n > 4) // masm doesn't like more..
206 msg("%x: align %d\n", ea, 1 << n);
207 do_unknown(ea, DOUNK_SIMPLE);
208 doAlign(ea, tmp_ea - ea, n);
212 static void idaapi run(int /*arg*/)
214 // isEnabled(ea) // address belongs to disassembly
215 // ea_t ea = get_screen_ea();
217 // foo = DecodeInstruction(ScreenEA());
225 ea_t ui_ea_block = 0, ea_size;
226 ea_t tmp_ea, target_ea;
237 // get rid of structs, masm doesn't understand them
238 idx = get_first_struc_idx();
239 while (idx != BADNODE) {
240 tid_t tid = get_struc_by_idx(idx);
241 struc_t *struc = get_struc(tid);
242 get_struc_name(tid, buf, sizeof(buf));
243 msg("removing struct '%s'\n", buf);
244 //del_struc_members(struc, 0, get_max_offset(struc));
247 idx = get_first_struc_idx();
250 rebuild_name_cache();
252 // 1st pass: walk through all funcs
257 func_tail_iterator_t fti(func);
259 msg("%x: func_tail_iterator_t main failed\n", ea);
262 const area_t &f_area = fti.chunk();
265 // rename global syms which conflict with frame member names
266 frame = get_frame(func);
269 for (m = 0; m < (int)frame->memqty; m++)
271 ret = get_member_name(frame->members[m].id, buf, sizeof(buf));
273 msg("%x: member has no name?\n", ea);
276 if (buf[0] == ' ') // what's this?
278 if (IS_START(buf, "arg_") || IS_START(buf, "var_"))
281 // check for dupe names
283 for (m1 = 0; m1 < m; m1++) {
284 get_member_name(frame->members[m1].id, buf2, sizeof(buf2));
285 if (stricmp(buf, buf2) == 0)
289 if (is_name_reserved(buf) || dupe) {
290 msg("%x: renaming '%s'\n", ea, buf);
291 qstrncat(buf, "_", sizeof(buf));
292 ret = set_member_name(frame, frame->members[m].soff, buf);
294 msg("%x: renaming failed\n", ea);
300 pp = (char **)bsearch(&p, name_cache, name_cache_size,
301 sizeof(name_cache[0]), name_cache_cmp);
305 tmp_ea = get_name_ea(BADADDR, *pp);
306 msg("%x: renaming '%s' because of '%s' at %x\n",
307 tmp_ea, *pp, buf, ea);
308 my_rename(tmp_ea, *pp);
312 // detect tailcalls to next func with 'jmp $+5' (offset 0)
313 if (f_area.endEA - f_area.startEA >= 5
314 && decode_insn(f_area.endEA - 5) && cmd.itype == NN_jmp
315 && cmd.Operands[0].type == o_near
316 && cmd.Operands[0].addr == f_area.endEA
317 && get_name(BADADDR, f_area.endEA, buf, sizeof(buf))
318 && get_cmt(f_area.endEA - 5, false, buf2, sizeof(buf2)) <= 0)
320 qsnprintf(buf2, sizeof(buf2), "sctpatch: jmp %s", buf);
321 set_cmt(f_area.endEA - 5, buf2, false);
324 func = get_next_func(ea);
327 // 2nd pass over whole .text and .(ro)data segments
328 for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA))
330 segment_t *seg = getseg(ea);
333 if (seg->type == SEG_XTRN)
335 if (seg->type != SEG_CODE && seg->type != SEG_DATA)
338 ea_flags = get_flags_novalue(ea);
340 if (isCode(ea_flags))
342 if (!decode_insn(ea)) {
343 msg("%x: decode_insn() failed\n", ea);
347 // masm doesn't understand IDA's float/xmm types
348 if (cmd.itype == NN_fld || cmd.itype == NN_fst
349 || cmd.itype == NN_movapd || cmd.itype == NN_movlpd)
351 for (o = 0; o < UA_MAXOP; o++) {
352 if (cmd.Operands[o].type == o_void)
355 if (cmd.Operands[o].type == o_mem) {
356 tmp_ea = cmd.Operands[o].addr;
357 flags_t tmp_ea_flags = get_flags_novalue(tmp_ea);
358 // ..but base float is ok..
359 int is_flt = isDwrd(tmp_ea_flags) || isFloat(tmp_ea_flags);
360 if (!is_flt && !isUnknown(tmp_ea_flags))
363 get_name(ea, tmp_ea, buf, sizeof(buf));
364 msg("%x: undefining %x '%s'\n", ea, tmp_ea, buf);
365 do_unknown(tmp_ea, DOUNK_EXPAND);
370 else if (cmd.itype == NN_lea) {
371 // detect code alignment
372 if (cmd.Operands[0].reg == cmd.Operands[1].reg
373 && cmd.Operands[1].type == o_displ
374 && cmd.Operands[1].addr == 0)
379 else if (!isDefArg1(ea_flags)
380 && cmd.Operands[1].type == o_mem // why o_mem?
381 && cmd.Operands[1].dtyp == dt_dword)
383 if (inf.minEA <= cmd.Operands[1].addr
384 && cmd.Operands[1].addr < inf.maxEA)
386 // lea to segments, like ds:58D6A8h[edx*8]
387 msg("%x: lea offset to %x\n", ea, cmd.Operands[1].addr);
388 op_offset(ea, 1, REF_OFF32);
392 // ds:0[eax*8] -> [eax*8+0]
393 msg("%x: dropping ds: for %x\n", ea, cmd.Operands[1].addr);
398 else if (cmd.itype == NN_mov && cmd.segpref == 0x1e // 2e?
399 && cmd.Operands[0].type == o_reg
400 && cmd.Operands[1].type == o_reg
401 && cmd.Operands[0].dtyp == cmd.Operands[1].dtyp
402 && cmd.Operands[0].reg == cmd.Operands[1].reg)
404 // db 2Eh; mov eax, eax
408 // find non-local branches
409 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near)
411 target_ea = cmd.Operands[0].addr;
413 nonlocal_add(target_ea);
415 ret = get_func_chunknum(func, target_ea);
417 // a jump to another func or chunk
418 // check if it lands on func start
419 if (!isFunc(get_flags_novalue(target_ea)))
420 nonlocal_add(target_ea);
427 ea_size = get_item_size(ea);
429 if (func == NULL && isOff0(ea_flags)) {
430 for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4)
431 nonlocal_add(get_long(ea + tmp_ea));
434 // IDA vs masm float/mmx/xmm type incompatibility
435 if (isDouble(ea_flags) || isTbyt(ea_flags)
436 || isPackReal(ea_flags))
440 else if (isOwrd(ea_flags)) {
442 get_name(BADADDR, ea, buf, sizeof(buf));
443 if (IS_START(buf, "xmm"))
446 // masm doesn't understand IDA's unicode
447 else if (isASCII(ea_flags) && ea_size >= 4
448 && (get_long(ea) & 0xff00ff00) == 0) // lame..
452 // masm doesn't understand large aligns
453 else if (isAlign(ea_flags) && ea_size > 0x10) {
454 msg("%x: undefining align %d\n", ea, ea_size);
455 do_unknown(ea, DOUNK_EXPAND);
460 get_name(BADADDR, ea, buf, sizeof(buf));
461 msg("%x: undefining '%s'\n", ea, buf);
462 do_unknown(ea, DOUNK_EXPAND);
467 // check namelist for reserved names
468 n = get_nlist_size();
469 for (i = 0; i < n; i++) {
470 ea = get_nlist_ea(i);
471 name = get_nlist_name(i);
473 msg("%x: null name?\n", ea);
477 // rename vars with '?@' (funcs are ok)
479 ea_flags = get_flags_novalue(ea);
480 if (!isCode(ea_flags) && strpbrk(name, "?@"))
483 if (change_qat || is_name_reserved(name)) {
484 msg("%x: renaming name '%s'\n", ea, name);
485 qsnprintf(buf, sizeof(buf), "%s", name);
488 for (p = buf; *p != 0; p++) {
489 if (*p == '?' || *p == '@') {
490 qsnprintf(buf2, sizeof(buf2), "%02x", (unsigned char)*p);
491 memmove(p + 1, p, strlen(p) + 1);
501 if (nonlocal_bt_cnt > 1) {
502 qsort(nonlocal_bt, nonlocal_bt_cnt,
503 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
506 char *fname = askfile_c(1, NULL, "Save asm file");
509 fout = qfopen(fname, "w");
511 msg("couldn't open '%s'\n", fname);
515 show_wait_box("Saving..");
517 // deal with the beginning
519 int flags = 0; // calc_default_idaplace_flags();
520 linearray_t ln(&flags);
525 n = ln.get_linecnt();
526 for (i = 0; i < n - 1; i++) {
527 do_def_line(buf, sizeof(buf), ln.down(), ea);
528 if (strstr(buf, "include"))
532 qfprintf(fout, "%s\n", buf);
533 p = strstr(buf, ".mmx");
535 memcpy(p, ".xmm", 4);
537 qfprintf(fout, "%s\n", buf);
540 p = strstr(buf, ".model");
542 qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf));
544 qfprintf(fout, "\n%s\n", buf);
553 int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0;
554 int word_imm = 0, dword_imm = 0, do_pushf = 0, do_nops = 0;
556 if ((ea >> 14) != ui_ea_block) {
557 ui_ea_block = ea >> 14;
563 segment_t *seg = getseg(ea);
564 if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA))
567 ea_flags = get_flags_novalue(ea);
568 if (isCode(ea_flags))
570 if (!decode_insn(ea))
573 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near
574 && cmd.Operands[0].dtyp == dt_dword)
578 else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf)
584 for (o = 0; o < UA_MAXOP; o++) {
585 const op_t &opr = cmd.Operands[o];
586 if (opr.type == o_void)
590 if (opr.type == o_mem && opr.specval_shorts.high == 0x21)
592 if (opr.hasSIB && x86_scale(opr) == 0
593 && x86_index(opr) != INDEX_NONE)
597 // annoying alignment variant..
598 if (opr.type == o_imm && opr.dtyp == dt_dword
599 && (opr.value < 0x80 || opr.value > 0xffffff80)
600 && cmd.size >= opr.offb + 4)
602 if (get_long(ea + opr.offb) == opr.value)
605 else if (opr.type == o_imm && opr.dtyp == dt_word
606 && (opr.value < 0x80 || opr.value > 0xff80)
607 && cmd.size >= opr.offb + 2)
609 if (get_word(ea + opr.offb) == (ushort)opr.value)
612 else if (opr.type == o_displ && opr.addr == 0
613 && opr.offb != 0 && opr.hasSIB && opr.sib == 0x24)
615 // uses [esp+0] with 0 encoded into op
621 if (isOff0(ea_flags))
626 n = ln.get_linecnt();
627 for (i = pl.lnnum; i < n; i++) {
628 do_def_line(buf, sizeof(buf), ln.down(), ea);
631 for (fw = buf; *fw != 0 && *fw == ' '; )
636 p = strstr(fw, "large ");
638 memmove(p, p + 6, strlen(p + 6) + 1);
641 p = strstr(fw, " rva ");
644 memmove(p + 4 + 3, p + 4, strlen(p + 4) + 1);
645 memcpy(p + 1, "offset", 6);
651 if (p != NULL && p[1] == 'e') {
653 // scale is 1, must specify it explicitly so that
654 // masm chooses the right scaled reg
655 memmove(p + 2, p, strlen(p) + 1);
661 if (fw != buf && fw[0] == 'j')
663 while (p && *p != ' ')
665 while (p && *p == ' ')
668 memmove(p + 9, p, strlen(p) + 1);
669 memcpy(p, "near ptr ", 9);
674 p = strstr(fw, ", ");
675 if (p != NULL && '0' <= p[2] && p[2] <= '9') {
677 memmove(p + 9, p, strlen(p) + 1);
678 memcpy(p, "word ptr ", 9);
681 else if (dword_imm) {
682 p = strstr(fw, ", ");
683 if (p != NULL && '0' <= p[2] && p[2] <= '9') {
685 memmove(p + 10, p, strlen(p) + 1);
686 memcpy(p, "dword ptr ", 10);
690 p = strstr(fw, "pushf");
692 p = strstr(fw, "popf");
694 p = strchr(p, 'f') + 1;
695 memmove(p + 1, p, strlen(p) + 1);
700 if (fw[0] == 'a' && IS_START(fw, "assume cs")) {
701 // "assume cs" causes problems with ext syms
702 memmove(fw + 1, fw, strlen(fw) + 1);
705 else if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') {
707 qfprintf(fout, "include public.inc\n\n");
714 qfprintf(fout, "%s\n", buf);
717 while (do_nops-- > 0)
718 qfprintf(fout, " nop ; adj\n");
720 // note: next_head skips some undefined stuff
721 ea = next_not_tail(ea); // correct?
736 msg("%d lines saved.\n", fout_line);
739 //--------------------------------------------------------------------------
741 static const char comment[] = "Generate disassembly for nasm";
742 static const char help[] = "Generate asm file\n";
743 static const char wanted_name[] = "Save asm";
744 static const char wanted_hotkey[] = "Shift-S";
746 //--------------------------------------------------------------------------
748 // PLUGIN DESCRIPTION BLOCK
750 //--------------------------------------------------------------------------
753 IDP_INTERFACE_VERSION,
756 term, // terminate. this pointer may be NULL.
757 run, // invoke plugin
758 comment, // long comment about the plugin
759 // it could appear in the status line
761 help, // multiline help about the plugin
762 wanted_name, // the preferred short name of the plugin
763 wanted_hotkey // the preferred hotkey to run the plugin
766 // vim:ts=2:shiftwidth=2:expandtab