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

source code of glibc/sysdeps/mach/hurd/i386/init-first.c