Fix several COFF parsing crashes. Bring back /B

This commit is contained in:
pancake 2014-09-20 19:28:56 +02:00
parent 43d213853f
commit ac22f9e25c
5 changed files with 48 additions and 25 deletions

View File

@ -468,7 +468,7 @@ R_API int r_bin_load_io(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr,
return r_bin_load_io_at_offset_as (bin, desc, baseaddr, loadaddr, xtr_idx, 0, NULL);
}
int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz) {
R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz) {
RIOBind *iob = &(bin->iob);
RIO *io = iob ? iob->get_io(iob) : NULL;
RListIter *it;

View File

@ -1,9 +1,10 @@
/* radare - LGPL - Copyright 2008-2014 pancake, inisider */
#include <r_util.h>
#include "coff.h"
int r_coff_supported_arch (const ut8 *buf)
{
int r_coff_supported_arch (const ut8 *buf) {
ut16 arch = *(ut16*)buf;
int ret;
@ -22,12 +23,16 @@ int r_coff_supported_arch (const ut8 *buf)
}
int r_coff_is_stripped (struct r_bin_coff_obj *obj) {
return !!(obj->hdr.f_flags & (COFF_FLAGS_TI_F_RELFLG | COFF_FLAGS_TI_F_LNNO | COFF_FLAGS_TI_F_LSYMS));
return !!(obj->hdr.f_flags & (COFF_FLAGS_TI_F_RELFLG | \
COFF_FLAGS_TI_F_LNNO | COFF_FLAGS_TI_F_LSYMS));
}
const char *r_coff_symbol_name (struct r_bin_coff_obj *obj, void *ptr) {
union { char name[8]; struct { ut32 zero; ut32 offset; }; } *p = ptr;
return NULL;
if (!ptr)
return NULL;
if (p->zero)
return p->name;
@ -56,6 +61,7 @@ RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj) {
/* No help from the header eh? Use the address of the symbols '_start'
* or 'main' if present */
if (obj->symbols)
for (i = 0; i < obj->hdr.f_nsyms; i++) {
if ((!strcmp (obj->symbols[i].n_name, "_start") ||
!strcmp (obj->symbols[i].n_name, "start")) &&
@ -63,6 +69,7 @@ RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj) {
return addr;
}
if (obj->symbols)
for (i = 0; i < obj->hdr.f_nsyms; i++) {
if ((!strcmp (obj->symbols[i].n_name, "_main") ||
!strcmp (obj->symbols[i].n_name, "main")) &&
@ -71,6 +78,7 @@ RBinAddr *r_coff_get_entry(struct r_bin_coff_obj *obj) {
}
/* Still clueless ? Let's just use the address of .text */
if (obj->scn_hdrs)
for (i = 0; i < obj->hdr.f_nscns; i++) {
if (!strcmp (obj->scn_hdrs[i].s_name, ".text")) {
addr->paddr = obj->scn_hdrs[i].s_scnptr;

View File

@ -17,7 +17,7 @@ static Sdb* get_sdb (RBinObject *o) {
return NULL;
}
static void * load_bytes(const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb){
static void * load_bytes(const ut8 *buf, ut64 sz, ut64 loadaddr, Sdb *sdb) {
void *res = NULL;
RBuffer *tbuf = NULL;
@ -67,8 +67,8 @@ static RList *entries(RBinFile *arch) {
return ret;
}
static RList *sections(RBinFile *arch)
{
static RList *sections(RBinFile *arch) {
char *coffname;
size_t i;
RList *ret = NULL;
RBinSection *ptr = NULL;
@ -79,10 +79,14 @@ static RList *sections(RBinFile *arch)
if (!ret)
return NULL;
if (obj && obj->scn_hdrs)
for (i = 0; i < obj->hdr.f_nscns; i++) {
ptr = R_NEW0 (RBinSection);
strncpy(ptr->name, r_coff_symbol_name (obj, &obj->scn_hdrs[i]), R_BIN_SIZEOF_STRINGS);
coffname = r_coff_symbol_name (obj, &obj->scn_hdrs[i]);
if (!coffname)
return NULL;
strncpy (ptr->name, coffname, R_BIN_SIZEOF_STRINGS);
ptr->size = obj->scn_hdrs[i].s_size;
ptr->vsize = obj->scn_hdrs[i].s_size;
@ -102,8 +106,8 @@ static RList *sections(RBinFile *arch)
return ret;
}
static RList *symbols(RBinFile *arch)
{
static RList *symbols(RBinFile *arch) {
char *coffname;
size_t i;
RList *ret = NULL;
RBinSymbol *ptr = NULL;
@ -115,11 +119,14 @@ static RList *symbols(RBinFile *arch)
ret->free = free;
if (obj->symbols)
for (i = 0; i < obj->hdr.f_nsyms; i++) {
if (!(ptr = R_NEW0 (RBinSymbol)))
break;
strncpy (ptr->name, r_coff_symbol_name (obj, &obj->symbols[i]), R_BIN_SIZEOF_STRINGS);
coffname = r_coff_symbol_name (obj, &obj->symbols[i]);
if (!coffname)
break;
strncpy (ptr->name, coffname, R_BIN_SIZEOF_STRINGS);
strncpy (ptr->forwarder, "NONE", R_BIN_SIZEOF_STRINGS);
strncpy (ptr->bind, "", R_BIN_SIZEOF_STRINGS);
@ -226,20 +233,18 @@ static RBinInfo *info(RBinFile *arch) {
}
break;
default:
strncpy(ret->machine, "unknown", R_BIN_SIZEOF_STRINGS);
strncpy (ret->machine, "unknown", R_BIN_SIZEOF_STRINGS);
}
return ret;
}
static RList *fields(RBinFile *arch)
{
static RList *fields(RBinFile *arch) {
return NULL;
}
static int size(RBinFile *arch)
{
static int size(RBinFile *arch) {
return 0;
}
@ -251,10 +256,19 @@ static int check(RBinFile *arch) {
}
static int check_bytes(const ut8 *buf, ut64 length) {
if (buf && length >= 2) {
if (r_coff_supported_arch(buf))
return R_TRUE;
}
#if 0
TODO: do more checks here to avoid false positives
ut16 MACHINE
ut16 NSECTIONS
ut32 DATE
ut32 PTRTOSYMTABLE
ut32 NUMOFSYMS
ut16 OPTHDRSIZE
ut16 CHARACTERISTICS
#endif
if (buf && length >= 20)
return r_coff_supported_arch (buf);
return R_FALSE;
}

View File

@ -11,7 +11,7 @@ static unsigned int searchcount = 0;
static void cmd_search_bin(RCore *core, ut64 from, ut64 to) {
RBinPlugin *plug;
ut8 buf[1024];
int sz = sizeof (buf);
int size, sz = sizeof (buf);
while (from <to) {
r_io_read_at (core->io, from, buf, sz);
@ -19,15 +19,15 @@ static void cmd_search_bin(RCore *core, ut64 from, ut64 to) {
if (plug) {
r_cons_printf ("0x%08"PFMT64x" %s\n",
from, plug->name);
#if TODO
// TODO: load the bin and calculate its size
if (plug->size) {
r_bin_load_io_at_offset_as_sz (core->bin,
core->file->desc, 0, 0, 0, core->offset,
plug->name, 4096);
eprintf ("Size %d\n", plug->size (core->bin));
size = plug->size (core->bin->cur);
if (size)
r_cons_printf ("size %d\n", size);
}
#endif
}
from ++;
}

View File

@ -354,6 +354,7 @@ R_API int r_bin_reload(RBin *bin, RIODesc *desc, ut64 baseaddr);
R_API int r_bin_load_as(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, int xtr_idx, int fd, int rawstr, int fileoffset, const char *name);
R_API int r_bin_load_io(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int xtr_idx);
R_API int r_bin_load_io_at_offset_as(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name);
R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, RIODesc *desc, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz);
R_API void r_bin_bind(RBin *b, RBinBind *bnd);
R_API int r_bin_add(RBin *bin, RBinPlugin *foo);
R_API int r_bin_xtr_add(RBin *bin, RBinXtrPlugin *foo);