Exemplo n.º 1
0
static void *load_bytes(RBinFile *bf, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
	RBinNROObj *bin = R_NEW0 (RBinNROObj);
	if (!bin) {
		return NULL;
	}
	ut64 ba = baddr (bf);
	bin->methods_list = r_list_newf ((RListFree)free);
	bin->imports_list = r_list_newf ((RListFree)free);
	bin->classes_list = r_list_newf ((RListFree)free);
	ut32 mod0 = readLE32 (bf->buf, NRO_OFFSET_MODMEMOFF);
	parseMod (bf, bin, mod0, ba);
	return (void *) bin;//(size_t) check_bytes (buf, sz);
}
Exemplo n.º 2
0
static bool load_bytes(RBinFile *bf, void **bin_obj, const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
	RBinNXOObj *bin = R_NEW0 (RBinNXOObj);
	if (!bin) {
		return false;
	}
	ut64 ba = baddr (bf);
	bin->methods_list = r_list_newf ((RListFree)free);
	bin->imports_list = r_list_newf ((RListFree)free);
	bin->classes_list = r_list_newf ((RListFree)free);
	ut32 mod0 = readLE32 (bf->buf, NRO_OFFSET_MODMEMOFF);
	parseMod (bf->buf, bin, mod0, ba);
	*bin_obj = bin;
	return true;
}
Exemplo n.º 3
0
static RList *r_debug_windbg_pids(RDebug *dbg, int pid) {
	RListIter *it;
	WindProc *p;

	RList *ret = r_list_newf (free);
	if (!ret) {
		return NULL;
	}

	RList *pids = windbg_list_process(wctx);
	if (!pids) {
		return ret;
	}
	r_list_foreach (pids, it, p) {
		RDebugPid *newpid = R_NEW0 (RDebugPid);
		if (!newpid) {
			r_list_free (ret);
			return NULL;
		}
		newpid->path = strdup (p->name);
		newpid->pid = p->uniqueid;
		newpid->status = 's';
		newpid->runnable = true;
		r_list_append (ret, newpid);
	}
Exemplo n.º 4
0
static RList * relocs(RBinFile *bf) {
	RList *ret = NULL;
	RBinReloc *rel = NULL;
	const struct r_bin_mz_reloc_t *relocs = NULL;
	int i;

	if (!bf || !bf->o || !bf->o->bin_obj) {
		return NULL;
	}
	if (!(ret = r_list_newf (free))) {
		return NULL;
	}
	if (!(relocs = r_bin_mz_get_relocs (bf->o->bin_obj))) {
		return ret;
	}
	for (i = 0; !relocs[i].last; i++) {
		if (!(rel = R_NEW0 (RBinReloc))) {
			free ((void *)relocs);
			r_list_free (ret);
			return NULL;
		}
		rel->type = R_BIN_RELOC_16;
		rel->vaddr = relocs[i].paddr;
		rel->paddr = relocs[i].paddr;
		r_list_append (ret, rel);
	}
	free ((void *)relocs);
	return ret;
}
Exemplo n.º 5
0
static RList *sections(RBinFile *arch) {
	RBinWasmObj *bin = arch && arch->o ? arch->o->bin_obj : NULL;
	RList *ret = NULL;
	RList *secs = NULL;
	RBinSection *ptr = NULL;
	RBinWasmSection *sec;

	if (!(ret = r_list_newf ((RListFree)free))) {
		return NULL;
	}
	if (!(secs = r_bin_wasm_get_sections (bin))) {
		r_list_free (ret);
		return NULL;
	}
	RListIter *iter;
	r_list_foreach (secs, iter, sec) {
		if (!(ptr = R_NEW0 (RBinSection))) {
			break;
		}
		strncpy (ptr->name, (char*)sec->name, R_BIN_SIZEOF_STRINGS);
		if (sec->id == R_BIN_WASM_SECTION_DATA || sec->id == R_BIN_WASM_SECTION_MEMORY) {
			ptr->is_data = true;
		}
		ptr->size = sec->payload_len;
		ptr->vsize = sec->payload_len;
		ptr->vaddr = sec->offset;
		ptr->paddr = sec->offset;
		ptr->add = true;
		// TODO permissions
		ptr->srwx = 0;
		r_list_append (ret, ptr);
	}
	return ret;
}
Exemplo n.º 6
0
static RList *parseSegments(RBuffer *buf, int off, int count) {
	ut8 *b = calloc (count, 32);
	(void)r_buf_read_at (buf, off, b, count * 32);
	int x = off;
	int X = 0;
	int i;
	RList *segments = r_list_newf ((RListFree)r_bin_section_free);
	if (!segments) {
		return NULL;
	}
	// eprintf ("Segments: %d\n", count);
	for (i = 0; i < count; i++) {
		int A = r_read_le32 (b + X + 16);
		int B = r_read_le32 (b + X + 16 + 8);
		//	eprintf ("0x%08x  segment  0x%08x 0x%08x  %s\n",
		//		x, A, A + B, b + X);
		const char *cname = (const char *)(b + X);
		char *name = r_str_ndup (cname, r_str_nlen (cname, 16));
		RBinSection *section = newSection (name, A, A + B, true);
		free (name);
		r_list_append (segments, section);
		x += 32;
		X += 32;
	}
	return segments;
}
Exemplo n.º 7
0
static bool spaces_unset_single(RSpaces *sp, const char *name) {
	RSpace *space = r_spaces_get (sp, name);
	if (!space) {
		return false;
	}

	RSpaceEvent ev = { .data.unset.space = space };
	r_event_send (sp->event, R_SPACE_EVENT_UNSET, &ev);
	return r_rbtree_delete (&sp->spaces, (void *)name, name_space_cmp, space_node_free);
}

R_API bool r_spaces_unset(RSpaces *sp, const char *name) {
	if (name) {
		return spaces_unset_single (sp, name);
	}

	RList *names = r_list_newf ((RListFree)free);
	if (!names) {
		return false;
	}

	RBIter it;
	RSpace *s;
	r_spaces_foreach (sp, it, s) {
		r_list_append (names, strdup (s->name));
	}

	RListIter *lit;
	const char *n;
	bool res = false;
	r_list_foreach (names, lit, n) {
		res |= spaces_unset_single (sp, n);
	}
Exemplo n.º 8
0
static RList * build_str_list_from_iterable (RList *the_list) {
	RList * res = r_list_newf (free);
	DsoJsonObj *json_obj;
	RListIter *iter;
	r_list_foreach (the_list, iter, json_obj) {
		char *str = dso_json_obj_to_str (json_obj);
		r_list_append (res, str);
	}
Exemplo n.º 9
0
Arquivo: buf.c Projeto: P4N74/radare2
R_API RBuffer *r_buf_new_sparse() {
	RBuffer *b = r_buf_new ();
	if (!b) {
		return NULL;
	}
	b->sparse = r_list_newf ((RListFree)free);
	return b;
}
Exemplo n.º 10
0
static RList *r_debug_native_modules_get (RDebug *dbg) {
	char *lastname = NULL;
	RDebugMap *map;
	RListIter *iter, *iter2;
	RList *list, *last;
	int must_delete;
#if __APPLE__
	list = xnu_dbg_maps (dbg, 1);
	if (list && !r_list_empty (list)) {
		return list;
	}
#endif
#if __WINDOWS__
	list = w32_dbg_modules (dbg);
	if (list && !r_list_empty (list)) {
		return list;
	}
#endif
	list = r_debug_native_map_get (dbg);
	if (!list) {
		return NULL;
	}
	last = r_list_newf ((RListFree)r_debug_map_free);
	if (!last) {
		r_list_free (list);
		return NULL;
	}
	r_list_foreach_safe (list, iter, iter2, map) {
		const char *file = map->file;
		if (!map->file) {
			file = map->file = strdup (map->name);
		}
		must_delete = 1;
		if (file && *file) {
			if (file[0] == '/') {
				if (lastname) {
					if (strcmp (lastname, file)) {
						must_delete = 0;
					}
				} else {
					must_delete = 0;
				}
			}
		}
		if (must_delete) {
			r_list_delete (list, iter);
		} else {
			r_list_append (last, map);
			free (lastname);
			lastname = strdup (file);
		}
	}
	list->free = NULL;
	free (lastname);
	r_list_free (list);
	return last;
}
Exemplo n.º 11
0
static RList* entries(RBinFile *arch) {
	RBinAddr *ptr = NULL;
	RList* ret = r_list_newf (free);
	if (!ret) return NULL;
	if ((ptr = R_NEW0 (RBinAddr))) {
		r_list_append (ret, ptr);
	}
	return ret;
}
Exemplo n.º 12
0
R_API RList *r_syscall_list(RSyscall *s) {
	RList *list;
	if (!s || !s->db) {
		return NULL;
	}
	// show list of syscalls to stdout
	list = r_list_newf ((RListFree)r_syscall_item_free);
	sdb_foreach (s->db, callback_list, list);
	return list;
}
Exemplo n.º 13
0
static RList* sections(RBinFile *arch) {
	RList *ret = NULL;
	RBinSection *ptr = NULL;
	ut64 textsize, datasize, symssize, spszsize, pcszsize;
	ut64 entry0 = findEntry (arch->buf, 0);
	ut64 entry1 = findEntry (arch->buf, 1);
	ut64 entry2 = findEntry (arch->buf, 2);

	if (!(ret = r_list_newf (free))) {
		return NULL;
	}
	// add text segment
	textsize = r_mem_get_num (arch->buf->buf + 4, 4);
	if (!(ptr = R_NEW0 (RBinSection))) {
		return ret;
	}
	if (!entry1) {
		entry1 = arch->buf->length;
	}
	strncpy (ptr->name, "init", R_BIN_SIZEOF_STRINGS);
	ptr->size = entry1-entry0;
	ptr->vsize = entry1-entry0;
	ptr->paddr = entry0 + 4;
	ptr->vaddr = entry0;
	ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; // r-x
	r_list_append (ret, ptr);

	if (entry1) {
		if (entry2) {
			if (!(ptr = R_NEW0 (RBinSection)))
				return ret;
			strncpy (ptr->name, "fini", R_BIN_SIZEOF_STRINGS);
			ptr->size = entry2-entry1;
			ptr->vsize = entry2-entry1;
			ptr->paddr = entry1 + 4;
			ptr->vaddr = entry1;
			ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; // r-x
			r_list_append (ret, ptr);
		} else {
			entry2 = entry1;
		}
	}
	if (entry2) {
		if (!(ptr = R_NEW0 (RBinSection)))
			return ret;
		strncpy (ptr->name, "text", R_BIN_SIZEOF_STRINGS);
		ptr->size = arch->buf->length - entry2;
		ptr->vsize = arch->buf->length - entry2;
		ptr->paddr = entry2 + 4;
		ptr->vaddr = entry2;
		ptr->srwx = R_BIN_SCN_READABLE | R_BIN_SCN_EXECUTABLE | R_BIN_SCN_MAP; // r-x
		r_list_append (ret, ptr);
	}
	return ret;
}
Exemplo n.º 14
0
static RList *entries(RBinFile *arch) {
	struct r_bin_coff_obj *obj = (struct r_bin_coff_obj*)arch->o->bin_obj;
	RList *ret;
	RBinAddr *ptr = NULL;
	if (!(ret = r_list_newf (free))) {
		return NULL;
	}
	ptr = r_coff_get_entry(obj);
	r_list_append(ret, ptr);
	return ret;
}
Exemplo n.º 15
0
static RList *sections(RBinFile *arch) {
	char *tmp, *coffname = NULL;
	size_t i;
	RList *ret = NULL;
	RBinSection *ptr = NULL;
	struct r_bin_coff_obj *obj = (struct r_bin_coff_obj*)arch->o->bin_obj;

	ret = r_list_newf (free);
	if (!ret) {
		return NULL;
	}
	if (obj && obj->scn_hdrs) {
		for (i = 0; i < obj->hdr.f_nscns; i++) {
			free (coffname);
			tmp = r_coff_symbol_name (obj, &obj->scn_hdrs[i]);
			if (!tmp) {
				r_list_free (ret);
				return NULL;
			}
			//IO does not like sections with the same name append idx
			//since it will update it
			coffname = r_str_newf ("%s-%d", tmp, i);
			free (tmp); 
			ptr = R_NEW0 (RBinSection);
			if (!ptr) {
				free (coffname);
				return ret;
			}
			strncpy (ptr->name, coffname, R_BIN_SIZEOF_STRINGS);
			if (strstr (ptr->name, "data")) {
				ptr->is_data = true;
			}
			ptr->size = obj->scn_hdrs[i].s_size;
			ptr->vsize = obj->scn_hdrs[i].s_size;
			ptr->paddr = obj->scn_hdrs[i].s_scnptr;
			ptr->add = true;
			ptr->srwx = R_BIN_SCN_MAP;
			if (obj->scn_hdrs[i].s_flags & COFF_SCN_MEM_READ) {
				ptr->srwx |= R_BIN_SCN_READABLE;
			}
			if (obj->scn_hdrs[i].s_flags & COFF_SCN_MEM_WRITE) {
				ptr->srwx |= R_BIN_SCN_WRITABLE;
			}
			if (obj->scn_hdrs[i].s_flags & COFF_SCN_MEM_EXECUTE) {
				ptr->srwx |= R_BIN_SCN_EXECUTABLE;
			}
			r_list_append (ret, ptr);
		}
	}
	free (coffname);
	return ret;
}
Exemplo n.º 16
0
static RList *parseStrings(RBuffer *buf, int string_section, int string_section_end) {
	int sss = string_section_end + string_section;
	if (sss < 1) {
		return NULL;
	}
	char *b = calloc (1, sss);
	if (!b) {
		return NULL;
	}
	int o = 0;
	char *s = b;
	char *os = s;
	int nstrings = 0;

	int available = r_buf_read_at (buf, string_section, (ut8 *)b, sss);
	if (available != sss) {
		sss = available;
	}
	if (sss < 1) {
		eprintf ("Cannot read strings at 0x%08" PFMT64x "\n", (ut64)string_section);
		free (b);
		return NULL;
	}
	RList *res = r_list_newf ((RListFree)r_bin_string_free);
	int i;
	char *s_end = s + sss;
	for (i = 0; true; i++) {
		o = s - os;
		if (string_section + o + 8 > string_section_end) {
			break;
		}
		if (s + 4 > s_end) {
			break;
		}
		nstrings++;
		//	eprintf ("0x%08x  0x%08x %s\n", o + string_section, o, s);
		RBinString *bs = R_NEW0 (RBinString);
		if (!bs) {
			break;
		}
		bs->string = strdup (s);
		// eprintf ("%s\n", s);
		bs->vaddr = o + string_section;
		bs->paddr = o + string_section;
		bs->ordinal = i;
		bs->length = strlen (s);
		r_list_append (res, bs);
		s += bs->length + 1;
	}
	free (b);
	return res;
}
Exemplo n.º 17
0
static RList *entries(RBinFile *bf) {
	RList /*<RBinAddr>*/ *ret = r_list_newf (free);
	if (!ret) {
		return NULL;
	}
	RBinAddr *ptr = R_NEW0 (RBinAddr);
	if (ptr) {
		ptr->paddr = N64_ROM_START;
		ptr->vaddr = baddr (bf);
		r_list_append (ret, ptr);
	}
	return ret;
}
Exemplo n.º 18
0
static RList *entries(RBinFile *bf) {
	RList *ret;
	RBinAddr *ptr = NULL;

	if (!(ret = r_list_newf (free))) {
		return NULL;
	}
	if (!(ptr = R_NEW0 (RBinAddr))) {
		return ret;
	}
	ptr->paddr = ptr->vaddr = 0;
	r_list_append (ret, ptr);
	return ret;
}
Exemplo n.º 19
0
static RList *imports(RBinFile *bf) {
	RBinWasmObj *bin = NULL;
	RList *imports = NULL;
	RBinImport *ptr = NULL;
	RList *ret = NULL;

	if (!bf || !bf->o || !bf->o->bin_obj) {
		return NULL;
	}
	bin = bf->o->bin_obj;
	if (!(ret = r_list_newf (r_bin_import_free))) {
		return NULL;
	}
	if (!(imports = r_bin_wasm_get_imports (bin))) {
		goto bad_alloc;
	}

	RBinWasmImportEntry *import = NULL;
	ut32 i = 0;
	RListIter *iter;
	r_list_foreach (imports, iter, import) {
		if (!(ptr = R_NEW0 (RBinImport))) {
			goto bad_alloc;
		}
		ptr->name = strdup (import->field_str);
		ptr->classname = strdup (import->module_str);
		ptr->ordinal = i;
		ptr->bind = r_str_const ("NONE");
		switch(import->kind) {
		case R_BIN_WASM_EXTERNALKIND_Function:
			ptr->type = r_str_const ("FUNC");
			break;
		case R_BIN_WASM_EXTERNALKIND_Table:
			ptr->type = r_str_const ("TABLE");
			break;
		case R_BIN_WASM_EXTERNALKIND_Memory:
			ptr->type = r_str_const ("MEM");
			break;
		case R_BIN_WASM_EXTERNALKIND_Global:
			ptr->type = r_str_const ("GLOBAL");
			break;
		}
		r_list_append (ret, ptr);
	}
	return ret;
bad_alloc:
	r_list_free (imports);
	r_list_free (ret);
	return NULL;
}
Exemplo n.º 20
0
static RList *r_debug_native_map_get(RDebug *dbg) {
	RIOBdescbg *o = dbg->iob.io->desc->data;
	BfvmCPU *c = o->bfvm;
	RList *list = r_list_newf ((RListFree)r_debug_map_free);
	if (!list) return NULL;
	r_list_append (list, r_debug_map_new (
		"code", 0, 4096, 6, 0));
	r_list_append (list, r_debug_map_new (
		"memory", c->base, c->base+c->size, 6, 0));
	r_list_append (list, r_debug_map_new (
		"screen", c->screen, c->screen+c->screen_size, 6, 0));
	r_list_append (list, r_debug_map_new (
		"input", c->input, c->input+c->input_size, 6, 0));
	return list;
}
Exemplo n.º 21
0
static RList * entries(RBinFile *bf) {
	RBinAddr *ptr = NULL;
	RList *res = NULL;
	if (!(res = r_list_newf (free))) {
		return NULL;
	}
	int entry = r_bin_mz_get_entrypoint (bf->o->bin_obj);
	if (entry >= 0) {
		if ((ptr = R_NEW0 (RBinAddr))) {
			ptr->paddr = (ut64) entry;
			ptr->vaddr = (ut64) entry;
			r_list_append (res, ptr);
		}
	}
	return res;
}
Exemplo n.º 22
0
static RList *entries(RBinFile *bf) {
	struct r_bin_bflt_obj *obj = (struct r_bin_bflt_obj *) bf->o->bin_obj;
	RList *ret;
	RBinAddr *ptr;

	if (!(ret = r_list_newf (free))) {
		return NULL;
	}
	ptr = r_bflt_get_entry (obj);
	if (!ptr) {
		r_list_free (ret);
		return NULL;
	}
	r_list_append (ret, ptr);
	return ret;
}
Exemplo n.º 23
0
static RList *entries(RBinFile *arch) {
	RList *ret = r_list_newf (free);
	RBinAddr *ptr = NULL;

	if (arch && arch->buf) {
		if (!ret) {
			return NULL;
		}
		if (!(ptr = R_NEW0 (RBinAddr))) {
			return ret;
		}
		ptr->paddr = ptr->vaddr = 0x8000000;
		r_list_append (ret, ptr);
	}
	return ret;
}
Exemplo n.º 24
0
static RList* lines(RBinFile *arch) {
	char *file = arch->file ? strdup (arch->file) : strdup ("");
	RList *list = r_list_newf (free);
	file = r_str_replace (file, ".class", ".java", 0);

	/*
	int i;
	RBinJavaObj *b = arch->o->bin_obj;
	for (i=0; i<b->lines.count; i++) {
		RBinDwarfRow *row = R_NEW0(RBinDwarfRow);
		r_bin_dwarf_line_new (row, b->lines.addr[i], file, b->lines.line[i]);
		r_list_append (list, row);
	}*/
	free (file);
	return list;
}
Exemplo n.º 25
0
static RList * oneshotall(RBin *bin, const ut8 *buf, ut64 size) {
	RList *res = NULL;
	int narch, i = 0;
	RBinXtrData *data = oneshot (bin, buf, size, i);

	if (!data) return res;
	// XXX - how do we validate a valid narch?
	narch = data->file_count;
	res = r_list_newf (r_bin_xtrdata_free);
	r_list_append (res, data);
	for (i = 1; data && i < narch; i++) {
		data = NULL;
		data = oneshot (bin, buf, size, i);
		r_list_append (res, data);
	}

	return res;
}
Exemplo n.º 26
0
static RList* entries(RBinFile *bf) {
	RList* ret = r_list_newf (free);
	RBinAddr *ptr = NULL;
	if (ret) {
		if ((ptr = R_NEW0 (RBinAddr))) {
			ut64 entry = (ut64)readLE32(bf->buf, 0x80);
			ut64 size = (ut64)readLE32(bf->buf, 0x84);
			// eprintf ("0x%x 0x%x\n", entry, size);
			ptr->paddr = entry;
			ptr->vaddr = entry;
			r_list_append (ret, ptr);
		} else {
			r_list_free (ret);
			ret = NULL;
		}
	}
	return ret;
}
Exemplo n.º 27
0
R_API RList *r_core_list_themes(RCore *core) {
	RList *list = r_list_newf (free);
	getNext = false;

	char *path = r_str_home (R2_HOME_THEMES R_SYS_DIR);
	if (path) {
		list_themes_in_path (list, path);
		R_FREE (path);
	}

	path = r_str_r2_prefix (R2_THEMES R_SYS_DIR);
	if (path) {
		list_themes_in_path (list, path);
		R_FREE (path);
	}

	return list;
}
Exemplo n.º 28
0
static RList *r_debug_bochs_map_get(RDebug* dbg) { //TODO
	//eprintf("bochs_map_getdebug:\n");
	RDebugMap *mr;
	RList *list = r_list_newf ((RListFree)map_free);
	if (!list) return NULL;
	mr = R_NEW0 (RDebugMap);
	if (!mr) {
		r_list_free (list);
		return NULL;
	}
	mr->name = strdup ("fake");
	mr->addr = 0;
	mr->addr_end = UT32_MAX;
	mr->size = UT32_MAX;
	mr->perm = 0;
	mr->user = 0;
	r_list_append (list, mr);
	return list;
}
Exemplo n.º 29
0
static RList *lines(RBinFile *bf) {
	return NULL;
#if 0
	char *file = bf->file? strdup (bf->file): strdup ("");
	RList *list = r_list_newf (free);
	// XXX the owner of this list should be the plugin, so we are leaking here
	file = r_str_replace (file, ".class", ".java", 0);
	/*
	   int i;
	   RBinJavaObj *b = bf->o->bin_obj;
	   for (i=0; i<b->lines.count; i++) {
	        RBinDwarfRow *row = R_NEW0(RBinDwarfRow);
	        r_bin_dwarf_line_new (row, b->lines.addr[i], file, b->lines.line[i]);
	        r_list_append (list, row);
	   }*/
	free (file);
	return list;
#endif
}
Exemplo n.º 30
0
static RList * extractall(RBin *bin) {
	RList *res = NULL;
	int narch, i = 0;
	RBinXtrData *data = NULL;

	data = extract (bin, i);
	if (!data) return res;

	// XXX - how do we validate a valid narch?
	narch = data->file_count;
	res = r_list_newf (r_bin_xtrdata_free);
	r_list_append (res, data);
	for (i = 1; data && i < narch; i++) {
		data = NULL;
		data = extract (bin, i);
		r_list_append (res, data);
	}
	return res;
}