1. Derivation of DYLD

1. First at the main function interrupt point, you can see libdyld. Dylib ‘start before main

2. At the load function interruption point, we can see from the stack information that DyLD did a lot of work

Dyld can be downloaded from Apple’s official website

Address: opensource.apple.com/release/mac…

Ii. Main function flow in DYLD

Dyldbootstrap ::start = dyLDbootstrap ::start

2. Search dyLDbootstrap globally and you can see the start function

3. In the start function, _main is returned

4. Simply look at the _main function, directly look at the last line, return result, find the result assignment, find the code related to sMainExecutable assignment, see line 7009

	sMainExecutable = instantiateFromLoadedImage(mainExecutableMH, mainExecutableSlide, sExecPath);
Copy the code

5. InstantiateFromLoadedImage initialization of the main program

6.7241 line initializeMainExecutable

7. RunInitializers function

8. ProcessInitializers function

9. RecursiveInitialization function (1654 notice of dependent files loaded, 1662 rows loaded notice itself)

10. NotifySingle function (line 1112 sNotifyObjCInit)

11. The registerObjCNotifiers assign sNotifyObjCInit

12._dyLD_OBJC_notify_register calls the registerObjCNotifiers

13. The _DYLD_OBJC_notify_register function in DYLD is called in the runtime source code

14. At the Runtime source _objc_init interrupt point, check the stack

15. Download the libdispatch library and search for _OS_object_init

16. In line 7923 of the libdispatch_init function

17. Download the libSystem library and search for the libSystem_initializer function on line 239

Go back to dyld and search for doModInitFunctions

Line 2318 determines that an exception will be thrown if the libSystemPath library is not initialized

Func = inits[j]. LibSystem_initializer will be called

19. DoInitialization function

20. Call doInitialization in the recursiveInitialization function

Return to the Runtime objc_init function

22. The registerObjCNotifiers assign parameters in dyLD

23. Search for sNotifyObjCMapped and call it in notifyBatchPartial

24. Search sNotifyObjCInit, in the notifySingle function

25. The notifySingle call is called in the recursiveInitialization function

Load function ->C++ function ->main function

27. The load_images function in Runtime

28 prepare_load_methods function

29. Schedule_class_load function

30. The add_class_to_loadable_list function collects loads into loadable_classes

31. The getLoadMethod function gets the load methods of all classes

Call the load function recursively

Breakpoint look at the C++ function call stack

34. The doInitialization function is in the recursiveInitialization function

35. Execute the main function in dyLD_start after the above execution

Finally, sort out the flow chart