]> git.saurik.com Git - apple/libc.git/blame - sys/crt_externs.c
Libc-498.1.7.tar.gz
[apple/libc.git] / sys / crt_externs.c
CommitLineData
e9ce8d39 1/*
224c7076 2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
e9ce8d39
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71
A
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
e9ce8d39
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
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.
e9ce8d39
A
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
9385eb3d 33#define DECLARE_VAR(var, type) \
e9ce8d39 34 static type * var ## _pointer = 0
9385eb3d
A
35#define DECLARE_PROGNAME(var, type) \
36 static type * var ## _pointer = 0; \
37 static type _priv_ ## var = 0
e9ce8d39
A
38#define SETUP_VAR(var) \
39 if ( var ## _pointer == 0) { \
40 _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
41 (unsigned long *) & var ## _pointer, 0); \
42 }
9385eb3d
A
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 }
e9ce8d39
A
57#define USE_VAR(var) (var ## _pointer)
58#else
59#define DECLARE_VAR(var, type) extern type var
9385eb3d 60#define DECLARE_PROGNAME(var, type) DECLARE_VAR(var, type)
e9ce8d39 61#define SETUP_VAR(var)
9385eb3d 62#define SETUP_PROGNAME(var) SETUP_VAR(var)
e9ce8d39
A
63#define USE_VAR(var) (& var)
64#endif
65
224c7076
A
66DECLARE_VAR(NXArgv, char **);
67DECLARE_VAR(NXArgc, int);
68DECLARE_VAR(environ, char **);
69DECLARE_VAR(_mh_execute_header, struct mach_header);
70DECLARE_PROGNAME(__progname, char *);
71
e9ce8d39 72char ***_NSGetArgv(void) {
e9ce8d39
A
73 SETUP_VAR(NXArgv);
74 return(USE_VAR(NXArgv));
75}
76
77int *_NSGetArgc(void) {
e9ce8d39
A
78 SETUP_VAR(NXArgc);
79 return(USE_VAR(NXArgc));
80}
81
82char ***_NSGetEnviron(void) {
e9ce8d39
A
83 SETUP_VAR(environ);
84 return(USE_VAR(environ));
85}
86
5b2abdfb 87char **_NSGetProgname(void) {
9385eb3d 88 SETUP_PROGNAME(__progname);
5b2abdfb
A
89 return(USE_VAR(__progname));
90}
91
e9ce8d39 92struct mach_header *_NSGetMachExecuteHeader(void) {
e9ce8d39
A
93 SETUP_VAR(_mh_execute_header);
94 return(USE_VAR(_mh_execute_header));
95}
96
224c7076
A
97#if __DYNAMIC__
98struct 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 */
112void __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 */
130void _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
e9ce8d39
A
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
162void *__eh_pc_gcc_272 = (void *)0;
163void *__eh_type_gcc_272 = (void *)0;
164void *__eh_value_gcc_272 = (void *)0;
165
166/* This is what egcs uses for its global data pointer */
167void *__eh_global_dataptr = (void *)0;