Skip to content

Commit

Permalink
Import safetiness symbol name database from iaito ##bin
Browse files Browse the repository at this point in the history
* Affects bin.symbol/import/reloc
* Update sdb for better indisk /istext/ check
  • Loading branch information
trufae authored Oct 14, 2023
1 parent ea7f03a commit 9b9d4ed
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 49 deletions.
1 change: 1 addition & 0 deletions doc/fortunes.fun
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,4 @@ Caps lock is on.
$10 or mistery box?
INSERT COIN
This program is not compatible with your device.
Hey hey hey everyone! welcome to infoflash
65 changes: 42 additions & 23 deletions libr/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,39 @@ R_API RBinSymbol *r_bin_symbol_new(const char *name, ut64 paddr, ut64 vaddr) {

R_API RBinSymbol *r_bin_symbol_clone(RBinSymbol *bs) {
r_return_val_if_fail (bs, NULL);
RBinSymbol *nbs = R_NEW (RBinSymbol);
memcpy (nbs, bs, sizeof (RBinSymbol));
nbs->name = strdup (nbs->name);
if (nbs->dname) {
nbs->dname = strdup (nbs->dname);
}
if (nbs->libname) {
nbs->libname = strdup (nbs->libname);
}
if (nbs->classname) {
nbs->classname = strdup (nbs->classname);
RBinSymbol *nbs = r_mem_dup (bs, sizeof (RBinSymbol));
if (nbs) {
nbs->name = strdup (nbs->name);
if (nbs->dname) {
nbs->dname = strdup (nbs->dname);
}
if (nbs->libname) {
nbs->libname = strdup (nbs->libname);
}
if (nbs->classname) {
nbs->classname = strdup (nbs->classname);
}
}
return nbs;
}

// query the symbol name into the symtypes database
R_API const char *r_bin_symbol_unsafe(RBin *bin, const char *name) {
Sdb *db = sdb_ns (bin->sdb, "symclass", true);
if (db) {
const char *s = sdb_const_get (db, name, 0);
eprintf ("UNSAF %s DB %p = %s\n", name, db, s);
return s;
}
return NULL;
}

R_API void r_bin_symbol_fini(RBinSymbol *sym) {
free (sym->name);
free (sym->libname);
free (sym->classname);
if (sym) {
free (sym->name);
free (sym->libname);
free (sym->classname);
}
}

R_API void r_bin_import_fini(RBinImport *imp) {
Expand Down Expand Up @@ -224,11 +238,6 @@ R_API void r_bin_string_free(void *_str) {
}
}

// XXX - change this to RBinObject instead of RBinFile
// makes no sense to pass in a binfile and set the RBinObject
// kinda a clunky functions
// XXX - this is a rather hacky way to do things, there may need to be a better
// way.
R_API bool r_bin_open(RBin *bin, const char *file, RBinFileOptions *opt) {
r_return_val_if_fail (bin && bin->iob.io && opt, false);

Expand Down Expand Up @@ -372,11 +381,10 @@ R_API bool r_bin_open_io(RBin *bin, RBinFileOptions *opt) {
}

R_IPI RBinPlugin *r_bin_get_binplugin_by_name(RBin *bin, const char *name) {
RBinPlugin *plugin;
RListIter *it;

r_return_val_if_fail (bin && name, NULL);

RBinPlugin *plugin;
RListIter *it;
r_list_foreach (bin->plugins, it, plugin) {
if (!strcmp (plugin->meta.name, name)) {
return plugin;
Expand Down Expand Up @@ -804,7 +812,7 @@ R_API RList *r_bin_get_mem(RBin *bin) {
return o ? o->mem : NULL;
}

// XXX badly designed api, should not exist, aka DEPRECATE
// XXX R2_590 badly designed api, should not exist, aka DEPRECATE
R_API int r_bin_is_big_endian(RBin *bin) {
r_return_val_if_fail (bin, -1);
RBinObject *o = r_bin_cur_object (bin);
Expand Down Expand Up @@ -834,6 +842,17 @@ R_API RBin *r_bin_new(void) {
bin->force = NULL;
bin->filter_rules = UT64_MAX;
bin->sdb = sdb_new0 ();
{
Sdb *db = sdb_new0 ();
const char *cs = R2_PREFIX R_SYS_DIR R2_SDB R_SYS_DIR "format" R_SYS_DIR "symclass.sdb";
bool res = sdb_open (db, cs);
if (res) {
sdb_ns_set (bin->sdb, "symclass", db);
} else {
R_LOG_DEBUG ("Cannot find symclass.sdb");
sdb_free (db);
}
}
bin->cb_printf = (PrintfCallback)printf;
bin->plugins = r_list_newf ((RListFree)r_bin_plugin_free);
bin->minstrlen = 0;
Expand Down
9 changes: 6 additions & 3 deletions libr/bin/d/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ clean:

.PHONY: all clean install install-symlink symstall uninstall

FORMATS=dex macho elf32 elf64 elf_enums pe32 trx mz zip
FORMATS=dex macho elf32 elf64 elf_enums pe32 trx mz zip symclass.sdb

install: ${F_SDB}
install: $(F_SDB)
@echo "[i] Installing bin format files"
rm -rf "$P"
mkdir -p "$P"
-cp -f $(FORMATS) "$P" || true
mkdir -p "$P/dll"
cp -f dll/*.sdb "$P/dll"

symclass.sdb:
-$(SDB) $@ = < $@.txt

CWD=$(shell pwd)
symstall install-symlink:
symstall install-symlink: symclass.sdb
mkdir -p "$P"
for FILE in * ; do \
if [ "$$FILE" != Makefile ]; then \
Expand Down
31 changes: 27 additions & 4 deletions libr/bin/d/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sdb_files = [
sdb_dll_files = [
'aclui',
'activeds',
'atl',
Expand Down Expand Up @@ -153,12 +153,10 @@ sdb_files = [

r_bin_d_sources = []

foreach file : sdb_files
foreach file : sdb_dll_files
if get_option('sdb_cgen')
outfile = '@0@.c'.format(file)
gen_cmd = sdb_gen_cmd_cgen
# outfile = join_paths(meson.current_source_dir(), 'dll', '@0@.sdb.txt'.format(file))
# XXX cgen builds not working well yet
else
outfile = '@0@.sdb'.format(file)
gen_cmd = sdb_gen_cmd
Expand All @@ -177,6 +175,31 @@ foreach file : sdb_files
endif
endforeach

# TODO: simplify this logic
sdb_files = [ 'symclass' ]

foreach file : sdb_files
if get_option('sdb_cgen')
outfile = '@0@.c'.format(file)
gen_cmd = sdb_gen_cmd_cgen
else
outfile = '@0@.sdb'.format(file)
gen_cmd = sdb_gen_cmd
endif
target = custom_target(outfile,
input: join_paths(meson.current_source_dir(), '@0@.sdb.txt'.format(file)),
output: outfile,
command: gen_cmd,
depends: sdb_exe,
build_by_default: true,
install: true,
install_dir: join_paths(r2_sdb, join_paths('format'))
)
if get_option('sdb_cgen')
r_bin_d_sources += target
endif
endforeach

format_files = [
'dex',
'macho',
Expand Down
Loading

0 comments on commit 9b9d4ed

Please sign in to comment.