2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / alpha / init-first.c
blob6e552258905c434033c010c7de8fe89c92a52738
1 /* Initialization code run first thing by the ELF startup code. Alpha/Hurd.
2 Copyright (C) 1995,96,97,98,99,2000,01,02,03 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 __init_misc (int, char **, char **);
32 #ifdef USE_NONOPTION_FLAGS
33 extern void __getopt_clean_environment (char **);
34 #endif
35 #ifndef SHARED
36 extern void _dl_non_dynamic_init (void) internal_function;
37 #endif
38 extern void __libc_global_ctors (void);
40 unsigned int __hurd_threadvar_max;
41 unsigned long int __hurd_threadvar_stack_offset;
42 unsigned long int __hurd_threadvar_stack_mask;
44 #ifndef SHARED
45 int __libc_enable_secure;
46 #endif
47 int __libc_multiple_libcs attribute_hidden = 1;
49 extern int __libc_argc attribute_hidden;
50 extern char **__libc_argv attribute_hidden;
51 extern char **_dl_argv;
53 void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
54 void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
56 /* Things that want to be run before _hurd_init or much anything else.
57 Importantly, these are called before anything tries to use malloc. */
58 DEFINE_HOOK (_hurd_preinit_hook, (void));
61 /* We call this once the Hurd magic is all set up and we are ready to be a
62 Posixoid program. This does the same things the generic version does. */
63 static void
64 posixland_init (int argc, char **argv, char **envp)
66 __libc_argc = argc;
67 __libc_argv = argv;
68 __environ = envp;
70 #ifndef SHARED
71 _dl_non_dynamic_init ();
72 #endif
73 __init_misc (argc, argv, envp);
75 #ifdef USE_NONOPTION_FLAGS
76 /* This is a hack to make the special getopt in GNU libc working. */
77 __getopt_clean_environment (envp);
78 #endif
80 #ifdef SHARED
81 __libc_global_ctors ();
82 #endif
86 static void
87 init1 (intptr_t *data)
89 int argc = (intptr_t) *data;
90 char **argv = (char **) &data[1];
91 char **envp = &argv[argc + 1];
92 struct hurd_startup_data *d;
94 while (*envp)
95 ++envp;
96 d = (void *) ++envp;
98 /* If we are the bootstrap task started by the kernel,
99 then after the environment pointers there is no Hurd
100 data block; the argument strings start there. */
101 /* OSF Mach starts the bootstrap task with argc == 0.
102 XXX This fails if a non-bootstrap task gets started
103 with argc == 0. */
104 if (argc && (void *) d != argv[0])
106 _hurd_init_dtable = d->dtable;
107 _hurd_init_dtablesize = d->dtablesize;
110 /* Check if the stack we are now on is different from
111 the one described by _hurd_stack_{base,size}. */
113 char dummy;
114 const vm_address_t newsp = (vm_address_t) &dummy;
116 if (d->stack_size != 0 && (newsp < d->stack_base ||
117 newsp - d->stack_base > d->stack_size))
118 /* The new stack pointer does not intersect with the
119 stack the exec server set up for us, so free that stack. */
120 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
124 if ((void *) d != argv[0] && (d->portarray || d->intarray))
125 /* Initialize library data structures, start signal processing, etc. */
126 _hurd_init (d->flags, argv,
127 d->portarray, d->portarraysize,
128 d->intarray, d->intarraysize);
130 #ifndef SHARED
131 __libc_enable_secure = d->flags & EXEC_SECURE;
132 #endif
136 static inline void
137 init (intptr_t *data)
139 int argc = *data;
140 char **argv = (void *) (data + 1);
141 char **envp = &argv[argc + 1];
142 struct hurd_startup_data *d;
143 unsigned long int threadvars[_HURD_THREADVAR_MAX];
145 /* Provide temporary storage for thread-specific variables on the
146 startup stack so the cthreads initialization code can use them
147 for malloc et al, or so we can use malloc below for the real
148 threadvars array. */
149 memset (threadvars, 0, sizeof threadvars);
150 __hurd_threadvar_stack_offset = (unsigned long int) threadvars;
152 /* Since the cthreads initialization code uses malloc, and the
153 malloc initialization code needs to get at the environment, make
154 sure we can find it. We'll need to do this again later on since
155 switching stacks changes the location where the environment is
156 stored. */
157 __environ = envp;
159 while (*envp)
160 ++envp;
161 d = (void *) ++envp;
163 /* The user might have defined a value for this, to get more variables.
164 Otherwise it will be zero on startup. We must make sure it is set
165 properly before before cthreads initialization, so cthreads can know
166 how much space to leave for thread variables. */
167 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
168 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
171 /* After possibly switching stacks, call `init1' (above) with the user
172 code as the return address, and the argument data immediately above
173 that on the stack. */
175 if (_cthread_init_routine)
177 /* Initialize cthreads, which will allocate us a new stack to run on. */
178 void *newsp = (*_cthread_init_routine) ();
179 struct hurd_startup_data *od;
181 void switch_stacks (void);
183 /* Copy per-thread variables from that temporary
184 area onto the new cthread stack. */
185 memcpy (__hurd_threadvar_location_from_sp (0, newsp),
186 threadvars, sizeof threadvars);
188 /* Copy the argdata from the old stack to the new one. */
189 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
190 (char *) d - (char *) data);
192 #ifdef SHARED
193 /* And readjust the dynamic linker's idea of where the argument
194 vector lives. */
195 assert (_dl_argv == argv);
196 _dl_argv = (void *) ((int *) newsp + 1);
197 #endif
199 /* Set up the Hurd startup data block immediately following
200 the argument and environment pointers on the new stack. */
201 od = (newsp + ((char *) d - (char *) data));
202 if ((void *) argv[0] == d)
203 /* We were started up by the kernel with arguments on the stack.
204 There is no Hurd startup data, so zero the block. */
205 memset (od, 0, sizeof *od);
206 else
207 /* Copy the Hurd startup data block to the new stack. */
208 *od = *d;
211 Force NEWSP into sp and &init1 into pv, then branch to pv (call init1).
213 asm volatile ("lda $30,0(%0); lda $27,0(%1); jsr $26,($27)"
214 : : "r" (newsp), "r" (&init1));
216 else
218 /* We are not using cthreads, so we will have just a single allocated
219 area for the per-thread variables of the main user thread. */
220 unsigned long int *array;
221 unsigned int i;
223 array = malloc (__hurd_threadvar_max * sizeof (unsigned long int));
224 if (array == NULL)
225 __libc_fatal ("Can't allocate single-threaded thread variables.");
227 /* Copy per-thread variables from the temporary array into the
228 newly malloc'd space. */
229 memcpy (array, threadvars, sizeof threadvars);
230 __hurd_threadvar_stack_offset = (unsigned long int) array;
231 for (i = _HURD_THREADVAR_MAX; i < __hurd_threadvar_max; ++i)
232 array[i] = 0;
234 init1 (data);
239 /* Do the first essential initializations that must precede all else. */
240 static inline void
241 first_init (void)
243 /* Initialize data structures so we can do RPCs. */
244 __mach_init ();
246 RUN_HOOK (_hurd_preinit_hook, ());
249 #ifdef SHARED
250 /* This function is called specially by the dynamic linker to do early
251 initialization of the shared C library before normal initializers
252 expecting a Posixoid environment can run. It gets called with the
253 stack set up just as the user will see it, so it can switch stacks. */
255 void
256 _dl_init_first (intptr_t argc, ...)
258 first_init ();
260 init (&argc);
262 #endif
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);
274 void
275 __libc_init_first (int argc, char **argv, char **envp)
277 /* Everything was done in the shared library initializer, _init. */
279 #else
280 strong_alias (posixland_init, __libc_init_first);
283 void
284 _hurd_stack_setup (volatile intptr_t argc, ...)
286 first_init ();
288 _hurd_startup ((void **) &argc, &init);
290 #endif
293 /* This function is defined here so that if this file ever gets into
294 ld.so we will get a link error. Having this file silently included
295 in ld.so causes disaster, because the _init definition above will
296 cause ld.so to gain an init function, which is not a cool thing. */
298 void
299 _dl_start (void)
301 abort ();