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.
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/_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;
53 /* XXX loser kludge for vm_map kernel bug */
54 static vm_address_t fmha;
55 static vm_size_t fmhs;
57 __vm_deallocate(__mach_task_self(),fmha,fmhs);}
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){
66 if (err) assert(err==KERN_NO_SPACE);
67 if (!fmha)fmhs=0;else{
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);
74 /* XXX loser kludge for vm_map kernel bug */
79 _dl_sysdep_start (void **start_argptr,
80 void (*dl_main) (const Elf32_Phdr *phdr, Elf32_Word phent,
81 Elf32_Addr *user_entry))
83 extern void _start ();
85 void go (int *argdata)
87 extern unsigned int _dl_skip_args; /* rtld.c */
90 /* Cache the information in various global variables. */
92 _dl_argv = 1 + (char **) argdata;
93 _environ = &_dl_argv[_dl_argc + 1];
94 for (p = _environ; *p++;); /* Skip environ pointers and terminator. */
96 if ((void *) p == _dl_argv[0])
98 static struct hurd_startup_data nodata;
99 _dl_hurd_data = &nodata;
100 nodata.user_entry = (vm_address_t) &_start;
103 _dl_hurd_data = (void *) p;
105 _dl_secure = _dl_hurd_data->flags & EXEC_SECURE;
109 if (_dl_hurd_data->user_entry == (vm_address_t) &_start)
110 /* We were invoked as a command, not as the program interpreter.
111 The generic ld.so code supports this: it will parse the args
112 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
113 support an additional special syntax:
114 ld.so [-LIBS...] PROGRAM [ARGS...]
115 Each LIBS word consists of "FILENAME=MEMOBJ";
116 for example "-/lib/libc.so=123" says that the contents of
117 /lib/libc.so are found in a memory object whose port name
118 in our task is 123. */
119 while (_dl_argc > 2 && _dl_argv[1][0] == '-' && _dl_argv[1][1] != '-')
121 char *lastslash, *memobjname, *p;
128 p = _dl_argv++[1] + 1;
130 memobjname = strchr (p, '=');
132 _dl_sysdep_fatal ("Bogus library spec: ", p, "\n", NULL);
133 *memobjname++ = '\0';
134 memobj = (mach_port_t) atoi (memobjname);
136 /* Add a user reference on the memory object port, so we will
137 still have one after _dl_map_object_from_fd calls our
139 err = __mach_port_mod_refs (__mach_task_self (), memobj,
140 MACH_PORT_RIGHT_SEND, +1);
143 lastslash = strrchr (p, '/');
144 l = _dl_map_object_from_fd (lastslash ? lastslash + 1 : p,
147 /* Squirrel away the memory object port where it
148 can be retrieved by the program later. */
149 l->l_info[DT_NULL] = (void *) memobj;
152 /* Call elf/rtld.c's main program. It will set everything
153 up and leave us to transfer control to USER_ENTRY. */
154 (*dl_main) ((const Elf32_Phdr *) _dl_hurd_data->phdr,
155 _dl_hurd_data->phdrsz / sizeof (Elf32_Phdr),
156 &_dl_hurd_data->user_entry);
158 /* Deallocate the reply port and task port rights acquired by
159 __mach_init. We are done with them now, and the user will
160 reacquire them for himself when he wants them. */
161 __mig_dealloc_reply_port (MACH_PORT_NULL);
162 __mach_port_deallocate (__mach_task_self (), __mach_task_self_);
164 if (_dl_skip_args && _dl_argv[-_dl_skip_args] == (char *) p)
166 /* We are ignoring the first few arguments, but we have no Hurd
167 startup data. It is magical convention that ARGV[0] == P in
168 this case. The startup code in init-first.c will get confused
169 if this is not the case, so we must rearrange things to make
170 it so. Overwrite the original ARGV[0] at P with
171 ARGV[_dl_skip_args]. */
172 assert ((char *) p < _dl_argv[0]);
173 _dl_argv[0] = strcpy ((char *) p, _dl_argv[0]);
177 extern void _dl_start_user (void);
178 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
179 to the RTLD_START code which will run the user's entry point. */
180 RETURN_TO (argdata, &_dl_start_user, _dl_hurd_data->user_entry);
184 /* Set up so we can do RPCs. */
189 /* See hurd/hurdstartup.c; this deals with getting information
190 from the exec server and slicing up the arguments.
191 Then it will call `go', above. */
192 _hurd_startup (start_argptr, &go);
199 _dl_sysdep_open_zero_fill (void)
201 return (int) MACH_PORT_NULL;
206 _dl_sysdep_fatal (const char *msg, ...)
213 size_t len = strlen (msg);
214 mach_msg_type_number_t nwrote;
217 if (__io_write (_hurd_init_dtable[2], msg, len, -1, &nwrote))
221 } while (nwrote > 0);
222 msg = va_arg (ap, const char *);
231 _dl_sysdep_message (const char *msg, ...)
238 size_t len = strlen (msg);
239 mach_msg_type_number_t nwrote;
242 if (__io_write (_hurd_init_dtable[1], msg, len, -1, &nwrote))
246 } while (nwrote > 0);
247 msg = va_arg (ap, const char *);
252 /* Minimal open/close/mmap implementation sufficient for initial loading of
253 shared libraries. These are weak definitions so that when the
254 dynamic linker re-relocates itself to be user-visible (for -ldl),
255 it will get the user's definition (i.e. usually libc's). */
258 open (const char *file_name, int mode, ...)
260 enum retry_type doretry;
261 char retryname[1024]; /* XXX string_t LOSES! */
262 file_t startdir, newpt, fileport;
267 assert (mode == O_RDONLY);
269 startdir = _dl_hurd_data->portarray[file_name[0] == '/' ?
270 INIT_PORT_CRDIR : INIT_PORT_CWDIR];
272 while (file_name[0] == '/')
275 if (err = __dir_lookup (startdir, file_name, mode, 0,
276 &doretry, retryname, &fileport))
277 return __hurd_fail (err);
285 __mach_port_deallocate (__mach_task_self (), startdir);
287 return __hurd_fail (err);
291 case FS_RETRY_REAUTH:
293 mach_port_t ref = __mach_reply_port ();
294 err = __io_reauthenticate (fileport, ref, MACH_MSG_TYPE_MAKE_SEND);
296 err = __auth_user_authenticate
297 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
299 ref, MACH_MSG_TYPE_MAKE_SEND,
301 __mach_port_destroy (__mach_task_self (), ref);
303 __mach_port_deallocate (__mach_task_self (), fileport);
305 return __hurd_fail (err);
309 case FS_RETRY_NORMAL:
311 if (nloops++ >= SYMLOOP_MAX)
312 return __hurd_fail (ELOOP);
315 /* An empty RETRYNAME indicates we have the final port. */
316 if (retryname[0] == '\0')
318 mach_port_t memobj_rd, memobj_wr;
323 /* We have the file open. Now map it. */
324 err = __io_map (fileport, &memobj_rd, &memobj_wr);
326 __mach_port_deallocate (__mach_task_self (), fileport);
328 return __hurd_fail (err);
329 if (memobj_wr != MACH_PORT_NULL)
330 __mach_port_deallocate (__mach_task_self (), memobj_wr);
332 return (int) memobj_rd;
337 file_name = retryname;
340 case FS_RETRY_MAGICAL:
341 switch (retryname[0])
344 startdir = _dl_hurd_data->portarray[INIT_PORT_CRDIR];
346 if (fileport != MACH_PORT_NULL)
347 __mach_port_deallocate (__mach_task_self (), fileport);
348 file_name = &retryname[1];
352 if (retryname[1] == 'd' && retryname[2] == '/')
357 fd = (int) strtol (retryname, &end, 10);
358 if (end == NULL || err || /* Malformed number. */
359 /* Check for excess text after the number. A slash
360 is valid; it ends the component. Anything else
361 does not name a numeric file descriptor. */
362 (*end != '/' && *end != '\0'))
363 return __hurd_fail (ENOENT);
364 if (fd < 0 || fd >= _dl_hurd_data->dtablesize ||
365 _dl_hurd_data->dtable[fd] == MACH_PORT_NULL)
366 /* If the name was a proper number, but the file
367 descriptor does not exist, we return EBADF instead
369 return __hurd_fail (EBADF);
370 fileport = _dl_hurd_data->dtable[fd];
373 /* This descriptor is the file port we want. */
379 /* Do a normal retry on the remaining components. */
382 file_name = end + 1; /* Skip the slash. */
391 if (retryname[1] == 'a' && retryname[2] == 'c' &&
392 retryname[3] == 'h' && retryname[4] == 't' &&
393 retryname[5] == 'y' && retryname[6] == 'p' &&
397 struct host_basic_info hostinfo;
398 mach_msg_type_number_t hostinfocnt = HOST_BASIC_INFO_COUNT;
400 if (err = __host_info (__mach_host_self (), HOST_BASIC_INFO,
401 (natural_t *) &hostinfo,
404 if (hostinfocnt != HOST_BASIC_INFO_COUNT)
406 p = _itoa (hostinfo.cpu_subtype, &retryname[8], 10, 0);
408 p = _itoa (hostinfo.cpu_type, &retryname[8], 10, 0);
410 abort (); /* XXX write this right if this ever happens */
412 strcpy (retryname, p);
421 if (retryname[1] == 't' && retryname[2] == 'y')
422 switch (retryname[3])
424 error_t opentty (file_t *result)
428 err = __termctty_open_terminal
429 (_dl_hurd_data->portarray[INIT_PORT_CTTYID],
433 mach_port_t ref = __mach_reply_port ();
434 err = __io_reauthenticate
435 (unauth, ref, MACH_MSG_TYPE_MAKE_SEND);
437 err = __auth_user_authenticate
438 (_dl_hurd_data->portarray[INIT_PORT_AUTH],
440 ref, MACH_MSG_TYPE_MAKE_SEND,
442 __mach_port_deallocate (__mach_task_self (),
444 __mach_port_destroy (__mach_task_self (), ref);
450 if (err = opentty (&fileport))
451 return __hurd_fail (err);
455 if (err = opentty (&startdir))
456 return __hurd_fail (err);
458 strcpy (retryname, &retryname[4]);
469 return __hurd_fail (EGRATUITOUS);
474 return __hurd_fail (EGRATUITOUS);
477 err = __dir_lookup (startdir, file_name, mode, 0,
478 &doretry, retryname, &fileport);
485 if (fd != (int) MACH_PORT_NULL)
486 __mach_port_deallocate (__mach_task_self (), (mach_port_t) fd);
491 mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
495 vm_address_t mapaddr;
497 vmprot = VM_PROT_NONE;
498 if (prot & PROT_READ)
499 vmprot |= VM_PROT_READ;
500 if (prot & PROT_WRITE)
501 vmprot |= VM_PROT_WRITE;
502 if (prot & PROT_EXEC)
503 vmprot |= VM_PROT_EXECUTE;
505 mapaddr = (vm_address_t) addr;
506 err = __vm_map (__mach_task_self (),
507 &mapaddr, (vm_size_t) len, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
508 !(flags & MAP_FIXED),
509 (mach_port_t) fd, (vm_offset_t) offset,
510 flags & (MAP_COPY|MAP_PRIVATE),
512 (flags & MAP_INHERIT) ? VM_INHERIT_COPY : VM_INHERIT_NONE);
513 return err ? (caddr_t) __hurd_fail (err) : (caddr_t) mapaddr;
519 __proc_mark_exit (_dl_hurd_data->portarray[INIT_PORT_PROC],
520 W_EXITCODE (status, 0));
521 while (__task_terminate (__mach_task_self ()))
522 __mach_task_self_ = (__mach_task_self) ();
530 /* Minimal `malloc' allocator for use while loading shared libraries.
531 Only small blocks are allocated, and none are ever freed. */
536 static vm_address_t ptr, end;
541 /* Consume any unused space in the last page of our data segment. */
543 ptr = (vm_address_t) &_end;
544 end = round_page (ptr);
547 /* Make sure the allocation pointer is ideally aligned. */
548 ptr += sizeof (double) - 1;
549 ptr &= ~(sizeof (double) - 1);
553 /* Insufficient space left; allocate another page. */
555 assert (n <= __vm_page_size);
556 __vm_allocate (__mach_task_self (), &page, __vm_page_size, 1);
559 end = page + __vm_page_size;
562 block = (void *) ptr;
569 /* These should never be called. */
570 void *realloc (void *ptr, size_t n) { ptr += n; abort (); }
571 void free (void *ptr) { ptr = ptr; abort (); }
572 weak_symbol (realloc)
575 /* Avoid signal frobnication in setjmp/longjmp. */
577 int __sigjmp_save (sigjmp_buf env, int savemask)
578 { env[0].__mask_was_saved = savemask; return 0; }
579 weak_symbol (__sigjmp_save)
582 longjmp (jmp_buf env, int val) { __longjmp (env[0].__jmpbuf, val); }
583 weak_symbol (longjmp)
586 /* This function is called by interruptible RPC stubs. For initial
587 dynamic linking, just use the normal mach_msg. Since this defn is
588 weak, the real defn in libc.so will override it if we are linked into
589 the user program (-ldl). */
592 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
593 mach_msg_option_t option,
594 mach_msg_size_t send_size,
595 mach_msg_size_t rcv_size,
596 mach_port_t rcv_name,
597 mach_msg_timeout_t timeout,
600 return __mach_msg (msg, option, send_size, rcv_size, rcv_name,
603 weak_symbol (_hurd_intr_rpc_mach_msg)