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 void go (int *argdata
)
87 /* Cache the information in various global variables. */
89 _dl_argv
= (void *) &argdata
[1];
90 _environ
= &_dl_argv
[_dl_argc
+ 1];
91 for (p
= _environ
; *p
; ++p
);
92 _dl_hurd_data
= (void *) ++p
;
94 _dl_secure
= _dl_hurd_data
->flags
& EXEC_SECURE
;
97 /* Call elf/rtld.c's main program. It will set everything
98 up and leave us to transfer control to USER_ENTRY. */
99 (*dl_main
) ((const Elf32_Phdr
*) _dl_hurd_data
->phdr
,
100 _dl_hurd_data
->phdrsz
/ sizeof (Elf32_Phdr
),
101 &_dl_hurd_data
->user_entry
);
103 /* Deallocate the reply port and task port rights acquired by
104 __mach_init. We are done with them now, and the user will
105 reacquire them for himself when he wants them. */
106 __mig_dealloc_reply_port (MACH_PORT_NULL
);
107 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
110 extern void _dl_start_user (void);
111 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
112 to the RTLD_START code which will run the user's entry point. */
113 RETURN_TO (argdata
, &_dl_start_user
, _dl_hurd_data
->user_entry
);
117 /* Set up so we can do RPCs. */
122 /* See hurd/hurdstartup.c; this deals with getting information
123 from the exec server and slicing up the arguments.
124 Then it will call `go', above. */
125 _hurd_startup (start_argptr
, &go
);
132 _dl_sysdep_open_zero_fill (void)
134 return (int) MACH_PORT_NULL
;
139 _dl_sysdep_fatal (const char *msg
, ...)
146 size_t len
= strlen (msg
);
147 mach_msg_type_number_t nwrote
;
150 if (__io_write (_hurd_init_dtable
[2], msg
, len
, -1, &nwrote
))
154 } while (nwrote
> 0);
155 msg
= va_arg (ap
, const char *);
163 /* Minimal open/close/mmap implementation sufficient for initial loading of
164 shared libraries. These are weak definitions so that when the
165 dynamic linker re-relocates itself to be user-visible (for -ldl),
166 it will get the user's definition (i.e. usually libc's). */
169 open (const char *file_name
, int mode
, ...)
171 enum retry_type doretry
;
172 char retryname
[1024]; /* XXX string_t LOSES! */
173 file_t startdir
, newpt
, fileport
;
178 assert (mode
== O_RDONLY
);
181 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/' ?
182 INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
184 while (file_name
[0] == '/')
187 if (errno
= __dir_lookup (startdir
, file_name
, mode
, 0,
188 &doretry
, retryname
, &fileport
))
198 __mach_port_deallocate (__mach_task_self (), startdir
);
204 case FS_RETRY_REAUTH
:
206 mach_port_t ref
= __mach_reply_port ();
207 errno
= __io_reauthenticate
208 (fileport
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
210 errno
= __auth_user_authenticate
211 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
213 ref
, MACH_MSG_TYPE_MAKE_SEND
,
215 __mach_port_destroy (__mach_task_self (), ref
);
217 __mach_port_deallocate (__mach_task_self (), fileport
);
223 case FS_RETRY_NORMAL
:
225 if (nloops
++ >= SYMLOOP_MAX
)
232 /* An empty RETRYNAME indicates we have the final port. */
233 if (retryname
[0] == '\0')
235 mach_port_t memobj_rd
, memobj_wr
;
240 /* We have the file open. Now map it. */
241 errno
= __io_map (fileport
, &memobj_rd
, &memobj_wr
);
243 __mach_port_deallocate (__mach_task_self (), fileport
);
246 if (memobj_wr
!= MACH_PORT_NULL
)
247 __mach_port_deallocate (__mach_task_self (), memobj_wr
);
249 return (int) memobj_rd
;
254 file_name
= retryname
;
257 case FS_RETRY_MAGICAL
:
258 switch (retryname
[0])
261 startdir
= _dl_hurd_data
->portarray
[INIT_PORT_CRDIR
];
263 if (fileport
!= MACH_PORT_NULL
)
264 __mach_port_deallocate (__mach_task_self (), fileport
);
265 file_name
= &retryname
[1];
269 if (retryname
[1] == 'd' && retryname
[2] == '/')
274 fd
= (int) strtol (retryname
, &end
, 10);
275 if (end
== NULL
|| errno
|| /* Malformed number. */
276 /* Check for excess text after the number. A slash
277 is valid; it ends the component. Anything else
278 does not name a numeric file descriptor. */
279 (*end
!= '/' && *end
!= '\0'))
284 if (fd
< 0 || fd
>= _dl_hurd_data
->dtablesize
||
285 _dl_hurd_data
->dtable
[fd
] == MACH_PORT_NULL
)
287 /* If the name was a proper number, but the file
288 descriptor does not exist, we return EBADF instead
293 fileport
= _dl_hurd_data
->dtable
[fd
];
296 /* This descriptor is the file port we want. */
302 /* Do a normal retry on the remaining components. */
305 file_name
= end
+ 1; /* Skip the slash. */
314 if (retryname
[1] == 'a' && retryname
[2] == 'c' &&
315 retryname
[3] == 'h' && retryname
[4] == 't' &&
316 retryname
[5] == 'y' && retryname
[6] == 'p' &&
320 struct host_basic_info hostinfo
;
321 mach_msg_type_number_t hostinfocnt
= HOST_BASIC_INFO_COUNT
;
323 if (err
= __host_info (__mach_host_self (), HOST_BASIC_INFO
,
324 (natural_t
*) &hostinfo
,
327 if (hostinfocnt
!= HOST_BASIC_INFO_COUNT
)
329 p
= _itoa (hostinfo
.cpu_subtype
, &retryname
[8], 10, 0);
331 p
= _itoa (hostinfo
.cpu_type
, &retryname
[8], 10, 0);
333 abort (); /* XXX write this right if this ever happens */
335 strcpy (retryname
, p
);
344 if (retryname
[1] == 't' && retryname
[2] == 'y')
345 switch (retryname
[3])
347 error_t
opentty (file_t
*result
)
351 err
= __termctty_open_terminal
352 (_dl_hurd_data
->portarray
[INIT_PORT_CTTYID
],
356 mach_port_t ref
= __mach_reply_port ();
357 err
= __io_reauthenticate
358 (unauth
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
360 err
= __auth_user_authenticate
361 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
363 ref
, MACH_MSG_TYPE_MAKE_SEND
,
365 __mach_port_deallocate (__mach_task_self (),
367 __mach_port_destroy (__mach_task_self (), ref
);
373 if (errno
= opentty (&fileport
))
378 if (errno
= opentty (&startdir
))
381 strcpy (retryname
, &retryname
[4]);
402 errno
= __dir_lookup (startdir
, file_name
, mode
, 0,
403 &doretry
, retryname
, &fileport
);
410 if (fd
!= (int) MACH_PORT_NULL
)
411 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
416 mmap (caddr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
419 vm_address_t mapaddr
;
421 vmprot
= VM_PROT_NONE
;
422 if (prot
& PROT_READ
)
423 vmprot
|= VM_PROT_READ
;
424 if (prot
& PROT_WRITE
)
425 vmprot
|= VM_PROT_WRITE
;
426 if (prot
& PROT_EXEC
)
427 vmprot
|= VM_PROT_EXECUTE
;
429 mapaddr
= (vm_address_t
) addr
;
430 errno
= __vm_map (__mach_task_self (),
431 &mapaddr
, (vm_size_t
) len
, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
432 !(flags
& MAP_FIXED
),
433 (mach_port_t
) fd
, (vm_offset_t
) offset
,
434 flags
& (MAP_COPY
|MAP_PRIVATE
),
436 (flags
& MAP_INHERIT
) ? VM_INHERIT_COPY
: VM_INHERIT_NONE
);
437 return errno
? (caddr_t
) -1 : (caddr_t
) mapaddr
;
443 __proc_mark_exit (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
444 W_EXITCODE (status
, 0));
445 while (__task_terminate (__mach_task_self ()))
446 __mach_task_self_
= (__mach_task_self
) ();
454 /* Minimal `malloc' allocator for use while loading shared libraries.
455 Only small blocks are allocated, and none are ever freed. */
460 static vm_address_t ptr
, end
;
465 /* Consume any unused space in the last page of our data segment. */
467 ptr
= (vm_address_t
) &_end
;
468 end
= round_page (ptr
);
471 /* Make sure the allocation pointer is ideally aligned. */
472 ptr
+= sizeof (double) - 1;
473 ptr
&= ~(sizeof (double) - 1);
477 /* Insufficient space left; allocate another page. */
479 assert (n
<= __vm_page_size
);
480 __vm_allocate (__mach_task_self (), &page
, __vm_page_size
, 1);
483 end
= page
+ __vm_page_size
;
486 block
= (void *) ptr
;
493 /* These should never be called. */
494 void *realloc (void *ptr
, size_t n
) { ptr
+= n
; abort (); }
495 void free (void *ptr
) { ptr
= ptr
; abort (); }
496 weak_symbol (realloc
)
499 /* Avoid signal frobnication in setjmp/longjmp. */
501 int __sigjmp_save (sigjmp_buf env
, int savemask
)
502 { env
[0].__mask_was_saved
= savemask
; return 0; }
503 weak_symbol (__sigjmp_save
)
506 longjmp (jmp_buf env
, int val
) { __longjmp (env
[0].__jmpbuf
, val
); }
507 weak_symbol (longjmp
)
510 /* This function is called by interruptible RPC stubs. For initial
511 dynamic linking, just use the normal mach_msg. Since this defn is
512 weak, the real defn in libc.so will override it if we are linked into
513 the user program (-ldl). */
516 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
517 mach_msg_option_t option
,
518 mach_msg_size_t send_size
,
519 mach_msg_size_t rcv_size
,
520 mach_port_t rcv_name
,
521 mach_msg_timeout_t timeout
,
524 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,
527 weak_symbol (_hurd_intr_rpc_mach_msg
)