Skip to content

Commit

Permalink
Refactor sections in RzBinPlugin from list to pvector
Browse files Browse the repository at this point in the history
  • Loading branch information
PeiweiHu committed Jan 9, 2024
1 parent 5c28709 commit 85b88bf
Show file tree
Hide file tree
Showing 92 changed files with 581 additions and 491 deletions.
8 changes: 5 additions & 3 deletions librz/analysis/p/analysis_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ static ut64 find_method(RzAnalysis *a, ut64 addr) {
}

RzBinSection *sec;
RzListIter *it;
RzList *list = a->binb.get_sections(a->binb.bin);
void **it;
RzBinObject *obj = rz_bin_cur_object(a->binb.bin);
const RzPVector *vec = obj ? a->binb.get_sections(obj) : NULL;

rz_list_foreach (list, it, sec) {
rz_pvector_foreach (vec, it) {
sec = *it;
ut64 from = sec->vaddr;
ut64 to = from + sec->vsize;
if (!(sec->perm & RZ_PERM_X) || addr < from || addr > to) {
Expand Down
8 changes: 5 additions & 3 deletions librz/analysis/vtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,19 @@ RZ_API RzList /*<RVTableInfo *>*/ *rz_analysis_vtable_search(RVTableContext *con
return NULL;
}

RzList *sections = analysis->binb.get_sections(analysis->binb.bin);
RzBinObject *obj = rz_bin_cur_object(analysis->binb.bin);
const RzPVector *sections = obj ? analysis->binb.get_sections(obj) : NULL;
if (!sections) {
rz_list_free(vtables);
return NULL;
}

rz_cons_break_push(NULL, NULL);

RzListIter *iter;
void **iter;
RzBinSection *section;
rz_list_foreach (sections, iter, section) {
rz_pvector_foreach (sections, iter) {
section = *iter;
if (rz_cons_is_breaked()) {
break;
}
Expand Down
8 changes: 5 additions & 3 deletions librz/asm/p/asm_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ static ut64 find_method(RzAsm *a) {
}

RzBinSection *sec;
RzListIter *it;
RzList *list = a->binb.get_sections(a->binb.bin);
void **it;
RzBinObject *obj = rz_bin_cur_object(a->binb.bin);
const RzPVector *vec = obj ? a->binb.get_sections(obj) : NULL;

rz_list_foreach (list, it, sec) {
rz_pvector_foreach (vec, it) {
sec = *it;
ut64 from = sec->vaddr;
ut64 to = from + sec->vsize;
if (!(sec->perm & RZ_PERM_X) || addr < from || addr > to) {
Expand Down
12 changes: 7 additions & 5 deletions librz/bin/bfile_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,14 @@ static void string_scan_range_cfstring(RzBinFile *bf, HtUP *strings_db, RzPVecto
}

static void scan_cfstring_table(RzBinFile *bf, HtUP *strings_db, RzPVector /*<RzBinString *>*/ *results, ut64 max_interval) {
RzListIter *iter = NULL;
void **iter = NULL;
RzBinSection *section = NULL;
RzBinObject *o = bf->o;
if (!o) {
return;
}
rz_list_foreach (o->sections, iter, section) {
rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (!section->name || section->paddr >= bf->size) {
continue;
} else if (max_interval && section->size > max_interval) {
Expand Down Expand Up @@ -412,12 +413,13 @@ RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_file_strings(RZ_NONNULL RzBi
goto fail;
}
}
} else if (bf->o && !rz_list_empty(bf->o->sections)) {
} else if (bf->o && !rz_pvector_empty(bf->o->sections)) {
// returns only the strings found on the RzBinFile but within the data section
RzListIter *iter = NULL;
void **iter = NULL;
RzBinSection *section = NULL;
RzBinObject *o = bf->o;
rz_list_foreach (o->sections, iter, section) {
rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (section->paddr >= bf->size) {
continue;
} else if (max_interval && section->size > max_interval) {
Expand Down
22 changes: 9 additions & 13 deletions librz/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,6 @@ RZ_DEPRECATE RZ_API RZ_BORROW RzBinInfo *rz_bin_get_info(RzBin *bin) {
return o ? (RzBinInfo *)rz_bin_object_get_info(o) : NULL;
}

RZ_DEPRECATE RZ_API RZ_BORROW RzList /*<RzBinSection *>*/ *rz_bin_get_sections(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_sections_all(o) : NULL;
}

/**
* \brief Find the binary section at offset \p off.
*
Expand All @@ -626,12 +620,13 @@ RZ_DEPRECATE RZ_API RZ_BORROW RzList /*<RzBinSection *>*/ *rz_bin_get_sections(R
*/
RZ_API RZ_BORROW RzBinSection *rz_bin_get_section_at(RzBinObject *o, ut64 off, int va) {
RzBinSection *section;
RzListIter *iter;
void **iter;
ut64 from, to;

rz_return_val_if_fail(o, NULL);
// TODO: must be O(1) .. use sdb here
rz_list_foreach (o->sections, iter, section) {
rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (section->is_segment) {
continue;
}
Expand Down Expand Up @@ -865,7 +860,7 @@ RZ_API void rz_bin_bind(RzBin *bin, RzBinBind *b) {
b->bin = bin;
b->get_offset = __getoffset;
b->get_name = __getname;
b->get_sections = rz_bin_get_sections;
b->get_sections = rz_bin_object_get_sections_all;
b->get_vsect_at = __get_vsection_at;
b->demangle = rz_bin_demangle;
}
Expand Down Expand Up @@ -1077,7 +1072,7 @@ RZ_API RZ_OWN RzPVector /*<RzBinMap *>*/ *rz_bin_maps_of_file_sections(RZ_NONNUL
if (!binfile->o || !binfile->o->plugin || !binfile->o->plugin->sections) {
return NULL;
}
RzList *sections = binfile->o->plugin->sections(binfile);
RzPVector *sections = binfile->o->plugin->sections(binfile);
if (!sections) {
return NULL;
}
Expand All @@ -1086,8 +1081,9 @@ RZ_API RZ_OWN RzPVector /*<RzBinMap *>*/ *rz_bin_maps_of_file_sections(RZ_NONNUL
goto hcf;
}
RzBinSection *sec;
RzListIter *it;
rz_list_foreach (sections, it, sec) {
void **it;
rz_pvector_foreach (sections, it) {
sec = *it;
RzBinMap *map = RZ_NEW0(RzBinMap);
if (!map) {
goto hcf;
Expand All @@ -1101,7 +1097,7 @@ RZ_API RZ_OWN RzPVector /*<RzBinMap *>*/ *rz_bin_maps_of_file_sections(RZ_NONNUL
rz_pvector_push(r, map);
}
hcf:
rz_list_free(sections);
rz_pvector_free(sections);
return r;
}

Expand Down
10 changes: 5 additions & 5 deletions librz/bin/bin_language.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ RZ_API RzBinLanguage rz_bin_language_detect(RzBinFile *binfile) {
RzBinSymbol *sym;
RzBinImport *imp;
RzBinSection *section;
RzListIter *iter;
void **iter;

if (!info) {
return RZ_BIN_LANGUAGE_UNKNOWN;
Expand Down Expand Up @@ -148,9 +148,8 @@ RZ_API RzBinLanguage rz_bin_language_detect(RzBinFile *binfile) {
return language_apply_blocks_mask(RZ_BIN_LANGUAGE_OBJC, is_blocks);
}

void **it;
rz_pvector_foreach (o->symbols, it) {
sym = *it;
rz_pvector_foreach (o->symbols, iter) {
sym = *iter;
if (!sym->name) {
continue;
}
Expand Down Expand Up @@ -193,7 +192,8 @@ RZ_API RzBinLanguage rz_bin_language_detect(RzBinFile *binfile) {
}

if (is_macho || is_elf) {
rz_list_foreach (o->sections, iter, section) {
rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (!section->name) {
continue;
}
Expand Down
33 changes: 18 additions & 15 deletions librz/bin/bobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ RZ_IPI void rz_bin_object_free(RzBinObject *o) {
rz_pvector_free(o->libs);
rz_pvector_free(o->maps);
rz_pvector_free(o->mem);
rz_list_free(o->sections);
rz_pvector_free(o->sections);
rz_pvector_free(o->symbols);
rz_pvector_free(o->vfiles);
rz_pvector_free(o->resources);
Expand Down Expand Up @@ -689,41 +689,42 @@ RZ_API const RzPVector /*<char *>*/ *rz_bin_object_get_libs(RZ_NONNULL RzBinObje
}

/**
* \brief Get list of \p RzBinSection representing both the sections and the segments of the binary object.
* \brief Get pvector of \p RzBinSection representing both the sections and the segments of the binary object.
*/
RZ_API const RzList /*<RzBinSection *>*/ *rz_bin_object_get_sections_all(RZ_NONNULL RzBinObject *obj) {
RZ_API const RzPVector /*<RzBinSection *>*/ *rz_bin_object_get_sections_all(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);
return obj->sections;
}

static RzList /*<RzBinSection *>*/ *get_sections_or_segment(RzBinObject *obj, bool is_segment) {
RzList *res = rz_list_new();
static RzPVector /*<RzBinSection *>*/ *get_sections_or_segment(RzBinObject *obj, bool is_segment) {
RzPVector *res = rz_pvector_new(NULL);
if (!res) {
return NULL;
}
const RzList *all = rz_bin_object_get_sections_all(obj);
RzListIter *it;
const RzPVector *all = rz_bin_object_get_sections_all(obj);
void **it;
RzBinSection *sec;
rz_list_foreach (all, it, sec) {
rz_pvector_foreach (all, it) {
sec = *it;
if (sec->is_segment == is_segment) {
rz_list_append(res, sec);
rz_pvector_push(res, sec);
}
}
return res;
}

/**
* \brief Get list of \p RzBinSection representing only the sections of the binary object.
* \brief Get pvector of \p RzBinSection representing only the sections of the binary object.
*/
RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_object_get_sections(RZ_NONNULL RzBinObject *obj) {
RZ_API RZ_OWN RzPVector /*<RzBinSection *>*/ *rz_bin_object_get_sections(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);
return get_sections_or_segment(obj, false);
}

/**
* \brief Get list of \p RzBinSection representing only the segments of the binary object.
* \brief Get pvector of \p RzBinSection representing only the segments of the binary object.
*/
RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_object_get_segments(RZ_NONNULL RzBinObject *obj) {
RZ_API RZ_OWN RzPVector /*<RzBinSection *>*/ *rz_bin_object_get_segments(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);
return get_sections_or_segment(obj, true);
}
Expand Down Expand Up @@ -826,7 +827,7 @@ static void bin_section_map_fini(void *e, void *user) {
RZ_API RZ_OWN RzVector /*<RzBinSectionMap>*/ *rz_bin_object_sections_mapping_list(RZ_NONNULL RzBinObject *obj) {
rz_return_val_if_fail(obj, NULL);

const RzList *all = rz_bin_object_get_sections_all(obj);
const RzPVector *all = rz_bin_object_get_sections_all(obj);
if (!all) {
return NULL;
}
Expand All @@ -836,7 +837,9 @@ RZ_API RZ_OWN RzVector /*<RzBinSectionMap>*/ *rz_bin_object_sections_mapping_lis
RzBinSection *section, *segment;
RzListIter *iter;

rz_list_foreach (all, iter, section) {
void **it;
rz_pvector_foreach (all, it) {
section = *it;
RzList *list = section->is_segment ? segments : sections;
rz_list_append(list, section);
}
Expand Down
9 changes: 5 additions & 4 deletions librz/bin/bobj_process_section.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ RZ_IPI void rz_bin_set_and_process_sections(RzBinFile *bf, RzBinObject *o) {
RzBin *bin = bf->rbin;
RzBinPlugin *plugin = o->plugin;

rz_list_free(o->sections);
rz_pvector_free(o->sections);
if (!plugin->sections || !(o->sections = plugin->sections(bf))) {
o->sections = rz_list_newf((RzListFree)rz_bin_section_free);
o->sections = rz_pvector_new((RzPVectorFree)rz_bin_section_free);
}

HtPP *filter_db = bin->filter ? ht_pp_new0() : NULL;

RzListIter *it;
void **it;
RzBinSection *element;
rz_list_foreach (o->sections, it, element) {
rz_pvector_foreach (o->sections, it) {
element = *it;
process_handle_section(element, o, filter_db);
}

Expand Down
5 changes: 3 additions & 2 deletions librz/bin/dwarf/endian_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

RZ_IPI RzBinSection *rz_bin_dwarf_section_by_name(RzBinFile *binfile, const char *sn, bool is_dwo) {
rz_return_val_if_fail(binfile && sn, NULL);
RzListIter *iter = NULL;
void **iter = NULL;
RzBinSection *section = NULL;
RzBinSection *result_section = NULL;
RzBinObject *o = binfile->o;
Expand All @@ -19,7 +19,8 @@ RZ_IPI RzBinSection *rz_bin_dwarf_section_by_name(RzBinFile *binfile, const char
if (!name) {
return NULL;
}
rz_list_foreach (o->sections, iter, section) {
rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (!section->name) {
continue;
}
Expand Down
14 changes: 7 additions & 7 deletions librz/bin/format/dex/dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1211,30 +1211,30 @@ static RzBinSection *section_new(const char *name, ut32 perm, ut32 size, ut64 pa
}

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

RzBinSection *section;
RzList *sections = NULL;
RzPVector *sections = NULL;

sections = rz_list_newf((RzListFree)rz_bin_section_free);
sections = rz_pvector_new((RzPVectorFree)rz_bin_section_free);
if (!sections) {
return NULL;
}
section = section_new("data", RZ_PERM_RWX, dex->data_size, dex->data_offset, RZ_DEX_VIRT_ADDRESS + dex->data_offset);
if (section && !rz_list_append(sections, section)) {
if (section && !rz_pvector_push(sections, section)) {
rz_bin_section_free(section);
}
section = section_new("file", RZ_PERM_R, dex->file_size, dex->header_offset, 0);
if (section && !rz_list_append(sections, section)) {
if (section && !rz_pvector_push(sections, section)) {
rz_bin_section_free(section);
}

if (dex->relocs_code) {
section = section_new(RZ_DEX_RELOC_TARGETS, RZ_PERM_RWX, dex->relocs_size, 0, dex->relocs_offset);
if (section && !rz_list_append(sections, section)) {
if (section && !rz_pvector_push(sections, section)) {
rz_bin_section_free(section);
}
}
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 @@ -210,7 +210,7 @@ RZ_API ut64 rz_bin_dex_debug_info(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinString *>*/ *rz_bin_dex_strings(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinClass *>*/ *rz_bin_dex_classes(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzList /*<RzBinClassField *>*/ *rz_bin_dex_fields(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzList /*<RzBinSection *>*/ *rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinSection *>*/ *rz_bin_dex_sections(RZ_NONNULL RzBinDex *dex);
RZ_API RZ_OWN RzPVector /*<RzBinSymbol *>*/ *rz_bin_dex_symbols(RZ_NONNULL RzBinDex *dex);
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);
Expand Down
Loading

0 comments on commit 85b88bf

Please sign in to comment.