%hookf
can be confusing at first, but it is pretty easy to understand once you get into it.
While %hook
is used to hook Objective-C classes, %hookf
is used to hook C functions. Its syntax is also different from %hook
.
%hookf(return_type, symbol_name, arguments...) {...}
return_type
- The return type of the function.symbol_name
- This is the name of the function being hooked.arguments
- These are the arguments that are passed into the function.
Let's say we want to hook CGFontRef CGFontCreateWithFontName(CFStringRef name);
. This would be done like so:
%hookf(CGFontRef, CGFontCreateWithFontName, CFStringRef name) {
// code
return %orig;
}
Below is the Substrate version of the above code, if needed.
static CGFontRef (*CGFontCreateWithFontName_orig)(CFStringRef) = NULL;
static CGFontRef CGFontCreateWithFontName_hook(CFStringRef name) {
return CGFontCreateWithFontName_orig(name);
}
__attribute__((constructor)) static void initialize() {
MSHookFunction(dlsym(RTLD_DEFAULT, "CGFontCreateWithFontName"), (void *)CGFontCreateWithFontName_hook, (void **)&CGFontCreateWithFontName_orig);
}
Due to the previously mentioned Spinlock panics on iOS 15 arm64e devices, %hookf
/MSHookFunction
should be used with caution. When possible, look into alternative methods of achieving your esired goal in order for a better user experience for users on iOS 15 arm64e devices.
For further information about %hookf
, please go here.