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