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