Use capstone for ARM disassembler

Change-Id: I1c9bf3f8178d4a06babe23a918e4411833ebc418
Signed-off-by: Marc Schink <dev@zapb.de>
Reviewed-on: http://openocd.zylin.com/4812
Tested-by: jenkins
Reviewed-by: Fredrik Hederstierna <fredrik@hederstierna.com>
Reviewed-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Marc Schink 2020-08-12 13:54:10 +02:00 committed by Antonio Borneo
parent 762ddcb749
commit 67008e1bea
5 changed files with 135 additions and 1628 deletions

View File

@ -639,6 +639,15 @@ PKG_CHECK_MODULES([LIBUSB1], [libusb-1.0], [
PKG_CHECK_MODULES([LIBUSB0], [libusb], [use_libusb0=yes], [use_libusb0=no])
PKG_CHECK_MODULES([CAPSTONE], [capstone], [have_capstone=yes],
[have_capstone=no])
AS_IF([test "x$have_capstone" = "xyes"], [
AC_DEFINE([HAVE_CAPSTONE], [1], [1 if you have captone disassembly framework.])
], [
AC_DEFINE([HAVE_CAPSTONE], [0], [0 if you don't have captone disassembly framework.])
])
for hidapi_lib in hidapi hidapi-hidraw hidapi-libusb; do
PKG_CHECK_MODULES([HIDAPI],[$hidapi_lib],[
use_hidapi=yes
@ -757,6 +766,7 @@ AM_CONDITIONAL([USE_LIBGPIOD], [test "x$use_libgpiod" = "xyes"])
AM_CONDITIONAL([USE_HIDAPI], [test "x$use_hidapi" = "xyes"])
AM_CONDITIONAL([USE_LIBJAYLINK], [test "x$use_libjaylink" = "xyes"])
AM_CONDITIONAL([RSHIM], [test "x$build_rshim" = "xyes"])
AM_CONDITIONAL([HAVE_CAPSTONE], [test "x$have_capstone" = "xyes"])
AM_CONDITIONAL([MINIDRIVER], [test "x$build_minidriver" = "xyes"])
AM_CONDITIONAL([MINIDRIVER_DUMMY], [test "x$build_minidriver_dummy" = "xyes"])

View File

@ -7,6 +7,7 @@ endif
%C%_libtarget_la_LIBADD = %D%/openrisc/libopenrisc.la \
%D%/riscv/libriscv.la
%C%_libtarget_la_CPPFLAGS = $(AM_CPPFLAGS)
STARTUP_TCL_SRCS += %D%/startup.tcl
@ -33,6 +34,11 @@ noinst_LTLIBRARIES += %D%/libtarget.la
$(ARMV8_SRC) \
$(MIPS64_SRC)
if HAVE_CAPSTONE
%C%_libtarget_la_CPPFLAGS += $(CAPSTONE_CFLAGS)
%C%_libtarget_la_LIBADD += $(CAPSTONE_LIBS)
endif
TARGET_CORE_SRC = \
%D%/algorithm.c \
%D%/register.c \

File diff suppressed because it is too large Load Diff

View File

@ -197,9 +197,11 @@ int arm_evaluate_opcode(uint32_t opcode, uint32_t address,
struct arm_instruction *instruction);
int thumb_evaluate_opcode(uint16_t opcode, uint32_t address,
struct arm_instruction *instruction);
int thumb2_opcode(struct target *target, uint32_t address,
struct arm_instruction *instruction);
int arm_access_size(struct arm_instruction *instruction);
#if HAVE_CAPSTONE
int arm_disassemble(struct command_invocation *cmd, struct target *target,
target_addr_t address, size_t count, bool thumb_mode);
#endif
#define COND(opcode) (arm_condition_strings[(opcode & 0xf0000000) >> 28])

View File

@ -942,7 +942,7 @@ COMMAND_HANDLER(handle_armv4_5_core_state_command)
COMMAND_HANDLER(handle_arm_disassemble_command)
{
int retval = ERROR_OK;
#if HAVE_CAPSTONE
struct target *target = get_current_target(CMD_CTX);
if (target == NULL) {
@ -952,8 +952,8 @@ COMMAND_HANDLER(handle_arm_disassemble_command)
struct arm *arm = target_to_arm(target);
target_addr_t address;
int count = 1;
int thumb = 0;
unsigned int count = 1;
bool thumb = false;
if (!is_arm(arm)) {
command_print(CMD, "current target isn't an ARM");
@ -962,62 +962,37 @@ COMMAND_HANDLER(handle_arm_disassemble_command)
if (arm->core_type == ARM_CORE_TYPE_M_PROFILE) {
/* armv7m is always thumb mode */
thumb = 1;
thumb = true;
}
switch (CMD_ARGC) {
case 3:
if (strcmp(CMD_ARGV[2], "thumb") != 0)
goto usage;
thumb = 1;
return ERROR_COMMAND_SYNTAX_ERROR;
thumb = true;
/* FALL THROUGH */
case 2:
COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], count);
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
/* FALL THROUGH */
case 1:
COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
if (address & 0x01) {
if (!thumb) {
command_print(CMD, "Disassemble as Thumb");
thumb = 1;
thumb = true;
}
address &= ~1;
}
break;
default:
usage:
count = 0;
retval = ERROR_COMMAND_SYNTAX_ERROR;
return ERROR_COMMAND_SYNTAX_ERROR;
}
while (count-- > 0) {
struct arm_instruction cur_instruction;
if (thumb) {
/* Always use Thumb2 disassembly for best handling
* of 32-bit BL/BLX, and to work with newer cores
* (some ARMv6, all ARMv7) that use Thumb2.
*/
retval = thumb2_opcode(target, address,
&cur_instruction);
if (retval != ERROR_OK)
break;
} else {
uint32_t opcode;
retval = target_read_u32(target, address, &opcode);
if (retval != ERROR_OK)
break;
retval = arm_evaluate_opcode(opcode, address,
&cur_instruction) != ERROR_OK;
if (retval != ERROR_OK)
break;
}
command_print(CMD, "%s", cur_instruction.text);
address += cur_instruction.instruction_size;
}
return retval;
return arm_disassemble(CMD, target, address, count, thumb);
#else
command_print(CMD, "capstone disassembly framework required");
return ERROR_FAIL;
#endif
}
static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj * const *argv)