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. */
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>
39 #include "dl-machine.h"
41 extern void __mach_init (void);
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_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 __libc_enable_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';
143 while (*memobjname
!= '\0')
144 memobj
= (memobj
* 10) + (*memobjname
++ - '0');
146 /* Add a user reference on the memory object port, so we will
147 still have one after _dl_map_object_from_fd calls our
149 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
150 MACH_PORT_RIGHT_SEND
, +1);
153 lastslash
= strrchr (p
, '/');
154 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : p
,
157 /* Squirrel away the memory object port where it
158 can be retrieved by the program later. */
159 l
->l_info
[DT_NULL
] = (void *) memobj
;
162 /* Call elf/rtld.c's main program. It will set everything
163 up and leave us to transfer control to USER_ENTRY. */
164 (*dl_main
) ((const Elf32_Phdr
*) _dl_hurd_data
->phdr
,
165 _dl_hurd_data
->phdrsz
/ sizeof (Elf32_Phdr
),
166 &_dl_hurd_data
->user_entry
);
168 if (_dl_skip_args
&& _dl_argv
[-_dl_skip_args
] == (char *) p
)
170 /* We are ignoring the first few arguments, but we have no Hurd
171 startup data. It is magical convention that ARGV[0] == P in
172 this case. The startup code in init-first.c will get confused
173 if this is not the case, so we must rearrange things to make
174 it so. Overwrite the original ARGV[0] at P with
175 ARGV[_dl_skip_args]. */
176 assert ((char *) p
< _dl_argv
[0]);
177 _dl_argv
[0] = strcpy ((char *) p
, _dl_argv
[0]);
181 extern void _dl_start_user (void);
182 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
183 to the RTLD_START code which will run the user's entry point. */
184 RETURN_TO (argdata
, &_dl_start_user
, _dl_hurd_data
->user_entry
);
188 /* Set up so we can do RPCs. */
193 /* See hurd/hurdstartup.c; this deals with getting information
194 from the exec server and slicing up the arguments.
195 Then it will call `go', above. */
196 _hurd_startup (start_argptr
, &go
);
203 _dl_sysdep_start_cleanup (void)
205 /* Deallocate the reply port and task port rights acquired by
206 __mach_init. We are done with them now, and the user will
207 reacquire them for himself when he wants them. */
208 __mig_dealloc_reply_port (MACH_PORT_NULL
);
209 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
213 _dl_sysdep_fatal (const char *msg
, ...)
220 size_t len
= strlen (msg
);
221 mach_msg_type_number_t nwrote
;
224 if (__io_write (_hurd_init_dtable
[2], msg
, len
, -1, &nwrote
))
228 } while (nwrote
> 0);
229 msg
= va_arg (ap
, const char *);
238 _dl_sysdep_error (const char *msg
, ...)
245 size_t len
= strlen (msg
);
246 mach_msg_type_number_t nwrote
;
249 if (__io_write (_hurd_init_dtable
[2], msg
, len
, -1, &nwrote
))
253 } while (nwrote
> 0);
254 msg
= va_arg (ap
, const char *);
261 _dl_sysdep_message (const char *msg
, ...)
268 size_t len
= strlen (msg
);
269 mach_msg_type_number_t nwrote
;
272 if (__io_write (_hurd_init_dtable
[1], msg
, len
, -1, &nwrote
))
276 } while (nwrote
> 0);
277 msg
= va_arg (ap
, const char *);
282 /* Minimal open/close/mmap implementation sufficient for initial loading of
283 shared libraries. These are weak definitions so that when the
284 dynamic linker re-relocates itself to be user-visible (for -ldl),
285 /* Minimal open/close/mmap implementation sufficient for initial loading of
286 shared libraries. These are weak definitions so that when the
287 dynamic linker re-relocates itself to be user-visible (for -ldl),
288 it will get the user's definition (i.e. usually libc's). */
290 /* Open FILE_NAME and return a read-mmappable port in MEMOBJ_RD for it, or
291 return an error. If STAT is non-zero, stat the file into that stat buffer. */
293 open_file (const char *file_name
, int mode
,
294 mach_port_t
*memobj_rd
, struct stat
*stat
)
296 enum retry_type doretry
;
297 char retryname
[1024]; /* XXX string_t LOSES! */
298 file_t startdir
, newpt
, fileport
;
303 assert (mode
== O_RDONLY
);
305 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/' ?
306 INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
308 while (file_name
[0] == '/')
311 if (err
= __dir_lookup (startdir
, (char *)file_name
, mode
, 0,
312 &doretry
, retryname
, &fileport
))
321 __mach_port_deallocate (__mach_task_self (), startdir
);
327 case FS_RETRY_REAUTH
:
329 mach_port_t ref
= __mach_reply_port ();
330 err
= __io_reauthenticate (fileport
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
332 err
= __auth_user_authenticate
333 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
334 ref
, MACH_MSG_TYPE_MAKE_SEND
,
336 __mach_port_destroy (__mach_task_self (), ref
);
338 __mach_port_deallocate (__mach_task_self (), fileport
);
344 case FS_RETRY_NORMAL
:
346 if (nloops
++ >= SYMLOOP_MAX
)
350 /* An empty RETRYNAME indicates we have the final port. */
351 if (retryname
[0] == '\0')
353 mach_port_t memobj_wr
;
358 /* We have the file open. Now map it. */
361 err
= __io_stat (fileport
, stat
);
363 err
= __io_map (fileport
, memobj_rd
, &memobj_wr
);
366 __mach_port_deallocate (__mach_task_self (), fileport
);
370 if (memobj_wr
!= MACH_PORT_NULL
)
371 __mach_port_deallocate (__mach_task_self (), memobj_wr
);
378 file_name
= retryname
;
381 case FS_RETRY_MAGICAL
:
382 switch (retryname
[0])
385 startdir
= _dl_hurd_data
->portarray
[INIT_PORT_CRDIR
];
387 if (fileport
!= MACH_PORT_NULL
)
388 __mach_port_deallocate (__mach_task_self (), fileport
);
389 file_name
= &retryname
[1];
393 if (retryname
[1] == 'd' && retryname
[2] == '/' &&
394 isdigit (retryname
[3]))
396 /* We can't use strtol for the decoding here
397 because it brings in hairy locale bloat. */
400 for (p
= &retryname
[3]; isdigit (*p
); ++p
)
401 fd
= (fd
* 10) + (*p
- '0');
402 /* Check for excess text after the number. A slash is
403 valid; it ends the component. Anything else does not
404 name a numeric file descriptor. */
405 if (*p
!= '/' && *p
!= '\0')
407 if (fd
< 0 || fd
>= _dl_hurd_data
->dtablesize
||
408 _dl_hurd_data
->dtable
[fd
] == MACH_PORT_NULL
)
409 /* If the name was a proper number, but the file
410 descriptor does not exist, we return EBADF instead
413 fileport
= _dl_hurd_data
->dtable
[fd
];
416 /* This descriptor is the file port we want. */
422 /* Do a normal retry on the remaining components. */
425 file_name
= p
+ 1; /* Skip the slash. */
434 if (retryname
[1] == 'a' && retryname
[2] == 'c' &&
435 retryname
[3] == 'h' && retryname
[4] == 't' &&
436 retryname
[5] == 'y' && retryname
[6] == 'p' &&
440 struct host_basic_info hostinfo
;
441 mach_msg_type_number_t hostinfocnt
= HOST_BASIC_INFO_COUNT
;
443 if (err
= __host_info (__mach_host_self (), HOST_BASIC_INFO
,
444 (natural_t
*) &hostinfo
,
447 if (hostinfocnt
!= HOST_BASIC_INFO_COUNT
)
449 p
= _itoa (hostinfo
.cpu_subtype
, &retryname
[8], 10, 0);
451 p
= _itoa (hostinfo
.cpu_type
, &retryname
[8], 10, 0);
453 abort (); /* XXX write this right if this ever happens */
455 strcpy (retryname
, p
);
464 if (retryname
[1] == 't' && retryname
[2] == 'y')
465 switch (retryname
[3])
467 error_t
opentty (file_t
*result
)
471 err
= __termctty_open_terminal
472 (_dl_hurd_data
->portarray
[INIT_PORT_CTTYID
],
476 mach_port_t ref
= __mach_reply_port ();
477 err
= __io_reauthenticate
478 (unauth
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
480 err
= __auth_user_authenticate
481 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
482 ref
, MACH_MSG_TYPE_MAKE_SEND
,
484 __mach_port_deallocate (__mach_task_self (),
486 __mach_port_destroy (__mach_task_self (), ref
);
492 if (err
= opentty (&fileport
))
497 if (err
= opentty (&startdir
))
500 strcpy (retryname
, &retryname
[4]);
519 err
= __dir_lookup (startdir
, (char *)file_name
, mode
, 0,
520 &doretry
, retryname
, &fileport
);
525 __open (const char *file_name
, int mode
, ...)
527 mach_port_t memobj_rd
;
528 error_t err
= open_file (file_name
, mode
, &memobj_rd
, 0);
530 return __hurd_fail (err
);
532 return (int)memobj_rd
;
538 if (fd
!= (int) MACH_PORT_NULL
)
539 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
543 caddr_t weak_function
544 __mmap (caddr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
548 vm_address_t mapaddr
;
550 vmprot
= VM_PROT_NONE
;
551 if (prot
& PROT_READ
)
552 vmprot
|= VM_PROT_READ
;
553 if (prot
& PROT_WRITE
)
554 vmprot
|= VM_PROT_WRITE
;
555 if (prot
& PROT_EXEC
)
556 vmprot
|= VM_PROT_EXECUTE
;
558 mapaddr
= (vm_address_t
) addr
;
559 err
= __vm_map (__mach_task_self (),
560 &mapaddr
, (vm_size_t
) len
, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
561 !(flags
& MAP_FIXED
),
562 (flags
& MAP_ANON
) ? MACH_PORT_NULL
: (mach_port_t
) fd
,
563 (vm_offset_t
) offset
,
564 flags
& (MAP_COPY
|MAP_PRIVATE
),
566 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
567 if (err
== KERN_NO_SPACE
&& (flags
& MAP_FIXED
))
571 _dl_sysdep_fatal (const char *msg
, ...)
578 size_t len
= strlen (msg
);
579 mach_msg_type_number_t nwrote
;
582 if (__io_write (_hurd_init_dtable
[2], msg
, len
, -1, &nwrote
))
586 } while (nwrote
> 0);
587 msg
= va_arg (ap
, const char *);
596 _dl_sysdep_error (const char *msg
, ...)
603 size_t len
= strlen (msg
);
604 mach_msg_type_number_t nwrote
;
607 if (__io_write (_hurd_init_dtable
[2], msg
, len
, -1, &nwrote
))
611 } while (nwrote
> 0);
612 msg
= va_arg (ap
, const char *);
619 _dl_sysdep_message (const char *msg
, ...)
626 size_t len
= strlen (msg
);
627 mach_msg_type_number_t nwrote
;
630 if (__io_write (_hurd_init_dtable
[1], msg
, len
, -1, &nwrote
))
634 } while (nwrote
> 0);
635 msg
= va_arg (ap
, const char *);
639 /* XXX this is not atomic as it is in unix! */
640 /* The region is already allocated; deallocate it first. */
641 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
643 err
= __vm_map (__mach_task_self (),
644 &mapaddr
, (vm_size_t
) len
, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
645 !(flags
& MAP_FIXED
),
646 (mach_port_t
) fd
, (vm_offset_t
) offset
,
647 flags
& (MAP_COPY
|MAP_PRIVATE
),
650 ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
653 return err
? (caddr_t
) __hurd_fail (err
) : (caddr_t
) mapaddr
;
659 __proc_mark_exit (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
660 W_EXITCODE (status
, 0), 0);
661 while (__task_terminate (__mach_task_self ()))
662 __mach_task_self_
= (__mach_task_self
) ();
665 /* Read the whole contents of FILE into new mmap'd space with given
666 protections. The size of the file is returned in SIZE. */
669 _dl_sysdep_read_whole_file (const char *file
, size_t *size
, int prot
)
672 mach_port_t memobj_rd
;
674 error_t err
= open_file (file
, O_RDONLY
, &memobj_rd
, &stat
);
678 /* Map a copy of the file contents. */
679 contents
= __mmap (0, stat
.st_size
, prot
, MAP_COPY
, memobj_rd
, 0);
680 if (contents
== (void *)-1)
683 *size
= stat
.st_size
;
685 __mach_port_deallocate (__mach_task_self (), memobj_rd
);
696 /* This function is called by interruptible RPC stubs. For initial
697 dynamic linking, just use the normal mach_msg. Since this defn is
698 weak, the real defn in libc.so will override it if we are linked into
699 the user program (-ldl). */
701 error_t weak_function
702 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
703 mach_msg_option_t option
,
704 mach_msg_size_t send_size
,
705 mach_msg_size_t rcv_size
,
706 mach_port_t rcv_name
,
707 mach_msg_timeout_t timeout
,
710 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,