preface

How does the code load into memory let’s explore

The code for

  • Libsystem – 1292.120.1
  • Libdispatch – 1271.120.2
  • dyld-852
  • Objc4-818.2 –

1. Application loading principle

Libraries: executable binaries -> can be loaded into memory by the operating system -> static and dynamic libraries

The build process

What is an executable

Open project Command +b into the following operation and the final result is the executable file

Verify that the executable executes the code in main

The process of loading executable files into memory through dynamic linker dyld is shown in the following figure

Runtime to register the callback function is

We print out the image list to see which dyld’s were loaded. We can find the path to the CoreFoundation library: dyLD source dyLD DYLD_ROOT_PATH

The main function is called by executing map_images and Load_images (which will be examined in the next section)

2.dyldWhich leads to the

How do we find dyld by first opening the project breakpoint main function

We find that on the left is0 main 1 startmainI left this earlier1 start 1 startWhat did he do

Welcome tolibdyld.dylibAdd system breakpoints to the worldstartWalk a wave and find no break but[ViewController load]mainIf we print the breakpoint here and see if we find something like this

Dyld ‘ ‘_dyLD_start | | | | | | | | | | | | | |

The application loading process is as follows

  • dyld`_dyld_start ->
  • dyld`dyldbootstrap::start ->
  • dyld`dyld::_main ->
  • dyld`dyld::useSimulatorDyld ->
  • dyld_sim`dyld::_main ->
  • dyld_sim`dyld::initializeMainExecutable() ->
  • dyld_sim`ImageLoader::runInitializers ->
  • dyld_sim`ImageLoader::processInitializers ->
  • dyld_sim`ImageLoader::recursiveInitialization ->
  • dyld_sim`dyld::notifySingle ->
  • libobjc.A.dylib`load_images

3.dyldIn the process

Search for _dyLD_start through the dyld source code and we enter the assembly

Call dyLDbootstrap ::start(app_mh, argc, argv, dyLD_MH, &startglue

This function returns onedyld::_main((macho_header*)appsMachHeader, appsSlide, argc, argv, envp, apple, startGlue);Let’s go in and see

4.dyldIn the processmainMain flow of functions

Because the main function has a long code and the main function is going to return result and it’s going to be executing sMainExecutable

Found this function sMainExecutable = instantiateFromLoadedImage instantiation of the main program

Use this function to add the image file

Add a format to machO using this function.

Add specific formats such as load_command with the sniffLoadCommands function

The next step is to load the inserted dynamic library

The next step links to the main program

Next link inserts the dynamic library

The next step is weakBind weak reference binding

The next step is to initialize initializeMainExecutable and run

The next step is to tell dyld that it can enter main()

5. Dyld process – Main program run

We mainly analyze initializeMainExecutable images to start initialization

Enter runInitializers -> processInitializers

Enter processInitializers->recursiveInitialization

Enter recursiveInitialization->context.notifySingle single notification injection

-> doInitialization Call the init method -> context.notifysingle to notify that initialization is complete

Go to notifySingle-> and find sNotifyObjCInit

Through the sNotifyObjCInit – > registerObjCNotifiers

Through the registerObjCNotifiers – > _dyld_objc_notify_register

_dyLD_OBJC_NOTIFy_register is the Runtime register callback

6.dyldProcess –imagesInitialization process

We pass objC4-818.2 source breakpoint _objc_init

libdispatch.dylib``_os_object_init

_OS_object_init -> Calls _objc_init

libdispatch.dylib``libdispatch_init->_os_object_init

libSystem.B.dylib``libSystem_initializer->libdispatch_init

Dyld ` ` ImageLoaderMachO: : doModInitFunctions: – > initialize libSystem_initializer (through STRCMP (installPath, libSystemPath (context))

ImageLoaderMachO::doInitialization->doModInitFunctions

Return to recursion recursiveInitialization-> doInitialization

Summarize the app startup flowchart