From f185180436e0eaf653675523fba1e0e908e07bad Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Sat, 21 Dec 2013 12:16:47 +0800 Subject: [PATCH] cleaner implementation for arch modularization --- Makefile | 8 +++---- arch/AArch64/module.h | 31 --------------------------- arch/ARM/module.h | 49 ------------------------------------------ arch/Mips/module.h | 48 ----------------------------------------- arch/X86/module.h | 50 ------------------------------------------- cs.c | 5 ++++- cs_priv.h | 6 ++++++ include/capstone.h | 1 + module.h | 25 ---------------------- 9 files changed, 15 insertions(+), 208 deletions(-) delete mode 100644 arch/AArch64/module.h delete mode 100644 arch/ARM/module.h delete mode 100644 arch/Mips/module.h delete mode 100644 arch/X86/module.h delete mode 100644 module.h diff --git a/Makefile b/Makefile index 4a410f24c..2df1d7836 100644 --- a/Makefile +++ b/Makefile @@ -24,28 +24,28 @@ LIBNAME = capstone LIBOBJ = LIBOBJ += cs.o utils.o SStream.o MCInstrDesc.o MCRegisterInfo.o ifneq (,$(filter $(CAPSTONE_ARCH), arm all)) - LIBOBJ += arch/ARM/ARMDisassembler.o arch/ARM/ARMInstPrinter.o arch/ARM/mapping.o + LIBOBJ += arch/ARM/ARMDisassembler.o arch/ARM/ARMInstPrinter.o arch/ARM/mapping.o arch/ARM/module.o CFLAGS += -DCS_SUPPORT_ARM ifeq ($(CAPSTONE_ARCH), arm) LIBNAME = capstone-arm endif endif ifneq (, $(filter $(CAPSTONE_ARCH), x86 all)) - LIBOBJ += arch/X86/X86DisassemblerDecoder.o arch/X86/X86Disassembler.o arch/X86/X86IntelInstPrinter.o arch/X86/X86ATTInstPrinter.o arch/X86/mapping.o + LIBOBJ += arch/X86/X86DisassemblerDecoder.o arch/X86/X86Disassembler.o arch/X86/X86IntelInstPrinter.o arch/X86/X86ATTInstPrinter.o arch/X86/mapping.o arch/X86/module.o CFLAGS += -DCS_SUPPORT_X86 ifeq ($(CAPSTONE_ARCH), x86) LIBNAME = capstone-x86 endif endif ifneq (, $(filter $(CAPSTONE_ARCH), mips all)) - LIBOBJ += arch/Mips/MipsDisassembler.o arch/Mips/MipsInstPrinter.o arch/Mips/mapping.o + LIBOBJ += arch/Mips/MipsDisassembler.o arch/Mips/MipsInstPrinter.o arch/Mips/mapping.o arch/Mips/module.o CFLAGS += -DCS_SUPPORT_MIPS ifeq ($(CAPSTONE_ARCH), mips) LIBNAME = capstone-mips endif endif ifneq (, $(filter $(CAPSTONE_ARCH), arm64 all)) - LIBOBJ += arch/AArch64/AArch64BaseInfo.o arch/AArch64/AArch64Disassembler.o arch/AArch64/AArch64InstPrinter.o arch/AArch64/mapping.o + LIBOBJ += arch/AArch64/AArch64BaseInfo.o arch/AArch64/AArch64Disassembler.o arch/AArch64/AArch64InstPrinter.o arch/AArch64/mapping.o arch/AArch64/module.o CFLAGS += -DCS_SUPPORT_AARCH64 ifeq ($(CAPSTONE_ARCH), arm64) LIBNAME = capstone-arm64 diff --git a/arch/AArch64/module.h b/arch/AArch64/module.h deleted file mode 100644 index 5cbbcb598..000000000 --- a/arch/AArch64/module.h +++ /dev/null @@ -1,31 +0,0 @@ -/* Capstone Disassembler Engine */ -/* By Dang Hoang Vu 2013 */ - -#ifndef __ARM64_INCLUDE_H__ -#define __ARM64_INCLUDE_H__ - -#include "AArch64Disassembler.h" -#include "AArch64InstPrinter.h" -#include "mapping.h" - -static void init_arm64(cs_struct *ud) -{ - MCRegisterInfo *mri = malloc(sizeof(*mri)); - - AArch64_init(mri); - ud->printer = AArch64_printInst; - ud->printer_info = mri; - ud->getinsn_info = mri; - ud->disasm = AArch64_getInstruction; - ud->reg_name = AArch64_reg_name; - ud->insn_id = AArch64_get_insn_id; - ud->insn_name = AArch64_insn_name; - ud->post_printer = AArch64_post_printer; -} - -static void __attribute__ ((constructor)) __init_arm64__() -{ - init_arch[CS_ARCH_ARM64] = init_arm64; -} - -#endif diff --git a/arch/ARM/module.h b/arch/ARM/module.h deleted file mode 100644 index cd61b6cb8..000000000 --- a/arch/ARM/module.h +++ /dev/null @@ -1,49 +0,0 @@ -/* Capstone Disassembler Engine */ -/* By Dang Hoang Vu 2013 */ - -#ifndef __ARM_INCLUDE_H__ -#define __ARM_INCLUDE_H__ - -#include "ARMDisassembler.h" -#include "ARMInstPrinter.h" -#include "mapping.h" - -static void init_arm(cs_struct *ud) -{ - MCRegisterInfo *mri = malloc(sizeof(*mri)); - - ARM_init(mri); - - ud->printer = ARM_printInst; - ud->printer_info = mri; - ud->reg_name = ARM_reg_name; - ud->insn_id = ARM_get_insn_id; - ud->insn_name = ARM_insn_name; - ud->post_printer = ARM_post_printer; - - if (ud->mode & CS_MODE_THUMB) - ud->disasm = Thumb_getInstruction; - else - ud->disasm = ARM_getInstruction; -} - -static cs_err option_arm(cs_struct *handle, cs_opt_type type, size_t value) -{ - if (type == CS_OPT_MODE) { - if (value & CS_MODE_THUMB) - handle->disasm = Thumb_getInstruction; - else - handle->disasm = ARM_getInstruction; - - handle->mode = value; - } - return CS_ERR_OK; -} - -static void __attribute__ ((constructor)) __init_arm__() -{ - init_arch[CS_ARCH_ARM] = init_arm; - option_arch[CS_ARCH_ARM] = option_arm; -} - -#endif diff --git a/arch/Mips/module.h b/arch/Mips/module.h deleted file mode 100644 index baeee9859..000000000 --- a/arch/Mips/module.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Capstone Disassembler Engine */ -/* By Dang Hoang Vu 2013 */ - -#ifndef __MIPS_INCLUDE_H__ -#define __MIPS_INCLUDE_H__ - -#include "MipsDisassembler.h" -#include "MipsInstPrinter.h" -#include "mapping.h" - -static void init_mips(cs_struct *ud) -{ - MCRegisterInfo *mri = malloc(sizeof(*mri)); - - Mips_init(mri); - ud->printer = Mips_printInst; - ud->printer_info = mri; - ud->getinsn_info = mri; - ud->reg_name = Mips_reg_name; - ud->insn_id = Mips_get_insn_id; - ud->insn_name = Mips_insn_name; - - if (ud->mode & CS_MODE_32) - ud->disasm = Mips_getInstruction; - else - ud->disasm = Mips64_getInstruction; -} - -static cs_err option_mips(cs_struct *handle, cs_opt_type type, size_t value) -{ - if (type == CS_OPT_MODE) { - if (value & CS_MODE_32) - handle->disasm = Mips_getInstruction; - else - handle->disasm = Mips64_getInstruction; - - handle->mode = value; - } - return CS_ERR_OK; -} - -static void __attribute__ ((constructor)) __init_mips__() -{ - init_arch[CS_ARCH_MIPS] = init_mips; - option_arch[CS_ARCH_MIPS] = option_mips; -} - -#endif diff --git a/arch/X86/module.h b/arch/X86/module.h deleted file mode 100644 index 4ee3f840b..000000000 --- a/arch/X86/module.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Capstone Disassembler Engine */ -/* By Dang Hoang Vu 2013 */ - -#ifndef __X86_INCLUDE_H__ -#define __X86_INCLUDE_H__ - -#include "X86Disassembler.h" -#include "X86InstPrinter.h" -#include "mapping.h" - -static void init_x86(cs_struct *ud) -{ - // by default, we use Intel syntax - ud->printer = X86_Intel_printInst; - ud->printer_info = NULL; - ud->disasm = X86_getInstruction; - ud->reg_name = X86_reg_name; - ud->insn_id = X86_get_insn_id; - ud->insn_name = X86_insn_name; - ud->post_printer = X86_post_printer; -} - -static cs_err option_x86(cs_struct *handle, cs_opt_type type, size_t value) -{ - if (type == CS_OPT_SYNTAX) { - switch(value) { - default: - // wrong syntax value - handle->errnum = CS_ERR_OPTION; - return CS_ERR_OPTION; - - case CS_OPT_SYNTAX_INTEL: - handle->printer = X86_Intel_printInst; - break; - - case CS_OPT_SYNTAX_ATT: - handle->printer = X86_ATT_printInst; - break; - } - } - return CS_ERR_OK; -} - -static void __attribute__ ((constructor)) __init_x86__() -{ - init_arch[CS_ARCH_X86] = init_x86; - option_arch[CS_ARCH_X86] = option_x86; -} - -#endif diff --git a/cs.c b/cs.c index ea551ae1f..8de123ae5 100644 --- a/cs.c +++ b/cs.c @@ -10,9 +10,12 @@ #include "MCRegisterInfo.h" -#include "module.h" #include "utils.h" +void (*init_arch[MAX_ARCH]) (cs_struct *); +cs_err (*option_arch[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value); + + void cs_version(int *major, int *minor) { *major = CS_API_MAJOR; diff --git a/cs_priv.h b/cs_priv.h index 33f74430b..2df496660 100644 --- a/cs_priv.h +++ b/cs_priv.h @@ -44,4 +44,10 @@ typedef struct cs_struct { cs_opt_value detail; } cs_struct; +#define MAX_ARCH 32 + +extern void (*init_arch[MAX_ARCH]) (cs_struct *); +extern cs_err (*option_arch[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value); + + #endif diff --git a/include/capstone.h b/include/capstone.h index 6d567401c..0182ab626 100644 --- a/include/capstone.h +++ b/include/capstone.h @@ -10,6 +10,7 @@ extern "C" { #include #include +#include // Capstone API version #define CS_API_MAJOR 1 diff --git a/module.h b/module.h deleted file mode 100644 index fd9c12e64..000000000 --- a/module.h +++ /dev/null @@ -1,25 +0,0 @@ -/* Capstone Disassembler Engine */ -/* By Dang Hoang Vu 2013 */ - -#ifndef __CS_MODULE_H__ -#define __CS_MODULE_H__ - -#define MAX_ARCH 32 - -void (*init_arch[MAX_ARCH]) (cs_struct *); -cs_err (*option_arch[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value); - -#ifdef CS_SUPPORT_X86 -#include "arch/X86/module.h" -#endif -#ifdef CS_SUPPORT_ARM -#include "arch/ARM/module.h" -#endif -#ifdef CS_SUPPORT_AARCH64 -#include "arch/AArch64/module.h" -#endif -#ifdef CS_SUPPORT_MIPS -#include "arch/Mips/module.h" -#endif - -#endif