| 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 | |
| 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> |
| 19 | #include <offset.hpp> |
| 20 | #include <auto.hpp> |
| 21 | #include <intel.hpp> |
| 22 | |
| 23 | #define IS_START(w, y) !strncmp(w, y, strlen(y)) |
| 24 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
| 25 | |
| 26 | static char **name_cache; |
| 27 | static size_t name_cache_size; |
| 28 | |
| 29 | // non-local branch targets |
| 30 | static ea_t *nonlocal_bt; |
| 31 | static int nonlocal_bt_alloc; |
| 32 | static int nonlocal_bt_cnt; |
| 33 | |
| 34 | //-------------------------------------------------------------------------- |
| 35 | static int idaapi init(void) |
| 36 | { |
| 37 | return PLUGIN_OK; |
| 38 | } |
| 39 | |
| 40 | //-------------------------------------------------------------------------- |
| 41 | static void idaapi term(void) |
| 42 | { |
| 43 | size_t i; |
| 44 | |
| 45 | if (nonlocal_bt != NULL) { |
| 46 | free(nonlocal_bt); |
| 47 | nonlocal_bt = NULL; |
| 48 | } |
| 49 | nonlocal_bt_alloc = 0; |
| 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; |
| 58 | } |
| 59 | |
| 60 | //-------------------------------------------------------------------------- |
| 61 | |
| 62 | static const char *reserved_names[] = { |
| 63 | "name", |
| 64 | "type", |
| 65 | "offset", |
| 66 | "aam", |
| 67 | "aas", |
| 68 | "text", |
| 69 | "size", |
| 70 | "c", |
| 71 | "align", |
| 72 | "addr", |
| 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 | |
| 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 | |
| 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 | |
| 111 | static void do_def_line(char *buf, size_t buf_size, const char *line, |
| 112 | ea_t ea, func_t *func) |
| 113 | { |
| 114 | char func_name[256] = "<nf>"; |
| 115 | int is_libfunc = 0; |
| 116 | int global_label; |
| 117 | ea_t *ea_ret; |
| 118 | int i, len; |
| 119 | char *p; |
| 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 |
| 128 | |
| 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); |
| 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) { |
| 151 | if (p[1] != ' ') |
| 152 | msg("no trailing blank in '%s'\n", buf); |
| 153 | else |
| 154 | p[1] = ':'; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 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 | |
| 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 | |
| 232 | static void idaapi run(int /*arg*/) |
| 233 | { |
| 234 | // isEnabled(ea) // address belongs to disassembly |
| 235 | // ea_t ea = get_screen_ea(); |
| 236 | // extern foo; |
| 237 | // foo = DecodeInstruction(ScreenEA()); |
| 238 | FILE *fout = NULL; |
| 239 | int fout_line = 0; |
| 240 | char buf[MAXSTR]; |
| 241 | char buf2[MAXSTR]; |
| 242 | const char *name; |
| 243 | struc_t *frame; |
| 244 | func_t *func; |
| 245 | ea_t ui_ea_block = 0, ea_size; |
| 246 | ea_t tmp_ea, target_ea; |
| 247 | ea_t ea; |
| 248 | flags_t ea_flags; |
| 249 | uval_t idx; |
| 250 | int i, o, m, n; |
| 251 | int ret; |
| 252 | char **pp; |
| 253 | char *p; |
| 254 | |
| 255 | nonlocal_bt_cnt = 0; |
| 256 | |
| 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 | |
| 270 | rebuild_name_cache(); |
| 271 | |
| 272 | // 1st pass: walk through all funcs |
| 273 | ea = inf.minEA; |
| 274 | func = get_func(ea); |
| 275 | while (func != NULL) |
| 276 | { |
| 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 |
| 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 | |
| 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) { |
| 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 | |
| 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) |
| 323 | continue; |
| 324 | |
| 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); |
| 329 | } |
| 330 | } |
| 331 | |
| 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 | |
| 344 | func = get_next_func(ea); |
| 345 | } |
| 346 | |
| 347 | // 2nd pass over whole .text and .(ro)data segments |
| 348 | for (ea = inf.minEA; ea != BADADDR; ea = next_head(ea, inf.maxEA)) |
| 349 | { |
| 350 | segment_t *seg = getseg(ea); |
| 351 | if (!seg) |
| 352 | break; |
| 353 | if (seg->type == SEG_XTRN) |
| 354 | continue; |
| 355 | if (seg->type != SEG_CODE && seg->type != SEG_DATA) |
| 356 | break; |
| 357 | |
| 358 | ea_flags = get_flags_novalue(ea); |
| 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 | |
| 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_flg = get_flags_novalue(tmp_ea); |
| 378 | buf[0] = 0; |
| 379 | if (isDouble(tmp_flg)) |
| 380 | { |
| 381 | get_name(ea, tmp_ea, buf, sizeof(buf)); |
| 382 | msg("%x: converting dbl %x '%s'\n", ea, tmp_ea, buf); |
| 383 | doQwrd(tmp_ea, 8); |
| 384 | } |
| 385 | if (isOwrd(tmp_flg) || isYwrd(tmp_flg) || isTbyt(tmp_flg)) |
| 386 | { |
| 387 | get_name(ea, tmp_ea, buf, sizeof(buf)); |
| 388 | msg("%x: undefining lrg %x '%s'\n", ea, tmp_ea, buf); |
| 389 | do_unknown(tmp_ea, DOUNK_EXPAND); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | else if (cmd.itype == NN_lea) { |
| 395 | // detect code alignment |
| 396 | if (cmd.Operands[0].reg == cmd.Operands[1].reg |
| 397 | && cmd.Operands[1].type == o_displ |
| 398 | && cmd.Operands[1].addr == 0) |
| 399 | { |
| 400 | // lea eax, [eax+0] |
| 401 | make_align(ea); |
| 402 | } |
| 403 | else if (!isDefArg1(ea_flags) |
| 404 | && cmd.Operands[1].type == o_mem // why o_mem? |
| 405 | && cmd.Operands[1].dtyp == dt_dword) |
| 406 | { |
| 407 | if (inf.minEA <= cmd.Operands[1].addr |
| 408 | && cmd.Operands[1].addr < inf.maxEA) |
| 409 | { |
| 410 | // lea to segments, like ds:58D6A8h[edx*8] |
| 411 | msg("%x: lea offset to %x\n", ea, cmd.Operands[1].addr); |
| 412 | op_offset(ea, 1, REF_OFF32); |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | // ds:0[eax*8] -> [eax*8+0] |
| 417 | msg("%x: dropping ds: for %x\n", ea, cmd.Operands[1].addr); |
| 418 | op_hex(ea, 1); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | else if (cmd.itype == NN_mov && cmd.segpref == 0x1e // 2e? |
| 423 | && cmd.Operands[0].type == o_reg |
| 424 | && cmd.Operands[1].type == o_reg |
| 425 | && cmd.Operands[0].dtyp == cmd.Operands[1].dtyp |
| 426 | && cmd.Operands[0].reg == cmd.Operands[1].reg) |
| 427 | { |
| 428 | // db 2Eh; mov eax, eax |
| 429 | make_align(ea); |
| 430 | } |
| 431 | |
| 432 | // find non-local branches |
| 433 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near) |
| 434 | { |
| 435 | target_ea = cmd.Operands[0].addr; |
| 436 | if (func == NULL) |
| 437 | nonlocal_add(target_ea); |
| 438 | else { |
| 439 | ret = get_func_chunknum(func, target_ea); |
| 440 | if (ret != 0) { |
| 441 | // a jump to another func or chunk |
| 442 | // check if it lands on func start |
| 443 | if (!isFunc(get_flags_novalue(target_ea))) |
| 444 | nonlocal_add(target_ea); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | else { // not code |
| 450 | int do_undef = 0; |
| 451 | ea_size = get_item_size(ea); |
| 452 | |
| 453 | if (func == NULL && isOff0(ea_flags)) { |
| 454 | for (tmp_ea = 0; tmp_ea < ea_size; tmp_ea += 4) |
| 455 | nonlocal_add(get_long(ea + tmp_ea)); |
| 456 | } |
| 457 | |
| 458 | // IDA vs masm float/mmx/xmm type incompatibility |
| 459 | if (isDouble(ea_flags)) |
| 460 | { |
| 461 | msg("%x: converting double\n", ea); |
| 462 | doQwrd(ea, 8); |
| 463 | } |
| 464 | else if (isTbyt(ea_flags) || isPackReal(ea_flags)) |
| 465 | { |
| 466 | do_undef = 1; |
| 467 | } |
| 468 | else if (isOwrd(ea_flags)) { |
| 469 | buf[0] = 0; |
| 470 | get_name(BADADDR, ea, buf, sizeof(buf)); |
| 471 | if (IS_START(buf, "xmm")) |
| 472 | do_undef = 1; |
| 473 | } |
| 474 | // masm doesn't understand IDA's unicode |
| 475 | else if (isASCII(ea_flags) && ea_size >= 4 |
| 476 | && (get_long(ea) & 0xff00ff00) == 0) // lame.. |
| 477 | { |
| 478 | do_undef = 1; |
| 479 | } |
| 480 | // masm doesn't understand large aligns |
| 481 | else if (isAlign(ea_flags) && ea_size >= 0x10) |
| 482 | { |
| 483 | msg("%x: undefining align %d\n", ea, ea_size); |
| 484 | do_unknown(ea, DOUNK_EXPAND); |
| 485 | } |
| 486 | |
| 487 | if (do_undef) { |
| 488 | buf[0] = 0; |
| 489 | get_name(BADADDR, ea, buf, sizeof(buf)); |
| 490 | msg("%x: undefining '%s'\n", ea, buf); |
| 491 | do_unknown(ea, DOUNK_EXPAND); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // check namelist for reserved names and |
| 497 | // matching names with different case (nasm ignores case) |
| 498 | n = get_nlist_size(); |
| 499 | for (i = 0; i < n; i++) { |
| 500 | ea = get_nlist_ea(i); |
| 501 | name = get_nlist_name(i); |
| 502 | if (name == NULL) { |
| 503 | msg("%x: null name?\n", ea); |
| 504 | continue; |
| 505 | } |
| 506 | qsnprintf(buf, sizeof(buf), "%s", name); |
| 507 | |
| 508 | int need_rename = is_name_reserved(name); |
| 509 | if (!need_rename) { |
| 510 | p = buf; |
| 511 | pp = (char **)bsearch(&p, name_cache, name_cache_size, |
| 512 | sizeof(name_cache[0]), name_cache_cmp); |
| 513 | if (pp != NULL) { |
| 514 | if (pp > name_cache && stricmp(pp[-1], pp[0]) == 0) |
| 515 | need_rename = 1; |
| 516 | else if (pp < name_cache + name_cache_size - 1 |
| 517 | && stricmp(pp[0], pp[1]) == 0) |
| 518 | { |
| 519 | need_rename = 1; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // rename vars with '?@' (funcs are ok) |
| 525 | int change_qat = 0; |
| 526 | ea_flags = get_flags_novalue(ea); |
| 527 | if (!isCode(ea_flags) && strpbrk(name, "?@")) |
| 528 | change_qat = 1; |
| 529 | |
| 530 | if (need_rename || change_qat) { |
| 531 | msg("%x: renaming name '%s'\n", ea, name); |
| 532 | |
| 533 | if (change_qat) { |
| 534 | for (p = buf; *p != 0; p++) { |
| 535 | if (*p == '?' || *p == '@') { |
| 536 | qsnprintf(buf2, sizeof(buf2), "%02x", (unsigned char)*p); |
| 537 | memmove(p + 1, p, strlen(p) + 1); |
| 538 | memcpy(p, buf2, 2); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | my_rename(ea, buf); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if (nonlocal_bt_cnt > 1) { |
| 548 | qsort(nonlocal_bt, nonlocal_bt_cnt, |
| 549 | sizeof(nonlocal_bt[0]), nonlocal_bt_cmp); |
| 550 | } |
| 551 | |
| 552 | char *fname = askfile_c(1, NULL, "Save asm file"); |
| 553 | if (fname == NULL) |
| 554 | return; |
| 555 | fout = qfopen(fname, "w"); |
| 556 | if (fout == NULL) { |
| 557 | msg("couldn't open '%s'\n", fname); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | show_wait_box("Saving.."); |
| 562 | |
| 563 | // deal with the beginning |
| 564 | ea = inf.minEA; |
| 565 | int flags = 0; // calc_default_idaplace_flags(); |
| 566 | linearray_t ln(&flags); |
| 567 | idaplace_t pl; |
| 568 | pl.ea = ea; |
| 569 | pl.lnnum = 0; |
| 570 | ln.set_place(&pl); |
| 571 | n = ln.get_linecnt(); |
| 572 | for (i = 0; i < n - 1; i++) { |
| 573 | do_def_line(buf, sizeof(buf), ln.down(), ea, NULL); |
| 574 | if (strstr(buf, "include")) |
| 575 | continue; |
| 576 | |
| 577 | fout_line++; |
| 578 | qfprintf(fout, "%s\n", buf); |
| 579 | p = strstr(buf, ".mmx"); |
| 580 | if (p != NULL) { |
| 581 | memcpy(p, ".xmm", 4); |
| 582 | fout_line++; |
| 583 | qfprintf(fout, "%s\n", buf); |
| 584 | continue; |
| 585 | } |
| 586 | p = strstr(buf, ".model"); |
| 587 | if (p != NULL) { |
| 588 | qstrncpy(p, "include imports.inc", sizeof(buf) - (p - buf)); |
| 589 | fout_line++; |
| 590 | qfprintf(fout, "\n%s\n", buf); |
| 591 | i++; |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | pl.lnnum = i; |
| 596 | |
| 597 | for (;;) |
| 598 | { |
| 599 | int drop_large = 0, do_rva = 0, set_scale = 0, jmp_near = 0; |
| 600 | int word_imm = 0, dword_imm = 0, do_pushf = 0, do_nops = 0; |
| 601 | |
| 602 | if ((ea >> 14) != ui_ea_block) { |
| 603 | ui_ea_block = ea >> 14; |
| 604 | showAddr(ea); |
| 605 | if (wasBreak()) |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | func = get_func(ea); |
| 610 | |
| 611 | segment_t *seg = getseg(ea); |
| 612 | if (!seg || (seg->type != SEG_CODE && seg->type != SEG_DATA)) |
| 613 | goto pass; |
| 614 | |
| 615 | ea_flags = get_flags_novalue(ea); |
| 616 | if (isCode(ea_flags)) |
| 617 | { |
| 618 | if (!decode_insn(ea)) |
| 619 | goto pass; |
| 620 | |
| 621 | if (is_insn_jmp(cmd.itype) && cmd.Operands[0].type == o_near |
| 622 | && cmd.Operands[0].dtyp == dt_dword) |
| 623 | { |
| 624 | jmp_near = 1; |
| 625 | } |
| 626 | else if ((cmd.itype == NN_pushf || cmd.itype == NN_popf) |
| 627 | && natop()) |
| 628 | { |
| 629 | do_pushf = 1; |
| 630 | } |
| 631 | |
| 632 | for (o = 0; o < UA_MAXOP; o++) { |
| 633 | const op_t &opr = cmd.Operands[o]; |
| 634 | if (opr.type == o_void) |
| 635 | break; |
| 636 | |
| 637 | // correct? |
| 638 | if (opr.type == o_mem && opr.specval_shorts.high == 0x21) |
| 639 | drop_large = 1; |
| 640 | if (opr.hasSIB && x86_scale(opr) == 0 |
| 641 | && x86_index(opr) != INDEX_NONE) |
| 642 | { |
| 643 | set_scale = 1; |
| 644 | } |
| 645 | // annoying alignment variant.. |
| 646 | if (opr.type == o_imm && opr.dtyp == dt_dword |
| 647 | && (opr.value < 0x80 || opr.value > 0xffffff80) |
| 648 | && cmd.size >= opr.offb + 4) |
| 649 | { |
| 650 | if (get_long(ea + opr.offb) == opr.value) |
| 651 | dword_imm = 1; |
| 652 | } |
| 653 | else if (opr.type == o_imm && opr.dtyp == dt_word |
| 654 | && (opr.value < 0x80 || opr.value > 0xff80) |
| 655 | && cmd.size >= opr.offb + 2) |
| 656 | { |
| 657 | if (get_word(ea + opr.offb) == (ushort)opr.value) |
| 658 | word_imm = 1; |
| 659 | } |
| 660 | else if (opr.type == o_displ && opr.addr == 0 |
| 661 | && opr.offb != 0 && opr.hasSIB && opr.sib == 0x24) |
| 662 | { |
| 663 | // uses [esp+0] with 0 encoded into op |
| 664 | do_nops++; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | else { // not code |
| 669 | if (isOff0(ea_flags)) |
| 670 | do_rva = 1; |
| 671 | } |
| 672 | |
| 673 | pass: |
| 674 | n = ln.get_linecnt(); |
| 675 | for (i = pl.lnnum; i < n; i++) { |
| 676 | do_def_line(buf, sizeof(buf), ln.down(), ea, func); |
| 677 | |
| 678 | char *fw; |
| 679 | for (fw = buf; *fw != 0 && *fw == ' '; ) |
| 680 | fw++; |
| 681 | |
| 682 | // patches.. |
| 683 | if (drop_large) { |
| 684 | p = strstr(fw, "large "); |
| 685 | if (p != NULL) |
| 686 | memmove(p, p + 6, strlen(p + 6) + 1); |
| 687 | } |
| 688 | while (do_rva) { |
| 689 | p = strstr(fw, " rva "); |
| 690 | if (p == NULL) |
| 691 | break; |
| 692 | memmove(p + 4 + 3, p + 4, strlen(p + 4) + 1); |
| 693 | memcpy(p + 1, "offset", 6); |
| 694 | } |
| 695 | if (set_scale) { |
| 696 | p = strchr(fw, '['); |
| 697 | if (p != NULL) |
| 698 | p = strchr(p, '+'); |
| 699 | if (p != NULL && p[1] == 'e') { |
| 700 | p += 4; |
| 701 | // scale is 1, must specify it explicitly so that |
| 702 | // masm chooses the right scaled reg |
| 703 | memmove(p + 2, p, strlen(p) + 1); |
| 704 | memcpy(p, "*1", 2); |
| 705 | } |
| 706 | } |
| 707 | else if (jmp_near) { |
| 708 | p = NULL; |
| 709 | if (fw != buf && fw[0] == 'j') |
| 710 | p = fw; |
| 711 | while (p && *p != ' ') |
| 712 | p++; |
| 713 | while (p && *p == ' ') |
| 714 | p++; |
| 715 | if (p != NULL) { |
| 716 | memmove(p + 9, p, strlen(p) + 1); |
| 717 | memcpy(p, "near ptr ", 9); |
| 718 | jmp_near = 0; |
| 719 | } |
| 720 | } |
| 721 | if (word_imm) { |
| 722 | p = strstr(fw, ", "); |
| 723 | if (p != NULL && '0' <= p[2] && p[2] <= '9') { |
| 724 | p += 2; |
| 725 | memmove(p + 9, p, strlen(p) + 1); |
| 726 | memcpy(p, "word ptr ", 9); |
| 727 | } |
| 728 | } |
| 729 | else if (dword_imm) { |
| 730 | p = strstr(fw, ", "); |
| 731 | if (p != NULL && '0' <= p[2] && p[2] <= '9') { |
| 732 | p += 2; |
| 733 | memmove(p + 10, p, strlen(p) + 1); |
| 734 | memcpy(p, "dword ptr ", 10); |
| 735 | } |
| 736 | } |
| 737 | else if (do_pushf) { |
| 738 | p = strstr(fw, "pushf"); |
| 739 | if (p == NULL) |
| 740 | p = strstr(fw, "popf"); |
| 741 | if (p != NULL) { |
| 742 | p = strchr(p, 'f') + 1; |
| 743 | memmove(p + 1, p, strlen(p) + 1); |
| 744 | *p = 'd'; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (fw[0] == 'a' && IS_START(fw, "assume cs")) { |
| 749 | // "assume cs" causes problems with ext syms |
| 750 | memmove(fw + 1, fw, strlen(fw) + 1); |
| 751 | *fw = ';'; |
| 752 | } |
| 753 | else if (fw[0] == 'e' && IS_START(fw, "end") && fw[3] == ' ') { |
| 754 | fout_line++; |
| 755 | qfprintf(fout, "include public.inc\n\n"); |
| 756 | |
| 757 | // kill entry point |
| 758 | fw[3] = 0; |
| 759 | } |
| 760 | |
| 761 | fout_line++; |
| 762 | qfprintf(fout, "%s\n", buf); |
| 763 | } |
| 764 | |
| 765 | while (do_nops-- > 0) |
| 766 | qfprintf(fout, " nop ; adj\n"); |
| 767 | |
| 768 | // note: next_head skips some undefined stuff |
| 769 | ea = next_not_tail(ea); // correct? |
| 770 | if (ea == BADADDR) |
| 771 | break; |
| 772 | |
| 773 | pl.ea = ea; |
| 774 | pl.lnnum = 0; |
| 775 | ln.set_place(&pl); |
| 776 | } |
| 777 | |
| 778 | if (fout != NULL) |
| 779 | qfclose(fout); |
| 780 | if (fname != NULL) |
| 781 | qfree(fname); |
| 782 | |
| 783 | hide_wait_box(); |
| 784 | msg("%d lines saved.\n", fout_line); |
| 785 | } |
| 786 | |
| 787 | //-------------------------------------------------------------------------- |
| 788 | |
| 789 | static const char comment[] = "Generate disassembly for nasm"; |
| 790 | static const char help[] = "Generate asm file\n"; |
| 791 | static const char wanted_name[] = "Save asm"; |
| 792 | static const char wanted_hotkey[] = "Shift-S"; |
| 793 | |
| 794 | //-------------------------------------------------------------------------- |
| 795 | // |
| 796 | // PLUGIN DESCRIPTION BLOCK |
| 797 | // |
| 798 | //-------------------------------------------------------------------------- |
| 799 | plugin_t PLUGIN = |
| 800 | { |
| 801 | IDP_INTERFACE_VERSION, |
| 802 | 0, // plugin flags |
| 803 | init, // initialize |
| 804 | term, // terminate. this pointer may be NULL. |
| 805 | run, // invoke plugin |
| 806 | comment, // long comment about the plugin |
| 807 | // it could appear in the status line |
| 808 | // or as a hint |
| 809 | help, // multiline help about the plugin |
| 810 | wanted_name, // the preferred short name of the plugin |
| 811 | wanted_hotkey // the preferred hotkey to run the plugin |
| 812 | }; |
| 813 | |
| 814 | // vim:ts=2:shiftwidth=2:expandtab |