libsubstrate.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <objc/runtime.h>
  2. #include <substitute.h>
  3. #include <os/log.h>
  4. extern void *SubGetImageByName(const char *filename) __asm__("SubGetImageByName");;
  5. void *MSGetImageByName(const char *filename) {
  6. return SubGetImageByName(filename);
  7. }
  8. extern void *SubFindSymbol(void *image, const char *name) __asm__("SubFindSymbol");
  9. void *MSFindSymbol(void *image, const char *name) {
  10. return SubFindSymbol(image, name);
  11. }
  12. extern void SubHookFunction(void *symbol, void *replace, void **result) __asm__("SubHookFunction");
  13. void MSHookFunction(void *symbol, void *replace, void **result) {
  14. SubHookFunction(symbol, replace, result);
  15. }
  16. extern void SubHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result) __asm__("SubHookMessageEx");
  17. void MSHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result) {
  18. if (class_getInstanceMethod(_class, sel) || class_getClassMethod(_class, sel)) {
  19. SubHookMessageEx(_class, sel, imp, result);
  20. } else {
  21. os_log_error(OS_LOG_DEFAULT, "libsubstrate-shim: Tried to hook non-existent selector %s on class %s",
  22. sel_getName(sel), class_getName(_class));
  23. if (result) *result = NULL;
  24. }
  25. }
  26. // i don't think anyone uses this function anymore, but it's here for completeness
  27. void MSHookClassPair(Class _class, Class hook, Class old) {
  28. unsigned int n_methods = 0;
  29. Method *hooks = class_copyMethodList(hook, &n_methods);
  30. for (unsigned int i = 0; i < n_methods; ++i) {
  31. SEL selector = method_getName(hooks[i]);
  32. const char *what = method_getTypeEncoding(hooks[i]);
  33. Method old_mptr = class_getInstanceMethod(old, selector);
  34. Method cls_mptr = class_getInstanceMethod(_class, selector);
  35. if (cls_mptr) {
  36. class_addMethod(old, selector, method_getImplementation(hooks[i]), what);
  37. method_exchangeImplementations(cls_mptr, old_mptr);
  38. } else {
  39. class_addMethod(_class, selector, method_getImplementation(hooks[i]), what);
  40. }
  41. }
  42. free(hooks);
  43. }