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

Go to the source code of this file.

Macros

#define DB   analysis->sdb_cc
 

Functions

RZ_API void rz_analysis_cc_del (RzAnalysis *analysis, const char *name)
 
RZ_API bool rz_analysis_cc_set (RzAnalysis *analysis, const char *expr)
 
RZ_API char * rz_analysis_cc_get (RzAnalysis *analysis, const char *name)
 
RZ_API bool rz_analysis_cc_exist (RzAnalysis *analysis, const char *convention)
 
RZ_API const char * rz_analysis_cc_arg (RzAnalysis *analysis, const char *convention, int n)
 
RZ_API const char * rz_analysis_cc_self (RzAnalysis *analysis, const char *convention)
 
RZ_API void rz_analysis_cc_set_self (RzAnalysis *analysis, const char *convention, const char *self)
 
RZ_API const char * rz_analysis_cc_error (RzAnalysis *analysis, const char *convention)
 
RZ_API void rz_analysis_cc_set_error (RzAnalysis *analysis, const char *convention, const char *error)
 
RZ_API int rz_analysis_cc_max_arg (RzAnalysis *analysis, const char *cc)
 
RZ_API const char * rz_analysis_cc_ret (RzAnalysis *analysis, const char *convention)
 
RZ_API const char * rz_analysis_cc_default (RzAnalysis *analysis)
 
RZ_API void rz_analysis_set_cc_default (RzAnalysis *analysis, const char *cc)
 
RZ_API const char * rz_analysis_syscc_default (RzAnalysis *analysis)
 
RZ_API void rz_analysis_set_syscc_default (RzAnalysis *analysis, const char *cc)
 
RZ_API const char * rz_analysis_cc_func (RzAnalysis *analysis, const char *func_name)
 
RZ_API RzListrz_analysis_calling_conventions (RzAnalysis *analysis)
 

Macro Definition Documentation

◆ DB

#define DB   analysis->sdb_cc

Definition at line 8 of file cc.c.

Function Documentation

◆ rz_analysis_calling_conventions()

RZ_API RzList* rz_analysis_calling_conventions ( RzAnalysis analysis)

Definition at line 226 of file cc.c.

226  {
227  RzList *ccl = rz_list_new();
228  SdbKv *kv;
229  SdbListIter *iter;
230  SdbList *l = sdb_foreach_list(analysis->sdb_cc, true);
231  ls_foreach (l, iter, kv) {
232  if (!strcmp(sdbkv_value(kv), "cc")) {
233  rz_list_append(ccl, strdup(sdbkv_key(kv)));
234  }
235  }
236  ls_free(l);
237  return ccl;
238 }
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
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
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
#define ls_foreach(list, it, pos)
Definition: ls.h:31
RZ_API SdbList * sdb_foreach_list(Sdb *s, bool sorted)
Definition: sdb.c:630
static char * sdbkv_key(const SdbKv *kv)
Definition: sdbht.h:21
static char * sdbkv_value(const SdbKv *kv)
Definition: sdbht.h:25
Definition: ls.h:17
Definition: ls.h:22
Definition: sdbht.h:14

References ls_foreach, ls_free(), rz_list_append(), rz_list_new(), rz_analysis_t::sdb_cc, sdb_foreach_list(), sdbkv_key(), sdbkv_value(), and strdup().

Referenced by rz_core_types_calling_conventions_print().

◆ rz_analysis_cc_arg()

RZ_API const char* rz_analysis_cc_arg ( RzAnalysis analysis,
const char *  convention,
int  n 
)

Definition at line 122 of file cc.c.

122  {
123  rz_return_val_if_fail(analysis, NULL);
125  if (!convention) {
126  return NULL;
127  }
128 
129  const char *query = sdb_fmt("cc.%s.arg%d", convention, n);
130  const char *ret = sdb_const_get(DB, query, 0);
131  if (!ret) {
132  query = sdb_fmt("cc.%s.argn", convention);
133  ret = sdb_const_get(DB, query, 0);
134  }
135  return ret ? rz_str_constpool_get(&analysis->constpool, ret) : NULL;
136 }
#define DB
Definition: cc.c:8
#define NULL
Definition: cris-opc.c:27
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
int n
Definition: mipsasm.c:19
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API const char * rz_str_constpool_get(RzStrConstPool *pool, const char *str)
Definition: str_constpool.c:19
RZ_API const char * sdb_const_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:279
RzStrConstPool constpool
Definition: rz_analysis.h:620

References rz_analysis_t::constpool, DB, n, NULL, rz_return_val_if_fail, rz_str_constpool_get(), sdb_const_get(), and sdb_fmt().

Referenced by extract_arg(), rz_analysis_extract_rarg(), rz_analysis_var_get_argnum(), rz_core_analysis_cc_print(), rz_core_arg_get(), rz_core_arg_set(), rz_core_get_func_args(), set_fcn_args_info(), and type_match().

◆ rz_analysis_cc_default()

◆ rz_analysis_cc_del()

RZ_API void rz_analysis_cc_del ( RzAnalysis analysis,
const char *  name 
)

Definition at line 10 of file cc.c.

10  {
11  rz_return_if_fail(analysis && name);
12  size_t i;
13  RzStrBuf sb;
14  sdb_unset(DB, rz_strbuf_initf(&sb, "%s", name), 0);
15  sdb_unset(DB, rz_strbuf_setf(&sb, "cc.%s.ret", name), 0);
16  sdb_unset(DB, rz_strbuf_setf(&sb, "cc.%s.argn", name), 0);
17  for (i = 0; i < RZ_ANALYSIS_CC_MAXARG; i++) {
18  sdb_unset(DB, rz_strbuf_setf(&sb, "cc.%s.arg%zu", name, i), 0);
19  }
20  sdb_unset(DB, rz_strbuf_setf(&sb, "cc.%s.self", name), 0);
21  sdb_unset(DB, rz_strbuf_setf(&sb, "cc.%s.error", name), 0);
23 }
lzma_index ** i
Definition: index.h:629
static SblHeader sb
Definition: bin_mbn.c:26
#define RZ_ANALYSIS_CC_MAXARG
Definition: rz_analysis.h:189
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
RZ_API const char * rz_strbuf_initf(RzStrBuf *sb, const char *fmt,...)
Definition: strbuf.c:38
RZ_API const char * rz_strbuf_setf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API void rz_strbuf_fini(RzStrBuf *sb)
Definition: strbuf.c:365
RZ_API int sdb_unset(Sdb *s, const char *key, ut32 cas)
Definition: sdb.c:294
Definition: z80asm.h:102

References DB, i, RZ_ANALYSIS_CC_MAXARG, rz_return_if_fail, rz_strbuf_fini(), rz_strbuf_initf(), rz_strbuf_setf(), sb, and sdb_unset().

Referenced by rz_type_cc_del_handler().

◆ rz_analysis_cc_error()

RZ_API const char* rz_analysis_cc_error ( RzAnalysis analysis,
const char *  convention 
)

Definition at line 155 of file cc.c.

155  {
156  rz_return_val_if_fail(analysis && convention, NULL);
157  const char *query = sdb_fmt("cc.%s.error", convention);
158  const char *error = sdb_const_get(DB, query, 0);
159  return error ? rz_str_constpool_get(&analysis->constpool, error) : NULL;
160 }
void error(const char *msg)
Definition: untgz.c:593

References rz_analysis_t::constpool, DB, error(), NULL, rz_return_val_if_fail, rz_str_constpool_get(), sdb_const_get(), and sdb_fmt().

Referenced by rz_analysis_cc_get(), rz_analysis_extract_rarg(), and rz_core_analysis_cc_print().

◆ rz_analysis_cc_exist()

RZ_API bool rz_analysis_cc_exist ( RzAnalysis analysis,
const char *  convention 
)

Definition at line 116 of file cc.c.

116  {
117  rz_return_val_if_fail(analysis && convention, false);
118  const char *x = sdb_const_get(DB, convention, 0);
119  return x && *x && !strcmp(x, "cc");
120 }
int x
Definition: mipsasm.c:20

References DB, rz_return_val_if_fail, sdb_const_get(), and x.

Referenced by propagate_types_among_used_variables(), rz_analysis_cc_set_error(), rz_analysis_cc_set_self(), rz_analysis_function_cc_set_get_handler(), and rz_core_bin_apply_config().

◆ rz_analysis_cc_func()

RZ_API const char* rz_analysis_cc_func ( RzAnalysis analysis,
const char *  func_name 
)

Definition at line 220 of file cc.c.

220  {
221  rz_return_val_if_fail(analysis && func_name, NULL);
222  const char *cc = rz_type_func_cc(analysis->typedb, func_name);
223  return cc ? cc : rz_analysis_cc_default(analysis);
224 }
RZ_API const char * rz_analysis_cc_default(RzAnalysis *analysis)
Definition: cc.c:200
RzTypeDB * typedb
Definition: rz_analysis.h:602
RZ_API RZ_BORROW const char * rz_type_func_cc(RzTypeDB *typedb, RZ_NONNULL const char *name)
Searches for the RzCallable type in types database and returns calling convention.
Definition: function.c:230

References NULL, rz_analysis_cc_default(), rz_return_val_if_fail, rz_type_func_cc(), and rz_analysis_t::typedb.

Referenced by propagate_types_among_used_variables(), rz_analysis_extract_rarg(), rz_core_get_func_args(), and set_retval().

◆ rz_analysis_cc_get()

RZ_API char* rz_analysis_cc_get ( RzAnalysis analysis,
const char *  name 
)

Definition at line 75 of file cc.c.

75  {
76  rz_return_val_if_fail(analysis && name, NULL);
77  int i;
78  // get cc by name and print the expr
79  if (rz_str_cmp(sdb_const_get(DB, name, 0), "cc", -1)) {
80  RZ_LOG_ERROR("'%s' is not a valid calling convention name\n", name);
81  return NULL;
82  }
83  const char *ret = sdb_const_get(DB, sdb_fmt("cc.%s.ret", name), 0);
84  if (!ret) {
85  RZ_LOG_ERROR("Cannot find return key in calling convention named '%s'\n", name);
86  return NULL;
87  }
89  const char *self = rz_analysis_cc_self(analysis, name);
90  rz_strbuf_appendf(sb, "%s %s%s%s (", ret, self ? self : "", self ? "." : "", name);
91  bool isFirst = true;
92  for (i = 0; i < RZ_ANALYSIS_CC_MAXARG; i++) {
93  const char *k = sdb_fmt("cc.%s.arg%d", name, i);
94  const char *arg = sdb_const_get(DB, k, 0);
95  if (!arg) {
96  break;
97  }
98  rz_strbuf_appendf(sb, "%s%s", isFirst ? "" : ", ", arg);
99  isFirst = false;
100  }
101  const char *argn = sdb_const_get(DB, sdb_fmt("cc.%s.argn", name), 0);
102  if (argn) {
103  rz_strbuf_appendf(sb, "%s%s", isFirst ? "" : ", ", argn);
104  }
105  rz_strbuf_append(sb, ")");
106 
107  const char *error = rz_analysis_cc_error(analysis, name);
108  if (error) {
109  rz_strbuf_appendf(sb, " %s", error);
110  }
111 
112  rz_strbuf_append(sb, ";");
113  return rz_strbuf_drain(sb);
114 }
RZ_API const char * rz_analysis_cc_self(RzAnalysis *analysis, const char *convention)
Definition: cc.c:138
RZ_API const char * rz_analysis_cc_error(RzAnalysis *analysis, const char *convention)
Definition: cc.c:155
const char * k
Definition: dsignal.c:11
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API int rz_str_cmp(const char *dst, const char *orig, int len)
Definition: str.c:974
RZ_API RZ_OWN char * rz_strbuf_drain(RzStrBuf *sb)
Definition: strbuf.c:342
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API RzStrBuf * rz_strbuf_new(const char *s)
Definition: strbuf.c:8
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2

References DB, error(), i, k, NULL, rz_analysis_cc_error(), RZ_ANALYSIS_CC_MAXARG, rz_analysis_cc_self(), RZ_LOG_ERROR, rz_return_val_if_fail, rz_str_cmp(), rz_strbuf_append(), rz_strbuf_appendf(), rz_strbuf_drain(), rz_strbuf_new(), sb, sdb_const_get(), and sdb_fmt().

Referenced by rz_core_types_calling_conventions_print(), and types_cc_print().

◆ rz_analysis_cc_max_arg()

RZ_API int rz_analysis_cc_max_arg ( RzAnalysis analysis,
const char *  cc 
)

Definition at line 171 of file cc.c.

171  {
172  int i = 0;
173  rz_return_val_if_fail(analysis && DB && cc, 0);
174  static void *oldDB = NULL;
175  static char *oldCC = NULL;
176  static int oldArg = 0;
177  if (oldDB == DB && !strcmp(cc, oldCC)) {
178  return oldArg;
179  }
180  oldDB = DB;
181  free(oldCC);
182  oldCC = strdup(cc);
183  for (i = 0; i < RZ_ANALYSIS_CC_MAXARG; i++) {
184  const char *query = sdb_fmt("cc.%s.arg%d", cc, i);
185  const char *res = sdb_const_get(DB, query, 0);
186  if (!res) {
187  break;
188  }
189  }
190  oldArg = i;
191  return i;
192 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References DB, free(), i, NULL, RZ_ANALYSIS_CC_MAXARG, rz_return_val_if_fail, sdb_const_get(), sdb_fmt(), and strdup().

Referenced by analysis_block_cb(), extract_arg(), rz_analysis_extract_rarg(), rz_analysis_var_get_argnum(), rz_core_analysis_cc_print(), and type_match().

◆ rz_analysis_cc_ret()

RZ_API const char* rz_analysis_cc_ret ( RzAnalysis analysis,
const char *  convention 
)

Definition at line 194 of file cc.c.

194  {
195  rz_return_val_if_fail(analysis && convention, NULL);
196  char *query = sdb_fmt("cc.%s.ret", convention);
197  return sdb_const_get(DB, query, 0);
198 }

References DB, NULL, rz_return_val_if_fail, sdb_const_get(), and sdb_fmt().

Referenced by propagate_types_among_used_variables(), rz_core_analysis_cc_print(), and set_retval().

◆ rz_analysis_cc_self()

RZ_API const char* rz_analysis_cc_self ( RzAnalysis analysis,
const char *  convention 
)

Definition at line 138 of file cc.c.

138  {
139  rz_return_val_if_fail(analysis && convention, NULL);
140  const char *query = sdb_fmt("cc.%s.self", convention);
141  const char *self = sdb_const_get(DB, query, 0);
142  return self ? rz_str_constpool_get(&analysis->constpool, self) : NULL;
143 }

References rz_analysis_t::constpool, DB, NULL, rz_return_val_if_fail, rz_str_constpool_get(), sdb_const_get(), and sdb_fmt().

Referenced by rz_analysis_cc_get(), rz_analysis_extract_rarg(), and rz_core_analysis_cc_print().

◆ rz_analysis_cc_set()

RZ_API bool rz_analysis_cc_set ( RzAnalysis analysis,
const char *  expr 
)

Definition at line 25 of file cc.c.

25  {
26  rz_return_val_if_fail(analysis && expr, false);
27  char *e = strdup(expr);
28  char *p = strchr(e, '(');
29  if (!p) {
30  free(e);
31  return false;
32  }
33  *p++ = 0;
34  char *args = strdup(p);
35  rz_str_trim(p);
36  char *end = strchr(args, ')');
37  if (!end) {
38  free(args);
39  free(e);
40  return false;
41  }
42  *end++ = 0;
43  rz_str_trim(p);
44  rz_str_trim(e);
45  char *ccname = strchr(e, ' ');
46  if (ccname) {
47  *ccname++ = 0;
48  rz_str_trim(ccname);
49  } else {
50  free(args);
51  free(e);
52  return false;
53  }
54  sdb_set(DB, ccname, "cc", 0);
55  sdb_set(DB, sdb_fmt("cc.%s.ret", ccname), e, 0);
56 
57  RzList *ccArgs = rz_str_split_list(args, ",", 0);
59  const char *arg;
60  int n = 0;
61  rz_list_foreach (ccArgs, iter, arg) {
62  if (!strcmp(arg, "stack")) {
63  sdb_set(DB, sdb_fmt("cc.%s.argn", ccname), arg, 0);
64  } else {
65  sdb_set(DB, sdb_fmt("cc.%s.arg%d", ccname, n), arg, 0);
66  n++;
67  }
68  }
69  rz_list_free(ccArgs);
70  free(e);
71  free(args);
72  return true;
73 }
#define e(frag)
static const char * arg(RzAnalysis *a, csh *handle, cs_insn *insn, char *buf, int n)
Definition: arm_esil32.c:136
static RzNumCalcValue expr(RzNum *, RzNumCalc *, int)
Definition: calc.c:167
void * p
Definition: libc.cpp:67
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
int args
Definition: mipsasm.c:18
RZ_API void rz_str_trim(RZ_NONNULL RZ_INOUT char *str)
Removes whitespace characters (space, tab, newline etc.) from the beginning and end of a string.
Definition: str_trim.c:190
RZ_API RzList * rz_str_split_list(char *str, const char *c, int n)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3429
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611

References arg(), args, DB, e, test_evm::end, expr(), free(), n, p, rz_list_free(), rz_return_val_if_fail, rz_str_split_list(), rz_str_trim(), sdb_fmt(), sdb_set(), and strdup().

Referenced by rz_core_analysis_cc_init(), and types_cc_print().

◆ rz_analysis_cc_set_error()

RZ_API void rz_analysis_cc_set_error ( RzAnalysis analysis,
const char *  convention,
const char *  error 
)

Definition at line 162 of file cc.c.

162  {
163  if (!rz_analysis_cc_exist(analysis, convention)) {
164  return;
165  }
166  RzStrBuf sb;
167  sdb_set(analysis->sdb_cc, rz_strbuf_initf(&sb, "cc.%s.error", convention), error, 0);
168  rz_strbuf_fini(&sb);
169 }
RZ_API bool rz_analysis_cc_exist(RzAnalysis *analysis, const char *convention)
Definition: cc.c:116

References error(), rz_analysis_cc_exist(), rz_strbuf_fini(), rz_strbuf_initf(), sb, rz_analysis_t::sdb_cc, and sdb_set().

◆ rz_analysis_cc_set_self()

RZ_API void rz_analysis_cc_set_self ( RzAnalysis analysis,
const char *  convention,
const char *  self 
)

Definition at line 145 of file cc.c.

145  {
146  rz_return_if_fail(analysis && convention && self);
147  if (!rz_analysis_cc_exist(analysis, convention)) {
148  return;
149  }
150  RzStrBuf sb;
151  sdb_set(analysis->sdb_cc, rz_strbuf_initf(&sb, "cc.%s.self", convention), self, 0);
152  rz_strbuf_fini(&sb);
153 }

References rz_analysis_cc_exist(), rz_return_if_fail, rz_strbuf_fini(), rz_strbuf_initf(), sb, rz_analysis_t::sdb_cc, and sdb_set().

◆ rz_analysis_set_cc_default()

RZ_API void rz_analysis_set_cc_default ( RzAnalysis analysis,
const char *  cc 
)

Definition at line 205 of file cc.c.

205  {
206  rz_return_if_fail(analysis && cc);
207  sdb_set(DB, "default.cc", cc, 0);
208 }

References DB, rz_return_if_fail, and sdb_set().

Referenced by cb_analysiscc().

◆ rz_analysis_set_syscc_default()

RZ_API void rz_analysis_set_syscc_default ( RzAnalysis analysis,
const char *  cc 
)

Definition at line 215 of file cc.c.

215  {
216  rz_return_if_fail(analysis && cc);
217  sdb_set(DB, "default.syscc", cc, 0);
218 }

References DB, rz_return_if_fail, and sdb_set().

Referenced by cb_analysissyscc().

◆ rz_analysis_syscc_default()

RZ_API const char* rz_analysis_syscc_default ( RzAnalysis analysis)

Definition at line 210 of file cc.c.

210  {
211  rz_return_val_if_fail(analysis, NULL);
212  return sdb_const_get(DB, "default.syscc", 0);
213 }

References DB, NULL, rz_return_val_if_fail, and sdb_const_get().

Referenced by ds_print_esil_analysis(), rz_core_arg_get(), rz_core_arg_set(), rz_core_config_init(), and rz_core_syscall_as_string().