创建类_类对象何去何从
创建类_类对象何去何从
-
在Objective-C 中,普通的对象是类的实例,而类也是一个对象,他是objc_class的一个实例,元类(MetaClass)也是一个对象,也是objc_class的一个实例
-
我们通常认识的,对象的isa 指向类,类的isa指向其元类,元类的isa指向根元类。
-
我们创建一个类文件的时候,我们就意味着认为它将在后面可以直接的去使用。那么它怎么来的,怎么加载到,怎么去获取里面的方法?
先看 Objc_Clang_after.cpp 这个文件经过clang 编译成CPP来的,实际上 我们Xcode也帮我们处理好。
在编译阶段 就如这个文件 出现大部分的结构体 把我们代码中声明的这些描述清楚。
这些_prop_t、_protocol_t 、_objc_method 、_protocol_t 、
_ivar_t、_class_ro_t、_class_t、_category_t
为我们整个文件的描述后续基础的准备。
在这个阶段
1、把一些方法、协议、属性、成员变量,装进ro这个定义的结构体里面;
2、把我们处理好的ro结构体放进_class_t之中去,等等为什么isa、superclass、cache、vtable。!!!都是0,特别是isa,superclass、明明就有isa与父类。
3、原来在后面会有一个OBJC_CLASS_SETUP_$_XXX
的静态函数会帮我们把(2)中缺少isa、superclass、cache等进行处理。
4、那到底谁负责调用这个函数呢?
留意到.objc_inithooks$B
猜想由hooks名字可知 会在objc_init中的某个时刻(readClass/realized之前)调用这个函数指针数组(因为每个类都有,所以多个就成了数组) A。 暂且留下思考。
5、我们处理好的两个_class_t ,存储到mach-o的section信息(__objc_classlist)
留下伏笔 就是为了前面文章说到的readClass 到时候进行读取。
提示点:
定义一个静态全局struct,声明不被优化,存储到mach-o的section信息中
如下存储的参数名为(__objc_classlist),后期根据此名称取值
static struct _class_t *L_OBJC_LABEL_CLASS_$ [2]
__attribute__((used, section ("__DATA, __objc_classlist,regular,no_dead_strip")))= {
__declspec(allocate("segname")) 告诉编译器分配一个名字为segname的段
在实际使用时,还需要#pragma section事先告诉编译器,我们要使用的段名
#pragma section(".objc_inithooks$B", long, read, write)
__declspec(allocate(".objc_inithooks$B")) static void *OBJC_CLASS_SETUP[] =
何时读取
关于存起来的这些那些的存储到mach-o数据段内容,什么时候读取到呢?不是每次我都去mach-o里面拿吧?
mach-o文件会在exec_mach_imgact这个阶段加载进内存,那么到时候去mach-o映射进内存的部分里面拿,取出来存在全局的gdb_objc_realized_classes里。
在调用map_images_nolock 中 _read_Images ==>中的discover classes流程中readClass来读取类信息
classref_t const *classlist = _getObjc2ClassList(hi, &count);
//_getObjc2ClassList ==> GETSECT(_getObjc2ClassList, classref_t const, "__objc_classlist");
bool headerIsBundle = hi->isBundle();
bool headerIsPreoptimized = hi->hasPreoptimizedClasses();
for (i = 0; i < count; i++) {
Class cls = (Class)classlist[i];
Class newCls = readClass(cls, headerIsBundle, headerIsPreoptimized);
......
}
-
readClass来读取类信息
- 其中涉及addNamedClass 把我们所有的Class 都存进一个gdb_objc_realized_classes的map中
- addClassTableEntry 已经分配了才进入 在所有类的表中添加一个类。如果addMeta为真,也自动添加类的元类。这里是不会进入的
- 注意点 这里仅仅!!!map 并没有做其他的处理 处理部分在于RealizeClass部分
- RealizeClass: 实现;认识到;了解 ,。。。变现 。。这里可以理解为 将class从仅仅的一个我们不为所知的class变现为 我们可以认识的class,我们认识到的class 那必须正常化把里面需要的信息都进行处理 _read_images 的markdown有部分总结 后期对此有不同的认识在进行总结
Class
objc 关于类方面的思考 为什么class 结构体相关的居然可以继承?
oc的结构体可以继承吗?不可以 。
在开头写到的objc_class这一个结构体 struct objc_class : objc_object,与此同时struct objc_object, objc_object也是一个结构体,大家有没有好奇到为什么结构体可以继承。这里的头文件,可以说用于C++之中,C++支持结构体继承,故在oc里会对应着一个Class这么一个结构体指针,而在oc里只知道他是这么一个东西,C++在runtime源码里面就会用到Class 里面具体的objc_class相关的内容。个人认为不能把这个搞混了,在头文件创建一个结构体继承(objc_class模拟这样子去使用的时候)之后没问题,一旦在.m文件用到这个继承就会报错(.mm没问题支持C++嘛)。
struct objc_class;
struct objc_object;
typedef struct objc_class *Class;
typedef struct objc_object *id;
Objc_Clang_after.cpp
#ifndef _REWRITER_typedef_Singer
#define _REWRITER_typedef_Singer
typedef struct objc_object Singer;
typedef struct {} _objc_exc_Singer;
#endif
extern "C" unsigned long OBJC_IVAR_$_Singer$_name;
extern "C" unsigned long OBJC_IVAR_$_Singer$_age;
struct Singer_IMPL {
struct NSObject_IMPL NSObject_IVARS;
NSString * _Nonnull _name;
int * _Nonnull _age;
};
// @property (nonatomic,copy)NSString *name;
// @property (nonatomic,assign)int *age;
/* @end */
#ifndef _REWRITER_typedef_Eason
#define _REWRITER_typedef_Eason
typedef struct objc_object Eason;
typedef struct {} _objc_exc_Eason;
#endif
extern "C" unsigned long OBJC_IVAR_$_Eason$_musicAblums;
struct Eason_IMPL {
struct Singer_IMPL Singer_IVARS;
NSMutableArray * _Nonnull _musicAblums;
};
// @property (nonatomic,strong)NSMutableArray *musicAblums;
/* @end */
#pragma clang assume_nonnull end
// @implementation Singer
static void _C_Singer_load(Class self, SEL _cmd) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_w__fpzqhmrd08dczx4bdjqly8040000gn_T_Singer_2e3388_mi_0);
}
static void _I_Singer_instanceFunction(Singer * self, SEL _cmd) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_w__fpzqhmrd08dczx4bdjqly8040000gn_T_Singer_2e3388_mi_1,__func__);
}
static void _C_Singer_classFunction(Class self, SEL _cmd) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_w__fpzqhmrd08dczx4bdjqly8040000gn_T_Singer_2e3388_mi_2);
}
static NSString * _Nonnull _I_Singer_name(Singer * self, SEL _cmd) { return (*(NSString * _Nonnull *)((char *)self + OBJC_IVAR_$_Singer$_name)); }
extern "C" __declspec(dllimport) void objc_setProperty (id, SEL, long, id, bool, bool);
static void _I_Singer_setName_(Singer * self, SEL _cmd, NSString * _Nonnull name) { objc_setProperty (self, _cmd, __OFFSETOFIVAR__(struct Singer, _name), (id)name, 0, 1); }
static int * _Nonnull _I_Singer_age(Singer * self, SEL _cmd) { return (*(int * _Nonnull *)((char *)self + OBJC_IVAR_$_Singer$_age)); }
static void _I_Singer_setAge_(Singer * self, SEL _cmd, int * _Nonnull age) { (*(int * _Nonnull *)((char *)self + OBJC_IVAR_$_Singer$_age)) = age; }
// @end
// @implementation Eason
static NSMutableArray * _Nonnull _I_Eason_musicAblums(Eason * self, SEL _cmd) { return (*(NSMutableArray * _Nonnull *)((char *)self + OBJC_IVAR_$_Eason$_musicAblums)); }
static void _I_Eason_setMusicAblums_(Eason * self, SEL _cmd, NSMutableArray * _Nonnull musicAblums) { (*(NSMutableArray * _Nonnull *)((char *)self + OBJC_IVAR_$_Eason$_musicAblums)) = musicAblums; }
// @end
struct _prop_t {
const char *name;
const char *attributes;
};
struct _protocol_t;
struct _objc_method {
struct objc_selector * _cmd;
const char *method_type;
void *_imp;
};
struct _protocol_t {
void * isa; // NULL
const char *protocol_name;
const struct _protocol_list_t * protocol_list; // super protocols
const struct method_list_t *instance_methods;
const struct method_list_t *class_methods;
const struct method_list_t *optionalInstanceMethods;
const struct method_list_t *optionalClassMethods;
const struct _prop_list_t * properties;
const unsigned int size; // sizeof(struct _protocol_t)
const unsigned int flags; // = 0
const char ** extendedMethodTypes;
};
struct _ivar_t {
unsigned long int *offset; // pointer to ivar offset location
const char *name;
const char *type;
unsigned int alignment;
unsigned int size;
};
struct _class_ro_t {
unsigned int flags;
unsigned int instanceStart;
unsigned int instanceSize;
const unsigned char *ivarLayout;
const char *name;
const struct _method_list_t *baseMethods;
const struct _objc_protocol_list *baseProtocols;
const struct _ivar_list_t *ivars;
const unsigned char *weakIvarLayout;
const struct _prop_list_t *properties;
};
struct _class_t {
struct _class_t *isa;
struct _class_t *superclass;
void *cache;
void *vtable;
struct _class_ro_t *ro;
};
struct _category_t {
const char *name;
struct _class_t *cls;
const struct _method_list_t *instance_methods;
const struct _method_list_t *class_methods;
const struct _protocol_list_t *protocols;
const struct _prop_list_t *properties;
};
extern "C" __declspec(dllimport) struct objc_cache _objc_empty_cache;
#pragma warning(disable:4273)
extern "C" unsigned long int OBJC_IVAR_$_Singer$_name __attribute__ ((used, section ("__DATA,__objc_ivar"))) = __OFFSETOFIVAR__(struct Singer, _name);
extern "C" unsigned long int OBJC_IVAR_$_Singer$_age __attribute__ ((used, section ("__DATA,__objc_ivar"))) = __OFFSETOFIVAR__(struct Singer, _age);
static struct /*_ivar_list_t*/ {
unsigned int entsize; // sizeof(struct _prop_t)
unsigned int count;
struct _ivar_t ivar_list[2];
} _OBJC_$_INSTANCE_VARIABLES_Singer __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_ivar_t),
2,
{{(unsigned long int *)&OBJC_IVAR_$_Singer$_name, "_name", "@\"NSString\"", 3, 8},
{(unsigned long int *)&OBJC_IVAR_$_Singer$_age, "_age", "^i", 3, 8}}
};
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[9];
} _OBJC_$_INSTANCE_METHODS_Singer __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
9,
{{(struct objc_selector *)"instanceFunction", "v16@0:8", (void *)_I_Singer_instanceFunction},
{(struct objc_selector *)"name", "@16@0:8", (void *)_I_Singer_name},
{(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_Singer_setName_},
{(struct objc_selector *)"age", "^i16@0:8", (void *)_I_Singer_age},
{(struct objc_selector *)"setAge:", "v24@0:8^i16", (void *)_I_Singer_setAge_},
{(struct objc_selector *)"name", "@16@0:8", (void *)_I_Singer_name},
{(struct objc_selector *)"setName:", "v24@0:8@16", (void *)_I_Singer_setName_},
{(struct objc_selector *)"age", "^i16@0:8", (void *)_I_Singer_age},
{(struct objc_selector *)"setAge:", "v24@0:8^i16", (void *)_I_Singer_setAge_}}
};
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[2];
} _OBJC_$_CLASS_METHODS_Singer __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
2,
{{(struct objc_selector *)"load", "v16@0:8", (void *)_C_Singer_load},
{(struct objc_selector *)"classFunction", "v16@0:8", (void *)_C_Singer_classFunction}}
};
static struct /*_prop_list_t*/ {
unsigned int entsize; // sizeof(struct _prop_t)
unsigned int count_of_properties;
struct _prop_t prop_list[2];
} _OBJC_$_PROP_LIST_Singer __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_prop_t),
2,
{{"name","T@\"NSString\",C,N,V_name"},
{"age","T^i,N,V_age"}}
};
static struct _class_ro_t _OBJC_METACLASS_RO_$_Singer __attribute__ ((used, section ("__DATA,__objc_const"))) = {
1, sizeof(struct _class_t), sizeof(struct _class_t),
0,
"Singer",
(const struct _method_list_t *)&_OBJC_$_CLASS_METHODS_Singer,
0,
0,
0,
0,
};
static struct _class_ro_t _OBJC_CLASS_RO_$_Singer __attribute__ ((used, section ("__DATA,__objc_const"))) = {
0, __OFFSETOFIVAR__(struct Singer, _name), sizeof(struct Singer_IMPL),
0,
"Singer",
(const struct _method_list_t *)&_OBJC_$_INSTANCE_METHODS_Singer,
0,
(const struct _ivar_list_t *)&_OBJC_$_INSTANCE_VARIABLES_Singer,
0,
(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_Singer,
};
extern "C" __declspec(dllimport) struct _class_t OBJC_METACLASS_$_NSObject;
extern "C" __declspec(dllexport) struct _class_t OBJC_METACLASS_$_Singer __attribute__ ((used, section ("__DATA,__objc_data"))) = {
0, // &OBJC_METACLASS_$_NSObject,
0, // &OBJC_METACLASS_$_NSObject,
0, // (void *)&_objc_empty_cache,
0, // unused, was (void *)&_objc_empty_vtable,
&_OBJC_METACLASS_RO_$_Singer,
};
extern "C" __declspec(dllimport) struct _class_t OBJC_CLASS_$_NSObject;
extern "C" __declspec(dllexport) struct _class_t OBJC_CLASS_$_Singer __attribute__ ((used, section ("__DATA,__objc_data"))) = {
0, // &OBJC_METACLASS_$_Singer,
0, // &OBJC_CLASS_$_NSObject,
0, // (void *)&_objc_empty_cache,
0, // unused, was (void *)&_objc_empty_vtable,
&_OBJC_CLASS_RO_$_Singer,
};
static void OBJC_CLASS_SETUP_$_Singer(void ) {
OBJC_METACLASS_$_Singer.isa = &OBJC_METACLASS_$_NSObject;
OBJC_METACLASS_$_Singer.superclass = &OBJC_METACLASS_$_NSObject;
OBJC_METACLASS_$_Singer.cache = &_objc_empty_cache;
OBJC_CLASS_$_Singer.isa = &OBJC_METACLASS_$_Singer;
OBJC_CLASS_$_Singer.superclass = &OBJC_CLASS_$_NSObject;
OBJC_CLASS_$_Singer.cache = &_objc_empty_cache;
}
extern "C" unsigned long int OBJC_IVAR_$_Eason$_musicAblums __attribute__ ((used, section ("__DATA,__objc_ivar"))) = __OFFSETOFIVAR__(struct Eason, _musicAblums);
static struct /*_ivar_list_t*/ {
unsigned int entsize; // sizeof(struct _prop_t)
unsigned int count;
struct _ivar_t ivar_list[1];
} _OBJC_$_INSTANCE_VARIABLES_Eason __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_ivar_t),
1,
{{(unsigned long int *)&OBJC_IVAR_$_Eason$_musicAblums, "_musicAblums", "@\"NSMutableArray\"", 3, 8}}
};
static struct /*_method_list_t*/ {
unsigned int entsize; // sizeof(struct _objc_method)
unsigned int method_count;
struct _objc_method method_list[4];
} _OBJC_$_INSTANCE_METHODS_Eason __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_objc_method),
4,
{{(struct objc_selector *)"musicAblums", "@16@0:8", (void *)_I_Eason_musicAblums},
{(struct objc_selector *)"setMusicAblums:", "v24@0:8@16", (void *)_I_Eason_setMusicAblums_},
{(struct objc_selector *)"musicAblums", "@16@0:8", (void *)_I_Eason_musicAblums},
{(struct objc_selector *)"setMusicAblums:", "v24@0:8@16", (void *)_I_Eason_setMusicAblums_}}
};
static struct /*_prop_list_t*/ {
unsigned int entsize; // sizeof(struct _prop_t)
unsigned int count_of_properties;
struct _prop_t prop_list[1];
} _OBJC_$_PROP_LIST_Eason __attribute__ ((used, section ("__DATA,__objc_const"))) = {
sizeof(_prop_t),
1,
{{"musicAblums","T@\"NSMutableArray\",&,N,V_musicAblums"}}
};
static struct _class_ro_t _OBJC_METACLASS_RO_$_Eason __attribute__ ((used, section ("__DATA,__objc_const"))) = {
1, sizeof(struct _class_t), sizeof(struct _class_t),
0,
"Eason",
0,
0,
0,
0,
0,
};
static struct _class_ro_t _OBJC_CLASS_RO_$_Eason __attribute__ ((used, section ("__DATA,__objc_const"))) = {
0, __OFFSETOFIVAR__(struct Eason, _musicAblums), sizeof(struct Eason_IMPL),
0,
"Eason",
(const struct _method_list_t *)&_OBJC_$_INSTANCE_METHODS_Eason,
0,
(const struct _ivar_list_t *)&_OBJC_$_INSTANCE_VARIABLES_Eason,
0,
(const struct _prop_list_t *)&_OBJC_$_PROP_LIST_Eason,
};
extern "C" __declspec(dllexport) struct _class_t OBJC_METACLASS_$_Singer;
extern "C" __declspec(dllimport) struct _class_t OBJC_METACLASS_$_NSObject;
extern "C" __declspec(dllexport) struct _class_t OBJC_METACLASS_$_Eason __attribute__ ((used, section ("__DATA,__objc_data"))) = {
0, // &OBJC_METACLASS_$_NSObject,
0, // &OBJC_METACLASS_$_Singer,
0, // (void *)&_objc_empty_cache,
0, // unused, was (void *)&_objc_empty_vtable,
&_OBJC_METACLASS_RO_$_Eason,
};
extern "C" __declspec(dllexport) struct _class_t OBJC_CLASS_$_Singer;
extern "C" __declspec(dllexport) struct _class_t OBJC_CLASS_$_Eason __attribute__ ((used, section ("__DATA,__objc_data"))) = {
0, // &OBJC_METACLASS_$_Eason,
0, // &OBJC_CLASS_$_Singer,
0, // (void *)&_objc_empty_cache,
0, // unused, was (void *)&_objc_empty_vtable,
&_OBJC_CLASS_RO_$_Eason,
};
static void OBJC_CLASS_SETUP_$_Eason(void ) {
OBJC_METACLASS_$_Eason.isa = &OBJC_METACLASS_$_NSObject;
OBJC_METACLASS_$_Eason.superclass = &OBJC_METACLASS_$_Singer;
OBJC_METACLASS_$_Eason.cache = &_objc_empty_cache;
OBJC_CLASS_$_Eason.isa = &OBJC_METACLASS_$_Eason;
OBJC_CLASS_$_Eason.superclass = &OBJC_CLASS_$_Singer;
OBJC_CLASS_$_Eason.cache = &_objc_empty_cache;
}
#pragma section(".objc_inithooks$B", long, read, write)
__declspec(allocate(".objc_inithooks$B")) static void *OBJC_CLASS_SETUP[] = {
(void *)&OBJC_CLASS_SETUP_$_Singer,
(void *)&OBJC_CLASS_SETUP_$_Eason,
};
static struct _class_t *L_OBJC_LABEL_CLASS_$ [2] __attribute__((used, section ("__DATA, __objc_classlist,regular,no_dead_strip")))= {
&OBJC_CLASS_$_Singer,
&OBJC_CLASS_$_Eason,
};
static struct _class_t *_OBJC_LABEL_NONLAZY_CLASS_$[] = {
&OBJC_CLASS_$_Singer,
};
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
__declspec(allocate("segname")) 告诉编译器分配一个名字为segname的段
在实际使用时,还需要#pragma section事先告诉编译器,我们要使用的段名