Blame sysdeps/mach/hurd/i386/init-first.c

Packit 6c4009
/* Initialization code run first thing by the ELF startup code.  For i386/Hurd.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <set-hooks.h>
Packit 6c4009
#include "hurdstartup.h"
Packit 6c4009
#include "hurdmalloc.h"		/* XXX */
Packit 6c4009
#include "../locale/localeinfo.h"
Packit 6c4009
Packit 6c4009
#include <ldsodefs.h>
Packit 6c4009
#include <fpu_control.h>
Packit 6c4009
Packit 6c4009
extern void __mach_init (void);
Packit 6c4009
extern void __init_misc (int, char **, char **);
Packit 6c4009
extern void __libc_global_ctors (void);
Packit 6c4009
Packit 6c4009
unsigned long int __hurd_threadvar_stack_offset;
Packit 6c4009
unsigned long int __hurd_threadvar_stack_mask;
Packit 6c4009
Packit 6c4009
#ifndef SHARED
Packit 6c4009
int __libc_enable_secure;
Packit 6c4009
#endif
Packit 6c4009
int __libc_multiple_libcs attribute_hidden = 1;
Packit 6c4009
Packit 6c4009
extern int __libc_argc attribute_hidden;
Packit 6c4009
extern char **__libc_argv attribute_hidden;
Packit 6c4009
extern char **_dl_argv;
Packit 6c4009
Packit 6c4009
extern void *(*_cthread_init_routine) (void) __attribute__ ((weak));
Packit 6c4009
void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
Packit 6c4009
Packit 6c4009
/* Things that want to be run before _hurd_init or much anything else.
Packit 6c4009
   Importantly, these are called before anything tries to use malloc.  */
Packit 6c4009
DEFINE_HOOK (_hurd_preinit_hook, (void));
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We call this once the Hurd magic is all set up and we are ready to be a
Packit 6c4009
   Posixoid program.  This does the same things the generic version does.  */
Packit 6c4009
static void
Packit 6c4009
posixland_init (int argc, char **argv, char **envp)
Packit 6c4009
{
Packit 6c4009
  __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
Packit 6c4009
Packit 6c4009
  /* Make sure we don't initialize twice.  */
Packit 6c4009
  if (!__libc_multiple_libcs)
Packit 6c4009
    {
Packit 6c4009
      /* Set the FPU control word to the proper default value.  */
Packit 6c4009
      __setfpucw (__fpu_control);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Initialize data structures so the additional libc can do RPCs.  */
Packit 6c4009
      __mach_init ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Save the command-line arguments.  */
Packit 6c4009
  __libc_argc = argc;
Packit 6c4009
  __libc_argv = argv;
Packit 6c4009
  __environ = envp;
Packit 6c4009
Packit 6c4009
#ifndef SHARED
Packit 6c4009
  _dl_non_dynamic_init ();
Packit 6c4009
#endif
Packit 6c4009
  __init_misc (argc, argv, envp);
Packit 6c4009
Packit 6c4009
  /* Initialize ctype data.  */
Packit 6c4009
  __ctype_init ();
Packit 6c4009
Packit 6c4009
#if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
Packit 6c4009
  __libc_global_ctors ();
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
init1 (int argc, char *arg0, ...)
Packit 6c4009
{
Packit 6c4009
  char **argv = &arg;;
Packit 6c4009
  char **envp = &argv[argc + 1];
Packit 6c4009
  struct hurd_startup_data *d;
Packit 6c4009
Packit 6c4009
  while (*envp)
Packit 6c4009
    ++envp;
Packit 6c4009
  d = (void *) ++envp;
Packit 6c4009
Packit 6c4009
  /* Initialize libpthread if linked in.  */
Packit 6c4009
  if (__pthread_initialize_minimal != NULL)
Packit 6c4009
    __pthread_initialize_minimal ();
Packit 6c4009
Packit 6c4009
  if ((void *) d == argv[0])
Packit 6c4009
    /* No Hurd data block to process.  */
Packit 6c4009
    return;
Packit 6c4009
Packit 6c4009
#ifndef SHARED
Packit 6c4009
  __libc_enable_secure = d->flags & EXEC_SECURE;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  _hurd_init_dtable = d->dtable;
Packit 6c4009
  _hurd_init_dtablesize = d->dtablesize;
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    /* Check if the stack we are now on is different from
Packit 6c4009
       the one described by _hurd_stack_{base,size}.  */
Packit 6c4009
Packit 6c4009
    char dummy;
Packit 6c4009
    const vm_address_t newsp = (vm_address_t) &dummy;
Packit 6c4009
Packit 6c4009
    if (d->stack_size != 0 && (newsp < d->stack_base ||
Packit 6c4009
			       newsp - d->stack_base > d->stack_size))
Packit 6c4009
      /* The new stack pointer does not intersect with the
Packit 6c4009
	 stack the exec server set up for us, so free that stack.  */
Packit 6c4009
      __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  if (d->portarray || d->intarray)
Packit 6c4009
    /* Initialize library data structures, start signal processing, etc.  */
Packit 6c4009
    _hurd_init (d->flags, argv,
Packit 6c4009
		d->portarray, d->portarraysize,
Packit 6c4009
		d->intarray, d->intarraysize);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline void
Packit 6c4009
init (int *data)
Packit 6c4009
{
Packit 6c4009
  int argc = *data;
Packit 6c4009
  char **argv = (void *) (data + 1);
Packit 6c4009
  char **envp = &argv[argc + 1];
Packit 6c4009
  struct hurd_startup_data *d;
Packit 6c4009
Packit 6c4009
  /* Since the cthreads initialization code uses malloc, and the
Packit 6c4009
     malloc initialization code needs to get at the environment, make
Packit 6c4009
     sure we can find it.  We'll need to do this again later on since
Packit 6c4009
     switching stacks changes the location where the environment is
Packit 6c4009
     stored.  */
Packit 6c4009
  __environ = envp;
Packit 6c4009
Packit 6c4009
  while (*envp)
Packit 6c4009
    ++envp;
Packit 6c4009
  d = (void *) ++envp;
Packit 6c4009
Packit 6c4009
#ifndef SHARED
Packit 6c4009
  /* If we are the bootstrap task started by the kernel,
Packit 6c4009
     then after the environment pointers there is no Hurd
Packit 6c4009
     data block; the argument strings start there.  */
Packit 6c4009
  if ((void *) d == argv[0] || d->phdr == 0)
Packit 6c4009
    {
Packit 6c4009
      /* With a new enough linker (binutils-2.23 or better),
Packit 6c4009
         the magic __ehdr_start symbol will be available and
Packit 6c4009
         __libc_start_main will have done this that way already.  */
Packit 6c4009
      if (_dl_phdr == NULL)
Packit 6c4009
        {
Packit 6c4009
          /* We may need to see our own phdrs, e.g. for TLS setup.
Packit 6c4009
             Try the usual kludge to find the headers without help from
Packit 6c4009
             the exec server.  */
Packit 6c4009
          extern const void __executable_start;
Packit 6c4009
          const ElfW(Ehdr) *const ehdr = &__executable_start;
Packit 6c4009
          _dl_phdr = (const void *) ehdr + ehdr->e_phoff;
Packit 6c4009
          _dl_phnum = ehdr->e_phnum;
Packit 6c4009
          assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      _dl_phdr = (ElfW(Phdr) *) d->phdr;
Packit 6c4009
      _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
Packit 6c4009
      assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* We need to setup TLS before initializing libpthread.  */
Packit 6c4009
  __libc_setup_tls ();
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* After possibly switching stacks, call `init1' (above) with the user
Packit 6c4009
     code as the return address, and the argument data immediately above
Packit 6c4009
     that on the stack.  */
Packit 6c4009
Packit 6c4009
  if (&_cthread_init_routine && _cthread_init_routine)
Packit 6c4009
    {
Packit 6c4009
      /* Initialize cthreads, which will allocate us a new stack to run on.  */
Packit 6c4009
      int *newsp = (*_cthread_init_routine) ();
Packit 6c4009
      struct hurd_startup_data *od;
Packit 6c4009
Packit 6c4009
      void switch_stacks (void);
Packit 6c4009
Packit 6c4009
      __libc_stack_end = newsp;
Packit 6c4009
Packit 6c4009
      /* Copy the argdata from the old stack to the new one.  */
Packit 6c4009
      newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
Packit 6c4009
		      (char *) d - (char *) data);
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
      /* And readjust the dynamic linker's idea of where the argument
Packit 6c4009
	 vector lives.  */
Packit 6c4009
      assert (_dl_argv == argv);
Packit 6c4009
      _dl_argv = (void *) (newsp + 1);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      /* Set up the Hurd startup data block immediately following
Packit 6c4009
	 the argument and environment pointers on the new stack.  */
Packit 6c4009
      od = ((void *) newsp + ((char *) d - (char *) data));
Packit 6c4009
      if ((void *) argv[0] == d)
Packit 6c4009
	/* We were started up by the kernel with arguments on the stack.
Packit 6c4009
	   There is no Hurd startup data, so zero the block.  */
Packit 6c4009
	memset (od, 0, sizeof *od);
Packit 6c4009
      else
Packit 6c4009
	/* Copy the Hurd startup data block to the new stack.  */
Packit 6c4009
	*od = *d;
Packit 6c4009
Packit 6c4009
      /* Push the user code address on the top of the new stack.  It will
Packit 6c4009
	 be the return address for `init1'; we will jump there with NEWSP
Packit 6c4009
	 as the stack pointer.  */
Packit 6c4009
      /* The following expression would typically be written as
Packit 6c4009
	 ``__builtin_return_address (0)''.  But, for example, GCC 4.4.6 doesn't
Packit 6c4009
	 recognize that this read operation may alias the following write
Packit 6c4009
	 operation, and thus is free to reorder the two, clobbering the
Packit 6c4009
	 original return address.  */
Packit 6c4009
      *--newsp = *((int *) __builtin_frame_address (0) + 1);
Packit 6c4009
      /* GCC 4.4.6 also wants us to force loading *NEWSP already here.  */
Packit 6c4009
      asm volatile ("# %0" : : "X" (*newsp));
Packit 6c4009
      *((void **) __builtin_frame_address (0) + 1) = &switch_stacks;
Packit 6c4009
      /* Force NEWSP into %eax and &init1 into %ecx, which are not restored
Packit 6c4009
	 by function return.  */
Packit 6c4009
      asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      int usercode;
Packit 6c4009
Packit 6c4009
      void call_init1 (void);
Packit 6c4009
Packit 6c4009
      /* The argument data is just above the stack frame we will unwind by
Packit 6c4009
	 returning.  Mutate our own return address to run the code below.  */
Packit 6c4009
      /* The following expression would typically be written as
Packit 6c4009
	 ``__builtin_return_address (0)''.  But, for example, GCC 4.4.6 doesn't
Packit 6c4009
	 recognize that this read operation may alias the following write
Packit 6c4009
	 operation, and thus is free to reorder the two, clobbering the
Packit 6c4009
	 original return address.  */
Packit 6c4009
      usercode = *((int *) __builtin_frame_address (0) + 1);
Packit 6c4009
      /* GCC 4.4.6 also wants us to force loading USERCODE already here.  */
Packit 6c4009
      asm volatile ("# %0" : : "X" (usercode));
Packit 6c4009
      *((void **) __builtin_frame_address (0) + 1) = &call_init1;
Packit 6c4009
      /* Force USERCODE into %eax and &init1 into %ecx, which are not
Packit 6c4009
	 restored by function return.  */
Packit 6c4009
      asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* These bits of inline assembler used to be located inside `init'.
Packit 6c4009
   However they were optimized away by gcc 2.95.  */
Packit 6c4009
Packit 6c4009
/* The return address of `init' above, was redirected to here, so at
Packit 6c4009
   this point our stack is unwound and callers' registers restored.
Packit 6c4009
   Only %ecx and %eax are call-clobbered and thus still have the
Packit 6c4009
   values we set just above.  Fetch from there the new stack pointer
Packit 6c4009
   we will run on, and jmp to the run-time address of `init1'; when it
Packit 6c4009
   returns, it will run the user code with the argument data at the
Packit 6c4009
   top of the stack.  */
Packit 6c4009
asm ("switch_stacks:\n"
Packit 6c4009
     "	movl %eax, %esp\n"
Packit 6c4009
     "	jmp *%ecx");
Packit 6c4009
Packit 6c4009
/* As in the stack-switching case, at this point our stack is unwound
Packit 6c4009
   and callers' registers restored, and only %ecx and %eax communicate
Packit 6c4009
   values from the lines above.  In this case we have stashed in %eax
Packit 6c4009
   the user code return address.  Push it on the top of the stack so
Packit 6c4009
   it acts as init1's return address, and then jump there.  */
Packit 6c4009
asm ("call_init1:\n"
Packit 6c4009
     "	push %eax\n"
Packit 6c4009
     "	jmp *%ecx\n");
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Do the first essential initializations that must precede all else.  */
Packit 6c4009
static inline void
Packit 6c4009
first_init (void)
Packit 6c4009
{
Packit 6c4009
  /* Initialize data structures so we can do RPCs.  */
Packit 6c4009
  __mach_init ();
Packit 6c4009
Packit 6c4009
  RUN_HOOK (_hurd_preinit_hook, ());
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
/* This function is called specially by the dynamic linker to do early
Packit 6c4009
   initialization of the shared C library before normal initializers
Packit 6c4009
   expecting a Posixoid environment can run.  It gets called with the
Packit 6c4009
   stack set up just as the user will see it, so it can switch stacks.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_dl_init_first (int argc, ...)
Packit 6c4009
{
Packit 6c4009
  first_init ();
Packit 6c4009
Packit 6c4009
  /* If we use ``__builtin_frame_address (0) + 2'' here, GCC gets confused.  */
Packit 6c4009
  init (&argc);
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
#ifdef SHARED
Packit 6c4009
/* The regular posixland initialization is what goes into libc's
Packit 6c4009
   normal initializer.  */
Packit 6c4009
/* NOTE!  The linker notices the magical name `_init' and sets the DT_INIT
Packit 6c4009
   pointer in the dynamic section based solely on that.  It is convention
Packit 6c4009
   for this function to be in the `.init' section, but the symbol name is
Packit 6c4009
   the only thing that really matters!!  */
Packit 6c4009
strong_alias (posixland_init, _init);
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__libc_init_first (int argc, char **argv, char **envp)
Packit 6c4009
{
Packit 6c4009
  /* Everything was done in the shared library initializer, _init.  */
Packit 6c4009
}
Packit 6c4009
#else
Packit 6c4009
strong_alias (posixland_init, __libc_init_first);
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* XXX This is all a crock and I am not happy with it.
Packit 6c4009
   This poorly-named function is called by static-start.S,
Packit 6c4009
   which should not exist at all.  */
Packit 6c4009
void
Packit 6c4009
_hurd_stack_setup (void)
Packit 6c4009
{
Packit 6c4009
  intptr_t caller = (intptr_t) __builtin_return_address (0);
Packit 6c4009
Packit 6c4009
  void doinit (intptr_t *data)
Packit 6c4009
    {
Packit 6c4009
      /* This function gets called with the argument data at TOS.  */
Packit 6c4009
      void doinit1 (int argc, ...)
Packit 6c4009
	{
Packit 6c4009
	  /* If we use ``__builtin_frame_address (0) + 2'' here, GCC gets
Packit 6c4009
	     confused.  */
Packit 6c4009
	  init ((int *) &argc);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Push the user return address after the argument data, and then
Packit 6c4009
	 jump to `doinit1' (above), so it is as if __libc_init_first's
Packit 6c4009
	 caller had called `doinit1' with the argument data already on the
Packit 6c4009
	 stack.  */
Packit 6c4009
      *--data = caller;
Packit 6c4009
      asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack.  */
Packit 6c4009
		    "movl $0, %%ebp\n" /* Clear outermost frame pointer.  */
Packit 6c4009
		    "jmp *%1" : : "r" (data), "r" (&doinit1) : "sp");
Packit 6c4009
      /* NOTREACHED */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  first_init ();
Packit 6c4009
Packit 6c4009
  _hurd_startup ((void **) __builtin_frame_address (0) + 2, &doinit);
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This function is defined here so that if this file ever gets into
Packit 6c4009
   ld.so we will get a link error.  Having this file silently included
Packit 6c4009
   in ld.so causes disaster, because the _init definition above will
Packit 6c4009
   cause ld.so to gain an init function, which is not a cool thing. */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_dl_start (void)
Packit 6c4009
{
Packit 6c4009
  abort ();
Packit 6c4009
}