1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
25 #include <set-hooks.h>
26 #include "hurdstartup.h"
27 #include "hurdmalloc.h" /* XXX */
29 extern void __mach_init (void);
30 extern void __libc_init_secure (void);
31 extern void __libc_init (int, char **, char **);
32 extern void __getopt_clean_environment (char **);
33 extern void __libc_global_ctors (void);
35 unsigned int __hurd_threadvar_max;
36 unsigned long int __hurd_threadvar_stack_offset;
37 unsigned long int __hurd_threadvar_stack_mask;
39 int __libc_multiple_libcs = 1;
41 extern int __libc_argc;
42 extern char **__libc_argv;
44 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
45 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
47 /* Things that want to be run before _hurd_init or much anything else.
48 Importantly, these are called before anything tries to use malloc. */
49 DEFINE_HOOK (_hurd_preinit_hook, (void));
52 init1 (int argc, char *arg0, ...)
55 char **envp = &argv[argc + 1];
56 struct hurd_startup_data *d;
66 /* If we are the bootstrap task started by the kernel,
67 then after the environment pointers there is no Hurd
68 data block; the argument strings start there. */
69 if ((void *) d != argv[0])
71 _hurd_init_dtable = d->dtable;
72 _hurd_init_dtablesize = d->dtablesize;
75 /* Check if the stack we are now on is different from
76 the one described by _hurd_stack_{base,size}. */
79 const vm_address_t newsp = (vm_address_t) &dummy;
81 if (d->stack_size != 0 && (newsp < d->stack_base ||
82 newsp - d->stack_base > d->stack_size))
83 /* The new stack pointer does not intersect with the
84 stack the exec server set up for us, so free that stack. */
85 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
89 if (__hurd_threadvar_stack_mask == 0)
91 /* We are not using cthreads, so we will have just a single allocated
92 area for the per-thread variables of the main user thread. */
94 __hurd_threadvar_stack_offset
95 = (unsigned long int) malloc (__hurd_threadvar_max *
96 sizeof (unsigned long int));
97 if (__hurd_threadvar_stack_offset == 0)
98 __libc_fatal ("Can't allocate single-threaded per-thread variables.");
99 for (i = 0; i < __hurd_threadvar_max; ++i)
100 ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
103 if ((void *) d != argv[0] && (d->portarray || d->intarray))
104 /* Initialize library data structures, start signal processing, etc. */
105 _hurd_init (d->flags, argv,
106 d->portarray, d->portarraysize,
107 d->intarray, d->intarraysize);
110 __libc_init_secure ();
113 __libc_init (argc, argv, __environ);
115 /* This is a hack to make the special getopt in GNU libc working. */
116 __getopt_clean_environment (envp);
119 __libc_global_ctors ();
127 char **argv = (void *) (data + 1);
128 char **envp = &argv[argc + 1];
129 struct hurd_startup_data *d;
136 /* The user might have defined a value for this, to get more variables.
137 Otherwise it will be zero on startup. We must make sure it is set
138 properly before before cthreads initialization, so cthreads can know
139 how much space to leave for thread variables. */
140 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
141 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
144 /* After possibly switching stacks, call `init1' (above) with the user
145 code as the return address, and the argument data immediately above
146 that on the stack. */
148 if (_cthread_init_routine)
150 /* Initialize cthreads, which will allocate us a new stack to run on. */
151 void *newsp = (*_cthread_init_routine) ();
152 struct hurd_startup_data *od;
154 /* Copy the argdata from the old stack to the new one. */
155 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
156 (char *) d - (char *) data);
158 /* Set up the Hurd startup data block immediately following
159 the argument and environment pointers on the new stack. */
160 od = (newsp + ((char *) d - (char *) data));
161 if ((void *) argv[0] == d)
162 /* We were started up by the kernel with arguments on the stack.
163 There is no Hurd startup data, so zero the block. */
164 memset (od, 0, sizeof *od);
166 /* Copy the Hurd startup data block to the new stack. */
169 /* Push the user code address on the top of the new stack. It will
170 be the return address for `init1'; we will jump there with NEWSP
171 as the stack pointer. */
172 *--(int *) newsp = data[-1];
173 ((void **) data)[-1] = &&switch_stacks;
174 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
175 by function return. */
176 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
180 /* The argument data is just above the stack frame we will unwind by
181 returning. Mutate our own return address to run the code below. */
182 int usercode = data[-1];
183 ((void **) data)[-1] = &&call_init1;
184 /* Force USERCODE into %eax and &init1 into %ecx, which are not
185 restored by function return. */
186 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
192 /* Our return address was redirected to here, so at this point our stack
193 is unwound and callers' registers restored. Only %ecx and %eax are
194 call-clobbered and thus still have the values we set just above.
195 Fetch from there the new stack pointer we will run on, and jmp to the
196 run-time address of `init1'; when it returns, it will run the user
197 code with the argument data at the top of the stack. */
198 asm volatile ("movl %eax, %esp; jmp *%ecx");
202 /* As in the stack-switching case, at this point our stack is unwound and
203 callers' registers restored, and only %ecx and %eax communicate values
204 from the lines above. In this case we have stashed in %eax the user
205 code return address. Push it on the top of the stack so it acts as
206 init1's return address, and then jump there. */
207 asm volatile ("pushl %eax; jmp *%ecx");
213 /* This function is called to initialize the shared C library.
214 It is called just before the user _start code from i386/elf/start.S,
215 with the stack set up as that code gets it. */
217 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
218 pointer in the dynamic section based solely on that. It is convention
219 for this function to be in the `.init' section, but the symbol name is
220 the only thing that really matters!! */
222 _init (int argc, ...)
224 /* Initialize data structures so we can do RPCs. */
227 RUN_HOOK (_hurd_preinit_hook, ());
235 __libc_init_first (int argc __attribute__ ((unused)), ...)
241 _hurd_stack_setup (int argc __attribute__ ((unused)), ...)
244 void doinit (int *data)
246 /* This function gets called with the argument data at TOS. */
247 void doinit1 (int argc, ...)
252 /* Push the user return address after the argument data, and then
253 jump to `doinit1' (above), so it is as if __libc_init_first's
254 caller had called `doinit1' with the argument data already on the
256 *--data = (&argc)[-1];
257 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
258 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
259 "jmp *%1" : : "r" (data), "r" (&doinit1));
263 /* Initialize data structures so we can do RPCs. */
266 RUN_HOOK (_hurd_preinit_hook, ());
268 _hurd_startup ((void **) &argc, &doinit);
273 /* This function is defined here so that if this file ever gets into
274 ld.so we will get a link error. Having this file silently included
275 in ld.so causes disaster, because the _init definition above will
276 cause ld.so to gain an init function, which is not a cool thing. */