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[] = {
45 static int is_name_reserved(const char *name)
48 for (i = 0; i < ARRAY_SIZE(reserved_names); i++)
49 if (strcasecmp(name, reserved_names[i]) == 0)
55 static int nonlocal_bt_cmp(const void *p1, const void *p2)
57 const ea_t *e1 = (const ea_t *)p1, *e2 = (const ea_t *)p2;
61 static void nonlocal_add(ea_t ea)
63 if (nonlocal_bt_cnt >= nonlocal_bt_alloc) {
64 nonlocal_bt_alloc += nonlocal_bt_alloc * 2 + 64;
65 nonlocal_bt = (ea_t *)realloc(nonlocal_bt,
66 nonlocal_bt_alloc * sizeof(nonlocal_bt[0]));
67 if (nonlocal_bt == NULL) {
72 nonlocal_bt[nonlocal_bt_cnt++] = ea;
75 static void do_def_line(char *buf, size_t buf_size, const char *line)
81 tag_remove(line, buf, buf_size); // remove color codes
87 memmove(buf, buf + 9, len - 9 + 1); // rm address
89 if (IS_START(buf, "loc_")) {
90 ea = strtoul(buf + 4, &endp, 16);
91 if (ea != 0 && *endp == ':') {
92 ea_ret = (ea_t *)bsearch(&ea, nonlocal_bt, nonlocal_bt_cnt,
93 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
96 msg("no trailing blank in '%s'\n", buf);
104 static void idaapi run(int /*arg*/)
106 // isEnabled(ea) // address belongs to disassembly
107 // ea_t ea = get_screen_ea();
108 // foo = DecodeInstruction(ScreenEA());
115 ea_t ui_ea_block = 0, ea_size;
116 ea_t tmp_ea, target_ea;
124 // 1st pass: walk through all funcs
125 func = get_func(inf.minEA);
128 func_tail_iterator_t fti(func);
130 msg("%x: func_tail_iterator_t main failed\n", ea);
133 const area_t &f_area = fti.chunk();
136 // rename global syms which conflict with frame member names
137 frame = get_frame(func);
140 for (m = 0; m < (int)frame->memqty; m++)
142 ret = get_member_name(frame->members[m].id, buf, sizeof(buf));
144 msg("%x: member has no name?\n", ea);
147 if (buf[0] == ' ') // what's this?
149 if (IS_START(buf, "arg_") || IS_START(buf, "var_"))
152 if (is_name_reserved(buf)) {
153 msg("%x: renaming '%s'\n", ea, buf);
154 qstrncat(buf, "_", sizeof(buf));
155 ret = set_member_name(frame, frame->members[m].soff, buf);
157 msg("%x: renaming failed\n", ea);
162 tmp_ea = get_name_ea(ea, buf);
163 if (tmp_ea == 0 || tmp_ea == ~0)
166 msg("%x: from %x: renaming '%s'\n", tmp_ea, ea, buf);
167 qstrncat(buf, "_g", sizeof(buf));
168 set_name(tmp_ea, buf);
172 func = get_next_func(ea);
175 // 2nd pass over whole .text segment
176 for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA))
178 segment_t *seg = getseg(ea);
179 if (!seg || seg->type != SEG_CODE)
182 flags_t ea_flags = get_flags_novalue(ea);
184 if (isCode(ea_flags))
186 if (!decode_insn(ea)) {
187 msg("%x: decode_insn() failed\n", ea);
191 // find non-local branches
192 if ((cmd.itype == NN_jmp || insn_jcc())
193 && cmd.Operands[0].type == o_near)
195 target_ea = cmd.Operands[0].addr;
197 nonlocal_add(target_ea);
199 ret = get_func_chunknum(func, target_ea);
201 // a jump to another func or chunk
202 // check if it lands on func start
203 if (!isFunc(get_flags_novalue(target_ea)))
204 nonlocal_add(target_ea);
210 if (func == NULL && isOff0(ea_flags)) {
211 ea_size = get_item_size(ea);
212 for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4)
213 nonlocal_add(get_long(ea + tmp_ea));
218 if (nonlocal_bt_cnt > 1) {
219 qsort(nonlocal_bt, nonlocal_bt_cnt,
220 sizeof(nonlocal_bt[0]), nonlocal_bt_cmp);
223 char *fname = askfile_c(1, NULL, "Save asm file");
226 fout = qfopen(fname, "w");
228 msg("couldn't open '%s'\n", fname);
232 show_wait_box("Saving..");
234 // deal with the beginning
236 int flags = 0; // calc_default_idaplace_flags();
237 linearray_t ln(&flags);
242 n = ln.get_linecnt();
243 for (i = 0; i < n - 1; i++) {
244 do_def_line(buf, sizeof(buf), ln.down());
245 if (strstr(buf, "include"))
249 qfprintf(fout, "%s\n", buf);
250 p = strstr(buf, ".mmx");
252 memcpy(p, ".xmm", 4);
254 qfprintf(fout, "%s\n", buf);
262 if ((ea >> 14) != ui_ea_block) {
263 ui_ea_block = ea >> 14;
269 segment_t *seg = getseg(ea);
270 if (!seg || seg->type != SEG_CODE)
272 if (!decode_insn(ea))
275 // note: decode_insn() picks up things like dd, size is then weird
276 //cmd_size = cmd.size;
278 for (o = 0; o < UA_MAXOP; o++) {
279 if (cmd.Operands[o].type == o_void)
282 if (cmd.Operands[o].type == o_mem
283 && cmd.Operands[o].specval_shorts.high == 0x21) // correct?
288 if (cmd.Operands[o].type == o_displ && cmd.Operands[o].reg == 5) {
291 m = get_stkvar(cmd.Operands[o], cmd.Operands[o].addr, NULL);
293 msg("%x: no stkvar for offs %x\n",
294 ea, cmd.Operands[o].addr);
297 if (get_struc_name(m->id, buf, sizeof(buf)) <= 0) {
298 msg("%x: stkvar with offs %x has no name?\n",
299 ea, cmd.Operands[o].addr);
302 msg("%x: name '%s'\n", ea, buf);
308 do_def_line(buf, sizeof(buf), ln.down());
310 p = strstr(buf, "large ");
312 memmove(p, p + 6, strlen(p + 6) + 1);
316 qfprintf(fout, "%s\n", buf);
318 // note: next_head skips some undefined stuff
319 ea = next_not_tail(ea); // correct?
326 n = ln.get_linecnt();
327 for (i = 0; i < n - 1; i++)
330 do_def_line(buf, sizeof(buf), ln.down());
331 qfprintf(fout, "%s\n", buf);
341 msg("%d lines saved.\n", fout_line);
344 //--------------------------------------------------------------------------
346 static const char comment[] = "Generate disassembly lines for one address";
347 static const char help[] = "Generate asm file\n";
348 static const char wanted_name[] = "Save asm";
349 static const char wanted_hotkey[] = "Ctrl-F6";
351 //--------------------------------------------------------------------------
353 // PLUGIN DESCRIPTION BLOCK
355 //--------------------------------------------------------------------------
358 IDP_INTERFACE_VERSION,
361 term, // terminate. this pointer may be NULL.
362 run, // invoke plugin
363 comment, // long comment about the plugin
364 // it could appear in the status line
366 help, // multiline help about the plugin
367 wanted_name, // the preferred short name of the plugin
368 wanted_hotkey // the preferred hotkey to run the plugin
371 // vim:ts=2:shiftwidth=2:expandtab