From 07c3693cf2ab8641008ddb8cd454c9201d90bba8 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Tue, 3 Jun 2014 18:33:15 +0800 Subject: [PATCH] cmake: properly export public APIs in capstone.DLL. thanks to Daniel Pistelli for helping to fix this issue --- CMakeLists.txt | 1 + cs.c | 16 ++++++++++++++++ include/capstone.h | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index eeed76707..dfde981c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ include_directories("${PROJECT_SOURCE_DIR}/include") if (BUILD_STATIC) add_library(capstone STATIC ${SOURCES}) else () + add_definitions(-DCAPSTONE_SHARED) add_library(capstone SHARED ${SOURCES}) endif () diff --git a/cs.c b/cs.c index 21ca39d71..08cf8e3eb 100644 --- a/cs.c +++ b/cs.c @@ -87,6 +87,7 @@ cs_free_t cs_mem_free = NULL; cs_vsnprintf_t cs_vsnprintf = NULL; #endif +CAPSTONE_EXPORT unsigned int cs_version(int *major, int *minor) { archs_enable(); @@ -99,6 +100,7 @@ unsigned int cs_version(int *major, int *minor) return (CS_API_MAJOR << 8) + CS_API_MINOR; } +CAPSTONE_EXPORT bool cs_support(int query) { archs_enable(); @@ -132,6 +134,7 @@ bool cs_support(int query) return false; } +CAPSTONE_EXPORT cs_err cs_errno(csh handle) { struct cs_struct *ud; @@ -143,6 +146,7 @@ cs_err cs_errno(csh handle) return ud->errnum; } +CAPSTONE_EXPORT const char *cs_strerror(cs_err code) { switch(code) { @@ -175,6 +179,7 @@ const char *cs_strerror(cs_err code) } } +CAPSTONE_EXPORT cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) { cs_err err; @@ -219,6 +224,7 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) } } +CAPSTONE_EXPORT cs_err cs_close(csh *handle) { struct cs_struct *ud; @@ -345,6 +351,7 @@ static uint8_t skipdata_size(cs_struct *handle) } } +CAPSTONE_EXPORT cs_err cs_option(csh ud, cs_opt_type type, size_t value) { struct cs_struct *handle; @@ -415,6 +422,7 @@ static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size) // dynamicly allocate memory to contain disasm insn // NOTE: caller must free() the allocated memory itself to avoid memory leaking +CAPSTONE_EXPORT size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) { struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -569,6 +577,7 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, return c; } +CAPSTONE_EXPORT void cs_free(cs_insn *insn, size_t count) { size_t i; @@ -582,6 +591,7 @@ void cs_free(cs_insn *insn, size_t count) } // return friendly name of regiser in a string +CAPSTONE_EXPORT const char *cs_reg_name(csh ud, unsigned int reg) { struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -593,6 +603,7 @@ const char *cs_reg_name(csh ud, unsigned int reg) return handle->reg_name(ud, reg); } +CAPSTONE_EXPORT const char *cs_insn_name(csh ud, unsigned int insn) { struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; @@ -616,6 +627,7 @@ static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id) return false; } +CAPSTONE_EXPORT bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id) { struct cs_struct *handle; @@ -642,6 +654,7 @@ bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id) return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id); } +CAPSTONE_EXPORT bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id) { struct cs_struct *handle; @@ -668,6 +681,7 @@ bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id) return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id); } +CAPSTONE_EXPORT bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id) { struct cs_struct *handle; @@ -694,6 +708,7 @@ bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id) return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id); } +CAPSTONE_EXPORT int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) { struct cs_struct *handle; @@ -769,6 +784,7 @@ int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) return count; } +CAPSTONE_EXPORT int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, unsigned int post) { diff --git a/include/capstone.h b/include/capstone.h index 9ec8bce28..de33b2f75 100644 --- a/include/capstone.h +++ b/include/capstone.h @@ -18,6 +18,13 @@ extern "C" { #ifdef _MSC_VER #pragma warning(disable:4201) #pragma warning(disable:4100) +#ifdef CAPSTONE_SHARED // compiling DLL file +#define CAPSTONE_EXPORT __declspec(dllexport) +#else +#define CAPSTONE_EXPORT __declspec(dllimport) +#endif +#else // not MSVC +#define CAPSTONE_EXPORT #endif // Capstone API version @@ -254,6 +261,7 @@ typedef enum cs_err { NOTE: if you only care about returned value, but not major and minor values, set both @major & @minor arguments to NULL. */ +CAPSTONE_EXPORT unsigned int cs_version(int *major, int *minor); @@ -270,6 +278,7 @@ unsigned int cs_version(int *major, int *minor); @return True if this library supports the given arch, or in 'diet' mode. */ +CAPSTONE_EXPORT bool cs_support(int query); /* @@ -282,6 +291,7 @@ bool cs_support(int query); @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum for detailed error). */ +CAPSTONE_EXPORT cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle); /* @@ -298,6 +308,7 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle); @return CS_ERR_OK on success, or other value on failure (refer to cs_err enum for detailed error). */ +CAPSTONE_EXPORT cs_err cs_close(csh *handle); /* @@ -314,6 +325,7 @@ cs_err cs_close(csh *handle); so that cs_option(handle, CS_OPT_MEM, value) can (i.e must) be called even before cs_open() */ +CAPSTONE_EXPORT cs_err cs_option(csh handle, cs_opt_type type, size_t value); /* @@ -324,6 +336,7 @@ cs_err cs_option(csh handle, cs_opt_type type, size_t value); @return: error code of cs_err enum type (CS_ERR_*, see above) */ +CAPSTONE_EXPORT cs_err cs_errno(csh handle); @@ -335,6 +348,7 @@ cs_err cs_errno(csh handle); @return: returns a pointer to a string that describes the error code passed in the argument @code */ +CAPSTONE_EXPORT const char *cs_strerror(cs_err code); /* @@ -358,6 +372,7 @@ const char *cs_strerror(cs_err code); On failure, call cs_errno() for error code. */ +CAPSTONE_EXPORT size_t cs_disasm_ex(csh handle, const uint8_t *code, size_t code_size, uint64_t address, @@ -370,6 +385,7 @@ size_t cs_disasm_ex(csh handle, @insn: pointer returned by @insn argument in cs_disasm_ex() @count: number of cs_insn structures returned by cs_disasm_ex() */ +CAPSTONE_EXPORT void cs_free(cs_insn *insn, size_t count); /* @@ -384,6 +400,7 @@ void cs_free(cs_insn *insn, size_t count); @reg: register id @return: string name of the register, or NULL if @reg_id is invalid. */ +CAPSTONE_EXPORT const char *cs_reg_name(csh handle, unsigned int reg_id); /* @@ -398,6 +415,7 @@ const char *cs_reg_name(csh handle, unsigned int reg_id); @return: string name of the instruction, or NULL if @insn_id is invalid. */ +CAPSTONE_EXPORT const char *cs_insn_name(csh handle, unsigned int insn_id); /* @@ -416,6 +434,7 @@ const char *cs_insn_name(csh handle, unsigned int insn_id); @return: true if this instruction indeed belongs to aboved group, or false otherwise. */ +CAPSTONE_EXPORT bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id); /* @@ -433,6 +452,7 @@ bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id); @return: true if this instruction indeed implicitly used aboved register, or false otherwise. */ +CAPSTONE_EXPORT bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id); /* @@ -450,6 +470,7 @@ bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id); @return: true if this instruction indeed implicitly modified aboved register, or false otherwise. */ +CAPSTONE_EXPORT bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id); /* @@ -465,6 +486,7 @@ bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id); @return: number of operands of given type @op_type in instruction @insn, or -1 on failure. */ +CAPSTONE_EXPORT int cs_op_count(csh handle, cs_insn *insn, unsigned int op_type); /* @@ -483,6 +505,7 @@ int cs_op_count(csh handle, cs_insn *insn, unsigned int op_type); @return: index of operand of given type @op_type in arch.op_info[] array in instruction @insn, or -1 on failure. */ +CAPSTONE_EXPORT int cs_op_index(csh handle, cs_insn *insn, unsigned int op_type, unsigned int position);