1 #define NO_OBSOLETE_FUNCS
14 #define IS_START(w, y) !strncmp(w, y, strlen(y))
15 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
17 // non-local branch targets
18 static ea_t *nonlocal_bt;
19 static int nonlocal_bt_alloc;
20 static int nonlocal_bt_cnt;
22 //--------------------------------------------------------------------------
23 static int idaapi init(void)
28 //--------------------------------------------------------------------------
29 static void idaapi term(void)
31 if (nonlocal_bt != NULL) {
35 nonlocal_bt_alloc = 0;
38 //--------------------------------------------------------------------------
40 static const char *reserved_names[] = {
47 static int is_name_reserved(const char *name)
50 for (i = 0; i < ARRAY_SIZE(reserved_names); i++)
51 if (strcasecmp(name, reserved_names[i]) == 0)
57 static int nonlocal_bt_cmp(const void *p1, const void *p2)
59 const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2;
63 static void nonlocal_add(ea_t ea)
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) {
74 nonlocal_bt[nonlocal_bt_cnt++] = ea;
77 // is instruction a (un)conditional jump (not call)?
78 static int is_insn_jmp(uint16 itype)
80 return itype == NN_jmp || (NN_ja <= itype && itype <= NN_jz);
83 static void do_def_line(char *buf, size_t buf_size, const char *line)
89 tag_remove(line, buf, buf_size); // remove color codes
95 memmove(buf, buf + 9, len - 9 + 1); // rm address
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);
104 msg("no trailing blank in '%s'\n", buf);
112 static void idaapi run(int /*arg*/)
114 // isEnabled(ea) // address belongs to disassembly
115 // ea_t ea = get_screen_ea();
116 // foo = DecodeInstruction(ScreenEA());
117 int drop_large, drop_rva;
124 ea_t ui_ea_block = 0, ea_size;
125 ea_t tmp_ea, target_ea;
134 // 1st pass: walk through all funcs
135 func = get_func(inf.minEA);
138 func_tail_iterator_t fti(func);
140 msg("%x: func_tail_iterator_t main failed\n", ea);
143 const area_t &f_area = fti.chunk();
146 // rename global syms which conflict with frame member names
147 frame = get_frame(func);
150 for (m = 0; m < (int)frame->memqty; m++)
152 ret = get_member_name(frame->members[m].id, buf, sizeof(buf));
154 msg("%x: member has no name?\n", ea);
157 if (buf[0] == ' ') // what's this?
159 if (IS_START(buf, "arg_") || IS_START(buf, "var_"))
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);
167 msg("%x: renaming failed\n", ea);
172 tmp_ea = get_name_ea(ea, buf);
173 if (tmp_ea == 0 || tmp_ea == ~0)
176 msg("%x: from %x: renaming '%s'\n", tmp_ea, ea, buf);
177 qstrncat(buf, "_g", sizeof(buf));
178 set_name(tmp_ea, buf);
182 func = get_next_func(ea);
185 // 2nd pass over whole .text and .(ro)data segments
186 for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA))
188 segment_t *seg = getseg(ea);
191 if (seg->type == SEG_XTRN)
193 if (seg->type != SEG_CODE && seg->type != SEG_DATA)
196 ea_flags = get_flags_novalue(ea);
198 if (isCode(ea_flags))
200 if (!decode_insn(ea)) {
201 msg("%x: decode_insn() failed\n", ea);
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)
209 for (o = 0; o < UA_MAXOP; o++) {
210 if (cmd.Operands[o].type == o_void)
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)) {
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);
226 // find non-local branches
227 if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near)
229 target_ea = cmd.Operands[0].addr;
231 nonlocal_add(target_ea);
233 ret = get_func_chunknum(func, target_ea);
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);
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));
250 // IDA vs masm float/mmx/xmm type incompatibility
251 if (isDouble(ea_flags) || isTbyt(ea_flags)
252 || isPackReal(ea_flags))
255 get_name(BADADDR, ea, buf, sizeof(buf));
256 msg("%x: undefining '%s'\n", ea, buf);
257 do_unknown(ea, DOUNK_EXPAND);
259 if (isOwrd(ea_flags)) {
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);
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);
276 msg("%x: null name?\n", ea);
280 if (is_name_reserved(name)) {
281 msg("%x: renaming name '%s'\n", ea, name);
282 qsnprintf(buf, sizeof(buf), "%s_g", name);
287 if (nonlocal_bt_cnt > 1) {
288 qsort(nonlocal_bt, nonlocal_bt_cnt,
289 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
292 char *fname = askfile_c(1, NULL, "Save asm file");
295 fout = qfopen(fname, "w");
297 msg("couldn't open '%s'\n", fname);
301 show_wait_box("Saving..");
303 // deal with the beginning
305 int flags = 0; // calc_default_idaplace_flags();
306 linearray_t ln(&flags);
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"))
318 qfprintf(fout, "%s\n", buf);
319 p = strstr(buf, ".mmx");
321 memcpy(p, ".xmm", 4);
323 qfprintf(fout, "%s\n", buf);
330 drop_large = drop_rva = 0;
332 if ((ea >> 14) != ui_ea_block) {
333 ui_ea_block = ea >> 14;
339 segment_t *seg = getseg(ea);
340 if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA))
343 ea_flags = get_flags_novalue(ea);
344 if (isCode(ea_flags))
346 if (!decode_insn(ea))
349 for (o = 0; o < UA_MAXOP; o++) {
350 if (cmd.Operands[o].type == o_void)
353 if (cmd.Operands[o].type == o_mem
354 && cmd.Operands[o].specval_shorts.high == 0x21) // correct?
361 if (isOff0(ea_flags))
366 n = ln.get_linecnt();
367 for (i = pl.lnnum; i < n; i++) {
368 do_def_line(buf, sizeof(buf), ln.down());
371 p = strstr(buf, "large ");
373 memmove(p, p + 6, strlen(p + 6) + 1);
376 p = strstr(buf, " rva ");
379 memmove(p, p + 4, strlen(p + 4) + 1);
383 qfprintf(fout, "%s\n", buf);
386 // note: next_head skips some undefined stuff
387 ea = next_not_tail(ea); // correct?
402 msg("%d lines saved.\n", fout_line);
405 //--------------------------------------------------------------------------
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";
412 //--------------------------------------------------------------------------
414 // PLUGIN DESCRIPTION BLOCK
416 //--------------------------------------------------------------------------
419 IDP_INTERFACE_VERSION,
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
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
432 // vim:ts=2:shiftwidth=2:expandtab