add cs_option() API. move ATT & Intel syntax here, rather than having them as CS_MODE, which is wrong

This commit is contained in:
Nguyen Anh Quynh 2013-12-03 21:00:09 +08:00
parent a5f0b1c3c3
commit 01aba002e3
5 changed files with 56 additions and 12 deletions

21
cs.c
View File

@ -70,10 +70,8 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
switch (ud->arch) {
case CS_ARCH_X86:
if (ud->mode & CS_MODE_SYNTAX_ATT)
ud->printer = X86_ATT_printInst;
else
ud->printer = X86_Intel_printInst;
// 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;
@ -198,6 +196,21 @@ static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mc
insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
}
cs_err cs_option(csh ud, unsigned int option)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;
if (!handle)
return CS_ERR_CSH;
if (option & CS_OPT_X86_INTEL)
handle->printer = X86_Intel_printInst;
if (option & CS_OPT_X86_ATT)
handle->printer = X86_ATT_printInst;
return CS_ERR_OK;
}
size_t cs_disasm(csh ud, unsigned char *buffer, size_t size, uint64_t offset, size_t count, cs_insn *insn)
{
cs_struct *handle = (cs_struct *)(uintptr_t)ud;

View File

@ -26,7 +26,6 @@ typedef enum cs_arch {
// Mode type
typedef enum cs_mode {
CS_MODE_LITTLE_ENDIAN = 0, // little endian mode (default mode)
CS_MODE_SYNTAX_INTEL = 0, // Intel X86 asm syntax (CS_ARCH_X86 architecture)
CS_MODE_ARM = 0, // 32-bit ARM
CS_MODE_16 = 1 << 1, // 16-bit mode
CS_MODE_32 = 1 << 2, // 32-bit mode
@ -34,11 +33,17 @@ typedef enum cs_mode {
CS_MODE_THUMB = 1 << 4, // ARM's Thumb mode, including Thumb-2
CS_MODE_MICRO = 1 << 4, // MicroMips mode (MIPS architecture)
CS_MODE_N64 = 1 << 5, // Nintendo-64 mode (MIPS architecture)
CS_MODE_SYNTAX_ATT = 1 << 30, // ATT asm syntax (CS_ARCH_X86 architecture)
CS_MODE_BIG_ENDIAN = 1 << 31 // big endian mode
} cs_mode;
// Option type
typedef enum cs_opt {
CS_OPT_X86_INTEL = 1 << 0, // Intel X86 asm syntax (CS_ARCH_X86 arch)
CS_OPT_X86_ATT = 1 << 1, // ATT asm syntax (CS_ARCH_X86 arch)
} cs_opt;
#include "arm.h"
#include "arm64.h"
#include "mips.h"
@ -124,6 +129,17 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle);
*/
cs_err cs_close(csh handle);
/*
Set option for disassembling
@handle: handle returned by cs_open()
@option: option to be set, which can be OR by several cs_opt enums
@return CS_ERR_OK on success, or other value on failure (refer to cs_err enum
for detailed error).
*/
cs_err cs_option(csh handle, unsigned int option);
/*
Report the last error number when some API function fail.
Like glibc's errno, cs_errno might not retain its old value once accessed.

View File

@ -13,6 +13,7 @@ struct platform {
unsigned char *code;
size_t size;
char *comment;
cs_opt option;
};
static void print_string_hex(unsigned char *str, int len)
@ -54,10 +55,11 @@ static void test()
},
{
.arch = CS_ARCH_X86,
.mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
.mode = CS_MODE_32,
.code = (unsigned char*)X86_CODE32,
.size = sizeof(X86_CODE32) - 1,
.comment = "X86 32bit (ATT syntax)"
.comment = "X86 32bit (ATT syntax)",
.option = CS_OPT_X86_ATT,
},
{
.arch = CS_ARCH_X86,
@ -137,6 +139,9 @@ static void test()
return;
}
if (platforms[i].option)
cs_option(handle, platforms[i].option);
//size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, insn);
size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {

View File

@ -13,6 +13,7 @@ struct platform {
unsigned char *code;
size_t size;
char *comment;
cs_opt option;
};
static void print_string_hex(unsigned char *str, int len)
@ -59,10 +60,11 @@ static void test()
},
{
.arch = CS_ARCH_X86,
.mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
.mode = CS_MODE_32,
.code = (unsigned char *)X86_CODE32,
.size = sizeof(X86_CODE32) - 1,
.comment = "X86 32bit (ATT syntax)"
.comment = "X86 32bit (ATT syntax)",
.option = CS_OPT_X86_ATT,
},
{
.arch = CS_ARCH_X86,
@ -139,6 +141,9 @@ static void test()
if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
return;
if (platforms[i].option)
cs_option(handle, platforms[i].option);
//size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, all_insn);
size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &all_insn);
if (count) {

View File

@ -15,6 +15,7 @@ struct platform {
unsigned char *code;
size_t size;
char *comment;
cs_opt option;
};
static void print_string_hex(char *comment, unsigned char *str, int len)
@ -131,10 +132,11 @@ static void test()
},
{
.arch = CS_ARCH_X86,
.mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
.mode = CS_MODE_32,
.code = (unsigned char *)X86_CODE32,
.size = sizeof(X86_CODE32) - 1,
.comment = "X86 32 (AT&T syntax)"
.comment = "X86 32 (AT&T syntax)",
.option = CS_OPT_X86_ATT,
},
{
.arch = CS_ARCH_X86,
@ -161,6 +163,9 @@ static void test()
if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
return;
if (platforms[i].option)
cs_option(handle, platforms[i].option);
//size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, insn);
size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
if (count) {