Skip to content

Commit

Permalink
OS X kernel support (see capstone-engine#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
07151129 committed Nov 10, 2015
1 parent ae7c484 commit b8741bd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ endif

ifeq ($(CAPSTONE_HAS_OSXKERNEL), yes)
CFLAGS += -DCAPSTONE_HAS_OSXKERNEL
SDKROOT ?= $(shell xcodebuild -version -sdk macosx Path)
CFLAGS += -mmacosx-version-min=10.5 \
-isysroot$(SDKROOT) \
-I$(SDKROOT)/System/Library/Frameworks/Kernel.framework/Headers \
-mkernel \
-fno-builtin
endif

CFLAGS += $(foreach arch,$(LIBARCHS),-arch $(arch))
Expand Down
7 changes: 7 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ CAPSTONE_STATIC ?= yes
# a shared library.

CAPSTONE_SHARED ?= yes

################################################################################
# Change 'CAPSTONE_HAS_OSXKERNEL = no' to 'CAPSTONE_HAS_OSXKERNEL = yes' to
# enable OS X kernel embedding support. If 'CAPSTONE_USE_SYS_DYN_MEM = yes',
# then kern_os_* functions are used for memory management.

CAPSTONE_HAS_OSXKERNEL ?= no
17 changes: 17 additions & 0 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,29 @@ static void archs_enable(void)
unsigned int all_arch = 0;

#ifdef CAPSTONE_USE_SYS_DYN_MEM
#ifndef CAPSTONE_HAS_OSXKERNEL
cs_malloc_t cs_mem_malloc = malloc;
cs_calloc_t cs_mem_calloc = calloc;
cs_realloc_t cs_mem_realloc = realloc;
cs_free_t cs_mem_free = free;
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
#else
extern void* kern_os_malloc(size_t size);
extern void kern_os_free(void* addr);
extern void* kern_os_realloc(void* addr, size_t nsize);

static void* cs_kern_os_calloc(size_t num, size_t size)
{
return kern_os_malloc(num * size); // malloc bzeroes the buffer
}

cs_malloc_t cs_mem_malloc = kern_os_malloc;
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
cs_realloc_t cs_mem_realloc = kern_os_realloc;
cs_free_t cs_mem_free = kern_os_free;
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
#endif
#else
cs_malloc_t cs_mem_malloc = NULL;
cs_calloc_t cs_mem_calloc = NULL;
cs_realloc_t cs_mem_realloc = NULL;
Expand Down
1 change: 1 addition & 0 deletions make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ case "$TARGET" in
"ios_armv7" ) build_iOS armv7 $*;;
"ios_armv7s" ) build_iOS armv7s $*;;
"ios_arm64" ) build_iOS arm64 $*;;
"osx-kernel" ) CAPSTONE_USE_SYS_DYN_MEM=yes CAPSTONE_HAS_OSXKERNEL=yes CAPSTONE_ARCHS=x86 CAPSTONE_SHARED=no CAPSTONE_BUILD_CORE_ONLY=yes build $*;;
* ) echo "Usage: make.sh [nix32|cross-win32|cross-win64|cygwin-mingw32|cygwin-mingw64|ios|ios_armv7|ios_armv7s|ios_arm64|cross-android arm|cross-android arm64|clang|gcc|install|uninstall]"; exit 1;;
esac

0 comments on commit b8741bd

Please sign in to comment.