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-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
;
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
;
107 if (_dl_hurd_data
->flags
& EXEC_STACK_ARGS
&&
108 _dl_hurd_data
->user_entry
== 0)
109 _dl_hurd_data
->user_entry
= (vm_address_t
) &_start
;
113 if (_dl_hurd_data
->user_entry
== (vm_address_t
) &_start
)
114 /* We were invoked as a command, not as the program interpreter.
115 The generic ld.so code supports this: it will parse the args
116 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
117 support an additional special syntax:
118 ld.so [-LIBS...] PROGRAM [ARGS...]
119 Each LIBS word consists of "FILENAME=MEMOBJ";
120 for example "-/lib/libc.so=123" says that the contents of
121 /lib/libc.so are found in a memory object whose port name
122 in our task is 123. */
123 while (_dl_argc
> 2 && _dl_argv
[1][0] == '-' && _dl_argv
[1][1] != '-')
125 char *lastslash
, *memobjname
, *p
;
132 p
= _dl_argv
++[1] + 1;
134 memobjname
= strchr (p
, '=');
136 _dl_sysdep_fatal ("Bogus library spec: ", p
, "\n", NULL
);
137 *memobjname
++ = '\0';
138 memobj
= (mach_port_t
) atoi (memobjname
);
140 /* Add a user reference on the memory object port, so we will
141 still have one after _dl_map_object_from_fd calls our
143 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
144 MACH_PORT_RIGHT_SEND
, +1);
147 lastslash
= strrchr (p
, '/');
148 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : p
,
151 /* Squirrel away the memory object port where it
152 can be retrieved by the program later. */
153 l
->l_info
[DT_NULL
] = (void *) memobj
;
156 /* Call elf/rtld.c's main program. It will set everything
157 up and leave us to transfer control to USER_ENTRY. */
158 (*dl_main
) ((const Elf32_Phdr
*) _dl_hurd_data
->phdr
,
159 _dl_hurd_data
->phdrsz
/ sizeof (Elf32_Phdr
),
160 &_dl_hurd_data
->user_entry
);
162 /* Deallocate the reply port and task port rights acquired by
163 __mach_init. We are done with them now, and the user will
164 reacquire them for himself when he wants them. */
165 __mig_dealloc_reply_port (MACH_PORT_NULL
);
166 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
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_open_zero_fill (void)
205 return (int) MACH_PORT_NULL
;
210 _dl_sysdep_fatal (const char *msg
, ...)
217 size_t len
= strlen (msg
);
218 mach_msg_type_number_t nwrote
;
221 if (__io_write (_hurd_init_dtable
[2], msg
, len
, -1, &nwrote
))
225 } while (nwrote
> 0);
226 msg
= va_arg (ap
, const char *);
235 _dl_sysdep_message (const char *msg
, ...)
242 size_t len
= strlen (msg
);
243 mach_msg_type_number_t nwrote
;
246 if (__io_write (_hurd_init_dtable
[1], msg
, len
, -1, &nwrote
))
250 } while (nwrote
> 0);
251 msg
= va_arg (ap
, const char *);
256 /* Minimal open/close/mmap implementation sufficient for initial loading of
257 shared libraries. These are weak definitions so that when the
258 dynamic linker re-relocates itself to be user-visible (for -ldl),
259 it will get the user's definition (i.e. usually libc's). */
262 open (const char *file_name
, int mode
, ...)
264 enum retry_type doretry
;
265 char retryname
[1024]; /* XXX string_t LOSES! */
266 file_t startdir
, newpt
, fileport
;
271 assert (mode
== O_RDONLY
);
273 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/' ?
274 INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
276 while (file_name
[0] == '/')
279 if (err
= __dir_lookup (startdir
, file_name
, mode
, 0,
280 &doretry
, retryname
, &fileport
))
281 return __hurd_fail (err
);
289 __mach_port_deallocate (__mach_task_self (), startdir
);
291 return __hurd_fail (err
);
295 case FS_RETRY_REAUTH
:
297 mach_port_t ref
= __mach_reply_port ();
298 err
= __io_reauthenticate (fileport
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
300 err
= __auth_user_authenticate
301 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
303 ref
, MACH_MSG_TYPE_MAKE_SEND
,
305 __mach_port_destroy (__mach_task_self (), ref
);
307 __mach_port_deallocate (__mach_task_self (), fileport
);
309 return __hurd_fail (err
);
313 case FS_RETRY_NORMAL
:
315 if (nloops
++ >= SYMLOOP_MAX
)
316 return __hurd_fail (ELOOP
);
319 /* An empty RETRYNAME indicates we have the final port. */
320 if (retryname
[0] == '\0')
322 mach_port_t memobj_rd
, memobj_wr
;
327 /* We have the file open. Now map it. */
328 err
= __io_map (fileport
, &memobj_rd
, &memobj_wr
);
330 __mach_port_deallocate (__mach_task_self (), fileport
);
332 return __hurd_fail (err
);
333 if (memobj_wr
!= MACH_PORT_NULL
)
334 __mach_port_deallocate (__mach_task_self (), memobj_wr
);
336 return (int) memobj_rd
;
341 file_name
= retryname
;
344 case FS_RETRY_MAGICAL
:
345 switch (retryname
[0])
348 startdir
= _dl_hurd_data
->portarray
[INIT_PORT_CRDIR
];
350 if (fileport
!= MACH_PORT_NULL
)
351 __mach_port_deallocate (__mach_task_self (), fileport
);
352 file_name
= &retryname
[1];
356 if (retryname
[1] == 'd' && retryname
[2] == '/')
361 fd
= (int) strtol (retryname
, &end
, 10);
362 if (end
== NULL
|| err
|| /* Malformed number. */
363 /* Check for excess text after the number. A slash
364 is valid; it ends the component. Anything else
365 does not name a numeric file descriptor. */
366 (*end
!= '/' && *end
!= '\0'))
367 return __hurd_fail (ENOENT
);
368 if (fd
< 0 || fd
>= _dl_hurd_data
->dtablesize
||
369 _dl_hurd_data
->dtable
[fd
] == MACH_PORT_NULL
)
370 /* If the name was a proper number, but the file
371 descriptor does not exist, we return EBADF instead
373 return __hurd_fail (EBADF
);
374 fileport
= _dl_hurd_data
->dtable
[fd
];
377 /* This descriptor is the file port we want. */
383 /* Do a normal retry on the remaining components. */
386 file_name
= end
+ 1; /* Skip the slash. */
395 if (retryname
[1] == 'a' && retryname
[2] == 'c' &&
396 retryname
[3] == 'h' && retryname
[4] == 't' &&
397 retryname
[5] == 'y' && retryname
[6] == 'p' &&
401 struct host_basic_info hostinfo
;
402 mach_msg_type_number_t hostinfocnt
= HOST_BASIC_INFO_COUNT
;
404 if (err
= __host_info (__mach_host_self (), HOST_BASIC_INFO
,
405 (natural_t
*) &hostinfo
,
408 if (hostinfocnt
!= HOST_BASIC_INFO_COUNT
)
410 p
= _itoa (hostinfo
.cpu_subtype
, &retryname
[8], 10, 0);
412 p
= _itoa (hostinfo
.cpu_type
, &retryname
[8], 10, 0);
414 abort (); /* XXX write this right if this ever happens */
416 strcpy (retryname
, p
);
425 if (retryname
[1] == 't' && retryname
[2] == 'y')
426 switch (retryname
[3])
428 error_t
opentty (file_t
*result
)
432 err
= __termctty_open_terminal
433 (_dl_hurd_data
->portarray
[INIT_PORT_CTTYID
],
437 mach_port_t ref
= __mach_reply_port ();
438 err
= __io_reauthenticate
439 (unauth
, ref
, MACH_MSG_TYPE_MAKE_SEND
);
441 err
= __auth_user_authenticate
442 (_dl_hurd_data
->portarray
[INIT_PORT_AUTH
],
444 ref
, MACH_MSG_TYPE_MAKE_SEND
,
446 __mach_port_deallocate (__mach_task_self (),
448 __mach_port_destroy (__mach_task_self (), ref
);
454 if (err
= opentty (&fileport
))
455 return __hurd_fail (err
);
459 if (err
= opentty (&startdir
))
460 return __hurd_fail (err
);
462 strcpy (retryname
, &retryname
[4]);
473 return __hurd_fail (EGRATUITOUS
);
478 return __hurd_fail (EGRATUITOUS
);
481 err
= __dir_lookup (startdir
, file_name
, mode
, 0,
482 &doretry
, retryname
, &fileport
);
489 if (fd
!= (int) MACH_PORT_NULL
)
490 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
495 mmap (caddr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
499 vm_address_t mapaddr
;
501 vmprot
= VM_PROT_NONE
;
502 if (prot
& PROT_READ
)
503 vmprot
|= VM_PROT_READ
;
504 if (prot
& PROT_WRITE
)
505 vmprot
|= VM_PROT_WRITE
;
506 if (prot
& PROT_EXEC
)
507 vmprot
|= VM_PROT_EXECUTE
;
509 mapaddr
= (vm_address_t
) addr
;
510 err
= __vm_map (__mach_task_self (),
511 &mapaddr
, (vm_size_t
) len
, 0 /*ELF_MACHINE_USER_ADDRESS_MASK*/,
512 !(flags
& MAP_FIXED
),
513 (mach_port_t
) fd
, (vm_offset_t
) offset
,
514 flags
& (MAP_COPY
|MAP_PRIVATE
),
516 (flags
& MAP_INHERIT
) ? VM_INHERIT_COPY
: VM_INHERIT_NONE
);
517 return err
? (caddr_t
) __hurd_fail (err
) : (caddr_t
) mapaddr
;
523 __proc_mark_exit (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
524 W_EXITCODE (status
, 0));
525 while (__task_terminate (__mach_task_self ()))
526 __mach_task_self_
= (__mach_task_self
) ();
534 /* Minimal `malloc' allocator for use while loading shared libraries.
535 Only small blocks are allocated, and none are ever freed. */
540 static vm_address_t ptr
, end
;
545 /* Consume any unused space in the last page of our data segment. */
547 ptr
= (vm_address_t
) &_end
;
548 end
= round_page (ptr
);
551 /* Make sure the allocation pointer is ideally aligned. */
552 ptr
+= sizeof (double) - 1;
553 ptr
&= ~(sizeof (double) - 1);
557 /* Insufficient space left; allocate another page. */
559 assert (n
<= __vm_page_size
);
560 __vm_allocate (__mach_task_self (), &page
, __vm_page_size
, 1);
563 end
= page
+ __vm_page_size
;
566 block
= (void *) ptr
;
573 /* These should never be called. */
574 void *realloc (void *ptr
, size_t n
) { ptr
+= n
; abort (); }
575 void free (void *ptr
) { ptr
= ptr
; abort (); }
576 weak_symbol (realloc
)
579 /* Avoid signal frobnication in setjmp/longjmp. */
581 int __sigjmp_save (sigjmp_buf env
, int savemask
)
582 { env
[0].__mask_was_saved
= savemask
; return 0; }
583 weak_symbol (__sigjmp_save
)
586 longjmp (jmp_buf env
, int val
) { __longjmp (env
[0].__jmpbuf
, val
); }
587 weak_symbol (longjmp
)
590 /* This function is called by interruptible RPC stubs. For initial
591 dynamic linking, just use the normal mach_msg. Since this defn is
592 weak, the real defn in libc.so will override it if we are linked into
593 the user program (-ldl). */
596 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
597 mach_msg_option_t option
,
598 mach_msg_size_t send_size
,
599 mach_msg_size_t rcv_size
,
600 mach_port_t rcv_name
,
601 mach_msg_timeout_t timeout
,
604 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,
607 weak_symbol (_hurd_intr_rpc_mach_msg
)