Small valley bottom exploration collection
Brothers. Did you enjoy your National Holiday? The baby slept for seven days, and it was blissful. 😆
- Everyone for
The startup process of the application
I 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.
-
- I worked very hard to draw a picture:
It’s a little rough.
-
- We came to the conclusion,
Application startup
The first thing I call is_dyld_start
And then callObjc source
- We came to the conclusion,
The first method is _objc_init.
2. _objc_init
-
- 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
-
- 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.
-
- _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
-
- Since the
objc
The source code can not continue to explore, but also anddyld
Okay, so let’s see if we can call itdyld
The source code.
- Since the
-
- We open
dyld
Source code discovery:
- We open
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
-
- This is a cross-library call (there is no way out)
That’s how gross it is)
- This is a cross-library call (there is no way out)
-
- 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
-
- Hey sex ~, this time I want to cut a picture ~ (code to write comments, color is not to force 😆)
-
- What does this mean?
objc
Source code is called across librariesdyld
Source code, and thendyld
The source code is in callobjc
The source code ~
- What does this mean?
4. Apply the startup flowchart extension
-
- We’ve already made it clear that the first call to start an application is
_dyld_start
And then to_objc_init
, and then called across librariesdyld
And then the purpose is still to callmap_images
andload_images
.
- We’ve already made it clear that the first call to start an application is
-
- 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 😆