Rizin
unix-like reverse engineering framework and cli tools
platform_profile.c File Reference
#include <rz_platform.h>
#include <string.h>

Go to the source code of this file.

Functions

RZ_API void rz_platform_profile_free (RzPlatformProfile *p)
 Frees an RzPlatformProfile type. More...
 
static void free_mmio_kv (HtUPKv *kv)
 
RZ_API RZ_OWN RzPlatformProfilerz_platform_profile_new ()
 Creates a new RzPlatformProfile type. More...
 
RZ_API RZ_OWN RzPlatformTargetrz_platform_target_new ()
 Creates a new RzPlatformTarget type. More...
 
RZ_API void rz_platform_target_free (RzPlatformTarget *t)
 Frees an RzPlatformTarget type. More...
 
RZ_API RZ_BORROW const char * rz_platform_profile_resolve_mmio (RZ_NONNULL RzPlatformProfile *profile, ut64 address)
 Resolves an address and returns the linked mmio. More...
 
RZ_API RZ_BORROW const char * rz_platform_profile_resolve_extended_register (RZ_NONNULL RzPlatformProfile *profile, ut64 address)
 Resolves an address and returns the linked extended register. More...
 
static bool cpu_reload_needed (RzPlatformTarget *c, const char *cpu, const char *arch)
 
static bool sdb_load_arch_profile (RzPlatformTarget *t, Sdb *sdb)
 
static bool sdb_load_arch_profile_by_path (RZ_NONNULL RzPlatformTarget *t, const char *path)
 
RZ_API bool rz_platform_load_profile_sdb (RzPlatformTarget *t, const char *path)
 Loads the contents of the CPU Profile to the RzPlatformProfile. More...
 
static bool is_cpu_valid (const char *cpu_dir, const char *cpu)
 
RZ_API bool rz_platform_profiles_init (RzPlatformTarget *t, const char *cpu, const char *arch, const char *cpus_dir)
 Initializes RzPlatformProfile by loading the path to the SDB file of the CPU profile. More...
 

Function Documentation

◆ cpu_reload_needed()

static bool cpu_reload_needed ( RzPlatformTarget c,
const char *  cpu,
const char *  arch 
)
inlinestatic

Definition at line 97 of file platform_profile.c.

97  {
98  if (!c->arch || strcmp(c->arch, arch)) {
99  return true;
100  }
101  return !c->cpu || strcmp(c->cpu, cpu);
102 }
static ut32 cpu[32]
Definition: analysis_or1k.c:21
cs_arch arch
Definition: cstool.c:13
#define c(i)
Definition: sha256.c:43

References arch, c, and cpu.

Referenced by rz_platform_profiles_init().

◆ free_mmio_kv()

static void free_mmio_kv ( HtUPKv *  kv)
static

Definition at line 22 of file platform_profile.c.

22  {
23  free(kv->value);
24 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

Referenced by rz_platform_profile_new().

◆ is_cpu_valid()

static bool is_cpu_valid ( const char *  cpu_dir,
const char *  cpu 
)
static

Definition at line 175 of file platform_profile.c.

175  {
176  RzList *files = rz_sys_dir(cpu_dir);
177  if (!files) {
178  return false;
179  }
180  RzListIter *it;
181  char *filename = NULL;
182  char *arch_cpu = NULL;
183 
184  rz_list_foreach (files, it, filename) {
185  char *cpu_name = NULL;
186  if (!strcmp(filename, "..") || !strcmp(filename, "..")) {
187  continue;
188  }
189  arch_cpu = rz_str_ndup(filename, strlen(filename) - 4);
190  if (!arch_cpu) {
191  continue;
192  }
193  cpu_name = strchr(arch_cpu, '-');
194  if (!cpu_name) {
195  free(arch_cpu);
196  continue;
197  }
198  cpu_name[0] = '\0';
199  if (!strcmp(cpu_name + 1, cpu)) {
201  free(arch_cpu);
202  return true;
203  }
204 
205  free(arch_cpu);
206  }
207 
209  return false;
210 }
#define NULL
Definition: cris-opc.c:27
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n output MIME type special files
Definition: file_opts.h:46
const char * filename
Definition: ioapi.h:137
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
RZ_API char * rz_str_ndup(RZ_NULLABLE const char *ptr, int len)
Create new copy of string ptr limited to size len.
Definition: str.c:1006
RZ_API RzList * rz_sys_dir(const char *path)
Definition: sys.c:216

References cpu, files, free(), NULL, rz_list_free(), rz_str_ndup(), and rz_sys_dir().

Referenced by rz_platform_profiles_init().

◆ rz_platform_load_profile_sdb()

RZ_API bool rz_platform_load_profile_sdb ( RzPlatformTarget t,
const char *  path 
)

Loads the contents of the CPU Profile to the RzPlatformProfile.

Parameters
treference to RzPlatformTarget
pathreference to path of the SDB file

Definition at line 168 of file platform_profile.c.

168  {
169  if (!rz_file_exists(path)) {
170  return false;
171  }
173 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
static bool sdb_load_arch_profile_by_path(RZ_NONNULL RzPlatformTarget *t, const char *path)
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192

References path, rz_file_exists(), and sdb_load_arch_profile_by_path().

Referenced by rz_platform_profiles_init().

◆ rz_platform_profile_free()

RZ_API void rz_platform_profile_free ( RzPlatformProfile p)

Frees an RzPlatformProfile type.

Frees the hashtables used for MMIO and extended
registers

Definition at line 13 of file platform_profile.c.

13  {
14  if (!p) {
15  return;
16  }
17  ht_up_free(p->registers_mmio);
18  ht_up_free(p->registers_extended);
19  free(p);
20 }
void * p
Definition: libc.cpp:67

References free(), and p.

Referenced by rz_platform_target_free(), and sdb_load_arch_profile().

◆ rz_platform_profile_new()

RZ_API RZ_OWN RzPlatformProfile* rz_platform_profile_new ( )

Creates a new RzPlatformProfile type.

Definition at line 29 of file platform_profile.c.

29  {
31  if (!profile) {
32  return NULL;
33  }
34  profile->registers_mmio = ht_up_new((HtUPDupValue)strdup, free_mmio_kv, (HtUPCalcSizeV)strlen);
35  if (!profile->registers_mmio) {
36  free(profile);
37  return NULL;
38  }
39  profile->registers_extended = ht_up_new((HtUPDupValue)strdup, free_mmio_kv, (HtUPCalcSizeV)strlen);
40  if (!profile->registers_extended) {
41  ht_up_free(profile->registers_mmio);
42  free(profile);
43  return NULL;
44  }
45  return profile;
46 }
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")
static void free_mmio_kv(HtUPKv *kv)
#define RZ_NEW0(x)
Definition: rz_types.h:284

References free(), free_mmio_kv(), NULL, rz_platform_profile_t::registers_extended, rz_platform_profile_t::registers_mmio, RZ_NEW0, and strdup().

Referenced by rz_platform_target_new(), and sdb_load_arch_profile().

◆ rz_platform_profile_resolve_extended_register()

RZ_API RZ_BORROW const char* rz_platform_profile_resolve_extended_register ( RZ_NONNULL RzPlatformProfile profile,
ut64  address 
)

Resolves an address and returns the linked extended register.

Definition at line 91 of file platform_profile.c.

91  {
92  rz_return_val_if_fail(profile, NULL);
93 
94  return ht_up_find(profile->registers_extended, (ut64)address, NULL);
95 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References NULL, rz_return_val_if_fail, and ut64().

Referenced by ds_build_op_str().

◆ rz_platform_profile_resolve_mmio()

RZ_API RZ_BORROW const char* rz_platform_profile_resolve_mmio ( RZ_NONNULL RzPlatformProfile profile,
ut64  address 
)

Resolves an address and returns the linked mmio.

Definition at line 82 of file platform_profile.c.

82  {
83  rz_return_val_if_fail(profile, NULL);
84 
85  return ht_up_find(profile->registers_mmio, (ut64)address, NULL);
86 }

References NULL, rz_return_val_if_fail, and ut64().

Referenced by ds_build_op_str(), and resolve_mmio().

◆ rz_platform_profiles_init()

RZ_API bool rz_platform_profiles_init ( RzPlatformTarget t,
const char *  cpu,
const char *  arch,
const char *  cpus_dir 
)

Initializes RzPlatformProfile by loading the path to the SDB file of the CPU profile.

Parameters
treference to RzPlatformTarget
cpureference to the selected CPU (value of asm.cpu)
archreference to the selected architecture (value of asm.arch)
cpus_dirreference to the directory containing cpu files

Definition at line 221 of file platform_profile.c.

221  {
222  if (!cpu_reload_needed(t, cpu, arch)) {
223  return false;
224  }
225  if (!cpus_dir || !arch || !cpu) {
226  return false;
227  }
228  char buf[50];
229  char *path = rz_file_path_join(cpus_dir, rz_strf(buf, "%s-%s.sdb", arch, cpu));
230  if (!path) {
231  return false;
232  }
233  if (!is_cpu_valid(cpus_dir, cpu)) {
234  if (!strcmp(arch, "avr")) {
235  free(path);
236  path = rz_file_path_join(cpus_dir, "avr-ATmega8.sdb");
237  }
238  }
239  free(t->cpu);
240  free(t->arch);
241  t->cpu = strdup(cpu);
242  t->arch = strdup(arch);
244  free(path);
245  return true;
246 }
voidpf void * buf
Definition: ioapi.h:138
static bool is_cpu_valid(const char *cpu_dir, const char *cpu)
static bool cpu_reload_needed(RzPlatformTarget *c, const char *cpu, const char *arch)
RZ_API bool rz_platform_load_profile_sdb(RzPlatformTarget *t, const char *path)
Loads the contents of the CPU Profile to the RzPlatformProfile.
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
#define rz_strf(buf,...)
Convenience macro for local temporary strings.
Definition: rz_str.h:59

References rz_platform_target_t::arch, arch, cpu, rz_platform_target_t::cpu, cpu_reload_needed(), free(), is_cpu_valid(), path, rz_file_path_join(), rz_platform_load_profile_sdb(), rz_strf, and strdup().

Referenced by cb_asmarch(), and cb_asmcpu().

◆ rz_platform_target_free()

RZ_API void rz_platform_target_free ( RzPlatformTarget t)

Frees an RzPlatformTarget type.

Frees the pointer to the SDB and the RzPlatformProfile

Definition at line 69 of file platform_profile.c.

69  {
70  if (!t) {
71  return;
72  }
74  free(t->cpu);
75  free(t->arch);
76  free(t);
77 }
RZ_API void rz_platform_profile_free(RzPlatformProfile *p)
Frees an RzPlatformProfile type.
RzPlatformProfile * profile
Definition: rz_platform.h:34

References rz_platform_target_t::arch, rz_platform_target_t::cpu, free(), rz_platform_target_t::profile, and rz_platform_profile_free().

Referenced by rz_analysis_free().

◆ rz_platform_target_new()

RZ_API RZ_OWN RzPlatformTarget* rz_platform_target_new ( )

Creates a new RzPlatformTarget type.

Definition at line 51 of file platform_profile.c.

51  {
53  if (!profile) {
54  return NULL;
55  }
56  profile->profile = rz_platform_profile_new();
57  if (!profile->profile) {
58  free(profile);
59  return NULL;
60  }
61  return profile;
62 }
RZ_API RZ_OWN RzPlatformProfile * rz_platform_profile_new()
Creates a new RzPlatformProfile type.

References free(), NULL, rz_platform_target_t::profile, RZ_NEW0, and rz_platform_profile_new().

Referenced by rz_analysis_new().

◆ sdb_load_arch_profile()

static bool sdb_load_arch_profile ( RzPlatformTarget t,
Sdb sdb 
)
static

Definition at line 104 of file platform_profile.c.

104  {
105  rz_return_val_if_fail(t && sdb, false);
106  SdbKv *kv;
107  SdbListIter *iter;
109  if (!c) {
110  return false;
111  }
112  SdbList *l = sdb_foreach_list(sdb, false);
113  ls_foreach (l, iter, kv) {
114  if (!strcmp(sdbkv_key(kv), "PC")) {
115  c->pc = rz_num_math(NULL, sdbkv_value(kv));
116  } else if (!strcmp(sdbkv_key(kv), "EEPROM_SIZE")) {
117  c->eeprom_size = rz_num_math(NULL, sdbkv_value(kv));
118  } else if (!strcmp(sdbkv_key(kv), "IO_SIZE")) {
119  c->io_size = rz_num_math(NULL, sdbkv_value(kv));
120  } else if (!strcmp(sdbkv_key(kv), "SRAM_START")) {
121  c->sram_start = rz_num_math(NULL, sdbkv_value(kv));
122  } else if (!strcmp(sdbkv_key(kv), "SRAM_SIZE")) {
123  c->sram_size = rz_num_math(NULL, sdbkv_value(kv));
124  } else if (!strcmp(sdbkv_key(kv), "PAGE_SIZE")) {
125  c->page_size = rz_num_math(NULL, sdbkv_value(kv));
126  } else if (!strcmp(sdbkv_key(kv), "ROM_SIZE")) {
127  c->rom_size = rz_num_math(NULL, sdbkv_value(kv));
128  } else if (!strcmp(sdbkv_key(kv), "ROM_ADDRESS")) {
129  c->rom_address = rz_num_math(NULL, sdbkv_value(kv));
130  } else if (!strcmp(sdbkv_key(kv), "RAM_SIZE")) {
131  c->ram_size = rz_num_math(NULL, sdbkv_value(kv));
132  }
133  if (!strcmp(sdbkv_value(kv), "io")) {
134  char *io_name = sdbkv_key(kv);
135  char *argument_key = rz_str_newf("%s.address", io_name);
136  ut64 io_address = sdb_num_get(sdb, argument_key, NULL);
137  free(argument_key);
138  ht_up_insert(c->registers_mmio, io_address, io_name);
139  }
140  if (!strcmp(sdbkv_value(kv), "ext_io")) {
141  char *ext_io_name = sdbkv_key(kv);
142  char *argument_key = rz_str_newf("%s.address", ext_io_name);
143  ut64 ext_io_address = sdb_num_get(sdb, argument_key, NULL);
144  free(argument_key);
145  ht_up_insert(c->registers_extended, ext_io_address, ext_io_name);
146  }
147  }
148  ls_free(l);
150  t->profile = c;
151  return true;
152 }
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
#define ls_foreach(list, it, pos)
Definition: ls.h:31
RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas)
Definition: num.c:13
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
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 c, free(), ls_foreach, ls_free(), NULL, rz_platform_target_t::profile, rz_num_math(), rz_platform_profile_free(), rz_platform_profile_new(), rz_return_val_if_fail, rz_str_newf(), sdb_foreach_list(), sdb_num_get(), sdbkv_key(), sdbkv_value(), and ut64().

Referenced by sdb_load_arch_profile_by_path().

◆ sdb_load_arch_profile_by_path()

static bool sdb_load_arch_profile_by_path ( RZ_NONNULL RzPlatformTarget t,
const char *  path 
)
static

Definition at line 154 of file platform_profile.c.

154  {
155  Sdb *db = sdb_new(0, path, 0);
156  bool result = sdb_load_arch_profile(t, db);
157  sdb_close(db);
158  sdb_free(db);
159  return result;
160 }
static bool sdb_load_arch_profile(RzPlatformTarget *t, Sdb *sdb)
RZ_API Sdb * sdb_new(const char *path, const char *name, int lock)
Definition: sdb.c:47
RZ_API void sdb_close(Sdb *s)
Definition: sdb.c:416
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206
Definition: sdb.h:63

References path, sdb_close(), sdb_free(), sdb_load_arch_profile(), and sdb_new().

Referenced by rz_platform_load_profile_sdb().