The process of application loading
Before introducing the application loading process, let me introduce some concepts about the library and compilation process
-
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
-
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.
inmain
Type a breakpoint on the first line of the function, and you can see the callmain
The bottom of the call stack isdyld_start
, it is to belong todyld
This library, we can download the latest one from Apple Open Resoucedyld
Source code, global search in the projectdyld_start
Discovery is calldyldbootstrap::start
The source code for this function is shown belowThe arguments:
dyld3::MachOLoaded* appsMachHeader
, this isapp Mach-O
Of the fileheader
There are some in thereload commands, cpu type ...
You can seeMach-OCheck out this articleMach-O
What is the structure of, or just useMachOView
This app to checkMach-O
The structure of the
dyld3::MachOLoaded* dyldsMachHeader
.dyld Mach-O
theheader
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
- Prepare conditions, environment, platform, path, host information, etc
- Shared cache loading
mapSharedCache(mainExecutableSlide)
- Instantiate the main program
mainExecutable = instantiateFromLoedImage
- Load the inserted dynamic library
loadInsertedDylib
- The link of the main program
- Link inserted dynamic library
initializeMainExecutable
Initialize the main program- Main program initialization finished, call
main
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?
for
To iterate oversImageRoots
.sImageRoots
isImageLoader
An array of instances of eachImageLoader
startrunInitializers
.- When performing the
1
, 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:
context.notifySingle
Notification will be initializeddyld_image_state_dependents_initialized
doInitialization
To initialize,doInitialization
There are two steps: 1.doImageInit
.image
Initialize; 2.doModInitFunctions
, according to thedoModInitFunctions
Code analysis can knowdoModInitFunctions
It’s just callinglibsystem
thelibSystem_initializer
And indoImageInit
When you can know iflibSystem
Has not beeninitialized
Is not allowed to otherimage
doinitialize
To deal with. So that tells uslibSystem
Is 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.notifySingle
In thenotifySingle
Calls a function pointer inNotifyObjCInit
Is in the_objc_init
Is assigned to.notifySingle
Is a notificationimage
Load the various states of the source code as shown belowNotifyObjCInit
It points tolibobjc
In theload_images
.
ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader: ImageLoader
conclusion
- Dyld will be the main program
iamge
Initialize the - by
ImageLoader
readimage
- First the
libsystem
Is initialized by the calllibSystem_initializer
To invoke theruntime
the_objc_init
To register a callback whenimage
Notification after being loaded into memoryruntime
To deal with.