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