Update.
[glibc.git] / sysdeps / mach / hurd / powerpc / init-first.c
blobc9ad66096b37072ae746adb98f5d151af8e8e670
1 /* Initialization code run first thing by the ELF startup code. PowerPC/Hurd.
2 Copyright (C) 1995-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.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 */
30 extern void __mach_init (void);
31 extern void __libc_init (int, char **, char **);
32 extern void __init_misc (int, char **, char **);
33 #ifdef USE_NONOPTION_FLAGS
34 extern void __getopt_clean_environment (char **);
35 #endif
36 extern void __libc_global_ctors (void);
38 unsigned int __hurd_threadvar_max;
39 unsigned long int __hurd_threadvar_stack_offset;
40 unsigned long int __hurd_threadvar_stack_mask;
42 #ifndef SHARED
43 int __libc_enable_secure;
44 #endif
45 int __libc_multiple_libcs = 1;
47 extern int __libc_argc;
48 extern char **__libc_argv;
49 extern char **_dl_argv;
51 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
52 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
54 #ifndef SHARED
55 static unsigned int return_address; /* Make init1 return to _start. */
56 #endif
58 /* Things that want to be run before _hurd_init or much anything else.
59 Importantly, these are called before anything tries to use malloc. */
60 DEFINE_HOOK (_hurd_preinit_hook, (void));
63 /* We call this once the Hurd magic is all set up and we are ready to be a
64 Posixoid program. This does the same things the generic version does. */
65 static void internal_function
66 posixland_init (int argc, char **argv, char **envp)
68 asm ("li 3,0xbb; .long 0");
69 __libc_argc = argc;
70 __libc_argv = argv;
71 __environ = envp;
73 __init_misc (argc, argv, envp);
74 __libc_init (argc, argv, envp);
76 #ifdef USE_NONOPTION_FLAGS
77 /* This is a hack to make the special getopt in GNU libc working. */
78 __getopt_clean_environment (__environ);
79 #endif
81 #ifdef SHARED
82 __libc_global_ctors ();
83 #endif
87 static void
88 init1 (int *data)
90 int argc = *data;
91 char **argv = (char **) &data[1];
92 char **envp = &argv[argc + 1];
93 struct hurd_startup_data *d;
95 while (*envp)
96 ++envp;
97 d = (void *) ++envp;
99 /* If we are the bootstrap task started by the kernel,
100 then after the environment pointers there is no Hurd
101 data block; the argument strings start there. */
102 /* OSF Mach starts the bootstrap task with argc == 0.
103 XXX This fails if a non-bootstrap task gets started
104 with argc == 0. */
105 if (argc && (void *) d != argv[0])
107 _hurd_init_dtable = d->dtable;
108 _hurd_init_dtablesize = d->dtablesize;
110 #if 0 /* We can't free the old stack because it contains the argument strings. */
112 /* Check if the stack we are now on is different from
113 the one described by _hurd_stack_{base,size}. */
115 char dummy;
116 const vm_address_t newsp = (vm_address_t) &dummy;
118 if (d->stack_size != 0 && (newsp < d->stack_base ||
119 newsp - d->stack_base > d->stack_size))
120 /* The new stack pointer does not intersect with the
121 stack the exec server set up for us, so free that stack. */
122 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
124 #endif
127 if (argc && (void *) d != argv[0] && (d->portarray || d->intarray))
128 /* Initialize library data structures, start signal processing, etc. */
129 _hurd_init (d->flags, argv,
130 d->portarray, d->portarraysize,
131 d->intarray, d->intarraysize);
133 #ifndef SHARED
134 __libc_enable_secure = d->flags & EXEC_SECURE;
135 #endif
139 static inline void
140 init (int *data)
142 int argc = *data;
143 char **argv = (void *) (data + 1);
144 char **envp = &argv[argc + 1];
145 struct hurd_startup_data *d;
146 unsigned long int threadvars[_HURD_THREADVAR_MAX];
148 /* Provide temporary storage for thread-specific variables on the startup
149 stack so the cthreads initialization code can use them for malloc et al,
150 or so we can use malloc below for the real threadvars array. */
151 memset (threadvars, 0, sizeof threadvars);
152 __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
154 while (*envp)
155 ++envp;
156 d = (void *) ++envp;
158 /* The user might have defined a value for this, to get more variables.
159 Otherwise it will be zero on startup. We must make sure it is set
160 properly before before cthreads initialization, so cthreads can know
161 how much space to leave for thread variables. */
162 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
163 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
166 /* After possibly switching stacks, call `init1' (above) with the user
167 code as the return address, and the argument data immediately above
168 that on the stack. */
170 if (_cthread_init_routine)
172 /* Initialize cthreads, which will allocate us a new stack to run on. */
173 void *newsp = (*_cthread_init_routine) ();
174 struct hurd_startup_data *od;
175 #ifdef SHARED
176 void *oldsp;
177 unsigned int i, data_offset;
178 #endif
180 /* Copy per-thread variables from that temporary
181 area onto the new cthread stack. */
182 memcpy (__hurd_threadvar_location_from_sp (0, newsp),
183 threadvars, sizeof threadvars);
185 /* Copy the argdata from the old stack to the new one. */
186 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
187 (char *) d - (char *) data);
189 #ifdef SHARED
190 /* And readjust the dynamic linker's idea of where the argument
191 vector lives. */
192 assert (_dl_argv == argv);
193 _dl_argv = (void *) ((int *) newsp + 1);
194 #endif
196 /* Set up the Hurd startup data block immediately following
197 the argument and environment pointers on the new stack. */
198 od = (newsp + ((char *) d - (char *) data));
199 if (!argc || (void *) argv[0] == d)
200 /* We were started up by the kernel with arguments on the stack.
201 There is no Hurd startup data, so zero the block. */
202 memset (od, 0, sizeof *od);
203 else
204 /* Copy the Hurd startup data block to the new stack. */
205 *od = *d;
207 #ifndef SHARED
208 asm ("mtlr %0; mr 1,%1; li 0,0; mr 3,%1; stwu 0,-16(1); b init1"
209 : : "r" (return_address), "r" (newsp));
210 (void) init1; /* To avoid `defined but not used' warning. */
211 /* NOTREACHED */
212 #else
213 /* Copy the rest of the stack. Don't call a function to do that,
214 because that will alter the current stack. */
215 asm ("mr %0,1" : "=r" (oldsp));
216 data_offset = (unsigned int) data - (unsigned int) oldsp;
217 newsp -= data_offset;
218 for (i = 0; i < data_offset / 4; i++)
219 ((unsigned int *)newsp)[i] = ((unsigned int *)oldsp)[i];
221 /* Relocate stack frames. */
223 unsigned int *oldframe0 = (unsigned int *)oldsp;
224 unsigned int *oldframe1 = *(unsigned int **)oldframe0;
225 unsigned int *oldframe2 = *(unsigned int **)oldframe1;
226 unsigned int *newframe0 = (unsigned int *)newsp;
227 unsigned int *newframe1 = newframe0 + (unsigned int)(oldframe1 - oldframe0);
228 unsigned int *newframe2 = newframe1 + (unsigned int)(oldframe2 - oldframe1);
229 *(unsigned int **)newframe0 = newframe1;
230 *(unsigned int **)newframe1 = newframe2;
233 asm ("mr 1,%0; mr 31,%0" : : "r" (newsp)); /* XXX */
234 init1 (newsp + data_offset);
235 #endif
237 else
239 /* We are not using cthreads, so we will have just a single allocated
240 area for the per-thread variables of the main user thread. */
241 unsigned long int *array;
242 unsigned int i;
244 array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
245 if (array == NULL)
246 __libc_fatal ("Can't allocate single-threaded thread variables.");
248 /* Copy per-thread variables from the temporary array into the
249 newly malloc'd space. */
250 memcpy (array, threadvars, sizeof threadvars);
251 __hurd_threadvar_stack_offset = (unsigned long int) array;
252 for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
253 array[i] = 0;
255 #ifndef SHARED
256 asm ("mr 3,%0; mtlr %1; addi 1,3,-16; b init1"
257 : : "r" (data), "r" (return_address));
258 /* NOTREACHED */
259 #else
260 init1 (data);
261 #endif
266 /* Do the first essential initializations that must precede all else. */
267 static inline void
268 first_init (void)
270 /* Initialize data structures so we can do RPCs. */
271 __mach_init ();
273 RUN_HOOK (_hurd_preinit_hook, ());
276 #ifdef SHARED
277 /* This function is called specially by the dynamic linker to do early
278 initialization of the shared C library before normal initializers
279 expecting a Posixoid environment can run. It gets called with the
280 stack set up just as the user will see it, so it can switch stacks. */
282 void
283 _dl_init_first (int argc, ...)
285 asm ("li 3,0xaa; .long 0");
286 first_init ();
288 init (&argc);
290 #endif
293 #ifdef SHARED
294 /* The regular posixland initialization is what goes into libc's
295 normal initializer. */
296 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
297 pointer in the dynamic section based solely on that. It is convention
298 for this function to be in the `.init' section, but the symbol name is
299 the only thing that really matters!! */
300 strong_alias (posixland_init, _init);
303 void
304 __libc_init_first (int argc, char **argv, char **envp)
306 /* Everything was done in the shared library initializer, _init. */
308 #else
309 strong_alias (posixland_init, __libc_init_first);
312 void
313 _hurd_stack_setup (int *data)
315 register unsigned int address;
316 asm ("mflr %0" : "=r" (address));
317 return_address = address;
319 first_init ();
321 _hurd_startup ((void **) data, &init);
323 #endif
326 /* This function is defined here so that if this file ever gets into
327 ld.so we will get a link error. Having this file silently included
328 in ld.so causes disaster, because the _init definition above will
329 cause ld.so to gain an init function, which is not a cool thing. */
331 void
332 _dl_start (void)
334 abort ();