]> git.saurik.com Git - apple/libsystem.git/blob - init.c
a78293c36239503dda8ca61db2cfd487f57da51f
[apple/libsystem.git] / init.c
1 /*
2 * Copyright (c) 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #ifdef __DYNAMIC__
29
30 #include <TargetConditionals.h> // for TARGET_OS_EMBEDDED
31
32 #include <_libkernel_init.h>
33 #include <dlfcn.h>
34
35 struct ProgramVars; /* forward reference */
36
37 // system library initialisers
38 extern void bootstrap_init(void); // from liblaunch.dylib
39 extern void mach_init(void); // from libsystem_mach.dylib
40 extern void pthread_init(void); // from libc.a
41 extern void __libc_init(const struct ProgramVars *vars, void (*atfork_prepare)(void), void (*atfork_parent)(void), void (*atfork_child)(void), const char *apple[]); // from libc.a
42 extern void __keymgr_initializer(void); // from libkeymgr.a
43 extern void _dyld_initializer(void); // from libdyld.a
44 extern void libdispatch_init(void); // from libdispatch.a
45 extern void _libxpc_initializer(void); // from libxpc
46
47 // system library atfork handlers
48 extern void _cthread_fork_prepare();
49 extern void _cthread_fork_parent();
50 extern void _cthread_fork_child();
51 extern void _cthread_fork_child_postinit();
52
53 extern void _mach_fork_child();
54 extern void _cproc_fork_child();
55 extern void _libc_fork_child();
56 extern void _notify_fork_child();
57 extern void _dyld_fork_child();
58 extern void xpc_atfork_prepare();
59 extern void xpc_atfork_parent();
60 extern void xpc_atfork_child();
61
62 // advance decls for below;
63 void libSystem_atfork_prepare();
64 void libSystem_atfork_parent();
65 void libSystem_atfork_child();
66
67 // from mig_support.c in libc
68 mach_port_t _mig_get_reply_port();
69 void _mig_set_reply_port(mach_port_t);
70
71 void cthread_set_errno_self(int);
72 int* __error(void);
73
74 /*
75 * libsyscall_initializer() initializes all of libSystem.dylib <rdar://problem/4892197>
76 */
77 static __attribute__((constructor))
78 void libSystem_initializer(int argc, const char* argv[], const char* envp[], const char* apple[], const struct ProgramVars* vars)
79 {
80 _libkernel_functions_t libkernel_funcs = {
81 .get_reply_port = _mig_get_reply_port,
82 .set_reply_port = _mig_set_reply_port,
83 .get_errno = __error,
84 .set_errno = cthread_set_errno_self,
85 .dlsym = dlsym,
86 };
87
88 _libkernel_init(libkernel_funcs);
89
90 bootstrap_init();
91 mach_init();
92 pthread_init();
93 __libc_init(vars, libSystem_atfork_prepare, libSystem_atfork_parent, libSystem_atfork_child, apple);
94 __keymgr_initializer();
95 _dyld_initializer();
96 libdispatch_init();
97 #if !TARGET_OS_EMBEDDED || __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000 // __IPHONE_5_0
98 _libxpc_initializer();
99 #endif
100 }
101
102 /*
103 * libSystem_atfork_{prepare,parent,child}() are called by libc when we fork, then we deal with running fork handlers
104 * for everyone else.
105 */
106 void libSystem_atfork_prepare()
107 {
108 #if !TARGET_OS_EMBEDDED || __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000 // __IPHONE_5_0
109 xpc_atfork_prepare();
110 #endif
111 _cthread_fork_prepare();
112 }
113
114 void libSystem_atfork_parent()
115 {
116 _cthread_fork_parent();
117 #if !TARGET_OS_EMBEDDED || __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000 // __IPHONE_5_0
118 xpc_atfork_parent();
119 #endif
120 }
121
122 void libSystem_atfork_child()
123 {
124 _dyld_fork_child();
125 _cthread_fork_child();
126
127 bootstrap_init();
128 _mach_fork_child();
129 _cproc_fork_child();
130 _libc_fork_child();
131 _notify_fork_child();
132 #if !TARGET_OS_EMBEDDED || __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000 // __IPHONE_5_0
133 xpc_atfork_child();
134 #endif
135
136 _cthread_fork_child_postinit();
137 }
138
139 /*
140 * Old crt1.o glue used to call through mach_init_routine which was used to initialize libSystem.
141 * LibSystem now auto-initializes but mach_init_routine is left for binary compatibility.
142 */
143 static void mach_init_old() {}
144 void (*mach_init_routine)(void) = &mach_init_old;
145
146 /*
147 * This __crashreporter_info__ symbol is for all non-dylib parts of libSystem.
148 */
149 const char *__crashreporter_info__;
150 asm (".desc __crashreporter_info__, 0x10");
151
152 #endif /* __DYNAMIC__ */