Rizin
unix-like reverse engineering framework and cli tools
xrefs.c File Reference
#include <rz_analysis.h>
#include <rz_cons.h>

Go to the source code of this file.

Functions

static RzAnalysisXRefrz_analysis_xref_new (ut64 from, ut64 to, ut64 type)
 
static void rz_analysis_xref_free (RzAnalysisXRef *xref)
 
RZ_API RzListrz_analysis_xref_list_new ()
 
static void xrefs_ht_free (HtUPKv *kv)
 
static void xrefs_ref_free (HtUPKv *kv)
 
static bool appendRef (void *u, const ut64 k, const void *v)
 
static bool mylistrefs_cb (void *list, const ut64 k, const void *v)
 
static int ref_cmp (const RzAnalysisXRef *a, const RzAnalysisXRef *b)
 
static void sortxrefs (RzList *list)
 
static void listxrefs (HtUP *m, ut64 addr, RzList *list)
 
static bool set_xref (HtUP *m, RzAnalysisXRef *xref, bool from2to)
 
RZ_API bool rz_analysis_xrefs_set (RzAnalysis *analysis, ut64 from, ut64 to, RzAnalysisXRefType type)
 
RZ_API bool rz_analysis_xrefs_deln (RzAnalysis *analysis, ut64 from, ut64 to, RzAnalysisXRefType type)
 
RZ_API bool rz_analysis_xref_del (RzAnalysis *analysis, ut64 from, ut64 to)
 
RZ_API RzListrz_analysis_xrefs_get_to (RzAnalysis *analysis, ut64 addr)
 
RZ_API RzListrz_analysis_xrefs_get_from (RzAnalysis *analysis, ut64 addr)
 
RZ_API RZ_OWN RzListrz_analysis_xrefs_list (RzAnalysis *analysis)
 Get list of all xrefs. More...
 
RZ_API const char * rz_analysis_xrefs_type_tostring (RzAnalysisXRefType type)
 
RZ_API RzAnalysisXRefType rz_analysis_xrefs_type (char ch)
 
RZ_API bool rz_analysis_xrefs_init (RzAnalysis *analysis)
 
static bool count_cb (void *user, const ut64 k, const void *v)
 
RZ_API ut64 rz_analysis_xrefs_count (RzAnalysis *analysis)
 
static RzListfcn_get_refs (RzAnalysisFunction *fcn, HtUP *ht)
 
RZ_API RzListrz_analysis_function_get_xrefs_from (RzAnalysisFunction *fcn)
 
RZ_API RzListrz_analysis_function_get_xrefs_to (RzAnalysisFunction *fcn)
 
RZ_API const char * rz_analysis_ref_type_tostring (RzAnalysisXRefType t)
 

Function Documentation

◆ appendRef()

static bool appendRef ( void *  u,
const ut64  k,
const void *  v 
)
static

Definition at line 50 of file xrefs.c.

50  {
51  RzList *list = (RzList *)u;
52  RzAnalysisXRef *xref = (RzAnalysisXRef *)v;
53  RzAnalysisXRef *cloned = rz_analysis_xref_new(xref->from, xref->to, xref->type);
54  if (cloned) {
55  rz_list_append(list, cloned);
56  return true;
57  }
58  return false;
59 }
const char * v
Definition: dsignal.c:12
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RzAnalysisXRefType type
Definition: rz_analysis.h:909
static RzAnalysisXRef * rz_analysis_xref_new(ut64 from, ut64 to, ut64 type)
Definition: xrefs.c:24

References rz_analysis_ref_t::from, list(), rz_analysis_xref_new(), rz_list_append(), rz_analysis_ref_t::to, rz_analysis_ref_t::type, and v.

Referenced by listxrefs(), and mylistrefs_cb().

◆ count_cb()

static bool count_cb ( void *  user,
const ut64  k,
const void *  v 
)
static

Definition at line 267 of file xrefs.c.

267  {
268  (*(ut64 *)user) += ((HtUP *)v)->count;
269  return true;
270 }
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References ut64(), and v.

Referenced by rz_analysis_xrefs_count().

◆ fcn_get_refs()

static RzList* fcn_get_refs ( RzAnalysisFunction fcn,
HtUP *  ht 
)
static

Definition at line 278 of file xrefs.c.

278  {
279  RzListIter *iter;
280  RzAnalysisBlock *bb;
282  if (!list) {
283  return NULL;
284  }
285  rz_list_foreach (fcn->bbs, iter, bb) {
286  int i;
287 
288  for (i = 0; i < bb->ninstr; i++) {
289  ut64 at = bb->addr + rz_analysis_block_get_op_offset(bb, i);
290  listxrefs(ht, at, list);
291  }
292  }
293  sortxrefs(list);
294  return list;
295 }
lzma_index ** i
Definition: index.h:629
RZ_API ut16 rz_analysis_block_get_op_offset(RzAnalysisBlock *block, size_t i)
Definition: block.c:1006
#define NULL
Definition: cris-opc.c:27
RZ_API RzList * rz_analysis_xref_list_new()
Definition: xrefs.c:38
static void listxrefs(HtUP *m, ut64 addr, RzList *list)
Definition: xrefs.c:87
static void sortxrefs(RzList *list)
Definition: xrefs.c:83

References rz_analysis_bb_t::addr, rz_analysis_function_t::bbs, i, list(), listxrefs(), rz_analysis_bb_t::ninstr, NULL, rz_analysis_block_get_op_offset(), rz_analysis_xref_list_new(), sortxrefs(), and ut64().

Referenced by rz_analysis_function_get_xrefs_from(), and rz_analysis_function_get_xrefs_to().

◆ listxrefs()

static void listxrefs ( HtUP *  m,
ut64  addr,
RzList list 
)
static

Definition at line 87 of file xrefs.c.

87  {
88  if (addr == UT64_MAX) {
89  ht_up_foreach(m, mylistrefs_cb, list);
90  } else {
91  HtUP *d = ht_up_find(m, addr, NULL);
92  if (d) {
93  ht_up_foreach(d, appendRef, list);
94  }
95  }
96 }
#define UT64_MAX
Definition: rz_types_base.h:86
#define d(i)
Definition: sha256.c:44
static bool mylistrefs_cb(void *list, const ut64 k, const void *v)
Definition: xrefs.c:61
static bool appendRef(void *u, const ut64 k, const void *v)
Definition: xrefs.c:50
static int addr
Definition: z80asm.c:58

References addr, appendRef(), d, list(), regress::m, mylistrefs_cb(), NULL, and UT64_MAX.

Referenced by fcn_get_refs(), rz_analysis_xrefs_get_from(), rz_analysis_xrefs_get_to(), and rz_analysis_xrefs_list().

◆ mylistrefs_cb()

static bool mylistrefs_cb ( void *  list,
const ut64  k,
const void *  v 
)
static

Definition at line 61 of file xrefs.c.

61  {
62  HtUP *ht = (HtUP *)v;
63  ht_up_foreach(ht, appendRef, list);
64  return true;
65 }

References appendRef(), list(), and v.

Referenced by listxrefs().

◆ ref_cmp()

static int ref_cmp ( const RzAnalysisXRef a,
const RzAnalysisXRef b 
)
static

Definition at line 67 of file xrefs.c.

67  {
68  if (a->from < b->from) {
69  return -1;
70  }
71  if (a->from > b->from) {
72  return 1;
73  }
74  if (a->to < b->to) {
75  return -1;
76  }
77  if (a->to > b->to) {
78  return 1;
79  }
80  return 0;
81 }
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, and b.

Referenced by sortxrefs().

◆ rz_analysis_function_get_xrefs_from()

◆ rz_analysis_function_get_xrefs_to()

RZ_API RzList* rz_analysis_function_get_xrefs_to ( RzAnalysisFunction fcn)

◆ rz_analysis_ref_type_tostring()

RZ_API const char* rz_analysis_ref_type_tostring ( RzAnalysisXRefType  t)

Definition at line 307 of file xrefs.c.

307  {
308  switch (t) {
310  return "null";
312  return "code";
314  return "call";
316  return "data";
318  return "string";
319  }
320  return "unknown";
321 }
@ RZ_ANALYSIS_XREF_TYPE_CODE
Definition: rz_analysis.h:900
@ RZ_ANALYSIS_XREF_TYPE_NULL
Definition: rz_analysis.h:899
@ RZ_ANALYSIS_XREF_TYPE_STRING
Definition: rz_analysis.h:903
@ RZ_ANALYSIS_XREF_TYPE_CALL
Definition: rz_analysis.h:901
@ RZ_ANALYSIS_XREF_TYPE_DATA
Definition: rz_analysis.h:902

References RZ_ANALYSIS_XREF_TYPE_CALL, RZ_ANALYSIS_XREF_TYPE_CODE, RZ_ANALYSIS_XREF_TYPE_DATA, RZ_ANALYSIS_XREF_TYPE_NULL, and RZ_ANALYSIS_XREF_TYPE_STRING.

◆ rz_analysis_xref_del()

RZ_API bool rz_analysis_xref_del ( RzAnalysis analysis,
ut64  from,
ut64  to 
)

Definition at line 163 of file xrefs.c.

163  {
164  bool res = false;
170  return res;
171 }
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
RZ_API bool rz_analysis_xrefs_deln(RzAnalysis *analysis, ut64 from, ut64 to, RzAnalysisXRefType type)
Definition: xrefs.c:148

References from, RZ_ANALYSIS_XREF_TYPE_CALL, RZ_ANALYSIS_XREF_TYPE_CODE, RZ_ANALYSIS_XREF_TYPE_DATA, RZ_ANALYSIS_XREF_TYPE_NULL, RZ_ANALYSIS_XREF_TYPE_STRING, rz_analysis_xrefs_deln(), and to.

Referenced by rz_analysis_xrefs_del_handler().

◆ rz_analysis_xref_free()

static void rz_analysis_xref_free ( RzAnalysisXRef xref)
static

Definition at line 34 of file xrefs.c.

34  {
35  free(xref);
36 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

Referenced by rz_analysis_xrefs_set(), and xrefs_ref_free().

◆ rz_analysis_xref_list_new()

RZ_API RzList* rz_analysis_xref_list_new ( )

Definition at line 38 of file xrefs.c.

38  {
39  return rz_list_newf((RzListFree)free);
40 }
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11

References free(), and rz_list_newf().

Referenced by fcn_get_refs(), rz_analysis_xrefs_get_from(), rz_analysis_xrefs_get_to(), and rz_analysis_xrefs_list().

◆ rz_analysis_xref_new()

static RzAnalysisXRef* rz_analysis_xref_new ( ut64  from,
ut64  to,
ut64  type 
)
static

Definition at line 24 of file xrefs.c.

24  {
26  if (xref) {
27  xref->from = from;
28  xref->to = to;
29  xref->type = (type == -1) ? RZ_ANALYSIS_XREF_TYPE_CODE : type;
30  }
31  return xref;
32 }
int type
Definition: mipsasm.c:17
#define RZ_NEW(x)
Definition: rz_types.h:285

References rz_analysis_ref_t::from, from, RZ_ANALYSIS_XREF_TYPE_CODE, RZ_NEW, rz_analysis_ref_t::to, to, type, and rz_analysis_ref_t::type.

Referenced by appendRef(), and rz_analysis_xrefs_set().

◆ rz_analysis_xrefs_count()

RZ_API ut64 rz_analysis_xrefs_count ( RzAnalysis analysis)

Definition at line 272 of file xrefs.c.

272  {
273  ut64 ret = 0;
274  ht_up_foreach(analysis->ht_xrefs_to, count_cb, &ret);
275  return ret;
276 }
static bool count_cb(void *user, const ut64 k, const void *v)
Definition: xrefs.c:267

References count_cb(), rz_analysis_t::ht_xrefs_to, and ut64().

Referenced by rz_print_analysis_details_handler().

◆ rz_analysis_xrefs_deln()

RZ_API bool rz_analysis_xrefs_deln ( RzAnalysis analysis,
ut64  from,
ut64  to,
RzAnalysisXRefType  type 
)

Definition at line 148 of file xrefs.c.

148  {
149  if (!analysis) {
150  return false;
151  }
152  HtUP *ht1 = ht_up_find(analysis->ht_xrefs_from, from, NULL);
153  if (ht1) {
154  ht_up_delete(ht1, to);
155  }
156  HtUP *ht2 = ht_up_find(analysis->ht_xrefs_to, to, NULL);
157  if (ht2) {
158  ht_up_delete(ht2, from);
159  }
160  return true;
161 }

References from, rz_analysis_t::ht_xrefs_from, rz_analysis_t::ht_xrefs_to, NULL, and to.

Referenced by rz_analysis_del_jmprefs(), rz_analysis_trim_jmprefs(), rz_analysis_xref_del(), and rz_analysis_xrefs_set().

◆ rz_analysis_xrefs_get_from()

RZ_API RzList* rz_analysis_xrefs_get_from ( RzAnalysis analysis,
ut64  addr 
)

Definition at line 187 of file xrefs.c.

187  {
189  if (!list) {
190  return NULL;
191  }
192  listxrefs(analysis->ht_xrefs_from, addr, list);
193  sortxrefs(list);
194  if (rz_list_empty(list)) {
196  list = NULL;
197  }
198  return list;
199 }
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137

References addr, rz_analysis_t::ht_xrefs_from, list(), listxrefs(), NULL, rz_analysis_xref_list_new(), rz_list_free(), and sortxrefs().

Referenced by ds_build_op_str(), ds_print_ptr(), ds_show_refs(), rz_analysis_xrefs_from_list_handler(), rz_analyze_all_data_references_to_code_handler(), rz_core_print_disasm_json(), and rz_core_visual_xrefs().

◆ rz_analysis_xrefs_get_to()

◆ rz_analysis_xrefs_init()

RZ_API bool rz_analysis_xrefs_init ( RzAnalysis analysis)

Definition at line 245 of file xrefs.c.

245  {
246  ht_up_free(analysis->ht_xrefs_from);
247  analysis->ht_xrefs_from = NULL;
248  ht_up_free(analysis->ht_xrefs_to);
249  analysis->ht_xrefs_to = NULL;
250 
251  HtUP *tmp = ht_up_new(NULL, xrefs_ht_free, NULL);
252  if (!tmp) {
253  return false;
254  }
255  analysis->ht_xrefs_from = tmp;
256 
257  tmp = ht_up_new(NULL, xrefs_ht_free, NULL);
258  if (!tmp) {
259  ht_up_free(analysis->ht_xrefs_from);
260  analysis->ht_xrefs_from = NULL;
261  return false;
262  }
263  analysis->ht_xrefs_to = tmp;
264  return true;
265 }
static void xrefs_ht_free(HtUPKv *kv)
Definition: xrefs.c:42

References rz_analysis_t::ht_xrefs_from, rz_analysis_t::ht_xrefs_to, NULL, autogen_x86imm::tmp, and xrefs_ht_free().

Referenced by __rebase_everything(), rz_analysis_new(), and rz_analysis_xrefs_del_all_handler().

◆ rz_analysis_xrefs_list()

RZ_API RZ_OWN RzList* rz_analysis_xrefs_list ( RzAnalysis analysis)

Get list of all xrefs.

Parameters
analysisRzAnalysis instance
Returns
RzList <RzAnalysisXRef *>

Definition at line 206 of file xrefs.c.

206  {
207  rz_return_val_if_fail(analysis, NULL);
209  if (list) {
210  listxrefs(analysis->ht_xrefs_from, UT64_MAX, list);
211  sortxrefs(list);
212  }
213  return list;
214 }

References rz_analysis_t::ht_xrefs_from, list(), listxrefs(), NULL, rz_analysis_xref_list_new(), rz_return_val_if_fail, sortxrefs(), and UT64_MAX.

Referenced by rz_analysis_xrefs_list_handler(), and rz_core_analysis_resolve_jumps().

◆ rz_analysis_xrefs_set()

RZ_API bool rz_analysis_xrefs_set ( RzAnalysis analysis,
ut64  from,
ut64  to,
RzAnalysisXRefType  type 
)

Definition at line 117 of file xrefs.c.

117  {
118  if (!analysis || from == to) {
119  return false;
120  }
121  if (analysis->iob.is_valid_offset) {
122  if (!analysis->iob.is_valid_offset(analysis->iob.io, from, 0)) {
123  return false;
124  }
125  if (!analysis->iob.is_valid_offset(analysis->iob.io, to, 0)) {
126  return false;
127  }
128  }
130  if (!xref) {
131  return false;
132  }
133  if (!set_xref(analysis->ht_xrefs_from, xref, true)) {
134  // Pointer isn't added to <ht_xrefs_from> so we have to release it
135  rz_analysis_xref_free(xref);
136  return false;
137  }
138  if (!set_xref(analysis->ht_xrefs_to, xref, false)) {
139  // Delete the entry in <ht_xrefs_from>
140  rz_analysis_xrefs_deln(analysis, from, to, type);
141  // Pointer isn't added to <ht_xrefs_to> so we have to release it
142  rz_analysis_xref_free(xref);
143  return false;
144  }
145  return true;
146 }
RzIOBind iob
Definition: rz_analysis.h:574
RzIOIsValidOff is_valid_offset
Definition: rz_io.h:257
RzIO * io
Definition: rz_io.h:232
static bool set_xref(HtUP *m, RzAnalysisXRef *xref, bool from2to)
Definition: xrefs.c:98
static void rz_analysis_xref_free(RzAnalysisXRef *xref)
Definition: xrefs.c:34

References from, rz_analysis_t::ht_xrefs_from, rz_analysis_t::ht_xrefs_to, rz_io_bind_t::io, rz_analysis_t::iob, rz_io_bind_t::is_valid_offset, rz_analysis_xref_free(), rz_analysis_xref_new(), rz_analysis_xrefs_deln(), set_xref(), to, and type.

Referenced by __core_analysis_fcn(), __rebase_xrefs_i(), _analysis_calls(), _CbInRangeAav(), add_string_ref(), apply_case(), apply_switch(), core_analysis_followptr(), esilbreak_mem_read(), golang_recover_string_arm32(), golang_recover_string_arm64(), golang_recover_string_mips32(), golang_recover_string_mips64(), golang_recover_string_ppc64(), golang_recover_string_riscv64(), golang_recover_string_x64(), golang_recover_string_x86(), objc_find_refs(), run_basic_block_analysis(), rz_analysis_set_stringrefs(), rz_analysis_xrefs_copy_handler(), rz_core_analysis_esil(), rz_core_analysis_fcn(), rz_core_bin_load(), set_new_xref(), xrefs_load_cb(), and xrefs_set().

◆ rz_analysis_xrefs_type()

RZ_API RzAnalysisXRefType rz_analysis_xrefs_type ( char  ch)

Definition at line 232 of file xrefs.c.

232  {
233  switch (ch) {
239  return (RzAnalysisXRefType)ch;
240  default:
242  }
243 }
RzAnalysisXRefType
Definition: rz_analysis.h:898

References RZ_ANALYSIS_XREF_TYPE_CALL, RZ_ANALYSIS_XREF_TYPE_CODE, RZ_ANALYSIS_XREF_TYPE_DATA, RZ_ANALYSIS_XREF_TYPE_NULL, and RZ_ANALYSIS_XREF_TYPE_STRING.

◆ rz_analysis_xrefs_type_tostring()

RZ_API const char* rz_analysis_xrefs_type_tostring ( RzAnalysisXRefType  type)

◆ set_xref()

static bool set_xref ( HtUP *  m,
RzAnalysisXRef xref,
bool  from2to 
)
static

Definition at line 98 of file xrefs.c.

98  {
99  ut64 key1 = from2to ? xref->from : xref->to;
100  HtUP *ht = ht_up_find(m, key1, NULL);
101  if (!ht) {
102  // RzAnalysis::ht_xrefs_to is responsible for releasing of pointers.
103  HtUPKvFreeFunc cb = from2to ? NULL : xrefs_ref_free;
104  ht = ht_up_new(NULL, cb, NULL);
105  if (!ht) {
106  return false;
107  }
108  if (!ht_up_insert(m, key1, ht)) {
109  return false;
110  }
111  }
112  ut64 key2 = from2to ? xref->to : xref->from;
113  return ht_up_update(ht, key2, xref);
114 }
static void xrefs_ref_free(HtUPKv *kv)
Definition: xrefs.c:46
static const char * cb[]
Definition: z80_tab.h:176

References cb, rz_analysis_ref_t::from, regress::m, NULL, rz_analysis_ref_t::to, ut64(), and xrefs_ref_free().

Referenced by rz_analysis_xrefs_set().

◆ sortxrefs()

static void sortxrefs ( RzList list)
static

Definition at line 83 of file xrefs.c.

83  {
85 }
RZ_API void rz_list_sort(RZ_NONNULL RzList *list, RZ_NONNULL RzListComparator cmp)
Sorts via merge sort or via insertion sort a list.
Definition: list.c:743
int(* RzListComparator)(const void *value, const void *list_data)
Definition: rz_list.h:33
static int ref_cmp(const RzAnalysisXRef *a, const RzAnalysisXRef *b)
Definition: xrefs.c:67

References list(), ref_cmp(), and rz_list_sort().

Referenced by fcn_get_refs(), rz_analysis_xrefs_get_from(), rz_analysis_xrefs_get_to(), and rz_analysis_xrefs_list().

◆ xrefs_ht_free()

static void xrefs_ht_free ( HtUPKv *  kv)
static

Definition at line 42 of file xrefs.c.

42  {
43  ht_up_free(kv->value);
44 }

Referenced by rz_analysis_xrefs_init().

◆ xrefs_ref_free()

static void xrefs_ref_free ( HtUPKv *  kv)
static

Definition at line 46 of file xrefs.c.

46  {
47  rz_analysis_xref_free(kv->value);
48 }

References rz_analysis_xref_free().

Referenced by set_xref().