-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.c
203 lines (181 loc) · 6.38 KB
/
loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdlib.h>
#include <assert.h>
#include "objc/runtime.h"
#include "objc/objc-auto.h"
#include "objc/objc-arc.h"
#include "lock.h"
#include "loader.h"
#include "visibility.h"
#ifdef ENABLE_GC
#include <gc/gc.h>
#endif
#include <stdio.h>
/**
* Runtime lock. This is exposed in
*/
PRIVATE mutex_t runtime_mutex;
LEGACY void *__objc_runtime_mutex = &runtime_mutex;
void init_alias_table(void);
void init_arc(void);
void init_class_tables(void);
void init_dispatch_tables(void);
void init_gc(void);
void init_protocol_table(void);
void init_selector_tables(void);
void init_trampolines(void);
void objc_send_load_message(Class class);
void log_selector_memory_usage(void);
static void log_memory_stats(void)
{
log_selector_memory_usage();
}
/* Number of threads that are alive. */
int __objc_runtime_threads_alive = 1; /* !T:MUTEX */
// libdispatch hooks for registering threads
__attribute__((weak)) void (*dispatch_begin_thread_4GC)(void);
__attribute__((weak)) void (*dispatch_end_thread_4GC)(void);
__attribute__((weak)) void *(*_dispatch_begin_NSAutoReleasePool)(void);
__attribute__((weak)) void (*_dispatch_end_NSAutoReleasePool)(void *);
void __objc_load_module(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));
if (first_run)
{
#if ENABLE_GC
init_gc();
#endif
// 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(runtime_mutex);
// Create the various tables that the runtime needs.
init_selector_tables();
init_protocol_table();
init_class_tables();
init_dispatch_tables();
init_alias_table();
init_arc();
init_trampolines();
first_run = NO;
if (dispatch_begin_thread_4GC != 0) {
dispatch_begin_thread_4GC = objc_registerThreadWithCollector;
}
if (dispatch_end_thread_4GC != 0) {
dispatch_end_thread_4GC = objc_unregisterThreadWithCollector;
}
if (_dispatch_begin_NSAutoReleasePool != 0) {
_dispatch_begin_NSAutoReleasePool = objc_autoreleasePoolPush;
}
if (_dispatch_end_NSAutoReleasePool != 0) {
_dispatch_end_NSAutoReleasePool = objc_autoreleasePoolPop;
}
}
// 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_RUNTIME_FOR_SCOPE();
struct objc_symbol_table_abi_8 *symbols = module->symbol_table;
// Register all of the selectors used in this module.
if (symbols->selectors)
{
if (module->version < 11) {
// the module has the old selector format
objc_register_selector_array8(symbols->selectors,
symbols->selector_count);
}
else {
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++));
}
// Load categories and statics that were deferred.
objc_load_buffered_categories();
objc_init_buffered_statics();
}
void __objc_resolve_module(struct objc_module_abi_8 *module)
{
LOCK_RUNTIME_FOR_SCOPE();
// Fix up the class links for loaded classes.
objc_resolve_class_links();
if (NULL != module) {
struct objc_symbol_table_abi_8 *symbols = module->symbol_table;
unsigned int category_start = symbols->class_count;
for (unsigned short i=0 ; i<symbols->category_count; i++)
{
struct objc_category *cat = (struct objc_category*)
symbols->definitions[category_start++];
Class class = (Class)objc_getClass(cat->class_name);
if ((Nil != class) &&
objc_test_class_flag(class, objc_class_flag_resolved))
{
objc_send_load_message(class);
}
}
}
}
#ifdef WINOBJC
// WinObjC breaks loading into two deterministic stages: class registration
// and class resolution. The original GNUstep ABI as implemented in Clang
// emits calls to __objc_exec_class into the user static init section,
// interleaving them with other static initializers and subjecting them to
// static initialization ordering issues. +load can therefore be called on one class
// when another class's module hasn't even been registered.
// The WinObjC ABI preferentially emits objc_module_abi_8*s into a binary section
// that's then registered and resolved (in that order) once on load.
//
// For backwards compatibility, however, it must support __objc_exec_class-based registration.
// To do so, this code batches all executed modules into a linked list for future resolution.
// This ensures that +load is only called after all referenced classes have completed
// registration and dtable installation.
static struct objc_module_abi_8 *legacy_module_head;
static void __objc_push_legacy_module(struct objc_module_abi_8 *module) {
LOCK_RUNTIME_FOR_SCOPE();
// repurpose module->name for an intrusive linked list; this is just like
// unresolved_class_{next,prev} in class_table.c
module->name = (char*)legacy_module_head;
legacy_module_head = module;
}
void __objc_resolve_legacy_modules(void) {
LOCK_RUNTIME_FOR_SCOPE();
struct objc_module_abi_8 *module = legacy_module_head;
legacy_module_head = NULL;
for(; module; module = (struct objc_module_abi_8 *)module->name) {
__objc_resolve_module(module);
}
}
#endif
void __objc_exec_class(struct objc_module_abi_8 *module)
{
__objc_load_module(module);
#ifdef WINOBJC
__objc_push_legacy_module(module);
#else
__objc_resolve_module(module);
#endif
}