more patches for masm to do correct output
[ia32rtools.git] / plugin / saveasm.cpp
1 #define NO_OBSOLETE_FUNCS
2 #include <ida.hpp>
3 #include <idp.hpp>
4 #include <bytes.hpp>
5 #include <loader.hpp>
6 #include <kernwin.hpp>
7
8 #include <name.hpp>
9 #include <frame.hpp>
10 #include <struct.hpp>
11 #include <auto.hpp>
12 #include <intel.hpp>
13
14 #define IS_START(w, y) !strncmp(w, y, strlen(y))
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
16
17 // non-local branch targets
18 static ea_t *nonlocal_bt;
19 static int nonlocal_bt_alloc;
20 static int nonlocal_bt_cnt;
21
22 //--------------------------------------------------------------------------
23 static int idaapi init(void)
24 {
25   return PLUGIN_OK;
26 }
27
28 //--------------------------------------------------------------------------
29 static void idaapi term(void)
30 {
31   if (nonlocal_bt != NULL) {
32     free(nonlocal_bt);
33     nonlocal_bt = NULL;
34   }
35   nonlocal_bt_alloc = 0;
36 }
37
38 //--------------------------------------------------------------------------
39
40 static const char *reserved_names[] = {
41   "name",
42   "type",
43   "offset",
44   "aam",
45 };
46
47 static int is_name_reserved(const char *name)
48 {
49   int i;
50   for (i = 0; i < ARRAY_SIZE(reserved_names); i++)
51     if (strcasecmp(name, reserved_names[i]) == 0)
52       return 1;
53
54   return 0;
55 }
56
57 static int nonlocal_bt_cmp(const void *p1, const void *p2)
58 {
59   const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2;
60   return *e1 - *e2;
61 }
62
63 static void nonlocal_add(ea_t ea)
64 {
65   if (nonlocal_bt_cnt >= nonlocal_bt_alloc) {
66     nonlocal_bt_alloc += nonlocal_bt_alloc * 2 + 64;
67     nonlocal_bt = (ea_t *)realloc(nonlocal_bt,
68       nonlocal_bt_alloc * sizeof(nonlocal_bt[0]));
69     if (nonlocal_bt == NULL) {
70       msg("OOM\n");
71       return;
72     }
73   }
74   nonlocal_bt[nonlocal_bt_cnt++] = ea;
75 }
76
77 // is instruction a (un)conditional jump (not call)?
78 static int is_insn_jmp(uint16 itype)
79 {
80   return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz);
81 }
82
83 static void do_def_line(char *buf, size_t buf_size, const char *line)
84 {
85   char *endp = NULL;
86   ea_t ea, *ea_ret;
87   int len;
88
89   tag_remove(line, buf, buf_size); // remove color codes
90   len = strlen(buf);
91   if (len < 9) {
92     buf[0] = 0;
93     return;
94   }
95   memmove(buf, buf + 9, len - 9 + 1); // rm address
96
97   if (IS_START(buf, "loc_")) {
98     ea = strtoul(buf + 4, &endp, 16);
99     if (ea != 0 && *endp == ':') {
100       ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
101         sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
102       if (ea_ret != 0) {
103         if (endp[1] != ' ')
104           msg("no trailing blank in '%s'\n", buf);
105         else
106           endp[1] = ':';
107       }
108     }
109   }
110 }
111
112 static void idaapi run(int /*arg*/)
113 {
114   // isEnabled(ea) // address belongs to disassembly
115   // ea_t ea = get_screen_ea();
116   // foo = DecodeInstruction(ScreenEA());
117   FILE *fout = NULL;
118   int fout_line = 0;
119   char buf[MAXSTR];
120   char buf2[MAXSTR];
121   const char *name;
122   struc_t *frame;
123   func_t *func;
124   ea_t ui_ea_block = 0, ea_size;
125   ea_t tmp_ea, target_ea;
126   ea_t ea;
127   flags_t ea_flags;
128   uval_t idx;
129   int i, o, m, n;
130   int ret;
131   char *p;
132
133   nonlocal_bt_cnt = 0;
134
135   // get rid of structs, masm doesn't understand them
136   idx = get_first_struc_idx();
137   while (idx != BADNODE) {
138     tid_t tid = get_struc_by_idx(idx);
139     struc_t *struc = get_struc(tid);
140     get_struc_name(tid, buf, sizeof(buf));
141     msg("removing struct '%s'\n", buf);
142     //del_struc_members(struc, 0, get_max_offset(struc));
143     del_struc(struc);
144
145     idx = get_first_struc_idx();
146   }
147
148   // 1st pass: walk through all funcs
149   func = get_func(inf.minEA);
150   while (func != NULL)
151   {
152     func_tail_iterator_t fti(func);
153     if (!fti.main()) {
154       msg("%x: func_tail_iterator_t main failed\n", ea);
155       return;
156     }
157     const area_t &f_area = fti.chunk();
158     ea = f_area.startEA;
159
160     // rename global syms which conflict with frame member names
161     frame = get_frame(func);
162     if (frame != NULL)
163     {
164       for (m = 0; m < (int)frame->memqty; m++)
165       {
166         ret = get_member_name(frame->members[m].id, buf, sizeof(buf));
167         if (ret <= 0) {
168           msg("%x: member has no name?\n", ea);
169           return;
170         }
171         if (buf[0] == ' ') // what's this?
172           continue;
173         if (IS_START(buf, "arg_") || IS_START(buf, "var_"))
174           continue;
175
176         // check for dupe names
177         int m1, dupe = 0;
178         for (m1 = 0; m1 < m; m1++) {
179           get_member_name(frame->members[m1].id, buf2, sizeof(buf2));
180           if (stricmp(buf, buf2) == 0)
181             dupe = 1;
182         }
183
184         if (is_name_reserved(buf) || dupe) {
185           msg("%x: renaming '%s'\n", ea, buf);
186           qstrncat(buf, "_", sizeof(buf));
187           ret = set_member_name(frame, frame->members[m].soff, buf);
188           if (!ret) {
189             msg("%x: renaming failed\n", ea);
190             return;
191           }
192         }
193
194         tmp_ea = get_name_ea(ea, buf);
195         if (tmp_ea == 0 || tmp_ea == ~0)
196           continue;
197
198         msg("%x: from %x: renaming '%s'\n", tmp_ea, ea, buf);
199         qstrncat(buf, "_g", sizeof(buf));
200         set_name(tmp_ea, buf);
201       }
202     }
203
204     func = get_next_func(ea);
205   }
206
207   // 2nd pass over whole .text and .(ro)data segments
208   for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA))
209   {
210     segment_t *seg = getseg(ea);
211     if (!seg)
212       break;
213     if (seg->type == SEG_XTRN)
214       continue;
215     if (seg->type != SEG_CODE && seg->type != SEG_DATA)
216       break;
217
218     ea_flags = get_flags_novalue(ea);
219     func = get_func(ea);
220     if (isCode(ea_flags))
221     {
222       if (!decode_insn(ea)) {
223         msg("%x: decode_insn() failed\n", ea);
224         continue;
225       }
226
227       // masm doesn't understand IDA's float/xmm types
228       if (cmd.itype == NN_fld || cmd.itype == NN_fst
229         || cmd.itype == NN_movapd || cmd.itype == NN_movlpd)
230       {
231         for (o = 0; o < UA_MAXOP; o++) {
232           if (cmd.Operands[o].type == o_void)
233             break;
234
235           if (cmd.Operands[o].type == o_mem) {
236             tmp_ea = cmd.Operands[o].addr;
237             flags_t tmp_ea_flags = get_flags_novalue(tmp_ea);
238             if (!isUnknown(tmp_ea_flags)) {
239               buf[0] = 0;
240               get_name(ea, tmp_ea, buf, sizeof(buf));
241               msg("%x: undefining %x '%s'\n", ea, tmp_ea, buf);
242               do_unknown(tmp_ea, DOUNK_EXPAND);
243             }
244           }
245         }
246       }
247       // detect code alignment
248       else if (cmd.itype == NN_lea) {
249         if (cmd.Operands[0].reg == cmd.Operands[1].reg
250           && cmd.Operands[1].type == o_displ
251           && cmd.Operands[1].addr == 0)
252         {
253           tmp_ea = next_head(ea, inf.maxEA);
254           if ((tmp_ea & 0x03) == 0) {
255             n = calc_max_align(tmp_ea);
256             if (n > 4) // masm doesn't like more..
257               n = 4;
258             msg("%x: align %d\n", ea, 1 << n);
259             do_unknown(ea, DOUNK_SIMPLE);
260             doAlign(ea, tmp_ea - ea, n);
261           }
262         }
263       }
264
265       // find non-local branches
266       if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near)
267       {
268         target_ea = cmd.Operands[0].addr;
269         if (func == NULL)
270           nonlocal_add(target_ea);
271         else {
272           ret = get_func_chunknum(func, target_ea);
273           if (ret != 0) {
274             // a jump to another func or chunk
275             // check if it lands on func start
276             if (!isFunc(get_flags_novalue(target_ea)))
277               nonlocal_add(target_ea);
278           }
279         }
280       }
281     }
282     else { // not code
283       int do_undef = 0;
284       ea_size = get_item_size(ea);
285
286       if (func == NULL && isOff0(ea_flags)) {
287         for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4)
288           nonlocal_add(get_long(ea + tmp_ea));
289       }
290
291       // IDA vs masm float/mmx/xmm type incompatibility
292       if (isDouble(ea_flags) || isTbyt(ea_flags)
293        || isPackReal(ea_flags))
294       {
295         do_undef = 1;
296       }
297       else if (isOwrd(ea_flags)) {
298         buf[0] = 0;
299         get_name(BADADDR, ea, buf, sizeof(buf));
300         if (IS_START(buf, "xmm"))
301           do_undef = 1;
302       }
303       // masm doesn't understand IDA's unicode
304       else if (isASCII(ea_flags) && ea_size >= 4
305         && (get_long(ea) & 0xff00ff00) == 0) // lame..
306       {
307         do_undef = 1;
308       }
309       // masm doesn't understand large aligns
310       else if (isAlign(ea_flags) && ea_size > 0x10) {
311         msg("%x: undefining align %d\n", ea, ea_size);
312         do_unknown(ea, DOUNK_EXPAND);
313       }
314
315       if (do_undef) {
316         buf[0] = 0;
317         get_name(BADADDR, ea, buf, sizeof(buf));
318         msg("%x: undefining '%s'\n", ea, buf);
319         do_unknown(ea, DOUNK_EXPAND);
320       }
321     }
322   }
323
324   // check namelist for reserved names
325   n = get_nlist_size();
326   for (i = 0; i < n; i++) {
327     ea = get_nlist_ea(i);
328     name = get_nlist_name(i);
329     if (name == NULL) {
330       msg("%x: null name?\n", ea);
331       continue;
332     }
333
334     if (is_name_reserved(name)) {
335       msg("%x: renaming name '%s'\n", ea, name);
336       qsnprintf(buf, sizeof(buf), "%s_g", name);
337       set_name(ea, buf);
338     }
339   }
340
341   if (nonlocal_bt_cnt > 1) {
342     qsort(nonlocal_bt, nonlocal_bt_cnt,
343       sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
344   }
345
346   char *fname = askfile_c(1, NULL, "Save asm file");
347   if (fname == NULL)
348     return;
349   fout = qfopen(fname, "w");
350   if (fout == NULL) {
351     msg("couldn't open '%s'\n", fname);
352     return;
353   }
354
355   show_wait_box("Saving..");
356
357   // deal with the beginning
358   ea = inf.minEA;
359   int flags = 0; // calc_default_idaplace_flags();
360   linearray_t ln(&flags);
361   idaplace_t pl;
362   pl.ea = ea;
363   pl.lnnum = 0;
364   ln.set_place(&pl);
365   n = ln.get_linecnt();
366   for (i = 0; i < n - 1; i++) {
367     do_def_line(buf, sizeof(buf), ln.down());
368     if (strstr(buf, "include"))
369       continue;
370
371     fout_line++;
372     qfprintf(fout, "%s\n", buf);
373     p = strstr(buf, ".mmx");
374     if (p != NULL) {
375       memcpy(p, ".xmm", 4);
376       fout_line++;
377       qfprintf(fout, "%s\n", buf);
378     }
379   }
380   pl.lnnum = i;
381
382   for (;;)
383   {
384     int drop_large = 0, drop_rva = 0, set_scale = 0, jmp_near = 0;
385     int word_imm = 0, dword_imm = 0, do_pushf = 0;
386
387     if ((ea >> 14) != ui_ea_block) {
388       ui_ea_block = ea >> 14;
389       showAddr(ea);
390       if (wasBreak())
391         break;
392     }
393
394     segment_t *seg = getseg(ea);
395     if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA))
396       goto pass;
397
398     ea_flags = get_flags_novalue(ea);
399     if (isCode(ea_flags))
400     {
401       if (!decode_insn(ea))
402         goto pass;
403
404       if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near
405         && cmd.Operands[0].dtyp == dt_dword)
406       {
407         jmp_near = 1;
408       }
409       else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf)
410         && natop())
411       {
412         do_pushf = 1;
413       }
414
415       for (o = 0; o < UA_MAXOP; o++) {
416         const op_t &opr = cmd.Operands[o];
417         if (opr.type == o_void)
418           break;
419
420         // correct?
421         if (opr.type == o_mem && opr.specval_shorts.high == 0x21)
422           drop_large = 1;
423         if (opr.hasSIB && x86_scale(opr) == 0
424           && x86_index(opr) != INDEX_NONE)
425         {
426           set_scale = 1;
427         }
428         // annoying alignment variant..
429         if (opr.type == o_imm && opr.dtyp == dt_dword
430           && (opr.value < 0x80 || opr.value > 0xffffff80)
431           && cmd.size >= opr.offb + 4)
432         {
433           if (get_long(ea + opr.offb) == opr.value)
434             dword_imm = 1;
435         }
436         else if (opr.type == o_imm && opr.dtyp == dt_word
437           && (opr.value < 0x80 || opr.value > 0xff80)
438           && cmd.size >= opr.offb + 2)
439         {
440           if (get_word(ea + opr.offb) == (ushort)opr.value)
441             word_imm = 1;
442         }
443       }
444     }
445     else { // not code
446       if (isOff0(ea_flags))
447         drop_rva = 1;
448     }
449
450 pass:
451     n = ln.get_linecnt();
452     for (i = pl.lnnum; i < n; i++) {
453       do_def_line(buf, sizeof(buf), ln.down());
454
455       // patches..
456       if (drop_large) {
457         p = strstr(buf, "large ");
458         if (p != NULL)
459           memmove(p, p + 6, strlen(p + 6) + 1);
460       }
461       while (drop_rva) {
462         p = strstr(buf, " rva ");
463         if (p == NULL)
464           break;
465         memmove(p, p + 4, strlen(p + 4) + 1);
466       }
467       if (set_scale) {
468         p = strchr(buf, '[');
469         if (p != NULL)
470           p = strchr(p, '+');
471         if (p != NULL && p[1] == 'e') {
472           p += 4;
473           // scale is 1, must specify it explicitly so that
474           // masm chooses the right scaled reg
475           memmove(p + 2, p, strlen(p) + 1);
476           memcpy(p, "*1", 2);
477         }
478       }
479       else if (jmp_near) {
480         p = strchr(buf, 'j');
481         while (p && *p != ' ')
482           p++;
483         while (p && *p == ' ')
484           p++;
485         if (p != NULL) {
486           memmove(p + 9, p, strlen(p) + 1);
487           memcpy(p, "near ptr ", 9);
488         }
489       }
490       if (word_imm) {
491         p = strstr(buf, ", ");
492         if (p != NULL && '0' <= p[2] && p[2] <= '9') {
493           p += 2;
494           memmove(p + 9, p, strlen(p) + 1);
495           memcpy(p, "word ptr ", 9);
496         }
497       }
498       else if (dword_imm) {
499         p = strstr(buf, ", ");
500         if (p != NULL && '0' <= p[2] && p[2] <= '9') {
501           p += 2;
502           memmove(p + 10, p, strlen(p) + 1);
503           memcpy(p, "dword ptr ", 10);
504         }
505       }
506       else if (do_pushf) {
507         p = strstr(buf, "pushf");
508         if (p == NULL)
509           p = strstr(buf, "popf");
510         if (p != NULL) {
511           p = strchr(p, 'f') + 1;
512           memmove(p + 1, p, strlen(p) + 1);
513           *p = 'd';
514         }
515       }
516
517       fout_line++;
518       qfprintf(fout, "%s\n", buf);
519     }
520
521     // note: next_head skips some undefined stuff
522     ea = next_not_tail(ea); // correct?
523     if (ea == BADADDR)
524       break;
525
526     pl.ea = ea;
527     pl.lnnum = 0;
528     ln.set_place(&pl);
529   }
530
531   if (fout != NULL)
532     qfclose(fout);
533   if (fname != NULL)
534     qfree(fname);
535
536   hide_wait_box();
537   msg("%d lines saved.\n", fout_line);
538 }
539
540 //--------------------------------------------------------------------------
541
542 static const char comment[] = "Generate disassembly lines for one address";
543 static const char help[] = "Generate asm file\n";
544 static const char wanted_name[] = "Save asm";
545 static const char wanted_hotkey[] = "Ctrl-F6";
546
547 //--------------------------------------------------------------------------
548 //
549 //      PLUGIN DESCRIPTION BLOCK
550 //
551 //--------------------------------------------------------------------------
552 plugin_t PLUGIN =
553 {
554   IDP_INTERFACE_VERSION,
555   0,                    // plugin flags
556   init,                 // initialize
557   term,                 // terminate. this pointer may be NULL.
558   run,                  // invoke plugin
559   comment,              // long comment about the plugin
560                         // it could appear in the status line
561                         // or as a hint
562   help,                 // multiline help about the plugin
563   wanted_name,          // the preferred short name of the plugin
564   wanted_hotkey         // the preferred hotkey to run the plugin
565 };
566
567 // vim:ts=2:shiftwidth=2:expandtab