1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <set-hooks.h>
27 #include "hurdstartup.h"
28 #include "hurdmalloc.h" /* XXX */
30 extern void __mach_init (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;
40 int __libc_enable_secure;
42 int __libc_multiple_libcs = 1;
44 extern int __libc_argc;
45 extern char **__libc_argv;
46 extern char **_dl_argv;
48 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
49 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
51 /* Things that want to be run before _hurd_init or much anything else.
52 Importantly, these are called before anything tries to use malloc. */
53 DEFINE_HOOK (_hurd_preinit_hook, (void));
56 /* We call this once the Hurd magic is all set up and we are ready to be a
57 Posixoid program. This does the same things the generic version does. */
59 posixland_init (int argc, char **argv, char **envp)
65 __libc_init (argc, argv, envp);
67 /* This is a hack to make the special getopt in GNU libc working. */
68 __getopt_clean_environment (envp);
71 __libc_global_ctors ();
77 init1 (int argc, char *arg0, ...)
80 char **envp = &argv[argc + 1];
81 struct hurd_startup_data *d;
87 /* If we are the bootstrap task started by the kernel,
88 then after the environment pointers there is no Hurd
89 data block; the argument strings start there. */
90 if ((void *) d != argv[0])
92 _hurd_init_dtable = d->dtable;
93 _hurd_init_dtablesize = d->dtablesize;
96 /* Check if the stack we are now on is different from
97 the one described by _hurd_stack_{base,size}. */
100 const vm_address_t newsp = (vm_address_t) &dummy;
102 if (d->stack_size != 0 && (newsp < d->stack_base ||
103 newsp - d->stack_base > d->stack_size))
104 /* The new stack pointer does not intersect with the
105 stack the exec server set up for us, so free that stack. */
106 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
110 if ((void *) d != argv[0] && (d->portarray || d->intarray))
111 /* Initialize library data structures, start signal processing, etc. */
112 _hurd_init (d->flags, argv,
113 d->portarray, d->portarraysize,
114 d->intarray, d->intarraysize);
117 __libc_enable_secure = d->flags & EXEC_SECURE;
126 char **argv = (void *) (data + 1);
127 char **envp = &argv[argc + 1];
128 struct hurd_startup_data *d;
129 unsigned long int threadvars[_HURD_THREADVAR_MAX];
131 /* Provide temporary storage for thread-specific variables on the startup
132 stack so the cthreads initialization code can use them for malloc et al,
133 or so we can use malloc below for the real threadvars array. */
134 memset (threadvars, 0, sizeof threadvars);
135 __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
141 /* The user might have defined a value for this, to get more variables.
142 Otherwise it will be zero on startup. We must make sure it is set
143 properly before before cthreads initialization, so cthreads can know
144 how much space to leave for thread variables. */
145 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
146 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
149 /* After possibly switching stacks, call `init1' (above) with the user
150 code as the return address, and the argument data immediately above
151 that on the stack. */
153 if (_cthread_init_routine)
155 /* Initialize cthreads, which will allocate us a new stack to run on. */
156 void *newsp = (*_cthread_init_routine) ();
157 struct hurd_startup_data *od;
159 void switch_stacks (void);
161 /* Copy per-thread variables from that temporary
162 area onto the new cthread stack. */
163 memcpy (__hurd_threadvar_location_from_sp (0, newsp),
164 threadvars, sizeof threadvars);
166 /* Copy the argdata from the old stack to the new one. */
167 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
168 (char *) d - (char *) data);
171 /* And readjust the dynamic linker's idea of where the argument
173 assert (_dl_argv == argv);
174 _dl_argv = (void *) ((int *) newsp + 1);
177 /* Set up the Hurd startup data block immediately following
178 the argument and environment pointers on the new stack. */
179 od = (newsp + ((char *) d - (char *) data));
180 if ((void *) argv[0] == d)
181 /* We were started up by the kernel with arguments on the stack.
182 There is no Hurd startup data, so zero the block. */
183 memset (od, 0, sizeof *od);
185 /* Copy the Hurd startup data block to the new stack. */
188 /* Push the user code address on the top of the new stack. It will
189 be the return address for `init1'; we will jump there with NEWSP
190 as the stack pointer. */
191 *--(int *) newsp = data[-1];
192 ((void **) data)[-1] = switch_stacks;
193 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
194 by function return. */
195 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
199 /* We are not using cthreads, so we will have just a single allocated
200 area for the per-thread variables of the main user thread. */
201 unsigned long int *array;
205 void call_init1 (void);
207 array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
209 __libc_fatal ("Can't allocate single-threaded thread variables.");
211 /* Copy per-thread variables from the temporary array into the
212 newly malloc'd space. */
213 memcpy (array, threadvars, sizeof threadvars);
214 __hurd_threadvar_stack_offset = (unsigned long int) array;
215 for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
218 /* The argument data is just above the stack frame we will unwind by
219 returning. Mutate our own return address to run the code below. */
221 ((void **) data)[-1] = call_init1;
222 /* Force USERCODE into %eax and &init1 into %ecx, which are not
223 restored by function return. */
224 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
228 /* These bits of inline assembler used to be located inside `init'.
229 However they were optimized away by gcc 2.95. */
231 /* The return address of `init' above, was redirected to here, so at
232 this point our stack is unwound and callers' registers restored.
233 Only %ecx and %eax are call-clobbered and thus still have the
234 values we set just above. Fetch from there the new stack pointer
235 we will run on, and jmp to the run-time address of `init1'; when it
236 returns, it will run the user code with the argument data at the
244 /* As in the stack-switching case, at this point our stack is unwound
245 and callers' registers restored, and only %ecx and %eax communicate
246 values from the lines above. In this case we have stashed in %eax
247 the user code return address. Push it on the top of the stack so
248 it acts as init1's return address, and then jump there. */
256 /* Do the first essential initializations that must precede all else. */
260 /* Initialize data structures so we can do RPCs. */
263 RUN_HOOK (_hurd_preinit_hook, ());
267 /* This function is called specially by the dynamic linker to do early
268 initialization of the shared C library before normal initializers
269 expecting a Posixoid environment can run. It gets called with the
270 stack set up just as the user will see it, so it can switch stacks. */
273 _dl_init_first (int argc, ...)
283 /* The regular posixland initialization is what goes into libc's
284 normal initializer. */
285 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
286 pointer in the dynamic section based solely on that. It is convention
287 for this function to be in the `.init' section, but the symbol name is
288 the only thing that really matters!! */
289 strong_alias (posixland_init, _init);
292 __libc_init_first (int argc, char **argv, char **envp)
294 /* Everything was done in the shared library initializer, _init. */
297 strong_alias (posixland_init, __libc_init_first);
300 /* XXX This is all a crock and I am not happy with it.
301 This poorly-named function is called by static-start.S,
302 which should not exist at all. */
304 _hurd_stack_setup (int argc __attribute__ ((unused)), ...)
306 void doinit (int *data)
308 /* This function gets called with the argument data at TOS. */
309 void doinit1 (int argc, ...)
314 /* Push the user return address after the argument data, and then
315 jump to `doinit1' (above), so it is as if __libc_init_first's
316 caller had called `doinit1' with the argument data already on the
318 *--data = (&argc)[-1];
319 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
320 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
321 "jmp *%1" : : "r" (data), "r" (&doinit1));
327 _hurd_startup ((void **) &argc, &doinit);
332 /* This function is defined here so that if this file ever gets into
333 ld.so we will get a link error. Having this file silently included
334 in ld.so causes disaster, because the _init definition above will
335 cause ld.so to gain an init function, which is not a cool thing. */