Rizin
unix-like reverse engineering framework and cli tools
asm_x86_nasm.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_lib.h>
5 #include <rz_asm.h>
6 
7 static int assemble(RzAsm *a, RzAsmOp *op, const char *buf) {
8  char *ipath, *opath;
9  if (a->syntax != RZ_ASM_SYNTAX_INTEL) {
10  RZ_LOG_ERROR("assembler: x86.nasm: the assembler does not support non-intel syntax\n");
11  return -1;
12  }
13 
14  int ifd = rz_file_mkstemp("rz_nasm", &ipath);
15  if (ifd == -1) {
16  return -1;
17  }
18 
19  int ofd = rz_file_mkstemp("rz_nasm", &opath);
20  if (ofd == -1) {
21  free(ipath);
22  return -1;
23  }
24 
25  char *asm_buf = rz_str_newf("[BITS %i]\nORG 0x%" PFMT64x "\n%s\n", a->bits, a->pc, buf);
26  if (asm_buf) {
27  rz_xwrite(ifd, asm_buf, strlen(asm_buf));
28  free(asm_buf);
29  }
30 
31  close(ifd);
32 
33  if (!rz_sys_cmdf("nasm %s -o %s", ipath, opath)) {
34  ut8 buf[512]; // TODO: remove limits
35  op->size = read(ofd, buf, sizeof(buf));
36  rz_asm_op_set_buf(op, buf, op->size);
37  } else {
38  RZ_LOG_ERROR("assembler: x86.nasm: failed to run command 'nasm %s -o %s'\n", ipath, opath);
39  }
40 
41  close(ofd);
42  unlink(ipath);
43  unlink(opath);
44  free(ipath);
45  free(opath);
46 
47  return op->size;
48 }
49 
51  .name = "x86.nasm",
52  .desc = "X86 nasm assembler",
53  .license = "LGPL3",
54  .arch = "x86",
55  // NOTE: 64bits is not supported on OSX's nasm :(
56  .bits = 16 | 32 | 64,
57  .endian = RZ_SYS_ENDIAN_LITTLE,
58  .assemble = &assemble
59 };
60 
61 #ifndef RZ_PLUGIN_INCORE
64  .data = &rz_asm_plugin_x86_nasm,
66 };
67 #endif
RZ_API void rz_asm_op_set_buf(RzAsmOp *op, const ut8 *buf, int len)
Definition: aop.c:83
RZ_API RzLibStruct rizin_plugin
Definition: asm_x86_nasm.c:62
RzAsmPlugin rz_asm_plugin_x86_nasm
Definition: asm_x86_nasm.c:50
static int assemble(RzAsm *a, RzAsmOp *op, const char *buf)
Definition: asm_x86_nasm.c:7
#define RZ_API
static static fork const void static count close
Definition: sflib.h:33
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41
@ RZ_ASM_SYNTAX_INTEL
Definition: rz_asm.h:50
RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname)
Definition: file.c:1058
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_sys_cmdf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
#define rz_xwrite(fd, buf, count)
Definition: rz_types.h:642
#define PFMT64x
Definition: rz_types.h:393
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
const char * name
Definition: rz_asm.h:130
const char * version
Definition: rz_asm.h:133
Definition: dis.c:32
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115