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>
37 #include "dl-machine.h"
39 extern void __mach_init (void);
42 extern char **_dl_argv;
43 extern char **_environ;
45 struct hurd_startup_data *_dl_hurd_data;
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 unsigned long int __hurd_sigthread_stack_base;
52 unsigned long int __hurd_sigthread_stack_end;
53 unsigned long int *__hurd_sigthread_variables;
54 unsigned long int __hurd_threadvar_stack_mask;
57 /* XXX loser kludge for vm_map kernel bug */
58 static vm_address_t fmha;
59 static vm_size_t fmhs;
60 static void unfmh(void){
61 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
62 static void fmh(void) {
63 error_t err;int x;mach_port_t p;
64 vm_address_t a=0x08000000U,max=VM_MAX_ADDRESS;
65 while (!(err=__vm_region(__mach_task_self(),&a,&fmhs,&x,&x,&x,&x,&p,&x))){
66 __mach_port_deallocate(__mach_task_self(),p);
67 if (a+fmhs>=0x80000000U){
70 if (err) assert(err==KERN_NO_SPACE);
71 if (!fmha)fmhs=0;else{
73 err = __vm_map (__mach_task_self (),
74 &fmha, fmhs, 0, 0, MACH_PORT_NULL, 0, 1,
75 VM_PROT_NONE, VM_PROT_NONE, VM_INHERIT_COPY);
78 /* XXX loser kludge for vm_map kernel bug */
83 _dl_sysdep_start (void **start_argptr,
84 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
85 Elf32_Addr *user_entry))
87 extern void _start ();
89 void go (int *argdata)
91 extern unsigned int _dl_skip_args; /* rtld.c */
94 /* Cache the information in various global variables. */
96 _dl_argv = 1 + (char **) argdata;
97 _environ = &_dl_argv[_dl_argc + 1];
98 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
100 if ((void *) p == _dl_argv[0])
102 static struct hurd_startup_data nodata;
103 _dl_hurd_data = &nodata;
104 nodata.user_entry = (vm_address_t) &_start;
107 _dl_hurd_data = (void *) p;
109 _dl_secure = _dl_hurd_data->flags & EXEC_SECURE;
111 if (_dl_hurd_data->flags & EXEC_STACK_ARGS &&
112 _dl_hurd_data->user_entry == 0)
113 _dl_hurd_data->user_entry = (vm_address_t) &_start;
117 if (_dl_hurd_data->user_entry == (vm_address_t) &_start)
118 /* We were invoked as a command, not as the program interpreter.
119 The generic ld.so code supports this: it will parse the args
120 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
121 support an additional special syntax:
122 ld.so [-LIBS...] PROGRAM [ARGS...]
123 Each LIBS word consists of "FILENAME=MEMOBJ";
124 for example "-/lib/libc.so=123" says that the contents of
125 /lib/libc.so are found in a memory object whose port name
126 in our task is 123. */
127 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
129 char *lastslash, *memobjname, *p;
136 p = _dl_argv++[1] + 1;
138 memobjname = strchr (p, '=');
140 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
141 *memobjname++ = '\0';
142 memobj = (mach_port_t) atoi (memobjname);
144 /* Add a user reference on the memory object port, so we will
145 still have one after _dl_map_object_from_fd calls our
147 err = __mach_port_mod_refs (__mach_task_self (), memobj,
148 MACH_PORT_RIGHT_SEND, +1);
151 lastslash = strrchr (p, '/');
152 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
155 /* Squirrel away the memory object port where it
156 can be retrieved by the program later. */
157 l->l_info[DT_NULL] = (void *) memobj;
160 /* Call elf/rtld.c's main program. It will set everything
161 up and leave us to transfer control to USER_ENTRY. */
162 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
163 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
164 &_dl_hurd_data->user_entry);
166 if (_dl_skip_args && _dl_argv[-_dl_skip_args] == (char *) p)
168 /* We are ignoring the first few arguments, but we have no Hurd
169 startup data. It is magical convention that ARGV[0] == P in
170 this case. The startup code in init-first.c will get confused
171 if this is not the case, so we must rearrange things to make
172 it so. Overwrite the original ARGV[0] at P with
173 ARGV[_dl_skip_args]. */
174 assert ((char *) p < _dl_argv[0]);
175 _dl_argv[0] = strcpy ((char *) p, _dl_argv[0]);
179 extern void _dl_start_user (void);
180 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
181 to the RTLD_START code which will run the user's entry point. */
182 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
186 /* Set up so we can do RPCs. */
191 /* See hurd/hurdstartup.c; this deals with getting information
192 from the exec server and slicing up the arguments.
193 Then it will call `go', above. */
194 _hurd_startup (start_argptr, &go);
201 _dl_sysdep_start_cleanup (void)
203 /* Deallocate the reply port and task port rights acquired by
204 __mach_init. We are done with them now, and the user will
205 reacquire them for himself when he wants them. */
206 __mig_dealloc_reply_port (MACH_PORT_NULL);
207 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
211 _dl_sysdep_open_zero_fill (void)
213 return (int) MACH_PORT_NULL;
218 _dl_sysdep_fatal (const char *msg, ...)
225 size_t len = strlen (msg);
226 mach_msg_type_number_t nwrote;
229 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
233 } while (nwrote > 0);
234 msg = va_arg (ap, const char *);
243 _dl_sysdep_message (const char *msg, ...)
250 size_t len = strlen (msg);
251 mach_msg_type_number_t nwrote;
254 if (__io_write (_hurd_init_dtable[1], msg, len, -1, &nwrote))
258 } while (nwrote > 0);
259 msg = va_arg (ap, const char *);
264 /* Minimal open/close/mmap implementation sufficient for initial loading of
265 shared libraries. These are weak definitions so that when the
266 dynamic linker re-relocates itself to be user-visible (for -ldl),
267 it will get the user's definition (i.e. usually libc's). */
270 open (const char *file_name, int mode, ...)
272 enum retry_type doretry;
273 char retryname[1024]; /* XXX string_t LOSES! */
274 file_t startdir, newpt, fileport;
279 assert (mode == O_RDONLY);
281 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
282 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
284 while (file_name[0] == '/')
287 if (err = __dir_lookup (startdir, file_name, mode, 0,
288 &doretry, retryname, &fileport))
289 return __hurd_fail (err);
297 __mach_port_deallocate (__mach_task_self (), startdir);
299 return __hurd_fail (err);
303 case FS_RETRY_REAUTH:
305 mach_port_t ref = __mach_reply_port ();
306 err = __io_reauthenticate (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
308 err = __auth_user_authenticate
309 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
311 ref, MACH_MSG_TYPE_MAKE_SEND,
313 __mach_port_destroy (__mach_task_self (), ref);
315 __mach_port_deallocate (__mach_task_self (), fileport);
317 return __hurd_fail (err);
321 case FS_RETRY_NORMAL:
323 if (nloops++ >= SYMLOOP_MAX)
324 return __hurd_fail (ELOOP);
327 /* An empty RETRYNAME indicates we have the final port. */
328 if (retryname[0] == '\0')
330 mach_port_t memobj_rd, memobj_wr;
335 /* We have the file open. Now map it. */
336 err = __io_map (fileport, &memobj_rd, &memobj_wr);
338 __mach_port_deallocate (__mach_task_self (), fileport);
340 return __hurd_fail (err);
341 if (memobj_wr != MACH_PORT_NULL)
342 __mach_port_deallocate (__mach_task_self (), memobj_wr);
344 return (int) memobj_rd;
349 file_name = retryname;
352 case FS_RETRY_MAGICAL:
353 switch (retryname[0])
356 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
358 if (fileport != MACH_PORT_NULL)
359 __mach_port_deallocate (__mach_task_self (), fileport);
360 file_name = &retryname[1];
364 if (retryname[1] == 'd' && retryname[2] == '/')
369 fd = (int) strtol (retryname, &end, 10);
370 if (end == NULL || err || /* Malformed number. */
371 /* Check for excess text after the number. A slash
372 is valid; it ends the component. Anything else
373 does not name a numeric file descriptor. */
374 (*end != '/' && *end != '\0'))
375 return __hurd_fail (ENOENT);
376 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
377 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
378 /* If the name was a proper number, but the file
379 descriptor does not exist, we return EBADF instead
381 return __hurd_fail (EBADF);
382 fileport = _dl_hurd_data->dtable[fd];
385 /* This descriptor is the file port we want. */
391 /* Do a normal retry on the remaining components. */
394 file_name = end + 1; /* Skip the slash. */
403 if (retryname[1] == 'a' && retryname[2] == 'c' &&
404 retryname[3] == 'h' && retryname[4] == 't' &&
405 retryname[5] == 'y' && retryname[6] == 'p' &&
409 struct host_basic_info hostinfo;
410 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
412 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
413 (natural_t *) &hostinfo,
416 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
418 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
420 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
422 abort (); /* XXX write this right if this ever happens */
424 strcpy (retryname, p);
433 if (retryname[1] == 't' && retryname[2] == 'y')
434 switch (retryname[3])
436 error_t opentty (file_t *result)
440 err = __termctty_open_terminal
441 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
445 mach_port_t ref = __mach_reply_port ();
446 err = __io_reauthenticate
447 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
449 err = __auth_user_authenticate
450 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
452 ref, MACH_MSG_TYPE_MAKE_SEND,
454 __mach_port_deallocate (__mach_task_self (),
456 __mach_port_destroy (__mach_task_self (), ref);
462 if (err = opentty (&fileport))
463 return __hurd_fail (err);
467 if (err = opentty (&startdir))
468 return __hurd_fail (err);
470 strcpy (retryname, &retryname[4]);
481 return __hurd_fail (EGRATUITOUS);
486 return __hurd_fail (EGRATUITOUS);
489 err = __dir_lookup (startdir, file_name, mode, 0,
490 &doretry, retryname, &fileport);
497 if (fd != (int) MACH_PORT_NULL)
498 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
503 mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
507 vm_address_t mapaddr;
509 vmprot = VM_PROT_NONE;
510 if (prot & PROT_READ)
511 vmprot |= VM_PROT_READ;
512 if (prot & PROT_WRITE)
513 vmprot |= VM_PROT_WRITE;
514 if (prot & PROT_EXEC)
515 vmprot |= VM_PROT_EXECUTE;
517 mapaddr = (vm_address_t) addr;
518 err = __vm_map (__mach_task_self (),
519 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
520 !(flags & MAP_FIXED),
521 (mach_port_t) fd, (vm_offset_t) offset,
522 flags & (MAP_COPY|MAP_PRIVATE),
524 (flags & MAP_SHARED) ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
525 if (err == KERN_NO_SPACE && (flags & MAP_FIXED))
527 /* XXX this is not atomic as it is in unix! */
528 /* The region is already allocated; deallocate it first. */
529 err = __vm_deallocate (__mach_task_self (), mapaddr, len);
531 err = __vm_map (__mach_task_self (),
532 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
533 !(flags & MAP_FIXED),
534 (mach_port_t) fd, (vm_offset_t) offset,
535 flags & (MAP_COPY|MAP_PRIVATE),
538 ? VM_INHERIT_SHARE : VM_INHERIT_COPY);
541 return err ? (caddr_t) __hurd_fail (err) : (caddr_t) mapaddr;
547 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
548 W_EXITCODE (status, 0));
549 while (__task_terminate (__mach_task_self ()))
550 __mach_task_self_ = (__mach_task_self) ();
559 /* This function is called by interruptible RPC stubs. For initial
560 dynamic linking, just use the normal mach_msg. Since this defn is
561 weak, the real defn in libc.so will override it if we are linked into
562 the user program (-ldl). */
565 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
566 mach_msg_option_t option,
567 mach_msg_size_t send_size,
568 mach_msg_size_t rcv_size,
569 mach_port_t rcv_name,
570 mach_msg_timeout_t timeout,
573 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
576 weak_symbol (_hurd_intr_rpc_mach_msg)