Rizin
unix-like reverse engineering framework and cli tools
typelink.c File Reference
#include <rz_util.h>
#include <rz_type.h>
#include <rz_analysis.h>
#include <string.h>
#include <sdb.h>

Go to the source code of this file.

Classes

struct  TListMeta
 

Functions

RZ_API bool rz_analysis_type_link_exists (RzAnalysis *analysis, ut64 addr)
 Checks if the RzType linked to the given address. More...
 
RZ_API RZ_BORROW RzTyperz_analysis_type_link_at (RzAnalysis *analysis, ut64 addr)
 Returns the RzType linked to the given address. More...
 
RZ_API bool rz_analysis_type_set_link (RzAnalysis *analysis, RZ_BORROW RzType *type, ut64 addr)
 Links the given RzType to the address. More...
 
RZ_API bool rz_analysis_type_unlink (RzAnalysis *analysis, ut64 addr)
 Removes the type link given the address. More...
 
RZ_API bool rz_analysis_type_unlink_all (RzAnalysis *analysis)
 Removes all type links. More...
 
static bool type_collect_cb (void *user, ut64 k, const void *v)
 
RZ_API RZ_OWN RzListrz_analysis_type_links (RzAnalysis *analysis)
 Returns the list of all linked types. More...
 
static bool type_paths_collect_by_offset_cb (void *user, ut64 k, const void *v)
 
RZ_API RZ_OWN RzListrz_analysis_type_links_by_offset (RzAnalysis *analysis, ut64 offset)
 Returns the list of all structured types that are linked and have members matching the offset. More...
 
static bool type_paths_collect_by_address_cb (void *user, ut64 k, const void *v)
 
RZ_API RZ_OWN RzListrz_analysis_type_paths_by_address (RzAnalysis *analysis, ut64 addr)
 Returns the list of all type paths that are linked to some address and have suitable offset. More...
 

Function Documentation

◆ rz_analysis_type_link_at()

RZ_API RZ_BORROW RzType* rz_analysis_type_link_at ( RzAnalysis analysis,
ut64  addr 
)

Returns the RzType linked to the given address.

Parameters
analysisRzAnalysis instance
addrThe address to check the link at

Definition at line 31 of file typelink.c.

31  {
32  rz_return_val_if_fail(analysis, NULL);
33  if (addr == UT64_MAX) {
34  return NULL;
35  }
36  bool found = false;
37  RzType *result = ht_up_find(analysis->type_links, addr, &found);
38  if (!found || !result) {
39  return NULL;
40  }
41  return result;
42 }
#define NULL
Definition: cris-opc.c:27
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define UT64_MAX
Definition: rz_types_base.h:86
HtUP * type_links
Definition: rz_analysis.h:603
static int addr
Definition: z80asm.c:58

References addr, found, NULL, rz_return_val_if_fail, rz_analysis_t::type_links, and UT64_MAX.

Referenced by rz_core_print_disasm(), and rz_core_types_link_show().

◆ rz_analysis_type_link_exists()

RZ_API bool rz_analysis_type_link_exists ( RzAnalysis analysis,
ut64  addr 
)

Checks if the RzType linked to the given address.

Parameters
analysisRzAnalysis instance
addrThe address to check the link at

Definition at line 16 of file typelink.c.

16  {
17  rz_return_val_if_fail(analysis, false);
18  if (addr == UT64_MAX) {
19  return false;
20  }
21  bool found = false;
22  return ht_up_find(analysis->type_links, addr, &found) && found;
23 }

References addr, found, rz_return_val_if_fail, rz_analysis_t::type_links, and UT64_MAX.

◆ rz_analysis_type_links()

RZ_API RZ_OWN RzList* rz_analysis_type_links ( RzAnalysis analysis)

Returns the list of all linked types.

Parameters
analysisRzAnalysis instance

Definition at line 96 of file typelink.c.

96  {
97  rz_return_val_if_fail(analysis, NULL);
99  ht_up_foreach(analysis->type_links, type_collect_cb, types);
100  return types;
101 }
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
insn_type_descr_t types[]
Definition: or1k_disas.c:7

References NULL, rz_list_new(), rz_return_val_if_fail, type_collect_cb(), rz_analysis_t::type_links, and types.

◆ rz_analysis_type_links_by_offset()

RZ_API RZ_OWN RzList* rz_analysis_type_links_by_offset ( RzAnalysis analysis,
ut64  offset 
)

Returns the list of all structured types that are linked and have members matching the offset.

Parameters
analysisRzAnalysis instance
offsetThe offset of the member to match against

Definition at line 142 of file typelink.c.

142  {
143  rz_return_val_if_fail(analysis, NULL);
144  if (offset == UT64_MAX) {
145  return NULL;
146  }
147  RzList *typepaths = rz_list_new();
148  struct TListMeta tl = {
149  .typedb = analysis->typedb,
150  .l = typepaths,
151  .addr = 0,
152  .offset = offset
153  };
154  ht_up_foreach(analysis->type_links, type_paths_collect_by_offset_cb, &tl);
155  return typepaths;
156 }
voidpf uLong offset
Definition: ioapi.h:144
const RzTypeDB * typedb
Definition: typelink.c:104
RzTypeDB * typedb
Definition: rz_analysis.h:602

References NULL, rz_list_new(), rz_return_val_if_fail, rz_analysis_t::type_links, type_paths_collect_by_offset_cb(), TListMeta::typedb, rz_analysis_t::typedb, and UT64_MAX.

◆ rz_analysis_type_paths_by_address()

RZ_API RZ_OWN RzList* rz_analysis_type_paths_by_address ( RzAnalysis analysis,
ut64  addr 
)

Returns the list of all type paths that are linked to some address and have suitable offset.

Parameters
analysisRzAnalysis instance
addrThe address to check against possible matches

Definition at line 201 of file typelink.c.

201  {
202  rz_return_val_if_fail(analysis, NULL);
203  if (addr == UT64_MAX) {
204  return NULL;
205  }
206  RzList *typepaths = rz_list_new();
207  struct TListMeta tl = {
208  .typedb = analysis->typedb,
209  .l = typepaths,
210  .addr = addr,
211  .offset = 0
212  };
213  ht_up_foreach(analysis->type_links, type_paths_collect_by_address_cb, &tl);
214  return typepaths;
215 }

References addr, NULL, rz_list_new(), rz_return_val_if_fail, rz_analysis_t::type_links, type_paths_collect_by_address_cb(), TListMeta::typedb, rz_analysis_t::typedb, and UT64_MAX.

Referenced by resolve_type_links(), and set_offset_hint().

◆ rz_analysis_type_set_link()

RZ_API bool rz_analysis_type_set_link ( RzAnalysis analysis,
RZ_BORROW RzType type,
ut64  addr 
)

Links the given RzType to the address.

Parameters
analysisRzAnalysis instance
typeRzType to link
addrThe address to add the type link

Definition at line 51 of file typelink.c.

51  {
52  rz_return_val_if_fail(analysis && type, false);
53  ht_up_insert(analysis->type_links, addr, type);
54  return true;
55 }
int type
Definition: mipsasm.c:17

References addr, rz_return_val_if_fail, type, and rz_analysis_t::type_links.

Referenced by rz_core_types_link().

◆ rz_analysis_type_unlink()

RZ_API bool rz_analysis_type_unlink ( RzAnalysis analysis,
ut64  addr 
)

Removes the type link given the address.

Parameters
analysisRzAnalysis instance
addrThe address to remove the type link from

Definition at line 63 of file typelink.c.

63  {
64  rz_return_val_if_fail(analysis, false);
65  ht_up_delete(analysis->type_links, addr);
66  return true;
67 }

References addr, rz_return_val_if_fail, and rz_analysis_t::type_links.

Referenced by rz_type_link_del_handler().

◆ rz_analysis_type_unlink_all()

RZ_API bool rz_analysis_type_unlink_all ( RzAnalysis analysis)

Removes all type links.

Parameters
analysisRzAnalysis instance

Definition at line 74 of file typelink.c.

74  {
75  rz_return_val_if_fail(analysis, false);
76  ht_up_free(analysis->type_links);
77  analysis->type_links = ht_up_new0();
78  if (!analysis->type_links) {
79  return false;
80  }
81  return true;
82 }

References rz_return_val_if_fail, and rz_analysis_t::type_links.

Referenced by rz_type_link_del_all_handler().

◆ type_collect_cb()

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

Definition at line 84 of file typelink.c.

84  {
85  rz_return_val_if_fail(user && v, false);
86  RzList *l = user;
87  rz_list_append(l, (RzType *)v);
88  return true;
89 }
const char * v
Definition: dsignal.c:12
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

References rz_list_append(), rz_return_val_if_fail, and v.

Referenced by rz_analysis_type_links().

◆ type_paths_collect_by_address_cb()

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

Definition at line 159 of file typelink.c.

159  {
160  rz_return_val_if_fail(user && v, false);
161  struct TListMeta *tl = (struct TListMeta *)user;
162  // If the possible offset doesn't make sense - we skip it
163  if (tl->addr < k) {
164  return true;
165  }
166 
167  RzType *t = (RzType *)v;
168  // Handle only identifiers here
169  if (t->kind != RZ_TYPE_KIND_IDENTIFIER) {
170  return true;
171  }
172  if (!t->identifier.name) {
173  return true;
174  }
175  // Get the base type
176  RzBaseType *btype = rz_type_db_get_base_type(tl->typedb, t->identifier.name);
177  if (!btype) {
178  return true;
179  }
180  // Calculate the possible offset as a difference between base address of the type link
181  // and the given address to check against
182  st64 offset = (st64)(tl->addr - k);
183  if (offset < 0) {
184  return true;
185  }
186  if (btype->kind == RZ_BASE_TYPE_KIND_STRUCT || btype->kind == RZ_BASE_TYPE_KIND_UNION) {
188  if (list) {
189  rz_list_join(tl->l, list);
190  }
191  }
192  return true;
193 }
RZ_API RZ_BORROW RzBaseType * rz_type_db_get_base_type(const RzTypeDB *typedb, RZ_NONNULL const char *name)
Searches for the RzBaseType in the types database given the name.
Definition: base.c:57
const char * k
Definition: dsignal.c:11
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API bool rz_list_join(RZ_NONNULL RzList *list1, RZ_NONNULL RzList *list2)
Joins 2 list into one (list2 pointer needs to be freed by the user)
Definition: list.c:209
@ RZ_BASE_TYPE_KIND_UNION
Definition: rz_type.h:74
@ RZ_BASE_TYPE_KIND_STRUCT
Definition: rz_type.h:73
@ RZ_TYPE_KIND_IDENTIFIER
Definition: rz_type.h:128
#define st64
Definition: rz_types_base.h:10
ut64 addr
Definition: typelink.c:106
RzList * l
Definition: typelink.c:105
RzBaseTypeKind kind
Definition: rz_type.h:115
RzTypeKind kind
Definition: rz_type.h:155
struct rz_type_t::@292::@294 identifier
RZ_API RZ_OWN RzList * rz_type_path_by_offset(const RzTypeDB *typedb, RzBaseType *btype, ut64 offset)
Returns the list of all type paths matching the offset.
Definition: path.c:176

References TListMeta::addr, rz_type_t::identifier, k, rz_base_type_t::kind, rz_type_t::kind, TListMeta::l, list(), RZ_BASE_TYPE_KIND_STRUCT, RZ_BASE_TYPE_KIND_UNION, rz_list_join(), rz_return_val_if_fail, rz_type_db_get_base_type(), RZ_TYPE_KIND_IDENTIFIER, rz_type_path_by_offset(), st64, TListMeta::typedb, and v.

Referenced by rz_analysis_type_paths_by_address().

◆ type_paths_collect_by_offset_cb()

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

Definition at line 111 of file typelink.c.

111  {
112  rz_return_val_if_fail(user && v, false);
113  struct TListMeta *tl = (struct TListMeta *)user;
114  RzType *t = (RzType *)v;
115  // Handle only identifiers here
116  if (t->kind != RZ_TYPE_KIND_IDENTIFIER) {
117  return true;
118  }
119  if (!t->identifier.name) {
120  return true;
121  }
122  // Get the base type
123  RzBaseType *btype = rz_type_db_get_base_type(tl->typedb, t->identifier.name);
124  if (!btype) {
125  return true;
126  }
127  if (btype->kind == RZ_BASE_TYPE_KIND_STRUCT || btype->kind == RZ_BASE_TYPE_KIND_UNION) {
128  RzList *list = rz_type_path_by_offset(tl->typedb, btype, tl->offset);
129  if (list) {
130  rz_list_join(tl->l, list);
131  }
132  }
133  return true;
134 }
ut64 offset
Definition: typelink.c:107

References rz_type_t::identifier, rz_base_type_t::kind, rz_type_t::kind, TListMeta::l, list(), TListMeta::offset, RZ_BASE_TYPE_KIND_STRUCT, RZ_BASE_TYPE_KIND_UNION, rz_list_join(), rz_return_val_if_fail, rz_type_db_get_base_type(), RZ_TYPE_KIND_IDENTIFIER, rz_type_path_by_offset(), TListMeta::typedb, and v.

Referenced by rz_analysis_type_links_by_offset().