1 /* Initialization code run first thing by the ELF startup code. PowerPC/Hurd.
2 Copyright (C) 1995-2001, 2002, 2003 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
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 **);
36 extern void _dl_non_dynamic_init (void) internal_function
;
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
;
45 int __libc_enable_secure
;
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__
));
57 static unsigned int return_address
; /* Make init1 return to _start. */
60 /* Things that want to be run before _hurd_init or much anything else.
61 Importantly, these are called before anything tries to use malloc. */
62 DEFINE_HOOK (_hurd_preinit_hook
, (void));
65 /* We call this once the Hurd magic is all set up and we are ready to be a
66 Posixoid program. This does the same things the generic version does. */
67 static void internal_function
68 posixland_init (int argc
, char **argv
, char **envp
)
70 asm ("li 3,0xbb; .long 0");
76 _dl_non_dynamic_init ();
78 __init_misc (argc
, argv
, envp
);
80 #ifdef USE_NONOPTION_FLAGS
81 /* This is a hack to make the special getopt in GNU libc working. */
82 __getopt_clean_environment (__environ
);
86 __libc_global_ctors ();
95 char **argv
= (char **) &data
[1];
96 char **envp
= &argv
[argc
+ 1];
97 struct hurd_startup_data
*d
;
103 /* If we are the bootstrap task started by the kernel,
104 then after the environment pointers there is no Hurd
105 data block; the argument strings start there. */
106 /* OSF Mach starts the bootstrap task with argc == 0.
107 XXX This fails if a non-bootstrap task gets started
109 if (argc
&& (void *) d
!= argv
[0])
111 _hurd_init_dtable
= d
->dtable
;
112 _hurd_init_dtablesize
= d
->dtablesize
;
114 #if 0 /* We can't free the old stack because it contains the argument strings. */
116 /* Check if the stack we are now on is different from
117 the one described by _hurd_stack_{base,size}. */
120 const vm_address_t newsp
= (vm_address_t
) &dummy
;
122 if (d
->stack_size
!= 0 && (newsp
< d
->stack_base
||
123 newsp
- d
->stack_base
> d
->stack_size
))
124 /* The new stack pointer does not intersect with the
125 stack the exec server set up for us, so free that stack. */
126 __vm_deallocate (__mach_task_self (), d
->stack_base
, d
->stack_size
);
131 if (argc
&& (void *) d
!= argv
[0] && (d
->portarray
|| d
->intarray
))
132 /* Initialize library data structures, start signal processing, etc. */
133 _hurd_init (d
->flags
, argv
,
134 d
->portarray
, d
->portarraysize
,
135 d
->intarray
, d
->intarraysize
);
138 __libc_enable_secure
= d
->flags
& EXEC_SECURE
;
147 char **argv
= (void *) (data
+ 1);
148 char **envp
= &argv
[argc
+ 1];
149 struct hurd_startup_data
*d
;
150 unsigned long int threadvars
[_HURD_THREADVAR_MAX
];
152 /* Provide temporary storage for thread-specific variables on the startup
153 stack so the cthreads initialization code can use them for malloc et al,
154 or so we can use malloc below for the real threadvars array. */
155 memset (threadvars
, 0, sizeof threadvars
);
156 __hurd_threadvar_stack_offset
= (unsigned long int) threadvars
;
162 /* The user might have defined a value for this, to get more variables.
163 Otherwise it will be zero on startup. We must make sure it is set
164 properly before before cthreads initialization, so cthreads can know
165 how much space to leave for thread variables. */
166 if (__hurd_threadvar_max
< _HURD_THREADVAR_MAX
)
167 __hurd_threadvar_max
= _HURD_THREADVAR_MAX
;
170 /* After possibly switching stacks, call `init1' (above) with the user
171 code as the return address, and the argument data immediately above
172 that on the stack. */
174 if (_cthread_init_routine
)
176 /* Initialize cthreads, which will allocate us a new stack to run on. */
177 void *newsp
= (*_cthread_init_routine
) ();
178 struct hurd_startup_data
*od
;
181 unsigned int i
, data_offset
;
184 /* Copy per-thread variables from that temporary
185 area onto the new cthread stack. */
186 memcpy (__hurd_threadvar_location_from_sp (0, newsp
),
187 threadvars
, sizeof threadvars
);
189 /* Copy the argdata from the old stack to the new one. */
190 newsp
= memcpy (newsp
- ((char *) &d
[1] - (char *) data
), data
,
191 (char *) d
- (char *) data
);
194 /* And readjust the dynamic linker's idea of where the argument
196 assert (_dl_argv
== argv
);
197 _dl_argv
= (void *) ((int *) newsp
+ 1);
200 /* Set up the Hurd startup data block immediately following
201 the argument and environment pointers on the new stack. */
202 od
= (newsp
+ ((char *) d
- (char *) data
));
203 if (!argc
|| (void *) argv
[0] == d
)
204 /* We were started up by the kernel with arguments on the stack.
205 There is no Hurd startup data, so zero the block. */
206 memset (od
, 0, sizeof *od
);
208 /* Copy the Hurd startup data block to the new stack. */
212 asm ("mtlr %0; mr 1,%1; li 0,0; mr 3,%1; stwu 0,-16(1); b init1"
213 : : "r" (return_address
), "r" (newsp
));
214 (void) init1
; /* To avoid `defined but not used' warning. */
217 /* Copy the rest of the stack. Don't call a function to do that,
218 because that will alter the current stack. */
219 asm ("mr %0,1" : "=r" (oldsp
));
220 data_offset
= (unsigned int) data
- (unsigned int) oldsp
;
221 newsp
-= data_offset
;
222 for (i
= 0; i
< data_offset
/ 4; i
++)
223 ((unsigned int *)newsp
)[i
] = ((unsigned int *)oldsp
)[i
];
225 /* Relocate stack frames. */
227 unsigned int *oldframe0
= (unsigned int *)oldsp
;
228 unsigned int *oldframe1
= *(unsigned int **)oldframe0
;
229 unsigned int *oldframe2
= *(unsigned int **)oldframe1
;
230 unsigned int *newframe0
= (unsigned int *)newsp
;
231 unsigned int *newframe1
= newframe0
+ (unsigned int)(oldframe1
- oldframe0
);
232 unsigned int *newframe2
= newframe1
+ (unsigned int)(oldframe2
- oldframe1
);
233 *(unsigned int **)newframe0
= newframe1
;
234 *(unsigned int **)newframe1
= newframe2
;
237 asm ("mr 1,%0; mr 31,%0" : : "r" (newsp
)); /* XXX */
238 init1 (newsp
+ data_offset
);
243 /* We are not using cthreads, so we will have just a single allocated
244 area for the per-thread variables of the main user thread. */
245 unsigned long int *array
;
248 array
= malloc (__hurd_threadvar_max
* sizeof (unsigned long int));
250 __libc_fatal ("Can't allocate single-threaded thread variables.");
252 /* Copy per-thread variables from the temporary array into the
253 newly malloc'd space. */
254 memcpy (array
, threadvars
, sizeof threadvars
);
255 __hurd_threadvar_stack_offset
= (unsigned long int) array
;
256 for (i
= _HURD_THREADVAR_MAX
; i
< __hurd_threadvar_max
; ++i
)
260 asm ("mr 3,%0; mtlr %1; addi 1,3,-16; b init1"
261 : : "r" (data
), "r" (return_address
));
270 /* Do the first essential initializations that must precede all else. */
274 /* Initialize data structures so we can do RPCs. */
277 RUN_HOOK (_hurd_preinit_hook
, ());
281 /* This function is called specially by the dynamic linker to do early
282 initialization of the shared C library before normal initializers
283 expecting a Posixoid environment can run. It gets called with the
284 stack set up just as the user will see it, so it can switch stacks. */
287 _dl_init_first (int argc
, ...)
289 asm ("li 3,0xaa; .long 0");
298 /* The regular posixland initialization is what goes into libc's
299 normal initializer. */
300 /* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
301 pointer in the dynamic section based solely on that. It is convention
302 for this function to be in the `.init' section, but the symbol name is
303 the only thing that really matters!! */
304 strong_alias (posixland_init
, _init
);
308 __libc_init_first (int argc
, char **argv
, char **envp
)
310 /* Everything was done in the shared library initializer, _init. */
313 strong_alias (posixland_init
, __libc_init_first
);
317 _hurd_stack_setup (int *data
)
319 register unsigned int address
;
320 asm ("mflr %0" : "=r" (address
));
321 return_address
= address
;
325 _hurd_startup ((void **) data
, &init
);
330 /* This function is defined here so that if this file ever gets into
331 ld.so we will get a link error. Having this file silently included
332 in ld.so causes disaster, because the _init definition above will
333 cause ld.so to gain an init function, which is not a cool thing. */