Ios – Loading process for classes (_read_images/ lazy loaded classes/non-lazy loaded classes)

Preface:

The previous article looked at the modules in read_images and implemented the process of loading non-lazy-loaded classes and lazy-loaded classes into memory. One of the problems left on the implementation of methodizeClass method, this article focuses on the analysis of methodizeClass method and classification loading process.

The nature of classification:

First look at the structure with clang-rewrite-objc main.m-o main.cpp

Thus it can be seen

The essence of classification is a _category_T structure

methodizeClass

  • Enter the**methodizeClass**Source code, to obtain the object to study

  • Add method list, property list, protocol to RWE

  • Paste the classification

AttachToClass implementation

The rWE logic is illustrated with methodList

The logic of RWE is as follows:

  • Get ro – > baseMethods ()

  • Sort by the prepareMethodLists method

  • Rwe – > the methods. AttachLists insert

Sort prepareMethodLists method explored

  • prepareMethodLists

  • fixupMethodList

  • Validation method sorting

View the kC_ro content

See the first method sorted by **prepareMethodLists** method

Print the list of methods on the console

Sort by method name

Imp address sort by method

Insert attachLists:

  • **method_list_t**Inheritance and**entsize_list_tt**Is a two-dimensional array

  • AttachLists implementation

The breakpoint goes to LGPerson, the object to be studied and makes a breakpoint in the attachLists method

Continue to follow up

Continue breakpoint follow-up

How is the list of methods inserted into memory? Same with hasArray()

So the loading class is a one-dimensional assignment with the path: map-images -> _read_images -> readClass -> realizeClassWithoutSwift -> mehodizeClass -> prepareMethodLists -> fizupMethodList ->attachToClass -> load_categories_nolock -> attachCategories -> extAllocIfNeeded ->ectAlloc -> attachLists

AttachCategories analysis

Go ahead and print the mList

Inserts the mList at the end of the defined 64-bit array

Rwe — LGB classification data loading

Continue to follow up

Enter the **attachLists** method for memory loading

To sum up, the attachLists method is mainly to load data of classes and categories into RWE

  • First load class data, rew has no value, list is empty, go first address assignment, one-dimensional array assignment

  • When the class is successfully loaded, add the category, list has a value, go 1+ multiple way

  • When you add a category. Rew is an array (class + first category), and hasArray (list of classes + categories) is a many-to-many process

The process is as follows:

The loading process of classification is shown in the figure below:

Lazy/non-lazy loading of classes and categories:

According to the state of lazy loading and non-lazy loading, classes and categories can be divided into the following situations:

  • Both classes and classes implement the load method

  • Classes implement a load method, but classes do not

  • Class implements the load method, not the classification

  • Neither class nor class implements the load method

Conclusion:

The loading process for classes and categories

  • Read the name and address information of the class readClass.

  • Load class properties, methods, and protocols from Math_O to in-memory realizeClassWithoutSwift based on name and address information

  • RealizeClassWithoutSwift has two things in it:

  1. Serialized class methods, protocols, etc
  2. Add class/category data informationattachCategories

In summary, the flow chart for loading classes from Mach-O into memory is shown below

Refer to “Loading classes” for this diagram.