2016-01-11 01:42:19 +01:00
|
|
|
/* radare - LGPL - Copyright 2009-2016 - pancake */
|
2009-04-13 22:47:02 +00:00
|
|
|
|
|
|
|
#include <r_types.h>
|
|
|
|
#include <r_util.h>
|
|
|
|
#include <r_lib.h>
|
|
|
|
#include <r_asm.h>
|
|
|
|
|
2011-02-24 16:50:29 +01:00
|
|
|
static int assemble(RAsm *a, RAsmOp *op, const char *buf) {
|
2011-03-18 09:24:16 +01:00
|
|
|
char *ipath, *opath;
|
|
|
|
int ifd, ofd;
|
2011-03-04 11:32:10 +01:00
|
|
|
char asm_buf[R_ASM_BUFSIZE];
|
2010-03-25 21:14:28 +01:00
|
|
|
int len = 0;
|
|
|
|
if (a->syntax != R_ASM_SYNTAX_INTEL) {
|
|
|
|
eprintf ("asm.x86.nasm does not support non-intel syntax\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-04 11:32:10 +01:00
|
|
|
|
2011-03-18 09:24:16 +01:00
|
|
|
ifd = r_file_mkstemp ("r_nasm", &ipath);
|
2014-04-27 19:42:36 +02:00
|
|
|
if (ifd == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-03-18 09:24:16 +01:00
|
|
|
ofd = r_file_mkstemp ("r_nasm", &opath);
|
2015-02-19 12:57:58 +01:00
|
|
|
if (ofd == -1) {
|
|
|
|
free (ipath);
|
2014-04-27 19:42:36 +02:00
|
|
|
return -1;
|
2015-02-19 12:57:58 +01:00
|
|
|
}
|
2011-03-18 09:24:16 +01:00
|
|
|
|
|
|
|
len = snprintf (asm_buf, sizeof (asm_buf),
|
2015-09-18 21:54:25 +02:00
|
|
|
"[BITS %i]\nORG 0x%"PFMT64x"\n%s\n", a->bits, a->pc, buf);
|
2011-03-18 09:24:16 +01:00
|
|
|
write (ifd, asm_buf, len);
|
|
|
|
|
|
|
|
close (ifd);
|
|
|
|
|
|
|
|
if ( !r_sys_cmdf ("nasm %s -o %s", ipath, opath)) {
|
|
|
|
len = read (ofd, op->buf, R_ASM_BUFSIZE);
|
2010-08-11 18:29:15 +02:00
|
|
|
} else {
|
|
|
|
eprintf ("Error running 'nasm'\n");
|
|
|
|
len = 0;
|
|
|
|
}
|
2011-03-18 09:24:16 +01:00
|
|
|
|
|
|
|
close (ofd);
|
|
|
|
unlink (ipath);
|
|
|
|
unlink (opath);
|
|
|
|
free (ipath);
|
|
|
|
free (opath);
|
|
|
|
|
2013-12-06 05:18:57 +01:00
|
|
|
op->size = len;
|
2009-04-13 22:47:02 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2010-05-26 01:42:22 +02:00
|
|
|
RAsmPlugin r_asm_plugin_x86_nasm = {
|
2009-09-24 12:29:05 +02:00
|
|
|
.name = "x86.nasm",
|
2014-02-25 17:03:12 +01:00
|
|
|
.desc = "X86 nasm assembler",
|
2013-12-02 04:44:26 +01:00
|
|
|
.license = "LGPL3",
|
2009-04-13 22:47:02 +00:00
|
|
|
.arch = "x86",
|
2011-08-03 21:01:56 +02:00
|
|
|
// NOTE: 64bits is not supported on OSX's nasm :(
|
2013-12-05 18:41:13 +01:00
|
|
|
.bits = 16|32|64,
|
2016-04-26 19:09:15 +10:00
|
|
|
.endian = R_SYS_ENDIAN_LITTLE,
|
|
|
|
.assemble = &assemble
|
2009-04-13 22:47:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifndef CORELIB
|
|
|
|
struct r_lib_struct_t radare_plugin = {
|
|
|
|
.type = R_LIB_TYPE_ASM,
|
2015-07-12 16:04:10 +02:00
|
|
|
.data = &r_asm_plugin_x86_nasm,
|
|
|
|
.version = R2_VERSION
|
2009-04-13 22:47:02 +00:00
|
|
|
};
|
|
|
|
#endif
|