Created
August 20, 2015 17:45
-
-
Save DavidGoldman/d263c3f8b785d9ba4bfe to your computer and use it in GitHub Desktop.
Hook late loaded classes without dlopen'ing anything
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <objc/runtime.h> | |
#include <dlfcn.h> | |
#include <mach-o/dyld.h> | |
%group NormalHooks | |
// Regular hooks in here. | |
%end | |
%group LateClassOrFrameworkA | |
// Specific late loaded stuff for Class A or Framework A. | |
%end | |
static void tryLateLoadClassA() { | |
static dispatch_once_t predicate; | |
// Check if Class A is loaded. | |
Class lateLoadedClassA = objc_getClass("lateLoadedClassA"); | |
if (lateLoadedClassA) { | |
dispatch_once(&predicate, ^{ | |
// Initialize hooks for late loaded Class A. | |
%init(LateClassOrFrameworkA); | |
}); | |
} | |
} | |
static void add_image_callback(const struct mach_header *mh, intptr_t vmaddr_slide) { | |
// Try to late load in here. | |
tryLateLoadClassA(); | |
} | |
%ctor { | |
%init(NormalHooks); | |
_dyld_register_func_for_add_image(&add_image_callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment