Rizin
unix-like reverse engineering framework and cli tools
binutils_as.c File Reference
#include "binutils_as.h"

Go to the source code of this file.

Functions

int binutils_assemble (RzAsm *a, RzAsmOp *op, const char *buf, const char *as, const char *env, const char *header, const char *cmd_opt)
 

Function Documentation

◆ binutils_assemble()

int binutils_assemble ( RzAsm a,
RzAsmOp op,
const char *  buf,
const char *  as,
const char *  env,
const char *  header,
const char *  cmd_opt 
)

Definition at line 7 of file binutils_as.c.

7  {
8  char *user_as = rz_sys_getenv(env);
9  if (user_as) {
10  as = user_as;
11  }
12  if (RZ_STR_ISEMPTY(as)) {
13  RZ_LOG_ERROR("Please set '%s' env to define a '%s' assembler program\n", env, a->cur->arch);
14  return 1;
15  }
16 
17  char *ipath, *opath;
18  int ifd = rz_file_mkstemp("rz_as", &ipath);
19  if (ifd == -1) {
20  free(user_as);
21  return -1;
22  }
23  int ofd = rz_file_mkstemp("rz_as", &opath);
24  if (ofd == -1) {
25  free(user_as);
26  free(ipath);
27  close(ifd);
28  return -1;
29  }
30 
31  int res = -1;
32  char *asm_buf = rz_str_newf("%s"
33  ".ascii \" BEGINMARK\"\n" // 4 bit align
34  "%s\n"
35  ".ascii \"ENDMARK\"\n",
36  header, buf);
37  if (!asm_buf) {
38  goto beach;
39  }
40  const size_t asm_buf_len = strlen(asm_buf);
41  const bool success = write(ifd, asm_buf, asm_buf_len) == asm_buf_len;
42  free(asm_buf);
43  if (!success) {
44  goto beach;
45  }
46 
47  char cmd[4096];
48  snprintf(cmd, sizeof(cmd), "%s %s %s -o %s", as, cmd_opt, ipath, opath);
49  if (!rz_sys_system(cmd)) {
50  int len = 0;
51  const ut8 *begin, *end;
52  close(ofd);
53  ofd = rz_sys_open(opath, O_BINARY | O_RDONLY, 0644);
54  if (ofd < 0) {
55  goto skip_ofd;
56  }
57  ut8 obuf[4096];
58  len = read(ofd, obuf, sizeof(obuf));
59  begin = rz_mem_mem(obuf, len, (const ut8 *)"BEGINMARK", 9);
60  end = rz_mem_mem(obuf, len, (const ut8 *)"ENDMARK", 7);
61  if (!begin || !end) {
62  RZ_LOG_ERROR("Cannot find water marks BEGINMARK or/and ENDMARK\n");
63  len = 0;
64  } else {
65  len = (int)(size_t)(end - begin - 9);
66  if (len > 0) {
67  rz_strbuf_setbin(&op->buf, begin + 9, len);
68  } else {
69  len = 0;
70  }
71  }
72  res = op->size = len;
73  } else {
74  RZ_LOG_ERROR("Failed to run command: '%s'\n", cmd);
75  }
76 
77 beach:
78  close(ofd);
79 skip_ofd:
80  close(ifd);
81 
82  unlink(ipath);
83  unlink(opath);
84 
85  free(ipath);
86  free(opath);
87  free(user_as);
88 
89  return res;
90 }
size_t len
Definition: 6502dis.c:15
#define O_BINARY
Definition: cpipe.c:13
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
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
#define header(is_bt, len_min, ret_op)
RZ_API int rz_file_mkstemp(RZ_NULLABLE const char *prefix, char **oname)
Definition: file.c:1058
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API const ut8 * rz_mem_mem(const ut8 *haystack, int hlen, const ut8 *needle, int nlen)
Definition: mem.c:246
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API bool rz_strbuf_setbin(RzStrBuf *sb, const ut8 *s, size_t len)
Definition: strbuf.c:85
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
RZ_API int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
RZ_API int rz_sys_system(const char *command)
Definition: sys.c:1658
static int
Definition: sfsocketcall.h:114
#define O_RDONLY
Definition: sftypes.h:486
#define a(i)
Definition: sha256.c:41
static char ** env
Definition: sys.c:32
Definition: dis.c:32
static unsigned char * obuf
Definition: z80asm.c:36
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References a, close, cmd, test_evm::end, env, free(), header, int, len, O_BINARY, O_RDONLY, obuf, read(), rz_file_mkstemp(), RZ_LOG_ERROR, rz_mem_mem(), RZ_STR_ISEMPTY, rz_str_newf(), rz_strbuf_setbin(), rz_sys_getenv(), rz_sys_open(), rz_sys_system(), snprintf, unlink, and write.

Referenced by assemble().