diff options
-rw-r--r-- | Makefile | 24 | ||||
-rw-r--r-- | lib/find-syms.c | 4 | ||||
-rw-r--r-- | lib/substitute-internal.h | 10 | ||||
-rw-r--r-- | lib/substitute.h | 11 | ||||
-rw-r--r-- | lib/substrate-compat.c | 55 | ||||
-rw-r--r-- | substrate/substrate.h | 292 | ||||
-rw-r--r-- | test/test-find-syms.c | 2 | ||||
-rw-r--r-- | test/test-substrate.cpp | 23 |
8 files changed, 411 insertions, 10 deletions
@@ -1,5 +1,9 @@ # todo -CC := clang -O3 -Wall -Wextra -Werror -arch x86_64 +CC := clang +CXX := clang++ +CFLAGS := -O3 -Wall -Wextra -Werror -arch x86_64 +override CC := $(CC) $(CFLAGS) +override CXX := $(CXX) $(CFLAGS) IMAON2 := /Users/comex/c/imaon2 GEN_JS := node $(IMAON2)/tables/gen.js @@ -10,20 +14,26 @@ all: \ generated/transform-dis-arm.inc \ generated/transform-dis-arm64.inc \ out/libsubstitute.dylib \ - test/test-find-syms + test/test-find-syms \ + test/test-find-syms-cpp \ + test/test-substrate out: mkdir out out/%.o: lib/%.c Makefile out - $(CC) -MMD -c -o $@ $< + $(CC) -fvisibility=hidden -std=c11 -c -o $@ $< -LIB_OBJS := out/find-syms.o -out/libsubstitute.dylib: $(LIB_OBJS) out - $(CC) -dynamiclib -o $@ $(LIB_OBJS) +LIB_OBJS := out/find-syms.o out/substrate-compat.o +out/libsubstitute.dylib: $(LIB_OBJS) lib/*.h out + $(CC) -dynamiclib -fvisibility=hidden -o $@ $(LIB_OBJS) test/test-%: test/test-%.c Makefile out/libsubstitute.dylib - $(CC) -o $@ $< -Ilib -Lout -lsubstitute + $(CC) -std=c89 -o $@ $< -Ilib -Lout -lsubstitute +test/test-%-cpp: test/test-%.c Makefile out/libsubstitute.dylib + $(CXX) -x c++ -std=c++98 -o $@ $< -Ilib -Lout -lsubstitute +test/test-%: test/test-%.cpp Makefile out/libsubstitute.dylib + $(CXX) -std=c++11 -o $@ $< -Ilib -Isubstrate -Lout -lsubstitute generated: Makefile rm -rf generated diff --git a/lib/find-syms.c b/lib/find-syms.c index 5e58cb4..78421c5 100644 --- a/lib/find-syms.c +++ b/lib/find-syms.c @@ -115,6 +115,7 @@ static void inspect_dyld() { } /* 'dlopen_header' keeps the image alive */ +EXPORT struct substitute_image *substitute_open_image(const char *filename) { pthread_once(&dyld_inspect_once, inspect_dyld); @@ -134,17 +135,20 @@ struct substitute_image *substitute_open_image(const char *filename) { return im; } +EXPORT void substitute_close_image(struct substitute_image *im) { dlclose(im->dlhandle); /* ignore errors */ free(im); } +EXPORT int substitute_find_private_syms(struct substitute_image *im, const char **names, substitute_sym **syms, size_t count) { find_syms_raw(im->image_header, &im->slide, names, syms, count); return SUBSTITUTE_OK; } +EXPORT void *substitute_sym_to_ptr(struct substitute_image *handle, substitute_sym *sym) { return sym_to_ptr(sym, handle->slide); } diff --git a/lib/substitute-internal.h b/lib/substitute-internal.h new file mode 100644 index 0000000..cbb7462 --- /dev/null +++ b/lib/substitute-internal.h @@ -0,0 +1,10 @@ +#pragma once + +#include <stdio.h> +#define panic(...) do { \ + fprintf(stderr, __VA_ARGS__); \ + abort(); \ + __builtin_unreachable(); \ +} while(0) + +#define EXPORT __attribute__ ((visibility("default"))) diff --git a/lib/substitute.h b/lib/substitute.h index 20dc020..a7fdd31 100644 --- a/lib/substitute.h +++ b/lib/substitute.h @@ -8,7 +8,11 @@ #include <stdlib.h> #include <stdint.h> -// TODO add numbers +#ifdef __cplusplus +extern "C" { +#endif + +/* TODO add numbers */ enum { SUBSTITUTE_OK = 0, }; @@ -42,7 +46,6 @@ struct substitute_image { * @filename the executable/library path (c.f. dyld(3) on Darwin) * @return a handle, or NULL if the image wasn't found */ - struct substitute_image *substitute_open_image(const char *filename); /* Release a handle opened with substitute_open_image. @@ -76,3 +79,7 @@ int substitute_find_private_syms(struct substitute_image *handle, void *substitute_sym_to_ptr(struct substitute_image *handle, substitute_sym *sym); #endif /* 1 */ + +#ifdef __cplusplus +} /* extern */ +#endif diff --git a/lib/substrate-compat.c b/lib/substrate-compat.c new file mode 100644 index 0000000..6bbe316 --- /dev/null +++ b/lib/substrate-compat.c @@ -0,0 +1,55 @@ +#include "substitute.h" +#include "substitute-internal.h" +#include <syslog.h> +#include <mach-o/dyld.h> + +EXPORT +void *SubGetImageByName(const char *filename) __asm__("SubGetImageByName"); +void *SubGetImageByName(const char *filename) { + return substitute_open_image(filename); +} + +EXPORT +void *SubFindSymbol(void *image, const char *name) __asm__("SubFindSymbol"); +void *SubFindSymbol(void *image, const char *name) { + if (!image) { + const char *s = "SubFindSymbol: 'any image' specified, which is incredibly slow - like, 2ms. I'm going to do it since it seems to be somewhat common, but you should be ashamed of yourself."; + syslog(LOG_WARNING, "%s", s); + fprintf(stderr, "%s\n", s); + /* and it isn't thread safe, but neither is MS */ + for(uint32_t i = 0; i < _dyld_image_count(); i++) { + const char *im_name = _dyld_get_image_name(i); + struct substitute_image *im = substitute_open_image(im_name); + if (!im) { + fprintf(stderr, "(btw, couldn't open %s?)\n", im_name); + continue; + } + void *r = SubFindSymbol(im, name); + substitute_close_image(im); + if (r) + return r; + } + return NULL; + } + + substitute_sym *sym; + if (substitute_find_private_syms(image, &name, &sym, 1) || !sym) + return NULL; + return substitute_sym_to_ptr(image, sym); +} + +/* +EXPORT +void SubHookFunction(void *symbol, void *replace, void **result) __asm__("SubHookFunction"); +void SubHookFunction(void *symbol, void *replace, void **result) { + // ... +} +*/ + +#ifdef __APPLE__ +/*void SubHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result) __asm__("SubHookMessageEx"); +void SubHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result) { + +}*/ + +#endif diff --git a/substrate/substrate.h b/substrate/substrate.h new file mode 100644 index 0000000..f1b8003 --- /dev/null +++ b/substrate/substrate.h @@ -0,0 +1,292 @@ +/* Cydia Substrate - Powerful Code Insertion Platform + * Copyright (C) 2008-2011 Jay Freeman (saurik) +*/ + +/* GNU Lesser General Public License, Version 3 {{{ */ +/* + * Substrate is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Substrate is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Substrate. If not, see <http://www.gnu.org/licenses/>. +**/ +/* }}} */ + +#ifndef SUBSTRATE_H_ +#define SUBSTRATE_H_ + +#ifdef __APPLE__ +#ifdef __cplusplus +extern "C" { +#endif +#include <mach-o/nlist.h> +#ifdef __cplusplus +} +#endif + +#include <objc/runtime.h> +#include <objc/message.h> +#endif + +#include <dlfcn.h> +#include <stdlib.h> + +#define _finline \ + inline __attribute__((__always_inline__)) +#define _disused \ + __attribute__((__unused__)) + +#define _extern \ + extern "C" __attribute__((__visibility__("default"))) + +#ifdef __cplusplus +#define _default(value) = value +#else +#define _default(value) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef const void *MSImageRef; + +MSImageRef MSGetImageByName(const char *file) +__asm__("SubGetImageByName"); +void *MSFindSymbol(MSImageRef image, const char *name) +__asm__("SubFindSymbol"); + +void MSHookFunction(void *symbol, void *replace, void **result) +__asm__("SubHookFunction"); + +#ifdef __APPLE__ +void MSHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result) +__asm__("SubHookMessageEx"); +#endif + +#ifdef __cplusplus +} +#endif + +#ifdef __cplusplus + +#ifdef __APPLE__ + +namespace etl { + +template <unsigned Case_> +struct Case { + static char value[Case_ + 1]; +}; + +typedef Case<true> Yes; +typedef Case<false> No; + +namespace be { + template <typename Checked_> + static Yes CheckClass_(void (Checked_::*)()); + + template <typename Checked_> + static No CheckClass_(...); +} + +template <typename Type_> +struct IsClass { + void gcc32(); + + static const bool value = (sizeof(be::CheckClass_<Type_>(0).value) == sizeof(Yes::value)); +}; + +} + +#ifdef __arm__ +template <typename Type_> +__attribute__((__deprecated__)) +static inline Type_ *MSHookMessage(Class _class, SEL sel, Type_ *imp, const char *prefix = NULL) { + return reinterpret_cast<Type_ *>(MSHookMessage(_class, sel, reinterpret_cast<IMP>(imp), prefix)); +} +#endif + +template <typename Type_> +static inline void MSHookMessage(Class _class, SEL sel, Type_ *imp, Type_ **result) { + return MSHookMessageEx(_class, sel, reinterpret_cast<IMP>(imp), reinterpret_cast<IMP *>(result)); +} + +template <typename Type_> +static inline Type_ &MSHookIvar(id self, const char *name) { + Ivar ivar(class_getInstanceVariable(object_getClass(self), name)); + void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>(self) + ivar_getOffset(ivar)); + return *reinterpret_cast<Type_ *>(pointer); +} + +#define MSAddMessage0(_class, type, arg0) \ + class_addMethod($ ## _class, @selector(arg0), (IMP) &$ ## _class ## $ ## arg0, type); +#define MSAddMessage1(_class, type, arg0) \ + class_addMethod($ ## _class, @selector(arg0:), (IMP) &$ ## _class ## $ ## arg0 ## $, type); +#define MSAddMessage2(_class, type, arg0, arg1) \ + class_addMethod($ ## _class, @selector(arg0:arg1:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $, type); +#define MSAddMessage3(_class, type, arg0, arg1, arg2) \ + class_addMethod($ ## _class, @selector(arg0:arg1:arg2:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $, type); +#define MSAddMessage4(_class, type, arg0, arg1, arg2, arg3) \ + class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $, type); +#define MSAddMessage5(_class, type, arg0, arg1, arg2, arg3, arg4) \ + class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $, type); +#define MSAddMessage6(_class, type, arg0, arg1, arg2, arg3, arg4, arg5) \ + class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $, type); + +#define MSHookMessage0(_class, arg0) \ + MSHookMessage($ ## _class, @selector(arg0), MSHake(_class ## $ ## arg0)) +#define MSHookMessage1(_class, arg0) \ + MSHookMessage($ ## _class, @selector(arg0:), MSHake(_class ## $ ## arg0 ## $)) +#define MSHookMessage2(_class, arg0, arg1) \ + MSHookMessage($ ## _class, @selector(arg0:arg1:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $)) +#define MSHookMessage3(_class, arg0, arg1, arg2) \ + MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $)) +#define MSHookMessage4(_class, arg0, arg1, arg2, arg3) \ + MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $)) +#define MSHookMessage5(_class, arg0, arg1, arg2, arg3, arg4) \ + MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $)) +#define MSHookMessage6(_class, arg0, arg1, arg2, arg3, arg4, arg5) \ + MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $)) + +#define MSRegister_(name, dollar, colon) \ + static class C_$ ## name ## $ ## dollar { public: _finline C_$ ## name ## $ ##dollar() { \ + MSHookMessage($ ## name, @selector(colon), MSHake(name ## $ ## dollar)); \ + } } V_$ ## name ## $ ## dollar; \ + +#define MSIgnore_(name, dollar, colon) + +#define MSMessage_(extra, type, _class, name, dollar, colon, call, args...) \ + static type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args); \ + MSHook(type, name ## $ ## dollar, _class self, SEL _cmd, ## args) { \ + Class const _cls($ ## name); \ + type (* const _old)(_class, SEL, ## args, ...) = reinterpret_cast<type (* const)(_class, SEL, ## args, ...)>(_ ## name ## $ ## dollar); \ + typedef type (*msgSendSuper_t)(struct objc_super *, SEL, ## args, ...); \ + msgSendSuper_t const _spr(::etl::IsClass<type>::value ? reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper_stret) : reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper)); \ + return _$ ## name ## $ ## dollar call; \ + } \ + extra(name, dollar, colon) \ + static _finline type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args) + +/* for((x=1;x!=7;++x)){ echo -n "#define MSMessage${x}_(extra, type, _class, name";for((y=0;y!=x;++y));do echo -n ", sel$y";done;for((y=0;y!=x;++y));do echo -n ", type$y, arg$y";done;echo ") \\";echo -n " MSMessage_(extra, type, _class, name,";for((y=0;y!=x;++y));do if [[ $y -ne 0 ]];then echo -n " ##";fi;echo -n " sel$y ## $";done;echo -n ", ";for((y=0;y!=x;++y));do echo -n "sel$y:";done;echo -n ", (_cls, _old, _spr, self, _cmd";for((y=0;y!=x;++y));do echo -n ", arg$y";done;echo -n ")";for((y=0;y!=x;++y));do echo -n ", type$y arg$y";done;echo ")";} */ + +#define MSMessage0_(extra, type, _class, name, sel0) \ + MSMessage_(extra, type, _class, name, sel0, sel0, (_cls, _old, _spr, self, _cmd)) +#define MSMessage1_(extra, type, _class, name, sel0, type0, arg0) \ + MSMessage_(extra, type, _class, name, sel0 ## $, sel0:, (_cls, _old, _spr, self, _cmd, arg0), type0 arg0) +#define MSMessage2_(extra, type, _class, name, sel0, sel1, type0, arg0, type1, arg1) \ + MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $, sel0:sel1:, (_cls, _old, _spr, self, _cmd, arg0, arg1), type0 arg0, type1 arg1) +#define MSMessage3_(extra, type, _class, name, sel0, sel1, sel2, type0, arg0, type1, arg1, type2, arg2) \ + MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $ ## sel2 ## $, sel0:sel1:sel2:, (_cls, _old, _spr, self, _cmd, arg0, arg1, arg2), type0 arg0, type1 arg1, type2 arg2) +#define MSMessage4_(extra, type, _class, name, sel0, sel1, sel2, sel3, type0, arg0, type1, arg1, type2, arg2, type3, arg3) \ + MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $ ## sel2 ## $ ## sel3 ## $, sel0:sel1:sel2:sel3:, (_cls, _old, _spr, self, _cmd, arg0, arg1, arg2, arg3), type0 arg0, type1 arg1, type2 arg2, type3 arg3) +#define MSMessage5_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \ + MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $ ## sel2 ## $ ## sel3 ## $ ## sel4 ## $, sel0:sel1:sel2:sel3:sel4:, (_cls, _old, _spr, self, _cmd, arg0, arg1, arg2, arg3, arg4), type0 arg0, type1 arg1, type2 arg2, type3 arg3, type4 arg4) +#define MSMessage6_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, sel5, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \ + MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $ ## sel2 ## $ ## sel3 ## $ ## sel4 ## $ ## sel5 ## $, sel0:sel1:sel2:sel3:sel4:sel5:, (_cls, _old, _spr, self, _cmd, arg0, arg1, arg2, arg3, arg4, arg5), type0 arg0, type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5) + +#define MSInstanceMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, _class *, _class, ## args) +#define MSInstanceMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, _class *, _class, ## args) +#define MSInstanceMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, _class *, _class, ## args) +#define MSInstanceMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, _class *, _class, ## args) +#define MSInstanceMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, _class *, _class, ## args) +#define MSInstanceMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, _class *, _class, ## args) +#define MSInstanceMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, _class *, _class, ## args) + +#define MSClassMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, Class, $ ## _class, ## args) +#define MSClassMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, Class, $ ## _class, ## args) +#define MSClassMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, Class, $ ## _class, ## args) +#define MSClassMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, Class, $ ## _class, ## args) +#define MSClassMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, Class, $ ## _class, ## args) +#define MSClassMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, Class, $ ## _class, ## args) +#define MSClassMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, Class, $ ## _class, ## args) + +#define MSInstanceMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, _class *, _class, ## args) +#define MSInstanceMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, _class *, _class, ## args) +#define MSInstanceMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, _class *, _class, ## args) +#define MSInstanceMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, _class *, _class, ## args) +#define MSInstanceMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, _class *, _class, ## args) +#define MSInstanceMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, _class *, _class, ## args) +#define MSInstanceMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, _class *, _class, ## args) + +#define MSClassMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, Class, $ ## _class, ## args) +#define MSClassMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, Class, $ ## _class, ## args) +#define MSClassMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, Class, $ ## _class, ## args) +#define MSClassMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, Class, $ ## _class, ## args) +#define MSClassMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, Class, $ ## _class, ## args) +#define MSClassMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, Class, $ ## _class, ## args) +#define MSClassMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, Class, $ ## _class, ## args) + +#define MSOldCall(args...) \ + _old(self, _cmd, ## args) +#define MSSuperCall(args...) \ + _spr(& (struct objc_super) {self, class_getSuperclass(_cls)}, _cmd, ## args) + +#define MSIvarHook(type, name) \ + type &name(MSHookIvar<type>(self, #name)) + +#define MSClassHook(name) \ + @class name; \ + static Class $ ## name = objc_getClass(#name); +#define MSMetaClassHook(name) \ + @class name; \ + static Class $$ ## name = object_getClass($ ## name); + +#endif/*__APPLE__*/ + +template <typename Type_> +static inline void MSHookFunction(Type_ *symbol, Type_ *replace, Type_ **result) { + return MSHookFunction( + reinterpret_cast<void *>(symbol), + reinterpret_cast<void *>(replace), + reinterpret_cast<void **>(result) + ); +} + +template <typename Type_> +static inline void MSHookFunction(Type_ *symbol, Type_ *replace) { + return MSHookFunction(symbol, replace, reinterpret_cast<Type_ **>(NULL)); +} + +template <typename Type_> +static inline void MSHookSymbol(Type_ *&value, const char *name, MSImageRef image = NULL) { + value = reinterpret_cast<Type_ *>(MSFindSymbol(image, name)); +} + +template <typename Type_> +static inline void MSHookFunction(const char *name, Type_ *replace, Type_ **result = NULL) { + Type_ *symbol; + MSHookSymbol(symbol, name); + return MSHookFunction(symbol, replace, result); +} + +#endif + +#define MSHook(type, name, args...) \ + _disused static type (*_ ## name)(args); \ + static type $ ## name(args) + +#ifdef __cplusplus +#define MSHake(name) \ + &$ ## name, &_ ## name +#else +#define MSHake(name) \ + &$ ## name, (void **) &_ ## name +#endif + +#define MSInitialize \ + __attribute__((__constructor__)) static void _MSInitialize(void) + +#define Foundation_f "/System/Library/Frameworks/Foundation.framework/Foundation" +#define UIKit_f "/System/Library/Frameworks/UIKit.framework/UIKit" +#define JavaScriptCore_f "/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore" +#define IOKit_f "/System/Library/Frameworks/IOKit.framework/IOKit" + +#endif//SUBSTRATE_H_ diff --git a/test/test-find-syms.c b/test/test-find-syms.c index 6a8d600..acd31ba 100644 --- a/test/test-find-syms.c +++ b/test/test-find-syms.c @@ -13,7 +13,7 @@ int main() { assert(!substitute_find_private_syms(im, names, syms, 1)); assert(syms[0]); - int (*f)(int) = substitute_sym_to_ptr(im, syms[0]); + int (*f)(int) = (int (*)(int)) substitute_sym_to_ptr(im, syms[0]); assert(f(12345) < 0); substitute_close_image(im); diff --git a/test/test-substrate.cpp b/test/test-substrate.cpp new file mode 100644 index 0000000..f30918e --- /dev/null +++ b/test/test-substrate.cpp @@ -0,0 +1,23 @@ +#include <substrate.h> +#include <stdio.h> +#include <assert.h> +#include <dlfcn.h> +#include <time.h> + +int main() { + const char *foundation = "/System/Library/Frameworks/Foundation.framework/Foundation"; + dlopen(foundation, RTLD_LAZY); + + MSImageRef im = MSGetImageByName(foundation); + assert(im); + + int (*f)(int); + MSHookSymbol(f, "_absolute_from_gregorian", im); + assert(f(12345) < 0); + clock_t a = clock(); + MSHookSymbol(f, "_absolute_from_gregorian"); + clock_t b = clock(); + printf("%ld\n", (long) (b - a)); + assert(f(12345) < 0); +} + |