update from main archive 970120
[glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
blobbf1462a8990b3bfcc6a22ae44c288970b489c531
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
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-common/_itoa.h"
33 #include <hurd/auth.h>
34 #include <hurd/term.h>
35 #include <stdarg.h>
36 #include <ctype.h>
37 #include <sys/stat.h>
39 #include "dl-machine.h"
41 extern void __mach_init (void);
43 extern int _dl_argc;
44 extern char **_dl_argv;
45 extern char **_environ;
47 int __libc_enable_secure;
49 struct hurd_startup_data *_dl_hurd_data;
51 unsigned int __hurd_threadvar_max = _HURD_THREADVAR_MAX;
52 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
53 unsigned long int __hurd_threadvar_stack_offset
54 = (unsigned long int) &threadvars;
55 unsigned long int __hurd_sigthread_stack_base;
56 unsigned long int __hurd_sigthread_stack_end;
57 unsigned long int *__hurd_sigthread_variables;
58 unsigned long int __hurd_threadvar_stack_mask;
61 /* XXX loser kludge for vm_map kernel bug */
62 static vm_address_t fmha;
63 static vm_size_t fmhs;
64 static void unfmh(void){
65 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
66 static void fmh(void) {
67 error_t err;int x;mach_port_t p;
68 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
69 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
70 __mach_port_deallocate(__mach_task_self(),p);
71 if (a+fmhs>=0x80000000U){
72 max=a; break;}
73 fmha=a+=fmhs;}
74 if (err) assert(err==KERN_NO_SPACE);
75 if (!fmha)fmhs=0;else{
76 fmhs=max-fmha;
77 err = __vm_map (__mach_task_self (),
78 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
79 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
80 assert_perror(err);}
82 /* XXX loser kludge for vm_map kernel bug */
86 Elf32_Addr
87 _dl_sysdep_start (void **start_argptr,
88 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
89 Elf32_Addr *user_entry))
91 extern void _start ();
93 void go (int *argdata)
95 extern unsigned int _dl_skip_args; /* rtld.c */
96 char **p;
98 /* Cache the information in various global variables. */
99 _dl_argc = *argdata;
100 _dl_argv = 1 + (char **) argdata;
101 _environ = &_dl_argv[_dl_argc + 1];
102 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
104 if ((void *) p == _dl_argv[0])
106 static struct hurd_startup_data nodata;
107 _dl_hurd_data = &nodata;
108 nodata.user_entry = (vm_address_t) &_start;
110 else
111 _dl_hurd_data = (void *) p;
113 __libc_enable_secure = _dl_hurd_data->flags & EXEC_SECURE;
115 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
116 _dl_hurd_data->user_entry == 0)
117 _dl_hurd_data->user_entry = (vm_address_t) &_start;
119 unfmh(); /* XXX */
121 if (_dl_hurd_data->user_entry == (vm_address_t) &_start)
122 /* We were invoked as a command, not as the program interpreter.
123 The generic ld.so code supports this: it will parse the args
124 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
125 support an additional special syntax:
126 ld.so [-LIBS...] PROGRAM [ARGS...]
127 Each LIBS word consists of "FILENAME=MEMOBJ";
128 for example "-/lib/libc.so=123" says that the contents of
129 /lib/libc.so are found in a memory object whose port name
130 in our task is 123. */
131 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
133 char *lastslash, *memobjname, *p;
134 struct link_map *l;
135 mach_port_t memobj;
136 error_t err;
138 ++_dl_skip_args;
139 --_dl_argc;
140 p = _dl_argv++[1] + 1;
142 memobjname = strchr (p, '=');
143 if (! memobjname)
144 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
145 *memobjname++ = '\0';
146 memobj = 0;
147 while (*memobjname != '\0')
148 memobj = (memobj * 10) + (*memobjname++ - '0');
150 /* Add a user reference on the memory object port, so we will
151 still have one after _dl_map_object_from_fd calls our
152 `close'. */
153 err = __mach_port_mod_refs (__mach_task_self (), memobj,
154 MACH_PORT_RIGHT_SEND, +1);
155 assert_perror (err);
157 lastslash = strrchr (p, '/');
158 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
159 memobj, strdup (p));
161 /* Squirrel away the memory object port where it
162 can be retrieved by the program later. */
163 l->l_info[DT_NULL] = (void *) memobj;
166 /* Call elf/rtld.c's main program. It will set everything
167 up and leave us to transfer control to USER_ENTRY. */
168 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
169 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
170 &_dl_hurd_data->user_entry);
172 if (_dl_skip_args && _dl_argv[-_dl_skip_args] == (char *) p)
174 /* We are ignoring the first few arguments, but we have no Hurd
175 startup data. It is magical convention that ARGV[0] == P in
176 this case. The startup code in init-first.c will get confused
177 if this is not the case, so we must rearrange things to make
178 it so. Overwrite the original ARGV[0] at P with
179 ARGV[_dl_skip_args]. */
180 assert ((char *) p < _dl_argv[0]);
181 _dl_argv[0] = strcpy ((char *) p, _dl_argv[0]);
185 extern void _dl_start_user (void);
186 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
187 to the RTLD_START code which will run the user's entry point. */
188 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
192 /* Set up so we can do RPCs. */
193 __mach_init ();
195 fmh(); /* XXX */
197 /* See hurd/hurdstartup.c; this deals with getting information
198 from the exec server and slicing up the arguments.
199 Then it will call `go', above. */
200 _hurd_startup (start_argptr, &go);
202 LOSE;
203 abort ();
206 void
207 _dl_sysdep_start_cleanup (void)
209 /* Deallocate the reply port and task port rights acquired by
210 __mach_init. We are done with them now, and the user will
211 reacquire them for himself when he wants them. */
212 __mig_dealloc_reply_port (MACH_PORT_NULL);
213 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
216 void
217 _dl_sysdep_fatal (const char *msg, ...)
219 va_list ap;
221 va_start (ap, msg);
224 size_t len = strlen (msg);
225 mach_msg_type_number_t nwrote;
228 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
229 break;
230 len -= nwrote;
231 msg += nwrote;
232 } while (nwrote > 0);
233 msg = va_arg (ap, const char *);
234 } while (msg);
235 va_end (ap);
237 _exit (127);
241 void
242 _dl_sysdep_error (const char *msg, ...)
244 va_list ap;
246 va_start (ap, msg);
249 size_t len = strlen (msg);
250 mach_msg_type_number_t nwrote;
253 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
254 break;
255 len -= nwrote;
256 msg += nwrote;
257 } while (nwrote > 0);
258 msg = va_arg (ap, const char *);
259 } while (msg);
260 va_end (ap);
264 void
265 _dl_sysdep_message (const char *msg, ...)
267 va_list ap;
269 va_start (ap, msg);
272 size_t len = strlen (msg);
273 mach_msg_type_number_t nwrote;
276 if (__io_write (_hurd_init_dtable[1], msg, len, -1, &nwrote))
277 break;
278 len -= nwrote;
279 msg += nwrote;
280 } while (nwrote > 0);
281 msg = va_arg (ap, const char *);
282 } while (msg);
283 va_end (ap);
286 /* Minimal open/close/mmap implementation sufficient for initial loading of
287 shared libraries. These are weak definitions so that when the
288 dynamic linker re-relocates itself to be user-visible (for -ldl),
289 it will get the user's definition (i.e. usually libc's). */
291 /* Open FILE_NAME and return a read-mmappable port in MEMOBJ_RD for it, or
292 return an error. If STAT is non-zero, stat the file into that stat buffer. */
293 static error_t
294 open_file (const char *file_name, int mode,
295 mach_port_t *memobj_rd, struct stat *stat)
297 enum retry_type doretry;
298 char retryname[1024]; /* XXX string_t LOSES! */
299 file_t startdir, newpt, fileport;
300 int dealloc_dir;
301 int nloops;
302 error_t err;
304 assert (mode == O_RDONLY);
306 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
307 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
309 while (file_name[0] == '/')
310 file_name++;
312 if (err = __dir_lookup (startdir, (char *)file_name, mode, 0,
313 &doretry, retryname, &fileport))
314 return err;
316 dealloc_dir = 0;
317 nloops = 0;
319 while (1)
321 if (dealloc_dir)
322 __mach_port_deallocate (__mach_task_self (), startdir);
323 if (err)
324 return err;
326 switch (doretry)
328 case FS_RETRY_REAUTH:
330 mach_port_t ref = __mach_reply_port ();
331 err = __io_reauthenticate (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
332 if (! err)
333 err = __auth_user_authenticate
334 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
335 ref, MACH_MSG_TYPE_MAKE_SEND,
336 &newpt);
337 __mach_port_destroy (__mach_task_self (), ref);
339 __mach_port_deallocate (__mach_task_self (), fileport);
340 if (err)
341 return err;
342 fileport = newpt;
343 /* Fall through. */
345 case FS_RETRY_NORMAL:
346 #ifdef SYMLOOP_MAX
347 if (nloops++ >= SYMLOOP_MAX)
348 return ELOOP;
349 #endif
351 /* An empty RETRYNAME indicates we have the final port. */
352 if (retryname[0] == '\0')
354 mach_port_t memobj_wr;
356 dealloc_dir = 1;
358 opened:
359 /* We have the file open. Now map it. */
361 if (stat)
362 err = __io_stat (fileport, stat);
363 if (! err)
364 err = __io_map (fileport, memobj_rd, &memobj_wr);
366 if (dealloc_dir)
367 __mach_port_deallocate (__mach_task_self (), fileport);
368 if (err)
369 return err;
371 if (memobj_wr != MACH_PORT_NULL)
372 __mach_port_deallocate (__mach_task_self (), memobj_wr);
374 return 0;
377 startdir = fileport;
378 dealloc_dir = 1;
379 file_name = retryname;
380 break;
382 case FS_RETRY_MAGICAL:
383 switch (retryname[0])
385 case '/':
386 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
387 dealloc_dir = 0;
388 if (fileport != MACH_PORT_NULL)
389 __mach_port_deallocate (__mach_task_self (), fileport);
390 file_name = &retryname[1];
391 break;
393 case 'f':
394 if (retryname[1] == 'd' && retryname[2] == '/' &&
395 isdigit (retryname[3]))
397 /* We can't use strtol for the decoding here
398 because it brings in hairy locale bloat. */
399 char *p;
400 int fd = 0;
401 for (p = &retryname[3]; isdigit (*p); ++p)
402 fd = (fd * 10) + (*p - '0');
403 /* Check for excess text after the number. A slash is
404 valid; it ends the component. Anything else does not
405 name a numeric file descriptor. */
406 if (*p != '/' && *p != '\0')
407 return ENOENT;
408 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
409 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
410 /* If the name was a proper number, but the file
411 descriptor does not exist, we return EBADF instead
412 of ENOENT. */
413 return EBADF;
414 fileport = _dl_hurd_data->dtable[fd];
415 if (*p == '\0')
417 /* This descriptor is the file port we want. */
418 dealloc_dir = 0;
419 goto opened;
421 else
423 /* Do a normal retry on the remaining components. */
424 startdir = fileport;
425 dealloc_dir = 1;
426 file_name = p + 1; /* Skip the slash. */
427 break;
430 else
431 goto bad_magic;
432 break;
434 case 'm':
435 if (retryname[1] == 'a' && retryname[2] == 'c' &&
436 retryname[3] == 'h' && retryname[4] == 't' &&
437 retryname[5] == 'y' && retryname[6] == 'p' &&
438 retryname[7] == 'e')
440 error_t err;
441 struct host_basic_info hostinfo;
442 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
443 char *p;
444 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
445 (natural_t *) &hostinfo,
446 &hostinfocnt))
447 return err;
448 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
449 return EGRATUITOUS;
450 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
451 *--p = '/';
452 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
453 if (p < retryname)
454 abort (); /* XXX write this right if this ever happens */
455 if (p > retryname)
456 strcpy (retryname, p);
457 startdir = fileport;
458 dealloc_dir = 1;
460 else
461 goto bad_magic;
462 break;
464 case 't':
465 if (retryname[1] == 't' && retryname[2] == 'y')
466 switch (retryname[3])
468 error_t opentty (file_t *result)
470 error_t err;
471 file_t unauth;
472 err = __termctty_open_terminal
473 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
474 mode, &unauth);
475 if (! err)
477 mach_port_t ref = __mach_reply_port ();
478 err = __io_reauthenticate
479 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
480 if (! err)
481 err = __auth_user_authenticate
482 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
483 ref, MACH_MSG_TYPE_MAKE_SEND,
484 result);
485 __mach_port_deallocate (__mach_task_self (),
486 unauth);
487 __mach_port_destroy (__mach_task_self (), ref);
489 return err;
492 case '\0':
493 if (err = opentty (&fileport))
494 return err;
495 dealloc_dir = 1;
496 goto opened;
497 case '/':
498 if (err = opentty (&startdir))
499 return err;
500 dealloc_dir = 1;
501 strcpy (retryname, &retryname[4]);
502 break;
503 default:
504 goto bad_magic;
506 else
507 goto bad_magic;
508 break;
510 default:
511 bad_magic:
512 return EGRATUITOUS;
514 break;
516 default:
517 return EGRATUITOUS;
520 err = __dir_lookup (startdir, (char *)file_name, mode, 0,
521 &doretry, retryname, &fileport);
525 int weak_function
526 __open (const char *file_name, int mode, ...)
528 mach_port_t memobj_rd;
529 error_t err = open_file (file_name, mode, &memobj_rd, 0);
530 if (err)
531 return __hurd_fail (err);
532 else
533 return (int)memobj_rd;
536 int weak_function
537 __close (int fd)
539 if (fd != (int) MACH_PORT_NULL)
540 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
541 return 0;
544 caddr_t weak_function
545 __mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
547 error_t err;
548 vm_prot_t vmprot;
549 vm_address_t mapaddr;
551 vmprot = VM_PROT_NONE;
552 if (prot & PROT_READ)
553 vmprot |= VM_PROT_READ;
554 if (prot & PROT_WRITE)
555 vmprot |= VM_PROT_WRITE;
556 if (prot & PROT_EXEC)
557 vmprot |= VM_PROT_EXECUTE;
559 mapaddr = (vm_address_t) addr;
560 err = __vm_map (__mach_task_self (),
561 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
562 !(flags & MAP_FIXED),
563 (flags & MAP_ANON) ? MACH_PORT_NULL : (mach_port_t) fd,
564 (vm_offset_t) offset,
565 flags & (MAP_COPY|MAP_PRIVATE),
566 vmprot, VM_PROT_ALL,
567 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
568 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
570 /* XXX this is not atomic as it is in unix! */
571 /* The region is already allocated; deallocate it first. */
572 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
573 if (! err)
574 err = __vm_map (__mach_task_self (),
575 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
576 !(flags & MAP_FIXED),
577 (mach_port_t) fd, (vm_offset_t) offset,
578 flags & (MAP_COPY|MAP_PRIVATE),
579 vmprot, VM_PROT_ALL,
580 (flags & MAP_SHARED)
581 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
584 return err ? (caddr_t) __hurd_fail (err) : (caddr_t) mapaddr;
587 void weak_function
588 _exit (int status)
590 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
591 W_EXITCODE (status, 0), 0);
592 while (__task_terminate (__mach_task_self ()))
593 __mach_task_self_ = (__mach_task_self) ();
596 /* Read the whole contents of FILE into new mmap'd space with given
597 protections. The size of the file is returned in SIZE. */
598 void *
599 _dl_sysdep_read_whole_file (const char *file, size_t *size, int prot)
601 struct stat stat;
602 mach_port_t memobj_rd;
603 void *contents;
604 error_t err = open_file (file, O_RDONLY, &memobj_rd, &stat);
606 if (! err)
608 /* Map a copy of the file contents. */
609 contents = __mmap (0, stat.st_size, prot, MAP_COPY, memobj_rd, 0);
610 if (contents == (void *)-1)
611 contents = 0;
612 else
613 *size = stat.st_size;
615 __mach_port_deallocate (__mach_task_self (), memobj_rd);
617 else
619 __hurd_fail (err);
620 contents = 0;
623 return contents;
626 /* This function is called by interruptible RPC stubs. For initial
627 dynamic linking, just use the normal mach_msg. Since this defn is
628 weak, the real defn in libc.so will override it if we are linked into
629 the user program (-ldl). */
631 error_t weak_function
632 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
633 mach_msg_option_t option,
634 mach_msg_size_t send_size,
635 mach_msg_size_t rcv_size,
636 mach_port_t rcv_name,
637 mach_msg_timeout_t timeout,
638 mach_port_t notify)
640 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
641 timeout, notify);