]> git.ipfire.org Git - thirdparty/gcc.git/blame - libobjc/init.c
Remove freebsd1 from libtool.m4 macros and config.rpath.
[thirdparty/gcc.git] / libobjc / init.c
CommitLineData
88e17b57 1/* GNU Objective C Runtime initialization
fed2b101 2 Copyright (C) 1993, 1995, 1996, 1997, 2002, 2009, 2010
748086b7 3 Free Software Foundation, Inc.
88e17b57
BE
4 Contributed by Kresten Krab Thorup
5 +load support contributed by Ovidiu Predescu <ovidiu@net-community.com>
6
38709cad 7This file is part of GCC.
88e17b57 8
38709cad 9GCC is free software; you can redistribute it and/or modify it under the
88e17b57 10terms of the GNU General Public License as published by the Free Software
748086b7 11Foundation; either version 3, or (at your option) any later version.
88e17b57 12
38709cad 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
88e17b57
BE
14WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16details.
17
748086b7
JJ
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
88e17b57 21
748086b7
JJ
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. */
88e17b57 26
e99776d8
NP
27/* Uncommented the following line to enable debug logging. Use this
28 only while debugging the runtime. */
29/* #define DEBUG 1 */
30
6dead247 31#include "objc-private/common.h"
7b869986 32#include "objc-private/error.h"
fed2b101 33#include "objc/runtime.h"
a19fac96 34#include "objc/thr.h"
5be9cdc1
NP
35#include "objc-private/hash.h"
36#include "objc-private/objc-list.h"
fed2b101 37#include "objc-private/module-abi-8.h"
41720477 38#include "objc-private/runtime.h" /* For __objc_resolve_class_links(). */
1af5b8f5 39#include "objc-private/selector.h" /* For __sel_register_typed_name(). */
fd312537 40#include "objc-private/objc-sync.h" /* For __objc_sync_init() */
f7185d47
NP
41#include "objc-private/protocols.h" /* For __objc_protocols_init(),
42 __objc_protocols_add_protocol()
43 __objc_protocols_register_selectors() */
682e805a 44#include "objc-private/accessors.h" /* For __objc_accessors_init() */
88e17b57 45
575584a9 46/* The version number of this runtime. This must match the number
40165636 47 defined in gcc (objc-act.c). */
88e17b57
BE
48#define OBJC_VERSION 8
49#define PROTOCOL_VERSION 2
50
c07499dc 51/* This list contains modules currently loaded into the runtime and
1588200e
NP
52 for which the +load method (and the load callback, if any) has not
53 been called yet. */
40165636 54static struct objc_list *__objc_module_list = 0; /* !T:MUTEX */
88e17b57 55
575584a9
NP
56/* This list contains all proto_list's not yet assigned class
57 links. */
40165636 58static struct objc_list *unclaimed_proto_list = 0; /* !T:MUTEX */
88e17b57
BE
59
60/* List of unresolved static instances. */
61static struct objc_list *uninitialized_statics = 0; /* !T:MUTEX */
62
0bfe3fa1
NP
63/* List of duplicated classes found while loading modules. If we find
64 a class twice, we ignore it the second time. On some platforms,
65 where the order in which modules are loaded is well defined, this
66 allows you to replace a class in a shared library by linking in a
67 new implementation which is loaded in in the right order, and which
68 overrides the existing one.
69
70 Protected by __objc_runtime_mutex. */
71static cache_ptr duplicate_classes = NULL;
72
debfbfee
NP
73/* Global runtime "write" mutex. Having a single mutex prevents
74 deadlocks, but reduces concurrency. To improve concurrency, some
75 groups of functions in the runtime have their own separate mutex
76 (eg, __class_table_lock in class.c); to avoid deadlocks, these
77 routines must make sure that they never acquire any other lock
78 while holding their own local lock. Ie, they should lock, execute
79 some C code that does not perform any calls to other runtime
80 functions which may potentially lock different locks, then unlock.
81 If they need to perform any calls to other runtime functions that
82 may potentially lock other locks, then they should use the global
83 __objc_runtime_mutex. */
88e17b57
BE
84objc_mutex_t __objc_runtime_mutex = 0;
85
40165636 86/* Number of threads that are alive. */
88e17b57
BE
87int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
88
40165636 89/* Check compiler vs runtime version. */
fed2b101 90static void init_check_module_version (struct objc_module *);
88e17b57 91
40165636
RB
92/* Assign isa links to protos. */
93static void __objc_init_protocols (struct objc_protocol_list *protos);
88e17b57 94
f7185d47
NP
95/* Assign isa link to a protocol, and register it. */
96static void __objc_init_protocol (struct objc_protocol *protocol);
97
40165636
RB
98/* Add protocol to class. */
99static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
88e17b57 100
120d5f8e 101/* Load callback hook. */
1588200e 102void (*_objc_load_callback) (Class class, struct objc_category *category) = 0; /* !T:SAFE */
88e17b57 103
0bfe3fa1 104/* Are all categories/classes resolved ? */
88e17b57
BE
105BOOL __objc_dangling_categories = NO; /* !T:UNUSED */
106
575584a9
NP
107/* Sends +load to all classes and categories in certain
108 situations. */
88e17b57
BE
109static void objc_send_load (void);
110
111/* Inserts all the classes defined in module in a tree of classes that
fa539f51 112 resembles the class hierarchy. This tree is traversed in preorder
40165636 113 and the classes in its nodes receive the +load message if these
fa539f51 114 methods were not executed before. The algorithm ensures that when
40165636
RB
115 the +load method of a class is executed all the superclasses have
116 been already received the +load message. */
fed2b101 117static void __objc_create_classes_tree (struct objc_module *module);
88e17b57 118
1588200e
NP
119/* Calls the _objc_load_callback for each class and category in the
120 module (if _objc_load_callback is not NULL). */
121static void __objc_call_load_callback (struct objc_module *module);
88e17b57
BE
122
123/* A special version that works only before the classes are completely
40165636 124 installed in the runtime. */
88e17b57
BE
125static BOOL class_is_subclass_of_class (Class class, Class superclass);
126
fa539f51
NP
127/* This is a node in the class tree hierarchy used to send +load
128 messages. */
0bfe3fa1
NP
129typedef struct objc_class_tree
130{
fa539f51 131 /* The class corresponding to the node. */
88e17b57 132 Class class;
fa539f51
NP
133
134 /* This is a linked list of all the direct subclasses of this class.
135 'head' points to a subclass node; 'tail' points to the next
136 objc_list node (whose 'head' points to another subclass node,
137 etc). */
138 struct objc_list *subclasses;
88e17b57
BE
139} objc_class_tree;
140
fa539f51
NP
141/* This is a linked list of objc_class_tree trees. The head of these
142 trees are root classes (their super class is Nil). These different
40165636 143 trees represent different class hierarchies. */
88e17b57
BE
144static struct objc_list *__objc_class_tree_list = NULL;
145
40165636
RB
146/* Keeps the +load methods who have been already executed. This hash
147 should not be destroyed during the execution of the program. */
88e17b57
BE
148static cache_ptr __objc_load_methods = NULL;
149
e083f3f9
RFM
150/* This function is used when building the class tree used to send
151 ordinately the +load message to all classes needing it. The tree
152 is really needed so that superclasses will get the message before
153 subclasses.
154
fa539f51 155 This tree may contain classes which are being loaded (or have just
e083f3f9
RFM
156 being loaded), and whose super_class pointers have not yet been
157 resolved. This implies that their super_class pointers point to a
158 string with the name of the superclass; when the first message is
159 sent to the class (/an object of that class) the class links will
160 be resolved, which will replace the super_class pointers with
161 pointers to the actual superclasses.
162
163 Unfortunately, the tree might also contain classes which had been
164 loaded previously, and whose class links have already been
165 resolved.
166
167 This function returns the superclass of a class in both cases, and
168 can be used to build the determine the class relationships while
575584a9 169 building the tree. */
e083f3f9
RFM
170static Class class_superclass_of_class (Class class)
171{
172 char *super_class_name;
173
174 /* If the class links have been resolved, use the resolved
575584a9 175 links. */
e083f3f9
RFM
176 if (CLS_ISRESOLV (class))
177 return class->super_class;
178
179 /* Else, 'class' has not yet been resolved. This means that its
575584a9
NP
180 super_class pointer is really the name of the super class (rather
181 than a pointer to the actual superclass). */
e083f3f9
RFM
182 super_class_name = (char *)class->super_class;
183
184 /* Return Nil for a root class. */
185 if (super_class_name == NULL)
186 return Nil;
187
188 /* Lookup the superclass of non-root classes. */
fed2b101 189 return objc_getClass (super_class_name);
e083f3f9
RFM
190}
191
192
40165636 193/* Creates a tree of classes whose topmost class is directly inherited
fa539f51
NP
194 from `upper' and the bottom class in this tree is `bottom_class'.
195 If `upper' is Nil, creates a class hierarchy up to a root class.
196 The classes in this tree are super classes of `bottom_class'. The
197 `subclasses' member of each tree node point to the list of
198 subclasses for the node. */
88e17b57
BE
199static objc_class_tree *
200create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
201{
fed2b101 202 Class superclass;
88e17b57
BE
203 objc_class_tree *tree, *prev;
204
205 DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
e99776d8 206 DEBUG_PRINTF (" bottom_class = %s, upper = %s\n",
88e17b57
BE
207 (bottom_class ? bottom_class->name : NULL),
208 (upper ? upper->name : NULL));
209
fa539f51
NP
210 superclass = class_superclass_of_class (bottom_class);
211
212 prev = objc_calloc (1, sizeof (objc_class_tree));
88e17b57
BE
213 prev->class = bottom_class;
214
fa539f51
NP
215 if (superclass == upper)
216 return prev;
217
88e17b57
BE
218 while (superclass != upper)
219 {
220 tree = objc_calloc (1, sizeof (objc_class_tree));
221 tree->class = superclass;
222 tree->subclasses = list_cons (prev, tree->subclasses);
e083f3f9 223 superclass = class_superclass_of_class (superclass);
88e17b57
BE
224 prev = tree;
225 }
226
227 return tree;
228}
229
40165636 230/* Insert the `class' into the proper place in the `tree' class
fa539f51 231 hierarchy. This function returns a new tree if the class has been
40165636 232 successfully inserted into the tree or NULL if the class is not
fa539f51
NP
233 part of the classes hierarchy described by `tree'. This function
234 is private to objc_tree_insert_class (), you should not call it
40165636 235 directly. */
88e17b57
BE
236static objc_class_tree *
237__objc_tree_insert_class (objc_class_tree *tree, Class class)
238{
fa539f51
NP
239 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %p (root: %s), class = %s\n",
240 tree, ((tree && tree->class) ? tree->class->name : "Nil"), class->name);
88e17b57
BE
241
242 if (tree == NULL)
243 return create_tree_of_subclasses_inherited_from (class, NULL);
244 else if (class == tree->class)
245 {
575584a9 246 /* `class' has been already inserted. */
e99776d8 247 DEBUG_PRINTF (" 1. class %s was previously inserted\n", class->name);
88e17b57
BE
248 return tree;
249 }
e083f3f9 250 else if (class_superclass_of_class (class) == tree->class)
88e17b57 251 {
575584a9
NP
252 /* If class is a direct subclass of tree->class then add class
253 to the list of subclasses. First check to see if it wasn't
254 already inserted. */
88e17b57
BE
255 struct objc_list *list = tree->subclasses;
256 objc_class_tree *node;
257
258 while (list)
259 {
260 /* Class has been already inserted; do nothing just return
40165636
RB
261 the tree. */
262 if (((objc_class_tree *) list->head)->class == class)
88e17b57 263 {
e99776d8 264 DEBUG_PRINTF (" 2. class %s was previously inserted\n",
88e17b57
BE
265 class->name);
266 return tree;
267 }
268 list = list->tail;
269 }
270
575584a9
NP
271 /* Create a new node class and insert it into the list of
272 subclasses. */
88e17b57
BE
273 node = objc_calloc (1, sizeof (objc_class_tree));
274 node->class = class;
275 tree->subclasses = list_cons (node, tree->subclasses);
e99776d8 276 DEBUG_PRINTF (" 3. class %s inserted\n", class->name);
88e17b57
BE
277 return tree;
278 }
279 else
280 {
575584a9
NP
281 /* The class is not a direct subclass of tree->class. Search
282 for class's superclasses in the list of subclasses. */
88e17b57
BE
283 struct objc_list *subclasses = tree->subclasses;
284
40165636
RB
285 /* Precondition: the class must be a subclass of tree->class;
286 otherwise return NULL to indicate our caller that it must
287 take the next tree. */
288 if (! class_is_subclass_of_class (class, tree->class))
88e17b57
BE
289 return NULL;
290
291 for (; subclasses != NULL; subclasses = subclasses->tail)
292 {
40165636 293 Class aClass = ((objc_class_tree *) (subclasses->head))->class;
88e17b57
BE
294
295 if (class_is_subclass_of_class (class, aClass))
296 {
40165636
RB
297 /* If we found one of class's superclasses we insert the
298 class into its subtree and return the original tree
299 since nothing has been changed. */
88e17b57
BE
300 subclasses->head
301 = __objc_tree_insert_class (subclasses->head, class);
e99776d8 302 DEBUG_PRINTF (" 4. class %s inserted\n", class->name);
88e17b57
BE
303 return tree;
304 }
305 }
306
40165636
RB
307 /* We haven't found a subclass of `class' in the `subclasses'
308 list. Create a new tree of classes whose topmost class is a
309 direct subclass of tree->class. */
88e17b57
BE
310 {
311 objc_class_tree *new_tree
40165636 312 = create_tree_of_subclasses_inherited_from (class, tree->class);
88e17b57 313 tree->subclasses = list_cons (new_tree, tree->subclasses);
e99776d8 314 DEBUG_PRINTF (" 5. class %s inserted\n", class->name);
88e17b57
BE
315 return tree;
316 }
317 }
318}
319
40165636 320/* This function inserts `class' in the right tree hierarchy classes. */
88e17b57
BE
321static void
322objc_tree_insert_class (Class class)
323{
324 struct objc_list *list_node;
325 objc_class_tree *tree;
fa539f51 326
88e17b57
BE
327 list_node = __objc_class_tree_list;
328 while (list_node)
329 {
fa539f51 330 /* Try to insert the class in this class hierarchy. */
88e17b57
BE
331 tree = __objc_tree_insert_class (list_node->head, class);
332 if (tree)
333 {
334 list_node->head = tree;
fa539f51 335 return;
88e17b57
BE
336 }
337 else
338 list_node = list_node->tail;
339 }
fa539f51
NP
340
341 /* If the list was finished but the class hasn't been inserted, we
342 don't have an existing class hierarchy that can accomodate it.
343 Create a new one. */
344 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
345 __objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
88e17b57
BE
346}
347
40165636 348/* Traverse tree in preorder. Used to send +load. */
88e17b57
BE
349static void
350objc_preorder_traverse (objc_class_tree *tree,
351 int level,
40165636 352 void (*function) (objc_class_tree *, int))
88e17b57
BE
353{
354 struct objc_list *node;
355
356 (*function) (tree, level);
357 for (node = tree->subclasses; node; node = node->tail)
358 objc_preorder_traverse (node->head, level + 1, function);
359}
360
40165636 361/* Traverse tree in postorder. Used to destroy a tree. */
88e17b57
BE
362static void
363objc_postorder_traverse (objc_class_tree *tree,
40165636
RB
364 int level,
365 void (*function) (objc_class_tree *, int))
88e17b57
BE
366{
367 struct objc_list *node;
368
369 for (node = tree->subclasses; node; node = node->tail)
370 objc_postorder_traverse (node->head, level + 1, function);
371 (*function) (tree, level);
372}
373
40165636 374/* Used to print a tree class hierarchy. */
88e17b57
BE
375#ifdef DEBUG
376static void
377__objc_tree_print (objc_class_tree *tree, int level)
378{
379 int i;
380
381 for (i = 0; i < level; i++)
382 printf (" ");
383 printf ("%s\n", tree->class->name);
384}
385#endif
386
40165636 387/* Walks on a linked list of methods in the reverse order and executes
c07499dc
NP
388 all the methods corresponding to the `+load' selector. Walking in
389 the reverse order assures the +load of class is executed first and
390 then +load of categories because of the way in which categories are
391 added to the class methods. This function needs to be called with
392 the objc_runtime_mutex locked. */
88e17b57 393static void
c07499dc 394__objc_send_load_using_method_list (struct objc_method_list *method_list, Class class)
88e17b57 395{
c07499dc 396 static SEL load_selector = 0;
88e17b57
BE
397 int i;
398
c07499dc 399 if (!method_list)
88e17b57
BE
400 return;
401
c07499dc
NP
402 /* This needs no lock protection because we are called with the
403 objc_runtime_mutex locked. */
404 if (!load_selector)
405 load_selector = sel_registerName ("load");
406
407 /* method_list is a linked list of method lists; since we're
408 executing in reverse order, we need to do the next list before we
409 do this one. */
410 __objc_send_load_using_method_list (method_list->method_next, class);
88e17b57 411
40165636 412 /* Search the method list. */
88e17b57
BE
413 for (i = 0; i < method_list->method_count; i++)
414 {
fed2b101 415 struct objc_method *mth = &method_list->method_list[i];
88e17b57 416
c07499dc
NP
417 /* We are searching for +load methods that we haven't executed
418 yet. */
419 if (mth->method_name && sel_eq (mth->method_name, load_selector)
270a1283 420 && ! objc_hash_is_key_in_hash (__objc_load_methods, mth->method_imp))
88e17b57 421 {
c07499dc
NP
422 /* Add this method into the +load hash table, so we won't
423 execute it again next time. */
270a1283
DA
424 objc_hash_add (&__objc_load_methods,
425 mth->method_imp,
426 mth->method_imp);
575584a9 427
c07499dc 428 /* Call +load. */
e99776d8 429 DEBUG_PRINTF (" begin of [%s +load]\n", class->name);
e6be21fe 430 (*mth->method_imp) ((id)class, mth->method_name);
e99776d8 431 DEBUG_PRINTF (" end of [%s +load]\n", class->name);
e6be21fe 432
88e17b57
BE
433 break;
434 }
435 }
436}
437
c07499dc
NP
438/* This function needs to be called with the objc_runtime_mutex
439 locked. */
88e17b57 440static void
8f8c44cb
KG
441__objc_send_load (objc_class_tree *tree,
442 int level __attribute__ ((__unused__)))
88e17b57 443{
88e17b57 444 Class class = tree->class;
fed2b101 445 struct objc_method_list *method_list = class->class_pointer->methods;
88e17b57 446
e99776d8 447 DEBUG_PRINTF ("+load: need to send load to class '%s'\n", class->name);
c07499dc 448 __objc_send_load_using_method_list (method_list, class);
88e17b57
BE
449}
450
451static void
8f8c44cb
KG
452__objc_destroy_class_tree_node (objc_class_tree *tree,
453 int level __attribute__ ((__unused__)))
88e17b57
BE
454{
455 objc_free (tree);
456}
457
40165636
RB
458/* This is used to check if the relationship between two classes
459 before the runtime completely installs the classes. */
88e17b57
BE
460static BOOL
461class_is_subclass_of_class (Class class, Class superclass)
462{
463 for (; class != Nil;)
464 {
465 if (class == superclass)
466 return YES;
e083f3f9 467 class = class_superclass_of_class (class);
88e17b57
BE
468 }
469
470 return NO;
471}
472
40165636
RB
473/* This list contains all the classes in the runtime system for whom
474 their superclasses are not yet known to the runtime. */
475static struct objc_list *unresolved_classes = 0;
88e17b57 476
1501d094 477/* Extern function used to reference the Object class. */
6000b42b
JDA
478extern void __objc_force_linking (void);
479
480void
88e17b57
BE
481__objc_force_linking (void)
482{
483 extern void __objc_linking (void);
484 __objc_linking ();
88e17b57
BE
485}
486
40165636
RB
487/* Run through the statics list, removing modules as soon as all its
488 statics have been initialized. */
88e17b57
BE
489static void
490objc_init_statics (void)
491{
492 struct objc_list **cell = &uninitialized_statics;
493 struct objc_static_instances **statics_in_module;
494
40165636 495 objc_mutex_lock (__objc_runtime_mutex);
88e17b57
BE
496
497 while (*cell)
498 {
499 int module_initialized = 1;
500
501 for (statics_in_module = (*cell)->head;
502 *statics_in_module; statics_in_module++)
503 {
504 struct objc_static_instances *statics = *statics_in_module;
fed2b101 505 Class class = objc_getClass (statics->class_name);
88e17b57 506
40165636 507 if (! class)
88e17b57 508 {
5254c66b
NP
509 /* It is unfortunate that this will cause all the
510 statics initialization to be done again (eg, if we
511 already initialized constant strings, and are now
512 initializing protocols, setting module_initialized to
513 0 would cause constant strings to be initialized
514 again). It would be good to be able to track if we
515 have already initialized some of them. */
516 module_initialized = 0;
517 }
518 else
519 {
520 /* Note that if this is a list of Protocol objects, some
521 of them may have been initialized already (because
522 they were attached to classes or categories, and the
523 class/category loading code automatically fixes them
524 up), and some of them may not. We really need to go
f7185d47
NP
525 through the whole list to be sure! Protocols are
526 also special because we want to register them and
527 register all their selectors. */
88e17b57
BE
528 id *inst;
529
f7185d47
NP
530 if (strcmp (statics->class_name, "Protocol") == 0)
531 {
532 /* Protocols are special, because not only we want
533 to fix up their class pointers, but we also want
534 to register them and their selectors with the
535 runtime. */
536 for (inst = &statics->instances[0]; *inst; inst++)
537 __objc_init_protocol ((struct objc_protocol *)*inst);
538 }
539 else
540 {
575584a9
NP
541 /* Other static instances (typically constant
542 strings) are easier as we just fix up their class
543 pointers. */
f7185d47
NP
544 for (inst = &statics->instances[0]; *inst; inst++)
545 (*inst)->class_pointer = class;
546 }
88e17b57
BE
547 }
548 }
549 if (module_initialized)
550 {
551 /* Remove this module from the uninitialized list. */
552 struct objc_list *this = *cell;
553 *cell = this->tail;
40165636 554 objc_free (this);
88e17b57
BE
555 }
556 else
557 cell = &(*cell)->tail;
558 }
559
40165636 560 objc_mutex_unlock (__objc_runtime_mutex);
575584a9 561}
88e17b57
BE
562
563/* This function is called by constructor functions generated for each
40165636
RB
564 module compiled. (_GLOBAL_$I$...) The purpose of this function is
565 to gather the module pointers so that they may be processed by the
566 initialization routines as soon as possible. */
88e17b57 567void
fed2b101 568__objc_exec_class (struct objc_module *module)
88e17b57 569{
fed2b101
NP
570 /* Have we processed any constructors previously? This flag is used
571 to indicate that some global data structures need to be
572 built. */
88e17b57
BE
573 static BOOL previous_constructors = 0;
574
40165636 575 static struct objc_list *unclaimed_categories = 0;
88e17b57 576
fed2b101
NP
577 /* The symbol table (defined in objc-private/module-abi-8.h)
578 generated by gcc. */
579 struct objc_symtab *symtab = module->symtab;
88e17b57 580
fed2b101 581 /* The statics in this module. */
88e17b57
BE
582 struct objc_static_instances **statics
583 = symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
584
fed2b101 585 /* Entry used to traverse hash lists. */
40165636 586 struct objc_list **cell;
88e17b57 587
fed2b101 588 /* The table of selector references for this module. */
600cbba2 589 struct objc_selector *selectors = symtab->refs;
88e17b57 590
88e17b57
BE
591 int i;
592
e99776d8 593 DEBUG_PRINTF ("\n__objc_exec_class (%p) - start processing module...\n", module);
88e17b57 594
575584a9 595 /* Check gcc version. */
40165636 596 init_check_module_version (module);
88e17b57 597
575584a9
NP
598 /* On the first call of this routine, initialize some data
599 structures. */
40165636 600 if (! previous_constructors)
88e17b57 601 {
575584a9 602 /* Initialize thread-safe system. */
40165636 603 __objc_init_thread_system ();
88e17b57 604 __objc_runtime_threads_alive = 1;
40165636 605 __objc_runtime_mutex = objc_mutex_allocate ();
88e17b57 606
40165636
RB
607 __objc_init_selector_tables ();
608 __objc_init_class_tables ();
609 __objc_init_dispatch_tables ();
0bfe3fa1
NP
610 duplicate_classes = objc_hash_new (8,
611 (hash_func_type)objc_hash_ptr,
612 objc_compare_ptrs);
270a1283
DA
613 __objc_load_methods = objc_hash_new (128,
614 (hash_func_type)objc_hash_ptr,
615 objc_compare_ptrs);
debfbfee 616 __objc_protocols_init ();
682e805a 617 __objc_accessors_init ();
fd312537 618 __objc_sync_init ();
88e17b57
BE
619 previous_constructors = 1;
620 }
621
c07499dc
NP
622 /* Save the module pointer so that later we remember to call +load
623 on all classes and categories on it. */
40165636
RB
624 objc_mutex_lock (__objc_runtime_mutex);
625 __objc_module_list = list_cons (module, __objc_module_list);
88e17b57 626
600cbba2 627 /* Replace referenced selectors from names to SELs. */
88e17b57 628 if (selectors)
e99776d8
NP
629 {
630 DEBUG_PRINTF (" registering selectors\n");
631 __objc_register_selectors_from_module (selectors);
632 }
88e17b57 633
575584a9
NP
634 /* Parse the classes in the load module and gather selector
635 information. */
88e17b57
BE
636 for (i = 0; i < symtab->cls_def_cnt; ++i)
637 {
638 Class class = (Class) symtab->defs[i];
40165636 639 const char *superclass = (char *) class->super_class;
88e17b57
BE
640
641 /* Make sure we have what we think. */
40165636
RB
642 assert (CLS_ISCLASS (class));
643 assert (CLS_ISMETA (class->class_pointer));
e99776d8 644 DEBUG_PRINTF (" installing class '%s'\n", class->name);
88e17b57 645
575584a9
NP
646 /* Initialize the subclass list to be NULL. In some cases it
647 isn't and this crashes the program. */
88e17b57
BE
648 class->subclass_list = NULL;
649
0bfe3fa1
NP
650 if (__objc_init_class (class))
651 {
652 /* Check to see if the superclass is known in this point. If
653 it's not add the class to the unresolved_classes list. */
654 if (superclass && ! objc_getClass (superclass))
655 unresolved_classes = list_cons (class, unresolved_classes);
656 }
657 }
88e17b57
BE
658
659 /* Process category information from the module. */
660 for (i = 0; i < symtab->cat_def_cnt; ++i)
661 {
fed2b101
NP
662 struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
663 Class class = objc_getClass (category->class_name);
88e17b57 664
575584a9
NP
665 /* If the class for the category exists then append its
666 methods. */
88e17b57
BE
667 if (class)
668 {
e99776d8 669 DEBUG_PRINTF (" installing category '%s (%s)'\n", category->class_name, category->category_name);
88e17b57
BE
670 /* Do instance methods. */
671 if (category->instance_methods)
672 class_add_method_list (class, category->instance_methods);
673
674 /* Do class methods. */
675 if (category->class_methods)
676 class_add_method_list ((Class) class->class_pointer,
677 category->class_methods);
678
679 if (category->protocols)
680 {
681 __objc_init_protocols (category->protocols);
682 __objc_class_add_protocols (class, category->protocols);
683 }
684
685 /* Register the instance methods as class methods, this is
40165636
RB
686 only done for root classes. */
687 __objc_register_instance_methods_to_class (class);
88e17b57
BE
688 }
689 else
690 {
e99776d8 691 DEBUG_PRINTF (" delaying installation of category '%s (%s)'\n", category->class_name, category->category_name);
575584a9
NP
692 /* The object to which the category methods belong can't be
693 found. Save the information. */
40165636 694 unclaimed_categories = list_cons (category, unclaimed_categories);
88e17b57
BE
695 }
696 }
697
698 if (statics)
699 uninitialized_statics = list_cons (statics, uninitialized_statics);
700 if (uninitialized_statics)
701 objc_init_statics ();
702
575584a9
NP
703 /* Scan the unclaimed category hash. Attempt to attach any
704 unclaimed categories to objects. */
e5e0f6f5 705 for (cell = &unclaimed_categories; *cell; )
88e17b57 706 {
fed2b101
NP
707 struct objc_category *category = (*cell)->head;
708 Class class = objc_getClass (category->class_name);
88e17b57
BE
709
710 if (class)
711 {
e99776d8 712 DEBUG_PRINTF (" installing (delayed) category '%s (%s)'\n", category->class_name, category->category_name);
88e17b57
BE
713 list_remove_head (cell);
714
715 if (category->instance_methods)
716 class_add_method_list (class, category->instance_methods);
717
718 if (category->class_methods)
719 class_add_method_list ((Class) class->class_pointer,
720 category->class_methods);
721
722 if (category->protocols)
723 {
724 __objc_init_protocols (category->protocols);
725 __objc_class_add_protocols (class, category->protocols);
726 }
727
728 /* Register the instance methods as class methods, this is
40165636
RB
729 only done for root classes. */
730 __objc_register_instance_methods_to_class (class);
88e17b57 731 }
e5e0f6f5
NP
732 else
733 cell = &(*cell)->tail;
88e17b57
BE
734 }
735
fed2b101 736 if (unclaimed_proto_list && objc_getClass ("Protocol"))
88e17b57 737 {
40165636
RB
738 list_mapcar (unclaimed_proto_list,
739 (void (*) (void *))__objc_init_protocols);
88e17b57
BE
740 list_free (unclaimed_proto_list);
741 unclaimed_proto_list = 0;
742 }
743
744 objc_send_load ();
745
41720477
NP
746 /* Check if there are no unresolved classes (ie, classes whose
747 superclass has not been loaded yet) and that the 'Object' class,
748 used as the class of classes, exist. If so, it is worth
749 "resolving the class links" at this point, which will setup all
750 the class/superclass pointers. */
751 if (!unresolved_classes && objc_getClass ("Object"))
e99776d8
NP
752 {
753 DEBUG_PRINTF (" resolving class links\n");
754 __objc_resolve_class_links ();
755 }
120d5f8e 756
41720477 757 objc_mutex_unlock (__objc_runtime_mutex);
e99776d8
NP
758
759 DEBUG_PRINTF ("__objc_exec_class (%p) - finished processing module...\n\n", module);
88e17b57
BE
760}
761
c07499dc
NP
762/* This function needs to be called with the objc_runtime_mutex
763 locked. */
40165636
RB
764static void
765objc_send_load (void)
88e17b57 766{
c07499dc 767 if (!__objc_module_list)
88e17b57
BE
768 return;
769
770 /* Try to find out if all the classes loaded so far also have their
c07499dc 771 superclasses known to the runtime. We suppose that the objects
40165636
RB
772 that are allocated in the +load method are in general of a class
773 declared in the same module. */
88e17b57
BE
774 if (unresolved_classes)
775 {
776 Class class = unresolved_classes->head;
777
fed2b101 778 while (objc_getClass ((char *) class->super_class))
88e17b57
BE
779 {
780 list_remove_head (&unresolved_classes);
781 if (unresolved_classes)
782 class = unresolved_classes->head;
783 else
784 break;
785 }
786
40165636
RB
787 /* If we still have classes for whom we don't have yet their
788 super classes known to the runtime we don't send the +load
1588200e 789 messages (and call the load callback) yet. */
88e17b57
BE
790 if (unresolved_classes)
791 return;
792 }
793
1501d094
NP
794 /* Special check. If 'Object', which is used by meta-classes, has
795 not been loaded yet, delay sending of +load. */
fed2b101 796 if (! objc_getClass ("Object"))
88e17b57
BE
797 return;
798
40165636 799 /* Iterate over all modules in the __objc_module_list and call on
1588200e 800 them the __objc_create_classes_tree function. This function
40165636
RB
801 creates a tree of classes that resembles the class hierarchy. */
802 list_mapcar (__objc_module_list,
803 (void (*) (void *)) __objc_create_classes_tree);
88e17b57
BE
804
805 while (__objc_class_tree_list)
806 {
807#ifdef DEBUG
808 objc_preorder_traverse (__objc_class_tree_list->head,
809 0, __objc_tree_print);
810#endif
811 objc_preorder_traverse (__objc_class_tree_list->head,
812 0, __objc_send_load);
813 objc_postorder_traverse (__objc_class_tree_list->head,
814 0, __objc_destroy_class_tree_node);
815 list_remove_head (&__objc_class_tree_list);
816 }
817
1588200e
NP
818 /* For each module, call the _objc_load_callback if any is
819 defined. */
820 list_mapcar (__objc_module_list, (void (*) (void *)) __objc_call_load_callback);
821
822 /* Empty the list of modules. */
88e17b57
BE
823 list_free (__objc_module_list);
824 __objc_module_list = NULL;
825}
826
827static void
fed2b101 828__objc_create_classes_tree (struct objc_module *module)
88e17b57 829{
575584a9 830 /* The runtime mutex is locked at this point */
fed2b101 831 struct objc_symtab *symtab = module->symtab;
88e17b57
BE
832 int i;
833
40165636
RB
834 /* Iterate thru classes defined in this module and insert them in
835 the classes tree hierarchy. */
88e17b57
BE
836 for (i = 0; i < symtab->cls_def_cnt; i++)
837 {
838 Class class = (Class) symtab->defs[i];
839
0bfe3fa1
NP
840 if (!objc_hash_is_key_in_hash (duplicate_classes, class))
841 objc_tree_insert_class (class);
88e17b57 842 }
c07499dc
NP
843
844 /* Now iterate over "claimed" categories too (ie, categories that
845 extend a class that has already been loaded by the runtime), and
846 insert them in the classes tree hiearchy too. Otherwise, if you
847 add a category, its +load method would not be called if the class
848 is already loaded in the runtime. It the category is
849 "unclaimed", ie, we haven't loaded the main class yet, postpone
850 sending +load as we want to execute +load from the class before
851 we execute the one from the category. */
852 for (i = 0; i < symtab->cat_def_cnt; ++i)
853 {
854 struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
855 Class class = objc_getClass (category->class_name);
856
857 /* If the class for the category exists then append its
858 methods. */
859 if (class)
860 objc_tree_insert_class (class);
861 }
88e17b57
BE
862}
863
864static void
1588200e 865__objc_call_load_callback (struct objc_module *module)
88e17b57 866{
1588200e 867 if (_objc_load_callback)
88e17b57 868 {
1588200e
NP
869 /* The runtime mutex is locked at this point. */
870 struct objc_symtab *symtab = module->symtab;
871 int i;
872
873 /* Iterate thru classes defined in this module and call the callback
874 for each one. */
875 for (i = 0; i < symtab->cls_def_cnt; i++)
876 {
877 Class class = (Class) symtab->defs[i];
0bfe3fa1
NP
878
879 if (!objc_hash_is_key_in_hash (duplicate_classes, class))
880 {
881 /* Call the _objc_load_callback for this class. */
e99776d8 882 DEBUG_PRINTF (" calling the load callback for class '%s'\n", class->name);
0bfe3fa1
NP
883 _objc_load_callback (class, 0);
884 }
1588200e
NP
885 }
886
887 /* Call the _objc_load_callback for categories. Don't register
888 the instance methods as class methods for categories to root
889 classes since they were already added in the class. */
890 for (i = 0; i < symtab->cat_def_cnt; i++)
891 {
892 struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
893 Class class = objc_getClass (category->class_name);
894
e99776d8
NP
895 DEBUG_PRINTF (" calling the load callback for category '%s (%s)'\n",
896 category->class_name, category->category_name);
1588200e
NP
897 _objc_load_callback (class, category);
898 }
88e17b57
BE
899 }
900}
901
40165636 902/* Sanity check the version of gcc used to compile `module'. */
40165636 903static void
fed2b101 904init_check_module_version (struct objc_module *module)
88e17b57 905{
fed2b101 906 if ((module->version != OBJC_VERSION) || (module->size != sizeof (struct objc_module)))
88e17b57 907 {
7b869986
NP
908 _objc_abort ("Module %s version %d doesn't match runtime %d\n",
909 module->name, (int)module->version, OBJC_VERSION);
88e17b57
BE
910 }
911}
912
0bfe3fa1
NP
913/* __objc_init_class must be called with __objc_runtime_mutex already
914 locked. Return YES if the class could be setup; return NO if the
915 class could not be setup because a class with the same name already
916 exists. */
917BOOL
6c5c7efd
NP
918__objc_init_class (Class class)
919{
920 /* Store the class in the class table and assign class numbers. */
1575c9de
NP
921 if (__objc_add_class_to_hash (class))
922 {
923 /* Register all of the selectors in the class and meta class. */
924 __objc_register_selectors_from_class (class);
925 __objc_register_selectors_from_class ((Class) class->class_pointer);
926
927 /* Install the fake dispatch tables. */
928 __objc_install_premature_dtable (class);
929 __objc_install_premature_dtable (class->class_pointer);
930
931 /* Register the instance methods as class methods, this is only
932 done for root classes. */
933 __objc_register_instance_methods_to_class (class);
934
935 if (class->protocols)
936 __objc_init_protocols (class->protocols);
0bfe3fa1
NP
937
938 return YES;
1575c9de
NP
939 }
940 else
0bfe3fa1
NP
941 {
942 /* The module contains a duplicate class. Remember it so that
943 we will ignore it later. */
e99776d8 944 DEBUG_PRINTF (" duplicate class '%s' - will be ignored\n", class->name);
0bfe3fa1
NP
945 objc_hash_add (&duplicate_classes, class, class);
946 return NO;
947 }
6c5c7efd
NP
948}
949
f7185d47
NP
950/* __objc_init_protocol must be called with __objc_runtime_mutex
951 already locked, and the "Protocol" class already registered. */
952static void
953__objc_init_protocol (struct objc_protocol *protocol)
954{
955 static Class proto_class = 0;
956
957 if (! proto_class)
fed2b101 958 proto_class = objc_getClass ("Protocol");
f7185d47
NP
959
960 if (((size_t)protocol->class_pointer) == PROTOCOL_VERSION)
961 {
575584a9 962 /* Assign class pointer. */
f7185d47
NP
963 protocol->class_pointer = proto_class;
964
965 /* Register all the selectors in the protocol with the runtime.
966 This both registers the selectors with the right types, and
967 it also fixes up the 'struct objc_method' structures inside
968 the protocol so that each method_name (a char * as compiled
969 by the compiler) is replaced with the appropriate runtime
970 SEL. */
971 if (protocol->class_methods)
972 __objc_register_selectors_from_description_list (protocol->class_methods);
973
974 if (protocol->instance_methods)
975 __objc_register_selectors_from_description_list (protocol->instance_methods);
976
977 /* Register the protocol in the hashtable or protocols by
978 name. */
979 __objc_protocols_add_protocol (protocol->protocol_name, protocol);
980
575584a9 981 /* Init super protocols. */
f7185d47
NP
982 __objc_init_protocols (protocol->protocol_list);
983 }
984 else if (protocol->class_pointer != proto_class)
985 {
986 _objc_abort ("Version %d doesn't match runtime protocol version %d\n",
987 (int) ((char *) protocol->class_pointer
988 - (char *) 0),
989 PROTOCOL_VERSION);
990 }
991}
992
88e17b57 993static void
40165636 994__objc_init_protocols (struct objc_protocol_list *protos)
88e17b57 995{
8f8c44cb 996 size_t i;
88e17b57
BE
997 static Class proto_class = 0;
998
999 if (! protos)
1000 return;
1001
40165636 1002 objc_mutex_lock (__objc_runtime_mutex);
88e17b57 1003
40165636 1004 if (! proto_class)
fed2b101 1005 proto_class = objc_getClass ("Protocol");
88e17b57 1006
40165636 1007 if (! proto_class)
88e17b57
BE
1008 {
1009 unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
40165636 1010 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
1011 return;
1012 }
1013
1014#if 0
575584a9 1015 assert (protos->next == 0); /* Only single ones allowed. */
88e17b57
BE
1016#endif
1017
40165636 1018 for (i = 0; i < protos->count; i++)
88e17b57 1019 {
40165636 1020 struct objc_protocol *aProto = protos->list[i];
f7185d47 1021 __objc_init_protocol (aProto);
88e17b57
BE
1022 }
1023
40165636 1024 objc_mutex_unlock (__objc_runtime_mutex);
88e17b57
BE
1025}
1026
40165636
RB
1027static void
1028__objc_class_add_protocols (Class class, struct objc_protocol_list *protos)
88e17b57 1029{
88e17b57
BE
1030 if (! protos)
1031 return;
1032
88e17b57
BE
1033 protos->next = class->protocols;
1034 class->protocols = protos;
1035}