diff --git a/config-user.mk.acr b/config-user.mk.acr index 8ac19641ab..e69ffd56ab 100644 --- a/config-user.mk.acr +++ b/config-user.mk.acr @@ -62,9 +62,12 @@ DL_LIBS= else ifeq ($(OSTYPE),windows) else +ifeq ($(OSTYPE),darwin) +else DL_LIBS=@DL_LIBS@ endif endif +endif else # WANT_DYLINK=0 diff --git a/libr/asm/meson.build b/libr/asm/meson.build index 8dd60b2f3d..ba4a0b73d0 100644 --- a/libr/asm/meson.build +++ b/libr/asm/meson.build @@ -47,7 +47,6 @@ r_asm = library('r_asm', r_asm_sources, dependencies: [ r_util_dep, r_syscall_dep, - r_lang_dep, r_arch_dep, r_reg_dep, r_flag_dep, @@ -73,7 +72,6 @@ r_asm_static = static_library('r_asm_static', r_asm_sources, r_reg_static_dep, r_arch_static_dep, r_syscall_static_dep, - r_lang_static_dep, r_flag_static_dep, r_socket_static_dep, capstone_dep, @@ -97,7 +95,6 @@ pkgconfig_mod.generate(r_asm, requires: [ 'r_util', 'r_syscall', - 'r_lang', 'r_reg', 'r_flag', 'r_arch', diff --git a/libr/debug/debug.c b/libr/debug/debug.c index 5e700b8b64..32b9467ef2 100644 --- a/libr/debug/debug.c +++ b/libr/debug/debug.c @@ -1520,7 +1520,7 @@ static int show_syscall(RDebug *dbg, const char *sysreg) { sysname = "unknown"; args = 3; } - RStrBuf *sb = r_str_newf ("--> %s 0x%08"PFMT64x" syscall %d %s (", sysreg, + RStrBuf *sb = r_strbuf_newf ("--> %s 0x%08"PFMT64x" syscall %d %s (", sysreg, r_debug_reg_get (dbg, "PC"), reg, sysname); for (i = 0; i < args; i++) { snprintf (regname, sizeof (regname) - 1, "A%d", i); diff --git a/libr/lang/Makefile b/libr/lang/Makefile index f670e300b0..8c721bb1ea 100644 --- a/libr/lang/Makefile +++ b/libr/lang/Makefile @@ -4,7 +4,7 @@ NAME=r_lang OBJS=lang.o R2DEPS=r_util r_cons r_asm CFLAGS+=-DR2_PLUGIN_INCORE -CFLAGS+=-DCONFIG_VERSION=\"0.0.0\" +# CFLAGS+=-DCONFIG_VERSION=\"0.0.0\" OBJS+=$(QJS_OBJS) # LINK+=$(QJS_FILES) diff --git a/libr/lang/lang.c b/libr/lang/lang.c index 35565143ac..82cd5194fa 100644 --- a/libr/lang/lang.c +++ b/libr/lang/lang.c @@ -9,6 +9,7 @@ R_LIB_VERSION (r_lang); #if HAVE_SYSTEM #include "p/pipe.c" #include "p/c.c" +#include "p/s.c" #include "p/v.c" #include "p/vala.c" #include "p/rust.c" @@ -44,6 +45,7 @@ R_API RLang *r_lang_new(void) { lang->cb_printf = (PrintfCallback)printf; #if HAVE_SYSTEM #if R2__UNIX__ + r_lang_add (lang, &r_lang_plugin_s); r_lang_add (lang, &r_lang_plugin_c); r_lang_add (lang, &r_lang_plugin_cpipe); #endif diff --git a/libr/lang/meson.build b/libr/lang/meson.build index b7e3f7c13e..c5e7ffca9c 100644 --- a/libr/lang/meson.build +++ b/libr/lang/meson.build @@ -13,7 +13,7 @@ r_lang_sources += '../../shlr/qjs/src/libunicode.c' r_lang = library('r_lang', r_lang_sources, include_directories: [platform_inc, spp_inc, qjs_inc], c_args: library_cflags, - dependencies: [r_util_dep, r_cons_dep], + dependencies: [r_util_dep, r_asm_dep, r_cons_dep], install: true, implicit_include_directories: false, install_rpath: rpath_lib, @@ -26,7 +26,7 @@ if get_option('blob') r_lang_static = static_library('r_lang_static', r_lang_sources, include_directories: [platform_inc, spp_inc, qjs_inc], c_args: library_cflags, - dependencies: [r_util_static_dep, r_cons_static_dep], + dependencies: [r_util_static_dep, r_asm_static_dep, r_cons_static_dep], install: true, implicit_include_directories: false, ) @@ -43,6 +43,7 @@ pkgconfig_mod.generate(r_lang, libraries: pkgcfg_sanitize_libs, requires: [ 'r_util', + 'r_asm', 'r_cons' ], description: 'radare foundation libraries' diff --git a/libr/lang/p/asm.c b/libr/lang/p/asm.c index 2c48d7c850..fbeeacd417 100644 --- a/libr/lang/p/asm.c +++ b/libr/lang/p/asm.c @@ -12,7 +12,7 @@ static bool lang_asm_run(RLangSession *s, const char *code, int len) { if (kode) { int i; eprintf ("CODE: %d\nBYTES: ", kode->len); - for (i = 0; i< kode->len; i++) { + for (i = 0; i < kode->len; i++) { eprintf ("%02x ", kode->bytes[i]); } eprintf ("\n"); @@ -49,9 +49,8 @@ static RLangPlugin r_lang_plugin_asm = { #endif }; #else -#ifdef _MSC_VER + #pragma message("Warning: C RLangPlugin is not implemented on this platform") -#else -#warning C RLangPlugin is not implemented on this platform -#endif +static RLangPlugin r_lang_plugin_asm = {NULL}; + #endif diff --git a/libr/lang/p/qjs.c b/libr/lang/p/qjs.c index 9155faa41d..4292265fc1 100644 --- a/libr/lang/p/qjs.c +++ b/libr/lang/p/qjs.c @@ -607,7 +607,7 @@ static bool fini(RLangSession *s) { k->r = NULL; qjsctx_free (); // free (k); - return NULL; + return true; } static RLangPlugin r_lang_plugin_qjs = { diff --git a/libr/lang/p/s.c b/libr/lang/p/s.c new file mode 100644 index 0000000000..0a6cf0c25d --- /dev/null +++ b/libr/lang/p/s.c @@ -0,0 +1,167 @@ +/* radare - LGPL - Copyright 2023 pancake */ + +#include "r_lib.h" +#include "r_core.h" +#include "r_lang.h" + +#if R2__UNIX__ && !__wasi__ + +static int lang_s_file(RLangSession *s, const char *file) { + char *a, *cc, *p; + const char *libpath, *libname; + void *lib; + + char *name = NULL; + if (r_str_endswith (file, ".s")) { + name = strdup (file); + } else { + name = r_str_newf ("%s.s", file); + } + if (!r_file_exists (name)) { + R_LOG_ERROR ("file not found (%s)", name); + free (name); + return false; + } + + a = (char*)r_str_lchr (name, '/'); + if (a) { + *a = 0; + libpath = name; + libname = a + 1; + } else { + libpath = "."; + libname = name; + } + // XXX check if ends with not just strstr + p = strstr (name, ".s"); + if (p) { + *p = 0; + } + cc = r_sys_getenv ("CC"); + if (R_STR_ISEMPTY (cc)) { + cc = strdup ("gcc"); + } + char *file_esc = r_str_escape_sh (file); + char *libpath_esc = r_str_escape_sh (libpath); + char *libname_esc = r_str_escape_sh (libname); + char *buf = r_str_newf ("%s -fPIC -shared \"%s\" -o \"%s/lib%s." R_LIB_EXT "\"" + " $(PKG_CONFIG_PATH=%s pkg-config --cflags --libs r_core)", + cc, file_esc, libpath_esc, libname_esc, R2_LIBDIR "/pkgconfig"); + free (libname_esc); + free (libpath_esc); + free (file_esc); + free (cc); + if (r_sandbox_system (buf, 1) != 0) { + free (buf); + free (name); + return false; + } + free (buf); + buf = r_str_newf ("%s/lib%s."R_LIB_EXT, libpath, libname); + lib = r_lib_dl_open (buf); + if (lib) { + void (*fcn)(RCore *, int argc, const char **argv); + fcn = r_lib_dl_sym (lib, "entry"); + if (!fcn) { + fcn = r_lib_dl_sym (lib, "main"); + if (!fcn) { + fcn = r_lib_dl_sym (lib, "_main"); + } + } + if (fcn) { + fcn (s->lang->user, ac, av); + ac = 0; + av = NULL; + } else { + R_LOG_ERROR ("Cannot find 'entry' symbol in library"); + } + r_lib_dl_close (lib); + } else { + R_LOG_ERROR ("Cannot open library"); + } + r_file_rm (buf); // remove lib + free (buf); + free (name); + return 0; +} + +static bool lang_s_run(RLangSession *s, const char *code, int len) { + if (!r_file_dump (".tmp.s", (const ut8*)code, len, false)) { + R_LOG_ERROR ("Cannot open .tmp.s"); + return false; + } + lang_s_file (s, ".tmp.s"); + r_file_rm (".tmp.s"); + return true; +} + +#define r_lang_s_example "" \ + ".extern _puts\n" \ + ".global _main\n" \ + ".extern _r_core_new\n" \ + ".extern _r_cons_flush\n" \ + ".extern _r_core_cmd_str\n" \ + ".p2align 2\n" \ + "_main:\n" \ + " // locals\n" \ + " // [sp, 0] ptr(LR)\n" \ + " // [sp, 8] RCore\n" \ + "\n" \ + " // prelude\n" \ + " sub sp, sp, 16 \n" \ + " str lr, [sp, 0] // lr \n" \ + "\n" \ + " // body\n" \ + " bl getbase\n" \ + " // mov x0, =text\n" \ + " bl _puts\n" \ + "\n" \ + " bl _r_core_new\n" \ + " str x0, [sp, 8] // rcore is stored in sp+8\n" \ + " bl getbase\n" \ + " mov x1, x0\n" \ + " ldr x0, [sp, 8]\n" \ + " bl _r_core_cmd_str\n" \ + " bl _puts\n" \ + " // bl _r_cons_flush\n" \ + "\n" \ + " // postlude\n" \ + " ldr lr, [sp]\n" \ + " add sp, sp, 16\n" \ + " ret\n" \ + "\n" \ + "// .equ delta, (getbase - _main)\n" \ + ".zerofill __DATA,__common,_core,4,2\n" \ + "\n" \ + ".equ bdelta, 4 * 3 // (baseaddr-_me)\n" \ + "getbase:\n" \ + " mov x12, lr\n" \ + " bl _me\n" \ + "_me:\n" \ + " add x0, lr, bdelta // 4 * 3 ;; 4*3 = text-_me\n" \ + " mov lr, x12\n" \ + " ret\n" \ + "baseaddr:\n" \ + "text:\n" \ + " .string \"?e winrar\x00\"\n" \ + "core:\n" \ + " .byte 0,0,0,0 ,0,0,0,0 ,0,0,0,0 ,0,0,0,0\n" \ + "" + +static RLangPlugin r_lang_plugin_s = { + .name = "s", + .ext = "s", + .desc = "GNU Assembler Source", + .author = "pancake", + .license = "LGPL", + .example = r_lang_s_example, + .run = lang_s_run, + .run_file = (void*)lang_s_file, +}; +#else +#ifdef _MSC_VER +#pragma message("Warning: C RLangPlugin is not implemented on this platform") +#else +#warning C RLangPlugin is not implemented on this platform +#endif +#endif diff --git a/libr/libs.mk b/libr/libs.mk index 9f36e5cea6..584c0d9b38 100644 --- a/libr/libs.mk +++ b/libr/libs.mk @@ -2,15 +2,16 @@ ifeq ($(LIBS0),) LIBS0=util -LIBS1=socket reg cons magic bp config crypto -LIBS2=syscall search flag arch esil io +LIBS1=socket reg cons magic bp config crypto syscall +LIBS2=search flag arch esil io LIBS3=asm fs anal LIBS4=lang bin LIBS5=debug egg LIBS6=core LIBS7=main +LIBS8= -LIBS=$(LIBS0) $(LIBS1) $(LIBS2) $(LIBS3) $(LIBS4) $(LIBS5) $(LIBS6) $(LIBS7) +LIBS=$(LIBS0) $(LIBS1) $(LIBS2) $(LIBS3) $(LIBS4) $(LIBS5) $(LIBS6) $(LIBS7) $(LIBS8) endif .PHONY: $(LIBS) diff --git a/libr/main/Makefile b/libr/main/Makefile index 553905ce8d..2cad3afbe8 100644 --- a/libr/main/Makefile +++ b/libr/main/Makefile @@ -2,6 +2,8 @@ include ../config.mk NAME=r_main +R2DEPS=r_core + OBJS+=main.o OBJS+=ravc2.o OBJS+=rax2.o diff --git a/libr/meson.build b/libr/meson.build index bf514c2934..812e32f0b8 100644 --- a/libr/meson.build +++ b/libr/meson.build @@ -387,6 +387,8 @@ lang_plugins += [ 'rust', 'go', 'zig', + 's', + 'asm', 'c', 'vala' ] diff --git a/libr/util/deps.mk b/libr/util/deps.mk index 68b9d3c581..7186c4ecbc 100644 --- a/libr/util/deps.mk +++ b/libr/util/deps.mk @@ -1,7 +1,9 @@ include $(SHLR)/sdb.mk include $(SHLR)/zip/deps.mk +ifneq (${BUILD_OS},darwin) LDFLAGS+=-lm +endif # NetBSD 7.0 ships with backtrace(3) in -lexecinfo ifeq (${BUILD_OS},netbsd) diff --git a/meson.build b/meson.build index 8e59b8014f..a651820209 100644 --- a/meson.build +++ b/meson.build @@ -608,8 +608,8 @@ subdir('libr/arch') subdir('libr/fs') subdir('libr/bin') subdir('libr/config') -subdir('libr/lang') subdir('libr/asm') +subdir('libr/lang') subdir('libr/esil') subdir('libr/anal') subdir('libr/egg') diff --git a/mk/tcc.mk b/mk/tcc.mk index 686ee5d22e..55f9831622 100644 --- a/mk/tcc.mk +++ b/mk/tcc.mk @@ -22,7 +22,7 @@ PARTIALLD=${LD} -r --whole-archive LDFLAGS_LIB=-shared LDFLAGS_LIB+=-Dxx #Wl,-soname,lib${NAME}.${EXT_SO}.${VERSION} -LDFLAGS_SONAME=-soname +LDFLAGS_SONAME=-soname= #Wl,-soname= endif