Skip to content

Commit

Permalink
Clean up more and split add_metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r committed Apr 1, 2021
1 parent d648dc7 commit 3234ec4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ RZ_API int rz_bin_object_set_items(RzBinFile *bf, RzBinObject *o) {
return true;
}

RZ_IPI RBNode *rz_bin_object_patch_relocs(RzBinFile *bf, RzBinObject *o) {
RZ_API RBNode *rz_bin_object_patch_relocs(RzBinFile *bf, RzBinObject *o) {
rz_return_val_if_fail(bf && o, NULL);

static bool first = true;
Expand Down
1 change: 0 additions & 1 deletion librz/bin/i/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ RZ_IPI void rz_bin_object_filter_strings(RzBinObject *bo);
RZ_IPI RzBinObject *rz_bin_object_new(RzBinFile *binfile, RzBinPlugin *plugin, ut64 baseaddr, ut64 loadaddr, ut64 offset, ut64 sz);
RZ_IPI RzBinObject *rz_bin_object_get_cur(RzBin *bin);
RZ_IPI RzBinObject *rz_bin_object_find_by_arch_bits(RzBinFile *binfile, const char *arch, int bits, const char *name);
RZ_IPI RBNode *rz_bin_object_patch_relocs(RzBinFile *bf, RzBinObject *o);

RZ_IPI const char *rz_bin_lang_tostring(int lang);
RZ_IPI int rz_bin_lang_type(RzBinFile *binfile, const char *def, const char *sym);
Expand Down
60 changes: 33 additions & 27 deletions librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,29 +542,28 @@ RZ_API bool rz_core_bin_apply_dwarf(RzCore *core, RzBinFile *binfile) {
return true;
}

/* Define new data at relocation address if it's not in an executable section */
static void add_metadata(RzCore *r, RzBinReloc *reloc, ut64 addr, int mode) {
RzBinFile *binfile = r->bin->cur;
RzBinObject *binobj = binfile ? binfile->o : NULL;
/*
* Decide whether a meta item should be created for the given reloc
* and figure out what size it should have.
* \return whether to put a meta item for the given reloc
*/
static bool meta_for_reloc(RzCore *r, RzBinObject *binobj, RzBinReloc *reloc, ut64 addr, RZ_OUT ut64 *size) {
rz_return_val_if_fail(binobj && reloc, false);
RzBinInfo *info = binobj ? binobj->info : NULL;

int cdsz = info ? (info->bits == 64 ? 8 : info->bits == 32 ? 4
: info->bits == 16 ? 4
: 0)
: 0;
if (cdsz == 0) {
return;
int cdsz = info ? (info->bits / 8) : 0;
if (cdsz <= 0) {
return false;
}

// only set meta if it's not in an executable section
RzIOMap *map = rz_io_map_get(r->io, addr);
if (!map || map->perm & RZ_PERM_X) {
return;
}
if (IS_MODE_SET(mode)) {
rz_meta_set(r->analysis, RZ_META_TYPE_DATA, reloc->vaddr, cdsz, NULL);
} else if (IS_MODE_RZCMD(mode)) {
rz_cons_printf("Cd %d @ 0x%08" PFMT64x "\n", cdsz, addr);
return false;
}

*size = cdsz;
return true;
}

static bool is_section_symbol(RzBinSymbol *s) {
Expand Down Expand Up @@ -730,20 +729,23 @@ static void set_bin_relocs(RzCore *r, RzBinReloc *reloc, ut64 addr, Sdb **db, ch

RZ_API bool rz_core_bin_apply_relocs(RzCore *core, RzBinFile *binfile, bool va_bool) {
rz_return_val_if_fail(core && binfile, false);
RBIter iter;
RzBinReloc *reloc = NULL;
Sdb *db = NULL;
char *sdb_module = NULL;
RzBinObject *o = binfile->o;
if (!o) {
return false;
}

int va = VA_TRUE; // XXX relocs always vaddr?
//this has been created for reloc object files
RBNode *relocs = rz_bin_patch_relocs(core->bin);
RBNode *relocs = rz_bin_object_patch_relocs(binfile, o);
if (!relocs) {
relocs = rz_bin_get_relocs(core->bin);
relocs = o->relocs;
}

rz_flag_space_set(core->flags, RZ_FLAGS_FS_RELOCS);

Sdb *db = NULL;
char *sdb_module = NULL;
RBIter iter;
RzBinReloc *reloc = NULL;
rz_rbtree_foreach (relocs, iter, reloc, RzBinReloc, vrb) {
ut64 addr = rva(core->bin, reloc->paddr, reloc->vaddr, va);
if ((is_section_reloc(reloc) || is_file_reloc(reloc))) {
Expand All @@ -754,12 +756,13 @@ RZ_API bool rz_core_bin_apply_relocs(RzCore *core, RzBinFile *binfile, bool va_b
continue;
}
set_bin_relocs(core, reloc, addr, &db, &sdb_module);
add_metadata(core, reloc, addr, RZ_MODE_SET); // TODO: kill the mode here
ut64 meta_sz;
if (meta_for_reloc(core, o, reloc, addr, &meta_sz)) {
rz_meta_set(core->analysis, RZ_META_TYPE_DATA, addr, meta_sz, NULL);
}
}

RZ_FREE(sdb_module);
sdb_free(db);
db = NULL;

return relocs != NULL;
}
Expand Down Expand Up @@ -1751,7 +1754,10 @@ static int bin_relocs(RzCore *r, PJ *pj, int mode, int va) {
rz_cons_printf("\"f %s%s%s %d 0x%08" PFMT64x "\"\n",
r->bin->prefix ? r->bin->prefix : "reloc.",
r->bin->prefix ? "." : "", n, reloc_size, addr);
add_metadata(r, reloc, addr, mode);
ut64 meta_sz;
if (r->bin->cur && r->bin->cur->o && meta_for_reloc(r, r->bin->cur->o, reloc, addr, &meta_sz)) {
rz_cons_printf("Cd %" PFMT64u " @ 0x%08" PFMT64x "\n", meta_sz, addr);
}
free(n);
free(name);
}
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ RZ_API void rz_bin_file_hash_free(RzBinFileHash *fhash);
RZ_API int rz_bin_object_set_items(RzBinFile *binfile, RzBinObject *o);
RZ_API bool rz_bin_object_delete(RzBin *bin, ut32 binfile_id);
RZ_API ut64 rz_bin_object_addr_with_base(RzBinObject *o, ut64 addr);
RZ_API RBNode *rz_bin_object_patch_relocs(RzBinFile *bf, RzBinObject *o);
RZ_API void rz_bin_mem_free(void *data);

// demangle functions
Expand Down

0 comments on commit 3234ec4

Please sign in to comment.