iOS自定义初始化方法

有很多初始化方法我们通常都是在applicationDidFinishLaunching里一个个调用,那么有没有办法像__attribute__((constructor)),能够自动调用被修饰的函数?

可以通过指定函数所在section的方式,然后获取section开头去逐个调用。但是这种方法有个问题,你只能获取到第一个函数的开头,但是你不知道它的大小,也就没有办法去依次获取了。

cpp 复制代码
__attribute__((used, section("__TEXT, CustomInit")))
void custom1() {
    NSLog(@"custom1");
}

__attribute__((used, section("__TEXT, CustomInit")))
void custom2() {
    NSLog(@"custom2");
}

- (void)callCustom
{
    Method orginalMethod = class_getClassMethod([self class], _cmd);
    IMP imp = method_getImplementation(orginalMethod);
    Dl_info info;
    if (dladdr((void *)imp, &info)) {
        printf("dli_fname: %s\n", info.dli_fname);
        printf("dli_sname: %s\n", info.dli_sname);
        printf("dli_fbase: %p\n", info.dli_fbase);
        printf("dli_saddr: %p\n", info.dli_saddr);
    } else {
        printf("error: can't find that symbol.\n");
    }
    
    unsigned long funcSize = 0;
    //CustomFunc段的开始
    uint8_t *funcStart = getsectiondata((struct mach_header_64 *)info.dli_fbase, "__TEXT", "CustomInit", &funcSize);
    ((void(*)())funcStart)();
}

所以可以通过声明全局或者静态指针变量,去持有这些函数,把这些指针放到__DATA里自定义的一个段里,因为指针大小都是8字节,每次跳8字节就能访问到下一个指针了。

cpp 复制代码
void custom1() {
    NSLog(@"custom1");
}

void(*customVar1)(void) __attribute__((used, section("__TEXT, CustomInit")))  = custom1;

void custom2() {
    NSLog(@"custom2");
}

void(*customVar2)(void) __attribute__((used, section("__TEXT, CustomInit")))  = custom1;


- (void)callCustom
{
    Method orginalMethod = class_getClassMethod([self class], _cmd);
    IMP imp = method_getImplementation(orginalMethod);
    Dl_info info;
    if (dladdr((void *)imp, &info)) {
        printf("dli_fname: %s\n", info.dli_fname);
        printf("dli_sname: %s\n", info.dli_sname);
        printf("dli_fbase: %p\n", info.dli_fbase);
        printf("dli_saddr: %p\n", info.dli_saddr);
    } else {
        printf("error: can't find that symbol.\n");
    }
    unsigned long size = 0;
    uint8_t *start = getsectiondata((struct mach_header_64 *)info.dli_fbase, "__DATA", "CustomInit", &size);
        int funcCount = size/sizeof(void *);
    for (int i=0;i<funcCount; i++) {
        //这里要用指针的指针
        void(**f)() = (void(**)())start + i;
        (*f)();
    }
}

这样声明函数和变量太麻烦了,我们可以使用宏来帮忙。

cpp 复制代码
#define CustomInitFuncBegin(funcName) \
void funcName() {

#define CustomInitFuncEnd(funcName) }\
static void (*funcName##_var)() __attribute__((used, section("__DATA, CustomInit")))  = funcName;


CustomInitFuncBegin(init1)
NSLog("测试init1\n");
CustomInitFuncEnd(init1)

CustomInitFuncBegin(init2);
NSLog("测试init2\n");
CustomInitFuncEnd(init2);


+ (void)callCustom
{
    Method orginalMethod = class_getClassMethod([self class], _cmd);
    IMP imp = method_getImplementation(orginalMethod);
    Dl_info info;
    if (dladdr((void *)imp, &info)) {
        printf("dli_fname: %s\n", info.dli_fname);
        printf("dli_sname: %s\n", info.dli_sname);
        printf("dli_fbase: %p\n", info.dli_fbase);
        printf("dli_saddr: %p\n", info.dli_saddr);
    } else {
        printf("error: can't find that symbol.\n");
    }
    unsigned long size = 0;
    uint8_t *start = getsectiondata((struct mach_header_64 *)info.dli_fbase, "__DATA", "CustomInit", &size);
    int funcCount = size/sizeof(void *);
    for (int i=0;i<funcCount; i++) {
        void(**f)() = (void(**)())start + i;
        (*f)();
    }
}

参考:

https://everettjf.github.io/2017/03/06/a-method-of-delay-premain-code/

相关推荐
*拯2 小时前
Uniapp Android/IOS 获取手机通讯录
android·ios·uni-app
天天打码5 小时前
Lynx-字节跳动跨平台框架多端兼容Android, iOS, Web 原生渲染
android·前端·javascript·ios
lilili啊啊啊6 小时前
iOS safari和android chrome开启网页调试与检查器的方法
android·ios·safari
名字不要太长 像我这样就好20 小时前
【iOS】源码阅读(二)——NSObject的alloc源码
开发语言·macos·ios·objective-c
I烟雨云渊T1 天前
iOS实名认证模块的具体实现过程(swift)
ios·cocoa·swift
小鹿撞出了脑震荡1 天前
汇编学习——iOS开发对arm64汇编的初步了解
汇编·学习·ios
小鹿撞出了脑震荡1 天前
「OC」源码学习—— 消息发送、动态方法解析和消息转发
学习·ios·objective-c
恋猫de小郭2 天前
Flutter 合并 ‘dot-shorthands‘ 语法糖,Dart 开始支持交叉编译
android·flutter·ios
恋猫de小郭2 天前
React Native 前瞻式重大更新 Skia & WebGPU & ThreeJS,未来可期
android·javascript·flutter·react native·react.js·ios
美狐美颜sdk2 天前
什么是直播美颜SDK?跨平台安卓、iOS美颜SDK开发实战详解
ios·音视频·美颜sdk·直播美颜sdk