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