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