Skip to content

Commit

Permalink
Split rz_core_get_boundaries_prot and rewrite the code. (#4724)
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio authored Nov 19, 2024
1 parent 03fccba commit 266fe6b
Show file tree
Hide file tree
Showing 26 changed files with 1,055 additions and 531 deletions.
37 changes: 33 additions & 4 deletions librz/bin/bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,16 +605,16 @@ RZ_API void rz_bin_set_baddr(RzBin *bin, ut64 baddr) {
*
* \param o Reference to the \p RzBinObject instance
* \param off Address to search
* \param va When 0 the offset \p off is considered a physical address, otherwise a virtual address
* \param va When false the offset \p off is considered a physical address, otherwise a virtual address
* \return Pointer to a \p RzBinSection containing the address
*/
RZ_API RZ_BORROW RzBinSection *rz_bin_get_section_at(RzBinObject *o, ut64 off, int va) {
RZ_API RZ_BORROW RzBinSection *rz_bin_get_section_at(RZ_NONNULL RzBinObject *o, ut64 off, bool va) {
rz_return_val_if_fail(o, NULL);

RzBinSection *section;
void **iter;
ut64 from, to;

rz_return_val_if_fail(o, NULL);
// TODO: must be O(1) .. use sdb here
rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (section->is_segment) {
Expand All @@ -629,6 +629,35 @@ RZ_API RZ_BORROW RzBinSection *rz_bin_get_section_at(RzBinObject *o, ut64 off, i
return NULL;
}

/**
* \brief Find the binary segment at offset \p off.
*
* \param o Reference to the \p RzBinObject instance
* \param off Address to search
* \param va When false the offset \p off is considered a physical address, otherwise a virtual address
* \return Pointer to a \p RzBinSection containing the address
*/
RZ_API RZ_BORROW RzBinSection *rz_bin_get_segment_at(RZ_NONNULL RzBinObject *o, ut64 off, bool va) {
rz_return_val_if_fail(o, NULL);

RzBinSection *section;
void **iter;
ut64 from, to;

rz_pvector_foreach (o->sections, iter) {
section = *iter;
if (!section->is_segment) {
continue;
}
from = va ? rz_bin_object_addr_with_base(o, section->vaddr) : section->paddr;
to = from + (va ? section->vsize : section->size);
if (off >= from && off < to) {
return section;
}
}
return NULL;
}

/**
* \brief Find the last binary map at offset \p off .
*
Expand Down
3 changes: 0 additions & 3 deletions librz/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ RZ_API const char *rz_config_node_type(RzConfigNode *node) {
return "str";
}
if (rz_config_node_is_int(node)) {
if (!strncmp(node->value, "0x", 2)) {
return "addr";
}
return "int";
}
return "";
Expand Down
16 changes: 10 additions & 6 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2091,11 +2091,11 @@ RZ_API bool rz_core_analysis_refs(RZ_NONNULL RzCore *core, size_t nbytes) {
return core_search_for_xrefs_in_boundaries(core, from, to);
}

RzList *list = rz_core_get_boundaries_prot(core, RZ_PERM_X, NULL, "analysis");
RzList *list = rz_core_get_boundaries_select(core, "analysis.from", "analysis.to", "analysis.in");
RzListIter *iter;
RzIOMap *map;
if (!list) {
RZ_LOG_ERROR("cannot find maps with exec permisions\n");
RZ_LOG_ERROR("Cannot get xrefs boundaries when analysis.in=%s.\n", rz_config_get(core->config, "analysis.in"));
return false;
}

Expand Down Expand Up @@ -4772,7 +4772,11 @@ RZ_IPI void rz_core_analysis_value_pointers(RzCore *core, RzOutputMode mode) {
rz_core_notify_done(core, "Analyze value pointers (aav)");
rz_cons_break_push(NULL, NULL);
if (is_debug) {
RzList *list = rz_core_get_boundaries_prot(core, 0, "dbg.map", "analysis");
RzInterval interval;
interval.addr = rz_config_get_i(core->config, "analysis.from");
interval.size = rz_config_get_i(core->config, "analysis.to") - interval.addr;

RzList *list = rz_core_get_boundaries_all_debug_maps(core, interval);
RzListIter *iter;
RzIOMap *map;
if (!list) {
Expand All @@ -4788,7 +4792,7 @@ RZ_IPI void rz_core_analysis_value_pointers(RzCore *core, RzOutputMode mode) {
}
rz_list_free(list);
} else {
RzList *list = rz_core_get_boundaries_prot(core, 0, NULL, "analysis");
RzList *list = rz_core_get_boundaries_select(core, "analysis.from", "analysis.to", "analysis.in");
if (!list) {
goto beach;
}
Expand Down Expand Up @@ -5825,14 +5829,14 @@ RZ_API void rz_core_analysis_calls(RZ_NONNULL RzCore *core, bool imports_only) {
RzBinFile *binfile = rz_bin_cur(core->bin);
addr = core->offset;
if (binfile) {
ranges = rz_core_get_boundaries_prot(core, RZ_PERM_X, NULL, "analysis");
ranges = rz_core_get_boundaries_select(core, "analysis.from", "analysis.to", "analysis.in");
}
rz_cons_break_push(NULL, NULL);
if (!binfile || rz_list_length(ranges) < 1) {
RzListIter *iter;
RzIOMap *map;
rz_list_free(ranges);
ranges = rz_core_get_boundaries_prot(core, 0, NULL, "analysis");
ranges = rz_core_get_boundaries_select(core, "analysis.from", "analysis.to", "analysis.in");
if (ranges) {
rz_list_foreach (ranges, iter, map) {
ut64 addr = map->itv.addr;
Expand Down
Loading

0 comments on commit 266fe6b

Please sign in to comment.