1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995-2012 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
29 #include <mach/mig_support.h>
30 #include "hurdstartup.h"
31 #include <hurd/lookup.h>
32 #include <hurd/auth.h>
33 #include <hurd/term.h>
40 #include <dl-machine.h>
41 #include <dl-procinfo.h>
43 extern void __mach_init (void);
46 extern char **_dl_argv
;
47 extern char **_environ
;
49 int __libc_enable_secure
= 0;
50 INTVARDEF(__libc_enable_secure
)
51 int __libc_multiple_libcs
= 0; /* Defining this here avoids the inclusion
53 /* This variable containts the lowest stack address ever used. */
54 void *__libc_stack_end
;
57 hp_timing_t _dl_cpuclock_offset
;
61 struct hurd_startup_data
*_dl_hurd_data
;
63 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
65 int errno attribute_hidden
;
67 /* Defining these variables here avoids the inclusion of hurdsig.c. */
68 unsigned long int __hurd_sigthread_stack_base
;
69 unsigned long int __hurd_sigthread_stack_end
;
70 unsigned long int *__hurd_sigthread_variables
;
72 /* Defining these variables here avoids the inclusion of init-first.c.
73 We need to provide temporary storage for the per-thread variables
74 of the main user thread here, since it is used for storing the
75 `errno' variable. Note that this information is lost once we
76 relocate the dynamic linker. */
77 static unsigned long int threadvars
[_HURD_THREADVAR_MAX
];
78 unsigned long int __hurd_threadvar_stack_offset
79 = (unsigned long int) &threadvars
;
80 unsigned long int __hurd_threadvar_stack_mask
;
82 #define FMH defined(__i386__)
84 # define fmh() ((void)0)
85 # define unfmh() ((void)0)
87 /* XXX loser kludge for vm_map kernel bug */
88 #undef ELF_MACHINE_USER_ADDRESS_MASK
89 #define ELF_MACHINE_USER_ADDRESS_MASK 0
90 static vm_address_t fmha
;
91 static vm_size_t fmhs
;
92 static void unfmh(void){
93 __vm_deallocate(__mach_task_self(),fmha
,fmhs
);}
94 static void fmh(void) {
95 error_t err
;int x
;mach_port_t p
;
96 vm_address_t a
=0x08000000U
,max
=VM_MAX_ADDRESS
;
97 while (!(err
=__vm_region(__mach_task_self(),&a
,&fmhs
,&x
,&x
,&x
,&x
,&p
,&x
))){
98 __mach_port_deallocate(__mach_task_self(),p
);
99 if (a
+fmhs
>=0x80000000U
){
102 if (err
) assert(err
==KERN_NO_SPACE
);
103 if (!fmha
)fmhs
=0;else{
105 err
= __vm_map (__mach_task_self (),
106 &fmha
, fmhs
, 0, 0, MACH_PORT_NULL
, 0, 1,
107 VM_PROT_NONE
, VM_PROT_NONE
, VM_INHERIT_COPY
);
110 /* XXX loser kludge for vm_map kernel bug */
115 _dl_sysdep_start (void **start_argptr
,
116 void (*dl_main
) (const ElfW(Phdr
) *phdr
, ElfW(Word
) phent
,
117 ElfW(Addr
) *user_entry
,
120 void go (intptr_t *argdata
)
122 extern unsigned int _dl_skip_args
; /* rtld.c */
125 /* Cache the information in various global variables. */
127 _dl_argv
= 1 + (char **) argdata
;
128 _environ
= &_dl_argv
[_dl_argc
+ 1];
129 for (p
= _environ
; *p
++;); /* Skip environ pointers and terminator. */
131 if ((void *) p
== _dl_argv
[0])
133 static struct hurd_startup_data nodata
;
134 _dl_hurd_data
= &nodata
;
135 nodata
.user_entry
= (vm_address_t
) ENTRY_POINT
;
138 _dl_hurd_data
= (void *) p
;
140 INTUSE(__libc_enable_secure
) = _dl_hurd_data
->flags
& EXEC_SECURE
;
142 if (_dl_hurd_data
->flags
& EXEC_STACK_ARGS
&&
143 _dl_hurd_data
->user_entry
== 0)
144 _dl_hurd_data
->user_entry
= (vm_address_t
) ENTRY_POINT
;
148 #if 0 /* XXX make this work for real someday... */
149 if (_dl_hurd_data
->user_entry
== (vm_address_t
) ENTRY_POINT
)
150 /* We were invoked as a command, not as the program interpreter.
151 The generic ld.so code supports this: it will parse the args
152 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
153 support an additional special syntax:
154 ld.so [-LIBS...] PROGRAM [ARGS...]
155 Each LIBS word consists of "FILENAME=MEMOBJ";
156 for example "-/lib/libc.so=123" says that the contents of
157 /lib/libc.so are found in a memory object whose port name
158 in our task is 123. */
159 while (_dl_argc
> 2 && _dl_argv
[1][0] == '-' && _dl_argv
[1][1] != '-')
161 char *lastslash
, *memobjname
, *p
;
168 p
= _dl_argv
++[1] + 1;
170 memobjname
= strchr (p
, '=');
172 _dl_sysdep_fatal ("Bogus library spec: ", p
, "\n", NULL
);
173 *memobjname
++ = '\0';
175 while (*memobjname
!= '\0')
176 memobj
= (memobj
* 10) + (*memobjname
++ - '0');
178 /* Add a user reference on the memory object port, so we will
179 still have one after _dl_map_object_from_fd calls our
181 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
182 MACH_PORT_RIGHT_SEND
, +1);
185 lastslash
= strrchr (p
, '/');
186 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : p
,
187 memobj
, strdup (p
), 0);
189 /* Squirrel away the memory object port where it
190 can be retrieved by the program later. */
191 l
->l_info
[DT_NULL
] = (void *) memobj
;
195 /* Call elf/rtld.c's main program. It will set everything
196 up and leave us to transfer control to USER_ENTRY. */
197 (*dl_main
) ((const ElfW(Phdr
) *) _dl_hurd_data
->phdr
,
198 _dl_hurd_data
->phdrsz
/ sizeof (ElfW(Phdr
)),
199 &_dl_hurd_data
->user_entry
, NULL
);
201 /* The call above might screw a few things up.
203 First of all, if _dl_skip_args is nonzero, we are ignoring
204 the first few arguments. However, if we have no Hurd startup
205 data, it is the magical convention that ARGV[0] == P. The
206 startup code in init-first.c will get confused if this is not
207 the case, so we must rearrange things to make it so. We'll
208 overwrite the origional ARGV[0] at P with ARGV[_dl_skip_args].
210 Secondly, if we need to be secure, it removes some dangerous
211 environment variables. If we have no Hurd startup date this
212 changes P (since that's the location after the terminating
213 NULL in the list of environment variables). We do the same
214 thing as in the first case but make sure we recalculate P.
215 If we do have Hurd startup data, we have to move the data
216 such that it starts just after the terminating NULL in the
219 We use memmove, since the locations might overlap. */
220 if (INTUSE(__libc_enable_secure
) || _dl_skip_args
)
224 for (newp
= _environ
; *newp
++;);
226 if (_dl_argv
[-_dl_skip_args
] == (char *) p
)
228 if ((char *) newp
!= _dl_argv
[0])
230 assert ((char *) newp
< _dl_argv
[0]);
231 _dl_argv
[0] = memmove ((char *) newp
, _dl_argv
[0],
232 strlen (_dl_argv
[0]) + 1);
237 if ((void *) newp
!= _dl_hurd_data
)
238 memmove (newp
, _dl_hurd_data
, sizeof (*_dl_hurd_data
));
243 extern void _dl_start_user (void);
244 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
245 to the RTLD_START code which will run the user's entry point. */
246 RETURN_TO (argdata
, &_dl_start_user
, _dl_hurd_data
->user_entry
);
250 /* Set up so we can do RPCs. */
253 /* Initialize frequently used global variable. */
254 GLRO(dl_pagesize
) = __getpagesize ();
257 HP_TIMING_NOW (_dl_cpuclock_offset
);
262 /* See hurd/hurdstartup.c; this deals with getting information
263 from the exec server and slicing up the arguments.
264 Then it will call `go', above. */
265 _hurd_startup (start_argptr
, &go
);
273 _dl_sysdep_start_cleanup (void)
275 /* Deallocate the reply port and task port rights acquired by
276 __mach_init. We are done with them now, and the user will
277 reacquire them for himself when he wants them. */
278 __mig_dealloc_reply_port (MACH_PORT_NULL
);
279 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
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 it will get the user's definition (i.e. usually libc's). */
287 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or return an
288 error. If STAT is non-zero, stat the file into that stat buffer. */
290 open_file (const char *file_name
, int flags
,
291 mach_port_t
*port
, struct stat64
*stat
)
293 enum retry_type doretry
;
294 char retryname
[1024]; /* XXX string_t LOSES! */
298 error_t
use_init_port (int which
, error_t (*operate
) (file_t
))
300 return (which
< _dl_hurd_data
->portarraysize
301 ? ((*operate
) (_dl_hurd_data
->portarray
[which
]))
304 file_t
get_dtable_port (int fd
)
306 if ((unsigned int) fd
< _dl_hurd_data
->dtablesize
307 && _dl_hurd_data
->dtable
[fd
] != MACH_PORT_NULL
)
309 __mach_port_mod_refs (__mach_task_self (), _dl_hurd_data
->dtable
[fd
],
310 MACH_PORT_RIGHT_SEND
, +1);
311 return _dl_hurd_data
->dtable
[fd
];
314 return MACH_PORT_NULL
;
317 assert (!(flags
& ~(O_READ
| O_CLOEXEC
)));
319 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/' ?
320 INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
322 while (file_name
[0] == '/')
325 err
= __dir_lookup (startdir
, (char *)file_name
, O_RDONLY
, 0,
326 &doretry
, retryname
, port
);
329 err
= __hurd_file_name_lookup_retry (use_init_port
, get_dtable_port
,
330 __dir_lookup
, doretry
, retryname
,
334 err
= __io_stat (*port
, stat
);
336 __mach_port_deallocate (__mach_task_self (), *port
);
343 __open (const char *file_name
, int mode
, ...)
346 error_t err
= open_file (file_name
, mode
, &port
, 0);
348 return __hurd_fail (err
);
356 if (fd
!= (int) MACH_PORT_NULL
)
357 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
361 __ssize_t weak_function
362 __libc_read (int fd
, void *buf
, size_t nbytes
)
366 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. */