* Implement S- command (remove sections)

- by index, offset or all (*)
* Fix block_read() issue with io.va
This commit is contained in:
pancake 2011-04-19 00:59:16 +02:00
parent 616395cca3
commit a16f3f27b4
8 changed files with 67 additions and 32 deletions

3
TODO
View File

@ -3,6 +3,9 @@
| < V . | . V . < _/ .-' _/| () |
|__\__|_|__|___/__|__|_\__\___/ |____(_)____/
* Make r_io happy with RList
To wipe:
========
* check iowrite

View File

@ -1295,7 +1295,20 @@ static int cmd_section(void *data, const char *input) {
" S* ; list sections (in radare commands)\n"
" S= ; list sections (in nice ascii-art bars)\n"
" S [offset] [vaddr] [size] [vsize] [name] [rwx] ; adds new section\n"
" S -[offset] ; remove this section definition\n");
" S-[id|0xoff|*] ; remove this section definition\n");
break;
case '-':
if (input[1] == '*') {
// remove all sections
r_io_section_init (core->io);
} else
if (input[1] == '0' && input[2]=='x') {
RIOSection *s = r_io_section_get (core->io, r_num_get (NULL, input+1));
// use offset
r_io_section_rm (core->io, s->id);
} else {
r_io_section_rm (core->io, atoi (input+1));
}
break;
case ' ':
switch (input[1]) {
@ -1368,6 +1381,7 @@ static int cmd_seek(void *data, const char *input) {
switch (input[0]) {
case ' ':
r_core_seek (core, off, 1);
r_core_block_read (core, 0);
r_io_sundo_push (core->io);
break;
case '*':

View File

@ -450,8 +450,9 @@ R_API int r_core_seek_delta(RCore *core, st64 addr) {
}
core->offset = addr;
ret = r_core_block_read (core, 0);
if (ret == -1)
core->offset = tmp;
//if (ret == -1)
// memset (core->block, 0xff, core->blocksize);
// core->offset = tmp;
return ret;
}

View File

@ -76,6 +76,9 @@ R_API boolt r_core_seek(RCore *core, ut64 addr, boolt rb) {
} else return R_FALSE;
*/
//core->offset = addr;
if (!core->io->va)
return R_FALSE;
memset (core->block, 0xff, core->blocksize);
} else core->offset = addr;
if (rb) {
ret = r_core_block_read (core, 0);
@ -109,13 +112,18 @@ R_API int r_core_write_at(RCore *core, ut64 addr, const ut8 *buf, int size) {
}
R_API int r_core_block_read(RCore *core, int next) {
ut64 off;
if (core->file == NULL) {
memset (core->block, 0xff, core->blocksize);
return -1;
}
r_io_set_fd (core->io, core->file->fd);
r_io_seek (core->io, core->offset+((next)?core->blocksize:0), R_IO_SEEK_SET);
return r_io_read (core->io, core->block, core->blocksize);
off = r_io_seek (core->io, core->offset+((next)?core->blocksize:0), R_IO_SEEK_SET);
if (off == UT64_MAX) {
memset (core->block, 0xff, core->blocksize);
return -1;
}
return (int)r_io_read (core->io, core->block, core->blocksize);
}
R_API int r_core_read_at(RCore *core, ut64 addr, ut8 *buf, int size) {
@ -126,5 +134,6 @@ R_API int r_core_read_at(RCore *core, ut64 addr, ut8 *buf, int size) {
ret = r_io_read_at (core->io, addr, buf, size);
if (addr>=core->offset && addr<=core->offset+core->blocksize)
r_core_block_read (core, 0);
return (ret!=-1);
else memset (buf, 0xff, size);
return (ret!=UT64_MAX);
}

View File

@ -103,6 +103,7 @@ typedef struct r_io_t {
struct r_io_undo_t undo;
struct list_head io_list;
struct list_head sections;
int next_section_id;
/* maps */
RList *maps; /*<RIOMap>*/
RList *desc;
@ -161,6 +162,7 @@ typedef struct r_io_section_t {
ut64 size;
ut64 vsize;
int rwx;
int id;
struct list_head list;
} RIOSection;

View File

@ -9,20 +9,19 @@
R_API struct r_io_t *r_io_new() {
RIO *io = R_NEW (struct r_io_t);
if (io) {
io->fd = NULL;
io->write_mask_fd = -1;
io->redirect = NULL;
io->printf = (void*) printf;
io->plugin = NULL;
io->raised = -1;
r_io_cache_init (io);
r_io_map_init (io);
r_io_section_init (io);
r_io_plugin_init (io);
r_io_desc_init (io);
r_io_undo_init (io);
}
if (!io) return NULL;
io->fd = NULL;
io->write_mask_fd = -1;
io->redirect = NULL;
io->printf = (void*) printf;
io->plugin = NULL;
io->raised = -1;
r_io_cache_init (io);
r_io_map_init (io);
r_io_section_init (io);
r_io_plugin_init (io);
r_io_desc_init (io);
r_io_undo_init (io);
return io;
}
@ -171,7 +170,7 @@ R_API int r_io_read(RIO *io, ut8 *buf, int len) {
}
#endif
off = io->off;
r_io_map_select (io, io->off);
ret = -1;
if (io->plugin && io->plugin->read) {
if (io->plugin->read != NULL)
ret = io->plugin->read (io, io->fd, buf, len);
@ -180,13 +179,16 @@ R_API int r_io_read(RIO *io, ut8 *buf, int len) {
if (ret>0 && ret<len)
memset (buf+ret, 0xff, len-ret);
// this must be before?? r_io_cache_read (io, io->off, buf, len);
r_io_seek (io, off, R_IO_SEEK_SET);
// eprintf ("--RET-- %llx\n", r_io_seek (io, off, R_IO_SEEK_SET));
return ret;
}
R_API int r_io_read_at(struct r_io_t *io, ut64 addr, ut8 *buf, int len) {
if (r_io_seek (io, addr, R_IO_SEEK_SET)==-1)
if (r_io_seek (io, addr, R_IO_SEEK_SET)==UT64_MAX) {
memset (buf, 0xff, len);
return -1;
}
return r_io_read (io, buf, len);
}
@ -283,7 +285,7 @@ R_API ut64 r_io_seek(struct r_io_t *io, ut64 offset, int whence) {
switch (whence) {
case R_IO_SEEK_SET:
posix_whence = SEEK_SET;
ret=offset;
ret = offset;
break;
case R_IO_SEEK_CUR:
// offset += io->off;
@ -301,6 +303,9 @@ R_API ut64 r_io_seek(struct r_io_t *io, ut64 offset, int whence) {
// XXX: list_empty trick must be done in r_io_set_va();
offset = (!io->debug && io->va && !list_empty (&io->sections))?
r_io_section_vaddr_to_offset (io, offset) : offset;
// if resolution fails... just return as invalid address
if (offset==UT64_MAX)
return UT64_MAX;
// TODO: implement io->enforce_seek here!
if (io->fd != NULL) {
if (io->plugin && io->plugin->lseek)

View File

@ -47,7 +47,7 @@ R_API RIOMap *r_io_map_add(struct r_io_t *io, int fd, int flags, ut64 delta, ut6
R_API int r_io_map_select(RIO *io, ut64 off) {
ut64 delta = 0;
ut64 fd = -1;//io->fd;
RIOMap *im;
RIOMap *im = NULL;
RListIter *iter;
r_list_foreach (io->maps, iter, im) { // _prev?
if (im && off >= im->from && off < im->to) {
@ -59,7 +59,8 @@ R_API int r_io_map_select(RIO *io, ut64 off) {
}
if (fd != -1) {
r_io_set_fdn (io, fd);
r_io_seek (io, delta, R_IO_SEEK_SET);
// eprintf ("seek ret = %llx\n",
// r_io_seek (io, delta, R_IO_SEEK_SET));
return R_TRUE;
}
return R_FALSE;

View File

@ -1,8 +1,9 @@
/* radare - LGPL - Copyright 2008-2010 pancake<nopcode.org> nibble <.ds@gmail.com> */
/* radare - LGPL - Copyright 2008-2011 pancake<nopcode.org> nibble <.ds@gmail.com> */
#include "r_io.h"
R_API void r_io_section_init(RIO *io) {
io->next_section_id = 0;
io->enforce_rwx = 0; // do not enforce RWX section permissions by default
io->enforce_seek = 0; // do not limit seeks out of the file by default
INIT_LIST_HEAD (&(io->sections));
@ -11,6 +12,7 @@ R_API void r_io_section_init(RIO *io) {
R_API void r_io_section_add(RIO *io, ut64 offset, ut64 vaddr, ut64 size, ut64 vsize, int rwx, const char *name) {
RIOSection *s = R_NEW (RIOSection);
s->offset = offset;
s->id = io->next_section_id++;
s->vaddr = vaddr;
s->size = size;
s->vsize = vsize;
@ -21,13 +23,11 @@ R_API void r_io_section_add(RIO *io, ut64 offset, ut64 vaddr, ut64 size, ut64 vs
}
R_API RIOSection *r_io_section_get_i(RIO *io, int idx) {
int i = 0;
struct list_head *pos;
list_for_each_prev (pos, &io->sections) {
RIOSection *s = (RIOSection *)list_entry (pos, RIOSection, list);
if (i == idx)
if (s->id == idx)
return s;
i++;
}
return NULL;
}
@ -53,8 +53,8 @@ R_API void r_io_section_list(RIO *io, ut64 offset, int rad) {
RIOSection *s = (RIOSection *)list_entry(pos, RIOSection, list);
if (rad) io->printf ("S 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" 0x%08"PFMT64x" %s %d\n",
s->offset, s->vaddr, s->size, s->vsize, s->name, s->rwx);
else io->printf ("[%02d] %c 0x%08"PFMT64x" %s va=0x%08"PFMT64x" sz=0x%08"PFMT64x" vsz=%08"PFMT64x" %s\n",
i, (offset>=s->offset && offset<s->offset+s->size)?'*':'.',
else io->printf ("[%.2d] %c 0x%08"PFMT64x" %s va=0x%08"PFMT64x" sz=0x%08"PFMT64x" vsz=%08"PFMT64x" %s\n",
s->id, (offset>=s->offset && offset<s->offset+s->size)?'*':'.',
s->offset, r_str_rwx_i (s->rwx), s->vaddr, s->size, s->vsize, s->name);
i++;
}