Load 方法 暨 程序的加载顺序
前言
众所周知,App 的入口是 main
函数,而在此之前,我们了解到的是系统会自动调用 load
方法。而且是先调用父类的,再是自己的,最后才是分类的。而为什么是这样呢,不清楚。 下面所有的 load
方法, 都指 + (void)load {}
方法。
入口
借助于可调试的 objc 源码 了解了 load
方法的具体流程。
- 创建一个类
XXObject
,新建一个load
方法,打断点,调用栈显示,引出了dyld
和ImageLoader
。
dyld: The Dynamic Link Editor
Apple 的动态链接库,系统内核做好启动程序的初始准备后,将其他事物交给 dyld 处理
详细可以看 sunnyxx
ImageLoader:
images 表示二进制文件(可执行文件或者动态链接库 .so 文件)编译后的符号、代码等
ImageLoader 作用是将这些文件加载进内存,且每一个文件对应一个 ImageLoader 实例来负责加载
- 在程序运行时它先将动态链接的 image 递归加载
- 再从可执行文件 image 递归加载所有符号
load 流程
- 在分析 load 之前,还需要了解下 runtime 的初始化入口。
__objc_init
Bootstrap initialization. Registers our image notifier with dyld. Called by libSystem BEFORE library initialization time
void _objc_init(void)
{static bool initialized = false;if (initialized) return;initialized = true;// 环境初始化environ_init();tls_init();static_init();lock_init();// 初始化 libobjc 的异常处理系统exception_init();// 添加通知、回调_dyld_objc_notify_register(&map_2_images, load_images, unmap_image);
}
复制代码
- 引导初始化
- 注册通知,当二进制文件 images 加载到内存时,通知 runtime 进行处理。
map_2_images
:处理已经被 mapped 的imagesload_images
:处理 已被 mapped 的 images 中的 +load 方法unmap_image
:处理将要 unmap 的 images
1. load_images
Process +load in the given images which are being mapped in by dyld
- 处理 dyld 提供的已被 map_images 处理后的 images 中的 +load 方法
void
load_images(const char *path __unused, const struct mach_header *mh)
{// Return without taking locks if there are no +load methods here.// 如果没有 load 方法,直接返回,if (!hasLoadMethods((const headerType *)mh)) return;recursive_mutex_locker_t lock(loadMethodLock);// Discover load methods// 收集 load 方法,为下面调用做准备{rwlock_writer_t lock2(runtimeLock);// 准备所有的 load 方法prepare_load_methods((const headerType *)mh);}// Call +load methods (without runtimeLock - re-entrant)// 调用 load 方法call_load_methods();
}
复制代码
快速查询,类和分类的方法列表中是否含有
load
方法,如果没有,直接返回递归查询所有的
load
方法,并存储起来依次调用所有的
load
方法
1.2 prepare_load_methods
void prepare_load_methods(const headerType *mhdr)
{size_t count, i;runtimeLock.assertWriting();// 收集所有类的列表classref_t *classlist = _getObjc2NonlazyClassList(mhdr, &count);for (i = 0; i < count; i++) {// 收集当前类和父类的 load 方法,父类优先schedule_class_load(remapClass(classlist[i]));}// 获取所有的分类category_t **categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);for (i = 0; i < count; i++) {category_t *cat = categorylist[i];// 获取到分类对应的类的指针Class cls = remapClass(cat->cls);// 若链接返回 nil, 如果是 弱链接,就跳过if (!cls) continue; // category for ignored weak-linked class// 对类进行第一次初始化,包括 读写空间,返回真正的类结构realizeClass(cls);assert(cls->ISA()->isRealized());// 把 分类加入到一个全局列表中add_category_to_loadable_list(cat);}
}
复制代码
存储当前类和父类的所有
load
方法,其中父类优先存储分类的
load
方法
1.2.1 schedule_class_load
Schedule +load for classes in this image, any un-+load-ed superclasses in other images, and any categories in this image.
- 该方法是递归函数,找到未被加载的最顶级的父类,然后依次存储
static void schedule_class_load(Class cls)
{if (!cls) return;assert(cls->isRealized()); // _read_images should realize// A. 判断 类 的 load 方法 是否被调用if (cls->data()->flags & RW_LOADED) return;// Ensure superclass-first orderingschedule_class_load(cls->superclass);// 把 含有 load 方法的类 添加到 全局的 loadable_classesadd_class_to_loadable_list(cls);// 添加标记,对应 Acls->setInfo(RW_LOADED);
}
复制代码
1.2.2 add_class_to_loadable_list
Class cls has just become connected. Schedule it for +load if it implements a +load method
- 存储实现了
load
方法的类
void add_class_to_loadable_list(Class cls)
{// 方法指针IMP method;loadMethodLock.assertLocked();// 方法内部会根据 方法名字 判断是否 load 方法,并返回method = cls->getLoadMethod();if (!method) return; // Don't bother if cls has no +load methodif (PrintLoading) {_objc_inform("LOAD: class '%s' scheduled for +load", cls->nameForLogging());}if (loadable_classes_used == loadable_classes_allocated) {loadable_classes_allocated = loadable_classes_allocated*2 + 16;loadable_classes = (struct loadable_class *)realloc(loadable_classes,loadable_classes_allocated *sizeof(struct loadable_class));}loadable_classes[loadable_classes_used].cls = cls;loadable_classes[loadable_classes_used].method = method;loadable_classes_used++;
}
复制代码
根据 "load" 获取对应方法的指针。其实方法实质上也是个对象,有它自己的成员变量,如下:
struct method_t {SEL name; const char *types; IMP imp; } 复制代码
静态全局数组存储,如果数组已满,动态扩容
loadable_classes
:数组,里面元素是结构体loadable_class
,存储类名和方法指针。loadable_classes_used
:数组内对象的个数,即已经存储的对象数量loadable_classes_allocated
:数组大小
add_category_to_loadable_list
分类存储方法,几乎一致
2. call_load_methods
Call all pending class and category +load methods. Class +load methods are called superclass-first. Category +load methods are not called until after the parent class's +load
- 依次执行已经被存储的
load
方法
void call_load_methods(void)
{// loading 设置为全局静态变量,保证只初始化一次,// 一旦执行一次, loading 即为 YES,static bool loading = NO;bool more_categories;loadMethodLock.assertLocked();// Re-entrant calls do nothing; the outermost call will finish the job.if (loading) return;loading = YES;// 创建自动释放池,在自动释放池中进行方法调用void *pool = objc_autoreleasePoolPush();do {// 1. Repeatedly call class +loads until there aren't any morewhile (loadable_classes_used > 0) {call_class_loads();}// 2. Call category +loads ONCEmore_categories = call_category_loads();// 3. Run more +loads if there are classes OR more untried categories} while (loadable_classes_used > 0 || more_categories);objc_autoreleasePoolPop(pool);loading = NO;
}
复制代码
首先,保证是首次执行,
load
方法只会执行一次。创建自动释放池,在池内执行方法,优化性能
do {} while
循环执行,直到数组为空,且分类方法也执行完毕,不再有新的分类方法
3. call_class_loads
call_class_loads
方法比较简单,主要看分类方法的调用,这里涉及到在运行期间,后续又添加的load
分类方法
static bool call_category_loads(void)
{int i, shift;bool new_categories_added = NO;// Detach current loadable list.// 1. 分离并获取当前的分类 列表 catsstruct loadable_category *cats = loadable_categories;int used = loadable_categories_used;int allocated = loadable_categories_allocated;loadable_categories = nil;loadable_categories_allocated = 0;loadable_categories_used = 0;// Call all +loads for the detached list.// 2. for 循环 进行调用 load 方法,执行完毕后,把分类置空for (i = 0; i < used; i++) {Category cat = cats[i].cat;load_method_t load_method = (load_method_t)cats[i].method;Class cls;if (!cat) continue;cls = _category_getClass(cat);if (cls && cls->isLoadable()) {if (PrintLoading) {_objc_inform("LOAD: +[%s(%s) load]\n", cls->nameForLogging(), _category_getName(cat));}(*load_method)(cls, SEL_load);cats[i].cat = nil;}}// Compact detached list (order-preserving)// 3. 将加载过的分类方法移除 分离列表,保留未被加载过的 分类方法shift = 0;for (i = 0; i < used; i++) {if (cats[i].cat) {cats[i-shift] = cats[i];} else {shift++;}}used -= shift;// Copy any new +load candidates from the new list to the detached list.// 运行过程中是否有新添加的分类方法new_categories_added = (loadable_categories_used > 0);// 4. 如果有,先存储在 分离列表 cats for (i = 0; i < loadable_categories_used; i++) {if (used == allocated) {allocated = allocated*2 + 16;cats = (struct loadable_category *)realloc(cats, allocated *sizeof(struct loadable_category));}cats[used++] = loadable_categories[i];}// Destroy the new list.// 释放清空全局列表,以便后面重新赋值if (loadable_categories) free(loadable_categories);// Reattach the (now augmented) detached list. // But if there's nothing left to load, destroy the list.// 5. 是否存在有新列表,并赋值给全局静态存储变量if (used) {loadable_categories = cats;loadable_categories_used = used;loadable_categories_allocated = allocated;} else {if (cats) free(cats);loadable_categories = nil;loadable_categories_used = 0;loadable_categories_allocated = 0;}if (PrintLoading) {if (loadable_categories_used != 0) {_objc_inform("LOAD: %d categories still waiting for +load\n",loadable_categories_used);}}return new_categories_added;
}
复制代码
上述代码中详细给出了5步,判断是否还有更多的分类方法,来决定是否继续在
while
循环中执行。这里着重提下,由于 OC 运行时的机制,系统之前已经收集完所有的
load
方法,并且正在执行load
方法的时候,又有含有load
方法的分类被添加进来,所以在执行分类的时候,又多出来 3、4、5 步,来保证所有的分类实现完毕。
4. load
+ (void)load
{NSLog(@"Load Hello World");
}
复制代码
总结:
从方法调用栈中,找到了系统在执行 load
前调用的方法:
启动 dyld,将二进制文件初始化
ImageLoader
把二进制文件加载进内存runtime 执行
load_images
,执行所有的load
方法- 使用一个全局数组从含有
load
方法的根父类到自身,依次添加 - 使用另一个全局数组添加含有
load
方法的所有分类 - 依次执行存储的
load
方法,父类 -> 自身 -> 分类
- 使用一个全局数组从含有
执行自定义的
load
方法
参考
load 的简单了解
iOS 程序 main 函数之前发生了什么
你真的了解 load 方法么?
load 方法全程跟踪
Load 方法 暨 程序的加载顺序相关推荐
- jsp ajax加载html页面,Ajax中的load()方法实现指定区域加载或刷新html与jsp
一.Jquery中的Ajax Jquery对Ajax操作进行了封装,在Jquery中: 最底层的是$.ajsx(), 第二层是load().$.get().$.post() 第三层是$.getJSON ...
- 【linux】程序找不到动态库.so的解决办法|查看.so动态库信息|.so动态库加载顺序
目录 找不到.so解决方法 方法一:添加环境变量 方法二:复制so文件到lib路径 方法三:(推荐)添加ldconfig寻找路径 方法四:在编译目标代码时指定该程序的动态库搜索路径 让程序在本目录找到 ...
- java静态方法加载顺序_java中类以及方法的加载顺序
先看下面的代码: public class Test { public static int a =0 ; static { a = 10; System.out.println("静态代码 ...
- iptables加载顺序问题及优化方法
iptables加载顺序问题及优化方法 1. iptables加载顺序问题及优化方法 1.1. 1. 问题背景 1.2. 2. 问题排查 1.3. 3. 根因分析 1.4. 4. 附带影响 1.5. ...
- Spring Boot程序无法加载主类解决方法
平时在打包war包程序后,经当再次右击程序"Run As --> Spring Boot App"时,控制台有时候会出现"应用程序无法加载主类"的报错,解 ...
- 小程序页面onload(),onready()加载顺序
小程序 onLoad(Object query) 页面加载时触发.一个页面只会调用一次,可以在 onLoad 的参数中获取打开当前页面路径中的参数. onShow() 页面显示/切入前台时触发. on ...
- JAVA方法加载顺序
JAVA方法加载顺序 类加载机制 类加载器的任务就是根据一个类的全限定名来读取次类的二进制字节流到JVM转换成 java.lang,class类 类加载过程:加载.验证.准备.解析和初始化. 双亲委托 ...
- Java类中方法的加载顺序
1 父类 public class Father {static {System.out.println("父类静态初始化块1");}public static int fsm = ...
- java中类的加载顺序
java中类加载顺序: 1)静态代码块只执行一次:静态代码块首先被初始化 2)构造代码块在每次创建对象都会执行:构造函数都是最后执行的. 3)按照父子类继承关系进行初始化,先执行父类的初始化: 4)程 ...
最新文章
- 【Dual-Path-RNN-Pytorch源码分析】Dual_RNN_Block
- forward declaration of class 错误
- 机构押注ABS云平台 融资成本下降1%~3%
- SpringBoot 热部署
- 领域驱动设计,让程序员心中有码(二)
- 十行Python以内代码能有什么高端操作?
- Linux 监视文件、文件夹改动
- Jmeter 时间函数工具汇总
- Spring系列:学习Spring的资源和讨论
- C++/CLI C#字符串转为C++字符串正确写法
- 如何在 Git 里撤销(几乎)任何操作
- .xls和.xlsx 有什么区别?
- 高速EDA设计课程报告(三)
- 二级造价师课件网课下载,二级造价工程师考试时间报考条件公布!
- 编程常用单词1500个
- 怎么用svg画一个圆圈(一)
- VBA中的MsgBox函数使用
- Python - 列表补充(二)
- so链接及动态加载原理分析
- python小白学习第三节