2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
21 * @APPLE_LICENSE_HEADER_END@
24 ** This file contains interfaces to the symbols defined int the crt modules.
26 ** Matt Watson (mwatson@next.com)
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); \
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); \
49 char *progname = _dyld_get_image_name(0); \
50 if(_priv_ ## var = strrchr(progname, '/')) \
53 _priv_ ## var = progname; \
54 var ## _pointer = & _priv_ ## var; \
57 #define USE_VAR(var) (var ## _pointer)
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)
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 *);
72 char ***_NSGetArgv(void) {
74 return(USE_VAR(NXArgv
));
77 int *_NSGetArgc(void) {
79 return(USE_VAR(NXArgc
));
82 char ***_NSGetEnviron(void) {
84 return(USE_VAR(environ
));
87 char **_NSGetProgname(void) {
88 SETUP_PROGNAME(__progname
);
89 return(USE_VAR(__progname
));
92 struct mach_header
*_NSGetMachExecuteHeader(void) {
93 SETUP_VAR(_mh_execute_header
);
94 return(USE_VAR(_mh_execute_header
));
104 char** __prognamePtr
;
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.
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
)
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
;
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.
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
;
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.
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.
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
162 void *__eh_pc_gcc_272
= (void *)0;
163 void *__eh_type_gcc_272
= (void *)0;
164 void *__eh_value_gcc_272
= (void *)0;
166 /* This is what egcs uses for its global data pointer */
167 void *__eh_global_dataptr
= (void *)0;