Skip to content

Commit

Permalink
Refactor the <entries> in RzBinPlugin from list to pvector
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu committed Jan 19, 2024
1 parent cad1e5c commit 8cf9904
Show file tree
Hide file tree
Showing 65 changed files with 293 additions and 368 deletions.
7 changes: 0 additions & 7 deletions librz/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,6 @@ RZ_API void rz_bin_set_baddr(RzBin *bin, ut64 baddr) {
}
}

// XXX: those accessors are redundant
RZ_DEPRECATE RZ_API RZ_BORROW RzList /*<RzBinAddr *>*/ *rz_bin_get_entries(RZ_NONNULL RzBin *bin) {
rz_return_val_if_fail(bin, NULL);
RzBinObject *o = rz_bin_cur_object(bin);
return o ? (RzList *)rz_bin_object_get_entries(o) : NULL;
}

RZ_DEPRECATE RZ_API RZ_BORROW RzBinInfo *rz_bin_get_info(RzBin *bin) {
rz_return_val_if_fail(bin, NULL);
RzBinObject *o = rz_bin_cur_object(bin);
Expand Down
6 changes: 3 additions & 3 deletions librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ RZ_IPI void rz_bin_object_free(RzBinObject *o) {
rz_bin_source_line_info_free(o->lines);
rz_bin_string_database_free(o->strings);
rz_pvector_free(o->classes);
rz_list_free(o->entries);
rz_pvector_free(o->entries);
rz_pvector_free(o->fields);
rz_pvector_free(o->imports);
rz_pvector_free(o->libs);
Expand Down Expand Up @@ -649,9 +649,9 @@ RZ_API const RzBinAddr *rz_bin_object_get_special_symbol(RzBinObject *o, RzBinSp
}

/**
* \brief Get list of \p RzBinAddr representing the entry points of the binary object.
* \brief Get pvector of \p RzBinAddr representing the entry points of the binary object.
*/
RZ_API const RzList /*<RzBinAddr *>*/ *rz_bin_object_get_entries(RZ_NONNULL RzBinObject *obj) {
RZ_API const RzPVector /*<RzBinAddr *>*/ *rz_bin_object_get_entries(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);
return obj->entries;
}
Expand Down
9 changes: 5 additions & 4 deletions librz/bin/bobj_process_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
RZ_IPI void rz_bin_set_and_process_entries(RzBinFile *bf, RzBinObject *o) {
RzBinPlugin *plugin = o->plugin;

rz_list_free(o->entries);
rz_pvector_free(o->entries);
if (!plugin->entries || !(o->entries = plugin->entries(bf))) {
o->entries = rz_list_newf(free);
o->entries = rz_pvector_new(free);
return;
}

RzListIter *it;
void **it;
RzBinAddr *element;
rz_list_foreach (o->entries, it, element) {
rz_pvector_foreach (o->entries, it) {
element = *it;
// rebase physical address
element->paddr += o->opts.loadaddr;
}
Expand Down
27 changes: 13 additions & 14 deletions librz/bin/format/dex/dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1688,12 +1688,12 @@ RZ_API RZ_OWN RzBinAddr *rz_bin_dex_resolve_symbol(RZ_NONNULL RzBinDex *dex, RzB
return ret;
}

static RzList /*<RzBinAddr *>*/ *dex_resolve_entrypoints_in_class(RzBinDex *dex, DexClassDef *class_def) {
static RzPVector /*<RzBinAddr *>*/ *dex_resolve_entrypoints_in_class(RzBinDex *dex, DexClassDef *class_def) {
RzListIter *it;
DexEncodedMethod *encoded_method = NULL;
RzList *entrypoints = NULL;
RzPVector *entrypoints = NULL;

entrypoints = rz_list_newf((RzListFree)free);
entrypoints = rz_pvector_new((RzPVectorFree)free);
if (!entrypoints) {
return NULL;
}
Expand Down Expand Up @@ -1732,7 +1732,7 @@ static RzList /*<RzBinAddr *>*/ *dex_resolve_entrypoints_in_class(RzBinDex *dex,
entrypoint->vaddr = encoded_method->code_offset;
entrypoint->paddr = 0;
}
if (entrypoint && !rz_list_append(entrypoints, entrypoint)) {
if (entrypoint && !rz_pvector_push(entrypoints, entrypoint)) {
free(entrypoint);
}
}
Expand Down Expand Up @@ -1776,7 +1776,7 @@ static RzList /*<RzBinAddr *>*/ *dex_resolve_entrypoints_in_class(RzBinDex *dex,
entrypoint->vaddr = encoded_method->code_offset;
entrypoint->paddr = 0;
}
if (entrypoint && !rz_list_append(entrypoints, entrypoint)) {
if (entrypoint && !rz_pvector_push(entrypoints, entrypoint)) {
free(entrypoint);
}
}
Expand All @@ -1785,27 +1785,26 @@ static RzList /*<RzBinAddr *>*/ *dex_resolve_entrypoints_in_class(RzBinDex *dex,
}

/**
* \brief Returns a RzList<RzBinAddr*> containing the dex entripoints
* \brief Returns a RzPVector<RzBinAddr*> containing the dex entripoints
*/
RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex) {
RZ_API RZ_OWN RzPVector /*<RzBinAddr *>*/ *rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex) {
rz_return_val_if_fail(dex, NULL);

DexClassDef *class_def;
RzList *list = NULL;
RzList *entrypoints = NULL;
RzPVector *entrypoints = NULL, *vec = NULL;
void **it;

entrypoints = rz_list_newf((RzListFree)free);
entrypoints = rz_pvector_new((RzPVectorFree)free);
if (!entrypoints) {
return NULL;
}

rz_pvector_foreach (dex->class_defs, it) {
class_def = (DexClassDef *)*it;
list = dex_resolve_entrypoints_in_class(dex, class_def);
if (list) {
rz_list_join(entrypoints, list);
rz_list_free(list);
vec = dex_resolve_entrypoints_in_class(dex, class_def);
if (vec) {
rz_pvector_join(entrypoints, vec);
rz_pvector_free(vec);
}
}

Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/dex/dex.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ RZ_API RZ_OWN RzPVector /*<RzBinSymbol *>*/ *rz_bin_dex_symbols(RZ_NONNULL RzBin
RZ_API RZ_OWN RzPVector /*<RzBinImport *>*/ *rz_bin_dex_imports(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<char *>*/ *rz_bin_dex_libraries(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzBinAddr *rz_bin_dex_resolve_symbol(RZ_NONNULL RzBinDex *dex, RzBinSpecialSymbol resolve);
RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinAddr *>*/ *rz_bin_dex_entrypoints(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_BORROW RzBuffer *rz_bin_dex_relocations(RZ_NONNULL RzBinDex *dex);

RZ_API RZ_OWN char *rz_bin_dex_resolve_method_by_idx(RZ_NONNULL RzBinDex *dex, ut32 method_idx);
Expand Down
12 changes: 6 additions & 6 deletions librz/bin/format/java/class_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,13 @@ RZ_API RZ_OWN RzBinAddr *rz_bin_java_class_resolve_symbol(RZ_NONNULL RzBinJavaCl
}

/**
* \brief Returns a RzList<RzBinAddr*> containing the entrypoints
* \brief Returns a RzPVector<RzBinAddr*> containing the entrypoints
*/
RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL RzBinJavaClass *bin) {
RZ_API RZ_OWN RzPVector /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL RzBinJavaClass *bin) {
rz_return_val_if_fail(bin, NULL);

RzList *list = rz_list_newf(free);
if (!list) {
RzPVector *vec = rz_pvector_new(free);
if (!vec) {
return NULL;
}

Expand Down Expand Up @@ -984,10 +984,10 @@ RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL
continue;
}
entrypoint->vaddr = entrypoint->paddr = addr;
rz_list_append(list, entrypoint);
rz_pvector_push(vec, entrypoint);
}
}
return list;
return vec;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/java/class_bin.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ RZ_API RZ_OWN char *rz_bin_java_class_const_pool_resolve_index(RZ_NONNULL RzBinJ
RZ_API void rz_bin_java_class_as_source_code(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb);
RZ_API RZ_OWN RzBinAddr *rz_bin_java_class_resolve_symbol(RZ_NONNULL RzBinJavaClass *bin, RzBinSpecialSymbol resolve);
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_java_class_strings(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzPVector /*<RzBinAddr *>*/ *rz_bin_java_class_entrypoints(RZ_NONNULL RzBinJavaClass *bin);
RZ_API RZ_OWN RzList /*<RzBinSymbol *>*/ *rz_bin_java_class_methods_as_symbols(RZ_NONNULL RzBinJavaClass *bin);
RZ_API void rz_bin_java_class_methods_as_text(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL RzStrBuf *sb);
RZ_API void rz_bin_java_class_methods_as_json(RZ_NONNULL RzBinJavaClass *bin, RZ_NONNULL PJ *j);
Expand Down
10 changes: 5 additions & 5 deletions librz/bin/format/le/le.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,14 +1649,14 @@ RZ_OWN RzPVector /*<RzBinSection *>*/ *rz_bin_le_get_sections(RzBinFile *bf) {
return sections;
}

RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf) {
RZ_OWN RzPVector /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf) {
rz_bin_le_obj_t *bin = bf->o->bin_obj;
LE_header *h = bin->header;
RzBinAddr *addr = NULL;
RzList *entries = rz_list_newf((RzListFree)free);
RzPVector *entries = rz_pvector_new((RzPVectorFree)free);
if (!entries) {
fail_cleanup:
rz_list_free(entries);
rz_pvector_free(entries);
free(addr);
return NULL;
}
Expand All @@ -1667,7 +1667,7 @@ RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf) {
CHECK(addr = RZ_NEW0(RzBinAddr));
addr->vaddr = bin->objects[h->startobj - 1].reloc_base_addr + h->eip;
addr->paddr = le_vaddr_to_paddr(bin, addr->vaddr);
CHECK(rz_list_append(entries, addr));
CHECK(rz_pvector_push(entries, addr));
addr = NULL;
}

Expand All @@ -1678,7 +1678,7 @@ RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf) {
CHECK(addr = RZ_NEW0(RzBinAddr));
addr->vaddr = e->symbol->vaddr;
addr->paddr = le_vaddr_to_paddr(bin, addr->vaddr);
CHECK(rz_list_append(entries, addr));
CHECK(rz_pvector_push(entries, addr));
addr = NULL;
}
}
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/le/le.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool rz_bin_le_check_buffer(RzBuffer *b);
bool rz_bin_le_load_buffer(RzBinFile *bf, RzBinObject *obj, RzBuffer *buf, Sdb *sdb);
void rz_bin_le_destroy(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinMap *>*/ *rz_bin_le_get_maps(RzBinFile *bf);
RZ_OWN RzList /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinAddr *>*/ *rz_bin_le_get_entry_points(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinSection *>*/ *rz_bin_le_get_sections(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinSymbol *>*/ *rz_bin_le_get_symbols(RzBinFile *bf);
RZ_OWN RzPVector /*<RzBinImport *>*/ *rz_bin_le_get_imports(RzBinFile *bf);
Expand Down
14 changes: 7 additions & 7 deletions librz/bin/format/luac/luac_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void luac_add_symbol(RzList /*<RzBinSymbol *>*/ *symbol_list, char *name, ut64 o
rz_list_append(symbol_list, bin_sym);
}

void luac_add_entry(RzList /*<RzBinAddr *>*/ *entry_list, ut64 offset, int entry_type) {
void luac_add_entry(RzPVector /*<RzBinAddr *>*/ *entry_vec, ut64 offset, int entry_type) {
RzBinAddr *entry = RZ_NEW0(RzBinAddr);
if (!entry) {
return;
Expand All @@ -54,7 +54,7 @@ void luac_add_entry(RzList /*<RzBinAddr *>*/ *entry_list, ut64 offset, int entry
entry->paddr = offset;
entry->type = entry_type;

rz_list_append(entry_list, entry);
rz_pvector_push(entry_vec, entry);
}

void luac_add_string(RzList /*<RzBinString *>*/ *string_list, char *string, ut64 offset, ut64 size) {
Expand Down Expand Up @@ -112,7 +112,7 @@ void luac_build_info_free(LuacBinInfo *bin_info) {
if (!bin_info) {
return;
}
rz_list_free(bin_info->entry_list);
rz_pvector_free(bin_info->entry_vec);
rz_list_free(bin_info->symbol_list);
rz_pvector_free(bin_info->section_vec);
rz_list_free(bin_info->string_list);
Expand All @@ -130,13 +130,13 @@ LuacBinInfo *luac_build_info(LuaProto *proto) {
return NULL;
}

ret->entry_list = rz_list_newf((RzListFree)free_rz_addr);
ret->entry_vec = rz_pvector_new((RzPVectorFree)free_rz_addr);
ret->symbol_list = rz_list_newf((RzListFree)rz_bin_symbol_free);
ret->section_vec = rz_pvector_new((RzPVectorFree)free_rz_section);
ret->string_list = rz_list_newf((RzListFree)free_rz_string);

if (!(ret->entry_list && ret->symbol_list && ret->section_vec && ret->string_list)) {
rz_list_free(ret->entry_list);
if (!(ret->entry_vec && ret->symbol_list && ret->section_vec && ret->string_list)) {
rz_pvector_free(ret->entry_vec);
rz_list_free(ret->symbol_list);
rz_pvector_free(ret->section_vec);
rz_list_free(ret->string_list);
Expand All @@ -147,7 +147,7 @@ LuacBinInfo *luac_build_info(LuaProto *proto) {
// add entry of main
ut64 main_entry_offset;
main_entry_offset = proto->code_offset + proto->code_skipped;
luac_add_entry(ret->entry_list, main_entry_offset, RZ_BIN_ENTRY_TYPE_PROGRAM);
luac_add_entry(ret->entry_vec, main_entry_offset, RZ_BIN_ENTRY_TYPE_PROGRAM);

return ret;
}
Expand Down
6 changes: 3 additions & 3 deletions librz/bin/format/luac/luac_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ typedef struct luac_bin_info {
st32 minor; ///< minor version
RzPVector /*<RzBinSection *>*/ *section_vec; ///< list of sections
RzList /*<RzBinSymbol *>*/ *symbol_list; ///< list of symbols
RzList /*<RzBinAddr *>*/ *entry_list; ///< list of entries
RzPVector /*<RzBinAddr *>*/ *entry_vec; ///< list of entries
RzList /*<RzBinString *>*/ *string_list; ///< list of strings
RzBinInfo *general_info; ///< general binary info from luac header
} LuacBinInfo;
Expand All @@ -187,9 +187,9 @@ void lua_free_proto_entry(LuaProto *);
* Common Operation to RzBinInfo
* Implemented in 'bin/format/luac/luac_bin.c'
* ======================================================== */
void luac_add_section(RzPVector /*<RzBinSection *>*/ *section_list, char *name, ut64 offset, ut32 size, bool is_func);
void luac_add_section(RzPVector /*<RzBinSection *>*/ *section_vec, char *name, ut64 offset, ut32 size, bool is_func);
void luac_add_symbol(RzList /*<RzBinSymbol *>*/ *symbol_list, char *name, ut64 offset, ut64 size, const char *type);
void luac_add_entry(RzList /*<RzBinAddr *>*/ *entry_list, ut64 offset, int entry_type);
void luac_add_entry(RzPVector /*<RzBinAddr *>*/ *entry_vec, ut64 offset, int entry_type);
void luac_add_string(RzList /*<RzBinString *>*/ *string_list, char *string, ut64 offset, ut64 size);

LuacBinInfo *luac_build_info(LuaProto *proto);
Expand Down
12 changes: 6 additions & 6 deletions librz/bin/format/mdmp/mdmp_pe.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "mdmp_pe.h"

static void PE_(add_tls_callbacks)(struct PE_(rz_bin_pe_obj_t) * bin, RzList /*<RzBinAddr *>*/ *list) {
static void PE_(add_tls_callbacks)(struct PE_(rz_bin_pe_obj_t) * bin, RzPVector /*<RzBinAddr *>*/ *vec) {
char *key;
int count = 0;
PE_DWord haddr, paddr, vaddr;
Expand Down Expand Up @@ -36,22 +36,22 @@ static void PE_(add_tls_callbacks)(struct PE_(rz_bin_pe_obj_t) * bin, RzList /*<
ptr->vaddr = vaddr;
ptr->hpaddr = haddr;
ptr->type = RZ_BIN_ENTRY_TYPE_TLS;
rz_list_append(list, ptr);
rz_pvector_push(vec, ptr);
}
count++;
} while (vaddr);
}

RzList /*<RzBinAddr *>*/ *PE_(rz_bin_mdmp_pe_get_entrypoint)(struct PE_(rz_bin_mdmp_pe_bin) * pe_bin) {
RzPVector /*<RzBinAddr *>*/ *PE_(rz_bin_mdmp_pe_get_entrypoint)(struct PE_(rz_bin_mdmp_pe_bin) * pe_bin) {
ut64 offset;
struct rz_bin_pe_addr_t *entry = NULL;
RzBinAddr *ptr = NULL;
RzList *ret;
RzPVector *ret;

if (!(entry = PE_(rz_bin_pe_get_entrypoint)(pe_bin->bin))) {
return NULL;
}
if (!(ret = rz_list_new())) {
if (!(ret = rz_pvector_new(NULL))) {
free(entry);
return NULL;
}
Expand All @@ -66,7 +66,7 @@ RzList /*<RzBinAddr *>*/ *PE_(rz_bin_mdmp_pe_get_entrypoint)(struct PE_(rz_bin_m
ptr->hpaddr = pe_bin->paddr + entry->haddr;
ptr->type = RZ_BIN_ENTRY_TYPE_PROGRAM;

rz_list_append(ret, ptr);
rz_pvector_push(ret, ptr);
}

PE_(add_tls_callbacks)
Expand Down
2 changes: 1 addition & 1 deletion librz/bin/format/mdmp/mdmp_pe.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct PE_(rz_bin_mdmp_pe_bin) {
struct PE_(rz_bin_pe_obj_t) * bin;
};

RzList /*<RzBinAddr *>*/ *PE_(rz_bin_mdmp_pe_get_entrypoint)(struct PE_(rz_bin_mdmp_pe_bin) * pe_bin);
RzPVector /*<RzBinAddr *>*/ *PE_(rz_bin_mdmp_pe_get_entrypoint)(struct PE_(rz_bin_mdmp_pe_bin) * pe_bin);
RzPVector /*<RzBinImport *>*/ *PE_(rz_bin_mdmp_pe_get_imports)(struct PE_(rz_bin_mdmp_pe_bin) * pe_bin);
RzPVector /*<RzBinSection *>*/ *PE_(rz_bin_mdmp_pe_get_sections)(struct PE_(rz_bin_mdmp_pe_bin) * pe_bin);
RzList /*<RzBinSymbol *>*/ *PE_(rz_bin_mdmp_pe_get_symbols)(RzBin *rbin, struct PE_(rz_bin_mdmp_pe_bin) * pe_bin);
Expand Down
Loading

0 comments on commit 8cf9904

Please sign in to comment.