1 /* GNU Objective C Runtime initialization
2 Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup
4 +load support contributed by Ovidiu Predescu <ovidiu@net-community.com>
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
12 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 You should have received a copy of the GNU General Public License along with
18 GNU CC; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* As a special exception, if you link this library with files compiled with
22 GCC to produce an executable, this does not cause the resulting executable
23 to be covered by the GNU General Public License. This exception does not
24 however invalidate any other reasons why the executable file might be
25 covered by the GNU General Public License. */
29 /* The version number of this runtime. This must match the number
30 defined in gcc (objc-act.c) */
31 #define OBJC_VERSION 8
32 #define PROTOCOL_VERSION 2
34 /* This list contains all modules currently loaded into the runtime */
35 static struct objc_list* __objc_module_list = 0; /* !T:MUTEX */
37 /* This list contains all proto_list's not yet assigned class links */
38 static struct objc_list* unclaimed_proto_list = 0; /* !T:MUTEX */
40 /* List of unresolved static instances. */
41 static struct objc_list *uninitialized_statics = 0; /* !T:MUTEX */
43 /* Global runtime "write" mutex. */
44 objc_mutex_t __objc_runtime_mutex = 0;
46 /* Number of threads that are alive. */
47 int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
49 /* Check compiler vs runtime version */
50 static void init_check_module_version (Module_t);
52 /* Assign isa links to protos */
53 static void __objc_init_protocols (struct objc_protocol_list* protos);
55 /* Add protocol to class */
56 static void __objc_class_add_protocols (Class, struct objc_protocol_list*);
58 /* This is a hook which is called by __objc_exec_class every time a class
59 or a category is loaded into the runtime. This may e.g. help a
60 dynamic loader determine the classes that have been loaded when
61 an object file is dynamically linked in */
62 void (*_objc_load_callback)(Class class, Category* category); /* !T:SAFE */
64 /* Is all categories/classes resolved? */
65 BOOL __objc_dangling_categories = NO; /* !T:UNUSED */
68 __sel_register_typed_name (const char *name, const char *types,
69 struct objc_selector *orig, BOOL is_const);
71 /* Sends +load to all classes and categories in certain situations. */
72 static void objc_send_load (void);
74 /* Inserts all the classes defined in module in a tree of classes that
75 resembles the class hierarchy. This tree is traversed in preorder and the
76 classes in its nodes receive the +load message if these methods were not
77 executed before. The algorithm ensures that when the +load method of a class
78 is executed all the superclasses have been already received the +load
80 static void __objc_create_classes_tree (Module_t module);
82 static void __objc_call_callback (Module_t module);
84 /* A special version that works only before the classes are completely
85 installed in the runtime. */
86 static BOOL class_is_subclass_of_class (Class class, Class superclass);
88 typedef struct objc_class_tree {
90 struct objc_list *subclasses; /* `head' is pointer to an objc_class_tree */
93 /* This is a linked list of objc_class_tree trees. The head of these trees
94 are root classes (their super class is Nil). These different trees
95 represent different class hierarchies. */
96 static struct objc_list *__objc_class_tree_list = NULL;
98 /* Keeps the +load methods who have been already executed. This hash should
99 not be destroyed during the execution of the program. */
100 static cache_ptr __objc_load_methods = NULL;
102 /* Creates a tree of classes whose topmost class is directly inherited from
103 `upper' and the bottom class in this tree is `bottom_class'. The classes
104 in this tree are super classes of `bottom_class'. `subclasses' member
105 of each tree node point to the next subclass tree node. */
106 static objc_class_tree *
107 create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
109 Class superclass = bottom_class->super_class ?
110 objc_lookup_class ((char*)bottom_class->super_class)
113 objc_class_tree *tree, *prev;
115 DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
116 DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
117 (bottom_class ? bottom_class->name : NULL),
118 (upper ? upper->name : NULL));
120 tree = prev = objc_calloc (1, sizeof (objc_class_tree));
121 prev->class = bottom_class;
123 while (superclass != upper)
125 tree = objc_calloc (1, sizeof (objc_class_tree));
126 tree->class = superclass;
127 tree->subclasses = list_cons (prev, tree->subclasses);
128 superclass = (superclass->super_class ?
129 objc_lookup_class ((char*)superclass->super_class)
137 /* Insert the `class' into the proper place in the `tree' class hierarchy. This
138 function returns a new tree if the class has been successfully inserted into
139 the tree or NULL if the class is not part of the classes hierarchy described
140 by `tree'. This function is private to objc_tree_insert_class(), you should
141 not call it directly. */
142 static objc_class_tree *
143 __objc_tree_insert_class (objc_class_tree *tree, Class class)
145 DEBUG_PRINTF ("__objc_tree_insert_class: tree = %x, class = %s\n",
149 return create_tree_of_subclasses_inherited_from (class, NULL);
150 else if (class == tree->class)
152 /* `class' has been already inserted */
153 DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name);
156 else if ((class->super_class ?
157 objc_lookup_class ((char*)class->super_class)
161 /* If class is a direct subclass of tree->class then add class to the
162 list of subclasses. First check to see if it wasn't already
164 struct objc_list *list = tree->subclasses;
165 objc_class_tree *node;
169 /* Class has been already inserted; do nothing just return
171 if (((objc_class_tree*)list->head)->class == class)
173 DEBUG_PRINTF ("2. class %s was previously inserted\n",
180 /* Create a new node class and insert it into the list of subclasses */
181 node = objc_calloc (1, sizeof (objc_class_tree));
183 tree->subclasses = list_cons (node, tree->subclasses);
184 DEBUG_PRINTF ("3. class %s inserted\n", class->name);
189 /* The class is not a direct subclass of tree->class. Search for class's
190 superclasses in the list of subclasses. */
191 struct objc_list *subclasses = tree->subclasses;
193 /* Precondition: the class must be a subclass of tree->class; otherwise
194 return NULL to indicate our caller that it must take the next tree. */
195 if (!class_is_subclass_of_class (class, tree->class))
198 for (; subclasses != NULL; subclasses = subclasses->tail)
200 Class aClass = ((objc_class_tree*)(subclasses->head))->class;
202 if (class_is_subclass_of_class (class, aClass))
204 /* If we found one of class's superclasses we insert the class
205 into its subtree and return the original tree since nothing
208 = __objc_tree_insert_class (subclasses->head, class);
209 DEBUG_PRINTF ("4. class %s inserted\n", class->name);
214 /* We haven't found a subclass of `class' in the `subclasses' list.
215 Create a new tree of classes whose topmost class is a direct subclass
218 objc_class_tree *new_tree
219 = create_tree_of_subclasses_inherited_from (class, tree->class);
220 tree->subclasses = list_cons (new_tree, tree->subclasses);
221 DEBUG_PRINTF ("5. class %s inserted\n", class->name);
227 /* This function inserts `class' in the right tree hierarchy classes. */
229 objc_tree_insert_class (Class class)
231 struct objc_list *list_node;
232 objc_class_tree *tree;
234 list_node = __objc_class_tree_list;
237 tree = __objc_tree_insert_class (list_node->head, class);
240 list_node->head = tree;
244 list_node = list_node->tail;
247 /* If the list was finished but the class hasn't been inserted, insert it
251 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
252 __objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
256 /* Traverse tree in preorder. Used to send +load. */
258 objc_preorder_traverse (objc_class_tree *tree,
260 void (*function)(objc_class_tree*, int))
262 struct objc_list *node;
264 (*function) (tree, level);
265 for (node = tree->subclasses; node; node = node->tail)
266 objc_preorder_traverse (node->head, level + 1, function);
269 /* Traverse tree in postorder. Used to destroy a tree. */
271 objc_postorder_traverse (objc_class_tree *tree,
273 void (*function)(objc_class_tree*, int))
275 struct objc_list *node;
277 for (node = tree->subclasses; node; node = node->tail)
278 objc_postorder_traverse (node->head, level + 1, function);
279 (*function) (tree, level);
282 /* Used to print a tree class hierarchy. */
285 __objc_tree_print (objc_class_tree *tree, int level)
289 for (i = 0; i < level; i++)
291 printf ("%s\n", tree->class->name);
295 /* Walks on a linked list of methods in the reverse order and executes all
296 the methods corresponding to `op' selector. Walking in the reverse order
297 assures the +load of class is executed first and then +load of categories
298 because of the way in which categories are added to the class methods. */
300 __objc_send_message_in_list (MethodList_t method_list, Class class, SEL op)
307 /* First execute the `op' message in the following method lists */
308 __objc_send_message_in_list (method_list->method_next, class, op);
310 /* Search the method list. */
311 for (i = 0; i < method_list->method_count; i++)
313 Method_t mth = &method_list->method_list[i];
315 if (mth->method_name && sel_eq (mth->method_name, op)
316 && !hash_is_key_in_hash (__objc_load_methods, mth->method_imp))
318 /* Add this method into the +load hash table */
319 hash_add (&__objc_load_methods, mth->method_imp, mth->method_imp);
321 DEBUG_PRINTF ("sending +load in class: %s\n", class->name);
323 /* The method was found and wasn't previously executed. */
324 (*mth->method_imp) ((id)class, mth->method_name);
332 __objc_send_load (objc_class_tree *tree,
333 int level __attribute__ ((__unused__)))
335 static SEL load_sel = 0;
336 Class class = tree->class;
337 MethodList_t method_list = class->class_pointer->methods;
340 load_sel = sel_register_name ("load");
342 __objc_send_message_in_list (method_list, class, load_sel);
346 __objc_destroy_class_tree_node (objc_class_tree *tree,
347 int level __attribute__ ((__unused__)))
352 /* This is used to check if the relationship between two classes before the
353 runtime completely installs the classes. */
355 class_is_subclass_of_class (Class class, Class superclass)
357 for (; class != Nil;)
359 if (class == superclass)
361 class = (class->super_class ?
362 objc_lookup_class ((char*)class->super_class)
369 /* This list contains all the classes in the runtime system for whom their
370 superclasses are not yet know to the runtime. */
371 static struct objc_list* unresolved_classes = 0;
373 /* Extern function used to reference the Object and NXConstantString classes.
376 extern void __objc_force_linking (void);
379 __objc_force_linking (void)
381 extern void __objc_linking (void);
385 /* Run through the statics list, removing modules as soon as all its statics
386 have been initialized. */
388 objc_init_statics (void)
390 struct objc_list **cell = &uninitialized_statics;
391 struct objc_static_instances **statics_in_module;
393 objc_mutex_lock(__objc_runtime_mutex);
397 int module_initialized = 1;
399 for (statics_in_module = (*cell)->head;
400 *statics_in_module; statics_in_module++)
402 struct objc_static_instances *statics = *statics_in_module;
403 Class class = objc_lookup_class (statics->class_name);
406 module_initialized = 0;
407 /* Actually, the static's class_pointer will be NULL when we
408 haven't been here before. However, the comparison is to be
409 reminded of taking into account class posing and to think about
410 possible semantics... */
411 else if (class != statics->instances[0]->class_pointer)
415 for (inst = &statics->instances[0]; *inst; inst++)
417 (*inst)->class_pointer = class;
419 /* ??? Make sure the object will not be freed. With
420 refcounting, invoke `-retain'. Without refcounting, do
421 nothing and hope that `-free' will never be invoked. */
423 /* ??? Send the object an `-initStatic' or something to
424 that effect now or later on? What are the semantics of
425 statically allocated instances, besides the trivial
426 NXConstantString, anyway? */
430 if (module_initialized)
432 /* Remove this module from the uninitialized list. */
433 struct objc_list *this = *cell;
438 cell = &(*cell)->tail;
441 objc_mutex_unlock(__objc_runtime_mutex);
442 } /* objc_init_statics */
444 /* This function is called by constructor functions generated for each
445 module compiled. (_GLOBAL_$I$...) The purpose of this function is to
446 gather the module pointers so that they may be processed by the
447 initialization routines as soon as possible */
450 __objc_exec_class (Module_t module)
452 /* Have we processed any constructors previously? This flag is used to
453 indicate that some global data structures need to be built. */
454 static BOOL previous_constructors = 0;
456 static struct objc_list* unclaimed_categories = 0;
458 /* The symbol table (defined in objc-api.h) generated by gcc */
459 Symtab_t symtab = module->symtab;
461 /* The statics in this module */
462 struct objc_static_instances **statics
463 = symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
465 /* Entry used to traverse hash lists */
466 struct objc_list** cell;
468 /* The table of selector references for this module */
469 SEL selectors = symtab->refs;
474 DEBUG_PRINTF ("received module: %s\n", module->name);
476 /* check gcc version */
477 init_check_module_version(module);
479 /* On the first call of this routine, initialize some data structures. */
480 if (!previous_constructors)
482 /* Initialize thread-safe system */
483 __objc_init_thread_system();
484 __objc_runtime_threads_alive = 1;
485 __objc_runtime_mutex = objc_mutex_allocate();
487 __objc_init_selector_tables();
488 __objc_init_class_tables();
489 __objc_init_dispatch_tables();
490 __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
492 = hash_new (128, (hash_func_type)hash_ptr, compare_ptrs);
493 previous_constructors = 1;
496 /* Save the module pointer for later processing. (not currently used) */
497 objc_mutex_lock(__objc_runtime_mutex);
498 __objc_module_list = list_cons(module, __objc_module_list);
500 /* Replace referenced selectors from names to SEL's. */
503 for (i = 0; selectors[i].sel_id; ++i)
505 const char *name, *type;
506 name = (char*)selectors[i].sel_id;
507 type = (char*)selectors[i].sel_types;
508 /* Constructors are constant static data so we can safely store
509 pointers to them in the runtime structures. is_const == YES */
510 __sel_register_typed_name (name, type,
511 (struct objc_selector*)&(selectors[i]),
516 /* Parse the classes in the load module and gather selector information. */
517 DEBUG_PRINTF ("gathering selectors from module: %s\n", module->name);
518 for (i = 0; i < symtab->cls_def_cnt; ++i)
520 Class class = (Class) symtab->defs[i];
521 const char* superclass = (char*)class->super_class;
523 /* Make sure we have what we think. */
524 assert (CLS_ISCLASS(class));
525 assert (CLS_ISMETA(class->class_pointer));
526 DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
528 /* Initialize the subclass list to be NULL.
529 In some cases it isn't and this crashes the program. */
530 class->subclass_list = NULL;
532 /* Store the class in the class table and assign class numbers. */
533 __objc_add_class_to_hash (class);
535 /* Register all of the selectors in the class and meta class. */
536 __objc_register_selectors_from_class (class);
537 __objc_register_selectors_from_class ((Class) class->class_pointer);
539 /* Install the fake dispatch tables */
540 __objc_install_premature_dtable(class);
541 __objc_install_premature_dtable(class->class_pointer);
543 /* Register the instance methods as class methods, this is
544 only done for root classes. */
545 __objc_register_instance_methods_to_class(class);
547 if (class->protocols)
548 __objc_init_protocols (class->protocols);
550 /* Check to see if the superclass is known in this point. If it's not
551 add the class to the unresolved_classes list. */
552 if (superclass && !objc_lookup_class (superclass))
553 unresolved_classes = list_cons (class, unresolved_classes);
556 /* Process category information from the module. */
557 for (i = 0; i < symtab->cat_def_cnt; ++i)
559 Category_t category = symtab->defs[i + symtab->cls_def_cnt];
560 Class class = objc_lookup_class (category->class_name);
562 /* If the class for the category exists then append its methods. */
566 DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
570 /* Do instance methods. */
571 if (category->instance_methods)
572 class_add_method_list (class, category->instance_methods);
574 /* Do class methods. */
575 if (category->class_methods)
576 class_add_method_list ((Class) class->class_pointer,
577 category->class_methods);
579 if (category->protocols)
581 __objc_init_protocols (category->protocols);
582 __objc_class_add_protocols (class, category->protocols);
585 /* Register the instance methods as class methods, this is
586 only done for root classes. */
587 __objc_register_instance_methods_to_class(class);
591 /* The object to which the category methods belong can't be found.
592 Save the information. */
593 unclaimed_categories = list_cons(category, unclaimed_categories);
598 uninitialized_statics = list_cons (statics, uninitialized_statics);
599 if (uninitialized_statics)
600 objc_init_statics ();
602 /* Scan the unclaimed category hash. Attempt to attach any unclaimed
603 categories to objects. */
604 for (cell = &unclaimed_categories; *cell; )
606 Category_t category = (*cell)->head;
607 Class class = objc_lookup_class (category->class_name);
611 DEBUG_PRINTF ("attaching stored categories to object: %s\n",
614 list_remove_head (cell);
616 if (category->instance_methods)
617 class_add_method_list (class, category->instance_methods);
619 if (category->class_methods)
620 class_add_method_list ((Class) class->class_pointer,
621 category->class_methods);
623 if (category->protocols)
625 __objc_init_protocols (category->protocols);
626 __objc_class_add_protocols (class, category->protocols);
629 /* Register the instance methods as class methods, this is
630 only done for root classes. */
631 __objc_register_instance_methods_to_class(class);
634 cell = &(*cell)->tail;
637 if (unclaimed_proto_list && objc_lookup_class ("Protocol"))
639 list_mapcar (unclaimed_proto_list,(void(*)(void*))__objc_init_protocols);
640 list_free (unclaimed_proto_list);
641 unclaimed_proto_list = 0;
646 objc_mutex_unlock(__objc_runtime_mutex);
649 static void objc_send_load (void)
651 if (!__objc_module_list)
654 /* Try to find out if all the classes loaded so far also have their
655 superclasses known to the runtime. We suppose that the objects that are
656 allocated in the +load method are in general of a class declared in the
658 if (unresolved_classes)
660 Class class = unresolved_classes->head;
662 while (objc_lookup_class ((char*)class->super_class))
664 list_remove_head (&unresolved_classes);
665 if (unresolved_classes)
666 class = unresolved_classes->head;
672 * If we still have classes for whom we don't have yet their super
673 * classes known to the runtime we don't send the +load messages.
675 if (unresolved_classes)
679 /* Special check to allow creating and sending messages to constant strings
680 in +load methods. If these classes are not yet known, even if all the
681 other classes are known, delay sending of +load. */
682 if (!objc_lookup_class ("NXConstantString") ||
683 !objc_lookup_class ("Object"))
686 /* Iterate over all modules in the __objc_module_list and call on them the
687 __objc_create_classes_tree function. This function creates a tree of
688 classes that resembles the class hierarchy. */
689 list_mapcar (__objc_module_list, (void(*)(void*))__objc_create_classes_tree);
691 while (__objc_class_tree_list)
694 objc_preorder_traverse (__objc_class_tree_list->head,
695 0, __objc_tree_print);
697 objc_preorder_traverse (__objc_class_tree_list->head,
698 0, __objc_send_load);
699 objc_postorder_traverse (__objc_class_tree_list->head,
700 0, __objc_destroy_class_tree_node);
701 list_remove_head (&__objc_class_tree_list);
704 list_mapcar (__objc_module_list, (void(*)(void*))__objc_call_callback);
705 list_free (__objc_module_list);
706 __objc_module_list = NULL;
710 __objc_create_classes_tree (Module_t module)
712 /* The runtime mutex is locked in this point */
714 Symtab_t symtab = module->symtab;
717 /* Iterate thru classes defined in this module and insert them in the classes
719 for (i = 0; i < symtab->cls_def_cnt; i++)
721 Class class = (Class) symtab->defs[i];
723 objc_tree_insert_class (class);
728 __objc_call_callback (Module_t module)
730 /* The runtime mutex is locked in this point */
732 Symtab_t symtab = module->symtab;
735 /* Iterate thru classes defined in this module and call the callback for
737 for (i = 0; i < symtab->cls_def_cnt; i++)
739 Class class = (Class) symtab->defs[i];
741 /* Call the _objc_load_callback for this class. */
742 if (_objc_load_callback)
743 _objc_load_callback(class, 0);
746 /* Call the _objc_load_callback for categories. Don't register the instance
747 methods as class methods for categories to root classes since they were
748 already added in the class. */
749 for (i = 0; i < symtab->cat_def_cnt; i++)
751 Category_t category = symtab->defs[i + symtab->cls_def_cnt];
752 Class class = objc_lookup_class (category->class_name);
754 if (_objc_load_callback)
755 _objc_load_callback(class, category);
759 /* Sanity check the version of gcc used to compile `module'*/
760 static void init_check_module_version(Module_t module)
762 if ((module->version != OBJC_VERSION) || (module->size != sizeof (Module)))
766 if(module->version > OBJC_VERSION)
767 code = OBJC_ERR_OBJC_VERSION;
768 else if (module->version < OBJC_VERSION)
769 code = OBJC_ERR_GCC_VERSION;
771 code = OBJC_ERR_MODULE_SIZE;
773 objc_error(nil, code, "Module %s version %d doesn't match runtime %d\n",
774 module->name, (int)module->version, OBJC_VERSION);
779 __objc_init_protocols (struct objc_protocol_list* protos)
782 static Class proto_class = 0;
787 objc_mutex_lock(__objc_runtime_mutex);
790 proto_class = objc_lookup_class("Protocol");
794 unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
795 objc_mutex_unlock(__objc_runtime_mutex);
800 assert (protos->next == 0); /* only single ones allowed */
803 for(i = 0; i < protos->count; i++)
805 struct objc_protocol* aProto = protos->list[i];
806 if (((size_t)aProto->class_pointer) == PROTOCOL_VERSION)
808 /* assign class pointer */
809 aProto->class_pointer = proto_class;
811 /* init super protocols */
812 __objc_init_protocols (aProto->protocol_list);
814 else if (protos->list[i]->class_pointer != proto_class)
816 objc_error(nil, OBJC_ERR_PROTOCOL_VERSION,
817 "Version %d doesn't match runtime protocol version %d\n",
818 (int)((char*)protos->list[i]->class_pointer-(char*)0),
823 objc_mutex_unlock(__objc_runtime_mutex);
826 static void __objc_class_add_protocols (Class class,
827 struct objc_protocol_list* protos)
834 protos->next = class->protocols;
835 class->protocols = protos;