Some fixes IO related

This commit is contained in:
alvarofe 2017-09-04 15:05:12 +02:00
parent 7a5498dc86
commit ee98981940
6 changed files with 97 additions and 60 deletions

View File

@ -443,7 +443,7 @@ static void map_list(RIO *io, int mode, RPrint *print, int fd) {
case '*':
case 'r':
print->cb_printf ("om %d 0x%"PFMT64x" 0x%"PFMT64x" 0x%"PFMT64x"\n", map->fd,
map->from, map->to - map->from, map->delta);
map->from, map->to - map->from + 1, map->delta);
break;
default:
print->cb_printf ("%2d fd: %i +0x%08"PFMT64x" 0x%08"PFMT64x

View File

@ -8,7 +8,7 @@
static const char *help_msg_S[] = {
"Usage:","S[?-.*=adlr] [...]","",
"S","","list sections",
"S"," paddr va sz [vsz] name mrwx","add new section (if(!vsz)vsz=sz)",
"S"," paddr va sz [vsz] name rwx","add new section (if(!vsz)vsz=sz)",
"S.","","show current section name",
"S.-*","","remove all sections in current offset",
"S*","","list sections (in radare commands)",
@ -517,7 +517,7 @@ static int cmd_section(void *data, const char *input) {
ut64 paddr = 0LL;
ut64 size = 0LL;
ut64 vsize = 0LL;
int fd = r_core_file_cur_fd(core);
int fd = r_core_file_cur_fd (core);
i = r_str_word_set0 (ptr);
switch (i) {
case 6: // get rwx
@ -546,7 +546,8 @@ static int cmd_section(void *data, const char *input) {
sprintf (vname, "area%d", (int)ls_length (core->io->sections));
name = vname;
}
r_io_section_add (core->io, paddr, vaddr, size, vsize, rwx, name, 0, fd);
RIOSection *sec = r_io_section_add (core->io, paddr, vaddr, size, vsize, rwx, name, -1, fd);
r_io_create_mem_for_section (core->io, sec);
free (ptr);
}
break;

View File

@ -470,6 +470,9 @@ R_API bool r_io_use_fd (RIO *io, int fd);
#define r_io_range_free(x) free(x)
/* io/ioutils.c */
R_API bool r_io_create_mem_map(RIO *io, RIOSection *sec, ut64 at, bool null);
R_API bool r_io_create_file_map(RIO *io, RIOSection *sec, ut64 size, bool patch);
R_API bool r_io_create_mem_for_section(RIO *io, RIOSection *sec);
R_API bool r_io_is_valid_offset (RIO *io, ut64 offset, int hasperm);
R_API bool r_io_addr_is_mapped(RIO *io, ut64 vaddr);
R_API bool r_io_read_i (RIO* io, ut64 addr, ut64 *val, int size, bool endian);

View File

@ -10,6 +10,91 @@ static int __access_log_e_cmp (const void *a, const void *b) {
return (A->buf_idx > B->buf_idx);
}
R_API bool r_io_create_mem_map(RIO *io, RIOSection *sec, ut64 at, bool null) {
RIOMap *map = NULL;
RIODesc *desc = NULL;
char *uri = NULL;
if (!io || !sec) {
return false;
}
if (null) {
uri = r_str_newf ("null://%"PFMT64u "", sec->vsize - sec->size);
} else {
uri = r_str_newf ("malloc://%"PFMT64u "", sec->vsize - sec->size);
}
desc = r_io_open_at (io, uri, sec->flags, 664, at);
free (uri);
if (!desc) {
return false;
}
// this works, because new maps are allways born on the top
map = r_io_map_get (io, at);
// check if the mapping failed
if (!map) {
r_io_desc_close (desc);
return false;
}
// let the section refere to the map as a memory-map
sec->memmap = map->id;
map->name = r_str_newf ("mmap.%s", sec->name);
return true;
}
R_API bool r_io_create_file_map(RIO *io, RIOSection *sec, ut64 size, bool patch) {
RIOMap *map = NULL;
int flags = 0;
RIODesc *desc;
if (!io || !sec) {
return false;
}
desc = r_io_desc_get (io, sec->fd);
if (!desc) {
return false;
}
flags = sec->flags;
//create file map for patching
if (patch) {
//add -w to the map for patching if needed
//if the file was not opened with -w desc->flags won't have that bit active
flags = flags | desc->flags;
}
map = r_io_map_add (io, sec->fd, flags, sec->paddr, sec->vaddr, size, false);
if (map) {
sec->filemap = map->id;
map->name = r_str_newf ("fmap.%s", sec->name);
return true;
}
return false;
}
R_API bool r_io_create_mem_for_section(RIO *io, RIOSection *sec) {
RIOMap *map = NULL;
RIODesc *desc = NULL;
char *uri = NULL;
if (!io || !sec) {
return false;
}
if (sec->vsize - sec->size > 0) {
ut64 at = sec->vaddr + sec->size;
if (!r_io_create_mem_map (io, sec, at, false)) {
return false;
}
RIOMap *map = r_io_map_get (io, at);
r_io_map_set_name (map, sdb_fmt (0, "mem.%s", sec->name));
}
if (sec->size) {
if (!r_io_create_file_map (io, sec, sec->size, false)) {
return false;
}
RIOMap *map = r_io_map_get (io, sec->vaddr);
r_io_map_set_name (map, sdb_fmt (1, "fmap.%s", sec->name));
}
return true;
}
//This helper function only check if the given vaddr is mapped, it does not account
//for map perms
R_API bool r_io_addr_is_mapped(RIO *io, ut64 vaddr) {

View File

@ -165,7 +165,7 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) {
}
int len;
ut8 *data = (ut8*)r_file_slurp (pathname+7, &len); //memleak here?
mal->buf = r_inflate (data, len, NULL, &mal->size);
mal->buf = r_inflate (data, len, NULL, (int*)&mal->size);
if (mal->buf) {
return r_io_desc_new (io, &r_io_plugin_gzip, pathname, rw, mode, mal);
}

View File

@ -395,59 +395,7 @@ R_API bool r_io_section_priorize_bin(RIO *io, ut32 bin_id) {
return true;
}
static bool _create_null_map(RIO *io, RIOSection *sec, ut64 at) {
RIOMap *map = NULL;
RIODesc *desc = NULL;
char *uri = NULL;
if (!io || !sec) {
return false;
}
uri = r_str_newf ("null://%"PFMT64u "", sec->vsize - sec->size);
desc = r_io_open_at (io, uri, sec->flags, 664, at);
free (uri);
if (!desc) {
return false;
}
// this works, because new maps are allways born on the top
map = r_io_map_get (io, at);
// check if the mapping failed
if (!map) {
r_io_desc_close (desc);
return false;
}
// let the section refere to the map as a memory-map
sec->memmap = map->id;
map->name = r_str_newf ("mmap.%s", sec->name);
return true;
}
static bool _create_file_map(RIO *io, RIOSection *sec, ut64 size, bool patch) {
RIOMap *map = NULL;
int flags = 0;
RIODesc *desc;
if (!io || !sec) {
return false;
}
desc = r_io_desc_get (io, sec->fd);
if (!desc) {
return false;
}
flags = sec->flags;
//create file map for patching
if (patch) {
//add -w to the map for patching if needed
//if the file was not opened with -w desc->flags won't have that bit active
flags = flags | desc->flags;
}
map = r_io_map_add (io, sec->fd, flags, sec->paddr, sec->vaddr, size, false);
if (map) {
sec->filemap = map->id;
map->name = r_str_newf ("fmap.%s", sec->name);
return true;
}
return false;
}
static bool _section_apply_for_anal_patch(RIO *io, RIOSection *sec, bool patch) {
ut64 at;
@ -458,11 +406,11 @@ static bool _section_apply_for_anal_patch(RIO *io, RIOSection *sec, bool patch)
at = sec->vaddr + sec->size;
// TODO: harden this, handle mapslit
// craft the uri for the null-fd
if (!_create_null_map (io, sec, at)) {
if (!r_io_create_mem_map (io, sec, at, true)) {
return false;
}
// we need to create this map for transfering the flags, no real remapping here
if (!_create_file_map (io, sec, sec->size, patch)) {
if (!r_io_create_file_map (io, sec, sec->size, patch)) {
return false;
}
return true;
@ -473,7 +421,7 @@ static bool _section_apply_for_anal_patch(RIO *io, RIOSection *sec, bool patch)
} else {
// same as above
if (!sec->filemap) {
if (_create_file_map (io, sec, sec->vsize, patch)) {
if (r_io_create_file_map (io, sec, sec->vsize, patch)) {
return true;
}
}