]>
Commit | Line | Data |
---|---|---|
99b306dc | 1 | /* Initialization code run first thing by the ELF startup code. For i386/Hurd. |
7cc27f44 UD |
2 | Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. | |
99b306dc | 4 | |
7cc27f44 UD |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | 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 | |
13 | Library General Public License for more details. | |
99b306dc | 14 | |
7cc27f44 UD |
15 | You should have received a copy of the GNU Library General Public |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
99b306dc RM |
19 | |
20 | #include <hurd.h> | |
21 | #include <stdio.h> | |
22 | #include <unistd.h> | |
a2fe9c76 | 23 | #include <string.h> |
99b306dc RM |
24 | #include "hurdstartup.h" |
25 | #include "set-hooks.h" | |
26 | #include "hurdmalloc.h" /* XXX */ | |
27 | ||
28 | extern void __mach_init (void); | |
29 | extern void __libc_init (int, char **, char **); | |
2604afb1 | 30 | extern void __getopt_clean_environment (char **); |
0324daa0 | 31 | extern void __libc_global_ctors (void); |
99b306dc | 32 | |
6bac11d9 MB |
33 | unsigned int __hurd_threadvar_max; |
34 | unsigned long int __hurd_threadvar_stack_offset; | |
35 | unsigned long int __hurd_threadvar_stack_mask; | |
36 | ||
548330d3 TBB |
37 | int __libc_multiple_libcs = 1; |
38 | ||
6bac11d9 MB |
39 | extern int __libc_argc; |
40 | extern char **__libc_argv; | |
acf51e02 | 41 | |
7cc27f44 UD |
42 | /* We often need the PID. Cache this value. */ |
43 | pid_t __libc_pid; | |
44 | ||
99b306dc RM |
45 | void *(*_cthread_init_routine) (void); /* Returns new SP to use. */ |
46 | void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__)); | |
47 | ||
99b306dc RM |
48 | /* Things that want to be run before _hurd_init or much anything else. |
49 | Importantly, these are called before anything tries to use malloc. */ | |
50 | DEFINE_HOOK (_hurd_preinit_hook, (void)); | |
51 | ||
99b306dc RM |
52 | static void |
53 | init1 (int argc, char *arg0, ...) | |
54 | { | |
55 | char **argv = &arg0; | |
56 | char **envp = &argv[argc + 1]; | |
57 | struct hurd_startup_data *d; | |
58 | ||
acf51e02 TBB |
59 | __libc_argc = argc; |
60 | __libc_argv = argv; | |
99b306dc RM |
61 | __environ = envp; |
62 | while (*envp) | |
63 | ++envp; | |
64 | d = (void *) ++envp; | |
65 | ||
66 | /* If we are the bootstrap task started by the kernel, | |
67 | then after the environment pointers there is no Hurd | |
68 | data block; the argument strings start there. */ | |
69 | if ((void *) d != argv[0]) | |
70 | { | |
71 | _hurd_init_dtable = d->dtable; | |
72 | _hurd_init_dtablesize = d->dtablesize; | |
73 | ||
74 | { | |
75 | /* Check if the stack we are now on is different from | |
76 | the one described by _hurd_stack_{base,size}. */ | |
77 | ||
78 | char dummy; | |
79 | const vm_address_t newsp = (vm_address_t) &dummy; | |
80 | ||
81 | if (d->stack_size != 0 && (newsp < d->stack_base || | |
82 | newsp - d->stack_base > d->stack_size)) | |
83 | /* The new stack pointer does not intersect with the | |
84 | stack the exec server set up for us, so free that stack. */ | |
85 | __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size); | |
86 | } | |
87 | } | |
88 | ||
89 | if (__hurd_threadvar_stack_mask == 0) | |
90 | { | |
91 | /* We are not using cthreads, so we will have just a single allocated | |
92 | area for the per-thread variables of the main user thread. */ | |
93 | unsigned long int i; | |
94 | __hurd_threadvar_stack_offset | |
95 | = (unsigned long int) malloc (__hurd_threadvar_max * | |
96 | sizeof (unsigned long int)); | |
97 | if (__hurd_threadvar_stack_offset == 0) | |
98 | __libc_fatal ("Can't allocate single-threaded per-thread variables."); | |
99 | for (i = 0; i < __hurd_threadvar_max; ++i) | |
100 | ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0; | |
101 | } | |
102 | ||
103 | if ((void *) d != argv[0] && (d->portarray || d->intarray)) | |
104 | /* Initialize library data structures, start signal processing, etc. */ | |
105 | _hurd_init (d->flags, argv, | |
106 | d->portarray, d->portarraysize, | |
107 | d->intarray, d->intarraysize); | |
108 | ||
109 | __libc_init (argc, argv, __environ); | |
7cc27f44 UD |
110 | |
111 | /* This is a hack to make the special getopt in GNU libc working. */ | |
2604afb1 | 112 | __getopt_clean_environment (envp); |
7cc27f44 | 113 | |
0324daa0 RM |
114 | #ifdef PIC |
115 | __libc_global_ctors (); | |
116 | #endif | |
99b306dc RM |
117 | } |
118 | ||
a1a9d215 | 119 | static void |
d819080c | 120 | init (int *data) |
99b306dc RM |
121 | { |
122 | int argc = *data; | |
123 | char **argv = (void *) (data + 1); | |
124 | char **envp = &argv[argc + 1]; | |
125 | struct hurd_startup_data *d; | |
126 | ||
127 | __environ = envp; | |
128 | while (*envp) | |
129 | ++envp; | |
130 | d = (void *) ++envp; | |
131 | ||
132 | /* The user might have defined a value for this, to get more variables. | |
133 | Otherwise it will be zero on startup. We must make sure it is set | |
134 | properly before before cthreads initialization, so cthreads can know | |
135 | how much space to leave for thread variables. */ | |
136 | if (__hurd_threadvar_max < _HURD_THREADVAR_MAX) | |
137 | __hurd_threadvar_max = _HURD_THREADVAR_MAX; | |
138 | ||
a1a9d215 RM |
139 | |
140 | /* After possibly switching stacks, call `init1' (above) with the user | |
141 | code as the return address, and the argument data immediately above | |
142 | that on the stack. */ | |
143 | ||
99b306dc RM |
144 | if (_cthread_init_routine) |
145 | { | |
146 | /* Initialize cthreads, which will allocate us a new stack to run on. */ | |
147 | void *newsp = (*_cthread_init_routine) (); | |
a2fe9c76 RM |
148 | struct hurd_startup_data *od; |
149 | ||
99b306dc RM |
150 | /* Copy the argdata from the old stack to the new one. */ |
151 | newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data, | |
a2fe9c76 RM |
152 | (char *) d - (char *) data); |
153 | ||
154 | /* Set up the Hurd startup data block immediately following | |
155 | the argument and environment pointers on the new stack. */ | |
156 | od = (newsp + ((char *) d - (char *) data)); | |
157 | if ((void *) argv[0] == d) | |
158 | /* We were started up by the kernel with arguments on the stack. | |
159 | There is no Hurd startup data, so zero the block. */ | |
160 | memset (od, 0, sizeof *od); | |
161 | else | |
162 | /* Copy the Hurd startup data block to the new stack. */ | |
163 | *od = *d; | |
164 | ||
a1a9d215 RM |
165 | /* Push the user code address on the top of the new stack. It will |
166 | be the return address for `init1'; we will jump there with NEWSP | |
167 | as the stack pointer. */ | |
d819080c RM |
168 | *--(int *) newsp = data[-1]; |
169 | ((void **) data)[-1] = &&switch_stacks; | |
a1a9d215 | 170 | /* Force NEWSP into %ecx and &init1 into %eax, which are not restored |
d819080c RM |
171 | by function return. */ |
172 | asm volatile ("# a %0 c %1" : : "a" (newsp), "c" (&init1)); | |
2604afb1 | 173 | } |
a1a9d215 RM |
174 | else |
175 | { | |
d819080c RM |
176 | /* The argument data is just above the stack frame we will unwind by |
177 | returning. Mutate our own return address to run the code below. */ | |
178 | int usercode = data[-1]; | |
179 | ((void **) data)[-1] = &&call_init1; | |
180 | /* Force USERCODE into %eax and &init1 into %ecx, which are not | |
181 | restored by function return. */ | |
182 | asm volatile ("# a %0 c %1" : : "a" (usercode), "c" (&init1)); | |
99b306dc | 183 | } |
d819080c RM |
184 | |
185 | return; | |
186 | ||
187 | switch_stacks: | |
188 | /* Our return address was redirected to here, so at this point our stack | |
189 | is unwound and callers' registers restored. Only %ecx and %eax are | |
190 | call-clobbered and thus still have the values we set just above. | |
191 | Fetch from there the new stack pointer we will run on, and jmp to the | |
192 | run-time address of `init1'; when it returns, it will run the user | |
193 | code with the argument data at the top of the stack. */ | |
194 | asm volatile ("movl %eax, %esp; jmp *%ecx"); | |
195 | /* NOTREACHED */ | |
196 | ||
197 | call_init1: | |
198 | /* As in the stack-switching case, at this point our stack is unwound and | |
199 | callers' registers restored, and only %ecx and %eax communicate values | |
200 | from the lines above. In this case we have stashed in %eax the user | |
201 | code return address. Push it on the top of the stack so it acts as | |
202 | init1's return address, and then jump there. */ | |
203 | asm volatile ("pushl %eax; jmp *%ecx"); | |
204 | /* NOTREACHED */ | |
2604afb1 | 205 | } |
99b306dc RM |
206 | |
207 | ||
208 | #ifdef PIC | |
209 | /* This function is called to initialize the shared C library. | |
210 | It is called just before the user _start code from i386/elf/start.S, | |
211 | with the stack set up as that code gets it. */ | |
212 | ||
a1a9d215 RM |
213 | /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT |
214 | pointer in the dynamic section based solely on that. It is convention | |
215 | for this function to be in the `.init' section, but the symbol name is | |
216 | the only thing that really matters!! */ | |
217 | /*void _init (int argc, ...) __attribute__ ((unused, section (".init")));*/ | |
99b306dc | 218 | |
a1a9d215 RM |
219 | void |
220 | _init (int argc, ...) | |
99b306dc RM |
221 | { |
222 | /* Initialize data structures so we can do RPCs. */ | |
223 | __mach_init (); | |
224 | ||
225 | RUN_HOOK (_hurd_preinit_hook, ()); | |
2604afb1 | 226 | |
d819080c | 227 | init (&argc); |
99b306dc RM |
228 | } |
229 | #endif | |
230 | ||
231 | ||
232 | void | |
a1a9d215 | 233 | __libc_init_first (int argc __attribute__ ((unused)), ...) |
99b306dc RM |
234 | { |
235 | #ifndef PIC | |
236 | void doinit (int *data) | |
237 | { | |
d819080c RM |
238 | /* This function gets called with the argument data at TOS. */ |
239 | void doinit1 (int argc, ...) | |
240 | { | |
241 | init (&argc); | |
242 | } | |
243 | ||
244 | /* Push the user return address after the argument data, and then | |
245 | jump to `doinit1' (above), so it is as if __libc_init_first's | |
246 | caller had called `doinit1' with the argument data already on the | |
247 | stack. */ | |
248 | *--data = (&argc)[-1]; | |
249 | asm volatile ("movl %0, %%esp\n" /* Switch to new outermost stack. */ | |
250 | "movl $0, %%ebp\n" /* Clear outermost frame pointer. */ | |
251 | "jmp *%1" : : "r" (data), "r" (&doinit1)); | |
252 | /* NOTREACHED */ | |
99b306dc RM |
253 | } |
254 | ||
255 | /* Initialize data structures so we can do RPCs. */ | |
256 | __mach_init (); | |
257 | ||
258 | RUN_HOOK (_hurd_preinit_hook, ()); | |
2604afb1 | 259 | |
99b306dc RM |
260 | _hurd_startup ((void **) &argc, &doinit); |
261 | #endif | |
262 | } | |
8a080f40 TBB |
263 | |
264 | ||
265 | /* This function is defined here so that if this file ever gets into | |
266 | ld.so we will get a link error. Having this file silently included | |
267 | in ld.so causes disaster, because the _init definition above will | |
268 | cause ld.so to gain an init function, which is not a cool thing. */ | |
269 | ||
2604afb1 UD |
270 | void |
271 | _dl_start (void) | |
272 | { | |
273 | abort (); | |
8a080f40 | 274 | } |