]> git.saurik.com Git - apple/libsystem.git/blob - init.c
b62772f8853df27a2132db74a376dd26eb12d3fa
[apple/libsystem.git] / init.c
1 /*
2 * Copyright (c) 2007, 2008, 2011-2013 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
29 #include <TargetConditionals.h> // for TARGET_OS_*
30
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <libc_private.h>
34 #include <pthread.h>
35 #include <pthread/private.h>
36 #include <dlfcn.h>
37 #include <errno.h>
38 #include <_libkernel_init.h> // Must be after voucher_private.h
39
40 #include <mach-o/dyld_priv.h>
41
42 // system library initialisers
43 extern void mach_init(void); // from libsystem_kernel.dylib
44 extern void __libplatform_init(void *future_use, const char *envp[], const char *apple[], const struct ProgramVars *vars);
45 extern void __pthread_init(const struct _libpthread_functions *libpthread_funcs, const char *envp[], const char *apple[], const struct ProgramVars *vars); // from libsystem_pthread.dylib
46 extern void __malloc_init(const char *apple[]); // from libsystem_malloc.dylib
47 extern void __keymgr_initializer(void); // from libkeymgr.dylib
48 extern void _dyld_initializer(void); // from libdyld.dylib
49 extern void libdispatch_init(void); // from libdispatch.dylib
50 extern void _libxpc_initializer(void); // from libxpc.dylib
51 extern void _libsecinit_initializer(void); // from libsecinit.dylib
52 extern void _libtrace_init(void); // from libsystem_trace.dylib
53 extern void _container_init(const char *apple[]); // from libsystem_containermanager.dylib
54 extern void __libdarwin_init(void); // from libsystem_darwin.dylib
55
56
57 // signal malloc stack logging that initialisation has finished
58 extern void __stack_logging_early_finished(void); // form libsystem_c.dylib
59
60 // clear qos tsd (from pthread)
61 extern void _pthread_clear_qos_tsd(mach_port_t) __attribute__((weak_import));
62
63 // system library atfork handlers
64 extern void _pthread_atfork_prepare(void);
65 extern void _pthread_atfork_parent(void);
66 extern void _pthread_atfork_child(void);
67 extern void _pthread_atfork_prepare_handlers();
68 extern void _pthread_atfork_parent_handlers(void);
69 extern void _pthread_atfork_child_handlers(void);
70 extern void _pthread_exit_if_canceled(int);
71
72 extern void dispatch_atfork_prepare(void);
73 extern void dispatch_atfork_parent(void);
74 extern void dispatch_atfork_child(void);
75
76 extern void _libtrace_fork_child(void);
77
78 extern void _malloc_fork_prepare(void);
79 extern void _malloc_fork_parent(void);
80 extern void _malloc_fork_child(void);
81
82 extern void _mach_fork_child(void);
83 extern void _notify_fork_child(void);
84 extern void _dyld_fork_child(void);
85 extern void xpc_atfork_prepare(void);
86 extern void xpc_atfork_parent(void);
87 extern void xpc_atfork_child(void);
88 extern void _libSC_info_fork_prepare(void);
89 extern void _libSC_info_fork_parent(void);
90 extern void _libSC_info_fork_child(void);
91 extern void _asl_fork_child(void);
92
93 #if defined(HAVE_SYSTEM_CORESERVICES)
94 // libsystem_coreservices.dylib
95 extern void _libcoreservices_fork_child(void);
96 extern char *_dirhelper(int, char *, size_t);
97 #endif
98
99 #if TARGET_OS_EMBEDDED && !TARGET_OS_WATCH && !__LP64__
100 extern void _vminterpose_init(void);
101 #endif
102
103 // advance decls for below;
104 void libSystem_atfork_prepare(void);
105 void libSystem_atfork_parent(void);
106 void libSystem_atfork_child(void);
107
108 // libsyscall_initializer() initializes all of libSystem.dylib
109 // <rdar://problem/4892197>
110 __attribute__((constructor))
111 static void
112 libSystem_initializer(int argc,
113 const char* argv[],
114 const char* envp[],
115 const char* apple[],
116 const struct ProgramVars* vars)
117 {
118 static const struct _libkernel_functions libkernel_funcs = {
119 .version = 3,
120 // V1 functions
121 .dlsym = dlsym,
122 .malloc = malloc,
123 .free = free,
124 .realloc = realloc,
125 ._pthread_exit_if_canceled = _pthread_exit_if_canceled,
126 // V2 functions (removed)
127 // V3 functions
128 .pthread_clear_qos_tsd = _pthread_clear_qos_tsd,
129 };
130
131 static const struct _libpthread_functions libpthread_funcs = {
132 .version = 2,
133 .exit = exit,
134 .malloc = malloc,
135 .free = free,
136 };
137
138 static const struct _libc_functions libc_funcs = {
139 .version = 1,
140 .atfork_prepare = libSystem_atfork_prepare,
141 .atfork_parent = libSystem_atfork_parent,
142 .atfork_child = libSystem_atfork_child,
143 #if defined(HAVE_SYSTEM_CORESERVICES)
144 .dirhelper = _dirhelper,
145 #endif
146 };
147
148 __libkernel_init(&libkernel_funcs, envp, apple, vars);
149
150 __libplatform_init(NULL, envp, apple, vars);
151
152 __pthread_init(&libpthread_funcs, envp, apple, vars);
153
154 _libc_initializer(&libc_funcs, envp, apple, vars);
155
156 // TODO: Move __malloc_init before __libc_init after breaking malloc's upward link to Libc
157 __malloc_init(apple);
158
159 #if TARGET_OS_OSX
160 /* <rdar://problem/9664631> */
161 __keymgr_initializer();
162 #endif
163
164 _dyld_initializer();
165
166 libdispatch_init();
167 _libxpc_initializer();
168
169 // must be initialized after dispatch
170 _libtrace_init();
171
172 #if !(TARGET_OS_EMBEDDED || TARGET_OS_SIMULATOR)
173 _libsecinit_initializer();
174 #endif
175
176 #if TARGET_OS_EMBEDDED
177 _container_init(apple);
178 #endif
179
180 __libdarwin_init();
181
182 __stack_logging_early_finished();
183
184 #if TARGET_OS_EMBEDDED && TARGET_OS_IOS && !__LP64__
185 _vminterpose_init();
186 #endif
187
188 #if !TARGET_OS_IPHONE
189 /* <rdar://problem/22139800> - Preserve the old behavior of apple[] for
190 * programs that haven't linked against newer SDK.
191 */
192 #define APPLE0_PREFIX "executable_path="
193 if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_11){
194 if (strncmp(apple[0], APPLE0_PREFIX, strlen(APPLE0_PREFIX)) == 0){
195 apple[0] = apple[0] + strlen(APPLE0_PREFIX);
196 }
197 }
198 #endif
199
200 /* <rdar://problem/11588042>
201 * C99 standard has the following in section 7.5(3):
202 * "The value of errno is zero at program startup, but is never set
203 * to zero by any library function."
204 */
205 errno = 0;
206 }
207
208 /*
209 * libSystem_atfork_{prepare,parent,child}() are called by libc during fork(2).
210 */
211 void
212 libSystem_atfork_prepare(void)
213 {
214 // first call client prepare handlers registered with pthread_atfork()
215 _pthread_atfork_prepare_handlers();
216
217 // second call hardwired fork prepare handlers for Libsystem components
218 // in the _reverse_ order of library initalization above
219 _libSC_info_fork_prepare();
220 xpc_atfork_prepare();
221 dispatch_atfork_prepare();
222 _malloc_fork_prepare();
223 _pthread_atfork_prepare();
224 }
225
226 void
227 libSystem_atfork_parent(void)
228 {
229 // first call hardwired fork parent handlers for Libsystem components
230 // in the order of library initalization above
231 _pthread_atfork_parent();
232 _malloc_fork_parent();
233 dispatch_atfork_parent();
234 xpc_atfork_parent();
235 _libSC_info_fork_parent();
236
237 // second call client parent handlers registered with pthread_atfork()
238 _pthread_atfork_parent_handlers();
239 }
240
241 void
242 libSystem_atfork_child(void)
243 {
244 // first call hardwired fork child handlers for Libsystem components
245 // in the order of library initalization above
246 _dyld_fork_child();
247 _pthread_atfork_child();
248 _mach_fork_child();
249 _malloc_fork_child();
250 _libc_fork_child(); // _arc4_fork_child calls malloc
251 dispatch_atfork_child();
252 #if defined(HAVE_SYSTEM_CORESERVICES)
253 _libcoreservices_fork_child();
254 #endif
255 _asl_fork_child();
256 _notify_fork_child();
257 xpc_atfork_child();
258 _libtrace_fork_child();
259 _libSC_info_fork_child();
260
261 // second call client parent handlers registered with pthread_atfork()
262 _pthread_atfork_child_handlers();
263 }
264
265 /*
266 * Old crt1.o glue used to call through mach_init_routine which was used to initialize libSystem.
267 * LibSystem now auto-initializes but mach_init_routine is left for binary compatibility.
268 */
269 static void mach_init_old(void) {}
270 void (*mach_init_routine)(void) = &mach_init_old;
271
272 /*
273 * This __crashreporter_info__ symbol is for all non-dylib parts of libSystem.
274 */
275 const char *__crashreporter_info__;
276 asm (".desc __crashreporter_info__, 0x10");