1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995, 1996 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
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>
38 #include "dl-machine.h"
40 extern void __mach_init (void);
43 extern char **_dl_argv;
44 extern char **_environ;
46 int __libc_enable_secure;
48 struct hurd_startup_data *_dl_hurd_data;
50 unsigned int __hurd_threadvar_max = _HURD_THREADVAR_MAX;
51 static unsigned long int threadvars[_HURD_THREADVAR_MAX];
52 unsigned long int __hurd_threadvar_stack_offset
53 = (unsigned long int) &threadvars;
54 unsigned long int __hurd_sigthread_stack_base;
55 unsigned long int __hurd_sigthread_stack_end;
56 unsigned long int *__hurd_sigthread_variables;
57 unsigned long int __hurd_threadvar_stack_mask;
60 /* XXX loser kludge for vm_map kernel bug */
61 static vm_address_t fmha;
62 static vm_size_t fmhs;
63 static void unfmh(void){
64 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
65 static void fmh(void) {
66 error_t err;int x;mach_port_t p;
67 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
68 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
69 __mach_port_deallocate(__mach_task_self(),p);
70 if (a+fmhs>=0x80000000U){
73 if (err) assert(err==KERN_NO_SPACE);
74 if (!fmha)fmhs=0;else{
76 err = __vm_map (__mach_task_self (),
77 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
78 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
81 /* XXX loser kludge for vm_map kernel bug */
86 _dl_sysdep_start (void **start_argptr,
87 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
88 Elf32_Addr *user_entry))
90 extern void _start ();
92 void go (int *argdata)
94 extern unsigned int _dl_skip_args; /* rtld.c */
97 /* Cache the information in various global variables. */
99 _dl_argv = 1 + (char **) argdata;
100 _environ = &_dl_argv[_dl_argc + 1];
101 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
103 if ((void *) p == _dl_argv[0])
105 static struct hurd_startup_data nodata;
106 _dl_hurd_data = &nodata;
107 nodata.user_entry = (vm_address_t) &_start;
110 _dl_hurd_data = (void *) p;
112 __libc_enable_secure = _dl_hurd_data->flags & EXEC_SECURE;
114 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
115 _dl_hurd_data->user_entry == 0)
116 _dl_hurd_data->user_entry = (vm_address_t) &_start;
120 if (_dl_hurd_data->user_entry == (vm_address_t) &_start)
121 /* We were invoked as a command, not as the program interpreter.
122 The generic ld.so code supports this: it will parse the args
123 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
124 support an additional special syntax:
125 ld.so [-LIBS...] PROGRAM [ARGS...]
126 Each LIBS word consists of "FILENAME=MEMOBJ";
127 for example "-/lib/libc.so=123" says that the contents of
128 /lib/libc.so are found in a memory object whose port name
129 in our task is 123. */
130 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
132 char *lastslash, *memobjname, *p;
139 p = _dl_argv++[1] + 1;
141 memobjname = strchr (p, '=');
143 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
144 *memobjname++ = '\0';
146 while (*memobjname != '\0')
147 memobj = (memobj * 10) + (*memobjname++ - '0');
149 /* Add a user reference on the memory object port, so we will
150 still have one after _dl_map_object_from_fd calls our
152 err = __mach_port_mod_refs (__mach_task_self (), memobj,
153 MACH_PORT_RIGHT_SEND, +1);
156 lastslash = strrchr (p, '/');
157 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
160 /* Squirrel away the memory object port where it
161 can be retrieved by the program later. */
162 l->l_info[DT_NULL] = (void *) memobj;
165 /* Call elf/rtld.c's main program. It will set everything
166 up and leave us to transfer control to USER_ENTRY. */
167 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
168 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
169 &_dl_hurd_data->user_entry);
171 if (_dl_skip_args && _dl_argv[-_dl_skip_args] == (char *) p)
173 /* We are ignoring the first few arguments, but we have no Hurd
174 startup data. It is magical convention that ARGV[0] == P in
175 this case. The startup code in init-first.c will get confused
176 if this is not the case, so we must rearrange things to make
177 it so. Overwrite the original ARGV[0] at P with
178 ARGV[_dl_skip_args]. */
179 assert ((char *) p < _dl_argv[0]);
180 _dl_argv[0] = strcpy ((char *) p, _dl_argv[0]);
184 extern void _dl_start_user (void);
185 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
186 to the RTLD_START code which will run the user's entry point. */
187 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
191 /* Set up so we can do RPCs. */
196 /* See hurd/hurdstartup.c; this deals with getting information
197 from the exec server and slicing up the arguments.
198 Then it will call `go', above. */
199 _hurd_startup (start_argptr, &go);
206 _dl_sysdep_start_cleanup (void)
208 /* Deallocate the reply port and task port rights acquired by
209 __mach_init. We are done with them now, and the user will
210 reacquire them for himself when he wants them. */
211 __mig_dealloc_reply_port (MACH_PORT_NULL);
212 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
216 _dl_sysdep_fatal (const char *msg, ...)
223 size_t len = strlen (msg);
224 mach_msg_type_number_t nwrote;
227 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
231 } while (nwrote > 0);
232 msg = va_arg (ap, const char *);
241 _dl_sysdep_message (const char *msg, ...)
248 size_t len = strlen (msg);
249 mach_msg_type_number_t nwrote;
252 if (__io_write (_hurd_init_dtable[1], msg, len, -1, &nwrote))
256 } while (nwrote > 0);
257 msg = va_arg (ap, const char *);
262 /* Minimal open/close/mmap implementation sufficient for initial loading of
263 shared libraries. These are weak definitions so that when the
264 dynamic linker re-relocates itself to be user-visible (for -ldl),
265 it will get the user's definition (i.e. usually libc's). */
268 __open (const char *file_name, int mode, ...)
270 enum retry_type doretry;
271 char retryname[1024]; /* XXX string_t LOSES! */
272 file_t startdir, newpt, fileport;
277 assert (mode == O_RDONLY);
279 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
280 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
282 while (file_name[0] == '/')
285 if (err = __dir_lookup (startdir, file_name, mode, 0,
286 &doretry, retryname, &fileport))
287 return __hurd_fail (err);
295 __mach_port_deallocate (__mach_task_self (), startdir);
297 return __hurd_fail (err);
301 case FS_RETRY_REAUTH:
303 mach_port_t ref = __mach_reply_port ();
304 err = __io_reauthenticate (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
306 err = __auth_user_authenticate
307 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
308 ref, MACH_MSG_TYPE_MAKE_SEND,
310 __mach_port_destroy (__mach_task_self (), ref);
312 __mach_port_deallocate (__mach_task_self (), fileport);
314 return __hurd_fail (err);
318 case FS_RETRY_NORMAL:
320 if (nloops++ >= SYMLOOP_MAX)
321 return __hurd_fail (ELOOP);
324 /* An empty RETRYNAME indicates we have the final port. */
325 if (retryname[0] == '\0')
327 mach_port_t memobj_rd, memobj_wr;
332 /* We have the file open. Now map it. */
333 err = __io_map (fileport, &memobj_rd, &memobj_wr);
335 __mach_port_deallocate (__mach_task_self (), fileport);
337 return __hurd_fail (err);
338 if (memobj_wr != MACH_PORT_NULL)
339 __mach_port_deallocate (__mach_task_self (), memobj_wr);
341 return (int) memobj_rd;
346 file_name = retryname;
349 case FS_RETRY_MAGICAL:
350 switch (retryname[0])
353 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
355 if (fileport != MACH_PORT_NULL)
356 __mach_port_deallocate (__mach_task_self (), fileport);
357 file_name = &retryname[1];
361 if (retryname[1] == 'd' && retryname[2] == '/' &&
362 isdigit (retryname[3]))
364 /* We can't use strtol for the decoding here
365 because it brings in hairy locale bloat. */
368 for (p = &retryname[3]; isdigit (*p); ++p)
369 fd = (fd * 10) + (*p - '0');
370 /* Check for excess text after the number. A slash is
371 valid; it ends the component. Anything else does not
372 name a numeric file descriptor. */
373 if (*p != '/' && *p != '\0')
374 return __hurd_fail (ENOENT);
375 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
376 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
377 /* If the name was a proper number, but the file
378 descriptor does not exist, we return EBADF instead
380 return __hurd_fail (EBADF);
381 fileport = _dl_hurd_data->dtable[fd];
384 /* This descriptor is the file port we want. */
390 /* Do a normal retry on the remaining components. */
393 file_name = p + 1; /* Skip the slash. */
402 if (retryname[1] == 'a' && retryname[2] == 'c' &&
403 retryname[3] == 'h' && retryname[4] == 't' &&
404 retryname[5] == 'y' && retryname[6] == 'p' &&
408 struct host_basic_info hostinfo;
409 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
411 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
412 (natural_t *) &hostinfo,
415 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
417 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
419 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
421 abort (); /* XXX write this right if this ever happens */
423 strcpy (retryname, p);
432 if (retryname[1] == 't' && retryname[2] == 'y')
433 switch (retryname[3])
435 error_t opentty (file_t *result)
439 err = __termctty_open_terminal
440 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
444 mach_port_t ref = __mach_reply_port ();
445 err = __io_reauthenticate
446 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
448 err = __auth_user_authenticate
449 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
450 ref, MACH_MSG_TYPE_MAKE_SEND,
452 __mach_port_deallocate (__mach_task_self (),
454 __mach_port_destroy (__mach_task_self (), ref);
460 if (err = opentty (&fileport))
461 return __hurd_fail (err);
465 if (err = opentty (&startdir))
466 return __hurd_fail (err);
468 strcpy (retryname, &retryname[4]);
479 return __hurd_fail (EGRATUITOUS);
484 return __hurd_fail (EGRATUITOUS);
487 err = __dir_lookup (startdir, file_name, mode, 0,
488 &doretry, retryname, &fileport);
495 if (fd != (int) MACH_PORT_NULL)
496 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
500 caddr_t weak_function
501 __mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
505 vm_address_t mapaddr;
507 vmprot = VM_PROT_NONE;
508 if (prot & PROT_READ)
509 vmprot |= VM_PROT_READ;
510 if (prot & PROT_WRITE)
511 vmprot |= VM_PROT_WRITE;
512 if (prot & PROT_EXEC)
513 vmprot |= VM_PROT_EXECUTE;
515 mapaddr = (vm_address_t) addr;
516 err = __vm_map (__mach_task_self (),
517 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
518 !(flags & MAP_FIXED),
519 (flags & MAP_ANON) ? MACH_PORT_NULL : (mach_port_t) fd,
520 (vm_offset_t) offset,
521 flags & (MAP_COPY|MAP_PRIVATE),
523 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
524 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
526 /* XXX this is not atomic as it is in unix! */
527 /* The region is already allocated; deallocate it first. */
528 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
530 err = __vm_map (__mach_task_self (),
531 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
532 !(flags & MAP_FIXED),
533 (mach_port_t) fd, (vm_offset_t) offset,
534 flags & (MAP_COPY|MAP_PRIVATE),
537 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
540 return err ? (caddr_t) __hurd_fail (err) : (caddr_t) mapaddr;
546 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
547 W_EXITCODE (status, 0), 0);
548 while (__task_terminate (__mach_task_self ()))
549 __mach_task_self_ = (__mach_task_self) ();
553 /* This function is called by interruptible RPC stubs. For initial
554 dynamic linking, just use the normal mach_msg. Since this defn is
555 weak, the real defn in libc.so will override it if we are linked into
556 the user program (-ldl). */
558 error_t weak_function
559 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
560 mach_msg_option_t option,
561 mach_msg_size_t send_size,
562 mach_msg_size_t rcv_size,
563 mach_port_t rcv_name,
564 mach_msg_timeout_t timeout,
567 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,