substrate.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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)(JNIEnv *, jclass, void *), 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 MSJavaDeleteObjectKey(MSJavaObjectKey key);
  78. _extern void *MSJavaGetObjectKey(JNIEnv *jni, jobject object, MSJavaObjectKey key);
  79. _extern void MSJavaSetObjectKey(JNIEnv *jni, jobject object, MSJavaObjectKey key, void *value, void (*clean)(void *, JNIEnv *, void *) _default(NULL), void *data _default(NULL));
  80. #endif
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #ifdef __cplusplus
  85. #ifdef SubstrateInternal
  86. struct SubstrateHookMemory {
  87. SubstrateMemoryRef handle_;
  88. SubstrateHookMemory(SubstrateProcessRef process, void *data, size_t size) :
  89. handle_(SubstrateMemoryCreate(NULL, NULL, data, size))
  90. {
  91. }
  92. ~SubstrateHookMemory() {
  93. if (handle_ != NULL)
  94. SubstrateMemoryRelease(handle_);
  95. }
  96. };
  97. #endif
  98. #ifdef __APPLE__
  99. namespace etl {
  100. template <unsigned Case_>
  101. struct Case {
  102. static char value[Case_ + 1];
  103. };
  104. typedef Case<true> Yes;
  105. typedef Case<false> No;
  106. namespace be {
  107. template <typename Checked_>
  108. static Yes CheckClass_(void (Checked_::*)());
  109. template <typename Checked_>
  110. static No CheckClass_(...);
  111. }
  112. template <typename Type_>
  113. struct IsClass {
  114. void gcc32();
  115. static const bool value = (sizeof(be::CheckClass_<Type_>(0).value) == sizeof(Yes::value));
  116. };
  117. }
  118. #ifdef __arm__
  119. template <typename Type_>
  120. __attribute__((__deprecated__))
  121. static inline Type_ *MSHookMessage(Class _class, SEL sel, Type_ *imp, const char *prefix = NULL) {
  122. return reinterpret_cast<Type_ *>(MSHookMessage(_class, sel, reinterpret_cast<IMP>(imp), prefix));
  123. }
  124. #endif
  125. template <typename Type_>
  126. static inline void MSHookMessage(Class _class, SEL sel, Type_ *imp, Type_ **result) {
  127. return MSHookMessageEx(_class, sel, reinterpret_cast<IMP>(imp), reinterpret_cast<IMP *>(result));
  128. }
  129. template <typename Type_>
  130. static inline Type_ &MSHookIvar(id self, const char *name) {
  131. Ivar ivar(class_getInstanceVariable(object_getClass(self), name));
  132. #if __has_feature(objc_arc)
  133. void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>((__bridge void *)self) + ivar_getOffset(ivar));
  134. #else
  135. void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>(self) + ivar_getOffset(ivar));
  136. #endif
  137. return *reinterpret_cast<Type_ *>(pointer);
  138. }
  139. #define MSAddMessage0(_class, type, arg0) \
  140. class_addMethod($ ## _class, @selector(arg0), (IMP) &$ ## _class ## $ ## arg0, type);
  141. #define MSAddMessage1(_class, type, arg0) \
  142. class_addMethod($ ## _class, @selector(arg0:), (IMP) &$ ## _class ## $ ## arg0 ## $, type);
  143. #define MSAddMessage2(_class, type, arg0, arg1) \
  144. class_addMethod($ ## _class, @selector(arg0:arg1:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $, type);
  145. #define MSAddMessage3(_class, type, arg0, arg1, arg2) \
  146. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $, type);
  147. #define MSAddMessage4(_class, type, arg0, arg1, arg2, arg3) \
  148. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $, type);
  149. #define MSAddMessage5(_class, type, arg0, arg1, arg2, arg3, arg4) \
  150. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $, type);
  151. #define MSAddMessage6(_class, type, arg0, arg1, arg2, arg3, arg4, arg5) \
  152. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $, type);
  153. #define MSAddMessage7(_class, type, arg0, arg1, arg2, arg3, arg4, arg5, arg6) \
  154. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ $$ arg6 ## $, type);
  155. #define MSAddMessage8(_class, type, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  156. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:arg7:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ $$ arg6 ## $ ## arg7 ## $, type);
  157. #define MSHookMessage0(_class, arg0) \
  158. MSHookMessage($ ## _class, @selector(arg0), MSHake(_class ## $ ## arg0))
  159. #define MSHookMessage1(_class, arg0) \
  160. MSHookMessage($ ## _class, @selector(arg0:), MSHake(_class ## $ ## arg0 ## $))
  161. #define MSHookMessage2(_class, arg0, arg1) \
  162. MSHookMessage($ ## _class, @selector(arg0:arg1:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $))
  163. #define MSHookMessage3(_class, arg0, arg1, arg2) \
  164. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $))
  165. #define MSHookMessage4(_class, arg0, arg1, arg2, arg3) \
  166. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $))
  167. #define MSHookMessage5(_class, arg0, arg1, arg2, arg3, arg4) \
  168. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $))
  169. #define MSHookMessage6(_class, arg0, arg1, arg2, arg3, arg4, arg5) \
  170. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $))
  171. #define MSHookMessage7(_class, arg0, arg1, arg2, arg3, arg4, arg5, arg6) \
  172. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ ## arg6 ## $))
  173. #define MSHookMessage8(_class, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  174. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:arg6:arg7:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $ ## arg6 ## $ ## arg7 ## $))
  175. #define MSRegister_(name, dollar, colon) \
  176. namespace { static class C_$ ## name ## $ ## dollar { public: _finline C_$ ## name ## $ ##dollar() { \
  177. MSHookMessage($ ## name, @selector(colon), MSHake(name ## $ ## dollar)); \
  178. } } V_$ ## name ## $ ## dollar; } \
  179. #define MSIgnore_(name, dollar, colon)
  180. #define MSMessage_(extra, type, _class, name, dollar, colon, call, args...) \
  181. static type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args); \
  182. MSHook(type, name ## $ ## dollar, _class self, SEL _cmd, ## args) { \
  183. Class const _cls($ ## name); \
  184. type (* const _old)(_class, SEL, ## args, ...) = reinterpret_cast<type (* const)(_class, SEL, ## args, ...)>(_ ## name ## $ ## dollar); \
  185. typedef type (*msgSendSuper_t)(struct objc_super *, SEL, ## args, ...); \
  186. msgSendSuper_t const _spr(::etl::IsClass<type>::value ? reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper_stret) : reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper)); \
  187. return _$ ## name ## $ ## dollar call; \
  188. } \
  189. extra(name, dollar, colon) \
  190. static _finline type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args)
  191. /* 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 ")";} */
  192. #define MSMessage0_(extra, type, _class, name, sel0) \
  193. MSMessage_(extra, type, _class, name, sel0, sel0, (_cls, _old, _spr, self, _cmd))
  194. #define MSMessage1_(extra, type, _class, name, sel0, type0, arg0) \
  195. MSMessage_(extra, type, _class, name, sel0 ## $, sel0:, (_cls, _old, _spr, self, _cmd, arg0), type0 arg0)
  196. #define MSMessage2_(extra, type, _class, name, sel0, sel1, type0, arg0, type1, arg1) \
  197. MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $, sel0:sel1:, (_cls, _old, _spr, self, _cmd, arg0, arg1), type0 arg0, type1 arg1)
  198. #define MSMessage3_(extra, type, _class, name, sel0, sel1, sel2, type0, arg0, type1, arg1, type2, arg2) \
  199. 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)
  200. #define MSMessage4_(extra, type, _class, name, sel0, sel1, sel2, sel3, type0, arg0, type1, arg1, type2, arg2, type3, arg3) \
  201. 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)
  202. #define MSMessage5_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
  203. 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)
  204. #define MSMessage6_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, sel5, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \
  205. 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)
  206. #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) \
  207. 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)
  208. #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) \
  209. 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)
  210. #define MSInstanceMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, _class *, _class, ## args)
  211. #define MSInstanceMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, _class *, _class, ## args)
  212. #define MSInstanceMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, _class *, _class, ## args)
  213. #define MSInstanceMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, _class *, _class, ## args)
  214. #define MSInstanceMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, _class *, _class, ## args)
  215. #define MSInstanceMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, _class *, _class, ## args)
  216. #define MSInstanceMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, _class *, _class, ## args)
  217. #define MSInstanceMessage7(type, _class, args...) MSMessage7_(MSIgnore_, type, _class *, _class, ## args)
  218. #define MSInstanceMessage8(type, _class, args...) MSMessage8_(MSIgnore_, type, _class *, _class, ## args)
  219. #define MSClassMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, Class, $ ## _class, ## args)
  220. #define MSClassMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, Class, $ ## _class, ## args)
  221. #define MSClassMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, Class, $ ## _class, ## args)
  222. #define MSClassMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, Class, $ ## _class, ## args)
  223. #define MSClassMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, Class, $ ## _class, ## args)
  224. #define MSClassMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, Class, $ ## _class, ## args)
  225. #define MSClassMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, Class, $ ## _class, ## args)
  226. #define MSClassMessage7(type, _class, args...) MSMessage7_(MSIgnore_, type, Class, $ ## _class, ## args)
  227. #define MSClassMessage8(type, _class, args...) MSMessage8_(MSIgnore_, type, Class, $ ## _class, ## args)
  228. #define MSInstanceMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, _class *, _class, ## args)
  229. #define MSInstanceMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, _class *, _class, ## args)
  230. #define MSInstanceMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, _class *, _class, ## args)
  231. #define MSInstanceMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, _class *, _class, ## args)
  232. #define MSInstanceMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, _class *, _class, ## args)
  233. #define MSInstanceMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, _class *, _class, ## args)
  234. #define MSInstanceMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, _class *, _class, ## args)
  235. #define MSInstanceMessageHook7(type, _class, args...) MSMessage7_(MSRegister_, type, _class *, _class, ## args)
  236. #define MSInstanceMessageHook8(type, _class, args...) MSMessage8_(MSRegister_, type, _class *, _class, ## args)
  237. #define MSClassMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, Class, $ ## _class, ## args)
  238. #define MSClassMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, Class, $ ## _class, ## args)
  239. #define MSClassMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, Class, $ ## _class, ## args)
  240. #define MSClassMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, Class, $ ## _class, ## args)
  241. #define MSClassMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, Class, $ ## _class, ## args)
  242. #define MSClassMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, Class, $ ## _class, ## args)
  243. #define MSClassMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, Class, $ ## _class, ## args)
  244. #define MSClassMessageHook7(type, _class, args...) MSMessage7_(MSRegister_, type, Class, $ ## _class, ## args)
  245. #define MSClassMessageHook8(type, _class, args...) MSMessage8_(MSRegister_, type, Class, $ ## _class, ## args)
  246. #define MSOldCall(args...) \
  247. _old(self, _cmd, ## args)
  248. #define MSSuperCall(args...) \
  249. _spr(& (struct objc_super) {self, class_getSuperclass(_cls)}, _cmd, ## args)
  250. #define MSIvarHook(type, name) \
  251. type &name(MSHookIvar<type>(self, #name))
  252. #define MSClassHook(name) \
  253. @class name; \
  254. static Class $ ## name = objc_getClass(#name);
  255. #define MSMetaClassHook(name) \
  256. @class name; \
  257. static Class $$ ## name = object_getClass($ ## name);
  258. #endif/*__APPLE__*/
  259. template <typename Type_>
  260. static inline void MSHookFunction(Type_ *symbol, Type_ *replace, Type_ **result) {
  261. return MSHookFunction(
  262. reinterpret_cast<void *>(symbol),
  263. reinterpret_cast<void *>(replace),
  264. reinterpret_cast<void **>(result)
  265. );
  266. }
  267. template <typename Type_>
  268. static inline void MSHookFunction(Type_ *symbol, Type_ *replace) {
  269. return MSHookFunction(symbol, replace, reinterpret_cast<Type_ **>(NULL));
  270. }
  271. template <typename Type_>
  272. static inline void MSHookSymbol(Type_ *&value, const char *name, MSImageRef image = NULL) {
  273. value = reinterpret_cast<Type_ *>(MSFindSymbol(image, name));
  274. }
  275. template <typename Type_>
  276. static inline void MSHookFunction(const char *name, Type_ *replace, Type_ **result = NULL) {
  277. Type_ *symbol;
  278. MSHookSymbol(symbol, name);
  279. return MSHookFunction(symbol, replace, result);
  280. }
  281. template <typename Type_>
  282. static inline void MSHookFunction(MSImageRef image, const char *name, Type_ *replace, Type_ **result = NULL) {
  283. Type_ *symbol;
  284. MSHookSymbol(symbol, name, image);
  285. return MSHookFunction(symbol, replace, result);
  286. }
  287. #endif
  288. #ifdef __ANDROID__
  289. #ifdef __cplusplus
  290. template <typename Type_, typename Kind_, typename ...Args_>
  291. static inline void MSJavaHookMethod(JNIEnv *jni, jclass _class, jmethodID method, Type_ (*replace)(JNIEnv *, Kind_, Args_...), Type_ (**result)(JNIEnv *, Kind_, ...)) {
  292. return MSJavaHookMethod(
  293. jni, _class, method,
  294. reinterpret_cast<void *>(replace),
  295. reinterpret_cast<void **>(result)
  296. );
  297. }
  298. #endif
  299. static inline void MSAndroidGetPackage(JNIEnv *jni, jobject global, const char *name, jobject &local, jobject &loader) {
  300. jclass Context(jni->FindClass("android/content/Context"));
  301. jmethodID Context$createPackageContext(jni->GetMethodID(Context, "createPackageContext", "(Ljava/lang/String;I)Landroid/content/Context;"));
  302. jmethodID Context$getClassLoader(jni->GetMethodID(Context, "getClassLoader", "()Ljava/lang/ClassLoader;"));
  303. jstring string(jni->NewStringUTF(name));
  304. local = jni->CallObjectMethod(global, Context$createPackageContext, string, 3);
  305. loader = jni->CallObjectMethod(local, Context$getClassLoader);
  306. }
  307. static inline jclass MSJavaFindClass(JNIEnv *jni, jobject loader, const char *name) {
  308. jclass Class(jni->FindClass("java/lang/Class"));
  309. jmethodID Class$forName(jni->GetStaticMethodID(Class, "forName", "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;"));
  310. jstring string(jni->NewStringUTF(name));
  311. jobject _class(jni->CallStaticObjectMethod(Class, Class$forName, string, JNI_TRUE, loader));
  312. if (jni->ExceptionCheck())
  313. return NULL;
  314. return reinterpret_cast<jclass>(_class);
  315. }
  316. _disused static void MSJavaCleanWeak(void *data, JNIEnv *jni, void *value) {
  317. jni->DeleteWeakGlobalRef(reinterpret_cast<jweak>(value));
  318. }
  319. #endif
  320. #define MSHook(type, name, args...) \
  321. _disused static type (*_ ## name)(args); \
  322. static type $ ## name(args)
  323. #define MSJavaHook(type, name, arg0, args...) \
  324. _disused static type (*_ ## name)(JNIEnv *jni, arg0, ...); \
  325. static type $ ## name(JNIEnv *jni, arg0, ## args)
  326. #ifdef __cplusplus
  327. #define MSHake(name) \
  328. &$ ## name, &_ ## name
  329. #else
  330. #define MSHake(name) \
  331. &$ ## name, (void **) &_ ## name
  332. #endif
  333. #define SubstrateConcat_(lhs, rhs) \
  334. lhs ## rhs
  335. #define SubstrateConcat(lhs, rhs) \
  336. SubstrateConcat_(lhs, rhs)
  337. #ifdef __APPLE__
  338. #define SubstrateSection \
  339. __attribute__((__section__("__TEXT, __substrate")))
  340. #else
  341. #define SubstrateSection \
  342. __attribute__((__section__(".substrate")))
  343. #endif
  344. #ifdef __APPLE__
  345. #define MSFilterCFBundleID "Filter:CFBundleID"
  346. #define MSFilterObjC_Class "Filter:ObjC.Class"
  347. #endif
  348. #define MSFilterLibrary "Filter:Library"
  349. #define MSFilterExecutable "Filter:Executable"
  350. #define MSConfig(name, value) \
  351. extern const char SubstrateConcat(_substrate_, __LINE__)[] SubstrateSection = name "=" value;
  352. #ifdef __cplusplus
  353. #define MSInitialize \
  354. static void _MSInitialize(void); \
  355. namespace { static class $MSInitialize { public: _finline $MSInitialize() { \
  356. _MSInitialize(); \
  357. } } $MSInitialize; } \
  358. static void _MSInitialize()
  359. #else
  360. #define MSInitialize \
  361. __attribute__((__constructor__)) static void _MSInitialize(void)
  362. #endif
  363. #define Foundation_f "/System/Library/Frameworks/Foundation.framework/Foundation"
  364. #define UIKit_f "/System/Library/Frameworks/UIKit.framework/UIKit"
  365. #define JavaScriptCore_f "/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"
  366. #define IOKit_f "/System/Library/Frameworks/IOKit.framework/IOKit"
  367. #endif//SUBSTRATE_H_