init.c 34 KB

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