1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 #include <mach/mig_support.h>
32 #include "hurdstartup.h"
33 #include <hurd/lookup.h>
34 #include <hurd/auth.h>
35 #include <hurd/term.h>
42 #include <dl-machine.h>
43 #include <dl-procinfo.h>
45 extern void __mach_init (void);
48 extern char **_dl_argv
;
49 extern char **_environ
;
51 int __libc_enable_secure
= 0;
52 INTVARDEF(__libc_enable_secure
)
53 int __libc_multiple_libcs
= 0; /* Defining this here avoids the inclusion
55 /* This variable containts the lowest stack address ever used. */
56 void *__libc_stack_end
;
59 hp_timing_t _dl_cpuclock_offset
;
63 struct hurd_startup_data
*_dl_hurd_data
;
65 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
67 int errno attribute_hidden
;
69 /* Defining these variables here avoids the inclusion of hurdsig.c. */
70 unsigned long int __hurd_sigthread_stack_base
;
71 unsigned long int __hurd_sigthread_stack_end
;
72 unsigned long int *__hurd_sigthread_variables
;
74 /* Defining these variables here avoids the inclusion of init-first.c.
75 We need to provide temporary storage for the per-thread variables
76 of the main user thread here, since it is used for storing the
77 `errno' variable. Note that this information is lost once we
78 relocate the dynamic linker. */
79 static unsigned long int threadvars
[_HURD_THREADVAR_MAX
];
80 unsigned long int __hurd_threadvar_stack_offset
81 = (unsigned long int) &threadvars
;
82 unsigned long int __hurd_threadvar_stack_mask
;
84 #define FMH defined(__i386__)
86 # define fmh() ((void)0)
87 # define unfmh() ((void)0)
89 /* XXX loser kludge for vm_map kernel bug */
90 #undef ELF_MACHINE_USER_ADDRESS_MASK
91 #define ELF_MACHINE_USER_ADDRESS_MASK 0
92 static vm_address_t fmha
;
93 static vm_size_t fmhs
;
94 static void unfmh(void){
95 __vm_deallocate(__mach_task_self(),fmha
,fmhs
);}
96 static void fmh(void) {
97 error_t err
;int x
;mach_port_t p
;
98 vm_address_t a
=0x08000000U
,max
=VM_MAX_ADDRESS
;
99 while (!(err
=__vm_region(__mach_task_self(),&a
,&fmhs
,&x
,&x
,&x
,&x
,&p
,&x
))){
100 __mach_port_deallocate(__mach_task_self(),p
);
101 if (a
+fmhs
>=0x80000000U
){
104 if (err
) assert(err
==KERN_NO_SPACE
);
105 if (!fmha
)fmhs
=0;else{
107 err
= __vm_map (__mach_task_self (),
108 &fmha
, fmhs
, 0, 0, MACH_PORT_NULL
, 0, 1,
109 VM_PROT_NONE
, VM_PROT_NONE
, VM_INHERIT_COPY
);
112 /* XXX loser kludge for vm_map kernel bug */
117 _dl_sysdep_start (void **start_argptr
,
118 void (*dl_main
) (const ElfW(Phdr
) *phdr
, ElfW(Word
) phent
,
119 ElfW(Addr
) *user_entry
))
121 void go (intptr_t *argdata
)
123 extern unsigned int _dl_skip_args
; /* rtld.c */
126 /* Cache the information in various global variables. */
128 _dl_argv
= 1 + (char **) argdata
;
129 _environ
= &_dl_argv
[_dl_argc
+ 1];
130 for (p
= _environ
; *p
++;); /* Skip environ pointers and terminator. */
132 if ((void *) p
== _dl_argv
[0])
134 static struct hurd_startup_data nodata
;
135 _dl_hurd_data
= &nodata
;
136 nodata
.user_entry
= (vm_address_t
) ENTRY_POINT
;
139 _dl_hurd_data
= (void *) p
;
141 INTUSE(__libc_enable_secure
) = _dl_hurd_data
->flags
& EXEC_SECURE
;
143 if (_dl_hurd_data
->flags
& EXEC_STACK_ARGS
&&
144 _dl_hurd_data
->user_entry
== 0)
145 _dl_hurd_data
->user_entry
= (vm_address_t
) ENTRY_POINT
;
149 #if 0 /* XXX make this work for real someday... */
150 if (_dl_hurd_data
->user_entry
== (vm_address_t
) ENTRY_POINT
)
151 /* We were invoked as a command, not as the program interpreter.
152 The generic ld.so code supports this: it will parse the args
153 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
154 support an additional special syntax:
155 ld.so [-LIBS...] PROGRAM [ARGS...]
156 Each LIBS word consists of "FILENAME=MEMOBJ";
157 for example "-/lib/libc.so=123" says that the contents of
158 /lib/libc.so are found in a memory object whose port name
159 in our task is 123. */
160 while (_dl_argc
> 2 && _dl_argv
[1][0] == '-' && _dl_argv
[1][1] != '-')
162 char *lastslash
, *memobjname
, *p
;
169 p
= _dl_argv
++[1] + 1;
171 memobjname
= strchr (p
, '=');
173 _dl_sysdep_fatal ("Bogus library spec: ", p
, "\n", NULL
);
174 *memobjname
++ = '\0';
176 while (*memobjname
!= '\0')
177 memobj
= (memobj
* 10) + (*memobjname
++ - '0');
179 /* Add a user reference on the memory object port, so we will
180 still have one after _dl_map_object_from_fd calls our
182 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
183 MACH_PORT_RIGHT_SEND
, +1);
186 lastslash
= strrchr (p
, '/');
187 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : p
,
188 memobj
, strdup (p
), 0);
190 /* Squirrel away the memory object port where it
191 can be retrieved by the program later. */
192 l
->l_info
[DT_NULL
] = (void *) memobj
;
196 /* Call elf/rtld.c's main program. It will set everything
197 up and leave us to transfer control to USER_ENTRY. */
198 (*dl_main
) ((const ElfW(Phdr
) *) _dl_hurd_data
->phdr
,
199 _dl_hurd_data
->phdrsz
/ sizeof (ElfW(Phdr
)),
200 &_dl_hurd_data
->user_entry
);
202 /* The call above might screw a few things up.
204 First of all, if _dl_skip_args is nonzero, we are ignoring
205 the first few arguments. However, if we have no Hurd startup
206 data, it is the magical convention that ARGV[0] == P. The
207 startup code in init-first.c will get confused if this is not
208 the case, so we must rearrange things to make it so. We'll
209 overwrite the origional ARGV[0] at P with ARGV[_dl_skip_args].
211 Secondly, if we need to be secure, it removes some dangerous
212 environment variables. If we have no Hurd startup date this
213 changes P (since that's the location after the terminating
214 NULL in the list of environment variables). We do the same
215 thing as in the first case but make sure we recalculate P.
216 If we do have Hurd startup data, we have to move the data
217 such that it starts just after the terminating NULL in the
220 We use memmove, since the locations might overlap. */
221 if (INTUSE(__libc_enable_secure
) || _dl_skip_args
)
225 for (newp
= _environ
; *newp
++;);
227 if (_dl_argv
[-_dl_skip_args
] == (char *) p
)
229 if ((char *) newp
!= _dl_argv
[0])
231 assert ((char *) newp
< _dl_argv
[0]);
232 _dl_argv
[0] = memmove ((char *) newp
, _dl_argv
[0],
233 strlen (_dl_argv
[0]) + 1);
238 if ((void *) newp
!= _dl_hurd_data
)
239 memmove (newp
, _dl_hurd_data
, sizeof (*_dl_hurd_data
));
244 extern void _dl_start_user (void);
245 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
246 to the RTLD_START code which will run the user's entry point. */
247 RETURN_TO (argdata
, &_dl_start_user
, _dl_hurd_data
->user_entry
);
251 /* Set up so we can do RPCs. */
254 /* Initialize frequently used global variable. */
255 GLRO(dl_pagesize
) = __getpagesize ();
258 HP_TIMING_NOW (_dl_cpuclock_offset
);
263 /* See hurd/hurdstartup.c; this deals with getting information
264 from the exec server and slicing up the arguments.
265 Then it will call `go', above. */
266 _hurd_startup (start_argptr
, &go
);
274 _dl_sysdep_start_cleanup (void)
276 /* Deallocate the reply port and task port rights acquired by
277 __mach_init. We are done with them now, and the user will
278 reacquire them for himself when he wants them. */
279 __mig_dealloc_reply_port (MACH_PORT_NULL
);
280 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
283 /* Minimal open/close/mmap implementation sufficient for initial loading of
284 shared libraries. These are weak definitions so that when the
285 dynamic linker re-relocates itself to be user-visible (for -ldl),
286 it will get the user's definition (i.e. usually libc's). */
288 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or return an
289 error. If STAT is non-zero, stat the file into that stat buffer. */
291 open_file (const char *file_name
, int flags
,
292 mach_port_t
*port
, struct stat64
*stat
)
294 enum retry_type doretry
;
295 char retryname
[1024]; /* XXX string_t LOSES! */
299 error_t
use_init_port (int which
, error_t (*operate
) (file_t
))
301 return (which
< _dl_hurd_data
->portarraysize
302 ? ((*operate
) (_dl_hurd_data
->portarray
[which
]))
305 file_t
get_dtable_port (int fd
)
307 if ((unsigned int) fd
< _dl_hurd_data
->dtablesize
308 && _dl_hurd_data
->dtable
[fd
] != MACH_PORT_NULL
)
310 __mach_port_mod_refs (__mach_task_self (), _dl_hurd_data
->dtable
[fd
],
311 MACH_PORT_RIGHT_SEND
, +1);
312 return _dl_hurd_data
->dtable
[fd
];
315 return MACH_PORT_NULL
;
318 assert (!(flags
& ~O_READ
));
320 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/' ?
321 INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
323 while (file_name
[0] == '/')
326 err
= __dir_lookup (startdir
, (char *)file_name
, O_RDONLY
, 0,
327 &doretry
, retryname
, port
);
330 err
= __hurd_file_name_lookup_retry (use_init_port
, get_dtable_port
,
331 __dir_lookup
, doretry
, retryname
,
335 err
= __io_stat (*port
, stat
);
337 __mach_port_deallocate (__mach_task_self (), *port
);
344 __open (const char *file_name
, int mode
, ...)
347 error_t err
= open_file (file_name
, mode
, &port
, 0);
349 return __hurd_fail (err
);
357 if (fd
!= (int) MACH_PORT_NULL
)
358 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
362 __ssize_t weak_function
363 __libc_read (int fd
, void *buf
, size_t nbytes
)
367 mach_msg_type_number_t nread
;
370 err
= __io_read ((mach_port_t
) fd
, &data
, &nread
, -1, nbytes
);
372 return __hurd_fail (err
);
376 memcpy (buf
, data
, nread
);
377 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, nread
);
382 libc_hidden_weak (__libc_read
)
384 __ssize_t weak_function
385 __libc_write (int fd
, const void *buf
, size_t nbytes
)
388 mach_msg_type_number_t nwrote
;
390 assert (fd
< _hurd_init_dtablesize
);
392 err
= __io_write (_hurd_init_dtable
[fd
], buf
, nbytes
, -1, &nwrote
);
394 return __hurd_fail (err
);
398 libc_hidden_weak (__libc_write
)
400 /* This is only used for printing messages (see dl-misc.c). */
401 __ssize_t weak_function
402 __writev (int fd
, const struct iovec
*iov
, int niov
)
404 if (fd
>= _hurd_init_dtablesize
)
412 for (i
= 0; i
< niov
; ++i
)
413 total
+= iov
[i
].iov_len
;
417 char buf
[total
], *bufp
= buf
;
419 mach_msg_type_number_t nwrote
;
421 for (i
= 0; i
< niov
; ++i
)
422 bufp
= (memcpy (bufp
, iov
[i
].iov_base
, iov
[i
].iov_len
)
425 err
= __io_write (_hurd_init_dtable
[fd
], buf
, total
, -1, &nwrote
);
427 return __hurd_fail (err
);
435 off64_t weak_function
436 __libc_lseek64 (int fd
, off64_t offset
, int whence
)
440 err
= __io_seek ((mach_port_t
) fd
, offset
, whence
, &offset
);
442 return __hurd_fail (err
);
447 __ptr_t weak_function
448 __mmap (__ptr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
452 vm_address_t mapaddr
;
453 mach_port_t memobj_rd
, memobj_wr
;
455 vmprot
= VM_PROT_NONE
;
456 if (prot
& PROT_READ
)
457 vmprot
|= VM_PROT_READ
;
458 if (prot
& PROT_WRITE
)
459 vmprot
|= VM_PROT_WRITE
;
460 if (prot
& PROT_EXEC
)
461 vmprot
|= VM_PROT_EXECUTE
;
463 if (flags
& MAP_ANON
)
464 memobj_rd
= MACH_PORT_NULL
;
467 assert (!(flags
& MAP_SHARED
));
468 err
= __io_map ((mach_port_t
) fd
, &memobj_rd
, &memobj_wr
);
470 return __hurd_fail (err
), MAP_FAILED
;
471 __mach_port_deallocate (__mach_task_self (), memobj_wr
);
474 mapaddr
= (vm_address_t
) addr
;
475 err
= __vm_map (__mach_task_self (),
476 &mapaddr
, (vm_size_t
) len
, ELF_MACHINE_USER_ADDRESS_MASK
,
477 !(flags
& MAP_FIXED
),
479 (vm_offset_t
) offset
,
480 flags
& (MAP_COPY
|MAP_PRIVATE
),
482 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
483 if (err
== KERN_NO_SPACE
&& (flags
& MAP_FIXED
))
485 /* XXX this is not atomic as it is in unix! */
486 /* The region is already allocated; deallocate it first. */
487 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
489 err
= __vm_map (__mach_task_self (),
490 &mapaddr
, (vm_size_t
) len
,
491 ELF_MACHINE_USER_ADDRESS_MASK
,
492 !(flags
& MAP_FIXED
),
493 memobj_rd
, (vm_offset_t
) offset
,
494 flags
& (MAP_COPY
|MAP_PRIVATE
),
497 ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
500 if ((flags
& MAP_ANON
) == 0)
501 __mach_port_deallocate (__mach_task_self (), memobj_rd
);
504 return __hurd_fail (err
), MAP_FAILED
;
505 return (__ptr_t
) mapaddr
;
509 __fxstat64 (int vers
, int fd
, struct stat64
*buf
)
513 assert (vers
== _STAT_VER
);
515 err
= __io_stat ((mach_port_t
) fd
, buf
);
517 return __hurd_fail (err
);
521 libc_hidden_def (__fxstat64
)
524 __xstat64 (int vers
, const char *file
, struct stat64
*buf
)
529 assert (vers
== _STAT_VER
);
531 err
= open_file (file
, 0, &port
, buf
);
533 return __hurd_fail (err
);
535 __mach_port_deallocate (__mach_task_self (), port
);
539 libc_hidden_def (__xstat64
)
541 /* This function is called by the dynamic linker (rtld.c) to check
542 whether debugging malloc is allowed even for SUID binaries. This
543 stub will always fail, which means that malloc-debugging is always
544 disabled for SUID binaries. */
546 __access (const char *file
, int type
)
558 if (__proc_getpids (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
559 &pid
, &ppid
, &orphaned
))
565 /* This is called only in some strange cases trying to guess a value
566 for $ORIGIN for the executable. The dynamic linker copes with
567 getcwd failing (dl-object.c), and it's too much hassle to include
568 the functionality here. (We could, it just requires duplicating or
569 reusing getcwd.c's code but using our special lookup function as in
573 __getcwd (char *buf
, size_t size
)
579 void weak_function attribute_hidden
582 __proc_mark_exit (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
583 W_EXITCODE (status
, 0), 0);
584 while (__task_terminate (__mach_task_self ()))
585 __mach_task_self_
= (__mach_task_self
) ();
587 /* We need this alias to satisfy references from libc_pic.a objects
588 that were affected by the libc_hidden_proto declaration for _exit. */
589 strong_alias (_exit
, __GI__exit
)
591 /* Try to get a machine dependent instruction which will make the
592 program crash. This is used in case everything else fails. */
593 #include <abort-instr.h>
594 #ifndef ABORT_INSTRUCTION
595 /* No such instruction is available. */
596 # define ABORT_INSTRUCTION
602 /* Try to abort using the system specific command. */
605 /* If the abort instruction failed, exit. */
608 /* If even this fails, make sure we never return. */
610 /* Try for ever and ever. */
614 /* We need this alias to satisfy references from libc_pic.a objects
615 that were affected by the libc_hidden_proto declaration for abort. */
616 strong_alias (abort
, __GI_abort
)
618 /* This function is called by interruptible RPC stubs. For initial
619 dynamic linking, just use the normal mach_msg. Since this defn is
620 weak, the real defn in libc.so will override it if we are linked into
621 the user program (-ldl). */
623 error_t weak_function
624 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
625 mach_msg_option_t option
,
626 mach_msg_size_t send_size
,
627 mach_msg_size_t rcv_size
,
628 mach_port_t rcv_name
,
629 mach_msg_timeout_t timeout
,
632 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,
641 /* There is nothing to print. Hurd has no auxiliary vector. */
645 /* Return an array of useful/necessary hardware capability names. */
646 const struct r_strlenpair
*
648 _dl_important_hwcaps (const char *platform
, size_t platform_len
, size_t *sz
,
649 size_t *max_capstrlen
)
651 struct r_strlenpair
*result
;
653 /* Return an empty array. Hurd has no hardware capabilities. */
654 result
= (struct r_strlenpair
*) malloc (sizeof (*result
));
656 _dl_signal_error (ENOMEM
, NULL
, NULL
, "cannot create capability list");
658 result
[0].str
= (char *) result
; /* Does not really matter. */
666 _dl_init_first (int argc
, ...)
668 /* This no-op definition only gets used if libc is not linked in. */