Workaround in fat binaries

This is not the final solution to the problem just a workaround.

Still the OS kill rabin2 -x with a dyldcache from 32 bits
This commit is contained in:
Álvaro Felipe Melchor 2017-01-25 23:46:34 +01:00
parent 87c2b5834e
commit 19659c9b06
6 changed files with 22 additions and 46 deletions

View File

@ -138,11 +138,9 @@ static bool isBinopHelp(const char *op) {
return false;
}
static bool extract_binobj(const RBinFile *bf, const RBinXtrData *data, int idx) {
static bool extract_binobj(const RBinFile *bf, RBinXtrData *data, int idx) {
ut64 bin_size = data ? data->size : 0;
ut8 *bytes;
ut8 *bytes_encoded;
//ut64 sz = bf ? r_buf_size (bf->buf) : 0;
char *arch = "unknown";
int bits = 0;
char *libname = NULL;
@ -163,24 +161,18 @@ static bool extract_binobj(const RBinFile *bf, const RBinXtrData *data, int idx)
eprintf ("This is not a fat bin\n");
return false;
}
bytes_encoded = (ut8 *) sdb_get (data->sdb, sdb_fmt (0, "%d", data->offset), 0);
bytes = sdb_decode ((const char *)bytes_encoded, NULL);
free (bytes_encoded);
bytes = data->buffer;
if (!bytes) {
eprintf ("error: BinFile buffer is empty\n");
return false;
}
if (!arch) {
arch = "unknown";
}
path = strdup (filename);
if (!path) {
return false;
}
// XXX: Wrong for w32 (/)
ptr = strrchr (path, DIRSEP);
if (ptr) {
@ -188,9 +180,7 @@ static bool extract_binobj(const RBinFile *bf, const RBinXtrData *data, int idx)
} else {
ptr = path;
}
outpath_sz = strlen (path) + 20;
if (outpath_sz > 0) {
outpath = malloc (outpath_sz);
}
@ -220,7 +210,6 @@ static bool extract_binobj(const RBinFile *bf, const RBinXtrData *data, int idx)
}
}
if (!outfile || !r_file_dump (outfile, bytes, bin_size, 0)) {
eprintf ("Error extracting %s\n", outfile);
res = false;
@ -232,8 +221,7 @@ static bool extract_binobj(const RBinFile *bf, const RBinXtrData *data, int idx)
free (outfile);
free (outpath);
free (path);
free (bytes);
R_FREE (data->buffer);
return res;
}

View File

@ -126,8 +126,7 @@ R_API void r_bin_iobind(RBin *bin, RIO *io) {
// TODO: move these two function do a different file
R_API RBinXtrData *r_bin_xtrdata_new(RBuffer *buf, ut64 offset, ut64 size,
ut32 file_count,
RBinXtrMetadata *metadata, Sdb *sdb) {
char *encoded_bin;
RBinXtrMetadata *metadata) {
RBinXtrData *data = R_NEW0 (RBinXtrData);
if (!data) {
return NULL;
@ -135,18 +134,16 @@ R_API RBinXtrData *r_bin_xtrdata_new(RBuffer *buf, ut64 offset, ut64 size,
data->offset = offset;
data->size = size;
data->file_count = file_count;
data->sdb = sdb;
data->metadata = metadata;
data->loaded = 0;
encoded_bin = sdb_encode (r_buf_buffer (buf), r_buf_size (buf));
if (encoded_bin) {
sdb_set (data->sdb, sdb_fmt (0, "%d", offset), encoded_bin, 0);
free (encoded_bin);
return data;
data->buffer = malloc (size + 1);
data->buffer[size] = 0;
if (!data->buffer) {
free (data);
return NULL;
}
free (data);
return NULL;
memcpy (data->buffer, r_buf_buffer (buf), size);
return data;
}
R_API const char *r_bin_string_type (int type) {
@ -162,7 +159,6 @@ R_API const char *r_bin_string_type (int type) {
R_API void r_bin_xtrdata_free(void /*RBinXtrData*/ *data_) {
RBinXtrData *data = data_;
if (data) {
sdb_remove (data->sdb, sdb_fmt (0, "%d", data->offset), 0);
if (data->metadata) {
free (data->metadata->libname);
free (data->metadata->arch);
@ -170,6 +166,7 @@ R_API void r_bin_xtrdata_free(void /*RBinXtrData*/ *data_) {
free (data->metadata);
}
free (data->file);
free (data->buffer);
free (data);
}
}
@ -1337,10 +1334,8 @@ R_API bool r_bin_file_object_new_from_xtr_data(RBin *bin, RBinFile *bf,
RBinObject *o = NULL;
RBinPlugin *plugin = NULL;
ut8* bytes;
char *bytes_encoded;
ut64 offset = data? data->offset: 0;
ut64 sz = data ? data->size : 0;
if (!data || !bf) {
return false;
}
@ -1350,12 +1345,7 @@ R_API bool r_bin_file_object_new_from_xtr_data(RBin *bin, RBinFile *bf,
// if the extraction requires some sort of transformation then this will
// need to be fixed
// here.
bytes_encoded = sdb_get (data->sdb, sdb_fmt (0, "%d", data->offset), 0);
if (!bytes_encoded) {
return false;
}
bytes = sdb_decode (bytes_encoded, NULL);
free (bytes_encoded);
bytes = data->buffer;
if (!bytes) {
return false;
}
@ -1365,7 +1355,6 @@ R_API bool r_bin_file_object_new_from_xtr_data(RBin *bin, RBinFile *bf,
}
r_buf_free (bf->buf);
bf->buf = r_buf_new_with_bytes ((const ut8*)bytes, data->size);
free (bytes);
//r_bin_object_new append the new object into binfile
o = r_bin_object_new (bf, plugin, baseaddr, loadaddr, offset, sz);
// size is set here because the reported size of the object depends on

View File

@ -102,8 +102,7 @@ static RBinXtrData *extract(RBin *bin, int idx) {
r_bin_dydlcache_get_libname (lib, &libname);
metadata->libname = strdup (libname);
res = r_bin_xtrdata_new (lib->b, lib->offset, lib->size, nlib,
metadata, bin->sdb);
res = r_bin_xtrdata_new (lib->b, lib->offset, lib->size, nlib, metadata);
r_buf_free (lib->b);
free (lib);
free (hdr);
@ -145,8 +144,7 @@ static RBinXtrData *oneshot(RBin *bin, const ut8* buf, ut64 size, int idx) {
r_bin_dydlcache_get_libname (lib, &libname);
metadata->libname = strdup (libname);
res = r_bin_xtrdata_new(lib->b, lib->offset, lib->b->length, nlib,
metadata, bin->sdb);
res = r_bin_xtrdata_new(lib->b, lib->offset, lib->b->length, nlib, metadata);
r_buf_free (lib->b);
free (hdr);
free (lib);

View File

@ -103,7 +103,7 @@ static RBinXtrData * extract(RBin* bin, int idx) {
}
fill_metadata_info_from_hdr (metadata, hdr);
res = r_bin_xtrdata_new (arch->b, arch->offset, arch->size,
narch, metadata, bin->sdb);
narch, metadata);
r_buf_free (arch->b);
free (arch);
free (hdr);
@ -143,8 +143,7 @@ static RBinXtrData * oneshot(RBin *bin, const ut8 *buf, ut64 size, int idx) {
return NULL;
}
fill_metadata_info_from_hdr (metadata, hdr);
res = r_bin_xtrdata_new (arch->b, arch->offset, arch->size, narch,
metadata, bin->sdb);
res = r_bin_xtrdata_new (arch->b, arch->offset, arch->size, narch, metadata);
r_buf_free (arch->b);
free (arch);
free (hdr);

View File

@ -257,7 +257,7 @@ typedef struct r_bin_xtr_metadata_t {
typedef int (*FREE_XTR)(void *xtr_obj);
typedef struct r_bin_xtr_extract_t {
char *file;
Sdb *sdb;
ut8 *buffer;
ut64 size;
ut64 offset;
ut64 baddr;
@ -267,7 +267,7 @@ typedef struct r_bin_xtr_extract_t {
RBinXtrMetadata *metadata;
} RBinXtrData;
R_API RBinXtrData * r_bin_xtrdata_new (RBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RBinXtrMetadata *metadata, Sdb *sdb);
R_API RBinXtrData * r_bin_xtrdata_new (RBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RBinXtrMetadata *metadata);
R_API void r_bin_xtrdata_free (void /*RBinXtrData*/ *data);
R_API void r_bin_info_free (RBinInfo *rb);
R_API void r_bin_import_free(void *_imp);

View File

@ -165,7 +165,9 @@ R_API const ut8 *r_buf_buffer (RBuffer *b) {
}
R_API ut64 r_buf_size (RBuffer *b) {
if (!b) return 0LL;
if (!b) {
return 0LL;
}
if (b->fd != -1) {
return b->length;
}