1 /* Operating system support for run-time dynamic linker. Hurd version.
2 Copyright (C) 1995-2016 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/>. */
19 /* In the static library, this is all handled by dl-support.c
20 or by the vanilla definitions in the rest of the C library. */
33 #include <mach/mig_support.h>
34 #include "hurdstartup.h"
35 #include <hurd/lookup.h>
36 #include <hurd/auth.h>
37 #include <hurd/term.h>
44 #include <dl-machine.h>
45 #include <dl-procinfo.h>
47 extern void __mach_init (void);
50 extern char **_dl_argv
;
51 extern char **_environ
;
53 int __libc_enable_secure
= 0;
54 rtld_hidden_data_def (__libc_enable_secure
)
55 int __libc_multiple_libcs
= 0; /* Defining this here avoids the inclusion
57 /* This variable contains the lowest stack address ever used. */
58 void *__libc_stack_end
= NULL
;
59 rtld_hidden_data_def(__libc_stack_end
)
62 hp_timing_t _dl_cpuclock_offset
;
66 struct hurd_startup_data
*_dl_hurd_data
;
68 /* This is used only within ld.so, via dl-minimal.c's __errno_location. */
70 int errno attribute_hidden
;
72 /* Defining these variables here avoids the inclusion of hurdsig.c. */
73 unsigned long int __hurd_sigthread_stack_base
;
74 unsigned long int __hurd_sigthread_stack_end
;
75 unsigned long int *__hurd_sigthread_variables
;
77 /* Defining these variables here avoids the inclusion of init-first.c.
78 We need to provide temporary storage for the per-thread variables
79 of the main user thread here, since it is used for storing the
80 `errno' variable. Note that this information is lost once we
81 relocate the dynamic linker. */
82 static unsigned long int threadvars
[_HURD_THREADVAR_MAX
];
83 unsigned long int __hurd_threadvar_stack_offset
84 = (unsigned long int) &threadvars
;
85 unsigned long int __hurd_threadvar_stack_mask
;
87 #define FMH defined(__i386__)
89 # define fmh() ((void)0)
90 # define unfmh() ((void)0)
92 /* XXX loser kludge for vm_map kernel bug */
93 #undef ELF_MACHINE_USER_ADDRESS_MASK
94 #define ELF_MACHINE_USER_ADDRESS_MASK 0
95 static vm_address_t fmha
;
96 static vm_size_t fmhs
;
97 static void unfmh(void){
98 __vm_deallocate(__mach_task_self(),fmha
,fmhs
);}
99 static void fmh(void) {
100 error_t err
;int x
;vm_offset_t o
;mach_port_t p
;
101 vm_address_t a
=0x08000000U
,max
=VM_MAX_ADDRESS
;
102 while (!(err
=__vm_region(__mach_task_self(),&a
,&fmhs
,&x
,&x
,&x
,&x
,&p
,&o
))){
103 __mach_port_deallocate(__mach_task_self(),p
);
104 if (a
+fmhs
>=0x80000000U
){
107 if (err
) assert(err
==KERN_NO_SPACE
);
108 if (!fmha
)fmhs
=0;else{
110 err
= __vm_map (__mach_task_self (),
111 &fmha
, fmhs
, 0, 0, MACH_PORT_NULL
, 0, 1,
112 VM_PROT_NONE
, VM_PROT_NONE
, VM_INHERIT_COPY
);
115 /* XXX loser kludge for vm_map kernel bug */
120 _dl_sysdep_start (void **start_argptr
,
121 void (*dl_main
) (const ElfW(Phdr
) *phdr
, ElfW(Word
) phent
,
122 ElfW(Addr
) *user_entry
,
125 void go (intptr_t *argdata
)
129 /* Cache the information in various global variables. */
131 _dl_argv
= 1 + (char **) argdata
;
132 _environ
= &_dl_argv
[_dl_argc
+ 1];
133 for (p
= _environ
; *p
++;); /* Skip environ pointers and terminator. */
135 if ((void *) p
== _dl_argv
[0])
137 static struct hurd_startup_data nodata
;
138 _dl_hurd_data
= &nodata
;
139 nodata
.user_entry
= (vm_address_t
) ENTRY_POINT
;
142 _dl_hurd_data
= (void *) p
;
144 __libc_enable_secure
= _dl_hurd_data
->flags
& EXEC_SECURE
;
146 if (_dl_hurd_data
->flags
& EXEC_STACK_ARGS
&&
147 _dl_hurd_data
->user_entry
== 0)
148 _dl_hurd_data
->user_entry
= (vm_address_t
) ENTRY_POINT
;
152 #if 0 /* XXX make this work for real someday... */
153 if (_dl_hurd_data
->user_entry
== (vm_address_t
) ENTRY_POINT
)
154 /* We were invoked as a command, not as the program interpreter.
155 The generic ld.so code supports this: it will parse the args
156 as "ld.so PROGRAM [ARGS...]". For booting the Hurd, we
157 support an additional special syntax:
158 ld.so [-LIBS...] PROGRAM [ARGS...]
159 Each LIBS word consists of "FILENAME=MEMOBJ";
160 for example "-/lib/libc.so=123" says that the contents of
161 /lib/libc.so are found in a memory object whose port name
162 in our task is 123. */
163 while (_dl_argc
> 2 && _dl_argv
[1][0] == '-' && _dl_argv
[1][1] != '-')
165 char *lastslash
, *memobjname
, *p
;
172 p
= _dl_argv
++[1] + 1;
174 memobjname
= strchr (p
, '=');
176 _dl_sysdep_fatal ("Bogus library spec: ", p
, "\n", NULL
);
177 *memobjname
++ = '\0';
179 while (*memobjname
!= '\0')
180 memobj
= (memobj
* 10) + (*memobjname
++ - '0');
182 /* Add a user reference on the memory object port, so we will
183 still have one after _dl_map_object_from_fd calls our
185 err
= __mach_port_mod_refs (__mach_task_self (), memobj
,
186 MACH_PORT_RIGHT_SEND
, +1);
189 lastslash
= strrchr (p
, '/');
190 l
= _dl_map_object_from_fd (lastslash
? lastslash
+ 1 : p
, NULL
,
191 memobj
, strdup (p
), 0);
193 /* Squirrel away the memory object port where it
194 can be retrieved by the program later. */
195 l
->l_info
[DT_NULL
] = (void *) memobj
;
199 /* Call elf/rtld.c's main program. It will set everything
200 up and leave us to transfer control to USER_ENTRY. */
201 (*dl_main
) ((const ElfW(Phdr
) *) _dl_hurd_data
->phdr
,
202 _dl_hurd_data
->phdrsz
/ sizeof (ElfW(Phdr
)),
203 &_dl_hurd_data
->user_entry
, NULL
);
205 /* The call above might screw a few things up.
207 First of all, if _dl_skip_args is nonzero, we are ignoring
208 the first few arguments. However, if we have no Hurd startup
209 data, it is the magical convention that ARGV[0] == P. The
210 startup code in init-first.c will get confused if this is not
211 the case, so we must rearrange things to make it so. We'll
212 overwrite the origional ARGV[0] at P with ARGV[_dl_skip_args].
214 Secondly, if we need to be secure, it removes some dangerous
215 environment variables. If we have no Hurd startup date this
216 changes P (since that's the location after the terminating
217 NULL in the list of environment variables). We do the same
218 thing as in the first case but make sure we recalculate P.
219 If we do have Hurd startup data, we have to move the data
220 such that it starts just after the terminating NULL in the
223 We use memmove, since the locations might overlap. */
224 if (__libc_enable_secure
|| _dl_skip_args
)
228 for (newp
= _environ
; *newp
++;);
230 if (_dl_argv
[-_dl_skip_args
] == (char *) p
)
232 if ((char *) newp
!= _dl_argv
[0])
234 assert ((char *) newp
< _dl_argv
[0]);
235 _dl_argv
[0] = memmove ((char *) newp
, _dl_argv
[0],
236 strlen (_dl_argv
[0]) + 1);
241 if ((void *) newp
!= _dl_hurd_data
)
242 memmove (newp
, _dl_hurd_data
, sizeof (*_dl_hurd_data
));
247 extern void _dl_start_user (void);
248 /* Unwind the stack to ARGDATA and simulate a return from _dl_start
249 to the RTLD_START code which will run the user's entry point. */
250 RETURN_TO (argdata
, &_dl_start_user
, _dl_hurd_data
->user_entry
);
254 /* Set up so we can do RPCs. */
257 /* Initialize frequently used global variable. */
258 GLRO(dl_pagesize
) = __getpagesize ();
261 HP_TIMING_NOW (_dl_cpuclock_offset
);
266 /* See hurd/hurdstartup.c; this deals with getting information
267 from the exec server and slicing up the arguments.
268 Then it will call `go', above. */
269 _hurd_startup (start_argptr
, &go
);
277 _dl_sysdep_start_cleanup (void)
279 /* Deallocate the reply port and task port rights acquired by
280 __mach_init. We are done with them now, and the user will
281 reacquire them for himself when he wants them. */
282 __mig_dealloc_reply_port (MACH_PORT_NULL
);
283 __mach_port_deallocate (__mach_task_self (), __mach_host_self_
);
284 __mach_port_deallocate (__mach_task_self (), __mach_task_self_
);
287 /* Minimal open/close/mmap implementation sufficient for initial loading of
288 shared libraries. These are weak definitions so that when the
289 dynamic linker re-relocates itself to be user-visible (for -ldl),
290 it will get the user's definition (i.e. usually libc's). */
292 /* Open FILE_NAME and return a Hurd I/O for it in *PORT, or return an
293 error. If STAT is non-zero, stat the file into that stat buffer. */
295 open_file (const char *file_name
, int flags
,
296 mach_port_t
*port
, struct stat64
*stat
)
298 enum retry_type doretry
;
299 char retryname
[1024]; /* XXX string_t LOSES! */
303 error_t
use_init_port (int which
, error_t (*operate
) (file_t
))
305 return (which
< _dl_hurd_data
->portarraysize
306 ? ((*operate
) (_dl_hurd_data
->portarray
[which
]))
309 file_t
get_dtable_port (int fd
)
311 if ((unsigned int) fd
< _dl_hurd_data
->dtablesize
312 && _dl_hurd_data
->dtable
[fd
] != MACH_PORT_NULL
)
314 __mach_port_mod_refs (__mach_task_self (), _dl_hurd_data
->dtable
[fd
],
315 MACH_PORT_RIGHT_SEND
, +1);
316 return _dl_hurd_data
->dtable
[fd
];
319 return MACH_PORT_NULL
;
322 assert (!(flags
& ~(O_READ
| O_CLOEXEC
)));
324 startdir
= _dl_hurd_data
->portarray
[file_name
[0] == '/' ?
325 INIT_PORT_CRDIR
: INIT_PORT_CWDIR
];
327 while (file_name
[0] == '/')
330 err
= __dir_lookup (startdir
, (char *)file_name
, O_RDONLY
, 0,
331 &doretry
, retryname
, port
);
334 err
= __hurd_file_name_lookup_retry (use_init_port
, get_dtable_port
,
335 __dir_lookup
, doretry
, retryname
,
339 err
= __io_stat (*port
, stat
);
341 __mach_port_deallocate (__mach_task_self (), *port
);
348 __open (const char *file_name
, int mode
, ...)
351 error_t err
= open_file (file_name
, mode
, &port
, 0);
353 return __hurd_fail (err
);
361 if (fd
!= (int) MACH_PORT_NULL
)
362 __mach_port_deallocate (__mach_task_self (), (mach_port_t
) fd
);
366 __ssize_t weak_function
367 __libc_read (int fd
, void *buf
, size_t nbytes
)
371 mach_msg_type_number_t nread
;
375 err
= __io_read ((mach_port_t
) fd
, &data
, &nread
, -1, nbytes
);
377 return __hurd_fail (err
);
381 memcpy (buf
, data
, nread
);
382 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, nread
);
387 libc_hidden_weak (__libc_read
)
389 __ssize_t weak_function
390 __libc_write (int fd
, const void *buf
, size_t nbytes
)
393 mach_msg_type_number_t nwrote
;
395 assert (fd
< _hurd_init_dtablesize
);
397 err
= __io_write (_hurd_init_dtable
[fd
], buf
, nbytes
, -1, &nwrote
);
399 return __hurd_fail (err
);
403 libc_hidden_weak (__libc_write
)
405 /* This is only used for printing messages (see dl-misc.c). */
406 __ssize_t weak_function
407 __writev (int fd
, const struct iovec
*iov
, int niov
)
409 if (fd
>= _hurd_init_dtablesize
)
417 for (i
= 0; i
< niov
; ++i
)
418 total
+= iov
[i
].iov_len
;
422 char buf
[total
], *bufp
= buf
;
424 mach_msg_type_number_t nwrote
;
426 for (i
= 0; i
< niov
; ++i
)
427 bufp
= (memcpy (bufp
, iov
[i
].iov_base
, iov
[i
].iov_len
)
430 err
= __io_write (_hurd_init_dtable
[fd
], buf
, total
, -1, &nwrote
);
432 return __hurd_fail (err
);
440 off64_t weak_function
441 __libc_lseek64 (int fd
, off64_t offset
, int whence
)
445 err
= __io_seek ((mach_port_t
) fd
, offset
, whence
, &offset
);
447 return __hurd_fail (err
);
452 __ptr_t weak_function
453 __mmap (__ptr_t addr
, size_t len
, int prot
, int flags
, int fd
, off_t offset
)
457 vm_address_t mapaddr
;
458 mach_port_t memobj_rd
, memobj_wr
;
460 vmprot
= VM_PROT_NONE
;
461 if (prot
& PROT_READ
)
462 vmprot
|= VM_PROT_READ
;
463 if (prot
& PROT_WRITE
)
464 vmprot
|= VM_PROT_WRITE
;
465 if (prot
& PROT_EXEC
)
466 vmprot
|= VM_PROT_EXECUTE
;
468 if (flags
& MAP_ANON
)
469 memobj_rd
= MACH_PORT_NULL
;
472 assert (!(flags
& MAP_SHARED
));
473 err
= __io_map ((mach_port_t
) fd
, &memobj_rd
, &memobj_wr
);
475 return __hurd_fail (err
), MAP_FAILED
;
476 __mach_port_deallocate (__mach_task_self (), memobj_wr
);
479 mapaddr
= (vm_address_t
) addr
;
480 err
= __vm_map (__mach_task_self (),
481 &mapaddr
, (vm_size_t
) len
, ELF_MACHINE_USER_ADDRESS_MASK
,
482 !(flags
& MAP_FIXED
),
484 (vm_offset_t
) offset
,
485 flags
& (MAP_COPY
|MAP_PRIVATE
),
487 (flags
& MAP_SHARED
) ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
488 if (err
== KERN_NO_SPACE
&& (flags
& MAP_FIXED
))
490 /* XXX this is not atomic as it is in unix! */
491 /* The region is already allocated; deallocate it first. */
492 err
= __vm_deallocate (__mach_task_self (), mapaddr
, len
);
494 err
= __vm_map (__mach_task_self (),
495 &mapaddr
, (vm_size_t
) len
,
496 ELF_MACHINE_USER_ADDRESS_MASK
,
497 !(flags
& MAP_FIXED
),
498 memobj_rd
, (vm_offset_t
) offset
,
499 flags
& (MAP_COPY
|MAP_PRIVATE
),
502 ? VM_INHERIT_SHARE
: VM_INHERIT_COPY
);
505 if ((flags
& MAP_ANON
) == 0)
506 __mach_port_deallocate (__mach_task_self (), memobj_rd
);
509 return __hurd_fail (err
), MAP_FAILED
;
510 return (__ptr_t
) mapaddr
;
514 __fxstat64 (int vers
, int fd
, struct stat64
*buf
)
518 assert (vers
== _STAT_VER
);
520 err
= __io_stat ((mach_port_t
) fd
, buf
);
522 return __hurd_fail (err
);
526 libc_hidden_def (__fxstat64
)
529 __xstat64 (int vers
, const char *file
, struct stat64
*buf
)
534 assert (vers
== _STAT_VER
);
536 err
= open_file (file
, 0, &port
, buf
);
538 return __hurd_fail (err
);
540 __mach_port_deallocate (__mach_task_self (), port
);
544 libc_hidden_def (__xstat64
)
546 /* This function is called by the dynamic linker (rtld.c) to check
547 whether debugging malloc is allowed even for SUID binaries. This
548 stub will always fail, which means that malloc-debugging is always
549 disabled for SUID binaries. */
551 __access (const char *file
, int type
)
563 if (__proc_getpids (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
564 &pid
, &ppid
, &orphaned
))
570 /* This is called only in some strange cases trying to guess a value
571 for $ORIGIN for the executable. The dynamic linker copes with
572 getcwd failing (dl-object.c), and it's too much hassle to include
573 the functionality here. (We could, it just requires duplicating or
574 reusing getcwd.c's code but using our special lookup function as in
578 __getcwd (char *buf
, size_t size
)
584 void weak_function attribute_hidden
587 __proc_mark_exit (_dl_hurd_data
->portarray
[INIT_PORT_PROC
],
588 W_EXITCODE (status
, 0), 0);
589 while (__task_terminate (__mach_task_self ()))
590 __mach_task_self_
= (__mach_task_self
) ();
592 /* We need this alias to satisfy references from libc_pic.a objects
593 that were affected by the libc_hidden_proto declaration for _exit. */
594 strong_alias (_exit
, __GI__exit
)
596 /* Try to get a machine dependent instruction which will make the
597 program crash. This is used in case everything else fails. */
598 #include <abort-instr.h>
599 #ifndef ABORT_INSTRUCTION
600 /* No such instruction is available. */
601 # define ABORT_INSTRUCTION
607 /* Try to abort using the system specific command. */
610 /* If the abort instruction failed, exit. */
613 /* If even this fails, make sure we never return. */
615 /* Try for ever and ever. */
619 /* We need this alias to satisfy references from libc_pic.a objects
620 that were affected by the libc_hidden_proto declaration for abort. */
621 strong_alias (abort
, __GI_abort
)
623 /* This function is called by interruptible RPC stubs. For initial
624 dynamic linking, just use the normal mach_msg. Since this defn is
625 weak, the real defn in libc.so will override it if we are linked into
626 the user program (-ldl). */
628 error_t weak_function
629 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
630 mach_msg_option_t option
,
631 mach_msg_size_t send_size
,
632 mach_msg_size_t rcv_size
,
633 mach_port_t rcv_name
,
634 mach_msg_timeout_t timeout
,
637 return __mach_msg (msg
, option
, send_size
, rcv_size
, rcv_name
,
646 /* There is nothing to print. Hurd has no auxiliary vector. */
651 _dl_init_first (int argc
, ...)
653 /* This no-op definition only gets used if libc is not linked in. */