1, the metaclassMetaClass

First, let’s take a lookclassWhat is,classIn OC it’s actually oneStructure pointer to objc_classLet’s first look at the constructs of the structures, many of which are going to be deprecated in OBJC2 (just for a general reference):

The existing ones are:In the OCobjectobjc_objectIs defined as:Here is another knowledge point involved__has_feature(ptrauth_calls)We will add it below

OC objects have a familiar feature: the message sending mechanism

  • The principle isOC objectWhen you send a message,The runtime libraryIt gets the class that the object belongs to, based on the ISA pointer to the objectContains all instance methods of the class and Pointers to the parent classSo that you can find instance methods of the parent class. The runtime library checks the list of methods for this class and its parent to find the corresponding method for the message. The compiler converts the message into a message functionobjc_msgSendCall.
  • The OC class is also an object. An object must have a class to which it belongs, which means that the class must also have an ISA pointer to the class to which it belongs, so,A metaclass is the class to which a class belongs.
    • When you send a message to an object, the message is a list of methods of the class that is looking for that object.
    • When you send a message to a class, the message is looking for a list of methods on that class’s metaclass.

Yuan class definition

  • A metaclass is the class to which a class belongs and provides a list of common methods for that class

  • Isa points to the upper root metaclass as the endpoint:

    • Object ISA -> class ISA -> metaclass ISA -> parent metaclass ISA -> root metaclass ISA -> root metaclass
    • Root class ISA -> root metaclass ISA
  • NSObject is the end point on inheritance:

    • Metaclass -> parent metaclass -> root metaclass -> root NSObject

__has_feature (ptrauth_calls) is introduced

2. Class structure analysis

Ordinary pointer

  • A and b are variables that point to 10, 10 isFixed memory space allocated by the systemAny other variable that requires a value of 10 can refer to a fixed memory generated 10
  • A and B address is different, this is a copy, belongs toCopy the valueIt can be found that the difference between a and B addresses is 4 bytes, and the difference between stra and STRB addresses is 8 bytes, depending on the type of A, B, and stra and STRB

Pointer to the object

  • P1 / p2Level 1 pointerThat points to theallocOpen the memory address of the space
  • & p1 & p2 / isThe secondary pointer, the address of the pointer to the object (0x7ffeefbff3f8.0x7ffeefbff3f0Is an object pointer.

Pointer to an array

  • &c == &c[0] == the address of the first element of the array, the name of the arrayThe first address
  • &c[0] differs from &C [1]4Bytes, depending onThe data type
  • Array type Pointers can pass throughStart address + offsetGet other elements (offset to array subscript)
  • The number of bytes moved is equal to the offset * the number of bytes of the data type, as shown by &c[0] and &c[1], which differ by 4

Class structure memory

  • bitsIn the storeThe class information, can be obtained by offset (32 bytes i.e0x20)class_data_bits_t bitscontent

  • Calculating the offset requires an analysis of the cache_t size: 16 bytes

  • 0x20 is a representation of an integer constant. Integer constants beginning with 0x, representing hexadecimal representation of subsequent characters. So 0x20 is the same thing as 20 in base 16, which is 32 in base 10. In addition, 0x20, represented as a single byte, can be used to assign a character variable, and when used for char, it represents the ASCII value 0x20, the character space ‘ ‘.

Now that we’re done with the preparatory work, let’s take a step-by-step look at bits:

Bits to explore

1. Obtain according to offsetclass_data_bits_tThe type ofbits
  • x/4gx testWhere the first address0x100645940
  • 0x100645940Translation 32 bytes is0x100645960
  • becausebitsisclass_data_bits_tType, we want to get the address soclass_data_bits_t *The type is forced to become a pointer address
2, getbitsclass_rw_t *The type ofdata
  • p $2->data()becausestruct class_data_bits_tThe source code (->Because the current is a pointer, struct.)

3. Explore separatelydataUnder theprspertiesMethods

You can seeclass_rw_tIt provides someThe attribute properties.List of methods.Protocol list protocolsMethods.

  • Print property list
    1. dataLook forproperty_array_tThe type ofThe attribute properties
    2. Properties view property_array_t(the property_T structure is used as the parameter), and then continue to view property_array_tt

  • Print a list of methods
    1. dataLook formethod_array_tThe type ofMethods the methods
    2. Look at method_array_t(taking the method_t structure as parameter) in methods, and continue to look at list_array_tt

  • Print protocol List

P $3.protocols()

supplement

1.objc_object VS NSObject,objc_class VS Class,dispatch_objc VS dispatch, the iOS underlying implementation uses tapeobjcContent to operate

2, __has_feature (ptrauth_calls)

Sometimes __has_feature(ptrAUTH_calls) is used with TARGET_OS_SIMULATOR, which requires popularizing the ARM64e concept first

// ARM64 simulators have a larger address space, So use the ARM64e\ // scheme even when simulators build for arm64-not-e. So use ARM64e\ // even when building emulators for ARM64-not-e.Copy the code

ARM64e is the ARM64e architecture for Apple A12 and later A series processors or new OS_SIMULATOR devices.

  • __has_feature(ptrauth_calls): indicates whether the compiler supports itPointer authentication
  • ptrauth_callsPointer authentication for ARM64E architecture; Devices using Apple A12 or later A series processors (such as iPhone XS, iPhone XS Max, and iPhone XR or newer devices) support the ARM64E architecture

MachOView(decompiled)

Symbol Table → SymbolsAs you can see, there is actually more at the bottom_OBJC_METACLASS(Meta Class: metaclass)