Modify Makefiles for TriCore architecture

This commit is contained in:
Sidney Pontes Filho 2016-05-21 14:49:53 +02:00 committed by billow
parent 67f3c46f1b
commit f5687523e3
11 changed files with 241 additions and 9 deletions

View File

@ -44,8 +44,8 @@ option(CAPSTONE_ARCHITECTURE_DEFAULT "Whether architectures are enabled by defau
option(CAPSTONE_DEBUG "Whether to enable extra debug assertions" OFF) option(CAPSTONE_DEBUG "Whether to enable extra debug assertions" OFF)
option(CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL}) option(CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL})
set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH) set(SUPPORTED_ARCHITECTURES ARM ARM64 M68K MIPS PPC SPARC SYSZ XCORE X86 TMS320C64X M680X EVM MOS65XX WASM BPF RISCV SH TriCore)
set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH) set(SUPPORTED_ARCHITECTURE_LABELS ARM ARM64 M68K MIPS PowerPC Sparc SystemZ XCore x86 TMS320C64x M680x EVM MOS65XX WASM BPF RISCV SH TriCore)
list(LENGTH SUPPORTED_ARCHITECTURES count) list(LENGTH SUPPORTED_ARCHITECTURES count)
math(EXPR count "${count}-1") math(EXPR count "${count}-1")
@ -135,6 +135,7 @@ set(HEADERS_COMMON
include/capstone/bpf.h include/capstone/bpf.h
include/capstone/riscv.h include/capstone/riscv.h
include/capstone/sh.h include/capstone/sh.h
include/capstone/tricore.h
include/capstone/platform.h include/capstone/platform.h
) )
@ -533,7 +534,27 @@ if(CAPSTONE_SH_SUPPORT)
set(TEST_SOURCES ${TEST_SOURCES} test_sh.c) set(TEST_SOURCES ${TEST_SOURCES} test_sh.c)
endif() endif()
if(CAPSTONE_OSXKERNEL_SUPPORT) if (CAPSTONE_TRICORE_SUPPORT)
add_definitions(-DCAPSTONE_HAS_TRICORE)
set(SOURCES_TRICORE
arch/TriCore/TriCoreDisassembler.c
arch/TriCore/TriCoreInstPrinter.c
arch/TriCore/TriCoreMapping.c
arch/TriCore/TriCoreModule.c
)
set(HEADERS_TRICORE
arch/TriCore/TriCoreDisassembler.h
arch/TriCore/TriCoreGenAsmWriter.inc
arch/TriCore/TriCoreGenDisassemblerTables.inc
arch/TriCore/TriCoreGenInstrInfo.inc
arch/TriCore/TriCoreGenRegisterInfo.inc
arch/TriCore/TriCoreInstPrinter.h
arch/TriCore/TriCoreMapping.h
)
set(TEST_SOURCES ${TEST_SOURCES} test_tricore.c)
endif ()
if (CAPSTONE_OSXKERNEL_SUPPORT)
add_definitions(-DCAPSTONE_HAS_OSXKERNEL) add_definitions(-DCAPSTONE_HAS_OSXKERNEL)
endif() endif()
@ -556,6 +577,7 @@ set(ALL_SOURCES
${SOURCES_BPF} ${SOURCES_BPF}
${SOURCES_RISCV} ${SOURCES_RISCV}
${SOURCES_SH} ${SOURCES_SH}
${SOURCES_TRICORE}
) )
set(ALL_HEADERS set(ALL_HEADERS
@ -578,6 +600,7 @@ set(ALL_HEADERS
${HEADERS_BPF} ${HEADERS_BPF}
${HEADERS_RISCV} ${HEADERS_RISCV}
${HEADERS_SH} ${HEADERS_SH}
${HEADERS_TRICORE}
) )
## properties ## properties
@ -640,6 +663,7 @@ source_group("Source\\MOS65XX" FILES ${SOURCES_MOS65XX})
source_group("Source\\BPF" FILES ${SOURCES_BPF}) source_group("Source\\BPF" FILES ${SOURCES_BPF})
source_group("Source\\RISCV" FILES ${SOURCES_RISCV}) source_group("Source\\RISCV" FILES ${SOURCES_RISCV})
source_group("Source\\SH" FILES ${SOURCES_SH}) source_group("Source\\SH" FILES ${SOURCES_SH})
source_group("Source\\TriCore" FILES ${SOURCES_TRICORE})
source_group("Include\\Common" FILES ${HEADERS_COMMON}) source_group("Include\\Common" FILES ${HEADERS_COMMON})
source_group("Include\\Engine" FILES ${HEADERS_ENGINE}) source_group("Include\\Engine" FILES ${HEADERS_ENGINE})
@ -660,6 +684,7 @@ source_group("Include\\MOS65XX" FILES ${HEADERS_MOS65XX})
source_group("Include\\BPF" FILES ${HEADERS_BPF}) source_group("Include\\BPF" FILES ${HEADERS_BPF})
source_group("Include\\RISCV" FILES ${HEADERS_RISCV}) source_group("Include\\RISCV" FILES ${HEADERS_RISCV})
source_group("Include\\SH" FILES ${HEADERS_SH}) source_group("Include\\SH" FILES ${HEADERS_SH})
source_group("Include\\TriCore" FILES ${HEADERS_TRICORE})
## installation ## installation
if(CAPSTONE_INSTALL) if(CAPSTONE_INSTALL)

View File

@ -100,6 +100,7 @@ Capstone requires no prerequisite packages, so it is easy to compile & install.
/usr/include/capstone/wasm.h /usr/include/capstone/wasm.h
/usr/include/capstone/x86.h /usr/include/capstone/x86.h
/usr/include/capstone/xcore.h /usr/include/capstone/xcore.h
/usr/include/capstone/tricore.h
/usr/lib/libcapstone.a /usr/lib/libcapstone.a
/usr/lib/libcapstone.so (for Linux/*nix), or /usr/lib/libcapstone.dylib (OSX) /usr/lib/libcapstone.so (for Linux/*nix), or /usr/lib/libcapstone.dylib (OSX)

View File

@ -29,6 +29,7 @@ Get CMake for free from http://www.cmake.org.
- CAPSTONE_SPARC_SUPPORT: support Sparc. Run cmake with -DCAPSTONE_SPARC_SUPPORT=0 to remove Sparc. - CAPSTONE_SPARC_SUPPORT: support Sparc. Run cmake with -DCAPSTONE_SPARC_SUPPORT=0 to remove Sparc.
- CAPSTONE_SYSZ_SUPPORT: support SystemZ. Run cmake with -DCAPSTONE_SYSZ_SUPPORT=0 to remove SystemZ. - CAPSTONE_SYSZ_SUPPORT: support SystemZ. Run cmake with -DCAPSTONE_SYSZ_SUPPORT=0 to remove SystemZ.
- CAPSTONE_XCORE_SUPPORT: support XCore. Run cmake with -DCAPSTONE_XCORE_SUPPORT=0 to remove XCore. - CAPSTONE_XCORE_SUPPORT: support XCore. Run cmake with -DCAPSTONE_XCORE_SUPPORT=0 to remove XCore.
- CAPSTONE_TRICORE_SUPPORT: support TriCore. Run cmake with -DCAPSTONE_TRICORE_SUPPORT=0 to remove TriCore.
- CAPSTONE_X86_SUPPORT: support X86. Run cmake with -DCAPSTONE_X86_SUPPORT=0 to remove X86. - CAPSTONE_X86_SUPPORT: support X86. Run cmake with -DCAPSTONE_X86_SUPPORT=0 to remove X86.
- CAPSTONE_X86_TMS320C64X: support TMS320C64X. Run cmake with -DCAPSTONE_TMS320C64X_SUPPORT=0 to remove TMS320C64X. - CAPSTONE_X86_TMS320C64X: support TMS320C64X. Run cmake with -DCAPSTONE_TMS320C64X_SUPPORT=0 to remove TMS320C64X.
- CAPSTONE_X86_M680X: support M680X. Run cmake with -DCAPSTONE_M680X_SUPPORT=0 to remove M680X. - CAPSTONE_X86_M680X: support M680X. Run cmake with -DCAPSTONE_M680X_SUPPORT=0 to remove M680X.

View File

@ -39,6 +39,7 @@ versions, and Windows Driver Kit 8.1 Update 1 or newer versions are required.
- CAPSTONE_HAS_SYSZ: support SystemZ. Delete this to remove SystemZ support. - CAPSTONE_HAS_SYSZ: support SystemZ. Delete this to remove SystemZ support.
- CAPSTONE_HAS_X86: support X86. Delete this to remove X86 support. - CAPSTONE_HAS_X86: support X86. Delete this to remove X86 support.
- CAPSTONE_HAS_XCORE: support XCore. Delete this to remove XCore support. - CAPSTONE_HAS_XCORE: support XCore. Delete this to remove XCore support.
- CAPSTONE_HAS_TRICORE: support TriCore. Delete this to remove TriCore support.
By default, all 9 architectures are compiled in. By default, all 9 architectures are compiled in.

View File

@ -306,11 +306,27 @@ ifneq (,$(findstring bpf,$(CAPSTONE_ARCHS)))
LIBOBJ_BPF += $(LIBSRC_BPF:%.c=$(OBJDIR)/%.o) LIBOBJ_BPF += $(LIBSRC_BPF:%.c=$(OBJDIR)/%.o)
endif endif
DEP_TRICORE =
DEP_TRICORE += arch/TriCore/TriCoreGenAsmWriter.inc
DEP_TRICORE += arch/TriCore/TriCoreGenInstrInfo.inc
DEP_TRICORE += arch/TriCore/TriCoreGenDisassemblerTables.inc
DEP_TRICORE += arch/TriCore/TriCoreGenRegisterInfo.inc
LIBOBJ_TRICORE =
ifneq (,$(findstring tricore,$(CAPSTONE_ARCHS)))
CFLAGS += -DCAPSTONE_HAS_TRICORE
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreDisassembler.o
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreInstPrinter.o
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreMapping.o
LIBOBJ_TRICORE += $(OBJDIR)/arch/TriCore/TriCoreModule.o
endif
LIBOBJ = LIBOBJ =
LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o LIBOBJ += $(OBJDIR)/cs.o $(OBJDIR)/utils.o $(OBJDIR)/SStream.o $(OBJDIR)/MCInstrDesc.o $(OBJDIR)/MCRegisterInfo.o
LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_RISCV) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ) LIBOBJ += $(LIBOBJ_ARM) $(LIBOBJ_ARM64) $(LIBOBJ_M68K) $(LIBOBJ_MIPS) $(LIBOBJ_PPC) $(LIBOBJ_RISCV) $(LIBOBJ_SPARC) $(LIBOBJ_SYSZ)
LIBOBJ += $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM) $(LIBOBJ_MOS65XX) $(LIBOBJ_WASM) $(LIBOBJ_BPF) LIBOBJ += $(LIBOBJ_X86) $(LIBOBJ_XCORE) $(LIBOBJ_TMS320C64X) $(LIBOBJ_M680X) $(LIBOBJ_EVM) $(LIBOBJ_MOS65XX) $(LIBOBJ_WASM) $(LIBOBJ_BPF)
LIBOBJ += $(LIBOBJ_TRICORE)
LIBOBJ += $(OBJDIR)/MCInst.o LIBOBJ += $(OBJDIR)/MCInst.o
@ -445,6 +461,7 @@ $(LIBOBJ_RISCV): $(DEP_RISCV)
$(LIBOBJ_WASM): $(DEP_WASM) $(LIBOBJ_WASM): $(DEP_WASM)
$(LIBOBJ_MOS65XX): $(DEP_MOS65XX) $(LIBOBJ_MOS65XX): $(DEP_MOS65XX)
$(LIBOBJ_BPF): $(DEP_BPF) $(LIBOBJ_BPF): $(DEP_BPF)
$(LIBOBJ_TRICORE): $(DEP_TRICORE)
ifeq ($(CAPSTONE_STATIC),yes) ifeq ($(CAPSTONE_STATIC),yes)
$(ARCHIVE): $(LIBOBJ) $(ARCHIVE): $(LIBOBJ)
@ -530,7 +547,7 @@ dist:
git archive --format=tar.gz --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).tgz git archive --format=tar.gz --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).tgz
git archive --format=zip --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).zip git archive --format=zip --prefix=capstone-$(DIST_VERSION)/ $(TAG) > capstone-$(DIST_VERSION).zip
TESTS = test_basic test_detail test_arm test_arm64 test_m68k test_mips test_ppc test_sparc TESTS = test_basic test_detail test_arm test_arm64 test_m68k test_mips test_ppc test_sparc
TESTS += test_systemz test_x86 test_xcore test_iter test_evm test_riscv test_mos65xx test_wasm test_bpf TESTS += test_systemz test_x86 test_xcore test_iter test_evm test_riscv test_mos65xx test_wasm test_bpf
TESTS += test_basic.static test_detail.static test_arm.static test_arm64.static TESTS += test_basic.static test_detail.static test_arm.static test_arm64.static
TESTS += test_m68k.static test_mips.static test_ppc.static test_sparc.static TESTS += test_m68k.static test_mips.static test_ppc.static test_sparc.static

View File

@ -0,0 +1,10 @@
//
// Created by aya on 3/4/23.
//
#ifndef CAPSTONE_TRICODEMODULE_H
#define CAPSTONE_TRICODEMODULE_H
// TODO: Add tricore function declarations here
#endif // CAPSTONE_TRICODEMODULE_H

View File

@ -4,7 +4,7 @@
################################################################################ ################################################################################
# Specify which archs you want to compile in. By default, we build all archs. # Specify which archs you want to compile in. By default, we build all archs.
CAPSTONE_ARCHS ?= arm aarch64 m68k mips powerpc sparc systemz x86 xcore tms320c64x m680x evm riscv mos65xx wasm bpf CAPSTONE_ARCHS ?= arm aarch64 m68k mips powerpc sparc systemz x86 xcore tms320c64x m680x evm riscv mos65xx wasm bpf tricore
################################################################################ ################################################################################

27
cs.c
View File

@ -69,6 +69,7 @@
#include "arch/MOS65XX/MOS65XXModule.h" #include "arch/MOS65XX/MOS65XXModule.h"
#include "arch/BPF/BPFModule.h" #include "arch/BPF/BPFModule.h"
#include "arch/SH/SHModule.h" #include "arch/SH/SHModule.h"
#include "arch/TriCore/TriCoreModule.h"
static const struct { static const struct {
// constructor initialization // constructor initialization
@ -298,6 +299,9 @@ static const uint32_t all_arch = 0
#ifdef CAPSTONE_HAS_SH #ifdef CAPSTONE_HAS_SH
| (1 << CS_ARCH_SH) | (1 << CS_ARCH_SH)
#endif #endif
#ifdef CAPSTONE_HAS_TRICORE
| (1 << CS_ARCH_TRICORE)
#endif
; ;
#if defined(CAPSTONE_USE_SYS_DYN_MEM) #if defined(CAPSTONE_USE_SYS_DYN_MEM)
@ -368,9 +372,9 @@ bool CAPSTONE_API cs_support(int query)
(1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) | (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
(1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) | (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
(1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) | (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
(1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) | (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
(1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF) | (1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF)) |
(1 << CS_ARCH_SH)); (1 << CS_ARCH_SH) | (1 << CS_ARCH_TRICORE);
if ((unsigned int)query < CS_ARCH_MAX) if ((unsigned int)query < CS_ARCH_MAX)
return all_arch & (1 << query); return all_arch & (1 << query);
@ -674,6 +678,10 @@ static uint8_t skipdata_size(cs_struct *handle)
return 4; return 4;
case CS_ARCH_SH: case CS_ARCH_SH:
return 2; return 2;
case CS_ARCH_TRICORE:
// TriCore instruction's length can be 2 or 4 bytes,
// so we just skip 2 bytes
return 2;
} }
} }
@ -1405,6 +1413,11 @@ int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type) if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
count++; count++;
break; break;
case CS_ARCH_TRICORE:
for (i = 0; i < insn->detail->tricore.op_count; i++)
if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
count++;
break;
} }
return count; return count;
@ -1506,6 +1519,14 @@ int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
return i; return i;
} }
break; break;
case CS_ARCH_TRICORE:
for (i = 0; i < insn->detail->tricore.op_count; i++) {
if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
count++;
if (count == post)
return i;
}
break;
case CS_ARCH_M68K: case CS_ARCH_M68K:
for (i = 0; i < insn->detail->m68k.op_count; i++) { for (i = 0; i < insn->detail->m68k.op_count; i++) {
if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type) if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)

View File

@ -72,6 +72,14 @@ typedef size_t csh;
/// Architecture type /// Architecture type
typedef enum cs_arch { typedef enum cs_arch {
CS_ARCH_ARM = 0, // ARM architecture (including Thumb, Thumb-2)
CS_ARCH_ARM64, // ARM-64, also called AArch64
CS_ARCH_MIPS, // Mips architecture
CS_ARCH_X86, // X86 architecture (including x86 & x86-64)
CS_ARCH_PPC, // PowerPC architecture
CS_ARCH_SPARC, // Sparc architecture
CS_ARCH_SYSZ, // SystemZ architecture
CS_ARCH_XCORE, // XCore architecture
CS_ARCH_ARM = 0, ///< ARM architecture (including Thumb, Thumb-2) CS_ARCH_ARM = 0, ///< ARM architecture (including Thumb, Thumb-2)
CS_ARCH_ARM64, ///< ARM-64, also called AArch64 CS_ARCH_ARM64, ///< ARM-64, also called AArch64
CS_ARCH_MIPS, ///< Mips architecture CS_ARCH_MIPS, ///< Mips architecture
@ -89,6 +97,7 @@ typedef enum cs_arch {
CS_ARCH_BPF, ///< Berkeley Packet Filter architecture (including eBPF) CS_ARCH_BPF, ///< Berkeley Packet Filter architecture (including eBPF)
CS_ARCH_RISCV, ///< RISCV architecture CS_ARCH_RISCV, ///< RISCV architecture
CS_ARCH_SH, ///< SH architecture CS_ARCH_SH, ///< SH architecture
CS_ARCH_TRICORE, // TriCore architecture
CS_ARCH_MAX, CS_ARCH_MAX,
CS_ARCH_ALL = 0xFFFF, // All architectures - for cs_support() CS_ARCH_ALL = 0xFFFF, // All architectures - for cs_support()
} cs_arch; } cs_arch;
@ -151,7 +160,7 @@ typedef enum cs_mode {
CS_MODE_MOS65XX_65C02 = 1 << 2, ///< MOS65XXX WDC 65c02 CS_MODE_MOS65XX_65C02 = 1 << 2, ///< MOS65XXX WDC 65c02
CS_MODE_MOS65XX_W65C02 = 1 << 3, ///< MOS65XXX WDC W65c02 CS_MODE_MOS65XX_W65C02 = 1 << 3, ///< MOS65XXX WDC W65c02
CS_MODE_MOS65XX_65816 = 1 << 4, ///< MOS65XXX WDC 65816, 8-bit m/x CS_MODE_MOS65XX_65816 = 1 << 4, ///< MOS65XXX WDC 65816, 8-bit m/x
CS_MODE_MOS65XX_65816_LONG_M = (1 << 5), ///< MOS65XXX WDC 65816, 16-bit m, 8-bit x CS_MODE_MOS65XX_65816_LONG_M = (1 << 5), ///< MOS65XXX WDC 65816, 16-bit m, 8-bit x
CS_MODE_MOS65XX_65816_LONG_X = (1 << 6), ///< MOS65XXX WDC 65816, 8-bit m, 16-bit x CS_MODE_MOS65XX_65816_LONG_X = (1 << 6), ///< MOS65XXX WDC 65816, 8-bit m, 16-bit x
CS_MODE_MOS65XX_65816_LONG_MX = CS_MODE_MOS65XX_65816_LONG_M | CS_MODE_MOS65XX_65816_LONG_X, CS_MODE_MOS65XX_65816_LONG_MX = CS_MODE_MOS65XX_65816_LONG_M | CS_MODE_MOS65XX_65816_LONG_X,
CS_MODE_SH2 = 1 << 1, ///< SH2 CS_MODE_SH2 = 1 << 1, ///< SH2
@ -289,6 +298,7 @@ typedef struct cs_opt_skipdata {
/// WASM: 1 bytes. /// WASM: 1 bytes.
/// MOS65XX: 1 bytes. /// MOS65XX: 1 bytes.
/// BPF: 8 bytes. /// BPF: 8 bytes.
/// TriCore: 2 bytes.
cs_skipdata_cb_t callback; // default value is NULL cs_skipdata_cb_t callback; // default value is NULL
/// User-defined data to be passed to @callback function pointer. /// User-defined data to be passed to @callback function pointer.
@ -313,6 +323,7 @@ typedef struct cs_opt_skipdata {
#include "mos65xx.h" #include "mos65xx.h"
#include "bpf.h" #include "bpf.h"
#include "sh.h" #include "sh.h"
#include "tricore.h"
/// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON /// NOTE: All information in cs_detail is only available when CS_OPT_DETAIL = CS_OPT_ON
/// Initialized as memset(., 0, offsetof(cs_detail, ARCH)+sizeof(cs_ARCH)) /// Initialized as memset(., 0, offsetof(cs_detail, ARCH)+sizeof(cs_ARCH))
@ -348,6 +359,7 @@ typedef struct cs_detail {
cs_bpf bpf; ///< Berkeley Packet Filter architecture (including eBPF) cs_bpf bpf; ///< Berkeley Packet Filter architecture (including eBPF)
cs_riscv riscv; ///< RISCV architecture cs_riscv riscv; ///< RISCV architecture
cs_sh sh; ///< SH architecture cs_sh sh; ///< SH architecture
cs_tricore tricore; // XCore architecture
}; };
} cs_detail; } cs_detail;

View File

@ -129,6 +129,9 @@ ifneq (,$(findstring bpf,$(CAPSTONE_ARCHS)))
CFLAGS += -DCAPSTONE_HAS_BPF CFLAGS += -DCAPSTONE_HAS_BPF
SOURCES += test_bpf.c SOURCES += test_bpf.c
endif endif
ifneq (,$(findstring tricore,$(CAPSTONE_ARCHS)))
SOURCES += test_tricore.c
endif
OBJS = $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o)) OBJS = $(addprefix $(OBJDIR)/,$(SOURCES:.c=.o))
BINARY = $(addprefix $(TESTDIR)/,$(SOURCES:.c=$(BIN_EXT))) BINARY = $(addprefix $(TESTDIR)/,$(SOURCES:.c=$(BIN_EXT)))

141
tests/test_tricore.c Normal file
View File

@ -0,0 +1,141 @@
/* Capstone Disassembler Engine */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
// TODO: This is just a copy of XCore. Implement for TriCore.
#include <stdio.h>
#include <platform.h>
#include <capstone.h>
struct platform {
cs_arch arch;
cs_mode mode;
unsigned char *code;
size_t size;
char *comment;
};
static csh handle;
static void print_string_hex(char *comment, unsigned char *str, size_t len)
{
unsigned char *c;
printf("%s", comment);
for (c = str; c < str + len; c++) {
printf("0x%02x ", *c & 0xff);
}
printf("\n");
}
static void print_insn_detail(cs_insn *ins)
{
cs_tricore *tricore;
int i;
// detail can be NULL on "data" instruction if SKIPDATA option is turned ON
if (ins->detail == NULL)
return;
tricore = &(ins->detail->tricore);
if (tricore->op_count)
printf("\top_count: %u\n", tricore->op_count);
for (i = 0; i < tricore->op_count; i++) {
cs_tricore_op *op = &(tricore->operands[i]);
switch((int)op->type) {
default:
break;
case TRICORE_OP_REG:
printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
break;
case TRICORE_OP_IMM:
printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
break;
case TRICORE_OP_MEM:
printf("\t\toperands[%u].type: MEM\n", i);
if (op->mem.base != TRICORE_REG_INVALID)
printf("\t\t\toperands[%u].mem.base: REG = %s\n",
i, cs_reg_name(handle, op->mem.base));
if (op->mem.index != TRICORE_REG_INVALID)
printf("\t\t\toperands[%u].mem.index: REG = %s\n",
i, cs_reg_name(handle, op->mem.index));
if (op->mem.disp != 0)
printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
if (op->mem.direct != 1)
printf("\t\t\toperands[%u].mem.direct: -1\n", i);
break;
}
}
printf("\n");
}
static void test()
{
#define TRICORE_CODE "\xfe\x0f\xfe\x17\x13\x17\xc6\xfe\xec\x17\x97\xf8\xec\x4f\x1f\xfd\xec\x37\x07\xf2\x45\x5b\xf9\xfa\x02\x06\x1b\x10\x09\xfd\xec\xa7"
struct platform platforms[] = {
{
CS_ARCH_TRICORE,
CS_MODE_BIG_ENDIAN,
(unsigned char*)TRICORE_CODE,
sizeof(TRICORE_CODE) - 1,
"TriCore",
},
};
uint64_t address = 0x1000;
cs_insn *insn;
int i;
size_t count;
for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
if (err) {
printf("Failed on cs_open() with error returned: %u\n", err);
continue;
}
cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {
size_t j;
printf("****************\n");
printf("Platform: %s\n", platforms[i].comment);
print_string_hex("Code:", platforms[i].code, platforms[i].size);
printf("Disasm:\n");
for (j = 0; j < count; j++) {
printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
print_insn_detail(&insn[j]);
}
printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
// free memory allocated by cs_disasm()
cs_free(insn, count);
} else {
printf("****************\n");
printf("Platform: %s\n", platforms[i].comment);
print_string_hex("Code:", platforms[i].code, platforms[i].size);
printf("ERROR: Failed to disasm given code!\n");
}
printf("\n");
cs_close(&handle);
}
}
int main()
{
test();
return 0;
}