static int lang_lib_file_run (RLang *user, const char *file) { char *libpath; void *lib; if (!(libpath = r_str_new (file))) { return -1; } if (!r_str_startswith (libpath, "/") && !r_str_startswith (libpath, "./")) { libpath = r_str_prefix (libpath, "./"); } if (!r_file_exists (libpath)) { if (!r_str_endswith (libpath, R_LIB_EXT)) { libpath = r_str_appendf (libpath, ".%s", R_LIB_EXT); } } if (!r_file_exists (libpath)) { free (libpath); return -1; } lib = r_lib_dl_open (libpath); if (lib) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) { fcn (user->user); } else { eprintf ("Cannot find 'entry' symbol in library\n"); } r_lib_dl_close (lib); } free (libpath); return 0; }
R_API int r_debug_signal_resolve(RDebug *dbg, const char *signame) { int ret; char *name; if (strchr (signame, '.')) return 0; name = strdup (signame); r_str_case (name, R_TRUE); if (strncmp (name, "SIG", 3)) name = r_str_prefix (name, "SIG"); ret = (int)sdb_num_get (DB, name, 0); free (name); return ret; }
/* umf..this should probably be outside this file */ R_API char* r_anal_reflines_str(RAnal *anal, RAnalRefline *list, ut64 addr, int opts) { int l, linestyle = opts & R_ANAL_REFLINE_TYPE_STYLE; int dir = 0, wide = opts & R_ANAL_REFLINE_TYPE_WIDE; char ch = ' ', *str = NULL; struct list_head *pos; RAnalRefline *ref; if (!list) return NULL; str = r_str_concat (str, " "); for (pos = linestyle?(&(list->list))->next:(&(list->list))->prev; pos != (&(list->list)); pos = linestyle?pos->next:pos->prev) { ref = list_entry (pos, RAnalRefline, list); dir = (addr == ref->to)? 1: (addr == ref->from)? 2: dir; if (addr == ref->to) { str = r_str_concat (str, (ref->from>ref->to)?".":"`"); ch = '-'; } else if (addr == ref->from) { str = r_str_concat (str, (ref->from>ref->to)?"`":","); ch = '='; } else if (ref->from < ref->to) { if (addr > ref->from && addr < ref->to) { if (ch=='-'||ch=='=') str = r_str_concatch (str, ch); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } else { if (addr < ref->from && addr > ref->to) { if (ch=='-'||ch=='=') str = r_str_concatch (str, ch); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } if (wide) str = r_str_concatch (str, (ch=='='||ch=='-')?ch:' '); } str = r_str_concat (str, (dir==1)?"-> ":(dir==2)?"=< ":" "); if (anal->lineswidth>0) { l = strlen (str); if (l>anal->lineswidth) r_str_cpy (str, str+l-anal->lineswidth); } for (l = anal->lineswidth-strlen (str);l-->0;) str = r_str_prefix (str, " "); return str; }
static int gdbr_parse_target_xml(libgdbr_t *g, char *xml_data, ut64 len) { char *regstr, *flagstr, *tmp, *profile = NULL, pc_alias[64], flag_bits[65]; RList *flags, *regs; RListIter *iter; gdbr_xml_flags_t *tmpflag; gdbr_xml_reg_t *tmpreg; ut64 profile_len = 0, profile_max_len, regnum = 0, regoff = 0; pc_alias[0] = '\0'; gdb_reg_t *arch_regs = NULL; if (_resolve_arch (g, xml_data) < 0) { return -1; } if (!(flagstr = strstr (xml_data, "<feature"))) { return -1; } regstr = flagstr; if (!(flags = _extract_flags (flagstr))) { return -1; } if (!(regs = _extract_regs (regstr, flags, pc_alias))) { r_list_free (flags); return -1; } if (!(arch_regs = malloc (sizeof (gdb_reg_t) * (r_list_length (regs) + 1)))) { goto exit_err; } // approximate per-reg size estimates profile_max_len = r_list_length (regs) * 128 + r_list_length (flags) * 128; if (!(profile = malloc (profile_max_len))) { goto exit_err; } r_list_foreach (regs, iter, tmpreg) { if (!tmpreg) { continue; } // regsize > 64 not supported by r2 currently if (tmpreg->size > 8) { regoff += tmpreg->size; continue; } memcpy (arch_regs[regnum].name, tmpreg->name, sizeof (tmpreg->name)); arch_regs[regnum].size = tmpreg->size; arch_regs[regnum].offset = regoff; if (profile_len + 128 >= profile_max_len) { if (!(tmp = realloc (profile, profile_max_len + 512))) { goto exit_err; } profile = tmp; profile_max_len += 512; } flag_bits[0] = '\0'; tmpflag = NULL; if (tmpreg->flagnum < r_list_length (flags)) { tmpflag = r_list_get_n (flags, tmpreg->flagnum); _write_flag_bits (flag_bits, tmpflag); } profile_len += snprintf (profile + profile_len, 128, "%s\t%s\t" ".%u\t%"PFMT64d"\t0\t%s\n", tmpreg->type, tmpreg->name, tmpreg->size * 8, regoff, flag_bits); // TODO write flag subregisters if (tmpflag) { int i; for (i = 0; i < tmpflag->num_fields; i++) { if (profile_len + 128 >= profile_max_len) { if (!(tmp = realloc (profile, profile_max_len + 512))) { goto exit_err; } profile = tmp; profile_max_len += 512; } profile_len += snprintf (profile + profile_len, 128, "gpr\t%s\t" ".%u\t.%"PFMT64d"\t0\n", tmpflag->fields[i].name, tmpflag->fields[i].sz, tmpflag->fields[i].bit_num + (regoff * 8)); } } regnum++; regoff += tmpreg->size; } // Difficult to parse these out from xml. So manually added from gdb's xml files switch (g->target.arch) { case R_SYS_ARCH_ARM: switch (g->target.bits) { case 32: if (!(profile = r_str_prefix (profile, "=PC r15\n" "=SP r14\n" // XXX "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" ))) { goto exit_err; } break; case 64: if (!(profile = r_str_prefix (profile, "=PC pc\n" "=SP sp\n" "=BP x29\n" "=A0 x0\n" "=A1 x1\n" "=A2 x2\n" "=A3 x3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN x8\n" ))) { goto exit_err; } } break; break; case R_SYS_ARCH_X86: switch (g->target.bits) { case 32: if (!(profile = r_str_prefix (profile, "=PC eip\n" "=SP esp\n" "=BP ebp\n"))) { goto exit_err; } break; case 64: if (!(profile = r_str_prefix (profile, "=PC rip\n" "=SP rsp\n" "=BP rbp\n"))) { goto exit_err; } } break; case R_SYS_ARCH_MIPS: if (!(profile = r_str_prefix (profile, "=PC pc\n" "=SP r29\n"))) { goto exit_err; } break; default: // TODO others if (*pc_alias) { if (!(profile = r_str_prefix (profile, pc_alias))) { goto exit_err; } } } // Special case for MIPS, since profile doesn't separate 32/64 bit MIPS if (g->target.arch == R_SYS_ARCH_MIPS) { if (arch_regs && arch_regs[0].size == 8) { g->target.bits = 64; } } r_list_free (flags); r_list_free (regs); free (g->target.regprofile); if (profile) { g->target.regprofile = strdup (profile); free (profile); } g->target.valid = true; g->registers = arch_regs; return 0; exit_err: r_list_free (flags); r_list_free (regs); free (profile); free (arch_regs); return -1; }
// TODO: move into another file // TODO: this is TOO SLOW. do not iterate over all reflines or gtfo R_API char* r_anal_reflines_str(void *core, ut64 addr, int opts) { int l, linestyle = opts & R_ANAL_REFLINE_TYPE_STYLE; int dir = 0, wide = opts & R_ANAL_REFLINE_TYPE_WIDE; char ch = ' ', *str = NULL; struct list_head *pos; RAnalRefline *ref, *list = ((RCore*)core)->reflines; if (!list) return NULL; str = r_str_concat (str, " "); for (pos = linestyle?(&(list->list))->next:(&(list->list))->prev; pos != (&(list->list)); pos = linestyle?pos->next:pos->prev) { ref = list_entry (pos, RAnalRefline, list); dir = (addr == ref->to)? 1: (addr == ref->from)? 2: dir; if (addr == ref->to) { str = r_str_concat (str, (ref->from>ref->to)? "." : "`"); ch = '-'; } else if (addr == ref->from) { str = r_str_concat (str, (ref->from>ref->to)? "`" : "," ); ch = '='; } else if (ref->from < ref->to) { if (addr > ref->from && addr < ref->to) { if (ch=='-' || ch=='=') str = r_str_concatch (str, ch); //else str = r_str_concat (str, ((RCore*)core)->cons->vline[LINE_VERT]); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } else { if (addr < ref->from && addr > ref->to) { if (ch=='-' || ch=='=') str = r_str_concatch (str, ch); //else str = r_str_concat (str, ((RCore*)core)->cons->vline[LINE_VERT]); else str = r_str_concatch (str, '|'); } else str = r_str_concatch (str, ch); } if (wide) str = r_str_concatch (str, (ch=='=' || ch=='-')? ch : ' '); } //str = r_str_concat (str, (dir==1)?"-> ":(dir==2)?"=< ":" "); str = r_str_concat (str, (dir==1)? "-> " :(dir==2)? "=< " : " "); if (((RCore*)core)->anal->lineswidth>0) { l = r_str_len_utf8 (str); if (l > ((RCore*)core)->anal->lineswidth) r_str_cpy (str, str + l - ((RCore*)core)->anal->lineswidth); } /* HACK */ if (((RCore*)core)->utf8 && ((RCore*)core)->cons->vline) { RCons *cons = ((RCore*)core)->cons; //str = r_str_replace (str, "=", "-", 1); str = r_str_replace (str, "<", cons->vline[ARROW_LEFT], 1); str = r_str_replace (str, ">", cons->vline[ARROW_RIGHT], 1); str = r_str_replace (str, "|", cons->vline[LINE_VERT], 1); str = r_str_replace (str, "=", cons->vline[LINE_HORIZ], 1); str = r_str_replace (str, "-", cons->vline[LINE_HORIZ], 1); //str = r_str_replace (str, ".", "\xe2\x94\x8c", 1); str = r_str_replace (str, ",", cons->vline[LUP_CORNER], 1); str = r_str_replace (str, ".", cons->vline[LUP_CORNER], 1); str = r_str_replace (str, "`", cons->vline[LDWN_CORNER], 1); } if (((RCore*)core)->anal->lineswidth>0) { char pfx[128]; int l = ((RCore*)core)->anal->lineswidth-r_str_len_utf8 (str); memset (pfx, ' ', sizeof (pfx)); if (l>=sizeof(pfx)) l = sizeof (pfx)-1; pfx[l] = 0; str = r_str_prefix (str, pfx); } return str; }