Finished rewriting the loader. It's now cleanly separated into logically-separate components, so there's a chance it might actually be maintainable...
parent
7c72069fd9
commit
bb8cafaa15
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* buffer.h defines a simple dynamic array that is used to store temporary
|
||||
* values for later processing. Define BUFFER_TYPE before including this file.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
static BUFFER_TYPE *buffered_object_buffer[BUFFER_SIZE];
|
||||
static BUFFER_TYPE **buffered_object_overflow;
|
||||
static int buffered_objects;
|
||||
static int buffered_object_overflow_space;
|
||||
|
||||
static void set_buffered_object_at_index(BUFFER_TYPE *cat, unsigned int i)
|
||||
{
|
||||
if (i < BUFFER_SIZE)
|
||||
{
|
||||
buffered_object_buffer[i] = cat;
|
||||
}
|
||||
else
|
||||
{
|
||||
i -= BUFFER_SIZE;
|
||||
if (NULL == buffered_object_overflow)
|
||||
{
|
||||
buffered_object_overflow =
|
||||
calloc(BUFFER_SIZE, sizeof(BUFFER_TYPE*));
|
||||
buffered_object_overflow_space = BUFFER_SIZE;
|
||||
}
|
||||
while (i > buffered_object_overflow_space)
|
||||
{
|
||||
buffered_object_overflow_space <<= 1;
|
||||
buffered_object_overflow = realloc(buffered_object_overflow,
|
||||
buffered_object_overflow_space * sizeof(BUFFER_TYPE*));
|
||||
}
|
||||
buffered_object_overflow[i] = cat;
|
||||
}
|
||||
}
|
||||
|
||||
static BUFFER_TYPE *buffered_object_at_index(unsigned int i)
|
||||
{
|
||||
if (i<BUFFER_SIZE)
|
||||
{
|
||||
return buffered_object_buffer[i];
|
||||
}
|
||||
return buffered_object_overflow[i-BUFFER_SIZE];
|
||||
}
|
||||
|
||||
static void compact_buffer(void)
|
||||
{
|
||||
// Move up all of the non-NULL pointers
|
||||
unsigned size = buffered_objects;
|
||||
unsigned insert = 0;
|
||||
for (unsigned i=0 ; i<size ; i++)
|
||||
{
|
||||
BUFFER_TYPE *c = buffered_object_at_index(i);
|
||||
if (c != NULL)
|
||||
{
|
||||
set_buffered_object_at_index(c, insert++);
|
||||
}
|
||||
}
|
||||
buffered_objects = insert;
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
|
||||
/**
|
||||
* The structure used to represent a category.
|
||||
*
|
||||
* This provides a set of new definitions that are used to replace those
|
||||
* contained within a class.
|
||||
*
|
||||
* Note: Objective-C 2 allows properties to be added to classes. The current
|
||||
* ABI does not provide a field for adding properties in categories. This is
|
||||
* likely to be added with ABI version 10. Until then, the methods created by
|
||||
* a declared property will work, but introspection on the property will not.
|
||||
*/
|
||||
struct objc_category
|
||||
{
|
||||
/**
|
||||
* The name of this category.
|
||||
*/
|
||||
const char *name;
|
||||
/**
|
||||
* The name of the class to which this category should be applied.
|
||||
*/
|
||||
const char *class_name;
|
||||
/**
|
||||
* The list of instance methods to add to the class.
|
||||
*/
|
||||
struct objc_method_list *instance_methods;
|
||||
/**
|
||||
* The list of class methods to add to the class.
|
||||
*/
|
||||
struct objc_method_list *class_methods;
|
||||
/**
|
||||
* The list of protocols adopted by this category.
|
||||
*/
|
||||
struct objc_protocol_list *protocols;
|
||||
};
|
||||
@ -0,0 +1,81 @@
|
||||
#include <stdio.h>
|
||||
#include "objc/runtime.h"
|
||||
#include "loader.h"
|
||||
|
||||
#define BUFFER_TYPE struct objc_category
|
||||
#include "buffer.h"
|
||||
|
||||
static void register_methods(struct objc_class *cls, struct objc_method_list *l)
|
||||
{
|
||||
if (NULL == l) { return; }
|
||||
|
||||
// Replace the method names with selectors.
|
||||
objc_register_selectors_from_list(l);
|
||||
// Add the method list at the head of the list of lists.
|
||||
l->next = cls->methods;
|
||||
cls->methods = l;
|
||||
// Update the dtable to catch the new methods.
|
||||
// FIXME: We can make this more efficient by simply passing the new method
|
||||
// list to the dtable and telling it only to update those methods.
|
||||
objc_update_dtable_for_class(cls);
|
||||
}
|
||||
|
||||
static void load_category(struct objc_category *cat, struct objc_class *class)
|
||||
{
|
||||
register_methods(class, cat->instance_methods);
|
||||
register_methods(class->isa, cat->class_methods);
|
||||
|
||||
if (cat->protocols)
|
||||
{
|
||||
objc_init_protocols(cat->protocols);
|
||||
cat->protocols->next = class->protocols;
|
||||
class->protocols = cat->protocols;
|
||||
}
|
||||
}
|
||||
|
||||
static BOOL try_load_category(struct objc_category *cat)
|
||||
{
|
||||
Class class = (Class)objc_getClass(cat->class_name);
|
||||
if (Nil != class)
|
||||
{
|
||||
load_category(cat, class);
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a category to its class, if the class is already loaded. Buffers
|
||||
* it for future resolution if not.
|
||||
*/
|
||||
void objc_try_load_category(struct objc_category *cat)
|
||||
{
|
||||
if (!try_load_category(cat))
|
||||
{
|
||||
set_buffered_object_at_index(cat, buffered_objects++);
|
||||
}
|
||||
}
|
||||
|
||||
void objc_load_buffered_categories(void)
|
||||
{
|
||||
BOOL shouldReshuffle = NO;
|
||||
|
||||
for (unsigned i=0 ; i<buffered_objects ; i++)
|
||||
{
|
||||
struct objc_category *c = buffered_object_at_index(i);
|
||||
if (NULL != c)
|
||||
{
|
||||
if (try_load_category(c))
|
||||
{
|
||||
set_buffered_object_at_index(NULL, i);
|
||||
shouldReshuffle = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldReshuffle)
|
||||
{
|
||||
compact_buffer();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,388 +0,0 @@
|
||||
/* GNU Objective C Runtime initialization
|
||||
Copyright (C) 1993, 1995, 1996, 1997, 2002, 2009
|
||||
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 the
|
||||
terms of the GNU General Public License as published by the Free Software
|
||||
Foundation; either version 3, or (at your option) any later version.
|
||||
|
||||
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
details.
|
||||
|
||||
Under Section 7 of GPL version 3, you are granted additional
|
||||
permissions described in the GCC Runtime Library Exception, version
|
||||
3.1, as published by the Free Software Foundation.
|
||||
|
||||
You should have received a copy of the GNU General Public License and
|
||||
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/>. */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "objc/runtime-legacy.h"
|
||||
#include "objc/encoding.h"
|
||||
#include "lock.h"
|
||||
#include "magic_objects.h"
|
||||
|
||||
/* The version number of this runtime. This must match the number
|
||||
defined in gcc (objc-act.c). */
|
||||
#define PROTOCOL_VERSION 2
|
||||
#define OBJC2_PROTOCOL_VERSION 3
|
||||
|
||||
void __objc_sync_init(void);
|
||||
void __objc_resolve_class_links(void);
|
||||
void objc_load_class(struct objc_class *class);
|
||||
|
||||
/* 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 */
|
||||
|
||||
/* Global runtime "write" mutex. */
|
||||
static mutex_t objc_runtime_mutex;
|
||||
objc_mutex_t __objc_runtime_mutex = &objc_runtime_mutex;
|
||||
|
||||
/* Number of threads that are alive. */
|
||||
int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
|
||||
|
||||
void __objc_register_selector_array(SEL selectors, unsigned long count);
|
||||
|
||||
/* Check compiler vs runtime version. */
|
||||
static void init_check_module_version (Module_t);
|
||||
|
||||
/* Assign isa links to protos. */
|
||||
void __objc_init_protocols (struct objc_protocol_list *protos);
|
||||
|
||||
/* Add protocol to class. */
|
||||
static void __objc_class_add_protocols (Class, struct objc_protocol_list *);
|
||||
|
||||
/* Is all categories/classes resolved? */
|
||||
BOOL __objc_dangling_categories = NO; /* !T:UNUSED */
|
||||
|
||||
/* Extern function used to reference the Object and NXConstantString
|
||||
classes. */
|
||||
|
||||
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 void
|
||||
objc_init_statics (void)
|
||||
{
|
||||
struct objc_list **cell = &uninitialized_statics;
|
||||
struct objc_static_instances **statics_in_module;
|
||||
|
||||
LOCK(__objc_runtime_mutex);
|
||||
|
||||
while (*cell)
|
||||
{
|
||||
int module_initialized = 1;
|
||||
|
||||
for (statics_in_module = (*cell)->head;
|
||||
*statics_in_module; statics_in_module++)
|
||||
{
|
||||
struct objc_static_instances *statics = *statics_in_module;
|
||||
Class class = objc_lookup_class (statics->class_name);
|
||||
if (strcmp(statics->class_name, "NXConstantString") == 0)
|
||||
{
|
||||
Class constStr = objc_lookup_class(CONSTANT_STRING_CLASS);
|
||||
if (constStr)
|
||||
{
|
||||
class = constStr;
|
||||
}
|
||||
}
|
||||
|
||||
if (! class)
|
||||
module_initialized = 0;
|
||||
/* Actually, the static's class_pointer will be NULL when we
|
||||
haven't been here before. However, the comparison is to be
|
||||
reminded of taking into account class posing and to think about
|
||||
possible semantics... */
|
||||
else if (class != statics->instances[0]->class_pointer)
|
||||
{
|
||||
id *inst;
|
||||
|
||||
for (inst = &statics->instances[0]; *inst; inst++)
|
||||
{
|
||||
(*inst)->class_pointer = class;
|
||||
|
||||
/* ??? Make sure the object will not be freed. With
|
||||
refcounting, invoke `-retain'. Without refcounting, do
|
||||
nothing and hope that `-free' will never be invoked. */
|
||||
|
||||
/* ??? Send the object an `-initStatic' or something to
|
||||
that effect now or later on? What are the semantics of
|
||||
statically allocated instances, besides the trivial
|
||||
NXConstantString, anyway? */
|
||||
}
|
||||
}
|
||||
}
|
||||
if (module_initialized)
|
||||
{
|
||||
/* Remove this module from the uninitialized list. */
|
||||
struct objc_list *this = *cell;
|
||||
*cell = this->tail;
|
||||
objc_free (this);
|
||||
}
|
||||
else
|
||||
cell = &(*cell)->tail;
|
||||
}
|
||||
|
||||
UNLOCK(__objc_runtime_mutex);
|
||||
} /* objc_init_statics */
|
||||
|
||||
void __objc_init_protocol_table(void);
|
||||
/* This function is called by constructor functions generated for each
|
||||
module compiled. (_GLOBAL_$I$...) The purpose of this function is
|
||||
to gather the module pointers so that they may be processed by the
|
||||
initialization routines as soon as possible. */
|
||||
|
||||
BOOL objc_check_abi_version(unsigned long version, unsigned long module_size);
|
||||
|
||||
void
|
||||
__objc_exec_class (Module_t module)
|
||||
{
|
||||
/* Have we processed any constructors previously? This flag is used to
|
||||
indicate that some global data structures need to be built. */
|
||||
static BOOL previous_constructors = 0;
|
||||
|
||||
static struct objc_list *unclaimed_categories = 0;
|
||||
|
||||
/* The symbol table (defined in objc-api.h) generated by gcc */
|
||||
Symtab_t symtab = module->symtab;
|
||||
|
||||
/* The statics in this module */
|
||||
struct objc_static_instances **statics
|
||||
= symtab->defs[symtab->cls_def_cnt + symtab->cat_def_cnt];
|
||||
|
||||
/* Entry used to traverse hash lists */
|
||||
struct objc_list **cell;
|
||||
|
||||
/* dummy counter */
|
||||
int i;
|
||||
|
||||
DEBUG_PRINTF ("received module: %s\n", module->name);
|
||||
|
||||
/* check compiler version */
|
||||
assert(objc_check_abi_version(module->version, module->size));
|
||||
|
||||
/* On the first call of this routine, initialize some data structures. */
|
||||
// FIXME: This should really be using a pthread_once or equivalent.
|
||||
if (! previous_constructors)
|
||||
{
|
||||
/* Initialize thread-safe system */
|
||||
INIT_LOCK(objc_runtime_mutex);
|
||||
__objc_init_thread_system ();
|
||||
__objc_sync_init();
|
||||
__objc_runtime_threads_alive = 1;
|
||||
|
||||
__objc_init_selector_tables ();
|
||||
__objc_init_protocol_table ();
|
||||
__objc_init_class_tables ();
|
||||
__objc_init_dispatch_tables ();
|
||||
previous_constructors = 1;
|
||||
}
|
||||
|
||||
/* Save the module pointer for later processing. (not currently used) */
|
||||
LOCK(__objc_runtime_mutex);
|
||||
|
||||
/* Replace referenced selectors from names to SEL's. */
|
||||
if (symtab->refs)
|
||||
{
|
||||
__objc_register_selector_array(symtab->refs, symtab->sel_ref_cnt);
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
objc_load_class((Class) symtab->defs[i]);
|
||||
}
|
||||
|
||||
/* Process category information from the module. */
|
||||
for (i = 0; i < symtab->cat_def_cnt; ++i)
|
||||
{
|
||||
Category_t category = symtab->defs[i + symtab->cls_def_cnt];
|
||||
Class class = objc_lookup_class (category->class_name);
|
||||
|
||||
/* If the class for the category exists then append its methods. */
|
||||
if (class)
|
||||
{
|
||||
|
||||
DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n",
|
||||
module->name,
|
||||
class->name);
|
||||
|
||||
/* Do instance methods. */
|
||||
if (category->instance_methods)
|
||||
class_add_method_list (class, category->instance_methods);
|
||||
|
||||
/* Do class methods. */
|
||||
if (category->class_methods)
|
||||
class_add_method_list ((Class) class->class_pointer,
|
||||
category->class_methods);
|
||||
|
||||
if (category->protocols)
|
||||
{
|
||||
__objc_init_protocols (category->protocols);
|
||||
__objc_class_add_protocols (class, category->protocols);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The object to which the category methods belong can't be found.
|
||||
Save the information. */
|
||||
unclaimed_categories = list_cons (category, unclaimed_categories);
|
||||
}
|
||||
}
|
||||
|
||||
if (statics)
|
||||
uninitialized_statics = list_cons (statics, uninitialized_statics);
|
||||
if (uninitialized_statics)
|
||||
objc_init_statics ();
|
||||
|
||||
/* Scan the unclaimed category hash. Attempt to attach any unclaimed
|
||||
categories to objects. */
|
||||
for (cell = &unclaimed_categories; *cell; )
|
||||
{
|
||||
Category_t category = (*cell)->head;
|
||||
Class class = objc_lookup_class (category->class_name);
|
||||
|
||||
if (class)
|
||||
{
|
||||
DEBUG_PRINTF ("attaching stored categories to object: %s\n",
|
||||
class->name);
|
||||
|
||||
list_remove_head (cell);
|
||||
|
||||
if (category->instance_methods)
|
||||
class_add_method_list (class, category->instance_methods);
|
||||
|
||||
if (category->class_methods)
|
||||
class_add_method_list ((Class) class->class_pointer,
|
||||
category->class_methods);
|
||||
|
||||
if (category->protocols)
|
||||
{
|
||||
__objc_init_protocols (category->protocols);
|
||||
__objc_class_add_protocols (class, category->protocols);
|
||||
}
|
||||
}
|
||||
else
|
||||
cell = &(*cell)->tail;
|
||||
}
|
||||
|
||||
if (unclaimed_proto_list && objc_lookup_class ("Protocol"))
|
||||
{
|
||||
list_mapcar (unclaimed_proto_list,
|
||||
(void (*) (void *))__objc_init_protocols);
|
||||
list_free (unclaimed_proto_list);
|
||||
unclaimed_proto_list = 0;
|
||||
}
|
||||
|
||||
__objc_resolve_class_links();
|
||||
|
||||
UNLOCK(__objc_runtime_mutex);
|
||||
}
|
||||
|
||||
void __objc_compute_ivar_offsets(Class class);
|
||||
|
||||
|
||||
struct objc_protocol *__objc_unique_protocol(struct objc_protocol*);
|
||||
void __objc_init_protocols (struct objc_protocol_list *protos)
|
||||
{
|
||||
size_t i;
|
||||
static Class proto_class = 0;
|
||||
static Class proto_class2 = 0;
|
||||
|
||||
if (! protos)
|
||||
return;
|
||||
|
||||
LOCK(__objc_runtime_mutex);
|
||||
|
||||
if (! proto_class)
|
||||
proto_class = objc_lookup_class ("Protocol");
|
||||
if (! proto_class2)
|
||||
proto_class2 = objc_lookup_class ("Protocol2");
|
||||
|
||||
/* Protocol2 will always exist if Protocol exists */
|
||||
if (! proto_class2)
|
||||
{
|
||||
unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
|
||||
UNLOCK(__objc_runtime_mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
#if 0
|
||||
assert (protos->next == 0); /* only single ones allowed */
|
||||
#endif
|
||||
|
||||
for (i = 0; i < protos->count; i++)
|
||||
{
|
||||
struct objc_protocol *aProto = protos->list[i];
|
||||
switch (((size_t)aProto->class_pointer))
|
||||
{
|
||||
case PROTOCOL_VERSION:
|
||||
{
|
||||
/* assign class pointer */
|
||||
aProto->class_pointer = proto_class;
|
||||
|
||||
/* init super protocols */
|
||||
__objc_init_protocols (aProto->protocol_list);
|
||||
protos->list[i] = __objc_unique_protocol (aProto);
|
||||
break;
|
||||
}
|
||||
// FIXME: Initialize empty protocol by updating fields to reflect
|
||||
// those of a real protocol with the same name
|
||||
case OBJC2_PROTOCOL_VERSION:
|
||||
{
|
||||
/* assign class pointer */
|
||||
aProto->class_pointer = proto_class2;
|
||||
/* init super protocols */
|
||||
__objc_init_protocols (aProto->protocol_list);
|
||||
protos->list[i] = __objc_unique_protocol (aProto);
|
||||
}
|
||||
default:
|
||||
{
|
||||
if (protos->list[i]->class_pointer != proto_class
|
||||
&& protos->list[i]->class_pointer != proto_class2)
|
||||
{
|
||||
objc_error (nil, OBJC_ERR_PROTOCOL_VERSION,
|
||||
"Version %d doesn't match runtime protocol version %d\n",
|
||||
(int) ((char *) protos->list[i]->class_pointer
|
||||
- (char *) 0),
|
||||
PROTOCOL_VERSION);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UNLOCK(__objc_runtime_mutex);
|
||||
}
|
||||
|
||||
static void
|
||||
__objc_class_add_protocols (Class class, struct objc_protocol_list *protos)
|
||||
{
|
||||
/* Well... */
|
||||
if (! protos)
|
||||
return;
|
||||
|
||||
/* Add it... */
|
||||
protos->next = class->protocols;
|
||||
class->protocols = protos;
|
||||
}
|
||||
@ -0,0 +1,92 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "objc/runtime.h"
|
||||
#include "lock.h"
|
||||
#include "loader.h"
|
||||
#include "magic_objects.h"
|
||||
|
||||
/**
|
||||
* Runtime lock. This is exposed in
|
||||
*/
|
||||
static mutex_t objc_runtime_mutex;
|
||||
void *__objc_runtime_mutex = &objc_runtime_mutex;
|
||||
|
||||
void __objc_sync_init(void);
|
||||
void __objc_init_selector_tables(void);
|
||||
void __objc_init_protocol_table(void);
|
||||
void __objc_init_class_tables(void);
|
||||
void __objc_init_dispatch_tables(void);
|
||||
|
||||
/* Number of threads that are alive. */
|
||||
int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
|
||||
|
||||
void __objc_exec_class(struct objc_module_abi_8 *module)
|
||||
{
|
||||
static BOOL first_run = YES;
|
||||
|
||||
// Check that this module uses an ABI version that we recognise.
|
||||
// In future, we should pass the ABI version to the class / category load
|
||||
// functions so that we can change various structures more easily.
|
||||
assert(objc_check_abi_version(module->version, module->size));
|
||||
|
||||
if (first_run)
|
||||
{
|
||||
// Create the main runtime lock. This is not safe in theory, but in
|
||||
// practice the first time that this function is called will be in the
|
||||
// loader, from the main thread. Future loaders may run concurrently,
|
||||
// but that is likely to break the semantics of a lot of languages, so
|
||||
// we don't have to worry about it for a long time.
|
||||
//
|
||||
// The only case when this can potentially go badly wrong is when a
|
||||
// pure-C main() function spawns two threads which then, concurrently,
|
||||
// call dlopen() or equivalent, and the platform's implementation of
|
||||
// this does not perform any synchronization.
|
||||
INIT_LOCK(objc_runtime_mutex);
|
||||
// Create the lock used to protect the creation of hidden classes by
|
||||
// @synchronize()
|
||||
__objc_sync_init();
|
||||
|
||||
// Create the various tables that the runtime needs.
|
||||
__objc_init_selector_tables();
|
||||
__objc_init_protocol_table();
|
||||
__objc_init_class_tables();
|
||||
__objc_init_dispatch_tables();
|
||||
first_run = NO;
|
||||
}
|
||||
|
||||
// The runtime mutex is held for the entire duration of a load. It does
|
||||
// not need to be acquired or released in any of the called load functions.
|
||||
LOCK_UNTIL_RETURN(__objc_runtime_mutex);
|
||||
|
||||
struct objc_symbol_table_abi_8 *symbols = module->symbol_table;
|
||||
// Register all of the selectors used in this module.
|
||||
if (symbols->selectors)
|
||||
{
|
||||
objc_register_selector_array(symbols->selectors,
|
||||
symbols->selector_count);
|
||||
}
|
||||
|
||||
unsigned short defs = 0;
|
||||
// Load the classes from this module
|
||||
for (unsigned short i=0 ; i<symbols->class_count ; i++)
|
||||
{
|
||||
objc_load_class(symbols->definitions[defs++]);
|
||||
}
|
||||
// Load the categories from this module
|
||||
for (unsigned short i=0 ; i<symbols->category_count; i++)
|
||||
{
|
||||
objc_try_load_category(symbols->definitions[defs++]);
|
||||
}
|
||||
// Load the static instances
|
||||
struct objc_static_instance_list **statics = (void*)symbols->definitions[defs];
|
||||
while (NULL != statics && NULL != *statics)
|
||||
{
|
||||
objc_init_statics(*(statics++));
|
||||
}
|
||||
|
||||
// Fix up the class links for loaded classes.
|
||||
objc_resolve_class_links();
|
||||
// Load categories and statics that were deferred.
|
||||
objc_load_buffered_categories();
|
||||
objc_init_buffered_statics();
|
||||
}
|
||||
@ -0,0 +1,67 @@
|
||||
#ifndef __OBJC_LOADER_H_INCLUDED
|
||||
#define __OBJC_LOADER_H_INCLUDED
|
||||
#include "category.h"
|
||||
#include "method_list.h"
|
||||
#include "module.h"
|
||||
#include "class.h"
|
||||
#include "protocol.h"
|
||||
|
||||
/**
|
||||
* Checks whether it is safe to load a module with the specified version and
|
||||
* module size. This depends on whether another module with an incompatible
|
||||
* ABI has already been loaded.
|
||||
*/
|
||||
BOOL objc_check_abi_version(unsigned long version, unsigned long module_size);
|
||||
/**
|
||||
* Initializes a protocol list, uniquing the protocols in the list.
|
||||
*/
|
||||
void objc_init_protocols(struct objc_protocol_list *protocols);
|
||||
/**
|
||||
* Registers a set of selectors from a method list.
|
||||
*/
|
||||
void objc_register_selectors_from_list(struct objc_method_list *l);
|
||||
/**
|
||||
* Register all of the (unregistered) selectors that are used in a class.
|
||||
*/
|
||||
void objc_register_selectors_from_class(Class class);
|
||||
/**
|
||||
* Registers all of the selectors in an array.
|
||||
*/
|
||||
void objc_register_selector_array(SEL selectors, unsigned long count);
|
||||
/**
|
||||
* Loads a class into the runtime system. If possible, the class is resolved
|
||||
* (inserted into the class tree) immediately. If its superclass is not yet
|
||||
* resolved, it is enqueued for later resolution.
|
||||
*/
|
||||
void objc_load_class(struct objc_class *class);
|
||||
/**
|
||||
* Resolves classes that have not yet been resolved, if their superclasses have
|
||||
* subsequently been loaded.
|
||||
*/
|
||||
void objc_resolve_class_links(void);
|
||||
/**
|
||||
* Attaches a category to its class, if the class is already loaded. Buffers
|
||||
* it for future resolution if not.
|
||||
*/
|
||||
void objc_try_load_category(struct objc_category *cat);
|
||||
/**
|
||||
* Tries to load all of the categories that could not previously be loaded
|
||||
* because their classes were not yet loaded.
|
||||
*/
|
||||
void objc_load_buffered_categories(void);
|
||||
/**
|
||||
* Updates the dispatch table for a class.
|
||||
*/
|
||||
void objc_update_dtable_for_class(Class cls);
|
||||
/**
|
||||
* Initialises a list of static object instances belonging to the same class if
|
||||
* possible, or defers initialisation until the class has been loaded it not.
|
||||
*/
|
||||
void objc_init_statics(struct objc_static_instance_list *statics);
|
||||
/**
|
||||
* Tries again to initialise static instances which could not be initialised
|
||||
* earlier.
|
||||
*/
|
||||
void objc_init_buffered_statics(void);
|
||||
|
||||
#endif //__OBJC_LOADER_H_INCLUDED
|
||||
@ -0,0 +1,99 @@
|
||||
|
||||
enum PropertyAttributeKind
|
||||
{
|
||||
/**
|
||||
* Property has no attributes.
|
||||
*/
|
||||
OBJC_PR_noattr = 0x00,
|
||||
/**
|
||||
* The property is declared read-only.
|
||||
*/
|
||||
OBJC_PR_readonly = (1<<0),
|
||||
/**
|
||||
* The property has a getter.
|
||||
*/
|
||||
OBJC_PR_getter = (1<<1),
|
||||
/**
|
||||
* The property has assign semantics.
|
||||
*/
|
||||
OBJC_PR_assign = (1<<2),
|
||||
/**
|
||||
* The property is declared read-write.
|
||||
*/
|
||||
OBJC_PR_readwrite = (1<<3),
|
||||
/**
|
||||
* Property has retain semantics.
|
||||
*/
|
||||
OBJC_PR_retain = (1<<4),
|
||||
/**
|
||||
* Property has copy semantics.
|
||||
*/
|
||||
OBJC_PR_copy = (1<<5),
|
||||
/**
|
||||
* Property is marked as non-atomic.
|
||||
*/
|
||||
OBJC_PR_nonatomic = (1<<6),
|
||||
/**
|
||||
* Property has setter.
|
||||
*/
|
||||
OBJC_PR_setter = (1<<7)
|
||||
};
|
||||
|
||||
/**
|
||||
* Structure used for property enumeration. Note that property enumeration is
|
||||
* currently quite broken on OS X, so achieving full compatibility there is
|
||||
* impossible. Instead, we strive to achieve com
|
||||
*/
|
||||
struct objc_property
|
||||
{
|
||||
/**
|
||||
* Name of this property.
|
||||
*/
|
||||
const char *name;
|
||||
/**
|
||||
* Attributes for this property. Made by ORing together
|
||||
* PropertyAttributeKinds.
|
||||
*/
|
||||
const char attributes;
|
||||
/**
|
||||
* Flag set if the property is synthesized.
|
||||
*/
|
||||
const char isSynthesized;
|
||||
/**
|
||||
* Name of the set method for this property.
|
||||
*/
|
||||
const char *setter_name;
|
||||
/**
|
||||
* Type encoding of the setter for this property.
|
||||
*/
|
||||
const char *setter_types;
|
||||
/**
|
||||
* Name of the getter for this property.
|
||||
*/
|
||||
const char *getter_name;
|
||||
/**
|
||||
* Type encoding for the get method for this property.
|
||||
*/
|
||||
const char *getter_types;
|
||||
};
|
||||
|
||||
/**
|
||||
* List of property inrospection data.
|
||||
*/
|
||||
struct objc_property_list
|
||||
{
|
||||
/**
|
||||
* Number of properties in this array.
|
||||
*/
|
||||
int count;
|
||||
/*
|
||||
* UNUSED. In future, categories will be allowed to add properties and
|
||||
* then this will be used to link the declarations together.
|
||||
*/
|
||||
struct objc_property_list *next;
|
||||
/**
|
||||
* List of properties.
|
||||
*/
|
||||
struct objc_property properties[1];
|
||||
};
|
||||
|
||||
@ -0,0 +1,115 @@
|
||||
#include "selector.h"
|
||||
|
||||
struct objc_method_description_list
|
||||
{
|
||||
/**
|
||||
* Number of method descriptions in this list.
|
||||
*/
|
||||
int count;
|
||||
/**
|
||||
* Methods in this list. Note: these selectors are NOT resolved. The name
|
||||
* field points to the name, not to the index of the uniqued version of the
|
||||
* name. You must not use them for dispatch.
|
||||
*/
|
||||
struct objc_selector methods[1];
|
||||
};
|
||||
|
||||
/**
|
||||
* Definition of the Protocol type. Protocols are objects, but are rarely used
|
||||
* as such.
|
||||
*/
|
||||
#ifdef __OBJC__
|
||||
@interface Protocol
|
||||
#else
|
||||
struct objc_protocol
|
||||
#endif
|
||||
{
|
||||
/** Class pointer. */
|
||||
id isa;
|
||||
/**
|
||||
* The name of this protocol. Two protocols are regarded as identical if
|
||||
* they have the same name.
|
||||
*/
|
||||
char *name;
|
||||
/**
|
||||
* The list of protocols that this protocol conforms to.
|
||||
*/
|
||||
struct objc_protocol_list *protocol_list;
|
||||
/**
|
||||
* List of instance methods required by this protocol.
|
||||
*/
|
||||
struct objc_method_description_list *instance_methods;
|
||||
/**
|
||||
* List of class methods required by this protocol.
|
||||
*/
|
||||
struct objc_method_description_list *class_methods;
|
||||
}
|
||||
#ifdef __OBJC__
|
||||
@end
|
||||
#else
|
||||
;
|
||||
#endif
|
||||
|
||||
#ifdef __OBJC__
|
||||
@interface Protocol2
|
||||
{
|
||||
#else
|
||||
typedef struct objc_protocol2
|
||||
{
|
||||
/**
|
||||
* Redefinition of the superclass ivars in the C version.
|
||||
*/
|
||||
id isa;
|
||||
char *name;
|
||||
struct objc_protocol_list *protocol_list;
|
||||
struct objc_method_description_list *instance_methods;
|
||||
struct objc_method_description_list *class_methods;
|
||||
#endif
|
||||
/**
|
||||
* Instance methods that are declared as optional for this protocol.
|
||||
*/
|
||||
struct objc_method_description_list *optional_instance_methods;
|
||||
/**
|
||||
* Class methods that are declared as optional for this protocol.
|
||||
*/
|
||||
struct objc_method_description_list *optional_class_methods;
|
||||
/**
|
||||
* Properties that are required by this protocol.
|
||||
*/
|
||||
struct objc_property_list *properties;
|
||||
/**
|
||||
* Optional properties.
|
||||
*/
|
||||
struct objc_property_list *optional_properties;
|
||||
}
|
||||
#ifdef __OBJC__
|
||||
@end
|
||||
#else
|
||||
Protocol2;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* List of protocols. Attached to a class or a category by the compiler and to
|
||||
* a class by the runtime.
|
||||
*/
|
||||
struct objc_protocol_list
|
||||
{
|
||||
/**
|
||||
* Additional protocol lists. Loading a category that declares protocols
|
||||
* will cause a new list to be prepended using this pointer to the protocol
|
||||
* list for the class. Unlike methods, protocols can not be overridden,
|
||||
* although it is possible for a protocol to appear twice.
|
||||
*/
|
||||
struct objc_protocol_list *next;
|
||||
/**
|
||||
* The number of protocols in this list.
|
||||
*/
|
||||
size_t count;
|
||||
/**
|
||||
* An array of protocols. Actually contains count elements, not 1.
|
||||
*
|
||||
* The instances in this array may be any version of protocols.
|
||||
*/
|
||||
Protocol2 *list[1];
|
||||
};
|
||||
|
||||
@ -0,0 +1,71 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "objc/runtime.h"
|
||||
#include "module.h"
|
||||
#include "magic_objects.h"
|
||||
|
||||
#define BUFFER_TYPE struct objc_static_instance_list
|
||||
#include "buffer.h"
|
||||
|
||||
static BOOL try_init_statics(struct objc_static_instance_list *statics)
|
||||
{
|
||||
const char *class_name = statics->class_name;
|
||||
|
||||
// This is a horrible hack.
|
||||
//
|
||||
// Very bad things happen when you have more than one constant string class
|
||||
// used in a program. Unfortunately, GCC defaults to using
|
||||
// NXConstantString, and if you forget to specify
|
||||
// -fconstant-string-class=NSConstantString for some compilation units then
|
||||
// you will end up with some NSConstantString instances and some
|
||||
// NXConstantString instances. This is a mess. We hack around this by
|
||||
// silently assuming that the user meant NSConstantString when they said
|
||||
// NXConstantString if NSConstantString is set as the constant string class
|
||||
// in magic_objects.h
|
||||
if (strcmp(class_name, "NXConstantString") == 0)
|
||||
{
|
||||
class_name = CONSTANT_STRING_CLASS;
|
||||
}
|
||||
|
||||
Class class = (Class)objc_getClass(class_name);
|
||||
|
||||
if (Nil == class)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
for (id *instance=statics->instances ; nil!=*instance ; instance++)
|
||||
{
|
||||
(*instance)->isa = class;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
void objc_init_statics(struct objc_static_instance_list *statics)
|
||||
{
|
||||
if (!try_init_statics(statics))
|
||||
{
|
||||
set_buffered_object_at_index(statics, buffered_objects++);
|
||||
}
|
||||
}
|
||||
|
||||
void objc_init_buffered_statics(void)
|
||||
{
|
||||
BOOL shouldReshuffle = NO;
|
||||
|
||||
for (unsigned i=0 ; i<buffered_objects ; i++)
|
||||
{
|
||||
struct objc_static_instance_list *c = buffered_object_at_index(i);
|
||||
if (NULL != c)
|
||||
{
|
||||
if (try_init_statics(c))
|
||||
{
|
||||
set_buffered_object_at_index(NULL, i);
|
||||
shouldReshuffle = YES;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldReshuffle)
|
||||
{
|
||||
compact_buffer();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue