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