Skip to content

Commit

Permalink
Initial implementation of RBinName and RBinAttr ##bin
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored and trufae committed Oct 28, 2023
1 parent 94a6c05 commit c1a17af
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 219 deletions.
64 changes: 64 additions & 0 deletions libr/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,7 @@ R_API void r_bin_trycatch_free(RBinTrycatch *tc) {
}

R_API const char *r_bin_field_kindstr(RBinField *f) {
r_return_val_if_fail (f, NULL);
switch (f->kind) {
case R_BIN_FIELD_KIND_PROPERTY:
return "property";
Expand All @@ -1530,3 +1531,66 @@ R_API const char *r_bin_field_kindstr(RBinField *f) {
return "var"; // maybe ivar for objc?
}
}

R_API RBinName *r_bin_name_new(const char *name) {
r_return_val_if_fail (name, NULL);
RBinName *bn = R_NEW0 (RBinName);
bn->oname = strdup (name);
// oname and dname
return bn;
}

R_API void r_bin_name_demangled(RBinName *bn, const char *dname) {
r_return_if_fail (bn && dname);
if (bn->name && !bn->oname) {
bn->oname = bn->name;
} else {
free (bn->name);
}
bn->name = strdup (dname);
}

R_API char *r_bin_name_tostring(RBinName *bn) {
r_return_val_if_fail (bn, NULL);
if (bn->name) {
return bn->name;
}
if (bn->oname) {
return bn->oname;
}
return bn->fname;
}

// prefered type
R_API char *r_bin_name_tostring2(RBinName *bn, int type) {
r_return_val_if_fail (bn, NULL);
if (type == 'd' && bn->name) {
return bn->name;
}
if (type == 'f' && bn->fname) {
return bn->fname;
}
if (type == 'o' && bn->oname) {
return bn->oname;
}
return r_bin_name_tostring (bn);
}

R_API void r_bin_name_free(RBinName *bn) {
if (bn) {
free (bn->name);
free (bn->oname);
free (bn->fname);
free (bn);
}
}

// TODO : not implemented yet
R_API char *r_bin_attr_tostring(ut64 attr) {
return NULL;
}

// TODO : not implemented yet
R_API ut64 r_bin_attr_fromstring(const char *s) {
return 0ULL;
}
50 changes: 22 additions & 28 deletions libr/bin/format/objc/mach0_classes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1249,9 +1249,9 @@ void MACH0_(get_class_t)(mach0_ut p, RBinFile *bf, RBinClass *klass, bool dupe,
const char *klass_name = get_class_name (c.superclass, bf);
if (klass_name) {
if (klass->super == NULL) {
klass->super = r_list_newf (free);
klass->super = r_list_newf ((void *)r_bin_name_free);
}
r_list_append (klass->super, (void *)klass_name);
r_list_append (klass->super, (void *)r_bin_name_new (klass_name));
}
} else if (relocs) {
struct reloc_t reloc_at_class_addr;
Expand All @@ -1263,20 +1263,18 @@ void MACH0_(get_class_t)(mach0_ut p, RBinFile *bf, RBinClass *klass, bool dupe,
char *target_class_name = (char*) ((struct reloc_t*) found->data)->name;
if (r_str_startswith (target_class_name, _objc_class)) {
target_class_name += _objc_class_len;
if (klass->super == NULL) {
klass->super = r_list_newf (free);
}
RBinName *sup = r_bin_name_new (target_class_name);
if (r_str_startswith (target_class_name, "_T")) {
char *dsuper = r_bin_demangle_swift (target_class_name, true, true);
if (!dsuper || !strcmp (dsuper, target_class_name)) {
R_LOG_DEBUG ("Failed to demangle");
r_list_append (klass->super, strdup (target_class_name));
} else {
r_list_append (klass->super, dsuper);
if (dsuper && strcmp (dsuper, target_class_name)) {
r_bin_name_demangled (sup, dsuper);
}
} else {
r_list_append (klass->super, strdup (target_class_name));
free (dsuper);
}
if (klass->super == NULL) {
klass->super = r_list_newf ((void *)r_bin_name_free);
}
r_list_append (klass->super, sup);
}
}
}
Expand Down Expand Up @@ -1399,21 +1397,15 @@ static void parse_type(RList *list, RBinFile *bf, SwiftType st, HtUP *symbols_ht
char *super_name = readstr (bf, st.super_addr);
if (super_name) {
if (*super_name > 5) {
klass->super = r_list_newf (free);
#if 1
klass->super = r_list_newf ((void *)r_bin_name_free);
RBinName *bn = r_bin_name_new (super_name);
char *sname = r_bin_demangle_swift (super_name, 0, false);
if (R_STR_ISNOTEMPTY (sname)) {
r_list_append (klass->super, sname);
free (super_name);
} else {
r_list_append (klass->super, super_name);
r_bin_name_demangled (bn, sname);
}
#else
r_list_append (klass->super, super_name);
#endif
} else {
free (super_name);
r_list_append (klass->super, bn);
}
free (super_name);
}
klass->addr = st.addr;
klass->lang = R_BIN_LANG_SWIFT;
Expand Down Expand Up @@ -1637,7 +1629,7 @@ RList *MACH0_(parse_classes)(RBinFile *bf, objc_cache_opt_info *oi) {
R_LOG_ERROR ("Chopped classlist data");
break;
}
klass = r_bin_class_new ("", "", R_BIN_CLASS_PUBLIC);
klass = r_bin_class_new ("", "", R_BIN_ATTR_PUBLIC);
R_FREE (klass->name); // allow NULL name in rbinclass?
klass->lang = R_BIN_LANG_OBJC;
size = sizeof (mach0_ut);
Expand Down Expand Up @@ -1710,15 +1702,17 @@ static RList *MACH0_(parse_categories)(RBinFile *bf, MetaSections *ms, const RSk
if (par) {
size_t idx = par - klass->name;
char *super = strdup (klass->name);
// TODO: demangle name!!
super[idx++] = 0;
char *cpar = strchr (super + idx, ')');
if (cpar) {
*cpar = 0;
}
r_list_free (klass->super);
klass->super = r_list_newf (free);
r_list_append (klass->super, super);
if (klass->super == NULL) {
klass->super = r_list_newf ((void *)r_bin_name_free);
}
RBinName *bn = r_bin_name_new (super);
// TODO: demangle name!!
r_list_append (klass->super, bn);
// char *name = strdup (super + idx);
// free (klass->name);
// klass->name = name;
Expand Down
2 changes: 1 addition & 1 deletion libr/bin/mangling/swift-sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <r_lib.h>

// set this to true for debugging purposes
#define USE_THIS_CODE 0
#define USE_THIS_CODE 1

static R_TH_LOCAL int have_swift_demangle = -1;
#if R2__UNIX__
Expand Down
38 changes: 12 additions & 26 deletions libr/bin/p/bin_dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,35 +183,22 @@ static char *createAccessFlagStr(ut32 flags, AccessFor forWhat) {
},
};
size_t i, count = r_num_bit_count (flags);
const int kLongest = 21;
const int maxSize = (count + 1) * (kLongest + 1);
char* str, *cp;
// produces a huge number????
if (count < 1 || (count * (kLongest+1)) < 1) {
return NULL;
}
cp = str = (char*) calloc (count + 1, (kLongest + 1));
if (!str) {
if (count < 1) {
return NULL;
}
RStrBuf *sb = r_strbuf_new ("");
for (i = 0; i < NUM_FLAGS; i++) {
if (flags & 0x01) {
if (flags & 1) {
const char *accessStr = kAccessStrings[forWhat][i];
int len = strlen (accessStr);
if (cp != str) {
*cp++ = ' ';
if (!r_strbuf_is_empty (sb)) {
r_strbuf_append (sb, " ");
}
if (((cp - str) + len) >= maxSize) {
free (str);
return NULL;
}
memcpy (cp, accessStr, len);
cp += len;
r_strbuf_append (sb, accessStr);
}
flags >>= 1;
}
*cp = '\0';
return str;
return r_strbuf_drain (sb);
}

static const char *dex_type_descriptor(RBinDexObj *dex, int type_idx) {
Expand Down Expand Up @@ -1017,7 +1004,6 @@ static ut64 dex_get_type_offset(RBinFile *bf, int type_idx) {

static const char *dex_class_super_name(RBinDexObj *bin, RBinDexClass *c) {
r_return_val_if_fail (bin && bin->types && c, NULL);

int cid = c->super_class;
if (cid < 0 || cid >= bin->header.types_size) {
return NULL;
Expand Down Expand Up @@ -1470,8 +1456,8 @@ static void parse_class(RBinFile *bf, RBinDexClass *c, int class_index, int *met
cls->methods = r_list_newf ((RListFree)r_bin_symbol_free);
const char *super = dex_class_super_name (dex, c);
if (super) {
cls->super = r_list_newf (free);
r_list_append (cls->super, strdup (super));
cls->super = r_list_newf ((void*)r_bin_name_free);
r_list_append (cls->super, r_bin_name_new (super));
}
if (!cls->methods) {
free (cls);
Expand All @@ -1490,11 +1476,11 @@ static void parse_class(RBinFile *bf, RBinDexClass *c, int class_index, int *met
rbin->cb_printf (" Access flags : 0x%04x (%s)\n", c->access_flags,
r_str_get (cls->visibility_str));
if (cls->super) {
char *sk;
RBinName *bn;
RListIter *iter;
rbin->cb_printf (" Superclass : '");
r_list_foreach (cls->super, iter, sk) {
rbin->cb_printf ("%s%s", iter->n? ",": "", sk);
r_list_foreach (cls->super, iter, bn) {
rbin->cb_printf ("%s%s", iter->n? ",": "", r_bin_name_tostring (bn));
}
rbin->cb_printf ("'\n");
}
Expand Down
16 changes: 5 additions & 11 deletions libr/bin/p/bin_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
#include "../../shlr/java/class.h"
#include "../../shlr/java/code.h"

static void add_bin_obj_to_sdb(RBinJavaObj *bin);
static int add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj);

static int add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj) {
int result = false;
char *addr, value[SDB_NUM_BUFSZ] = {
0
};
addr = sdb_itoa ((ut64) (size_t) bin_obj, 16, value, sizeof (value));
static bool add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj) {
char value[SDB_NUM_BUFSZ] = {0};
char *addr = sdb_itoa ((ut64) (size_t) bin_obj, 16, value, sizeof (value));
if (key && bin_obj && bin_obj->kv) {
R_LOG_DEBUG ("Adding %s:%s to the bin_objs db", key, addr);
sdb_set (bin_obj->kv, key, addr, 0);
result = true;
return true;
}
return result;
return false;
}

static void add_bin_obj_to_sdb(RBinJavaObj *bj) {
Expand Down
Loading

0 comments on commit c1a17af

Please sign in to comment.