Skip to content

Commit

Permalink
Merge capstone-engine#929 with some changes to get things compile (ca…
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 authored and aquynh committed May 25, 2017
1 parent 79194cc commit a8c7e4a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions arch/X86/X86GenAsmWriter1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20484,6 +20484,8 @@ static char *getRegisterName(unsigned RegNo)
#ifdef PRINT_ALIAS_INSTR
#undef PRINT_ALIAS_INSTR

#ifndef CAPSTONE_DIET

static void printCustomAliasOperand(MCInst *MI, unsigned OpIdx,
unsigned PrintMethodIdx, SStream *OS)
{
Expand Down Expand Up @@ -20573,4 +20575,6 @@ static char *printAliasInstr(MCInst *MI, SStream *OS, void *info)
return tmp;
}

#endif

#endif // PRINT_ALIAS_INSTR
4 changes: 4 additions & 0 deletions arch/X86/X86GenAsmWriter1_reduce.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4575,6 +4575,8 @@ static char *getRegisterName(unsigned RegNo)
#ifdef PRINT_ALIAS_INSTR
#undef PRINT_ALIAS_INSTR

#ifndef CAPSTONE_DIET

static void printCustomAliasOperand(MCInst *MI, unsigned OpIdx,
unsigned PrintMethodIdx, SStream *OS)
{
Expand Down Expand Up @@ -4646,4 +4648,6 @@ static char *printAliasInstr(MCInst *MI, SStream *OS, void *info)
return tmp;
}

#endif

#endif // PRINT_ALIAS_INSTR
7 changes: 7 additions & 0 deletions arch/X86/X86IntelInstPrinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,16 @@ static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)
printMemOffset(MI, OpNo, O);
}

#ifndef CAPSTONE_DIET
static char *printAliasInstr(MCInst *MI, SStream *OS, void *info);
#endif
static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI);

void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
{
#ifndef CAPSTONE_DIET
char *mnem;
#endif
x86_reg reg, reg2;
enum cs_ac_type access1, access2;

Expand All @@ -734,11 +739,13 @@ void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
return;
}

#ifndef CAPSTONE_DIET
// Try to print any aliases first.
mnem = printAliasInstr(MI, O, Info);
if (mnem)
cs_mem_free(mnem);
else
#endif
printInstruction(MI, O, Info);

reg = X86_insn_reg_intel(MCInst_getOpcode(MI), &access1);
Expand Down
4 changes: 4 additions & 0 deletions arch/X86/X86Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#ifdef CAPSTONE_HAS_X86

#include <string.h>
#ifndef CAPSTONE_HAS_OSXKERNEL
#include <stdlib.h>
#endif

#include "X86Mapping.h"
#include "X86DisassemblerDecoder.h"
Expand Down Expand Up @@ -3128,6 +3130,7 @@ static bool valid_repne(cs_struct *h, unsigned int opcode)

// given MCInst's id, find out if this insn is valid for BND prefix
// BND prefix is valid for CALL/JMP/RET
#ifndef CAPSTONE_DIET
static bool valid_bnd(cs_struct *h, unsigned int opcode)
{
unsigned int id;
Expand Down Expand Up @@ -3170,6 +3173,7 @@ static bool valid_bnd(cs_struct *h, unsigned int opcode)
// not found
return false;
}
#endif

// given MCInst's id, find out if this insn is valid for REP prefix
static bool valid_rep(cs_struct *h, unsigned int opcode)
Expand Down
16 changes: 16 additions & 0 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
#endif

// default SKIPDATA mnemonic
#ifndef CAPSTONE_DIET
#define SKIPDATA_MNEM ".byte"
#else // No printing is available in diet mode
#define SKIPDATA_MNEM NULL
#endif

cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
Expand Down Expand Up @@ -552,6 +556,7 @@ cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
}

// generate @op_str for data instruction of SKIPDATA
#ifndef CAPSTONE_DIET
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
{
char *p = opstr;
Expand Down Expand Up @@ -580,6 +585,7 @@ static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
available -= len;
}
}
#endif

// dynamicly allocate memory to contain disasm insn
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Expand Down Expand Up @@ -708,9 +714,14 @@ size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64
insn_cache->address = offset;
insn_cache->size = (uint16_t)skipdata_bytes;
memcpy(insn_cache->bytes, buffer, skipdata_bytes);
#ifdef CAPSTONE_DIET
insn_cache->mnemonic[0] = '\0';
insn_cache->op_str[0] = '\0';
#else
strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
sizeof(insn_cache->mnemonic) - 1);
skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
#endif
insn_cache->detail = NULL;

next_offset = skipdata_bytes;
Expand Down Expand Up @@ -912,10 +923,15 @@ bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
insn->id = 0; // invalid ID for this "data" instruction
insn->address = *address;
insn->size = (uint16_t)skipdata_bytes;
#ifdef CAPSTONE_DIET
insn->mnemonic[0] = '\0';
insn->op_str[0] = '\0';
#else
memcpy(insn->bytes, *code, skipdata_bytes);
strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
sizeof(insn->mnemonic) - 1);
skipdata_opstr(insn->op_str, *code, skipdata_bytes);
#endif

*code += skipdata_bytes;
*size -= skipdata_bytes;
Expand Down
4 changes: 2 additions & 2 deletions include/capstone/capstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ extern "C" {
#endif
#else
#define CAPSTONE_API
#ifdef __GNUC__
#if defined(__GNUC__) && !defined(CAPSTONE_STATIC)
#define CAPSTONE_EXPORT __attribute__((visibility("default")))
#else
#else // defined(CAPSTONE_STATIC)
#define CAPSTONE_EXPORT
#endif
#endif
Expand Down
5 changes: 5 additions & 0 deletions include/capstone/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ typedef unsigned long long uint64_t;
#define UINT64_MAX 0xffffffffffffffffui64
#endif // defined(_MSC_VER) && (_MSC_VER <= 1700 || defined(_KERNEL_MODE))

#ifdef CAPSTONE_HAS_OSXKERNEL
// this system has stdint.h
#include <stdint.h>
#endif

#define __PRI_8_LENGTH_MODIFIER__ "hh"
#define __PRI_64_LENGTH_MODIFIER__ "ll"

Expand Down

0 comments on commit a8c7e4a

Please sign in to comment.