]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/dl-sysdep.c
Mon Aug 14 16:51:13 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[thirdparty/glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995 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 <hurd.h>
21 #include <link.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
27 #include <assert.h>
28 #include <sysdep.h>
29 #include <mach/mig_support.h>
30 #include "hurdstartup.h"
31 #include <mach/host_info.h>
32 #include "../stdio/_itoa.h"
33 #include <hurd/auth.h>
34 #include <hurd/term.h>
35 #include <stdarg.h>
36
37 #include "dl-machine.h"
38
39 extern void __mach_init (void);
40
41 extern int _dl_argc;
42 extern char **_dl_argv;
43 extern char **_environ;
44
45 struct hurd_startup_data *_dl_hurd_data;
46
47 unsigned int __hurd_threadvar_max = _HURD_THREADVAR_MAX;
48 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
49 unsigned long int __hurd_threadvar_stack_offset
50 = (unsigned long int) &threadvars;
51
52
53 /* XXX loser kludge for vm_map kernel bug */
54 static vm_address_t fmha;
55 static vm_size_t fmhs;
56 static void unfmh(){
57 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
58 static void fmh() {
59 error_t err;int x;mach_port_t p;
60 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
61 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
62 __mach_port_deallocate(__mach_task_self(),p);
63 if (a+fmhs>=0x80000000U){
64 max=a; break;}
65 fmha=a+=fmhs;}
66 if (err) assert(err==KERN_NO_SPACE);
67 if (!fmha)fmhs=0;else{
68 fmhs=max-fmha;
69 err = __vm_map (__mach_task_self (),
70 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
71 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
72 assert_perror(err);}
73 }
74 /* XXX loser kludge for vm_map kernel bug */
75
76
77
78 Elf32_Addr
79 _dl_sysdep_start (void **start_argptr,
80 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
81 Elf32_Addr *user_entry))
82 {
83 void go (int *argdata)
84 {
85 char **p;
86
87 /* Cache the information in various global variables. */
88 _dl_argc = *argdata;
89 _dl_argv = (void *) &argdata[1];
90 _environ = &_dl_argv[_dl_argc + 1];
91 for (p = _environ; *p; ++p);
92 _dl_hurd_data = (void *) ++p;
93
94 _dl_secure = _dl_hurd_data->flags & EXEC_SECURE;
95
96 unfmh(); /* XXX */
97 /* Call elf/rtld.c's main program. It will set everything
98 up and leave us to transfer control to USER_ENTRY. */
99 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
100 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
101 &_dl_hurd_data->user_entry);
102
103 /* Deallocate the reply port and task port rights acquired by
104 __mach_init. We are done with them now, and the user will
105 reacquire them for himself when he wants them. */
106 __mig_dealloc_reply_port (MACH_PORT_NULL);
107 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
108
109 {
110 extern void _dl_start_user (void);
111 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
112 to the RTLD_START code which will run the user's entry point. */
113 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
114 }
115 }
116
117 /* Set up so we can do RPCs. */
118 __mach_init ();
119
120 fmh(); /* XXX */
121
122 /* See hurd/hurdstartup.c; this deals with getting information
123 from the exec server and slicing up the arguments.
124 Then it will call `go', above. */
125 _hurd_startup (start_argptr, &go);
126
127 LOSE;
128 abort ();
129 }
130 \f
131 int
132 _dl_sysdep_open_zero_fill (void)
133 {
134 return (int) MACH_PORT_NULL;
135 }
136
137
138 void
139 _dl_sysdep_fatal (const char *msg, ...)
140 {
141 va_list ap;
142
143 va_start (ap, msg);
144 do
145 {
146 size_t len = strlen (msg);
147 mach_msg_type_number_t nwrote;
148 do
149 {
150 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
151 break;
152 len -= nwrote;
153 msg += nwrote;
154 } while (nwrote > 0);
155 msg = va_arg (ap, const char *);
156 } while (msg);
157 va_end (ap);
158
159 _exit (127);
160 }
161
162
163 /* Minimal open/close/mmap implementation sufficient for initial loading of
164 shared libraries. These are weak definitions so that when the
165 dynamic linker re-relocates itself to be user-visible (for -ldl),
166 it will get the user's definition (i.e. usually libc's). */
167
168 int
169 open (const char *file_name, int mode, ...)
170 {
171 enum retry_type doretry;
172 char retryname[1024]; /* XXX string_t LOSES! */
173 file_t startdir, newpt, fileport;
174 int dealloc_dir;
175 int nloops;
176
177
178 assert (mode == O_RDONLY);
179
180
181 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
182 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
183
184 while (file_name[0] == '/')
185 file_name++;
186
187 if (errno = __dir_lookup (startdir, file_name, mode, 0,
188 &doretry, retryname, &fileport))
189 return -1;
190
191 dealloc_dir = 0;
192 nloops = 0;
193 errno = 0;
194
195 while (1)
196 {
197 if (dealloc_dir)
198 __mach_port_deallocate (__mach_task_self (), startdir);
199 if (errno)
200 return -1;
201
202 switch (doretry)
203 {
204 case FS_RETRY_REAUTH:
205 {
206 mach_port_t ref = __mach_reply_port ();
207 errno = __io_reauthenticate
208 (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
209 if (! errno)
210 errno = __auth_user_authenticate
211 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
212 fileport,
213 ref, MACH_MSG_TYPE_MAKE_SEND,
214 &newpt);
215 __mach_port_destroy (__mach_task_self (), ref);
216 }
217 __mach_port_deallocate (__mach_task_self (), fileport);
218 if (errno)
219 return -1;
220 fileport = newpt;
221 /* Fall through. */
222
223 case FS_RETRY_NORMAL:
224 #ifdef SYMLOOP_MAX
225 if (nloops++ >= SYMLOOP_MAX)
226 {
227 errno = ELOOP;
228 return -1;
229 }
230 #endif
231
232 /* An empty RETRYNAME indicates we have the final port. */
233 if (retryname[0] == '\0')
234 {
235 mach_port_t memobj_rd, memobj_wr;
236
237 dealloc_dir = 1;
238
239 opened:
240 /* We have the file open. Now map it. */
241 errno = __io_map (fileport, &memobj_rd, &memobj_wr);
242 if (dealloc_dir)
243 __mach_port_deallocate (__mach_task_self (), fileport);
244 if (errno)
245 return -1;
246 if (memobj_wr != MACH_PORT_NULL)
247 __mach_port_deallocate (__mach_task_self (), memobj_wr);
248
249 return (int) memobj_rd;
250 }
251
252 startdir = fileport;
253 dealloc_dir = 1;
254 file_name = retryname;
255 break;
256
257 case FS_RETRY_MAGICAL:
258 switch (retryname[0])
259 {
260 case '/':
261 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
262 dealloc_dir = 0;
263 if (fileport != MACH_PORT_NULL)
264 __mach_port_deallocate (__mach_task_self (), fileport);
265 file_name = &retryname[1];
266 break;
267
268 case 'f':
269 if (retryname[1] == 'd' && retryname[2] == '/')
270 {
271 int fd;
272 char *end;
273 errno = 0;
274 fd = (int) strtol (retryname, &end, 10);
275 if (end == NULL || errno || /* Malformed number. */
276 /* Check for excess text after the number. A slash
277 is valid; it ends the component. Anything else
278 does not name a numeric file descriptor. */
279 (*end != '/' && *end != '\0'))
280 {
281 errno = ENOENT;
282 return -1;
283 }
284 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
285 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
286 {
287 /* If the name was a proper number, but the file
288 descriptor does not exist, we return EBADF instead
289 of ENOENT. */
290 errno = EBADF;
291 return -1;
292 }
293 fileport = _dl_hurd_data->dtable[fd];
294 if (*end == '\0')
295 {
296 /* This descriptor is the file port we want. */
297 dealloc_dir = 0;
298 goto opened;
299 }
300 else
301 {
302 /* Do a normal retry on the remaining components. */
303 startdir = fileport;
304 dealloc_dir = 1;
305 file_name = end + 1; /* Skip the slash. */
306 break;
307 }
308 }
309 else
310 goto bad_magic;
311 break;
312
313 case 'm':
314 if (retryname[1] == 'a' && retryname[2] == 'c' &&
315 retryname[3] == 'h' && retryname[4] == 't' &&
316 retryname[5] == 'y' && retryname[6] == 'p' &&
317 retryname[7] == 'e')
318 {
319 error_t err;
320 struct host_basic_info hostinfo;
321 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
322 char *p;
323 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
324 (natural_t *) &hostinfo,
325 &hostinfocnt))
326 return err;
327 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
328 return EGRATUITOUS;
329 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
330 *--p = '/';
331 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
332 if (p < retryname)
333 abort (); /* XXX write this right if this ever happens */
334 if (p > retryname)
335 strcpy (retryname, p);
336 startdir = fileport;
337 dealloc_dir = 1;
338 }
339 else
340 goto bad_magic;
341 break;
342
343 case 't':
344 if (retryname[1] == 't' && retryname[2] == 'y')
345 switch (retryname[3])
346 {
347 error_t opentty (file_t *result)
348 {
349 error_t err;
350 file_t unauth;
351 err = __termctty_open_terminal
352 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
353 mode, &unauth);
354 if (! err)
355 {
356 mach_port_t ref = __mach_reply_port ();
357 err = __io_reauthenticate
358 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
359 if (! err)
360 err = __auth_user_authenticate
361 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
362 unauth,
363 ref, MACH_MSG_TYPE_MAKE_SEND,
364 result);
365 __mach_port_deallocate (__mach_task_self (),
366 unauth);
367 __mach_port_destroy (__mach_task_self (), ref);
368 }
369 return err;
370 }
371
372 case '\0':
373 if (errno = opentty (&fileport))
374 return -1;
375 dealloc_dir = 1;
376 goto opened;
377 case '/':
378 if (errno = opentty (&startdir))
379 return -1;
380 dealloc_dir = 1;
381 strcpy (retryname, &retryname[4]);
382 break;
383 default:
384 goto bad_magic;
385 }
386 else
387 goto bad_magic;
388 break;
389
390 default:
391 bad_magic:
392 errno = EGRATUITOUS;
393 return -1;
394 }
395 break;
396
397 default:
398 errno = EGRATUITOUS;
399 return -1;
400 }
401
402 errno = __dir_lookup (startdir, file_name, mode, 0,
403 &doretry, retryname, &fileport);
404 }
405 }
406
407 int
408 close (int fd)
409 {
410 if (fd != (int) MACH_PORT_NULL)
411 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
412 return 0;
413 }
414
415 caddr_t
416 mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
417 {
418 vm_prot_t vmprot;
419 vm_address_t mapaddr;
420
421 vmprot = VM_PROT_NONE;
422 if (prot & PROT_READ)
423 vmprot |= VM_PROT_READ;
424 if (prot & PROT_WRITE)
425 vmprot |= VM_PROT_WRITE;
426 if (prot & PROT_EXEC)
427 vmprot |= VM_PROT_EXECUTE;
428
429 mapaddr = (vm_address_t) addr;
430 errno = __vm_map (__mach_task_self (),
431 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
432 !(flags & MAP_FIXED),
433 (mach_port_t) fd, (vm_offset_t) offset,
434 flags & (MAP_COPY|MAP_PRIVATE),
435 vmprot, VM_PROT_ALL,
436 (flags & MAP_INHERIT) ? VM_INHERIT_COPY : VM_INHERIT_NONE);
437 return errno ? (caddr_t) -1 : (caddr_t) mapaddr;
438 }
439
440 void
441 _exit (int status)
442 {
443 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
444 W_EXITCODE (status, 0));
445 while (__task_terminate (__mach_task_self ()))
446 __mach_task_self_ = (__mach_task_self) ();
447 }
448
449 weak_symbol (_exit)
450 weak_symbol (open)
451 weak_symbol (close)
452 weak_symbol (mmap)
453 \f
454 /* Minimal `malloc' allocator for use while loading shared libraries.
455 Only small blocks are allocated, and none are ever freed. */
456
457 void *
458 malloc (size_t n)
459 {
460 static vm_address_t ptr, end;
461 void *block;
462
463 if (end == 0)
464 {
465 /* Consume any unused space in the last page of our data segment. */
466 extern char _end;
467 ptr = (vm_address_t) &_end;
468 end = round_page (ptr);
469 }
470
471 /* Make sure the allocation pointer is ideally aligned. */
472 ptr += sizeof (double) - 1;
473 ptr &= ~(sizeof (double) - 1);
474
475 if (ptr + n >= end)
476 {
477 /* Insufficient space left; allocate another page. */
478 vm_address_t page;
479 assert (n <= __vm_page_size);
480 __vm_allocate (__mach_task_self (), &page, __vm_page_size, 1);
481 if (page != end)
482 ptr = page;
483 end = page + __vm_page_size;
484 }
485
486 block = (void *) ptr;
487 ptr += n;
488 return block;
489 }
490
491 weak_symbol (malloc)
492
493 /* These should never be called. */
494 void *realloc (void *ptr, size_t n) { ptr += n; abort (); }
495 void free (void *ptr) { ptr = ptr; abort (); }
496 weak_symbol (realloc)
497 weak_symbol (free)
498 \f
499 /* Avoid signal frobnication in setjmp/longjmp. */
500
501 int __sigjmp_save (sigjmp_buf env, int savemask)
502 { env[0].__mask_was_saved = savemask; return 0; }
503 weak_symbol (__sigjmp_save)
504
505 void
506 longjmp (jmp_buf env, int val) { __longjmp (env[0].__jmpbuf, val); }
507 weak_symbol (longjmp)
508
509
510 /* This function is called by interruptible RPC stubs. For initial
511 dynamic linking, just use the normal mach_msg. Since this defn is
512 weak, the real defn in libc.so will override it if we are linked into
513 the user program (-ldl). */
514
515 error_t
516 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
517 mach_msg_option_t option,
518 mach_msg_size_t send_size,
519 mach_msg_size_t rcv_size,
520 mach_port_t rcv_name,
521 mach_msg_timeout_t timeout,
522 mach_port_t notify)
523 {
524 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
525 timeout, notify);
526 }
527 weak_symbol (_hurd_intr_rpc_mach_msg)