]> git.saurik.com Git - apple/libc.git/blob - sys/crt_externs.c
Libc-498.1.7.tar.gz
[apple/libc.git] / sys / crt_externs.c
1 /*
2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 ** This file contains interfaces to the symbols defined int the crt modules.
25 ** 3 April 1995
26 ** Matt Watson (mwatson@next.com)
27 **
28 */
29
30 #if defined(__DYNAMIC__)
31 #include "mach-o/dyld.h" /* defines _dyld_lookup_and_bind() */
32 #define STRINGIFY(a) # a
33 #define DECLARE_VAR(var, type) \
34 static type * var ## _pointer = 0
35 #define DECLARE_PROGNAME(var, type) \
36 static type * var ## _pointer = 0; \
37 static type _priv_ ## var = 0
38 #define SETUP_VAR(var) \
39 if ( var ## _pointer == 0) { \
40 _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
41 (unsigned long *) & var ## _pointer, 0); \
42 }
43 #define SETUP_PROGNAME(var) \
44 if ( var ## _pointer == 0) { \
45 if(NSIsSymbolNameDefined( STRINGIFY(_ ## var) )) \
46 _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
47 (unsigned long *) & var ## _pointer, 0); \
48 else { \
49 char *progname = _dyld_get_image_name(0); \
50 if(_priv_ ## var = strrchr(progname, '/')) \
51 _priv_ ## var ++; \
52 else \
53 _priv_ ## var = progname; \
54 var ## _pointer = & _priv_ ## var; \
55 } \
56 }
57 #define USE_VAR(var) (var ## _pointer)
58 #else
59 #define DECLARE_VAR(var, type) extern type var
60 #define DECLARE_PROGNAME(var, type) DECLARE_VAR(var, type)
61 #define SETUP_VAR(var)
62 #define SETUP_PROGNAME(var) SETUP_VAR(var)
63 #define USE_VAR(var) (& var)
64 #endif
65
66 DECLARE_VAR(NXArgv, char **);
67 DECLARE_VAR(NXArgc, int);
68 DECLARE_VAR(environ, char **);
69 DECLARE_VAR(_mh_execute_header, struct mach_header);
70 DECLARE_PROGNAME(__progname, char *);
71
72 char ***_NSGetArgv(void) {
73 SETUP_VAR(NXArgv);
74 return(USE_VAR(NXArgv));
75 }
76
77 int *_NSGetArgc(void) {
78 SETUP_VAR(NXArgc);
79 return(USE_VAR(NXArgc));
80 }
81
82 char ***_NSGetEnviron(void) {
83 SETUP_VAR(environ);
84 return(USE_VAR(environ));
85 }
86
87 char **_NSGetProgname(void) {
88 SETUP_PROGNAME(__progname);
89 return(USE_VAR(__progname));
90 }
91
92 struct mach_header *_NSGetMachExecuteHeader(void) {
93 SETUP_VAR(_mh_execute_header);
94 return(USE_VAR(_mh_execute_header));
95 }
96
97 #if __DYNAMIC__
98 struct ProgramVars
99 {
100 void* mh;
101 int* NXArgcPtr;
102 char*** NXArgvPtr;
103 char*** environPtr;
104 char** __prognamePtr;
105 };
106
107 /*
108 * dyld calls libSystem_initializer() and passes it a ProgramVars struct containing pointers to the
109 * main executable's NXArg* global variables. libSystem_initializer() calls __libc_init() which calls
110 * _program_vars_init() passing the ProgramVars parameter.
111 */
112 void __attribute__((visibility("hidden")))
113 _program_vars_init(const struct ProgramVars* vars) {
114 // to support transitional 10.5 main executables that don't have extended __dyld section and instead call _NSSetProgramVars,
115 // don't overwrite values set by _NSSetProgramVars()
116 if ( NXArgv_pointer != NULL )
117 return;
118 NXArgv_pointer = vars->NXArgvPtr;
119 NXArgc_pointer = vars->NXArgcPtr;
120 environ_pointer = vars->environPtr;
121 __progname_pointer = vars->__prognamePtr;
122 _mh_execute_header_pointer = vars->mh;
123 }
124
125 /*
126 * This is only called by main executables built with pre 10-5 GM crt1.10.5.o. In those programs,
127 * there is no extended __dyld section, dyld cannot tell _program_vars_init() where the real program
128 * variables are, so they get temp values and are set for real here.
129 */
130 void _NSSetProgramVars(int* crt_argc, char*** crt_argv, char*** crt_environ, struct mach_header* crt_mh, char** crt_progname) {
131 NXArgv_pointer = crt_argv;
132 NXArgc_pointer = crt_argc;
133 environ_pointer = crt_environ;
134 __progname_pointer = crt_progname;
135 _mh_execute_header_pointer = crt_mh;
136 }
137 #endif
138
139 /*
140 * Fix for Radar bug 2200596 --
141 * EH symbol definitions for gcc 2.7.2.x implementation of
142 * C++ exception handling. The problem: the EH implementation
143 * has "throw" store stuff into these pointers, and then as the
144 * stack is unwound, the code generated into each function (for
145 * checking whether this function contains a relevant "catch"
146 * clause and for calling destructors for local variables) looks
147 * at these globals to find the type and value thrown.
148 *
149 * The problem was that the compiler generated the symbols as
150 * "common" symbols, and common symbols cannot be placed in
151 * dynamic shared libraries. So we must put these guys as
152 * "data" symbols into crt0.o or the System Framework (library),
153 * and the compiler must generate code that defines the symbols
154 * as external references instead of common.
155 *
156 * I changed the symbol names (added the "_272") to be utterly
157 * paranoid about any possible future use of similar names by
158 * any future versions of gcc.
159 * -- D. Landauer, Jan. 1998
160 */
161
162 void *__eh_pc_gcc_272 = (void *)0;
163 void *__eh_type_gcc_272 = (void *)0;
164 void *__eh_value_gcc_272 = (void *)0;
165
166 /* This is what egcs uses for its global data pointer */
167 void *__eh_global_dataptr = (void *)0;