OSDN Git Service

PR ada/60703
[pf3gnuchains/gcc-fork.git] / libobjc / init.c
index 370dea9..23ba41b 100644 (file)
@@ -24,6 +24,10 @@ a copy of the GCC Runtime Library Exception along with this program;
 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 <http://www.gnu.org/licenses/>.  */
 
+/* Uncommented the following line to enable debug logging.  Use this
+   only while debugging the runtime.  */
+/* #define DEBUG 1 */
+
 #include "objc-private/common.h"
 #include "objc-private/error.h"
 #include "objc/runtime.h"
@@ -31,7 +35,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include "objc-private/hash.h"
 #include "objc-private/objc-list.h" 
 #include "objc-private/module-abi-8.h" 
-#include "objc-private/runtime.h"
+#include "objc-private/runtime.h"   /* For __objc_resolve_class_links().  */
 #include "objc-private/selector.h"  /* For __sel_register_typed_name().  */
 #include "objc-private/objc-sync.h" /* For __objc_sync_init() */
 #include "objc-private/protocols.h" /* For __objc_protocols_init(),
@@ -44,8 +48,9 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #define OBJC_VERSION 8
 #define PROTOCOL_VERSION 2
 
-/* This list contains all modules currently loaded into the
-   runtime.  */
+/* This list contains modules currently loaded into the runtime and
+   for which the +load method (and the load callback, if any) has not
+   been called yet.  */
 static struct objc_list *__objc_module_list = 0;       /* !T:MUTEX */
 
 /* This list contains all proto_list's not yet assigned class
@@ -55,6 +60,16 @@ static struct objc_list *unclaimed_proto_list = 0;   /* !T:MUTEX */
 /* List of unresolved static instances.  */
 static struct objc_list *uninitialized_statics = 0;    /* !T:MUTEX */
 
+/* List of duplicated classes found while loading modules.  If we find
+   a class twice, we ignore it the second time.  On some platforms,
+   where the order in which modules are loaded is well defined, this
+   allows you to replace a class in a shared library by linking in a
+   new implementation which is loaded in in the right order, and which
+   overrides the existing one.
+
+   Protected by __objc_runtime_mutex.  */
+static cache_ptr duplicate_classes = NULL;
+
 /* Global runtime "write" mutex.  Having a single mutex prevents
    deadlocks, but reduces concurrency.  To improve concurrency, some
    groups of functions in the runtime have their own separate mutex
@@ -83,14 +98,10 @@ static void __objc_init_protocol (struct objc_protocol *protocol);
 /* Add protocol to class.  */
 static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
 
-/* This is a hook which is called by __objc_exec_class every time a
-   class or a category is loaded into the runtime.  This may e.g. help
-   a dynamic loader determine the classes that have been loaded when
-   an object file is dynamically linked in.  */
-/* TODO: This needs to be declared in a public file with the new API.  */
-void (*_objc_load_callback) (Class class, struct objc_category *category); /* !T:SAFE */
+/* Load callback hook.  */
+void (*_objc_load_callback) (Class class, struct objc_category *category) = 0; /* !T:SAFE */
 
-/* Are all categories/classes resolved?  */
+/* Are all categories/classes resolved ?  */
 BOOL __objc_dangling_categories = NO;           /* !T:UNUSED */
 
 /* Sends +load to all classes and categories in certain
@@ -98,26 +109,37 @@ BOOL __objc_dangling_categories = NO;           /* !T:UNUSED */
 static void objc_send_load (void);
 
 /* Inserts all the classes defined in module in a tree of classes that
-   resembles the class hierarchy. This tree is traversed in preorder
+   resembles the class hierarchy.  This tree is traversed in preorder
    and the classes in its nodes receive the +load message if these
-   methods were not executed before. The algorithm ensures that when
+   methods were not executed before.  The algorithm ensures that when
    the +load method of a class is executed all the superclasses have
    been already received the +load message.  */
 static void __objc_create_classes_tree (struct objc_module *module);
 
-static void __objc_call_callback (struct objc_module *module);
+/* Calls the _objc_load_callback for each class and category in the
+   module (if _objc_load_callback is not NULL).  */
+static void __objc_call_load_callback (struct objc_module *module);
 
 /* A special version that works only before the classes are completely
    installed in the runtime.  */
 static BOOL class_is_subclass_of_class (Class class, Class superclass);
 
-typedef struct objc_class_tree {
+/* This is a node in the class tree hierarchy used to send +load
+   messages.  */
+typedef struct objc_class_tree
+{
+  /* The class corresponding to the node.  */
   Class class;
-  struct objc_list *subclasses; /* `head' is pointer to an objc_class_tree */
+
+  /* This is a linked list of all the direct subclasses of this class.
+     'head' points to a subclass node; 'tail' points to the next
+     objc_list node (whose 'head' points to another subclass node,
+     etc).  */
+  struct objc_list *subclasses;
 } objc_class_tree;
 
-/* This is a linked list of objc_class_tree trees. The head of these
-   trees are root classes (their super class is Nil). These different
+/* This is a linked list of objc_class_tree trees.  The head of these
+   trees are root classes (their super class is Nil).  These different
    trees represent different class hierarchies.  */
 static struct objc_list *__objc_class_tree_list = NULL;
 
@@ -130,7 +152,7 @@ static cache_ptr __objc_load_methods = NULL;
    is really needed so that superclasses will get the message before
    subclasses.
 
-   This tree will contain classes which are being loaded (or have just
+   This tree may contain classes which are being loaded (or have just
    being loaded), and whose super_class pointers have not yet been
    resolved.  This implies that their super_class pointers point to a
    string with the name of the superclass; when the first message is
@@ -169,29 +191,30 @@ static Class  class_superclass_of_class (Class class)
 
 
 /* Creates a tree of classes whose topmost class is directly inherited
-   from `upper' and the bottom class in this tree is
-   `bottom_class'. The classes in this tree are super classes of
-   `bottom_class'. `subclasses' member of each tree node point to the
-   next subclass tree node.  */
+   from `upper' and the bottom class in this tree is `bottom_class'.
+   If `upper' is Nil, creates a class hierarchy up to a root class.
+   The classes in this tree are super classes of `bottom_class'.  The
+   `subclasses' member of each tree node point to the list of
+   subclasses for the node.  */
 static objc_class_tree *
 create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
 {
   Class superclass;
   objc_class_tree *tree, *prev;
 
-  if (bottom_class->super_class)
-    superclass = objc_getClass ((char *) bottom_class->super_class);
-  else
-    superclass = Nil;
-
   DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");
-  DEBUG_PRINTF ("bottom_class = %s, upper = %s\n",
+  DEBUG_PRINTF (" bottom_class = %s, upper = %s\n",
                (bottom_class ? bottom_class->name : NULL),
                (upper ? upper->name : NULL));
 
-  tree = prev = objc_calloc (1, sizeof (objc_class_tree));
+  superclass = class_superclass_of_class (bottom_class);
+
+  prev = objc_calloc (1, sizeof (objc_class_tree));
   prev->class = bottom_class;
 
+  if (superclass == upper)
+    return prev;
+
   while (superclass != upper)
     {
       tree = objc_calloc (1, sizeof (objc_class_tree));
@@ -205,23 +228,23 @@ create_tree_of_subclasses_inherited_from (Class bottom_class, Class upper)
 }
 
 /* Insert the `class' into the proper place in the `tree' class
-   hierarchy. This function returns a new tree if the class has been
+   hierarchy.  This function returns a new tree if the class has been
    successfully inserted into the tree or NULL if the class is not
-   part of the classes hierarchy described by `tree'. This function is
-   private to objc_tree_insert_class (), you should not call it
+   part of the classes hierarchy described by `tree'.  This function
+   is private to objc_tree_insert_class (), you should not call it
    directly.  */
 static objc_class_tree *
 __objc_tree_insert_class (objc_class_tree *tree, Class class)
 {
-  DEBUG_PRINTF ("__objc_tree_insert_class: tree = %x, class = %s\n",
-               tree, class->name);
+  DEBUG_PRINTF ("__objc_tree_insert_class: tree = %p (root: %s), class = %s\n",
+               tree, ((tree && tree->class) ? tree->class->name : "Nil"), class->name);
 
   if (tree == NULL)
     return create_tree_of_subclasses_inherited_from (class, NULL);
   else if (class == tree->class)
     {
       /* `class' has been already inserted.  */
-      DEBUG_PRINTF ("1. class %s was previously inserted\n", class->name);
+      DEBUG_PRINTF (" 1. class %s was previously inserted\n", class->name);
       return tree;
     }
   else if (class_superclass_of_class (class) == tree->class)
@@ -238,7 +261,7 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
             the tree.  */
          if (((objc_class_tree *) list->head)->class == class)
            {
-             DEBUG_PRINTF ("2. class %s was previously inserted\n",
+             DEBUG_PRINTF (" 2. class %s was previously inserted\n",
                            class->name);
              return tree;
            }
@@ -250,7 +273,7 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
       node = objc_calloc (1, sizeof (objc_class_tree));
       node->class = class;
       tree->subclasses = list_cons (node, tree->subclasses);
-      DEBUG_PRINTF ("3. class %s inserted\n", class->name);
+      DEBUG_PRINTF (" 3. class %s inserted\n", class->name);
       return tree;
     }
   else
@@ -276,7 +299,7 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
                 since nothing has been changed.  */
              subclasses->head
                  = __objc_tree_insert_class (subclasses->head, class);
-             DEBUG_PRINTF ("4. class %s inserted\n", class->name);
+             DEBUG_PRINTF (" 4. class %s inserted\n", class->name);
              return tree;
            }
        }
@@ -288,7 +311,7 @@ __objc_tree_insert_class (objc_class_tree *tree, Class class)
        objc_class_tree *new_tree
          = create_tree_of_subclasses_inherited_from (class, tree->class);
        tree->subclasses = list_cons (new_tree, tree->subclasses);
-       DEBUG_PRINTF ("5. class %s inserted\n", class->name);
+       DEBUG_PRINTF (" 5. class %s inserted\n", class->name);
        return tree;
       }
     }
@@ -300,27 +323,26 @@ objc_tree_insert_class (Class class)
 {
   struct objc_list *list_node;
   objc_class_tree *tree;
-
+  
   list_node = __objc_class_tree_list;
   while (list_node)
     {
+      /* Try to insert the class in this class hierarchy.  */
       tree = __objc_tree_insert_class (list_node->head, class);
       if (tree)
        {
          list_node->head = tree;
-         break;
+         return;
        }
       else
        list_node = list_node->tail;
     }
-
-  /* If the list was finished but the class hasn't been inserted,
-     insert it here.  */
-  if (! list_node)
-    {
-      __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
-      __objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
-    }
+  
+  /* If the list was finished but the class hasn't been inserted, we
+     don't have an existing class hierarchy that can accomodate it.
+     Create a new one.  */
+  __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
+  __objc_class_tree_list->head = __objc_tree_insert_class (NULL, class);
 }
 
 /* Traverse tree in preorder. Used to send +load.  */
@@ -363,56 +385,67 @@ __objc_tree_print (objc_class_tree *tree, int level)
 #endif
 
 /* Walks on a linked list of methods in the reverse order and executes
-   all the methods corresponding to `op' selector. Walking in the
-   reverse order assures the +load of class is executed first and then
-   +load of categories because of the way in which categories are
-   added to the class methods.  */
+   all the methods corresponding to the `+load' selector.  Walking in
+   the reverse order assures the +load of class is executed first and
+   then +load of categories because of the way in which categories are
+   added to the class methods.  This function needs to be called with
+   the objc_runtime_mutex locked.  */
 static void
-__objc_send_message_in_list (struct objc_method_list *method_list, Class class, SEL op)
+__objc_send_load_using_method_list (struct objc_method_list *method_list, Class class)
 {
+  static SEL load_selector = 0;
   int i;
 
-  if (! method_list)
+  if (!method_list)
     return;
 
-  /* First execute the `op' message in the following method lists.  */
-  __objc_send_message_in_list (method_list->method_next, class, op);
+  /* This needs no lock protection because we are called with the
+     objc_runtime_mutex locked.  */
+  if (!load_selector)
+    load_selector = sel_registerName ("load");
+
+  /* method_list is a linked list of method lists; since we're
+     executing in reverse order, we need to do the next list before we
+     do this one.  */
+  __objc_send_load_using_method_list (method_list->method_next, class);
 
   /* Search the method list.  */
   for (i = 0; i < method_list->method_count; i++)
     {
       struct objc_method *mth = &method_list->method_list[i];
 
-      if (mth->method_name && sel_eq (mth->method_name, op)
+      /* We are searching for +load methods that we haven't executed
+        yet.  */
+      if (mth->method_name && sel_eq (mth->method_name, load_selector)
          && ! objc_hash_is_key_in_hash (__objc_load_methods, mth->method_imp))
        {
-         /* Add this method into the +load hash table.  */
+         /* Add this method into the +load hash table, so we won't
+            execute it again next time.  */
          objc_hash_add (&__objc_load_methods,
                         mth->method_imp,
                         mth->method_imp);
          
-         DEBUG_PRINTF ("sending +load in class: %s\n", class->name);
-         
-         /* The method was found and wasn't previously executed.  */
+         /* Call +load.  */
+         DEBUG_PRINTF (" begin of [%s +load]\n", class->name);
          (*mth->method_imp) ((id)class, mth->method_name);
+         DEBUG_PRINTF (" end of [%s +load]\n", class->name);
 
          break;
        }
     }
 }
 
+/* This function needs to be called with the objc_runtime_mutex
+   locked.  */
 static void
 __objc_send_load (objc_class_tree *tree,
                  int level __attribute__ ((__unused__)))
 {
-  static SEL load_sel = 0;
   Class class = tree->class;
   struct objc_method_list *method_list = class->class_pointer->methods;
 
-  if (! load_sel)
-    load_sel = sel_registerName ("load");
-
-  __objc_send_message_in_list (method_list, class, load_sel);
+  DEBUG_PRINTF ("+load: need to send load to class '%s'\n", class->name);
+  __objc_send_load_using_method_list (method_list, class);
 }
 
 static void
@@ -553,11 +586,11 @@ __objc_exec_class (struct objc_module *module)
   struct objc_list **cell;
 
   /* The table of selector references for this module.  */
-  SEL selectors = symtab->refs; 
+  struct objc_selector *selectors = symtab->refs;
 
   int i;
 
-  DEBUG_PRINTF ("received module: %s\n", module->name);
+  DEBUG_PRINTF ("\n__objc_exec_class (%p) - start processing module...\n", module);
 
   /* Check gcc version.  */
   init_check_module_version (module);
@@ -574,7 +607,9 @@ __objc_exec_class (struct objc_module *module)
       __objc_init_selector_tables ();
       __objc_init_class_tables ();
       __objc_init_dispatch_tables ();
-      __objc_class_tree_list = list_cons (NULL, __objc_class_tree_list);
+      duplicate_classes = objc_hash_new (8,
+                                        (hash_func_type)objc_hash_ptr,
+                                        objc_compare_ptrs);
       __objc_load_methods = objc_hash_new (128, 
                                           (hash_func_type)objc_hash_ptr,
                                           objc_compare_ptrs);
@@ -584,31 +619,20 @@ __objc_exec_class (struct objc_module *module)
       previous_constructors = 1;
     }
 
-  /* Save the module pointer for later processing. (not currently
-     used).  */
+  /* Save the module pointer so that later we remember to call +load
+     on all classes and categories on it.  */
   objc_mutex_lock (__objc_runtime_mutex);
   __objc_module_list = list_cons (module, __objc_module_list);
 
-  /* Replace referenced selectors from names to SEL's.  */
+  /* Replace referenced selectors from names to SELs.  */
   if (selectors)
     {
-      for (i = 0; selectors[i].sel_id; ++i)
-       {
-         const char *name, *type;
-         name = (char *) selectors[i].sel_id;
-         type = (char *) selectors[i].sel_types;
-         /* Constructors are constant static data so we can safely
-            store pointers to them in the runtime
-            structures. is_const == YES.  */
-         __sel_register_typed_name (name, type, 
-                                    (struct objc_selector *) &(selectors[i]),
-                                    YES);
-       }
+      DEBUG_PRINTF (" registering selectors\n");
+      __objc_register_selectors_from_module (selectors);
     }
 
   /* Parse the classes in the load module and gather selector
      information.  */
-  DEBUG_PRINTF ("gathering selectors from module: %s\n", module->name);
   for (i = 0; i < symtab->cls_def_cnt; ++i)
     {
       Class class = (Class) symtab->defs[i];
@@ -617,19 +641,29 @@ __objc_exec_class (struct objc_module *module)
       /* Make sure we have what we think.  */
       assert (CLS_ISCLASS (class));
       assert (CLS_ISMETA (class->class_pointer));
-      DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
+      DEBUG_PRINTF (" installing class '%s'\n", class->name);
+
+      /* Workaround for a bug in clang: Clang may set flags other than
+        _CLS_CLASS and _CLS_META even when compiling for the
+        traditional ABI (version 8), confusing our runtime.  Try to
+        wipe these flags out.  */
+      if (CLS_ISCLASS (class))
+       __CLS_INFO (class) = _CLS_CLASS;
+      else
+       __CLS_INFO (class) = _CLS_META;
 
       /* Initialize the subclass list to be NULL.  In some cases it
         isn't and this crashes the program.  */
       class->subclass_list = NULL;
 
-      __objc_init_class (class);
-
-      /* Check to see if the superclass is known in this point. If
-        it's not add the class to the unresolved_classes list.  */
-      if (superclass && ! objc_getClass (superclass))
-       unresolved_classes = list_cons (class, unresolved_classes);
-   }
+      if (__objc_init_class (class))
+       {
+         /* Check to see if the superclass is known in this point. If
+            it's not add the class to the unresolved_classes list.  */
+         if (superclass && ! objc_getClass (superclass))
+           unresolved_classes = list_cons (class, unresolved_classes);
+       }
+    }
 
   /* Process category information from the module.  */
   for (i = 0; i < symtab->cat_def_cnt; ++i)
@@ -641,11 +675,7 @@ __objc_exec_class (struct objc_module *module)
         methods.  */
       if (class)
        {
-
-         DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
-                       module->name,
-                       class->name);
-
+         DEBUG_PRINTF (" installing category '%s (%s)'\n", category->class_name, category->category_name);
          /* Do instance methods.  */
          if (category->instance_methods)
            class_add_method_list (class, category->instance_methods);
@@ -667,6 +697,7 @@ __objc_exec_class (struct objc_module *module)
        }
       else
        {
+         DEBUG_PRINTF (" delaying installation of category '%s (%s)'\n", category->class_name, category->category_name);
          /* The object to which the category methods belong can't be
             found.  Save the information.  */
          unclaimed_categories = list_cons (category, unclaimed_categories);
@@ -687,9 +718,7 @@ __objc_exec_class (struct objc_module *module)
       
       if (class)
        {
-         DEBUG_PRINTF ("attaching stored categories to object: %s\n",
-                       class->name);
-         
+         DEBUG_PRINTF (" installing (delayed) category '%s (%s)'\n", category->class_name, category->category_name);
          list_remove_head (cell);
          
          if (category->instance_methods)
@@ -723,17 +752,32 @@ __objc_exec_class (struct objc_module *module)
 
   objc_send_load ();
 
+  /* Check if there are no unresolved classes (ie, classes whose
+     superclass has not been loaded yet) and that the 'Object' class,
+     used as the class of classes, exist.  If so, it is worth
+     "resolving the class links" at this point, which will setup all
+     the class/superclass pointers.  */
+  if (!unresolved_classes && objc_getClass ("Object"))
+    {
+      DEBUG_PRINTF (" resolving class links\n");
+      __objc_resolve_class_links ();
+    }
+
   objc_mutex_unlock (__objc_runtime_mutex);
+
+  DEBUG_PRINTF ("__objc_exec_class (%p) - finished processing module...\n\n", module);
 }
 
+/* This function needs to be called with the objc_runtime_mutex
+   locked.  */
 static void
 objc_send_load (void)
 {
-  if (! __objc_module_list)
+  if (!__objc_module_list)
     return;
  
   /* Try to find out if all the classes loaded so far also have their
-     superclasses known to the runtime. We suppose that the objects
+     superclasses known to the runtime.  We suppose that the objects
      that are allocated in the +load method are in general of a class
      declared in the same module.  */
   if (unresolved_classes)
@@ -751,7 +795,7 @@ objc_send_load (void)
 
       /* If we still have classes for whom we don't have yet their
          super classes known to the runtime we don't send the +load
-         messages.  */
+         messages (and call the load callback) yet.  */
       if (unresolved_classes)
        return;
     }
@@ -762,7 +806,7 @@ objc_send_load (void)
     return;
 
   /* Iterate over all modules in the __objc_module_list and call on
-     them the __objc_create_classes_tree function. This function
+     them the __objc_create_classes_tree function.  This function
      creates a tree of classes that resembles the class hierarchy.  */
   list_mapcar (__objc_module_list,
               (void (*) (void *)) __objc_create_classes_tree);
@@ -780,7 +824,11 @@ objc_send_load (void)
       list_remove_head (&__objc_class_tree_list);
     }
 
-  list_mapcar (__objc_module_list, (void (*) (void *)) __objc_call_callback);
+  /* For each module, call the _objc_load_callback if any is
+     defined.  */
+  list_mapcar (__objc_module_list, (void (*) (void *)) __objc_call_load_callback);
+
+  /* Empty the list of modules.  */
   list_free (__objc_module_list);
   __objc_module_list = NULL;
 }
@@ -798,38 +846,65 @@ __objc_create_classes_tree (struct objc_module *module)
     {
       Class class = (Class) symtab->defs[i];
 
-      objc_tree_insert_class (class);
+      if (!objc_hash_is_key_in_hash (duplicate_classes, class))
+       objc_tree_insert_class (class);
     }
-}
 
-static void
-__objc_call_callback (struct objc_module *module)
-{
-  /* The runtime mutex is locked at this point.  */
-  struct objc_symtab *symtab = module->symtab;
-  int i;
-
-  /* Iterate thru classes defined in this module and call the callback
-     for each one.  */
-  for (i = 0; i < symtab->cls_def_cnt; i++)
-    {
-      Class class = (Class) symtab->defs[i];
-
-      /* Call the _objc_load_callback for this class.  */
-      if (_objc_load_callback)
-       _objc_load_callback (class, 0);
-    }
-
-  /* Call the _objc_load_callback for categories. Don't register the
-     instance methods as class methods for categories to root classes
-     since they were already added in the class.  */
-  for (i = 0; i < symtab->cat_def_cnt; i++)
+  /* Now iterate over "claimed" categories too (ie, categories that
+     extend a class that has already been loaded by the runtime), and
+     insert them in the classes tree hiearchy too.  Otherwise, if you
+     add a category, its +load method would not be called if the class
+     is already loaded in the runtime.  It the category is
+     "unclaimed", ie, we haven't loaded the main class yet, postpone
+     sending +load as we want to execute +load from the class before
+     we execute the one from the category.  */
+  for (i = 0; i < symtab->cat_def_cnt; ++i)
     {
       struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
       Class class = objc_getClass (category->class_name);
+      
+      /* If the class for the category exists then append its
+        methods.  */
+      if (class)
+       objc_tree_insert_class (class);
+    }
+}
 
-      if (_objc_load_callback)
-       _objc_load_callback (class, category);
+static void
+__objc_call_load_callback (struct objc_module *module)
+{
+  if (_objc_load_callback)
+    {
+      /* The runtime mutex is locked at this point.  */
+      struct objc_symtab *symtab = module->symtab;
+      int i;
+      
+      /* Iterate thru classes defined in this module and call the callback
+        for each one.  */
+      for (i = 0; i < symtab->cls_def_cnt; i++)
+       {
+         Class class = (Class) symtab->defs[i];
+       
+         if (!objc_hash_is_key_in_hash (duplicate_classes, class))
+           {
+             /* Call the _objc_load_callback for this class.  */
+             DEBUG_PRINTF (" calling the load callback for class '%s'\n", class->name);
+             _objc_load_callback (class, 0);
+           }
+       }
+      
+      /* Call the _objc_load_callback for categories.  Don't register
+        the instance methods as class methods for categories to root
+        classes since they were already added in the class.  */
+      for (i = 0; i < symtab->cat_def_cnt; i++)
+       {
+         struct objc_category *category = symtab->defs[i + symtab->cls_def_cnt];
+         Class class = objc_getClass (category->class_name);
+         
+         DEBUG_PRINTF (" calling the load callback for category '%s (%s)'\n",
+                       category->class_name, category->category_name);
+         _objc_load_callback (class, category);
+       }
     }
 }
 
@@ -844,27 +919,41 @@ init_check_module_version (struct objc_module *module)
     }
 }
 
-/* __objc_init_class must be called with __objc_runtime_mutex already locked.  */
-void
+/* __objc_init_class must be called with __objc_runtime_mutex already
+   locked.  Return YES if the class could be setup; return NO if the
+   class could not be setup because a class with the same name already
+   exists.  */
+BOOL
 __objc_init_class (Class class)
 {
   /* Store the class in the class table and assign class numbers.  */
-  __objc_add_class_to_hash (class);
-  
-  /* Register all of the selectors in the class and meta class.  */
-  __objc_register_selectors_from_class (class);
-  __objc_register_selectors_from_class ((Class) class->class_pointer);
-
-  /* Install the fake dispatch tables.  */
-  __objc_install_premature_dtable (class);
-  __objc_install_premature_dtable (class->class_pointer);
-
-  /* Register the instance methods as class methods, this is only done
-     for root classes.  */
-  __objc_register_instance_methods_to_class (class);
+  if (__objc_add_class_to_hash (class))
+    {
+      /* Register all of the selectors in the class and meta class.  */
+      __objc_register_selectors_from_class (class);
+      __objc_register_selectors_from_class ((Class) class->class_pointer);
+      
+      /* Install the fake dispatch tables.  */
+      __objc_install_premature_dtable (class);
+      __objc_install_premature_dtable (class->class_pointer);
+      
+      /* Register the instance methods as class methods, this is only
+        done for root classes.  */
+      __objc_register_instance_methods_to_class (class);
+      
+      if (class->protocols)
+       __objc_init_protocols (class->protocols);
 
-  if (class->protocols)
-    __objc_init_protocols (class->protocols);
+      return YES;
+    }
+  else
+    {
+      /* The module contains a duplicate class.  Remember it so that
+        we will ignore it later.  */
+      DEBUG_PRINTF (" duplicate class '%s' - will be ignored\n", class->name);
+      objc_hash_add (&duplicate_classes, class, class);
+      return NO;
+    }
 }
 
 /* __objc_init_protocol must be called with __objc_runtime_mutex