#include <libdisasm.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/byteorder.h>
#include <string.h>
#include "libdisasm_impl.h"
#include "dis_sparc.h"
static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
uint32_t);
static uint32_t dis_get_bits(uint32_t, int, int);
#if !defined(DIS_STANDALONE)
static void do_binary(uint32_t);
#endif
static void
dis_sparc_handle_detach(dis_handle_t *dhp)
{
dis_free(dhp->dh_arch_private, sizeof (dis_handle_sparc_t));
dhp->dh_arch_private = NULL;
}
static int
dis_sparc_handle_attach(dis_handle_t *dhp)
{
dis_handle_sparc_t *dhx;
#if !defined(DIS_STANDALONE)
char *opt = NULL;
char *opt2, *save, *end;
#endif
if ((dhp->dh_flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI))
== 0) {
(void) dis_seterrno(E_DIS_INVALFLAG);
return (-1);
}
if ((dhx = dis_zalloc(sizeof (dis_handle_sparc_t))) == NULL) {
(void) dis_seterrno(E_DIS_NOMEM);
return (0);
}
dhx->dhx_debug = DIS_DEBUG_COMPAT;
dhp->dh_arch_private = dhx;
#if !defined(DIS_STANDALONE)
opt = getenv("_LIBDISASM_DEBUG");
if (opt == NULL)
return (0);
opt2 = strdup(opt);
if (opt2 == NULL) {
dis_handle_destroy(dhp);
dis_free(dhx, sizeof (dis_handle_sparc_t));
(void) dis_seterrno(E_DIS_NOMEM);
return (-1);
}
save = opt2;
while (opt2 != NULL) {
end = strchr(opt2, ',');
if (end != 0)
*end++ = '\0';
if (strcasecmp("synth-all", opt2) == 0)
dhx->dhx_debug |= DIS_DEBUG_SYN_ALL;
if (strcasecmp("compat", opt2) == 0)
dhx->dhx_debug |= DIS_DEBUG_COMPAT;
if (strcasecmp("synth-none", opt2) == 0)
dhx->dhx_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
if (strcasecmp("binary", opt2) == 0)
dhx->dhx_debug |= DIS_DEBUG_PRTBIN;
if (strcasecmp("format", opt2) == 0)
dhx->dhx_debug |= DIS_DEBUG_PRTFMT;
if (strcasecmp("all", opt2) == 0)
dhx->dhx_debug = DIS_DEBUG_ALL;
if (strcasecmp("none", opt2) == 0)
dhx->dhx_debug = DIS_DEBUG_NONE;
opt2 = end;
}
free(save);
#endif
return (0);
}
static int
dis_sparc_max_instrlen(dis_handle_t *dhp)
{
return (4);
}
static int
dis_sparc_min_instrlen(dis_handle_t *dhp)
{
return (4);
}
static uint64_t
dis_sparc_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
{
if (n <= 0)
return (pc);
if (pc < n)
return (pc);
return (pc - n*4);
}
static int
dis_sparc_instrlen(dis_handle_t *dhp, uint64_t pc)
{
return (4);
}
static int
dis_sparc_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf,
size_t buflen)
{
dis_handle_sparc_t *dhx = dhp->dh_arch_private;
const table_t *tp = &initial_table;
const inst_t *inp = NULL;
uint32_t instr;
uint32_t idx = 0;
if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
sizeof (instr))
return (-1);
dhx->dhx_buf = buf;
dhx->dhx_buflen = buflen;
dhp->dh_addr = addr;
buf[0] = '\0';
#if !defined(DIS_STANDALONE)
instr = BE_32(instr);
#endif
#if !defined(DIS_STANDALONE)
if ((dhx->dhx_debug & DIS_DEBUG_PRTBIN) != 0)
do_binary(instr);
#endif
while (1) {
idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
inp = &tp->tbl_inp[idx];
inp = dis_get_overlay(dhp, tp, idx);
if ((inp->in_type == INST_NONE) ||
((inp->in_arch & dhp->dh_flags) == 0))
goto error;
if (inp->in_type == INST_TBL) {
tp = inp->in_data.in_tbl;
continue;
}
break;
}
if (tp->tbl_fmt(dhp, instr, inp, idx) == 0)
return (0);
error:
(void) dis_snprintf(buf, buflen,
((dhp->dh_flags & DIS_OCTAL) != 0) ? "0%011lo" : "0x%08lx",
instr);
return (0);
}
static uint32_t
dis_get_bits(uint32_t instr, int offset, int length)
{
uint32_t mask, val;
int i;
for (i = 0, mask = 0; i < length; ++i)
mask |= (1UL << i);
mask = mask << (offset - length + 1);
val = instr & mask;
val = val >> (offset - length + 1);
return (val);
}
static const inst_t *
dis_get_overlay(dis_handle_t *dhp, const table_t *tp, uint32_t idx)
{
const inst_t *ip = &tp->tbl_inp[idx];
int i;
if (tp->tbl_ovp == NULL)
return (ip);
for (i = 0; tp->tbl_ovp[i].ov_idx != -1; ++i) {
if (tp->tbl_ovp[i].ov_idx != idx)
continue;
if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
continue;
ip = &tp->tbl_ovp[i].ov_inst;
break;
}
return (ip);
}
#if !defined(DIS_STANDALONE)
static void
do_binary(uint32_t instr)
{
(void) fprintf(stderr, "DISASM: ");
prt_binary(instr, 32);
(void) fprintf(stderr, "\n");
}
#endif
static int
dis_sparc_supports_flags(int flags)
{
int archflags = flags & DIS_ARCH_MASK;
if (archflags == DIS_SPARC_V8 ||
(archflags & (DIS_SPARC_V9 | DIS_SPARC_V8)) == DIS_SPARC_V9)
return (1);
return (0);
}
const dis_arch_t dis_arch_sparc = {
.da_supports_flags = dis_sparc_supports_flags,
.da_handle_attach = dis_sparc_handle_attach,
.da_handle_detach = dis_sparc_handle_detach,
.da_disassemble = dis_sparc_disassemble,
.da_previnstr = dis_sparc_previnstr,
.da_min_instrlen = dis_sparc_min_instrlen,
.da_max_instrlen = dis_sparc_max_instrlen,
.da_instrlen = dis_sparc_instrlen
};