2003-04-29 22:41:16 +02:00
|
|
|
/* General "disassemble this chunk" code. Used for debugging. */
|
2016-01-29 18:50:05 +01:00
|
|
|
#include "qemu/osdep.h"
|
2023-05-09 18:14:36 +02:00
|
|
|
#include "disas/disas-internal.h"
|
2003-04-29 22:41:16 +02:00
|
|
|
#include "elf.h"
|
2019-04-17 21:18:03 +02:00
|
|
|
#include "qemu/qemu-print.h"
|
2012-10-24 11:12:21 +02:00
|
|
|
#include "disas/disas.h"
|
2017-09-14 18:41:12 +02:00
|
|
|
#include "disas/capstone.h"
|
2023-05-03 09:23:29 +02:00
|
|
|
#include "hw/core/cpu.h"
|
|
|
|
#include "exec/memory.h"
|
2003-10-27 22:13:58 +01:00
|
|
|
|
2003-04-29 22:41:16 +02:00
|
|
|
/* Filled in by elfload.c. Simplistic, but will do for now. */
|
2004-12-20 00:18:01 +01:00
|
|
|
struct syminfo *syminfos = NULL;
|
2003-04-29 22:41:16 +02:00
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
/*
|
|
|
|
* Get LENGTH bytes from info's buffer, at host address memaddr.
|
|
|
|
* Transfer them to myaddr.
|
|
|
|
*/
|
|
|
|
static int host_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
|
|
|
|
struct disassemble_info *info)
|
2003-06-09 17:23:31 +02:00
|
|
|
{
|
2003-10-27 22:13:58 +01:00
|
|
|
if (memaddr < info->buffer_vma
|
2020-09-10 23:40:54 +02:00
|
|
|
|| memaddr + length > info->buffer_vma + info->buffer_length) {
|
2003-10-27 22:13:58 +01:00
|
|
|
/* Out of bounds. Use EIO because GDB uses it. */
|
|
|
|
return EIO;
|
2020-09-10 23:40:54 +02:00
|
|
|
}
|
2003-10-27 22:13:58 +01:00
|
|
|
memcpy (myaddr, info->buffer + (memaddr - info->buffer_vma), length);
|
|
|
|
return 0;
|
2003-06-09 17:23:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
/*
|
|
|
|
* Get LENGTH bytes from info's buffer, at target address memaddr.
|
|
|
|
* Transfer them to myaddr.
|
|
|
|
*/
|
|
|
|
static int target_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
|
|
|
|
struct disassemble_info *info)
|
2003-10-27 22:13:58 +01:00
|
|
|
{
|
2012-09-08 14:40:00 +02:00
|
|
|
CPUDebug *s = container_of(info, CPUDebug, info);
|
2020-09-10 23:40:54 +02:00
|
|
|
int r = cpu_memory_rw_debug(s->cpu, memaddr, myaddr, length, 0);
|
2020-05-18 17:53:04 +02:00
|
|
|
return r ? EIO : 0;
|
2003-10-27 22:13:58 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
/*
|
|
|
|
* Print an error message. We can assume that this is in response to
|
|
|
|
* an error return from {host,target}_read_memory.
|
|
|
|
*/
|
|
|
|
static void perror_memory(int status, bfd_vma memaddr,
|
|
|
|
struct disassemble_info *info)
|
2003-06-09 17:23:31 +02:00
|
|
|
{
|
2020-09-10 23:40:54 +02:00
|
|
|
if (status != EIO) {
|
|
|
|
/* Can't happen. */
|
|
|
|
info->fprintf_func(info->stream, "Unknown error %d\n", status);
|
|
|
|
} else {
|
|
|
|
/* Address between memaddr and memaddr + len was out of bounds. */
|
|
|
|
info->fprintf_func(info->stream,
|
|
|
|
"Address 0x%" PRIx64 " is out of bounds.\n",
|
|
|
|
memaddr);
|
|
|
|
}
|
2003-06-09 17:23:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
/* Print address in hex. */
|
|
|
|
static void print_address(bfd_vma addr, struct disassemble_info *info)
|
2003-06-09 17:23:31 +02:00
|
|
|
{
|
2020-09-10 23:40:54 +02:00
|
|
|
info->fprintf_func(info->stream, "0x%" PRIx64, addr);
|
2003-06-09 17:23:31 +02:00
|
|
|
}
|
|
|
|
|
2012-06-25 06:55:55 +02:00
|
|
|
/* Print address in hex, truncated to the width of a host virtual address. */
|
2020-09-10 23:40:54 +02:00
|
|
|
static void host_print_address(bfd_vma addr, struct disassemble_info *info)
|
2012-06-25 06:55:55 +02:00
|
|
|
{
|
2020-09-10 23:40:54 +02:00
|
|
|
print_address((uintptr_t)addr, info);
|
2012-06-25 06:55:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
/* Stub prevents some fruitless earching in optabs disassemblers. */
|
|
|
|
static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
|
2003-06-09 17:23:31 +02:00
|
|
|
{
|
2020-09-10 23:40:54 +02:00
|
|
|
return 1;
|
2003-06-09 17:23:31 +02:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:29:45 +02:00
|
|
|
static int print_insn_objdump(bfd_vma pc, disassemble_info *info,
|
|
|
|
const char *prefix)
|
|
|
|
{
|
|
|
|
int i, n = info->buffer_length;
|
2022-09-29 13:42:14 +02:00
|
|
|
g_autofree uint8_t *buf = g_malloc(n);
|
|
|
|
|
|
|
|
if (info->read_memory_func(pc, buf, n, info) == 0) {
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
if (i % 32 == 0) {
|
|
|
|
info->fprintf_func(info->stream, "\n%s: ", prefix);
|
|
|
|
}
|
|
|
|
info->fprintf_func(info->stream, "%02x", buf[i]);
|
2013-08-17 08:29:45 +02:00
|
|
|
}
|
2022-09-29 13:42:14 +02:00
|
|
|
} else {
|
|
|
|
info->fprintf_func(info->stream, "unable to read memory");
|
2013-08-17 08:29:45 +02:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_insn_od_host(bfd_vma pc, disassemble_info *info)
|
|
|
|
{
|
|
|
|
return print_insn_objdump(pc, info, "OBJD-H");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_insn_od_target(bfd_vma pc, disassemble_info *info)
|
|
|
|
{
|
|
|
|
return print_insn_objdump(pc, info, "OBJD-T");
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
static void initialize_debug(CPUDebug *s)
|
|
|
|
{
|
|
|
|
memset(s, 0, sizeof(*s));
|
|
|
|
s->info.arch = bfd_arch_unknown;
|
|
|
|
s->info.cap_arch = -1;
|
|
|
|
s->info.cap_insn_unit = 4;
|
|
|
|
s->info.cap_insn_split = 4;
|
|
|
|
s->info.memory_error_func = perror_memory;
|
|
|
|
s->info.symbol_at_address_func = symbol_at_address;
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:14:36 +02:00
|
|
|
void disas_initialize_debug_target(CPUDebug *s, CPUState *cpu)
|
2020-09-10 23:40:54 +02:00
|
|
|
{
|
|
|
|
initialize_debug(s);
|
|
|
|
|
|
|
|
s->cpu = cpu;
|
|
|
|
s->info.read_memory_func = target_read_memory;
|
|
|
|
s->info.print_address_func = print_address;
|
2023-05-09 18:27:48 +02:00
|
|
|
if (target_words_bigendian()) {
|
|
|
|
s->info.endian = BFD_ENDIAN_BIG;
|
|
|
|
} else {
|
|
|
|
s->info.endian = BFD_ENDIAN_LITTLE;
|
|
|
|
}
|
2020-09-10 23:40:54 +02:00
|
|
|
|
|
|
|
CPUClass *cc = CPU_GET_CLASS(cpu);
|
|
|
|
if (cc->disas_set_info) {
|
|
|
|
cc->disas_set_info(cpu, &s->info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initialize_debug_host(CPUDebug *s)
|
|
|
|
{
|
|
|
|
initialize_debug(s);
|
|
|
|
|
|
|
|
s->info.read_memory_func = host_read_memory;
|
|
|
|
s->info.print_address_func = host_print_address;
|
2022-03-23 16:57:17 +01:00
|
|
|
#if HOST_BIG_ENDIAN
|
2020-09-10 23:40:54 +02:00
|
|
|
s->info.endian = BFD_ENDIAN_BIG;
|
|
|
|
#else
|
|
|
|
s->info.endian = BFD_ENDIAN_LITTLE;
|
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_TCG_INTERPRETER)
|
|
|
|
s->info.print_insn = print_insn_tci;
|
|
|
|
#elif defined(__i386__)
|
|
|
|
s->info.mach = bfd_mach_i386_i386;
|
|
|
|
s->info.cap_arch = CS_ARCH_X86;
|
|
|
|
s->info.cap_mode = CS_MODE_32;
|
|
|
|
s->info.cap_insn_unit = 1;
|
|
|
|
s->info.cap_insn_split = 8;
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
s->info.mach = bfd_mach_x86_64;
|
|
|
|
s->info.cap_arch = CS_ARCH_X86;
|
|
|
|
s->info.cap_mode = CS_MODE_64;
|
|
|
|
s->info.cap_insn_unit = 1;
|
|
|
|
s->info.cap_insn_split = 8;
|
|
|
|
#elif defined(_ARCH_PPC)
|
|
|
|
s->info.cap_arch = CS_ARCH_PPC;
|
|
|
|
# ifdef _ARCH_PPC64
|
|
|
|
s->info.cap_mode = CS_MODE_64;
|
|
|
|
# endif
|
2023-05-09 18:27:48 +02:00
|
|
|
#elif defined(__riscv)
|
2020-09-10 23:40:54 +02:00
|
|
|
#if defined(_ILP32) || (__riscv_xlen == 32)
|
|
|
|
s->info.print_insn = print_insn_riscv32;
|
|
|
|
#elif defined(_LP64)
|
|
|
|
s->info.print_insn = print_insn_riscv64;
|
|
|
|
#else
|
|
|
|
#error unsupported RISC-V ABI
|
|
|
|
#endif
|
2020-09-12 02:59:01 +02:00
|
|
|
#elif defined(__aarch64__)
|
2020-09-10 23:40:54 +02:00
|
|
|
s->info.cap_arch = CS_ARCH_ARM64;
|
|
|
|
#elif defined(__alpha__)
|
|
|
|
s->info.print_insn = print_insn_alpha;
|
|
|
|
#elif defined(__sparc__)
|
|
|
|
s->info.print_insn = print_insn_sparc;
|
|
|
|
s->info.mach = bfd_mach_sparc_v9b;
|
|
|
|
#elif defined(__arm__)
|
|
|
|
/* TCG only generates code for arm mode. */
|
|
|
|
s->info.cap_arch = CS_ARCH_ARM;
|
|
|
|
#elif defined(__MIPSEB__)
|
|
|
|
s->info.print_insn = print_insn_big_mips;
|
|
|
|
#elif defined(__MIPSEL__)
|
|
|
|
s->info.print_insn = print_insn_little_mips;
|
|
|
|
#elif defined(__m68k__)
|
|
|
|
s->info.print_insn = print_insn_m68k;
|
|
|
|
#elif defined(__s390__)
|
2020-01-03 22:24:59 +01:00
|
|
|
s->info.cap_arch = CS_ARCH_SYSZ;
|
|
|
|
s->info.cap_insn_unit = 2;
|
|
|
|
s->info.cap_insn_split = 6;
|
2020-09-10 23:40:54 +02:00
|
|
|
#elif defined(__hppa__)
|
|
|
|
s->info.print_insn = print_insn_hppa;
|
2022-12-01 01:22:09 +01:00
|
|
|
#elif defined(__loongarch__)
|
|
|
|
s->info.print_insn = print_insn_loongarch;
|
2020-09-10 23:40:54 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:38:35 +02:00
|
|
|
/* Disassemble this for me please... (debugging). */
|
2023-05-03 09:23:27 +02:00
|
|
|
void target_disas(FILE *out, CPUState *cpu, uint64_t code, size_t size)
|
2003-04-29 22:41:16 +02:00
|
|
|
{
|
2023-05-03 09:23:27 +02:00
|
|
|
uint64_t pc;
|
2003-04-29 22:41:16 +02:00
|
|
|
int count;
|
2012-09-08 14:40:00 +02:00
|
|
|
CPUDebug s;
|
2003-04-29 22:41:16 +02:00
|
|
|
|
2023-05-09 18:14:36 +02:00
|
|
|
disas_initialize_debug_target(&s, cpu);
|
2020-09-10 23:40:54 +02:00
|
|
|
s.info.fprintf_func = fprintf;
|
|
|
|
s.info.stream = out;
|
2012-09-08 14:40:00 +02:00
|
|
|
s.info.buffer_vma = code;
|
|
|
|
s.info.buffer_length = size;
|
2015-06-24 05:57:33 +02:00
|
|
|
|
2017-09-14 18:41:12 +02:00
|
|
|
if (s.info.cap_arch >= 0 && cap_disas_target(&s.info, code, size)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-24 05:57:32 +02:00
|
|
|
if (s.info.print_insn == NULL) {
|
|
|
|
s.info.print_insn = print_insn_od_target;
|
2013-08-17 08:29:45 +02:00
|
|
|
}
|
2003-10-27 22:13:58 +01:00
|
|
|
|
2009-02-13 22:44:41 +01:00
|
|
|
for (pc = code; size > 0; pc += count, size -= count) {
|
2023-05-03 09:23:27 +02:00
|
|
|
fprintf(out, "0x%08" PRIx64 ": ", pc);
|
2023-05-10 19:04:45 +02:00
|
|
|
count = s.info.print_insn(pc, &s.info);
|
|
|
|
fprintf(out, "\n");
|
|
|
|
if (count < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2009-04-22 00:26:22 +02:00
|
|
|
if (size < count) {
|
|
|
|
fprintf(out,
|
|
|
|
"Disassembler disagrees with translator over instruction "
|
|
|
|
"decoding\n"
|
|
|
|
"Please report this to qemu-devel@nongnu.org\n");
|
|
|
|
break;
|
|
|
|
}
|
2005-01-04 00:35:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:14:36 +02:00
|
|
|
int disas_gstring_printf(FILE *stream, const char *fmt, ...)
|
2019-05-22 11:27:14 +02:00
|
|
|
{
|
2020-09-11 00:13:38 +02:00
|
|
|
/* We abuse the FILE parameter to pass a GString. */
|
|
|
|
GString *s = (GString *)stream;
|
2019-05-22 11:27:14 +02:00
|
|
|
int initial_len = s->len;
|
2020-09-11 00:13:38 +02:00
|
|
|
va_list va;
|
2019-05-22 11:27:14 +02:00
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
g_string_append_vprintf(s, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return s->len - initial_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void plugin_print_address(bfd_vma addr, struct disassemble_info *info)
|
|
|
|
{
|
|
|
|
/* does nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We should only be dissembling one instruction at a time here. If
|
|
|
|
* there is left over it usually indicates the front end has read more
|
|
|
|
* bytes than it needed.
|
|
|
|
*/
|
|
|
|
char *plugin_disas(CPUState *cpu, uint64_t addr, size_t size)
|
|
|
|
{
|
|
|
|
CPUDebug s;
|
2020-09-11 00:13:38 +02:00
|
|
|
GString *ds = g_string_new(NULL);
|
2019-05-22 11:27:14 +02:00
|
|
|
|
2023-05-09 18:14:36 +02:00
|
|
|
disas_initialize_debug_target(&s, cpu);
|
|
|
|
s.info.fprintf_func = disas_gstring_printf;
|
2020-09-11 00:13:38 +02:00
|
|
|
s.info.stream = (FILE *)ds; /* abuse this slot */
|
2019-05-22 11:27:14 +02:00
|
|
|
s.info.buffer_vma = addr;
|
|
|
|
s.info.buffer_length = size;
|
|
|
|
s.info.print_address_func = plugin_print_address;
|
|
|
|
|
|
|
|
if (s.info.cap_arch >= 0 && cap_disas_plugin(&s.info, addr, size)) {
|
2020-09-11 00:13:38 +02:00
|
|
|
; /* done */
|
|
|
|
} else if (s.info.print_insn) {
|
|
|
|
s.info.print_insn(addr, &s.info);
|
|
|
|
} else {
|
|
|
|
; /* cannot disassemble -- return empty string */
|
2019-05-22 11:27:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 00:13:38 +02:00
|
|
|
/* Return the buffer, freeing the GString container. */
|
|
|
|
return g_string_free(ds, false);
|
2019-05-22 11:27:14 +02:00
|
|
|
}
|
|
|
|
|
2005-01-04 00:35:10 +01:00
|
|
|
/* Disassemble this for me please... (debugging). */
|
2023-05-03 09:23:27 +02:00
|
|
|
void disas(FILE *out, const void *code, size_t size)
|
2005-01-04 00:35:10 +01:00
|
|
|
{
|
2012-04-12 15:44:35 +02:00
|
|
|
uintptr_t pc;
|
2005-01-04 00:35:10 +01:00
|
|
|
int count;
|
2012-09-08 14:40:00 +02:00
|
|
|
CPUDebug s;
|
2005-01-04 00:35:10 +01:00
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
initialize_debug_host(&s);
|
|
|
|
s.info.fprintf_func = fprintf;
|
|
|
|
s.info.stream = out;
|
2012-09-08 14:40:00 +02:00
|
|
|
s.info.buffer = code;
|
|
|
|
s.info.buffer_vma = (uintptr_t)code;
|
|
|
|
s.info.buffer_length = size;
|
2017-09-14 18:41:12 +02:00
|
|
|
|
2020-09-10 21:15:04 +02:00
|
|
|
if (s.info.cap_arch >= 0 && cap_disas_host(&s.info, code, size)) {
|
2017-09-14 18:41:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:40:54 +02:00
|
|
|
if (s.info.print_insn == NULL) {
|
|
|
|
s.info.print_insn = print_insn_od_host;
|
2013-08-17 08:29:45 +02:00
|
|
|
}
|
2012-04-12 15:44:35 +02:00
|
|
|
for (pc = (uintptr_t)code; size > 0; pc += count, size -= count) {
|
|
|
|
fprintf(out, "0x%08" PRIxPTR ": ", pc);
|
2020-09-10 23:40:54 +02:00
|
|
|
count = s.info.print_insn(pc, &s.info);
|
2020-05-13 19:51:32 +02:00
|
|
|
fprintf(out, "\n");
|
|
|
|
if (count < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2003-04-29 22:41:16 +02:00
|
|
|
}
|
2020-05-13 19:51:32 +02:00
|
|
|
|
2003-04-29 22:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look up symbol for debugging purpose. Returns "" if unknown. */
|
2023-05-03 09:23:27 +02:00
|
|
|
const char *lookup_symbol(uint64_t orig_addr)
|
2003-04-29 22:41:16 +02:00
|
|
|
{
|
2008-10-22 17:11:31 +02:00
|
|
|
const char *symbol = "";
|
2004-12-20 00:18:01 +01:00
|
|
|
struct syminfo *s;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-12-20 00:18:01 +01:00
|
|
|
for (s = syminfos; s; s = s->next) {
|
2008-10-22 17:11:31 +02:00
|
|
|
symbol = s->lookup_symbol(s, orig_addr);
|
|
|
|
if (symbol[0] != '\0') {
|
|
|
|
break;
|
|
|
|
}
|
2003-04-29 22:41:16 +02:00
|
|
|
}
|
2008-10-22 17:11:31 +02:00
|
|
|
|
|
|
|
return symbol;
|
2003-04-29 22:41:16 +02:00
|
|
|
}
|