Initial implementation of lang.s assembly scripting ##lang

This commit is contained in:
pancake 2023-01-10 19:17:18 +01:00 committed by pancake
parent 1546936a7c
commit c3a1fe170d
15 changed files with 194 additions and 18 deletions

View File

@ -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

View File

@ -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',

View File

@ -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);

View File

@ -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)

View File

@ -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

View File

@ -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'

View File

@ -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

View File

@ -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 = {

167
libr/lang/p/s.c Normal file
View File

@ -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

View File

@ -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)

View File

@ -2,6 +2,8 @@ include ../config.mk
NAME=r_main
R2DEPS=r_core
OBJS+=main.o
OBJS+=ravc2.o
OBJS+=rax2.o

View File

@ -387,6 +387,8 @@ lang_plugins += [
'rust',
'go',
'zig',
's',
'asm',
'c',
'vala'
]

View File

@ -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)

View File

@ -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')

View File

@ -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