X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/734aad71947a79037af64f74c683f5eb36fe6065..2acb89982f71719aec26ca16705bd2c0400a9550:/sys/crt_externs.c?ds=sidebyside diff --git a/sys/crt_externs.c b/sys/crt_externs.c index d86b8a6..a78524f 100644 --- a/sys/crt_externs.c +++ b/sys/crt_externs.c @@ -1,10 +1,8 @@ /* - * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. + * Copyright (c) 1999-2007 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * - * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. - * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in @@ -31,51 +29,96 @@ #if defined(__DYNAMIC__) #include "mach-o/dyld.h" /* defines _dyld_lookup_and_bind() */ -#define STRINGIFY(a) # a -#define DECLARE_VAR(var, type) \ +#define DECLARE_VAR(var, type) \ static type * var ## _pointer = 0 -#define SETUP_VAR(var) \ - if ( var ## _pointer == 0) { \ - _dyld_lookup_and_bind( STRINGIFY(_ ## var), \ - (unsigned long *) & var ## _pointer, 0); \ - } +#define DECLARE_PROGNAME(var, type) \ + static type * var ## _pointer = 0; \ + static type _priv_ ## var = 0 #define USE_VAR(var) (var ## _pointer) #else #define DECLARE_VAR(var, type) extern type var -#define SETUP_VAR(var) +#define DECLARE_PROGNAME(var, type) DECLARE_VAR(var, type) #define USE_VAR(var) (& var) #endif +DECLARE_VAR(NXArgv, char **); +DECLARE_VAR(NXArgc, int); +DECLARE_VAR(environ, char **); +DECLARE_VAR(_mh_execute_header, struct mach_header); +DECLARE_PROGNAME(__progname, char *); + char ***_NSGetArgv(void) { - DECLARE_VAR(NXArgv, char **); - SETUP_VAR(NXArgv); return(USE_VAR(NXArgv)); } int *_NSGetArgc(void) { - DECLARE_VAR(NXArgc, int); - SETUP_VAR(NXArgc); return(USE_VAR(NXArgc)); } char ***_NSGetEnviron(void) { - DECLARE_VAR(environ, char **); - SETUP_VAR(environ); return(USE_VAR(environ)); } char **_NSGetProgname(void) { - DECLARE_VAR(__progname, char *); - SETUP_VAR(__progname); return(USE_VAR(__progname)); } struct mach_header *_NSGetMachExecuteHeader(void) { - DECLARE_VAR(_mh_execute_header, struct mach_header); - SETUP_VAR(_mh_execute_header); return(USE_VAR(_mh_execute_header)); } +#if __DYNAMIC__ +struct ProgramVars +{ + void* mh; + int* NXArgcPtr; + char*** NXArgvPtr; + char*** environPtr; + char** __prognamePtr; +}; + + +#define SUPPORT_PRE_GM_10_5_EXECUTABLES (__ppc__ || __i386__) + + +/* + * dyld calls libSystem_initializer() and passes it a ProgramVars struct + * containing pointers to the main executable's NXArg* global variables. + * libSystem_initializer() calls _libc_initializer() which calls + * _program_vars_init() passing the ProgramVars parameter. + */ +void __attribute__((visibility("hidden"))) +_program_vars_init(const struct ProgramVars* vars) { +#if SUPPORT_PRE_GM_10_5_EXECUTABLES + // to support transitional 10.5 main executables that don't have extended __dyld section and instead call _NSSetProgramVars, + // don't overwrite values set by _NSSetProgramVars() + if ( NXArgv_pointer != NULL ) + return; +#endif + NXArgv_pointer = vars->NXArgvPtr; + NXArgc_pointer = vars->NXArgcPtr; + environ_pointer = vars->environPtr; + __progname_pointer = vars->__prognamePtr; + _mh_execute_header_pointer = vars->mh; +} + +#if SUPPORT_PRE_GM_10_5_EXECUTABLES +/* + * This is only called by main executables built with pre 10-5 GM crt1.10.5.o. In those programs, + * there is no extended __dyld section, dyld cannot tell _program_vars_init() where the real program + * variables are, so they get temp values and are set for real here. + */ +void _NSSetProgramVars(int* crt_argc, char*** crt_argv, char*** crt_environ, struct mach_header* crt_mh, char** crt_progname) { + NXArgv_pointer = crt_argv; + NXArgc_pointer = crt_argc; + environ_pointer = crt_environ; + __progname_pointer = crt_progname; + _mh_execute_header_pointer = crt_mh; +} +#endif +#endif /* __DYNAMIC__ */ + +#if __ppc__ /* * Fix for Radar bug 2200596 -- * EH symbol definitions for gcc 2.7.2.x implementation of @@ -105,5 +148,5 @@ void *__eh_value_gcc_272 = (void *)0; /* This is what egcs uses for its global data pointer */ void *__eh_global_dataptr = (void *)0; +#endif /* __ppc__ */ -void *__keymgr_global[3] = { (void *)0 };