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