1, the metaclassMetaClass
First, let’s take a lookclassWhat is,classIn OC it’s actually oneStructure pointer to objc_class
Let’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_object
Is 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 library
It 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_msgSend
Call. - 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 is
Fixed memory space allocated by the system
Any 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 to
Copy the value
It 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 / p2
Level 1 pointer
That points to theallocOpen the memory address of the space - & p1 & p2 / is
The 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 array
The first address
- &c[0] differs from &C [1]
4
Bytes, depending onThe data type
- Array type Pointers can pass through
Start address + offset
Get 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
bits
In 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_t
The type ofbits
x/4gx test
Where the first address0x100645940
0x100645940
Translation 32 bytes is0x100645960
- because
bits
isclass_data_bits_t
Type, we want to get the address soclass_data_bits_t *
The type is forced to become a pointer address
2, getbits 中class_rw_t *
The type ofdata
p $2->data()
becausestruct class_data_bits_t
The source code (->
Because the current is a pointer, struct.
)
3. Explore separatelydataUnder theprsperties 与 Methods
You can seeclass_rw_t
It provides someThe attribute properties
.List of methods
.Protocol list protocols
Methods.
- Print property list
- dataLook for
property_array_t
The type ofThe attribute properties - Properties view property_array_t(the property_T structure is used as the parameter), and then continue to view property_array_tt
- dataLook for
- Print a list of methods
- dataLook for
method_array_t
The type ofMethods the methods - Look at method_array_t(taking the method_t structure as parameter) in methods, and continue to look at list_array_tt
- dataLook for
- 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 authenticationptrauth_calls
Pointer 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
→ Symbols
As you can see, there is actually more at the bottom_OBJC_METACLASS
(Meta Class: metaclass)