substratesub.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* Cydia Substrate - Powerful Code Insertion Platform
  2. * Copyright (C) 2008-2011 Jay Freeman (saurik)
  3. */
  4. /* Modified from git revision 904d20f414c79a2716680f4d29d833e6ce0dcea2 by comex
  5. * to act as a compatibility shim for libsubstitute; the changes consist of
  6. * removing MSHookMessage, MSHookProcess (for now), and some internal
  7. * functions, and setting custom asm names for all non-inline functions.
  8. *
  9. * The purpose of the custom asm names is to allow the shim to be applied at
  10. * either the source or binary level (the latter mainly for testing purposes).
  11. * Normally, due to two-level name lookup, exposing the regular names in
  12. * libsubstitute.dylib would suffice for this, even if it's loaded in the same
  13. * process as Substrate, but not with DYLD_FORCE_FLAT_NAMESPACE etc.
  14. * Therefore, making a binary use this requires hacking it up with sed. With
  15. * source, just put the directory containing this file in your include path.
  16. */
  17. /* GNU Lesser General Public License, Version 3 {{{ */
  18. /*
  19. * Substrate is free software: you can redistribute it and/or modify it under
  20. * the terms of the GNU Lesser General Public License as published by the
  21. * Free Software Foundation, either version 3 of the License, or (at your
  22. * option) any later version.
  23. *
  24. * Substrate is distributed in the hope that it will be useful, but WITHOUT
  25. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  26. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  27. * License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public License
  30. * along with Substrate. If not, see <http://www.gnu.org/licenses/>.
  31. **/
  32. /* }}} */
  33. #ifndef SUBSTRATE_H_
  34. #define SUBSTRATE_H_
  35. #ifdef __APPLE__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include <mach-o/nlist.h>
  40. #ifdef __cplusplus
  41. }
  42. #endif
  43. #include <objc/runtime.h>
  44. #include <objc/message.h>
  45. #endif
  46. #include <dlfcn.h>
  47. #include <stdlib.h>
  48. #define _finline \
  49. inline __attribute__((__always_inline__))
  50. #define _disused \
  51. __attribute__((__unused__))
  52. #define _extern \
  53. extern "C" __attribute__((__visibility__("default")))
  54. #ifdef __cplusplus
  55. #define _default(value) = value
  56. #else
  57. #define _default(value)
  58. #endif
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. typedef const void *MSImageRef;
  63. MSImageRef MSGetImageByName(const char *file)
  64. __asm__("SubGetImageByName");
  65. void *MSFindSymbol(MSImageRef image, const char *name)
  66. __asm__("SubFindSymbol");
  67. void MSHookFunction(void *symbol, void *replace, void **result)
  68. __asm__("SubHookFunction");
  69. #ifdef __APPLE__
  70. void MSHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result)
  71. __asm__("SubHookMessageEx");
  72. #endif
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #ifdef __cplusplus
  77. #ifdef __APPLE__
  78. namespace etl {
  79. template <unsigned Case_>
  80. struct Case {
  81. static char value[Case_ + 1];
  82. };
  83. typedef Case<true> Yes;
  84. typedef Case<false> No;
  85. namespace be {
  86. template <typename Checked_>
  87. static Yes CheckClass_(void (Checked_::*)());
  88. template <typename Checked_>
  89. static No CheckClass_(...);
  90. }
  91. template <typename Type_>
  92. struct IsClass {
  93. void gcc32();
  94. static const bool value = (sizeof(be::CheckClass_<Type_>(0).value) == sizeof(Yes::value));
  95. };
  96. }
  97. #ifdef __arm__
  98. template <typename Type_>
  99. __attribute__((__deprecated__))
  100. static inline Type_ *MSHookMessage(Class _class, SEL sel, Type_ *imp, const char *prefix = NULL) {
  101. return reinterpret_cast<Type_ *>(MSHookMessage(_class, sel, reinterpret_cast<IMP>(imp), prefix));
  102. }
  103. #endif
  104. template <typename Type_>
  105. static inline void MSHookMessage(Class _class, SEL sel, Type_ *imp, Type_ **result) {
  106. return MSHookMessageEx(_class, sel, reinterpret_cast<IMP>(imp), reinterpret_cast<IMP *>(result));
  107. }
  108. template <typename Type_>
  109. static inline Type_ &MSHookIvar(id self, const char *name) {
  110. Ivar ivar(class_getInstanceVariable(object_getClass(self), name));
  111. void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>(self) + ivar_getOffset(ivar));
  112. return *reinterpret_cast<Type_ *>(pointer);
  113. }
  114. #define MSAddMessage0(_class, type, arg0) \
  115. class_addMethod($ ## _class, @selector(arg0), (IMP) &$ ## _class ## $ ## arg0, type);
  116. #define MSAddMessage1(_class, type, arg0) \
  117. class_addMethod($ ## _class, @selector(arg0:), (IMP) &$ ## _class ## $ ## arg0 ## $, type);
  118. #define MSAddMessage2(_class, type, arg0, arg1) \
  119. class_addMethod($ ## _class, @selector(arg0:arg1:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $, type);
  120. #define MSAddMessage3(_class, type, arg0, arg1, arg2) \
  121. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $, type);
  122. #define MSAddMessage4(_class, type, arg0, arg1, arg2, arg3) \
  123. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $, type);
  124. #define MSAddMessage5(_class, type, arg0, arg1, arg2, arg3, arg4) \
  125. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $, type);
  126. #define MSAddMessage6(_class, type, arg0, arg1, arg2, arg3, arg4, arg5) \
  127. class_addMethod($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), (IMP) &$ ## _class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $, type);
  128. #define MSHookMessage0(_class, arg0) \
  129. MSHookMessage($ ## _class, @selector(arg0), MSHake(_class ## $ ## arg0))
  130. #define MSHookMessage1(_class, arg0) \
  131. MSHookMessage($ ## _class, @selector(arg0:), MSHake(_class ## $ ## arg0 ## $))
  132. #define MSHookMessage2(_class, arg0, arg1) \
  133. MSHookMessage($ ## _class, @selector(arg0:arg1:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $))
  134. #define MSHookMessage3(_class, arg0, arg1, arg2) \
  135. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $))
  136. #define MSHookMessage4(_class, arg0, arg1, arg2, arg3) \
  137. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $))
  138. #define MSHookMessage5(_class, arg0, arg1, arg2, arg3, arg4) \
  139. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $))
  140. #define MSHookMessage6(_class, arg0, arg1, arg2, arg3, arg4, arg5) \
  141. MSHookMessage($ ## _class, @selector(arg0:arg1:arg2:arg3:arg4:arg5:), MSHake(_class ## $ ## arg0 ## $ ## arg1 ## $ ## arg2 ## $ ## arg3 ## $ ## arg4 ## $ ## arg5 ## $))
  142. #define MSRegister_(name, dollar, colon) \
  143. static class C_$ ## name ## $ ## dollar { public: _finline C_$ ## name ## $ ##dollar() { \
  144. MSHookMessage($ ## name, @selector(colon), MSHake(name ## $ ## dollar)); \
  145. } } V_$ ## name ## $ ## dollar; \
  146. #define MSIgnore_(name, dollar, colon)
  147. #define MSMessage_(extra, type, _class, name, dollar, colon, call, args...) \
  148. static type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args); \
  149. MSHook(type, name ## $ ## dollar, _class self, SEL _cmd, ## args) { \
  150. Class const _cls($ ## name); \
  151. type (* const _old)(_class, SEL, ## args, ...) = reinterpret_cast<type (* const)(_class, SEL, ## args, ...)>(_ ## name ## $ ## dollar); \
  152. typedef type (*msgSendSuper_t)(struct objc_super *, SEL, ## args, ...); \
  153. msgSendSuper_t const _spr(::etl::IsClass<type>::value ? reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper_stret) : reinterpret_cast<msgSendSuper_t>(&objc_msgSendSuper)); \
  154. return _$ ## name ## $ ## dollar call; \
  155. } \
  156. extra(name, dollar, colon) \
  157. static _finline type _$ ## name ## $ ## dollar(Class _cls, type (*_old)(_class, SEL, ## args, ...), type (*_spr)(struct objc_super *, SEL, ## args, ...), _class self, SEL _cmd, ## args)
  158. /* 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 ")";} */
  159. #define MSMessage0_(extra, type, _class, name, sel0) \
  160. MSMessage_(extra, type, _class, name, sel0, sel0, (_cls, _old, _spr, self, _cmd))
  161. #define MSMessage1_(extra, type, _class, name, sel0, type0, arg0) \
  162. MSMessage_(extra, type, _class, name, sel0 ## $, sel0:, (_cls, _old, _spr, self, _cmd, arg0), type0 arg0)
  163. #define MSMessage2_(extra, type, _class, name, sel0, sel1, type0, arg0, type1, arg1) \
  164. MSMessage_(extra, type, _class, name, sel0 ## $ ## sel1 ## $, sel0:sel1:, (_cls, _old, _spr, self, _cmd, arg0, arg1), type0 arg0, type1 arg1)
  165. #define MSMessage3_(extra, type, _class, name, sel0, sel1, sel2, type0, arg0, type1, arg1, type2, arg2) \
  166. 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)
  167. #define MSMessage4_(extra, type, _class, name, sel0, sel1, sel2, sel3, type0, arg0, type1, arg1, type2, arg2, type3, arg3) \
  168. 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)
  169. #define MSMessage5_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4) \
  170. 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)
  171. #define MSMessage6_(extra, type, _class, name, sel0, sel1, sel2, sel3, sel4, sel5, type0, arg0, type1, arg1, type2, arg2, type3, arg3, type4, arg4, type5, arg5) \
  172. 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)
  173. #define MSInstanceMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, _class *, _class, ## args)
  174. #define MSInstanceMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, _class *, _class, ## args)
  175. #define MSInstanceMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, _class *, _class, ## args)
  176. #define MSInstanceMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, _class *, _class, ## args)
  177. #define MSInstanceMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, _class *, _class, ## args)
  178. #define MSInstanceMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, _class *, _class, ## args)
  179. #define MSInstanceMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, _class *, _class, ## args)
  180. #define MSClassMessage0(type, _class, args...) MSMessage0_(MSIgnore_, type, Class, $ ## _class, ## args)
  181. #define MSClassMessage1(type, _class, args...) MSMessage1_(MSIgnore_, type, Class, $ ## _class, ## args)
  182. #define MSClassMessage2(type, _class, args...) MSMessage2_(MSIgnore_, type, Class, $ ## _class, ## args)
  183. #define MSClassMessage3(type, _class, args...) MSMessage3_(MSIgnore_, type, Class, $ ## _class, ## args)
  184. #define MSClassMessage4(type, _class, args...) MSMessage4_(MSIgnore_, type, Class, $ ## _class, ## args)
  185. #define MSClassMessage5(type, _class, args...) MSMessage5_(MSIgnore_, type, Class, $ ## _class, ## args)
  186. #define MSClassMessage6(type, _class, args...) MSMessage6_(MSIgnore_, type, Class, $ ## _class, ## args)
  187. #define MSInstanceMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, _class *, _class, ## args)
  188. #define MSInstanceMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, _class *, _class, ## args)
  189. #define MSInstanceMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, _class *, _class, ## args)
  190. #define MSInstanceMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, _class *, _class, ## args)
  191. #define MSInstanceMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, _class *, _class, ## args)
  192. #define MSInstanceMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, _class *, _class, ## args)
  193. #define MSInstanceMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, _class *, _class, ## args)
  194. #define MSClassMessageHook0(type, _class, args...) MSMessage0_(MSRegister_, type, Class, $ ## _class, ## args)
  195. #define MSClassMessageHook1(type, _class, args...) MSMessage1_(MSRegister_, type, Class, $ ## _class, ## args)
  196. #define MSClassMessageHook2(type, _class, args...) MSMessage2_(MSRegister_, type, Class, $ ## _class, ## args)
  197. #define MSClassMessageHook3(type, _class, args...) MSMessage3_(MSRegister_, type, Class, $ ## _class, ## args)
  198. #define MSClassMessageHook4(type, _class, args...) MSMessage4_(MSRegister_, type, Class, $ ## _class, ## args)
  199. #define MSClassMessageHook5(type, _class, args...) MSMessage5_(MSRegister_, type, Class, $ ## _class, ## args)
  200. #define MSClassMessageHook6(type, _class, args...) MSMessage6_(MSRegister_, type, Class, $ ## _class, ## args)
  201. #define MSOldCall(args...) \
  202. _old(self, _cmd, ## args)
  203. #define MSSuperCall(args...) \
  204. _spr(& (struct objc_super) {self, class_getSuperclass(_cls)}, _cmd, ## args)
  205. #define MSIvarHook(type, name) \
  206. type &name(MSHookIvar<type>(self, #name))
  207. #define MSClassHook(name) \
  208. @class name; \
  209. static Class $ ## name = objc_getClass(#name);
  210. #define MSMetaClassHook(name) \
  211. @class name; \
  212. static Class $$ ## name = object_getClass($ ## name);
  213. #endif/*__APPLE__*/
  214. template <typename Type_>
  215. static inline void MSHookFunction(Type_ *symbol, Type_ *replace, Type_ **result) {
  216. return MSHookFunction(
  217. reinterpret_cast<void *>(symbol),
  218. reinterpret_cast<void *>(replace),
  219. reinterpret_cast<void **>(result)
  220. );
  221. }
  222. template <typename Type_>
  223. static inline void MSHookFunction(Type_ *symbol, Type_ *replace) {
  224. return MSHookFunction(symbol, replace, reinterpret_cast<Type_ **>(NULL));
  225. }
  226. template <typename Type_>
  227. static inline void MSHookSymbol(Type_ *&value, const char *name, MSImageRef image = NULL) {
  228. value = reinterpret_cast<Type_ *>(MSFindSymbol(image, name));
  229. }
  230. template <typename Type_>
  231. static inline void MSHookFunction(const char *name, Type_ *replace, Type_ **result = NULL) {
  232. Type_ *symbol;
  233. MSHookSymbol(symbol, name);
  234. return MSHookFunction(symbol, replace, result);
  235. }
  236. #endif
  237. #define MSHook(type, name, args...) \
  238. _disused static type (*_ ## name)(args); \
  239. static type $ ## name(args)
  240. #ifdef __cplusplus
  241. #define MSHake(name) \
  242. &$ ## name, &_ ## name
  243. #else
  244. #define MSHake(name) \
  245. &$ ## name, (void **) &_ ## name
  246. #endif
  247. #define MSInitialize \
  248. __attribute__((__constructor__)) static void _MSInitialize(void)
  249. #define Foundation_f "/System/Library/Frameworks/Foundation.framework/Foundation"
  250. #define UIKit_f "/System/Library/Frameworks/UIKit.framework/UIKit"
  251. #define JavaScriptCore_f "/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"
  252. #define IOKit_f "/System/Library/Frameworks/IOKit.framework/IOKit"
  253. #endif//SUBSTRATE_H_