Small valley bottom exploration collection

Brothers. Did you enjoy your National Holiday? The baby slept for seven days, and it was blissful. 😆

  • Everyone forThe startup process of the applicationI think I know a little bit about that, because I was lazy (sleeping), so I didn’t write the app launch blog.

1. Introduction to the application startup process

I’ll just write it down. I will not take you step by step (forgive me, forgive me ~), we have time or can debug a wave.

    1. I worked very hard to draw a picture:

It’s a little rough.

    1. We came to the conclusion,Application startupThe first thing I call is_dyld_startAnd then callObjc source

The first method is _objc_init.

2. _objc_init

    1. First let’s take a look at the source code:
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
}
Copy the code
    1. A wave of proper nouns to explain ~
environ_init(); // Read environment variables initializes tls_init(); // Automatically release pool, runloop thread related static_init(); // Run the C ++ static constructor runtime_init(); // Add a new one. The Runtime runtime environment initializes Exception_init (); Cache_init (); _imp_implementationWithBlock_init(); // Start the callback mechanismCopy the code

Well, the above are not the key to explore ~😆, here first do not explain.

    1. _dyld_objc_notify_register inquiry

_DYLD_OBJC_Notify_register is the source of this exploration. It can be seen that dyLD is related to the naked eye ~ after we click on it, we find that we can’t follow it ~

3. Dyld source code analysis

    1. Since theobjcThe source code can not continue to explore, but also anddyldOkay, so let’s see if we can call itdyldThe source code.
    1. We opendyldSource code discovery:
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);
}
Copy the code
    1. This is a cross-library call (there is no way out)That’s how gross it is)
    1. Let’s analyze the source code he called:
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
     try {
         notifyBatchPartial(dyld_image_state_bound, true.NULL.false.true);
     }
     catch (const char* msg) {
         // ignore request to abort during registration
     }


     // <rdar://problem/32209809> call 'init' function on all images already init'ed (below libSystem)
     for (std: :vector<ImageLoader*>::iterator it=sAllImages.begin(); it ! = sAllImages.end(); it++) { ImageLoader* image = *it;if ( (image->getState() == dyld_image_state_initialized) && image->notifyObjC() ) {
             dyld3::ScopedTimer timer(DBG_DYLD_TIMING_OBJC_INIT, (uint64_t)image->machHeader(), 0.0); (*sNotifyObjCInit)(image->getRealPath(), image->machHeader()); }}}Copy the code
    1. Hey sex ~, this time I want to cut a picture ~ (code to write comments, color is not to force 😆)

    1. What does this mean?objcSource code is called across librariesdyldSource code, and thendyldThe source code is in callobjcThe source code ~

4. Apply the startup flowchart extension

    1. We’ve already made it clear that the first call to start an application is_dyld_startAnd then to_objc_init, and then called across librariesdyldAnd then the purpose is still to callmap_imagesandload_images.
    1. It feels so convoluted. 😭. So I drew an extended picture:

Brothers ~ I tried my best with this picture after all, I am not a soul painter 😆