Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
blobfc355ed6c3ec2244dc135a429bfc84119a9f4582
1 /* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2 Copyright (C) 1995-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <ctype.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 extern void __libc_global_ctors (void);
41 unsigned int __hurd_threadvar_max;
42 unsigned long int __hurd_threadvar_stack_offset;
43 unsigned long int __hurd_threadvar_stack_mask;
45 #ifndef SHARED
46 int __libc_enable_secure;
47 #endif
48 int __libc_multiple_libcs attribute_hidden = 1;
50 extern int __libc_argc attribute_hidden;
51 extern char **__libc_argv attribute_hidden;
52 extern char **_dl_argv;
54 extern void *(*_cthread_init_routine) (void) __attribute__ ((weak));
55 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
57 /* Things that want to be run before _hurd_init or much anything else.
58 Importantly, these are called before anything tries to use malloc. */
59 DEFINE_HOOK (_hurd_preinit_hook, (void));
62 /* We call this once the Hurd magic is all set up and we are ready to be a
63 Posixoid program. This does the same things the generic version does. */
64 static void
65 posixland_init (int argc, char **argv, char **envp)
67 __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
69 /* Make sure we don't initialize twice. */
70 if (!__libc_multiple_libcs)
72 /* Set the FPU control word to the proper default value. */
73 __setfpucw (__fpu_control);
76 /* Save the command-line arguments. */
77 __libc_argc = argc;
78 __libc_argv = argv;
79 __environ = envp;
81 #ifndef SHARED
82 _dl_non_dynamic_init ();
83 #endif
84 __init_misc (argc, argv, envp);
86 #ifdef USE_NONOPTION_FLAGS
87 /* This is a hack to make the special getopt in GNU libc working. */
88 __getopt_clean_environment (envp);
89 #endif
91 /* Initialize ctype data. */
92 __ctype_init ();
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;
107 while (*envp)
108 ++envp;
109 d = (void *) ++envp;
111 /* If we are the bootstrap task started by the kernel,
112 then after the environment pointers there is no Hurd
113 data block; the argument strings start there. */
114 if ((void *) d == argv[0])
116 #ifndef SHARED
117 /* With a new enough linker (binutils-2.23 or better),
118 the magic __ehdr_start symbol will be available and
119 __libc_start_main will have done this that way already. */
120 if (_dl_phdr == NULL)
122 /* We may need to see our own phdrs, e.g. for TLS setup.
123 Try the usual kludge to find the headers without help from
124 the exec server. */
125 extern const void __executable_start;
126 const ElfW(Ehdr) *const ehdr = &__executable_start;
127 _dl_phdr = (const void *) ehdr + ehdr->e_phoff;
128 _dl_phnum = ehdr->e_phnum;
129 assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
131 #endif
132 return;
135 #ifndef SHARED
136 __libc_enable_secure = d->flags & EXEC_SECURE;
138 _dl_phdr = (ElfW(Phdr) *) d->phdr;
139 _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
140 assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
141 #endif
143 _hurd_init_dtable = d->dtable;
144 _hurd_init_dtablesize = d->dtablesize;
147 /* Check if the stack we are now on is different from
148 the one described by _hurd_stack_{base,size}. */
150 char dummy;
151 const vm_address_t newsp = (vm_address_t) &dummy;
153 if (d->stack_size != 0 && (newsp < d->stack_base ||
154 newsp - d->stack_base > d->stack_size))
155 /* The new stack pointer does not intersect with the
156 stack the exec server set up for us, so free that stack. */
157 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
160 if (d->portarray || d->intarray)
161 /* Initialize library data structures, start signal processing, etc. */
162 _hurd_init (d->flags, argv,
163 d->portarray, d->portarraysize,
164 d->intarray, d->intarraysize);
168 static inline void
169 init (int *data)
171 int argc = *data;
172 char **argv = (void *) (data + 1);
173 char **envp = &argv[argc + 1];
174 struct hurd_startup_data *d;
175 unsigned long int threadvars[_HURD_THREADVAR_MAX];
177 /* Provide temporary storage for thread-specific variables on the
178 startup stack so the cthreads initialization code can use them
179 for malloc et al, or so we can use malloc below for the real
180 threadvars array. */
181 memset (threadvars, 0, sizeof threadvars);
182 threadvars[_HURD_THREADVAR_LOCALE] = (unsigned long int) &_nl_global_locale;
183 __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
185 /* Since the cthreads initialization code uses malloc, and the
186 malloc initialization code needs to get at the environment, make
187 sure we can find it. We'll need to do this again later on since
188 switching stacks changes the location where the environment is
189 stored. */
190 __environ = envp;
192 while (*envp)
193 ++envp;
194 d = (void *) ++envp;
196 /* The user might have defined a value for this, to get more variables.
197 Otherwise it will be zero on startup. We must make sure it is set
198 properly before before cthreads initialization, so cthreads can know
199 how much space to leave for thread variables. */
200 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
201 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
204 /* After possibly switching stacks, call `init1' (above) with the user
205 code as the return address, and the argument data immediately above
206 that on the stack. */
208 if (&_cthread_init_routine && _cthread_init_routine)
210 /* Initialize cthreads, which will allocate us a new stack to run on. */
211 int *newsp = (*_cthread_init_routine) ();
212 struct hurd_startup_data *od;
214 void switch_stacks (void);
216 __libc_stack_end = newsp;
218 /* Copy per-thread variables from that temporary
219 area onto the new cthread stack. */
220 memcpy (__hurd_threadvar_location_from_sp (0, newsp),
221 threadvars, sizeof threadvars);
223 /* Copy the argdata from the old stack to the new one. */
224 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
225 (char *) d - (char *) data);
227 #ifdef SHARED
228 /* And readjust the dynamic linker's idea of where the argument
229 vector lives. */
230 assert (_dl_argv == argv);
231 _dl_argv = (void *) (newsp + 1);
232 #endif
234 /* Set up the Hurd startup data block immediately following
235 the argument and environment pointers on the new stack. */
236 od = ((void *) newsp + ((char *) d - (char *) data));
237 if ((void *) argv[0] == d)
238 /* We were started up by the kernel with arguments on the stack.
239 There is no Hurd startup data, so zero the block. */
240 memset (od, 0, sizeof *od);
241 else
242 /* Copy the Hurd startup data block to the new stack. */
243 *od = *d;
245 /* Push the user code address on the top of the new stack. It will
246 be the return address for `init1'; we will jump there with NEWSP
247 as the stack pointer. */
248 /* The following expression would typically be written as
249 ``__builtin_return_address (0)''. But, for example, GCC 4.4.6 doesn't
250 recognize that this read operation may alias the following write
251 operation, and thus is free to reorder the two, clobbering the
252 original return address. */
253 *--newsp = *((int *) __builtin_frame_address (0) + 1);
254 /* GCC 4.4.6 also wants us to force loading *NEWSP already here. */
255 asm volatile ("# %0" : : "X" (*newsp));
256 *((void **) __builtin_frame_address (0) + 1) = &switch_stacks;
257 /* Force NEWSP into %eax and &init1 into %ecx, which are not restored
258 by function return. */
259 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
261 else
263 /* We are not using cthreads, so we will have just a single allocated
264 area for the per-thread variables of the main user thread. */
265 unsigned long int *array;
266 unsigned int i;
267 int usercode;
269 void call_init1 (void);
271 array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
272 if (array == NULL)
273 __libc_fatal ("Can't allocate single-threaded thread variables.");
275 /* Copy per-thread variables from the temporary array into the
276 newly malloc'd space. */
277 memcpy (array, threadvars, sizeof threadvars);
278 __hurd_threadvar_stack_offset = (unsigned long int) array;
279 for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
280 array[i] = 0;
282 /* The argument data is just above the stack frame we will unwind by
283 returning. Mutate our own return address to run the code below. */
284 /* The following expression would typically be written as
285 ``__builtin_return_address (0)''. But, for example, GCC 4.4.6 doesn't
286 recognize that this read operation may alias the following write
287 operation, and thus is free to reorder the two, clobbering the
288 original return address. */
289 usercode = *((int *) __builtin_frame_address (0) + 1);
290 /* GCC 4.4.6 also wants us to force loading USERCODE already here. */
291 asm volatile ("# %0" : : "X" (usercode));
292 *((void **) __builtin_frame_address (0) + 1) = &call_init1;
293 /* Force USERCODE into %eax and &init1 into %ecx, which are not
294 restored by function return. */
295 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
299 /* These bits of inline assembler used to be located inside `init'.
300 However they were optimized away by gcc 2.95. */
302 /* The return address of `init' above, was redirected to here, so at
303 this point our stack is unwound and callers' registers restored.
304 Only %ecx and %eax are call-clobbered and thus still have the
305 values we set just above. Fetch from there the new stack pointer
306 we will run on, and jmp to the run-time address of `init1'; when it
307 returns, it will run the user code with the argument data at the
308 top of the stack. */
309 asm ("switch_stacks:\n"
310 " movl %eax, %esp\n"
311 " jmp *%ecx");
313 /* As in the stack-switching case, at this point our stack is unwound
314 and callers' registers restored, and only %ecx and %eax communicate
315 values from the lines above. In this case we have stashed in %eax
316 the user code return address. Push it on the top of the stack so
317 it acts as init1's return address, and then jump there. */
318 asm ("call_init1:\n"
319 " push %eax\n"
320 " jmp *%ecx\n");
323 /* Do the first essential initializations that must precede all else. */
324 static inline void
325 first_init (void)
327 /* Initialize data structures so we can do RPCs. */
328 __mach_init ();
330 RUN_HOOK (_hurd_preinit_hook, ());
333 #ifdef SHARED
334 /* This function is called specially by the dynamic linker to do early
335 initialization of the shared C library before normal initializers
336 expecting a Posixoid environment can run. It gets called with the
337 stack set up just as the user will see it, so it can switch stacks. */
339 void
340 _dl_init_first (int argc, ...)
342 first_init ();
344 /* If we use ``__builtin_frame_address (0) + 2'' here, GCC gets confused. */
345 init (&argc);
347 #endif
350 #ifdef SHARED
351 /* The regular posixland initialization is what goes into libc's
352 normal initializer. */
353 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
354 pointer in the dynamic section based solely on that. It is convention
355 for this function to be in the `.init' section, but the symbol name is
356 the only thing that really matters!! */
357 strong_alias (posixland_init, _init);
359 void
360 __libc_init_first (int argc, char **argv, char **envp)
362 /* Everything was done in the shared library initializer, _init. */
364 #else
365 strong_alias (posixland_init, __libc_init_first);
368 /* XXX This is all a crock and I am not happy with it.
369 This poorly-named function is called by static-start.S,
370 which should not exist at all. */
371 void
372 _hurd_stack_setup (void)
374 intptr_t caller = (intptr_t) __builtin_return_address (0);
376 void doinit (intptr_t *data)
378 /* This function gets called with the argument data at TOS. */
379 void doinit1 (int argc, ...)
381 /* If we use ``__builtin_frame_address (0) + 2'' here, GCC gets
382 confused. */
383 init ((int *) &argc);
386 /* Push the user return address after the argument data, and then
387 jump to `doinit1' (above), so it is as if __libc_init_first's
388 caller had called `doinit1' with the argument data already on the
389 stack. */
390 *--data = caller;
391 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
392 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
393 "jmp *%1" : : "r" (data), "r" (&doinit1) : "sp");
394 /* NOTREACHED */
397 first_init ();
399 _hurd_startup ((void **) __builtin_frame_address (0) + 2, &doinit);
401 #endif
404 /* This function is defined here so that if this file ever gets into
405 ld.so we will get a link error. Having this file silently included
406 in ld.so causes disaster, because the _init definition above will
407 cause ld.so to gain an init function, which is not a cool thing. */
409 void
410 _dl_start (void)
412 abort ();