Update.
[glibc.git] / sysdeps / mach / hurd / dl-sysdep.c
blob8edde693379ed6ae0580ef52f576519592790e45
1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995, 1996, 1997, 1998 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 <elf/ldsodefs.h>
27 #include <sys/wait.h>
28 #include <assert.h>
29 #include <sysdep.h>
30 #include <mach/mig_support.h>
31 #include "hurdstartup.h"
32 #include <mach/host_info.h>
33 #include <stdio-common/_itoa.h>
34 #include <hurd/auth.h>
35 #include <hurd/term.h>
36 #include <stdarg.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
40 #include <entry.h>
41 #include <dl-machine.h>
42 #include <dl-procinfo.h>
44 extern void __mach_init (void);
46 extern int _dl_argc;
47 extern char **_dl_argv;
48 extern char **_environ;
49 extern void ENTRY_POINT (void);
51 int __libc_enable_secure;
52 int __libc_multiple_libcs; /* Defining this here avoids the inclusion
53 of init-first. */
54 /* This variable containts the lowest stack address ever used. */
55 void *__libc_stack_end;
56 unsigned long int _dl_hwcap_mask = HWCAP_IMPORTANT;
59 struct hurd_startup_data *_dl_hurd_data;
61 unsigned int __hurd_threadvar_max = _HURD_THREADVAR_MAX;
62 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
63 unsigned long int __hurd_threadvar_stack_offset
64 = (unsigned long int) &threadvars;
65 unsigned long int __hurd_threadvar_stack_mask;
67 /* XXX loser kludge for vm_map kernel bug */
68 static vm_address_t fmha;
69 static vm_size_t fmhs;
70 static void unfmh(void){
71 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
72 static void fmh(void) {
73 error_t err;int x;mach_port_t p;
74 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
75 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
76 __mach_port_deallocate(__mach_task_self(),p);
77 if (a+fmhs>=0x80000000U){
78 max=a; break;}
79 fmha=a+=fmhs;}
80 if (err) assert(err==KERN_NO_SPACE);
81 if (!fmha)fmhs=0;else{
82 fmhs=max-fmha;
83 err = __vm_map (__mach_task_self (),
84 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
85 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
86 assert_perror(err);}
88 /* XXX loser kludge for vm_map kernel bug */
92 Elf32_Addr
93 _dl_sysdep_start (void **start_argptr,
94 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
95 Elf32_Addr *user_entry))
97 void go (int *argdata)
99 extern unsigned int _dl_skip_args; /* rtld.c */
100 char **p;
102 /* Cache the information in various global variables. */
103 _dl_argc = *argdata;
104 _dl_argv = 1 + (char **) argdata;
105 _environ = &_dl_argv[_dl_argc + 1];
106 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
108 if ((void *) p == _dl_argv[0])
110 static struct hurd_startup_data nodata;
111 _dl_hurd_data = &nodata;
112 nodata.user_entry = (vm_address_t) &ENTRY_POINT;
114 else
115 _dl_hurd_data = (void *) p;
117 __libc_enable_secure = _dl_hurd_data->flags & EXEC_SECURE;
119 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
120 _dl_hurd_data->user_entry == 0)
121 _dl_hurd_data->user_entry = (vm_address_t) &ENTRY_POINT;
123 unfmh(); /* XXX */
125 #if 0 /* XXX make this work for real someday... */
126 if (_dl_hurd_data->user_entry == (vm_address_t) &ENTRY_POINT)
127 /* We were invoked as a command, not as the program interpreter.
128 The generic ld.so code supports this: it will parse the args
129 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
130 support an additional special syntax:
131 ld.so [-LIBS...] PROGRAM [ARGS...]
132 Each LIBS word consists of "FILENAME=MEMOBJ";
133 for example "-/lib/libc.so=123" says that the contents of
134 /lib/libc.so are found in a memory object whose port name
135 in our task is 123. */
136 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
138 char *lastslash, *memobjname, *p;
139 struct link_map *l;
140 mach_port_t memobj;
141 error_t err;
143 ++_dl_skip_args;
144 --_dl_argc;
145 p = _dl_argv++[1] + 1;
147 memobjname = strchr (p, '=');
148 if (! memobjname)
149 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
150 *memobjname++ = '\0';
151 memobj = 0;
152 while (*memobjname != '\0')
153 memobj = (memobj * 10) + (*memobjname++ - '0');
155 /* Add a user reference on the memory object port, so we will
156 still have one after _dl_map_object_from_fd calls our
157 `close'. */
158 err = __mach_port_mod_refs (__mach_task_self (), memobj,
159 MACH_PORT_RIGHT_SEND, +1);
160 assert_perror (err);
162 lastslash = strrchr (p, '/');
163 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
164 memobj, strdup (p));
166 /* Squirrel away the memory object port where it
167 can be retrieved by the program later. */
168 l->l_info[DT_NULL] = (void *) memobj;
170 #endif
172 /* Call elf/rtld.c's main program. It will set everything
173 up and leave us to transfer control to USER_ENTRY. */
174 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
175 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
176 &_dl_hurd_data->user_entry);
178 if (_dl_skip_args && _dl_argv[-_dl_skip_args] == (char *) p)
180 /* We are ignoring the first few arguments, but we have no Hurd
181 startup data. It is magical convention that ARGV[0] == P in
182 this case. The startup code in init-first.c will get confused
183 if this is not the case, so we must rearrange things to make
184 it so. Overwrite the original ARGV[0] at P with
185 ARGV[_dl_skip_args]. */
186 assert ((char *) p < _dl_argv[0]);
187 _dl_argv[0] = strcpy ((char *) p, _dl_argv[0]);
191 extern void _dl_start_user (void);
192 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
193 to the RTLD_START code which will run the user's entry point. */
194 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
198 /* Set up so we can do RPCs. */
199 __mach_init ();
201 /* Initialize frequently used global variable. */
202 _dl_pagesize = __getpagesize ();
204 fmh(); /* XXX */
206 /* See hurd/hurdstartup.c; this deals with getting information
207 from the exec server and slicing up the arguments.
208 Then it will call `go', above. */
209 _hurd_startup (start_argptr, &go);
211 LOSE;
212 abort ();
215 void
216 _dl_sysdep_start_cleanup (void)
218 /* Deallocate the reply port and task port rights acquired by
219 __mach_init. We are done with them now, and the user will
220 reacquire them for himself when he wants them. */
221 __mig_dealloc_reply_port (MACH_PORT_NULL);
222 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
225 /* Minimal open/close/mmap implementation sufficient for initial loading of
226 shared libraries. These are weak definitions so that when the
227 dynamic linker re-relocates itself to be user-visible (for -ldl),
228 it will get the user's definition (i.e. usually libc's). */
230 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or
231 return an error. If STAT is non-zero, stat the file into that stat buffer. */
232 static error_t
233 open_file (const char *file_name, int mode,
234 mach_port_t *port, struct stat *stat)
236 enum retry_type doretry;
237 char retryname[1024]; /* XXX string_t LOSES! */
238 file_t startdir, newpt, fileport;
239 int dealloc_dir;
240 int nloops;
241 error_t err;
243 assert (mode == O_RDONLY);
245 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
246 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
248 while (file_name[0] == '/')
249 file_name++;
251 if (err = __dir_lookup (startdir, (char *)file_name, mode, 0,
252 &doretry, retryname, &fileport))
253 return err;
255 dealloc_dir = 0;
256 nloops = 0;
258 while (1)
260 if (dealloc_dir)
261 __mach_port_deallocate (__mach_task_self (), startdir);
262 if (err)
263 return err;
265 switch (doretry)
267 case FS_RETRY_REAUTH:
269 mach_port_t ref = __mach_reply_port ();
270 err = __io_reauthenticate (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
271 if (! err)
272 err = __auth_user_authenticate
273 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
274 ref, MACH_MSG_TYPE_MAKE_SEND,
275 &newpt);
276 __mach_port_destroy (__mach_task_self (), ref);
278 __mach_port_deallocate (__mach_task_self (), fileport);
279 if (err)
280 return err;
281 fileport = newpt;
282 /* Fall through. */
284 case FS_RETRY_NORMAL:
285 #ifdef SYMLOOP_MAX
286 if (nloops++ >= SYMLOOP_MAX)
287 return ELOOP;
288 #endif
290 /* An empty RETRYNAME indicates we have the final port. */
291 if (retryname[0] == '\0')
293 dealloc_dir = 1;
294 opened:
295 /* We have the file open. Now map it. */
296 if (stat)
297 err = __io_stat (fileport, stat);
299 if (err)
301 if (dealloc_dir)
302 __mach_port_deallocate (__mach_task_self (), fileport);
304 else
306 if (!dealloc_dir)
307 __mach_port_mod_refs (__mach_task_self (), fileport,
308 MACH_PORT_RIGHT_SEND, 1);
309 *port = fileport;
312 return err;
315 startdir = fileport;
316 dealloc_dir = 1;
317 file_name = retryname;
318 break;
320 case FS_RETRY_MAGICAL:
321 switch (retryname[0])
323 case '/':
324 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
325 dealloc_dir = 0;
326 if (fileport != MACH_PORT_NULL)
327 __mach_port_deallocate (__mach_task_self (), fileport);
328 file_name = &retryname[1];
329 break;
331 case 'f':
332 if (retryname[1] == 'd' && retryname[2] == '/' &&
333 isdigit (retryname[3]))
335 /* We can't use strtol for the decoding here
336 because it brings in hairy locale bloat. */
337 char *p;
338 int fd = 0;
339 for (p = &retryname[3]; isdigit (*p); ++p)
340 fd = (fd * 10) + (*p - '0');
341 /* Check for excess text after the number. A slash is
342 valid; it ends the component. Anything else does not
343 name a numeric file descriptor. */
344 if (*p != '/' && *p != '\0')
345 return ENOENT;
346 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
347 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
348 /* If the name was a proper number, but the file
349 descriptor does not exist, we return EBADF instead
350 of ENOENT. */
351 return EBADF;
352 fileport = _dl_hurd_data->dtable[fd];
353 if (*p == '\0')
355 /* This descriptor is the file port we want. */
356 dealloc_dir = 0;
357 goto opened;
359 else
361 /* Do a normal retry on the remaining components. */
362 startdir = fileport;
363 dealloc_dir = 1;
364 file_name = p + 1; /* Skip the slash. */
365 break;
368 else
369 goto bad_magic;
370 break;
372 case 'm':
373 if (retryname[1] == 'a' && retryname[2] == 'c' &&
374 retryname[3] == 'h' && retryname[4] == 't' &&
375 retryname[5] == 'y' && retryname[6] == 'p' &&
376 retryname[7] == 'e')
378 error_t err;
379 struct host_basic_info hostinfo;
380 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
381 char *p;
382 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
383 (natural_t *) &hostinfo,
384 &hostinfocnt))
385 return err;
386 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
387 return EGRATUITOUS;
388 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
389 *--p = '/';
390 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
391 if (p < retryname)
392 abort (); /* XXX write this right if this ever happens */
393 if (p > retryname)
394 strcpy (retryname, p);
395 startdir = fileport;
396 dealloc_dir = 1;
398 else
399 goto bad_magic;
400 break;
402 case 't':
403 if (retryname[1] == 't' && retryname[2] == 'y')
404 switch (retryname[3])
406 error_t opentty (file_t *result)
408 error_t err;
409 file_t unauth;
410 err = __termctty_open_terminal
411 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
412 mode, &unauth);
413 if (! err)
415 mach_port_t ref = __mach_reply_port ();
416 err = __io_reauthenticate
417 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
418 if (! err)
419 err = __auth_user_authenticate
420 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
421 ref, MACH_MSG_TYPE_MAKE_SEND,
422 result);
423 __mach_port_deallocate (__mach_task_self (),
424 unauth);
425 __mach_port_destroy (__mach_task_self (), ref);
427 return err;
430 case '\0':
431 if (err = opentty (&fileport))
432 return err;
433 dealloc_dir = 1;
434 goto opened;
435 case '/':
436 if (err = opentty (&startdir))
437 return err;
438 dealloc_dir = 1;
439 strcpy (retryname, &retryname[4]);
440 break;
441 default:
442 goto bad_magic;
444 else
445 goto bad_magic;
446 break;
448 default:
449 bad_magic:
450 return EGRATUITOUS;
452 break;
454 default:
455 return EGRATUITOUS;
458 err = __dir_lookup (startdir, (char *)file_name, mode, 0,
459 &doretry, retryname, &fileport);
463 int weak_function
464 __open (const char *file_name, int mode, ...)
466 mach_port_t port;
467 error_t err = open_file (file_name, mode, &port, 0);
468 if (err)
469 return __hurd_fail (err);
470 else
471 return (int)port;
474 int weak_function
475 __close (int fd)
477 if (fd != (int) MACH_PORT_NULL)
478 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
479 return 0;
482 __ssize_t weak_function
483 __libc_read (int fd, void *buf, size_t nbytes)
485 error_t err;
486 char *data;
487 mach_msg_type_number_t nread;
489 data = buf;
490 err = __io_read ((mach_port_t) fd, &data, &nread, -1, nbytes);
491 if (err)
492 return __hurd_fail (err);
494 if (data != buf)
496 memcpy (buf, data, nread);
497 __vm_deallocate (__mach_task_self (), (vm_address_t) data, nread);
500 return nread;
503 __ptr_t weak_function
504 __mmap (__ptr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
506 error_t err;
507 vm_prot_t vmprot;
508 vm_address_t mapaddr;
509 mach_port_t memobj_rd, memobj_wr;
511 vmprot = VM_PROT_NONE;
512 if (prot & PROT_READ)
513 vmprot |= VM_PROT_READ;
514 if (prot & PROT_WRITE)
515 vmprot |= VM_PROT_WRITE;
516 if (prot & PROT_EXEC)
517 vmprot |= VM_PROT_EXECUTE;
519 if (flags & MAP_ANON)
520 memobj_rd = MACH_PORT_NULL;
521 else
523 assert (!(flags & MAP_SHARED));
524 err = __io_map ((mach_port_t) fd, &memobj_rd, &memobj_wr);
525 if (err)
526 return (__ptr_t) __hurd_fail (err);
527 __mach_port_deallocate (__mach_task_self (), memobj_wr);
530 mapaddr = (vm_address_t) addr;
531 err = __vm_map (__mach_task_self (),
532 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
533 !(flags & MAP_FIXED),
534 memobj_rd,
535 (vm_offset_t) offset,
536 flags & (MAP_COPY|MAP_PRIVATE),
537 vmprot, VM_PROT_ALL,
538 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
539 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
541 /* XXX this is not atomic as it is in unix! */
542 /* The region is already allocated; deallocate it first. */
543 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
544 if (! err)
545 err = __vm_map (__mach_task_self (),
546 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
547 !(flags & MAP_FIXED),
548 memobj_rd, (vm_offset_t) offset,
549 flags & (MAP_COPY|MAP_PRIVATE),
550 vmprot, VM_PROT_ALL,
551 (flags & MAP_SHARED)
552 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
555 if ((flags & MAP_ANON) == 0)
556 __mach_port_deallocate (__mach_task_self (), memobj_rd);
558 return err ? (__ptr_t) __hurd_fail (err) : (__ptr_t) mapaddr;
561 void weak_function
562 _exit (int status)
564 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
565 W_EXITCODE (status, 0), 0);
566 while (__task_terminate (__mach_task_self ()))
567 __mach_task_self_ = (__mach_task_self) ();
570 /* Read the whole contents of FILE into new mmap'd space with given
571 protections. The size of the file is returned in SIZE. */
572 void *
573 weak_function
574 _dl_sysdep_read_whole_file (const char *file, size_t *size, int prot)
576 struct stat stat;
577 mach_port_t memobj_rd;
578 void *contents;
579 error_t err = open_file (file, O_RDONLY, &memobj_rd, &stat);
581 if (! err)
583 /* Map a copy of the file contents. */
584 contents = __mmap (0, stat.st_size, prot, MAP_COPY, memobj_rd, 0);
585 if (contents == (void *)-1)
586 contents = 0;
587 else
588 *size = stat.st_size;
590 __mach_port_deallocate (__mach_task_self (), memobj_rd);
592 else
594 __hurd_fail (err);
595 contents = 0;
598 return contents;
601 /* This function is called by interruptible RPC stubs. For initial
602 dynamic linking, just use the normal mach_msg. Since this defn is
603 weak, the real defn in libc.so will override it if we are linked into
604 the user program (-ldl). */
606 error_t weak_function
607 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
608 mach_msg_option_t option,
609 mach_msg_size_t send_size,
610 mach_msg_size_t rcv_size,
611 mach_port_t rcv_name,
612 mach_msg_timeout_t timeout,
613 mach_port_t notify)
615 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
616 timeout, notify);
620 void
621 internal_function
622 _dl_show_auxv (void)
624 /* There is nothing to print. Hurd has no auxiliary vector. */
628 /* Return an array of useful/necessary hardware capability names. */
629 const struct r_strlenpair *
630 internal_function
631 _dl_important_hwcaps (const char *platform, size_t platform_len, size_t *sz,
632 size_t *max_capstrlen)
634 struct r_strlenpair *result;
636 /* Return an empty array. Hurd has no hardware capabilities. */
637 result = (struct r_strlenpair *) malloc (sizeof (*result));
638 if (result == NULL)
639 _dl_signal_error (ENOMEM, NULL, "cannot create capability list");
641 result[0].str = (char *) result; /* Does not really matter. */
642 result[0].len = 0;
644 *sz = 1;
645 return result;
649 void weak_function
650 _dl_sysdep_output (int fd, const char *msg, ...)
652 va_list ap;
654 assert(fd < _hurd_init_dtablesize);
656 va_start (ap, msg);
659 size_t len = strlen (msg);
660 mach_msg_type_number_t nwrote;
663 if (__io_write (_hurd_init_dtable[fd], msg, len, -1, &nwrote))
664 break;
665 len -= nwrote;
666 msg += nwrote;
667 } while (nwrote > 0);
668 msg = va_arg (ap, const char *);
669 } while (msg);
670 va_end (ap);