Fix #3136 - Add ARM and ARM64 assemmbler support from NDK

This commit is contained in:
pancake 2015-08-24 15:43:13 +02:00
parent d7598b3be0
commit 59dfda515f
8 changed files with 156 additions and 21 deletions

12
libr/asm/p/arm_as.mk Normal file
View File

@ -0,0 +1,12 @@
OBJ_ARM_AS=asm_arm_as.o
STATIC_OBJ+=${OBJ_ARM_AS}
TARGET_ARM_AS=asm_arm_as.${EXT_SO}
ifeq ($(WITHPIC),1)
ALL_TARGETS+=${TARGET_ARM_AS}
${TARGET_ARM_AS}: ${OBJ_ARM_AS}
${CC} $(call libname,asm_arm_nasm) ${LDFLAGS} ${CFLAGS} \
-o ${TARGET_ARM_AS} ${OBJ_ARM_AS}
endif

114
libr/asm/p/asm_arm_as.c Normal file
View File

@ -0,0 +1,114 @@
/* radare - LGPL - Copyright 2015 pancake */
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_asm.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
// USE ARM_AS environment variable
#define ARM32_AS "arm-linux-androideabi-as"
#define ARM64_AS "aarch64-linux-android-as"
// toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-arm_64/bin/
// toolchains/aarch64-linux-android-4.9/prebuilt/darwin-arm_64/bin/
static int assemble(RAsm *a, RAsmOp *op, const char *buf) {
const char *bitconfig = "";
char *ipath, *opath;
int ifd, ofd;
char *as = NULL;
char asm_buf[R_ASM_BUFSIZE];
int len = 0;
ifd = r_file_mkstemp ("r_as", &ipath);
if (ifd == -1)
return -1;
ofd = r_file_mkstemp ("r_as", &opath);
if (ofd == -1) {
free (ipath);
return -1;
}
as = r_sys_getenv ("ARM_AS");
if (!as || !*as) {
free (as);
if (a->bits == 64) {
as = strdup (ARM64_AS);
} else {
as = strdup (ARM32_AS);
}
}
if (a->bits == 16) {
bitconfig = ".thumb";
}
len = snprintf (asm_buf, sizeof (asm_buf),
"%s\n" //.org 0x%"PFMT64x"\n"
".ascii \"BEGINMARK\"\n"
"%s\n"
".ascii \"ENDMARK\"\n",
bitconfig, buf); // a->pc ??
write (ifd, asm_buf, len);
close (ifd);
if (!r_sys_cmdf ("%s %s -o %s", as, ipath, opath)) {
const ut8 *begin, *end;
close (ofd);
ofd = open (opath, O_BINARY|O_RDONLY);
if (ofd < 0) {
free (ipath);
free (opath);
return -1;
}
len = read (ofd, op->buf, R_ASM_BUFSIZE);
begin = r_mem_mem (op->buf, len, (const ut8*)"BEGINMARK", 9);
end = r_mem_mem (op->buf, len, (const ut8*)"ENDMARK", 7);
if (!begin || !end) {
eprintf ("Cannot find water marks\n");
len = 0;
} else {
len = (int)(size_t)(end-begin-9);
if (len>0) memcpy (op->buf, begin+9, len);
else len = 0;
}
} else {
eprintf ("Error running: %s %s -o %s", as, ipath, opath);
eprintf ("export PATH=~/NDK/toolchains/arm-linux*/prebuilt/darwin-arm_64/bin\n");
len = 0;
}
free (as);
close (ofd);
unlink (ipath);
unlink (opath);
free (ipath);
free (opath);
op->size = len;
return len;
}
RAsmPlugin r_asm_plugin_arm_as = {
.name = "arm.as",
.desc = "as ARM Assembler (use ARM_AS environment)",
.arch = "arm",
.license = "LGPL3",
.bits = 16|32|64,
.init = NULL,
.fini = NULL,
.disassemble = NULL,
.assemble = &assemble,
};
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_arm_as,
.version = R2_VERSION
};
#endif

View File

@ -1,4 +1,6 @@
/* radare - LGPL - Copyright 2010-2013 - pancake */
/* radare - LGPL - Copyright 2010-2015 - pancake */
// XXX: deprecate
#include <r_types.h>
#include <r_util.h>
@ -24,10 +26,10 @@ static int assemble(RAsm *a, RAsmOp *op, const char *buf) {
}
RAsmPlugin r_asm_plugin_x86_gas = {
.name = "gas",
.name = "x86.gas",
.license = "LGPL3",
.desc = "GNU Assembler (gas)",
.arch = "x86", // XXX
.arch = NULL, //"x86", // XXX
.bits = 16|32|64,
.init = NULL,
.fini = NULL,

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2011-2014 pancake */
/* radare - LGPL - Copyright 2011-2015 pancake */
#include <r_types.h>
#include <r_util.h>

View File

@ -3,9 +3,7 @@ OBJ_X86=asm_gas.o
STATIC_OBJ+=${OBJ_X86}
TARGET_X86=asm_gas.${EXT_SO}
ifeq ($(WITHPIC),1)
ALL_TARGETS+=${TARGET_X86}
${TARGET_X86}: ${OBJ_X86}
${CC} $(call libname,asm_gas) ${LDFLAGS} ${CFLAGS} -o ${TARGET_X86} ${OBJ_X86}
endif

View File

@ -85,21 +85,27 @@ static int __esil_detach(int pid) {
static char *__esil_reg_profile(RDebug *dbg) {
eprintf ("TODO: esil %s\n", r_sys_arch_str (dbg->arch));
return strdup (
"=pc pc\n"
"=sp esp\n"
"=bp ptr\n"
"gpr rax .32 0 0\n"
"gpr pc .32 0 0\n"
"gpr ptr .32 4 0\n"
"gpr esp .32 8 0\n"
"gpr scr .32 12 0\n"
"gpr scri .32 16 0\n"
"gpr inp .32 20 0\n"
"gpr inpi .32 24 0\n"
"gpr mem .32 28 0\n"
"gpr memi .32 32 0\n"
);
if (!strcmp (dbg->arch, "bf")) {
return strdup (
"=pc pc\n"
"=sp esp\n"
"=bp ptr\n"
"gpr rax .32 0 0\n"
"gpr pc .32 0 0\n"
"gpr ptr .32 4 0\n"
"gpr esp .32 8 0\n"
"gpr scr .32 12 0\n"
"gpr scri .32 16 0\n"
"gpr inp .32 20 0\n"
"gpr inpi .32 24 0\n"
"gpr mem .32 28 0\n"
"gpr memi .32 32 0\n"
);
} else if (!strcmp (dbg->arch, "x86")) {
eprintf ("[DEBUGESIL] Missing regprofile for x86\n");
} else {
return NULL;
}
}
static int __esil_breakpoint (RBreakpointItem *bp, int set, void *user) {

View File

@ -175,6 +175,7 @@ extern RAsmPlugin r_asm_plugin_x86_nasm;
extern RAsmPlugin r_asm_plugin_x86_cs;
extern RAsmPlugin r_asm_plugin_arm_gnu;
extern RAsmPlugin r_asm_plugin_arm_cs;
extern RAsmPlugin r_asm_plugin_arm_as;
extern RAsmPlugin r_asm_plugin_armthumb;
extern RAsmPlugin r_asm_plugin_arm_winedbg;
extern RAsmPlugin r_asm_plugin_csr;

View File

@ -73,6 +73,8 @@ asm.sysz
asm.tms320
asm.v850
asm.ws
asm.arm_as
asm.x86_as
asm.x86_cs
asm.x86_tab
asm.x86_nz