substrate.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Cydia Substrate - Powerful Code Insertion Platform
  2. * Copyright (C) 2008-2012 Jay Freeman (saurik)
  3. */
  4. /* GNU Lesser General Public License, Version 3 {{{ */
  5. /*
  6. * Substrate is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Substrate is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  14. * License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Substrate. If not, see <http://www.gnu.org/licenses/>.
  18. **/
  19. /* }}} */
  20. #ifndef SUBSTRATE_H_
  21. #define SUBSTRATE_H_
  22. #ifdef __APPLE__
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #include <mach-o/nlist.h>
  27. #ifdef __cplusplus
  28. }
  29. #endif
  30. #include <objc/runtime.h>
  31. #include <objc/message.h>
  32. #endif
  33. #include <dlfcn.h>
  34. #include <stdlib.h>
  35. #define _finline \
  36. inline __attribute__((__always_inline__))
  37. #define _disused \
  38. __attribute__((__unused__))
  39. #define _extern \
  40. extern "C" __attribute__((__visibility__("default")))
  41. #ifdef __cplusplus
  42. #define _default(value) = value
  43. #else
  44. #define _default(value)
  45. #endif
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. bool MSHookProcess(pid_t pid, const char *library);
  50. typedef const void *MSImageRef;
  51. MSImageRef MSGetImageByName(const char *file);
  52. void *MSFindSymbol(MSImageRef image, const char *name);
  53. void MSHookFunction(void *symbol, void *replace, void **result);
  54. #ifdef __APPLE__
  55. #ifdef __arm__
  56. __attribute__((__deprecated__))
  57. IMP MSHookMessage(Class _class, SEL sel, IMP imp, const char *prefix _default(NULL));
  58. #endif
  59. void MSHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result);
  60. #endif
  61. #ifdef SubstrateInternal
  62. typedef void *SubstrateAllocatorRef;
  63. typedef struct __SubstrateProcess *SubstrateProcessRef;
  64. typedef struct __SubstrateMemory *SubstrateMemoryRef;
  65. SubstrateProcessRef SubstrateProcessCreate(SubstrateAllocatorRef allocator, pid_t pid);
  66. void SubstrateProcessRelease(SubstrateProcessRef process);
  67. SubstrateMemoryRef SubstrateMemoryCreate(SubstrateAllocatorRef allocator, SubstrateProcessRef process, void *data, size_t size);
  68. void SubstrateMemoryRelease(SubstrateMemoryRef memory);
  69. #endif
  70. #ifdef __ANDROID__
  71. #include <jni.h>
  72. _extern void MSJavaHookClassLoad(JNIEnv *jni, const char *name, void (*callback)(void *, JNIEnv *, jclass), void *data _default(NULL));
  73. _extern void MSJavaHookMethod(JNIEnv *jni, jclass _class, jmethodID methodID, void *function, void **result);
  74. _extern void MSJavaBlessClassLoader(JNIEnv *jni, jobject loader);
  75. typedef struct MSJavaObjectKey_ *MSJavaObjectKey;
  76. _extern MSJavaObjectKey MSJavaNewObjectKey();
  77. _extern void *MSJavaGetObjectKey(JNIEnv *jni, jobject object, MSJavaObjectKey key);
  78. _extern void MSJavaSetObjectKey(JNIEnv *jni, jobject object, MSJavaObjectKey key, void *value, void (*clean)(void *, JNIEnv *, void *) _default(NULL), void *data _default(NULL));
  79. #endif
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #ifdef __cplusplus
  84. #ifdef SubstrateInternal
  85. struct SubstrateHookMemory {
  86. SubstrateMemoryRef handle_;
  87. SubstrateHookMemory(SubstrateProcessRef process, void *data, size_t size) :
  88. handle_(SubstrateMemoryCreate(NULL, NULL, data, size))
  89. {
  90. }
  91. ~SubstrateHookMemory() {
  92. if (handle_ != NULL)
  93. SubstrateMemoryRelease(handle_);
  94. }
  95. };
  96. #endif
  97. #ifdef __APPLE__
  98. namespace etl {
  99. template <unsigned Case_>
  100. struct Case {
  101. static char value[Case_ + 1];
  102. };
  103. typedef Case<true> Yes;
  104. typedef Case<false> No;
  105. namespace be {
  106. template <typename Checked_>
  107. static Yes CheckClass_(void (Checked_::*)());
  108. template <typename Checked_>
  109. static No CheckClass_(...);
  110. }
  111. template <typename Type_>
  112. struct IsClass {
  113. void gcc32();
  114. static const bool value = (sizeof(be::CheckClass_<Type_>(0).value) == sizeof(Yes::value));
  115. };
  116. }
  117. #ifdef __arm__
  118. template <typename Type_>
  119. __attribute__((__deprecated__))
  120. static inline Type_ *MSHookMessage(Class _class, SEL sel, Type_ *imp, const char *prefix = NULL) {
  121. return reinterpret_cast<Type_ *>(MSHookMessage(_class, sel, reinterpret_cast<IMP>(imp), prefix));
  122. }
  123. #endif
  124. template <typename Type_>
  125. static inline void MSHookMessage(Class _class, SEL sel, Type_ *imp, Type_ **result) {
  126. return MSHookMessageEx(_class, sel, reinterpret_cast<IMP>(imp), reinterpret_cast<IMP *>(result));
  127. }
  128. template <typename Type_>
  129. static inline Type_ &MSHookIvar(id self, const char *name) {
  130. Ivar ivar(class_getInstanceVariable(object_getClass(self), name));
  131. #if __has_feature(objc_arc)
  132. void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>((__bridge void *)self) + ivar_getOffset(ivar));
  133. #else
  134. void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>(self) + ivar_getOffset(ivar));
  135. #endif
  136. return *reinterpret_cast<Type_ *>(pointer);
  137. }
  138. #define MSAddMessage0(_class, type, arg0) \
  139. class_addMethod($ ## _class, @selector(arg0), (IMP) &$ ## _class ## $ ## arg0, type);
  140. #define MSAddMessage1(_class, type, arg0) \
  141. class_addMethod($ ## _class, @selector(arg0:), (IMP) &$ ## _class ## $ ## arg0 ## $, type);
  142. #define MSAddMessage2(_class, type, arg0, arg1) \
  143. class_addMethod($ ## _class, @selector(arg0:arg1:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $, type);
  144. #define MSAddMessage3(_class, type, arg0, arg1, arg2) \
  145. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $, type);
  146. #define MSAddMessage4(_class, type, arg0, arg1, arg2, arg3) \
  147. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $, type);
  148. #define MSAddMessage5(_class, type, arg0, arg1, arg2, arg3, arg4) \
  149. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $, type);
  150. #define MSAddMessage6(_class, type, arg0, arg1, arg2, arg3, arg4, arg5) \
  151. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $, type);
  152. #define MSAddMessage7(_class, type, arg0, arg1, arg2, arg3, arg4, arg5, arg6) \
  153. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ $$ arg6 ## $, type);
  154. #define MSAddMessage8(_class, type, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  155. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:arg7:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ $$ arg6 ## $ ## arg7 ## $, type);
  156. #define MSHookMessage0(_class, arg0) \
  157. MSHookMessage($ ## _class, @selector(arg0), MSHake(_class ## $ ## arg0))
  158. #define MSHookMessage1(_class, arg0) \
  159. MSHookMessage($ ## _class, @selector(arg0:), MSHake(_class ## $ ## arg0 ## $))
  160. #define MSHookMessage2(_class, arg0, arg1) \
  161. MSHookMessage($ ## _class, @selector(arg0:arg1:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $))
  162. #define MSHookMessage3(_class, arg0, arg1, arg2) \
  163. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $))
  164. #define MSHookMessage4(_class, arg0, arg1, arg2, arg3) \
  165. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $))
  166. #define MSHookMessage5(_class, arg0, arg1, arg2, arg3, arg4) \
  167. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $))
  168. #define MSHookMessage6(_class, arg0, arg1, arg2, arg3, arg4, arg5) \
  169. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $))
  170. #define MSHookMessage7(_class, arg0, arg1, arg2, arg3, arg4, arg5, arg6) \
  171. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ ## arg6 ## $))
  172. #define MSHookMessage8(_class, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  173. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:arg7:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ ## arg6 ## $ ## arg7 ## $))
  174. #define MSRegister_(name, dollar, colon) \
  175. namespace { static class C_$ ## name ## $ ## dollar { public: _finline C_$ ## name ## $ ##dollar() { \
  176. MSHookMessage($ ## name, @selector(colon), MSHake(name ## $ ## dollar)); \
  177. } } V_$ ## name ## $ ## dollar; } \
  178. #define MSIgnore_(name, dollar, colon)
  179. #define MSMessage_(extra, type, _class, name, dollar, colon, call, args...) \
  180. static type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args); \
  181. MSHook(type, name ## $ ## dollar, _class self, SEL _cmd, ## args) { \
  182. Class const _cls($ ## name); \
  183. type (* const _old)(_class, SEL, ## args, ...) = reinterpret_cast<type (* const)(_class, SEL, ## args, ...)>(_ ## name ## $ ## dollar); \
  184. typedef type (*msgSendSuper_t)(struct objc_super *, SEL, ## args, ...); \
  185. msgSendSuper_t const _spr(::etl::IsClass<type>::value ? reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper_stret) : reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper)); \
  186. return _$ ## name ## $ ## dollar call; \
  187. } \
  188. extra(name, dollar, colon) \
  189. static _finline type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args)
  190. /* 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 ")";} */
  191. #define MSMessage0_(extra, type, _class, name, sel0) \
  192. MSMessage_(extra, type, _class, name, sel0, sel0, (_cls, _old, _spr, self, _cmd))
  193. #define MSMessage1_(extra, type, _class, name, sel0, type0, arg0) \
  194. MSMessage_(extra, type, _class, name, sel0 ## $, sel0:, (_cls, _old, _spr, self, _cmd, arg0), type0 arg0)
  195. #define MSMessage2_(extra, type, _class, name, sel0, sel1, type0, arg0, type1, arg1) \
  196. MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $, sel0:sel1:, (_cls, _old, _spr, self, _cmd, arg0, arg1), type0 arg0, type1 arg1)
  197. #define MSMessage3_(extra, type, _class, name, sel0, sel1, sel2, type0, arg0, type1, arg1, type2, arg2) \
  198. 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)
  199. #define MSMessage4_(extra, type, _class, name, sel0, sel1, sel2, sel3, type0, arg0, type1, arg1, type2, arg2, type3, arg3) \
  200. 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)
  201. #define MSMessage5_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
  202. 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)
  203. #define MSMessage6_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, sel5, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \
  204. 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)
  205. #define MSMessage7_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, sel5, sel6, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6) \
  206. MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $ ## sel2 ## $ ## sel3 ## $ ## sel4 ## $ ## sel5 ## $ ## sel6 ## $, sel0:sel1:sel2:sel3:sel4:sel5:sel6:, (_cls, _old, _spr, self, _cmd, arg0, arg1, arg2, arg3, arg4, arg5, arg6), type0 arg0, type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6)
  207. #define MSMessage8_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, sel5, sel6, sel7, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5, type6, arg6, type7, arg7) \
  208. MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $ ## sel2 ## $ ## sel3 ## $ ## sel4 ## $ ## sel5 ## $ ## sel6 ## $ ## sel7 ## $, sel0:sel1:sel2:sel3:sel4:sel5:sel6:sel7:, (_cls, _old, _spr, self, _cmd, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7), type0 arg0, type1 arg1, type2 arg2, type3 arg3, type4 arg4, type5 arg5, type6 arg6, type7 arg7)
  209. #define MSInstanceMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, _class *, _class, ## args)
  210. #define MSInstanceMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, _class *, _class, ## args)
  211. #define MSInstanceMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, _class *, _class, ## args)
  212. #define MSInstanceMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, _class *, _class, ## args)
  213. #define MSInstanceMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, _class *, _class, ## args)
  214. #define MSInstanceMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, _class *, _class, ## args)
  215. #define MSInstanceMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, _class *, _class, ## args)
  216. #define MSInstanceMessage7(type, _class, args...) MSMessage7_(MSIgnore_, type, _class *, _class, ## args)
  217. #define MSInstanceMessage8(type, _class, args...) MSMessage8_(MSIgnore_, type, _class *, _class, ## args)
  218. #define MSClassMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, Class, $ ## _class, ## args)
  219. #define MSClassMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, Class, $ ## _class, ## args)
  220. #define MSClassMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, Class, $ ## _class, ## args)
  221. #define MSClassMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, Class, $ ## _class, ## args)
  222. #define MSClassMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, Class, $ ## _class, ## args)
  223. #define MSClassMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, Class, $ ## _class, ## args)
  224. #define MSClassMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, Class, $ ## _class, ## args)
  225. #define MSClassMessage7(type, _class, args...) MSMessage7_(MSIgnore_, type, Class, $ ## _class, ## args)
  226. #define MSClassMessage8(type, _class, args...) MSMessage8_(MSIgnore_, type, Class, $ ## _class, ## args)
  227. #define MSInstanceMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, _class *, _class, ## args)
  228. #define MSInstanceMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, _class *, _class, ## args)
  229. #define MSInstanceMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, _class *, _class, ## args)
  230. #define MSInstanceMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, _class *, _class, ## args)
  231. #define MSInstanceMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, _class *, _class, ## args)
  232. #define MSInstanceMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, _class *, _class, ## args)
  233. #define MSInstanceMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, _class *, _class, ## args)
  234. #define MSInstanceMessageHook7(type, _class, args...) MSMessage7_(MSRegister_, type, _class *, _class, ## args)
  235. #define MSInstanceMessageHook8(type, _class, args...) MSMessage8_(MSRegister_, type, _class *, _class, ## args)
  236. #define MSClassMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, Class, $ ## _class, ## args)
  237. #define MSClassMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, Class, $ ## _class, ## args)
  238. #define MSClassMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, Class, $ ## _class, ## args)
  239. #define MSClassMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, Class, $ ## _class, ## args)
  240. #define MSClassMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, Class, $ ## _class, ## args)
  241. #define MSClassMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, Class, $ ## _class, ## args)
  242. #define MSClassMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, Class, $ ## _class, ## args)
  243. #define MSClassMessageHook7(type, _class, args...) MSMessage7_(MSRegister_, type, Class, $ ## _class, ## args)
  244. #define MSClassMessageHook8(type, _class, args...) MSMessage8_(MSRegister_, type, Class, $ ## _class, ## args)
  245. #define MSOldCall(args...) \
  246. _old(self, _cmd, ## args)
  247. #define MSSuperCall(args...) \
  248. _spr(& (struct objc_super) {self, class_getSuperclass(_cls)}, _cmd, ## args)
  249. #define MSIvarHook(type, name) \
  250. type &name(MSHookIvar<type>(self, #name))
  251. #define MSClassHook(name) \
  252. @class name; \
  253. static Class $ ## name = objc_getClass(#name);
  254. #define MSMetaClassHook(name) \
  255. @class name; \
  256. static Class $$ ## name = object_getClass($ ## name);
  257. #endif/*__APPLE__*/
  258. template <typename Type_>
  259. static inline void MSHookFunction(Type_ *symbol, Type_ *replace, Type_ **result) {
  260. return MSHookFunction(
  261. reinterpret_cast<void *>(symbol),
  262. reinterpret_cast<void *>(replace),
  263. reinterpret_cast<void **>(result)
  264. );
  265. }
  266. template <typename Type_>
  267. static inline void MSHookFunction(Type_ *symbol, Type_ *replace) {
  268. return MSHookFunction(symbol, replace, reinterpret_cast<Type_ **>(NULL));
  269. }
  270. template <typename Type_>
  271. static inline void MSHookSymbol(Type_ *&value, const char *name, MSImageRef image = NULL) {
  272. value = reinterpret_cast<Type_ *>(MSFindSymbol(image, name));
  273. }
  274. template <typename Type_>
  275. static inline void MSHookFunction(const char *name, Type_ *replace, Type_ **result = NULL) {
  276. Type_ *symbol;
  277. MSHookSymbol(symbol, name);
  278. return MSHookFunction(symbol, replace, result);
  279. }
  280. template <typename Type_>
  281. static inline void MSHookFunction(MSImageRef image, const char *name, Type_ *replace, Type_ **result = NULL) {
  282. Type_ *symbol;
  283. MSHookSymbol(symbol, name, image);
  284. return MSHookFunction(symbol, replace, result);
  285. }
  286. #endif
  287. #ifdef __ANDROID__
  288. template <typename Type_>
  289. static inline void MSJavaHookMethod(JNIEnv *jni, jclass _class, jmethodID method, Type_ *replace, Type_ **result) {
  290. return MSJavaHookMethod(
  291. jni, _class, method,
  292. reinterpret_cast<void *>(replace),
  293. reinterpret_cast<void **>(result)
  294. );
  295. }
  296. static inline void MSAndroidGetPackage(JNIEnv *jni, jobject global, const char *name, jobject &local, jobject &loader) {
  297. jclass Context(jni->FindClass("android/content/Context"));
  298. jmethodID Context$createPackageContext(jni->GetMethodID(Context, "createPackageContext", "(Ljava/lang/String;I)Landroid/content/Context;"));
  299. jmethodID Context$getClassLoader(jni->GetMethodID(Context, "getClassLoader", "()Ljava/lang/ClassLoader;"));
  300. jstring string(jni->NewStringUTF(name));
  301. local = jni->CallObjectMethod(global, Context$createPackageContext, string, 3);
  302. loader = jni->CallObjectMethod(local, Context$getClassLoader);
  303. }
  304. static inline jclass MSJavaFindClass(JNIEnv *jni, jobject loader, const char *name) {
  305. jclass Class(jni->FindClass("java/lang/Class"));
  306. jmethodID Class$forName(jni->GetStaticMethodID(Class, "forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"));
  307. jstring string(jni->NewStringUTF(name));
  308. jobject _class(jni->CallStaticObjectMethod(Class, Class$forName, string, JNI_TRUE, loader));
  309. if (jni->ExceptionCheck())
  310. return NULL;
  311. return reinterpret_cast<jclass>(_class);
  312. }
  313. _disused static void MSJavaCleanWeak(void *data, JNIEnv *jni, void *value) {
  314. jni->DeleteWeakGlobalRef(reinterpret_cast<jweak>(value));
  315. }
  316. #endif
  317. #define MSHook(type, name, args...) \
  318. _disused static type (*_ ## name)(args); \
  319. static type $ ## name(args)
  320. #define MSJavaHook(type, name, args...) \
  321. MSHook(type, name, JNIEnv *jni, ## args)
  322. #ifdef __cplusplus
  323. #define MSHake(name) \
  324. &$ ## name, &_ ## name
  325. #else
  326. #define MSHake(name) \
  327. &$ ## name, (void **) &_ ## name
  328. #endif
  329. #define SubstrateConcat_(lhs, rhs) \
  330. lhs ## rhs
  331. #define SubstrateConcat(lhs, rhs) \
  332. SubstrateConcat_(lhs, rhs)
  333. #ifdef __APPLE__
  334. #define SubstrateSection \
  335. __attribute__((__section__("__TEXT, __substrate")))
  336. #else
  337. #define SubstrateSection \
  338. __attribute__((__section__(".substrate")))
  339. #endif
  340. #ifdef __APPLE__
  341. #define MSFilterCFBundleID "Filter:CFBundleID"
  342. #define MSFilterObjC_Class "Filter:ObjC.Class"
  343. #endif
  344. #define MSFilterExecutable "Filter:Executable"
  345. #define MSConfig(name, value) \
  346. extern const char SubstrateConcat(_substrate_, __LINE__)[] SubstrateSection = name "=" value;
  347. #ifdef __cplusplus
  348. #define MSInitialize \
  349. static void _MSInitialize(void); \
  350. namespace { static class $MSInitialize { public: _finline $MSInitialize() { \
  351. _MSInitialize(); \
  352. } } $MSInitialize; } \
  353. static void _MSInitialize()
  354. #else
  355. #define MSInitialize \
  356. __attribute__((__constructor__)) static void _MSInitialize(void)
  357. #endif
  358. #define Foundation_f "/System/Library/Frameworks/Foundation.framework/Foundation"
  359. #define UIKit_f "/System/Library/Frameworks/UIKit.framework/UIKit"
  360. #define JavaScriptCore_f "/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"
  361. #define IOKit_f "/System/Library/Frameworks/IOKit.framework/IOKit"
  362. #endif//SUBSTRATE_H_