plugin: don't rename stdcall names
[ia32rtools.git] / ida / saveasm / saveasm.cpp
CommitLineData
fc1c61f5 1/*
2 * ia32rtools
3 * (C) notaz, 2013,2014
4 *
5 * This work is licensed under the terms of 3-clause BSD license.
6 * See COPYING file in the top-level directory.
7 */
8
d8891fcc 9#define NO_OBSOLETE_FUNCS
10#include <ida.hpp>
11#include <idp.hpp>
12#include <bytes.hpp>
13#include <loader.hpp>
14#include <kernwin.hpp>
15
16#include <name.hpp>
17#include <frame.hpp>
18#include <struct.hpp>
fc1c61f5 19#include <offset.hpp>
d8891fcc 20#include <auto.hpp>
15c7b2a4 21#include <intel.hpp>
d8891fcc 22
23#define IS_START(w, y) !strncmp(w, y, strlen(y))
24#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
25
97609f07 26static char **name_cache;
27static size_t name_cache_size;
28
15c7b2a4 29// non-local branch targets
30static ea_t *nonlocal_bt;
31static int nonlocal_bt_alloc;
32static int nonlocal_bt_cnt;
33
d8891fcc 34//--------------------------------------------------------------------------
35static int idaapi init(void)
36{
37 return PLUGIN_OK;
38}
39
40//--------------------------------------------------------------------------
41static void idaapi term(void)
42{
97609f07 43 size_t i;
44
15c7b2a4 45 if (nonlocal_bt != NULL) {
46 free(nonlocal_bt);
47 nonlocal_bt = NULL;
48 }
49 nonlocal_bt_alloc = 0;
97609f07 50
51 if (name_cache != NULL) {
52 for (i = 0; i < name_cache_size; i++)
53 free(name_cache[i]);
54 free(name_cache);
55 name_cache = NULL;
56 }
57 name_cache_size = 0;
d8891fcc 58}
59
60//--------------------------------------------------------------------------
61
62static const char *reserved_names[] = {
63 "name",
b587e6ae 64 "type",
d8891fcc 65 "offset",
b587e6ae 66 "aam",
e74324f9 67 "aas",
1402b79d 68 "text",
de8a204c 69 "size",
70 "c",
97609f07 71 "align",
e74324f9 72 "addr",
d8891fcc 73};
74
75static int is_name_reserved(const char *name)
76{
77 int i;
78 for (i = 0; i < ARRAY_SIZE(reserved_names); i++)
79 if (strcasecmp(name, reserved_names[i]) == 0)
80 return 1;
81
82 return 0;
83}
84
15c7b2a4 85static int nonlocal_bt_cmp(const void *p1, const void *p2)
86{
87 const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2;
88 return *e1 - *e2;
89}
90
91static void nonlocal_add(ea_t ea)
92{
93 if (nonlocal_bt_cnt >= nonlocal_bt_alloc) {
94 nonlocal_bt_alloc += nonlocal_bt_alloc * 2 + 64;
95 nonlocal_bt = (ea_t *)realloc(nonlocal_bt,
96 nonlocal_bt_alloc * sizeof(nonlocal_bt[0]));
97 if (nonlocal_bt == NULL) {
98 msg("OOM\n");
99 return;
100 }
101 }
102 nonlocal_bt[nonlocal_bt_cnt++] = ea;
103}
104
b587e6ae 105// is instruction a (un)conditional jump (not call)?
106static int is_insn_jmp(uint16 itype)
107{
108 return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz);
109}
110
d53d4cc7 111static void do_def_line(char *buf, size_t buf_size, const char *line,
e74324f9 112 ea_t ea, func_t *func)
d8891fcc 113{
e74324f9 114 char func_name[256] = "<nf>";
115 int is_libfunc = 0;
116 int global_label;
d53d4cc7 117 ea_t *ea_ret;
e74324f9 118 int i, len;
d53d4cc7 119 char *p;
d8891fcc 120
121 tag_remove(line, buf, buf_size); // remove color codes
122 len = strlen(buf);
123 if (len < 9) {
124 buf[0] = 0;
125 return;
126 }
127 memmove(buf, buf + 9, len - 9 + 1); // rm address
15c7b2a4 128
d53d4cc7 129 p = buf;
130 while (*p && *p != ' ' && *p != ':')
131 p++;
132 if (*p == ':') {
133 ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
134 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
e74324f9 135 global_label = (ea_ret != NULL);
136 if (!global_label) {
137 if (func != NULL) {
138 get_func_name(ea, func_name, sizeof(func_name));
139 is_libfunc = func->flags & FUNC_LIB;
140 }
141 for (i = 0; i < get_item_size(ea); i++) {
142 xrefblk_t xb;
143 if (xb.first_to(ea + i, XREF_DATA)) {
144 if (!is_libfunc && xb.type == dr_O)
145 msg("%x: offset xref in %s\n", ea, func_name);
146 global_label = 1;
147 }
148 }
149 }
150 if (global_label) {
d53d4cc7 151 if (p[1] != ' ')
152 msg("no trailing blank in '%s'\n", buf);
153 else
154 p[1] = ':';
15c7b2a4 155 }
156 }
d8891fcc 157}
158
97609f07 159static int name_cache_cmp(const void *p1, const void *p2)
160{
161 // masm ignores case, so do we
162 return stricmp(*(char * const *)p1, *(char * const *)p2);
163}
164
165static void rebuild_name_cache(void)
166{
167 size_t i, newsize;
168 void *tmp;
169
170 // build a sorted name cache
171 newsize = get_nlist_size();
172 if (newsize > name_cache_size) {
173 tmp = realloc(name_cache, newsize * sizeof(name_cache[0]));
174 if (tmp == NULL) {
175 msg("OOM for name cache\n");
176 return;
177 }
178 name_cache = (char **)tmp;
179 }
180 for (i = 0; i < name_cache_size; i++)
181 free(name_cache[i]);
182 for (i = 0; i < newsize; i++)
183 name_cache[i] = strdup(get_nlist_name(i));
184
185 name_cache_size = newsize;
186 qsort(name_cache, name_cache_size, sizeof(name_cache[0]),
187 name_cache_cmp);
188}
189
190static void my_rename(ea_t ea, char *name)
191{
192 char buf[256];
193 char *p, **pp;
194 int n = 0;
195
196 qsnprintf(buf, sizeof(buf), "%s", name);
197 do {
198 p = buf;
199 pp = (char **)bsearch(&p, name_cache, name_cache_size,
200 sizeof(name_cache[0]), name_cache_cmp);
201 if (pp == NULL)
202 break;
203
204 qsnprintf(buf, sizeof(buf), "%s_g%d", name, n);
205 n++;
206 }
207 while (n < 100);
208
209 if (n == 100)
210 msg("rename failure? '%s'\n", name);
211
212 do_name_anyway(ea, buf);
213 rebuild_name_cache();
214}
215
ee8e1bea 216static void make_align(ea_t ea)
217{
218 ea_t tmp_ea;
219 int n;
220
221 tmp_ea = next_head(ea, inf.maxEA);
222 if ((tmp_ea & 0x03) == 0) {
223 n = calc_max_align(tmp_ea);
224 if (n > 4) // masm doesn't like more..
225 n = 4;
226 msg("%x: align %d\n", ea, 1 << n);
227 do_unknown(ea, DOUNK_SIMPLE);
228 doAlign(ea, tmp_ea - ea, n);
229 }
230}
231
d8891fcc 232static void idaapi run(int /*arg*/)
233{
15c7b2a4 234 // isEnabled(ea) // address belongs to disassembly
d8891fcc 235 // ea_t ea = get_screen_ea();
ee8e1bea 236 // extern foo;
15c7b2a4 237 // foo = DecodeInstruction(ScreenEA());
d8891fcc 238 FILE *fout = NULL;
239 int fout_line = 0;
240 char buf[MAXSTR];
1caf86bb 241 char buf2[MAXSTR];
b587e6ae 242 const char *name;
3a0df310 243 const char *cp;
d8891fcc 244 struc_t *frame;
245 func_t *func;
15c7b2a4 246 ea_t ui_ea_block = 0, ea_size;
247 ea_t tmp_ea, target_ea;
d8891fcc 248 ea_t ea;
b587e6ae 249 flags_t ea_flags;
1caf86bb 250 uval_t idx;
d8891fcc 251 int i, o, m, n;
252 int ret;
97609f07 253 char **pp;
d8891fcc 254 char *p;
255
15c7b2a4 256 nonlocal_bt_cnt = 0;
257
1caf86bb 258 // get rid of structs, masm doesn't understand them
259 idx = get_first_struc_idx();
260 while (idx != BADNODE) {
261 tid_t tid = get_struc_by_idx(idx);
262 struc_t *struc = get_struc(tid);
263 get_struc_name(tid, buf, sizeof(buf));
264 msg("removing struct '%s'\n", buf);
265 //del_struc_members(struc, 0, get_max_offset(struc));
266 del_struc(struc);
267
268 idx = get_first_struc_idx();
269 }
270
97609f07 271 rebuild_name_cache();
272
15c7b2a4 273 // 1st pass: walk through all funcs
97609f07 274 ea = inf.minEA;
275 func = get_func(ea);
d8891fcc 276 while (func != NULL)
277 {
15c7b2a4 278 func_tail_iterator_t fti(func);
279 if (!fti.main()) {
280 msg("%x: func_tail_iterator_t main failed\n", ea);
281 return;
282 }
283 const area_t &f_area = fti.chunk();
284 ea = f_area.startEA;
285
286 // rename global syms which conflict with frame member names
d8891fcc 287 frame = get_frame(func);
288 if (frame != NULL)
289 {
290 for (m = 0; m < (int)frame->memqty; m++)
291 {
292 ret = get_member_name(frame->members[m].id, buf, sizeof(buf));
293 if (ret <= 0) {
294 msg("%x: member has no name?\n", ea);
295 return;
296 }
297 if (buf[0] == ' ') // what's this?
298 continue;
299 if (IS_START(buf, "arg_") || IS_START(buf, "var_"))
300 continue;
301
1caf86bb 302 // check for dupe names
303 int m1, dupe = 0;
304 for (m1 = 0; m1 < m; m1++) {
305 get_member_name(frame->members[m1].id, buf2, sizeof(buf2));
306 if (stricmp(buf, buf2) == 0)
307 dupe = 1;
308 }
309
310 if (is_name_reserved(buf) || dupe) {
d8891fcc 311 msg("%x: renaming '%s'\n", ea, buf);
312 qstrncat(buf, "_", sizeof(buf));
313 ret = set_member_name(frame, frame->members[m].soff, buf);
314 if (!ret) {
315 msg("%x: renaming failed\n", ea);
316 return;
317 }
318 }
319
97609f07 320 p = buf;
321 pp = (char **)bsearch(&p, name_cache, name_cache_size,
322 sizeof(name_cache[0]), name_cache_cmp);
323 if (pp == NULL)
d8891fcc 324 continue;
325
97609f07 326 tmp_ea = get_name_ea(BADADDR, *pp);
327 msg("%x: renaming '%s' because of '%s' at %x\n",
328 tmp_ea, *pp, buf, ea);
329 my_rename(tmp_ea, *pp);
d8891fcc 330 }
331 }
332
a84bddd1 333 // detect tailcalls to next func with 'jmp $+5' (offset 0)
334 if (f_area.endEA - f_area.startEA >= 5
335 && decode_insn(f_area.endEA - 5) && cmd.itype == NN_jmp
336 && cmd.Operands[0].type == o_near
337 && cmd.Operands[0].addr == f_area.endEA
338 && get_name(BADADDR, f_area.endEA, buf, sizeof(buf))
339 && get_cmt(f_area.endEA - 5, false, buf2, sizeof(buf2)) <= 0)
340 {
341 qsnprintf(buf2, sizeof(buf2), "sctpatch: jmp %s", buf);
342 set_cmt(f_area.endEA - 5, buf2, false);
343 }
344
d8891fcc 345 func = get_next_func(ea);
15c7b2a4 346 }
347
b587e6ae 348 // 2nd pass over whole .text and .(ro)data segments
15c7b2a4 349 for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA))
350 {
351 segment_t *seg = getseg(ea);
b587e6ae 352 if (!seg)
353 break;
354 if (seg->type == SEG_XTRN)
355 continue;
356 if (seg->type != SEG_CODE && seg->type != SEG_DATA)
15c7b2a4 357 break;
358
b587e6ae 359 ea_flags = get_flags_novalue(ea);
15c7b2a4 360 func = get_func(ea);
361 if (isCode(ea_flags))
362 {
363 if (!decode_insn(ea)) {
364 msg("%x: decode_insn() failed\n", ea);
365 continue;
366 }
367
b587e6ae 368 // masm doesn't understand IDA's float/xmm types
369 if (cmd.itype == NN_fld || cmd.itype == NN_fst
370 || cmd.itype == NN_movapd || cmd.itype == NN_movlpd)
371 {
372 for (o = 0; o < UA_MAXOP; o++) {
373 if (cmd.Operands[o].type == o_void)
374 break;
375
376 if (cmd.Operands[o].type == o_mem) {
377 tmp_ea = cmd.Operands[o].addr;
11437ea1 378 flags_t tmp_flg = get_flags_novalue(tmp_ea);
379 buf[0] = 0;
380 if (isDouble(tmp_flg))
feb0ee5d 381 {
b587e6ae 382 get_name(ea, tmp_ea, buf, sizeof(buf));
11437ea1 383 msg("%x: converting dbl %x '%s'\n", ea, tmp_ea, buf);
384 doQwrd(tmp_ea, 8);
385 }
386 if (isOwrd(tmp_flg) || isYwrd(tmp_flg) || isTbyt(tmp_flg))
387 {
388 get_name(ea, tmp_ea, buf, sizeof(buf));
389 msg("%x: undefining lrg %x '%s'\n", ea, tmp_ea, buf);
b587e6ae 390 do_unknown(tmp_ea, DOUNK_EXPAND);
391 }
392 }
393 }
394 }
1caf86bb 395 else if (cmd.itype == NN_lea) {
fc1c61f5 396 // detect code alignment
1caf86bb 397 if (cmd.Operands[0].reg == cmd.Operands[1].reg
398 && cmd.Operands[1].type == o_displ
399 && cmd.Operands[1].addr == 0)
400 {
ee8e1bea 401 // lea eax, [eax+0]
402 make_align(ea);
1caf86bb 403 }
fc1c61f5 404 else if (!isDefArg1(ea_flags)
405 && cmd.Operands[1].type == o_mem // why o_mem?
406 && cmd.Operands[1].dtyp == dt_dword)
407 {
408 if (inf.minEA <= cmd.Operands[1].addr
409 && cmd.Operands[1].addr < inf.maxEA)
410 {
411 // lea to segments, like ds:58D6A8h[edx*8]
412 msg("%x: lea offset to %x\n", ea, cmd.Operands[1].addr);
413 op_offset(ea, 1, REF_OFF32);
414 }
415 else
416 {
417 // ds:0[eax*8] -> [eax*8+0]
418 msg("%x: dropping ds: for %x\n", ea, cmd.Operands[1].addr);
419 op_hex(ea, 1);
420 }
421 }
1caf86bb 422 }
ee8e1bea 423 else if (cmd.itype == NN_mov && cmd.segpref == 0x1e // 2e?
424 && cmd.Operands[0].type == o_reg
425 && cmd.Operands[1].type == o_reg
426 && cmd.Operands[0].dtyp == cmd.Operands[1].dtyp
427 && cmd.Operands[0].reg == cmd.Operands[1].reg)
428 {
429 // db 2Eh; mov eax, eax
430 make_align(ea);
431 }
b587e6ae 432
15c7b2a4 433 // find non-local branches
b587e6ae 434 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near)
15c7b2a4 435 {
436 target_ea = cmd.Operands[0].addr;
437 if (func == NULL)
438 nonlocal_add(target_ea);
439 else {
440 ret = get_func_chunknum(func, target_ea);
441 if (ret != 0) {
442 // a jump to another func or chunk
443 // check if it lands on func start
444 if (!isFunc(get_flags_novalue(target_ea)))
445 nonlocal_add(target_ea);
446 }
447 }
448 }
449 }
450 else { // not code
1caf86bb 451 int do_undef = 0;
452 ea_size = get_item_size(ea);
453
15c7b2a4 454 if (func == NULL && isOff0(ea_flags)) {
15c7b2a4 455 for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4)
456 nonlocal_add(get_long(ea + tmp_ea));
457 }
b587e6ae 458
459 // IDA vs masm float/mmx/xmm type incompatibility
11437ea1 460 if (isDouble(ea_flags))
461 {
462 msg("%x: converting double\n", ea);
463 doQwrd(ea, 8);
464 }
465 else if (isTbyt(ea_flags) || isPackReal(ea_flags))
b587e6ae 466 {
1caf86bb 467 do_undef = 1;
468 }
469 else if (isOwrd(ea_flags)) {
b587e6ae 470 buf[0] = 0;
471 get_name(BADADDR, ea, buf, sizeof(buf));
1caf86bb 472 if (IS_START(buf, "xmm"))
473 do_undef = 1;
474 }
475 // masm doesn't understand IDA's unicode
476 else if (isASCII(ea_flags) && ea_size >= 4
477 && (get_long(ea) & 0xff00ff00) == 0) // lame..
478 {
479 do_undef = 1;
480 }
481 // masm doesn't understand large aligns
e74324f9 482 else if (isAlign(ea_flags) && ea_size >= 0x10)
483 {
1caf86bb 484 msg("%x: undefining align %d\n", ea, ea_size);
b587e6ae 485 do_unknown(ea, DOUNK_EXPAND);
486 }
1caf86bb 487
488 if (do_undef) {
b587e6ae 489 buf[0] = 0;
490 get_name(BADADDR, ea, buf, sizeof(buf));
1caf86bb 491 msg("%x: undefining '%s'\n", ea, buf);
492 do_unknown(ea, DOUNK_EXPAND);
b587e6ae 493 }
494 }
495 }
496
e74324f9 497 // check namelist for reserved names and
498 // matching names with different case (nasm ignores case)
b587e6ae 499 n = get_nlist_size();
500 for (i = 0; i < n; i++) {
501 ea = get_nlist_ea(i);
502 name = get_nlist_name(i);
503 if (name == NULL) {
504 msg("%x: null name?\n", ea);
505 continue;
506 }
e74324f9 507 qsnprintf(buf, sizeof(buf), "%s", name);
508
509 int need_rename = is_name_reserved(name);
510 if (!need_rename) {
511 p = buf;
512 pp = (char **)bsearch(&p, name_cache, name_cache_size,
513 sizeof(name_cache[0]), name_cache_cmp);
514 if (pp != NULL) {
515 if (pp > name_cache && stricmp(pp[-1], pp[0]) == 0)
516 need_rename = 1;
517 else if (pp < name_cache + name_cache_size - 1
518 && stricmp(pp[0], pp[1]) == 0)
519 {
520 need_rename = 1;
521 }
522 }
523 }
b587e6ae 524
b25f320a 525 // rename vars with '?@' (funcs are ok)
526 int change_qat = 0;
527 ea_flags = get_flags_novalue(ea);
3a0df310 528 if (!isCode(ea_flags)) {
529 if (strchr(name, '?'))
530 change_qat = 1;
531 else if ((cp = strchr(name, '@'))) {
532 char *endp = NULL;
533 strtol(cp + 1, &endp, 10);
534 if (endp == NULL || *endp != 0)
535 change_qat = 1;
536 }
537 }
b25f320a 538
e74324f9 539 if (need_rename || change_qat) {
b587e6ae 540 msg("%x: renaming name '%s'\n", ea, name);
b25f320a 541
542 if (change_qat) {
543 for (p = buf; *p != 0; p++) {
544 if (*p == '?' || *p == '@') {
545 qsnprintf(buf2, sizeof(buf2), "%02x", (unsigned char)*p);
546 memmove(p + 1, p, strlen(p) + 1);
547 memcpy(p, buf2, 2);
548 }
549 }
550 }
551
97609f07 552 my_rename(ea, buf);
15c7b2a4 553 }
554 }
555
556 if (nonlocal_bt_cnt > 1) {
557 qsort(nonlocal_bt, nonlocal_bt_cnt,
558 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
d8891fcc 559 }
560
561 char *fname = askfile_c(1, NULL, "Save asm file");
562 if (fname == NULL)
563 return;
564 fout = qfopen(fname, "w");
565 if (fout == NULL) {
566 msg("couldn't open '%s'\n", fname);
567 return;
568 }
569
570 show_wait_box("Saving..");
571
572 // deal with the beginning
573 ea = inf.minEA;
574 int flags = 0; // calc_default_idaplace_flags();
575 linearray_t ln(&flags);
576 idaplace_t pl;
577 pl.ea = ea;
578 pl.lnnum = 0;
579 ln.set_place(&pl);
580 n = ln.get_linecnt();
581 for (i = 0; i < n - 1; i++) {
e74324f9 582 do_def_line(buf, sizeof(buf), ln.down(), ea, NULL);
d8891fcc 583 if (strstr(buf, "include"))
584 continue;
585
586 fout_line++;
587 qfprintf(fout, "%s\n", buf);
588 p = strstr(buf, ".mmx");
589 if (p != NULL) {
590 memcpy(p, ".xmm", 4);
591 fout_line++;
592 qfprintf(fout, "%s\n", buf);
1402b79d 593 continue;
594 }
595 p = strstr(buf, ".model");
596 if (p != NULL) {
597 qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf));
598 fout_line++;
599 qfprintf(fout, "\n%s\n", buf);
de8a204c 600 i++;
601 break;
d8891fcc 602 }
603 }
b587e6ae 604 pl.lnnum = i;
d8891fcc 605
606 for (;;)
607 {
94cd6e34 608 int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0;
ed6ebb48 609 int word_imm = 0, dword_imm = 0, do_pushf = 0, do_nops = 0;
15c7b2a4 610
d8891fcc 611 if ((ea >> 14) != ui_ea_block) {
612 ui_ea_block = ea >> 14;
613 showAddr(ea);
614 if (wasBreak())
615 break;
616 }
617
e74324f9 618 func = get_func(ea);
619
d8891fcc 620 segment_t *seg = getseg(ea);
b587e6ae 621 if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA))
d8891fcc 622 goto pass;
623
b587e6ae 624 ea_flags = get_flags_novalue(ea);
625 if (isCode(ea_flags))
626 {
627 if (!decode_insn(ea))
628 goto pass;
d8891fcc 629
1caf86bb 630 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near
631 && cmd.Operands[0].dtyp == dt_dword)
632 {
633 jmp_near = 1;
634 }
635 else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf)
636 && natop())
637 {
638 do_pushf = 1;
639 }
640
b587e6ae 641 for (o = 0; o < UA_MAXOP; o++) {
1caf86bb 642 const op_t &opr = cmd.Operands[o];
643 if (opr.type == o_void)
b587e6ae 644 break;
d8891fcc 645
1caf86bb 646 // correct?
647 if (opr.type == o_mem && opr.specval_shorts.high == 0x21)
b587e6ae 648 drop_large = 1;
1caf86bb 649 if (opr.hasSIB && x86_scale(opr) == 0
650 && x86_index(opr) != INDEX_NONE)
651 {
652 set_scale = 1;
653 }
654 // annoying alignment variant..
655 if (opr.type == o_imm && opr.dtyp == dt_dword
656 && (opr.value < 0x80 || opr.value > 0xffffff80)
657 && cmd.size >= opr.offb + 4)
658 {
659 if (get_long(ea + opr.offb) == opr.value)
660 dword_imm = 1;
661 }
662 else if (opr.type == o_imm && opr.dtyp == dt_word
663 && (opr.value < 0x80 || opr.value > 0xff80)
664 && cmd.size >= opr.offb + 2)
665 {
666 if (get_word(ea + opr.offb) == (ushort)opr.value)
667 word_imm = 1;
15c7b2a4 668 }
ed6ebb48 669 else if (opr.type == o_displ && opr.addr == 0
670 && opr.offb != 0 && opr.hasSIB && opr.sib == 0x24)
671 {
672 // uses [esp+0] with 0 encoded into op
673 do_nops++;
674 }
15c7b2a4 675 }
b587e6ae 676 }
677 else { // not code
678 if (isOff0(ea_flags))
94cd6e34 679 do_rva = 1;
d8891fcc 680 }
681
682pass:
b587e6ae 683 n = ln.get_linecnt();
684 for (i = pl.lnnum; i < n; i++) {
e74324f9 685 do_def_line(buf, sizeof(buf), ln.down(), ea, func);
15c7b2a4 686
1402b79d 687 char *fw;
688 for (fw = buf; *fw != 0 && *fw == ' '; )
689 fw++;
690
1caf86bb 691 // patches..
b587e6ae 692 if (drop_large) {
1402b79d 693 p = strstr(fw, "large ");
b587e6ae 694 if (p != NULL)
695 memmove(p, p + 6, strlen(p + 6) + 1);
696 }
94cd6e34 697 while (do_rva) {
1402b79d 698 p = strstr(fw, " rva ");
b587e6ae 699 if (p == NULL)
700 break;
94cd6e34 701 memmove(p + 4 + 3, p + 4, strlen(p + 4) + 1);
702 memcpy(p + 1, "offset", 6);
b587e6ae 703 }
1caf86bb 704 if (set_scale) {
1402b79d 705 p = strchr(fw, '[');
1caf86bb 706 if (p != NULL)
707 p = strchr(p, '+');
708 if (p != NULL && p[1] == 'e') {
709 p += 4;
710 // scale is 1, must specify it explicitly so that
711 // masm chooses the right scaled reg
712 memmove(p + 2, p, strlen(p) + 1);
713 memcpy(p, "*1", 2);
714 }
715 }
716 else if (jmp_near) {
9aaf1dcf 717 p = NULL;
718 if (fw != buf && fw[0] == 'j')
719 p = fw;
1caf86bb 720 while (p && *p != ' ')
721 p++;
722 while (p && *p == ' ')
723 p++;
724 if (p != NULL) {
725 memmove(p + 9, p, strlen(p) + 1);
726 memcpy(p, "near ptr ", 9);
9aaf1dcf 727 jmp_near = 0;
1caf86bb 728 }
729 }
730 if (word_imm) {
1402b79d 731 p = strstr(fw, ", ");
1caf86bb 732 if (p != NULL && '0' <= p[2] && p[2] <= '9') {
733 p += 2;
734 memmove(p + 9, p, strlen(p) + 1);
735 memcpy(p, "word ptr ", 9);
736 }
737 }
738 else if (dword_imm) {
1402b79d 739 p = strstr(fw, ", ");
1caf86bb 740 if (p != NULL && '0' <= p[2] && p[2] <= '9') {
741 p += 2;
742 memmove(p + 10, p, strlen(p) + 1);
743 memcpy(p, "dword ptr ", 10);
744 }
745 }
746 else if (do_pushf) {
1402b79d 747 p = strstr(fw, "pushf");
1caf86bb 748 if (p == NULL)
1402b79d 749 p = strstr(fw, "popf");
1caf86bb 750 if (p != NULL) {
751 p = strchr(p, 'f') + 1;
752 memmove(p + 1, p, strlen(p) + 1);
753 *p = 'd';
754 }
755 }
b587e6ae 756
de8a204c 757 if (fw[0] == 'a' && IS_START(fw, "assume cs")) {
758 // "assume cs" causes problems with ext syms
759 memmove(fw + 1, fw, strlen(fw) + 1);
760 *fw = ';';
761 }
762 else if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') {
1402b79d 763 fout_line++;
764 qfprintf(fout, "include public.inc\n\n");
765
766 // kill entry point
767 fw[3] = 0;
768 }
769
b587e6ae 770 fout_line++;
771 qfprintf(fout, "%s\n", buf);
772 }
d8891fcc 773
ed6ebb48 774 while (do_nops-- > 0)
775 qfprintf(fout, " nop ; adj\n");
776
15c7b2a4 777 // note: next_head skips some undefined stuff
d8891fcc 778 ea = next_not_tail(ea); // correct?
15c7b2a4 779 if (ea == BADADDR)
d8891fcc 780 break;
781
782 pl.ea = ea;
783 pl.lnnum = 0;
784 ln.set_place(&pl);
d8891fcc 785 }
786
787 if (fout != NULL)
788 qfclose(fout);
15c7b2a4 789 if (fname != NULL)
790 qfree(fname);
d8891fcc 791
792 hide_wait_box();
793 msg("%d lines saved.\n", fout_line);
794}
795
796//--------------------------------------------------------------------------
797
3682b4b1 798static const char comment[] = "Generate disassembly for nasm";
d8891fcc 799static const char help[] = "Generate asm file\n";
800static const char wanted_name[] = "Save asm";
3682b4b1 801static const char wanted_hotkey[] = "Shift-S";
d8891fcc 802
803//--------------------------------------------------------------------------
804//
805// PLUGIN DESCRIPTION BLOCK
806//
807//--------------------------------------------------------------------------
808plugin_t PLUGIN =
809{
810 IDP_INTERFACE_VERSION,
811 0, // plugin flags
812 init, // initialize
813 term, // terminate. this pointer may be NULL.
814 run, // invoke plugin
815 comment, // long comment about the plugin
816 // it could appear in the status line
817 // or as a hint
818 help, // multiline help about the plugin
819 wanted_name, // the preferred short name of the plugin
820 wanted_hotkey // the preferred hotkey to run the plugin
821};
822
823// vim:ts=2:shiftwidth=2:expandtab