From 86ad3d6a678571aa11876fc0c7a0f8598d405c71 Mon Sep 17 00:00:00 2001 From: Pb Date: Thu, 5 Nov 2015 18:08:25 +0100 Subject: [PATCH 1/3] Support of xnu memory allocator --- cs.c | 16 ++++++++++++++++ make.sh | 1 + 2 files changed, 17 insertions(+) diff --git a/cs.c b/cs.c index 9d745cfe8..5f755571f 100644 --- a/cs.c +++ b/cs.c @@ -83,12 +83,28 @@ 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* 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 = 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; diff --git a/make.sh b/make.sh index d8d5733c4..bd2edeb4e 100755 --- a/make.sh +++ b/make.sh @@ -143,6 +143,7 @@ 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 build $*;; * ) echo "Usage: $0 ["`grep '^ "' $0 | cut -d '"' -f 2 | tr "\\n" "|"`"]" exit 1;; From fce49770148238dcb045f05283175c6a140a6c7c Mon Sep 17 00:00:00 2001 From: Pb Date: Thu, 5 Nov 2015 22:59:45 +0100 Subject: [PATCH 2/3] Added CAPSTONE_HAS_OSXKERNEL to config.mk --- config.mk | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config.mk b/config.mk index 10304d3db..a954c6fb5 100644 --- a/config.mk +++ b/config.mk @@ -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 From 2769c772ce7dbd1f7d2beb2059258ae129083f7b Mon Sep 17 00:00:00 2001 From: Pb Date: Fri, 6 Nov 2015 14:44:55 +0100 Subject: [PATCH 3/3] Correct build settings for xnu kext. Changed calloc name to avoid potential conflicts --- Makefile | 6 ++++++ cs.c | 4 ++-- make.sh | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 18b630c88..136656cbc 100644 --- a/Makefile +++ b/Makefile @@ -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)) diff --git a/cs.c b/cs.c index 5f755571f..36627298f 100644 --- a/cs.c +++ b/cs.c @@ -94,12 +94,12 @@ 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* kern_os_calloc(size_t num, size_t size) { +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 = kern_os_calloc; +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; diff --git a/make.sh b/make.sh index bd2edeb4e..ae504ea02 100755 --- a/make.sh +++ b/make.sh @@ -143,7 +143,7 @@ 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 build $*;; + "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: $0 ["`grep '^ "' $0 | cut -d '"' -f 2 | tr "\\n" "|"`"]" exit 1;;