-
Notifications
You must be signed in to change notification settings - Fork 118
/
legacy.c
460 lines (438 loc) · 12.7 KB
/
legacy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "objc/runtime.h"
#include "objc/encoding.h"
#include "legacy.h"
#include "properties.h"
#include "protocol.h"
#include "class.h"
#include "loader.h"
PRIVATE size_t lengthOfTypeEncoding(const char *types);
enum objc_class_flags_gsv1
{
/** This class structure represents a class. */
objc_class_flag_class_gsv1 = (1<<0),
/** This class structure represents a metaclass. */
objc_class_flag_meta_gsv1 = (1<<1),
/**
* The class uses the new, Objective-C 2, runtime ABI. This ABI defines an
* ABI version field inside the class, and so will be used for all
* subsequent versions that retain some degree of compatibility.
*/
objc_class_flag_new_abi_gsv1 = (1<<4)
};
static inline BOOL objc_test_class_flag_gsv1(struct objc_class_gsv1 *aClass,
enum objc_class_flags_gsv1 flag)
{
return (aClass->info & (unsigned long)flag) == (unsigned long)flag;
}
/**
* Checks the version of a class. Return values are:
* 0. Legacy GCC ABI compatible class.
* 1. First release of GNUstep ABI.
* 2. Second release of the GNUstep ABI, adds strong / weak ivar bitmaps.
* 3. Third release of the GNUstep ABI. Many cleanups.
*/
static inline int objc_get_class_version_gsv1(struct objc_class_gsv1 *aClass)
{
if (!objc_test_class_flag_gsv1(aClass, objc_class_flag_new_abi_gsv1))
{
return 0;
}
return aClass->abi_version + 1;
}
static objc_ivar_ownership ownershipForIvar(struct objc_class_gsv1 *cls, int idx)
{
if (objc_get_class_version_gsv1(cls) < 2)
{
return ownership_unsafe;
}
if (objc_bitfield_test(cls->strong_pointers, idx))
{
return ownership_strong;
}
if (objc_bitfield_test(cls->weak_pointers, idx))
{
return ownership_weak;
}
return ownership_unsafe;
}
static struct objc_ivar_list *upgradeIvarList(struct objc_class_gsv1 *cls)
{
struct objc_ivar_list_gcc *l = cls->ivars;
if (l == NULL)
{
return NULL;
}
struct objc_ivar_list *n = calloc(1, sizeof(struct objc_ivar_list) +
l->count*sizeof(struct objc_ivar));
n->size = sizeof(struct objc_ivar);
n->count = l->count;
for (int i=0 ; i<l->count ; i++)
{
BOOL isBitfield = NO;
int bitfieldSize = 0;
int nextOffset;
// Bitfields have the same offset, but should have their size set to
// the size of the bitfield. We calculate the size of the bitfield by
// looking for the next ivar after the current one that has a different
// offset.
if (i+1 < l->count)
{
nextOffset = l->ivar_list[i+1].offset;
if (l->ivar_list[i].offset == l->ivar_list[i+1].offset)
{
isBitfield = YES;
for (int j=i+2 ; j<l->count ; j++)
{
if (l->ivar_list[i].offset != l->ivar_list[j].offset)
{
bitfieldSize = l->ivar_list[j].offset - l->ivar_list[i].offset;
break;
}
}
if (bitfieldSize == 0)
{
bitfieldSize = cls->instance_size - l->ivar_list[i].offset;
}
}
}
else
{
nextOffset = cls->instance_size;
}
if (nextOffset < 0)
{
nextOffset = -nextOffset;
}
const char *type = l->ivar_list[i].type;
int size = nextOffset - l->ivar_list[i].offset;
n->ivar_list[i].name = l->ivar_list[i].name;
n->ivar_list[i].type = type;
n->ivar_list[i].size = isBitfield ? bitfieldSize : size;
if (objc_test_class_flag_gsv1(cls, objc_class_flag_new_abi_gsv1))
{
n->ivar_list[i].offset = cls->ivar_offsets[i];
}
else
{
n->ivar_list[i].offset = &l->ivar_list[i].offset;
}
ivarSetAlign(&n->ivar_list[i], ((type == NULL) || type[0] == 0) ? __alignof__(void*) : objc_alignof_type(type));
if (type[0] == '\0')
{
ivarSetAlign(&n->ivar_list[i], size);
}
ivarSetOwnership(&n->ivar_list[i], ownershipForIvar(cls, i));
}
return n;
}
static struct objc_method_list *upgradeMethodList(struct objc_method_list_gcc *old)
{
if (old == NULL)
{
return NULL;
}
if (old->count == 0)
{
return NULL;
}
struct objc_method_list *l = calloc(sizeof(struct objc_method_list) + old->count * sizeof(struct objc_method), 1);
l->count = old->count;
if (old->next)
{
l->next = upgradeMethodList(old->next);
}
l->size = sizeof(struct objc_method);
for (int i=0 ; i<old->count ; i++)
{
l->methods[i].imp = old->methods[i].imp;
l->methods[i].selector = old->methods[i].selector;
l->methods[i].types = old->methods[i].types;
}
return l;
}
static inline BOOL checkAttribute(char field, int attr)
{
return (field & attr) == attr;
}
static void upgradeProperty(struct objc_property *n, struct objc_property_gsv1 *o)
{
char *typeEncoding;
ptrdiff_t typeSize;
if (o->name[0] == '\0')
{
n->name = o->name + o->name[1];
n->attributes = o->name + 2;
// If we have an attribute string, then it will contain a more accurate
// version of the types than we'll find in the getter (qualifiers such
// as _Atomic and volatile may be dropped)
assert(n->attributes[0] == 'T');
const char *type_start = &n->attributes[1];
const char *type_end = strchr(type_start, ',');
if (type_end == NULL)
{
type_end = type_start + strlen(type_start);
}
typeSize = type_end - type_start;
typeEncoding = malloc(typeSize + 1);
memcpy(typeEncoding, type_start, typeSize);
typeEncoding[typeSize] = 0;
}
else
{
typeSize = (ptrdiff_t)lengthOfTypeEncoding(o->getter_types);
typeEncoding = malloc(typeSize + 1);
memcpy(typeEncoding, o->getter_types, typeSize);
typeEncoding[typeSize] = 0;
}
n->type = typeEncoding;
if (o->getter_name)
{
n->getter = sel_registerTypedName_np(o->getter_name, o->getter_types);
}
if (o->setter_name)
{
n->setter = sel_registerTypedName_np(o->setter_name, o->setter_types);
}
if (o->name[0] == '\0')
{
return;
}
n->name = o->name;
const char *name = o->name;
size_t nameSize = (NULL == name) ? 0 : strlen(name);
// Encoding is T{type},V{name}, so 4 bytes for the "T,V" that we always
// need. We also need two bytes for the leading null and the length.
size_t encodingSize = typeSize + nameSize + 6;
char flags[20];
size_t i = 0;
// Flags that are a comma then a character
if (checkAttribute(o->attributes, OBJC_PR_readonly))
{
flags[i++] = ',';
flags[i++] = 'R';
}
if (checkAttribute(o->attributes, OBJC_PR_retain))
{
flags[i++] = ',';
flags[i++] = '&';
}
if (checkAttribute(o->attributes, OBJC_PR_copy))
{
flags[i++] = ',';
flags[i++] = 'C';
}
if (checkAttribute(o->attributes2, OBJC_PR_weak))
{
flags[i++] = ',';
flags[i++] = 'W';
}
if (checkAttribute(o->attributes2, OBJC_PR_dynamic))
{
flags[i++] = ',';
flags[i++] = 'D';
}
if ((o->attributes & OBJC_PR_nonatomic) == OBJC_PR_nonatomic)
{
flags[i++] = ',';
flags[i++] = 'N';
}
encodingSize += i;
flags[i] = '\0';
size_t setterLength = 0;
size_t getterLength = 0;
if ((o->attributes & OBJC_PR_getter) == OBJC_PR_getter)
{
getterLength = strlen(o->getter_name);
encodingSize += 2 + getterLength;
}
if ((o->attributes & OBJC_PR_setter) == OBJC_PR_setter)
{
setterLength = strlen(o->setter_name);
encodingSize += 2 + setterLength;
}
unsigned char *encoding = malloc(encodingSize);
// Set the leading 0 and the offset of the name
unsigned char *insert = encoding;
BOOL needsComma = NO;
*(insert++) = 0;
*(insert++) = 0;
// Set the type encoding
*(insert++) = 'T';
memcpy(insert, typeEncoding, typeSize);
insert += typeSize;
needsComma = YES;
// Set the flags
memcpy(insert, flags, i);
insert += i;
if ((o->attributes & OBJC_PR_getter) == OBJC_PR_getter)
{
if (needsComma)
{
*(insert++) = ',';
}
i++;
needsComma = YES;
*(insert++) = 'G';
memcpy(insert, o->getter_name, getterLength);
insert += getterLength;
}
if ((o->attributes & OBJC_PR_setter) == OBJC_PR_setter)
{
if (needsComma)
{
*(insert++) = ',';
}
i++;
needsComma = YES;
*(insert++) = 'S';
memcpy(insert, o->setter_name, setterLength);
insert += setterLength;
}
if (needsComma)
{
*(insert++) = ',';
}
*(insert++) = 'V';
memcpy(insert, name, nameSize);
insert += nameSize;
*(insert++) = '\0';
n->attributes = (const char*)encoding;
}
static struct objc_property_list *upgradePropertyList(struct objc_property_list_gsv1 *l)
{
if (l == NULL)
{
return NULL;
}
size_t data_size = l->count * sizeof(struct objc_property);
struct objc_property_list *n = calloc(1, sizeof(struct objc_property_list) + data_size);
n->count = l->count;
n->size = sizeof(struct objc_property);
for (int i=0 ; i<l->count ; i++)
{
upgradeProperty(&n->properties[i], &l->properties[i]);
}
return n;
}
static int legacy_key;
PRIVATE struct objc_class_gsv1* objc_legacy_class_for_class(Class cls)
{
return (struct objc_class_gsv1*)objc_getAssociatedObject((id)cls, &legacy_key);
}
PRIVATE Class objc_upgrade_class(struct objc_class_gsv1 *oldClass)
{
Class cls = calloc(sizeof(struct objc_class), 1);
cls->isa = oldClass->isa;
// super_class is left nil and we upgrade it later.
cls->name = oldClass->name;
cls->version = oldClass->version;
cls->info = objc_class_flag_meta;
cls->instance_size = oldClass->instance_size;
cls->ivars = upgradeIvarList(oldClass);
cls->methods = upgradeMethodList(oldClass->methods);
cls->protocols = oldClass->protocols;
cls->abi_version = oldClass->abi_version;
cls->properties = upgradePropertyList(oldClass->properties);
objc_register_selectors_from_class(cls);
if (!objc_test_class_flag_gsv1(oldClass, objc_class_flag_meta_gsv1))
{
cls->info = 0;
cls->isa = objc_upgrade_class((struct objc_class_gsv1*)cls->isa);
objc_setAssociatedObject((id)cls, &legacy_key, (id)oldClass, OBJC_ASSOCIATION_ASSIGN);
}
else
{
cls->instance_size = sizeof(struct objc_class);
}
return cls;
}
PRIVATE struct objc_category *objc_upgrade_category(struct objc_category_gcc *old)
{
struct objc_category *cat = calloc(1, sizeof(struct objc_category));
memcpy(cat, old, sizeof(struct objc_category_gcc));
cat->instance_methods = upgradeMethodList(old->instance_methods);
cat->class_methods = upgradeMethodList(old->class_methods);
if (cat->instance_methods != NULL)
{
objc_register_selectors_from_list(cat->instance_methods);
}
if (cat->class_methods != NULL)
{
objc_register_selectors_from_list(cat->class_methods);
}
for (int i=0 ; i<cat->protocols->count ; i++)
{
objc_init_protocols(cat->protocols);
}
return cat;
}
static struct objc_protocol_method_description_list*
upgrade_protocol_method_list_gcc(struct objc_protocol_method_description_list_gcc *l)
{
if ((l == NULL) || (l->count == 0))
{
return NULL;
}
struct objc_protocol_method_description_list *n =
malloc(sizeof(struct objc_protocol_method_description_list) +
l->count * sizeof(struct objc_protocol_method_description));
n->count = l->count;
n->size = sizeof(struct objc_protocol_method_description);
for (int i=0 ; i<n->count ; i++)
{
n->methods[i].selector = sel_registerTypedName_np(l->methods[i].name, l->methods[i].types);
n->methods[i].types = l->methods[i].types;
}
return n;
}
PRIVATE struct objc_protocol *objc_upgrade_protocol_gcc(struct objc_protocol_gcc *p)
{
// If the protocol has already been upgraded, the don't try to upgrade it twice.
if (p->isa == (id)&_OBJC_CLASS_ProtocolGCC)
{
return objc_getProtocol(p->name);
}
p->isa = (id)&_OBJC_CLASS_ProtocolGCC;
Protocol *proto =
(Protocol*)class_createInstance(&_OBJC_CLASS_Protocol, 0);
proto->name = p->name;
// Aliasing of this between the new and old structures means that when this
// returns these will all be updated.
proto->protocol_list = p->protocol_list;
proto->instance_methods = upgrade_protocol_method_list_gcc(p->instance_methods);
proto->class_methods = upgrade_protocol_method_list_gcc(p->class_methods);
assert(proto->isa);
return proto;
}
PRIVATE struct objc_protocol *objc_upgrade_protocol_gsv1(struct objc_protocol_gsv1 *p)
{
// If the protocol has already been upgraded, the don't try to upgrade it twice.
if (p->isa == (id)&_OBJC_CLASS_ProtocolGSv1)
{
return objc_getProtocol(p->name);
}
Protocol *n =
(Protocol*)class_createInstance(&_OBJC_CLASS_Protocol, 0);
n->instance_methods = upgrade_protocol_method_list_gcc(p->instance_methods);
// Aliasing of this between the new and old structures means that when this
// returns these will all be updated.
n->name = p->name;
n->protocol_list = p->protocol_list;
n->class_methods = upgrade_protocol_method_list_gcc(p->class_methods);
n->properties = upgradePropertyList(p->properties);
n->optional_properties = upgradePropertyList(p->optional_properties);
n->isa = (id)&_OBJC_CLASS_Protocol;
// We do in-place upgrading of these, because they might be referenced
// directly
p->instance_methods = (struct objc_protocol_method_description_list_gcc*)n->instance_methods;
p->class_methods = (struct objc_protocol_method_description_list_gcc*)n->class_methods;
p->properties = (struct objc_property_list_gsv1*)n->properties;
p->optional_properties = (struct objc_property_list_gsv1*)n->optional_properties;
p->isa = (id)&_OBJC_CLASS_ProtocolGSv1;
assert(p->isa);
return n;
}