First, the nature of the object
1. How to compile the OC class into c++ files
Clang-rewrite-objc main.m -o main. CPP compiles the object file into a c++ file
UIKit reported an error
Clang-rewrite-objc-fobjc-arc-fobjc-runtime =ios-13.0.0 -isysroot / Applications/Xcode. App/Contents/Developer/Platforms/iPhoneSimulator platform/Developer/SDKs/iPhoneSimulator13.0. The SDK main.m
Xcode is installed with the xcrun command, which is a bit more wrapped around clang to make it easier to use
Xcrun – SDK iphonesimulator clang -arch arm64 -rewrite-objc main.m -o main-arm64.cpp
Xcrun – SDK iphoneOS clang -arch arm64 – rerewrite -objc main.m -o main-arm64.cpp
2. Discovered secrets
①OC classes are structures, and the lowest level is the objC_Object structure
②Class is objc_class and ID is objc_object
③ The function is preceded by two implicit arguments selfCMD, get name with self + OBJC_IVAR
KcName (self prefix +OBJC_IVAR
_kcName offset address obtained)
Two, the commonwealth bit domain
Structure: Allocates memory for all members, whether they use it or not
Union: Members are mutually exclusive. There is only one member at a time. The memory is the memory of the largest member
3. Analysis of nonPointerIsa
Isa isa combination of isa_t, and isa_t is defined as follows
(2) interpretation
Nonpointer: indicates whether to enable pointer optimization for the ISA pointer. 0: indicates the pure ISA pointer. 1: Indicates that the ISA contains not only the address of the class object but also the class information and reference count of the object
Has_assoc: flag bit of the associated object. 0 does not exist and 1 exists
Has_cxx_dtor: does the object have a destructor for C++ or Objc? If it has a destructor, the destructor logic needs to be done. If not, the object can be freed faster
Shiftcls: Stores the value of the class pointer. With pointer optimization turned on, 33 bits are used to store class Pointers in the ARM64 architecture.
Magic: Used by the debugger to determine whether the current object is a real object or has no space to initialize
Weakly_referenced: Indicates whether an object is pointed to or has been pointed to an ARC weak variable. Objects without weak references can be released faster.
Deallocating: Indicates whether the object is freeing memory
Has_sidetable_rc: When the object reference technique is greater than 10, this variable is borrowed to store carry
Extra_rc: When representing the reference count of this object, the reference count is actually subtracted by 1. For example, if the object’s reference count is 10, the extra_rc is 9. If the reference count is greater than 10, the following has_sideTABLE_rc is used.
Get class from isa
Class information is stored in Shiftcls. The lower 3 bits and the higher 17 bits store other information. How to offset the middle 44 bits? The resulting address is the same as that of the class printed directly, perfect
Let’s finish today and continue next