美文网首页
iOS原理探索10-应用程序的加载流程

iOS原理探索10-应用程序的加载流程

作者: HardCabbage | 来源:发表于2020-11-04 17:20 被阅读0次

    首先我们来看一个案例,viewController添加load方法main.m文件添加一个c++函数,思考一下load方法,main函数、c++函数的执行顺序是什么?

    viewController添加load方法,main.m文件添加一个c++函数 运行程序,我们发现打印的结果为 运行结果 从结果可以看出load方法,main函数、c++函数的执行顺序为:C++ -> load -> main,这是为什么呢?一般的程序入口不应该是main函数吗?这就 是本篇内容研究的重点dyld应用程序的加载流程

    概念补充:

    静态库:在链接阶段,将汇编生成的目标文件和引用的一些库一起链接打包到可执行文件当中。
    优点:编译完成后库就没有用处了,可以直接运行,并且目标程序没有外部的依赖。
    缺点:使用静态库会目标程序的体积增加,对内存、性能消耗比较大。
    动态库:程序在编译时不会看链接到目标程序,目标程序只会存储指向一些动态库的引用,在程序运行是才会被加载进来。

    优点:可以减少目标程序的体积;并且共享内存节约资源;由于运行时才会被载入,所以可以动态的更改动态库,达到更新程序目的。

    缺点:由于动态库在运行时才会被加载,所以程序就比较依赖外界的环境,如果外界动态库缺失错误就会造成程序错误。

    dyld链接器

    名词解释:dyld是苹果的动态链接器,苹果操作系统重要的组成部分,在app被编译打包成可执行文件格式的Mach-O文件后,交由dyld负责链接,加载程序

    • 在案例demo的load方法添加一个断点,通过堆栈信息来看一下APP启动的第一个函数。 堆栈信息 从堆栈信息可以看出,程序启动调用的第一个函数为_dyld_start (),下面就根据dyld源码进行分析
    • 在dyld-750.6源码中全局搜索_dyld_start,发现是有汇编写的,我们可以根据汇编注释发现,调用的是dyldbootstrap::start(app_mh, argc, argv, dyld_mh, &startGlue)方法, `_dyld_start`汇编
    • 在源码中搜索dyldbootstrap命名空间的start方法查看方法的实现流程,源码如下
    uintptr_t start(const dyld3::MachOLoaded* appsMachHeader, int argc, const char* argv[],
                    const dyld3::MachOLoaded* dyldsMachHeader, uintptr_t* startGlue)
    {
    
        // Emit kdebug tracepoint to indicate dyld bootstrap has started <rdar://46878536>
        dyld3::kdebug_trace_dyld_marker(DBG_DYLD_TIMING_BOOTSTRAP_START, 0, 0, 0, 0);
    
        // if kernel had to slide dyld, we need to fix up load sensitive locations
        // we have to do this before using any global variables
        rebaseDyld(dyldsMachHeader);
    
        // kernel sets up env pointer to be just past end of agv array
        const char** envp = &argv[argc+1];
        
        // kernel sets up apple pointer to be just past end of envp array
        const char** apple = envp;
        while(*apple != NULL) { ++apple; }
        ++;
    
        // set up random value for stack canary
        __guard_setup();
    
    #if DYLD_INITIALIZER_SUPPORT
        // run all C++ initializers inside dyld
        runDyldInitializers(argc, argv, envp, );
    #endif
    
        // now that we are done bootstrapping dyld, call dyld's main
        uintptr_t appsSlide = appsMachHeader->getSlide();
        return dyld::_main((macho_header*)appsMachHeader, appsSlide, argc, argv, envp, apple, startGlue);
    }
    

    核心返回的是_main方法,其中macho_headermacho文件的头,而dyld加载的文件就是Mach-O类型的,即Mach-O类型可执行文件类型

    • 进入_mian函数,发现总共大约600多行,如果对dyld加载流程不太了解的童鞋,可以根据_main函数的返回值进行反推。main函数主要做了以下几件事情。

      mian函数
      • 【第一步】环境变量配置
    //检查设置的环境变量
    checkEnvironmentVariables(envp);
    //如果DYLD_FALLBACK为nil,将其设置为默认值
    defaultUninitializedFallbackPaths(envp);
    
    .....
    
    //获取当前运行环境架构的信息
    getHostInfo(mainExecutableMH, mainExecutableSlide);
    
    
    • 【第二步:共享缓存】:检查是否开启了共享缓存,以及共享缓存是否映射到了共享区域,例如UIKit、CoreFoundation
        //检查共享缓存是否开启,在iOS中必须开启
        checkSharedRegionDisable((dyld3::MachOLoaded*)mainExecutableMH, mainExecutableSlide);
        if ( gLinkContext.sharedRegionMode != ImageLoader::kDontUseSharedRegion ) {
    #if TARGET_OS_SIMULATOR
            if ( sSharedCacheOverrideDir)
                //检查共享缓存是否映射到了共享区域
                mapSharedCache();
    #else
            mapSharedCache();
    #endif
        }
    
        // If we haven't got a closure mode yet, then check the environment and cache type
        //如果还没有关闭模式,请检查环境和缓存类型
        if ( sClosureMode == ClosureMode::Unset ) {
            // First test to see if we forced in dyld2 via a kernel boot-arg
            if ( dyld3::BootArgs::forceDyld2() ) {
                sClosureMode = ClosureMode::Off;
            } else if ( inDenyList(sExecPath) ) {
                sClosureMode = ClosureMode::Off;
            } else if ( sEnv.hasOverride ) {
                sClosureMode = ClosureMode::Off;
            } else if ( dyld3::BootArgs::forceDyld3() ) {
                sClosureMode = ClosureMode::On;
            } else {
                sClosureMode = getPlatformDefaultClosureMode();
            }
        }
    
    • 【第三步】主程序的初始化
            //加载可执行文件,并生成一个imageLoader实例对象
            sMainExecutable = instantiateFromLoadedImage(mainExecutableMH, mainExecutableSlide, sExecPath);
            gLinkContext.mainExecutable = sMainExecutable;
            gLinkContext.mainExecutableCodeSigned = hasCodeSignatureLoadCommand(mainExecutableMH);
    
    重点分析

    (1)sMainExecutable表示主程序变量,查看其赋值,是通过instantiateFromLoadedImage方法初化的,进入instantiateFromLoadedImage源码,其中创建一个ImageLoader实例对象,通过instantiateMainExecutable方法创建

    static ImageLoaderMachO* instantiateFromLoadedImage(const macho_header* mh, uintptr_t slide, const char* path)
    {
        // try mach-o loader
        if ( isCompatibleMachO((const uint8_t*)mh, path) ) {
            ImageLoader* image = ImageLoaderMachO::instantiateMainExecutable(mh, slide, path, gLinkContext);
            addImage(image);
            return (ImageLoaderMachO*)image;
        }
        
        throw "main executable not a known format";
    }
    
    
    

    (2)instantiateMainExecutable源码,为主程序创建镜像,返回一个ImageLoader类型的image对象,即主程序。其中sniffLoadCommands函数时获取Mach-O类型文件的Load Command的相关信息,并对其进行各种校验

    ImageLoader* ImageLoaderMachO::instantiateMainExecutable(const macho_header* mh, uintptr_t slide, const char* path, const LinkContext& context)
    {
        //dyld::log("ImageLoader=%ld, ImageLoaderMachO=%ld, ImageLoaderMachOClassic=%ld, ImageLoaderMachOCompressed=%ld\n",
        //  sizeof(ImageLoader), sizeof(ImageLoaderMachO), sizeof(ImageLoaderMachOClassic), sizeof(ImageLoaderMachOCompressed));
        bool compressed;
        unsigned int segCount;
        unsigned int libCount;
        const linkedit_data_command* codeSigCmd;
        const encryption_info_command* encryptCmd;
        //确定此mach-o文件是否具有经典或压缩的LINKEDIT以及其具有的段数
        sniffLoadCommands(mh, path, false, &compressed, &segCount, &libCount, context, &codeSigCmd, &encryptCmd);
        // instantiate concrete class based on content of load commands
        if ( compressed ) 
            return ImageLoaderMachOCompressed::instantiateMainExecutable(mh, slide, path, segCount, libCount, context);
        else
    #if SUPPORT_CLASSIC_MACHO
            return ImageLoaderMachOClassic::instantiateMainExecutable(mh, slide, path, segCount, libCount, context);
    #else
            throw "missing LC_DYLD_INFO load command";
    #endif
    }
    
    • 【第四步:插入动态库】
            //加载所有DYLD_INSERT_LIBRARIES指定的库
            if  ( sEnv.DYLD_INSERT_LIBRARIES != NULL ) {
                for (const char* const* lib = sEnv.DYLD_INSERT_LIBRARIES; *lib != NULL; ++lib) 
                    loadInsertedDylib(*lib);
            }
            // record count of inserted libraries so that a flat search will look at 
            // inserted libraries, then main, then others.
            sInsertedDylibCount = sAllImages.size()-1;
    
            // link main executable
            gLinkContext.linkingMainExecutable = true;
    #if SUPPORT_ACCELERATE_TABLES
            if ( mainExcutableAlreadyRebased ) {
                // previous link() on main executable has already adjusted its internal pointers for ASLR
                // work around that by rebasing by inverse amount
                sMainExecutable->rebase(gLinkContext, -mainExecutableSlide);
            }
    #endif
    
    • 【第五步:link主程序】
    //链接主程序
            link(sMainExecutable, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL), -1);
            sMainExecutable->setNeverUnloadRecursive();
            if ( sMainExecutable->forceFlat() ) {
                gLinkContext.bindFlat = true;
                gLinkContext.prebindUsage = ImageLoader::kUseNoPrebinding;
            }
    
    • 【第六步:link动态库】
        if ( sInsertedDylibCount > 0 ) {
                for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
                    ImageLoader* image = sAllImages[i+1];
                    //链接可执行文件后执行此操作,这样插入的dylib(例如libSystem)插入的dylib不会在程序使用的dylib的前面
                    link(image, sEnv.DYLD_BIND_AT_LAUNCH, true, ImageLoader::RPathChain(NULL, NULL), -1);
                    image->setNeverUnloadRecursive();
                }
                if ( gLinkContext.allowInterposing ) {
                    // only INSERTED libraries can interpose:只有插入的库可以插入
                    // register interposing info after all inserted libraries are bound so chaining works
                    //绑定所有插入的库后,注册插入信息,以便链接工作
                    for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
                        ImageLoader* image = sAllImages[i+1];
                        //注册符号插入
                        image->registerInterposing(gLinkContext);
                    }
                }
            }
    
    • 【第七步:弱符号绑定】
            // apply interposing to initial set of images
            for(int i=0; i < sImageRoots.size(); ++i) {
                //应用符号插入
                sImageRoots[i]->applyInterposing(gLinkContext);
            }
            ImageLoader::applyInterposingToDyldCache(gLinkContext);
    
            // Bind and notify for the main executable now that interposing has been registered
            //绑定并通知主要可执行文件,现在插入已注册
            uint64_t bindMainExecutableStartTime = mach_absolute_time();
            sMainExecutable->recursiveBindWithAccounting(gLinkContext, sEnv.DYLD_BIND_AT_LAUNCH, true);
            uint64_t bindMainExecutableEndTime = mach_absolute_time();
            ImageLoaderMachO::fgTotalBindTime += bindMainExecutableEndTime - bindMainExecutableStartTime;
            gLinkContext.notifyBatch(dyld_image_state_bound, false);
    
            // Bind and notify for the inserted images now interposing has been registered
            //绑定并通知已插入镜像已被注册
            if ( sInsertedDylibCount > 0 ) {
                for(unsigned int i=0; i < sInsertedDylibCount; ++i) {
                    ImageLoader* image = sAllImages[i+1];
                    image->recursiveBind(gLinkContext, sEnv.DYLD_BIND_AT_LAUNCH, true);
                }
            }
            
            // <rdar://problem/12186933> do weak binding only after all inserted images linked
            //弱符号绑定
            sMainExecutable->weakBind(gLinkContext);
            gLinkContext.linkingMainExecutable = false;
    
            sMainExecutable->recursiveMakeDataReadOnly(gLinkContext);
    
    • 【第八步:执行初始化方法】
      执行初始化方法 ######重点分析
      (1)initializeMainExecutable源码,主要是循环遍历,都会执行runInitializers方法
    void initializeMainExecutable()
    {
        // record that we've reached this step
        gLinkContext.startedInitializingMainExecutable = true;
    
        // run initialzers for any inserted dylibs
        //  主要是循环遍历,都会执行runInitializers方法
        ImageLoader::InitializerTimingList initializerTimes[allImagesCount()];
        initializerTimes[0].count = 0;
        const size_t rootCount = sImageRoots.size();
        if ( rootCount > 1 ) {
            for(size_t i=1; i < rootCount; ++i) {
                sImageRoots[i]->runInitializers(gLinkContext, initializerTimes[0]);
            }
        }
        
        // run initializers for main executable and everything it brings up 
        sMainExecutable->runInitializers(gLinkContext, initializerTimes[0]);
        
        // register cxa_atexit() handler to run static terminators in all loaded images when this process exits
        if ( gLibSystemHelpers != NULL ) 
            (*gLibSystemHelpers->cxa_atexit)(&runAllStaticTerminators, NULL, NULL);
    
        // dump info if requested
        if ( sEnv.DYLD_PRINT_STATISTICS )
            ImageLoader::printStatistics((unsigned int)allImagesCount(), initializerTimes[0]);
        if ( sEnv.DYLD_PRINT_STATISTICS_DETAILS )
            ImageLoaderMachO::printStatisticsDetails((unsigned int)allImagesCount(), initializerTimes[0]);
    }
    

    (2)``runInitializers`源码实现

    void ImageLoader::runInitializers(const LinkContext& context, InitializerTimingList& timingInfo)
    {
        uint64_t t1 = mach_absolute_time();
        mach_port_t thisThread = mach_thread_self();
        ImageLoader::UninitedUpwards up;
        up.count = 1;
        up.imagesAndPaths[0] = { this, this->getPath() };
        processInitializers(context, thisThread, timingInfo, up);
        context.notifyBatch(dyld_image_state_initialized, false);
        mach_port_deallocate(mach_task_self(), thisThread);
        uint64_t t2 = mach_absolute_time();
        fgTotalInitTime += (t2 - t1);
    }
    

    (3)processInitializers源码实现,其中对镜像列表调用recursiveInitialization函数进行递归实例化

                                         InitializerTimingList& timingInfo, ImageLoader::UninitedUpwards& images)
    {
        uint32_t maxImageCount = context.imageCount()+2;
        ImageLoader::UninitedUpwards upsBuffer[maxImageCount];
        ImageLoader::UninitedUpwards& ups = upsBuffer[0];
        ups.count = 0;
        // Calling recursive init on all images in images list, building a new list of
        // uninitialized upward dependencies.
        for (uintptr_t i=0; i < images.count; ++i) {
            images.imagesAndPaths[i].first->recursiveInitialization(context, thisThread, images.imagesAndPaths[i].second, timingInfo, ups);
        }
        // If any upward dependencies remain, init them.
        if ( ups.count > 0 )
            processInitializers(context, thisThread, timingInfo, ups);
    }
    
    

    (4)recursiveInitialization源码实现

    void ImageLoader::recursiveInitialization(const LinkContext& context, mach_port_t this_thread, const char* pathToInitialize,
                                              InitializerTimingList& timingInfo, UninitedUpwards& uninitUps)
    {
        recursive_lock lock_info(this_thread);
        recursiveSpinLock(lock_info);
    
        if ( fState < dyld_image_state_dependents_initialized-1 ) {
            uint8_t oldState = fState;
            // break cycles
            fState = dyld_image_state_dependents_initialized-1;
            try {
                // initialize lower level libraries first
                for(unsigned int i=0; i < libraryCount(); ++i) {
                    ImageLoader* dependentImage = libImage(i);
                    if ( dependentImage != NULL ) {
                        // don't try to initialize stuff "above" me yet
                        if ( libIsUpward(i) ) {
                            uninitUps.imagesAndPaths[uninitUps.count] = { dependentImage, libPath(i) };
                            uninitUps.count++;
                        }
                        else if ( dependentImage->fDepth >= fDepth ) {
                            dependentImage->recursiveInitialization(context, this_thread, libPath(i), timingInfo, uninitUps);
                        }
                    }
                }
                
                // record termination order
                if ( this->needsTermination() )
                    context.terminationRecorder(this);
    
                // let objc know we are about to initialize this image
                uint64_t t1 = mach_absolute_time();
                fState = dyld_image_state_dependents_initialized;
                oldState = fState;
                context.notifySingle(dyld_image_state_dependents_initialized, this, &timingInfo);
                
                // initialize this image
                bool hasInitializers = this->doInitialization(context);
    
                // let anyone know we finished initializing this image
                fState = dyld_image_state_initialized;
                oldState = fState;
                context.notifySingle(dyld_image_state_initialized, this, NULL);
                
                if ( hasInitializers ) {
                    uint64_t t2 = mach_absolute_time();
                    timingInfo.addTime(this->getShortName(), t2-t1);
                }
            }
            catch (const char* msg) {
                // this image is not initialized
                fState = oldState;
                recursiveSpinUnLock();
                throw;
            }
        }
        
        recursiveSpinUnLock();
    }
    

    这部分主要研究一下,notifySingle函数doInitialization函数

    notifySingle函数
    static void notifySingle(dyld_image_states state, const ImageLoader* image, ImageLoader::InitializerTimingList* timingInfo)
    {
        //dyld::log("notifySingle(state=%d, image=%s)\n", state, image->getPath());
        std::vector<dyld_image_state_change_handler>* handlers = stateToHandlers(state, sSingleHandlers);
        if ( handlers != NULL ) {
            dyld_image_info info;
            info.imageLoadAddress   = image->machHeader();
            info.imageFilePath      = image->getRealPath();
            info.imageFileModDate   = image->lastModified();
            for (std::vector<dyld_image_state_change_handler>::iterator it = handlers->begin(); it != handlers->end(); ++it) {
                const char* result = (*it)(state, 1, &info);
                if ( (result != NULL) && (state == dyld_image_state_mapped) ) {
                    //fprintf(stderr, "  image rejected by handler=%p\n", *it);
                    // make copy of thrown string so that later catch clauses can free it
                    const char* str = strdup(result);
                    throw str;
                }
            }
        }
        if ( state == dyld_image_state_mapped ) {
            // <rdar://problem/7008875> Save load addr + UUID for images from outside the shared cache
            if ( !image->inSharedCache() ) {
                dyld_uuid_info info;
                if ( image->getUUID(info.imageUUID) ) {
                    info.imageLoadAddress = image->machHeader();
                    addNonSharedCacheImageUUID(info);
                }
            }
        }
        if ( (state == dyld_image_state_dependents_initialized) && (sNotifyObjCInit != NULL) && image->notifyObjC() ) {
            uint64_t t0 = mach_absolute_time();
            dyld3::ScopedTimer timer(DBG_DYLD_TIMING_OBJC_INIT, (uint64_t)image->machHeader(), 0, 0);
            (*sNotifyObjCInit)(image->getRealPath(), image->machHeader());
            uint64_t t1 = mach_absolute_time();
            uint64_t t2 = mach_absolute_time();
            uint64_t timeInObjC = t1-t0;
            uint64_t emptyTime = (t2-t1)*100;
            if ( (timeInObjC > emptyTime) && (timingInfo != NULL) ) {
                timingInfo->addTime(image->getShortName(), timeInObjC);
            }
        }
        // mach message csdlc about dynamically unloaded images
        if ( image->addFuncNotified() && (state == dyld_image_state_terminated) ) {
            notifyKernel(*image, false);
            const struct mach_header* loadAddress[] = { image->machHeader() };
            const char* loadPath[] = { image->getPath() };
            notifyMonitoringDyld(true, 1, loadAddress, loadPath);
        }
    }
    

    上述源码的关键语句(*sNotifyObjCInit)(image->getRealPath(), image->machHeader());,接下来我们看一下sNotifyObjCInit

    全局搜索到sNotifyObjCInit关键代码 所以搜索registerObjCNotifiers在哪里调用了,发现在_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, ![截屏2020-11-04 17.08.19.png](https://img.haomeiwen.com/i2646923/d92fd4f7a1545140.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    );
    }
    
    

    注意:_dyld_objc_notify_register的函数需要在libobjc源码中搜索
    所以我们在objc4-781源码中搜索_dyld_objc_notify_register发现_objc_init方法中调用。所以sNotifyObjCInit的赋值的就是objc中的load_images,而load_images会调用所有的+load方法。所以综上所述,notifySingle是一个回调函数。

    
    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();
        runtime_init();
        exception_init();
        cache_init();
        _imp_implementationWithBlock_init();
    
        _dyld_objc_notify_register(&map_images, load_images, unmap_image);
    
    #if __OBJC2__
        didCallDyldNotifyRegister = true;
    #endif
    }
    
    load加载

    (1)进入load_images源码,发现调用了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();
    }
    

    (2)进入call_load_methods();源码,发现函数内循环调用call_class_loads方法

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

    (3)call_class_loads方法源码,该方法的实现(*load_method)(cls, @selector(load));加载所有的load方法。

    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的源码链为:_dyld_start --> dyldbootstrap::start --> dyld::_main --> dyld::initializeMainExecutable --> ImageLoader::runInitializers --> ImageLoader::processInitializers --> ImageLoader::recursiveInitialization --> dyld::notifySingle(是一个回调处理) --> sNotifyObjCInit --> load_images(libobjc.A.dylib)

    doInitialization函数
    recursiveInitialization函数
    (1)进入doInitialization函数的源码实现
    {
        CRSetCrashLogMessage2(this->getPath());
    
        // mach-o has -init and static initializers
        doImageInit(context);
        doModInitFunctions(context);
        
        CRSetCrashLogMessage2(NULL);
        
        return (fHasDashInit || fHasInitializers);
    }
    

    这里也需要分成两部分,一部分是doImageInit函数,一部分是doModInitFunctions函数

    • doImageInit函数源代码
    void ImageLoaderMachO::doImageInit(const LinkContext& context)
    {
        if ( fHasDashInit ) {
            const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
            const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
            const struct load_command* cmd = cmds;
            for (uint32_t i = 0; i < cmd_count; ++i) {
                switch (cmd->cmd) {
                    case LC_ROUTINES_COMMAND:
                        Initializer func = (Initializer)(((struct macho_routines_command*)cmd)->init_address + fSlide);
    #if __has_feature(ptrauth_calls)
                        func = (Initializer)__builtin_ptrauth_sign_unauthenticated((void*)func, ptrauth_key_asia, 0);
    #endif
                        // <rdar://problem/8543820&9228031> verify initializers are in image
                        if ( ! this->containsAddress(stripPointer((void*)func)) ) {
                            dyld::throwf("initializer function %p not in mapped image for %s\n", func, this->getPath());
                        }
                        if ( ! dyld::gProcessInfo->libSystemInitialized ) {
                            // <rdar://problem/17973316> libSystem initializer must run first
                            dyld::throwf("-init function in image (%s) that does not link with libSystem.dylib\n", this->getPath());
                        }
                        if ( context.verboseInit )
                            dyld::log("dyld: calling -init function %p in %s\n", func, this->getPath());
                        {
                            dyld3::ScopedTimer(DBG_DYLD_TIMING_STATIC_INITIALIZER, (uint64_t)fMachOData, (uint64_t)func, 0);
                            func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
                        }
                        break;
                }
                cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
            }
        }
    }
    

    for循环加载方法的调用,这里需要注意的一点是,libSystem的初始化必须先运行

    • 进入doModInitFunctions源码实现,这个方法中加载了所有Cxx文件
    void ImageLoaderMachO::doModInitFunctions(const LinkContext& context)
    {
        if ( fHasInitializers ) {
            const uint32_t cmd_count = ((macho_header*)fMachOData)->ncmds;
            const struct load_command* const cmds = (struct load_command*)&fMachOData[sizeof(macho_header)];
            const struct load_command* cmd = cmds;
            for (uint32_t i = 0; i < cmd_count; ++i) {
                if ( cmd->cmd == LC_SEGMENT_COMMAND ) {
                    const struct macho_segment_command* seg = (struct macho_segment_command*)cmd;
                    const struct macho_section* const sectionsStart = (struct macho_section*)((char*)seg + sizeof(struct macho_segment_command));
                    const struct macho_section* const sectionsEnd = &sectionsStart[seg->nsects];
                    for (const struct macho_section* sect=sectionsStart; sect < sectionsEnd; ++sect) {
                        const uint8_t type = sect->flags & SECTION_TYPE;
                        if ( type == S_MOD_INIT_FUNC_POINTERS ) {
                            Initializer* inits = (Initializer*)(sect->addr + fSlide);
                            const size_t count = sect->size / sizeof(uintptr_t);
                            // <rdar://problem/23929217> Ensure __mod_init_func section is within segment
                            if ( (sect->addr < seg->vmaddr) || (sect->addr+sect->size > seg->vmaddr+seg->vmsize) || (sect->addr+sect->size < sect->addr) )
                                dyld::throwf("__mod_init_funcs section has malformed address range for %s\n", this->getPath());
                            for (size_t j=0; j < count; ++j) {
                                Initializer func = inits[j];
                                // <rdar://problem/8543820&9228031> verify initializers are in image
                                if ( ! this->containsAddress(stripPointer((void*)func)) ) {
                                    dyld::throwf("initializer function %p not in mapped image for %s\n", func, this->getPath());
                                }
                                if ( ! dyld::gProcessInfo->libSystemInitialized ) {
                                    // <rdar://problem/17973316> libSystem initializer must run first
                                    const char* installPath = getInstallPath();
                                    if ( (installPath == NULL) || (strcmp(installPath, libSystemPath(context)) != 0) )
                                        dyld::throwf("initializer in image (%s) that does not link with libSystem.dylib\n", this->getPath());
                                }
                                if ( context.verboseInit )
                                    dyld::log("dyld: calling initializer function %p in %s\n", func, this->getPath());
                                bool haveLibSystemHelpersBefore = (dyld::gLibSystemHelpers != NULL);
                                {
                                    dyld3::ScopedTimer(DBG_DYLD_TIMING_STATIC_INITIALIZER, (uint64_t)fMachOData, (uint64_t)func, 0);
                                    func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
                                }
                                bool haveLibSystemHelpersAfter = (dyld::gLibSystemHelpers != NULL);
                                if ( !haveLibSystemHelpersBefore && haveLibSystemHelpersAfter ) {
                                    // now safe to use malloc() and other calls in libSystem.dylib
                                    dyld::gProcessInfo->libSystemInitialized = true;
                                }
                            }
                        }
                        else if ( type == S_INIT_FUNC_OFFSETS ) {
                            const uint32_t* inits = (uint32_t*)(sect->addr + fSlide);
                            const size_t count = sect->size / sizeof(uint32_t);
                            // Ensure section is within segment
                            if ( (sect->addr < seg->vmaddr) || (sect->addr+sect->size > seg->vmaddr+seg->vmsize) || (sect->addr+sect->size < sect->addr) )
                                dyld::throwf("__init_offsets section has malformed address range for %s\n", this->getPath());
                            if ( seg->initprot & VM_PROT_WRITE )
                                dyld::throwf("__init_offsets section is not in read-only segment %s\n", this->getPath());
                            for (size_t j=0; j < count; ++j) {
                                uint32_t funcOffset = inits[j];
                                // verify initializers are in image
                                if ( ! this->containsAddress((uint8_t*)this->machHeader() + funcOffset) ) {
                                    dyld::throwf("initializer function offset 0x%08X not in mapped image for %s\n", funcOffset, this->getPath());
                                }
                                if ( ! dyld::gProcessInfo->libSystemInitialized ) {
                                    // <rdar://problem/17973316> libSystem initializer must run first
                                    const char* installPath = getInstallPath();
                                    if ( (installPath == NULL) || (strcmp(installPath, libSystemPath(context)) != 0) )
                                        dyld::throwf("initializer in image (%s) that does not link with libSystem.dylib\n", this->getPath());
                                }
                                Initializer func = (Initializer)((uint8_t*)this->machHeader() + funcOffset);
                                if ( context.verboseInit )
                                    dyld::log("dyld: calling initializer function %p in %s\n", func, this->getPath());
    #if __has_feature(ptrauth_calls)
                                func = (Initializer)__builtin_ptrauth_sign_unauthenticated((void*)func, ptrauth_key_asia, 0);
    #endif
                                bool haveLibSystemHelpersBefore = (dyld::gLibSystemHelpers != NULL);
                                {
                                    dyld3::ScopedTimer(DBG_DYLD_TIMING_STATIC_INITIALIZER, (uint64_t)fMachOData, (uint64_t)func, 0);
                                    func(context.argc, context.argv, context.envp, context.apple, &context.programVars);
                                }
                                bool haveLibSystemHelpersAfter = (dyld::gLibSystemHelpers != NULL);
                                if ( !haveLibSystemHelpersBefore && haveLibSystemHelpersAfter ) {
                                    // now safe to use malloc() and other calls in libSystem.dylib
                                    dyld::gProcessInfo->libSystemInitialized = true;
                                }
                            }
                        }
                    }
                }
                cmd = (const struct load_command*)(((char*)cmd)+cmd->cmdsize);
            }
        }
    }
    
    
    • 【第九步:寻找主程序入口即main函数】:从Load Command读取LC_MAIN入口,如果没有,就读取LC_UNIXTHREAD,这样就来到了日常开发中熟悉的main函数了
                // find entry point for main executable
                result = (uintptr_t)sMainExecutable->getEntryFromLC_MAIN();
                if ( result != 0 ) {
                    // main executable uses LC_MAIN, we need to use helper in libdyld to call into main()
                    if ( (gLibSystemHelpers != NULL) && (gLibSystemHelpers->version >= 9) )
                        *startGlue = (uintptr_t)gLibSystemHelpers->startGlueToCallExit;
                    else
                        halt("libdyld.dylib support not present for LC_MAIN");
                }
                else {
                    // main executable uses LC_UNIXTHREAD, dyld needs to let "start" in program set up for main()
                    result = (uintptr_t)sMainExecutable->getEntryFromLC_UNIXTHREAD();
                    *startGlue = 0;
                }
            }
    

    相关文章

      网友评论

          本文标题:iOS原理探索10-应用程序的加载流程

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