]> git.saurik.com Git - apple/libc.git/blame - sys/crt_externs.c
Libc-1439.100.3.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() */
9385eb3d 32#define DECLARE_VAR(var, type) \
e9ce8d39 33 static type * var ## _pointer = 0
9385eb3d
A
34#define DECLARE_PROGNAME(var, type) \
35 static type * var ## _pointer = 0; \
b061a43b 36 __unused static type _priv_ ## var = 0
e9ce8d39
A
37#define USE_VAR(var) (var ## _pointer)
38#else
39#define DECLARE_VAR(var, type) extern type var
9385eb3d 40#define DECLARE_PROGNAME(var, type) DECLARE_VAR(var, type)
e9ce8d39
A
41#define USE_VAR(var) (& var)
42#endif
43
224c7076
A
44DECLARE_VAR(NXArgv, char **);
45DECLARE_VAR(NXArgc, int);
46DECLARE_VAR(environ, char **);
47DECLARE_VAR(_mh_execute_header, struct mach_header);
48DECLARE_PROGNAME(__progname, char *);
49
e9ce8d39 50char ***_NSGetArgv(void) {
e9ce8d39
A
51 return(USE_VAR(NXArgv));
52}
53
54int *_NSGetArgc(void) {
e9ce8d39
A
55 return(USE_VAR(NXArgc));
56}
57
58char ***_NSGetEnviron(void) {
e9ce8d39
A
59 return(USE_VAR(environ));
60}
61
5b2abdfb 62char **_NSGetProgname(void) {
5b2abdfb
A
63 return(USE_VAR(__progname));
64}
65
e9ce8d39 66struct mach_header *_NSGetMachExecuteHeader(void) {
e9ce8d39
A
67 return(USE_VAR(_mh_execute_header));
68}
69
224c7076
A
70#if __DYNAMIC__
71struct ProgramVars
72{
73 void* mh;
74 int* NXArgcPtr;
75 char*** NXArgvPtr;
76 char*** environPtr;
77 char** __prognamePtr;
78};
79
34e8f829
A
80
81#define SUPPORT_PRE_GM_10_5_EXECUTABLES (__ppc__ || __i386__)
82
83
224c7076 84/*
23e20b00
A
85 * dyld calls libSystem_initializer() and passes it a ProgramVars struct
86 * containing pointers to the main executable's NXArg* global variables.
87 * libSystem_initializer() calls _libc_initializer() which calls
224c7076
A
88 * _program_vars_init() passing the ProgramVars parameter.
89 */
90void __attribute__((visibility("hidden")))
91_program_vars_init(const struct ProgramVars* vars) {
34e8f829 92#if SUPPORT_PRE_GM_10_5_EXECUTABLES
224c7076
A
93 // to support transitional 10.5 main executables that don't have extended __dyld section and instead call _NSSetProgramVars,
94 // don't overwrite values set by _NSSetProgramVars()
95 if ( NXArgv_pointer != NULL )
96 return;
34e8f829 97#endif
224c7076
A
98 NXArgv_pointer = vars->NXArgvPtr;
99 NXArgc_pointer = vars->NXArgcPtr;
100 environ_pointer = vars->environPtr;
101 __progname_pointer = vars->__prognamePtr;
102 _mh_execute_header_pointer = vars->mh;
103}
104
34e8f829 105#if SUPPORT_PRE_GM_10_5_EXECUTABLES
224c7076
A
106/*
107 * This is only called by main executables built with pre 10-5 GM crt1.10.5.o. In those programs,
108 * there is no extended __dyld section, dyld cannot tell _program_vars_init() where the real program
109 * variables are, so they get temp values and are set for real here.
110 */
111void _NSSetProgramVars(int* crt_argc, char*** crt_argv, char*** crt_environ, struct mach_header* crt_mh, char** crt_progname) {
112 NXArgv_pointer = crt_argv;
113 NXArgc_pointer = crt_argc;
114 environ_pointer = crt_environ;
115 __progname_pointer = crt_progname;
116 _mh_execute_header_pointer = crt_mh;
117}
34e8f829
A
118#endif
119#endif /* __DYNAMIC__ */
224c7076 120
34e8f829 121#if __ppc__
e9ce8d39
A
122/*
123 * Fix for Radar bug 2200596 --
124 * EH symbol definitions for gcc 2.7.2.x implementation of
125 * C++ exception handling. The problem: the EH implementation
126 * has "throw" store stuff into these pointers, and then as the
127 * stack is unwound, the code generated into each function (for
128 * checking whether this function contains a relevant "catch"
129 * clause and for calling destructors for local variables) looks
130 * at these globals to find the type and value thrown.
131 *
132 * The problem was that the compiler generated the symbols as
133 * "common" symbols, and common symbols cannot be placed in
134 * dynamic shared libraries. So we must put these guys as
135 * "data" symbols into crt0.o or the System Framework (library),
136 * and the compiler must generate code that defines the symbols
137 * as external references instead of common.
138 *
139 * I changed the symbol names (added the "_272") to be utterly
140 * paranoid about any possible future use of similar names by
141 * any future versions of gcc.
142 * -- D. Landauer, Jan. 1998
143 */
144
145void *__eh_pc_gcc_272 = (void *)0;
146void *__eh_type_gcc_272 = (void *)0;
147void *__eh_value_gcc_272 = (void *)0;
148
149/* This is what egcs uses for its global data pointer */
150void *__eh_global_dataptr = (void *)0;
34e8f829
A
151#endif /* __ppc__ */
152