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", |
e74324f9 |
67 | "aas", |
1402b79d |
68 | "text", |
de8a204c |
69 | "size", |
70 | "c", |
97609f07 |
71 | "align", |
e74324f9 |
72 | "addr", |
d8891fcc |
73 | }; |
74 | |
75 | static 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 |
85 | static 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 | |
91 | static 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)? |
106 | static int is_insn_jmp(uint16 itype) |
107 | { |
108 | return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz); |
109 | } |
110 | |
d53d4cc7 |
111 | static 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 |
159 | static 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 | |
165 | static 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 | |
190 | static 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 |
216 | static 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 |
232 | static 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; |
d8891fcc |
243 | struc_t *frame; |
244 | func_t *func; |
15c7b2a4 |
245 | ea_t ui_ea_block = 0, ea_size; |
246 | ea_t tmp_ea, target_ea; |
d8891fcc |
247 | ea_t ea; |
b587e6ae |
248 | flags_t ea_flags; |
1caf86bb |
249 | uval_t idx; |
d8891fcc |
250 | int i, o, m, n; |
251 | int ret; |
97609f07 |
252 | char **pp; |
d8891fcc |
253 | char *p; |
254 | |
15c7b2a4 |
255 | nonlocal_bt_cnt = 0; |
256 | |
1caf86bb |
257 | // get rid of structs, masm doesn't understand them |
258 | idx = get_first_struc_idx(); |
259 | while (idx != BADNODE) { |
260 | tid_t tid = get_struc_by_idx(idx); |
261 | struc_t *struc = get_struc(tid); |
262 | get_struc_name(tid, buf, sizeof(buf)); |
263 | msg("removing struct '%s'\n", buf); |
264 | //del_struc_members(struc, 0, get_max_offset(struc)); |
265 | del_struc(struc); |
266 | |
267 | idx = get_first_struc_idx(); |
268 | } |
269 | |
97609f07 |
270 | rebuild_name_cache(); |
271 | |
15c7b2a4 |
272 | // 1st pass: walk through all funcs |
97609f07 |
273 | ea = inf.minEA; |
274 | func = get_func(ea); |
d8891fcc |
275 | while (func != NULL) |
276 | { |
15c7b2a4 |
277 | func_tail_iterator_t fti(func); |
278 | if (!fti.main()) { |
279 | msg("%x: func_tail_iterator_t main failed\n", ea); |
280 | return; |
281 | } |
282 | const area_t &f_area = fti.chunk(); |
283 | ea = f_area.startEA; |
284 | |
285 | // rename global syms which conflict with frame member names |
d8891fcc |
286 | frame = get_frame(func); |
287 | if (frame != NULL) |
288 | { |
289 | for (m = 0; m < (int)frame->memqty; m++) |
290 | { |
291 | ret = get_member_name(frame->members[m].id, buf, sizeof(buf)); |
292 | if (ret <= 0) { |
293 | msg("%x: member has no name?\n", ea); |
294 | return; |
295 | } |
296 | if (buf[0] == ' ') // what's this? |
297 | continue; |
298 | if (IS_START(buf, "arg_") || IS_START(buf, "var_")) |
299 | continue; |
300 | |
1caf86bb |
301 | // check for dupe names |
302 | int m1, dupe = 0; |
303 | for (m1 = 0; m1 < m; m1++) { |
304 | get_member_name(frame->members[m1].id, buf2, sizeof(buf2)); |
305 | if (stricmp(buf, buf2) == 0) |
306 | dupe = 1; |
307 | } |
308 | |
309 | if (is_name_reserved(buf) || dupe) { |
d8891fcc |
310 | msg("%x: renaming '%s'\n", ea, buf); |
311 | qstrncat(buf, "_", sizeof(buf)); |
312 | ret = set_member_name(frame, frame->members[m].soff, buf); |
313 | if (!ret) { |
314 | msg("%x: renaming failed\n", ea); |
315 | return; |
316 | } |
317 | } |
318 | |
97609f07 |
319 | p = buf; |
320 | pp = (char **)bsearch(&p, name_cache, name_cache_size, |
321 | sizeof(name_cache[0]), name_cache_cmp); |
322 | if (pp == NULL) |
d8891fcc |
323 | continue; |
324 | |
97609f07 |
325 | tmp_ea = get_name_ea(BADADDR, *pp); |
326 | msg("%x: renaming '%s' because of '%s' at %x\n", |
327 | tmp_ea, *pp, buf, ea); |
328 | my_rename(tmp_ea, *pp); |
d8891fcc |
329 | } |
330 | } |
331 | |
a84bddd1 |
332 | // detect tailcalls to next func with 'jmp $+5' (offset 0) |
333 | if (f_area.endEA - f_area.startEA >= 5 |
334 | && decode_insn(f_area.endEA - 5) && cmd.itype == NN_jmp |
335 | && cmd.Operands[0].type == o_near |
336 | && cmd.Operands[0].addr == f_area.endEA |
337 | && get_name(BADADDR, f_area.endEA, buf, sizeof(buf)) |
338 | && get_cmt(f_area.endEA - 5, false, buf2, sizeof(buf2)) <= 0) |
339 | { |
340 | qsnprintf(buf2, sizeof(buf2), "sctpatch: jmp %s", buf); |
341 | set_cmt(f_area.endEA - 5, buf2, false); |
342 | } |
343 | |
d8891fcc |
344 | func = get_next_func(ea); |
15c7b2a4 |
345 | } |
346 | |
b587e6ae |
347 | // 2nd pass over whole .text and .(ro)data segments |
15c7b2a4 |
348 | for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA)) |
349 | { |
350 | segment_t *seg = getseg(ea); |
b587e6ae |
351 | if (!seg) |
352 | break; |
353 | if (seg->type == SEG_XTRN) |
354 | continue; |
355 | if (seg->type != SEG_CODE && seg->type != SEG_DATA) |
15c7b2a4 |
356 | break; |
357 | |
b587e6ae |
358 | ea_flags = get_flags_novalue(ea); |
15c7b2a4 |
359 | func = get_func(ea); |
360 | if (isCode(ea_flags)) |
361 | { |
362 | if (!decode_insn(ea)) { |
363 | msg("%x: decode_insn() failed\n", ea); |
364 | continue; |
365 | } |
366 | |
b587e6ae |
367 | // masm doesn't understand IDA's float/xmm types |
368 | if (cmd.itype == NN_fld || cmd.itype == NN_fst |
369 | || cmd.itype == NN_movapd || cmd.itype == NN_movlpd) |
370 | { |
371 | for (o = 0; o < UA_MAXOP; o++) { |
372 | if (cmd.Operands[o].type == o_void) |
373 | break; |
374 | |
375 | if (cmd.Operands[o].type == o_mem) { |
376 | tmp_ea = cmd.Operands[o].addr; |
377 | flags_t tmp_ea_flags = get_flags_novalue(tmp_ea); |
feb0ee5d |
378 | // ..but base float is ok.. |
379 | int is_flt = isDwrd(tmp_ea_flags) || isFloat(tmp_ea_flags); |
380 | if (!is_flt && !isUnknown(tmp_ea_flags)) |
381 | { |
b587e6ae |
382 | buf[0] = 0; |
383 | get_name(ea, tmp_ea, buf, sizeof(buf)); |
384 | msg("%x: undefining %x '%s'\n", ea, tmp_ea, buf); |
385 | do_unknown(tmp_ea, DOUNK_EXPAND); |
386 | } |
387 | } |
388 | } |
389 | } |
1caf86bb |
390 | else if (cmd.itype == NN_lea) { |
fc1c61f5 |
391 | // detect code alignment |
1caf86bb |
392 | if (cmd.Operands[0].reg == cmd.Operands[1].reg |
393 | && cmd.Operands[1].type == o_displ |
394 | && cmd.Operands[1].addr == 0) |
395 | { |
ee8e1bea |
396 | // lea eax, [eax+0] |
397 | make_align(ea); |
1caf86bb |
398 | } |
fc1c61f5 |
399 | else if (!isDefArg1(ea_flags) |
400 | && cmd.Operands[1].type == o_mem // why o_mem? |
401 | && cmd.Operands[1].dtyp == dt_dword) |
402 | { |
403 | if (inf.minEA <= cmd.Operands[1].addr |
404 | && cmd.Operands[1].addr < inf.maxEA) |
405 | { |
406 | // lea to segments, like ds:58D6A8h[edx*8] |
407 | msg("%x: lea offset to %x\n", ea, cmd.Operands[1].addr); |
408 | op_offset(ea, 1, REF_OFF32); |
409 | } |
410 | else |
411 | { |
412 | // ds:0[eax*8] -> [eax*8+0] |
413 | msg("%x: dropping ds: for %x\n", ea, cmd.Operands[1].addr); |
414 | op_hex(ea, 1); |
415 | } |
416 | } |
1caf86bb |
417 | } |
ee8e1bea |
418 | else if (cmd.itype == NN_mov && cmd.segpref == 0x1e // 2e? |
419 | && cmd.Operands[0].type == o_reg |
420 | && cmd.Operands[1].type == o_reg |
421 | && cmd.Operands[0].dtyp == cmd.Operands[1].dtyp |
422 | && cmd.Operands[0].reg == cmd.Operands[1].reg) |
423 | { |
424 | // db 2Eh; mov eax, eax |
425 | make_align(ea); |
426 | } |
b587e6ae |
427 | |
15c7b2a4 |
428 | // find non-local branches |
b587e6ae |
429 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near) |
15c7b2a4 |
430 | { |
431 | target_ea = cmd.Operands[0].addr; |
432 | if (func == NULL) |
433 | nonlocal_add(target_ea); |
434 | else { |
435 | ret = get_func_chunknum(func, target_ea); |
436 | if (ret != 0) { |
437 | // a jump to another func or chunk |
438 | // check if it lands on func start |
439 | if (!isFunc(get_flags_novalue(target_ea))) |
440 | nonlocal_add(target_ea); |
441 | } |
442 | } |
443 | } |
444 | } |
445 | else { // not code |
1caf86bb |
446 | int do_undef = 0; |
447 | ea_size = get_item_size(ea); |
448 | |
15c7b2a4 |
449 | if (func == NULL && isOff0(ea_flags)) { |
15c7b2a4 |
450 | for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4) |
451 | nonlocal_add(get_long(ea + tmp_ea)); |
452 | } |
b587e6ae |
453 | |
454 | // IDA vs masm float/mmx/xmm type incompatibility |
455 | if (isDouble(ea_flags) || isTbyt(ea_flags) |
456 | || isPackReal(ea_flags)) |
457 | { |
1caf86bb |
458 | do_undef = 1; |
459 | } |
460 | else if (isOwrd(ea_flags)) { |
b587e6ae |
461 | buf[0] = 0; |
462 | get_name(BADADDR, ea, buf, sizeof(buf)); |
1caf86bb |
463 | if (IS_START(buf, "xmm")) |
464 | do_undef = 1; |
465 | } |
466 | // masm doesn't understand IDA's unicode |
467 | else if (isASCII(ea_flags) && ea_size >= 4 |
468 | && (get_long(ea) & 0xff00ff00) == 0) // lame.. |
469 | { |
470 | do_undef = 1; |
471 | } |
472 | // masm doesn't understand large aligns |
e74324f9 |
473 | else if (isAlign(ea_flags) && ea_size >= 0x10) |
474 | { |
1caf86bb |
475 | msg("%x: undefining align %d\n", ea, ea_size); |
b587e6ae |
476 | do_unknown(ea, DOUNK_EXPAND); |
477 | } |
1caf86bb |
478 | |
479 | if (do_undef) { |
b587e6ae |
480 | buf[0] = 0; |
481 | get_name(BADADDR, ea, buf, sizeof(buf)); |
1caf86bb |
482 | msg("%x: undefining '%s'\n", ea, buf); |
483 | do_unknown(ea, DOUNK_EXPAND); |
b587e6ae |
484 | } |
485 | } |
486 | } |
487 | |
e74324f9 |
488 | // check namelist for reserved names and |
489 | // matching names with different case (nasm ignores case) |
b587e6ae |
490 | n = get_nlist_size(); |
491 | for (i = 0; i < n; i++) { |
492 | ea = get_nlist_ea(i); |
493 | name = get_nlist_name(i); |
494 | if (name == NULL) { |
495 | msg("%x: null name?\n", ea); |
496 | continue; |
497 | } |
e74324f9 |
498 | qsnprintf(buf, sizeof(buf), "%s", name); |
499 | |
500 | int need_rename = is_name_reserved(name); |
501 | if (!need_rename) { |
502 | p = buf; |
503 | pp = (char **)bsearch(&p, name_cache, name_cache_size, |
504 | sizeof(name_cache[0]), name_cache_cmp); |
505 | if (pp != NULL) { |
506 | if (pp > name_cache && stricmp(pp[-1], pp[0]) == 0) |
507 | need_rename = 1; |
508 | else if (pp < name_cache + name_cache_size - 1 |
509 | && stricmp(pp[0], pp[1]) == 0) |
510 | { |
511 | need_rename = 1; |
512 | } |
513 | } |
514 | } |
b587e6ae |
515 | |
b25f320a |
516 | // rename vars with '?@' (funcs are ok) |
517 | int change_qat = 0; |
518 | ea_flags = get_flags_novalue(ea); |
519 | if (!isCode(ea_flags) && strpbrk(name, "?@")) |
520 | change_qat = 1; |
521 | |
e74324f9 |
522 | if (need_rename || change_qat) { |
b587e6ae |
523 | msg("%x: renaming name '%s'\n", ea, name); |
b25f320a |
524 | |
525 | if (change_qat) { |
526 | for (p = buf; *p != 0; p++) { |
527 | if (*p == '?' || *p == '@') { |
528 | qsnprintf(buf2, sizeof(buf2), "%02x", (unsigned char)*p); |
529 | memmove(p + 1, p, strlen(p) + 1); |
530 | memcpy(p, buf2, 2); |
531 | } |
532 | } |
533 | } |
534 | |
97609f07 |
535 | my_rename(ea, buf); |
15c7b2a4 |
536 | } |
537 | } |
538 | |
539 | if (nonlocal_bt_cnt > 1) { |
540 | qsort(nonlocal_bt, nonlocal_bt_cnt, |
541 | sizeof(nonlocal_bt[0]), nonlocal_bt_cmp); |
d8891fcc |
542 | } |
543 | |
544 | char *fname = askfile_c(1, NULL, "Save asm file"); |
545 | if (fname == NULL) |
546 | return; |
547 | fout = qfopen(fname, "w"); |
548 | if (fout == NULL) { |
549 | msg("couldn't open '%s'\n", fname); |
550 | return; |
551 | } |
552 | |
553 | show_wait_box("Saving.."); |
554 | |
555 | // deal with the beginning |
556 | ea = inf.minEA; |
557 | int flags = 0; // calc_default_idaplace_flags(); |
558 | linearray_t ln(&flags); |
559 | idaplace_t pl; |
560 | pl.ea = ea; |
561 | pl.lnnum = 0; |
562 | ln.set_place(&pl); |
563 | n = ln.get_linecnt(); |
564 | for (i = 0; i < n - 1; i++) { |
e74324f9 |
565 | do_def_line(buf, sizeof(buf), ln.down(), ea, NULL); |
d8891fcc |
566 | if (strstr(buf, "include")) |
567 | continue; |
568 | |
569 | fout_line++; |
570 | qfprintf(fout, "%s\n", buf); |
571 | p = strstr(buf, ".mmx"); |
572 | if (p != NULL) { |
573 | memcpy(p, ".xmm", 4); |
574 | fout_line++; |
575 | qfprintf(fout, "%s\n", buf); |
1402b79d |
576 | continue; |
577 | } |
578 | p = strstr(buf, ".model"); |
579 | if (p != NULL) { |
580 | qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf)); |
581 | fout_line++; |
582 | qfprintf(fout, "\n%s\n", buf); |
de8a204c |
583 | i++; |
584 | break; |
d8891fcc |
585 | } |
586 | } |
b587e6ae |
587 | pl.lnnum = i; |
d8891fcc |
588 | |
589 | for (;;) |
590 | { |
94cd6e34 |
591 | int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0; |
ed6ebb48 |
592 | int word_imm = 0, dword_imm = 0, do_pushf = 0, do_nops = 0; |
15c7b2a4 |
593 | |
d8891fcc |
594 | if ((ea >> 14) != ui_ea_block) { |
595 | ui_ea_block = ea >> 14; |
596 | showAddr(ea); |
597 | if (wasBreak()) |
598 | break; |
599 | } |
600 | |
e74324f9 |
601 | func = get_func(ea); |
602 | |
d8891fcc |
603 | segment_t *seg = getseg(ea); |
b587e6ae |
604 | if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA)) |
d8891fcc |
605 | goto pass; |
606 | |
b587e6ae |
607 | ea_flags = get_flags_novalue(ea); |
608 | if (isCode(ea_flags)) |
609 | { |
610 | if (!decode_insn(ea)) |
611 | goto pass; |
d8891fcc |
612 | |
1caf86bb |
613 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near |
614 | && cmd.Operands[0].dtyp == dt_dword) |
615 | { |
616 | jmp_near = 1; |
617 | } |
618 | else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf) |
619 | && natop()) |
620 | { |
621 | do_pushf = 1; |
622 | } |
623 | |
b587e6ae |
624 | for (o = 0; o < UA_MAXOP; o++) { |
1caf86bb |
625 | const op_t &opr = cmd.Operands[o]; |
626 | if (opr.type == o_void) |
b587e6ae |
627 | break; |
d8891fcc |
628 | |
1caf86bb |
629 | // correct? |
630 | if (opr.type == o_mem && opr.specval_shorts.high == 0x21) |
b587e6ae |
631 | drop_large = 1; |
1caf86bb |
632 | if (opr.hasSIB && x86_scale(opr) == 0 |
633 | && x86_index(opr) != INDEX_NONE) |
634 | { |
635 | set_scale = 1; |
636 | } |
637 | // annoying alignment variant.. |
638 | if (opr.type == o_imm && opr.dtyp == dt_dword |
639 | && (opr.value < 0x80 || opr.value > 0xffffff80) |
640 | && cmd.size >= opr.offb + 4) |
641 | { |
642 | if (get_long(ea + opr.offb) == opr.value) |
643 | dword_imm = 1; |
644 | } |
645 | else if (opr.type == o_imm && opr.dtyp == dt_word |
646 | && (opr.value < 0x80 || opr.value > 0xff80) |
647 | && cmd.size >= opr.offb + 2) |
648 | { |
649 | if (get_word(ea + opr.offb) == (ushort)opr.value) |
650 | word_imm = 1; |
15c7b2a4 |
651 | } |
ed6ebb48 |
652 | else if (opr.type == o_displ && opr.addr == 0 |
653 | && opr.offb != 0 && opr.hasSIB && opr.sib == 0x24) |
654 | { |
655 | // uses [esp+0] with 0 encoded into op |
656 | do_nops++; |
657 | } |
15c7b2a4 |
658 | } |
b587e6ae |
659 | } |
660 | else { // not code |
661 | if (isOff0(ea_flags)) |
94cd6e34 |
662 | do_rva = 1; |
d8891fcc |
663 | } |
664 | |
665 | pass: |
b587e6ae |
666 | n = ln.get_linecnt(); |
667 | for (i = pl.lnnum; i < n; i++) { |
e74324f9 |
668 | do_def_line(buf, sizeof(buf), ln.down(), ea, func); |
15c7b2a4 |
669 | |
1402b79d |
670 | char *fw; |
671 | for (fw = buf; *fw != 0 && *fw == ' '; ) |
672 | fw++; |
673 | |
1caf86bb |
674 | // patches.. |
b587e6ae |
675 | if (drop_large) { |
1402b79d |
676 | p = strstr(fw, "large "); |
b587e6ae |
677 | if (p != NULL) |
678 | memmove(p, p + 6, strlen(p + 6) + 1); |
679 | } |
94cd6e34 |
680 | while (do_rva) { |
1402b79d |
681 | p = strstr(fw, " rva "); |
b587e6ae |
682 | if (p == NULL) |
683 | break; |
94cd6e34 |
684 | memmove(p + 4 + 3, p + 4, strlen(p + 4) + 1); |
685 | memcpy(p + 1, "offset", 6); |
b587e6ae |
686 | } |
1caf86bb |
687 | if (set_scale) { |
1402b79d |
688 | p = strchr(fw, '['); |
1caf86bb |
689 | if (p != NULL) |
690 | p = strchr(p, '+'); |
691 | if (p != NULL && p[1] == 'e') { |
692 | p += 4; |
693 | // scale is 1, must specify it explicitly so that |
694 | // masm chooses the right scaled reg |
695 | memmove(p + 2, p, strlen(p) + 1); |
696 | memcpy(p, "*1", 2); |
697 | } |
698 | } |
699 | else if (jmp_near) { |
9aaf1dcf |
700 | p = NULL; |
701 | if (fw != buf && fw[0] == 'j') |
702 | p = fw; |
1caf86bb |
703 | while (p && *p != ' ') |
704 | p++; |
705 | while (p && *p == ' ') |
706 | p++; |
707 | if (p != NULL) { |
708 | memmove(p + 9, p, strlen(p) + 1); |
709 | memcpy(p, "near ptr ", 9); |
9aaf1dcf |
710 | jmp_near = 0; |
1caf86bb |
711 | } |
712 | } |
713 | if (word_imm) { |
1402b79d |
714 | p = strstr(fw, ", "); |
1caf86bb |
715 | if (p != NULL && '0' <= p[2] && p[2] <= '9') { |
716 | p += 2; |
717 | memmove(p + 9, p, strlen(p) + 1); |
718 | memcpy(p, "word ptr ", 9); |
719 | } |
720 | } |
721 | else if (dword_imm) { |
1402b79d |
722 | p = strstr(fw, ", "); |
1caf86bb |
723 | if (p != NULL && '0' <= p[2] && p[2] <= '9') { |
724 | p += 2; |
725 | memmove(p + 10, p, strlen(p) + 1); |
726 | memcpy(p, "dword ptr ", 10); |
727 | } |
728 | } |
729 | else if (do_pushf) { |
1402b79d |
730 | p = strstr(fw, "pushf"); |
1caf86bb |
731 | if (p == NULL) |
1402b79d |
732 | p = strstr(fw, "popf"); |
1caf86bb |
733 | if (p != NULL) { |
734 | p = strchr(p, 'f') + 1; |
735 | memmove(p + 1, p, strlen(p) + 1); |
736 | *p = 'd'; |
737 | } |
738 | } |
b587e6ae |
739 | |
de8a204c |
740 | if (fw[0] == 'a' && IS_START(fw, "assume cs")) { |
741 | // "assume cs" causes problems with ext syms |
742 | memmove(fw + 1, fw, strlen(fw) + 1); |
743 | *fw = ';'; |
744 | } |
745 | else if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') { |
1402b79d |
746 | fout_line++; |
747 | qfprintf(fout, "include public.inc\n\n"); |
748 | |
749 | // kill entry point |
750 | fw[3] = 0; |
751 | } |
752 | |
b587e6ae |
753 | fout_line++; |
754 | qfprintf(fout, "%s\n", buf); |
755 | } |
d8891fcc |
756 | |
ed6ebb48 |
757 | while (do_nops-- > 0) |
758 | qfprintf(fout, " nop ; adj\n"); |
759 | |
15c7b2a4 |
760 | // note: next_head skips some undefined stuff |
d8891fcc |
761 | ea = next_not_tail(ea); // correct? |
15c7b2a4 |
762 | if (ea == BADADDR) |
d8891fcc |
763 | break; |
764 | |
765 | pl.ea = ea; |
766 | pl.lnnum = 0; |
767 | ln.set_place(&pl); |
d8891fcc |
768 | } |
769 | |
770 | if (fout != NULL) |
771 | qfclose(fout); |
15c7b2a4 |
772 | if (fname != NULL) |
773 | qfree(fname); |
d8891fcc |
774 | |
775 | hide_wait_box(); |
776 | msg("%d lines saved.\n", fout_line); |
777 | } |
778 | |
779 | //-------------------------------------------------------------------------- |
780 | |
3682b4b1 |
781 | static const char comment[] = "Generate disassembly for nasm"; |
d8891fcc |
782 | static const char help[] = "Generate asm file\n"; |
783 | static const char wanted_name[] = "Save asm"; |
3682b4b1 |
784 | static const char wanted_hotkey[] = "Shift-S"; |
d8891fcc |
785 | |
786 | //-------------------------------------------------------------------------- |
787 | // |
788 | // PLUGIN DESCRIPTION BLOCK |
789 | // |
790 | //-------------------------------------------------------------------------- |
791 | plugin_t PLUGIN = |
792 | { |
793 | IDP_INTERFACE_VERSION, |
794 | 0, // plugin flags |
795 | init, // initialize |
796 | term, // terminate. this pointer may be NULL. |
797 | run, // invoke plugin |
798 | comment, // long comment about the plugin |
799 | // it could appear in the status line |
800 | // or as a hint |
801 | help, // multiline help about the plugin |
802 | wanted_name, // the preferred short name of the plugin |
803 | wanted_hotkey // the preferred hotkey to run the plugin |
804 | }; |
805 | |
806 | // vim:ts=2:shiftwidth=2:expandtab |