1 #define NO_OBSOLETE_FUNCS
14 #define IS_START(w, y) !strncmp(w, y, strlen(y))
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
17 // non-local branch targets
18 static ea_t *nonlocal_bt;
19 static int nonlocal_bt_alloc;
20 static int nonlocal_bt_cnt;
22 //--------------------------------------------------------------------------
23 static int idaapi init(void)
28 //--------------------------------------------------------------------------
29 static void idaapi term(void)
31 if (nonlocal_bt != NULL) {
35 nonlocal_bt_alloc = 0;
38 //--------------------------------------------------------------------------
40 static const char *reserved_names[] = {
48 static int is_name_reserved(const char *name)
51 for (i = 0; i < ARRAY_SIZE(reserved_names); i++)
52 if (strcasecmp(name, reserved_names[i]) == 0)
58 static int nonlocal_bt_cmp(const void *p1, const void *p2)
60 const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2;
64 static void nonlocal_add(ea_t ea)
66 if (nonlocal_bt_cnt >= nonlocal_bt_alloc) {
67 nonlocal_bt_alloc += nonlocal_bt_alloc * 2 + 64;
68 nonlocal_bt = (ea_t *)realloc(nonlocal_bt,
69 nonlocal_bt_alloc * sizeof(nonlocal_bt[0]));
70 if (nonlocal_bt == NULL) {
75 nonlocal_bt[nonlocal_bt_cnt++] = ea;
78 // is instruction a (un)conditional jump (not call)?
79 static int is_insn_jmp(uint16 itype)
81 return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz);
84 static void do_def_line(char *buf, size_t buf_size, const char *line)
90 tag_remove(line, buf, buf_size); // remove color codes
96 memmove(buf, buf + 9, len - 9 + 1); // rm address
98 if (IS_START(buf, "loc_")) {
99 ea = strtoul(buf + 4, &endp, 16);
100 if (ea != 0 && *endp == ':') {
101 ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
102 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
105 msg("no trailing blank in '%s'\n", buf);
113 static void idaapi run(int /*arg*/)
115 // isEnabled(ea) // address belongs to disassembly
116 // ea_t ea = get_screen_ea();
117 // foo = DecodeInstruction(ScreenEA());
125 ea_t ui_ea_block = 0, ea_size;
126 ea_t tmp_ea, target_ea;
136 // get rid of structs, masm doesn't understand them
137 idx = get_first_struc_idx();
138 while (idx != BADNODE) {
139 tid_t tid = get_struc_by_idx(idx);
140 struc_t *struc = get_struc(tid);
141 get_struc_name(tid, buf, sizeof(buf));
142 msg("removing struct '%s'\n", buf);
143 //del_struc_members(struc, 0, get_max_offset(struc));
146 idx = get_first_struc_idx();
149 // 1st pass: walk through all funcs
150 func = get_func(inf.minEA);
153 func_tail_iterator_t fti(func);
155 msg("%x: func_tail_iterator_t main failed\n", ea);
158 const area_t &f_area = fti.chunk();
161 // rename global syms which conflict with frame member names
162 frame = get_frame(func);
165 for (m = 0; m < (int)frame->memqty; m++)
167 ret = get_member_name(frame->members[m].id, buf, sizeof(buf));
169 msg("%x: member has no name?\n", ea);
172 if (buf[0] == ' ') // what's this?
174 if (IS_START(buf, "arg_") || IS_START(buf, "var_"))
177 // check for dupe names
179 for (m1 = 0; m1 < m; m1++) {
180 get_member_name(frame->members[m1].id, buf2, sizeof(buf2));
181 if (stricmp(buf, buf2) == 0)
185 if (is_name_reserved(buf) || dupe) {
186 msg("%x: renaming '%s'\n", ea, buf);
187 qstrncat(buf, "_", sizeof(buf));
188 ret = set_member_name(frame, frame->members[m].soff, buf);
190 msg("%x: renaming failed\n", ea);
195 tmp_ea = get_name_ea(ea, buf);
196 if (tmp_ea == 0 || tmp_ea == ~0)
199 msg("%x: from %x: renaming '%s'\n", tmp_ea, ea, buf);
200 qstrncat(buf, "_g", sizeof(buf));
201 set_name(tmp_ea, buf);
205 func = get_next_func(ea);
208 // 2nd pass over whole .text and .(ro)data segments
209 for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA))
211 segment_t *seg = getseg(ea);
214 if (seg->type == SEG_XTRN)
216 if (seg->type != SEG_CODE && seg->type != SEG_DATA)
219 ea_flags = get_flags_novalue(ea);
221 if (isCode(ea_flags))
223 if (!decode_insn(ea)) {
224 msg("%x: decode_insn() failed\n", ea);
228 // masm doesn't understand IDA's float/xmm types
229 if (cmd.itype == NN_fld || cmd.itype == NN_fst
230 || cmd.itype == NN_movapd || cmd.itype == NN_movlpd)
232 for (o = 0; o < UA_MAXOP; o++) {
233 if (cmd.Operands[o].type == o_void)
236 if (cmd.Operands[o].type == o_mem) {
237 tmp_ea = cmd.Operands[o].addr;
238 flags_t tmp_ea_flags = get_flags_novalue(tmp_ea);
239 if (!isUnknown(tmp_ea_flags)) {
241 get_name(ea, tmp_ea, buf, sizeof(buf));
242 msg("%x: undefining %x '%s'\n", ea, tmp_ea, buf);
243 do_unknown(tmp_ea, DOUNK_EXPAND);
248 // detect code alignment
249 else if (cmd.itype == NN_lea) {
250 if (cmd.Operands[0].reg == cmd.Operands[1].reg
251 && cmd.Operands[1].type == o_displ
252 && cmd.Operands[1].addr == 0)
254 tmp_ea = next_head(ea, inf.maxEA);
255 if ((tmp_ea & 0x03) == 0) {
256 n = calc_max_align(tmp_ea);
257 if (n > 4) // masm doesn't like more..
259 msg("%x: align %d\n", ea, 1 << n);
260 do_unknown(ea, DOUNK_SIMPLE);
261 doAlign(ea, tmp_ea - ea, n);
266 // find non-local branches
267 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near)
269 target_ea = cmd.Operands[0].addr;
271 nonlocal_add(target_ea);
273 ret = get_func_chunknum(func, target_ea);
275 // a jump to another func or chunk
276 // check if it lands on func start
277 if (!isFunc(get_flags_novalue(target_ea)))
278 nonlocal_add(target_ea);
285 ea_size = get_item_size(ea);
287 if (func == NULL && isOff0(ea_flags)) {
288 for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4)
289 nonlocal_add(get_long(ea + tmp_ea));
292 // IDA vs masm float/mmx/xmm type incompatibility
293 if (isDouble(ea_flags) || isTbyt(ea_flags)
294 || isPackReal(ea_flags))
298 else if (isOwrd(ea_flags)) {
300 get_name(BADADDR, ea, buf, sizeof(buf));
301 if (IS_START(buf, "xmm"))
304 // masm doesn't understand IDA's unicode
305 else if (isASCII(ea_flags) && ea_size >= 4
306 && (get_long(ea) & 0xff00ff00) == 0) // lame..
310 // masm doesn't understand large aligns
311 else if (isAlign(ea_flags) && ea_size > 0x10) {
312 msg("%x: undefining align %d\n", ea, ea_size);
313 do_unknown(ea, DOUNK_EXPAND);
318 get_name(BADADDR, ea, buf, sizeof(buf));
319 msg("%x: undefining '%s'\n", ea, buf);
320 do_unknown(ea, DOUNK_EXPAND);
325 // check namelist for reserved names
326 n = get_nlist_size();
327 for (i = 0; i < n; i++) {
328 ea = get_nlist_ea(i);
329 name = get_nlist_name(i);
331 msg("%x: null name?\n", ea);
335 if (is_name_reserved(name)) {
336 msg("%x: renaming name '%s'\n", ea, name);
337 qsnprintf(buf, sizeof(buf), "%s_g", name);
342 if (nonlocal_bt_cnt > 1) {
343 qsort(nonlocal_bt, nonlocal_bt_cnt,
344 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
347 char *fname = askfile_c(1, NULL, "Save asm file");
350 fout = qfopen(fname, "w");
352 msg("couldn't open '%s'\n", fname);
356 show_wait_box("Saving..");
358 // deal with the beginning
360 int flags = 0; // calc_default_idaplace_flags();
361 linearray_t ln(&flags);
366 n = ln.get_linecnt();
367 for (i = 0; i < n - 1; i++) {
368 do_def_line(buf, sizeof(buf), ln.down());
369 if (strstr(buf, "include"))
371 p = strstr(buf, "assume cs");
373 memmove(p + 1, p, strlen(p) + 1);
378 qfprintf(fout, "%s\n", buf);
379 p = strstr(buf, ".mmx");
381 memcpy(p, ".xmm", 4);
383 qfprintf(fout, "%s\n", buf);
386 p = strstr(buf, ".model");
388 qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf));
390 qfprintf(fout, "\n%s\n", buf);
398 int drop_large = 0, drop_rva = 0, set_scale = 0, jmp_near = 0;
399 int word_imm = 0, dword_imm = 0, do_pushf = 0;
401 if ((ea >> 14) != ui_ea_block) {
402 ui_ea_block = ea >> 14;
408 segment_t *seg = getseg(ea);
409 if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA))
412 ea_flags = get_flags_novalue(ea);
413 if (isCode(ea_flags))
415 if (!decode_insn(ea))
418 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near
419 && cmd.Operands[0].dtyp == dt_dword)
423 else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf)
429 for (o = 0; o < UA_MAXOP; o++) {
430 const op_t &opr = cmd.Operands[o];
431 if (opr.type == o_void)
435 if (opr.type == o_mem && opr.specval_shorts.high == 0x21)
437 if (opr.hasSIB && x86_scale(opr) == 0
438 && x86_index(opr) != INDEX_NONE)
442 // annoying alignment variant..
443 if (opr.type == o_imm && opr.dtyp == dt_dword
444 && (opr.value < 0x80 || opr.value > 0xffffff80)
445 && cmd.size >= opr.offb + 4)
447 if (get_long(ea + opr.offb) == opr.value)
450 else if (opr.type == o_imm && opr.dtyp == dt_word
451 && (opr.value < 0x80 || opr.value > 0xff80)
452 && cmd.size >= opr.offb + 2)
454 if (get_word(ea + opr.offb) == (ushort)opr.value)
460 if (isOff0(ea_flags))
465 n = ln.get_linecnt();
466 for (i = pl.lnnum; i < n; i++) {
467 do_def_line(buf, sizeof(buf), ln.down());
470 for (fw = buf; *fw != 0 && *fw == ' '; )
475 p = strstr(fw, "large ");
477 memmove(p, p + 6, strlen(p + 6) + 1);
480 p = strstr(fw, " rva ");
483 memmove(p, p + 4, strlen(p + 4) + 1);
489 if (p != NULL && p[1] == 'e') {
491 // scale is 1, must specify it explicitly so that
492 // masm chooses the right scaled reg
493 memmove(p + 2, p, strlen(p) + 1);
499 while (p && *p != ' ')
501 while (p && *p == ' ')
504 memmove(p + 9, p, strlen(p) + 1);
505 memcpy(p, "near ptr ", 9);
509 p = strstr(fw, ", ");
510 if (p != NULL && '0' <= p[2] && p[2] <= '9') {
512 memmove(p + 9, p, strlen(p) + 1);
513 memcpy(p, "word ptr ", 9);
516 else if (dword_imm) {
517 p = strstr(fw, ", ");
518 if (p != NULL && '0' <= p[2] && p[2] <= '9') {
520 memmove(p + 10, p, strlen(p) + 1);
521 memcpy(p, "dword ptr ", 10);
525 p = strstr(fw, "pushf");
527 p = strstr(fw, "popf");
529 p = strchr(p, 'f') + 1;
530 memmove(p + 1, p, strlen(p) + 1);
535 if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') {
537 qfprintf(fout, "include public.inc\n\n");
544 qfprintf(fout, "%s\n", buf);
547 // note: next_head skips some undefined stuff
548 ea = next_not_tail(ea); // correct?
563 msg("%d lines saved.\n", fout_line);
566 //--------------------------------------------------------------------------
568 static const char comment[] = "Generate disassembly lines for one address";
569 static const char help[] = "Generate asm file\n";
570 static const char wanted_name[] = "Save asm";
571 static const char wanted_hotkey[] = "Ctrl-F6";
573 //--------------------------------------------------------------------------
575 // PLUGIN DESCRIPTION BLOCK
577 //--------------------------------------------------------------------------
580 IDP_INTERFACE_VERSION,
583 term, // terminate. this pointer may be NULL.
584 run, // invoke plugin
585 comment, // long comment about the plugin
586 // it could appear in the status line
588 help, // multiline help about the plugin
589 wanted_name, // the preferred short name of the plugin
590 wanted_hotkey // the preferred hotkey to run the plugin
593 // vim:ts=2:shiftwidth=2:expandtab