Linux/unix 提供了使用 dlopen 和 dlsym 方法动态加载库和调用函数,这套方法在 macOS 和 iOS 上也支持。

dlopen 打开一个库,获取句柄。
dlsym 在打开的库中查找符号的值。
dlclose 关闭句柄。
dlerror 返回一个描述最后一次调用dlopen、dlsym,或 dlclose 的错误信息的字符串。

动态调用 printf 函数,编写测试代码如下:

#import <dlfcn.h>typedef int (*printf_func_pointer) (const char * __restrict, ...);void dynamic_call_function(){//动态库路径char *dylib_path = "/usr/lib/libSystem.dylib";//打开动态库void *handle = dlopen(dylib_path, RTLD_GLOBAL | RTLD_NOW);if (handle == NULL) {//打开动态库出错fprintf(stderr, "%s\n", dlerror());} else {//获取 printf 地址printf_func_pointer printf_func = dlsym(handle, "printf");//地址获取成功则调用if (printf_func) {int num = 100;printf_func("Hello exchen.net %d\n", num);printf_func("printf function address 0x%lx\n", printf_func);}dlclose(handle); //关闭句柄}
}int main(int argc, char * argv[]) {@autoreleasepool {dynamic_call_function();return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));}
}

在手机上运行的输出结果如下:

Hello exchen.net 100
printf function address 0x189f0da78


原文地址:https://www.exchen.net/ios-hacker-dlopen-%E5%92%8C-dlsym-%E5%8A%A8%E6%80%81%E8%B0%83%E7%94%A8%E5%87%BD%E6%95%B0.html