]> git.saurik.com Git - apple/libsystem.git/blame - init.c
Libsystem-1213.tar.gz
[apple/libsystem.git] / init.c
CommitLineData
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
A
28
29#include <TargetConditionals.h> // for TARGET_OS_EMBEDDED
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
A
39
40// system library initialisers
afef526e
A
41extern void mach_init(void); // from libsystem_kernel.dylib
42extern void __libplatform_init(void *future_use, const char *envp[], const char *apple[], const struct ProgramVars *vars);
43extern 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
44extern void __malloc_init(const char *apple[]); // from libsystem_malloc.dylib
45extern void __keymgr_initializer(void); // from libkeymgr.dylib
46extern void _dyld_initializer(void); // from libdyld.dylib
47extern void libdispatch_init(void); // from libdispatch.dylib
48extern void _libxpc_initializer(void); // from libxpc.dylib
2fbbb8fa
A
49extern void _libsecinit_initializer(void); // from libsecinit.dylib
50
ec8f0a04 51
bdffa7b9
A
52// signal malloc stack logging that initialisation has finished
53extern void __stack_logging_early_finished(void); // form libsystem_c.dylib
54
2fbbb8fa
A
55// clear qos tsd (from pthread)
56extern void _pthread_clear_qos_tsd(mach_port_t) __attribute__((weak_import));
57
ec8f0a04 58// system library atfork handlers
afef526e
A
59extern void _pthread_fork_prepare(void);
60extern void _pthread_fork_parent(void);
61extern void _pthread_fork_child(void);
62extern void _pthread_fork_child_postinit(void);
2fbbb8fa 63extern void _pthread_exit_if_canceled(int);
afef526e
A
64
65extern void dispatch_atfork_prepare(void);
66extern void dispatch_atfork_parent(void);
67extern void dispatch_atfork_child(void);
68
69extern void _malloc_fork_prepare(void);
70extern void _malloc_fork_parent(void);
71extern void _malloc_fork_child(void);
bdffa7b9
A
72
73extern void _mach_fork_child(void);
bdffa7b9
A
74extern void _notify_fork_child(void);
75extern void _dyld_fork_child(void);
76extern void xpc_atfork_prepare(void);
77extern void xpc_atfork_parent(void);
78extern void xpc_atfork_child(void);
afef526e
A
79extern void _libSC_info_fork_prepare(void);
80extern void _libSC_info_fork_parent(void);
81extern void _libSC_info_fork_child(void);
82extern void _asl_fork_child(void);
ec8f0a04 83
2fbbb8fa
A
84#if defined(HAVE_SYSTEM_CORESERVICES)
85// libsystem_coreservices.dylib
86extern void _libcoreservices_fork_child(void);
87extern char *_dirhelper(int, char *, size_t);
88#endif
89
90#if TARGET_IPHONE_SIMULATOR
91// no-op _pthread_clear_qos_tsd in the simulator, as its an upcall from libsyscall
92#define _pthread_clear_qos_tsd NULL
93#endif
94
ec8f0a04 95// advance decls for below;
bdffa7b9
A
96void libSystem_atfork_prepare(void);
97void libSystem_atfork_parent(void);
98void libSystem_atfork_child(void);
ec8f0a04 99
2fbbb8fa
A
100// libsyscall_initializer() initializes all of libSystem.dylib
101// <rdar://problem/4892197>
102__attribute__((constructor))
103static void
104libSystem_initializer(int argc,
105 const char* argv[],
106 const char* envp[],
107 const char* apple[],
108 const struct ProgramVars* vars)
ec8f0a04 109{
afef526e 110 static const struct _libkernel_functions libkernel_funcs = {
2fbbb8fa
A
111 .version = 3,
112 // V1 functions
ec8f0a04 113 .dlsym = dlsym,
afef526e
A
114 .malloc = malloc,
115 .free = free,
116 .realloc = realloc,
117 ._pthread_exit_if_canceled = _pthread_exit_if_canceled,
2fbbb8fa
A
118 // V2 functions (removed)
119 // V3 functions
120 .pthread_clear_qos_tsd = _pthread_clear_qos_tsd,
afef526e
A
121 };
122
123 static const struct _libpthread_functions libpthread_funcs = {
2fbbb8fa 124 .version = 2,
afef526e 125 .exit = exit,
2fbbb8fa
A
126 .malloc = malloc,
127 .free = free,
128 };
129
130 static const struct _libc_functions libc_funcs = {
131 .version = 1,
132 .atfork_prepare = libSystem_atfork_prepare,
133 .atfork_parent = libSystem_atfork_parent,
134 .atfork_child = libSystem_atfork_child,
135#if defined(HAVE_SYSTEM_CORESERVICES)
136 .dirhelper = _dirhelper,
137#endif
ec8f0a04
A
138 };
139
afef526e 140 __libkernel_init(&libkernel_funcs, envp, apple, vars);
ec8f0a04 141
afef526e 142 __libplatform_init(NULL, envp, apple, vars);
afef526e 143 __pthread_init(&libpthread_funcs, envp, apple, vars);
2fbbb8fa 144 _libc_initializer(&libc_funcs, envp, apple, vars);
afef526e
A
145
146 // TODO: Move __malloc_init before __libc_init after breaking malloc's upward link to Libc
147 __malloc_init(apple);
148
2fbbb8fa
A
149#if !TARGET_IPHONE_SIMULATOR
150 /* <rdar://problem/9664631> */
151 __keymgr_initializer();
152#endif
153
ec8f0a04
A
154 _dyld_initializer();
155 libdispatch_init();
ec8f0a04 156 _libxpc_initializer();
bdffa7b9 157
2fbbb8fa
A
158#if !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
159 _libsecinit_initializer();
160#endif
161
bdffa7b9
A
162 __stack_logging_early_finished();
163
2fbbb8fa 164
bdffa7b9
A
165 /* <rdar://problem/11588042>
166 * C99 standard has the following in section 7.5(3):
167 * "The value of errno is zero at program startup, but is never set
168 * to zero by any library function."
169 */
170 errno = 0;
ec8f0a04
A
171}
172
173/*
2fbbb8fa
A
174 * libSystem_atfork_{prepare,parent,child}() are called by libc during fork(2).
175 * They call the corresponding atfork handlers for other libsystem components.
ec8f0a04 176 */
2fbbb8fa
A
177void
178libSystem_atfork_prepare(void)
ec8f0a04 179{
afef526e 180 _libSC_info_fork_prepare();
ec8f0a04 181 xpc_atfork_prepare();
afef526e
A
182 dispatch_atfork_prepare();
183 _pthread_fork_prepare();
184 _malloc_fork_prepare();
ec8f0a04
A
185}
186
2fbbb8fa
A
187void
188libSystem_atfork_parent(void)
ec8f0a04 189{
afef526e
A
190 _malloc_fork_parent();
191 _pthread_fork_parent();
192 dispatch_atfork_parent();
ec8f0a04 193 xpc_atfork_parent();
afef526e 194 _libSC_info_fork_parent();
ec8f0a04
A
195}
196
2fbbb8fa
A
197void
198libSystem_atfork_child(void)
ec8f0a04
A
199{
200 _dyld_fork_child();
afef526e
A
201 _pthread_fork_child();
202 _malloc_fork_child();
203 dispatch_atfork_child();
ec8f0a04 204
ec8f0a04 205 _mach_fork_child();
ec8f0a04 206 _libc_fork_child();
2fbbb8fa
A
207
208#if defined(HAVE_SYSTEM_CORESERVICES)
209 _libcoreservices_fork_child();
210#endif
211
afef526e 212 _asl_fork_child();
ec8f0a04 213 _notify_fork_child();
ec8f0a04 214 xpc_atfork_child();
afef526e 215 _libSC_info_fork_child();
ec8f0a04 216
afef526e 217 _pthread_fork_child_postinit();
ec8f0a04
A
218}
219
220/*
221 * Old crt1.o glue used to call through mach_init_routine which was used to initialize libSystem.
222 * LibSystem now auto-initializes but mach_init_routine is left for binary compatibility.
223 */
bdffa7b9 224static void mach_init_old(void) {}
ec8f0a04
A
225void (*mach_init_routine)(void) = &mach_init_old;
226
227/*
228 * This __crashreporter_info__ symbol is for all non-dylib parts of libSystem.
229 */
230const char *__crashreporter_info__;
231asm (".desc __crashreporter_info__, 0x10");