iOS自定义初始化方法

news/2024/7/20 21:28:56 标签: ios, 自定义初始化, section

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

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

__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字节就能访问到下一个指针了。

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)();
    }
}

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

#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/


http://www.niftyadmin.cn/n/5477279.html

相关文章

boost库搜索引擎

文章目录 0. 前言1. 搜索引擎原理2. 技术栈和项目环境3. 正排索引和倒排索引3.1 正排索引3.2 倒排索引3.3 模拟查找 4. 获取数据源5. 数据清洗5.1 保存路径5.2 解析文件提取标题提取内容构造url 5.4 保存内容 6. 建立索引6.1 建立正排索引6.2 建立倒排索引6.3 构建索引 7. 搜索…

CSS - 浮动、定位

浮动 CSS浮动&#xff08;Float&#xff09;是一种布局技术&#xff0c;用于控制元素在页面中的位置。通过将元素浮动到其容器的左侧或右侧&#xff0c;可以使其他元素环绕在其周围。 相关属性&#xff1a; float&#xff1a;用于设置元素的浮动方向。可以设置为left&#xf…

研判特征流量

研判溯源应急 准备-检测-抑制-根除-恢复-跟踪总结 中级 内存马的判断和清除&#xff1a; 判断方式&#xff1a;先判断是通过什么方法注入的内存马&#xff0c;可以先查看web日志是否有可疑的web访问日志&#xff0c;如果是filter或者listener类型就会有大量url请求路径相同…

用TensorBoard可视化PyTorch

一、TensorBoard与PyTorch配合使用的基本步骤 PyTorch可以直接与TensorBoard进行集成&#xff0c;因为TensorBoard是一个独立于TensorFlow之外的可视化工具。TensorBoard被设计为支持机器学习实验的可视化&#xff0c;如训练的进度和结果等。PyTorch中的torch.utils.tensorboa…

网络基础三——IP协议补充

3.网段划分 3.1IP划分的前置知识 ​ IP地址分为两个部分&#xff0c;网络号和主机号&#xff1b;网络号&#xff1a;保证两个相互连接的网段具有不同的标识&#xff1b;主机号&#xff1a;同一个网段内&#xff0c;主机之间具有相同的网段号&#xff0c;但是必须具有不同的主…

CGAL的交叉编译-androidlinux-arm64

由于项目算法需要从Linux移植到android&#xff0c;原先的CGAL库也需要进行移植&#xff0c;先现对CGAL的移植过程做下记录&#xff0c;主要是其交叉编译的过程.。 前提条件&#xff1a; 1、主机已安装NDK编译器&#xff0c;版本大于19 2、主机已安装cmake 和 make 3、主机…

React - 请你说一说setState是同步的还是异步的

难度级别:中高级及以上 提问概率:70% 在React项目中,使用setState可以更新状态数据,而不能直接使用为this.state赋值的方式。而为了避免重复更新state数据,React首先将state添加到状态队列中,此时我们可以通过shouldComponentUpdate这个钩…

curaengine编译源码之libarcus编译记录

libArcus的编译&#xff08;成功安装&#xff09; This library contains C code and Python3 bindings for creating a socket in a thread and using this socket to send and receive messages based on the Protocol Buffers library. It is designed to facilitate the c…