]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdstartup.c
Update.
[thirdparty/glibc.git] / hurd / hurdstartup.c
CommitLineData
11872325 1/* Initial program startup for running under the GNU Hurd.
f69139ec 2 Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc.
c84142e8
UD
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
11872325
RM
19
20#include <errno.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <hurd.h>
f44f9c33 25#include <hurd/exec_startup.h>
11872325
RM
26#include <sysdep.h>
27#include <hurd/threadvar.h>
28#include <unistd.h>
99b306dc 29#include <elf.h>
6747b8fc 30#include <set-hooks.h>
99b306dc 31#include "hurdstartup.h"
75cd5204 32#include <argz.h>
11872325
RM
33
34mach_port_t *_hurd_init_dtable;
35mach_msg_type_number_t _hurd_init_dtablesize;
36
11872325 37extern void __mach_init (void);
11872325 38
11872325
RM
39/* Entry point. This is the first thing in the text segment.
40
41 The exec server started the initial thread in our task with this spot the
42 PC, and a stack that is presumably big enough. We do basic Mach
43 initialization so mig-generated stubs work, and then do an exec_startup
44 RPC on our bootstrap port, to which the exec server responds with the
45 information passed in the exec call, as well as our original bootstrap
46 port, and the base address and size of the preallocated stack.
47
48 If using cthreads, we are given a new stack by cthreads initialization and
49 deallocate the stack set up by the exec server. On the new stack we call
50 `start1' (above) to do the rest of the startup work. Since the stack may
51 disappear out from under us in a machine-dependent way, we use a pile of
52 static variables to communicate the information from exec_startup to start1.
53 This is unfortunate but preferable to machine-dependent frobnication to copy
54 the state from the old stack to the new one. */
55
99b306dc 56
11872325 57void
99b306dc 58_hurd_startup (void **argptr, void (*main) (int *data))
11872325
RM
59{
60 error_t err;
61 mach_port_t in_bootstrap;
99b306dc
RM
62 char *args, *env;
63 mach_msg_type_number_t argslen, envlen;
64 struct hurd_startup_data data;
65 char **argv, **envp;
66 int argc, envc;
67 int *argcptr;
68dbb3a6
UD
68 vm_address_t addr;
69
70 /* Attempt to map page zero redzoned before we receive any RPC
71 data that might get allocated there. We can ignore errors. */
72 addr = 0;
73 __vm_map (__mach_task_self (),
74 &addr, __vm_page_size, 0, 0, MACH_PORT_NULL, 0, 1,
75 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
11872325 76
11872325
RM
77 if (err = __task_get_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
78 &in_bootstrap))
79 LOSE;
80
81 if (in_bootstrap != MACH_PORT_NULL)
82 {
83 /* Call the exec server on our bootstrap port and
84 get all our standard information from it. */
85
99b306dc
RM
86 argslen = envlen = 0;
87 data.dtablesize = data.portarraysize = data.intarraysize = 0;
11872325 88
f44f9c33
RM
89 err = __exec_startup_get_info (in_bootstrap,
90 &data.user_entry,
91 &data.phdr, &data.phdrsz,
92 &data.stack_base, &data.stack_size,
93 &data.flags,
94 &args, &argslen,
95 &env, &envlen,
96 &data.dtable, &data.dtablesize,
97 &data.portarray, &data.portarraysize,
98 &data.intarray, &data.intarraysize);
11872325
RM
99 __mach_port_deallocate (__mach_task_self (), in_bootstrap);
100 }
101
60092701 102 if (err || in_bootstrap == MACH_PORT_NULL || (data.flags & EXEC_STACK_ARGS))
11872325
RM
103 {
104 /* Either we have no bootstrap port, or the RPC to the exec server
60092701
RM
105 failed, or whoever started us up passed the flag saying args are
106 on the stack. Try to snarf the args in the canonical Mach way.
11872325 107 Hopefully either they will be on the stack as expected, or the
60092701 108 stack will be zeros so we don't crash. */
11872325 109
a2fe9c76
RM
110 argcptr = (int *) argptr;
111 argc = argcptr[0];
112 argv = (char **) &argcptr[1];
113 envp = &argv[argc + 1];
114 envc = 0;
115 while (envp[envc])
116 ++envc;
11872325
RM
117 }
118 else
11872325 119 {
60092701
RM
120 /* Turn the block of null-separated strings we were passed for the
121 arguments and environment into vectors of pointers to strings. */
122
99b306dc 123 /* Count up the arguments so we can allocate ARGV. */
75cd5204 124 argc = __argz_count (args, argslen);
99b306dc 125 /* Count up the environment variables so we can allocate ENVP. */
75cd5204 126 envc = __argz_count (env, envlen);
99b306dc
RM
127
128 /* There were some arguments. Allocate space for the vectors of
129 pointers and fill them in. We allocate the space for the
130 environment pointers immediately after the argv pointers because
131 the ELF ABI will expect it. */
132 argcptr = __alloca (sizeof (int) +
133 (argc + 1 + envc + 1) * sizeof (char *) +
134 sizeof (struct hurd_startup_data));
135 *argcptr = argc;
136 argv = (void *) (argcptr + 1);
395565f0 137 __argz_extract (args, argslen, argv);
99b306dc
RM
138
139 /* There was some environment. */
140 envp = &argv[argc + 1];
395565f0 141 __argz_extract (env, envlen, envp);
11872325
RM
142 }
143
60092701
RM
144 if (err || in_bootstrap == MACH_PORT_NULL)
145 {
146 /* Either we have no bootstrap port, or the RPC to the exec server
147 failed. Set all our other variables to have empty information. */
148
149 data.flags = 0;
150 args = env = NULL;
151 argslen = envlen = 0;
152 data.dtable = NULL;
153 data.dtablesize = 0;
154 data.portarray = NULL;
155 data.portarraysize = 0;
156 data.intarray = NULL;
157 data.intarraysize = 0;
158 }
159 else if ((void *) &envp[envc + 1] == argv[0])
160 {
161 /* The arguments arrived on the stack from the kernel, but our
162 protocol requires some space after them for a `struct
163 hurd_startup_data'. Move them. */
164 struct
165 {
166 int count;
167 char *argv[argc + 1];
168 char *envp[envc + 1];
169 struct hurd_startup_data data;
170 } *args = alloca (sizeof *args);
171 if ((void *) &args[1] == (void *) argcptr)
172 args = alloca (-((char *) &args->data - (char *) args));
173 memmove (args, argcptr, (char *) &args->data - (char *) args);
174 argcptr = (void *) args;
175 argv = args->argv;
176 envp = args->envp;
177 }
178
99b306dc
RM
179 {
180 struct hurd_startup_data *d = (void *) &envp[envc + 1];
181
a2fe9c76
RM
182 if ((void *) d != argv[0])
183 {
184 *d = data;
185 _hurd_init_dtable = d->dtable;
186 _hurd_init_dtablesize = d->dtablesize;
a2fe9c76 187 }
99b306dc
RM
188
189 (*main) (argcptr);
190 }
11872325
RM
191
192 /* Should never get here. */
193 LOSE;
99b306dc 194 abort ();
11872325 195}