objc中load方法详解

2019-06-18  本文已影响0人  字节码

原文在我的blog objc.com

+load方法简介

+load方法在这个类的文件被程序装载时调用。只要是在Compile Sources中出现的文件总是会被装载,不管这个类是否被用到过。在一个程序开始运行之前(在main函数开始执行之前),类文件开始被程序加载,+load方法就会开始被执行;因此+load方法总是在main函数之前调用。

疑问:

通过分析objc/runtime源码消除以上疑问。

声明

@interface NSObject <NSObject>
...
+ (void)load;
...
@end

示例代码:

@interface Person : NSObject
@end

@implementation Person

+ (void)load {
    NSLog(@"%s", __func__);
}
@end

@interface Woman : Person
@end

@implementation Woman

+ (void)load {
    NSLog(@"%s", __func__);
}
@end

@implementation Woman (XYExtension)
+ (void)load {
    NSLog(@"%s", __func__);
}

@end

@implementation Woman (XYExtension1)
+ (void)load {
    NSLog(@"%s", __func__);
}

@end

输出结果:

2019-06-18 15:23:59.862941+0800 objc-test[17947:205876] +[Person load]
2019-06-18 15:23:59.863319+0800 objc-test[17947:205876] +[Woman load]
2019-06-18 15:24:02.215828+0800 objc-test[17947:205876] +[Woman(XYExtension1) load]
2019-06-18 15:24:03.911969+0800 objc-test[17947:205876] +[Woman(XYExtension) load]

调用顺序

根据上面的示例可以看到,宿主类的load方法会先被调用,然后再调用分类的load方法,其中宿主类中父类的load方法先调用,非父子类型的按照Compile source中的顺序调用,分类的load方法根据Compile source中的顺序调用。

从我们可以看到源码的位置开始,依次调用的顺序为:

load_images -> call_load_methods -> call_class_loads -> +load

这三个函数的源码分别为:

/***********************************************************************
* load_images
* Process +load in the given images which are being mapped in by dyld.
*
* Locking: write-locks runtimeLock and loadMethodLock
**********************************************************************/
extern bool hasLoadMethods(const headerType *mhdr);
extern void prepare_load_methods(const headerType *mhdr);

void
load_images(const char *path __unused, const struct mach_header *mh)
{
    // 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();
}

/***********************************************************************
* call_class_loads
* Call all pending class +load methods.
* If new classes become loadable, +load is NOT called for them.
*
* Called only by call_load_methods().
**********************************************************************/
static void call_class_loads(void)
{
    int I;
    
    // Detach current loadable list.
    struct loadable_class *classes = loadable_classes;
    int used = loadable_classes_used;
    loadable_classes = nil;
    loadable_classes_allocated = 0;
    loadable_classes_used = 0;
    
    // Call all +loads for the detached list.
    for (i = 0; i < used; i++) {
        Class cls = classes[i].cls;
        load_method_t load_method = (load_method_t)classes[i].method;
        if (!cls) continue; 

        if (PrintLoading) {
            _objc_inform("LOAD: +[%s load]\n", cls->nameForLogging());
        }
        (*load_method)(cls, SEL_load);
    }
    
    // Destroy the detached list.
    if (classes) free(classes);
}

/***********************************************************************
* call_class_loads
* Call all pending class +load methods.
* If new classes become loadable, +load is NOT called for them.
*
* Called only by call_load_methods().
**********************************************************************/
static void call_class_loads(void)
{
    int I;
    
    // Detach current loadable list.
    struct loadable_class *classes = loadable_classes;
    int used = loadable_classes_used;
    loadable_classes = nil;
    loadable_classes_allocated = 0;
    loadable_classes_used = 0;
    
    // Call all +loads for the detached list.
    for (i = 0; i < used; i++) {
        Class cls = classes[i].cls;
        load_method_t load_method = (load_method_t)classes[i].method;
        if (!cls) continue; 

        if (PrintLoading) {
            _objc_inform("LOAD: +[%s load]\n", cls->nameForLogging());
        }
        (*load_method)(cls, SEL_load);
    }
    
    // Destroy the detached list.
    if (classes) free(classes);
}

我们找到load_images是在_objc_init函数中被调用的。

_objc_init的源码

/***********************************************************************
* _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;
    
    // fixme defer initialization until an objc-using image is found?
    environ_init();
    tls_init();
    static_init();
    lock_init();
    exception_init();

    _dyld_objc_notify_register(&map_images, load_images, unmap_image);
}

我们在_objc_init函数打断点,查看其调用栈,如下图

_objc_init的调用栈截图.png
发现一切都是从一个叫 _dyld_start 的方法开始的
load调用栈和以上函数的实现,可以看到load的执行过程:

程序启动运行时会依赖很多系统动态库,而系统动态库会通过dyld(动态加载器)(/usr/lib/dyld)加载到内存中,这时候就执行了_dyld_start,而在_dyld_start中进行了动态库加载和初始化runtime的工作。

_dyld_objc_notify_register这个方法在苹果开源的dyld里面可以找到,然后看到调用了dyld::registerObjCNotifiers这个方法:

void registerObjCNotifiers(_dyld_objc_notify_mapped mapped, _dyld_objc_notify_init init, _dyld_objc_notify_unmapped unmapped)
{
  // record functions to call
  sNotifyObjCMapped   = mapped;
  sNotifyObjCInit     = init;
  sNotifyObjCUnmapped = unmapped;

  // call 'mapped' function with all images mapped so far
       // 第一次先触发一次ObjCMapped
  try {
      notifyBatchPartial(dyld_image_state_bound, true, NULL, false, true);
  }
  catch (const char* msg) {
      // ignore request to abort during registration
  }
}

另外 +loadconstructor的执行时机是差不多的。

load_images函数的函数主要分为两部分

load_images函数线程安全的,它使用了mutex_locker加锁。

1.准备所有需要调用+load方法的类型。
void prepare_load_methods(const headerType *mhdr)
{
    size_t count, I;

    runtimeLock.assertLocked();

    classref_t *classlist = 
        _getObjc2NonlazyClassList(mhdr, &count);
    for (i = 0; i < count; i++) {
        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);
        if (!cls) continue;  // category for ignored weak-linked class
        realizeClass(cls);
        assert(cls->ISA()->isRealized());
        add_category_to_loadable_list(cat);
    }
}

2.调用准备好的类的+load方法。

我们先看下call_load_methods函数的实现:

void call_load_methods(void)
{
    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 more
        while (loadable_classes_used > 0) {
            // 调用宿主类的`+load`方法
            call_class_loads();
        }

        // 2. Call category +loads ONCE
        // 调用分类的`+load`方法
        more_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;
}

从函数的实现可以看出,使用do {}while循环遍历loadable_classes类别,并让每个类执行call_class_loads函数。然后执行call_category_loads函数调用分类的+load函数,这也是为什么宿主类的+load方法比分类的+load早调用的原因。

上一篇 下一篇

猜你喜欢

热点阅读