类加载

作者: 暴躁的小豆子 | 来源:发表于2020-07-28 19:50 被阅读0次

    上篇说了,app加载的流程,接下来我们看看 objc_init,从苹果开发文档中下载源码objc4,这里是objc4-779.1版本

    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();
        //关于线程key的绑定--比如每线程数据的析构函数
        tls_init();
        //运行系统的C++静态构造函数,在dyld调用我们的静态构造函数之前,libc会调用_objc_init(),所以我们必须自己做
        static_init();
        runtime_init();
         //初始化异常处理系统,比如注册异常的回调函数,来监控异常
        exception_init();
        cache_init();
        _imp_implementationWithBlock_init();
     //仅供objc运行时使用,注册处理程序,以便在映射、取消映射和初始化objc镜像文件时调用
        _dyld_objc_notify_register(&map_images, load_images, unmap_image);
    
    #if __OBJC2__
        didCallDyldNotifyRegister = true;
    #endif
    }
    

    我们来说一下_dyld_objc_notify_register,其他你也可以下载源码看一下,我们在objc中注册函数回调,那必然会有调用者,这里调用者大家可能已经猜到,对,就是dyld这个动态链接库,在dyld库中搜索_dyld_objc_notify_register:

    void _dyld_objc_notify_register(_dyld_objc_notify_mapped    mapped,
                                    _dyld_objc_notify_init      init,
                                    _dyld_objc_notify_unmapped  unmapped)
    {
        dyld::registerObjCNotifiers(mapped, init, unmapped);
    }
    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;
    ...
    }
    

    上面的方法保存了三个函数指针,其作用是为了在某一时刻调用,根据源码分析

    • sNotifyObjCMapped是在notifyBatchPartial函数中调用的
    • sNotifyObjCInit 是在notifySingle函数中调用的
      下面我们来看看 objc中具体是怎么定义的函数(map_images,load_images)
    map_images(unsigned count, const char * const paths[],
               const struct mach_header * const mhdrs[])
    {
        mutex_locker_t lock(runtimeLock);
        return map_images_nolock(count, paths, mhdrs);
    }
    void 
    map_images_nolock(unsigned mhCount, const char * const mhPaths[],
                      const struct mach_header * const mhdrs[]){
    ...
            _read_images(hList, hCount, totalClasses, unoptimizedTotalClasses);
    
    ...
    }
    

    从上面代码可以看出经过一系列的函数调用栈,会调用_read_images,我们点进去看看

    void _read_images{
        
        //第一次进来 创建表   所有类的表 实现的+未实现的 gdb_objc_realized_classes
        //之前的版本 allocatedClasses 表也是在这里创建的  这个版本是在objc_init中runtime_init()中创建的
        //gdb_objc_realized_classes 包含 allocatedClasses   allocatedClasses是以分配的类 元类
        //1
        if (!doneOnce) {}
        //2 方法编号处理
        for (EACH_HEADER) {}
        //3 类处理
        for (i = 0; i < count; i++) {}
        //4 协议处理
        for (EACH_HEADER) {}
        //5 分类处理
        for (EACH_HEADER) {}
        //6 非加载类处理
        for (EACH_HEADER) {}
        //7 待处理的类
        if (resolvedFutureClasses) {}
        
    }
    

    这里直接看加载非懒加载类

    void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClasses)
    {
        //加载非懒加载类
        for (EACH_HEADER) {
            classref_t const *classlist = 
                _getObjc2NonlazyClassList(hi, &count);
            for (i = 0; i < count; i++) {
                Class cls = remapClass(classlist[i]);
                if (!cls) continue;
    
                addClassTableEntry(cls);
    
                if (cls->isSwiftStable()) {
                    if (cls->swiftMetadataInitializer()) {
                        _objc_fatal("Swift class %s with a metadata initializer "
                                    "is not allowed to be non-lazy",
                                    cls->nameForLogging());
                    }
                    // fixme also disallow relocatable classes
                    // We can't disallow all Swift classes because of
                    // classes like Swift.__EmptyArrayStorage
                }            
                realizeClassWithoutSwift(cls,nil);
                //添加一行
                _objc_inform("non lazy class realized: %s",cls->nameForLogging());
            }
        }
    }
    

    addClassTableEntry(cls) 是类及其元类添加到allocatedClasses这张表中
    realizeClassWithoutSwift函数太长,我做了一次修剪,留下主要流程:

    static Class realizeClassWithoutSwift(Class cls, Class previously)
    {
    
        const class_ro_t *ro;
        class_rw_t *rw;
        Class supercls;
        Class metacls;
        bool isMeta;
    
        /*省略*/
        ro = (const class_ro_t *)cls->data();
        rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1);
        rw->ro = ro;
        rw->flags = RW_REALIZED|RW_REALIZING;
        cls->setData(rw);
        
       /*省略*/
       
        supercls = realizeClassWithoutSwift(remapClass(cls->superclass), nil);
        metacls = realizeClassWithoutSwift(remapClass(cls->ISA()), nil);
    
         /*省略*/
    
        // Update superclass and metaclass in case of remapping
        cls->superclass = supercls;
        cls->initClassIsa(metacls);
        
         /*省略*/
        
        // Set fastInstanceSize if it wasn't set already.
        cls->setInstanceSize(ro->instanceSize);
    
        /*省略*/
    
        // Connect this class to its superclass's subclass lists
        if (supercls) {
            addSubclass(supercls, cls);
        } else {
            addRootClass(cls);
        }
    
        // Attach categories
        methodizeClass(cls, previously);
    
        return cls;
    }
    
    

    先说函数头注释

    • 执行类的首次初始化
    • 为rw分配内存
    • 返回真正的cls结构体

    第11~15
    1 给rw分配内存
    2 把ro赋给rw局部变量
    3 赋给类的rw
    第19~20行,沿着类的继承链以及元类的继承链递归调用realizeClass()函数,使继承链中的所有父类都完成上述操作。
    第25~26行,给当前类的superclass成员赋值(儿子认爹),然后初始化类的isa成员。
    第31行,将ro中记录的实例大小信息写入rw中。
    第37行,将当前class添加为父类的subclass(爹认儿子),到这里父子相认了。
    第43行,开始处理类的方法列表,最后返回类的结构体实例。

    前面是从镜像读取ro数据,并将类架子填充好,下面开始为类赋值methodizeClass

       static void methodizeClass(Class cls, Class previously)
    {
    
        bool isMeta = cls->isMetaClass();
        auto rw = cls->data();
        auto ro = rw->ro;
        // Install methods and properties that the class implements itself.
        method_list_t *list = ro->baseMethods();
        if (list) {
            prepareMethodLists(cls, &list, 1, YES, isBundleClass(cls));
            rw->methods.attachLists(&list, 1);
        }
    
        property_list_t *proplist = ro->baseProperties;
        if (proplist) {
            rw->properties.attachLists(&proplist, 1);
        }
    
        protocol_list_t *protolist = ro->baseProtocols;
        if (protolist) {
            rw->protocols.attachLists(&protolist, 1);
        }
    
        // Root classes get bonus method implementations if they don't have
        // them already. These apply before category replacements.
        if (cls->isRootMetaclass()) {
            // root metaclass
            addMethod(cls, @selector(initialize), (IMP)&objc_noop_imp, "", NO);
        }
    
        // Attach categories.
        if (previously) {
            if (isMeta) {
                objc::unattachedCategories.attachToClass(cls, previously,
                                                         ATTACH_METACLASS);
            } else {
                // When a class relocates, categories with class methods
                // may be registered on the class itself rather than on
                // the metaclass. Tell attachToClass to look for those.
                objc::unattachedCategories.attachToClass(cls, previously,
                                                         ATTACH_CLASS_AND_METACLASS);
            }
        }
        objc::unattachedCategories.attachToClass(cls, cls,
                                                 isMeta ? ATTACH_METACLASS : ATTACH_CLASS);
    }
    
    
    • 依次从ro中读取出方法列表、属性列表、协议列表,写入rw中
    • 对根元类做一个特殊处理
    • 获取分类列表,并将分类中方法列表、属性列表以及协议列表写入rw中

    添加原理

    void attachLists(List* const * addedLists, uint32_t addedCount) {
    if (addedCount == 0) return;

        if (hasArray()) {
            // many lists -> many lists
            uint32_t oldCount = array()->count;
            uint32_t newCount = oldCount + addedCount;
            setArray((array_t *)realloc(array(), array_t::byteSize(newCount)));
            array()->count = newCount;
            memmove(array()->lists + addedCount, array()->lists, 
                    oldCount * sizeof(array()->lists[0]));
            memcpy(array()->lists, addedLists, 
                   addedCount * sizeof(array()->lists[0]));
        }
        else if (!list  &&  addedCount == 1) {
            // 0 lists -> 1 list
            list = addedLists[0];
        } 
        else {
            // 1 list -> many lists
            List* oldList = list;
            uint32_t oldCount = oldList ? 1 : 0;
            uint32_t newCount = oldCount + addedCount;
            setArray((array_t *)malloc(array_t::byteSize(newCount)));
            array()->count = newCount;
            if (oldList) array()->lists[addedCount] = oldList;
            memcpy(array()->lists, addedLists, 
                   addedCount * sizeof(array()->lists[0]));
        }
    }
    
    • 当前列表为空时,直接将list指向新列表的首地址;
    • 当前列表只有一个时,根据newCount新开辟一块内存(malloc()),将原来的一个列表放在最后,然后拷贝待添加的列表到前面(memcpy()头部插入)。
    • 当前已经有多个列表时,先根据newCount在原来的内存基础上扩容(realloc()),然后将原来的列表通过memmove()拷贝到扩容后内存空间的尾部,再通过memcpy()将待添加列表拷贝到前面。

    realloc()函数会在原先内存空间的基础上,继续向后开辟(扩容时),如果后面的内存够用,则扩容成功,仍然返回原内存空间的起始地址;如果后面的连续内存空间不够,则在堆上重新寻找开辟一块newCount大小的空间,并将原空间的内容copy过去,返回新开辟的空间地址。

    总结
    • 当dyld加载链接完所有的库,objc开始加载类
    • 从可执行二进制文件Mach-o中读取方法列表,协议列表,属性列表,等原始数据写入ro,
    • ro依次对应赋值给rw(rw对ro是包含的关系)
    • 获取类的分类信息,将分类中的方法列表、属性列表、协议列表写入rw中

    load_images

    下面说第二个函数 load_images

    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();
    }
    
    
    • 1 prepare_load_methods 准备阶段
    void prepare_load_methods(const headerType *mhdr)
    {
        size_t count, i;
    
        runtimeLock.assertLocked();
    
        classref_t const *classlist = 
            _getObjc2NonlazyClassList(mhdr, &count);
        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);
        }
    }
    

    第7~11行
    遍历类的列表,递归调用,保证父类先调用load方法,然后依次添加到loadable_classes
    第13~行
    遍历分类列表,确定初始化过,添加到loadable_categories

    • 懒加载的类在编译阶段已经确定了,所以这里获取的是非懒加载类列表
    struct loadable_class {
        Class cls;  // may be nil
        IMP method;
    };
    
    struct loadable_category {
        Category cat;  // may be nil
        IMP method;
    };
    

    添加到两个数组中的都是各自类型的结构体

    来看一下添加的方法,分类跟类的方法类似,拿类的添加方法来看

    void add_class_to_loadable_list(Class cls)
    {
        IMP method;
    
        loadMethodLock.assertLocked();
    
        method = cls->getLoadMethod();
        if (!method) return;  // Don't bother if cls has no +load method
        
        if (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方法,没有就返回,然后2倍扩容来创建loadable_classes数组,将类跟方法包装成结构体存入loadable_classes

    2 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) {
                call_class_loads();
            }
    
            // 2. Call category +loads ONCE
            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;
    }
    

    call_class_loads()跟 call_category_loads() 调用方法类似都是遍历取出方法,然后执行。

     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, @selector(load));
        }
        
        // Destroy the detached list.
        if (classes) free(classes);
    }
    
    • 这里说明一下 由于先执行的类的load方法,在执行分类的load方法,上面有说到类是确保父类先调用,所以load的方法调用顺序是 父类->子类->分类

    相关文章

      网友评论

          本文标题:类加载

          本文链接:https://www.haomeiwen.com/subject/ppntlktx.html