Back to index

glibc  2.9
init-first.c
Go to the documentation of this file.
00001 /* Initialization code run first thing by the ELF startup code.  For i386/Hurd.
00002    Copyright (C) 1995,96,97,98,99,2000,01,02,03,04,05
00003        Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <hurd.h>
00023 #include <stdio.h>
00024 #include <unistd.h>
00025 #include <string.h>
00026 #include <sysdep.h>
00027 #include <set-hooks.h>
00028 #include "hurdstartup.h"
00029 #include "hurdmalloc.h"            /* XXX */
00030 #include "../locale/localeinfo.h"
00031 
00032 #include <ldsodefs.h>
00033 #include <fpu_control.h>
00034 
00035 extern void __mach_init (void);
00036 extern void __init_misc (int, char **, char **);
00037 #ifdef USE_NONOPTION_FLAGS
00038 extern void __getopt_clean_environment (char **);
00039 #endif
00040 #ifndef SHARED
00041 extern void _dl_non_dynamic_init (void) internal_function;
00042 #endif
00043 extern void __libc_global_ctors (void);
00044 
00045 unsigned int __hurd_threadvar_max;
00046 unsigned long int __hurd_threadvar_stack_offset;
00047 unsigned long int __hurd_threadvar_stack_mask;
00048 
00049 #ifndef SHARED
00050 int __libc_enable_secure;
00051 #endif
00052 int __libc_multiple_libcs attribute_hidden = 1;
00053 
00054 extern int __libc_argc attribute_hidden;
00055 extern char **__libc_argv attribute_hidden;
00056 extern char **_dl_argv;
00057 
00058 extern void *(*_cthread_init_routine) (void) __attribute__ ((weak));
00059 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
00060 
00061 /* Things that want to be run before _hurd_init or much anything else.
00062    Importantly, these are called before anything tries to use malloc.  */
00063 DEFINE_HOOK (_hurd_preinit_hook, (void));
00064 
00065 
00066 /* We call this once the Hurd magic is all set up and we are ready to be a
00067    Posixoid program.  This does the same things the generic version does.  */
00068 static void
00069 posixland_init (int argc, char **argv, char **envp)
00070 {
00071   __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
00072 
00073   /* Make sure we don't initialize twice.  */
00074   if (!__libc_multiple_libcs)
00075     {
00076       /* Set the FPU control word to the proper default value.  */
00077       __setfpucw (__fpu_control);
00078     }
00079 
00080   /* Save the command-line arguments.  */
00081   __libc_argc = argc;
00082   __libc_argv = argv;
00083   __environ = envp;
00084 
00085 #ifndef SHARED
00086   _dl_non_dynamic_init ();
00087 #endif
00088   __init_misc (argc, argv, envp);
00089 
00090 #ifdef USE_NONOPTION_FLAGS
00091   /* This is a hack to make the special getopt in GNU libc working.  */
00092   __getopt_clean_environment (envp);
00093 #endif
00094 
00095 #ifdef SHARED
00096   __libc_global_ctors ();
00097 #endif
00098 }
00099 
00100 
00101 static void
00102 init1 (int argc, char *arg0, ...)
00103 {
00104   char **argv = &arg0;
00105   char **envp = &argv[argc + 1];
00106   struct hurd_startup_data *d;
00107 #ifndef SHARED
00108   extern ElfW(Phdr) *_dl_phdr;
00109   extern size_t _dl_phnum;
00110 #endif
00111 
00112   while (*envp)
00113     ++envp;
00114   d = (void *) ++envp;
00115 
00116   /* If we are the bootstrap task started by the kernel,
00117      then after the environment pointers there is no Hurd
00118      data block; the argument strings start there.  */
00119   if ((void *) d == argv[0])
00120     {
00121 #ifndef SHARED
00122       /* We may need to see our own phdrs, e.g. for TLS setup.
00123          Try the usual kludge to find the headers without help from
00124         the exec server.  */
00125       extern const void _start;
00126       const ElfW(Ehdr) *const ehdr = &_start;
00127       _dl_phdr = (ElfW(Phdr) *) ((const void *) ehdr + ehdr->e_phoff);
00128       _dl_phnum = ehdr->e_phnum;
00129       assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
00130 #endif
00131       return;
00132     }
00133 
00134 #ifndef SHARED
00135   __libc_enable_secure = d->flags & EXEC_SECURE;
00136 
00137   _dl_phdr = (ElfW(Phdr) *) d->phdr;
00138   _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
00139   assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
00140 #endif
00141 
00142   _hurd_init_dtable = d->dtable;
00143   _hurd_init_dtablesize = d->dtablesize;
00144 
00145   {
00146     /* Check if the stack we are now on is different from
00147        the one described by _hurd_stack_{base,size}.  */
00148 
00149     char dummy;
00150     const vm_address_t newsp = (vm_address_t) &dummy;
00151 
00152     if (d->stack_size != 0 && (newsp < d->stack_base ||
00153                             newsp - d->stack_base > d->stack_size))
00154       /* The new stack pointer does not intersect with the
00155         stack the exec server set up for us, so free that stack.  */
00156       __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
00157   }
00158 
00159   if (d->portarray || d->intarray)
00160     /* Initialize library data structures, start signal processing, etc.  */
00161     _hurd_init (d->flags, argv,
00162               d->portarray, d->portarraysize,
00163               d->intarray, d->intarraysize);
00164 }
00165 
00166 
00167 static inline void
00168 init (int *data)
00169 {
00170   int argc = *data;
00171   char **argv = (void *) (data + 1);
00172   char **envp = &argv[argc + 1];
00173   struct hurd_startup_data *d;
00174   unsigned long int threadvars[_HURD_THREADVAR_MAX];
00175 
00176   /* Provide temporary storage for thread-specific variables on the
00177      startup stack so the cthreads initialization code can use them
00178      for malloc et al, or so we can use malloc below for the real
00179      threadvars array.  */
00180   memset (threadvars, 0, sizeof threadvars);
00181   threadvars[_HURD_THREADVAR_LOCALE] = (unsigned long int) &_nl_global_locale;
00182   __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
00183 
00184   /* Since the cthreads initialization code uses malloc, and the
00185      malloc initialization code needs to get at the environment, make
00186      sure we can find it.  We'll need to do this again later on since
00187      switching stacks changes the location where the environment is
00188      stored.  */
00189   __environ = envp;
00190 
00191   while (*envp)
00192     ++envp;
00193   d = (void *) ++envp;
00194 
00195   /* The user might have defined a value for this, to get more variables.
00196      Otherwise it will be zero on startup.  We must make sure it is set
00197      properly before before cthreads initialization, so cthreads can know
00198      how much space to leave for thread variables.  */
00199   if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
00200     __hurd_threadvar_max = _HURD_THREADVAR_MAX;
00201 
00202 
00203   /* After possibly switching stacks, call `init1' (above) with the user
00204      code as the return address, and the argument data immediately above
00205      that on the stack.  */
00206 
00207   if (&_cthread_init_routine && _cthread_init_routine)
00208     {
00209       /* Initialize cthreads, which will allocate us a new stack to run on.  */
00210       int *newsp = (*_cthread_init_routine) ();
00211       struct hurd_startup_data *od;
00212 
00213       void switch_stacks (void);
00214 
00215       /* Copy per-thread variables from that temporary
00216         area onto the new cthread stack.  */
00217       memcpy (__hurd_threadvar_location_from_sp (0, newsp),
00218              threadvars, sizeof threadvars);
00219 
00220       /* Copy the argdata from the old stack to the new one.  */
00221       newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
00222                     (char *) d - (char *) data);
00223 
00224 #ifdef SHARED
00225       /* And readjust the dynamic linker's idea of where the argument
00226          vector lives.  */
00227       assert (_dl_argv == argv);
00228       _dl_argv = (void *) (newsp + 1);
00229 #endif
00230 
00231       /* Set up the Hurd startup data block immediately following
00232         the argument and environment pointers on the new stack.  */
00233       od = ((void *) newsp + ((char *) d - (char *) data));
00234       if ((void *) argv[0] == d)
00235        /* We were started up by the kernel with arguments on the stack.
00236           There is no Hurd startup data, so zero the block.  */
00237        memset (od, 0, sizeof *od);
00238       else
00239        /* Copy the Hurd startup data block to the new stack.  */
00240        *od = *d;
00241 
00242       /* Push the user code address on the top of the new stack.  It will
00243         be the return address for `init1'; we will jump there with NEWSP
00244         as the stack pointer.  */
00245       *--newsp = data[-1];
00246       ((void **) data)[-1] = switch_stacks;
00247       /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
00248         by function return.  */
00249       asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
00250     }
00251   else
00252     {
00253       /* We are not using cthreads, so we will have just a single allocated
00254         area for the per-thread variables of the main user thread.  */
00255       unsigned long int *array;
00256       unsigned int i;
00257       int usercode;
00258 
00259       void call_init1 (void);
00260 
00261       array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
00262       if (array == NULL)
00263        __libc_fatal ("Can't allocate single-threaded thread variables.");
00264 
00265       /* Copy per-thread variables from the temporary array into the
00266         newly malloc'd space.  */
00267       memcpy (array, threadvars, sizeof threadvars);
00268       __hurd_threadvar_stack_offset = (unsigned long int) array;
00269       for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
00270        array[i] = 0;
00271 
00272       /* The argument data is just above the stack frame we will unwind by
00273         returning.  Mutate our own return address to run the code below.  */
00274       usercode = data[-1];
00275       data[-1] = (int) &call_init1;
00276       /* Force USERCODE into %eax and &init1 into %ecx, which are not
00277         restored by function return.  */
00278       asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
00279     }
00280 }
00281 
00282 /* These bits of inline assembler used to be located inside `init'.
00283    However they were optimized away by gcc 2.95.  */
00284 
00285 /* The return address of `init' above, was redirected to here, so at
00286    this point our stack is unwound and callers' registers restored.
00287    Only %ecx and %eax are call-clobbered and thus still have the
00288    values we set just above.  Fetch from there the new stack pointer
00289    we will run on, and jmp to the run-time address of `init1'; when it
00290    returns, it will run the user code with the argument data at the
00291    top of the stack.  */
00292 asm ("switch_stacks:\n"
00293      " movl %eax, %esp\n"
00294      " jmp *%ecx");
00295 
00296 /* As in the stack-switching case, at this point our stack is unwound
00297    and callers' registers restored, and only %ecx and %eax communicate
00298    values from the lines above.  In this case we have stashed in %eax
00299    the user code return address.  Push it on the top of the stack so
00300    it acts as init1's return address, and then jump there.  */
00301 asm ("call_init1:\n"
00302      " push %eax\n"
00303      " jmp *%ecx\n");
00304 
00305 
00306 /* Do the first essential initializations that must precede all else.  */
00307 static inline void
00308 first_init (void)
00309 {
00310   /* Initialize data structures so we can do RPCs.  */
00311   __mach_init ();
00312 
00313   RUN_HOOK (_hurd_preinit_hook, ());
00314 }
00315 
00316 #ifdef SHARED
00317 /* This function is called specially by the dynamic linker to do early
00318    initialization of the shared C library before normal initializers
00319    expecting a Posixoid environment can run.  It gets called with the
00320    stack set up just as the user will see it, so it can switch stacks.  */
00321 
00322 void
00323 _dl_init_first (void)
00324 {
00325   first_init ();
00326 
00327   init ((int *) __builtin_frame_address (0) + 2);
00328 }
00329 #endif
00330 
00331 
00332 #ifdef SHARED
00333 /* The regular posixland initialization is what goes into libc's
00334    normal initializer.  */
00335 /* NOTE!  The linker notices the magical name `_init' and sets the DT_INIT
00336    pointer in the dynamic section based solely on that.  It is convention
00337    for this function to be in the `.init' section, but the symbol name is
00338    the only thing that really matters!!  */
00339 strong_alias (posixland_init, _init);
00340 
00341 void
00342 __libc_init_first (int argc, char **argv, char **envp)
00343 {
00344   /* Everything was done in the shared library initializer, _init.  */
00345 }
00346 #else
00347 strong_alias (posixland_init, __libc_init_first);
00348 
00349 
00350 /* XXX This is all a crock and I am not happy with it.
00351    This poorly-named function is called by static-start.S,
00352    which should not exist at all.  */
00353 void
00354 _hurd_stack_setup (void)
00355 {
00356   intptr_t caller = (intptr_t) __builtin_return_address (0);
00357 
00358   void doinit (intptr_t *data)
00359     {
00360       /* This function gets called with the argument data at TOS.  */
00361       void doinit1 (void)
00362        {
00363          init ((int *) __builtin_frame_address (0) + 2);
00364        }
00365 
00366       /* Push the user return address after the argument data, and then
00367          jump to `doinit1' (above), so it is as if __libc_init_first's
00368          caller had called `doinit1' with the argument data already on the
00369          stack.  */
00370       *--data = caller;
00371       asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack.  */
00372                   "movl $0, %%ebp\n" /* Clear outermost frame pointer.  */
00373                   "jmp *%1" : : "r" (data), "r" (&doinit1) : "sp");
00374       /* NOTREACHED */
00375     }
00376 
00377   first_init ();
00378 
00379   _hurd_startup ((void **) __builtin_frame_address (0) + 2, &doinit);
00380 }
00381 #endif
00382 
00383 
00384 /* This function is defined here so that if this file ever gets into
00385    ld.so we will get a link error.  Having this file silently included
00386    in ld.so causes disaster, because the _init definition above will
00387    cause ld.so to gain an init function, which is not a cool thing. */
00388 
00389 void
00390 _dl_start (void)
00391 {
00392   abort ();
00393 }