2 * Copyright (c) 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <TargetConditionals.h> // for TARGET_OS_EMBEDDED
32 #include <pthread/private.h>
33 #include <_libkernel_init.h>
37 #include <TargetConditionals.h>
39 struct ProgramVars
; /* forward reference */
41 // system library initialisers
42 extern void bootstrap_init(void); // from liblaunch.dylib
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 __libc_init(const struct ProgramVars
*vars
, void (*atfork_prepare
)(void), void (*atfork_parent
)(void), void (*atfork_child
)(void), const char *apple
[]); // from libsystem_c.dylib
47 extern void __malloc_init(const char *apple
[]); // from libsystem_malloc.dylib
48 extern void __keymgr_initializer(void); // from libkeymgr.dylib
49 extern void _dyld_initializer(void); // from libdyld.dylib
50 extern void libdispatch_init(void); // from libdispatch.dylib
51 extern void _libxpc_initializer(void); // from libxpc.dylib
53 // signal malloc stack logging that initialisation has finished
54 extern void __stack_logging_early_finished(void); // form libsystem_c.dylib
56 // system library atfork handlers
57 extern void _pthread_fork_prepare(void);
58 extern void _pthread_fork_parent(void);
59 extern void _pthread_fork_child(void);
60 extern void _pthread_fork_child_postinit(void);
62 extern void dispatch_atfork_prepare(void);
63 extern void dispatch_atfork_parent(void);
64 extern void dispatch_atfork_child(void);
66 extern void _malloc_fork_prepare(void);
67 extern void _malloc_fork_parent(void);
68 extern void _malloc_fork_child(void);
70 extern void _mach_fork_child(void);
71 extern void _libc_fork_child(void);
72 extern void _notify_fork_child(void);
73 extern void _dyld_fork_child(void);
74 extern void xpc_atfork_prepare(void);
75 extern void xpc_atfork_parent(void);
76 extern void xpc_atfork_child(void);
77 extern void _libSC_info_fork_prepare(void);
78 extern void _libSC_info_fork_parent(void);
79 extern void _libSC_info_fork_child(void);
80 extern void _asl_fork_child(void);
82 // advance decls for below;
83 void libSystem_atfork_prepare(void);
84 void libSystem_atfork_parent(void);
85 void libSystem_atfork_child(void);
87 void _pthread_exit_if_canceled(int);
90 * libsyscall_initializer() initializes all of libSystem.dylib <rdar://problem/4892197>
92 static __attribute__((constructor
))
93 void libSystem_initializer(int argc
, const char* argv
[], const char* envp
[], const char* apple
[], const struct ProgramVars
* vars
)
95 static const struct _libkernel_functions libkernel_funcs
= {
101 ._pthread_exit_if_canceled
= _pthread_exit_if_canceled
,
104 static const struct _libpthread_functions libpthread_funcs
= {
109 __libkernel_init(&libkernel_funcs
, envp
, apple
, vars
);
112 __libplatform_init(NULL
, envp
, apple
, vars
);
114 __pthread_init(&libpthread_funcs
, envp
, apple
, vars
);
115 __libc_init(vars
, libSystem_atfork_prepare
, libSystem_atfork_parent
, libSystem_atfork_child
, apple
);
117 // TODO: Move __malloc_init before __libc_init after breaking malloc's upward link to Libc
118 __malloc_init(apple
);
122 _libxpc_initializer();
124 __stack_logging_early_finished();
126 /* <rdar://problem/11588042>
127 * C99 standard has the following in section 7.5(3):
128 * "The value of errno is zero at program startup, but is never set
129 * to zero by any library function."
135 * libSystem_atfork_{prepare,parent,child}() are called by libc when we fork, then we deal with running fork handlers
138 void libSystem_atfork_prepare(void)
140 _libSC_info_fork_prepare();
141 xpc_atfork_prepare();
142 dispatch_atfork_prepare();
143 _pthread_fork_prepare();
144 _malloc_fork_prepare();
147 void libSystem_atfork_parent(void)
149 _malloc_fork_parent();
150 _pthread_fork_parent();
151 dispatch_atfork_parent();
153 _libSC_info_fork_parent();
156 void libSystem_atfork_child(void)
159 _pthread_fork_child();
160 _malloc_fork_child();
161 dispatch_atfork_child();
167 _notify_fork_child();
169 _libSC_info_fork_child();
171 _pthread_fork_child_postinit();
175 * Old crt1.o glue used to call through mach_init_routine which was used to initialize libSystem.
176 * LibSystem now auto-initializes but mach_init_routine is left for binary compatibility.
178 static void mach_init_old(void) {}
179 void (*mach_init_routine
)(void) = &mach_init_old
;
182 * This __crashreporter_info__ symbol is for all non-dylib parts of libSystem.
184 const char *__crashreporter_info__
;
185 asm (".desc __crashreporter_info__, 0x10");