]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/i386/init-first.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
CommitLineData
99b306dc 1/* Initialization code run first thing by the ELF startup code. For i386/Hurd.
bfff8b1b 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
7cc27f44 3 This file is part of the GNU C Library.
99b306dc 4
7cc27f44 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
99b306dc 9
7cc27f44
UD
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
41bdb6e2 13 Lesser General Public License for more details.
99b306dc 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
99b306dc 18
8443afdc 19#include <assert.h>
228c019e 20#include <ctype.h>
99b306dc
RM
21#include <hurd.h>
22#include <stdio.h>
23#include <unistd.h>
a2fe9c76 24#include <string.h>
0ca7e46e
UD
25#include <sysdep.h>
26#include <set-hooks.h>
99b306dc 27#include "hurdstartup.h"
99b306dc 28#include "hurdmalloc.h" /* XXX */
cc13edc8 29#include "../locale/localeinfo.h"
99b306dc 30
9d8b8e94
RM
31#include <ldsodefs.h>
32#include <fpu_control.h>
33
99b306dc 34extern void __mach_init (void);
557fab43 35extern void __init_misc (int, char **, char **);
0ed24bc6 36#ifdef USE_NONOPTION_FLAGS
2604afb1 37extern void __getopt_clean_environment (char **);
0ed24bc6 38#endif
0324daa0 39extern void __libc_global_ctors (void);
99b306dc 40
6bac11d9
MB
41unsigned int __hurd_threadvar_max;
42unsigned long int __hurd_threadvar_stack_offset;
43unsigned long int __hurd_threadvar_stack_mask;
44
5879ee9f 45#ifndef SHARED
98375f9c 46int __libc_enable_secure;
a825f8e8 47#endif
ab26a24a 48int __libc_multiple_libcs attribute_hidden = 1;
548330d3 49
100351c3
UD
50extern int __libc_argc attribute_hidden;
51extern char **__libc_argv attribute_hidden;
8443afdc 52extern char **_dl_argv;
acf51e02 53
133bf22e 54extern void *(*_cthread_init_routine) (void) __attribute__ ((weak));
99b306dc
RM
55void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
56
99b306dc
RM
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. */
59DEFINE_HOOK (_hurd_preinit_hook, (void));
60
a13d5ca5
RM
61
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. */
5879ee9f
RM
64static void
65posixland_init (int argc, char **argv, char **envp)
a13d5ca5 66{
54789f38
RM
67 __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
68
69 /* Make sure we don't initialize twice. */
70 if (!__libc_multiple_libcs)
71 {
72 /* Set the FPU control word to the proper default value. */
73 __setfpucw (__fpu_control);
74 }
e0cfa510
ST
75 else
76 {
77 /* Initialize data structures so the additional libc can do RPCs. */
78 __mach_init ();
79 }
54789f38
RM
80
81 /* Save the command-line arguments. */
9129b987
RM
82 __libc_argc = argc;
83 __libc_argv = argv;
84 __environ = envp;
85
d417e0ff
UD
86#ifndef SHARED
87 _dl_non_dynamic_init ();
88#endif
557fab43 89 __init_misc (argc, argv, envp);
a13d5ca5 90
0ed24bc6 91#ifdef USE_NONOPTION_FLAGS
a13d5ca5 92 /* This is a hack to make the special getopt in GNU libc working. */
9129b987 93 __getopt_clean_environment (envp);
0ed24bc6 94#endif
a13d5ca5 95
228c019e
TS
96 /* Initialize ctype data. */
97 __ctype_init ();
98
4a531bb0 99#if defined SHARED && !defined NO_CTORS_DTORS_SECTIONS
a13d5ca5
RM
100 __libc_global_ctors ();
101#endif
102}
103
104
99b306dc
RM
105static void
106init1 (int argc, char *arg0, ...)
107{
108 char **argv = &arg0;
109 char **envp = &argv[argc + 1];
110 struct hurd_startup_data *d;
111
99b306dc
RM
112 while (*envp)
113 ++envp;
114 d = (void *) ++envp;
115
116 /* If we are the bootstrap task started by the kernel,
117 then after the environment pointers there is no Hurd
118 data block; the argument strings start there. */
9d78d122 119 if ((void *) d == argv[0])
99b306dc 120 {
9d78d122 121#ifndef SHARED
288f7d79
RM
122 /* With a new enough linker (binutils-2.23 or better),
123 the magic __ehdr_start symbol will be available and
124 __libc_start_main will have done this that way already. */
125 if (_dl_phdr == NULL)
126 {
127 /* We may need to see our own phdrs, e.g. for TLS setup.
128 Try the usual kludge to find the headers without help from
129 the exec server. */
ed479b02
ST
130 extern const void __executable_start;
131 const ElfW(Ehdr) *const ehdr = &__executable_start;
288f7d79
RM
132 _dl_phdr = (const void *) ehdr + ehdr->e_phoff;
133 _dl_phnum = ehdr->e_phnum;
134 assert (ehdr->e_phentsize == sizeof (ElfW(Phdr)));
135 }
9d78d122
RM
136#endif
137 return;
99b306dc
RM
138 }
139
9d78d122
RM
140#ifndef SHARED
141 __libc_enable_secure = d->flags & EXEC_SECURE;
142
c0154f46
RM
143 _dl_phdr = (ElfW(Phdr) *) d->phdr;
144 _dl_phnum = d->phdrsz / sizeof (ElfW(Phdr));
145 assert (d->phdrsz % sizeof (ElfW(Phdr)) == 0);
9d78d122
RM
146#endif
147
148 _hurd_init_dtable = d->dtable;
149 _hurd_init_dtablesize = d->dtablesize;
150
151 {
152 /* Check if the stack we are now on is different from
153 the one described by _hurd_stack_{base,size}. */
154
155 char dummy;
156 const vm_address_t newsp = (vm_address_t) &dummy;
157
158 if (d->stack_size != 0 && (newsp < d->stack_base ||
159 newsp - d->stack_base > d->stack_size))
160 /* The new stack pointer does not intersect with the
161 stack the exec server set up for us, so free that stack. */
162 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
163 }
164
165 if (d->portarray || d->intarray)
99b306dc
RM
166 /* Initialize library data structures, start signal processing, etc. */
167 _hurd_init (d->flags, argv,
168 d->portarray, d->portarraysize,
169 d->intarray, d->intarraysize);
99b306dc
RM
170}
171
a13d5ca5 172
77dd7355
RM
173static inline void
174init (int *data)
175{
176 int argc = *data;
177 char **argv = (void *) (data + 1);
178 char **envp = &argv[argc + 1];
179 struct hurd_startup_data *d;
180 unsigned long int threadvars[_HURD_THREADVAR_MAX];
181
16195d20
MK
182 /* Provide temporary storage for thread-specific variables on the
183 startup stack so the cthreads initialization code can use them
184 for malloc et al, or so we can use malloc below for the real
185 threadvars array. */
77dd7355 186 memset (threadvars, 0, sizeof threadvars);
cc13edc8 187 threadvars[_HURD_THREADVAR_LOCALE] = (unsigned long int) &_nl_global_locale;
77dd7355
RM
188 __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
189
16195d20
MK
190 /* Since the cthreads initialization code uses malloc, and the
191 malloc initialization code needs to get at the environment, make
192 sure we can find it. We'll need to do this again later on since
193 switching stacks changes the location where the environment is
194 stored. */
195 __environ = envp;
196
77dd7355
RM
197 while (*envp)
198 ++envp;
199 d = (void *) ++envp;
200
201 /* The user might have defined a value for this, to get more variables.
202 Otherwise it will be zero on startup. We must make sure it is set
203 properly before before cthreads initialization, so cthreads can know
204 how much space to leave for thread variables. */
205 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
206 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
207
208
209 /* After possibly switching stacks, call `init1' (above) with the user
210 code as the return address, and the argument data immediately above
211 that on the stack. */
212
133bf22e 213 if (&_cthread_init_routine && _cthread_init_routine)
77dd7355
RM
214 {
215 /* Initialize cthreads, which will allocate us a new stack to run on. */
66cc59de 216 int *newsp = (*_cthread_init_routine) ();
77dd7355
RM
217 struct hurd_startup_data *od;
218
83ddec31
RM
219 void switch_stacks (void);
220
5d5722e8
ST
221 __libc_stack_end = newsp;
222
77dd7355
RM
223 /* Copy per-thread variables from that temporary
224 area onto the new cthread stack. */
225 memcpy (__hurd_threadvar_location_from_sp (0, newsp),
226 threadvars, sizeof threadvars);
227
228 /* Copy the argdata from the old stack to the new one. */
229 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
230 (char *) d - (char *) data);
231
8443afdc
MK
232#ifdef SHARED
233 /* And readjust the dynamic linker's idea of where the argument
18bad2ae 234 vector lives. */
8443afdc 235 assert (_dl_argv == argv);
66cc59de 236 _dl_argv = (void *) (newsp + 1);
8443afdc
MK
237#endif
238
77dd7355
RM
239 /* Set up the Hurd startup data block immediately following
240 the argument and environment pointers on the new stack. */
66cc59de 241 od = ((void *) newsp + ((char *) d - (char *) data));
77dd7355
RM
242 if ((void *) argv[0] == d)
243 /* We were started up by the kernel with arguments on the stack.
244 There is no Hurd startup data, so zero the block. */
245 memset (od, 0, sizeof *od);
246 else
247 /* Copy the Hurd startup data block to the new stack. */
248 *od = *d;
249
250 /* Push the user code address on the top of the new stack. It will
251 be the return address for `init1'; we will jump there with NEWSP
252 as the stack pointer. */
18bad2ae
TS
253 /* The following expression would typically be written as
254 ``__builtin_return_address (0)''. But, for example, GCC 4.4.6 doesn't
255 recognize that this read operation may alias the following write
256 operation, and thus is free to reorder the two, clobbering the
257 original return address. */
258 *--newsp = *((int *) __builtin_frame_address (0) + 1);
259 /* GCC 4.4.6 also wants us to force loading *NEWSP already here. */
260 asm volatile ("# %0" : : "X" (*newsp));
261 *((void **) __builtin_frame_address (0) + 1) = &switch_stacks;
262 /* Force NEWSP into %eax and &init1 into %ecx, which are not restored
77dd7355
RM
263 by function return. */
264 asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1));
265 }
266 else
267 {
268 /* We are not using cthreads, so we will have just a single allocated
269 area for the per-thread variables of the main user thread. */
270 unsigned long int *array;
271 unsigned int i;
272 int usercode;
273
83ddec31
RM
274 void call_init1 (void);
275
77dd7355
RM
276 array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
277 if (array == NULL)
278 __libc_fatal ("Can't allocate single-threaded thread variables.");
279
280 /* Copy per-thread variables from the temporary array into the
281 newly malloc'd space. */
282 memcpy (array, threadvars, sizeof threadvars);
283 __hurd_threadvar_stack_offset = (unsigned long int) array;
284 for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
285 array[i] = 0;
286
287 /* The argument data is just above the stack frame we will unwind by
288 returning. Mutate our own return address to run the code below. */
18bad2ae
TS
289 /* The following expression would typically be written as
290 ``__builtin_return_address (0)''. But, for example, GCC 4.4.6 doesn't
291 recognize that this read operation may alias the following write
292 operation, and thus is free to reorder the two, clobbering the
293 original return address. */
294 usercode = *((int *) __builtin_frame_address (0) + 1);
295 /* GCC 4.4.6 also wants us to force loading USERCODE already here. */
296 asm volatile ("# %0" : : "X" (usercode));
297 *((void **) __builtin_frame_address (0) + 1) = &call_init1;
77dd7355
RM
298 /* Force USERCODE into %eax and &init1 into %ecx, which are not
299 restored by function return. */
300 asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1));
301 }
83ddec31 302}
77dd7355 303
83ddec31
RM
304/* These bits of inline assembler used to be located inside `init'.
305 However they were optimized away by gcc 2.95. */
306
307/* The return address of `init' above, was redirected to here, so at
308 this point our stack is unwound and callers' registers restored.
309 Only %ecx and %eax are call-clobbered and thus still have the
310 values we set just above. Fetch from there the new stack pointer
311 we will run on, and jmp to the run-time address of `init1'; when it
312 returns, it will run the user code with the argument data at the
313 top of the stack. */
ca18306b
RM
314asm ("switch_stacks:\n"
315 " movl %eax, %esp\n"
316 " jmp *%ecx");
83ddec31
RM
317
318/* As in the stack-switching case, at this point our stack is unwound
319 and callers' registers restored, and only %ecx and %eax communicate
320 values from the lines above. In this case we have stashed in %eax
321 the user code return address. Push it on the top of the stack so
322 it acts as init1's return address, and then jump there. */
ca18306b
RM
323asm ("call_init1:\n"
324 " push %eax\n"
325 " jmp *%ecx\n");
77dd7355
RM
326
327
5879ee9f
RM
328/* Do the first essential initializations that must precede all else. */
329static inline void
330first_init (void)
99b306dc
RM
331{
332 /* Initialize data structures so we can do RPCs. */
333 __mach_init ();
334
335 RUN_HOOK (_hurd_preinit_hook, ());
5879ee9f
RM
336}
337
338#ifdef SHARED
339/* This function is called specially by the dynamic linker to do early
340 initialization of the shared C library before normal initializers
341 expecting a Posixoid environment can run. It gets called with the
342 stack set up just as the user will see it, so it can switch stacks. */
343
344void
18bad2ae 345_dl_init_first (int argc, ...)
5879ee9f
RM
346{
347 first_init ();
2604afb1 348
18bad2ae
TS
349 /* If we use ``__builtin_frame_address (0) + 2'' here, GCC gets confused. */
350 init (&argc);
54509b04 351}
77dd7355 352#endif
99b306dc
RM
353
354
5879ee9f
RM
355#ifdef SHARED
356/* The regular posixland initialization is what goes into libc's
357 normal initializer. */
358/* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
359 pointer in the dynamic section based solely on that. It is convention
360 for this function to be in the `.init' section, but the symbol name is
361 the only thing that really matters!! */
362strong_alias (posixland_init, _init);
363
99b306dc 364void
a13d5ca5 365__libc_init_first (int argc, char **argv, char **envp)
99b306dc 366{
a13d5ca5
RM
367 /* Everything was done in the shared library initializer, _init. */
368}
369#else
5879ee9f
RM
370strong_alias (posixland_init, __libc_init_first);
371
0ca7e46e 372
a13d5ca5
RM
373/* XXX This is all a crock and I am not happy with it.
374 This poorly-named function is called by static-start.S,
375 which should not exist at all. */
0ca7e46e 376void
133bf22e 377_hurd_stack_setup (void)
0ca7e46e 378{
133bf22e
RM
379 intptr_t caller = (intptr_t) __builtin_return_address (0);
380
cc13edc8 381 void doinit (intptr_t *data)
99b306dc 382 {
d819080c 383 /* This function gets called with the argument data at TOS. */
18bad2ae 384 void doinit1 (int argc, ...)
77dd7355 385 {
18bad2ae
TS
386 /* If we use ``__builtin_frame_address (0) + 2'' here, GCC gets
387 confused. */
388 init ((int *) &argc);
77dd7355 389 }
d819080c
RM
390
391 /* Push the user return address after the argument data, and then
18bad2ae
TS
392 jump to `doinit1' (above), so it is as if __libc_init_first's
393 caller had called `doinit1' with the argument data already on the
394 stack. */
133bf22e 395 *--data = caller;
d819080c
RM
396 asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */
397 "movl $0, %%ebp\n" /* Clear outermost frame pointer. */
66cc59de 398 "jmp *%1" : : "r" (data), "r" (&doinit1) : "sp");
d819080c 399 /* NOTREACHED */
99b306dc
RM
400 }
401
5879ee9f 402 first_init ();
2604afb1 403
133bf22e 404 _hurd_startup ((void **) __builtin_frame_address (0) + 2, &doinit);
99b306dc 405}
a13d5ca5 406#endif
8a080f40
TBB
407
408
409/* This function is defined here so that if this file ever gets into
410 ld.so we will get a link error. Having this file silently included
411 in ld.so causes disaster, because the _init definition above will
412 cause ld.so to gain an init function, which is not a cool thing. */
413
2604afb1
UD
414void
415_dl_start (void)
416{
417 abort ();
8a080f40 418}