[TOC]
Summary review
1: class load – protocol property ro-rw slow process – lazy load – non-lazy load 2: map_images() when to load 3: load_images() Load collection of methods – CXX + load + main 4: Dyld -> main 5: wwdc2017 about dyld2 vs DyLD3 Optimized point launchclusure avoids some closure mode loading
Apple WWDC 2020 about the Runtime optimization developer.apple.com/videos/play… CoreFoudation source: opensource.apple.com/tarballs/CF… Dyld expand supplementary WWDC2017 developer.apple.com/videos/play… ?
Wwdc2017 about dyld2 vs dyld3 launchclusure avoid changing the point for the optimization of some closure mode loading dyld:developer.apple.com/videos/play…
Dyld link objc function execution
Dyld how to connect image files and reverse callback in objc source _objc_init() method call _dyLD_OBJC_notify_register (&map_images, load_images, unmap_image); For &map_images, how does load_images call the process that communicates objC and dyld to be explored
In the dyld source _dyLD_objC_notify_register (registerObjCNotifiers)
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
RegisterObjCNotifiers there are three important assignments in the method
sNotifyObjCMapped = mapped; // The map_images() method is loaded
sNotifyObjCInit = init; // The load_images() method loads
sNotifyObjCUnmapped = unmapped;
Copy the code
map_images
The sNotifyObjCMapped method is searched and found inside the method
try {
notifyBatchPartial(dyld_image_state_bound, true.NULL.false.true);
}
Copy the code
load_images
SNotifyObjCInit method
A recursive process
Load and CXX and main call processes
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
NSLog(@ "1223333");
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
appDelegateClassName = NSStringFromClass([AppDelegate class]);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
__attribute__((constructor)) void kcFunc(){
printf("Coming: %s \n",__func__);
}
Copy the code
ViewController.m
#import "ViewController.h"
@interface ViewController(a)
@end
@implementation ViewController
+ (void)load{
NSLog(@"%s",__func__);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end
Copy the code
Result 1. Load 2. C++ 3
+[ViewController Load] comes: lrFunc 1223333Copy the code
The load method call explores the load_images() method
All load methods _dyLD_OBJC_NOTIFY_register (&map_images, load_images, unmap_image); load_images(
Prepare the load method to call the load method
1.prepare_load_methods((const headerType *)mh); // Prepare the load method
Prepare the load methods classified into the load methods of this class
2.call_load_methods(); // Call the load method
Call the load methods that are also classified into this class’s load methods
Prepare the load method prepare_load_methods((const headerType *)mh);
prepare_load_methods(
1.schedule_class_load(remapClass(classlist[i])); 2. Add_category_to_loadable_list (cat); // Call the load method of the class and then call the load method of the class
Take the class load method collection as an example to explore, the classification of the load method collection process is similar
schedule_class_load(
add_class_to_loadable_list(
objc_class::getLoadMethod()
Call the load method call_load_methods();
Call the load method that is also the class’s load method class
call_class_loads();
call_category_loads();
C++ method call exploration
Why are c++ methods automatically called?
Bt Stack debugging
recursiveInitialization -> doInitialization -> doModInitFunctions -> libSystem_initializer -> libdispatch_init -> NotifySingle (dyLD_image_state_dependents_initialized, this,) &timingInfo); The method is the call to the load method and the doInitialization method is the point where c++ actively calls it
The doModInitFunctions method reads cmd_count, which is what’s in Mach-o,
Main function call exploration
Finally, how do I call main after __dyLD_start
I’m going to open assembly and I’m going to put a breakpoint in the c++ method lrFunc and I’m going to step through the load and the c++ method and I’m going to go to the main method and I’m going to register read, and I’m going to see that main is here, that main is called