]> git.saurik.com Git - apple/libsystem.git/blob - init.c
Libsystem-1238.tar.gz
[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
54
55 // signal malloc stack logging that initialisation has finished
56 extern void __stack_logging_early_finished(void); // form libsystem_c.dylib
57
58 // clear qos tsd (from pthread)
59 extern void _pthread_clear_qos_tsd(mach_port_t) __attribute__((weak_import));
60
61 // system library atfork handlers
62 extern void _pthread_fork_prepare(void);
63 extern void _pthread_fork_parent(void);
64 extern void _pthread_fork_child(void);
65 extern void _pthread_fork_child_postinit(void);
66 extern void _pthread_exit_if_canceled(int);
67
68 extern void dispatch_atfork_prepare(void);
69 extern void dispatch_atfork_parent(void);
70 extern void dispatch_atfork_child(void);
71
72 extern void _libtrace_fork_child(void);
73
74 extern void _malloc_fork_prepare(void);
75 extern void _malloc_fork_parent(void);
76 extern void _malloc_fork_child(void);
77
78 extern void _mach_fork_child(void);
79 extern void _notify_fork_child(void);
80 extern void _dyld_fork_child(void);
81 extern void xpc_atfork_prepare(void);
82 extern void xpc_atfork_parent(void);
83 extern void xpc_atfork_child(void);
84 extern void _libSC_info_fork_prepare(void);
85 extern void _libSC_info_fork_parent(void);
86 extern void _libSC_info_fork_child(void);
87 extern void _asl_fork_child(void);
88
89 #if defined(HAVE_SYSTEM_CORESERVICES)
90 // libsystem_coreservices.dylib
91 extern void _libcoreservices_fork_child(void);
92 extern char *_dirhelper(int, char *, size_t);
93 #endif
94
95 #if TARGET_OS_EMBEDDED && !TARGET_OS_WATCH && !__LP64__
96 extern void _vminterpose_init(void);
97 #endif
98
99 // advance decls for below;
100 void libSystem_atfork_prepare(void);
101 void libSystem_atfork_parent(void);
102 void libSystem_atfork_child(void);
103
104 // libsyscall_initializer() initializes all of libSystem.dylib
105 // <rdar://problem/4892197>
106 __attribute__((constructor))
107 static void
108 libSystem_initializer(int argc,
109 const char* argv[],
110 const char* envp[],
111 const char* apple[],
112 const struct ProgramVars* vars)
113 {
114 static const struct _libkernel_functions libkernel_funcs = {
115 .version = 3,
116 // V1 functions
117 .dlsym = dlsym,
118 .malloc = malloc,
119 .free = free,
120 .realloc = realloc,
121 ._pthread_exit_if_canceled = _pthread_exit_if_canceled,
122 // V2 functions (removed)
123 // V3 functions
124 .pthread_clear_qos_tsd = _pthread_clear_qos_tsd,
125 };
126
127 static const struct _libpthread_functions libpthread_funcs = {
128 .version = 2,
129 .exit = exit,
130 .malloc = malloc,
131 .free = free,
132 };
133
134 static const struct _libc_functions libc_funcs = {
135 .version = 1,
136 .atfork_prepare = libSystem_atfork_prepare,
137 .atfork_parent = libSystem_atfork_parent,
138 .atfork_child = libSystem_atfork_child,
139 #if defined(HAVE_SYSTEM_CORESERVICES)
140 .dirhelper = _dirhelper,
141 #endif
142 };
143
144 __libkernel_init(&libkernel_funcs, envp, apple, vars);
145
146 __libplatform_init(NULL, envp, apple, vars);
147
148 __pthread_init(&libpthread_funcs, envp, apple, vars);
149
150 _libc_initializer(&libc_funcs, envp, apple, vars);
151
152 // TODO: Move __malloc_init before __libc_init after breaking malloc's upward link to Libc
153 __malloc_init(apple);
154
155 #if !TARGET_OS_SIMULATOR && !TARGET_OS_TV && !TARGET_OS_WATCH
156 /* <rdar://problem/9664631> */
157 __keymgr_initializer();
158 #endif
159
160 _dyld_initializer();
161
162 libdispatch_init();
163 _libxpc_initializer();
164
165 #if !(TARGET_OS_EMBEDDED || TARGET_OS_SIMULATOR)
166 _libsecinit_initializer();
167 #endif
168
169 __stack_logging_early_finished();
170
171 #if TARGET_OS_EMBEDDED && !TARGET_OS_WATCH && !__LP64__
172 _vminterpose_init();
173 #endif
174
175 _libtrace_init(); // must be initialized after dispatch
176
177 #if !TARGET_OS_IPHONE
178 /* <rdar://problem/22139800> - Preserve the old behavior of apple[] for
179 * programs that haven't linked against newer SDK.
180 */
181 #define APPLE0_PREFIX "executable_path="
182 if (dyld_get_program_sdk_version() < DYLD_MACOSX_VERSION_10_11){
183 if (strncmp(apple[0], APPLE0_PREFIX, strlen(APPLE0_PREFIX)) == 0){
184 apple[0] = apple[0] + strlen(APPLE0_PREFIX);
185 }
186 }
187 #endif
188
189 /* <rdar://problem/11588042>
190 * C99 standard has the following in section 7.5(3):
191 * "The value of errno is zero at program startup, but is never set
192 * to zero by any library function."
193 */
194 errno = 0;
195 }
196
197 /*
198 * libSystem_atfork_{prepare,parent,child}() are called by libc during fork(2).
199 * They call the corresponding atfork handlers for other libsystem components.
200 */
201 void
202 libSystem_atfork_prepare(void)
203 {
204 _libSC_info_fork_prepare();
205 xpc_atfork_prepare();
206 dispatch_atfork_prepare();
207 _pthread_fork_prepare();
208 _malloc_fork_prepare();
209 }
210
211 void
212 libSystem_atfork_parent(void)
213 {
214 _malloc_fork_parent();
215 _pthread_fork_parent();
216 dispatch_atfork_parent();
217 xpc_atfork_parent();
218 _libSC_info_fork_parent();
219 }
220
221 void
222 libSystem_atfork_child(void)
223 {
224 _dyld_fork_child();
225 _pthread_fork_child();
226 _malloc_fork_child();
227 dispatch_atfork_child();
228
229 _mach_fork_child();
230 _libc_fork_child();
231
232 #if defined(HAVE_SYSTEM_CORESERVICES)
233 _libcoreservices_fork_child();
234 #endif
235
236 _asl_fork_child();
237 _notify_fork_child();
238 xpc_atfork_child();
239 _libSC_info_fork_child();
240
241 _pthread_fork_child_postinit();
242 _libtrace_fork_child(); // no prep work required for the fork
243 }
244
245 /*
246 * Old crt1.o glue used to call through mach_init_routine which was used to initialize libSystem.
247 * LibSystem now auto-initializes but mach_init_routine is left for binary compatibility.
248 */
249 static void mach_init_old(void) {}
250 void (*mach_init_routine)(void) = &mach_init_old;
251
252 /*
253 * This __crashreporter_info__ symbol is for all non-dylib parts of libSystem.
254 */
255 const char *__crashreporter_info__;
256 asm (".desc __crashreporter_info__, 0x10");