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