load_images分析
- 在objc源码中,查看load_images源码,查看 prepare_load_methods和call_load_methods方法
void
load_images(const char *path __unused, const struct mach_header *mh)
{
if (!didInitialAttachCategories && didCallDyldNotifyRegister) {
didInitialAttachCategories = true;
//加载分类
loadAllCategories();
}
// Return without taking locks if there are no +load methods here.
if (!hasLoadMethods((const headerType *)mh)) return;
recursive_mutex_locker_t lock(loadMethodLock);
// Discover load methods 查找方法
{
mutex_locker_t lock2(runtimeLock);
prepare_load_methods((const headerType *)mh);
}
// Call +load methods (without runtimeLock - re-entrant)
//调用方法
call_load_methods();
}
👇
查找load方法并加载
- 查看prepare_load_methods源码,分别获取到非懒加载类的列表和非懒加载分类的列表,并循环加载load方法。查看schedule_class_load方法。
void prepare_load_methods(const headerType *mhdr)
{
size_t count, i;
runtimeLock.assertLocked();
//非懒加载类的列表
classref_t const *classlist =
_getObjc2NonlazyClassList(mhdr, &count);
//循环加载非懒加载类的load方法
for (i = 0; i < count; i++) {
schedule_class_load(remapClass(classlist[i]));
}
//获取非懒加载分类
category_t * const *categorylist = _getObjc2NonlazyCategoryList(mhdr, &count);
for (i = 0; i < count; i++) {
category_t *cat = categorylist[i];
Class cls = remapClass(cat->cls);
if (!cls) continue; // category for ignored weak-linked class
if (cls->isSwiftStable()) {
_objc_fatal("Swift class extensions and categories on Swift "
"classes are not allowed to have +load methods");
}
realizeClassWithoutSwift(cls, nil);
ASSERT(cls->ISA()->isRealized());
add_category_to_loadable_list(cat)