Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
blob7a10e7857b9d6e7565dce7928709e40e16bf8595
1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995,96,97,98,99,2000,01,02,03,04,05
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <hurd.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sysdep.h>
26 #include <set-hooks.h>
27 #include "hurdstartup.h"
28 #include "hurdmalloc.h" /* XXX */
29 #include "../locale/localeinfo.h"
31 #include <ldsodefs.h>
32 #include <fpu_control.h>
34 extern void __mach_init (void);
35 extern void __init_misc (int, char **, char **);
36 #ifdef USE_NONOPTION_FLAGS
37 extern void __getopt_clean_environment (char **);
38 #endif
39 #ifndef SHARED
40 extern void _dl_non_dynamic_init (void) internal_function;
41 #endif
42 extern void __libc_global_ctors (void);
44 unsigned int __hurd_threadvar_max;
45 unsigned long int __hurd_threadvar_stack_offset;
46 unsigned long int __hurd_threadvar_stack_mask;
48 #ifndef SHARED
49 int __libc_enable_secure;
50 #endif
51 int __libc_multiple_libcs attribute_hidden = 1;
53 extern int __libc_argc attribute_hidden;
54 extern char **__libc_argv attribute_hidden;
55 extern char **_dl_argv;
57 extern void *(*_cthread_init_routine) (void) __attribute__ ((weak));
58 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
60 /* Things that want to be run before _hurd_init or much anything else.
61 Importantly, these are called before anything tries to use malloc. */
62 DEFINE_HOOK (_hurd_preinit_hook, (void));
65 /* We call this once the Hurd magic is all set up and we are ready to be a
66 Posixoid program. This does the same things the generic version does. */
67 static void
68 posixland_init (int argc, char **argv, char **envp)
70 __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
72 /* Make sure we don't initialize twice. */
73 if (!__libc_multiple_libcs)
75 /* Set the FPU control word to the proper default value. */
76 __setfpucw (__fpu_control);
79 /* Save the command-line arguments. */
80 __libc_argc = argc;
81 __libc_argv = argv;
82 __environ = envp;
84 #ifndef SHARED
85 _dl_non_dynamic_init ();
86 #endif
87 __init_misc (argc, argv, envp);
89 #ifdef USE_NONOPTION_FLAGS
90 /* This is a hack to make the special getopt in GNU libc working. */
91 __getopt_clean_environment (envp);
92 #endif
94 #if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
95 __libc_global_ctors ();
96 #endif
100 static void
101 init1 (int argc, char *arg0, ...)
103 char **argv = &arg0;
104 char **envp = &argv[argc + 1];
105 struct hurd_startup_data *d;
106 #ifndef SHARED
107 extern ElfW(Phdr) *_dl_phdr;
108 extern size_t _dl_phnum;
109 #endif
111 while (*envp)
112 ++envp;
113 d = (void *) ++envp;
115 /* If we are the bootstrap task started by the kernel,
116 then after the environment pointers there is no Hurd
117 data block; the argument strings start there. */
118 if ((void *) d == argv[0])
120 #ifndef SHARED
121 /* We may need to see our own phdrs, e.g. for TLS setup.
122 Try the usual kludge to find the headers without help from
123 the exec server. */
124 extern const void _start;
125 const ElfW(Ehdr) *const ehdr = &_start;
126 _dl_phdr = (ElfW(Phdr) *) ((const void *) ehdr + ehdr->e_phoff);
127 _dl_phnum = ehdr->e_phnum;
128 assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
129 #endif
130 return;
133 #ifndef SHARED
134 __libc_enable_secure = d->flags & EXEC_SECURE;
136 _dl_phdr = (ElfW(Phdr) *) d->phdr;
137 _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
138 assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
139 #endif
141 _hurd_init_dtable = d->dtable;
142 _hurd_init_dtablesize = d->dtablesize;
145 /* Check if the stack we are now on is different from
146 the one described by _hurd_stack_{base,size}. */
148 char dummy;
149 const vm_address_t newsp = (vm_address_t) &dummy;
151 if (d->stack_size != 0 && (newsp < d->stack_base ||
152 newsp - d->stack_base > d->stack_size))
153 /* The new stack pointer does not intersect with the
154 stack the exec server set up for us, so free that stack. */
155 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
158 if (d->portarray || d->intarray)
159 /* Initialize library data structures, start signal processing, etc. */
160 _hurd_init (d->flags, argv,
161 d->portarray, d->portarraysize,
162 d->intarray, d->intarraysize);
166 static inline void
167 init (int *data)
169 int argc = *data;
170 char **argv = (void *) (data + 1);
171 char **envp = &argv[argc + 1];
172 struct hurd_startup_data *d;
173 unsigned long int threadvars[_HURD_THREADVAR_MAX];
175 /* Provide temporary storage for thread-specific variables on the
176 startup stack so the cthreads initialization code can use them
177 for malloc et al, or so we can use malloc below for the real
178 threadvars array. */
179 memset (threadvars, 0, sizeof threadvars);
180 threadvars[_HURD_THREADVAR_LOCALE] = (unsigned long int) &_nl_global_locale;
181 __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
183 /* Since the cthreads initialization code uses malloc, and the
184 malloc initialization code needs to get at the environment, make
185 sure we can find it. We'll need to do this again later on since
186 switching stacks changes the location where the environment is
187 stored. */
188 __environ = envp;
190 while (*envp)
191 ++envp;
192 d = (void *) ++envp;
194 /* The user might have defined a value for this, to get more variables.
195 Otherwise it will be zero on startup. We must make sure it is set
196 properly before before cthreads initialization, so cthreads can know
197 how much space to leave for thread variables. */
198 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
199 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
202 /* After possibly switching stacks, call `init1' (above) with the user
203 code as the return address, and the argument data immediately above
204 that on the stack. */
206 if (&_cthread_init_routine && _cthread_init_routine)
208 /* Initialize cthreads, which will allocate us a new stack to run on. */
209 int *newsp = (*_cthread_init_routine) ();
210 struct hurd_startup_data *od;
212 void switch_stacks (void);
214 /* Copy per-thread variables from that temporary
215 area onto the new cthread stack. */
216 memcpy (__hurd_threadvar_location_from_sp (0, newsp),
217 threadvars, sizeof threadvars);
219 /* Copy the argdata from the old stack to the new one. */
220 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
221 (char *) d - (char *) data);
223 #ifdef SHARED
224 /* And readjust the dynamic linker's idea of where the argument
225 vector lives. */
226 assert (_dl_argv == argv);
227 _dl_argv = (void *) (newsp + 1);
228 #endif
230 /* Set up the Hurd startup data block immediately following
231 the argument and environment pointers on the new stack. */
232 od = ((void *) newsp + ((char *) d - (char *) data));
233 if ((void *) argv[0] == d)
234 /* We were started up by the kernel with arguments on the stack.
235 There is no Hurd startup data, so zero the block. */
236 memset (od, 0, sizeof *od);
237 else
238 /* Copy the Hurd startup data block to the new stack. */
239 *od = *d;
241 /* Push the user code address on the top of the new stack. It will
242 be the return address for `init1'; we will jump there with NEWSP
243 as the stack pointer. */
244 *--newsp = data[-1];
245 ((void **) data)[-1] = switch_stacks;
246 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
247 by function return. */
248 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
250 else
252 /* We are not using cthreads, so we will have just a single allocated
253 area for the per-thread variables of the main user thread. */
254 unsigned long int *array;
255 unsigned int i;
256 int usercode;
258 void call_init1 (void);
260 array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
261 if (array == NULL)
262 __libc_fatal ("Can't allocate single-threaded thread variables.");
264 /* Copy per-thread variables from the temporary array into the
265 newly malloc'd space. */
266 memcpy (array, threadvars, sizeof threadvars);
267 __hurd_threadvar_stack_offset = (unsigned long int) array;
268 for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
269 array[i] = 0;
271 /* The argument data is just above the stack frame we will unwind by
272 returning. Mutate our own return address to run the code below. */
273 usercode = data[-1];
274 data[-1] = (int) &call_init1;
275 /* Force USERCODE into %eax and &init1 into %ecx, which are not
276 restored by function return. */
277 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
281 /* These bits of inline assembler used to be located inside `init'.
282 However they were optimized away by gcc 2.95. */
284 /* The return address of `init' above, was redirected to here, so at
285 this point our stack is unwound and callers' registers restored.
286 Only %ecx and %eax are call-clobbered and thus still have the
287 values we set just above. Fetch from there the new stack pointer
288 we will run on, and jmp to the run-time address of `init1'; when it
289 returns, it will run the user code with the argument data at the
290 top of the stack. */
291 asm ("switch_stacks:\n"
292 " movl %eax, %esp\n"
293 " jmp *%ecx");
295 /* As in the stack-switching case, at this point our stack is unwound
296 and callers' registers restored, and only %ecx and %eax communicate
297 values from the lines above. In this case we have stashed in %eax
298 the user code return address. Push it on the top of the stack so
299 it acts as init1's return address, and then jump there. */
300 asm ("call_init1:\n"
301 " push %eax\n"
302 " jmp *%ecx\n");
305 /* Do the first essential initializations that must precede all else. */
306 static inline void
307 first_init (void)
309 /* Initialize data structures so we can do RPCs. */
310 __mach_init ();
312 RUN_HOOK (_hurd_preinit_hook, ());
315 #ifdef SHARED
316 /* This function is called specially by the dynamic linker to do early
317 initialization of the shared C library before normal initializers
318 expecting a Posixoid environment can run. It gets called with the
319 stack set up just as the user will see it, so it can switch stacks. */
321 void
322 _dl_init_first (void)
324 first_init ();
326 init ((int *) __builtin_frame_address (0) + 2);
328 #endif
331 #ifdef SHARED
332 /* The regular posixland initialization is what goes into libc's
333 normal initializer. */
334 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
335 pointer in the dynamic section based solely on that. It is convention
336 for this function to be in the `.init' section, but the symbol name is
337 the only thing that really matters!! */
338 strong_alias (posixland_init, _init);
340 void
341 __libc_init_first (int argc, char **argv, char **envp)
343 /* Everything was done in the shared library initializer, _init. */
345 #else
346 strong_alias (posixland_init, __libc_init_first);
349 /* XXX This is all a crock and I am not happy with it.
350 This poorly-named function is called by static-start.S,
351 which should not exist at all. */
352 void
353 _hurd_stack_setup (void)
355 intptr_t caller = (intptr_t) __builtin_return_address (0);
357 void doinit (intptr_t *data)
359 /* This function gets called with the argument data at TOS. */
360 void doinit1 (void)
362 init ((int *) __builtin_frame_address (0) + 2);
365 /* Push the user return address after the argument data, and then
366 jump to `doinit1' (above), so it is as if __libc_init_first's
367 caller had called `doinit1' with the argument data already on the
368 stack. */
369 *--data = caller;
370 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
371 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
372 "jmp *%1" : : "r" (data), "r" (&doinit1) : "sp");
373 /* NOTREACHED */
376 first_init ();
378 _hurd_startup ((void **) __builtin_frame_address (0) + 2, &doinit);
380 #endif
383 /* This function is defined here so that if this file ever gets into
384 ld.so we will get a link error. Having this file silently included
385 in ld.so causes disaster, because the _init definition above will
386 cause ld.so to gain an init function, which is not a cool thing. */
388 void
389 _dl_start (void)
391 abort ();