Dyld loads Mach-O and dyld loads mach-o.

Source: Internet
Author: User

Dyld loads Mach-O and dyld loads mach-o.

Refer to the following link for more technical tips: Alibaba Cloud blog

Preface

Recently, I checked how ObjC's runtime Implementation + load hook function implementation. This part of dyld's mechanism for Mach-O processing is further analyzed.

1. analyze how Mach-O is loaded into the memory in dyld;

2. Analyzed the special loading time of + load;

+ Load

The call stack tells us which functions are called.

Dyld is Apple's dynamic linker. After the xnu kernel is ready to start the program, the PC control is handed over to dyld for the rest of the work (dyld runs in the user State, here, the kernel state is switched to the user State ).

Every time a new image is loaded, the load-images method will be executed for callback. The callback here is registered during the entire ObjC runtime initialization-objc-init:

When a new image is mapped to runtime, call the load-images method and input the information list of the latest image, infoList:

The image here is the binary of some System framework.

Enter the function load-images-nolock to find the load function.

Call prepare-load-methods to prepare for calling the load method (add the class that needs to call the load method to a list)

After you call-getObjc2NonlazyClassList to obtain the list of all classes, the corresponding pointer of the class will be obtained through remapClass, call schedule-class-load to recursively display the current class and the parent class without calling + load.

Before you execute add-class-to-loadable-list (cls) to add the current class to the load list, the parent class is added to the list to be loaded, ensure that the parent class calls the load method before the subclass.

When the image is loaded to the runtime and the load method is ready, run call-load-methods to start calling the load method:

Here, call-class-loads searches for the corresponding class from the loadable-classes list of classes to be loaded, finds the implementation of @ selector (load) and executes it.

The analysis shows how the load function is called.

Next, we will analyze how dyld loads the image.

1.1 Data Structure

Mach-o file header operation.

1.2 ImageLoader

Every loaded Mach-O file has such an ImageLoader instance. It can be seen that ImageLoader is an abstract class, and each specific Mach-O file inherits the ImageLoader class, inheritance relationships include:

During the loading, the unused instance is selected based on the Mach-O format.

1.3-main

After calling the-main function, we did the following:

  1. Select the running environment (iOS simulator)

  2. Initialize data, set global variables, and context information

  3. Check whether the file is Restricted

After completing these steps, you will call the instantiateFromLoadedImage function to load Mach-O and instantiate it as ImageLoader.

1.4 instantiateFromLoadedImage

This function does three things:

  1. Check whether the Mach-O file is valid

  2. Initialize an ImageLoader instance

  3. Call the addImage function to add the initialized instance to the management module.

1.5 isCompatibleMachO

Check the validity of the Mach-O file:

  1. Is cputype In the mach-header supported by the current running CPU version?

  2. All versions of the subtype in the mach-header can be supported in this CPU architecture.

Cputype is the CPU platform, x86, ARM, POWERPC, and so on, and subtype is different versions of the same platform, such as: arm7.

1.6 ImageLoaderMachO: instantiateMainExecutable

This function mainly uses the sniffLoadCommands function to determine whether the Mach-O file is compressed, and then select different subclass instantiation.

1.7 sniffLoadCommands

This function mainly performs two tasks:

  1. Determine whether the Mach-O file is classic or compressed.

  2. Obtain the number of segments of the mach-O file.

1.8 ImageLoaderMachOClassic: instantiateMainExecutable

The initialization of classic is similar to that of compressed. First, analyze the implementation of Classic.

The loaded core code is still in the instantiateStart function.

1.9 instantiateStart

There is still no core code to load, but the memory is allocated based on the previous data application and the segments pointer is calculated. ImageLoaderMachOClassic constructor is the core logic of loading.

2.0 ImageLoaderMachOClassic

Load data to the memory according to the Mach-O file segments, and call the addImage function for any response.

2.1 addImage

This function only updates data.

  1. Add an image to the management container

  2. Updated the memory distribution information.

End

The entire loading process is divided into three steps:

  1. Legal addition test

  2. Parse the Mach-O file header information and build the specific information of segments into the image instance.

  3. Add an image to a management container

According to the Rough Analysis of the source code of dyld, The xnu kernel code needs to be analyzed for more information.

Reference

ObjC runtime source code

Dyld source code

Mac OSX and iOS Internals

Link: http://blog.tingyun.com/web/article/detail/1346

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.