Wolfgang Thallergcc16 merge934c3885 months agoHistory
1043 lines · 34.3 KB · C
RawDownload
/* GNU Objective C Runtime initialization    Copyright (C) 1993-2026 Free Software Foundation, Inc.   Contributed by Kresten Krab Thorup   +load support contributed by Ovidiu Predescu <ovidiu@net-community.com> This file is part of GCC. GCC is free software; you can redistribute it and/or modify it under theterms of the GNU General Public License as published by the Free SoftwareFoundation; either version 3, or (at your option) any later version. GCC is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE.  See the GNU General Public License for moredetails. Under Section 7 of GPL version 3, you are granted additionalpermissions described in the GCC Runtime Library Exception, version3.1, as published by the Free Software Foundation. You should have received a copy of the GNU General Public License anda 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"#include "objc/thr.h"#include "objc-private/hash.h"#include "objc-private/objc-list.h" #include "objc-private/module-abi-8.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(),				       __objc_protocols_add_protocol()				       __objc_protocols_register_selectors() */#include "objc-private/accessors.h" /* For __objc_accessors_init() */ /* The version number of this runtime.  This must match the number   defined in gcc (objc-act.c).  */#define OBJC_VERSION 8#define PROTOCOL_VERSION 2 /* 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   links.  */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 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   (eg, __class_table_lock in class.c); to avoid deadlocks, these   routines must make sure that they never acquire any other lock   while holding their own local lock.  Ie, they should lock, execute   some C code that does not perform any calls to other runtime   functions which may potentially lock different locks, then unlock.   If they need to perform any calls to other runtime functions that   may potentially lock other locks, then they should use the global   __objc_runtime_mutex.  */objc_mutex_t __objc_runtime_mutex = 0; /* Number of threads that are alive.  */int __objc_runtime_threads_alive = 1;			/* !T:MUTEX */ /* Check compiler vs runtime version.  */static void init_check_module_version (struct objc_module *); /* Assign isa links to protos.  */static void __objc_init_protocols (struct objc_protocol_list *protos); /* Assign isa link to a protocol, and register it.  */static void __objc_init_protocol (struct objc_protocol *protocol); /* Add protocol to class.  */static void __objc_class_add_protocols (Class, struct objc_protocol_list *); /* Load callback hook.  */void (*_objc_load_callback) (Class class, struct objc_category *category) = 0; /* !T:SAFE */ /* Are all categories/classes resolved ?  */BOOL __objc_dangling_categories = NO;           /* !T:UNUSED */ /* Sends +load to all classes and categories in certain   situations.  */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   and the classes in its nodes receive the +load message if these   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); /* 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); /* 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;   /* 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   trees represent different class hierarchies.  */static struct objc_list *__objc_class_tree_list = NULL; /* Keeps the +load methods who have been already executed. This hash   should not be destroyed during the execution of the program.  */static cache_ptr __objc_load_methods = NULL; /* This function is used when building the class tree used to send   ordinately the +load message to all classes needing it.  The tree   is really needed so that superclasses will get the message before   subclasses.    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   sent to the class (/an object of that class) the class links will   be resolved, which will replace the super_class pointers with   pointers to the actual superclasses.    Unfortunately, the tree might also contain classes which had been   loaded previously, and whose class links have already been   resolved.    This function returns the superclass of a class in both cases, and   can be used to build the determine the class relationships while   building the tree.  */static Class  class_superclass_of_class (Class class){  char *super_class_name;   /* If the class links have been resolved, use the resolved     links.  */  if (CLS_ISRESOLV (class))    return class->super_class;    /* Else, 'class' has not yet been resolved.  This means that its     super_class pointer is really the name of the super class (rather     than a pointer to the actual superclass).  */  super_class_name = (char *)class->super_class;   /* Return Nil for a root class.  */  if (super_class_name == NULL)    return Nil;   /* Lookup the superclass of non-root classes.  */  return objc_getClass (super_class_name);}  /* Creates a tree of classes whose topmost class is directly inherited   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;   DEBUG_PRINTF ("create_tree_of_subclasses_inherited_from:");  DEBUG_PRINTF (" bottom_class = %s, upper = %s\n",		(bottom_class ? bottom_class->name : NULL),		(upper ? upper->name : NULL));   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));      tree->class = superclass;      tree->subclasses = list_cons (prev, tree->subclasses);      superclass = class_superclass_of_class (superclass);      prev = tree;    }   return tree;} /* Insert the `class' into the proper place in the `tree' class   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   directly.  */static objc_class_tree *__objc_tree_insert_class (objc_class_tree *tree, Class class){  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);      return tree;    }  else if (class_superclass_of_class (class) == tree->class)    {      /* If class is a direct subclass of tree->class then add class	 to the list of subclasses. First check to see if it wasn't	 already inserted.  */      struct objc_list *list = tree->subclasses;      objc_class_tree *node;       while (list)	{	  /* Class has been already inserted; do nothing just return	     the tree.  */	  if (((objc_class_tree *) list->head)->class == class)	    {	      DEBUG_PRINTF (" 2. class %s was previously inserted\n",			    class->name);	      return tree;	    }	  list = list->tail;	}       /* Create a new node class and insert it into the list of	 subclasses.  */      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);      return tree;    }  else    {      /* The class is not a direct subclass of tree->class.  Search         for class's superclasses in the list of subclasses.  */      struct objc_list *subclasses = tree->subclasses;       /* Precondition: the class must be a subclass of tree->class;         otherwise return NULL to indicate our caller that it must         take the next tree.  */      if (! class_is_subclass_of_class (class, tree->class))	return NULL;       for (; subclasses != NULL; subclasses = subclasses->tail)	{	  Class aClass = ((objc_class_tree *) (subclasses->head))->class; 	  if (class_is_subclass_of_class (class, aClass))	    {	      /* If we found one of class's superclasses we insert the	         class into its subtree and return the original tree	         since nothing has been changed.  */	      subclasses->head		  = __objc_tree_insert_class (subclasses->head, class); 	      DEBUG_PRINTF (" 4. class %s inserted\n", class->name);	      return tree;	    }	}       /* We haven't found a subclass of `class' in the `subclasses'         list.  Create a new tree of classes whose topmost class is a         direct subclass of tree->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);	return tree;      }    }} /* This function inserts `class' in the right tree hierarchy classes.  */static voidobjc_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;	  return;	}      else	list_node = list_node->tail;    }    /* If the list was finished but the class hasn't been inserted, we     don't have an existing class hierarchy that can accommodate 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.  */static voidobjc_preorder_traverse (objc_class_tree *tree,			int level,			void (*function) (objc_class_tree *, int)){  struct objc_list *node;   (*function) (tree, level);  for (node = tree->subclasses; node; node = node->tail)    objc_preorder_traverse (node->head, level + 1, function);} /* Traverse tree in postorder. Used to destroy a tree.  */static voidobjc_postorder_traverse (objc_class_tree *tree,			 int level,			 void (*function) (objc_class_tree *, int)){  struct objc_list *node;   for (node = tree->subclasses; node; node = node->tail)    objc_postorder_traverse (node->head, level + 1, function);  (*function) (tree, level);} /* Used to print a tree class hierarchy.  */#ifdef DEBUGstatic void__objc_tree_print (objc_class_tree *tree, int level){  int i;   for (i = 0; i < level; i++)    printf ("  ");  printf ("%s\n", tree->class->name);}#endif /* Walks on a linked list of methods in the reverse order and executes   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_load_using_method_list (struct objc_method_list *method_list, Class class){  static SEL load_selector = 0;  int i;   if (!method_list)    return;   /* 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];       /* 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, so we won't	     execute it again next time.  */	  objc_hash_add (&__objc_load_methods,			 mth->method_imp,			 mth->method_imp);	  	  /* 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__))){  Class class = tree->class;  struct objc_method_list *method_list = class->class_pointer->methods;   DEBUG_PRINTF ("+load: need to send load to class '%s'\n", class->name);  __objc_send_load_using_method_list (method_list, class);} static void__objc_destroy_class_tree_node (objc_class_tree *tree,				int level __attribute__ ((__unused__))){  objc_free (tree);} /* This is used to check if the relationship between two classes   before the runtime completely installs the classes.  */static BOOLclass_is_subclass_of_class (Class class, Class superclass){  for (; class != Nil;)    {      if (class == superclass)	return YES;      class = class_superclass_of_class (class);    }   return NO;} /* This list contains all the classes in the runtime system for whom   their superclasses are not yet known to the runtime.  */static struct objc_list *unresolved_classes = 0; /* Extern function used to reference the Object class.  */extern void __objc_force_linking (void); void__objc_force_linking (void){  extern void __objc_linking (void);  __objc_linking ();} /* Run through the statics list, removing modules as soon as all its   statics have been initialized.  */static voidobjc_init_statics (void){  struct objc_list **cell = &uninitialized_statics;  struct objc_static_instances **statics_in_module;   objc_mutex_lock (__objc_runtime_mutex);   while (*cell)    {      int module_initialized = 1;       for (statics_in_module = (*cell)->head;

Lines 1–500 of 1043.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t