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. */
26 #include <elf/ldsodefs.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>
41 #include <dl-machine.h>
42 #include <dl-procinfo.h>
44 extern void __mach_init (void);
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
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
){
80 if (err
) assert(err
==KERN_NO_SPACE
);
81 if (!fmha
)fmhs
=0;else{
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
);
88 /* XXX loser kludge for vm_map kernel bug */
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 */
102 /* Cache the information in various global variables. */
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
;
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
;
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
;
145 p
= _dl_argv
++[1] + 1;
147 memobjname
= strchr (p
, '=');
149 _dl_sysdep_fatal ("Bogus library spec: ", p
, "\n", NULL
);
150 *memobjname
++ = '\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
158 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
159 MACH_PORT_RIGHT_SEND
, +1);
162 lastslash
= strrchr (p
, '/');
163 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : 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
;
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. */
201 /* Initialize frequently used global variable. */
202 _dl_pagesize
= __getpagesize ();
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
);
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. */
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
;
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] == '/')
251 if (err
= __dir_lookup (startdir
, (char *)file_name
, mode
, 0,
252 &doretry
, retryname
, &fileport
))
261 __mach_port_deallocate (__mach_task_self (), startdir
);
267 case FS_RETRY_REAUTH
:
269 mach_port_t ref
= __mach_reply_port ();
270 err
= __io_reauthenticate (fileport
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
272 err
= __auth_user_authenticate
273 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
274 ref
, MACH_MSG_TYPE_MAKE_SEND
,
276 __mach_port_destroy (__mach_task_self (), ref
);
278 __mach_port_deallocate (__mach_task_self (), fileport
);
284 case FS_RETRY_NORMAL
:
286 if (nloops
++ >= SYMLOOP_MAX
)
290 /* An empty RETRYNAME indicates we have the final port. */
291 if (retryname
[0] == '\0')
295 /* We have the file open. Now map it. */
297 err
= __io_stat (fileport
, stat
);
302 __mach_port_deallocate (__mach_task_self (), fileport
);
307 __mach_port_mod_refs (__mach_task_self (), fileport
,
308 MACH_PORT_RIGHT_SEND
, 1);
317 file_name
= retryname
;
320 case FS_RETRY_MAGICAL
:
321 switch (retryname
[0])
324 startdir
= _dl_hurd_data
->portarray
[INIT_PORT_CRDIR
];
326 if (fileport
!= MACH_PORT_NULL
)
327 __mach_port_deallocate (__mach_task_self (), fileport
);
328 file_name
= &retryname
[1];
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. */
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')
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
352 fileport
= _dl_hurd_data
->dtable
[fd
];
355 /* This descriptor is the file port we want. */
361 /* Do a normal retry on the remaining components. */
364 file_name
= p
+ 1; /* Skip the slash. */
373 if (retryname
[1] == 'a' && retryname
[2] == 'c' &&
374 retryname
[3] == 'h' && retryname
[4] == 't' &&
375 retryname
[5] == 'y' && retryname
[6] == 'p' &&
379 struct host_basic_info hostinfo
;
380 mach_msg_type_number_t hostinfocnt
= HOST_BASIC_INFO_COUNT
;
382 if (err
= __host_info (__mach_host_self (), HOST_BASIC_INFO
,
383 (natural_t
*) &hostinfo
,
386 if (hostinfocnt
!= HOST_BASIC_INFO_COUNT
)
388 p
= _itoa (hostinfo
.cpu_subtype
, &retryname
[8], 10, 0);
390 p
= _itoa (hostinfo
.cpu_type
, &retryname
[8], 10, 0);
392 abort (); /* XXX write this right if this ever happens */
394 strcpy (retryname
, p
);
403 if (retryname
[1] == 't' && retryname
[2] == 'y')
404 switch (retryname
[3])
406 error_t
opentty (file_t
*result
)
410 err
= __termctty_open_terminal
411 (_dl_hurd_data
->portarray
[INIT_PORT_CTTYID
],
415 mach_port_t ref
= __mach_reply_port ();
416 err
= __io_reauthenticate
417 (unauth
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
419 err
= __auth_user_authenticate
420 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
421 ref
, MACH_MSG_TYPE_MAKE_SEND
,
423 __mach_port_deallocate (__mach_task_self (),
425 __mach_port_destroy (__mach_task_self (), ref
);
431 if (err
= opentty (&fileport
))
436 if (err
= opentty (&startdir
))
439 strcpy (retryname
, &retryname
[4]);
458 err
= __dir_lookup (startdir
, (char *)file_name
, mode
, 0,
459 &doretry
, retryname
, &fileport
);
464 __open (const char *file_name
, int mode
, ...)
467 error_t err
= open_file (file_name
, mode
, &port
, 0);
469 return __hurd_fail (err
);
477 if (fd
!= (int) MACH_PORT_NULL
)
478 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
482 __ssize_t weak_function
483 __libc_read (int fd
, void *buf
, size_t nbytes
)
487 mach_msg_type_number_t nread
;
490 err
= __io_read ((mach_port_t
) fd
, &data
, &nread
, -1, nbytes
);
492 return __hurd_fail (err
);
496 memcpy (buf
, data
, nread
);
497 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, nread
);
503 __ptr_t weak_function
504 __mmap (__ptr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
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
;
523 assert (!(flags
& MAP_SHARED
));
524 err
= __io_map ((mach_port_t
) fd
, &memobj_rd
, &memobj_wr
);
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
),
535 (vm_offset_t
) offset
,
536 flags
& (MAP_COPY
|MAP_PRIVATE
),
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
);
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
),
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
;
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. */
574 _dl_sysdep_read_whole_file (const char *file
, size_t *size
, int prot
)
577 mach_port_t memobj_rd
;
579 error_t err
= open_file (file
, O_RDONLY
, &memobj_rd
, &stat
);
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)
588 *size
= stat
.st_size
;
590 __mach_port_deallocate (__mach_task_self (), memobj_rd
);
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
,
615 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,
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
*
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
));
639 _dl_signal_error (ENOMEM
, NULL
, "cannot create capability list");
641 result
[0].str
= (char *) result
; /* Does not really matter. */
650 _dl_sysdep_output (int fd
, const char *msg
, ...)
654 assert(fd
< _hurd_init_dtablesize
);
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
))
667 } while (nwrote
> 0);
668 msg
= va_arg (ap
, const char *);