d8891fcc |
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> |
15c7b2a4 |
12 | #include <intel.hpp> |
d8891fcc |
13 | |
14 | #define IS_START(w, y) !strncmp(w, y, strlen(y)) |
15 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
16 | |
15c7b2a4 |
17 | // non-local branch targets |
18 | static ea_t *nonlocal_bt; |
19 | static int nonlocal_bt_alloc; |
20 | static int nonlocal_bt_cnt; |
21 | |
d8891fcc |
22 | //-------------------------------------------------------------------------- |
23 | static int idaapi init(void) |
24 | { |
25 | return PLUGIN_OK; |
26 | } |
27 | |
28 | //-------------------------------------------------------------------------- |
29 | static void idaapi term(void) |
30 | { |
15c7b2a4 |
31 | if (nonlocal_bt != NULL) { |
32 | free(nonlocal_bt); |
33 | nonlocal_bt = NULL; |
34 | } |
35 | nonlocal_bt_alloc = 0; |
d8891fcc |
36 | } |
37 | |
38 | //-------------------------------------------------------------------------- |
39 | |
40 | static const char *reserved_names[] = { |
41 | "name", |
b587e6ae |
42 | "type", |
d8891fcc |
43 | "offset", |
b587e6ae |
44 | "aam", |
d8891fcc |
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 | |
15c7b2a4 |
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 | |
b587e6ae |
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 | |
d8891fcc |
83 | static void do_def_line(char *buf, size_t buf_size, const char *line) |
84 | { |
15c7b2a4 |
85 | char *endp = NULL; |
86 | ea_t ea, *ea_ret; |
d8891fcc |
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 |
15c7b2a4 |
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 | } |
d8891fcc |
110 | } |
111 | |
112 | static void idaapi run(int /*arg*/) |
113 | { |
15c7b2a4 |
114 | // isEnabled(ea) // address belongs to disassembly |
d8891fcc |
115 | // ea_t ea = get_screen_ea(); |
15c7b2a4 |
116 | // foo = DecodeInstruction(ScreenEA()); |
b587e6ae |
117 | int drop_large, drop_rva; |
d8891fcc |
118 | FILE *fout = NULL; |
119 | int fout_line = 0; |
120 | char buf[MAXSTR]; |
b587e6ae |
121 | const char *name; |
d8891fcc |
122 | struc_t *frame; |
123 | func_t *func; |
15c7b2a4 |
124 | ea_t ui_ea_block = 0, ea_size; |
125 | ea_t tmp_ea, target_ea; |
d8891fcc |
126 | ea_t ea; |
b587e6ae |
127 | flags_t ea_flags; |
d8891fcc |
128 | int i, o, m, n; |
129 | int ret; |
130 | char *p; |
131 | |
15c7b2a4 |
132 | nonlocal_bt_cnt = 0; |
133 | |
134 | // 1st pass: walk through all funcs |
135 | func = get_func(inf.minEA); |
d8891fcc |
136 | while (func != NULL) |
137 | { |
15c7b2a4 |
138 | func_tail_iterator_t fti(func); |
139 | if (!fti.main()) { |
140 | msg("%x: func_tail_iterator_t main failed\n", ea); |
141 | return; |
142 | } |
143 | const area_t &f_area = fti.chunk(); |
144 | ea = f_area.startEA; |
145 | |
146 | // rename global syms which conflict with frame member names |
d8891fcc |
147 | frame = get_frame(func); |
148 | if (frame != NULL) |
149 | { |
150 | for (m = 0; m < (int)frame->memqty; m++) |
151 | { |
152 | ret = get_member_name(frame->members[m].id, buf, sizeof(buf)); |
153 | if (ret <= 0) { |
154 | msg("%x: member has no name?\n", ea); |
155 | return; |
156 | } |
157 | if (buf[0] == ' ') // what's this? |
158 | continue; |
159 | if (IS_START(buf, "arg_") || IS_START(buf, "var_")) |
160 | continue; |
161 | |
162 | if (is_name_reserved(buf)) { |
163 | msg("%x: renaming '%s'\n", ea, buf); |
164 | qstrncat(buf, "_", sizeof(buf)); |
165 | ret = set_member_name(frame, frame->members[m].soff, buf); |
166 | if (!ret) { |
167 | msg("%x: renaming failed\n", ea); |
168 | return; |
169 | } |
170 | } |
171 | |
172 | tmp_ea = get_name_ea(ea, buf); |
173 | if (tmp_ea == 0 || tmp_ea == ~0) |
174 | continue; |
175 | |
176 | msg("%x: from %x: renaming '%s'\n", tmp_ea, ea, buf); |
177 | qstrncat(buf, "_g", sizeof(buf)); |
178 | set_name(tmp_ea, buf); |
179 | } |
180 | } |
181 | |
182 | func = get_next_func(ea); |
15c7b2a4 |
183 | } |
184 | |
b587e6ae |
185 | // 2nd pass over whole .text and .(ro)data segments |
15c7b2a4 |
186 | for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA)) |
187 | { |
188 | segment_t *seg = getseg(ea); |
b587e6ae |
189 | if (!seg) |
190 | break; |
191 | if (seg->type == SEG_XTRN) |
192 | continue; |
193 | if (seg->type != SEG_CODE && seg->type != SEG_DATA) |
15c7b2a4 |
194 | break; |
195 | |
b587e6ae |
196 | ea_flags = get_flags_novalue(ea); |
15c7b2a4 |
197 | func = get_func(ea); |
198 | if (isCode(ea_flags)) |
199 | { |
200 | if (!decode_insn(ea)) { |
201 | msg("%x: decode_insn() failed\n", ea); |
202 | continue; |
203 | } |
204 | |
b587e6ae |
205 | // masm doesn't understand IDA's float/xmm types |
206 | if (cmd.itype == NN_fld || cmd.itype == NN_fst |
207 | || cmd.itype == NN_movapd || cmd.itype == NN_movlpd) |
208 | { |
209 | for (o = 0; o < UA_MAXOP; o++) { |
210 | if (cmd.Operands[o].type == o_void) |
211 | break; |
212 | |
213 | if (cmd.Operands[o].type == o_mem) { |
214 | tmp_ea = cmd.Operands[o].addr; |
215 | flags_t tmp_ea_flags = get_flags_novalue(tmp_ea); |
216 | if (!isUnknown(tmp_ea_flags)) { |
217 | buf[0] = 0; |
218 | get_name(ea, tmp_ea, buf, sizeof(buf)); |
219 | msg("%x: undefining %x '%s'\n", ea, tmp_ea, buf); |
220 | do_unknown(tmp_ea, DOUNK_EXPAND); |
221 | } |
222 | } |
223 | } |
224 | } |
225 | |
15c7b2a4 |
226 | // find non-local branches |
b587e6ae |
227 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near) |
15c7b2a4 |
228 | { |
229 | target_ea = cmd.Operands[0].addr; |
230 | if (func == NULL) |
231 | nonlocal_add(target_ea); |
232 | else { |
233 | ret = get_func_chunknum(func, target_ea); |
234 | if (ret != 0) { |
235 | // a jump to another func or chunk |
236 | // check if it lands on func start |
237 | if (!isFunc(get_flags_novalue(target_ea))) |
238 | nonlocal_add(target_ea); |
239 | } |
240 | } |
241 | } |
242 | } |
243 | else { // not code |
244 | if (func == NULL && isOff0(ea_flags)) { |
245 | ea_size = get_item_size(ea); |
246 | for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4) |
247 | nonlocal_add(get_long(ea + tmp_ea)); |
248 | } |
b587e6ae |
249 | |
250 | // IDA vs masm float/mmx/xmm type incompatibility |
251 | if (isDouble(ea_flags) || isTbyt(ea_flags) |
252 | || isPackReal(ea_flags)) |
253 | { |
254 | buf[0] = 0; |
255 | get_name(BADADDR, ea, buf, sizeof(buf)); |
256 | msg("%x: undefining '%s'\n", ea, buf); |
257 | do_unknown(ea, DOUNK_EXPAND); |
258 | } |
259 | if (isOwrd(ea_flags)) { |
260 | buf[0] = 0; |
261 | get_name(BADADDR, ea, buf, sizeof(buf)); |
262 | if (IS_START(buf, "xmm")) { |
263 | msg("%x: undefining '%s'\n", ea, buf); |
264 | do_unknown(ea, DOUNK_EXPAND); |
265 | } |
266 | } |
267 | } |
268 | } |
269 | |
270 | // check namelist for reserved names |
271 | n = get_nlist_size(); |
272 | for (i = 0; i < n; i++) { |
273 | ea = get_nlist_ea(i); |
274 | name = get_nlist_name(i); |
275 | if (name == NULL) { |
276 | msg("%x: null name?\n", ea); |
277 | continue; |
278 | } |
279 | |
280 | if (is_name_reserved(name)) { |
281 | msg("%x: renaming name '%s'\n", ea, name); |
282 | qsnprintf(buf, sizeof(buf), "%s_g", name); |
283 | set_name(ea, buf); |
15c7b2a4 |
284 | } |
285 | } |
286 | |
287 | if (nonlocal_bt_cnt > 1) { |
288 | qsort(nonlocal_bt, nonlocal_bt_cnt, |
289 | sizeof(nonlocal_bt[0]), nonlocal_bt_cmp); |
d8891fcc |
290 | } |
291 | |
292 | char *fname = askfile_c(1, NULL, "Save asm file"); |
293 | if (fname == NULL) |
294 | return; |
295 | fout = qfopen(fname, "w"); |
296 | if (fout == NULL) { |
297 | msg("couldn't open '%s'\n", fname); |
298 | return; |
299 | } |
300 | |
301 | show_wait_box("Saving.."); |
302 | |
303 | // deal with the beginning |
304 | ea = inf.minEA; |
305 | int flags = 0; // calc_default_idaplace_flags(); |
306 | linearray_t ln(&flags); |
307 | idaplace_t pl; |
308 | pl.ea = ea; |
309 | pl.lnnum = 0; |
310 | ln.set_place(&pl); |
311 | n = ln.get_linecnt(); |
312 | for (i = 0; i < n - 1; i++) { |
313 | do_def_line(buf, sizeof(buf), ln.down()); |
314 | if (strstr(buf, "include")) |
315 | continue; |
316 | |
317 | fout_line++; |
318 | qfprintf(fout, "%s\n", buf); |
319 | p = strstr(buf, ".mmx"); |
320 | if (p != NULL) { |
321 | memcpy(p, ".xmm", 4); |
322 | fout_line++; |
323 | qfprintf(fout, "%s\n", buf); |
324 | } |
325 | } |
b587e6ae |
326 | pl.lnnum = i; |
d8891fcc |
327 | |
328 | for (;;) |
329 | { |
b587e6ae |
330 | drop_large = drop_rva = 0; |
15c7b2a4 |
331 | |
d8891fcc |
332 | if ((ea >> 14) != ui_ea_block) { |
333 | ui_ea_block = ea >> 14; |
334 | showAddr(ea); |
335 | if (wasBreak()) |
336 | break; |
337 | } |
338 | |
339 | segment_t *seg = getseg(ea); |
b587e6ae |
340 | if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA)) |
d8891fcc |
341 | goto pass; |
342 | |
b587e6ae |
343 | ea_flags = get_flags_novalue(ea); |
344 | if (isCode(ea_flags)) |
345 | { |
346 | if (!decode_insn(ea)) |
347 | goto pass; |
d8891fcc |
348 | |
b587e6ae |
349 | for (o = 0; o < UA_MAXOP; o++) { |
350 | if (cmd.Operands[o].type == o_void) |
351 | break; |
d8891fcc |
352 | |
b587e6ae |
353 | if (cmd.Operands[o].type == o_mem |
354 | && cmd.Operands[o].specval_shorts.high == 0x21) // correct? |
355 | { |
356 | drop_large = 1; |
15c7b2a4 |
357 | } |
15c7b2a4 |
358 | } |
b587e6ae |
359 | } |
360 | else { // not code |
361 | if (isOff0(ea_flags)) |
362 | drop_rva = 1; |
d8891fcc |
363 | } |
364 | |
365 | pass: |
b587e6ae |
366 | n = ln.get_linecnt(); |
367 | for (i = pl.lnnum; i < n; i++) { |
368 | do_def_line(buf, sizeof(buf), ln.down()); |
15c7b2a4 |
369 | |
b587e6ae |
370 | if (drop_large) { |
371 | p = strstr(buf, "large "); |
372 | if (p != NULL) |
373 | memmove(p, p + 6, strlen(p + 6) + 1); |
374 | } |
375 | while (drop_rva) { |
376 | p = strstr(buf, " rva "); |
377 | if (p == NULL) |
378 | break; |
379 | memmove(p, p + 4, strlen(p + 4) + 1); |
380 | } |
381 | |
382 | fout_line++; |
383 | qfprintf(fout, "%s\n", buf); |
384 | } |
d8891fcc |
385 | |
15c7b2a4 |
386 | // note: next_head skips some undefined stuff |
d8891fcc |
387 | ea = next_not_tail(ea); // correct? |
15c7b2a4 |
388 | if (ea == BADADDR) |
d8891fcc |
389 | break; |
390 | |
391 | pl.ea = ea; |
392 | pl.lnnum = 0; |
393 | ln.set_place(&pl); |
d8891fcc |
394 | } |
395 | |
396 | if (fout != NULL) |
397 | qfclose(fout); |
15c7b2a4 |
398 | if (fname != NULL) |
399 | qfree(fname); |
d8891fcc |
400 | |
401 | hide_wait_box(); |
402 | msg("%d lines saved.\n", fout_line); |
403 | } |
404 | |
405 | //-------------------------------------------------------------------------- |
406 | |
407 | static const char comment[] = "Generate disassembly lines for one address"; |
408 | static const char help[] = "Generate asm file\n"; |
409 | static const char wanted_name[] = "Save asm"; |
410 | static const char wanted_hotkey[] = "Ctrl-F6"; |
411 | |
412 | //-------------------------------------------------------------------------- |
413 | // |
414 | // PLUGIN DESCRIPTION BLOCK |
415 | // |
416 | //-------------------------------------------------------------------------- |
417 | plugin_t PLUGIN = |
418 | { |
419 | IDP_INTERFACE_VERSION, |
420 | 0, // plugin flags |
421 | init, // initialize |
422 | term, // terminate. this pointer may be NULL. |
423 | run, // invoke plugin |
424 | comment, // long comment about the plugin |
425 | // it could appear in the status line |
426 | // or as a hint |
427 | help, // multiline help about the plugin |
428 | wanted_name, // the preferred short name of the plugin |
429 | wanted_hotkey // the preferred hotkey to run the plugin |
430 | }; |
431 | |
432 | // vim:ts=2:shiftwidth=2:expandtab |