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 |
26 | static char **name_cache; |
27 | static size_t name_cache_size; |
28 | |
15c7b2a4 |
29 | // non-local branch targets |
30 | static ea_t *nonlocal_bt; |
31 | static int nonlocal_bt_alloc; |
32 | static int nonlocal_bt_cnt; |
33 | |
d8891fcc |
34 | //-------------------------------------------------------------------------- |
35 | static int idaapi init(void) |
36 | { |
37 | return PLUGIN_OK; |
38 | } |
39 | |
40 | //-------------------------------------------------------------------------- |
41 | static 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 | |
62 | static const char *reserved_names[] = { |
63 | "name", |
b587e6ae |
64 | "type", |
d8891fcc |
65 | "offset", |
b587e6ae |
66 | "aam", |
1402b79d |
67 | "text", |
de8a204c |
68 | "size", |
69 | "c", |
97609f07 |
70 | "align", |
d8891fcc |
71 | }; |
72 | |
73 | static int is_name_reserved(const char *name) |
74 | { |
75 | int i; |
76 | for (i = 0; i < ARRAY_SIZE(reserved_names); i++) |
77 | if (strcasecmp(name, reserved_names[i]) == 0) |
78 | return 1; |
79 | |
80 | return 0; |
81 | } |
82 | |
15c7b2a4 |
83 | static int nonlocal_bt_cmp(const void *p1, const void *p2) |
84 | { |
85 | const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2; |
86 | return *e1 - *e2; |
87 | } |
88 | |
89 | static void nonlocal_add(ea_t ea) |
90 | { |
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) { |
96 | msg("OOM\n"); |
97 | return; |
98 | } |
99 | } |
100 | nonlocal_bt[nonlocal_bt_cnt++] = ea; |
101 | } |
102 | |
b587e6ae |
103 | // is instruction a (un)conditional jump (not call)? |
104 | static int is_insn_jmp(uint16 itype) |
105 | { |
106 | return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz); |
107 | } |
108 | |
d53d4cc7 |
109 | static void do_def_line(char *buf, size_t buf_size, const char *line, |
110 | ea_t ea) |
d8891fcc |
111 | { |
d53d4cc7 |
112 | ea_t *ea_ret; |
113 | char *p; |
d8891fcc |
114 | int len; |
115 | |
116 | tag_remove(line, buf, buf_size); // remove color codes |
117 | len = strlen(buf); |
118 | if (len < 9) { |
119 | buf[0] = 0; |
120 | return; |
121 | } |
122 | memmove(buf, buf + 9, len - 9 + 1); // rm address |
15c7b2a4 |
123 | |
d53d4cc7 |
124 | p = buf; |
125 | while (*p && *p != ' ' && *p != ':') |
126 | p++; |
127 | if (*p == ':') { |
128 | ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt, |
129 | sizeof(nonlocal_bt[0]), nonlocal_bt_cmp); |
130 | if (ea_ret != 0) { |
131 | if (p[1] != ' ') |
132 | msg("no trailing blank in '%s'\n", buf); |
133 | else |
134 | p[1] = ':'; |
15c7b2a4 |
135 | } |
136 | } |
d8891fcc |
137 | } |
138 | |
97609f07 |
139 | static int name_cache_cmp(const void *p1, const void *p2) |
140 | { |
141 | // masm ignores case, so do we |
142 | return stricmp(*(char * const *)p1, *(char * const *)p2); |
143 | } |
144 | |
145 | static void rebuild_name_cache(void) |
146 | { |
147 | size_t i, newsize; |
148 | void *tmp; |
149 | |
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])); |
154 | if (tmp == NULL) { |
155 | msg("OOM for name cache\n"); |
156 | return; |
157 | } |
158 | name_cache = (char **)tmp; |
159 | } |
160 | for (i = 0; i < name_cache_size; i++) |
161 | free(name_cache[i]); |
162 | for (i = 0; i < newsize; i++) |
163 | name_cache[i] = strdup(get_nlist_name(i)); |
164 | |
165 | name_cache_size = newsize; |
166 | qsort(name_cache, name_cache_size, sizeof(name_cache[0]), |
167 | name_cache_cmp); |
168 | } |
169 | |
170 | static void my_rename(ea_t ea, char *name) |
171 | { |
172 | char buf[256]; |
173 | char *p, **pp; |
174 | int n = 0; |
175 | |
176 | qsnprintf(buf, sizeof(buf), "%s", name); |
177 | do { |
178 | p = buf; |
179 | pp = (char **)bsearch(&p, name_cache, name_cache_size, |
180 | sizeof(name_cache[0]), name_cache_cmp); |
181 | if (pp == NULL) |
182 | break; |
183 | |
184 | qsnprintf(buf, sizeof(buf), "%s_g%d", name, n); |
185 | n++; |
186 | } |
187 | while (n < 100); |
188 | |
189 | if (n == 100) |
190 | msg("rename failure? '%s'\n", name); |
191 | |
192 | do_name_anyway(ea, buf); |
193 | rebuild_name_cache(); |
194 | } |
195 | |
ee8e1bea |
196 | static void make_align(ea_t ea) |
197 | { |
198 | ea_t tmp_ea; |
199 | int n; |
200 | |
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.. |
205 | n = 4; |
206 | msg("%x: align %d\n", ea, 1 << n); |
207 | do_unknown(ea, DOUNK_SIMPLE); |
208 | doAlign(ea, tmp_ea - ea, n); |
209 | } |
210 | } |
211 | |
d8891fcc |
212 | static void idaapi run(int /*arg*/) |
213 | { |
15c7b2a4 |
214 | // isEnabled(ea) // address belongs to disassembly |
d8891fcc |
215 | // ea_t ea = get_screen_ea(); |
ee8e1bea |
216 | // extern foo; |
15c7b2a4 |
217 | // foo = DecodeInstruction(ScreenEA()); |
d8891fcc |
218 | FILE *fout = NULL; |
219 | int fout_line = 0; |
220 | char buf[MAXSTR]; |
1caf86bb |
221 | char buf2[MAXSTR]; |
b587e6ae |
222 | const char *name; |
d8891fcc |
223 | struc_t *frame; |
224 | func_t *func; |
15c7b2a4 |
225 | ea_t ui_ea_block = 0, ea_size; |
226 | ea_t tmp_ea, target_ea; |
d8891fcc |
227 | ea_t ea; |
b587e6ae |
228 | flags_t ea_flags; |
1caf86bb |
229 | uval_t idx; |
d8891fcc |
230 | int i, o, m, n; |
231 | int ret; |
97609f07 |
232 | char **pp; |
d8891fcc |
233 | char *p; |
234 | |
15c7b2a4 |
235 | nonlocal_bt_cnt = 0; |
236 | |
1caf86bb |
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)); |
245 | del_struc(struc); |
246 | |
247 | idx = get_first_struc_idx(); |
248 | } |
249 | |
97609f07 |
250 | rebuild_name_cache(); |
251 | |
15c7b2a4 |
252 | // 1st pass: walk through all funcs |
97609f07 |
253 | ea = inf.minEA; |
254 | func = get_func(ea); |
d8891fcc |
255 | while (func != NULL) |
256 | { |
15c7b2a4 |
257 | func_tail_iterator_t fti(func); |
258 | if (!fti.main()) { |
259 | msg("%x: func_tail_iterator_t main failed\n", ea); |
260 | return; |
261 | } |
262 | const area_t &f_area = fti.chunk(); |
263 | ea = f_area.startEA; |
264 | |
265 | // rename global syms which conflict with frame member names |
d8891fcc |
266 | frame = get_frame(func); |
267 | if (frame != NULL) |
268 | { |
269 | for (m = 0; m < (int)frame->memqty; m++) |
270 | { |
271 | ret = get_member_name(frame->members[m].id, buf, sizeof(buf)); |
272 | if (ret <= 0) { |
273 | msg("%x: member has no name?\n", ea); |
274 | return; |
275 | } |
276 | if (buf[0] == ' ') // what's this? |
277 | continue; |
278 | if (IS_START(buf, "arg_") || IS_START(buf, "var_")) |
279 | continue; |
280 | |
1caf86bb |
281 | // check for dupe names |
282 | int m1, dupe = 0; |
283 | for (m1 = 0; m1 < m; m1++) { |
284 | get_member_name(frame->members[m1].id, buf2, sizeof(buf2)); |
285 | if (stricmp(buf, buf2) == 0) |
286 | dupe = 1; |
287 | } |
288 | |
289 | if (is_name_reserved(buf) || dupe) { |
d8891fcc |
290 | msg("%x: renaming '%s'\n", ea, buf); |
291 | qstrncat(buf, "_", sizeof(buf)); |
292 | ret = set_member_name(frame, frame->members[m].soff, buf); |
293 | if (!ret) { |
294 | msg("%x: renaming failed\n", ea); |
295 | return; |
296 | } |
297 | } |
298 | |
97609f07 |
299 | p = buf; |
300 | pp = (char **)bsearch(&p, name_cache, name_cache_size, |
301 | sizeof(name_cache[0]), name_cache_cmp); |
302 | if (pp == NULL) |
d8891fcc |
303 | continue; |
304 | |
97609f07 |
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); |
d8891fcc |
309 | } |
310 | } |
311 | |
a84bddd1 |
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) |
319 | { |
320 | qsnprintf(buf2, sizeof(buf2), "sctpatch: jmp %s", buf); |
321 | set_cmt(f_area.endEA - 5, buf2, false); |
322 | } |
323 | |
d8891fcc |
324 | func = get_next_func(ea); |
15c7b2a4 |
325 | } |
326 | |
b587e6ae |
327 | // 2nd pass over whole .text and .(ro)data segments |
15c7b2a4 |
328 | for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA)) |
329 | { |
330 | segment_t *seg = getseg(ea); |
b587e6ae |
331 | if (!seg) |
332 | break; |
333 | if (seg->type == SEG_XTRN) |
334 | continue; |
335 | if (seg->type != SEG_CODE && seg->type != SEG_DATA) |
15c7b2a4 |
336 | break; |
337 | |
b587e6ae |
338 | ea_flags = get_flags_novalue(ea); |
15c7b2a4 |
339 | func = get_func(ea); |
340 | if (isCode(ea_flags)) |
341 | { |
342 | if (!decode_insn(ea)) { |
343 | msg("%x: decode_insn() failed\n", ea); |
344 | continue; |
345 | } |
346 | |
b587e6ae |
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) |
350 | { |
351 | for (o = 0; o < UA_MAXOP; o++) { |
352 | if (cmd.Operands[o].type == o_void) |
353 | break; |
354 | |
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); |
feb0ee5d |
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)) |
361 | { |
b587e6ae |
362 | buf[0] = 0; |
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); |
366 | } |
367 | } |
368 | } |
369 | } |
1caf86bb |
370 | else if (cmd.itype == NN_lea) { |
fc1c61f5 |
371 | // detect code alignment |
1caf86bb |
372 | if (cmd.Operands[0].reg == cmd.Operands[1].reg |
373 | && cmd.Operands[1].type == o_displ |
374 | && cmd.Operands[1].addr == 0) |
375 | { |
ee8e1bea |
376 | // lea eax, [eax+0] |
377 | make_align(ea); |
1caf86bb |
378 | } |
fc1c61f5 |
379 | else if (!isDefArg1(ea_flags) |
380 | && cmd.Operands[1].type == o_mem // why o_mem? |
381 | && cmd.Operands[1].dtyp == dt_dword) |
382 | { |
383 | if (inf.minEA <= cmd.Operands[1].addr |
384 | && cmd.Operands[1].addr < inf.maxEA) |
385 | { |
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); |
389 | } |
390 | else |
391 | { |
392 | // ds:0[eax*8] -> [eax*8+0] |
393 | msg("%x: dropping ds: for %x\n", ea, cmd.Operands[1].addr); |
394 | op_hex(ea, 1); |
395 | } |
396 | } |
1caf86bb |
397 | } |
ee8e1bea |
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) |
403 | { |
404 | // db 2Eh; mov eax, eax |
405 | make_align(ea); |
406 | } |
b587e6ae |
407 | |
15c7b2a4 |
408 | // find non-local branches |
b587e6ae |
409 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near) |
15c7b2a4 |
410 | { |
411 | target_ea = cmd.Operands[0].addr; |
412 | if (func == NULL) |
413 | nonlocal_add(target_ea); |
414 | else { |
415 | ret = get_func_chunknum(func, target_ea); |
416 | if (ret != 0) { |
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); |
421 | } |
422 | } |
423 | } |
424 | } |
425 | else { // not code |
1caf86bb |
426 | int do_undef = 0; |
427 | ea_size = get_item_size(ea); |
428 | |
15c7b2a4 |
429 | if (func == NULL && isOff0(ea_flags)) { |
15c7b2a4 |
430 | for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4) |
431 | nonlocal_add(get_long(ea + tmp_ea)); |
432 | } |
b587e6ae |
433 | |
434 | // IDA vs masm float/mmx/xmm type incompatibility |
435 | if (isDouble(ea_flags) || isTbyt(ea_flags) |
436 | || isPackReal(ea_flags)) |
437 | { |
1caf86bb |
438 | do_undef = 1; |
439 | } |
440 | else if (isOwrd(ea_flags)) { |
b587e6ae |
441 | buf[0] = 0; |
442 | get_name(BADADDR, ea, buf, sizeof(buf)); |
1caf86bb |
443 | if (IS_START(buf, "xmm")) |
444 | do_undef = 1; |
445 | } |
446 | // masm doesn't understand IDA's unicode |
447 | else if (isASCII(ea_flags) && ea_size >= 4 |
448 | && (get_long(ea) & 0xff00ff00) == 0) // lame.. |
449 | { |
450 | do_undef = 1; |
451 | } |
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); |
b587e6ae |
455 | do_unknown(ea, DOUNK_EXPAND); |
456 | } |
1caf86bb |
457 | |
458 | if (do_undef) { |
b587e6ae |
459 | buf[0] = 0; |
460 | get_name(BADADDR, ea, buf, sizeof(buf)); |
1caf86bb |
461 | msg("%x: undefining '%s'\n", ea, buf); |
462 | do_unknown(ea, DOUNK_EXPAND); |
b587e6ae |
463 | } |
464 | } |
465 | } |
466 | |
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); |
472 | if (name == NULL) { |
473 | msg("%x: null name?\n", ea); |
474 | continue; |
475 | } |
476 | |
b25f320a |
477 | // rename vars with '?@' (funcs are ok) |
478 | int change_qat = 0; |
479 | ea_flags = get_flags_novalue(ea); |
480 | if (!isCode(ea_flags) && strpbrk(name, "?@")) |
481 | change_qat = 1; |
482 | |
483 | if (change_qat || is_name_reserved(name)) { |
b587e6ae |
484 | msg("%x: renaming name '%s'\n", ea, name); |
97609f07 |
485 | qsnprintf(buf, sizeof(buf), "%s", name); |
b25f320a |
486 | |
487 | if (change_qat) { |
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); |
492 | memcpy(p, buf2, 2); |
493 | } |
494 | } |
495 | } |
496 | |
97609f07 |
497 | my_rename(ea, buf); |
15c7b2a4 |
498 | } |
499 | } |
500 | |
501 | if (nonlocal_bt_cnt > 1) { |
502 | qsort(nonlocal_bt, nonlocal_bt_cnt, |
503 | sizeof(nonlocal_bt[0]), nonlocal_bt_cmp); |
d8891fcc |
504 | } |
505 | |
506 | char *fname = askfile_c(1, NULL, "Save asm file"); |
507 | if (fname == NULL) |
508 | return; |
509 | fout = qfopen(fname, "w"); |
510 | if (fout == NULL) { |
511 | msg("couldn't open '%s'\n", fname); |
512 | return; |
513 | } |
514 | |
515 | show_wait_box("Saving.."); |
516 | |
517 | // deal with the beginning |
518 | ea = inf.minEA; |
519 | int flags = 0; // calc_default_idaplace_flags(); |
520 | linearray_t ln(&flags); |
521 | idaplace_t pl; |
522 | pl.ea = ea; |
523 | pl.lnnum = 0; |
524 | ln.set_place(&pl); |
525 | n = ln.get_linecnt(); |
526 | for (i = 0; i < n - 1; i++) { |
d53d4cc7 |
527 | do_def_line(buf, sizeof(buf), ln.down(), ea); |
d8891fcc |
528 | if (strstr(buf, "include")) |
529 | continue; |
530 | |
531 | fout_line++; |
532 | qfprintf(fout, "%s\n", buf); |
533 | p = strstr(buf, ".mmx"); |
534 | if (p != NULL) { |
535 | memcpy(p, ".xmm", 4); |
536 | fout_line++; |
537 | qfprintf(fout, "%s\n", buf); |
1402b79d |
538 | continue; |
539 | } |
540 | p = strstr(buf, ".model"); |
541 | if (p != NULL) { |
542 | qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf)); |
543 | fout_line++; |
544 | qfprintf(fout, "\n%s\n", buf); |
de8a204c |
545 | i++; |
546 | break; |
d8891fcc |
547 | } |
548 | } |
b587e6ae |
549 | pl.lnnum = i; |
d8891fcc |
550 | |
551 | for (;;) |
552 | { |
94cd6e34 |
553 | int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0; |
ed6ebb48 |
554 | int word_imm = 0, dword_imm = 0, do_pushf = 0, do_nops = 0; |
15c7b2a4 |
555 | |
d8891fcc |
556 | if ((ea >> 14) != ui_ea_block) { |
557 | ui_ea_block = ea >> 14; |
558 | showAddr(ea); |
559 | if (wasBreak()) |
560 | break; |
561 | } |
562 | |
563 | segment_t *seg = getseg(ea); |
b587e6ae |
564 | if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA)) |
d8891fcc |
565 | goto pass; |
566 | |
b587e6ae |
567 | ea_flags = get_flags_novalue(ea); |
568 | if (isCode(ea_flags)) |
569 | { |
570 | if (!decode_insn(ea)) |
571 | goto pass; |
d8891fcc |
572 | |
1caf86bb |
573 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near |
574 | && cmd.Operands[0].dtyp == dt_dword) |
575 | { |
576 | jmp_near = 1; |
577 | } |
578 | else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf) |
579 | && natop()) |
580 | { |
581 | do_pushf = 1; |
582 | } |
583 | |
b587e6ae |
584 | for (o = 0; o < UA_MAXOP; o++) { |
1caf86bb |
585 | const op_t &opr = cmd.Operands[o]; |
586 | if (opr.type == o_void) |
b587e6ae |
587 | break; |
d8891fcc |
588 | |
1caf86bb |
589 | // correct? |
590 | if (opr.type == o_mem && opr.specval_shorts.high == 0x21) |
b587e6ae |
591 | drop_large = 1; |
1caf86bb |
592 | if (opr.hasSIB && x86_scale(opr) == 0 |
593 | && x86_index(opr) != INDEX_NONE) |
594 | { |
595 | set_scale = 1; |
596 | } |
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) |
601 | { |
602 | if (get_long(ea + opr.offb) == opr.value) |
603 | dword_imm = 1; |
604 | } |
605 | else if (opr.type == o_imm && opr.dtyp == dt_word |
606 | && (opr.value < 0x80 || opr.value > 0xff80) |
607 | && cmd.size >= opr.offb + 2) |
608 | { |
609 | if (get_word(ea + opr.offb) == (ushort)opr.value) |
610 | word_imm = 1; |
15c7b2a4 |
611 | } |
ed6ebb48 |
612 | else if (opr.type == o_displ && opr.addr == 0 |
613 | && opr.offb != 0 && opr.hasSIB && opr.sib == 0x24) |
614 | { |
615 | // uses [esp+0] with 0 encoded into op |
616 | do_nops++; |
617 | } |
15c7b2a4 |
618 | } |
b587e6ae |
619 | } |
620 | else { // not code |
621 | if (isOff0(ea_flags)) |
94cd6e34 |
622 | do_rva = 1; |
d8891fcc |
623 | } |
624 | |
625 | pass: |
b587e6ae |
626 | n = ln.get_linecnt(); |
627 | for (i = pl.lnnum; i < n; i++) { |
d53d4cc7 |
628 | do_def_line(buf, sizeof(buf), ln.down(), ea); |
15c7b2a4 |
629 | |
1402b79d |
630 | char *fw; |
631 | for (fw = buf; *fw != 0 && *fw == ' '; ) |
632 | fw++; |
633 | |
1caf86bb |
634 | // patches.. |
b587e6ae |
635 | if (drop_large) { |
1402b79d |
636 | p = strstr(fw, "large "); |
b587e6ae |
637 | if (p != NULL) |
638 | memmove(p, p + 6, strlen(p + 6) + 1); |
639 | } |
94cd6e34 |
640 | while (do_rva) { |
1402b79d |
641 | p = strstr(fw, " rva "); |
b587e6ae |
642 | if (p == NULL) |
643 | break; |
94cd6e34 |
644 | memmove(p + 4 + 3, p + 4, strlen(p + 4) + 1); |
645 | memcpy(p + 1, "offset", 6); |
b587e6ae |
646 | } |
1caf86bb |
647 | if (set_scale) { |
1402b79d |
648 | p = strchr(fw, '['); |
1caf86bb |
649 | if (p != NULL) |
650 | p = strchr(p, '+'); |
651 | if (p != NULL && p[1] == 'e') { |
652 | p += 4; |
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); |
656 | memcpy(p, "*1", 2); |
657 | } |
658 | } |
659 | else if (jmp_near) { |
9aaf1dcf |
660 | p = NULL; |
661 | if (fw != buf && fw[0] == 'j') |
662 | p = fw; |
1caf86bb |
663 | while (p && *p != ' ') |
664 | p++; |
665 | while (p && *p == ' ') |
666 | p++; |
667 | if (p != NULL) { |
668 | memmove(p + 9, p, strlen(p) + 1); |
669 | memcpy(p, "near ptr ", 9); |
9aaf1dcf |
670 | jmp_near = 0; |
1caf86bb |
671 | } |
672 | } |
673 | if (word_imm) { |
1402b79d |
674 | p = strstr(fw, ", "); |
1caf86bb |
675 | if (p != NULL && '0' <= p[2] && p[2] <= '9') { |
676 | p += 2; |
677 | memmove(p + 9, p, strlen(p) + 1); |
678 | memcpy(p, "word ptr ", 9); |
679 | } |
680 | } |
681 | else if (dword_imm) { |
1402b79d |
682 | p = strstr(fw, ", "); |
1caf86bb |
683 | if (p != NULL && '0' <= p[2] && p[2] <= '9') { |
684 | p += 2; |
685 | memmove(p + 10, p, strlen(p) + 1); |
686 | memcpy(p, "dword ptr ", 10); |
687 | } |
688 | } |
689 | else if (do_pushf) { |
1402b79d |
690 | p = strstr(fw, "pushf"); |
1caf86bb |
691 | if (p == NULL) |
1402b79d |
692 | p = strstr(fw, "popf"); |
1caf86bb |
693 | if (p != NULL) { |
694 | p = strchr(p, 'f') + 1; |
695 | memmove(p + 1, p, strlen(p) + 1); |
696 | *p = 'd'; |
697 | } |
698 | } |
b587e6ae |
699 | |
de8a204c |
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); |
703 | *fw = ';'; |
704 | } |
705 | else if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') { |
1402b79d |
706 | fout_line++; |
707 | qfprintf(fout, "include public.inc\n\n"); |
708 | |
709 | // kill entry point |
710 | fw[3] = 0; |
711 | } |
712 | |
b587e6ae |
713 | fout_line++; |
714 | qfprintf(fout, "%s\n", buf); |
715 | } |
d8891fcc |
716 | |
ed6ebb48 |
717 | while (do_nops-- > 0) |
718 | qfprintf(fout, " nop ; adj\n"); |
719 | |
15c7b2a4 |
720 | // note: next_head skips some undefined stuff |
d8891fcc |
721 | ea = next_not_tail(ea); // correct? |
15c7b2a4 |
722 | if (ea == BADADDR) |
d8891fcc |
723 | break; |
724 | |
725 | pl.ea = ea; |
726 | pl.lnnum = 0; |
727 | ln.set_place(&pl); |
d8891fcc |
728 | } |
729 | |
730 | if (fout != NULL) |
731 | qfclose(fout); |
15c7b2a4 |
732 | if (fname != NULL) |
733 | qfree(fname); |
d8891fcc |
734 | |
735 | hide_wait_box(); |
736 | msg("%d lines saved.\n", fout_line); |
737 | } |
738 | |
739 | //-------------------------------------------------------------------------- |
740 | |
3682b4b1 |
741 | static const char comment[] = "Generate disassembly for nasm"; |
d8891fcc |
742 | static const char help[] = "Generate asm file\n"; |
743 | static const char wanted_name[] = "Save asm"; |
3682b4b1 |
744 | static const char wanted_hotkey[] = "Shift-S"; |
d8891fcc |
745 | |
746 | //-------------------------------------------------------------------------- |
747 | // |
748 | // PLUGIN DESCRIPTION BLOCK |
749 | // |
750 | //-------------------------------------------------------------------------- |
751 | plugin_t PLUGIN = |
752 | { |
753 | IDP_INTERFACE_VERSION, |
754 | 0, // plugin flags |
755 | init, // initialize |
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 |
760 | // or as a hint |
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 |
764 | }; |
765 | |
766 | // vim:ts=2:shiftwidth=2:expandtab |