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:
Select the running environment (iOS simulator)
Initialize data, set global variables, and context information
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:
Check whether the Mach-O file is valid
Initialize an ImageLoader instance
Call the addImage function to add the initialized instance to the management module.
1.5 isCompatibleMachO
Check the validity of the Mach-O file:
Is cputype In the mach-header supported by the current running CPU version?
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:
Determine whether the Mach-O file is classic or compressed.
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.
Add an image to the management container
Updated the memory distribution information.
End
The entire loading process is divided into three steps:
Legal addition test
Parse the Mach-O file header information and build the specific information of segments into the image instance.
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