]>
Commit | Line | Data |
---|---|---|
b3962a83 A |
1 | /* |
2 | * Copyright (c) 1999-2007 Apple Inc. All Rights Reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /*********************************************************************** | |
25 | * objc-runtime-old.m | |
26 | * Support for old-ABI classes and images. | |
27 | **********************************************************************/ | |
28 | ||
29 | /*********************************************************************** | |
30 | * Class loading and connecting (GrP 2004-2-11) | |
31 | * | |
32 | * When images are loaded (during program startup or otherwise), the | |
33 | * runtime needs to load classes and categories from the images, connect | |
34 | * classes to superclasses and categories to parent classes, and call | |
35 | * +load methods. | |
36 | * | |
37 | * The Objective-C runtime can cope with classes arriving in any order. | |
38 | * That is, a class may be discovered by the runtime before some | |
39 | * superclass is known. To handle out-of-order class loads, the | |
40 | * runtime uses a "pending class" system. | |
41 | * | |
42 | * (Historical note) | |
43 | * Panther and earlier: many classes arrived out-of-order because of | |
44 | * the poorly-ordered callback from dyld. However, the runtime's | |
45 | * pending mechanism only handled "missing superclass" and not | |
46 | * "present superclass but missing higher class". See Radar #3225652. | |
47 | * Tiger: The runtime's pending mechanism was augmented to handle | |
48 | * arbitrary missing classes. In addition, dyld was rewritten and | |
49 | * now sends the callbacks in strictly bottom-up link order. | |
50 | * The pending mechanism may now be needed only for rare and | |
51 | * hard to construct programs. | |
52 | * (End historical note) | |
53 | * | |
54 | * A class when first seen in an image is considered "unconnected". | |
55 | * It is stored in `unconnected_class_hash`. If all of the class's | |
56 | * superclasses exist and are already "connected", then the new class | |
57 | * can be connected to its superclasses and moved to `class_hash` for | |
58 | * normal use. Otherwise, the class waits in `unconnected_class_hash` | |
59 | * until the superclasses finish connecting. | |
60 | * | |
61 | * A "connected" class is | |
62 | * (1) in `class_hash`, | |
63 | * (2) connected to its superclasses, | |
64 | * (3) has no unconnected superclasses, | |
65 | * (4) is otherwise initialized and ready for use, and | |
66 | * (5) is eligible for +load if +load has not already been called. | |
67 | * | |
68 | * An "unconnected" class is | |
69 | * (1) in `unconnected_class_hash`, | |
70 | * (2) not connected to its superclasses, | |
71 | * (3) has an immediate superclass which is either missing or unconnected, | |
72 | * (4) is not ready for use, and | |
73 | * (5) is not yet eligible for +load. | |
74 | * | |
75 | * Image mapping is NOT CURRENTLY THREAD-SAFE with respect to just about | |
ee974f79 | 76 | * anything. Image mapping IS RE-ENTRANT in several places: superclass |
b3962a83 A |
77 | * lookup may cause ZeroLink to load another image, and +load calls may |
78 | * cause dyld to load another image. | |
79 | * | |
80 | * Image mapping sequence: | |
81 | * | |
82 | * Read all classes in all new images. | |
83 | * Add them all to unconnected_class_hash. | |
84 | * Note any +load implementations before categories are attached. | |
b3962a83 A |
85 | * Attach any pending categories. |
86 | * Read all categories in all new images. | |
87 | * Attach categories whose parent class exists (connected or not), | |
88 | * and pend the rest. | |
89 | * Mark them all eligible for +load (if implemented), even if the | |
90 | * parent class is missing. | |
91 | * Try to connect all classes in all new images. | |
92 | * If the superclass is missing, pend the class | |
93 | * If the superclass is unconnected, try to recursively connect it | |
94 | * If the superclass is connected: | |
95 | * connect the class | |
96 | * mark the class eligible for +load, if implemented | |
ee974f79 | 97 | * fix up any pended classrefs referring to the class |
b3962a83 A |
98 | * connect any pended subclasses of the class |
99 | * Resolve selector refs and class refs in all new images. | |
100 | * Class refs whose classes still do not exist are pended. | |
101 | * Fix up protocol objects in all new images. | |
102 | * Call +load for classes and categories. | |
103 | * May include classes or categories that are not in these images, | |
104 | * but are newly eligible because of these image. | |
105 | * Class +loads will be called superclass-first because of the | |
106 | * superclass-first nature of the connecting process. | |
107 | * Category +load needs to be deferred until the parent class is | |
108 | * connected and has had its +load called. | |
109 | * | |
110 | * Performance: all classes are read before any categories are read. | |
111 | * Fewer categories need be pended for lack of a parent class. | |
112 | * | |
113 | * Performance: all categories are attempted to be attached before | |
114 | * any classes are connected. Fewer class caches need be flushed. | |
115 | * (Unconnected classes and their respective subclasses are guaranteed | |
116 | * to be un-messageable, so their caches will be empty.) | |
117 | * | |
118 | * Performance: all classes are read before any classes are connected. | |
119 | * Fewer classes need be pended for lack of a superclass. | |
120 | * | |
121 | * Correctness: all selector and class refs are fixed before any | |
122 | * protocol fixups or +load methods. libobjc itself contains selector | |
123 | * and class refs which are used in protocol fixup and +load. | |
124 | * | |
125 | * Correctness: +load methods are scheduled in bottom-up link order. | |
126 | * This constraint is in addition to superclass order. Some +load | |
127 | * implementations expect to use another class in a linked-to library, | |
128 | * even if the two classes don't share a direct superclass relationship. | |
129 | * | |
130 | * Correctness: all classes are scanned for +load before any categories | |
131 | * are attached. Otherwise, if a category implements +load and its class | |
132 | * has no class methods, the class's +load scan would find the category's | |
133 | * +load method, which would then be called twice. | |
ee974f79 A |
134 | * |
135 | * Correctness: pended class refs are not fixed up until the class is | |
136 | * connected. Classes with missing weak superclasses remain unconnected. | |
7257e56c | 137 | * Class refs to classes with missing weak superclasses must be nil. |
ee974f79 | 138 | * Therefore class refs to unconnected classes must remain un-fixed. |
b3962a83 A |
139 | * |
140 | **********************************************************************/ | |
141 | ||
142 | #if !__OBJC2__ | |
143 | ||
7af964d1 | 144 | #include "objc-private.h" |
8972963c | 145 | #include "objc-runtime-old.h" |
7257e56c A |
146 | #include "objc-file-old.h" |
147 | #include "objc-cache-old.h" | |
7af964d1 | 148 | #include "objc-loadmethod.h" |
b3962a83 | 149 | |
b3962a83 A |
150 | |
151 | typedef struct _objc_unresolved_category | |
152 | { | |
153 | struct _objc_unresolved_category *next; | |
7257e56c | 154 | old_category *cat; // may be nil |
b3962a83 A |
155 | long version; |
156 | } _objc_unresolved_category; | |
157 | ||
158 | typedef struct _PendingSubclass | |
159 | { | |
7257e56c | 160 | Class subclass; // subclass to finish connecting; may be nil |
b3962a83 A |
161 | struct _PendingSubclass *next; |
162 | } PendingSubclass; | |
163 | ||
164 | typedef struct _PendingClassRef | |
165 | { | |
7257e56c | 166 | Class *ref; // class reference to fix up; may be nil |
b3962a83 A |
167 | // (ref & 1) is a metaclass reference |
168 | struct _PendingClassRef *next; | |
169 | } PendingClassRef; | |
170 | ||
171 | ||
172 | static uintptr_t classHash(void *info, Class data); | |
173 | static int classIsEqual(void *info, Class name, Class cls); | |
174 | static int _objc_defaultClassHandler(const char *clsName); | |
b3962a83 A |
175 | static inline NXMapTable *pendingClassRefsMapTable(void); |
176 | static inline NXMapTable *pendingSubclassesMapTable(void); | |
7257e56c | 177 | static void pendClassInstallation(Class cls, const char *superName); |
31875a97 | 178 | static void pendClassReference(Class *ref, const char *className, bool isMeta); |
7257e56c A |
179 | static void resolve_references_to_class(Class cls); |
180 | static void resolve_subclasses_of_class(Class cls); | |
181 | static void really_connect_class(Class cls, Class supercls); | |
31875a97 A |
182 | static bool connect_class(Class cls); |
183 | static void map_method_descs (struct objc_method_description_list * methods, bool copy); | |
7257e56c A |
184 | static void _objcTweakMethodListPointerForClass(Class cls); |
185 | static inline void _objc_add_category(Class cls, old_category *category, int version); | |
31875a97 | 186 | static bool _objc_add_category_flush_caches(Class cls, old_category *category, int version); |
b3962a83 | 187 | static _objc_unresolved_category *reverse_cat(_objc_unresolved_category *cat); |
7257e56c | 188 | static void resolve_categories_for_class(Class cls); |
31875a97 | 189 | static bool _objc_register_category(old_category *cat, int version); |
b3962a83 A |
190 | |
191 | ||
192 | // Function called when a class is loaded from an image | |
cd5f04f5 | 193 | void (*callbackFunction)(Class, Category) = 0; |
b3962a83 | 194 | |
b3962a83 | 195 | // Hash table of classes |
cd5f04f5 | 196 | NXHashTable * class_hash = 0; |
b3962a83 A |
197 | static NXHashTablePrototype classHashPrototype = |
198 | { | |
199 | (uintptr_t (*) (const void *, const void *)) classHash, | |
200 | (int (*)(const void *, const void *, const void *)) classIsEqual, | |
201 | NXNoEffectFree, 0 | |
202 | }; | |
203 | ||
204 | // Hash table of unconnected classes | |
7257e56c | 205 | static NXHashTable *unconnected_class_hash = nil; |
b3962a83 A |
206 | |
207 | // Exported copy of class_hash variable (hook for debugging tools) | |
7257e56c | 208 | NXHashTable *_objc_debug_class_hash = nil; |
b3962a83 A |
209 | |
210 | // Category and class registries | |
211 | // Keys are COPIES of strings, to prevent stale pointers with unloaded bundles | |
212 | // Use NXMapKeyCopyingInsert and NXMapKeyFreeingRemove | |
7257e56c | 213 | static NXMapTable * category_hash = nil; |
b3962a83 A |
214 | |
215 | // Keys are COPIES of strings, to prevent stale pointers with unloaded bundles | |
216 | // Use NXMapKeyCopyingInsert and NXMapKeyFreeingRemove | |
7257e56c A |
217 | static NXMapTable * pendingClassRefsMap = nil; |
218 | static NXMapTable * pendingSubclassesMap = nil; | |
b3962a83 A |
219 | |
220 | // Protocols | |
7257e56c A |
221 | static NXMapTable *protocol_map = nil; // name -> protocol |
222 | static NXMapTable *protocol_ext_map = nil; // protocol -> protocol ext | |
b3962a83 A |
223 | |
224 | // Function pointer objc_getClass calls through when class is not found | |
225 | static int (*objc_classHandler) (const char *) = _objc_defaultClassHandler; | |
226 | ||
227 | // Function pointer called by objc_getClass and objc_lookupClass when | |
228 | // class is not found. _objc_classLoader is called before objc_classHandler. | |
7257e56c | 229 | static BOOL (*_objc_classLoader)(const char *) = nil; |
b3962a83 A |
230 | |
231 | ||
b3962a83 A |
232 | /*********************************************************************** |
233 | * objc_dump_class_hash. Log names of all known classes. | |
234 | **********************************************************************/ | |
cd5f04f5 | 235 | void objc_dump_class_hash(void) |
b3962a83 A |
236 | { |
237 | NXHashTable *table; | |
238 | unsigned count; | |
239 | Class data; | |
240 | NXHashState state; | |
241 | ||
242 | table = class_hash; | |
243 | count = 0; | |
244 | state = NXInitHashState (table); | |
245 | while (NXNextHashState (table, &state, (void **) &data)) | |
8070259c | 246 | printf ("class %d: %s\n", ++count, data->nameForLogging()); |
b3962a83 A |
247 | } |
248 | ||
249 | ||
250 | /*********************************************************************** | |
251 | * _objc_init_class_hash. Return the class lookup table, create it if | |
252 | * necessary. | |
253 | **********************************************************************/ | |
cd5f04f5 | 254 | void _objc_init_class_hash(void) |
b3962a83 A |
255 | { |
256 | // Do nothing if class hash table already exists | |
257 | if (class_hash) | |
258 | return; | |
259 | ||
260 | // class_hash starts small, with only enough capacity for libobjc itself. | |
261 | // If a second library is found by map_images(), class_hash is immediately | |
262 | // resized to capacity 1024 to cut down on rehashes. | |
263 | // Old numbers: A smallish Foundation+AppKit program will have | |
264 | // about 520 classes. Larger apps (like IB or WOB) have more like | |
265 | // 800 classes. Some customers have massive quantities of classes. | |
266 | // Foundation-only programs aren't likely to notice the ~6K loss. | |
31875a97 | 267 | class_hash = NXCreateHashTable(classHashPrototype, 16, nil); |
b3962a83 A |
268 | _objc_debug_class_hash = class_hash; |
269 | } | |
270 | ||
271 | ||
272 | /*********************************************************************** | |
273 | * objc_getClassList. Return the known classes. | |
274 | **********************************************************************/ | |
275 | int objc_getClassList(Class *buffer, int bufferLen) | |
276 | { | |
277 | NXHashState state; | |
7af964d1 | 278 | Class cls; |
b3962a83 A |
279 | int cnt, num; |
280 | ||
31875a97 A |
281 | mutex_locker_t lock(classLock); |
282 | if (!class_hash) return 0; | |
283 | ||
b3962a83 | 284 | num = NXCountHashTable(class_hash); |
31875a97 A |
285 | if (nil == buffer) return num; |
286 | ||
b3962a83 A |
287 | cnt = 0; |
288 | state = NXInitHashState(class_hash); | |
289 | while (cnt < bufferLen && | |
7af964d1 | 290 | NXNextHashState(class_hash, &state, (void **)&cls)) |
b3962a83 | 291 | { |
7af964d1 | 292 | buffer[cnt++] = cls; |
b3962a83 | 293 | } |
31875a97 | 294 | |
b3962a83 A |
295 | return num; |
296 | } | |
297 | ||
298 | ||
8972963c A |
299 | /*********************************************************************** |
300 | * objc_copyClassList | |
301 | * Returns pointers to all classes. | |
302 | * This requires all classes be realized, which is regretfully non-lazy. | |
303 | * | |
7257e56c A |
304 | * outCount may be nil. *outCount is the number of classes returned. |
305 | * If the returned array is not nil, it is nil-terminated and must be | |
8972963c A |
306 | * freed with free(). |
307 | * Locking: acquires classLock | |
308 | **********************************************************************/ | |
309 | Class * | |
310 | objc_copyClassList(unsigned int *outCount) | |
311 | { | |
312 | Class *result; | |
313 | unsigned int count; | |
314 | ||
31875a97 | 315 | mutex_locker_t lock(classLock); |
7257e56c | 316 | result = nil; |
8972963c A |
317 | count = class_hash ? NXCountHashTable(class_hash) : 0; |
318 | ||
319 | if (count > 0) { | |
320 | Class cls; | |
321 | NXHashState state = NXInitHashState(class_hash); | |
7257e56c | 322 | result = (Class *)malloc((1+count) * sizeof(Class)); |
8972963c A |
323 | count = 0; |
324 | while (NXNextHashState(class_hash, &state, (void **)&cls)) { | |
325 | result[count++] = cls; | |
326 | } | |
7257e56c | 327 | result[count] = nil; |
8972963c | 328 | } |
8972963c A |
329 | |
330 | if (outCount) *outCount = count; | |
331 | return result; | |
332 | } | |
333 | ||
334 | ||
b3962a83 A |
335 | /*********************************************************************** |
336 | * objc_copyProtocolList | |
337 | * Returns pointers to all protocols. | |
338 | * Locking: acquires classLock | |
339 | **********************************************************************/ | |
8972963c | 340 | Protocol * __unsafe_unretained * |
b3962a83 A |
341 | objc_copyProtocolList(unsigned int *outCount) |
342 | { | |
b3962a83 A |
343 | int count, i; |
344 | Protocol *proto; | |
345 | const char *name; | |
346 | NXMapState state; | |
347 | Protocol **result; | |
348 | ||
31875a97 | 349 | mutex_locker_t lock(classLock); |
7af964d1 | 350 | |
b3962a83 A |
351 | count = NXCountMapTable(protocol_map); |
352 | if (count == 0) { | |
b3962a83 | 353 | if (outCount) *outCount = 0; |
7257e56c | 354 | return nil; |
b3962a83 A |
355 | } |
356 | ||
7257e56c | 357 | result = (Protocol **)calloc(1 + count, sizeof(Protocol *)); |
b3962a83 A |
358 | |
359 | i = 0; | |
360 | state = NXInitMapState(protocol_map); | |
361 | while (NXNextMapState(protocol_map, &state, | |
362 | (const void **)&name, (const void **)&proto)) | |
363 | { | |
364 | result[i++] = proto; | |
365 | } | |
366 | ||
7257e56c | 367 | result[i++] = nil; |
1807f628 | 368 | ASSERT(i == count+1); |
b3962a83 | 369 | |
b3962a83 A |
370 | if (outCount) *outCount = count; |
371 | return result; | |
372 | } | |
373 | ||
374 | ||
375 | /*********************************************************************** | |
376 | * objc_getClasses. Return class lookup table. | |
377 | * | |
378 | * NOTE: This function is very dangerous, since you cannot safely use | |
379 | * the hashtable without locking it, and the lock is private! | |
380 | **********************************************************************/ | |
381 | void *objc_getClasses(void) | |
382 | { | |
383 | OBJC_WARN_DEPRECATED; | |
384 | ||
385 | // Return the class lookup hash table | |
386 | return class_hash; | |
387 | } | |
388 | ||
389 | ||
390 | /*********************************************************************** | |
391 | * classHash. | |
392 | **********************************************************************/ | |
393 | static uintptr_t classHash(void *info, Class data) | |
394 | { | |
395 | // Nil classes hash to zero | |
396 | if (!data) | |
397 | return 0; | |
398 | ||
399 | // Call through to real hash function | |
8070259c | 400 | return _objc_strhash (data->mangledName()); |
b3962a83 A |
401 | } |
402 | ||
403 | /*********************************************************************** | |
404 | * classIsEqual. Returns whether the class names match. If we ever | |
405 | * check more than the name, routines like objc_lookUpClass have to | |
406 | * change as well. | |
407 | **********************************************************************/ | |
408 | static int classIsEqual(void *info, Class name, Class cls) | |
409 | { | |
410 | // Standard string comparison | |
8070259c | 411 | return strcmp(name->mangledName(), cls->mangledName()) == 0; |
b3962a83 A |
412 | } |
413 | ||
414 | ||
415 | // Unresolved future classes | |
7257e56c | 416 | static NXHashTable *future_class_hash = nil; |
b3962a83 A |
417 | |
418 | // Resolved future<->original classes | |
7257e56c A |
419 | static NXMapTable *future_class_to_original_class_map = nil; |
420 | static NXMapTable *original_class_to_future_class_map = nil; | |
b3962a83 A |
421 | |
422 | // CF requests about 20 future classes; HIToolbox requests one. | |
423 | #define FUTURE_COUNT 32 | |
424 | ||
425 | ||
426 | /*********************************************************************** | |
427 | * setOriginalClassForFutureClass | |
428 | * Record resolution of a future class. | |
429 | **********************************************************************/ | |
7257e56c A |
430 | static void setOriginalClassForFutureClass(Class futureClass, |
431 | Class originalClass) | |
b3962a83 A |
432 | { |
433 | if (!future_class_to_original_class_map) { | |
434 | future_class_to_original_class_map = | |
31875a97 | 435 | NXCreateMapTable(NXPtrValueMapPrototype, FUTURE_COUNT); |
b3962a83 | 436 | original_class_to_future_class_map = |
31875a97 | 437 | NXCreateMapTable(NXPtrValueMapPrototype, FUTURE_COUNT); |
b3962a83 A |
438 | } |
439 | ||
440 | NXMapInsert (future_class_to_original_class_map, | |
441 | futureClass, originalClass); | |
442 | NXMapInsert (original_class_to_future_class_map, | |
443 | originalClass, futureClass); | |
444 | ||
445 | if (PrintFuture) { | |
7257e56c | 446 | _objc_inform("FUTURE: using %p instead of %p for %s", (void*)futureClass, (void*)originalClass, originalClass->name); |
b3962a83 A |
447 | } |
448 | } | |
449 | ||
450 | /*********************************************************************** | |
451 | * getOriginalClassForFutureClass | |
452 | * getFutureClassForOriginalClass | |
453 | * Switch between a future class and its corresponding original class. | |
454 | * The future class is the one actually in use. | |
455 | * The original class is the one from disk. | |
456 | **********************************************************************/ | |
457 | /* | |
7257e56c A |
458 | static Class |
459 | getOriginalClassForFutureClass(Class futureClass) | |
b3962a83 A |
460 | { |
461 | if (!future_class_to_original_class_map) return Nil; | |
462 | return NXMapGet (future_class_to_original_class_map, futureClass); | |
463 | } | |
464 | */ | |
7257e56c A |
465 | static Class |
466 | getFutureClassForOriginalClass(Class originalClass) | |
b3962a83 A |
467 | { |
468 | if (!original_class_to_future_class_map) return Nil; | |
7257e56c | 469 | return (Class)NXMapGet(original_class_to_future_class_map, originalClass); |
b3962a83 A |
470 | } |
471 | ||
472 | ||
473 | /*********************************************************************** | |
474 | * makeFutureClass | |
475 | * Initialize the memory in *cls with an unresolved future class with the | |
476 | * given name. The memory is recorded in future_class_hash. | |
477 | **********************************************************************/ | |
7257e56c | 478 | static void makeFutureClass(Class cls, const char *name) |
b3962a83 A |
479 | { |
480 | // CF requests about 20 future classes, plus HIToolbox has one. | |
481 | if (!future_class_hash) { | |
482 | future_class_hash = | |
31875a97 | 483 | NXCreateHashTable(classHashPrototype, FUTURE_COUNT, nil); |
b3962a83 A |
484 | } |
485 | ||
31875a97 | 486 | cls->name = strdup(name); |
b3962a83 A |
487 | NXHashInsert(future_class_hash, cls); |
488 | ||
489 | if (PrintFuture) { | |
7257e56c | 490 | _objc_inform("FUTURE: reserving %p for %s", (void*)cls, name); |
b3962a83 A |
491 | } |
492 | } | |
493 | ||
494 | ||
495 | /*********************************************************************** | |
496 | * _objc_allocateFutureClass | |
497 | * Allocate an unresolved future class for the given class name. | |
498 | * Returns any existing allocation if one was already made. | |
499 | * Assumes the named class doesn't exist yet. | |
500 | * Not thread safe. | |
501 | **********************************************************************/ | |
cd5f04f5 | 502 | Class _objc_allocateFutureClass(const char *name) |
b3962a83 | 503 | { |
7257e56c | 504 | Class cls; |
b3962a83 A |
505 | |
506 | if (future_class_hash) { | |
7257e56c | 507 | objc_class query; |
b3962a83 | 508 | query.name = name; |
7257e56c | 509 | if ((cls = (Class)NXHashGet(future_class_hash, &query))) { |
b3962a83 | 510 | // Already have a future class for this name. |
7257e56c | 511 | return cls; |
b3962a83 A |
512 | } |
513 | } | |
514 | ||
7257e56c | 515 | cls = _calloc_class(sizeof(objc_class)); |
b3962a83 | 516 | makeFutureClass(cls, name); |
7257e56c | 517 | return cls; |
b3962a83 A |
518 | } |
519 | ||
520 | ||
8070259c A |
521 | /*********************************************************************** |
522 | * objc_getFutureClass. Return the id of the named class. | |
523 | * If the class does not exist, return an uninitialized class | |
524 | * structure that will be used for the class when and if it | |
525 | * does get loaded. | |
526 | * Not thread safe. | |
527 | **********************************************************************/ | |
528 | Class objc_getFutureClass(const char *name) | |
529 | { | |
530 | Class cls; | |
531 | ||
532 | // YES unconnected, NO class handler | |
533 | // (unconnected is OK because it will someday be the real class) | |
534 | cls = look_up_class(name, YES, NO); | |
535 | if (cls) { | |
536 | if (PrintFuture) { | |
537 | _objc_inform("FUTURE: found %p already in use for %s", | |
538 | (void*)cls, name); | |
539 | } | |
540 | return cls; | |
541 | } | |
542 | ||
543 | // No class or future class with that name yet. Make one. | |
544 | // fixme not thread-safe with respect to | |
545 | // simultaneous library load or getFutureClass. | |
546 | return _objc_allocateFutureClass(name); | |
547 | } | |
548 | ||
549 | ||
31875a97 | 550 | BOOL _class_isFutureClass(Class cls) |
b3962a83 | 551 | { |
31875a97 | 552 | return cls && cls->isFuture(); |
b3962a83 A |
553 | } |
554 | ||
31875a97 | 555 | bool objc_class::isFuture() |
7257e56c | 556 | { |
31875a97 | 557 | return future_class_hash && NXHashGet(future_class_hash, this); |
7257e56c A |
558 | } |
559 | ||
560 | ||
b3962a83 A |
561 | /*********************************************************************** |
562 | * _objc_defaultClassHandler. Default objc_classHandler. Does nothing. | |
563 | **********************************************************************/ | |
564 | static int _objc_defaultClassHandler(const char *clsName) | |
565 | { | |
566 | // Return zero so objc_getClass doesn't bother re-searching | |
567 | return 0; | |
568 | } | |
569 | ||
570 | /*********************************************************************** | |
571 | * objc_setClassHandler. Set objc_classHandler to the specified value. | |
572 | * | |
7257e56c | 573 | * NOTE: This should probably deal with userSuppliedHandler being nil, |
b3962a83 | 574 | * because the objc_classHandler caller does not check... it would bus |
7257e56c | 575 | * error. It would make sense to handle nil by restoring the default |
b3962a83 A |
576 | * handler. Is anyone hacking with this, though? |
577 | **********************************************************************/ | |
578 | void objc_setClassHandler(int (*userSuppliedHandler)(const char *)) | |
579 | { | |
580 | OBJC_WARN_DEPRECATED; | |
581 | ||
582 | objc_classHandler = userSuppliedHandler; | |
583 | } | |
584 | ||
585 | ||
586 | /*********************************************************************** | |
587 | * _objc_setClassLoader | |
588 | * Similar to objc_setClassHandler, but objc_classLoader is used for | |
589 | * both objc_getClass() and objc_lookupClass(), and objc_classLoader | |
590 | * pre-empts objc_classHandler. | |
591 | **********************************************************************/ | |
592 | void _objc_setClassLoader(BOOL (*newClassLoader)(const char *)) | |
593 | { | |
594 | _objc_classLoader = newClassLoader; | |
595 | } | |
596 | ||
597 | ||
598 | /*********************************************************************** | |
599 | * objc_getProtocol | |
7257e56c | 600 | * Get a protocol by name, or nil. |
b3962a83 A |
601 | **********************************************************************/ |
602 | Protocol *objc_getProtocol(const char *name) | |
603 | { | |
31875a97 | 604 | mutex_locker_t lock(classLock); |
7257e56c | 605 | if (!protocol_map) return nil; |
31875a97 | 606 | return (Protocol *)NXMapGet(protocol_map, name); |
b3962a83 A |
607 | } |
608 | ||
609 | ||
610 | /*********************************************************************** | |
611 | * look_up_class | |
612 | * Map a class name to a class using various methods. | |
613 | * This is the common implementation of objc_lookUpClass and objc_getClass, | |
614 | * and is also used internally to get additional search options. | |
615 | * Sequence: | |
616 | * 1. class_hash | |
617 | * 2. unconnected_class_hash (optional) | |
618 | * 3. classLoader callback | |
619 | * 4. classHandler callback (optional) | |
620 | **********************************************************************/ | |
31875a97 A |
621 | Class look_up_class(const char *aClassName, bool includeUnconnected, |
622 | bool includeClassHandler) | |
b3962a83 | 623 | { |
31875a97 | 624 | bool includeClassLoader = YES; // class loader cannot be skipped |
7257e56c A |
625 | Class result = nil; |
626 | struct objc_class query; | |
b3962a83 A |
627 | |
628 | query.name = aClassName; | |
629 | ||
630 | retry: | |
631 | ||
632 | if (!result && class_hash) { | |
633 | // Check ordinary classes | |
31875a97 | 634 | mutex_locker_t lock(classLock); |
7257e56c | 635 | result = (Class)NXHashGet(class_hash, &query); |
b3962a83 A |
636 | } |
637 | ||
638 | if (!result && includeUnconnected && unconnected_class_hash) { | |
639 | // Check not-yet-connected classes | |
31875a97 | 640 | mutex_locker_t lock(classLock); |
7257e56c | 641 | result = (Class)NXHashGet(unconnected_class_hash, &query); |
b3962a83 A |
642 | } |
643 | ||
644 | if (!result && includeClassLoader && _objc_classLoader) { | |
645 | // Try class loader callback | |
646 | if ((*_objc_classLoader)(aClassName)) { | |
647 | // Re-try lookup without class loader | |
648 | includeClassLoader = NO; | |
649 | goto retry; | |
650 | } | |
651 | } | |
652 | ||
653 | if (!result && includeClassHandler && objc_classHandler) { | |
654 | // Try class handler callback | |
655 | if ((*objc_classHandler)(aClassName)) { | |
656 | // Re-try lookup without class handler or class loader | |
657 | includeClassLoader = NO; | |
658 | includeClassHandler = NO; | |
659 | goto retry; | |
660 | } | |
661 | } | |
662 | ||
663 | return result; | |
664 | } | |
665 | ||
666 | ||
667 | /*********************************************************************** | |
7257e56c | 668 | * objc_class::isConnected |
b3962a83 | 669 | * Returns TRUE if class cls is connected. |
7257e56c | 670 | * A connected class has either a connected superclass or a nil superclass, |
b3962a83 A |
671 | * and is present in class_hash. |
672 | **********************************************************************/ | |
7257e56c | 673 | bool objc_class::isConnected() |
b3962a83 | 674 | { |
31875a97 A |
675 | mutex_locker_t lock(classLock); |
676 | return NXHashMember(class_hash, this); | |
b3962a83 A |
677 | } |
678 | ||
679 | ||
b3962a83 A |
680 | /*********************************************************************** |
681 | * pendingClassRefsMapTable. Return a pointer to the lookup table for | |
682 | * pending class refs. | |
683 | **********************************************************************/ | |
684 | static inline NXMapTable *pendingClassRefsMapTable(void) | |
685 | { | |
686 | // Allocate table if needed | |
687 | if (!pendingClassRefsMap) { | |
31875a97 | 688 | pendingClassRefsMap = NXCreateMapTable(NXStrValueMapPrototype, 10); |
b3962a83 A |
689 | } |
690 | ||
691 | // Return table pointer | |
692 | return pendingClassRefsMap; | |
693 | } | |
694 | ||
695 | ||
696 | /*********************************************************************** | |
697 | * pendingSubclassesMapTable. Return a pointer to the lookup table for | |
698 | * pending subclasses. | |
699 | **********************************************************************/ | |
700 | static inline NXMapTable *pendingSubclassesMapTable(void) | |
701 | { | |
702 | // Allocate table if needed | |
703 | if (!pendingSubclassesMap) { | |
31875a97 | 704 | pendingSubclassesMap = NXCreateMapTable(NXStrValueMapPrototype, 10); |
b3962a83 A |
705 | } |
706 | ||
707 | // Return table pointer | |
708 | return pendingSubclassesMap; | |
709 | } | |
710 | ||
711 | ||
712 | /*********************************************************************** | |
713 | * pendClassInstallation | |
714 | * Finish connecting class cls when its superclass becomes connected. | |
715 | * Check for multiple pends of the same class because connect_class does not. | |
716 | **********************************************************************/ | |
7257e56c | 717 | static void pendClassInstallation(Class cls, const char *superName) |
b3962a83 A |
718 | { |
719 | NXMapTable *table; | |
720 | PendingSubclass *pending; | |
721 | PendingSubclass *oldList; | |
722 | PendingSubclass *l; | |
723 | ||
724 | // Create and/or locate pending class lookup table | |
725 | table = pendingSubclassesMapTable (); | |
726 | ||
727 | // Make sure this class isn't already in the pending list. | |
7257e56c A |
728 | oldList = (PendingSubclass *)NXMapGet(table, superName); |
729 | for (l = oldList; l != nil; l = l->next) { | |
b3962a83 A |
730 | if (l->subclass == cls) return; // already here, nothing to do |
731 | } | |
732 | ||
733 | // Create entry referring to this class | |
31875a97 | 734 | pending = (PendingSubclass *)malloc(sizeof(PendingSubclass)); |
b3962a83 A |
735 | pending->subclass = cls; |
736 | ||
737 | // Link new entry into head of list of entries for this class | |
738 | pending->next = oldList; | |
739 | ||
740 | // (Re)place entry list in the table | |
7af964d1 | 741 | NXMapKeyCopyingInsert (table, superName, pending); |
b3962a83 A |
742 | } |
743 | ||
744 | ||
745 | /*********************************************************************** | |
746 | * pendClassReference | |
747 | * Fix up a class ref when the class with the given name becomes connected. | |
748 | **********************************************************************/ | |
31875a97 | 749 | static void pendClassReference(Class *ref, const char *className, bool isMeta) |
b3962a83 A |
750 | { |
751 | NXMapTable *table; | |
752 | PendingClassRef *pending; | |
753 | ||
754 | // Create and/or locate pending class lookup table | |
755 | table = pendingClassRefsMapTable (); | |
756 | ||
757 | // Create entry containing the class reference | |
31875a97 | 758 | pending = (PendingClassRef *)malloc(sizeof(PendingClassRef)); |
b3962a83 A |
759 | pending->ref = ref; |
760 | if (isMeta) { | |
7257e56c | 761 | pending->ref = (Class *)((uintptr_t)pending->ref | 1); |
b3962a83 A |
762 | } |
763 | ||
764 | // Link new entry into head of list of entries for this class | |
7257e56c | 765 | pending->next = (PendingClassRef *)NXMapGet(table, className); |
b3962a83 A |
766 | |
767 | // (Re)place entry list in the table | |
7af964d1 | 768 | NXMapKeyCopyingInsert (table, className, pending); |
b3962a83 A |
769 | |
770 | if (PrintConnecting) { | |
771 | _objc_inform("CONNECT: pended reference to class '%s%s' at %p", | |
772 | className, isMeta ? " (meta)" : "", (void *)ref); | |
773 | } | |
774 | } | |
775 | ||
776 | ||
777 | /*********************************************************************** | |
778 | * resolve_references_to_class | |
779 | * Fix up any pending class refs to this class. | |
780 | **********************************************************************/ | |
7257e56c | 781 | static void resolve_references_to_class(Class cls) |
b3962a83 A |
782 | { |
783 | PendingClassRef *pending; | |
784 | ||
785 | if (!pendingClassRefsMap) return; // no unresolved refs for any class | |
786 | ||
7257e56c | 787 | pending = (PendingClassRef *)NXMapGet(pendingClassRefsMap, cls->name); |
b3962a83 A |
788 | if (!pending) return; // no unresolved refs for this class |
789 | ||
790 | NXMapKeyFreeingRemove(pendingClassRefsMap, cls->name); | |
791 | ||
792 | if (PrintConnecting) { | |
793 | _objc_inform("CONNECT: resolving references to class '%s'", cls->name); | |
794 | } | |
795 | ||
796 | while (pending) { | |
797 | PendingClassRef *next = pending->next; | |
798 | if (pending->ref) { | |
31875a97 | 799 | bool isMeta = (uintptr_t)pending->ref & 1; |
7257e56c A |
800 | Class *ref = |
801 | (Class *)((uintptr_t)pending->ref & ~(uintptr_t)1); | |
802 | *ref = isMeta ? cls->ISA() : cls; | |
b3962a83 | 803 | } |
31875a97 | 804 | free(pending); |
b3962a83 A |
805 | pending = next; |
806 | } | |
807 | ||
808 | if (NXCountMapTable(pendingClassRefsMap) == 0) { | |
809 | NXFreeMapTable(pendingClassRefsMap); | |
7257e56c | 810 | pendingClassRefsMap = nil; |
b3962a83 A |
811 | } |
812 | } | |
813 | ||
814 | ||
815 | /*********************************************************************** | |
816 | * resolve_subclasses_of_class | |
817 | * Fix up any pending subclasses of this class. | |
818 | **********************************************************************/ | |
7257e56c | 819 | static void resolve_subclasses_of_class(Class cls) |
b3962a83 A |
820 | { |
821 | PendingSubclass *pending; | |
822 | ||
823 | if (!pendingSubclassesMap) return; // no unresolved subclasses | |
824 | ||
7257e56c | 825 | pending = (PendingSubclass *)NXMapGet(pendingSubclassesMap, cls->name); |
b3962a83 A |
826 | if (!pending) return; // no unresolved subclasses for this class |
827 | ||
828 | NXMapKeyFreeingRemove(pendingSubclassesMap, cls->name); | |
829 | ||
830 | // Destroy the pending table if it's now empty, to save memory. | |
831 | if (NXCountMapTable(pendingSubclassesMap) == 0) { | |
832 | NXFreeMapTable(pendingSubclassesMap); | |
7257e56c | 833 | pendingSubclassesMap = nil; |
b3962a83 A |
834 | } |
835 | ||
836 | if (PrintConnecting) { | |
837 | _objc_inform("CONNECT: resolving subclasses of class '%s'", cls->name); | |
838 | } | |
839 | ||
840 | while (pending) { | |
841 | PendingSubclass *next = pending->next; | |
842 | if (pending->subclass) connect_class(pending->subclass); | |
31875a97 | 843 | free(pending); |
b3962a83 A |
844 | pending = next; |
845 | } | |
846 | } | |
847 | ||
848 | ||
849 | /*********************************************************************** | |
850 | * really_connect_class | |
851 | * Connect cls to superclass supercls unconditionally. | |
852 | * Also adjust the class hash tables and handle pended subclasses. | |
853 | * | |
854 | * This should be called from connect_class() ONLY. | |
855 | **********************************************************************/ | |
7257e56c A |
856 | static void really_connect_class(Class cls, |
857 | Class supercls) | |
b3962a83 | 858 | { |
7257e56c | 859 | Class oldCls; |
b3962a83 A |
860 | |
861 | // Connect superclass pointers. | |
8972963c | 862 | set_superclass(cls, supercls, YES); |
b3962a83 | 863 | |
7c0e6487 | 864 | // Done! |
b3962a83 A |
865 | cls->info |= CLS_CONNECTED; |
866 | ||
31875a97 A |
867 | { |
868 | mutex_locker_t lock(classLock); | |
869 | ||
870 | // Update hash tables. | |
871 | NXHashRemove(unconnected_class_hash, cls); | |
872 | oldCls = (Class)NXHashInsert(class_hash, cls); | |
31875a97 A |
873 | |
874 | // Delete unconnected_class_hash if it is now empty. | |
875 | if (NXCountHashTable(unconnected_class_hash) == 0) { | |
876 | NXFreeHashTable(unconnected_class_hash); | |
877 | unconnected_class_hash = nil; | |
878 | } | |
879 | ||
880 | // No duplicate classes allowed. | |
881 | // Duplicates should have been rejected by _objc_read_classes_from_image | |
1807f628 | 882 | ASSERT(!oldCls); |
31875a97 | 883 | } |
b3962a83 | 884 | |
ee974f79 A |
885 | // Fix up pended class refs to this class, if any |
886 | resolve_references_to_class(cls); | |
887 | ||
b3962a83 A |
888 | // Connect newly-connectable subclasses |
889 | resolve_subclasses_of_class(cls); | |
890 | ||
b3962a83 A |
891 | // Debugging: if this class has ivars, make sure this class's ivars don't |
892 | // overlap with its super's. This catches some broken fragile base classes. | |
893 | // Do not use super->instance_size vs. self->ivar[0] to check this. | |
894 | // Ivars may be packed across instance_size boundaries. | |
895 | if (DebugFragileSuperclasses && cls->ivars && cls->ivars->ivar_count) { | |
7257e56c | 896 | Class ivar_cls = supercls; |
b3962a83 A |
897 | |
898 | // Find closest superclass that has some ivars, if one exists. | |
899 | while (ivar_cls && | |
900 | (!ivar_cls->ivars || ivar_cls->ivars->ivar_count == 0)) | |
901 | { | |
7257e56c | 902 | ivar_cls = ivar_cls->superclass; |
b3962a83 A |
903 | } |
904 | ||
905 | if (ivar_cls) { | |
906 | // Compare superclass's last ivar to this class's first ivar | |
7257e56c | 907 | old_ivar *super_ivar = |
b3962a83 | 908 | &ivar_cls->ivars->ivar_list[ivar_cls->ivars->ivar_count - 1]; |
7257e56c | 909 | old_ivar *self_ivar = |
b3962a83 A |
910 | &cls->ivars->ivar_list[0]; |
911 | ||
912 | // fixme could be smarter about super's ivar size | |
913 | if (self_ivar->ivar_offset <= super_ivar->ivar_offset) { | |
914 | _objc_inform("WARNING: ivars of superclass '%s' and " | |
915 | "subclass '%s' overlap; superclass may have " | |
916 | "changed since subclass was compiled", | |
917 | ivar_cls->name, cls->name); | |
918 | } | |
919 | } | |
920 | } | |
921 | } | |
922 | ||
923 | ||
924 | /*********************************************************************** | |
925 | * connect_class | |
926 | * Connect class cls to its superclasses, if possible. | |
927 | * If cls becomes connected, move it from unconnected_class_hash | |
928 | * to connected_class_hash. | |
929 | * Returns TRUE if cls is connected. | |
930 | * Returns FALSE if cls could not be connected for some reason | |
931 | * (missing superclass or still-unconnected superclass) | |
932 | **********************************************************************/ | |
31875a97 | 933 | static bool connect_class(Class cls) |
b3962a83 | 934 | { |
7257e56c | 935 | if (cls->isConnected()) { |
b3962a83 A |
936 | // This class is already connected to its superclass. |
937 | // Do nothing. | |
938 | return TRUE; | |
939 | } | |
7257e56c | 940 | else if (cls->superclass == nil) { |
b3962a83 A |
941 | // This class is a root class. |
942 | // Connect it to itself. | |
943 | ||
944 | if (PrintConnecting) { | |
945 | _objc_inform("CONNECT: class '%s' now connected (root class)", | |
946 | cls->name); | |
947 | } | |
948 | ||
7257e56c | 949 | really_connect_class(cls, nil); |
b3962a83 A |
950 | return TRUE; |
951 | } | |
952 | else { | |
953 | // This class is not a root class and is not yet connected. | |
954 | // Connect it if its superclass and root class are already connected. | |
955 | // Otherwise, add this class to the to-be-connected list, | |
956 | // pending the completion of its superclass and root class. | |
957 | ||
7257e56c A |
958 | // At this point, cls->superclass and cls->ISA()->ISA() are still STRINGS |
959 | char *supercls_name = (char *)cls->superclass; | |
960 | Class supercls; | |
b3962a83 A |
961 | |
962 | // YES unconnected, YES class handler | |
7257e56c | 963 | if (nil == (supercls = look_up_class(supercls_name, YES, YES))) { |
b3962a83 A |
964 | // Superclass does not exist yet. |
965 | // pendClassInstallation will handle duplicate pends of this class | |
966 | pendClassInstallation(cls, supercls_name); | |
967 | ||
968 | if (PrintConnecting) { | |
969 | _objc_inform("CONNECT: class '%s' NOT connected (missing super)", cls->name); | |
970 | } | |
971 | return FALSE; | |
972 | } | |
973 | ||
974 | if (! connect_class(supercls)) { | |
975 | // Superclass exists but is not yet connected. | |
976 | // pendClassInstallation will handle duplicate pends of this class | |
977 | pendClassInstallation(cls, supercls_name); | |
978 | ||
979 | if (PrintConnecting) { | |
980 | _objc_inform("CONNECT: class '%s' NOT connected (unconnected super)", cls->name); | |
981 | } | |
982 | return FALSE; | |
983 | } | |
984 | ||
985 | // Superclass exists and is connected. | |
986 | // Connect this class to the superclass. | |
987 | ||
988 | if (PrintConnecting) { | |
989 | _objc_inform("CONNECT: class '%s' now connected", cls->name); | |
990 | } | |
991 | ||
992 | really_connect_class(cls, supercls); | |
993 | return TRUE; | |
994 | } | |
995 | } | |
996 | ||
997 | ||
998 | /*********************************************************************** | |
999 | * _objc_read_categories_from_image. | |
1000 | * Read all categories from the given image. | |
1001 | * Install them on their parent classes, or register them for later | |
1002 | * installation. | |
1003 | * Returns YES if some method caches now need to be flushed. | |
1004 | **********************************************************************/ | |
31875a97 | 1005 | static bool _objc_read_categories_from_image (header_info * hi) |
b3962a83 A |
1006 | { |
1007 | Module mods; | |
1008 | size_t midx; | |
31875a97 | 1009 | bool needFlush = NO; |
b3962a83 | 1010 | |
c1e772c4 | 1011 | if (hi->info()->isReplacement()) { |
b3962a83 A |
1012 | // Ignore any categories in this image |
1013 | return NO; | |
1014 | } | |
1015 | ||
b3962a83 A |
1016 | // Major loop - process all modules in the header |
1017 | mods = hi->mod_ptr; | |
1018 | ||
1019 | // NOTE: The module and category lists are traversed backwards | |
1020 | // to preserve the pre-10.4 processing order. Changing the order | |
1021 | // would have a small chance of introducing binary compatibility bugs. | |
1022 | midx = hi->mod_count; | |
1023 | while (midx-- > 0) { | |
1024 | unsigned int index; | |
1025 | unsigned int total; | |
1026 | ||
1027 | // Nothing to do for a module without a symbol table | |
7257e56c | 1028 | if (mods[midx].symtab == nil) |
b3962a83 A |
1029 | continue; |
1030 | ||
1031 | // Total entries in symbol table (class entries followed | |
1032 | // by category entries) | |
1033 | total = mods[midx].symtab->cls_def_cnt + | |
1034 | mods[midx].symtab->cat_def_cnt; | |
1035 | ||
1036 | // Minor loop - register all categories from given module | |
1037 | index = total; | |
1038 | while (index-- > mods[midx].symtab->cls_def_cnt) { | |
7257e56c | 1039 | old_category *cat = (old_category *)mods[midx].symtab->defs[index]; |
b3962a83 A |
1040 | needFlush |= _objc_register_category(cat, (int)mods[midx].version); |
1041 | } | |
1042 | } | |
1043 | ||
1044 | return needFlush; | |
1045 | } | |
1046 | ||
1047 | ||
1048 | /*********************************************************************** | |
1049 | * _objc_read_classes_from_image. | |
1050 | * Read classes from the given image, perform assorted minor fixups, | |
1051 | * scan for +load implementation. | |
1052 | * Does not connect classes to superclasses. | |
1053 | * Does attach pended categories to the classes. | |
1054 | * Adds all classes to unconnected_class_hash. class_hash is unchanged. | |
1055 | **********************************************************************/ | |
1056 | static void _objc_read_classes_from_image(header_info *hi) | |
1057 | { | |
1058 | unsigned int index; | |
1059 | unsigned int midx; | |
1060 | Module mods; | |
7af964d1 | 1061 | int isBundle = headerIsBundle(hi); |
b3962a83 | 1062 | |
c1e772c4 | 1063 | if (hi->info()->isReplacement()) { |
b3962a83 A |
1064 | // Ignore any classes in this image |
1065 | return; | |
1066 | } | |
1067 | ||
1068 | // class_hash starts small, enough only for libobjc itself. | |
1069 | // If other Objective-C libraries are found, immediately resize | |
1070 | // class_hash, assuming that Foundation and AppKit are about | |
1071 | // to add lots of classes. | |
31875a97 A |
1072 | { |
1073 | mutex_locker_t lock(classLock); | |
c1e772c4 | 1074 | if (hi->mhdr() != libobjc_header && _NXHashCapacity(class_hash) < 1024) { |
31875a97 A |
1075 | _NXHashRehashToCapacity(class_hash, 1024); |
1076 | } | |
b3962a83 | 1077 | } |
b3962a83 A |
1078 | |
1079 | // Major loop - process all modules in the image | |
1080 | mods = hi->mod_ptr; | |
1081 | for (midx = 0; midx < hi->mod_count; midx += 1) | |
1082 | { | |
1083 | // Skip module containing no classes | |
7257e56c | 1084 | if (mods[midx].symtab == nil) |
b3962a83 A |
1085 | continue; |
1086 | ||
1087 | // Minor loop - process all the classes in given module | |
1088 | for (index = 0; index < mods[midx].symtab->cls_def_cnt; index += 1) | |
1089 | { | |
7257e56c | 1090 | Class newCls, oldCls; |
31875a97 | 1091 | bool rejected; |
b3962a83 A |
1092 | |
1093 | // Locate the class description pointer | |
7257e56c | 1094 | newCls = (Class)mods[midx].symtab->defs[index]; |
b3962a83 A |
1095 | |
1096 | // Classes loaded from Mach-O bundles can be unloaded later. | |
7257e56c | 1097 | // Nothing uses this class yet, so cls->setInfo is not needed. |
b3962a83 | 1098 | if (isBundle) newCls->info |= CLS_FROM_BUNDLE; |
7257e56c | 1099 | if (isBundle) newCls->ISA()->info |= CLS_FROM_BUNDLE; |
b3962a83 | 1100 | |
7257e56c A |
1101 | // Use common static empty cache instead of nil |
1102 | if (newCls->cache == nil) | |
b3962a83 | 1103 | newCls->cache = (Cache) &_objc_empty_cache; |
7257e56c A |
1104 | if (newCls->ISA()->cache == nil) |
1105 | newCls->ISA()->cache = (Cache) &_objc_empty_cache; | |
b3962a83 A |
1106 | |
1107 | // Set metaclass version | |
7257e56c | 1108 | newCls->ISA()->version = mods[midx].version; |
b3962a83 | 1109 | |
7257e56c | 1110 | // methodLists is nil or a single list, not an array |
b3962a83 | 1111 | newCls->info |= CLS_NO_METHOD_ARRAY|CLS_NO_PROPERTY_ARRAY; |
7257e56c | 1112 | newCls->ISA()->info |= CLS_NO_METHOD_ARRAY|CLS_NO_PROPERTY_ARRAY; |
b3962a83 A |
1113 | |
1114 | // class has no subclasses for cache flushing | |
1115 | newCls->info |= CLS_LEAF; | |
7257e56c | 1116 | newCls->ISA()->info |= CLS_LEAF; |
b3962a83 A |
1117 | |
1118 | if (mods[midx].version >= 6) { | |
1119 | // class structure has ivar_layout and ext fields | |
1120 | newCls->info |= CLS_EXT; | |
7257e56c | 1121 | newCls->ISA()->info |= CLS_EXT; |
b3962a83 A |
1122 | } |
1123 | ||
1124 | // Check for +load implementation before categories are attached | |
7257e56c A |
1125 | if (_class_hasLoadMethod(newCls)) { |
1126 | newCls->ISA()->info |= CLS_HAS_LOAD_METHOD; | |
b3962a83 | 1127 | } |
b3962a83 | 1128 | |
31875a97 A |
1129 | // Install into unconnected_class_hash. |
1130 | { | |
1131 | mutex_locker_t lock(classLock); | |
1132 | ||
1133 | if (future_class_hash) { | |
1134 | Class futureCls = (Class) | |
1135 | NXHashRemove(future_class_hash, newCls); | |
1136 | if (futureCls) { | |
1137 | // Another class structure for this class was already | |
1138 | // prepared by objc_getFutureClass(). Use it instead. | |
1139 | free((char *)futureCls->name); | |
1140 | memcpy(futureCls, newCls, sizeof(objc_class)); | |
1141 | setOriginalClassForFutureClass(futureCls, newCls); | |
1142 | newCls = futureCls; | |
1143 | ||
1144 | if (NXCountHashTable(future_class_hash) == 0) { | |
1145 | NXFreeHashTable(future_class_hash); | |
1146 | future_class_hash = nil; | |
1147 | } | |
b3962a83 A |
1148 | } |
1149 | } | |
31875a97 A |
1150 | |
1151 | if (!unconnected_class_hash) { | |
1152 | unconnected_class_hash = | |
1153 | NXCreateHashTable(classHashPrototype, 128, nil); | |
1154 | } | |
1155 | ||
1156 | if ((oldCls = (Class)NXHashGet(class_hash, newCls)) || | |
1157 | (oldCls = (Class)NXHashGet(unconnected_class_hash, newCls))) | |
1158 | { | |
1159 | // Another class with this name exists. Complain and reject. | |
1160 | inform_duplicate(newCls->name, oldCls, newCls); | |
1161 | rejected = YES; | |
1162 | } | |
1163 | else { | |
1164 | NXHashInsert(unconnected_class_hash, newCls); | |
1165 | rejected = NO; | |
1166 | } | |
b3962a83 A |
1167 | } |
1168 | ||
7af964d1 | 1169 | if (!rejected) { |
7af964d1 A |
1170 | // Attach pended categories for this class, if any |
1171 | resolve_categories_for_class(newCls); | |
1172 | } | |
b3962a83 A |
1173 | } |
1174 | } | |
1175 | } | |
1176 | ||
1177 | ||
1178 | /*********************************************************************** | |
1179 | * _objc_connect_classes_from_image. | |
1180 | * Connect the classes in the given image to their superclasses, | |
1181 | * or register them for later connection if any superclasses are missing. | |
1182 | **********************************************************************/ | |
1183 | static void _objc_connect_classes_from_image(header_info *hi) | |
1184 | { | |
1185 | unsigned int index; | |
1186 | unsigned int midx; | |
1187 | Module mods; | |
c1e772c4 | 1188 | bool replacement = hi->info()->isReplacement(); |
b3962a83 A |
1189 | |
1190 | // Major loop - process all modules in the image | |
1191 | mods = hi->mod_ptr; | |
1192 | for (midx = 0; midx < hi->mod_count; midx += 1) | |
1193 | { | |
1194 | // Skip module containing no classes | |
7257e56c | 1195 | if (mods[midx].symtab == nil) |
b3962a83 A |
1196 | continue; |
1197 | ||
1198 | // Minor loop - process all the classes in given module | |
1199 | for (index = 0; index < mods[midx].symtab->cls_def_cnt; index += 1) | |
1200 | { | |
7257e56c | 1201 | Class cls = (Class)mods[midx].symtab->defs[index]; |
b3962a83 | 1202 | if (! replacement) { |
31875a97 | 1203 | bool connected; |
7257e56c | 1204 | Class futureCls = getFutureClassForOriginalClass(cls); |
b3962a83 A |
1205 | if (futureCls) { |
1206 | // objc_getFutureClass() requested a different class | |
7257e56c | 1207 | // struct. Fix up the original struct's superclass |
b3962a83 A |
1208 | // field for [super ...] use, but otherwise perform |
1209 | // fixups on the new class struct only. | |
7257e56c A |
1210 | const char *super_name = (const char *) cls->superclass; |
1211 | if (super_name) cls->superclass = objc_getClass(super_name); | |
b3962a83 A |
1212 | cls = futureCls; |
1213 | } | |
1214 | connected = connect_class(cls); | |
1215 | if (connected && callbackFunction) { | |
7257e56c | 1216 | (*callbackFunction)(cls, 0); |
b3962a83 A |
1217 | } |
1218 | } else { | |
7257e56c A |
1219 | // Replacement image - fix up superclass only (#3704817) |
1220 | // And metaclass's superclass (#5351107) | |
1221 | const char *super_name = (const char *) cls->superclass; | |
b3962a83 | 1222 | if (super_name) { |
7257e56c | 1223 | cls->superclass = objc_getClass(super_name); |
b3962a83 | 1224 | // metaclass's superclass is superclass's metaclass |
7257e56c | 1225 | cls->ISA()->superclass = cls->superclass->ISA(); |
b3962a83 A |
1226 | } else { |
1227 | // Replacement for a root class | |
7257e56c | 1228 | // cls->superclass already nil |
b3962a83 | 1229 | // root metaclass's superclass is root class |
7257e56c | 1230 | cls->ISA()->superclass = cls; |
b3962a83 A |
1231 | } |
1232 | } | |
1233 | } | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | ||
1238 | /*********************************************************************** | |
1239 | * _objc_map_class_refs_for_image. Convert the class ref entries from | |
1240 | * a class name string pointer to a class pointer. If the class does | |
1241 | * not yet exist, the reference is added to a list of pending references | |
1242 | * to be fixed up at a later date. | |
1243 | **********************************************************************/ | |
31875a97 | 1244 | static void fix_class_ref(Class *ref, const char *name, bool isMeta) |
b3962a83 | 1245 | { |
7257e56c | 1246 | Class cls; |
b3962a83 A |
1247 | |
1248 | // Get pointer to class of this name | |
ee974f79 A |
1249 | // NO unconnected, YES class loader |
1250 | // (real class with weak-missing superclass is unconnected now) | |
7257e56c | 1251 | cls = look_up_class(name, NO, YES); |
b3962a83 A |
1252 | if (cls) { |
1253 | // Referenced class exists. Fix up the reference. | |
7257e56c | 1254 | *ref = isMeta ? cls->ISA() : cls; |
b3962a83 | 1255 | } else { |
7257e56c | 1256 | // Referenced class does not exist yet. Insert nil for now |
ee974f79 | 1257 | // (weak-linking) and fix up the reference if the class arrives later. |
b3962a83 | 1258 | pendClassReference (ref, name, isMeta); |
7257e56c | 1259 | *ref = nil; |
b3962a83 A |
1260 | } |
1261 | } | |
1262 | ||
1263 | static void _objc_map_class_refs_for_image (header_info * hi) | |
1264 | { | |
7257e56c | 1265 | Class *cls_refs; |
b3962a83 A |
1266 | size_t count; |
1267 | unsigned int index; | |
1268 | ||
1269 | // Locate class refs in image | |
1270 | cls_refs = _getObjcClassRefs (hi, &count); | |
1271 | if (cls_refs) { | |
1272 | // Process each class ref | |
1273 | for (index = 0; index < count; index += 1) { | |
1274 | // Ref is initially class name char* | |
1275 | const char *name = (const char *) cls_refs[index]; | |
7af964d1 | 1276 | if (!name) continue; |
b3962a83 A |
1277 | fix_class_ref(&cls_refs[index], name, NO /*never meta*/); |
1278 | } | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | ||
1283 | /*********************************************************************** | |
1284 | * _objc_remove_pending_class_refs_in_image | |
1285 | * Delete any pending class ref fixups for class refs in the given image, | |
1286 | * because the image is about to be unloaded. | |
1287 | **********************************************************************/ | |
7257e56c | 1288 | static void removePendingReferences(Class *refs, size_t count) |
b3962a83 | 1289 | { |
7257e56c | 1290 | Class *end = refs + count; |
b3962a83 A |
1291 | |
1292 | if (!refs) return; | |
1293 | if (!pendingClassRefsMap) return; | |
1294 | ||
1295 | // Search the pending class ref table for class refs in this range. | |
7257e56c | 1296 | // The class refs may have already been stomped with nil, |
b3962a83 | 1297 | // so there's no way to recover the original class name. |
7af964d1 A |
1298 | |
1299 | { | |
1300 | const char *key; | |
1301 | PendingClassRef *pending; | |
1302 | NXMapState state = NXInitMapState(pendingClassRefsMap); | |
1303 | while(NXNextMapState(pendingClassRefsMap, &state, | |
1304 | (const void **)&key, (const void **)&pending)) | |
1305 | { | |
7257e56c | 1306 | for ( ; pending != nil; pending = pending->next) { |
7af964d1 | 1307 | if (pending->ref >= refs && pending->ref < end) { |
7257e56c | 1308 | pending->ref = nil; |
7af964d1 | 1309 | } |
b3962a83 A |
1310 | } |
1311 | } | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | static void _objc_remove_pending_class_refs_in_image(header_info *hi) | |
1316 | { | |
7257e56c | 1317 | Class *cls_refs; |
b3962a83 A |
1318 | size_t count; |
1319 | ||
1320 | // Locate class refs in this image | |
1321 | cls_refs = _getObjcClassRefs(hi, &count); | |
1322 | removePendingReferences(cls_refs, count); | |
1323 | } | |
1324 | ||
1325 | ||
1326 | /*********************************************************************** | |
1327 | * map_selrefs. For each selector in the specified array, | |
1328 | * replace the name pointer with a uniqued selector. | |
1329 | * If copy is TRUE, all selector data is always copied. This is used | |
1330 | * for registering selectors from unloadable bundles, so the selector | |
1331 | * can still be used after the bundle's data segment is unmapped. | |
1332 | * Returns YES if dst was written to, NO if it was unchanged. | |
1333 | **********************************************************************/ | |
31875a97 | 1334 | static inline void map_selrefs(SEL *sels, size_t count, bool copy) |
b3962a83 | 1335 | { |
b3962a83 A |
1336 | size_t index; |
1337 | ||
7af964d1 | 1338 | if (!sels) return; |
b3962a83 | 1339 | |
66799735 | 1340 | mutex_locker_t lock(selLock); |
b3962a83 A |
1341 | |
1342 | // Process each selector | |
7af964d1 | 1343 | for (index = 0; index < count; index += 1) |
b3962a83 A |
1344 | { |
1345 | SEL sel; | |
1346 | ||
1347 | // Lookup pointer to uniqued string | |
7af964d1 | 1348 | sel = sel_registerNameNoLock((const char *) sels[index], copy); |
b3962a83 A |
1349 | |
1350 | // Replace this selector with uniqued one (avoid | |
1351 | // modifying the VM page if this would be a NOP) | |
7af964d1 A |
1352 | if (sels[index] != sel) { |
1353 | sels[index] = sel; | |
b3962a83 A |
1354 | } |
1355 | } | |
b3962a83 A |
1356 | } |
1357 | ||
1358 | ||
1359 | /*********************************************************************** | |
1360 | * map_method_descs. For each method in the specified method list, | |
1361 | * replace the name pointer with a uniqued selector. | |
1362 | * If copy is TRUE, all selector data is always copied. This is used | |
1363 | * for registering selectors from unloadable bundles, so the selector | |
1364 | * can still be used after the bundle's data segment is unmapped. | |
1365 | **********************************************************************/ | |
31875a97 | 1366 | static void map_method_descs (struct objc_method_description_list * methods, bool copy) |
b3962a83 | 1367 | { |
7af964d1 | 1368 | int index; |
b3962a83 A |
1369 | |
1370 | if (!methods) return; | |
1371 | ||
66799735 | 1372 | mutex_locker_t lock(selLock); |
b3962a83 A |
1373 | |
1374 | // Process each method | |
1375 | for (index = 0; index < methods->count; index += 1) | |
1376 | { | |
1377 | struct objc_method_description * method; | |
1378 | SEL sel; | |
1379 | ||
1380 | // Get method entry to fix up | |
1381 | method = &methods->list[index]; | |
1382 | ||
1383 | // Lookup pointer to uniqued string | |
1384 | sel = sel_registerNameNoLock((const char *) method->name, copy); | |
1385 | ||
1386 | // Replace this selector with uniqued one (avoid | |
1387 | // modifying the VM page if this would be a NOP) | |
1388 | if (method->name != sel) | |
1389 | method->name = sel; | |
1390 | } | |
b3962a83 A |
1391 | } |
1392 | ||
1393 | ||
1394 | /*********************************************************************** | |
1395 | * ext_for_protocol | |
1396 | * Returns the protocol extension for the given protocol. | |
7257e56c | 1397 | * Returns nil if the protocol has no extension. |
b3962a83 | 1398 | **********************************************************************/ |
7257e56c | 1399 | static old_protocol_ext *ext_for_protocol(old_protocol *proto) |
b3962a83 | 1400 | { |
7257e56c A |
1401 | if (!proto) return nil; |
1402 | if (!protocol_ext_map) return nil; | |
1403 | else return (old_protocol_ext *)NXMapGet(protocol_ext_map, proto); | |
b3962a83 A |
1404 | } |
1405 | ||
1406 | ||
1407 | /*********************************************************************** | |
1408 | * lookup_method | |
1409 | * Search a protocol method list for a selector. | |
1410 | **********************************************************************/ | |
1411 | static struct objc_method_description * | |
1412 | lookup_method(struct objc_method_description_list *mlist, SEL aSel) | |
1413 | { | |
1414 | if (mlist) { | |
1415 | int i; | |
1416 | for (i = 0; i < mlist->count; i++) { | |
1417 | if (mlist->list[i].name == aSel) { | |
1418 | return mlist->list+i; | |
1419 | } | |
1420 | } | |
1421 | } | |
7257e56c | 1422 | return nil; |
b3962a83 A |
1423 | } |
1424 | ||
1425 | ||
1426 | /*********************************************************************** | |
1427 | * lookup_protocol_method | |
cd5f04f5 A |
1428 | * Search for a selector in a protocol |
1429 | * (and optionally recursively all incorporated protocols) | |
b3962a83 | 1430 | **********************************************************************/ |
cd5f04f5 | 1431 | struct objc_method_description * |
7257e56c | 1432 | lookup_protocol_method(old_protocol *proto, SEL aSel, |
31875a97 A |
1433 | bool isRequiredMethod, bool isInstanceMethod, |
1434 | bool recursive) | |
b3962a83 | 1435 | { |
7257e56c A |
1436 | struct objc_method_description *m = nil; |
1437 | old_protocol_ext *ext; | |
b3962a83 A |
1438 | |
1439 | if (isRequiredMethod) { | |
1440 | if (isInstanceMethod) { | |
1441 | m = lookup_method(proto->instance_methods, aSel); | |
1442 | } else { | |
1443 | m = lookup_method(proto->class_methods, aSel); | |
1444 | } | |
1445 | } else if ((ext = ext_for_protocol(proto))) { | |
1446 | if (isInstanceMethod) { | |
1447 | m = lookup_method(ext->optional_instance_methods, aSel); | |
1448 | } else { | |
1449 | m = lookup_method(ext->optional_class_methods, aSel); | |
1450 | } | |
1451 | } | |
1452 | ||
cd5f04f5 | 1453 | if (!m && recursive && proto->protocol_list) { |
b3962a83 A |
1454 | int i; |
1455 | for (i = 0; !m && i < proto->protocol_list->count; i++) { | |
1456 | m = lookup_protocol_method(proto->protocol_list->list[i], aSel, | |
cd5f04f5 | 1457 | isRequiredMethod,isInstanceMethod,true); |
b3962a83 A |
1458 | } |
1459 | } | |
1460 | ||
1461 | return m; | |
1462 | } | |
1463 | ||
1464 | ||
1465 | /*********************************************************************** | |
1466 | * protocol_getName | |
1467 | * Returns the name of the given protocol. | |
1468 | **********************************************************************/ | |
1469 | const char *protocol_getName(Protocol *p) | |
1470 | { | |
7257e56c | 1471 | old_protocol *proto = oldprotocol(p); |
b3962a83 A |
1472 | if (!proto) return "nil"; |
1473 | return proto->protocol_name; | |
1474 | } | |
1475 | ||
1476 | ||
1477 | /*********************************************************************** | |
1478 | * protocol_getMethodDescription | |
1479 | * Returns the description of a named method. | |
1480 | * Searches either required or optional methods. | |
1481 | * Searches either instance or class methods. | |
1482 | **********************************************************************/ | |
1483 | struct objc_method_description | |
1484 | protocol_getMethodDescription(Protocol *p, SEL aSel, | |
1485 | BOOL isRequiredMethod, BOOL isInstanceMethod) | |
1486 | { | |
7257e56c A |
1487 | struct objc_method_description empty = {nil, nil}; |
1488 | old_protocol *proto = oldprotocol(p); | |
7af964d1 A |
1489 | struct objc_method_description *desc; |
1490 | if (!proto) return empty; | |
b3962a83 | 1491 | |
7af964d1 | 1492 | desc = lookup_protocol_method(proto, aSel, |
cd5f04f5 | 1493 | isRequiredMethod, isInstanceMethod, true); |
b3962a83 | 1494 | if (desc) return *desc; |
7af964d1 | 1495 | else return empty; |
b3962a83 A |
1496 | } |
1497 | ||
1498 | ||
1499 | /*********************************************************************** | |
1500 | * protocol_copyMethodDescriptionList | |
1501 | * Returns an array of method descriptions from a protocol. | |
1502 | * Copies either required or optional methods. | |
1503 | * Copies either instance or class methods. | |
1504 | **********************************************************************/ | |
1505 | struct objc_method_description * | |
1506 | protocol_copyMethodDescriptionList(Protocol *p, | |
1507 | BOOL isRequiredMethod, | |
1508 | BOOL isInstanceMethod, | |
1509 | unsigned int *outCount) | |
1510 | { | |
7257e56c A |
1511 | struct objc_method_description_list *mlist = nil; |
1512 | old_protocol *proto = oldprotocol(p); | |
1513 | old_protocol_ext *ext; | |
7af964d1 A |
1514 | unsigned int i, count; |
1515 | struct objc_method_description *result; | |
b3962a83 A |
1516 | |
1517 | if (!proto) { | |
1518 | if (outCount) *outCount = 0; | |
7257e56c | 1519 | return nil; |
b3962a83 A |
1520 | } |
1521 | ||
1522 | if (isRequiredMethod) { | |
1523 | if (isInstanceMethod) { | |
1524 | mlist = proto->instance_methods; | |
1525 | } else { | |
1526 | mlist = proto->class_methods; | |
1527 | } | |
1528 | } else if ((ext = ext_for_protocol(proto))) { | |
1529 | if (isInstanceMethod) { | |
1530 | mlist = ext->optional_instance_methods; | |
1531 | } else { | |
1532 | mlist = ext->optional_class_methods; | |
1533 | } | |
1534 | } | |
1535 | ||
1536 | if (!mlist) { | |
1537 | if (outCount) *outCount = 0; | |
7257e56c | 1538 | return nil; |
b3962a83 A |
1539 | } |
1540 | ||
7af964d1 | 1541 | count = mlist->count; |
7257e56c | 1542 | result = (struct objc_method_description *) |
b3962a83 A |
1543 | calloc(count + 1, sizeof(struct objc_method_description)); |
1544 | for (i = 0; i < count; i++) { | |
1545 | result[i] = mlist->list[i]; | |
1546 | } | |
1547 | ||
1548 | if (outCount) *outCount = count; | |
1549 | return result; | |
1550 | } | |
1551 | ||
1552 | ||
c1e772c4 A |
1553 | objc_property_t |
1554 | protocol_getProperty(Protocol *p, const char *name, | |
1555 | BOOL isRequiredProperty, BOOL isInstanceProperty) | |
b3962a83 | 1556 | { |
7257e56c A |
1557 | old_protocol *proto = oldprotocol(p); |
1558 | old_protocol_ext *ext; | |
1559 | old_protocol_list *proto_list; | |
b3962a83 | 1560 | |
7257e56c | 1561 | if (!proto || !name) return nil; |
b3962a83 | 1562 | |
c1e772c4 A |
1563 | if (!isRequiredProperty) { |
1564 | // Only required properties are currently supported | |
7257e56c | 1565 | return nil; |
b3962a83 A |
1566 | } |
1567 | ||
b3962a83 | 1568 | if ((ext = ext_for_protocol(proto))) { |
7257e56c | 1569 | old_property_list *plist; |
c1e772c4 A |
1570 | if (isInstanceProperty) plist = ext->instance_properties; |
1571 | else if (ext->hasClassPropertiesField()) plist = ext->class_properties; | |
1572 | else plist = nil; | |
1573 | ||
1574 | if (plist) { | |
b3962a83 A |
1575 | uint32_t i; |
1576 | for (i = 0; i < plist->count; i++) { | |
7257e56c | 1577 | old_property *prop = property_list_nth(plist, i); |
b3962a83 | 1578 | if (0 == strcmp(name, prop->name)) { |
8972963c | 1579 | return (objc_property_t)prop; |
b3962a83 A |
1580 | } |
1581 | } | |
1582 | } | |
1583 | } | |
1584 | ||
cd5f04f5 | 1585 | if ((proto_list = proto->protocol_list)) { |
b3962a83 | 1586 | int i; |
cd5f04f5 | 1587 | for (i = 0; i < proto_list->count; i++) { |
8972963c | 1588 | objc_property_t prop = |
cd5f04f5 | 1589 | protocol_getProperty((Protocol *)proto_list->list[i], name, |
b3962a83 A |
1590 | isRequiredProperty, isInstanceProperty); |
1591 | if (prop) return prop; | |
1592 | } | |
1593 | } | |
1594 | ||
7257e56c | 1595 | return nil; |
b3962a83 A |
1596 | } |
1597 | ||
1598 | ||
c1e772c4 A |
1599 | objc_property_t * |
1600 | protocol_copyPropertyList2(Protocol *p, unsigned int *outCount, | |
1601 | BOOL isRequiredProperty, BOOL isInstanceProperty) | |
b3962a83 | 1602 | { |
7257e56c A |
1603 | old_property **result = nil; |
1604 | old_protocol_ext *ext; | |
1605 | old_property_list *plist; | |
b3962a83 | 1606 | |
7257e56c | 1607 | old_protocol *proto = oldprotocol(p); |
c1e772c4 A |
1608 | if (! (ext = ext_for_protocol(proto)) || !isRequiredProperty) { |
1609 | // Only required properties are currently supported. | |
b3962a83 | 1610 | if (outCount) *outCount = 0; |
7257e56c | 1611 | return nil; |
b3962a83 A |
1612 | } |
1613 | ||
c1e772c4 A |
1614 | if (isInstanceProperty) plist = ext->instance_properties; |
1615 | else if (ext->hasClassPropertiesField()) plist = ext->class_properties; | |
1616 | else plist = nil; | |
1617 | ||
b3962a83 A |
1618 | result = copyPropertyList(plist, outCount); |
1619 | ||
8972963c | 1620 | return (objc_property_t *)result; |
b3962a83 A |
1621 | } |
1622 | ||
c1e772c4 A |
1623 | objc_property_t *protocol_copyPropertyList(Protocol *p, unsigned int *outCount) |
1624 | { | |
1625 | return protocol_copyPropertyList2(p, outCount, YES, YES); | |
1626 | } | |
1627 | ||
b3962a83 A |
1628 | |
1629 | /*********************************************************************** | |
1630 | * protocol_copyProtocolList | |
1631 | * Copies this protocol's incorporated protocols. | |
1632 | * Does not copy those protocol's incorporated protocols in turn. | |
1633 | **********************************************************************/ | |
8972963c A |
1634 | Protocol * __unsafe_unretained * |
1635 | protocol_copyProtocolList(Protocol *p, unsigned int *outCount) | |
b3962a83 A |
1636 | { |
1637 | unsigned int count = 0; | |
7257e56c A |
1638 | Protocol **result = nil; |
1639 | old_protocol *proto = oldprotocol(p); | |
b3962a83 A |
1640 | |
1641 | if (!proto) { | |
1642 | if (outCount) *outCount = 0; | |
7257e56c | 1643 | return nil; |
b3962a83 A |
1644 | } |
1645 | ||
1646 | if (proto->protocol_list) { | |
1647 | count = (unsigned int)proto->protocol_list->count; | |
1648 | } | |
1649 | if (count > 0) { | |
7af964d1 | 1650 | unsigned int i; |
7257e56c | 1651 | result = (Protocol **)malloc((count+1) * sizeof(Protocol *)); |
b3962a83 | 1652 | |
b3962a83 A |
1653 | for (i = 0; i < count; i++) { |
1654 | result[i] = (Protocol *)proto->protocol_list->list[i]; | |
1655 | } | |
7257e56c | 1656 | result[i] = nil; |
b3962a83 A |
1657 | } |
1658 | ||
1659 | if (outCount) *outCount = count; | |
1660 | return result; | |
1661 | } | |
1662 | ||
1663 | ||
1664 | BOOL protocol_conformsToProtocol(Protocol *self_gen, Protocol *other_gen) | |
1665 | { | |
7257e56c A |
1666 | old_protocol *self = oldprotocol(self_gen); |
1667 | old_protocol *other = oldprotocol(other_gen); | |
b3962a83 A |
1668 | |
1669 | if (!self || !other) { | |
1670 | return NO; | |
1671 | } | |
1672 | ||
1673 | if (0 == strcmp(self->protocol_name, other->protocol_name)) { | |
1674 | return YES; | |
1675 | } | |
1676 | ||
1677 | if (self->protocol_list) { | |
1678 | int i; | |
1679 | for (i = 0; i < self->protocol_list->count; i++) { | |
7257e56c | 1680 | old_protocol *proto = self->protocol_list->list[i]; |
b3962a83 A |
1681 | if (0 == strcmp(other->protocol_name, proto->protocol_name)) { |
1682 | return YES; | |
1683 | } | |
1684 | if (protocol_conformsToProtocol((Protocol *)proto, other_gen)) { | |
1685 | return YES; | |
1686 | } | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | return NO; | |
1691 | } | |
1692 | ||
1693 | ||
1694 | BOOL protocol_isEqual(Protocol *self, Protocol *other) | |
1695 | { | |
1696 | if (self == other) return YES; | |
1697 | if (!self || !other) return NO; | |
1698 | ||
1699 | if (!protocol_conformsToProtocol(self, other)) return NO; | |
1700 | if (!protocol_conformsToProtocol(other, self)) return NO; | |
1701 | ||
1702 | return YES; | |
1703 | } | |
1704 | ||
1705 | ||
cd5f04f5 A |
1706 | /*********************************************************************** |
1707 | * _protocol_getMethodTypeEncoding | |
1708 | * Return the @encode string for the requested protocol method. | |
7257e56c | 1709 | * Returns nil if the compiler did not emit any extended @encode data. |
cd5f04f5 A |
1710 | * Locking: runtimeLock must not be held by the caller |
1711 | **********************************************************************/ | |
1712 | const char * | |
1713 | _protocol_getMethodTypeEncoding(Protocol *proto_gen, SEL sel, | |
1714 | BOOL isRequiredMethod, BOOL isInstanceMethod) | |
1715 | { | |
7257e56c A |
1716 | old_protocol *proto = oldprotocol(proto_gen); |
1717 | if (!proto) return nil; | |
1718 | old_protocol_ext *ext = ext_for_protocol(proto); | |
1719 | if (!ext) return nil; | |
1720 | if (ext->size < offsetof(old_protocol_ext, extendedMethodTypes) + sizeof(ext->extendedMethodTypes)) return nil; | |
1721 | if (! ext->extendedMethodTypes) return nil; | |
cd5f04f5 A |
1722 | |
1723 | struct objc_method_description *m = | |
1724 | lookup_protocol_method(proto, sel, | |
1725 | isRequiredMethod, isInstanceMethod, false); | |
1726 | if (!m) { | |
1727 | // No method with that name. Search incorporated protocols. | |
1728 | if (proto->protocol_list) { | |
1729 | for (int i = 0; i < proto->protocol_list->count; i++) { | |
1730 | const char *enc = | |
1731 | _protocol_getMethodTypeEncoding((Protocol *)proto->protocol_list->list[i], sel, isRequiredMethod, isInstanceMethod); | |
1732 | if (enc) return enc; | |
1733 | } | |
1734 | } | |
7257e56c | 1735 | return nil; |
cd5f04f5 A |
1736 | } |
1737 | ||
1738 | int i = 0; | |
1739 | if (isRequiredMethod && isInstanceMethod) { | |
1740 | i += ((uintptr_t)m - (uintptr_t)proto->instance_methods) / sizeof(proto->instance_methods->list[0]); | |
1741 | goto done; | |
1742 | } else if (proto->instance_methods) { | |
1743 | i += proto->instance_methods->count; | |
1744 | } | |
1745 | ||
1746 | if (isRequiredMethod && !isInstanceMethod) { | |
1747 | i += ((uintptr_t)m - (uintptr_t)proto->class_methods) / sizeof(proto->class_methods->list[0]); | |
1748 | goto done; | |
1749 | } else if (proto->class_methods) { | |
1750 | i += proto->class_methods->count; | |
1751 | } | |
1752 | ||
1753 | if (!isRequiredMethod && isInstanceMethod) { | |
1754 | i += ((uintptr_t)m - (uintptr_t)ext->optional_instance_methods) / sizeof(ext->optional_instance_methods->list[0]); | |
1755 | goto done; | |
1756 | } else if (ext->optional_instance_methods) { | |
1757 | i += ext->optional_instance_methods->count; | |
1758 | } | |
1759 | ||
1760 | if (!isRequiredMethod && !isInstanceMethod) { | |
1761 | i += ((uintptr_t)m - (uintptr_t)ext->optional_class_methods) / sizeof(ext->optional_class_methods->list[0]); | |
1762 | goto done; | |
1763 | } else if (ext->optional_class_methods) { | |
1764 | i += ext->optional_class_methods->count; | |
1765 | } | |
1766 | ||
1767 | done: | |
1768 | return ext->extendedMethodTypes[i]; | |
1769 | } | |
1770 | ||
1771 | ||
8972963c A |
1772 | /*********************************************************************** |
1773 | * objc_allocateProtocol | |
1774 | * Creates a new protocol. The protocol may not be used until | |
1775 | * objc_registerProtocol() is called. | |
7257e56c | 1776 | * Returns nil if a protocol with the same name already exists. |
8972963c A |
1777 | * Locking: acquires classLock |
1778 | **********************************************************************/ | |
1779 | Protocol * | |
1780 | objc_allocateProtocol(const char *name) | |
1781 | { | |
7257e56c | 1782 | Class cls = objc_getClass("__IncompleteProtocol"); |
1807f628 | 1783 | ASSERT(cls); |
8972963c | 1784 | |
31875a97 | 1785 | mutex_locker_t lock(classLock); |
8972963c | 1786 | |
31875a97 | 1787 | if (NXMapGet(protocol_map, name)) return nil; |
8972963c | 1788 | |
7257e56c | 1789 | old_protocol *result = (old_protocol *) |
31875a97 | 1790 | calloc(1, sizeof(old_protocol) |
7257e56c A |
1791 | + sizeof(old_protocol_ext)); |
1792 | old_protocol_ext *ext = (old_protocol_ext *)(result+1); | |
8972963c A |
1793 | |
1794 | result->isa = cls; | |
31875a97 | 1795 | result->protocol_name = strdup(name); |
7257e56c | 1796 | ext->size = sizeof(old_protocol_ext); |
8972963c A |
1797 | |
1798 | // fixme reserve name without installing | |
1799 | ||
1800 | NXMapInsert(protocol_ext_map, result, result+1); | |
1801 | ||
8972963c A |
1802 | return (Protocol *)result; |
1803 | } | |
1804 | ||
1805 | ||
1806 | /*********************************************************************** | |
1807 | * objc_registerProtocol | |
1808 | * Registers a newly-constructed protocol. The protocol is now | |
1809 | * ready for use and immutable. | |
1810 | * Locking: acquires classLock | |
1811 | **********************************************************************/ | |
1812 | void objc_registerProtocol(Protocol *proto_gen) | |
1813 | { | |
7257e56c | 1814 | old_protocol *proto = oldprotocol(proto_gen); |
8972963c | 1815 | |
7257e56c A |
1816 | Class oldcls = objc_getClass("__IncompleteProtocol"); |
1817 | Class cls = objc_getClass("Protocol"); | |
8972963c | 1818 | |
31875a97 | 1819 | mutex_locker_t lock(classLock); |
8972963c A |
1820 | |
1821 | if (proto->isa == cls) { | |
1822 | _objc_inform("objc_registerProtocol: protocol '%s' was already " | |
1823 | "registered!", proto->protocol_name); | |
8972963c A |
1824 | return; |
1825 | } | |
1826 | if (proto->isa != oldcls) { | |
1827 | _objc_inform("objc_registerProtocol: protocol '%s' was not allocated " | |
1828 | "with objc_allocateProtocol!", proto->protocol_name); | |
8972963c A |
1829 | return; |
1830 | } | |
1831 | ||
1832 | proto->isa = cls; | |
1833 | ||
1834 | NXMapKeyCopyingInsert(protocol_map, proto->protocol_name, proto); | |
8972963c A |
1835 | } |
1836 | ||
1837 | ||
1838 | /*********************************************************************** | |
1839 | * protocol_addProtocol | |
1840 | * Adds an incorporated protocol to another protocol. | |
1841 | * No method enforcement is performed. | |
1842 | * `proto` must be under construction. `addition` must not. | |
1843 | * Locking: acquires classLock | |
1844 | **********************************************************************/ | |
1845 | void | |
1846 | protocol_addProtocol(Protocol *proto_gen, Protocol *addition_gen) | |
1847 | { | |
7257e56c A |
1848 | old_protocol *proto = oldprotocol(proto_gen); |
1849 | old_protocol *addition = oldprotocol(addition_gen); | |
8972963c | 1850 | |
7257e56c | 1851 | Class cls = objc_getClass("__IncompleteProtocol"); |
8972963c A |
1852 | |
1853 | if (!proto_gen) return; | |
1854 | if (!addition_gen) return; | |
1855 | ||
31875a97 | 1856 | mutex_locker_t lock(classLock); |
8972963c A |
1857 | |
1858 | if (proto->isa != cls) { | |
1859 | _objc_inform("protocol_addProtocol: modified protocol '%s' is not " | |
1860 | "under construction!", proto->protocol_name); | |
8972963c A |
1861 | return; |
1862 | } | |
1863 | if (addition->isa == cls) { | |
1864 | _objc_inform("protocol_addProtocol: added protocol '%s' is still " | |
1865 | "under construction!", addition->protocol_name); | |
8972963c A |
1866 | return; |
1867 | } | |
1868 | ||
7257e56c | 1869 | old_protocol_list *protolist = proto->protocol_list; |
8972963c | 1870 | if (protolist) { |
7257e56c | 1871 | size_t size = sizeof(old_protocol_list) |
8972963c | 1872 | + protolist->count * sizeof(protolist->list[0]); |
7257e56c | 1873 | protolist = (old_protocol_list *) |
31875a97 | 1874 | realloc(protolist, size); |
8972963c | 1875 | } else { |
7257e56c | 1876 | protolist = (old_protocol_list *) |
31875a97 | 1877 | calloc(1, sizeof(old_protocol_list)); |
8972963c A |
1878 | } |
1879 | ||
1880 | protolist->list[protolist->count++] = addition; | |
1881 | proto->protocol_list = protolist; | |
8972963c A |
1882 | } |
1883 | ||
1884 | ||
1885 | /*********************************************************************** | |
1886 | * protocol_addMethodDescription | |
1887 | * Adds a method to a protocol. The protocol must be under construction. | |
1888 | * Locking: acquires classLock | |
1889 | **********************************************************************/ | |
1890 | static void | |
1891 | _protocol_addMethod(struct objc_method_description_list **list, SEL name, const char *types) | |
1892 | { | |
1893 | if (!*list) { | |
1894 | *list = (struct objc_method_description_list *) | |
31875a97 | 1895 | calloc(sizeof(struct objc_method_description_list), 1); |
8972963c A |
1896 | } else { |
1897 | size_t size = sizeof(struct objc_method_description_list) | |
1898 | + (*list)->count * sizeof(struct objc_method_description); | |
1899 | *list = (struct objc_method_description_list *) | |
31875a97 | 1900 | realloc(*list, size); |
8972963c A |
1901 | } |
1902 | ||
1903 | struct objc_method_description *desc = &(*list)->list[(*list)->count++]; | |
1904 | desc->name = name; | |
31875a97 | 1905 | desc->types = strdup(types ?: ""); |
8972963c A |
1906 | } |
1907 | ||
1908 | void | |
1909 | protocol_addMethodDescription(Protocol *proto_gen, SEL name, const char *types, | |
1910 | BOOL isRequiredMethod, BOOL isInstanceMethod) | |
1911 | { | |
7257e56c | 1912 | old_protocol *proto = oldprotocol(proto_gen); |
8972963c | 1913 | |
7257e56c | 1914 | Class cls = objc_getClass("__IncompleteProtocol"); |
8972963c A |
1915 | |
1916 | if (!proto_gen) return; | |
1917 | ||
31875a97 | 1918 | mutex_locker_t lock(classLock); |
8972963c A |
1919 | |
1920 | if (proto->isa != cls) { | |
1921 | _objc_inform("protocol_addMethodDescription: protocol '%s' is not " | |
1922 | "under construction!", proto->protocol_name); | |
8972963c A |
1923 | return; |
1924 | } | |
1925 | ||
1926 | if (isRequiredMethod && isInstanceMethod) { | |
1927 | _protocol_addMethod(&proto->instance_methods, name, types); | |
1928 | } else if (isRequiredMethod && !isInstanceMethod) { | |
1929 | _protocol_addMethod(&proto->class_methods, name, types); | |
1930 | } else if (!isRequiredMethod && isInstanceMethod) { | |
7257e56c | 1931 | old_protocol_ext *ext = (old_protocol_ext *)(proto+1); |
8972963c A |
1932 | _protocol_addMethod(&ext->optional_instance_methods, name, types); |
1933 | } else /* !isRequiredMethod && !isInstanceMethod) */ { | |
7257e56c | 1934 | old_protocol_ext *ext = (old_protocol_ext *)(proto+1); |
8972963c A |
1935 | _protocol_addMethod(&ext->optional_class_methods, name, types); |
1936 | } | |
8972963c A |
1937 | } |
1938 | ||
1939 | ||
1940 | /*********************************************************************** | |
1941 | * protocol_addProperty | |
1942 | * Adds a property to a protocol. The protocol must be under construction. | |
1943 | * Locking: acquires classLock | |
1944 | **********************************************************************/ | |
1945 | static void | |
7257e56c | 1946 | _protocol_addProperty(old_property_list **plist, const char *name, |
8972963c A |
1947 | const objc_property_attribute_t *attrs, |
1948 | unsigned int count) | |
1949 | { | |
1950 | if (!*plist) { | |
7257e56c | 1951 | *plist = (old_property_list *) |
31875a97 | 1952 | calloc(sizeof(old_property_list), 1); |
7257e56c | 1953 | (*plist)->entsize = sizeof(old_property); |
8972963c | 1954 | } else { |
7257e56c | 1955 | *plist = (old_property_list *) |
31875a97 | 1956 | realloc(*plist, sizeof(old_property_list) |
8972963c A |
1957 | + (*plist)->count * (*plist)->entsize); |
1958 | } | |
1959 | ||
7257e56c | 1960 | old_property *prop = property_list_nth(*plist, (*plist)->count++); |
31875a97 | 1961 | prop->name = strdup(name); |
8972963c A |
1962 | prop->attributes = copyPropertyAttributeString(attrs, count); |
1963 | } | |
1964 | ||
1965 | void | |
1966 | protocol_addProperty(Protocol *proto_gen, const char *name, | |
1967 | const objc_property_attribute_t *attrs, | |
1968 | unsigned int count, | |
1969 | BOOL isRequiredProperty, BOOL isInstanceProperty) | |
1970 | { | |
7257e56c | 1971 | old_protocol *proto = oldprotocol(proto_gen); |
8972963c | 1972 | |
7257e56c | 1973 | Class cls = objc_getClass("__IncompleteProtocol"); |
8972963c A |
1974 | |
1975 | if (!proto) return; | |
1976 | if (!name) return; | |
1977 | ||
31875a97 | 1978 | mutex_locker_t lock(classLock); |
8972963c A |
1979 | |
1980 | if (proto->isa != cls) { | |
1981 | _objc_inform("protocol_addProperty: protocol '%s' is not " | |
1982 | "under construction!", proto->protocol_name); | |
8972963c A |
1983 | return; |
1984 | } | |
1985 | ||
7257e56c | 1986 | old_protocol_ext *ext = ext_for_protocol(proto); |
8972963c A |
1987 | |
1988 | if (isRequiredProperty && isInstanceProperty) { | |
1989 | _protocol_addProperty(&ext->instance_properties, name, attrs, count); | |
1990 | } | |
c1e772c4 A |
1991 | else if (isRequiredProperty && !isInstanceProperty) { |
1992 | _protocol_addProperty(&ext->class_properties, name, attrs, count); | |
1993 | } | |
1994 | // else if (!isRequiredProperty && isInstanceProperty) { | |
8972963c | 1995 | // _protocol_addProperty(&ext->optional_instance_properties, name, attrs, count); |
c1e772c4 A |
1996 | //} |
1997 | // else /* !isRequiredProperty && !isInstanceProperty) */ { | |
8972963c A |
1998 | // _protocol_addProperty(&ext->optional_class_properties, name, attrs, count); |
1999 | //} | |
8972963c A |
2000 | } |
2001 | ||
2002 | ||
b3962a83 A |
2003 | /*********************************************************************** |
2004 | * _objc_fixup_protocol_objects_for_image. For each protocol in the | |
2005 | * specified image, selectorize the method names and add to the protocol hash. | |
2006 | **********************************************************************/ | |
2007 | ||
31875a97 | 2008 | static bool versionIsExt(uintptr_t version, const char *names, size_t size) |
b3962a83 A |
2009 | { |
2010 | // CodeWarrior used isa field for string "Protocol" | |
2011 | // from section __OBJC,__class_names. rdar://4951638 | |
2012 | // gcc (10.4 and earlier) used isa field for version number; | |
2013 | // the only version number used on Mac OS X was 2. | |
2014 | // gcc (10.5 and later) uses isa field for ext pointer | |
2015 | ||
8070259c | 2016 | if (version < 4096 /* not PAGE_SIZE */) { |
b3962a83 A |
2017 | return NO; |
2018 | } | |
2019 | ||
2020 | if (version >= (uintptr_t)names && version < (uintptr_t)(names + size)) { | |
2021 | return NO; | |
2022 | } | |
2023 | ||
2024 | return YES; | |
2025 | } | |
2026 | ||
7257e56c | 2027 | static void fix_protocol(old_protocol *proto, Class protocolClass, |
31875a97 | 2028 | bool isBundle, const char *names, size_t names_size) |
b3962a83 | 2029 | { |
7af964d1 | 2030 | uintptr_t version; |
b3962a83 A |
2031 | if (!proto) return; |
2032 | ||
7af964d1 | 2033 | version = (uintptr_t)proto->isa; |
b3962a83 A |
2034 | |
2035 | // Set the protocol's isa | |
2036 | proto->isa = protocolClass; | |
2037 | ||
2038 | // Fix up method lists | |
2039 | // fixme share across duplicates | |
2040 | map_method_descs (proto->instance_methods, isBundle); | |
2041 | map_method_descs (proto->class_methods, isBundle); | |
2042 | ||
2043 | // Fix up ext, if any | |
2044 | if (versionIsExt(version, names, names_size)) { | |
7257e56c | 2045 | old_protocol_ext *ext = (old_protocol_ext *)version; |
b3962a83 A |
2046 | NXMapInsert(protocol_ext_map, proto, ext); |
2047 | map_method_descs (ext->optional_instance_methods, isBundle); | |
2048 | map_method_descs (ext->optional_class_methods, isBundle); | |
2049 | } | |
2050 | ||
2051 | // Record the protocol it if we don't have one with this name yet | |
2052 | // fixme bundles - copy protocol | |
2053 | // fixme unloading | |
2054 | if (!NXMapGet(protocol_map, proto->protocol_name)) { | |
2055 | NXMapKeyCopyingInsert(protocol_map, proto->protocol_name, proto); | |
2056 | if (PrintProtocols) { | |
2057 | _objc_inform("PROTOCOLS: protocol at %p is %s", | |
2058 | proto, proto->protocol_name); | |
2059 | } | |
2060 | } else { | |
2061 | // duplicate - do nothing | |
2062 | if (PrintProtocols) { | |
2063 | _objc_inform("PROTOCOLS: protocol at %p is %s (duplicate)", | |
2064 | proto, proto->protocol_name); | |
2065 | } | |
2066 | } | |
2067 | } | |
2068 | ||
2069 | static void _objc_fixup_protocol_objects_for_image (header_info * hi) | |
2070 | { | |
7257e56c | 2071 | Class protocolClass = objc_getClass("Protocol"); |
b3962a83 | 2072 | size_t count, i; |
7257e56c | 2073 | old_protocol **protos; |
7af964d1 | 2074 | int isBundle = headerIsBundle(hi); |
b3962a83 A |
2075 | const char *names; |
2076 | size_t names_size; | |
2077 | ||
31875a97 | 2078 | mutex_locker_t lock(classLock); |
b3962a83 A |
2079 | |
2080 | // Allocate the protocol registry if necessary. | |
2081 | if (!protocol_map) { | |
2082 | protocol_map = | |
31875a97 | 2083 | NXCreateMapTable(NXStrValueMapPrototype, 32); |
b3962a83 A |
2084 | } |
2085 | if (!protocol_ext_map) { | |
2086 | protocol_ext_map = | |
31875a97 | 2087 | NXCreateMapTable(NXPtrValueMapPrototype, 32); |
b3962a83 A |
2088 | } |
2089 | ||
2090 | protos = _getObjcProtocols(hi, &count); | |
2091 | names = _getObjcClassNames(hi, &names_size); | |
2092 | for (i = 0; i < count; i++) { | |
7af964d1 | 2093 | fix_protocol(protos[i], protocolClass, isBundle, names, names_size); |
b3962a83 | 2094 | } |
b3962a83 A |
2095 | } |
2096 | ||
2097 | ||
2098 | /*********************************************************************** | |
2099 | * _objc_fixup_selector_refs. Register all of the selectors in each | |
2100 | * image, and fix them all up. | |
2101 | **********************************************************************/ | |
2102 | static void _objc_fixup_selector_refs (const header_info *hi) | |
2103 | { | |
2104 | size_t count; | |
2105 | SEL *sels; | |
2106 | ||
31875a97 | 2107 | bool preoptimized = hi->isPreoptimized(); |
31875a97 | 2108 | |
7af964d1 | 2109 | if (PrintPreopt) { |
31875a97 | 2110 | if (preoptimized) { |
7af964d1 | 2111 | _objc_inform("PREOPTIMIZATION: honoring preoptimized selectors in %s", |
c1e772c4 | 2112 | hi->fname()); |
7af964d1 | 2113 | } |
c1e772c4 | 2114 | else if (hi->info()->optimizedByDyld()) { |
7af964d1 | 2115 | _objc_inform("PREOPTIMIZATION: IGNORING preoptimized selectors in %s", |
c1e772c4 | 2116 | hi->fname()); |
7af964d1 A |
2117 | } |
2118 | } | |
2119 | ||
31875a97 | 2120 | if (preoptimized) return; |
7af964d1 | 2121 | |
b3962a83 | 2122 | sels = _getObjcSelectorRefs (hi, &count); |
7af964d1 A |
2123 | |
2124 | map_selrefs(sels, count, headerIsBundle(hi)); | |
2125 | } | |
2126 | ||
31875a97 | 2127 | static inline bool _is_threaded() { |
7af964d1 A |
2128 | #if TARGET_OS_WIN32 |
2129 | return YES; | |
2130 | #else | |
2131 | return pthread_is_threaded_np() != 0; | |
2132 | #endif | |
2133 | } | |
2134 | ||
8972963c | 2135 | #if !TARGET_OS_WIN32 |
7af964d1 A |
2136 | /*********************************************************************** |
2137 | * unmap_image | |
2138 | * Process the given image which is about to be unmapped by dyld. | |
2139 | * mh is mach_header instead of headerType because that's what | |
2140 | * dyld_priv.h says even for 64-bit. | |
2141 | **********************************************************************/ | |
cd5f04f5 | 2142 | void |
c1e772c4 | 2143 | unmap_image(const char *path __unused, const struct mach_header *mh) |
7af964d1 | 2144 | { |
31875a97 | 2145 | recursive_mutex_locker_t lock(loadMethodLock); |
8972963c | 2146 | unmap_image_nolock(mh); |
7af964d1 A |
2147 | } |
2148 | ||
2149 | ||
2150 | /*********************************************************************** | |
2151 | * map_images | |
2152 | * Process the given images which are being mapped in by dyld. | |
2153 | * Calls ABI-agnostic code after taking ABI-specific locks. | |
2154 | **********************************************************************/ | |
c1e772c4 | 2155 | void |
bd8dfcfc A |
2156 | map_images(unsigned count, const char * const paths[], |
2157 | const struct mach_header * const mhdrs[]) | |
7af964d1 | 2158 | { |
31875a97 | 2159 | recursive_mutex_locker_t lock(loadMethodLock); |
c1e772c4 | 2160 | map_images_nolock(count, paths, mhdrs); |
7af964d1 A |
2161 | } |
2162 | ||
2163 | ||
2164 | /*********************************************************************** | |
2165 | * load_images | |
2166 | * Process +load in the given images which are being mapped in by dyld. | |
7af964d1 A |
2167 | * |
2168 | * Locking: acquires classLock and loadMethodLock | |
2169 | **********************************************************************/ | |
c1e772c4 | 2170 | extern void prepare_load_methods(const headerType *mhdr); |
7af964d1 | 2171 | |
c1e772c4 A |
2172 | void |
2173 | load_images(const char *path __unused, const struct mach_header *mh) | |
2174 | { | |
31875a97 | 2175 | recursive_mutex_locker_t lock(loadMethodLock); |
7af964d1 A |
2176 | |
2177 | // Discover +load methods | |
c1e772c4 | 2178 | prepare_load_methods((const headerType *)mh); |
7af964d1 A |
2179 | |
2180 | // Call +load methods (without classLock - re-entrant) | |
c1e772c4 | 2181 | call_load_methods(); |
b3962a83 | 2182 | } |
8972963c | 2183 | #endif |
b3962a83 A |
2184 | |
2185 | ||
2186 | /*********************************************************************** | |
2187 | * _read_images | |
2188 | * Perform metadata processing for hCount images starting with firstNewHeader | |
2189 | **********************************************************************/ | |
c1e772c4 | 2190 | void _read_images(header_info **hList, uint32_t hCount, int totalClasses, int unoptimizedTotalClass) |
b3962a83 A |
2191 | { |
2192 | uint32_t i; | |
31875a97 | 2193 | bool categoriesLoaded = NO; |
b3962a83 A |
2194 | |
2195 | if (!class_hash) _objc_init_class_hash(); | |
2196 | ||
2197 | // Parts of this order are important for correctness or performance. | |
2198 | ||
1807f628 A |
2199 | // Fix up selector refs from all images. |
2200 | for (i = 0; i < hCount; i++) { | |
2201 | _objc_fixup_selector_refs(hList[i]); | |
2202 | } | |
2203 | ||
b3962a83 A |
2204 | // Read classes from all images. |
2205 | for (i = 0; i < hCount; i++) { | |
2206 | _objc_read_classes_from_image(hList[i]); | |
2207 | } | |
2208 | ||
2209 | // Read categories from all images. | |
7af964d1 A |
2210 | // But not if any other threads are running - they might |
2211 | // call a category method before the fixups below are complete. | |
2212 | if (!_is_threaded()) { | |
31875a97 | 2213 | bool needFlush = NO; |
7af964d1 A |
2214 | for (i = 0; i < hCount; i++) { |
2215 | needFlush |= _objc_read_categories_from_image(hList[i]); | |
2216 | } | |
2217 | if (needFlush) flush_marked_caches(); | |
2218 | categoriesLoaded = YES; | |
b3962a83 | 2219 | } |
b3962a83 A |
2220 | |
2221 | // Connect classes from all images. | |
2222 | for (i = 0; i < hCount; i++) { | |
2223 | _objc_connect_classes_from_image(hList[i]); | |
2224 | } | |
2225 | ||
1807f628 | 2226 | // Fix up class refs, and protocol objects from all images. |
b3962a83 A |
2227 | for (i = 0; i < hCount; i++) { |
2228 | _objc_map_class_refs_for_image(hList[i]); | |
b3962a83 A |
2229 | _objc_fixup_protocol_objects_for_image(hList[i]); |
2230 | } | |
7af964d1 A |
2231 | |
2232 | // Read categories from all images. | |
2233 | // But not if this is the only thread - it's more | |
2234 | // efficient to attach categories earlier if safe. | |
2235 | if (!categoriesLoaded) { | |
31875a97 | 2236 | bool needFlush = NO; |
7af964d1 A |
2237 | for (i = 0; i < hCount; i++) { |
2238 | needFlush |= _objc_read_categories_from_image(hList[i]); | |
2239 | } | |
2240 | if (needFlush) flush_marked_caches(); | |
2241 | } | |
2242 | ||
2243 | // Multi-threaded category load MUST BE LAST to avoid a race. | |
b3962a83 A |
2244 | } |
2245 | ||
2246 | ||
2247 | /*********************************************************************** | |
2248 | * prepare_load_methods | |
2249 | * Schedule +load for classes in this image, any un-+load-ed | |
2250 | * superclasses in other images, and any categories in this image. | |
2251 | **********************************************************************/ | |
2252 | // Recursively schedule +load for cls and any un-+load-ed superclasses. | |
2253 | // cls must already be connected. | |
7257e56c | 2254 | static void schedule_class_load(Class cls) |
b3962a83 A |
2255 | { |
2256 | if (cls->info & CLS_LOADED) return; | |
7257e56c A |
2257 | if (cls->superclass) schedule_class_load(cls->superclass); |
2258 | add_class_to_loadable_list(cls); | |
b3962a83 A |
2259 | cls->info |= CLS_LOADED; |
2260 | } | |
2261 | ||
31875a97 | 2262 | void prepare_load_methods(const headerType *mhdr) |
b3962a83 A |
2263 | { |
2264 | Module mods; | |
2265 | unsigned int midx; | |
31875a97 A |
2266 | |
2267 | header_info *hi; | |
c1e772c4 A |
2268 | for (hi = FirstHeader; hi; hi = hi->getNext()) { |
2269 | if (mhdr == hi->mhdr()) break; | |
31875a97 A |
2270 | } |
2271 | if (!hi) return; | |
b3962a83 | 2272 | |
c1e772c4 | 2273 | if (hi->info()->isReplacement()) { |
b3962a83 A |
2274 | // Ignore any classes in this image |
2275 | return; | |
2276 | } | |
2277 | ||
2278 | // Major loop - process all modules in the image | |
2279 | mods = hi->mod_ptr; | |
2280 | for (midx = 0; midx < hi->mod_count; midx += 1) | |
2281 | { | |
2282 | unsigned int index; | |
2283 | ||
2284 | // Skip module containing no classes | |
7257e56c | 2285 | if (mods[midx].symtab == nil) |
b3962a83 A |
2286 | continue; |
2287 | ||
2288 | // Minor loop - process all the classes in given module | |
2289 | for (index = 0; index < mods[midx].symtab->cls_def_cnt; index += 1) | |
2290 | { | |
2291 | // Locate the class description pointer | |
7257e56c | 2292 | Class cls = (Class)mods[midx].symtab->defs[index]; |
b3962a83 A |
2293 | if (cls->info & CLS_CONNECTED) { |
2294 | schedule_class_load(cls); | |
2295 | } | |
2296 | } | |
2297 | } | |
2298 | ||
2299 | ||
2300 | // Major loop - process all modules in the header | |
2301 | mods = hi->mod_ptr; | |
2302 | ||
2303 | // NOTE: The module and category lists are traversed backwards | |
2304 | // to preserve the pre-10.4 processing order. Changing the order | |
2305 | // would have a small chance of introducing binary compatibility bugs. | |
7af964d1 | 2306 | midx = (unsigned int)hi->mod_count; |
b3962a83 A |
2307 | while (midx-- > 0) { |
2308 | unsigned int index; | |
2309 | unsigned int total; | |
2310 | Symtab symtab = mods[midx].symtab; | |
2311 | ||
2312 | // Nothing to do for a module without a symbol table | |
7257e56c | 2313 | if (mods[midx].symtab == nil) |
b3962a83 A |
2314 | continue; |
2315 | // Total entries in symbol table (class entries followed | |
2316 | // by category entries) | |
2317 | total = mods[midx].symtab->cls_def_cnt + | |
2318 | mods[midx].symtab->cat_def_cnt; | |
2319 | ||
2320 | // Minor loop - register all categories from given module | |
2321 | index = total; | |
2322 | while (index-- > mods[midx].symtab->cls_def_cnt) { | |
7257e56c | 2323 | old_category *cat = (old_category *)symtab->defs[index]; |
b3962a83 A |
2324 | add_category_to_loadable_list((Category)cat); |
2325 | } | |
2326 | } | |
2327 | } | |
2328 | ||
2329 | ||
7af964d1 A |
2330 | #if TARGET_OS_WIN32 |
2331 | ||
7257e56c | 2332 | void unload_class(Class cls) |
7af964d1 A |
2333 | { |
2334 | } | |
2335 | ||
2336 | #else | |
2337 | ||
b3962a83 A |
2338 | /*********************************************************************** |
2339 | * _objc_remove_classes_in_image | |
2340 | * Remove all classes in the given image from the runtime, because | |
2341 | * the image is about to be unloaded. | |
2342 | * Things to clean up: | |
2343 | * class_hash | |
2344 | * unconnected_class_hash | |
2345 | * pending subclasses list (only if class is still unconnected) | |
2346 | * loadable class list | |
2347 | * class's method caches | |
2348 | * class refs in all other images | |
2349 | **********************************************************************/ | |
2350 | // Re-pend any class references in refs that point into [start..end) | |
7257e56c | 2351 | static void rependClassReferences(Class *refs, size_t count, |
b3962a83 A |
2352 | uintptr_t start, uintptr_t end) |
2353 | { | |
2354 | size_t i; | |
2355 | ||
2356 | if (!refs) return; | |
2357 | ||
2358 | // Process each class ref | |
2359 | for (i = 0; i < count; i++) { | |
2360 | if ((uintptr_t)(refs[i]) >= start && (uintptr_t)(refs[i]) < end) { | |
31875a97 A |
2361 | pendClassReference(&refs[i], refs[i]->name, |
2362 | refs[i]->info & CLS_META); | |
7257e56c | 2363 | refs[i] = nil; |
b3962a83 A |
2364 | } |
2365 | } | |
2366 | } | |
2367 | ||
2368 | ||
cd5f04f5 | 2369 | void try_free(const void *p) |
b3962a83 A |
2370 | { |
2371 | if (p && malloc_size(p)) free((void *)p); | |
2372 | } | |
2373 | ||
2374 | // Deallocate all memory in a method list | |
7257e56c | 2375 | static void unload_mlist(old_method_list *mlist) |
b3962a83 A |
2376 | { |
2377 | int i; | |
7af964d1 A |
2378 | for (i = 0; i < mlist->method_count; i++) { |
2379 | try_free(mlist->method_list[i].method_types); | |
b3962a83 | 2380 | } |
7af964d1 | 2381 | try_free(mlist); |
b3962a83 A |
2382 | } |
2383 | ||
7257e56c | 2384 | static void unload_property_list(old_property_list *proplist) |
8972963c A |
2385 | { |
2386 | uint32_t i; | |
2387 | ||
2388 | if (!proplist) return; | |
2389 | ||
2390 | for (i = 0; i < proplist->count; i++) { | |
7257e56c | 2391 | old_property *prop = property_list_nth(proplist, i); |
8972963c A |
2392 | try_free(prop->name); |
2393 | try_free(prop->attributes); | |
2394 | } | |
2395 | try_free(proplist); | |
2396 | } | |
2397 | ||
2398 | ||
b3962a83 | 2399 | // Deallocate all memory in a class. |
7257e56c | 2400 | void unload_class(Class cls) |
b3962a83 | 2401 | { |
7af964d1 A |
2402 | // Free method cache |
2403 | // This dereferences the cache contents; do this before freeing methods | |
2404 | if (cls->cache && cls->cache != &_objc_empty_cache) { | |
2405 | _cache_free(cls->cache); | |
2406 | } | |
2407 | ||
b3962a83 A |
2408 | // Free ivar lists |
2409 | if (cls->ivars) { | |
2410 | int i; | |
2411 | for (i = 0; i < cls->ivars->ivar_count; i++) { | |
2412 | try_free(cls->ivars->ivar_list[i].ivar_name); | |
2413 | try_free(cls->ivars->ivar_list[i].ivar_type); | |
2414 | } | |
2415 | try_free(cls->ivars); | |
2416 | } | |
2417 | ||
2418 | // Free fixed-up method lists and method list array | |
2419 | if (cls->methodLists) { | |
2420 | // more than zero method lists | |
2421 | if (cls->info & CLS_NO_METHOD_ARRAY) { | |
2422 | // one method list | |
7257e56c | 2423 | unload_mlist((old_method_list *)cls->methodLists); |
b3962a83 A |
2424 | } |
2425 | else { | |
2426 | // more than one method list | |
7257e56c | 2427 | old_method_list **mlistp; |
b3962a83 | 2428 | for (mlistp = cls->methodLists; |
7257e56c | 2429 | *mlistp != nil && *mlistp != END_OF_METHODS_LIST; |
b3962a83 A |
2430 | mlistp++) |
2431 | { | |
2432 | unload_mlist(*mlistp); | |
2433 | } | |
2434 | free(cls->methodLists); | |
2435 | } | |
2436 | } | |
2437 | ||
2438 | // Free protocol list | |
7257e56c | 2439 | old_protocol_list *protos = cls->protocols; |
b3962a83 | 2440 | while (protos) { |
7257e56c | 2441 | old_protocol_list *dead = protos; |
b3962a83 A |
2442 | protos = protos->next; |
2443 | try_free(dead); | |
2444 | } | |
2445 | ||
b3962a83 A |
2446 | if ((cls->info & CLS_EXT)) { |
2447 | if (cls->ext) { | |
2448 | // Free property lists and property list array | |
2449 | if (cls->ext->propertyLists) { | |
2450 | // more than zero property lists | |
2451 | if (cls->info & CLS_NO_PROPERTY_ARRAY) { | |
2452 | // one property list | |
7257e56c A |
2453 | old_property_list *proplist = |
2454 | (old_property_list *)cls->ext->propertyLists; | |
8972963c | 2455 | unload_property_list(proplist); |
b3962a83 A |
2456 | } else { |
2457 | // more than one property list | |
7257e56c | 2458 | old_property_list **plistp; |
b3962a83 | 2459 | for (plistp = cls->ext->propertyLists; |
7257e56c | 2460 | *plistp != nil; |
b3962a83 A |
2461 | plistp++) |
2462 | { | |
8972963c | 2463 | unload_property_list(*plistp); |
b3962a83 A |
2464 | } |
2465 | try_free(cls->ext->propertyLists); | |
2466 | } | |
2467 | } | |
2468 | ||
2469 | // Free weak ivar layout | |
2470 | try_free(cls->ext->weak_ivar_layout); | |
2471 | ||
2472 | // Free ext | |
2473 | try_free(cls->ext); | |
2474 | } | |
2475 | ||
2476 | // Free non-weak ivar layout | |
2477 | try_free(cls->ivar_layout); | |
2478 | } | |
2479 | ||
2480 | // Free class name | |
2481 | try_free(cls->name); | |
2482 | ||
2483 | // Free cls | |
2484 | try_free(cls); | |
2485 | } | |
2486 | ||
2487 | ||
2488 | static void _objc_remove_classes_in_image(header_info *hi) | |
2489 | { | |
2490 | unsigned int index; | |
2491 | unsigned int midx; | |
2492 | Module mods; | |
2493 | ||
31875a97 | 2494 | mutex_locker_t lock(classLock); |
b3962a83 A |
2495 | |
2496 | // Major loop - process all modules in the image | |
2497 | mods = hi->mod_ptr; | |
2498 | for (midx = 0; midx < hi->mod_count; midx += 1) | |
2499 | { | |
2500 | // Skip module containing no classes | |
7257e56c | 2501 | if (mods[midx].symtab == nil) |
b3962a83 A |
2502 | continue; |
2503 | ||
2504 | // Minor loop - process all the classes in given module | |
2505 | for (index = 0; index < mods[midx].symtab->cls_def_cnt; index += 1) | |
2506 | { | |
7257e56c | 2507 | Class cls; |
b3962a83 A |
2508 | |
2509 | // Locate the class description pointer | |
7257e56c | 2510 | cls = (Class)mods[midx].symtab->defs[index]; |
b3962a83 A |
2511 | |
2512 | // Remove from loadable class list, if present | |
7257e56c | 2513 | remove_class_from_loadable_list(cls); |
b3962a83 A |
2514 | |
2515 | // Remove from unconnected_class_hash and pending subclasses | |
2516 | if (unconnected_class_hash && NXHashMember(unconnected_class_hash, cls)) { | |
2517 | NXHashRemove(unconnected_class_hash, cls); | |
2518 | if (pendingSubclassesMap) { | |
2519 | // Find this class in its superclass's pending list | |
7257e56c A |
2520 | char *supercls_name = (char *)cls->superclass; |
2521 | PendingSubclass *pending = (PendingSubclass *) | |
b3962a83 | 2522 | NXMapGet(pendingSubclassesMap, supercls_name); |
7257e56c | 2523 | for ( ; pending != nil; pending = pending->next) { |
b3962a83 A |
2524 | if (pending->subclass == cls) { |
2525 | pending->subclass = Nil; | |
2526 | break; | |
2527 | } | |
2528 | } | |
2529 | } | |
2530 | } | |
2531 | ||
2532 | // Remove from class_hash | |
2533 | NXHashRemove(class_hash, cls); | |
2534 | ||
2535 | // Free heap memory pointed to by the class | |
7257e56c | 2536 | unload_class(cls->ISA()); |
b3962a83 A |
2537 | unload_class(cls); |
2538 | } | |
2539 | } | |
2540 | ||
2541 | ||
2542 | // Search all other images for class refs that point back to this range. | |
2543 | // Un-fix and re-pend any such class refs. | |
2544 | ||
2545 | // Get the location of the dying image's __OBJC segment | |
8972963c A |
2546 | uintptr_t seg; |
2547 | unsigned long seg_size; | |
c1e772c4 | 2548 | seg = (uintptr_t)getsegmentdata(hi->mhdr(), "__OBJC", &seg_size); |
b3962a83 A |
2549 | |
2550 | header_info *other_hi; | |
c1e772c4 | 2551 | for (other_hi = FirstHeader; other_hi != nil; other_hi = other_hi->getNext()) { |
7257e56c | 2552 | Class *other_refs; |
b3962a83 A |
2553 | size_t count; |
2554 | if (other_hi == hi) continue; // skip the image being unloaded | |
2555 | ||
2556 | // Fix class refs in the other image | |
2557 | other_refs = _getObjcClassRefs(other_hi, &count); | |
2558 | rependClassReferences(other_refs, count, seg, seg+seg_size); | |
2559 | } | |
b3962a83 A |
2560 | } |
2561 | ||
2562 | ||
2563 | /*********************************************************************** | |
2564 | * _objc_remove_categories_in_image | |
2565 | * Remove all categories in the given image from the runtime, because | |
2566 | * the image is about to be unloaded. | |
2567 | * Things to clean up: | |
2568 | * unresolved category list | |
2569 | * loadable category list | |
2570 | **********************************************************************/ | |
2571 | static void _objc_remove_categories_in_image(header_info *hi) | |
2572 | { | |
2573 | Module mods; | |
2574 | unsigned int midx; | |
2575 | ||
2576 | // Major loop - process all modules in the header | |
2577 | mods = hi->mod_ptr; | |
2578 | ||
2579 | for (midx = 0; midx < hi->mod_count; midx++) { | |
2580 | unsigned int index; | |
2581 | unsigned int total; | |
2582 | Symtab symtab = mods[midx].symtab; | |
2583 | ||
2584 | // Nothing to do for a module without a symbol table | |
7257e56c | 2585 | if (symtab == nil) continue; |
b3962a83 A |
2586 | |
2587 | // Total entries in symbol table (class entries followed | |
2588 | // by category entries) | |
2589 | total = symtab->cls_def_cnt + symtab->cat_def_cnt; | |
2590 | ||
2591 | // Minor loop - check all categories from given module | |
2592 | for (index = symtab->cls_def_cnt; index < total; index++) { | |
7257e56c | 2593 | old_category *cat = (old_category *)symtab->defs[index]; |
b3962a83 A |
2594 | |
2595 | // Clean up loadable category list | |
2596 | remove_category_from_loadable_list((Category)cat); | |
2597 | ||
2598 | // Clean up category_hash | |
2599 | if (category_hash) { | |
7257e56c A |
2600 | _objc_unresolved_category *cat_entry = (_objc_unresolved_category *)NXMapGet(category_hash, cat->class_name); |
2601 | for ( ; cat_entry != nil; cat_entry = cat_entry->next) { | |
b3962a83 | 2602 | if (cat_entry->cat == cat) { |
7257e56c | 2603 | cat_entry->cat = nil; |
b3962a83 A |
2604 | break; |
2605 | } | |
2606 | } | |
2607 | } | |
2608 | } | |
2609 | } | |
2610 | } | |
2611 | ||
2612 | ||
2613 | /*********************************************************************** | |
2614 | * unload_paranoia | |
2615 | * Various paranoid debugging checks that look for poorly-behaving | |
2616 | * unloadable bundles. | |
2617 | * Called by _objc_unmap_image when OBJC_UNLOAD_DEBUG is set. | |
2618 | **********************************************************************/ | |
2619 | static void unload_paranoia(header_info *hi) | |
2620 | { | |
2621 | // Get the location of the dying image's __OBJC segment | |
8972963c A |
2622 | uintptr_t seg; |
2623 | unsigned long seg_size; | |
c1e772c4 | 2624 | seg = (uintptr_t)getsegmentdata(hi->mhdr(), "__OBJC", &seg_size); |
b3962a83 A |
2625 | |
2626 | _objc_inform("UNLOAD DEBUG: unloading image '%s' [%p..%p]", | |
c1e772c4 | 2627 | hi->fname(), (void *)seg, (void*)(seg+seg_size)); |
b3962a83 | 2628 | |
31875a97 | 2629 | mutex_locker_t lock(classLock); |
b3962a83 A |
2630 | |
2631 | // Make sure the image contains no categories on surviving classes. | |
2632 | { | |
2633 | Module mods; | |
2634 | unsigned int midx; | |
2635 | ||
2636 | // Major loop - process all modules in the header | |
2637 | mods = hi->mod_ptr; | |
2638 | ||
2639 | for (midx = 0; midx < hi->mod_count; midx++) { | |
2640 | unsigned int index; | |
2641 | unsigned int total; | |
2642 | Symtab symtab = mods[midx].symtab; | |
2643 | ||
2644 | // Nothing to do for a module without a symbol table | |
7257e56c | 2645 | if (symtab == nil) continue; |
b3962a83 A |
2646 | |
2647 | // Total entries in symbol table (class entries followed | |
2648 | // by category entries) | |
2649 | total = symtab->cls_def_cnt + symtab->cat_def_cnt; | |
2650 | ||
2651 | // Minor loop - check all categories from given module | |
2652 | for (index = symtab->cls_def_cnt; index < total; index++) { | |
7257e56c A |
2653 | old_category *cat = (old_category *)symtab->defs[index]; |
2654 | struct objc_class query; | |
b3962a83 A |
2655 | |
2656 | query.name = cat->class_name; | |
2657 | if (NXHashMember(class_hash, &query)) { | |
2658 | _objc_inform("UNLOAD DEBUG: dying image contains category '%s(%s)' on surviving class '%s'!", cat->class_name, cat->category_name, cat->class_name); | |
2659 | } | |
2660 | } | |
2661 | } | |
2662 | } | |
2663 | ||
2664 | // Make sure no surviving class is in the dying image. | |
2665 | // Make sure no surviving class has a superclass in the dying image. | |
2666 | // fixme check method implementations too | |
2667 | { | |
7257e56c | 2668 | Class cls; |
b3962a83 A |
2669 | NXHashState state; |
2670 | ||
2671 | state = NXInitHashState(class_hash); | |
2672 | while (NXNextHashState(class_hash, &state, (void **)&cls)) { | |
2673 | if ((vm_address_t)cls >= seg && | |
2674 | (vm_address_t)cls < seg+seg_size) | |
2675 | { | |
2676 | _objc_inform("UNLOAD DEBUG: dying image contains surviving class '%s'!", cls->name); | |
2677 | } | |
2678 | ||
7257e56c A |
2679 | if ((vm_address_t)cls->superclass >= seg && |
2680 | (vm_address_t)cls->superclass < seg+seg_size) | |
b3962a83 | 2681 | { |
7257e56c | 2682 | _objc_inform("UNLOAD DEBUG: dying image contains superclass '%s' of surviving class '%s'!", cls->superclass->name, cls->name); |
b3962a83 A |
2683 | } |
2684 | } | |
2685 | } | |
b3962a83 A |
2686 | } |
2687 | ||
2688 | ||
2689 | /*********************************************************************** | |
2690 | * _unload_image | |
2691 | * Only handles MH_BUNDLE for now. | |
7af964d1 | 2692 | * Locking: loadMethodLock acquired by unmap_image |
b3962a83 | 2693 | **********************************************************************/ |
cd5f04f5 | 2694 | void _unload_image(header_info *hi) |
b3962a83 | 2695 | { |
31875a97 | 2696 | loadMethodLock.assertLocked(); |
7af964d1 | 2697 | |
b3962a83 A |
2698 | // Cleanup: |
2699 | // Remove image's classes from the class list and free auxiliary data. | |
2700 | // Remove image's unresolved or loadable categories and free auxiliary data | |
2701 | // Remove image's unresolved class refs. | |
2702 | _objc_remove_classes_in_image(hi); | |
2703 | _objc_remove_categories_in_image(hi); | |
2704 | _objc_remove_pending_class_refs_in_image(hi); | |
c1e772c4 A |
2705 | if (hi->proto_refs) try_free(hi->proto_refs); |
2706 | ||
b3962a83 A |
2707 | // Perform various debugging checks if requested. |
2708 | if (DebugUnload) unload_paranoia(hi); | |
2709 | } | |
2710 | ||
7af964d1 A |
2711 | #endif |
2712 | ||
b3962a83 A |
2713 | |
2714 | /*********************************************************************** | |
2715 | * objc_addClass. Add the specified class to the table of known classes, | |
2716 | * after doing a little verification and fixup. | |
2717 | **********************************************************************/ | |
7257e56c | 2718 | void objc_addClass (Class cls) |
b3962a83 | 2719 | { |
b3962a83 A |
2720 | OBJC_WARN_DEPRECATED; |
2721 | ||
2722 | // Synchronize access to hash table | |
31875a97 | 2723 | mutex_locker_t lock(classLock); |
b3962a83 A |
2724 | |
2725 | // Make sure both the class and the metaclass have caches! | |
2726 | // Clear all bits of the info fields except CLS_CLASS and CLS_META. | |
2727 | // Normally these bits are already clear but if someone tries to cons | |
2728 | // up their own class on the fly they might need to be cleared. | |
7257e56c | 2729 | if (cls->cache == nil) { |
b3962a83 A |
2730 | cls->cache = (Cache) &_objc_empty_cache; |
2731 | cls->info = CLS_CLASS; | |
2732 | } | |
2733 | ||
7257e56c A |
2734 | if (cls->ISA()->cache == nil) { |
2735 | cls->ISA()->cache = (Cache) &_objc_empty_cache; | |
2736 | cls->ISA()->info = CLS_META; | |
b3962a83 A |
2737 | } |
2738 | ||
2739 | // methodLists should be: | |
7257e56c | 2740 | // 1. nil (Tiger and later only) |
b3962a83 A |
2741 | // 2. A -1 terminated method list array |
2742 | // In either case, CLS_NO_METHOD_ARRAY remains clear. | |
2743 | // If the user manipulates the method list directly, | |
2744 | // they must use the magic private format. | |
2745 | ||
2746 | // Add the class to the table | |
2747 | (void) NXHashInsert (class_hash, cls); | |
2748 | ||
2749 | // Superclass is no longer a leaf for cache flushing | |
7257e56c A |
2750 | if (cls->superclass && (cls->superclass->info & CLS_LEAF)) { |
2751 | cls->superclass->clearInfo(CLS_LEAF); | |
2752 | cls->superclass->ISA()->clearInfo(CLS_LEAF); | |
b3962a83 | 2753 | } |
b3962a83 A |
2754 | } |
2755 | ||
2756 | /*********************************************************************** | |
2757 | * _objcTweakMethodListPointerForClass. | |
2758 | * Change the class's method list pointer to a method list array. | |
2759 | * Does nothing if the method list pointer is already a method list array. | |
2760 | * If the class is currently in use, methodListLock must be held by the caller. | |
2761 | **********************************************************************/ | |
7257e56c | 2762 | static void _objcTweakMethodListPointerForClass(Class cls) |
b3962a83 | 2763 | { |
7257e56c | 2764 | old_method_list * originalList; |
b3962a83 A |
2765 | const int initialEntries = 4; |
2766 | size_t mallocSize; | |
7257e56c | 2767 | old_method_list ** ptr; |
b3962a83 A |
2768 | |
2769 | // Do nothing if methodLists is already an array. | |
2770 | if (cls->methodLists && !(cls->info & CLS_NO_METHOD_ARRAY)) return; | |
2771 | ||
2772 | // Remember existing list | |
7257e56c | 2773 | originalList = (old_method_list *) cls->methodLists; |
b3962a83 A |
2774 | |
2775 | // Allocate and zero a method list array | |
7257e56c | 2776 | mallocSize = sizeof(old_method_list *) * initialEntries; |
31875a97 | 2777 | ptr = (old_method_list **) calloc(1, mallocSize); |
b3962a83 A |
2778 | |
2779 | // Insert the existing list into the array | |
2780 | ptr[initialEntries - 1] = END_OF_METHODS_LIST; | |
2781 | ptr[0] = originalList; | |
2782 | ||
2783 | // Replace existing list with array | |
2784 | cls->methodLists = ptr; | |
7257e56c | 2785 | cls->clearInfo(CLS_NO_METHOD_ARRAY); |
b3962a83 A |
2786 | } |
2787 | ||
2788 | ||
2789 | /*********************************************************************** | |
2790 | * _objc_insertMethods. | |
2791 | * Adds methods to a class. | |
2792 | * Does not flush any method caches. | |
2793 | * Does not take any locks. | |
2794 | * If the class is already in use, use class_addMethods() instead. | |
2795 | **********************************************************************/ | |
7257e56c | 2796 | void _objc_insertMethods(Class cls, old_method_list *mlist, old_category *cat) |
b3962a83 | 2797 | { |
7257e56c A |
2798 | old_method_list ***list; |
2799 | old_method_list **ptr; | |
b3962a83 A |
2800 | ptrdiff_t endIndex; |
2801 | size_t oldSize; | |
2802 | size_t newSize; | |
2803 | ||
2804 | if (!cls->methodLists) { | |
2805 | // cls has no methods - simply use this method list | |
7257e56c A |
2806 | cls->methodLists = (old_method_list **)mlist; |
2807 | cls->setInfo(CLS_NO_METHOD_ARRAY); | |
b3962a83 A |
2808 | return; |
2809 | } | |
2810 | ||
2811 | // Log any existing methods being replaced | |
2812 | if (PrintReplacedMethods) { | |
2813 | int i; | |
2814 | for (i = 0; i < mlist->method_count; i++) { | |
7257e56c | 2815 | extern IMP findIMPInClass(Class cls, SEL sel); |
b3962a83 A |
2816 | SEL sel = sel_registerName((char *)mlist->method_list[i].method_name); |
2817 | IMP newImp = mlist->method_list[i].method_imp; | |
2818 | IMP oldImp; | |
2819 | ||
2820 | if ((oldImp = findIMPInClass(cls, sel))) { | |
7af964d1 | 2821 | logReplacedMethod(cls->name, sel, ISMETA(cls), |
7257e56c | 2822 | cat ? cat->category_name : nil, |
7af964d1 | 2823 | oldImp, newImp); |
b3962a83 A |
2824 | } |
2825 | } | |
2826 | } | |
2827 | ||
2828 | // Create method list array if necessary | |
2829 | _objcTweakMethodListPointerForClass(cls); | |
2830 | ||
2831 | list = &cls->methodLists; | |
2832 | ||
2833 | // Locate unused entry for insertion point | |
2834 | ptr = *list; | |
2835 | while ((*ptr != 0) && (*ptr != END_OF_METHODS_LIST)) | |
2836 | ptr += 1; | |
2837 | ||
2838 | // If array is full, add to it | |
2839 | if (*ptr == END_OF_METHODS_LIST) | |
2840 | { | |
2841 | // Calculate old and new dimensions | |
2842 | endIndex = ptr - *list; | |
2843 | oldSize = (endIndex + 1) * sizeof(void *); | |
7257e56c | 2844 | newSize = oldSize + sizeof(old_method_list *); // only increase by 1 |
b3962a83 A |
2845 | |
2846 | // Grow the method list array by one. | |
7257e56c | 2847 | *list = (old_method_list **)realloc(*list, newSize); |
b3962a83 A |
2848 | |
2849 | // Zero out addition part of new array | |
2850 | bzero (&((*list)[endIndex]), newSize - oldSize); | |
2851 | ||
2852 | // Place new end marker | |
2853 | (*list)[(newSize/sizeof(void *)) - 1] = END_OF_METHODS_LIST; | |
2854 | ||
2855 | // Insertion point corresponds to old array end | |
2856 | ptr = &((*list)[endIndex]); | |
2857 | } | |
2858 | ||
2859 | // Right shift existing entries by one | |
7af964d1 | 2860 | bcopy (*list, (*list) + 1, (uint8_t *)ptr - (uint8_t *)*list); |
b3962a83 A |
2861 | |
2862 | // Insert at method list at beginning of array | |
2863 | **list = mlist; | |
2864 | } | |
2865 | ||
2866 | /*********************************************************************** | |
2867 | * _objc_removeMethods. | |
2868 | * Remove methods from a class. | |
2869 | * Does not take any locks. | |
2870 | * Does not flush any method caches. | |
2871 | * If the class is currently in use, use class_removeMethods() instead. | |
2872 | **********************************************************************/ | |
7257e56c | 2873 | void _objc_removeMethods(Class cls, old_method_list *mlist) |
b3962a83 | 2874 | { |
7257e56c A |
2875 | old_method_list ***list; |
2876 | old_method_list **ptr; | |
b3962a83 | 2877 | |
7257e56c | 2878 | if (cls->methodLists == nil) { |
b3962a83 A |
2879 | // cls has no methods |
2880 | return; | |
2881 | } | |
7257e56c | 2882 | if (cls->methodLists == (old_method_list **)mlist) { |
b3962a83 | 2883 | // mlist is the class's only method list - erase it |
7257e56c | 2884 | cls->methodLists = nil; |
b3962a83 A |
2885 | return; |
2886 | } | |
2887 | if (cls->info & CLS_NO_METHOD_ARRAY) { | |
2888 | // cls has only one method list, and this isn't it - do nothing | |
2889 | return; | |
2890 | } | |
2891 | ||
2892 | // cls has a method list array - search it | |
2893 | ||
2894 | list = &cls->methodLists; | |
2895 | ||
2896 | // Locate list in the array | |
2897 | ptr = *list; | |
2898 | while (*ptr != mlist) { | |
2899 | // fix for radar # 2538790 | |
2900 | if ( *ptr == END_OF_METHODS_LIST ) return; | |
2901 | ptr += 1; | |
2902 | } | |
2903 | ||
2904 | // Remove this entry | |
2905 | *ptr = 0; | |
2906 | ||
2907 | // Left shift the following entries | |
2908 | while (*(++ptr) != END_OF_METHODS_LIST) | |
2909 | *(ptr-1) = *ptr; | |
2910 | *(ptr-1) = 0; | |
2911 | } | |
2912 | ||
2913 | /*********************************************************************** | |
2914 | * _objc_add_category. Install the specified category's methods and | |
2915 | * protocols into the class it augments. | |
2916 | * The class is assumed not to be in use yet: no locks are taken and | |
2917 | * no method caches are flushed. | |
2918 | **********************************************************************/ | |
7257e56c | 2919 | static inline void _objc_add_category(Class cls, old_category *category, int version) |
b3962a83 A |
2920 | { |
2921 | if (PrintConnecting) { | |
2922 | _objc_inform("CONNECT: attaching category '%s (%s)'", cls->name, category->category_name); | |
2923 | } | |
2924 | ||
2925 | // Augment instance methods | |
2926 | if (category->instance_methods) | |
2927 | _objc_insertMethods (cls, category->instance_methods, category); | |
2928 | ||
2929 | // Augment class methods | |
2930 | if (category->class_methods) | |
7257e56c | 2931 | _objc_insertMethods (cls->ISA(), category->class_methods, category); |
b3962a83 A |
2932 | |
2933 | // Augment protocols | |
2934 | if ((version >= 5) && category->protocols) | |
2935 | { | |
7257e56c | 2936 | if (cls->ISA()->version >= 5) |
b3962a83 A |
2937 | { |
2938 | category->protocols->next = cls->protocols; | |
2939 | cls->protocols = category->protocols; | |
7257e56c | 2940 | cls->ISA()->protocols = category->protocols; |
b3962a83 A |
2941 | } |
2942 | else | |
2943 | { | |
2944 | _objc_inform ("unable to add protocols from category %s...\n", category->category_name); | |
2945 | _objc_inform ("class `%s' must be recompiled\n", category->class_name); | |
2946 | } | |
2947 | } | |
2948 | ||
c1e772c4 | 2949 | // Augment instance properties |
b3962a83 | 2950 | if (version >= 7 && category->instance_properties) { |
7257e56c | 2951 | if (cls->ISA()->version >= 6) { |
b3962a83 A |
2952 | _class_addProperties(cls, category->instance_properties); |
2953 | } else { | |
c1e772c4 A |
2954 | _objc_inform ("unable to add instance properties from category %s...\n", category->category_name); |
2955 | _objc_inform ("class `%s' must be recompiled\n", category->class_name); | |
2956 | } | |
2957 | } | |
2958 | ||
2959 | // Augment class properties | |
2960 | if (version >= 7 && category->hasClassPropertiesField() && | |
2961 | category->class_properties) | |
2962 | { | |
2963 | if (cls->ISA()->version >= 6) { | |
2964 | _class_addProperties(cls->ISA(), category->class_properties); | |
2965 | } else { | |
2966 | _objc_inform ("unable to add class properties from category %s...\n", category->category_name); | |
b3962a83 A |
2967 | _objc_inform ("class `%s' must be recompiled\n", category->class_name); |
2968 | } | |
2969 | } | |
2970 | } | |
2971 | ||
2972 | /*********************************************************************** | |
2973 | * _objc_add_category_flush_caches. Install the specified category's | |
2974 | * methods into the class it augments, and flush the class' method cache. | |
2975 | * Return YES if some method caches now need to be flushed. | |
2976 | **********************************************************************/ | |
31875a97 | 2977 | static bool _objc_add_category_flush_caches(Class cls, old_category *category, int version) |
b3962a83 | 2978 | { |
31875a97 | 2979 | bool needFlush = NO; |
b3962a83 A |
2980 | |
2981 | // Install the category's methods into its intended class | |
31875a97 A |
2982 | { |
2983 | mutex_locker_t lock(methodListLock); | |
2984 | _objc_add_category (cls, category, version); | |
2985 | } | |
b3962a83 A |
2986 | |
2987 | // Queue for cache flushing so category's methods can get called | |
2988 | if (category->instance_methods) { | |
7257e56c | 2989 | cls->setInfo(CLS_FLUSH_CACHE); |
b3962a83 A |
2990 | needFlush = YES; |
2991 | } | |
2992 | if (category->class_methods) { | |
7257e56c | 2993 | cls->ISA()->setInfo(CLS_FLUSH_CACHE); |
b3962a83 A |
2994 | needFlush = YES; |
2995 | } | |
2996 | ||
2997 | return needFlush; | |
2998 | } | |
2999 | ||
3000 | ||
3001 | /*********************************************************************** | |
3002 | * reverse_cat | |
3003 | * Reverse the given linked list of pending categories. | |
3004 | * The pending category list is built backwards, and needs to be | |
3005 | * reversed before actually attaching the categories to a class. | |
3006 | * Returns the head of the new linked list. | |
3007 | **********************************************************************/ | |
3008 | static _objc_unresolved_category *reverse_cat(_objc_unresolved_category *cat) | |
3009 | { | |
7af964d1 A |
3010 | _objc_unresolved_category *prev; |
3011 | _objc_unresolved_category *cur; | |
3012 | _objc_unresolved_category *ahead; | |
3013 | ||
7257e56c | 3014 | if (!cat) return nil; |
b3962a83 | 3015 | |
7257e56c | 3016 | prev = nil; |
7af964d1 A |
3017 | cur = cat; |
3018 | ahead = cat->next; | |
b3962a83 A |
3019 | |
3020 | while (cur) { | |
3021 | ahead = cur->next; | |
3022 | cur->next = prev; | |
3023 | prev = cur; | |
3024 | cur = ahead; | |
3025 | } | |
3026 | ||
3027 | return prev; | |
3028 | } | |
3029 | ||
3030 | ||
3031 | /*********************************************************************** | |
3032 | * resolve_categories_for_class. | |
3033 | * Install all existing categories intended for the specified class. | |
3034 | * cls must be a true class and not a metaclass. | |
3035 | **********************************************************************/ | |
7257e56c | 3036 | static void resolve_categories_for_class(Class cls) |
b3962a83 A |
3037 | { |
3038 | _objc_unresolved_category * pending; | |
3039 | _objc_unresolved_category * next; | |
3040 | ||
3041 | // Nothing to do if there are no categories at all | |
3042 | if (!category_hash) return; | |
3043 | ||
3044 | // Locate and remove first element in category list | |
3045 | // associated with this class | |
7257e56c A |
3046 | pending = (_objc_unresolved_category *) |
3047 | NXMapKeyFreeingRemove (category_hash, cls->name); | |
b3962a83 A |
3048 | |
3049 | // Traverse the list of categories, if any, registered for this class | |
3050 | ||
3051 | // The pending list is built backwards. Reverse it and walk forwards. | |
3052 | pending = reverse_cat(pending); | |
3053 | ||
3054 | while (pending) { | |
3055 | if (pending->cat) { | |
3056 | // Install the category | |
3057 | // use the non-flush-cache version since we are only | |
3058 | // called from the class intialization code | |
3059 | _objc_add_category(cls, pending->cat, (int)pending->version); | |
3060 | } | |
3061 | ||
3062 | // Delink and reclaim this registration | |
3063 | next = pending->next; | |
31875a97 | 3064 | free(pending); |
b3962a83 A |
3065 | pending = next; |
3066 | } | |
3067 | } | |
3068 | ||
3069 | ||
3070 | /*********************************************************************** | |
3071 | * _objc_resolve_categories_for_class. | |
3072 | * Public version of resolve_categories_for_class. This was | |
3073 | * exported pre-10.4 for Omni et al. to workaround a problem | |
3074 | * with too-lazy category attachment. | |
3075 | * cls should be a class, but this function can also cope with metaclasses. | |
3076 | **********************************************************************/ | |
7257e56c | 3077 | void _objc_resolve_categories_for_class(Class cls) |
b3962a83 | 3078 | { |
b3962a83 A |
3079 | // If cls is a metaclass, get the class. |
3080 | // resolve_categories_for_class() requires a real class to work correctly. | |
3081 | if (ISMETA(cls)) { | |
3082 | if (strncmp(cls->name, "_%", 2) == 0) { | |
3083 | // Posee's meta's name is smashed and isn't in the class_hash, | |
3084 | // so objc_getClass doesn't work. | |
7af964d1 | 3085 | const char *baseName = strchr(cls->name, '%'); // get posee's real name |
7257e56c | 3086 | cls = objc_getClass(baseName); |
b3962a83 | 3087 | } else { |
7257e56c | 3088 | cls = objc_getClass(cls->name); |
b3962a83 A |
3089 | } |
3090 | } | |
3091 | ||
3092 | resolve_categories_for_class(cls); | |
3093 | } | |
3094 | ||
3095 | ||
3096 | /*********************************************************************** | |
3097 | * _objc_register_category. | |
3098 | * Process a category read from an image. | |
3099 | * If the category's class exists, attach the category immediately. | |
3100 | * Classes that need cache flushing are marked but not flushed. | |
3101 | * If the category's class does not exist yet, pend the category for | |
3102 | * later attachment. Pending categories are attached in the order | |
3103 | * they were discovered. | |
3104 | * Returns YES if some method caches now need to be flushed. | |
3105 | **********************************************************************/ | |
31875a97 | 3106 | static bool _objc_register_category(old_category *cat, int version) |
b3962a83 A |
3107 | { |
3108 | _objc_unresolved_category * new_cat; | |
3109 | _objc_unresolved_category * old; | |
7257e56c | 3110 | Class theClass; |
b3962a83 A |
3111 | |
3112 | // If the category's class exists, attach the category. | |
7257e56c | 3113 | if ((theClass = objc_lookUpClass(cat->class_name))) { |
b3962a83 A |
3114 | return _objc_add_category_flush_caches(theClass, cat, version); |
3115 | } | |
3116 | ||
3117 | // If the category's class exists but is unconnected, | |
3118 | // then attach the category to the class but don't bother | |
3119 | // flushing any method caches (because they must be empty). | |
3120 | // YES unconnected, NO class_handler | |
7257e56c | 3121 | if ((theClass = look_up_class(cat->class_name, YES, NO))) { |
b3962a83 A |
3122 | _objc_add_category(theClass, cat, version); |
3123 | return NO; | |
3124 | } | |
3125 | ||
3126 | ||
3127 | // Category's class does not exist yet. | |
3128 | // Save the category for later attachment. | |
3129 | ||
3130 | if (PrintConnecting) { | |
3131 | _objc_inform("CONNECT: pending category '%s (%s)'", cat->class_name, cat->category_name); | |
3132 | } | |
3133 | ||
3134 | // Create category lookup table if needed | |
3135 | if (!category_hash) | |
31875a97 | 3136 | category_hash = NXCreateMapTable(NXStrValueMapPrototype, 128); |
b3962a83 A |
3137 | |
3138 | // Locate an existing list of categories, if any, for the class. | |
7257e56c A |
3139 | old = (_objc_unresolved_category *) |
3140 | NXMapGet (category_hash, cat->class_name); | |
b3962a83 A |
3141 | |
3142 | // Register the category to be fixed up later. | |
3143 | // The category list is built backwards, and is reversed again | |
3144 | // by resolve_categories_for_class(). | |
7257e56c | 3145 | new_cat = (_objc_unresolved_category *) |
31875a97 | 3146 | malloc(sizeof(_objc_unresolved_category)); |
b3962a83 A |
3147 | new_cat->next = old; |
3148 | new_cat->cat = cat; | |
3149 | new_cat->version = version; | |
3150 | (void) NXMapKeyCopyingInsert (category_hash, cat->class_name, new_cat); | |
3151 | ||
3152 | return NO; | |
3153 | } | |
3154 | ||
3155 | ||
66799735 A |
3156 | const char **objc_copyImageNames(unsigned int *outCount) |
3157 | { | |
3158 | header_info *hi; | |
3159 | int count = 0; | |
1807f628 A |
3160 | int max = 0; |
3161 | for (hi = FirstHeader; hi != nil; hi = hi->getNext()) { | |
3162 | max++; | |
3163 | } | |
66799735 A |
3164 | #if TARGET_OS_WIN32 |
3165 | const TCHAR **names = (const TCHAR **)calloc(max+1, sizeof(TCHAR *)); | |
3166 | #else | |
3167 | const char **names = (const char **)calloc(max+1, sizeof(char *)); | |
3168 | #endif | |
3169 | ||
3170 | for (hi = FirstHeader; hi != NULL && count < max; hi = hi->getNext()) { | |
3171 | #if TARGET_OS_WIN32 | |
3172 | if (hi->moduleName) { | |
3173 | names[count++] = hi->moduleName; | |
3174 | } | |
3175 | #else | |
3176 | const char *fname = hi->fname(); | |
3177 | if (fname) { | |
3178 | names[count++] = fname; | |
3179 | } | |
3180 | #endif | |
3181 | } | |
3182 | names[count] = NULL; | |
3183 | ||
3184 | if (count == 0) { | |
3185 | // Return NULL instead of empty list if there are no images | |
3186 | free((void *)names); | |
3187 | names = NULL; | |
3188 | } | |
3189 | ||
3190 | if (outCount) *outCount = count; | |
3191 | return names; | |
3192 | } | |
3193 | ||
3194 | ||
3195 | static const char ** | |
b3962a83 A |
3196 | _objc_copyClassNamesForImage(header_info *hi, unsigned int *outCount) |
3197 | { | |
3198 | Module mods; | |
7af964d1 | 3199 | unsigned int m; |
b3962a83 A |
3200 | const char **list; |
3201 | int count; | |
3202 | int allocated; | |
3203 | ||
7257e56c | 3204 | list = nil; |
b3962a83 A |
3205 | count = 0; |
3206 | allocated = 0; | |
3207 | ||
3208 | mods = hi->mod_ptr; | |
3209 | for (m = 0; m < hi->mod_count; m++) { | |
3210 | int d; | |
3211 | ||
3212 | if (!mods[m].symtab) continue; | |
3213 | ||
3214 | for (d = 0; d < mods[m].symtab->cls_def_cnt; d++) { | |
7257e56c | 3215 | Class cls = (Class)mods[m].symtab->defs[d]; |
b3962a83 | 3216 | // fixme what about future-ified classes? |
7257e56c | 3217 | if (cls->isConnected()) { |
b3962a83 A |
3218 | if (count == allocated) { |
3219 | allocated = allocated*2 + 16; | |
7af964d1 A |
3220 | list = (const char **) |
3221 | realloc((void *)list, allocated * sizeof(char *)); | |
b3962a83 A |
3222 | } |
3223 | list[count++] = cls->name; | |
3224 | } | |
3225 | } | |
3226 | } | |
3227 | ||
3228 | if (count > 0) { | |
7257e56c | 3229 | // nil-terminate non-empty list |
b3962a83 A |
3230 | if (count == allocated) { |
3231 | allocated = allocated+1; | |
7af964d1 A |
3232 | list = (const char **) |
3233 | realloc((void *)list, allocated * sizeof(char *)); | |
b3962a83 | 3234 | } |
7257e56c | 3235 | list[count] = nil; |
b3962a83 A |
3236 | } |
3237 | ||
3238 | if (outCount) *outCount = count; | |
3239 | return list; | |
3240 | } | |
3241 | ||
66799735 A |
3242 | |
3243 | /********************************************************************** | |
3244 | * | |
3245 | **********************************************************************/ | |
3246 | const char ** | |
3247 | objc_copyClassNamesForImage(const char *image, unsigned int *outCount) | |
3248 | { | |
3249 | header_info *hi; | |
3250 | ||
3251 | if (!image) { | |
3252 | if (outCount) *outCount = 0; | |
3253 | return NULL; | |
3254 | } | |
3255 | ||
3256 | // Find the image. | |
3257 | for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) { | |
3258 | #if TARGET_OS_WIN32 | |
3259 | if (0 == wcscmp((TCHAR *)image, hi->moduleName)) break; | |
3260 | #else | |
3261 | if (0 == strcmp(image, hi->fname())) break; | |
3262 | #endif | |
3263 | } | |
3264 | ||
3265 | if (!hi) { | |
3266 | if (outCount) *outCount = 0; | |
3267 | return NULL; | |
3268 | } | |
3269 | ||
3270 | return _objc_copyClassNamesForImage(hi, outCount); | |
3271 | } | |
3272 | ||
3273 | ||
3274 | ||
3275 | /********************************************************************** | |
3276 | * | |
3277 | **********************************************************************/ | |
3278 | const char ** | |
3279 | objc_copyClassNamesForImageHeader(const struct mach_header *mh, unsigned int *outCount) | |
3280 | { | |
3281 | header_info *hi; | |
3282 | ||
3283 | if (!mh) { | |
3284 | if (outCount) *outCount = 0; | |
3285 | return NULL; | |
3286 | } | |
3287 | ||
3288 | // Find the image. | |
3289 | for (hi = FirstHeader; hi != NULL; hi = hi->getNext()) { | |
3290 | if (hi->mhdr() == (const headerType *)mh) break; | |
3291 | } | |
3292 | ||
3293 | if (!hi) { | |
3294 | if (outCount) *outCount = 0; | |
3295 | return NULL; | |
3296 | } | |
3297 | ||
3298 | return _objc_copyClassNamesForImage(hi, outCount); | |
3299 | } | |
3300 | ||
3301 | ||
8972963c A |
3302 | Class gdb_class_getClass(Class cls) |
3303 | { | |
3304 | const char *className = cls->name; | |
3305 | if(!className || !strlen(className)) return Nil; | |
3306 | Class rCls = look_up_class(className, NO, NO); | |
3307 | return rCls; | |
3308 | ||
3309 | } | |
3310 | ||
3311 | Class gdb_object_getClass(id obj) | |
3312 | { | |
7257e56c A |
3313 | if (!obj) return nil; |
3314 | return gdb_class_getClass(obj->getIsa()); | |
7af964d1 A |
3315 | } |
3316 | ||
3317 | ||
66799735 A |
3318 | /*********************************************************************** |
3319 | * objc_setMultithreaded. | |
3320 | **********************************************************************/ | |
3321 | void objc_setMultithreaded (BOOL flag) | |
3322 | { | |
3323 | OBJC_WARN_DEPRECATED; | |
3324 | ||
3325 | // Nothing here. Thread synchronization in the runtime is always active. | |
3326 | } | |
3327 | ||
3328 | ||
7af964d1 A |
3329 | /*********************************************************************** |
3330 | * Lock management | |
7af964d1 | 3331 | **********************************************************************/ |
66799735 | 3332 | mutex_t selLock; |
31875a97 A |
3333 | mutex_t classLock; |
3334 | mutex_t methodListLock; | |
3335 | mutex_t cacheUpdateLock; | |
3336 | recursive_mutex_t loadMethodLock; | |
7af964d1 | 3337 | |
1807f628 | 3338 | void runtime_init(void) |
7af964d1 | 3339 | { |
7af964d1 A |
3340 | } |
3341 | ||
3342 | ||
b3962a83 | 3343 | #endif |