]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/i386/init-first.c
(libdl.so): Pass $(LDFLAGS.so).
[thirdparty/glibc.git] / sysdeps / mach / hurd / i386 / init-first.c
CommitLineData
99b306dc
RM
1/* Initialization code run first thing by the ELF startup code. For i386/Hurd.
2Copyright (C) 1995 Free Software Foundation, Inc.
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA. */
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
28extern void __mach_init (void);
29extern void __libc_init (int, char **, char **);
30
31void *(*_cthread_init_routine) (void); /* Returns new SP to use. */
32void (*_cthread_exit_routine) (int status) __attribute__ ((__noreturn__));
33
34
35/* Things that want to be run before _hurd_init or much anything else.
36 Importantly, these are called before anything tries to use malloc. */
37DEFINE_HOOK (_hurd_preinit_hook, (void));
38
39
40static void
41init1 (int argc, char *arg0, ...)
42{
43 char **argv = &arg0;
44 char **envp = &argv[argc + 1];
45 struct hurd_startup_data *d;
46
47 __environ = envp;
48 while (*envp)
49 ++envp;
50 d = (void *) ++envp;
51
52 /* If we are the bootstrap task started by the kernel,
53 then after the environment pointers there is no Hurd
54 data block; the argument strings start there. */
55 if ((void *) d != argv[0])
56 {
57 _hurd_init_dtable = d->dtable;
58 _hurd_init_dtablesize = d->dtablesize;
59
60 {
61 /* Check if the stack we are now on is different from
62 the one described by _hurd_stack_{base,size}. */
63
64 char dummy;
65 const vm_address_t newsp = (vm_address_t) &dummy;
66
67 if (d->stack_size != 0 && (newsp < d->stack_base ||
68 newsp - d->stack_base > d->stack_size))
69 /* The new stack pointer does not intersect with the
70 stack the exec server set up for us, so free that stack. */
71 __vm_deallocate (__mach_task_self (), d->stack_base, d->stack_size);
72 }
73 }
74
75 if (__hurd_threadvar_stack_mask == 0)
76 {
77 /* We are not using cthreads, so we will have just a single allocated
78 area for the per-thread variables of the main user thread. */
79 unsigned long int i;
80 __hurd_threadvar_stack_offset
81 = (unsigned long int) malloc (__hurd_threadvar_max *
82 sizeof (unsigned long int));
83 if (__hurd_threadvar_stack_offset == 0)
84 __libc_fatal ("Can't allocate single-threaded per-thread variables.");
85 for (i = 0; i < __hurd_threadvar_max; ++i)
86 ((unsigned long int *) __hurd_threadvar_stack_offset)[i] = 0;
87 }
88
89 if ((void *) d != argv[0] && (d->portarray || d->intarray))
90 /* Initialize library data structures, start signal processing, etc. */
91 _hurd_init (d->flags, argv,
92 d->portarray, d->portarraysize,
93 d->intarray, d->intarraysize);
94
95 __libc_init (argc, argv, __environ);
96}
97
a1a9d215
RM
98static void
99init (int *data, void *usercode, void **retaddrloc)
99b306dc
RM
100{
101 int argc = *data;
102 char **argv = (void *) (data + 1);
103 char **envp = &argv[argc + 1];
104 struct hurd_startup_data *d;
105
106 __environ = envp;
107 while (*envp)
108 ++envp;
109 d = (void *) ++envp;
110
111 /* The user might have defined a value for this, to get more variables.
112 Otherwise it will be zero on startup. We must make sure it is set
113 properly before before cthreads initialization, so cthreads can know
114 how much space to leave for thread variables. */
115 if (__hurd_threadvar_max < _HURD_THREADVAR_MAX)
116 __hurd_threadvar_max = _HURD_THREADVAR_MAX;
117
a1a9d215
RM
118
119 /* After possibly switching stacks, call `init1' (above) with the user
120 code as the return address, and the argument data immediately above
121 that on the stack. */
122
99b306dc
RM
123 if (_cthread_init_routine)
124 {
125 /* Initialize cthreads, which will allocate us a new stack to run on. */
126 void *newsp = (*_cthread_init_routine) ();
a2fe9c76
RM
127 struct hurd_startup_data *od;
128
99b306dc
RM
129 /* Copy the argdata from the old stack to the new one. */
130 newsp = memcpy (newsp - ((char *) &d[1] - (char *) data), data,
a2fe9c76
RM
131 (char *) d - (char *) data);
132
133 /* Set up the Hurd startup data block immediately following
134 the argument and environment pointers on the new stack. */
135 od = (newsp + ((char *) d - (char *) data));
136 if ((void *) argv[0] == d)
137 /* We were started up by the kernel with arguments on the stack.
138 There is no Hurd startup data, so zero the block. */
139 memset (od, 0, sizeof *od);
140 else
141 /* Copy the Hurd startup data block to the new stack. */
142 *od = *d;
143
a1a9d215
RM
144 /* Push the user code address on the top of the new stack. It will
145 be the return address for `init1'; we will jump there with NEWSP
146 as the stack pointer. */
147 *--(void **) newsp = usercode;
148 /* Mutate our own return address to run the code below. */
149 *retaddrloc = &&switch_stacks;
150 /* Force NEWSP into %ecx and &init1 into %eax, which are not restored
151 by function return. */
152 asm volatile ("# a %0 c %1" : : "a" (&init1), "c" (newsp));
153 return;
154 switch_stacks:
155 /* Our return address was redirected to here, so at this point our
156 stack is unwound and callers' registers restored. Only %ecx and
157 %eax are call-clobbered and thus still have the values we set just
158 above. Fetch from there the new stack pointer we will run on, and
159 jmp to the run-time address of `init1'; when it returns, it will
160 run the user code with the argument data at the top of the stack. */
161 asm volatile ("movl %ecx, %esp; jmp *%eax");
162 /* NOTREACHED */
163 }
164 else
165 {
166 /* We are not switching stacks, but we must play some games with
167 the one we've got, similar to the stack-switching code above. */
168 *retaddrloc = &&call_init1;
169 /* Force the user code address into %ecx and the run-time address of
170 `init1' into %eax, for use below. */
171 asm volatile ("# a %0 c %1" : : "a" (&init1), "c" (usercode));
172 return;
173 call_init1:
174 /* As in the stack-switching case, at this point our stack is unwound
175 and callers' registers restored, and only %ecx and %eax
176 communicate values from the lines above. In this case we have
177 stashed in %ecx the user code return address. Push it on the top
178 of the stack so it acts as init1's return address, and then jump
179 there. */
180 asm volatile ("pushl %ecx; jmp *%eax");
181 /* NOTREACHED */
99b306dc 182 }
99b306dc
RM
183}
184
185
186#ifdef PIC
187/* This function is called to initialize the shared C library.
188 It is called just before the user _start code from i386/elf/start.S,
189 with the stack set up as that code gets it. */
190
a1a9d215
RM
191/* NOTE! The linker notices the magical name `_init' and sets the DT_INIT
192 pointer in the dynamic section based solely on that. It is convention
193 for this function to be in the `.init' section, but the symbol name is
194 the only thing that really matters!! */
195/*void _init (int argc, ...) __attribute__ ((unused, section (".init")));*/
99b306dc 196
a1a9d215
RM
197void
198_init (int argc, ...)
99b306dc
RM
199{
200 /* Initialize data structures so we can do RPCs. */
201 __mach_init ();
202
203 RUN_HOOK (_hurd_preinit_hook, ());
204
a1a9d215 205 init (&argc, ((void **) &argc)[-1], &((void **) &argc)[-1]);
99b306dc
RM
206}
207#endif
208
209
210void
a1a9d215 211__libc_init_first (int argc __attribute__ ((unused)), ...)
99b306dc
RM
212{
213#ifndef PIC
214 void doinit (int *data)
215 {
a1a9d215 216 init (data, ((void **) &argc)[-1], &((void **) &data)[-1]);
99b306dc
RM
217 }
218
219 /* Initialize data structures so we can do RPCs. */
220 __mach_init ();
221
222 RUN_HOOK (_hurd_preinit_hook, ());
223
224 _hurd_startup ((void **) &argc, &doinit);
225#endif
226}