Use fishhook to dynamically rebind symbols in Mach-O binaries running on iOS (both simulator and device). This is useful for hooking calls in libSystem for debugging or tracing purposes.
To use it, add fishhook.h and fishhook.c to your project and call rebind_symbols with an array of struct rebinding elements. Each element specifies the symbol name to hook, the replacement function, and a pointer to store the original function pointer so you can call it from your hook.
#import <dlfcn.h>
#import <UIKit/UIKit.h>
#import "fishhook.h"
// 1. Define function pointers to hold the original implementations
static int (*orig_close)(int);
static int (*orig_open)(const char *, int, ...);
// 2. Define your replacement functions
int my_close(int fd) {
printf("Calling real close(%d)\n", fd);
return orig_close(fd); // Call the original function
}
int my_open(const char *path, int oflag, ...) {
// ... implementation logic ...
return orig_open(path, oflag, mode); // Call the original function
}
int main(int argc, char * argv[])
{
@autoreleasepool {
// 3. Perform the rebinding
// The array contains: {symbol_name, replacement_function, original_function_pointer_storage}
rebind_symbols((struct rebinding[2]){
{"close", my_close, (void *)&orig_close},
{"open", my_open, (void *)&orig_open}
}, 2);
// ... rest of application ...
}
}