The process of application loading

Before introducing the application loading process, let me introduce some concepts about the library and compilation process

  1. Libraries: executable binary files that can be loaded into memory by the system. They are divided into static and dynamic libraries. The difference between dynamic and static is the difference between links

  2. Compilation process

Let’s preview the app startup process in general

  • Image: The library maps a copy to memory
  • When we debug, we can enter image list in LLDB to get the libraries loaded by the current app.

inmainType a breakpoint on the first line of the function, and you can see the callmainThe bottom of the call stack isdyld_start, it is to belong todyldThis library, we can download the latest one from Apple Open ResoucedyldSource code, global search in the projectdyld_start Discovery is calldyldbootstrap::startThe source code for this function is shown belowThe arguments:

  • dyld3::MachOLoaded* appsMachHeader, this isapp Mach-OOf the fileheaderThere are some in thereload commands, cpu type ...You can seeMach-OCheck out this articleMach-OWhat is the structure of, or just useMachOViewThis app to checkMach-OThe structure of the

  • dyld3::MachOLoaded* dyldsMachHeader.dyld Mach-Otheheader

dyld::main

From the implementation of dyldbootstrap::start, we can see that the final return is dyld::_main, because the dyld::_main function is too long, directly attached to the analysis of dyld::_main process

  1. Prepare conditions, environment, platform, path, host information, etc
  2. Shared cache loadingmapSharedCache(mainExecutableSlide)
  3. Instantiate the main programmainExecutable = instantiateFromLoedImage
  4. Load the inserted dynamic libraryloadInsertedDylib
  5. The link of the main program
  6. Link inserted dynamic library
  7. initializeMainExecutableInitialize the main program
  8. Main program initialization finished, callmain

Note: ImageLoader is a class that loads images into memory, and each image is loaded by an instance of ImageLoader, which is loaded recursively.

What’s going on inside initializeMainExecutable?

  1. forTo iterate oversImageRoots.sImageRootsisImageLoaderAn array of instances of eachImageLoaderstartrunInitializers.
  2. When performing the1, the main program beginsrunInitializers

So what does the runInitializers do in every ImageLoader runInitializers in sImageRoots?

In processInitializers, each image is recursively initialized by calling recursiveInitialization. RecursiveInitialization is divided into three steps:

  1. context.notifySingleNotification will be initializeddyld_image_state_dependents_initialized
  2. doInitializationTo initialize,doInitializationThere are two steps: 1.doImageInit.imageInitialize; 2.doModInitFunctions, according to thedoModInitFunctionsCode analysis can knowdoModInitFunctionsIt’s just callinglibsystemthelibSystem_initializerAnd indoImageInitWhen you can know iflibSystemHas not beeninitializedIs not allowed to otherimagedoinitializeTo deal with. So that tells uslibSystemIs the first library to be loaded and initialized.

So why does libSystem need to be loaded first? Because libSystem is a collection of system libs including libDispatch, libsystem_c, libsystem_blocks, it’s just a container lib, and it’s open source, it’s essentially a file, Init. C, libSystem_initializer calls libDispatch_init of libDispatch, libdispatch_init calls _objc_init of libobjc, This is the entry point for objC and Runtime initialization.

At point one up herecontext.notifySingleIn thenotifySingleCalls a function pointer inNotifyObjCInitIs in the_objc_initIs assigned to.notifySingleIs a notificationimageLoad the various states of the source code as shown belowNotifyObjCInitIt points tolibobjcIn theload_images.

ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader

conclusion

  1. Dyld will be the main programiamgeInitialize the
  2. byImageLoaderreadimage
  3. First thelibsystemIs initialized by the calllibSystem_initializerTo invoke theruntimethe_objc_initTo register a callback whenimageNotification after being loaded into memoryruntimeTo deal with.