Skip to content

Commit

Permalink
Use more sdb in mach0
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Oct 10, 2017
1 parent b0beb37 commit 675dfdd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
22 changes: 14 additions & 8 deletions libr/bin/format/mach0/mach0.c
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,9 @@ struct addr_t* MACH0_(get_entrypoint)(struct MACH0_(obj_t)* bin) {
entry->addr = entry_to_vaddr (bin);
entry->offset = addr_to_offset (bin, entry->addr);
entry->haddr = sdb_num_get (bin->kv, "mach0.entry.offset", 0);
sdb_num_set (bin->kv, "mach0.entry.vaddr", entry->addr, 0);
sdb_num_set (bin->kv, "mach0.entry.paddr", bin->entry, 0);
}

if (!bin->entry || entry->offset == 0) {
// XXX: section name doesnt matters at all.. just check for exec flags
for (i = 0; i < bin->nsects; i++) {
Expand All @@ -1968,6 +1969,13 @@ struct addr_t* MACH0_(get_entrypoint)(struct MACH0_(obj_t)* bin) {
return entry;
}

void MACH0_(kv_loadlibs)(struct MACH0_(obj_t)* bin) {
int i;
for (i = 0; i < bin->nlibs; i++) {
sdb_set (bin->kv, sdb_fmt (0, "libs.%d.name", i), bin->libs[i], 0);
}
}

struct lib_t* MACH0_(get_libs)(struct MACH0_(obj_t)* bin) {
struct lib_t *libs;
int i;
Expand All @@ -1979,6 +1987,7 @@ struct lib_t* MACH0_(get_libs)(struct MACH0_(obj_t)* bin) {
return NULL;
}
for (i = 0; i < bin->nlibs; i++) {
sdb_set (bin->kv, sdb_fmt (0, "libs.%d.name", i), bin->libs[i], 0);
strncpy (libs[i].name, bin->libs[i], R_BIN_MACH0_STRING_LENGTH);
libs[i].name[R_BIN_MACH0_STRING_LENGTH-1] = '\0';
libs[i].last = 0;
Expand Down Expand Up @@ -2056,7 +2065,7 @@ const char* MACH0_(get_os)(struct MACH0_(obj_t)* bin) {
return "darwin";
}

char* MACH0_(get_cputype_from_hdr)(struct MACH0_(mach_header) *hdr) {
const char* MACH0_(get_cputype_from_hdr)(struct MACH0_(mach_header) *hdr) {
const char *archstr = "unknown";
switch (hdr->cputype) {
case CPU_TYPE_VAX:
Expand Down Expand Up @@ -2095,14 +2104,11 @@ char* MACH0_(get_cputype_from_hdr)(struct MACH0_(mach_header) *hdr) {
case CPU_TYPE_POWERPC64:
archstr = "ppc";
}
return strdup (archstr);
return archstr;
}

char* MACH0_(get_cputype)(struct MACH0_(obj_t)* bin) {
if (bin) {
return MACH0_(get_cputype_from_hdr) (&bin->hdr);
}
return strdup ("unknown");
const char* MACH0_(get_cputype)(struct MACH0_(obj_t)* bin) {
return bin? MACH0_(get_cputype_from_hdr) (&bin->hdr): "unknown";
}

// TODO: use const char*
Expand Down
6 changes: 3 additions & 3 deletions libr/bin/format/mach0/mach0.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2010-2016 - pancake, nibble */
/* radare - LGPL - Copyright 2010-2017 - pancake, nibble */

#include <r_bin.h>
#include <r_types.h>
Expand Down Expand Up @@ -152,13 +152,13 @@ int MACH0_(is_pie)(struct MACH0_(obj_t)* bin);
int MACH0_(has_nx)(struct MACH0_(obj_t)* bin);
const char* MACH0_(get_intrp)(struct MACH0_(obj_t)* bin);
const char* MACH0_(get_os)(struct MACH0_(obj_t)* bin);
char* MACH0_(get_cputype)(struct MACH0_(obj_t)* bin);
const char* MACH0_(get_cputype)(struct MACH0_(obj_t)* bin);
char* MACH0_(get_cpusubtype)(struct MACH0_(obj_t)* bin);
char* MACH0_(get_cpusubtype_from_hdr)(struct MACH0_(mach_header) *hdr);
char* MACH0_(get_filetype)(struct MACH0_(obj_t)* bin);
char* MACH0_(get_filetype_from_hdr)(struct MACH0_(mach_header) *hdr);
ut64 MACH0_(get_main)(struct MACH0_(obj_t)* bin);
char* MACH0_(get_cputype_from_hdr)(struct MACH0_(mach_header) *hdr);
const char* MACH0_(get_cputype_from_hdr)(struct MACH0_(mach_header) *hdr);
int MACH0_(get_bits_from_hdr)(struct MACH0_(mach_header)* hdr);
struct MACH0_(mach_header)* MACH0_(get_hdr_from_bytes)(RBuffer *buf);
void MACH0_(mach_headerfields)(RBinFile *arch);
Expand Down
17 changes: 9 additions & 8 deletions libr/bin/p/bin_mach0.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ static void _handle_arm_thumb(struct MACH0_(obj_t) *bin, RBinSymbol **p) {
ptr->bits = 16;
}
}

}

static RList* symbols(RBinFile *arch) {
Expand Down Expand Up @@ -253,9 +252,11 @@ static RList* symbols(RBinFile *arch) {
}
//functions from LC_FUNCTION_STARTS
if (bin->func_start) {
char symstr[128];
ut64 value = 0, address = 0;
const ut8* temp = bin->func_start;
const ut8* temp_end = bin->func_start + bin->func_size;
strcpy (symstr, "sym0x");
while (temp + 3 < temp_end && *temp) {
temp = r_uleb128_decode (temp, NULL, &value);
address += value;
Expand All @@ -267,21 +268,21 @@ static RList* symbols(RBinFile *arch) {
ptr->paddr = address;
ptr->size = 0;
ptr->name = r_str_newf ("func.%08"PFMT64x, ptr->vaddr);
ptr->type = r_str_const ("FUNC");
ptr->forwarder = r_str_const ("NONE");
ptr->bind = r_str_const ("LOCAL");
ptr->type = "FUNC";
ptr->forwarder = "NONE";
ptr->bind = "LOCAL";
ptr->ordinal = i++;
if (bin->hdr.cputype == CPU_TYPE_ARM && wordsize < 64) {
_handle_arm_thumb (bin, &ptr);
}
r_list_append (ret, ptr);
// if any func is not found in symbols then we can consider it is stripped
if (!isStripped) {
if (!sdb_const_get (symcache, sdb_fmt (0, "sym0x%llx", ptr->vaddr), 0)) {
snprintf (symstr + 5, sizeof (symstr) - 5 , "%" PFMT64x, ptr->vaddr);
if (!sdb_const_get (symcache, symstr, 0)) {
isStripped = true;
}
}

}
}
bin->lang = lang;
Expand All @@ -294,6 +295,7 @@ static RList* symbols(RBinFile *arch) {
}

static RList* imports(RBinFile *arch) {
RBinObject *obj = arch ? arch->o : NULL;
const char *_objc_class = "_OBJC_CLASS_$";
const int _objc_class_len = strlen (_objc_class);
const char *_objc_metaclass = "_OBJC_METACLASS_$";
Expand All @@ -304,7 +306,6 @@ static RList* imports(RBinFile *arch) {
RBinImport *ptr = NULL;
RList *ret = NULL;
int i;
RBinObject *obj = arch ? arch->o : NULL;

if (!obj || !bin || !obj->bin_obj || !(ret = r_list_newf (free))) {
return NULL;
Expand Down Expand Up @@ -437,7 +438,7 @@ static RBinInfo* info(RBinFile *arch) {
ret->rclass = strdup ("mach0");
ret->os = strdup (MACH0_(get_os)(arch->o->bin_obj));
ret->subsystem = strdup ("darwin");
ret->arch = MACH0_(get_cputype) (arch->o->bin_obj);
ret->arch = strdup (MACH0_(get_cputype) (arch->o->bin_obj));
ret->machine = MACH0_(get_cpusubtype) (arch->o->bin_obj);
ret->has_lit = true;
ret->type = MACH0_(get_filetype) (arch->o->bin_obj);
Expand Down
4 changes: 2 additions & 2 deletions libr/bin/p/bin_xtr_fatmach0.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2016 - nibble, pancake */
/* radare - LGPL - Copyright 2009-2017 - nibble, pancake */

#include <r_types.h>
#include <r_util.h>
Expand Down Expand Up @@ -58,7 +58,7 @@ static int size(RBin *bin) {
}

static inline void fill_metadata_info_from_hdr(RBinXtrMetadata *meta, struct MACH0_(mach_header) *hdr) {
meta->arch = MACH0_(get_cputype_from_hdr) (hdr);
meta->arch = strdup (MACH0_(get_cputype_from_hdr) (hdr));
meta->bits = MACH0_(get_bits_from_hdr) (hdr);
meta->machine = MACH0_(get_cpusubtype_from_hdr) (hdr);
meta->type = MACH0_(get_filetype_from_hdr) (hdr);
Expand Down

0 comments on commit 675dfdd

Please sign in to comment.