Rizin
unix-like reverse engineering framework and cli tools
c.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
8 #include <rz_lib.h>
9 #include <rz_core.h>
10 #include <rz_lang.h>
11 
12 #if __UNIX__
13 static int ac = 0;
14 static const char **av = NULL;
15 
16 static bool lang_c_set_argv(RzLang *lang, int argc, const char **argv) {
17  ac = argc;
18  av = argv;
19  return true;
20 }
21 
22 static int lang_c_file(RzLang *lang, const char *file) {
23  char *a, *cc, *p, name[512];
24  const char *libpath, *libname;
25  void *lib;
26 
27  if (strlen(file) > (sizeof(name) - 10)) {
28  return false;
29  }
30  if (!strstr(file, ".c")) {
31  sprintf(name, "%s.c", file);
32  } else {
33  strcpy(name, file);
34  }
35  if (!rz_file_exists(name)) {
36  eprintf("file not found (%s)\n", name);
37  return false;
38  }
39 
40  a = (char *)rz_str_lchr(name, '/');
41  if (a) {
42  *a = 0;
43  libpath = name;
44  libname = a + 1;
45  } else {
46  libpath = ".";
47  libname = name;
48  }
49  p = strstr(name, ".c");
50  if (p) {
51  *p = 0;
52  }
53  cc = rz_sys_getenv("CC");
54  if (RZ_STR_ISEMPTY(cc)) {
55  cc = strdup("gcc");
56  }
57  char *libdir = rz_path_libdir();
58  char *pkgconf_path = rz_file_path_join(libdir, "pkgconfig");
59  char *file_esc = rz_str_escape_sh(file);
60  char *libpath_esc = rz_str_escape_sh(libpath);
61  char *libname_esc = rz_str_escape_sh(libname);
62  char *buf = rz_str_newf("%s -fPIC -shared \"%s\" -o \"%s/lib%s." RZ_LIB_EXT "\""
63  " $(PKG_CONFIG_PATH=%s pkg-config --cflags --libs rz_core)",
64  cc, file_esc, libpath_esc, libname_esc, pkgconf_path);
65  free(libname_esc);
66  free(libpath_esc);
67  free(file_esc);
68  free(libdir);
69  free(pkgconf_path);
70  free(cc);
71  if (rz_sys_system(buf) != 0) {
72  free(buf);
73  return false;
74  }
75  free(buf);
76  buf = rz_str_newf("%s/lib%s." RZ_LIB_EXT, libpath, libname);
77  lib = rz_lib_dl_open(buf);
78  if (lib) {
79  void (*fcn)(RzCore *, int argc, const char **argv);
80  fcn = rz_lib_dl_sym(lib, "entry");
81  if (fcn) {
82  fcn(lang->user, ac, av);
83  ac = 0;
84  av = NULL;
85  } else {
86  eprintf("Cannot find 'entry' symbol in library\n");
87  }
88  rz_lib_dl_close(lib);
89  } else {
90  eprintf("Cannot open library\n");
91  }
92  rz_file_rm(buf); // remove lib
93  free(buf);
94  return 0;
95 }
96 
97 static int lang_c_init(void *user) {
98  // TODO: check if C compiler is found in path
99  return true;
100 }
101 
102 static int lang_c_run(RzLang *lang, const char *code, int len) {
103  FILE *fd = rz_sys_fopen(".tmp.c", "w");
104  if (fd) {
105  fputs("#include <rz_core.h>\n\nvoid entry(RzCore *core, int argc, const char **argv) {\n", fd);
106  fputs(code, fd);
107  fputs("\n}\n", fd);
108  fclose(fd);
109  lang_c_file(lang, ".tmp.c");
110  rz_file_rm(".tmp.c");
111  } else
112  eprintf("Cannot open .tmp.c\n");
113  return true;
114 }
115 
116 RzLangPlugin rz_lang_plugin_c = {
117  .name = "c",
118  .ext = "c",
119  .desc = "C language extension",
120  .license = "LGPL",
121  .run = lang_c_run,
122  .init = (void *)lang_c_init,
123  .run_file = (void *)lang_c_file,
124  .set_argv = (void *)lang_c_set_argv,
125 };
126 
127 #ifndef RZ_PLUGIN_INCORE
130  .data = &rz_lang_plugin_c,
131  .version = RZ_VERSION
132 };
133 #endif
134 
135 #else
136 #ifdef _MSC_VER
137 #pragma message("Warning: C RzLangPlugin is not implemented on this platform")
138 #else
139 #warning C RzLangPlugin is not implemented on this platform
140 #endif
141 #endif
size_t len
Definition: 6502dis.c:15
RZ_API RzLibStruct rizin_plugin
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
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")
string FILE
Definition: benchmark.py:21
const char * name
Definition: op.c:541
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API bool rz_file_exists(const char *str)
Definition: file.c:192
RZ_API bool rz_file_rm(const char *file)
Definition: file.c:865
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
RZ_API void * rz_lib_dl_sym(void *handler, const char *name)
Definition: lib.c:90
RZ_API void * rz_lib_dl_open(const char *libname)
Definition: lib.c:54
@ RZ_LIB_TYPE_LANG
Definition: rz_lib.h:71
#define RZ_LIB_EXT
Definition: rz_lib.h:31
RZ_API int rz_lib_dl_close(void *handler)
Definition: lib.c:104
RZ_API RZ_OWN char * rz_path_libdir(void)
Return the directory where the Rizin libraries are placed.
Definition: path.c:155
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API char * rz_str_escape_sh(const char *buf)
Definition: str.c:1560
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API FILE * rz_sys_fopen(const char *path, const char *mode)
Definition: sys.c:1815
RZ_API int rz_sys_system(const char *command)
Definition: sys.c:1658
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
Definition: inftree9.h:24
Definition: gzappend.c:170
Definition: z80asm.h:102
const char * name
Definition: rz_lang.h:28
void * user
Definition: rz_lang.h:19
static const z80_opcode fd[]
Definition: z80_tab.h:997